d811aa987b342c1f2e30406c8bde6c91f07731a7
[binutils-gdb.git] / gas / config / tc-ia64.c
1 /* tc-ia64.c -- Assembler for the HP/Intel IA-64 architecture.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /*
23 TODO:
24
25 - optional operands
26 - directives:
27 .alias
28 .eb
29 .estate
30 .lb
31 .popsection
32 .previous
33 .psr
34 .pushsection
35 - labels are wrong if automatic alignment is introduced
36 (e.g., checkout the second real10 definition in test-data.s)
37 - DV-related stuff:
38 <reg>.safe_across_calls and any other DV-related directives I don't
39 have documentation for.
40 verify mod-sched-brs reads/writes are checked/marked (and other
41 notes)
42
43 */
44
45 #include "as.h"
46 #include "dwarf2dbg.h"
47 #include "subsegs.h"
48
49 #include "opcode/ia64.h"
50
51 #include "elf/ia64.h"
52
53 #define NELEMS(a) ((int) (sizeof (a)/sizeof ((a)[0])))
54 #define MIN(a,b) ((a) < (b) ? (a) : (b))
55
56 #define NUM_SLOTS 4
57 #define PREV_SLOT md.slot[(md.curr_slot + NUM_SLOTS - 1) % NUM_SLOTS]
58 #define CURR_SLOT md.slot[md.curr_slot]
59
60 #define O_pseudo_fixup (O_max + 1)
61
62 enum special_section
63 {
64 /* IA-64 ABI section pseudo-ops. */
65 SPECIAL_SECTION_BSS = 0,
66 SPECIAL_SECTION_SBSS,
67 SPECIAL_SECTION_SDATA,
68 SPECIAL_SECTION_RODATA,
69 SPECIAL_SECTION_COMMENT,
70 SPECIAL_SECTION_UNWIND,
71 SPECIAL_SECTION_UNWIND_INFO,
72 /* HPUX specific section pseudo-ops. */
73 SPECIAL_SECTION_INIT_ARRAY,
74 SPECIAL_SECTION_FINI_ARRAY,
75 };
76
77 enum reloc_func
78 {
79 FUNC_FPTR_RELATIVE,
80 FUNC_GP_RELATIVE,
81 FUNC_LT_RELATIVE,
82 FUNC_PC_RELATIVE,
83 FUNC_PLT_RELATIVE,
84 FUNC_SEC_RELATIVE,
85 FUNC_SEG_RELATIVE,
86 FUNC_LTV_RELATIVE,
87 FUNC_LT_FPTR_RELATIVE,
88 FUNC_IPLT_RELOC,
89 };
90
91 enum reg_symbol
92 {
93 REG_GR = 0,
94 REG_FR = (REG_GR + 128),
95 REG_AR = (REG_FR + 128),
96 REG_CR = (REG_AR + 128),
97 REG_P = (REG_CR + 128),
98 REG_BR = (REG_P + 64),
99 REG_IP = (REG_BR + 8),
100 REG_CFM,
101 REG_PR,
102 REG_PR_ROT,
103 REG_PSR,
104 REG_PSR_L,
105 REG_PSR_UM,
106 /* The following are pseudo-registers for use by gas only. */
107 IND_CPUID,
108 IND_DBR,
109 IND_DTR,
110 IND_ITR,
111 IND_IBR,
112 IND_MEM,
113 IND_MSR,
114 IND_PKR,
115 IND_PMC,
116 IND_PMD,
117 IND_RR,
118 /* The following pseudo-registers are used for unwind directives only: */
119 REG_PSP,
120 REG_PRIUNAT,
121 REG_NUM
122 };
123
124 enum dynreg_type
125 {
126 DYNREG_GR = 0, /* dynamic general purpose register */
127 DYNREG_FR, /* dynamic floating point register */
128 DYNREG_PR, /* dynamic predicate register */
129 DYNREG_NUM_TYPES
130 };
131
132 enum operand_match_result
133 {
134 OPERAND_MATCH,
135 OPERAND_OUT_OF_RANGE,
136 OPERAND_MISMATCH
137 };
138
139 /* On the ia64, we can't know the address of a text label until the
140 instructions are packed into a bundle. To handle this, we keep
141 track of the list of labels that appear in front of each
142 instruction. */
143 struct label_fix
144 {
145 struct label_fix *next;
146 struct symbol *sym;
147 };
148
149 extern int target_big_endian;
150
151 /* Characters which always start a comment. */
152 const char comment_chars[] = "";
153
154 /* Characters which start a comment at the beginning of a line. */
155 const char line_comment_chars[] = "#";
156
157 /* Characters which may be used to separate multiple commands on a
158 single line. */
159 const char line_separator_chars[] = ";";
160
161 /* Characters which are used to indicate an exponent in a floating
162 point number. */
163 const char EXP_CHARS[] = "eE";
164
165 /* Characters which mean that a number is a floating point constant,
166 as in 0d1.0. */
167 const char FLT_CHARS[] = "rRsSfFdDxXpP";
168
169 /* ia64-specific option processing: */
170
171 const char *md_shortopts = "m:N:x::";
172
173 struct option md_longopts[] =
174 {
175 #define OPTION_MCONSTANT_GP (OPTION_MD_BASE + 1)
176 {"mconstant-gp", no_argument, NULL, OPTION_MCONSTANT_GP},
177 #define OPTION_MAUTO_PIC (OPTION_MD_BASE + 2)
178 {"mauto-pic", no_argument, NULL, OPTION_MAUTO_PIC}
179 };
180
181 size_t md_longopts_size = sizeof (md_longopts);
182
183 static struct
184 {
185 struct hash_control *pseudo_hash; /* pseudo opcode hash table */
186 struct hash_control *reg_hash; /* register name hash table */
187 struct hash_control *dynreg_hash; /* dynamic register hash table */
188 struct hash_control *const_hash; /* constant hash table */
189 struct hash_control *entry_hash; /* code entry hint hash table */
190
191 symbolS *regsym[REG_NUM];
192
193 /* If X_op is != O_absent, the registername for the instruction's
194 qualifying predicate. If NULL, p0 is assumed for instructions
195 that are predicatable. */
196 expressionS qp;
197
198 unsigned int
199 manual_bundling : 1,
200 debug_dv: 1,
201 detect_dv: 1,
202 explicit_mode : 1, /* which mode we're in */
203 default_explicit_mode : 1, /* which mode is the default */
204 mode_explicitly_set : 1, /* was the current mode explicitly set? */
205 auto_align : 1,
206 keep_pending_output : 1;
207
208 /* Each bundle consists of up to three instructions. We keep
209 track of four most recent instructions so we can correctly set
210 the end_of_insn_group for the last instruction in a bundle. */
211 int curr_slot;
212 int num_slots_in_use;
213 struct slot
214 {
215 unsigned int
216 end_of_insn_group : 1,
217 manual_bundling_on : 1,
218 manual_bundling_off : 1;
219 signed char user_template; /* user-selected template, if any */
220 unsigned char qp_regno; /* qualifying predicate */
221 /* This duplicates a good fraction of "struct fix" but we
222 can't use a "struct fix" instead since we can't call
223 fix_new_exp() until we know the address of the instruction. */
224 int num_fixups;
225 struct insn_fix
226 {
227 bfd_reloc_code_real_type code;
228 enum ia64_opnd opnd; /* type of operand in need of fix */
229 unsigned int is_pcrel : 1; /* is operand pc-relative? */
230 expressionS expr; /* the value to be inserted */
231 }
232 fixup[2]; /* at most two fixups per insn */
233 struct ia64_opcode *idesc;
234 struct label_fix *label_fixups;
235 struct label_fix *tag_fixups;
236 struct unw_rec_list *unwind_record; /* Unwind directive. */
237 expressionS opnd[6];
238 char *src_file;
239 unsigned int src_line;
240 struct dwarf2_line_info debug_line;
241 }
242 slot[NUM_SLOTS];
243
244 segT last_text_seg;
245
246 struct dynreg
247 {
248 struct dynreg *next; /* next dynamic register */
249 const char *name;
250 unsigned short base; /* the base register number */
251 unsigned short num_regs; /* # of registers in this set */
252 }
253 *dynreg[DYNREG_NUM_TYPES], in, loc, out, rot;
254
255 flagword flags; /* ELF-header flags */
256
257 struct mem_offset {
258 unsigned hint:1; /* is this hint currently valid? */
259 bfd_vma offset; /* mem.offset offset */
260 bfd_vma base; /* mem.offset base */
261 } mem_offset;
262
263 int path; /* number of alt. entry points seen */
264 const char **entry_labels; /* labels of all alternate paths in
265 the current DV-checking block. */
266 int maxpaths; /* size currently allocated for
267 entry_labels */
268 /* Support for hardware errata workarounds. */
269
270 /* Record data about the last three insn groups. */
271 struct group
272 {
273 /* B-step workaround.
274 For each predicate register, this is set if the corresponding insn
275 group conditionally sets this register with one of the affected
276 instructions. */
277 int p_reg_set[64];
278 /* B-step workaround.
279 For each general register, this is set if the corresponding insn
280 a) is conditional one one of the predicate registers for which
281 P_REG_SET is 1 in the corresponding entry of the previous group,
282 b) sets this general register with one of the affected
283 instructions. */
284 int g_reg_set_conditionally[128];
285 } last_groups[3];
286 int group_idx;
287
288 int pointer_size; /* size in bytes of a pointer */
289 int pointer_size_shift; /* shift size of a pointer for alignment */
290 }
291 md;
292
293 /* application registers: */
294
295 #define AR_K0 0
296 #define AR_K7 7
297 #define AR_RSC 16
298 #define AR_BSP 17
299 #define AR_BSPSTORE 18
300 #define AR_RNAT 19
301 #define AR_UNAT 36
302 #define AR_FPSR 40
303 #define AR_ITC 44
304 #define AR_PFS 64
305 #define AR_LC 65
306
307 static const struct
308 {
309 const char *name;
310 int regnum;
311 }
312 ar[] =
313 {
314 {"ar.k0", 0}, {"ar.k1", 1}, {"ar.k2", 2}, {"ar.k3", 3},
315 {"ar.k4", 4}, {"ar.k5", 5}, {"ar.k6", 6}, {"ar.k7", 7},
316 {"ar.rsc", 16}, {"ar.bsp", 17},
317 {"ar.bspstore", 18}, {"ar.rnat", 19},
318 {"ar.fcr", 21}, {"ar.eflag", 24},
319 {"ar.csd", 25}, {"ar.ssd", 26},
320 {"ar.cflg", 27}, {"ar.fsr", 28},
321 {"ar.fir", 29}, {"ar.fdr", 30},
322 {"ar.ccv", 32}, {"ar.unat", 36},
323 {"ar.fpsr", 40}, {"ar.itc", 44},
324 {"ar.pfs", 64}, {"ar.lc", 65},
325 {"ar.ec", 66},
326 };
327
328 #define CR_IPSR 16
329 #define CR_ISR 17
330 #define CR_IIP 19
331 #define CR_IFA 20
332 #define CR_ITIR 21
333 #define CR_IIPA 22
334 #define CR_IFS 23
335 #define CR_IIM 24
336 #define CR_IHA 25
337 #define CR_IVR 65
338 #define CR_TPR 66
339 #define CR_EOI 67
340 #define CR_IRR0 68
341 #define CR_IRR3 71
342 #define CR_LRR0 80
343 #define CR_LRR1 81
344
345 /* control registers: */
346 static const struct
347 {
348 const char *name;
349 int regnum;
350 }
351 cr[] =
352 {
353 {"cr.dcr", 0},
354 {"cr.itm", 1},
355 {"cr.iva", 2},
356 {"cr.pta", 8},
357 {"cr.gpta", 9},
358 {"cr.ipsr", 16},
359 {"cr.isr", 17},
360 {"cr.iip", 19},
361 {"cr.ifa", 20},
362 {"cr.itir", 21},
363 {"cr.iipa", 22},
364 {"cr.ifs", 23},
365 {"cr.iim", 24},
366 {"cr.iha", 25},
367 {"cr.lid", 64},
368 {"cr.ivr", 65},
369 {"cr.tpr", 66},
370 {"cr.eoi", 67},
371 {"cr.irr0", 68},
372 {"cr.irr1", 69},
373 {"cr.irr2", 70},
374 {"cr.irr3", 71},
375 {"cr.itv", 72},
376 {"cr.pmv", 73},
377 {"cr.cmcv", 74},
378 {"cr.lrr0", 80},
379 {"cr.lrr1", 81}
380 };
381
382 #define PSR_MFL 4
383 #define PSR_IC 13
384 #define PSR_DFL 18
385 #define PSR_CPL 32
386
387 static const struct const_desc
388 {
389 const char *name;
390 valueT value;
391 }
392 const_bits[] =
393 {
394 /* PSR constant masks: */
395
396 /* 0: reserved */
397 {"psr.be", ((valueT) 1) << 1},
398 {"psr.up", ((valueT) 1) << 2},
399 {"psr.ac", ((valueT) 1) << 3},
400 {"psr.mfl", ((valueT) 1) << 4},
401 {"psr.mfh", ((valueT) 1) << 5},
402 /* 6-12: reserved */
403 {"psr.ic", ((valueT) 1) << 13},
404 {"psr.i", ((valueT) 1) << 14},
405 {"psr.pk", ((valueT) 1) << 15},
406 /* 16: reserved */
407 {"psr.dt", ((valueT) 1) << 17},
408 {"psr.dfl", ((valueT) 1) << 18},
409 {"psr.dfh", ((valueT) 1) << 19},
410 {"psr.sp", ((valueT) 1) << 20},
411 {"psr.pp", ((valueT) 1) << 21},
412 {"psr.di", ((valueT) 1) << 22},
413 {"psr.si", ((valueT) 1) << 23},
414 {"psr.db", ((valueT) 1) << 24},
415 {"psr.lp", ((valueT) 1) << 25},
416 {"psr.tb", ((valueT) 1) << 26},
417 {"psr.rt", ((valueT) 1) << 27},
418 /* 28-31: reserved */
419 /* 32-33: cpl (current privilege level) */
420 {"psr.is", ((valueT) 1) << 34},
421 {"psr.mc", ((valueT) 1) << 35},
422 {"psr.it", ((valueT) 1) << 36},
423 {"psr.id", ((valueT) 1) << 37},
424 {"psr.da", ((valueT) 1) << 38},
425 {"psr.dd", ((valueT) 1) << 39},
426 {"psr.ss", ((valueT) 1) << 40},
427 /* 41-42: ri (restart instruction) */
428 {"psr.ed", ((valueT) 1) << 43},
429 {"psr.bn", ((valueT) 1) << 44},
430 };
431
432 /* indirect register-sets/memory: */
433
434 static const struct
435 {
436 const char *name;
437 int regnum;
438 }
439 indirect_reg[] =
440 {
441 { "CPUID", IND_CPUID },
442 { "cpuid", IND_CPUID },
443 { "dbr", IND_DBR },
444 { "dtr", IND_DTR },
445 { "itr", IND_ITR },
446 { "ibr", IND_IBR },
447 { "msr", IND_MSR },
448 { "pkr", IND_PKR },
449 { "pmc", IND_PMC },
450 { "pmd", IND_PMD },
451 { "rr", IND_RR },
452 };
453
454 /* Pseudo functions used to indicate relocation types (these functions
455 start with an at sign (@). */
456 static struct
457 {
458 const char *name;
459 enum pseudo_type
460 {
461 PSEUDO_FUNC_NONE,
462 PSEUDO_FUNC_RELOC,
463 PSEUDO_FUNC_CONST,
464 PSEUDO_FUNC_REG,
465 PSEUDO_FUNC_FLOAT
466 }
467 type;
468 union
469 {
470 unsigned long ival;
471 symbolS *sym;
472 }
473 u;
474 }
475 pseudo_func[] =
476 {
477 /* reloc pseudo functions (these must come first!): */
478 { "fptr", PSEUDO_FUNC_RELOC, { 0 } },
479 { "gprel", PSEUDO_FUNC_RELOC, { 0 } },
480 { "ltoff", PSEUDO_FUNC_RELOC, { 0 } },
481 { "pcrel", PSEUDO_FUNC_RELOC, { 0 } },
482 { "pltoff", PSEUDO_FUNC_RELOC, { 0 } },
483 { "secrel", PSEUDO_FUNC_RELOC, { 0 } },
484 { "segrel", PSEUDO_FUNC_RELOC, { 0 } },
485 { "ltv", PSEUDO_FUNC_RELOC, { 0 } },
486 { "", 0, { 0 } }, /* placeholder for FUNC_LT_FPTR_RELATIVE */
487 { "iplt", PSEUDO_FUNC_RELOC, { 0 } },
488
489 /* mbtype4 constants: */
490 { "alt", PSEUDO_FUNC_CONST, { 0xa } },
491 { "brcst", PSEUDO_FUNC_CONST, { 0x0 } },
492 { "mix", PSEUDO_FUNC_CONST, { 0x8 } },
493 { "rev", PSEUDO_FUNC_CONST, { 0xb } },
494 { "shuf", PSEUDO_FUNC_CONST, { 0x9 } },
495
496 /* fclass constants: */
497 { "nat", PSEUDO_FUNC_CONST, { 0x100 } },
498 { "qnan", PSEUDO_FUNC_CONST, { 0x080 } },
499 { "snan", PSEUDO_FUNC_CONST, { 0x040 } },
500 { "pos", PSEUDO_FUNC_CONST, { 0x001 } },
501 { "neg", PSEUDO_FUNC_CONST, { 0x002 } },
502 { "zero", PSEUDO_FUNC_CONST, { 0x004 } },
503 { "unorm", PSEUDO_FUNC_CONST, { 0x008 } },
504 { "norm", PSEUDO_FUNC_CONST, { 0x010 } },
505 { "inf", PSEUDO_FUNC_CONST, { 0x020 } },
506
507 { "natval", PSEUDO_FUNC_CONST, { 0x100 } }, /* old usage */
508
509 /* unwind-related constants: */
510 { "svr4", PSEUDO_FUNC_CONST, { 0 } },
511 { "hpux", PSEUDO_FUNC_CONST, { 1 } },
512 { "nt", PSEUDO_FUNC_CONST, { 2 } },
513
514 /* unwind-related registers: */
515 { "priunat",PSEUDO_FUNC_REG, { REG_PRIUNAT } }
516 };
517
518 /* 41-bit nop opcodes (one per unit): */
519 static const bfd_vma nop[IA64_NUM_UNITS] =
520 {
521 0x0000000000LL, /* NIL => break 0 */
522 0x0008000000LL, /* I-unit nop */
523 0x0008000000LL, /* M-unit nop */
524 0x4000000000LL, /* B-unit nop */
525 0x0008000000LL, /* F-unit nop */
526 0x0008000000LL, /* L-"unit" nop */
527 0x0008000000LL, /* X-unit nop */
528 };
529
530 /* Can't be `const' as it's passed to input routines (which have the
531 habit of setting temporary sentinels. */
532 static char special_section_name[][20] =
533 {
534 {".bss"}, {".sbss"}, {".sdata"}, {".rodata"}, {".comment"},
535 {".IA_64.unwind"}, {".IA_64.unwind_info"},
536 {".init_array"}, {".fini_array"}
537 };
538
539 static char *special_linkonce_name[] =
540 {
541 ".gnu.linkonce.ia64unw.", ".gnu.linkonce.ia64unwi."
542 };
543
544 /* The best template for a particular sequence of up to three
545 instructions: */
546 #define N IA64_NUM_TYPES
547 static unsigned char best_template[N][N][N];
548 #undef N
549
550 /* Resource dependencies currently in effect */
551 static struct rsrc {
552 int depind; /* dependency index */
553 const struct ia64_dependency *dependency; /* actual dependency */
554 unsigned specific:1, /* is this a specific bit/regno? */
555 link_to_qp_branch:1; /* will a branch on the same QP clear it?*/
556 int index; /* specific regno/bit within dependency */
557 int note; /* optional qualifying note (0 if none) */
558 #define STATE_NONE 0
559 #define STATE_STOP 1
560 #define STATE_SRLZ 2
561 int insn_srlz; /* current insn serialization state */
562 int data_srlz; /* current data serialization state */
563 int qp_regno; /* qualifying predicate for this usage */
564 char *file; /* what file marked this dependency */
565 unsigned int line; /* what line marked this dependency */
566 struct mem_offset mem_offset; /* optional memory offset hint */
567 enum { CMP_NONE, CMP_OR, CMP_AND } cmp_type; /* OR or AND compare? */
568 int path; /* corresponding code entry index */
569 } *regdeps = NULL;
570 static int regdepslen = 0;
571 static int regdepstotlen = 0;
572 static const char *dv_mode[] = { "RAW", "WAW", "WAR" };
573 static const char *dv_sem[] = { "none", "implied", "impliedf",
574 "data", "instr", "specific", "stop", "other" };
575 static const char *dv_cmp_type[] = { "none", "OR", "AND" };
576
577 /* Current state of PR mutexation */
578 static struct qpmutex {
579 valueT prmask;
580 int path;
581 } *qp_mutexes = NULL; /* QP mutex bitmasks */
582 static int qp_mutexeslen = 0;
583 static int qp_mutexestotlen = 0;
584 static valueT qp_safe_across_calls = 0;
585
586 /* Current state of PR implications */
587 static struct qp_imply {
588 unsigned p1:6;
589 unsigned p2:6;
590 unsigned p2_branched:1;
591 int path;
592 } *qp_implies = NULL;
593 static int qp_implieslen = 0;
594 static int qp_impliestotlen = 0;
595
596 /* Keep track of static GR values so that indirect register usage can
597 sometimes be tracked. */
598 static struct gr {
599 unsigned known:1;
600 int path;
601 valueT value;
602 } gr_values[128] = {{ 1, 0, 0 }};
603
604 /* These are the routines required to output the various types of
605 unwind records. */
606
607 /* A slot_number is a frag address plus the slot index (0-2). We use the
608 frag address here so that if there is a section switch in the middle of
609 a function, then instructions emitted to a different section are not
610 counted. Since there may be more than one frag for a function, this
611 means we also need to keep track of which frag this address belongs to
612 so we can compute inter-frag distances. This also nicely solves the
613 problem with nops emitted for align directives, which can't easily be
614 counted, but can easily be derived from frag sizes. */
615
616 typedef struct unw_rec_list {
617 unwind_record r;
618 unsigned long slot_number;
619 fragS *slot_frag;
620 struct unw_rec_list *next;
621 } unw_rec_list;
622
623 #define SLOT_NUM_NOT_SET (unsigned)-1
624
625 static struct
626 {
627 unsigned long next_slot_number;
628 fragS *next_slot_frag;
629
630 /* Maintain a list of unwind entries for the current function. */
631 unw_rec_list *list;
632 unw_rec_list *tail;
633
634 /* Any unwind entires that should be attached to the current slot
635 that an insn is being constructed for. */
636 unw_rec_list *current_entry;
637
638 /* These are used to create the unwind table entry for this function. */
639 symbolS *proc_start;
640 symbolS *proc_end;
641 symbolS *info; /* pointer to unwind info */
642 symbolS *personality_routine;
643 segT saved_text_seg;
644 subsegT saved_text_subseg;
645 unsigned int force_unwind_entry : 1; /* force generation of unwind entry? */
646
647 /* TRUE if processing unwind directives in a prologue region. */
648 int prologue;
649 int prologue_mask;
650 unsigned int prologue_count; /* number of .prologues seen so far */
651 } unwind;
652
653 typedef void (*vbyte_func) PARAMS ((int, char *, char *));
654
655 /* Forward delarations: */
656 static int ar_is_in_integer_unit PARAMS ((int regnum));
657 static void set_section PARAMS ((char *name));
658 static unsigned int set_regstack PARAMS ((unsigned int, unsigned int,
659 unsigned int, unsigned int));
660 static void dot_radix PARAMS ((int));
661 static void dot_special_section PARAMS ((int));
662 static void dot_proc PARAMS ((int));
663 static void dot_fframe PARAMS ((int));
664 static void dot_vframe PARAMS ((int));
665 static void dot_vframesp PARAMS ((int));
666 static void dot_vframepsp PARAMS ((int));
667 static void dot_save PARAMS ((int));
668 static void dot_restore PARAMS ((int));
669 static void dot_restorereg PARAMS ((int));
670 static void dot_restorereg_p PARAMS ((int));
671 static void dot_handlerdata PARAMS ((int));
672 static void dot_unwentry PARAMS ((int));
673 static void dot_altrp PARAMS ((int));
674 static void dot_savemem PARAMS ((int));
675 static void dot_saveg PARAMS ((int));
676 static void dot_savef PARAMS ((int));
677 static void dot_saveb PARAMS ((int));
678 static void dot_savegf PARAMS ((int));
679 static void dot_spill PARAMS ((int));
680 static void dot_spillreg PARAMS ((int));
681 static void dot_spillmem PARAMS ((int));
682 static void dot_spillreg_p PARAMS ((int));
683 static void dot_spillmem_p PARAMS ((int));
684 static void dot_label_state PARAMS ((int));
685 static void dot_copy_state PARAMS ((int));
686 static void dot_unwabi PARAMS ((int));
687 static void dot_personality PARAMS ((int));
688 static void dot_body PARAMS ((int));
689 static void dot_prologue PARAMS ((int));
690 static void dot_endp PARAMS ((int));
691 static void dot_template PARAMS ((int));
692 static void dot_regstk PARAMS ((int));
693 static void dot_rot PARAMS ((int));
694 static void dot_byteorder PARAMS ((int));
695 static void dot_psr PARAMS ((int));
696 static void dot_alias PARAMS ((int));
697 static void dot_ln PARAMS ((int));
698 static char *parse_section_name PARAMS ((void));
699 static void dot_xdata PARAMS ((int));
700 static void stmt_float_cons PARAMS ((int));
701 static void stmt_cons_ua PARAMS ((int));
702 static void dot_xfloat_cons PARAMS ((int));
703 static void dot_xstringer PARAMS ((int));
704 static void dot_xdata_ua PARAMS ((int));
705 static void dot_xfloat_cons_ua PARAMS ((int));
706 static void print_prmask PARAMS ((valueT mask));
707 static void dot_pred_rel PARAMS ((int));
708 static void dot_reg_val PARAMS ((int));
709 static void dot_dv_mode PARAMS ((int));
710 static void dot_entry PARAMS ((int));
711 static void dot_mem_offset PARAMS ((int));
712 static void add_unwind_entry PARAMS((unw_rec_list *ptr));
713 static symbolS *declare_register PARAMS ((const char *name, int regnum));
714 static void declare_register_set PARAMS ((const char *, int, int));
715 static unsigned int operand_width PARAMS ((enum ia64_opnd));
716 static enum operand_match_result operand_match PARAMS ((const struct ia64_opcode *idesc,
717 int index,
718 expressionS *e));
719 static int parse_operand PARAMS ((expressionS *e));
720 static struct ia64_opcode * parse_operands PARAMS ((struct ia64_opcode *));
721 static void build_insn PARAMS ((struct slot *, bfd_vma *));
722 static void emit_one_bundle PARAMS ((void));
723 static void fix_insn PARAMS ((fixS *, const struct ia64_operand *, valueT));
724 static bfd_reloc_code_real_type ia64_gen_real_reloc_type PARAMS ((struct symbol *sym,
725 bfd_reloc_code_real_type r_type));
726 static void insn_group_break PARAMS ((int, int, int));
727 static void mark_resource PARAMS ((struct ia64_opcode *, const struct ia64_dependency *,
728 struct rsrc *, int depind, int path));
729 static void add_qp_mutex PARAMS((valueT mask));
730 static void add_qp_imply PARAMS((int p1, int p2));
731 static void clear_qp_branch_flag PARAMS((valueT mask));
732 static void clear_qp_mutex PARAMS((valueT mask));
733 static void clear_qp_implies PARAMS((valueT p1_mask, valueT p2_mask));
734 static void clear_register_values PARAMS ((void));
735 static void print_dependency PARAMS ((const char *action, int depind));
736 static void instruction_serialization PARAMS ((void));
737 static void data_serialization PARAMS ((void));
738 static void remove_marked_resource PARAMS ((struct rsrc *));
739 static int is_conditional_branch PARAMS ((struct ia64_opcode *));
740 static int is_taken_branch PARAMS ((struct ia64_opcode *));
741 static int is_interruption_or_rfi PARAMS ((struct ia64_opcode *));
742 static int depends_on PARAMS ((int, struct ia64_opcode *));
743 static int specify_resource PARAMS ((const struct ia64_dependency *,
744 struct ia64_opcode *, int, struct rsrc [], int, int));
745 static int check_dv PARAMS((struct ia64_opcode *idesc));
746 static void check_dependencies PARAMS((struct ia64_opcode *));
747 static void mark_resources PARAMS((struct ia64_opcode *));
748 static void update_dependencies PARAMS((struct ia64_opcode *));
749 static void note_register_values PARAMS((struct ia64_opcode *));
750 static int qp_mutex PARAMS ((int, int, int));
751 static int resources_match PARAMS ((struct rsrc *, struct ia64_opcode *, int, int, int));
752 static void output_vbyte_mem PARAMS ((int, char *, char *));
753 static void count_output PARAMS ((int, char *, char *));
754 static void output_R1_format PARAMS ((vbyte_func, unw_record_type, int));
755 static void output_R2_format PARAMS ((vbyte_func, int, int, unsigned long));
756 static void output_R3_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
757 static void output_P1_format PARAMS ((vbyte_func, int));
758 static void output_P2_format PARAMS ((vbyte_func, int, int));
759 static void output_P3_format PARAMS ((vbyte_func, unw_record_type, int));
760 static void output_P4_format PARAMS ((vbyte_func, unsigned char *, unsigned long));
761 static void output_P5_format PARAMS ((vbyte_func, int, unsigned long));
762 static void output_P6_format PARAMS ((vbyte_func, unw_record_type, int));
763 static void output_P7_format PARAMS ((vbyte_func, unw_record_type, unsigned long, unsigned long));
764 static void output_P8_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
765 static void output_P9_format PARAMS ((vbyte_func, int, int));
766 static void output_P10_format PARAMS ((vbyte_func, int, int));
767 static void output_B1_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
768 static void output_B2_format PARAMS ((vbyte_func, unsigned long, unsigned long));
769 static void output_B3_format PARAMS ((vbyte_func, unsigned long, unsigned long));
770 static void output_B4_format PARAMS ((vbyte_func, unw_record_type, unsigned long));
771 static char format_ab_reg PARAMS ((int, int));
772 static void output_X1_format PARAMS ((vbyte_func, unw_record_type, int, int, unsigned long,
773 unsigned long));
774 static void output_X2_format PARAMS ((vbyte_func, int, int, int, int, int, unsigned long));
775 static void output_X3_format PARAMS ((vbyte_func, unw_record_type, int, int, int, unsigned long,
776 unsigned long));
777 static void output_X4_format PARAMS ((vbyte_func, int, int, int, int, int, int, unsigned long));
778 static void free_list_records PARAMS ((unw_rec_list *));
779 static unw_rec_list *output_prologue PARAMS ((void));
780 static unw_rec_list *output_prologue_gr PARAMS ((unsigned int, unsigned int));
781 static unw_rec_list *output_body PARAMS ((void));
782 static unw_rec_list *output_mem_stack_f PARAMS ((unsigned int));
783 static unw_rec_list *output_mem_stack_v PARAMS ((void));
784 static unw_rec_list *output_psp_gr PARAMS ((unsigned int));
785 static unw_rec_list *output_psp_sprel PARAMS ((unsigned int));
786 static unw_rec_list *output_rp_when PARAMS ((void));
787 static unw_rec_list *output_rp_gr PARAMS ((unsigned int));
788 static unw_rec_list *output_rp_br PARAMS ((unsigned int));
789 static unw_rec_list *output_rp_psprel PARAMS ((unsigned int));
790 static unw_rec_list *output_rp_sprel PARAMS ((unsigned int));
791 static unw_rec_list *output_pfs_when PARAMS ((void));
792 static unw_rec_list *output_pfs_gr PARAMS ((unsigned int));
793 static unw_rec_list *output_pfs_psprel PARAMS ((unsigned int));
794 static unw_rec_list *output_pfs_sprel PARAMS ((unsigned int));
795 static unw_rec_list *output_preds_when PARAMS ((void));
796 static unw_rec_list *output_preds_gr PARAMS ((unsigned int));
797 static unw_rec_list *output_preds_psprel PARAMS ((unsigned int));
798 static unw_rec_list *output_preds_sprel PARAMS ((unsigned int));
799 static unw_rec_list *output_fr_mem PARAMS ((unsigned int));
800 static unw_rec_list *output_frgr_mem PARAMS ((unsigned int, unsigned int));
801 static unw_rec_list *output_gr_gr PARAMS ((unsigned int, unsigned int));
802 static unw_rec_list *output_gr_mem PARAMS ((unsigned int));
803 static unw_rec_list *output_br_mem PARAMS ((unsigned int));
804 static unw_rec_list *output_br_gr PARAMS ((unsigned int, unsigned int));
805 static unw_rec_list *output_spill_base PARAMS ((unsigned int));
806 static unw_rec_list *output_unat_when PARAMS ((void));
807 static unw_rec_list *output_unat_gr PARAMS ((unsigned int));
808 static unw_rec_list *output_unat_psprel PARAMS ((unsigned int));
809 static unw_rec_list *output_unat_sprel PARAMS ((unsigned int));
810 static unw_rec_list *output_lc_when PARAMS ((void));
811 static unw_rec_list *output_lc_gr PARAMS ((unsigned int));
812 static unw_rec_list *output_lc_psprel PARAMS ((unsigned int));
813 static unw_rec_list *output_lc_sprel PARAMS ((unsigned int));
814 static unw_rec_list *output_fpsr_when PARAMS ((void));
815 static unw_rec_list *output_fpsr_gr PARAMS ((unsigned int));
816 static unw_rec_list *output_fpsr_psprel PARAMS ((unsigned int));
817 static unw_rec_list *output_fpsr_sprel PARAMS ((unsigned int));
818 static unw_rec_list *output_priunat_when_gr PARAMS ((void));
819 static unw_rec_list *output_priunat_when_mem PARAMS ((void));
820 static unw_rec_list *output_priunat_gr PARAMS ((unsigned int));
821 static unw_rec_list *output_priunat_psprel PARAMS ((unsigned int));
822 static unw_rec_list *output_priunat_sprel PARAMS ((unsigned int));
823 static unw_rec_list *output_bsp_when PARAMS ((void));
824 static unw_rec_list *output_bsp_gr PARAMS ((unsigned int));
825 static unw_rec_list *output_bsp_psprel PARAMS ((unsigned int));
826 static unw_rec_list *output_bsp_sprel PARAMS ((unsigned int));
827 static unw_rec_list *output_bspstore_when PARAMS ((void));
828 static unw_rec_list *output_bspstore_gr PARAMS ((unsigned int));
829 static unw_rec_list *output_bspstore_psprel PARAMS ((unsigned int));
830 static unw_rec_list *output_bspstore_sprel PARAMS ((unsigned int));
831 static unw_rec_list *output_rnat_when PARAMS ((void));
832 static unw_rec_list *output_rnat_gr PARAMS ((unsigned int));
833 static unw_rec_list *output_rnat_psprel PARAMS ((unsigned int));
834 static unw_rec_list *output_rnat_sprel PARAMS ((unsigned int));
835 static unw_rec_list *output_unwabi PARAMS ((unsigned long, unsigned long));
836 static unw_rec_list *output_epilogue PARAMS ((unsigned long));
837 static unw_rec_list *output_label_state PARAMS ((unsigned long));
838 static unw_rec_list *output_copy_state PARAMS ((unsigned long));
839 static unw_rec_list *output_spill_psprel PARAMS ((unsigned int, unsigned int, unsigned int));
840 static unw_rec_list *output_spill_sprel PARAMS ((unsigned int, unsigned int, unsigned int));
841 static unw_rec_list *output_spill_psprel_p PARAMS ((unsigned int, unsigned int, unsigned int,
842 unsigned int));
843 static unw_rec_list *output_spill_sprel_p PARAMS ((unsigned int, unsigned int, unsigned int,
844 unsigned int));
845 static unw_rec_list *output_spill_reg PARAMS ((unsigned int, unsigned int, unsigned int,
846 unsigned int));
847 static unw_rec_list *output_spill_reg_p PARAMS ((unsigned int, unsigned int, unsigned int,
848 unsigned int, unsigned int));
849 static void process_one_record PARAMS ((unw_rec_list *, vbyte_func));
850 static void process_unw_records PARAMS ((unw_rec_list *, vbyte_func));
851 static int calc_record_size PARAMS ((unw_rec_list *));
852 static void set_imask PARAMS ((unw_rec_list *, unsigned long, unsigned long, unsigned int));
853 static int count_bits PARAMS ((unsigned long));
854 static unsigned long slot_index PARAMS ((unsigned long, fragS *,
855 unsigned long, fragS *));
856 static unw_rec_list *optimize_unw_records PARAMS ((unw_rec_list *));
857 static void fixup_unw_records PARAMS ((unw_rec_list *));
858 static int output_unw_records PARAMS ((unw_rec_list *, void **));
859 static int convert_expr_to_ab_reg PARAMS ((expressionS *, unsigned int *, unsigned int *));
860 static int convert_expr_to_xy_reg PARAMS ((expressionS *, unsigned int *, unsigned int *));
861 static int generate_unwind_image PARAMS ((const char *));
862
863 /* Build the unwind section name by appending the (possibly stripped)
864 text section NAME to the unwind PREFIX. The resulting string
865 pointer is assigned to RESULT. The string is allocated on the
866 stack, so this must be a macro... */
867 #define make_unw_section_name(special, text_name, result) \
868 { \
869 char *_prefix = special_section_name[special]; \
870 char *_suffix = text_name; \
871 size_t _prefix_len, _suffix_len; \
872 char *_result; \
873 if (strncmp (text_name, ".gnu.linkonce.t.", \
874 sizeof (".gnu.linkonce.t.") - 1) == 0) \
875 { \
876 _prefix = special_linkonce_name[special - SPECIAL_SECTION_UNWIND]; \
877 _suffix += sizeof (".gnu.linkonce.t.") - 1; \
878 } \
879 _prefix_len = strlen (_prefix), _suffix_len = strlen (_suffix); \
880 _result = alloca (_prefix_len + _suffix_len + 1); \
881 memcpy (_result, _prefix, _prefix_len); \
882 memcpy (_result + _prefix_len, _suffix, _suffix_len); \
883 _result[_prefix_len + _suffix_len] = '\0'; \
884 result = _result; \
885 } \
886 while (0)
887
888 /* Determine if application register REGNUM resides in the integer
889 unit (as opposed to the memory unit). */
890 static int
891 ar_is_in_integer_unit (reg)
892 int reg;
893 {
894 reg -= REG_AR;
895
896 return (reg == 64 /* pfs */
897 || reg == 65 /* lc */
898 || reg == 66 /* ec */
899 /* ??? ias accepts and puts these in the integer unit. */
900 || (reg >= 112 && reg <= 127));
901 }
902
903 /* Switch to section NAME and create section if necessary. It's
904 rather ugly that we have to manipulate input_line_pointer but I
905 don't see any other way to accomplish the same thing without
906 changing obj-elf.c (which may be the Right Thing, in the end). */
907 static void
908 set_section (name)
909 char *name;
910 {
911 char *saved_input_line_pointer;
912
913 saved_input_line_pointer = input_line_pointer;
914 input_line_pointer = name;
915 obj_elf_section (0);
916 input_line_pointer = saved_input_line_pointer;
917 }
918
919 /* Map SHF_IA_64_SHORT to SEC_SMALL_DATA. */
920
921 flagword
922 ia64_elf_section_flags (flags, attr, type)
923 flagword flags;
924 int attr, type ATTRIBUTE_UNUSED;
925 {
926 if (attr & SHF_IA_64_SHORT)
927 flags |= SEC_SMALL_DATA;
928 return flags;
929 }
930
931 int
932 ia64_elf_section_type (str, len)
933 const char *str;
934 size_t len;
935 {
936 len = sizeof (ELF_STRING_ia64_unwind_info) - 1;
937 if (strncmp (str, ELF_STRING_ia64_unwind_info, len) == 0)
938 return SHT_PROGBITS;
939
940 len = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
941 if (strncmp (str, ELF_STRING_ia64_unwind_info_once, len) == 0)
942 return SHT_PROGBITS;
943
944 len = sizeof (ELF_STRING_ia64_unwind) - 1;
945 if (strncmp (str, ELF_STRING_ia64_unwind, len) == 0)
946 return SHT_IA_64_UNWIND;
947
948 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
949 if (strncmp (str, ELF_STRING_ia64_unwind_once, len) == 0)
950 return SHT_IA_64_UNWIND;
951
952 return -1;
953 }
954
955 static unsigned int
956 set_regstack (ins, locs, outs, rots)
957 unsigned int ins, locs, outs, rots;
958 {
959 /* Size of frame. */
960 unsigned int sof;
961
962 sof = ins + locs + outs;
963 if (sof > 96)
964 {
965 as_bad ("Size of frame exceeds maximum of 96 registers");
966 return 0;
967 }
968 if (rots > sof)
969 {
970 as_warn ("Size of rotating registers exceeds frame size");
971 return 0;
972 }
973 md.in.base = REG_GR + 32;
974 md.loc.base = md.in.base + ins;
975 md.out.base = md.loc.base + locs;
976
977 md.in.num_regs = ins;
978 md.loc.num_regs = locs;
979 md.out.num_regs = outs;
980 md.rot.num_regs = rots;
981 return sof;
982 }
983
984 void
985 ia64_flush_insns ()
986 {
987 struct label_fix *lfix;
988 segT saved_seg;
989 subsegT saved_subseg;
990 unw_rec_list *ptr;
991
992 if (!md.last_text_seg)
993 return;
994
995 saved_seg = now_seg;
996 saved_subseg = now_subseg;
997
998 subseg_set (md.last_text_seg, 0);
999
1000 while (md.num_slots_in_use > 0)
1001 emit_one_bundle (); /* force out queued instructions */
1002
1003 /* In case there are labels following the last instruction, resolve
1004 those now: */
1005 for (lfix = CURR_SLOT.label_fixups; lfix; lfix = lfix->next)
1006 {
1007 S_SET_VALUE (lfix->sym, frag_now_fix ());
1008 symbol_set_frag (lfix->sym, frag_now);
1009 }
1010 CURR_SLOT.label_fixups = 0;
1011 for (lfix = CURR_SLOT.tag_fixups; lfix; lfix = lfix->next)
1012 {
1013 S_SET_VALUE (lfix->sym, frag_now_fix ());
1014 symbol_set_frag (lfix->sym, frag_now);
1015 }
1016 CURR_SLOT.tag_fixups = 0;
1017
1018 /* In case there are unwind directives following the last instruction,
1019 resolve those now. We only handle body and prologue directives here.
1020 Give an error for others. */
1021 for (ptr = unwind.current_entry; ptr; ptr = ptr->next)
1022 {
1023 if (ptr->r.type == prologue || ptr->r.type == prologue_gr
1024 || ptr->r.type == body)
1025 {
1026 ptr->slot_number = (unsigned long) frag_more (0);
1027 ptr->slot_frag = frag_now;
1028 }
1029 else
1030 as_bad (_("Unwind directive not followed by an instruction."));
1031 }
1032 unwind.current_entry = NULL;
1033
1034 subseg_set (saved_seg, saved_subseg);
1035
1036 if (md.qp.X_op == O_register)
1037 as_bad ("qualifying predicate not followed by instruction");
1038 }
1039
1040 void
1041 ia64_do_align (nbytes)
1042 int nbytes;
1043 {
1044 char *saved_input_line_pointer = input_line_pointer;
1045
1046 input_line_pointer = "";
1047 s_align_bytes (nbytes);
1048 input_line_pointer = saved_input_line_pointer;
1049 }
1050
1051 void
1052 ia64_cons_align (nbytes)
1053 int nbytes;
1054 {
1055 if (md.auto_align)
1056 {
1057 char *saved_input_line_pointer = input_line_pointer;
1058 input_line_pointer = "";
1059 s_align_bytes (nbytes);
1060 input_line_pointer = saved_input_line_pointer;
1061 }
1062 }
1063
1064 /* Output COUNT bytes to a memory location. */
1065 static unsigned char *vbyte_mem_ptr = NULL;
1066
1067 void
1068 output_vbyte_mem (count, ptr, comment)
1069 int count;
1070 char *ptr;
1071 char *comment ATTRIBUTE_UNUSED;
1072 {
1073 int x;
1074 if (vbyte_mem_ptr == NULL)
1075 abort ();
1076
1077 if (count == 0)
1078 return;
1079 for (x = 0; x < count; x++)
1080 *(vbyte_mem_ptr++) = ptr[x];
1081 }
1082
1083 /* Count the number of bytes required for records. */
1084 static int vbyte_count = 0;
1085 void
1086 count_output (count, ptr, comment)
1087 int count;
1088 char *ptr ATTRIBUTE_UNUSED;
1089 char *comment ATTRIBUTE_UNUSED;
1090 {
1091 vbyte_count += count;
1092 }
1093
1094 static void
1095 output_R1_format (f, rtype, rlen)
1096 vbyte_func f;
1097 unw_record_type rtype;
1098 int rlen;
1099 {
1100 int r = 0;
1101 char byte;
1102 if (rlen > 0x1f)
1103 {
1104 output_R3_format (f, rtype, rlen);
1105 return;
1106 }
1107
1108 if (rtype == body)
1109 r = 1;
1110 else if (rtype != prologue)
1111 as_bad ("record type is not valid");
1112
1113 byte = UNW_R1 | (r << 5) | (rlen & 0x1f);
1114 (*f) (1, &byte, NULL);
1115 }
1116
1117 static void
1118 output_R2_format (f, mask, grsave, rlen)
1119 vbyte_func f;
1120 int mask, grsave;
1121 unsigned long rlen;
1122 {
1123 char bytes[20];
1124 int count = 2;
1125 mask = (mask & 0x0f);
1126 grsave = (grsave & 0x7f);
1127
1128 bytes[0] = (UNW_R2 | (mask >> 1));
1129 bytes[1] = (((mask & 0x01) << 7) | grsave);
1130 count += output_leb128 (bytes + 2, rlen, 0);
1131 (*f) (count, bytes, NULL);
1132 }
1133
1134 static void
1135 output_R3_format (f, rtype, rlen)
1136 vbyte_func f;
1137 unw_record_type rtype;
1138 unsigned long rlen;
1139 {
1140 int r = 0, count;
1141 char bytes[20];
1142 if (rlen <= 0x1f)
1143 {
1144 output_R1_format (f, rtype, rlen);
1145 return;
1146 }
1147
1148 if (rtype == body)
1149 r = 1;
1150 else if (rtype != prologue)
1151 as_bad ("record type is not valid");
1152 bytes[0] = (UNW_R3 | r);
1153 count = output_leb128 (bytes + 1, rlen, 0);
1154 (*f) (count + 1, bytes, NULL);
1155 }
1156
1157 static void
1158 output_P1_format (f, brmask)
1159 vbyte_func f;
1160 int brmask;
1161 {
1162 char byte;
1163 byte = UNW_P1 | (brmask & 0x1f);
1164 (*f) (1, &byte, NULL);
1165 }
1166
1167 static void
1168 output_P2_format (f, brmask, gr)
1169 vbyte_func f;
1170 int brmask;
1171 int gr;
1172 {
1173 char bytes[2];
1174 brmask = (brmask & 0x1f);
1175 bytes[0] = UNW_P2 | (brmask >> 1);
1176 bytes[1] = (((brmask & 1) << 7) | gr);
1177 (*f) (2, bytes, NULL);
1178 }
1179
1180 static void
1181 output_P3_format (f, rtype, reg)
1182 vbyte_func f;
1183 unw_record_type rtype;
1184 int reg;
1185 {
1186 char bytes[2];
1187 int r = 0;
1188 reg = (reg & 0x7f);
1189 switch (rtype)
1190 {
1191 case psp_gr:
1192 r = 0;
1193 break;
1194 case rp_gr:
1195 r = 1;
1196 break;
1197 case pfs_gr:
1198 r = 2;
1199 break;
1200 case preds_gr:
1201 r = 3;
1202 break;
1203 case unat_gr:
1204 r = 4;
1205 break;
1206 case lc_gr:
1207 r = 5;
1208 break;
1209 case rp_br:
1210 r = 6;
1211 break;
1212 case rnat_gr:
1213 r = 7;
1214 break;
1215 case bsp_gr:
1216 r = 8;
1217 break;
1218 case bspstore_gr:
1219 r = 9;
1220 break;
1221 case fpsr_gr:
1222 r = 10;
1223 break;
1224 case priunat_gr:
1225 r = 11;
1226 break;
1227 default:
1228 as_bad ("Invalid record type for P3 format.");
1229 }
1230 bytes[0] = (UNW_P3 | (r >> 1));
1231 bytes[1] = (((r & 1) << 7) | reg);
1232 (*f) (2, bytes, NULL);
1233 }
1234
1235 static void
1236 output_P4_format (f, imask, imask_size)
1237 vbyte_func f;
1238 unsigned char *imask;
1239 unsigned long imask_size;
1240 {
1241 imask[0] = UNW_P4;
1242 (*f) (imask_size, imask, NULL);
1243 }
1244
1245 static void
1246 output_P5_format (f, grmask, frmask)
1247 vbyte_func f;
1248 int grmask;
1249 unsigned long frmask;
1250 {
1251 char bytes[4];
1252 grmask = (grmask & 0x0f);
1253
1254 bytes[0] = UNW_P5;
1255 bytes[1] = ((grmask << 4) | ((frmask & 0x000f0000) >> 16));
1256 bytes[2] = ((frmask & 0x0000ff00) >> 8);
1257 bytes[3] = (frmask & 0x000000ff);
1258 (*f) (4, bytes, NULL);
1259 }
1260
1261 static void
1262 output_P6_format (f, rtype, rmask)
1263 vbyte_func f;
1264 unw_record_type rtype;
1265 int rmask;
1266 {
1267 char byte;
1268 int r = 0;
1269
1270 if (rtype == gr_mem)
1271 r = 1;
1272 else if (rtype != fr_mem)
1273 as_bad ("Invalid record type for format P6");
1274 byte = (UNW_P6 | (r << 4) | (rmask & 0x0f));
1275 (*f) (1, &byte, NULL);
1276 }
1277
1278 static void
1279 output_P7_format (f, rtype, w1, w2)
1280 vbyte_func f;
1281 unw_record_type rtype;
1282 unsigned long w1;
1283 unsigned long w2;
1284 {
1285 char bytes[20];
1286 int count = 1;
1287 int r = 0;
1288 count += output_leb128 (bytes + 1, w1, 0);
1289 switch (rtype)
1290 {
1291 case mem_stack_f:
1292 r = 0;
1293 count += output_leb128 (bytes + count, w2 >> 4, 0);
1294 break;
1295 case mem_stack_v:
1296 r = 1;
1297 break;
1298 case spill_base:
1299 r = 2;
1300 break;
1301 case psp_sprel:
1302 r = 3;
1303 break;
1304 case rp_when:
1305 r = 4;
1306 break;
1307 case rp_psprel:
1308 r = 5;
1309 break;
1310 case pfs_when:
1311 r = 6;
1312 break;
1313 case pfs_psprel:
1314 r = 7;
1315 break;
1316 case preds_when:
1317 r = 8;
1318 break;
1319 case preds_psprel:
1320 r = 9;
1321 break;
1322 case lc_when:
1323 r = 10;
1324 break;
1325 case lc_psprel:
1326 r = 11;
1327 break;
1328 case unat_when:
1329 r = 12;
1330 break;
1331 case unat_psprel:
1332 r = 13;
1333 break;
1334 case fpsr_when:
1335 r = 14;
1336 break;
1337 case fpsr_psprel:
1338 r = 15;
1339 break;
1340 default:
1341 break;
1342 }
1343 bytes[0] = (UNW_P7 | r);
1344 (*f) (count, bytes, NULL);
1345 }
1346
1347 static void
1348 output_P8_format (f, rtype, t)
1349 vbyte_func f;
1350 unw_record_type rtype;
1351 unsigned long t;
1352 {
1353 char bytes[20];
1354 int r = 0;
1355 int count = 2;
1356 bytes[0] = UNW_P8;
1357 switch (rtype)
1358 {
1359 case rp_sprel:
1360 r = 1;
1361 break;
1362 case pfs_sprel:
1363 r = 2;
1364 break;
1365 case preds_sprel:
1366 r = 3;
1367 break;
1368 case lc_sprel:
1369 r = 4;
1370 break;
1371 case unat_sprel:
1372 r = 5;
1373 break;
1374 case fpsr_sprel:
1375 r = 6;
1376 break;
1377 case bsp_when:
1378 r = 7;
1379 break;
1380 case bsp_psprel:
1381 r = 8;
1382 break;
1383 case bsp_sprel:
1384 r = 9;
1385 break;
1386 case bspstore_when:
1387 r = 10;
1388 break;
1389 case bspstore_psprel:
1390 r = 11;
1391 break;
1392 case bspstore_sprel:
1393 r = 12;
1394 break;
1395 case rnat_when:
1396 r = 13;
1397 break;
1398 case rnat_psprel:
1399 r = 14;
1400 break;
1401 case rnat_sprel:
1402 r = 15;
1403 break;
1404 case priunat_when_gr:
1405 r = 16;
1406 break;
1407 case priunat_psprel:
1408 r = 17;
1409 break;
1410 case priunat_sprel:
1411 r = 18;
1412 break;
1413 case priunat_when_mem:
1414 r = 19;
1415 break;
1416 default:
1417 break;
1418 }
1419 bytes[1] = r;
1420 count += output_leb128 (bytes + 2, t, 0);
1421 (*f) (count, bytes, NULL);
1422 }
1423
1424 static void
1425 output_P9_format (f, grmask, gr)
1426 vbyte_func f;
1427 int grmask;
1428 int gr;
1429 {
1430 char bytes[3];
1431 bytes[0] = UNW_P9;
1432 bytes[1] = (grmask & 0x0f);
1433 bytes[2] = (gr & 0x7f);
1434 (*f) (3, bytes, NULL);
1435 }
1436
1437 static void
1438 output_P10_format (f, abi, context)
1439 vbyte_func f;
1440 int abi;
1441 int context;
1442 {
1443 char bytes[3];
1444 bytes[0] = UNW_P10;
1445 bytes[1] = (abi & 0xff);
1446 bytes[2] = (context & 0xff);
1447 (*f) (3, bytes, NULL);
1448 }
1449
1450 static void
1451 output_B1_format (f, rtype, label)
1452 vbyte_func f;
1453 unw_record_type rtype;
1454 unsigned long label;
1455 {
1456 char byte;
1457 int r = 0;
1458 if (label > 0x1f)
1459 {
1460 output_B4_format (f, rtype, label);
1461 return;
1462 }
1463 if (rtype == copy_state)
1464 r = 1;
1465 else if (rtype != label_state)
1466 as_bad ("Invalid record type for format B1");
1467
1468 byte = (UNW_B1 | (r << 5) | (label & 0x1f));
1469 (*f) (1, &byte, NULL);
1470 }
1471
1472 static void
1473 output_B2_format (f, ecount, t)
1474 vbyte_func f;
1475 unsigned long ecount;
1476 unsigned long t;
1477 {
1478 char bytes[20];
1479 int count = 1;
1480 if (ecount > 0x1f)
1481 {
1482 output_B3_format (f, ecount, t);
1483 return;
1484 }
1485 bytes[0] = (UNW_B2 | (ecount & 0x1f));
1486 count += output_leb128 (bytes + 1, t, 0);
1487 (*f) (count, bytes, NULL);
1488 }
1489
1490 static void
1491 output_B3_format (f, ecount, t)
1492 vbyte_func f;
1493 unsigned long ecount;
1494 unsigned long t;
1495 {
1496 char bytes[20];
1497 int count = 1;
1498 if (ecount <= 0x1f)
1499 {
1500 output_B2_format (f, ecount, t);
1501 return;
1502 }
1503 bytes[0] = UNW_B3;
1504 count += output_leb128 (bytes + 1, t, 0);
1505 count += output_leb128 (bytes + count, ecount, 0);
1506 (*f) (count, bytes, NULL);
1507 }
1508
1509 static void
1510 output_B4_format (f, rtype, label)
1511 vbyte_func f;
1512 unw_record_type rtype;
1513 unsigned long label;
1514 {
1515 char bytes[20];
1516 int r = 0;
1517 int count = 1;
1518 if (label <= 0x1f)
1519 {
1520 output_B1_format (f, rtype, label);
1521 return;
1522 }
1523
1524 if (rtype == copy_state)
1525 r = 1;
1526 else if (rtype != label_state)
1527 as_bad ("Invalid record type for format B1");
1528
1529 bytes[0] = (UNW_B4 | (r << 3));
1530 count += output_leb128 (bytes + 1, label, 0);
1531 (*f) (count, bytes, NULL);
1532 }
1533
1534 static char
1535 format_ab_reg (ab, reg)
1536 int ab;
1537 int reg;
1538 {
1539 int ret;
1540 ab = (ab & 3);
1541 reg = (reg & 0x1f);
1542 ret = (ab << 5) | reg;
1543 return ret;
1544 }
1545
1546 static void
1547 output_X1_format (f, rtype, ab, reg, t, w1)
1548 vbyte_func f;
1549 unw_record_type rtype;
1550 int ab, reg;
1551 unsigned long t;
1552 unsigned long w1;
1553 {
1554 char bytes[20];
1555 int r = 0;
1556 int count = 2;
1557 bytes[0] = UNW_X1;
1558
1559 if (rtype == spill_sprel)
1560 r = 1;
1561 else if (rtype != spill_psprel)
1562 as_bad ("Invalid record type for format X1");
1563 bytes[1] = ((r << 7) | format_ab_reg (ab, reg));
1564 count += output_leb128 (bytes + 2, t, 0);
1565 count += output_leb128 (bytes + count, w1, 0);
1566 (*f) (count, bytes, NULL);
1567 }
1568
1569 static void
1570 output_X2_format (f, ab, reg, x, y, treg, t)
1571 vbyte_func f;
1572 int ab, reg;
1573 int x, y, treg;
1574 unsigned long t;
1575 {
1576 char bytes[20];
1577 int count = 3;
1578 bytes[0] = UNW_X2;
1579 bytes[1] = (((x & 1) << 7) | format_ab_reg (ab, reg));
1580 bytes[2] = (((y & 1) << 7) | (treg & 0x7f));
1581 count += output_leb128 (bytes + 3, t, 0);
1582 (*f) (count, bytes, NULL);
1583 }
1584
1585 static void
1586 output_X3_format (f, rtype, qp, ab, reg, t, w1)
1587 vbyte_func f;
1588 unw_record_type rtype;
1589 int qp;
1590 int ab, reg;
1591 unsigned long t;
1592 unsigned long w1;
1593 {
1594 char bytes[20];
1595 int r = 0;
1596 int count = 3;
1597 bytes[0] = UNW_X3;
1598
1599 if (rtype == spill_sprel_p)
1600 r = 1;
1601 else if (rtype != spill_psprel_p)
1602 as_bad ("Invalid record type for format X3");
1603 bytes[1] = ((r << 7) | (qp & 0x3f));
1604 bytes[2] = format_ab_reg (ab, reg);
1605 count += output_leb128 (bytes + 3, t, 0);
1606 count += output_leb128 (bytes + count, w1, 0);
1607 (*f) (count, bytes, NULL);
1608 }
1609
1610 static void
1611 output_X4_format (f, qp, ab, reg, x, y, treg, t)
1612 vbyte_func f;
1613 int qp;
1614 int ab, reg;
1615 int x, y, treg;
1616 unsigned long t;
1617 {
1618 char bytes[20];
1619 int count = 4;
1620 bytes[0] = UNW_X4;
1621 bytes[1] = (qp & 0x3f);
1622 bytes[2] = (((x & 1) << 7) | format_ab_reg (ab, reg));
1623 bytes[3] = (((y & 1) << 7) | (treg & 0x7f));
1624 count += output_leb128 (bytes + 4, t, 0);
1625 (*f) (count, bytes, NULL);
1626 }
1627
1628 /* This function allocates a record list structure, and initializes fields. */
1629
1630 static unw_rec_list *
1631 alloc_record (unw_record_type t)
1632 {
1633 unw_rec_list *ptr;
1634 ptr = xmalloc (sizeof (*ptr));
1635 ptr->next = NULL;
1636 ptr->slot_number = SLOT_NUM_NOT_SET;
1637 ptr->r.type = t;
1638 return ptr;
1639 }
1640
1641 /* This function frees an entire list of record structures. */
1642
1643 void
1644 free_list_records (unw_rec_list *first)
1645 {
1646 unw_rec_list *ptr;
1647 for (ptr = first; ptr != NULL;)
1648 {
1649 unw_rec_list *tmp = ptr;
1650
1651 if ((tmp->r.type == prologue || tmp->r.type == prologue_gr)
1652 && tmp->r.record.r.mask.i)
1653 free (tmp->r.record.r.mask.i);
1654
1655 ptr = ptr->next;
1656 free (tmp);
1657 }
1658 }
1659
1660 static unw_rec_list *
1661 output_prologue ()
1662 {
1663 unw_rec_list *ptr = alloc_record (prologue);
1664 memset (&ptr->r.record.r.mask, 0, sizeof (ptr->r.record.r.mask));
1665 return ptr;
1666 }
1667
1668 static unw_rec_list *
1669 output_prologue_gr (saved_mask, reg)
1670 unsigned int saved_mask;
1671 unsigned int reg;
1672 {
1673 unw_rec_list *ptr = alloc_record (prologue_gr);
1674 memset (&ptr->r.record.r.mask, 0, sizeof (ptr->r.record.r.mask));
1675 ptr->r.record.r.grmask = saved_mask;
1676 ptr->r.record.r.grsave = reg;
1677 return ptr;
1678 }
1679
1680 static unw_rec_list *
1681 output_body ()
1682 {
1683 unw_rec_list *ptr = alloc_record (body);
1684 return ptr;
1685 }
1686
1687 static unw_rec_list *
1688 output_mem_stack_f (size)
1689 unsigned int size;
1690 {
1691 unw_rec_list *ptr = alloc_record (mem_stack_f);
1692 ptr->r.record.p.size = size;
1693 return ptr;
1694 }
1695
1696 static unw_rec_list *
1697 output_mem_stack_v ()
1698 {
1699 unw_rec_list *ptr = alloc_record (mem_stack_v);
1700 return ptr;
1701 }
1702
1703 static unw_rec_list *
1704 output_psp_gr (gr)
1705 unsigned int gr;
1706 {
1707 unw_rec_list *ptr = alloc_record (psp_gr);
1708 ptr->r.record.p.gr = gr;
1709 return ptr;
1710 }
1711
1712 static unw_rec_list *
1713 output_psp_sprel (offset)
1714 unsigned int offset;
1715 {
1716 unw_rec_list *ptr = alloc_record (psp_sprel);
1717 ptr->r.record.p.spoff = offset / 4;
1718 return ptr;
1719 }
1720
1721 static unw_rec_list *
1722 output_rp_when ()
1723 {
1724 unw_rec_list *ptr = alloc_record (rp_when);
1725 return ptr;
1726 }
1727
1728 static unw_rec_list *
1729 output_rp_gr (gr)
1730 unsigned int gr;
1731 {
1732 unw_rec_list *ptr = alloc_record (rp_gr);
1733 ptr->r.record.p.gr = gr;
1734 return ptr;
1735 }
1736
1737 static unw_rec_list *
1738 output_rp_br (br)
1739 unsigned int br;
1740 {
1741 unw_rec_list *ptr = alloc_record (rp_br);
1742 ptr->r.record.p.br = br;
1743 return ptr;
1744 }
1745
1746 static unw_rec_list *
1747 output_rp_psprel (offset)
1748 unsigned int offset;
1749 {
1750 unw_rec_list *ptr = alloc_record (rp_psprel);
1751 ptr->r.record.p.pspoff = offset / 4;
1752 return ptr;
1753 }
1754
1755 static unw_rec_list *
1756 output_rp_sprel (offset)
1757 unsigned int offset;
1758 {
1759 unw_rec_list *ptr = alloc_record (rp_sprel);
1760 ptr->r.record.p.spoff = offset / 4;
1761 return ptr;
1762 }
1763
1764 static unw_rec_list *
1765 output_pfs_when ()
1766 {
1767 unw_rec_list *ptr = alloc_record (pfs_when);
1768 return ptr;
1769 }
1770
1771 static unw_rec_list *
1772 output_pfs_gr (gr)
1773 unsigned int gr;
1774 {
1775 unw_rec_list *ptr = alloc_record (pfs_gr);
1776 ptr->r.record.p.gr = gr;
1777 return ptr;
1778 }
1779
1780 static unw_rec_list *
1781 output_pfs_psprel (offset)
1782 unsigned int offset;
1783 {
1784 unw_rec_list *ptr = alloc_record (pfs_psprel);
1785 ptr->r.record.p.pspoff = offset / 4;
1786 return ptr;
1787 }
1788
1789 static unw_rec_list *
1790 output_pfs_sprel (offset)
1791 unsigned int offset;
1792 {
1793 unw_rec_list *ptr = alloc_record (pfs_sprel);
1794 ptr->r.record.p.spoff = offset / 4;
1795 return ptr;
1796 }
1797
1798 static unw_rec_list *
1799 output_preds_when ()
1800 {
1801 unw_rec_list *ptr = alloc_record (preds_when);
1802 return ptr;
1803 }
1804
1805 static unw_rec_list *
1806 output_preds_gr (gr)
1807 unsigned int gr;
1808 {
1809 unw_rec_list *ptr = alloc_record (preds_gr);
1810 ptr->r.record.p.gr = gr;
1811 return ptr;
1812 }
1813
1814 static unw_rec_list *
1815 output_preds_psprel (offset)
1816 unsigned int offset;
1817 {
1818 unw_rec_list *ptr = alloc_record (preds_psprel);
1819 ptr->r.record.p.pspoff = offset / 4;
1820 return ptr;
1821 }
1822
1823 static unw_rec_list *
1824 output_preds_sprel (offset)
1825 unsigned int offset;
1826 {
1827 unw_rec_list *ptr = alloc_record (preds_sprel);
1828 ptr->r.record.p.spoff = offset / 4;
1829 return ptr;
1830 }
1831
1832 static unw_rec_list *
1833 output_fr_mem (mask)
1834 unsigned int mask;
1835 {
1836 unw_rec_list *ptr = alloc_record (fr_mem);
1837 ptr->r.record.p.rmask = mask;
1838 return ptr;
1839 }
1840
1841 static unw_rec_list *
1842 output_frgr_mem (gr_mask, fr_mask)
1843 unsigned int gr_mask;
1844 unsigned int fr_mask;
1845 {
1846 unw_rec_list *ptr = alloc_record (frgr_mem);
1847 ptr->r.record.p.grmask = gr_mask;
1848 ptr->r.record.p.frmask = fr_mask;
1849 return ptr;
1850 }
1851
1852 static unw_rec_list *
1853 output_gr_gr (mask, reg)
1854 unsigned int mask;
1855 unsigned int reg;
1856 {
1857 unw_rec_list *ptr = alloc_record (gr_gr);
1858 ptr->r.record.p.grmask = mask;
1859 ptr->r.record.p.gr = reg;
1860 return ptr;
1861 }
1862
1863 static unw_rec_list *
1864 output_gr_mem (mask)
1865 unsigned int mask;
1866 {
1867 unw_rec_list *ptr = alloc_record (gr_mem);
1868 ptr->r.record.p.rmask = mask;
1869 return ptr;
1870 }
1871
1872 static unw_rec_list *
1873 output_br_mem (unsigned int mask)
1874 {
1875 unw_rec_list *ptr = alloc_record (br_mem);
1876 ptr->r.record.p.brmask = mask;
1877 return ptr;
1878 }
1879
1880 static unw_rec_list *
1881 output_br_gr (save_mask, reg)
1882 unsigned int save_mask;
1883 unsigned int reg;
1884 {
1885 unw_rec_list *ptr = alloc_record (br_gr);
1886 ptr->r.record.p.brmask = save_mask;
1887 ptr->r.record.p.gr = reg;
1888 return ptr;
1889 }
1890
1891 static unw_rec_list *
1892 output_spill_base (offset)
1893 unsigned int offset;
1894 {
1895 unw_rec_list *ptr = alloc_record (spill_base);
1896 ptr->r.record.p.pspoff = offset / 4;
1897 return ptr;
1898 }
1899
1900 static unw_rec_list *
1901 output_unat_when ()
1902 {
1903 unw_rec_list *ptr = alloc_record (unat_when);
1904 return ptr;
1905 }
1906
1907 static unw_rec_list *
1908 output_unat_gr (gr)
1909 unsigned int gr;
1910 {
1911 unw_rec_list *ptr = alloc_record (unat_gr);
1912 ptr->r.record.p.gr = gr;
1913 return ptr;
1914 }
1915
1916 static unw_rec_list *
1917 output_unat_psprel (offset)
1918 unsigned int offset;
1919 {
1920 unw_rec_list *ptr = alloc_record (unat_psprel);
1921 ptr->r.record.p.pspoff = offset / 4;
1922 return ptr;
1923 }
1924
1925 static unw_rec_list *
1926 output_unat_sprel (offset)
1927 unsigned int offset;
1928 {
1929 unw_rec_list *ptr = alloc_record (unat_sprel);
1930 ptr->r.record.p.spoff = offset / 4;
1931 return ptr;
1932 }
1933
1934 static unw_rec_list *
1935 output_lc_when ()
1936 {
1937 unw_rec_list *ptr = alloc_record (lc_when);
1938 return ptr;
1939 }
1940
1941 static unw_rec_list *
1942 output_lc_gr (gr)
1943 unsigned int gr;
1944 {
1945 unw_rec_list *ptr = alloc_record (lc_gr);
1946 ptr->r.record.p.gr = gr;
1947 return ptr;
1948 }
1949
1950 static unw_rec_list *
1951 output_lc_psprel (offset)
1952 unsigned int offset;
1953 {
1954 unw_rec_list *ptr = alloc_record (lc_psprel);
1955 ptr->r.record.p.pspoff = offset / 4;
1956 return ptr;
1957 }
1958
1959 static unw_rec_list *
1960 output_lc_sprel (offset)
1961 unsigned int offset;
1962 {
1963 unw_rec_list *ptr = alloc_record (lc_sprel);
1964 ptr->r.record.p.spoff = offset / 4;
1965 return ptr;
1966 }
1967
1968 static unw_rec_list *
1969 output_fpsr_when ()
1970 {
1971 unw_rec_list *ptr = alloc_record (fpsr_when);
1972 return ptr;
1973 }
1974
1975 static unw_rec_list *
1976 output_fpsr_gr (gr)
1977 unsigned int gr;
1978 {
1979 unw_rec_list *ptr = alloc_record (fpsr_gr);
1980 ptr->r.record.p.gr = gr;
1981 return ptr;
1982 }
1983
1984 static unw_rec_list *
1985 output_fpsr_psprel (offset)
1986 unsigned int offset;
1987 {
1988 unw_rec_list *ptr = alloc_record (fpsr_psprel);
1989 ptr->r.record.p.pspoff = offset / 4;
1990 return ptr;
1991 }
1992
1993 static unw_rec_list *
1994 output_fpsr_sprel (offset)
1995 unsigned int offset;
1996 {
1997 unw_rec_list *ptr = alloc_record (fpsr_sprel);
1998 ptr->r.record.p.spoff = offset / 4;
1999 return ptr;
2000 }
2001
2002 static unw_rec_list *
2003 output_priunat_when_gr ()
2004 {
2005 unw_rec_list *ptr = alloc_record (priunat_when_gr);
2006 return ptr;
2007 }
2008
2009 static unw_rec_list *
2010 output_priunat_when_mem ()
2011 {
2012 unw_rec_list *ptr = alloc_record (priunat_when_mem);
2013 return ptr;
2014 }
2015
2016 static unw_rec_list *
2017 output_priunat_gr (gr)
2018 unsigned int gr;
2019 {
2020 unw_rec_list *ptr = alloc_record (priunat_gr);
2021 ptr->r.record.p.gr = gr;
2022 return ptr;
2023 }
2024
2025 static unw_rec_list *
2026 output_priunat_psprel (offset)
2027 unsigned int offset;
2028 {
2029 unw_rec_list *ptr = alloc_record (priunat_psprel);
2030 ptr->r.record.p.pspoff = offset / 4;
2031 return ptr;
2032 }
2033
2034 static unw_rec_list *
2035 output_priunat_sprel (offset)
2036 unsigned int offset;
2037 {
2038 unw_rec_list *ptr = alloc_record (priunat_sprel);
2039 ptr->r.record.p.spoff = offset / 4;
2040 return ptr;
2041 }
2042
2043 static unw_rec_list *
2044 output_bsp_when ()
2045 {
2046 unw_rec_list *ptr = alloc_record (bsp_when);
2047 return ptr;
2048 }
2049
2050 static unw_rec_list *
2051 output_bsp_gr (gr)
2052 unsigned int gr;
2053 {
2054 unw_rec_list *ptr = alloc_record (bsp_gr);
2055 ptr->r.record.p.gr = gr;
2056 return ptr;
2057 }
2058
2059 static unw_rec_list *
2060 output_bsp_psprel (offset)
2061 unsigned int offset;
2062 {
2063 unw_rec_list *ptr = alloc_record (bsp_psprel);
2064 ptr->r.record.p.pspoff = offset / 4;
2065 return ptr;
2066 }
2067
2068 static unw_rec_list *
2069 output_bsp_sprel (offset)
2070 unsigned int offset;
2071 {
2072 unw_rec_list *ptr = alloc_record (bsp_sprel);
2073 ptr->r.record.p.spoff = offset / 4;
2074 return ptr;
2075 }
2076
2077 static unw_rec_list *
2078 output_bspstore_when ()
2079 {
2080 unw_rec_list *ptr = alloc_record (bspstore_when);
2081 return ptr;
2082 }
2083
2084 static unw_rec_list *
2085 output_bspstore_gr (gr)
2086 unsigned int gr;
2087 {
2088 unw_rec_list *ptr = alloc_record (bspstore_gr);
2089 ptr->r.record.p.gr = gr;
2090 return ptr;
2091 }
2092
2093 static unw_rec_list *
2094 output_bspstore_psprel (offset)
2095 unsigned int offset;
2096 {
2097 unw_rec_list *ptr = alloc_record (bspstore_psprel);
2098 ptr->r.record.p.pspoff = offset / 4;
2099 return ptr;
2100 }
2101
2102 static unw_rec_list *
2103 output_bspstore_sprel (offset)
2104 unsigned int offset;
2105 {
2106 unw_rec_list *ptr = alloc_record (bspstore_sprel);
2107 ptr->r.record.p.spoff = offset / 4;
2108 return ptr;
2109 }
2110
2111 static unw_rec_list *
2112 output_rnat_when ()
2113 {
2114 unw_rec_list *ptr = alloc_record (rnat_when);
2115 return ptr;
2116 }
2117
2118 static unw_rec_list *
2119 output_rnat_gr (gr)
2120 unsigned int gr;
2121 {
2122 unw_rec_list *ptr = alloc_record (rnat_gr);
2123 ptr->r.record.p.gr = gr;
2124 return ptr;
2125 }
2126
2127 static unw_rec_list *
2128 output_rnat_psprel (offset)
2129 unsigned int offset;
2130 {
2131 unw_rec_list *ptr = alloc_record (rnat_psprel);
2132 ptr->r.record.p.pspoff = offset / 4;
2133 return ptr;
2134 }
2135
2136 static unw_rec_list *
2137 output_rnat_sprel (offset)
2138 unsigned int offset;
2139 {
2140 unw_rec_list *ptr = alloc_record (rnat_sprel);
2141 ptr->r.record.p.spoff = offset / 4;
2142 return ptr;
2143 }
2144
2145 static unw_rec_list *
2146 output_unwabi (abi, context)
2147 unsigned long abi;
2148 unsigned long context;
2149 {
2150 unw_rec_list *ptr = alloc_record (unwabi);
2151 ptr->r.record.p.abi = abi;
2152 ptr->r.record.p.context = context;
2153 return ptr;
2154 }
2155
2156 static unw_rec_list *
2157 output_epilogue (unsigned long ecount)
2158 {
2159 unw_rec_list *ptr = alloc_record (epilogue);
2160 ptr->r.record.b.ecount = ecount;
2161 return ptr;
2162 }
2163
2164 static unw_rec_list *
2165 output_label_state (unsigned long label)
2166 {
2167 unw_rec_list *ptr = alloc_record (label_state);
2168 ptr->r.record.b.label = label;
2169 return ptr;
2170 }
2171
2172 static unw_rec_list *
2173 output_copy_state (unsigned long label)
2174 {
2175 unw_rec_list *ptr = alloc_record (copy_state);
2176 ptr->r.record.b.label = label;
2177 return ptr;
2178 }
2179
2180 static unw_rec_list *
2181 output_spill_psprel (ab, reg, offset)
2182 unsigned int ab;
2183 unsigned int reg;
2184 unsigned int offset;
2185 {
2186 unw_rec_list *ptr = alloc_record (spill_psprel);
2187 ptr->r.record.x.ab = ab;
2188 ptr->r.record.x.reg = reg;
2189 ptr->r.record.x.pspoff = offset / 4;
2190 return ptr;
2191 }
2192
2193 static unw_rec_list *
2194 output_spill_sprel (ab, reg, offset)
2195 unsigned int ab;
2196 unsigned int reg;
2197 unsigned int offset;
2198 {
2199 unw_rec_list *ptr = alloc_record (spill_sprel);
2200 ptr->r.record.x.ab = ab;
2201 ptr->r.record.x.reg = reg;
2202 ptr->r.record.x.spoff = offset / 4;
2203 return ptr;
2204 }
2205
2206 static unw_rec_list *
2207 output_spill_psprel_p (ab, reg, offset, predicate)
2208 unsigned int ab;
2209 unsigned int reg;
2210 unsigned int offset;
2211 unsigned int predicate;
2212 {
2213 unw_rec_list *ptr = alloc_record (spill_psprel_p);
2214 ptr->r.record.x.ab = ab;
2215 ptr->r.record.x.reg = reg;
2216 ptr->r.record.x.pspoff = offset / 4;
2217 ptr->r.record.x.qp = predicate;
2218 return ptr;
2219 }
2220
2221 static unw_rec_list *
2222 output_spill_sprel_p (ab, reg, offset, predicate)
2223 unsigned int ab;
2224 unsigned int reg;
2225 unsigned int offset;
2226 unsigned int predicate;
2227 {
2228 unw_rec_list *ptr = alloc_record (spill_sprel_p);
2229 ptr->r.record.x.ab = ab;
2230 ptr->r.record.x.reg = reg;
2231 ptr->r.record.x.spoff = offset / 4;
2232 ptr->r.record.x.qp = predicate;
2233 return ptr;
2234 }
2235
2236 static unw_rec_list *
2237 output_spill_reg (ab, reg, targ_reg, xy)
2238 unsigned int ab;
2239 unsigned int reg;
2240 unsigned int targ_reg;
2241 unsigned int xy;
2242 {
2243 unw_rec_list *ptr = alloc_record (spill_reg);
2244 ptr->r.record.x.ab = ab;
2245 ptr->r.record.x.reg = reg;
2246 ptr->r.record.x.treg = targ_reg;
2247 ptr->r.record.x.xy = xy;
2248 return ptr;
2249 }
2250
2251 static unw_rec_list *
2252 output_spill_reg_p (ab, reg, targ_reg, xy, predicate)
2253 unsigned int ab;
2254 unsigned int reg;
2255 unsigned int targ_reg;
2256 unsigned int xy;
2257 unsigned int predicate;
2258 {
2259 unw_rec_list *ptr = alloc_record (spill_reg_p);
2260 ptr->r.record.x.ab = ab;
2261 ptr->r.record.x.reg = reg;
2262 ptr->r.record.x.treg = targ_reg;
2263 ptr->r.record.x.xy = xy;
2264 ptr->r.record.x.qp = predicate;
2265 return ptr;
2266 }
2267
2268 /* Given a unw_rec_list process the correct format with the
2269 specified function. */
2270
2271 static void
2272 process_one_record (ptr, f)
2273 unw_rec_list *ptr;
2274 vbyte_func f;
2275 {
2276 unsigned long fr_mask, gr_mask;
2277
2278 switch (ptr->r.type)
2279 {
2280 case gr_mem:
2281 case fr_mem:
2282 case br_mem:
2283 case frgr_mem:
2284 /* These are taken care of by prologue/prologue_gr. */
2285 break;
2286
2287 case prologue_gr:
2288 case prologue:
2289 if (ptr->r.type == prologue_gr)
2290 output_R2_format (f, ptr->r.record.r.grmask,
2291 ptr->r.record.r.grsave, ptr->r.record.r.rlen);
2292 else
2293 output_R1_format (f, ptr->r.type, ptr->r.record.r.rlen);
2294
2295 /* Output descriptor(s) for union of register spills (if any). */
2296 gr_mask = ptr->r.record.r.mask.gr_mem;
2297 fr_mask = ptr->r.record.r.mask.fr_mem;
2298 if (fr_mask)
2299 {
2300 if ((fr_mask & ~0xfUL) == 0)
2301 output_P6_format (f, fr_mem, fr_mask);
2302 else
2303 {
2304 output_P5_format (f, gr_mask, fr_mask);
2305 gr_mask = 0;
2306 }
2307 }
2308 if (gr_mask)
2309 output_P6_format (f, gr_mem, gr_mask);
2310 if (ptr->r.record.r.mask.br_mem)
2311 output_P1_format (f, ptr->r.record.r.mask.br_mem);
2312
2313 /* output imask descriptor if necessary: */
2314 if (ptr->r.record.r.mask.i)
2315 output_P4_format (f, ptr->r.record.r.mask.i,
2316 ptr->r.record.r.imask_size);
2317 break;
2318
2319 case body:
2320 output_R1_format (f, ptr->r.type, ptr->r.record.r.rlen);
2321 break;
2322 case mem_stack_f:
2323 case mem_stack_v:
2324 output_P7_format (f, ptr->r.type, ptr->r.record.p.t,
2325 ptr->r.record.p.size);
2326 break;
2327 case psp_gr:
2328 case rp_gr:
2329 case pfs_gr:
2330 case preds_gr:
2331 case unat_gr:
2332 case lc_gr:
2333 case fpsr_gr:
2334 case priunat_gr:
2335 case bsp_gr:
2336 case bspstore_gr:
2337 case rnat_gr:
2338 output_P3_format (f, ptr->r.type, ptr->r.record.p.gr);
2339 break;
2340 case rp_br:
2341 output_P3_format (f, rp_br, ptr->r.record.p.br);
2342 break;
2343 case psp_sprel:
2344 output_P7_format (f, psp_sprel, ptr->r.record.p.spoff, 0);
2345 break;
2346 case rp_when:
2347 case pfs_when:
2348 case preds_when:
2349 case unat_when:
2350 case lc_when:
2351 case fpsr_when:
2352 output_P7_format (f, ptr->r.type, ptr->r.record.p.t, 0);
2353 break;
2354 case rp_psprel:
2355 case pfs_psprel:
2356 case preds_psprel:
2357 case unat_psprel:
2358 case lc_psprel:
2359 case fpsr_psprel:
2360 case spill_base:
2361 output_P7_format (f, ptr->r.type, ptr->r.record.p.pspoff, 0);
2362 break;
2363 case rp_sprel:
2364 case pfs_sprel:
2365 case preds_sprel:
2366 case unat_sprel:
2367 case lc_sprel:
2368 case fpsr_sprel:
2369 case priunat_sprel:
2370 case bsp_sprel:
2371 case bspstore_sprel:
2372 case rnat_sprel:
2373 output_P8_format (f, ptr->r.type, ptr->r.record.p.spoff);
2374 break;
2375 case gr_gr:
2376 output_P9_format (f, ptr->r.record.p.grmask, ptr->r.record.p.gr);
2377 break;
2378 case br_gr:
2379 output_P2_format (f, ptr->r.record.p.brmask, ptr->r.record.p.gr);
2380 break;
2381 case spill_mask:
2382 as_bad ("spill_mask record unimplemented.");
2383 break;
2384 case priunat_when_gr:
2385 case priunat_when_mem:
2386 case bsp_when:
2387 case bspstore_when:
2388 case rnat_when:
2389 output_P8_format (f, ptr->r.type, ptr->r.record.p.t);
2390 break;
2391 case priunat_psprel:
2392 case bsp_psprel:
2393 case bspstore_psprel:
2394 case rnat_psprel:
2395 output_P8_format (f, ptr->r.type, ptr->r.record.p.pspoff);
2396 break;
2397 case unwabi:
2398 output_P10_format (f, ptr->r.record.p.abi, ptr->r.record.p.context);
2399 break;
2400 case epilogue:
2401 output_B3_format (f, ptr->r.record.b.ecount, ptr->r.record.b.t);
2402 break;
2403 case label_state:
2404 case copy_state:
2405 output_B4_format (f, ptr->r.type, ptr->r.record.b.label);
2406 break;
2407 case spill_psprel:
2408 output_X1_format (f, ptr->r.type, ptr->r.record.x.ab,
2409 ptr->r.record.x.reg, ptr->r.record.x.t,
2410 ptr->r.record.x.pspoff);
2411 break;
2412 case spill_sprel:
2413 output_X1_format (f, ptr->r.type, ptr->r.record.x.ab,
2414 ptr->r.record.x.reg, ptr->r.record.x.t,
2415 ptr->r.record.x.spoff);
2416 break;
2417 case spill_reg:
2418 output_X2_format (f, ptr->r.record.x.ab, ptr->r.record.x.reg,
2419 ptr->r.record.x.xy >> 1, ptr->r.record.x.xy,
2420 ptr->r.record.x.treg, ptr->r.record.x.t);
2421 break;
2422 case spill_psprel_p:
2423 output_X3_format (f, ptr->r.type, ptr->r.record.x.qp,
2424 ptr->r.record.x.ab, ptr->r.record.x.reg,
2425 ptr->r.record.x.t, ptr->r.record.x.pspoff);
2426 break;
2427 case spill_sprel_p:
2428 output_X3_format (f, ptr->r.type, ptr->r.record.x.qp,
2429 ptr->r.record.x.ab, ptr->r.record.x.reg,
2430 ptr->r.record.x.t, ptr->r.record.x.spoff);
2431 break;
2432 case spill_reg_p:
2433 output_X4_format (f, ptr->r.record.x.qp, ptr->r.record.x.ab,
2434 ptr->r.record.x.reg, ptr->r.record.x.xy >> 1,
2435 ptr->r.record.x.xy, ptr->r.record.x.treg,
2436 ptr->r.record.x.t);
2437 break;
2438 default:
2439 as_bad ("record_type_not_valid");
2440 break;
2441 }
2442 }
2443
2444 /* Given a unw_rec_list list, process all the records with
2445 the specified function. */
2446 static void
2447 process_unw_records (list, f)
2448 unw_rec_list *list;
2449 vbyte_func f;
2450 {
2451 unw_rec_list *ptr;
2452 for (ptr = list; ptr; ptr = ptr->next)
2453 process_one_record (ptr, f);
2454 }
2455
2456 /* Determine the size of a record list in bytes. */
2457 static int
2458 calc_record_size (list)
2459 unw_rec_list *list;
2460 {
2461 vbyte_count = 0;
2462 process_unw_records (list, count_output);
2463 return vbyte_count;
2464 }
2465
2466 /* Update IMASK bitmask to reflect the fact that one or more registers
2467 of type TYPE are saved starting at instruction with index T. If N
2468 bits are set in REGMASK, it is assumed that instructions T through
2469 T+N-1 save these registers.
2470
2471 TYPE values:
2472 0: no save
2473 1: instruction saves next fp reg
2474 2: instruction saves next general reg
2475 3: instruction saves next branch reg */
2476 static void
2477 set_imask (region, regmask, t, type)
2478 unw_rec_list *region;
2479 unsigned long regmask;
2480 unsigned long t;
2481 unsigned int type;
2482 {
2483 unsigned char *imask;
2484 unsigned long imask_size;
2485 unsigned int i;
2486 int pos;
2487
2488 imask = region->r.record.r.mask.i;
2489 imask_size = region->r.record.r.imask_size;
2490 if (!imask)
2491 {
2492 imask_size = (region->r.record.r.rlen * 2 + 7) / 8 + 1;
2493 imask = xmalloc (imask_size);
2494 memset (imask, 0, imask_size);
2495
2496 region->r.record.r.imask_size = imask_size;
2497 region->r.record.r.mask.i = imask;
2498 }
2499
2500 i = (t / 4) + 1;
2501 pos = 2 * (3 - t % 4);
2502 while (regmask)
2503 {
2504 if (i >= imask_size)
2505 {
2506 as_bad ("Ignoring attempt to spill beyond end of region");
2507 return;
2508 }
2509
2510 imask[i] |= (type & 0x3) << pos;
2511
2512 regmask &= (regmask - 1);
2513 pos -= 2;
2514 if (pos < 0)
2515 {
2516 pos = 0;
2517 ++i;
2518 }
2519 }
2520 }
2521
2522 static int
2523 count_bits (unsigned long mask)
2524 {
2525 int n = 0;
2526
2527 while (mask)
2528 {
2529 mask &= mask - 1;
2530 ++n;
2531 }
2532 return n;
2533 }
2534
2535 /* Return the number of instruction slots from FIRST_ADDR to SLOT_ADDR.
2536 SLOT_FRAG is the frag containing SLOT_ADDR, and FIRST_FRAG is the frag
2537 containing FIRST_ADDR. */
2538
2539 unsigned long
2540 slot_index (slot_addr, slot_frag, first_addr, first_frag)
2541 unsigned long slot_addr;
2542 fragS *slot_frag;
2543 unsigned long first_addr;
2544 fragS *first_frag;
2545 {
2546 unsigned long index = 0;
2547
2548 /* First time we are called, the initial address and frag are invalid. */
2549 if (first_addr == 0)
2550 return 0;
2551
2552 /* If the two addresses are in different frags, then we need to add in
2553 the remaining size of this frag, and then the entire size of intermediate
2554 frags. */
2555 while (slot_frag != first_frag)
2556 {
2557 unsigned long start_addr = (unsigned long) &first_frag->fr_literal;
2558
2559 /* Add in the full size of the frag converted to instruction slots. */
2560 index += 3 * (first_frag->fr_fix >> 4);
2561 /* Subtract away the initial part before first_addr. */
2562 index -= (3 * ((first_addr >> 4) - (start_addr >> 4))
2563 + ((first_addr & 0x3) - (start_addr & 0x3)));
2564
2565 /* Move to the beginning of the next frag. */
2566 first_frag = first_frag->fr_next;
2567 first_addr = (unsigned long) &first_frag->fr_literal;
2568 }
2569
2570 /* Add in the used part of the last frag. */
2571 index += (3 * ((slot_addr >> 4) - (first_addr >> 4))
2572 + ((slot_addr & 0x3) - (first_addr & 0x3)));
2573 return index;
2574 }
2575
2576 /* Optimize unwind record directives. */
2577
2578 static unw_rec_list *
2579 optimize_unw_records (list)
2580 unw_rec_list *list;
2581 {
2582 if (!list)
2583 return NULL;
2584
2585 /* If the only unwind record is ".prologue" or ".prologue" followed
2586 by ".body", then we can optimize the unwind directives away. */
2587 if (list->r.type == prologue
2588 && (list->next == NULL
2589 || (list->next->r.type == body && list->next->next == NULL)))
2590 return NULL;
2591
2592 return list;
2593 }
2594
2595 /* Given a complete record list, process any records which have
2596 unresolved fields, (ie length counts for a prologue). After
2597 this has been run, all neccessary information should be available
2598 within each record to generate an image. */
2599
2600 static void
2601 fixup_unw_records (list)
2602 unw_rec_list *list;
2603 {
2604 unw_rec_list *ptr, *region = 0;
2605 unsigned long first_addr = 0, rlen = 0, t;
2606 fragS *first_frag = 0;
2607
2608 for (ptr = list; ptr; ptr = ptr->next)
2609 {
2610 if (ptr->slot_number == SLOT_NUM_NOT_SET)
2611 as_bad (" Insn slot not set in unwind record.");
2612 t = slot_index (ptr->slot_number, ptr->slot_frag,
2613 first_addr, first_frag);
2614 switch (ptr->r.type)
2615 {
2616 case prologue:
2617 case prologue_gr:
2618 case body:
2619 {
2620 unw_rec_list *last;
2621 int size, dir_len = 0;
2622 unsigned long last_addr;
2623 fragS *last_frag;
2624
2625 first_addr = ptr->slot_number;
2626 first_frag = ptr->slot_frag;
2627 ptr->slot_number = 0;
2628 /* Find either the next body/prologue start, or the end of
2629 the list, and determine the size of the region. */
2630 last_addr = unwind.next_slot_number;
2631 last_frag = unwind.next_slot_frag;
2632 for (last = ptr->next; last != NULL; last = last->next)
2633 if (last->r.type == prologue || last->r.type == prologue_gr
2634 || last->r.type == body)
2635 {
2636 last_addr = last->slot_number;
2637 last_frag = last->slot_frag;
2638 break;
2639 }
2640 else if (!last->next)
2641 {
2642 /* In the absence of an explicit .body directive,
2643 the prologue ends after the last instruction
2644 covered by an unwind directive. */
2645 if (ptr->r.type != body)
2646 {
2647 last_addr = last->slot_number;
2648 last_frag = last->slot_frag;
2649 switch (last->r.type)
2650 {
2651 case frgr_mem:
2652 dir_len = (count_bits (last->r.record.p.frmask)
2653 + count_bits (last->r.record.p.grmask));
2654 break;
2655 case fr_mem:
2656 case gr_mem:
2657 dir_len += count_bits (last->r.record.p.rmask);
2658 break;
2659 case br_mem:
2660 case br_gr:
2661 dir_len += count_bits (last->r.record.p.brmask);
2662 break;
2663 case gr_gr:
2664 dir_len += count_bits (last->r.record.p.grmask);
2665 break;
2666 default:
2667 dir_len = 1;
2668 break;
2669 }
2670 }
2671 break;
2672 }
2673 size = (slot_index (last_addr, last_frag, first_addr, first_frag)
2674 + dir_len);
2675 rlen = ptr->r.record.r.rlen = size;
2676 region = ptr;
2677 break;
2678 }
2679 case epilogue:
2680 ptr->r.record.b.t = rlen - 1 - t;
2681 break;
2682
2683 case mem_stack_f:
2684 case mem_stack_v:
2685 case rp_when:
2686 case pfs_when:
2687 case preds_when:
2688 case unat_when:
2689 case lc_when:
2690 case fpsr_when:
2691 case priunat_when_gr:
2692 case priunat_when_mem:
2693 case bsp_when:
2694 case bspstore_when:
2695 case rnat_when:
2696 ptr->r.record.p.t = t;
2697 break;
2698
2699 case spill_reg:
2700 case spill_sprel:
2701 case spill_psprel:
2702 case spill_reg_p:
2703 case spill_sprel_p:
2704 case spill_psprel_p:
2705 ptr->r.record.x.t = t;
2706 break;
2707
2708 case frgr_mem:
2709 if (!region)
2710 {
2711 as_bad ("frgr_mem record before region record!\n");
2712 return;
2713 }
2714 region->r.record.r.mask.fr_mem |= ptr->r.record.p.frmask;
2715 region->r.record.r.mask.gr_mem |= ptr->r.record.p.grmask;
2716 set_imask (region, ptr->r.record.p.frmask, t, 1);
2717 set_imask (region, ptr->r.record.p.grmask, t, 2);
2718 break;
2719 case fr_mem:
2720 if (!region)
2721 {
2722 as_bad ("fr_mem record before region record!\n");
2723 return;
2724 }
2725 region->r.record.r.mask.fr_mem |= ptr->r.record.p.rmask;
2726 set_imask (region, ptr->r.record.p.rmask, t, 1);
2727 break;
2728 case gr_mem:
2729 if (!region)
2730 {
2731 as_bad ("gr_mem record before region record!\n");
2732 return;
2733 }
2734 region->r.record.r.mask.gr_mem |= ptr->r.record.p.rmask;
2735 set_imask (region, ptr->r.record.p.rmask, t, 2);
2736 break;
2737 case br_mem:
2738 if (!region)
2739 {
2740 as_bad ("br_mem record before region record!\n");
2741 return;
2742 }
2743 region->r.record.r.mask.br_mem |= ptr->r.record.p.brmask;
2744 set_imask (region, ptr->r.record.p.brmask, t, 3);
2745 break;
2746
2747 case gr_gr:
2748 if (!region)
2749 {
2750 as_bad ("gr_gr record before region record!\n");
2751 return;
2752 }
2753 set_imask (region, ptr->r.record.p.grmask, t, 2);
2754 break;
2755 case br_gr:
2756 if (!region)
2757 {
2758 as_bad ("br_gr record before region record!\n");
2759 return;
2760 }
2761 set_imask (region, ptr->r.record.p.brmask, t, 3);
2762 break;
2763
2764 default:
2765 break;
2766 }
2767 }
2768 }
2769
2770 /* Helper routine for output_unw_records. Emits the header for the unwind
2771 info. */
2772
2773 static int
2774 setup_unwind_header (int size, unsigned char **mem)
2775 {
2776 int x, extra = 0;
2777
2778 /* pad to pointer-size boundry. */
2779 x = size % md.pointer_size;
2780 if (x != 0)
2781 extra = md.pointer_size - x;
2782
2783 /* Add 8 for the header + a pointer for the
2784 personality offset. */
2785 *mem = xmalloc (size + extra + 8 + md.pointer_size);
2786
2787 /* Clear the padding area and personality. */
2788 memset (*mem + 8 + size, 0 , extra + md.pointer_size);
2789 /* Initialize the header area. */
2790
2791 md_number_to_chars (*mem, (((bfd_vma) 1 << 48) /* version */
2792 | (unwind.personality_routine
2793 ? ((bfd_vma) 3 << 32) /* U & E handler flags */
2794 : 0)
2795 | ((size + extra) / md.pointer_size)), /* length */
2796 8);
2797
2798 return extra;
2799 }
2800
2801 /* Generate an unwind image from a record list. Returns the number of
2802 bytes in the resulting image. The memory image itselof is returned
2803 in the 'ptr' parameter. */
2804 static int
2805 output_unw_records (list, ptr)
2806 unw_rec_list *list;
2807 void **ptr;
2808 {
2809 int size, extra;
2810 unsigned char *mem;
2811
2812 *ptr = NULL;
2813
2814 list = optimize_unw_records (list);
2815 fixup_unw_records (list);
2816 size = calc_record_size (list);
2817
2818 if (size > 0 || unwind.force_unwind_entry)
2819 {
2820 unwind.force_unwind_entry = 0;
2821 extra = setup_unwind_header (size, &mem);
2822
2823 vbyte_mem_ptr = mem + 8;
2824 process_unw_records (list, output_vbyte_mem);
2825
2826 *ptr = mem;
2827
2828 size += extra + 8 + md.pointer_size;
2829 }
2830 return size;
2831 }
2832
2833 static int
2834 convert_expr_to_ab_reg (e, ab, regp)
2835 expressionS *e;
2836 unsigned int *ab;
2837 unsigned int *regp;
2838 {
2839 unsigned int reg;
2840
2841 if (e->X_op != O_register)
2842 return 0;
2843
2844 reg = e->X_add_number;
2845 if (reg >= (REG_GR + 4) && reg <= (REG_GR + 7))
2846 {
2847 *ab = 0;
2848 *regp = reg - REG_GR;
2849 }
2850 else if ((reg >= (REG_FR + 2) && reg <= (REG_FR + 5))
2851 || (reg >= (REG_FR + 16) && reg <= (REG_FR + 31)))
2852 {
2853 *ab = 1;
2854 *regp = reg - REG_FR;
2855 }
2856 else if (reg >= (REG_BR + 1) && reg <= (REG_BR + 5))
2857 {
2858 *ab = 2;
2859 *regp = reg - REG_BR;
2860 }
2861 else
2862 {
2863 *ab = 3;
2864 switch (reg)
2865 {
2866 case REG_PR: *regp = 0; break;
2867 case REG_PSP: *regp = 1; break;
2868 case REG_PRIUNAT: *regp = 2; break;
2869 case REG_BR + 0: *regp = 3; break;
2870 case REG_AR + AR_BSP: *regp = 4; break;
2871 case REG_AR + AR_BSPSTORE: *regp = 5; break;
2872 case REG_AR + AR_RNAT: *regp = 6; break;
2873 case REG_AR + AR_UNAT: *regp = 7; break;
2874 case REG_AR + AR_FPSR: *regp = 8; break;
2875 case REG_AR + AR_PFS: *regp = 9; break;
2876 case REG_AR + AR_LC: *regp = 10; break;
2877
2878 default:
2879 return 0;
2880 }
2881 }
2882 return 1;
2883 }
2884
2885 static int
2886 convert_expr_to_xy_reg (e, xy, regp)
2887 expressionS *e;
2888 unsigned int *xy;
2889 unsigned int *regp;
2890 {
2891 unsigned int reg;
2892
2893 if (e->X_op != O_register)
2894 return 0;
2895
2896 reg = e->X_add_number;
2897
2898 if (/* reg >= REG_GR && */ reg <= (REG_GR + 127))
2899 {
2900 *xy = 0;
2901 *regp = reg - REG_GR;
2902 }
2903 else if (reg >= REG_FR && reg <= (REG_FR + 127))
2904 {
2905 *xy = 1;
2906 *regp = reg - REG_FR;
2907 }
2908 else if (reg >= REG_BR && reg <= (REG_BR + 7))
2909 {
2910 *xy = 2;
2911 *regp = reg - REG_BR;
2912 }
2913 else
2914 return -1;
2915 return 1;
2916 }
2917
2918 static void
2919 dot_radix (dummy)
2920 int dummy ATTRIBUTE_UNUSED;
2921 {
2922 int radix;
2923
2924 SKIP_WHITESPACE ();
2925 radix = *input_line_pointer++;
2926
2927 if (radix != 'C' && !is_end_of_line[(unsigned char) radix])
2928 {
2929 as_bad ("Radix `%c' unsupported", *input_line_pointer);
2930 ignore_rest_of_line ();
2931 return;
2932 }
2933 }
2934
2935 /* .sbss, .bss etc. are macros that expand into ".section SECNAME". */
2936 static void
2937 dot_special_section (which)
2938 int which;
2939 {
2940 set_section ((char *) special_section_name[which]);
2941 }
2942
2943 static void
2944 add_unwind_entry (ptr)
2945 unw_rec_list *ptr;
2946 {
2947 if (unwind.tail)
2948 unwind.tail->next = ptr;
2949 else
2950 unwind.list = ptr;
2951 unwind.tail = ptr;
2952
2953 /* The current entry can in fact be a chain of unwind entries. */
2954 if (unwind.current_entry == NULL)
2955 unwind.current_entry = ptr;
2956 }
2957
2958 static void
2959 dot_fframe (dummy)
2960 int dummy ATTRIBUTE_UNUSED;
2961 {
2962 expressionS e;
2963
2964 parse_operand (&e);
2965
2966 if (e.X_op != O_constant)
2967 as_bad ("Operand to .fframe must be a constant");
2968 else
2969 add_unwind_entry (output_mem_stack_f (e.X_add_number));
2970 }
2971
2972 static void
2973 dot_vframe (dummy)
2974 int dummy ATTRIBUTE_UNUSED;
2975 {
2976 expressionS e;
2977 unsigned reg;
2978
2979 parse_operand (&e);
2980 reg = e.X_add_number - REG_GR;
2981 if (e.X_op == O_register && reg < 128)
2982 {
2983 add_unwind_entry (output_mem_stack_v ());
2984 if (! (unwind.prologue_mask & 2))
2985 add_unwind_entry (output_psp_gr (reg));
2986 }
2987 else
2988 as_bad ("First operand to .vframe must be a general register");
2989 }
2990
2991 static void
2992 dot_vframesp (dummy)
2993 int dummy ATTRIBUTE_UNUSED;
2994 {
2995 expressionS e;
2996
2997 parse_operand (&e);
2998 if (e.X_op == O_constant)
2999 {
3000 add_unwind_entry (output_mem_stack_v ());
3001 add_unwind_entry (output_psp_sprel (e.X_add_number));
3002 }
3003 else
3004 as_bad ("First operand to .vframesp must be a general register");
3005 }
3006
3007 static void
3008 dot_vframepsp (dummy)
3009 int dummy ATTRIBUTE_UNUSED;
3010 {
3011 expressionS e;
3012
3013 parse_operand (&e);
3014 if (e.X_op == O_constant)
3015 {
3016 add_unwind_entry (output_mem_stack_v ());
3017 add_unwind_entry (output_psp_sprel (e.X_add_number));
3018 }
3019 else
3020 as_bad ("First operand to .vframepsp must be a general register");
3021 }
3022
3023 static void
3024 dot_save (dummy)
3025 int dummy ATTRIBUTE_UNUSED;
3026 {
3027 expressionS e1, e2;
3028 int sep;
3029 int reg1, reg2;
3030
3031 sep = parse_operand (&e1);
3032 if (sep != ',')
3033 as_bad ("No second operand to .save");
3034 sep = parse_operand (&e2);
3035
3036 reg1 = e1.X_add_number;
3037 reg2 = e2.X_add_number - REG_GR;
3038
3039 /* Make sure its a valid ar.xxx reg, OR its br0, aka 'rp'. */
3040 if (e1.X_op == O_register)
3041 {
3042 if (e2.X_op == O_register && reg2 >= 0 && reg2 < 128)
3043 {
3044 switch (reg1)
3045 {
3046 case REG_AR + AR_BSP:
3047 add_unwind_entry (output_bsp_when ());
3048 add_unwind_entry (output_bsp_gr (reg2));
3049 break;
3050 case REG_AR + AR_BSPSTORE:
3051 add_unwind_entry (output_bspstore_when ());
3052 add_unwind_entry (output_bspstore_gr (reg2));
3053 break;
3054 case REG_AR + AR_RNAT:
3055 add_unwind_entry (output_rnat_when ());
3056 add_unwind_entry (output_rnat_gr (reg2));
3057 break;
3058 case REG_AR + AR_UNAT:
3059 add_unwind_entry (output_unat_when ());
3060 add_unwind_entry (output_unat_gr (reg2));
3061 break;
3062 case REG_AR + AR_FPSR:
3063 add_unwind_entry (output_fpsr_when ());
3064 add_unwind_entry (output_fpsr_gr (reg2));
3065 break;
3066 case REG_AR + AR_PFS:
3067 add_unwind_entry (output_pfs_when ());
3068 if (! (unwind.prologue_mask & 4))
3069 add_unwind_entry (output_pfs_gr (reg2));
3070 break;
3071 case REG_AR + AR_LC:
3072 add_unwind_entry (output_lc_when ());
3073 add_unwind_entry (output_lc_gr (reg2));
3074 break;
3075 case REG_BR:
3076 add_unwind_entry (output_rp_when ());
3077 if (! (unwind.prologue_mask & 8))
3078 add_unwind_entry (output_rp_gr (reg2));
3079 break;
3080 case REG_PR:
3081 add_unwind_entry (output_preds_when ());
3082 if (! (unwind.prologue_mask & 1))
3083 add_unwind_entry (output_preds_gr (reg2));
3084 break;
3085 case REG_PRIUNAT:
3086 add_unwind_entry (output_priunat_when_gr ());
3087 add_unwind_entry (output_priunat_gr (reg2));
3088 break;
3089 default:
3090 as_bad ("First operand not a valid register");
3091 }
3092 }
3093 else
3094 as_bad (" Second operand not a valid register");
3095 }
3096 else
3097 as_bad ("First operand not a register");
3098 }
3099
3100 static void
3101 dot_restore (dummy)
3102 int dummy ATTRIBUTE_UNUSED;
3103 {
3104 expressionS e1, e2;
3105 unsigned long ecount; /* # of _additional_ regions to pop */
3106 int sep;
3107
3108 sep = parse_operand (&e1);
3109 if (e1.X_op != O_register || e1.X_add_number != REG_GR + 12)
3110 {
3111 as_bad ("First operand to .restore must be stack pointer (sp)");
3112 return;
3113 }
3114
3115 if (sep == ',')
3116 {
3117 parse_operand (&e2);
3118 if (e2.X_op != O_constant || e2.X_add_number < 0)
3119 {
3120 as_bad ("Second operand to .restore must be a constant >= 0");
3121 return;
3122 }
3123 ecount = e2.X_add_number;
3124 }
3125 else
3126 ecount = unwind.prologue_count - 1;
3127 add_unwind_entry (output_epilogue (ecount));
3128
3129 if (ecount < unwind.prologue_count)
3130 unwind.prologue_count -= ecount + 1;
3131 else
3132 unwind.prologue_count = 0;
3133 }
3134
3135 static void
3136 dot_restorereg (dummy)
3137 int dummy ATTRIBUTE_UNUSED;
3138 {
3139 unsigned int ab, reg;
3140 expressionS e;
3141
3142 parse_operand (&e);
3143
3144 if (!convert_expr_to_ab_reg (&e, &ab, &reg))
3145 {
3146 as_bad ("First operand to .restorereg must be a preserved register");
3147 return;
3148 }
3149 add_unwind_entry (output_spill_reg (ab, reg, 0, 0));
3150 }
3151
3152 static void
3153 dot_restorereg_p (dummy)
3154 int dummy ATTRIBUTE_UNUSED;
3155 {
3156 unsigned int qp, ab, reg;
3157 expressionS e1, e2;
3158 int sep;
3159
3160 sep = parse_operand (&e1);
3161 if (sep != ',')
3162 {
3163 as_bad ("No second operand to .restorereg.p");
3164 return;
3165 }
3166
3167 parse_operand (&e2);
3168
3169 qp = e1.X_add_number - REG_P;
3170 if (e1.X_op != O_register || qp > 63)
3171 {
3172 as_bad ("First operand to .restorereg.p must be a predicate");
3173 return;
3174 }
3175
3176 if (!convert_expr_to_ab_reg (&e2, &ab, &reg))
3177 {
3178 as_bad ("Second operand to .restorereg.p must be a preserved register");
3179 return;
3180 }
3181 add_unwind_entry (output_spill_reg_p (ab, reg, 0, 0, qp));
3182 }
3183
3184 static int
3185 generate_unwind_image (text_name)
3186 const char *text_name;
3187 {
3188 int size;
3189 unsigned char *unw_rec;
3190
3191 /* Force out pending instructions, to make sure all unwind records have
3192 a valid slot_number field. */
3193 ia64_flush_insns ();
3194
3195 /* Generate the unwind record. */
3196 size = output_unw_records (unwind.list, (void **) &unw_rec);
3197 if (size % md.pointer_size != 0)
3198 as_bad ("Unwind record is not a multiple of %d bytes.", md.pointer_size);
3199
3200 /* If there are unwind records, switch sections, and output the info. */
3201 if (size != 0)
3202 {
3203 unsigned char *where;
3204 char *sec_name;
3205 expressionS exp;
3206
3207 make_unw_section_name (SPECIAL_SECTION_UNWIND_INFO, text_name, sec_name);
3208 set_section (sec_name);
3209 bfd_set_section_flags (stdoutput, now_seg,
3210 SEC_LOAD | SEC_ALLOC | SEC_READONLY);
3211
3212 /* Make sure the section has 4 byte alignment for ILP32 and
3213 8 byte alignment for LP64. */
3214 frag_align (md.pointer_size_shift, 0, 0);
3215 record_alignment (now_seg, md.pointer_size_shift);
3216
3217 /* Set expression which points to start of unwind descriptor area. */
3218 unwind.info = expr_build_dot ();
3219
3220 where = (unsigned char *) frag_more (size);
3221
3222 /* Issue a label for this address, and keep track of it to put it
3223 in the unwind section. */
3224
3225 /* Copy the information from the unwind record into this section. The
3226 data is already in the correct byte order. */
3227 memcpy (where, unw_rec, size);
3228
3229 /* Add the personality address to the image. */
3230 if (unwind.personality_routine != 0)
3231 {
3232 exp.X_op = O_symbol;
3233 exp.X_add_symbol = unwind.personality_routine;
3234 exp.X_add_number = 0;
3235 fix_new_exp (frag_now, frag_now_fix () - 8, 8,
3236 &exp, 0, BFD_RELOC_IA64_LTOFF_FPTR64LSB);
3237 unwind.personality_routine = 0;
3238 }
3239 }
3240
3241 free_list_records (unwind.list);
3242 unwind.list = unwind.tail = unwind.current_entry = NULL;
3243
3244 return size;
3245 }
3246
3247 static void
3248 dot_handlerdata (dummy)
3249 int dummy ATTRIBUTE_UNUSED;
3250 {
3251 const char *text_name = segment_name (now_seg);
3252
3253 /* If text section name starts with ".text" (which it should),
3254 strip this prefix off. */
3255 if (strcmp (text_name, ".text") == 0)
3256 text_name = "";
3257
3258 unwind.force_unwind_entry = 1;
3259
3260 /* Remember which segment we're in so we can switch back after .endp */
3261 unwind.saved_text_seg = now_seg;
3262 unwind.saved_text_subseg = now_subseg;
3263
3264 /* Generate unwind info into unwind-info section and then leave that
3265 section as the currently active one so dataXX directives go into
3266 the language specific data area of the unwind info block. */
3267 generate_unwind_image (text_name);
3268 demand_empty_rest_of_line ();
3269 }
3270
3271 static void
3272 dot_unwentry (dummy)
3273 int dummy ATTRIBUTE_UNUSED;
3274 {
3275 unwind.force_unwind_entry = 1;
3276 demand_empty_rest_of_line ();
3277 }
3278
3279 static void
3280 dot_altrp (dummy)
3281 int dummy ATTRIBUTE_UNUSED;
3282 {
3283 expressionS e;
3284 unsigned reg;
3285
3286 parse_operand (&e);
3287 reg = e.X_add_number - REG_BR;
3288 if (e.X_op == O_register && reg < 8)
3289 add_unwind_entry (output_rp_br (reg));
3290 else
3291 as_bad ("First operand not a valid branch register");
3292 }
3293
3294 static void
3295 dot_savemem (psprel)
3296 int psprel;
3297 {
3298 expressionS e1, e2;
3299 int sep;
3300 int reg1, val;
3301
3302 sep = parse_operand (&e1);
3303 if (sep != ',')
3304 as_bad ("No second operand to .save%ssp", psprel ? "p" : "");
3305 sep = parse_operand (&e2);
3306
3307 reg1 = e1.X_add_number;
3308 val = e2.X_add_number;
3309
3310 /* Make sure its a valid ar.xxx reg, OR its br0, aka 'rp'. */
3311 if (e1.X_op == O_register)
3312 {
3313 if (e2.X_op == O_constant)
3314 {
3315 switch (reg1)
3316 {
3317 case REG_AR + AR_BSP:
3318 add_unwind_entry (output_bsp_when ());
3319 add_unwind_entry ((psprel
3320 ? output_bsp_psprel
3321 : output_bsp_sprel) (val));
3322 break;
3323 case REG_AR + AR_BSPSTORE:
3324 add_unwind_entry (output_bspstore_when ());
3325 add_unwind_entry ((psprel
3326 ? output_bspstore_psprel
3327 : output_bspstore_sprel) (val));
3328 break;
3329 case REG_AR + AR_RNAT:
3330 add_unwind_entry (output_rnat_when ());
3331 add_unwind_entry ((psprel
3332 ? output_rnat_psprel
3333 : output_rnat_sprel) (val));
3334 break;
3335 case REG_AR + AR_UNAT:
3336 add_unwind_entry (output_unat_when ());
3337 add_unwind_entry ((psprel
3338 ? output_unat_psprel
3339 : output_unat_sprel) (val));
3340 break;
3341 case REG_AR + AR_FPSR:
3342 add_unwind_entry (output_fpsr_when ());
3343 add_unwind_entry ((psprel
3344 ? output_fpsr_psprel
3345 : output_fpsr_sprel) (val));
3346 break;
3347 case REG_AR + AR_PFS:
3348 add_unwind_entry (output_pfs_when ());
3349 add_unwind_entry ((psprel
3350 ? output_pfs_psprel
3351 : output_pfs_sprel) (val));
3352 break;
3353 case REG_AR + AR_LC:
3354 add_unwind_entry (output_lc_when ());
3355 add_unwind_entry ((psprel
3356 ? output_lc_psprel
3357 : output_lc_sprel) (val));
3358 break;
3359 case REG_BR:
3360 add_unwind_entry (output_rp_when ());
3361 add_unwind_entry ((psprel
3362 ? output_rp_psprel
3363 : output_rp_sprel) (val));
3364 break;
3365 case REG_PR:
3366 add_unwind_entry (output_preds_when ());
3367 add_unwind_entry ((psprel
3368 ? output_preds_psprel
3369 : output_preds_sprel) (val));
3370 break;
3371 case REG_PRIUNAT:
3372 add_unwind_entry (output_priunat_when_mem ());
3373 add_unwind_entry ((psprel
3374 ? output_priunat_psprel
3375 : output_priunat_sprel) (val));
3376 break;
3377 default:
3378 as_bad ("First operand not a valid register");
3379 }
3380 }
3381 else
3382 as_bad (" Second operand not a valid constant");
3383 }
3384 else
3385 as_bad ("First operand not a register");
3386 }
3387
3388 static void
3389 dot_saveg (dummy)
3390 int dummy ATTRIBUTE_UNUSED;
3391 {
3392 expressionS e1, e2;
3393 int sep;
3394 sep = parse_operand (&e1);
3395 if (sep == ',')
3396 parse_operand (&e2);
3397
3398 if (e1.X_op != O_constant)
3399 as_bad ("First operand to .save.g must be a constant.");
3400 else
3401 {
3402 int grmask = e1.X_add_number;
3403 if (sep != ',')
3404 add_unwind_entry (output_gr_mem (grmask));
3405 else
3406 {
3407 int reg = e2.X_add_number - REG_GR;
3408 if (e2.X_op == O_register && reg >= 0 && reg < 128)
3409 add_unwind_entry (output_gr_gr (grmask, reg));
3410 else
3411 as_bad ("Second operand is an invalid register.");
3412 }
3413 }
3414 }
3415
3416 static void
3417 dot_savef (dummy)
3418 int dummy ATTRIBUTE_UNUSED;
3419 {
3420 expressionS e1;
3421 int sep;
3422 sep = parse_operand (&e1);
3423
3424 if (e1.X_op != O_constant)
3425 as_bad ("Operand to .save.f must be a constant.");
3426 else
3427 add_unwind_entry (output_fr_mem (e1.X_add_number));
3428 }
3429
3430 static void
3431 dot_saveb (dummy)
3432 int dummy ATTRIBUTE_UNUSED;
3433 {
3434 expressionS e1, e2;
3435 unsigned int reg;
3436 unsigned char sep;
3437 int brmask;
3438
3439 sep = parse_operand (&e1);
3440 if (e1.X_op != O_constant)
3441 {
3442 as_bad ("First operand to .save.b must be a constant.");
3443 return;
3444 }
3445 brmask = e1.X_add_number;
3446
3447 if (sep == ',')
3448 {
3449 sep = parse_operand (&e2);
3450 reg = e2.X_add_number - REG_GR;
3451 if (e2.X_op != O_register || reg > 127)
3452 {
3453 as_bad ("Second operand to .save.b must be a general register.");
3454 return;
3455 }
3456 add_unwind_entry (output_br_gr (brmask, e2.X_add_number));
3457 }
3458 else
3459 add_unwind_entry (output_br_mem (brmask));
3460
3461 if (!is_end_of_line[sep] && !is_it_end_of_statement ())
3462 ignore_rest_of_line ();
3463 }
3464
3465 static void
3466 dot_savegf (dummy)
3467 int dummy ATTRIBUTE_UNUSED;
3468 {
3469 expressionS e1, e2;
3470 int sep;
3471 sep = parse_operand (&e1);
3472 if (sep == ',')
3473 parse_operand (&e2);
3474
3475 if (e1.X_op != O_constant || sep != ',' || e2.X_op != O_constant)
3476 as_bad ("Both operands of .save.gf must be constants.");
3477 else
3478 {
3479 int grmask = e1.X_add_number;
3480 int frmask = e2.X_add_number;
3481 add_unwind_entry (output_frgr_mem (grmask, frmask));
3482 }
3483 }
3484
3485 static void
3486 dot_spill (dummy)
3487 int dummy ATTRIBUTE_UNUSED;
3488 {
3489 expressionS e;
3490 unsigned char sep;
3491
3492 sep = parse_operand (&e);
3493 if (!is_end_of_line[sep] && !is_it_end_of_statement ())
3494 ignore_rest_of_line ();
3495
3496 if (e.X_op != O_constant)
3497 as_bad ("Operand to .spill must be a constant");
3498 else
3499 add_unwind_entry (output_spill_base (e.X_add_number));
3500 }
3501
3502 static void
3503 dot_spillreg (dummy)
3504 int dummy ATTRIBUTE_UNUSED;
3505 {
3506 int sep, ab, xy, reg, treg;
3507 expressionS e1, e2;
3508
3509 sep = parse_operand (&e1);
3510 if (sep != ',')
3511 {
3512 as_bad ("No second operand to .spillreg");
3513 return;
3514 }
3515
3516 parse_operand (&e2);
3517
3518 if (!convert_expr_to_ab_reg (&e1, &ab, &reg))
3519 {
3520 as_bad ("First operand to .spillreg must be a preserved register");
3521 return;
3522 }
3523
3524 if (!convert_expr_to_xy_reg (&e2, &xy, &treg))
3525 {
3526 as_bad ("Second operand to .spillreg must be a register");
3527 return;
3528 }
3529
3530 add_unwind_entry (output_spill_reg (ab, reg, treg, xy));
3531 }
3532
3533 static void
3534 dot_spillmem (psprel)
3535 int psprel;
3536 {
3537 expressionS e1, e2;
3538 int sep, ab, reg;
3539
3540 sep = parse_operand (&e1);
3541 if (sep != ',')
3542 {
3543 as_bad ("Second operand missing");
3544 return;
3545 }
3546
3547 parse_operand (&e2);
3548
3549 if (!convert_expr_to_ab_reg (&e1, &ab, &reg))
3550 {
3551 as_bad ("First operand to .spill%s must be a preserved register",
3552 psprel ? "psp" : "sp");
3553 return;
3554 }
3555
3556 if (e2.X_op != O_constant)
3557 {
3558 as_bad ("Second operand to .spill%s must be a constant",
3559 psprel ? "psp" : "sp");
3560 return;
3561 }
3562
3563 if (psprel)
3564 add_unwind_entry (output_spill_psprel (ab, reg, e2.X_add_number));
3565 else
3566 add_unwind_entry (output_spill_sprel (ab, reg, e2.X_add_number));
3567 }
3568
3569 static void
3570 dot_spillreg_p (dummy)
3571 int dummy ATTRIBUTE_UNUSED;
3572 {
3573 int sep, ab, xy, reg, treg;
3574 expressionS e1, e2, e3;
3575 unsigned int qp;
3576
3577 sep = parse_operand (&e1);
3578 if (sep != ',')
3579 {
3580 as_bad ("No second and third operand to .spillreg.p");
3581 return;
3582 }
3583
3584 sep = parse_operand (&e2);
3585 if (sep != ',')
3586 {
3587 as_bad ("No third operand to .spillreg.p");
3588 return;
3589 }
3590
3591 parse_operand (&e3);
3592
3593 qp = e1.X_add_number - REG_P;
3594
3595 if (e1.X_op != O_register || qp > 63)
3596 {
3597 as_bad ("First operand to .spillreg.p must be a predicate");
3598 return;
3599 }
3600
3601 if (!convert_expr_to_ab_reg (&e2, &ab, &reg))
3602 {
3603 as_bad ("Second operand to .spillreg.p must be a preserved register");
3604 return;
3605 }
3606
3607 if (!convert_expr_to_xy_reg (&e3, &xy, &treg))
3608 {
3609 as_bad ("Third operand to .spillreg.p must be a register");
3610 return;
3611 }
3612
3613 add_unwind_entry (output_spill_reg_p (ab, reg, treg, xy, qp));
3614 }
3615
3616 static void
3617 dot_spillmem_p (psprel)
3618 int psprel;
3619 {
3620 expressionS e1, e2, e3;
3621 int sep, ab, reg;
3622 unsigned int qp;
3623
3624 sep = parse_operand (&e1);
3625 if (sep != ',')
3626 {
3627 as_bad ("Second operand missing");
3628 return;
3629 }
3630
3631 parse_operand (&e2);
3632 if (sep != ',')
3633 {
3634 as_bad ("Second operand missing");
3635 return;
3636 }
3637
3638 parse_operand (&e3);
3639
3640 qp = e1.X_add_number - REG_P;
3641 if (e1.X_op != O_register || qp > 63)
3642 {
3643 as_bad ("First operand to .spill%s_p must be a predicate",
3644 psprel ? "psp" : "sp");
3645 return;
3646 }
3647
3648 if (!convert_expr_to_ab_reg (&e2, &ab, &reg))
3649 {
3650 as_bad ("Second operand to .spill%s_p must be a preserved register",
3651 psprel ? "psp" : "sp");
3652 return;
3653 }
3654
3655 if (e3.X_op != O_constant)
3656 {
3657 as_bad ("Third operand to .spill%s_p must be a constant",
3658 psprel ? "psp" : "sp");
3659 return;
3660 }
3661
3662 if (psprel)
3663 add_unwind_entry (output_spill_psprel_p (ab, reg, e3.X_add_number, qp));
3664 else
3665 add_unwind_entry (output_spill_sprel_p (ab, reg, e3.X_add_number, qp));
3666 }
3667
3668 static void
3669 dot_label_state (dummy)
3670 int dummy ATTRIBUTE_UNUSED;
3671 {
3672 expressionS e;
3673
3674 parse_operand (&e);
3675 if (e.X_op != O_constant)
3676 {
3677 as_bad ("Operand to .label_state must be a constant");
3678 return;
3679 }
3680 add_unwind_entry (output_label_state (e.X_add_number));
3681 }
3682
3683 static void
3684 dot_copy_state (dummy)
3685 int dummy ATTRIBUTE_UNUSED;
3686 {
3687 expressionS e;
3688
3689 parse_operand (&e);
3690 if (e.X_op != O_constant)
3691 {
3692 as_bad ("Operand to .copy_state must be a constant");
3693 return;
3694 }
3695 add_unwind_entry (output_copy_state (e.X_add_number));
3696 }
3697
3698 static void
3699 dot_unwabi (dummy)
3700 int dummy ATTRIBUTE_UNUSED;
3701 {
3702 expressionS e1, e2;
3703 unsigned char sep;
3704
3705 sep = parse_operand (&e1);
3706 if (sep != ',')
3707 {
3708 as_bad ("Second operand to .unwabi missing");
3709 return;
3710 }
3711 sep = parse_operand (&e2);
3712 if (!is_end_of_line[sep] && !is_it_end_of_statement ())
3713 ignore_rest_of_line ();
3714
3715 if (e1.X_op != O_constant)
3716 {
3717 as_bad ("First operand to .unwabi must be a constant");
3718 return;
3719 }
3720
3721 if (e2.X_op != O_constant)
3722 {
3723 as_bad ("Second operand to .unwabi must be a constant");
3724 return;
3725 }
3726
3727 add_unwind_entry (output_unwabi (e1.X_add_number, e2.X_add_number));
3728 }
3729
3730 static void
3731 dot_personality (dummy)
3732 int dummy ATTRIBUTE_UNUSED;
3733 {
3734 char *name, *p, c;
3735 SKIP_WHITESPACE ();
3736 name = input_line_pointer;
3737 c = get_symbol_end ();
3738 p = input_line_pointer;
3739 unwind.personality_routine = symbol_find_or_make (name);
3740 unwind.force_unwind_entry = 1;
3741 *p = c;
3742 SKIP_WHITESPACE ();
3743 demand_empty_rest_of_line ();
3744 }
3745
3746 static void
3747 dot_proc (dummy)
3748 int dummy ATTRIBUTE_UNUSED;
3749 {
3750 char *name, *p, c;
3751 symbolS *sym;
3752
3753 unwind.proc_start = expr_build_dot ();
3754 /* Parse names of main and alternate entry points and mark them as
3755 function symbols: */
3756 while (1)
3757 {
3758 SKIP_WHITESPACE ();
3759 name = input_line_pointer;
3760 c = get_symbol_end ();
3761 p = input_line_pointer;
3762 sym = symbol_find_or_make (name);
3763 if (unwind.proc_start == 0)
3764 {
3765 unwind.proc_start = sym;
3766 }
3767 symbol_get_bfdsym (sym)->flags |= BSF_FUNCTION;
3768 *p = c;
3769 SKIP_WHITESPACE ();
3770 if (*input_line_pointer != ',')
3771 break;
3772 ++input_line_pointer;
3773 }
3774 demand_empty_rest_of_line ();
3775 ia64_do_align (16);
3776
3777 unwind.prologue_count = 0;
3778 unwind.list = unwind.tail = unwind.current_entry = NULL;
3779 unwind.personality_routine = 0;
3780 }
3781
3782 static void
3783 dot_body (dummy)
3784 int dummy ATTRIBUTE_UNUSED;
3785 {
3786 unwind.prologue = 0;
3787 unwind.prologue_mask = 0;
3788
3789 add_unwind_entry (output_body ());
3790 demand_empty_rest_of_line ();
3791 }
3792
3793 static void
3794 dot_prologue (dummy)
3795 int dummy ATTRIBUTE_UNUSED;
3796 {
3797 unsigned char sep;
3798 int mask = 0, grsave = 0;
3799
3800 if (!is_it_end_of_statement ())
3801 {
3802 expressionS e1, e2;
3803 sep = parse_operand (&e1);
3804 if (sep != ',')
3805 as_bad ("No second operand to .prologue");
3806 sep = parse_operand (&e2);
3807 if (!is_end_of_line[sep] && !is_it_end_of_statement ())
3808 ignore_rest_of_line ();
3809
3810 if (e1.X_op == O_constant)
3811 {
3812 mask = e1.X_add_number;
3813
3814 if (e2.X_op == O_constant)
3815 grsave = e2.X_add_number;
3816 else if (e2.X_op == O_register
3817 && (grsave = e2.X_add_number - REG_GR) < 128)
3818 ;
3819 else
3820 as_bad ("Second operand not a constant or general register");
3821
3822 add_unwind_entry (output_prologue_gr (mask, grsave));
3823 }
3824 else
3825 as_bad ("First operand not a constant");
3826 }
3827 else
3828 add_unwind_entry (output_prologue ());
3829
3830 unwind.prologue = 1;
3831 unwind.prologue_mask = mask;
3832 ++unwind.prologue_count;
3833 }
3834
3835 static void
3836 dot_endp (dummy)
3837 int dummy ATTRIBUTE_UNUSED;
3838 {
3839 expressionS e;
3840 unsigned char *ptr;
3841 int bytes_per_address;
3842 long where;
3843 segT saved_seg;
3844 subsegT saved_subseg;
3845 const char *sec_name, *text_name;
3846 char *name, *p, c;
3847 symbolS *sym;
3848
3849 if (unwind.saved_text_seg)
3850 {
3851 saved_seg = unwind.saved_text_seg;
3852 saved_subseg = unwind.saved_text_subseg;
3853 unwind.saved_text_seg = NULL;
3854 }
3855 else
3856 {
3857 saved_seg = now_seg;
3858 saved_subseg = now_subseg;
3859 }
3860
3861 /*
3862 Use a slightly ugly scheme to derive the unwind section names from
3863 the text section name:
3864
3865 text sect. unwind table sect.
3866 name: name: comments:
3867 ---------- ----------------- --------------------------------
3868 .text .IA_64.unwind
3869 .text.foo .IA_64.unwind.text.foo
3870 .foo .IA_64.unwind.foo
3871 .gnu.linkonce.t.foo
3872 .gnu.linkonce.ia64unw.foo
3873 _info .IA_64.unwind_info gas issues error message (ditto)
3874 _infoFOO .IA_64.unwind_infoFOO gas issues error message (ditto)
3875
3876 This mapping is done so that:
3877
3878 (a) An object file with unwind info only in .text will use
3879 unwind section names .IA_64.unwind and .IA_64.unwind_info.
3880 This follows the letter of the ABI and also ensures backwards
3881 compatibility with older toolchains.
3882
3883 (b) An object file with unwind info in multiple text sections
3884 will use separate unwind sections for each text section.
3885 This allows us to properly set the "sh_info" and "sh_link"
3886 fields in SHT_IA_64_UNWIND as required by the ABI and also
3887 lets GNU ld support programs with multiple segments
3888 containing unwind info (as might be the case for certain
3889 embedded applications).
3890
3891 (c) An error is issued if there would be a name clash.
3892 */
3893 text_name = segment_name (saved_seg);
3894 if (strncmp (text_name, "_info", 5) == 0)
3895 {
3896 as_bad ("Illegal section name `%s' (causes unwind section name clash)",
3897 text_name);
3898 ignore_rest_of_line ();
3899 return;
3900 }
3901 if (strcmp (text_name, ".text") == 0)
3902 text_name = "";
3903
3904 insn_group_break (1, 0, 0);
3905
3906 /* If there wasn't a .handlerdata, we haven't generated an image yet. */
3907 if (!unwind.info)
3908 generate_unwind_image (text_name);
3909
3910 if (unwind.info || unwind.force_unwind_entry)
3911 {
3912 subseg_set (md.last_text_seg, 0);
3913 unwind.proc_end = expr_build_dot ();
3914
3915 make_unw_section_name (SPECIAL_SECTION_UNWIND, text_name, sec_name);
3916 set_section ((char *) sec_name);
3917 bfd_set_section_flags (stdoutput, now_seg,
3918 SEC_LOAD | SEC_ALLOC | SEC_READONLY);
3919
3920 /* Make sure that section has 4 byte alignment for ILP32 and
3921 8 byte alignment for LP64. */
3922 record_alignment (now_seg, md.pointer_size_shift);
3923
3924 /* Need space for 3 pointers for procedure start, procedure end,
3925 and unwind info. */
3926 ptr = frag_more (3 * md.pointer_size);
3927 where = frag_now_fix () - (3 * md.pointer_size);
3928 bytes_per_address = bfd_arch_bits_per_address (stdoutput) / 8;
3929
3930 /* Issue the values of a) Proc Begin, b) Proc End, c) Unwind Record. */
3931 e.X_op = O_pseudo_fixup;
3932 e.X_op_symbol = pseudo_func[FUNC_SEG_RELATIVE].u.sym;
3933 e.X_add_number = 0;
3934 e.X_add_symbol = unwind.proc_start;
3935 ia64_cons_fix_new (frag_now, where, bytes_per_address, &e);
3936
3937 e.X_op = O_pseudo_fixup;
3938 e.X_op_symbol = pseudo_func[FUNC_SEG_RELATIVE].u.sym;
3939 e.X_add_number = 0;
3940 e.X_add_symbol = unwind.proc_end;
3941 ia64_cons_fix_new (frag_now, where + bytes_per_address,
3942 bytes_per_address, &e);
3943
3944 if (unwind.info)
3945 {
3946 e.X_op = O_pseudo_fixup;
3947 e.X_op_symbol = pseudo_func[FUNC_SEG_RELATIVE].u.sym;
3948 e.X_add_number = 0;
3949 e.X_add_symbol = unwind.info;
3950 ia64_cons_fix_new (frag_now, where + (bytes_per_address * 2),
3951 bytes_per_address, &e);
3952 }
3953 else
3954 md_number_to_chars (ptr + (bytes_per_address * 2), 0,
3955 bytes_per_address);
3956
3957 }
3958 subseg_set (saved_seg, saved_subseg);
3959
3960 /* Parse names of main and alternate entry points and set symbol sizes. */
3961 while (1)
3962 {
3963 SKIP_WHITESPACE ();
3964 name = input_line_pointer;
3965 c = get_symbol_end ();
3966 p = input_line_pointer;
3967 sym = symbol_find (name);
3968 if (sym && unwind.proc_start
3969 && (symbol_get_bfdsym (sym)->flags & BSF_FUNCTION)
3970 && S_GET_SIZE (sym) == 0 && symbol_get_obj (sym)->size == NULL)
3971 {
3972 fragS *fr = symbol_get_frag (unwind.proc_start);
3973 fragS *frag = symbol_get_frag (sym);
3974
3975 /* Check whether the function label is at or beyond last
3976 .proc directive. */
3977 while (fr && fr != frag)
3978 fr = fr->fr_next;
3979 if (fr)
3980 {
3981 if (frag == frag_now && SEG_NORMAL (now_seg))
3982 S_SET_SIZE (sym, frag_now_fix () - S_GET_VALUE (sym));
3983 else
3984 {
3985 symbol_get_obj (sym)->size =
3986 (expressionS *) xmalloc (sizeof (expressionS));
3987 symbol_get_obj (sym)->size->X_op = O_subtract;
3988 symbol_get_obj (sym)->size->X_add_symbol
3989 = symbol_new (FAKE_LABEL_NAME, now_seg,
3990 frag_now_fix (), frag_now);
3991 symbol_get_obj (sym)->size->X_op_symbol = sym;
3992 symbol_get_obj (sym)->size->X_add_number = 0;
3993 }
3994 }
3995 }
3996 *p = c;
3997 SKIP_WHITESPACE ();
3998 if (*input_line_pointer != ',')
3999 break;
4000 ++input_line_pointer;
4001 }
4002 demand_empty_rest_of_line ();
4003 unwind.proc_start = unwind.proc_end = unwind.info = 0;
4004 }
4005
4006 static void
4007 dot_template (template)
4008 int template;
4009 {
4010 CURR_SLOT.user_template = template;
4011 }
4012
4013 static void
4014 dot_regstk (dummy)
4015 int dummy ATTRIBUTE_UNUSED;
4016 {
4017 int ins, locs, outs, rots;
4018
4019 if (is_it_end_of_statement ())
4020 ins = locs = outs = rots = 0;
4021 else
4022 {
4023 ins = get_absolute_expression ();
4024 if (*input_line_pointer++ != ',')
4025 goto err;
4026 locs = get_absolute_expression ();
4027 if (*input_line_pointer++ != ',')
4028 goto err;
4029 outs = get_absolute_expression ();
4030 if (*input_line_pointer++ != ',')
4031 goto err;
4032 rots = get_absolute_expression ();
4033 }
4034 set_regstack (ins, locs, outs, rots);
4035 return;
4036
4037 err:
4038 as_bad ("Comma expected");
4039 ignore_rest_of_line ();
4040 }
4041
4042 static void
4043 dot_rot (type)
4044 int type;
4045 {
4046 unsigned num_regs, num_alloced = 0;
4047 struct dynreg **drpp, *dr;
4048 int ch, base_reg = 0;
4049 char *name, *start;
4050 size_t len;
4051
4052 switch (type)
4053 {
4054 case DYNREG_GR: base_reg = REG_GR + 32; break;
4055 case DYNREG_FR: base_reg = REG_FR + 32; break;
4056 case DYNREG_PR: base_reg = REG_P + 16; break;
4057 default: break;
4058 }
4059
4060 /* First, remove existing names from hash table. */
4061 for (dr = md.dynreg[type]; dr && dr->num_regs; dr = dr->next)
4062 {
4063 hash_delete (md.dynreg_hash, dr->name);
4064 dr->num_regs = 0;
4065 }
4066
4067 drpp = &md.dynreg[type];
4068 while (1)
4069 {
4070 start = input_line_pointer;
4071 ch = get_symbol_end ();
4072 *input_line_pointer = ch;
4073 len = (input_line_pointer - start);
4074
4075 SKIP_WHITESPACE ();
4076 if (*input_line_pointer != '[')
4077 {
4078 as_bad ("Expected '['");
4079 goto err;
4080 }
4081 ++input_line_pointer; /* skip '[' */
4082
4083 num_regs = get_absolute_expression ();
4084
4085 if (*input_line_pointer++ != ']')
4086 {
4087 as_bad ("Expected ']'");
4088 goto err;
4089 }
4090 SKIP_WHITESPACE ();
4091
4092 num_alloced += num_regs;
4093 switch (type)
4094 {
4095 case DYNREG_GR:
4096 if (num_alloced > md.rot.num_regs)
4097 {
4098 as_bad ("Used more than the declared %d rotating registers",
4099 md.rot.num_regs);
4100 goto err;
4101 }
4102 break;
4103 case DYNREG_FR:
4104 if (num_alloced > 96)
4105 {
4106 as_bad ("Used more than the available 96 rotating registers");
4107 goto err;
4108 }
4109 break;
4110 case DYNREG_PR:
4111 if (num_alloced > 48)
4112 {
4113 as_bad ("Used more than the available 48 rotating registers");
4114 goto err;
4115 }
4116 break;
4117
4118 default:
4119 break;
4120 }
4121
4122 name = obstack_alloc (&notes, len + 1);
4123 memcpy (name, start, len);
4124 name[len] = '\0';
4125
4126 if (!*drpp)
4127 {
4128 *drpp = obstack_alloc (&notes, sizeof (*dr));
4129 memset (*drpp, 0, sizeof (*dr));
4130 }
4131
4132 dr = *drpp;
4133 dr->name = name;
4134 dr->num_regs = num_regs;
4135 dr->base = base_reg;
4136 drpp = &dr->next;
4137 base_reg += num_regs;
4138
4139 if (hash_insert (md.dynreg_hash, name, dr))
4140 {
4141 as_bad ("Attempt to redefine register set `%s'", name);
4142 goto err;
4143 }
4144
4145 if (*input_line_pointer != ',')
4146 break;
4147 ++input_line_pointer; /* skip comma */
4148 SKIP_WHITESPACE ();
4149 }
4150 demand_empty_rest_of_line ();
4151 return;
4152
4153 err:
4154 ignore_rest_of_line ();
4155 }
4156
4157 static void
4158 dot_byteorder (byteorder)
4159 int byteorder;
4160 {
4161 target_big_endian = byteorder;
4162 }
4163
4164 static void
4165 dot_psr (dummy)
4166 int dummy ATTRIBUTE_UNUSED;
4167 {
4168 char *option;
4169 int ch;
4170
4171 while (1)
4172 {
4173 option = input_line_pointer;
4174 ch = get_symbol_end ();
4175 if (strcmp (option, "lsb") == 0)
4176 md.flags &= ~EF_IA_64_BE;
4177 else if (strcmp (option, "msb") == 0)
4178 md.flags |= EF_IA_64_BE;
4179 else if (strcmp (option, "abi32") == 0)
4180 md.flags &= ~EF_IA_64_ABI64;
4181 else if (strcmp (option, "abi64") == 0)
4182 md.flags |= EF_IA_64_ABI64;
4183 else
4184 as_bad ("Unknown psr option `%s'", option);
4185 *input_line_pointer = ch;
4186
4187 SKIP_WHITESPACE ();
4188 if (*input_line_pointer != ',')
4189 break;
4190
4191 ++input_line_pointer;
4192 SKIP_WHITESPACE ();
4193 }
4194 demand_empty_rest_of_line ();
4195 }
4196
4197 static void
4198 dot_alias (dummy)
4199 int dummy ATTRIBUTE_UNUSED;
4200 {
4201 as_bad (".alias not implemented yet");
4202 }
4203
4204 static void
4205 dot_ln (dummy)
4206 int dummy ATTRIBUTE_UNUSED;
4207 {
4208 new_logical_line (0, get_absolute_expression ());
4209 demand_empty_rest_of_line ();
4210 }
4211
4212 static char *
4213 parse_section_name ()
4214 {
4215 char *name;
4216 int len;
4217
4218 SKIP_WHITESPACE ();
4219 if (*input_line_pointer != '"')
4220 {
4221 as_bad ("Missing section name");
4222 ignore_rest_of_line ();
4223 return 0;
4224 }
4225 name = demand_copy_C_string (&len);
4226 if (!name)
4227 {
4228 ignore_rest_of_line ();
4229 return 0;
4230 }
4231 SKIP_WHITESPACE ();
4232 if (*input_line_pointer != ',')
4233 {
4234 as_bad ("Comma expected after section name");
4235 ignore_rest_of_line ();
4236 return 0;
4237 }
4238 ++input_line_pointer; /* skip comma */
4239 return name;
4240 }
4241
4242 static void
4243 dot_xdata (size)
4244 int size;
4245 {
4246 char *name = parse_section_name ();
4247 if (!name)
4248 return;
4249
4250 md.keep_pending_output = 1;
4251 set_section (name);
4252 cons (size);
4253 obj_elf_previous (0);
4254 md.keep_pending_output = 0;
4255 }
4256
4257 /* Why doesn't float_cons() call md_cons_align() the way cons() does? */
4258
4259 static void
4260 stmt_float_cons (kind)
4261 int kind;
4262 {
4263 size_t size;
4264
4265 switch (kind)
4266 {
4267 case 'd': size = 8; break;
4268 case 'x': size = 10; break;
4269
4270 case 'f':
4271 default:
4272 size = 4;
4273 break;
4274 }
4275 ia64_do_align (size);
4276 float_cons (kind);
4277 }
4278
4279 static void
4280 stmt_cons_ua (size)
4281 int size;
4282 {
4283 int saved_auto_align = md.auto_align;
4284
4285 md.auto_align = 0;
4286 cons (size);
4287 md.auto_align = saved_auto_align;
4288 }
4289
4290 static void
4291 dot_xfloat_cons (kind)
4292 int kind;
4293 {
4294 char *name = parse_section_name ();
4295 if (!name)
4296 return;
4297
4298 md.keep_pending_output = 1;
4299 set_section (name);
4300 stmt_float_cons (kind);
4301 obj_elf_previous (0);
4302 md.keep_pending_output = 0;
4303 }
4304
4305 static void
4306 dot_xstringer (zero)
4307 int zero;
4308 {
4309 char *name = parse_section_name ();
4310 if (!name)
4311 return;
4312
4313 md.keep_pending_output = 1;
4314 set_section (name);
4315 stringer (zero);
4316 obj_elf_previous (0);
4317 md.keep_pending_output = 0;
4318 }
4319
4320 static void
4321 dot_xdata_ua (size)
4322 int size;
4323 {
4324 int saved_auto_align = md.auto_align;
4325 char *name = parse_section_name ();
4326 if (!name)
4327 return;
4328
4329 md.keep_pending_output = 1;
4330 set_section (name);
4331 md.auto_align = 0;
4332 cons (size);
4333 md.auto_align = saved_auto_align;
4334 obj_elf_previous (0);
4335 md.keep_pending_output = 0;
4336 }
4337
4338 static void
4339 dot_xfloat_cons_ua (kind)
4340 int kind;
4341 {
4342 int saved_auto_align = md.auto_align;
4343 char *name = parse_section_name ();
4344 if (!name)
4345 return;
4346
4347 md.keep_pending_output = 1;
4348 set_section (name);
4349 md.auto_align = 0;
4350 stmt_float_cons (kind);
4351 md.auto_align = saved_auto_align;
4352 obj_elf_previous (0);
4353 md.keep_pending_output = 0;
4354 }
4355
4356 /* .reg.val <regname>,value */
4357
4358 static void
4359 dot_reg_val (dummy)
4360 int dummy ATTRIBUTE_UNUSED;
4361 {
4362 expressionS reg;
4363
4364 expression (&reg);
4365 if (reg.X_op != O_register)
4366 {
4367 as_bad (_("Register name expected"));
4368 ignore_rest_of_line ();
4369 }
4370 else if (*input_line_pointer++ != ',')
4371 {
4372 as_bad (_("Comma expected"));
4373 ignore_rest_of_line ();
4374 }
4375 else
4376 {
4377 valueT value = get_absolute_expression ();
4378 int regno = reg.X_add_number;
4379 if (regno < REG_GR || regno > REG_GR + 128)
4380 as_warn (_("Register value annotation ignored"));
4381 else
4382 {
4383 gr_values[regno - REG_GR].known = 1;
4384 gr_values[regno - REG_GR].value = value;
4385 gr_values[regno - REG_GR].path = md.path;
4386 }
4387 }
4388 demand_empty_rest_of_line ();
4389 }
4390
4391 /* select dv checking mode
4392 .auto
4393 .explicit
4394 .default
4395
4396 A stop is inserted when changing modes
4397 */
4398
4399 static void
4400 dot_dv_mode (type)
4401 int type;
4402 {
4403 if (md.manual_bundling)
4404 as_warn (_("Directive invalid within a bundle"));
4405
4406 if (type == 'E' || type == 'A')
4407 md.mode_explicitly_set = 0;
4408 else
4409 md.mode_explicitly_set = 1;
4410
4411 md.detect_dv = 1;
4412 switch (type)
4413 {
4414 case 'A':
4415 case 'a':
4416 if (md.explicit_mode)
4417 insn_group_break (1, 0, 0);
4418 md.explicit_mode = 0;
4419 break;
4420 case 'E':
4421 case 'e':
4422 if (!md.explicit_mode)
4423 insn_group_break (1, 0, 0);
4424 md.explicit_mode = 1;
4425 break;
4426 default:
4427 case 'd':
4428 if (md.explicit_mode != md.default_explicit_mode)
4429 insn_group_break (1, 0, 0);
4430 md.explicit_mode = md.default_explicit_mode;
4431 md.mode_explicitly_set = 0;
4432 break;
4433 }
4434 }
4435
4436 static void
4437 print_prmask (mask)
4438 valueT mask;
4439 {
4440 int regno;
4441 char *comma = "";
4442 for (regno = 0; regno < 64; regno++)
4443 {
4444 if (mask & ((valueT) 1 << regno))
4445 {
4446 fprintf (stderr, "%s p%d", comma, regno);
4447 comma = ",";
4448 }
4449 }
4450 }
4451
4452 /*
4453 .pred.rel.clear [p1 [,p2 [,...]]] (also .pred.rel "clear")
4454 .pred.rel.imply p1, p2 (also .pred.rel "imply")
4455 .pred.rel.mutex p1, p2 [,...] (also .pred.rel "mutex")
4456 .pred.safe_across_calls p1 [, p2 [,...]]
4457 */
4458
4459 static void
4460 dot_pred_rel (type)
4461 int type;
4462 {
4463 valueT mask = 0;
4464 int count = 0;
4465 int p1 = -1, p2 = -1;
4466
4467 if (type == 0)
4468 {
4469 if (*input_line_pointer != '"')
4470 {
4471 as_bad (_("Missing predicate relation type"));
4472 ignore_rest_of_line ();
4473 return;
4474 }
4475 else
4476 {
4477 int len;
4478 char *form = demand_copy_C_string (&len);
4479 if (strcmp (form, "mutex") == 0)
4480 type = 'm';
4481 else if (strcmp (form, "clear") == 0)
4482 type = 'c';
4483 else if (strcmp (form, "imply") == 0)
4484 type = 'i';
4485 else
4486 {
4487 as_bad (_("Unrecognized predicate relation type"));
4488 ignore_rest_of_line ();
4489 return;
4490 }
4491 }
4492 if (*input_line_pointer == ',')
4493 ++input_line_pointer;
4494 SKIP_WHITESPACE ();
4495 }
4496
4497 SKIP_WHITESPACE ();
4498 while (1)
4499 {
4500 valueT bit = 1;
4501 int regno;
4502
4503 if (toupper (*input_line_pointer) != 'P'
4504 || (regno = atoi (++input_line_pointer)) < 0
4505 || regno > 63)
4506 {
4507 as_bad (_("Predicate register expected"));
4508 ignore_rest_of_line ();
4509 return;
4510 }
4511 while (isdigit (*input_line_pointer))
4512 ++input_line_pointer;
4513 if (p1 == -1)
4514 p1 = regno;
4515 else if (p2 == -1)
4516 p2 = regno;
4517 bit <<= regno;
4518 if (mask & bit)
4519 as_warn (_("Duplicate predicate register ignored"));
4520 mask |= bit;
4521 count++;
4522 /* See if it's a range. */
4523 if (*input_line_pointer == '-')
4524 {
4525 valueT stop = 1;
4526 ++input_line_pointer;
4527
4528 if (toupper (*input_line_pointer) != 'P'
4529 || (regno = atoi (++input_line_pointer)) < 0
4530 || regno > 63)
4531 {
4532 as_bad (_("Predicate register expected"));
4533 ignore_rest_of_line ();
4534 return;
4535 }
4536 while (isdigit (*input_line_pointer))
4537 ++input_line_pointer;
4538 stop <<= regno;
4539 if (bit >= stop)
4540 {
4541 as_bad (_("Bad register range"));
4542 ignore_rest_of_line ();
4543 return;
4544 }
4545 while (bit < stop)
4546 {
4547 bit <<= 1;
4548 mask |= bit;
4549 count++;
4550 }
4551 SKIP_WHITESPACE ();
4552 }
4553 if (*input_line_pointer != ',')
4554 break;
4555 ++input_line_pointer;
4556 SKIP_WHITESPACE ();
4557 }
4558
4559 switch (type)
4560 {
4561 case 'c':
4562 if (count == 0)
4563 mask = ~(valueT) 0;
4564 clear_qp_mutex (mask);
4565 clear_qp_implies (mask, (valueT) 0);
4566 break;
4567 case 'i':
4568 if (count != 2 || p1 == -1 || p2 == -1)
4569 as_bad (_("Predicate source and target required"));
4570 else if (p1 == 0 || p2 == 0)
4571 as_bad (_("Use of p0 is not valid in this context"));
4572 else
4573 add_qp_imply (p1, p2);
4574 break;
4575 case 'm':
4576 if (count < 2)
4577 {
4578 as_bad (_("At least two PR arguments expected"));
4579 break;
4580 }
4581 else if (mask & 1)
4582 {
4583 as_bad (_("Use of p0 is not valid in this context"));
4584 break;
4585 }
4586 add_qp_mutex (mask);
4587 break;
4588 case 's':
4589 /* note that we don't override any existing relations */
4590 if (count == 0)
4591 {
4592 as_bad (_("At least one PR argument expected"));
4593 break;
4594 }
4595 if (md.debug_dv)
4596 {
4597 fprintf (stderr, "Safe across calls: ");
4598 print_prmask (mask);
4599 fprintf (stderr, "\n");
4600 }
4601 qp_safe_across_calls = mask;
4602 break;
4603 }
4604 demand_empty_rest_of_line ();
4605 }
4606
4607 /* .entry label [, label [, ...]]
4608 Hint to DV code that the given labels are to be considered entry points.
4609 Otherwise, only global labels are considered entry points. */
4610
4611 static void
4612 dot_entry (dummy)
4613 int dummy ATTRIBUTE_UNUSED;
4614 {
4615 const char *err;
4616 char *name;
4617 int c;
4618 symbolS *symbolP;
4619
4620 do
4621 {
4622 name = input_line_pointer;
4623 c = get_symbol_end ();
4624 symbolP = symbol_find_or_make (name);
4625
4626 err = hash_insert (md.entry_hash, S_GET_NAME (symbolP), (PTR) symbolP);
4627 if (err)
4628 as_fatal (_("Inserting \"%s\" into entry hint table failed: %s"),
4629 name, err);
4630
4631 *input_line_pointer = c;
4632 SKIP_WHITESPACE ();
4633 c = *input_line_pointer;
4634 if (c == ',')
4635 {
4636 input_line_pointer++;
4637 SKIP_WHITESPACE ();
4638 if (*input_line_pointer == '\n')
4639 c = '\n';
4640 }
4641 }
4642 while (c == ',');
4643
4644 demand_empty_rest_of_line ();
4645 }
4646
4647 /* .mem.offset offset, base
4648 "base" is used to distinguish between offsets from a different base. */
4649
4650 static void
4651 dot_mem_offset (dummy)
4652 int dummy ATTRIBUTE_UNUSED;
4653 {
4654 md.mem_offset.hint = 1;
4655 md.mem_offset.offset = get_absolute_expression ();
4656 if (*input_line_pointer != ',')
4657 {
4658 as_bad (_("Comma expected"));
4659 ignore_rest_of_line ();
4660 return;
4661 }
4662 ++input_line_pointer;
4663 md.mem_offset.base = get_absolute_expression ();
4664 demand_empty_rest_of_line ();
4665 }
4666
4667 /* ia64-specific pseudo-ops: */
4668 const pseudo_typeS md_pseudo_table[] =
4669 {
4670 { "radix", dot_radix, 0 },
4671 { "lcomm", s_lcomm_bytes, 1 },
4672 { "bss", dot_special_section, SPECIAL_SECTION_BSS },
4673 { "sbss", dot_special_section, SPECIAL_SECTION_SBSS },
4674 { "sdata", dot_special_section, SPECIAL_SECTION_SDATA },
4675 { "rodata", dot_special_section, SPECIAL_SECTION_RODATA },
4676 { "comment", dot_special_section, SPECIAL_SECTION_COMMENT },
4677 { "ia_64.unwind", dot_special_section, SPECIAL_SECTION_UNWIND },
4678 { "ia_64.unwind_info", dot_special_section, SPECIAL_SECTION_UNWIND_INFO },
4679 { "init_array", dot_special_section, SPECIAL_SECTION_INIT_ARRAY },
4680 { "fini_array", dot_special_section, SPECIAL_SECTION_FINI_ARRAY },
4681 { "proc", dot_proc, 0 },
4682 { "body", dot_body, 0 },
4683 { "prologue", dot_prologue, 0 },
4684 { "endp", dot_endp, 0 },
4685 { "file", dwarf2_directive_file, 0 },
4686 { "loc", dwarf2_directive_loc, 0 },
4687
4688 { "fframe", dot_fframe, 0 },
4689 { "vframe", dot_vframe, 0 },
4690 { "vframesp", dot_vframesp, 0 },
4691 { "vframepsp", dot_vframepsp, 0 },
4692 { "save", dot_save, 0 },
4693 { "restore", dot_restore, 0 },
4694 { "restorereg", dot_restorereg, 0 },
4695 { "restorereg.p", dot_restorereg_p, 0 },
4696 { "handlerdata", dot_handlerdata, 0 },
4697 { "unwentry", dot_unwentry, 0 },
4698 { "altrp", dot_altrp, 0 },
4699 { "savesp", dot_savemem, 0 },
4700 { "savepsp", dot_savemem, 1 },
4701 { "save.g", dot_saveg, 0 },
4702 { "save.f", dot_savef, 0 },
4703 { "save.b", dot_saveb, 0 },
4704 { "save.gf", dot_savegf, 0 },
4705 { "spill", dot_spill, 0 },
4706 { "spillreg", dot_spillreg, 0 },
4707 { "spillsp", dot_spillmem, 0 },
4708 { "spillpsp", dot_spillmem, 1 },
4709 { "spillreg.p", dot_spillreg_p, 0 },
4710 { "spillsp.p", dot_spillmem_p, 0 },
4711 { "spillpsp.p", dot_spillmem_p, 1 },
4712 { "label_state", dot_label_state, 0 },
4713 { "copy_state", dot_copy_state, 0 },
4714 { "unwabi", dot_unwabi, 0 },
4715 { "personality", dot_personality, 0 },
4716 #if 0
4717 { "estate", dot_estate, 0 },
4718 #endif
4719 { "mii", dot_template, 0x0 },
4720 { "mli", dot_template, 0x2 }, /* old format, for compatibility */
4721 { "mlx", dot_template, 0x2 },
4722 { "mmi", dot_template, 0x4 },
4723 { "mfi", dot_template, 0x6 },
4724 { "mmf", dot_template, 0x7 },
4725 { "mib", dot_template, 0x8 },
4726 { "mbb", dot_template, 0x9 },
4727 { "bbb", dot_template, 0xb },
4728 { "mmb", dot_template, 0xc },
4729 { "mfb", dot_template, 0xe },
4730 #if 0
4731 { "lb", dot_scope, 0 },
4732 { "le", dot_scope, 1 },
4733 #endif
4734 { "align", s_align_bytes, 0 },
4735 { "regstk", dot_regstk, 0 },
4736 { "rotr", dot_rot, DYNREG_GR },
4737 { "rotf", dot_rot, DYNREG_FR },
4738 { "rotp", dot_rot, DYNREG_PR },
4739 { "lsb", dot_byteorder, 0 },
4740 { "msb", dot_byteorder, 1 },
4741 { "psr", dot_psr, 0 },
4742 { "alias", dot_alias, 0 },
4743 { "ln", dot_ln, 0 }, /* source line info (for debugging) */
4744
4745 { "xdata1", dot_xdata, 1 },
4746 { "xdata2", dot_xdata, 2 },
4747 { "xdata4", dot_xdata, 4 },
4748 { "xdata8", dot_xdata, 8 },
4749 { "xreal4", dot_xfloat_cons, 'f' },
4750 { "xreal8", dot_xfloat_cons, 'd' },
4751 { "xreal10", dot_xfloat_cons, 'x' },
4752 { "xstring", dot_xstringer, 0 },
4753 { "xstringz", dot_xstringer, 1 },
4754
4755 /* unaligned versions: */
4756 { "xdata2.ua", dot_xdata_ua, 2 },
4757 { "xdata4.ua", dot_xdata_ua, 4 },
4758 { "xdata8.ua", dot_xdata_ua, 8 },
4759 { "xreal4.ua", dot_xfloat_cons_ua, 'f' },
4760 { "xreal8.ua", dot_xfloat_cons_ua, 'd' },
4761 { "xreal10.ua", dot_xfloat_cons_ua, 'x' },
4762
4763 /* annotations/DV checking support */
4764 { "entry", dot_entry, 0 },
4765 { "mem.offset", dot_mem_offset, 0 },
4766 { "pred.rel", dot_pred_rel, 0 },
4767 { "pred.rel.clear", dot_pred_rel, 'c' },
4768 { "pred.rel.imply", dot_pred_rel, 'i' },
4769 { "pred.rel.mutex", dot_pred_rel, 'm' },
4770 { "pred.safe_across_calls", dot_pred_rel, 's' },
4771 { "reg.val", dot_reg_val, 0 },
4772 { "auto", dot_dv_mode, 'a' },
4773 { "explicit", dot_dv_mode, 'e' },
4774 { "default", dot_dv_mode, 'd' },
4775
4776 /* ??? These are needed to make gas/testsuite/gas/elf/ehopt.s work.
4777 IA-64 aligns data allocation pseudo-ops by default, so we have to
4778 tell it that these ones are supposed to be unaligned. Long term,
4779 should rewrite so that only IA-64 specific data allocation pseudo-ops
4780 are aligned by default. */
4781 {"2byte", stmt_cons_ua, 2},
4782 {"4byte", stmt_cons_ua, 4},
4783 {"8byte", stmt_cons_ua, 8},
4784
4785 { NULL, 0, 0 }
4786 };
4787
4788 static const struct pseudo_opcode
4789 {
4790 const char *name;
4791 void (*handler) (int);
4792 int arg;
4793 }
4794 pseudo_opcode[] =
4795 {
4796 /* these are more like pseudo-ops, but don't start with a dot */
4797 { "data1", cons, 1 },
4798 { "data2", cons, 2 },
4799 { "data4", cons, 4 },
4800 { "data8", cons, 8 },
4801 { "data16", cons, 16 },
4802 { "real4", stmt_float_cons, 'f' },
4803 { "real8", stmt_float_cons, 'd' },
4804 { "real10", stmt_float_cons, 'x' },
4805 { "string", stringer, 0 },
4806 { "stringz", stringer, 1 },
4807
4808 /* unaligned versions: */
4809 { "data2.ua", stmt_cons_ua, 2 },
4810 { "data4.ua", stmt_cons_ua, 4 },
4811 { "data8.ua", stmt_cons_ua, 8 },
4812 { "data16.ua", stmt_cons_ua, 16 },
4813 { "real4.ua", float_cons, 'f' },
4814 { "real8.ua", float_cons, 'd' },
4815 { "real10.ua", float_cons, 'x' },
4816 };
4817
4818 /* Declare a register by creating a symbol for it and entering it in
4819 the symbol table. */
4820
4821 static symbolS *
4822 declare_register (name, regnum)
4823 const char *name;
4824 int regnum;
4825 {
4826 const char *err;
4827 symbolS *sym;
4828
4829 sym = symbol_new (name, reg_section, regnum, &zero_address_frag);
4830
4831 err = hash_insert (md.reg_hash, S_GET_NAME (sym), (PTR) sym);
4832 if (err)
4833 as_fatal ("Inserting \"%s\" into register table failed: %s",
4834 name, err);
4835
4836 return sym;
4837 }
4838
4839 static void
4840 declare_register_set (prefix, num_regs, base_regnum)
4841 const char *prefix;
4842 int num_regs;
4843 int base_regnum;
4844 {
4845 char name[8];
4846 int i;
4847
4848 for (i = 0; i < num_regs; ++i)
4849 {
4850 sprintf (name, "%s%u", prefix, i);
4851 declare_register (name, base_regnum + i);
4852 }
4853 }
4854
4855 static unsigned int
4856 operand_width (opnd)
4857 enum ia64_opnd opnd;
4858 {
4859 const struct ia64_operand *odesc = &elf64_ia64_operands[opnd];
4860 unsigned int bits = 0;
4861 int i;
4862
4863 bits = 0;
4864 for (i = 0; i < NELEMS (odesc->field) && odesc->field[i].bits; ++i)
4865 bits += odesc->field[i].bits;
4866
4867 return bits;
4868 }
4869
4870 static enum operand_match_result
4871 operand_match (idesc, index, e)
4872 const struct ia64_opcode *idesc;
4873 int index;
4874 expressionS *e;
4875 {
4876 enum ia64_opnd opnd = idesc->operands[index];
4877 int bits, relocatable = 0;
4878 struct insn_fix *fix;
4879 bfd_signed_vma val;
4880
4881 switch (opnd)
4882 {
4883 /* constants: */
4884
4885 case IA64_OPND_AR_CCV:
4886 if (e->X_op == O_register && e->X_add_number == REG_AR + 32)
4887 return OPERAND_MATCH;
4888 break;
4889
4890 case IA64_OPND_AR_PFS:
4891 if (e->X_op == O_register && e->X_add_number == REG_AR + 64)
4892 return OPERAND_MATCH;
4893 break;
4894
4895 case IA64_OPND_GR0:
4896 if (e->X_op == O_register && e->X_add_number == REG_GR + 0)
4897 return OPERAND_MATCH;
4898 break;
4899
4900 case IA64_OPND_IP:
4901 if (e->X_op == O_register && e->X_add_number == REG_IP)
4902 return OPERAND_MATCH;
4903 break;
4904
4905 case IA64_OPND_PR:
4906 if (e->X_op == O_register && e->X_add_number == REG_PR)
4907 return OPERAND_MATCH;
4908 break;
4909
4910 case IA64_OPND_PR_ROT:
4911 if (e->X_op == O_register && e->X_add_number == REG_PR_ROT)
4912 return OPERAND_MATCH;
4913 break;
4914
4915 case IA64_OPND_PSR:
4916 if (e->X_op == O_register && e->X_add_number == REG_PSR)
4917 return OPERAND_MATCH;
4918 break;
4919
4920 case IA64_OPND_PSR_L:
4921 if (e->X_op == O_register && e->X_add_number == REG_PSR_L)
4922 return OPERAND_MATCH;
4923 break;
4924
4925 case IA64_OPND_PSR_UM:
4926 if (e->X_op == O_register && e->X_add_number == REG_PSR_UM)
4927 return OPERAND_MATCH;
4928 break;
4929
4930 case IA64_OPND_C1:
4931 if (e->X_op == O_constant)
4932 {
4933 if (e->X_add_number == 1)
4934 return OPERAND_MATCH;
4935 else
4936 return OPERAND_OUT_OF_RANGE;
4937 }
4938 break;
4939
4940 case IA64_OPND_C8:
4941 if (e->X_op == O_constant)
4942 {
4943 if (e->X_add_number == 8)
4944 return OPERAND_MATCH;
4945 else
4946 return OPERAND_OUT_OF_RANGE;
4947 }
4948 break;
4949
4950 case IA64_OPND_C16:
4951 if (e->X_op == O_constant)
4952 {
4953 if (e->X_add_number == 16)
4954 return OPERAND_MATCH;
4955 else
4956 return OPERAND_OUT_OF_RANGE;
4957 }
4958 break;
4959
4960 /* register operands: */
4961
4962 case IA64_OPND_AR3:
4963 if (e->X_op == O_register && e->X_add_number >= REG_AR
4964 && e->X_add_number < REG_AR + 128)
4965 return OPERAND_MATCH;
4966 break;
4967
4968 case IA64_OPND_B1:
4969 case IA64_OPND_B2:
4970 if (e->X_op == O_register && e->X_add_number >= REG_BR
4971 && e->X_add_number < REG_BR + 8)
4972 return OPERAND_MATCH;
4973 break;
4974
4975 case IA64_OPND_CR3:
4976 if (e->X_op == O_register && e->X_add_number >= REG_CR
4977 && e->X_add_number < REG_CR + 128)
4978 return OPERAND_MATCH;
4979 break;
4980
4981 case IA64_OPND_F1:
4982 case IA64_OPND_F2:
4983 case IA64_OPND_F3:
4984 case IA64_OPND_F4:
4985 if (e->X_op == O_register && e->X_add_number >= REG_FR
4986 && e->X_add_number < REG_FR + 128)
4987 return OPERAND_MATCH;
4988 break;
4989
4990 case IA64_OPND_P1:
4991 case IA64_OPND_P2:
4992 if (e->X_op == O_register && e->X_add_number >= REG_P
4993 && e->X_add_number < REG_P + 64)
4994 return OPERAND_MATCH;
4995 break;
4996
4997 case IA64_OPND_R1:
4998 case IA64_OPND_R2:
4999 case IA64_OPND_R3:
5000 if (e->X_op == O_register && e->X_add_number >= REG_GR
5001 && e->X_add_number < REG_GR + 128)
5002 return OPERAND_MATCH;
5003 break;
5004
5005 case IA64_OPND_R3_2:
5006 if (e->X_op == O_register && e->X_add_number >= REG_GR)
5007 {
5008 if (e->X_add_number < REG_GR + 4)
5009 return OPERAND_MATCH;
5010 else if (e->X_add_number < REG_GR + 128)
5011 return OPERAND_OUT_OF_RANGE;
5012 }
5013 break;
5014
5015 /* indirect operands: */
5016 case IA64_OPND_CPUID_R3:
5017 case IA64_OPND_DBR_R3:
5018 case IA64_OPND_DTR_R3:
5019 case IA64_OPND_ITR_R3:
5020 case IA64_OPND_IBR_R3:
5021 case IA64_OPND_MSR_R3:
5022 case IA64_OPND_PKR_R3:
5023 case IA64_OPND_PMC_R3:
5024 case IA64_OPND_PMD_R3:
5025 case IA64_OPND_RR_R3:
5026 if (e->X_op == O_index && e->X_op_symbol
5027 && (S_GET_VALUE (e->X_op_symbol) - IND_CPUID
5028 == opnd - IA64_OPND_CPUID_R3))
5029 return OPERAND_MATCH;
5030 break;
5031
5032 case IA64_OPND_MR3:
5033 if (e->X_op == O_index && !e->X_op_symbol)
5034 return OPERAND_MATCH;
5035 break;
5036
5037 /* immediate operands: */
5038 case IA64_OPND_CNT2a:
5039 case IA64_OPND_LEN4:
5040 case IA64_OPND_LEN6:
5041 bits = operand_width (idesc->operands[index]);
5042 if (e->X_op == O_constant)
5043 {
5044 if ((bfd_vma) (e->X_add_number - 1) < ((bfd_vma) 1 << bits))
5045 return OPERAND_MATCH;
5046 else
5047 return OPERAND_OUT_OF_RANGE;
5048 }
5049 break;
5050
5051 case IA64_OPND_CNT2b:
5052 if (e->X_op == O_constant)
5053 {
5054 if ((bfd_vma) (e->X_add_number - 1) < 3)
5055 return OPERAND_MATCH;
5056 else
5057 return OPERAND_OUT_OF_RANGE;
5058 }
5059 break;
5060
5061 case IA64_OPND_CNT2c:
5062 val = e->X_add_number;
5063 if (e->X_op == O_constant)
5064 {
5065 if ((val == 0 || val == 7 || val == 15 || val == 16))
5066 return OPERAND_MATCH;
5067 else
5068 return OPERAND_OUT_OF_RANGE;
5069 }
5070 break;
5071
5072 case IA64_OPND_SOR:
5073 /* SOR must be an integer multiple of 8 */
5074 if (e->X_op == O_constant && e->X_add_number & 0x7)
5075 return OPERAND_OUT_OF_RANGE;
5076 case IA64_OPND_SOF:
5077 case IA64_OPND_SOL:
5078 if (e->X_op == O_constant)
5079 {
5080 if ((bfd_vma) e->X_add_number <= 96)
5081 return OPERAND_MATCH;
5082 else
5083 return OPERAND_OUT_OF_RANGE;
5084 }
5085 break;
5086
5087 case IA64_OPND_IMMU62:
5088 if (e->X_op == O_constant)
5089 {
5090 if ((bfd_vma) e->X_add_number < ((bfd_vma) 1 << 62))
5091 return OPERAND_MATCH;
5092 else
5093 return OPERAND_OUT_OF_RANGE;
5094 }
5095 else
5096 {
5097 /* FIXME -- need 62-bit relocation type */
5098 as_bad (_("62-bit relocation not yet implemented"));
5099 }
5100 break;
5101
5102 case IA64_OPND_IMMU64:
5103 if (e->X_op == O_symbol || e->X_op == O_pseudo_fixup
5104 || e->X_op == O_subtract)
5105 {
5106 fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
5107 fix->code = BFD_RELOC_IA64_IMM64;
5108 if (e->X_op != O_subtract)
5109 {
5110 fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
5111 if (e->X_op == O_pseudo_fixup)
5112 e->X_op = O_symbol;
5113 }
5114
5115 fix->opnd = idesc->operands[index];
5116 fix->expr = *e;
5117 fix->is_pcrel = 0;
5118 ++CURR_SLOT.num_fixups;
5119 return OPERAND_MATCH;
5120 }
5121 else if (e->X_op == O_constant)
5122 return OPERAND_MATCH;
5123 break;
5124
5125 case IA64_OPND_CCNT5:
5126 case IA64_OPND_CNT5:
5127 case IA64_OPND_CNT6:
5128 case IA64_OPND_CPOS6a:
5129 case IA64_OPND_CPOS6b:
5130 case IA64_OPND_CPOS6c:
5131 case IA64_OPND_IMMU2:
5132 case IA64_OPND_IMMU7a:
5133 case IA64_OPND_IMMU7b:
5134 case IA64_OPND_IMMU21:
5135 case IA64_OPND_IMMU24:
5136 case IA64_OPND_MBTYPE4:
5137 case IA64_OPND_MHTYPE8:
5138 case IA64_OPND_POS6:
5139 bits = operand_width (idesc->operands[index]);
5140 if (e->X_op == O_constant)
5141 {
5142 if ((bfd_vma) e->X_add_number < ((bfd_vma) 1 << bits))
5143 return OPERAND_MATCH;
5144 else
5145 return OPERAND_OUT_OF_RANGE;
5146 }
5147 break;
5148
5149 case IA64_OPND_IMMU9:
5150 bits = operand_width (idesc->operands[index]);
5151 if (e->X_op == O_constant)
5152 {
5153 if ((bfd_vma) e->X_add_number < ((bfd_vma) 1 << bits))
5154 {
5155 int lobits = e->X_add_number & 0x3;
5156 if (((bfd_vma) e->X_add_number & 0x3C) != 0 && lobits == 0)
5157 e->X_add_number |= (bfd_vma) 0x3;
5158 return OPERAND_MATCH;
5159 }
5160 else
5161 return OPERAND_OUT_OF_RANGE;
5162 }
5163 break;
5164
5165 case IA64_OPND_IMM44:
5166 /* least 16 bits must be zero */
5167 if ((e->X_add_number & 0xffff) != 0)
5168 /* XXX technically, this is wrong: we should not be issuing warning
5169 messages until we're sure this instruction pattern is going to
5170 be used! */
5171 as_warn (_("lower 16 bits of mask ignored"));
5172
5173 if (e->X_op == O_constant)
5174 {
5175 if (((e->X_add_number >= 0
5176 && (bfd_vma) e->X_add_number < ((bfd_vma) 1 << 44))
5177 || (e->X_add_number < 0
5178 && (bfd_vma) -e->X_add_number <= ((bfd_vma) 1 << 44))))
5179 {
5180 /* sign-extend */
5181 if (e->X_add_number >= 0
5182 && (e->X_add_number & ((bfd_vma) 1 << 43)) != 0)
5183 {
5184 e->X_add_number |= ~(((bfd_vma) 1 << 44) - 1);
5185 }
5186 return OPERAND_MATCH;
5187 }
5188 else
5189 return OPERAND_OUT_OF_RANGE;
5190 }
5191 break;
5192
5193 case IA64_OPND_IMM17:
5194 /* bit 0 is a don't care (pr0 is hardwired to 1) */
5195 if (e->X_op == O_constant)
5196 {
5197 if (((e->X_add_number >= 0
5198 && (bfd_vma) e->X_add_number < ((bfd_vma) 1 << 17))
5199 || (e->X_add_number < 0
5200 && (bfd_vma) -e->X_add_number <= ((bfd_vma) 1 << 17))))
5201 {
5202 /* sign-extend */
5203 if (e->X_add_number >= 0
5204 && (e->X_add_number & ((bfd_vma) 1 << 16)) != 0)
5205 {
5206 e->X_add_number |= ~(((bfd_vma) 1 << 17) - 1);
5207 }
5208 return OPERAND_MATCH;
5209 }
5210 else
5211 return OPERAND_OUT_OF_RANGE;
5212 }
5213 break;
5214
5215 case IA64_OPND_IMM14:
5216 case IA64_OPND_IMM22:
5217 relocatable = 1;
5218 case IA64_OPND_IMM1:
5219 case IA64_OPND_IMM8:
5220 case IA64_OPND_IMM8U4:
5221 case IA64_OPND_IMM8M1:
5222 case IA64_OPND_IMM8M1U4:
5223 case IA64_OPND_IMM8M1U8:
5224 case IA64_OPND_IMM9a:
5225 case IA64_OPND_IMM9b:
5226 bits = operand_width (idesc->operands[index]);
5227 if (relocatable && (e->X_op == O_symbol
5228 || e->X_op == O_subtract
5229 || e->X_op == O_pseudo_fixup))
5230 {
5231 fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
5232
5233 if (idesc->operands[index] == IA64_OPND_IMM14)
5234 fix->code = BFD_RELOC_IA64_IMM14;
5235 else
5236 fix->code = BFD_RELOC_IA64_IMM22;
5237
5238 if (e->X_op != O_subtract)
5239 {
5240 fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
5241 if (e->X_op == O_pseudo_fixup)
5242 e->X_op = O_symbol;
5243 }
5244
5245 fix->opnd = idesc->operands[index];
5246 fix->expr = *e;
5247 fix->is_pcrel = 0;
5248 ++CURR_SLOT.num_fixups;
5249 return OPERAND_MATCH;
5250 }
5251 else if (e->X_op != O_constant
5252 && ! (e->X_op == O_big && opnd == IA64_OPND_IMM8M1U8))
5253 return OPERAND_MISMATCH;
5254
5255 if (opnd == IA64_OPND_IMM8M1U4)
5256 {
5257 /* Zero is not valid for unsigned compares that take an adjusted
5258 constant immediate range. */
5259 if (e->X_add_number == 0)
5260 return OPERAND_OUT_OF_RANGE;
5261
5262 /* Sign-extend 32-bit unsigned numbers, so that the following range
5263 checks will work. */
5264 val = e->X_add_number;
5265 if (((val & (~(bfd_vma) 0 << 32)) == 0)
5266 && ((val & ((bfd_vma) 1 << 31)) != 0))
5267 val = ((val << 32) >> 32);
5268
5269 /* Check for 0x100000000. This is valid because
5270 0x100000000-1 is the same as ((uint32_t) -1). */
5271 if (val == ((bfd_signed_vma) 1 << 32))
5272 return OPERAND_MATCH;
5273
5274 val = val - 1;
5275 }
5276 else if (opnd == IA64_OPND_IMM8M1U8)
5277 {
5278 /* Zero is not valid for unsigned compares that take an adjusted
5279 constant immediate range. */
5280 if (e->X_add_number == 0)
5281 return OPERAND_OUT_OF_RANGE;
5282
5283 /* Check for 0x10000000000000000. */
5284 if (e->X_op == O_big)
5285 {
5286 if (generic_bignum[0] == 0
5287 && generic_bignum[1] == 0
5288 && generic_bignum[2] == 0
5289 && generic_bignum[3] == 0
5290 && generic_bignum[4] == 1)
5291 return OPERAND_MATCH;
5292 else
5293 return OPERAND_OUT_OF_RANGE;
5294 }
5295 else
5296 val = e->X_add_number - 1;
5297 }
5298 else if (opnd == IA64_OPND_IMM8M1)
5299 val = e->X_add_number - 1;
5300 else if (opnd == IA64_OPND_IMM8U4)
5301 {
5302 /* Sign-extend 32-bit unsigned numbers, so that the following range
5303 checks will work. */
5304 val = e->X_add_number;
5305 if (((val & (~(bfd_vma) 0 << 32)) == 0)
5306 && ((val & ((bfd_vma) 1 << 31)) != 0))
5307 val = ((val << 32) >> 32);
5308 }
5309 else
5310 val = e->X_add_number;
5311
5312 if ((val >= 0 && (bfd_vma) val < ((bfd_vma) 1 << (bits - 1)))
5313 || (val < 0 && (bfd_vma) -val <= ((bfd_vma) 1 << (bits - 1))))
5314 return OPERAND_MATCH;
5315 else
5316 return OPERAND_OUT_OF_RANGE;
5317
5318 case IA64_OPND_INC3:
5319 /* +/- 1, 4, 8, 16 */
5320 val = e->X_add_number;
5321 if (val < 0)
5322 val = -val;
5323 if (e->X_op == O_constant)
5324 {
5325 if ((val == 1 || val == 4 || val == 8 || val == 16))
5326 return OPERAND_MATCH;
5327 else
5328 return OPERAND_OUT_OF_RANGE;
5329 }
5330 break;
5331
5332 case IA64_OPND_TGT25:
5333 case IA64_OPND_TGT25b:
5334 case IA64_OPND_TGT25c:
5335 case IA64_OPND_TGT64:
5336 if (e->X_op == O_symbol)
5337 {
5338 fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
5339 if (opnd == IA64_OPND_TGT25)
5340 fix->code = BFD_RELOC_IA64_PCREL21F;
5341 else if (opnd == IA64_OPND_TGT25b)
5342 fix->code = BFD_RELOC_IA64_PCREL21M;
5343 else if (opnd == IA64_OPND_TGT25c)
5344 fix->code = BFD_RELOC_IA64_PCREL21B;
5345 else if (opnd == IA64_OPND_TGT64)
5346 fix->code = BFD_RELOC_IA64_PCREL60B;
5347 else
5348 abort ();
5349
5350 fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
5351 fix->opnd = idesc->operands[index];
5352 fix->expr = *e;
5353 fix->is_pcrel = 1;
5354 ++CURR_SLOT.num_fixups;
5355 return OPERAND_MATCH;
5356 }
5357 case IA64_OPND_TAG13:
5358 case IA64_OPND_TAG13b:
5359 switch (e->X_op)
5360 {
5361 case O_constant:
5362 return OPERAND_MATCH;
5363
5364 case O_symbol:
5365 fix = CURR_SLOT.fixup + CURR_SLOT.num_fixups;
5366 /* There are no external relocs for TAG13/TAG13b fields, so we
5367 create a dummy reloc. This will not live past md_apply_fix3. */
5368 fix->code = BFD_RELOC_UNUSED;
5369 fix->code = ia64_gen_real_reloc_type (e->X_op_symbol, fix->code);
5370 fix->opnd = idesc->operands[index];
5371 fix->expr = *e;
5372 fix->is_pcrel = 1;
5373 ++CURR_SLOT.num_fixups;
5374 return OPERAND_MATCH;
5375
5376 default:
5377 break;
5378 }
5379 break;
5380
5381 default:
5382 break;
5383 }
5384 return OPERAND_MISMATCH;
5385 }
5386
5387 static int
5388 parse_operand (e)
5389 expressionS *e;
5390 {
5391 int sep = '\0';
5392
5393 memset (e, 0, sizeof (*e));
5394 e->X_op = O_absent;
5395 SKIP_WHITESPACE ();
5396 if (*input_line_pointer != '}')
5397 expression (e);
5398 sep = *input_line_pointer++;
5399
5400 if (sep == '}')
5401 {
5402 if (!md.manual_bundling)
5403 as_warn ("Found '}' when manual bundling is off");
5404 else
5405 CURR_SLOT.manual_bundling_off = 1;
5406 md.manual_bundling = 0;
5407 sep = '\0';
5408 }
5409 return sep;
5410 }
5411
5412 /* Returns the next entry in the opcode table that matches the one in
5413 IDESC, and frees the entry in IDESC. If no matching entry is
5414 found, NULL is returned instead. */
5415
5416 static struct ia64_opcode *
5417 get_next_opcode (struct ia64_opcode *idesc)
5418 {
5419 struct ia64_opcode *next = ia64_find_next_opcode (idesc);
5420 ia64_free_opcode (idesc);
5421 return next;
5422 }
5423
5424 /* Parse the operands for the opcode and find the opcode variant that
5425 matches the specified operands, or NULL if no match is possible. */
5426
5427 static struct ia64_opcode *
5428 parse_operands (idesc)
5429 struct ia64_opcode *idesc;
5430 {
5431 int i = 0, highest_unmatched_operand, num_operands = 0, num_outputs = 0;
5432 int error_pos, out_of_range_pos, curr_out_of_range_pos, sep = 0;
5433 enum ia64_opnd expected_operand = IA64_OPND_NIL;
5434 enum operand_match_result result;
5435 char mnemonic[129];
5436 char *first_arg = 0, *end, *saved_input_pointer;
5437 unsigned int sof;
5438
5439 assert (strlen (idesc->name) <= 128);
5440
5441 strcpy (mnemonic, idesc->name);
5442 if (idesc->operands[2] == IA64_OPND_SOF)
5443 {
5444 /* To make the common idiom "alloc loc?=ar.pfs,0,1,0,0" work, we
5445 can't parse the first operand until we have parsed the
5446 remaining operands of the "alloc" instruction. */
5447 SKIP_WHITESPACE ();
5448 first_arg = input_line_pointer;
5449 end = strchr (input_line_pointer, '=');
5450 if (!end)
5451 {
5452 as_bad ("Expected separator `='");
5453 return 0;
5454 }
5455 input_line_pointer = end + 1;
5456 ++i;
5457 ++num_outputs;
5458 }
5459
5460 for (; i < NELEMS (CURR_SLOT.opnd); ++i)
5461 {
5462 sep = parse_operand (CURR_SLOT.opnd + i);
5463 if (CURR_SLOT.opnd[i].X_op == O_absent)
5464 break;
5465
5466 ++num_operands;
5467
5468 if (sep != '=' && sep != ',')
5469 break;
5470
5471 if (sep == '=')
5472 {
5473 if (num_outputs > 0)
5474 as_bad ("Duplicate equal sign (=) in instruction");
5475 else
5476 num_outputs = i + 1;
5477 }
5478 }
5479 if (sep != '\0')
5480 {
5481 as_bad ("Illegal operand separator `%c'", sep);
5482 return 0;
5483 }
5484
5485 if (idesc->operands[2] == IA64_OPND_SOF)
5486 {
5487 /* map alloc r1=ar.pfs,i,l,o,r to alloc r1=ar.pfs,(i+l+o),(i+l),r */
5488 know (strcmp (idesc->name, "alloc") == 0);
5489 if (num_operands == 5 /* first_arg not included in this count! */
5490 && CURR_SLOT.opnd[2].X_op == O_constant
5491 && CURR_SLOT.opnd[3].X_op == O_constant
5492 && CURR_SLOT.opnd[4].X_op == O_constant
5493 && CURR_SLOT.opnd[5].X_op == O_constant)
5494 {
5495 sof = set_regstack (CURR_SLOT.opnd[2].X_add_number,
5496 CURR_SLOT.opnd[3].X_add_number,
5497 CURR_SLOT.opnd[4].X_add_number,
5498 CURR_SLOT.opnd[5].X_add_number);
5499
5500 /* now we can parse the first arg: */
5501 saved_input_pointer = input_line_pointer;
5502 input_line_pointer = first_arg;
5503 sep = parse_operand (CURR_SLOT.opnd + 0);
5504 if (sep != '=')
5505 --num_outputs; /* force error */
5506 input_line_pointer = saved_input_pointer;
5507
5508 CURR_SLOT.opnd[2].X_add_number = sof;
5509 CURR_SLOT.opnd[3].X_add_number
5510 = sof - CURR_SLOT.opnd[4].X_add_number;
5511 CURR_SLOT.opnd[4] = CURR_SLOT.opnd[5];
5512 }
5513 }
5514
5515 highest_unmatched_operand = 0;
5516 curr_out_of_range_pos = -1;
5517 error_pos = 0;
5518 expected_operand = idesc->operands[0];
5519 for (; idesc; idesc = get_next_opcode (idesc))
5520 {
5521 if (num_outputs != idesc->num_outputs)
5522 continue; /* mismatch in # of outputs */
5523
5524 CURR_SLOT.num_fixups = 0;
5525
5526 /* Try to match all operands. If we see an out-of-range operand,
5527 then continue trying to match the rest of the operands, since if
5528 the rest match, then this idesc will give the best error message. */
5529
5530 out_of_range_pos = -1;
5531 for (i = 0; i < num_operands && idesc->operands[i]; ++i)
5532 {
5533 result = operand_match (idesc, i, CURR_SLOT.opnd + i);
5534 if (result != OPERAND_MATCH)
5535 {
5536 if (result != OPERAND_OUT_OF_RANGE)
5537 break;
5538 if (out_of_range_pos < 0)
5539 /* remember position of the first out-of-range operand: */
5540 out_of_range_pos = i;
5541 }
5542 }
5543
5544 /* If we did not match all operands, or if at least one operand was
5545 out-of-range, then this idesc does not match. Keep track of which
5546 idesc matched the most operands before failing. If we have two
5547 idescs that failed at the same position, and one had an out-of-range
5548 operand, then prefer the out-of-range operand. Thus if we have
5549 "add r0=0x1000000,r1" we get an error saying the constant is out
5550 of range instead of an error saying that the constant should have been
5551 a register. */
5552
5553 if (i != num_operands || out_of_range_pos >= 0)
5554 {
5555 if (i > highest_unmatched_operand
5556 || (i == highest_unmatched_operand
5557 && out_of_range_pos > curr_out_of_range_pos))
5558 {
5559 highest_unmatched_operand = i;
5560 if (out_of_range_pos >= 0)
5561 {
5562 expected_operand = idesc->operands[out_of_range_pos];
5563 error_pos = out_of_range_pos;
5564 }
5565 else
5566 {
5567 expected_operand = idesc->operands[i];
5568 error_pos = i;
5569 }
5570 curr_out_of_range_pos = out_of_range_pos;
5571 }
5572 continue;
5573 }
5574
5575 if (num_operands < NELEMS (idesc->operands)
5576 && idesc->operands[num_operands])
5577 continue; /* mismatch in number of arguments */
5578
5579 break;
5580 }
5581 if (!idesc)
5582 {
5583 if (expected_operand)
5584 as_bad ("Operand %u of `%s' should be %s",
5585 error_pos + 1, mnemonic,
5586 elf64_ia64_operands[expected_operand].desc);
5587 else
5588 as_bad ("Operand mismatch");
5589 return 0;
5590 }
5591 return idesc;
5592 }
5593
5594 /* Keep track of state necessary to determine whether a NOP is necessary
5595 to avoid an erratum in A and B step Itanium chips, and return 1 if we
5596 detect a case where additional NOPs may be necessary. */
5597 static int
5598 errata_nop_necessary_p (slot, insn_unit)
5599 struct slot *slot;
5600 enum ia64_unit insn_unit;
5601 {
5602 int i;
5603 struct group *this_group = md.last_groups + md.group_idx;
5604 struct group *prev_group = md.last_groups + (md.group_idx + 2) % 3;
5605 struct ia64_opcode *idesc = slot->idesc;
5606
5607 /* Test whether this could be the first insn in a problematic sequence. */
5608 if (insn_unit == IA64_UNIT_F)
5609 {
5610 for (i = 0; i < idesc->num_outputs; i++)
5611 if (idesc->operands[i] == IA64_OPND_P1
5612 || idesc->operands[i] == IA64_OPND_P2)
5613 {
5614 int regno = slot->opnd[i].X_add_number - REG_P;
5615 /* Ignore invalid operands; they generate errors elsewhere. */
5616 if (regno >= 64)
5617 return 0;
5618 this_group->p_reg_set[regno] = 1;
5619 }
5620 }
5621
5622 /* Test whether this could be the second insn in a problematic sequence. */
5623 if (insn_unit == IA64_UNIT_M && slot->qp_regno > 0
5624 && prev_group->p_reg_set[slot->qp_regno])
5625 {
5626 for (i = 0; i < idesc->num_outputs; i++)
5627 if (idesc->operands[i] == IA64_OPND_R1
5628 || idesc->operands[i] == IA64_OPND_R2
5629 || idesc->operands[i] == IA64_OPND_R3)
5630 {
5631 int regno = slot->opnd[i].X_add_number - REG_GR;
5632 /* Ignore invalid operands; they generate errors elsewhere. */
5633 if (regno >= 128)
5634 return 0;
5635 if (strncmp (idesc->name, "add", 3) != 0
5636 && strncmp (idesc->name, "sub", 3) != 0
5637 && strncmp (idesc->name, "shladd", 6) != 0
5638 && (idesc->flags & IA64_OPCODE_POSTINC) == 0)
5639 this_group->g_reg_set_conditionally[regno] = 1;
5640 }
5641 }
5642
5643 /* Test whether this could be the third insn in a problematic sequence. */
5644 for (i = 0; i < NELEMS (idesc->operands) && idesc->operands[i]; i++)
5645 {
5646 if (/* For fc, ptc, ptr, tak, thash, tpa, ttag, probe, ptr, ptc. */
5647 idesc->operands[i] == IA64_OPND_R3
5648 /* For mov indirect. */
5649 || idesc->operands[i] == IA64_OPND_RR_R3
5650 || idesc->operands[i] == IA64_OPND_DBR_R3
5651 || idesc->operands[i] == IA64_OPND_IBR_R3
5652 || idesc->operands[i] == IA64_OPND_PKR_R3
5653 || idesc->operands[i] == IA64_OPND_PMC_R3
5654 || idesc->operands[i] == IA64_OPND_PMD_R3
5655 || idesc->operands[i] == IA64_OPND_MSR_R3
5656 || idesc->operands[i] == IA64_OPND_CPUID_R3
5657 /* For itr. */
5658 || idesc->operands[i] == IA64_OPND_ITR_R3
5659 || idesc->operands[i] == IA64_OPND_DTR_R3
5660 /* Normal memory addresses (load, store, xchg, cmpxchg, etc.). */
5661 || idesc->operands[i] == IA64_OPND_MR3)
5662 {
5663 int regno = slot->opnd[i].X_add_number - REG_GR;
5664 /* Ignore invalid operands; they generate errors elsewhere. */
5665 if (regno >= 128)
5666 return 0;
5667 if (idesc->operands[i] == IA64_OPND_R3)
5668 {
5669 if (strcmp (idesc->name, "fc") != 0
5670 && strcmp (idesc->name, "tak") != 0
5671 && strcmp (idesc->name, "thash") != 0
5672 && strcmp (idesc->name, "tpa") != 0
5673 && strcmp (idesc->name, "ttag") != 0
5674 && strncmp (idesc->name, "ptr", 3) != 0
5675 && strncmp (idesc->name, "ptc", 3) != 0
5676 && strncmp (idesc->name, "probe", 5) != 0)
5677 return 0;
5678 }
5679 if (prev_group->g_reg_set_conditionally[regno])
5680 return 1;
5681 }
5682 }
5683 return 0;
5684 }
5685
5686 static void
5687 build_insn (slot, insnp)
5688 struct slot *slot;
5689 bfd_vma *insnp;
5690 {
5691 const struct ia64_operand *odesc, *o2desc;
5692 struct ia64_opcode *idesc = slot->idesc;
5693 bfd_signed_vma insn, val;
5694 const char *err;
5695 int i;
5696
5697 insn = idesc->opcode | slot->qp_regno;
5698
5699 for (i = 0; i < NELEMS (idesc->operands) && idesc->operands[i]; ++i)
5700 {
5701 if (slot->opnd[i].X_op == O_register
5702 || slot->opnd[i].X_op == O_constant
5703 || slot->opnd[i].X_op == O_index)
5704 val = slot->opnd[i].X_add_number;
5705 else if (slot->opnd[i].X_op == O_big)
5706 {
5707 /* This must be the value 0x10000000000000000. */
5708 assert (idesc->operands[i] == IA64_OPND_IMM8M1U8);
5709 val = 0;
5710 }
5711 else
5712 val = 0;
5713
5714 switch (idesc->operands[i])
5715 {
5716 case IA64_OPND_IMMU64:
5717 *insnp++ = (val >> 22) & 0x1ffffffffffLL;
5718 insn |= (((val & 0x7f) << 13) | (((val >> 7) & 0x1ff) << 27)
5719 | (((val >> 16) & 0x1f) << 22) | (((val >> 21) & 0x1) << 21)
5720 | (((val >> 63) & 0x1) << 36));
5721 continue;
5722
5723 case IA64_OPND_IMMU62:
5724 val &= 0x3fffffffffffffffULL;
5725 if (val != slot->opnd[i].X_add_number)
5726 as_warn (_("Value truncated to 62 bits"));
5727 *insnp++ = (val >> 21) & 0x1ffffffffffLL;
5728 insn |= (((val & 0xfffff) << 6) | (((val >> 20) & 0x1) << 36));
5729 continue;
5730
5731 case IA64_OPND_TGT64:
5732 val >>= 4;
5733 *insnp++ = ((val >> 20) & 0x7fffffffffLL) << 2;
5734 insn |= ((((val >> 59) & 0x1) << 36)
5735 | (((val >> 0) & 0xfffff) << 13));
5736 continue;
5737
5738 case IA64_OPND_AR3:
5739 val -= REG_AR;
5740 break;
5741
5742 case IA64_OPND_B1:
5743 case IA64_OPND_B2:
5744 val -= REG_BR;
5745 break;
5746
5747 case IA64_OPND_CR3:
5748 val -= REG_CR;
5749 break;
5750
5751 case IA64_OPND_F1:
5752 case IA64_OPND_F2:
5753 case IA64_OPND_F3:
5754 case IA64_OPND_F4:
5755 val -= REG_FR;
5756 break;
5757
5758 case IA64_OPND_P1:
5759 case IA64_OPND_P2:
5760 val -= REG_P;
5761 break;
5762
5763 case IA64_OPND_R1:
5764 case IA64_OPND_R2:
5765 case IA64_OPND_R3:
5766 case IA64_OPND_R3_2:
5767 case IA64_OPND_CPUID_R3:
5768 case IA64_OPND_DBR_R3:
5769 case IA64_OPND_DTR_R3:
5770 case IA64_OPND_ITR_R3:
5771 case IA64_OPND_IBR_R3:
5772 case IA64_OPND_MR3:
5773 case IA64_OPND_MSR_R3:
5774 case IA64_OPND_PKR_R3:
5775 case IA64_OPND_PMC_R3:
5776 case IA64_OPND_PMD_R3:
5777 case IA64_OPND_RR_R3:
5778 val -= REG_GR;
5779 break;
5780
5781 default:
5782 break;
5783 }
5784
5785 odesc = elf64_ia64_operands + idesc->operands[i];
5786 err = (*odesc->insert) (odesc, val, &insn);
5787 if (err)
5788 as_bad_where (slot->src_file, slot->src_line,
5789 "Bad operand value: %s", err);
5790 if (idesc->flags & IA64_OPCODE_PSEUDO)
5791 {
5792 if ((idesc->flags & IA64_OPCODE_F2_EQ_F3)
5793 && odesc == elf64_ia64_operands + IA64_OPND_F3)
5794 {
5795 o2desc = elf64_ia64_operands + IA64_OPND_F2;
5796 (*o2desc->insert) (o2desc, val, &insn);
5797 }
5798 if ((idesc->flags & IA64_OPCODE_LEN_EQ_64MCNT)
5799 && (odesc == elf64_ia64_operands + IA64_OPND_CPOS6a
5800 || odesc == elf64_ia64_operands + IA64_OPND_POS6))
5801 {
5802 o2desc = elf64_ia64_operands + IA64_OPND_LEN6;
5803 (*o2desc->insert) (o2desc, 64 - val, &insn);
5804 }
5805 }
5806 }
5807 *insnp = insn;
5808 }
5809
5810 static void
5811 emit_one_bundle ()
5812 {
5813 unsigned int manual_bundling_on = 0, manual_bundling_off = 0;
5814 unsigned int manual_bundling = 0;
5815 enum ia64_unit required_unit, insn_unit = 0;
5816 enum ia64_insn_type type[3], insn_type;
5817 unsigned int template, orig_template;
5818 bfd_vma insn[3] = { -1, -1, -1 };
5819 struct ia64_opcode *idesc;
5820 int end_of_insn_group = 0, user_template = -1;
5821 int n, i, j, first, curr;
5822 unw_rec_list *ptr;
5823 bfd_vma t0 = 0, t1 = 0;
5824 struct label_fix *lfix;
5825 struct insn_fix *ifix;
5826 char mnemonic[16];
5827 fixS *fix;
5828 char *f;
5829
5830 first = (md.curr_slot + NUM_SLOTS - md.num_slots_in_use) % NUM_SLOTS;
5831 know (first >= 0 & first < NUM_SLOTS);
5832 n = MIN (3, md.num_slots_in_use);
5833
5834 /* Determine template: user user_template if specified, best match
5835 otherwise: */
5836
5837 if (md.slot[first].user_template >= 0)
5838 user_template = template = md.slot[first].user_template;
5839 else
5840 {
5841 /* Auto select appropriate template. */
5842 memset (type, 0, sizeof (type));
5843 curr = first;
5844 for (i = 0; i < n; ++i)
5845 {
5846 if (md.slot[curr].label_fixups && i != 0)
5847 break;
5848 type[i] = md.slot[curr].idesc->type;
5849 curr = (curr + 1) % NUM_SLOTS;
5850 }
5851 template = best_template[type[0]][type[1]][type[2]];
5852 }
5853
5854 /* initialize instructions with appropriate nops: */
5855 for (i = 0; i < 3; ++i)
5856 insn[i] = nop[ia64_templ_desc[template].exec_unit[i]];
5857
5858 f = frag_more (16);
5859
5860 /* now fill in slots with as many insns as possible: */
5861 curr = first;
5862 idesc = md.slot[curr].idesc;
5863 end_of_insn_group = 0;
5864 for (i = 0; i < 3 && md.num_slots_in_use > 0; ++i)
5865 {
5866 /* Set the slot number for prologue/body records now as those
5867 refer to the current point, not the point after the
5868 instruction has been issued: */
5869 /* Don't try to delete prologue/body records here, as that will cause
5870 them to also be deleted from the master list of unwind records. */
5871 for (ptr = md.slot[curr].unwind_record; ptr; ptr = ptr->next)
5872 if (ptr->r.type == prologue || ptr->r.type == prologue_gr
5873 || ptr->r.type == body)
5874 {
5875 ptr->slot_number = (unsigned long) f + i;
5876 ptr->slot_frag = frag_now;
5877 }
5878
5879 if (idesc->flags & IA64_OPCODE_SLOT2)
5880 {
5881 if (manual_bundling && i != 2)
5882 as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
5883 "`%s' must be last in bundle", idesc->name);
5884 else
5885 i = 2;
5886 }
5887 if (idesc->flags & IA64_OPCODE_LAST)
5888 {
5889 int required_slot;
5890 unsigned int required_template;
5891
5892 /* If we need a stop bit after an M slot, our only choice is
5893 template 5 (M;;MI). If we need a stop bit after a B
5894 slot, our only choice is to place it at the end of the
5895 bundle, because the only available templates are MIB,
5896 MBB, BBB, MMB, and MFB. We don't handle anything other
5897 than M and B slots because these are the only kind of
5898 instructions that can have the IA64_OPCODE_LAST bit set. */
5899 required_template = template;
5900 switch (idesc->type)
5901 {
5902 case IA64_TYPE_M:
5903 required_slot = 0;
5904 required_template = 5;
5905 break;
5906
5907 case IA64_TYPE_B:
5908 required_slot = 2;
5909 break;
5910
5911 default:
5912 as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
5913 "Internal error: don't know how to force %s to end"
5914 "of instruction group", idesc->name);
5915 required_slot = i;
5916 break;
5917 }
5918 if (manual_bundling && i != required_slot)
5919 as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
5920 "`%s' must be last in instruction group",
5921 idesc->name);
5922 if (required_slot < i)
5923 /* Can't fit this instruction. */
5924 break;
5925
5926 i = required_slot;
5927 if (required_template != template)
5928 {
5929 /* If we switch the template, we need to reset the NOPs
5930 after slot i. The slot-types of the instructions ahead
5931 of i never change, so we don't need to worry about
5932 changing NOPs in front of this slot. */
5933 for (j = i; j < 3; ++j)
5934 insn[j] = nop[ia64_templ_desc[required_template].exec_unit[j]];
5935 }
5936 template = required_template;
5937 }
5938 if (curr != first && md.slot[curr].label_fixups)
5939 {
5940 if (manual_bundling_on)
5941 as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
5942 "Label must be first in a bundle");
5943 /* This insn must go into the first slot of a bundle. */
5944 break;
5945 }
5946
5947 manual_bundling_on = md.slot[curr].manual_bundling_on;
5948 manual_bundling_off = md.slot[curr].manual_bundling_off;
5949
5950 if (manual_bundling_on)
5951 {
5952 if (curr == first)
5953 manual_bundling = 1;
5954 else
5955 break; /* need to start a new bundle */
5956 }
5957
5958 if (end_of_insn_group && md.num_slots_in_use >= 1)
5959 {
5960 /* We need an instruction group boundary in the middle of a
5961 bundle. See if we can switch to an other template with
5962 an appropriate boundary. */
5963
5964 orig_template = template;
5965 if (i == 1 && (user_template == 4
5966 || (user_template < 0
5967 && (ia64_templ_desc[template].exec_unit[0]
5968 == IA64_UNIT_M))))
5969 {
5970 template = 5;
5971 end_of_insn_group = 0;
5972 }
5973 else if (i == 2 && (user_template == 0
5974 || (user_template < 0
5975 && (ia64_templ_desc[template].exec_unit[1]
5976 == IA64_UNIT_I)))
5977 /* This test makes sure we don't switch the template if
5978 the next instruction is one that needs to be first in
5979 an instruction group. Since all those instructions are
5980 in the M group, there is no way such an instruction can
5981 fit in this bundle even if we switch the template. The
5982 reason we have to check for this is that otherwise we
5983 may end up generating "MI;;I M.." which has the deadly
5984 effect that the second M instruction is no longer the
5985 first in the bundle! --davidm 99/12/16 */
5986 && (idesc->flags & IA64_OPCODE_FIRST) == 0)
5987 {
5988 template = 1;
5989 end_of_insn_group = 0;
5990 }
5991 else if (curr != first)
5992 /* can't fit this insn */
5993 break;
5994
5995 if (template != orig_template)
5996 /* if we switch the template, we need to reset the NOPs
5997 after slot i. The slot-types of the instructions ahead
5998 of i never change, so we don't need to worry about
5999 changing NOPs in front of this slot. */
6000 for (j = i; j < 3; ++j)
6001 insn[j] = nop[ia64_templ_desc[template].exec_unit[j]];
6002 }
6003 required_unit = ia64_templ_desc[template].exec_unit[i];
6004
6005 /* resolve dynamic opcodes such as "break" and "nop": */
6006 if (idesc->type == IA64_TYPE_DYN)
6007 {
6008 if ((strcmp (idesc->name, "nop") == 0)
6009 || (strcmp (idesc->name, "break") == 0))
6010 insn_unit = required_unit;
6011 else if (strcmp (idesc->name, "chk.s") == 0)
6012 {
6013 insn_unit = IA64_UNIT_M;
6014 if (required_unit == IA64_UNIT_I)
6015 insn_unit = IA64_UNIT_I;
6016 }
6017 else
6018 as_fatal ("emit_one_bundle: unexpected dynamic op");
6019
6020 sprintf (mnemonic, "%s.%c", idesc->name, "?imbf??"[insn_unit]);
6021 ia64_free_opcode (idesc);
6022 md.slot[curr].idesc = idesc = ia64_find_opcode (mnemonic);
6023 #if 0
6024 know (!idesc->next); /* no resolved dynamic ops have collisions */
6025 #endif
6026 }
6027 else
6028 {
6029 insn_type = idesc->type;
6030 insn_unit = IA64_UNIT_NIL;
6031 switch (insn_type)
6032 {
6033 case IA64_TYPE_A:
6034 if (required_unit == IA64_UNIT_I || required_unit == IA64_UNIT_M)
6035 insn_unit = required_unit;
6036 break;
6037 case IA64_TYPE_X: insn_unit = IA64_UNIT_L; break;
6038 case IA64_TYPE_I: insn_unit = IA64_UNIT_I; break;
6039 case IA64_TYPE_M: insn_unit = IA64_UNIT_M; break;
6040 case IA64_TYPE_B: insn_unit = IA64_UNIT_B; break;
6041 case IA64_TYPE_F: insn_unit = IA64_UNIT_F; break;
6042 default: break;
6043 }
6044 }
6045
6046 if (insn_unit != required_unit)
6047 {
6048 if (required_unit == IA64_UNIT_L
6049 && insn_unit == IA64_UNIT_I
6050 && !(idesc->flags & IA64_OPCODE_X_IN_MLX))
6051 {
6052 /* we got ourselves an MLX template but the current
6053 instruction isn't an X-unit, or an I-unit instruction
6054 that can go into the X slot of an MLX template. Duh. */
6055 if (md.num_slots_in_use >= NUM_SLOTS)
6056 {
6057 as_bad_where (md.slot[curr].src_file,
6058 md.slot[curr].src_line,
6059 "`%s' can't go in X slot of "
6060 "MLX template", idesc->name);
6061 /* drop this insn so we don't livelock: */
6062 --md.num_slots_in_use;
6063 }
6064 break;
6065 }
6066 continue; /* try next slot */
6067 }
6068
6069 {
6070 bfd_vma addr;
6071
6072 addr = frag_now->fr_address + frag_now_fix () - 16 + i;
6073 dwarf2_gen_line_info (addr, &md.slot[curr].debug_line);
6074 }
6075
6076 if (errata_nop_necessary_p (md.slot + curr, insn_unit))
6077 as_warn (_("Additional NOP may be necessary to workaround Itanium processor A/B step errata"));
6078
6079 build_insn (md.slot + curr, insn + i);
6080
6081 /* Set slot counts for non prologue/body unwind records. */
6082 for (ptr = md.slot[curr].unwind_record; ptr; ptr = ptr->next)
6083 if (ptr->r.type != prologue && ptr->r.type != prologue_gr
6084 && ptr->r.type != body)
6085 {
6086 ptr->slot_number = (unsigned long) f + i;
6087 ptr->slot_frag = frag_now;
6088 }
6089 md.slot[curr].unwind_record = NULL;
6090
6091 if (required_unit == IA64_UNIT_L)
6092 {
6093 know (i == 1);
6094 /* skip one slot for long/X-unit instructions */
6095 ++i;
6096 }
6097 --md.num_slots_in_use;
6098
6099 /* now is a good time to fix up the labels for this insn: */
6100 for (lfix = md.slot[curr].label_fixups; lfix; lfix = lfix->next)
6101 {
6102 S_SET_VALUE (lfix->sym, frag_now_fix () - 16);
6103 symbol_set_frag (lfix->sym, frag_now);
6104 }
6105 /* and fix up the tags also. */
6106 for (lfix = md.slot[curr].tag_fixups; lfix; lfix = lfix->next)
6107 {
6108 S_SET_VALUE (lfix->sym, frag_now_fix () - 16 + i);
6109 symbol_set_frag (lfix->sym, frag_now);
6110 }
6111
6112 for (j = 0; j < md.slot[curr].num_fixups; ++j)
6113 {
6114 ifix = md.slot[curr].fixup + j;
6115 fix = fix_new_exp (frag_now, frag_now_fix () - 16 + i, 8,
6116 &ifix->expr, ifix->is_pcrel, ifix->code);
6117 fix->tc_fix_data.opnd = ifix->opnd;
6118 fix->fx_plt = (fix->fx_r_type == BFD_RELOC_IA64_PLTOFF22);
6119 fix->fx_file = md.slot[curr].src_file;
6120 fix->fx_line = md.slot[curr].src_line;
6121 }
6122
6123 end_of_insn_group = md.slot[curr].end_of_insn_group;
6124
6125 if (end_of_insn_group)
6126 {
6127 md.group_idx = (md.group_idx + 1) % 3;
6128 memset (md.last_groups + md.group_idx, 0, sizeof md.last_groups[0]);
6129 }
6130
6131 /* clear slot: */
6132 ia64_free_opcode (md.slot[curr].idesc);
6133 memset (md.slot + curr, 0, sizeof (md.slot[curr]));
6134 md.slot[curr].user_template = -1;
6135
6136 if (manual_bundling_off)
6137 {
6138 manual_bundling = 0;
6139 break;
6140 }
6141 curr = (curr + 1) % NUM_SLOTS;
6142 idesc = md.slot[curr].idesc;
6143 }
6144 if (manual_bundling)
6145 {
6146 if (md.num_slots_in_use > 0)
6147 as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
6148 "`%s' does not fit into %s template",
6149 idesc->name, ia64_templ_desc[template].name);
6150 else
6151 as_bad_where (md.slot[curr].src_file, md.slot[curr].src_line,
6152 "Missing '}' at end of file");
6153 }
6154 know (md.num_slots_in_use < NUM_SLOTS);
6155
6156 t0 = end_of_insn_group | (template << 1) | (insn[0] << 5) | (insn[1] << 46);
6157 t1 = ((insn[1] >> 18) & 0x7fffff) | (insn[2] << 23);
6158
6159 number_to_chars_littleendian (f + 0, t0, 8);
6160 number_to_chars_littleendian (f + 8, t1, 8);
6161
6162 unwind.next_slot_number = (unsigned long) f + 16;
6163 unwind.next_slot_frag = frag_now;
6164 }
6165
6166 int
6167 md_parse_option (c, arg)
6168 int c;
6169 char *arg;
6170 {
6171
6172 switch (c)
6173 {
6174 /* Switches from the Intel assembler. */
6175 case 'm':
6176 if (strcmp (arg, "ilp64") == 0
6177 || strcmp (arg, "lp64") == 0
6178 || strcmp (arg, "p64") == 0)
6179 {
6180 md.flags |= EF_IA_64_ABI64;
6181 }
6182 else if (strcmp (arg, "ilp32") == 0)
6183 {
6184 md.flags &= ~EF_IA_64_ABI64;
6185 }
6186 else if (strcmp (arg, "le") == 0)
6187 {
6188 md.flags &= ~EF_IA_64_BE;
6189 }
6190 else if (strcmp (arg, "be") == 0)
6191 {
6192 md.flags |= EF_IA_64_BE;
6193 }
6194 else
6195 return 0;
6196 break;
6197
6198 case 'N':
6199 if (strcmp (arg, "so") == 0)
6200 {
6201 /* Suppress signon message. */
6202 }
6203 else if (strcmp (arg, "pi") == 0)
6204 {
6205 /* Reject privileged instructions. FIXME */
6206 }
6207 else if (strcmp (arg, "us") == 0)
6208 {
6209 /* Allow union of signed and unsigned range. FIXME */
6210 }
6211 else if (strcmp (arg, "close_fcalls") == 0)
6212 {
6213 /* Do not resolve global function calls. */
6214 }
6215 else
6216 return 0;
6217 break;
6218
6219 case 'C':
6220 /* temp[="prefix"] Insert temporary labels into the object file
6221 symbol table prefixed by "prefix".
6222 Default prefix is ":temp:".
6223 */
6224 break;
6225
6226 case 'a':
6227 /* indirect=<tgt> Assume unannotated indirect branches behavior
6228 according to <tgt> --
6229 exit: branch out from the current context (default)
6230 labels: all labels in context may be branch targets
6231 */
6232 if (strncmp (arg, "indirect=", 9) != 0)
6233 return 0;
6234 break;
6235
6236 case 'x':
6237 /* -X conflicts with an ignored option, use -x instead */
6238 md.detect_dv = 1;
6239 if (!arg || strcmp (arg, "explicit") == 0)
6240 {
6241 /* set default mode to explicit */
6242 md.default_explicit_mode = 1;
6243 break;
6244 }
6245 else if (strcmp (arg, "auto") == 0)
6246 {
6247 md.default_explicit_mode = 0;
6248 }
6249 else if (strcmp (arg, "debug") == 0)
6250 {
6251 md.debug_dv = 1;
6252 }
6253 else if (strcmp (arg, "debugx") == 0)
6254 {
6255 md.default_explicit_mode = 1;
6256 md.debug_dv = 1;
6257 }
6258 else
6259 {
6260 as_bad (_("Unrecognized option '-x%s'"), arg);
6261 }
6262 break;
6263
6264 case 'S':
6265 /* nops Print nops statistics. */
6266 break;
6267
6268 /* GNU specific switches for gcc. */
6269 case OPTION_MCONSTANT_GP:
6270 md.flags |= EF_IA_64_CONS_GP;
6271 break;
6272
6273 case OPTION_MAUTO_PIC:
6274 md.flags |= EF_IA_64_NOFUNCDESC_CONS_GP;
6275 break;
6276
6277 default:
6278 return 0;
6279 }
6280
6281 return 1;
6282 }
6283
6284 void
6285 md_show_usage (stream)
6286 FILE *stream;
6287 {
6288 fputs (_("\
6289 IA-64 options:\n\
6290 -milp32|-milp64|-mlp64|-mp64 select data model (default -mlp64)\n\
6291 -mle | -mbe select little- or big-endian byte order (default -mle)\n\
6292 -x | -xexplicit turn on dependency violation checking (default)\n\
6293 -xauto automagically remove dependency violations\n\
6294 -xdebug debug dependency violation checker\n"),
6295 stream);
6296 }
6297
6298 /* Return true if TYPE fits in TEMPL at SLOT. */
6299
6300 static int
6301 match (int templ, int type, int slot)
6302 {
6303 enum ia64_unit unit;
6304 int result;
6305
6306 unit = ia64_templ_desc[templ].exec_unit[slot];
6307 switch (type)
6308 {
6309 case IA64_TYPE_DYN: result = 1; break; /* for nop and break */
6310 case IA64_TYPE_A:
6311 result = (unit == IA64_UNIT_I || unit == IA64_UNIT_M);
6312 break;
6313 case IA64_TYPE_X: result = (unit == IA64_UNIT_L); break;
6314 case IA64_TYPE_I: result = (unit == IA64_UNIT_I); break;
6315 case IA64_TYPE_M: result = (unit == IA64_UNIT_M); break;
6316 case IA64_TYPE_B: result = (unit == IA64_UNIT_B); break;
6317 case IA64_TYPE_F: result = (unit == IA64_UNIT_F); break;
6318 default: result = 0; break;
6319 }
6320 return result;
6321 }
6322
6323 /* Add a bit of extra goodness if a nop of type F or B would fit
6324 in TEMPL at SLOT. */
6325
6326 static inline int
6327 extra_goodness (int templ, int slot)
6328 {
6329 if (slot == 1 && match (templ, IA64_TYPE_F, slot))
6330 return 2;
6331 if (slot == 2 && match (templ, IA64_TYPE_B, slot))
6332 return 1;
6333 return 0;
6334 }
6335
6336 /* This function is called once, at assembler startup time. It sets
6337 up all the tables, etc. that the MD part of the assembler will need
6338 that can be determined before arguments are parsed. */
6339 void
6340 md_begin ()
6341 {
6342 int i, j, k, t, total, ar_base, cr_base, goodness, best, regnum, ok;
6343 const char *err;
6344 char name[8];
6345
6346 md.auto_align = 1;
6347 md.explicit_mode = md.default_explicit_mode;
6348
6349 bfd_set_section_alignment (stdoutput, text_section, 4);
6350
6351 target_big_endian = TARGET_BYTES_BIG_ENDIAN;
6352 pseudo_func[FUNC_FPTR_RELATIVE].u.sym =
6353 symbol_new (".<fptr>", undefined_section, FUNC_FPTR_RELATIVE,
6354 &zero_address_frag);
6355
6356 pseudo_func[FUNC_GP_RELATIVE].u.sym =
6357 symbol_new (".<gprel>", undefined_section, FUNC_GP_RELATIVE,
6358 &zero_address_frag);
6359
6360 pseudo_func[FUNC_LT_RELATIVE].u.sym =
6361 symbol_new (".<ltoff>", undefined_section, FUNC_LT_RELATIVE,
6362 &zero_address_frag);
6363
6364 pseudo_func[FUNC_PC_RELATIVE].u.sym =
6365 symbol_new (".<pcrel>", undefined_section, FUNC_PC_RELATIVE,
6366 &zero_address_frag);
6367
6368 pseudo_func[FUNC_PLT_RELATIVE].u.sym =
6369 symbol_new (".<pltoff>", undefined_section, FUNC_PLT_RELATIVE,
6370 &zero_address_frag);
6371
6372 pseudo_func[FUNC_SEC_RELATIVE].u.sym =
6373 symbol_new (".<secrel>", undefined_section, FUNC_SEC_RELATIVE,
6374 &zero_address_frag);
6375
6376 pseudo_func[FUNC_SEG_RELATIVE].u.sym =
6377 symbol_new (".<segrel>", undefined_section, FUNC_SEG_RELATIVE,
6378 &zero_address_frag);
6379
6380 pseudo_func[FUNC_LTV_RELATIVE].u.sym =
6381 symbol_new (".<ltv>", undefined_section, FUNC_LTV_RELATIVE,
6382 &zero_address_frag);
6383
6384 pseudo_func[FUNC_LT_FPTR_RELATIVE].u.sym =
6385 symbol_new (".<ltoff.fptr>", undefined_section, FUNC_LT_FPTR_RELATIVE,
6386 &zero_address_frag);
6387
6388 pseudo_func[FUNC_IPLT_RELOC].u.sym =
6389 symbol_new (".<iplt>", undefined_section, FUNC_IPLT_RELOC,
6390 &zero_address_frag);
6391
6392 /* Compute the table of best templates. We compute goodness as a
6393 base 4 value, in which each match counts for 3, each F counts
6394 for 2, each B counts for 1. This should maximize the number of
6395 F and B nops in the chosen bundles, which is good because these
6396 pipelines are least likely to be overcommitted. */
6397 for (i = 0; i < IA64_NUM_TYPES; ++i)
6398 for (j = 0; j < IA64_NUM_TYPES; ++j)
6399 for (k = 0; k < IA64_NUM_TYPES; ++k)
6400 {
6401 best = 0;
6402 for (t = 0; t < NELEMS (ia64_templ_desc); ++t)
6403 {
6404 goodness = 0;
6405 if (match (t, i, 0))
6406 {
6407 if (match (t, j, 1))
6408 {
6409 if (match (t, k, 2))
6410 goodness = 3 + 3 + 3;
6411 else
6412 goodness = 3 + 3 + extra_goodness (t, 2);
6413 }
6414 else if (match (t, j, 2))
6415 goodness = 3 + 3 + extra_goodness (t, 1);
6416 else
6417 {
6418 goodness = 3;
6419 goodness += extra_goodness (t, 1);
6420 goodness += extra_goodness (t, 2);
6421 }
6422 }
6423 else if (match (t, i, 1))
6424 {
6425 if (match (t, j, 2))
6426 goodness = 3 + 3;
6427 else
6428 goodness = 3 + extra_goodness (t, 2);
6429 }
6430 else if (match (t, i, 2))
6431 goodness = 3 + extra_goodness (t, 1);
6432
6433 if (goodness > best)
6434 {
6435 best = goodness;
6436 best_template[i][j][k] = t;
6437 }
6438 }
6439 }
6440
6441 for (i = 0; i < NUM_SLOTS; ++i)
6442 md.slot[i].user_template = -1;
6443
6444 md.pseudo_hash = hash_new ();
6445 for (i = 0; i < NELEMS (pseudo_opcode); ++i)
6446 {
6447 err = hash_insert (md.pseudo_hash, pseudo_opcode[i].name,
6448 (void *) (pseudo_opcode + i));
6449 if (err)
6450 as_fatal ("ia64.md_begin: can't hash `%s': %s",
6451 pseudo_opcode[i].name, err);
6452 }
6453
6454 md.reg_hash = hash_new ();
6455 md.dynreg_hash = hash_new ();
6456 md.const_hash = hash_new ();
6457 md.entry_hash = hash_new ();
6458
6459 /* general registers: */
6460
6461 total = 128;
6462 for (i = 0; i < total; ++i)
6463 {
6464 sprintf (name, "r%d", i - REG_GR);
6465 md.regsym[i] = declare_register (name, i);
6466 }
6467
6468 /* floating point registers: */
6469 total += 128;
6470 for (; i < total; ++i)
6471 {
6472 sprintf (name, "f%d", i - REG_FR);
6473 md.regsym[i] = declare_register (name, i);
6474 }
6475
6476 /* application registers: */
6477 total += 128;
6478 ar_base = i;
6479 for (; i < total; ++i)
6480 {
6481 sprintf (name, "ar%d", i - REG_AR);
6482 md.regsym[i] = declare_register (name, i);
6483 }
6484
6485 /* control registers: */
6486 total += 128;
6487 cr_base = i;
6488 for (; i < total; ++i)
6489 {
6490 sprintf (name, "cr%d", i - REG_CR);
6491 md.regsym[i] = declare_register (name, i);
6492 }
6493
6494 /* predicate registers: */
6495 total += 64;
6496 for (; i < total; ++i)
6497 {
6498 sprintf (name, "p%d", i - REG_P);
6499 md.regsym[i] = declare_register (name, i);
6500 }
6501
6502 /* branch registers: */
6503 total += 8;
6504 for (; i < total; ++i)
6505 {
6506 sprintf (name, "b%d", i - REG_BR);
6507 md.regsym[i] = declare_register (name, i);
6508 }
6509
6510 md.regsym[REG_IP] = declare_register ("ip", REG_IP);
6511 md.regsym[REG_CFM] = declare_register ("cfm", REG_CFM);
6512 md.regsym[REG_PR] = declare_register ("pr", REG_PR);
6513 md.regsym[REG_PR_ROT] = declare_register ("pr.rot", REG_PR_ROT);
6514 md.regsym[REG_PSR] = declare_register ("psr", REG_PSR);
6515 md.regsym[REG_PSR_L] = declare_register ("psr.l", REG_PSR_L);
6516 md.regsym[REG_PSR_UM] = declare_register ("psr.um", REG_PSR_UM);
6517
6518 for (i = 0; i < NELEMS (indirect_reg); ++i)
6519 {
6520 regnum = indirect_reg[i].regnum;
6521 md.regsym[regnum] = declare_register (indirect_reg[i].name, regnum);
6522 }
6523
6524 /* define synonyms for application registers: */
6525 for (i = REG_AR; i < REG_AR + NELEMS (ar); ++i)
6526 md.regsym[i] = declare_register (ar[i - REG_AR].name,
6527 REG_AR + ar[i - REG_AR].regnum);
6528
6529 /* define synonyms for control registers: */
6530 for (i = REG_CR; i < REG_CR + NELEMS (cr); ++i)
6531 md.regsym[i] = declare_register (cr[i - REG_CR].name,
6532 REG_CR + cr[i - REG_CR].regnum);
6533
6534 declare_register ("gp", REG_GR + 1);
6535 declare_register ("sp", REG_GR + 12);
6536 declare_register ("rp", REG_BR + 0);
6537
6538 /* pseudo-registers used to specify unwind info: */
6539 declare_register ("psp", REG_PSP);
6540
6541 declare_register_set ("ret", 4, REG_GR + 8);
6542 declare_register_set ("farg", 8, REG_FR + 8);
6543 declare_register_set ("fret", 8, REG_FR + 8);
6544
6545 for (i = 0; i < NELEMS (const_bits); ++i)
6546 {
6547 err = hash_insert (md.const_hash, const_bits[i].name,
6548 (PTR) (const_bits + i));
6549 if (err)
6550 as_fatal ("Inserting \"%s\" into constant hash table failed: %s",
6551 name, err);
6552 }
6553
6554 /* Set the architecture and machine depending on defaults and command line
6555 options. */
6556 if (md.flags & EF_IA_64_ABI64)
6557 ok = bfd_set_arch_mach (stdoutput, bfd_arch_ia64, bfd_mach_ia64_elf64);
6558 else
6559 ok = bfd_set_arch_mach (stdoutput, bfd_arch_ia64, bfd_mach_ia64_elf32);
6560
6561 if (! ok)
6562 as_warn (_("Could not set architecture and machine"));
6563
6564 /* Set the pointer size and pointer shift size depending on md.flags */
6565
6566 if (md.flags & EF_IA_64_ABI64)
6567 {
6568 md.pointer_size = 8; /* pointers are 8 bytes */
6569 md.pointer_size_shift = 3; /* alignment is 8 bytes = 2^2 */
6570 }
6571 else
6572 {
6573 md.pointer_size = 4; /* pointers are 4 bytes */
6574 md.pointer_size_shift = 2; /* alignment is 4 bytes = 2^2 */
6575 }
6576
6577 md.mem_offset.hint = 0;
6578 md.path = 0;
6579 md.maxpaths = 0;
6580 md.entry_labels = NULL;
6581 }
6582
6583 /* Set the elf type to 64 bit ABI by default. Cannot do this in md_begin
6584 because that is called after md_parse_option which is where we do the
6585 dynamic changing of md.flags based on -mlp64 or -milp32. Also, set the
6586 default endianness. */
6587
6588 void
6589 ia64_init (argc, argv)
6590 int argc ATTRIBUTE_UNUSED;
6591 char **argv ATTRIBUTE_UNUSED;
6592 {
6593 md.flags = EF_IA_64_ABI64;
6594 if (TARGET_BYTES_BIG_ENDIAN)
6595 md.flags |= EF_IA_64_BE;
6596 }
6597
6598 /* Return a string for the target object file format. */
6599
6600 const char *
6601 ia64_target_format ()
6602 {
6603 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
6604 {
6605 if (md.flags & EF_IA_64_BE)
6606 {
6607 if (md.flags & EF_IA_64_ABI64)
6608 #ifdef TE_AIX50
6609 return "elf64-ia64-aix-big";
6610 #else
6611 return "elf64-ia64-big";
6612 #endif
6613 else
6614 #ifdef TE_AIX50
6615 return "elf32-ia64-aix-big";
6616 #else
6617 return "elf32-ia64-big";
6618 #endif
6619 }
6620 else
6621 {
6622 if (md.flags & EF_IA_64_ABI64)
6623 #ifdef TE_AIX50
6624 return "elf64-ia64-aix-little";
6625 #else
6626 return "elf64-ia64-little";
6627 #endif
6628 else
6629 #ifdef TE_AIX50
6630 return "elf32-ia64-aix-little";
6631 #else
6632 return "elf32-ia64-little";
6633 #endif
6634 }
6635 }
6636 else
6637 return "unknown-format";
6638 }
6639
6640 void
6641 ia64_end_of_source ()
6642 {
6643 /* terminate insn group upon reaching end of file: */
6644 insn_group_break (1, 0, 0);
6645
6646 /* emits slots we haven't written yet: */
6647 ia64_flush_insns ();
6648
6649 bfd_set_private_flags (stdoutput, md.flags);
6650
6651 md.mem_offset.hint = 0;
6652 }
6653
6654 void
6655 ia64_start_line ()
6656 {
6657 if (md.qp.X_op == O_register)
6658 as_bad ("qualifying predicate not followed by instruction");
6659 md.qp.X_op = O_absent;
6660
6661 if (ignore_input ())
6662 return;
6663
6664 if (input_line_pointer[0] == ';' && input_line_pointer[-1] == ';')
6665 {
6666 if (md.detect_dv && !md.explicit_mode)
6667 as_warn (_("Explicit stops are ignored in auto mode"));
6668 else
6669 insn_group_break (1, 0, 0);
6670 }
6671 }
6672
6673 /* This is a hook for ia64_frob_label, so that it can distinguish tags from
6674 labels. */
6675 static int defining_tag = 0;
6676
6677 int
6678 ia64_unrecognized_line (ch)
6679 int ch;
6680 {
6681 switch (ch)
6682 {
6683 case '(':
6684 expression (&md.qp);
6685 if (*input_line_pointer++ != ')')
6686 {
6687 as_bad ("Expected ')'");
6688 return 0;
6689 }
6690 if (md.qp.X_op != O_register)
6691 {
6692 as_bad ("Qualifying predicate expected");
6693 return 0;
6694 }
6695 if (md.qp.X_add_number < REG_P || md.qp.X_add_number >= REG_P + 64)
6696 {
6697 as_bad ("Predicate register expected");
6698 return 0;
6699 }
6700 return 1;
6701
6702 case '{':
6703 if (md.manual_bundling)
6704 as_warn ("Found '{' when manual bundling is already turned on");
6705 else
6706 CURR_SLOT.manual_bundling_on = 1;
6707 md.manual_bundling = 1;
6708
6709 /* Bundling is only acceptable in explicit mode
6710 or when in default automatic mode. */
6711 if (md.detect_dv && !md.explicit_mode)
6712 {
6713 if (!md.mode_explicitly_set
6714 && !md.default_explicit_mode)
6715 dot_dv_mode ('E');
6716 else
6717 as_warn (_("Found '{' after explicit switch to automatic mode"));
6718 }
6719 return 1;
6720
6721 case '}':
6722 if (!md.manual_bundling)
6723 as_warn ("Found '}' when manual bundling is off");
6724 else
6725 PREV_SLOT.manual_bundling_off = 1;
6726 md.manual_bundling = 0;
6727
6728 /* switch back to automatic mode, if applicable */
6729 if (md.detect_dv
6730 && md.explicit_mode
6731 && !md.mode_explicitly_set
6732 && !md.default_explicit_mode)
6733 dot_dv_mode ('A');
6734
6735 /* Allow '{' to follow on the same line. We also allow ";;", but that
6736 happens automatically because ';' is an end of line marker. */
6737 SKIP_WHITESPACE ();
6738 if (input_line_pointer[0] == '{')
6739 {
6740 input_line_pointer++;
6741 return ia64_unrecognized_line ('{');
6742 }
6743
6744 demand_empty_rest_of_line ();
6745 return 1;
6746
6747 case '[':
6748 {
6749 char *s;
6750 char c;
6751 symbolS *tag;
6752 int temp;
6753
6754 if (md.qp.X_op == O_register)
6755 {
6756 as_bad ("Tag must come before qualifying predicate.");
6757 return 0;
6758 }
6759
6760 /* This implements just enough of read_a_source_file in read.c to
6761 recognize labels. */
6762 if (is_name_beginner (*input_line_pointer))
6763 {
6764 s = input_line_pointer;
6765 c = get_symbol_end ();
6766 }
6767 else if (LOCAL_LABELS_FB
6768 && isdigit ((unsigned char) *input_line_pointer))
6769 {
6770 temp = 0;
6771 while (isdigit ((unsigned char) *input_line_pointer))
6772 temp = (temp * 10) + *input_line_pointer++ - '0';
6773 fb_label_instance_inc (temp);
6774 s = fb_label_name (temp, 0);
6775 c = *input_line_pointer;
6776 }
6777 else
6778 {
6779 s = NULL;
6780 c = '\0';
6781 }
6782 if (c != ':')
6783 {
6784 /* Put ':' back for error messages' sake. */
6785 *input_line_pointer++ = ':';
6786 as_bad ("Expected ':'");
6787 return 0;
6788 }
6789
6790 defining_tag = 1;
6791 tag = colon (s);
6792 defining_tag = 0;
6793 /* Put ':' back for error messages' sake. */
6794 *input_line_pointer++ = ':';
6795 if (*input_line_pointer++ != ']')
6796 {
6797 as_bad ("Expected ']'");
6798 return 0;
6799 }
6800 if (! tag)
6801 {
6802 as_bad ("Tag name expected");
6803 return 0;
6804 }
6805 return 1;
6806 }
6807
6808 default:
6809 break;
6810 }
6811
6812 /* Not a valid line. */
6813 return 0;
6814 }
6815
6816 void
6817 ia64_frob_label (sym)
6818 struct symbol *sym;
6819 {
6820 struct label_fix *fix;
6821
6822 /* Tags need special handling since they are not bundle breaks like
6823 labels. */
6824 if (defining_tag)
6825 {
6826 fix = obstack_alloc (&notes, sizeof (*fix));
6827 fix->sym = sym;
6828 fix->next = CURR_SLOT.tag_fixups;
6829 CURR_SLOT.tag_fixups = fix;
6830
6831 return;
6832 }
6833
6834 if (bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE)
6835 {
6836 md.last_text_seg = now_seg;
6837 fix = obstack_alloc (&notes, sizeof (*fix));
6838 fix->sym = sym;
6839 fix->next = CURR_SLOT.label_fixups;
6840 CURR_SLOT.label_fixups = fix;
6841
6842 /* Keep track of how many code entry points we've seen. */
6843 if (md.path == md.maxpaths)
6844 {
6845 md.maxpaths += 20;
6846 md.entry_labels = (const char **)
6847 xrealloc ((void *) md.entry_labels,
6848 md.maxpaths * sizeof (char *));
6849 }
6850 md.entry_labels[md.path++] = S_GET_NAME (sym);
6851 }
6852 }
6853
6854 void
6855 ia64_flush_pending_output ()
6856 {
6857 if (!md.keep_pending_output
6858 && bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE)
6859 {
6860 /* ??? This causes many unnecessary stop bits to be emitted.
6861 Unfortunately, it isn't clear if it is safe to remove this. */
6862 insn_group_break (1, 0, 0);
6863 ia64_flush_insns ();
6864 }
6865 }
6866
6867 /* Do ia64-specific expression optimization. All that's done here is
6868 to transform index expressions that are either due to the indexing
6869 of rotating registers or due to the indexing of indirect register
6870 sets. */
6871 int
6872 ia64_optimize_expr (l, op, r)
6873 expressionS *l;
6874 operatorT op;
6875 expressionS *r;
6876 {
6877 unsigned num_regs;
6878
6879 if (op == O_index)
6880 {
6881 if (l->X_op == O_register && r->X_op == O_constant)
6882 {
6883 num_regs = (l->X_add_number >> 16);
6884 if ((unsigned) r->X_add_number >= num_regs)
6885 {
6886 if (!num_regs)
6887 as_bad ("No current frame");
6888 else
6889 as_bad ("Index out of range 0..%u", num_regs - 1);
6890 r->X_add_number = 0;
6891 }
6892 l->X_add_number = (l->X_add_number & 0xffff) + r->X_add_number;
6893 return 1;
6894 }
6895 else if (l->X_op == O_register && r->X_op == O_register)
6896 {
6897 if (l->X_add_number < IND_CPUID || l->X_add_number > IND_RR
6898 || l->X_add_number == IND_MEM)
6899 {
6900 as_bad ("Indirect register set name expected");
6901 l->X_add_number = IND_CPUID;
6902 }
6903 l->X_op = O_index;
6904 l->X_op_symbol = md.regsym[l->X_add_number];
6905 l->X_add_number = r->X_add_number;
6906 return 1;
6907 }
6908 }
6909 return 0;
6910 }
6911
6912 int
6913 ia64_parse_name (name, e)
6914 char *name;
6915 expressionS *e;
6916 {
6917 struct const_desc *cdesc;
6918 struct dynreg *dr = 0;
6919 unsigned int regnum;
6920 struct symbol *sym;
6921 char *end;
6922
6923 /* first see if NAME is a known register name: */
6924 sym = hash_find (md.reg_hash, name);
6925 if (sym)
6926 {
6927 e->X_op = O_register;
6928 e->X_add_number = S_GET_VALUE (sym);
6929 return 1;
6930 }
6931
6932 cdesc = hash_find (md.const_hash, name);
6933 if (cdesc)
6934 {
6935 e->X_op = O_constant;
6936 e->X_add_number = cdesc->value;
6937 return 1;
6938 }
6939
6940 /* check for inN, locN, or outN: */
6941 switch (name[0])
6942 {
6943 case 'i':
6944 if (name[1] == 'n' && isdigit (name[2]))
6945 {
6946 dr = &md.in;
6947 name += 2;
6948 }
6949 break;
6950
6951 case 'l':
6952 if (name[1] == 'o' && name[2] == 'c' && isdigit (name[3]))
6953 {
6954 dr = &md.loc;
6955 name += 3;
6956 }
6957 break;
6958
6959 case 'o':
6960 if (name[1] == 'u' && name[2] == 't' && isdigit (name[3]))
6961 {
6962 dr = &md.out;
6963 name += 3;
6964 }
6965 break;
6966
6967 default:
6968 break;
6969 }
6970
6971 if (dr)
6972 {
6973 /* The name is inN, locN, or outN; parse the register number. */
6974 regnum = strtoul (name, &end, 10);
6975 if (end > name && *end == '\0')
6976 {
6977 if ((unsigned) regnum >= dr->num_regs)
6978 {
6979 if (!dr->num_regs)
6980 as_bad ("No current frame");
6981 else
6982 as_bad ("Register number out of range 0..%u",
6983 dr->num_regs - 1);
6984 regnum = 0;
6985 }
6986 e->X_op = O_register;
6987 e->X_add_number = dr->base + regnum;
6988 return 1;
6989 }
6990 }
6991
6992 if ((dr = hash_find (md.dynreg_hash, name)))
6993 {
6994 /* We've got ourselves the name of a rotating register set.
6995 Store the base register number in the low 16 bits of
6996 X_add_number and the size of the register set in the top 16
6997 bits. */
6998 e->X_op = O_register;
6999 e->X_add_number = dr->base | (dr->num_regs << 16);
7000 return 1;
7001 }
7002 return 0;
7003 }
7004
7005 /* Remove the '#' suffix that indicates a symbol as opposed to a register. */
7006
7007 char *
7008 ia64_canonicalize_symbol_name (name)
7009 char *name;
7010 {
7011 size_t len = strlen (name);
7012 if (len > 1 && name[len - 1] == '#')
7013 name[len - 1] = '\0';
7014 return name;
7015 }
7016
7017 /* Return true if idesc is a conditional branch instruction. This excludes
7018 the modulo scheduled branches, and br.ia. Mod-sched branches are excluded
7019 because they always read/write resources regardless of the value of the
7020 qualifying predicate. br.ia must always use p0, and hence is always
7021 taken. Thus this function returns true for branches which can fall
7022 through, and which use no resources if they do fall through. */
7023
7024 static int
7025 is_conditional_branch (idesc)
7026 struct ia64_opcode *idesc;
7027 {
7028 /* br is a conditional branch. Everything that starts with br. except
7029 br.ia, br.c{loop,top,exit}, and br.w{top,exit} is a conditional branch.
7030 Everything that starts with brl is a conditional branch. */
7031 return (idesc->name[0] == 'b' && idesc->name[1] == 'r'
7032 && (idesc->name[2] == '\0'
7033 || (idesc->name[2] == '.' && idesc->name[3] != 'i'
7034 && idesc->name[3] != 'c' && idesc->name[3] != 'w')
7035 || idesc->name[2] == 'l'
7036 /* br.cond, br.call, br.clr */
7037 || (idesc->name[2] == '.' && idesc->name[3] == 'c'
7038 && (idesc->name[4] == 'a' || idesc->name[4] == 'o'
7039 || (idesc->name[4] == 'l' && idesc->name[5] == 'r')))));
7040 }
7041
7042 /* Return whether the given opcode is a taken branch. If there's any doubt,
7043 returns zero. */
7044
7045 static int
7046 is_taken_branch (idesc)
7047 struct ia64_opcode *idesc;
7048 {
7049 return ((is_conditional_branch (idesc) && CURR_SLOT.qp_regno == 0)
7050 || strncmp (idesc->name, "br.ia", 5) == 0);
7051 }
7052
7053 /* Return whether the given opcode is an interruption or rfi. If there's any
7054 doubt, returns zero. */
7055
7056 static int
7057 is_interruption_or_rfi (idesc)
7058 struct ia64_opcode *idesc;
7059 {
7060 if (strcmp (idesc->name, "rfi") == 0)
7061 return 1;
7062 return 0;
7063 }
7064
7065 /* Returns the index of the given dependency in the opcode's list of chks, or
7066 -1 if there is no dependency. */
7067
7068 static int
7069 depends_on (depind, idesc)
7070 int depind;
7071 struct ia64_opcode *idesc;
7072 {
7073 int i;
7074 const struct ia64_opcode_dependency *dep = idesc->dependencies;
7075 for (i = 0; i < dep->nchks; i++)
7076 {
7077 if (depind == DEP (dep->chks[i]))
7078 return i;
7079 }
7080 return -1;
7081 }
7082
7083 /* Determine a set of specific resources used for a particular resource
7084 class. Returns the number of specific resources identified For those
7085 cases which are not determinable statically, the resource returned is
7086 marked nonspecific.
7087
7088 Meanings of value in 'NOTE':
7089 1) only read/write when the register number is explicitly encoded in the
7090 insn.
7091 2) only read CFM when accessing a rotating GR, FR, or PR. mov pr only
7092 accesses CFM when qualifying predicate is in the rotating region.
7093 3) general register value is used to specify an indirect register; not
7094 determinable statically.
7095 4) only read the given resource when bits 7:0 of the indirect index
7096 register value does not match the register number of the resource; not
7097 determinable statically.
7098 5) all rules are implementation specific.
7099 6) only when both the index specified by the reader and the index specified
7100 by the writer have the same value in bits 63:61; not determinable
7101 statically.
7102 7) only access the specified resource when the corresponding mask bit is
7103 set
7104 8) PSR.dfh is only read when these insns reference FR32-127. PSR.dfl is
7105 only read when these insns reference FR2-31
7106 9) PSR.mfl is only written when these insns write FR2-31. PSR.mfh is only
7107 written when these insns write FR32-127
7108 10) The PSR.bn bit is only accessed when one of GR16-31 is specified in the
7109 instruction
7110 11) The target predicates are written independently of PR[qp], but source
7111 registers are only read if PR[qp] is true. Since the state of PR[qp]
7112 cannot statically be determined, all source registers are marked used.
7113 12) This insn only reads the specified predicate register when that
7114 register is the PR[qp].
7115 13) This reference to ld-c only applies to teh GR whose value is loaded
7116 with data returned from memory, not the post-incremented address register.
7117 14) The RSE resource includes the implementation-specific RSE internal
7118 state resources. At least one (and possibly more) of these resources are
7119 read by each instruction listed in IC:rse-readers. At least one (and
7120 possibly more) of these resources are written by each insn listed in
7121 IC:rse-writers.
7122 15+16) Represents reserved instructions, which the assembler does not
7123 generate.
7124
7125 Memory resources (i.e. locations in memory) are *not* marked or tracked by
7126 this code; there are no dependency violations based on memory access.
7127 */
7128
7129 #define MAX_SPECS 256
7130 #define DV_CHK 1
7131 #define DV_REG 0
7132
7133 static int
7134 specify_resource (dep, idesc, type, specs, note, path)
7135 const struct ia64_dependency *dep;
7136 struct ia64_opcode *idesc;
7137 int type; /* is this a DV chk or a DV reg? */
7138 struct rsrc specs[MAX_SPECS]; /* returned specific resources */
7139 int note; /* resource note for this insn's usage */
7140 int path; /* which execution path to examine */
7141 {
7142 int count = 0;
7143 int i;
7144 int rsrc_write = 0;
7145 struct rsrc tmpl;
7146
7147 if (dep->mode == IA64_DV_WAW
7148 || (dep->mode == IA64_DV_RAW && type == DV_REG)
7149 || (dep->mode == IA64_DV_WAR && type == DV_CHK))
7150 rsrc_write = 1;
7151
7152 /* template for any resources we identify */
7153 tmpl.dependency = dep;
7154 tmpl.note = note;
7155 tmpl.insn_srlz = tmpl.data_srlz = 0;
7156 tmpl.qp_regno = CURR_SLOT.qp_regno;
7157 tmpl.link_to_qp_branch = 1;
7158 tmpl.mem_offset.hint = 0;
7159 tmpl.specific = 1;
7160 tmpl.index = 0;
7161 tmpl.cmp_type = CMP_NONE;
7162
7163 #define UNHANDLED \
7164 as_warn (_("Unhandled dependency %s for %s (%s), note %d"), \
7165 dep->name, idesc->name, (rsrc_write?"write":"read"), note)
7166 #define KNOWN(REG) (gr_values[REG].known && gr_values[REG].path >= path)
7167
7168 /* we don't need to track these */
7169 if (dep->semantics == IA64_DVS_NONE)
7170 return 0;
7171
7172 switch (dep->specifier)
7173 {
7174 case IA64_RS_AR_K:
7175 if (note == 1)
7176 {
7177 if (idesc->operands[!rsrc_write] == IA64_OPND_AR3)
7178 {
7179 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_AR;
7180 if (regno >= 0 && regno <= 7)
7181 {
7182 specs[count] = tmpl;
7183 specs[count++].index = regno;
7184 }
7185 }
7186 }
7187 else if (note == 0)
7188 {
7189 for (i = 0; i < 8; i++)
7190 {
7191 specs[count] = tmpl;
7192 specs[count++].index = i;
7193 }
7194 }
7195 else
7196 {
7197 UNHANDLED;
7198 }
7199 break;
7200
7201 case IA64_RS_AR_UNAT:
7202 /* This is a mov =AR or mov AR= instruction. */
7203 if (idesc->operands[!rsrc_write] == IA64_OPND_AR3)
7204 {
7205 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_AR;
7206 if (regno == AR_UNAT)
7207 {
7208 specs[count++] = tmpl;
7209 }
7210 }
7211 else
7212 {
7213 /* This is a spill/fill, or other instruction that modifies the
7214 unat register. */
7215
7216 /* Unless we can determine the specific bits used, mark the whole
7217 thing; bits 8:3 of the memory address indicate the bit used in
7218 UNAT. The .mem.offset hint may be used to eliminate a small
7219 subset of conflicts. */
7220 specs[count] = tmpl;
7221 if (md.mem_offset.hint)
7222 {
7223 if (md.debug_dv)
7224 fprintf (stderr, " Using hint for spill/fill\n");
7225 /* The index isn't actually used, just set it to something
7226 approximating the bit index. */
7227 specs[count].index = (md.mem_offset.offset >> 3) & 0x3F;
7228 specs[count].mem_offset.hint = 1;
7229 specs[count].mem_offset.offset = md.mem_offset.offset;
7230 specs[count++].mem_offset.base = md.mem_offset.base;
7231 }
7232 else
7233 {
7234 specs[count++].specific = 0;
7235 }
7236 }
7237 break;
7238
7239 case IA64_RS_AR:
7240 if (note == 1)
7241 {
7242 if (idesc->operands[!rsrc_write] == IA64_OPND_AR3)
7243 {
7244 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_AR;
7245 if ((regno >= 8 && regno <= 15)
7246 || (regno >= 20 && regno <= 23)
7247 || (regno >= 31 && regno <= 39)
7248 || (regno >= 41 && regno <= 47)
7249 || (regno >= 67 && regno <= 111))
7250 {
7251 specs[count] = tmpl;
7252 specs[count++].index = regno;
7253 }
7254 }
7255 }
7256 else
7257 {
7258 UNHANDLED;
7259 }
7260 break;
7261
7262 case IA64_RS_ARb:
7263 if (note == 1)
7264 {
7265 if (idesc->operands[!rsrc_write] == IA64_OPND_AR3)
7266 {
7267 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_AR;
7268 if ((regno >= 48 && regno <= 63)
7269 || (regno >= 112 && regno <= 127))
7270 {
7271 specs[count] = tmpl;
7272 specs[count++].index = regno;
7273 }
7274 }
7275 }
7276 else if (note == 0)
7277 {
7278 for (i = 48; i < 64; i++)
7279 {
7280 specs[count] = tmpl;
7281 specs[count++].index = i;
7282 }
7283 for (i = 112; i < 128; i++)
7284 {
7285 specs[count] = tmpl;
7286 specs[count++].index = i;
7287 }
7288 }
7289 else
7290 {
7291 UNHANDLED;
7292 }
7293 break;
7294
7295 case IA64_RS_BR:
7296 if (note != 1)
7297 {
7298 UNHANDLED;
7299 }
7300 else
7301 {
7302 if (rsrc_write)
7303 {
7304 for (i = 0; i < idesc->num_outputs; i++)
7305 if (idesc->operands[i] == IA64_OPND_B1
7306 || idesc->operands[i] == IA64_OPND_B2)
7307 {
7308 specs[count] = tmpl;
7309 specs[count++].index =
7310 CURR_SLOT.opnd[i].X_add_number - REG_BR;
7311 }
7312 }
7313 else
7314 {
7315 for (i = idesc->num_outputs;i < NELEMS (idesc->operands); i++)
7316 if (idesc->operands[i] == IA64_OPND_B1
7317 || idesc->operands[i] == IA64_OPND_B2)
7318 {
7319 specs[count] = tmpl;
7320 specs[count++].index =
7321 CURR_SLOT.opnd[i].X_add_number - REG_BR;
7322 }
7323 }
7324 }
7325 break;
7326
7327 case IA64_RS_CPUID: /* four or more registers */
7328 if (note == 3)
7329 {
7330 if (idesc->operands[!rsrc_write] == IA64_OPND_CPUID_R3)
7331 {
7332 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_GR;
7333 if (regno >= 0 && regno < NELEMS (gr_values)
7334 && KNOWN (regno))
7335 {
7336 specs[count] = tmpl;
7337 specs[count++].index = gr_values[regno].value & 0xFF;
7338 }
7339 else
7340 {
7341 specs[count] = tmpl;
7342 specs[count++].specific = 0;
7343 }
7344 }
7345 }
7346 else
7347 {
7348 UNHANDLED;
7349 }
7350 break;
7351
7352 case IA64_RS_DBR: /* four or more registers */
7353 if (note == 3)
7354 {
7355 if (idesc->operands[!rsrc_write] == IA64_OPND_DBR_R3)
7356 {
7357 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_GR;
7358 if (regno >= 0 && regno < NELEMS (gr_values)
7359 && KNOWN (regno))
7360 {
7361 specs[count] = tmpl;
7362 specs[count++].index = gr_values[regno].value & 0xFF;
7363 }
7364 else
7365 {
7366 specs[count] = tmpl;
7367 specs[count++].specific = 0;
7368 }
7369 }
7370 }
7371 else if (note == 0 && !rsrc_write)
7372 {
7373 specs[count] = tmpl;
7374 specs[count++].specific = 0;
7375 }
7376 else
7377 {
7378 UNHANDLED;
7379 }
7380 break;
7381
7382 case IA64_RS_IBR: /* four or more registers */
7383 if (note == 3)
7384 {
7385 if (idesc->operands[!rsrc_write] == IA64_OPND_IBR_R3)
7386 {
7387 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_GR;
7388 if (regno >= 0 && regno < NELEMS (gr_values)
7389 && KNOWN (regno))
7390 {
7391 specs[count] = tmpl;
7392 specs[count++].index = gr_values[regno].value & 0xFF;
7393 }
7394 else
7395 {
7396 specs[count] = tmpl;
7397 specs[count++].specific = 0;
7398 }
7399 }
7400 }
7401 else
7402 {
7403 UNHANDLED;
7404 }
7405 break;
7406
7407 case IA64_RS_MSR:
7408 if (note == 5)
7409 {
7410 /* These are implementation specific. Force all references to
7411 conflict with all other references. */
7412 specs[count] = tmpl;
7413 specs[count++].specific = 0;
7414 }
7415 else
7416 {
7417 UNHANDLED;
7418 }
7419 break;
7420
7421 case IA64_RS_PKR: /* 16 or more registers */
7422 if (note == 3 || note == 4)
7423 {
7424 if (idesc->operands[!rsrc_write] == IA64_OPND_PKR_R3)
7425 {
7426 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_GR;
7427 if (regno >= 0 && regno < NELEMS (gr_values)
7428 && KNOWN (regno))
7429 {
7430 if (note == 3)
7431 {
7432 specs[count] = tmpl;
7433 specs[count++].index = gr_values[regno].value & 0xFF;
7434 }
7435 else
7436 for (i = 0; i < NELEMS (gr_values); i++)
7437 {
7438 /* Uses all registers *except* the one in R3. */
7439 if ((unsigned)i != (gr_values[regno].value & 0xFF))
7440 {
7441 specs[count] = tmpl;
7442 specs[count++].index = i;
7443 }
7444 }
7445 }
7446 else
7447 {
7448 specs[count] = tmpl;
7449 specs[count++].specific = 0;
7450 }
7451 }
7452 }
7453 else if (note == 0)
7454 {
7455 /* probe et al. */
7456 specs[count] = tmpl;
7457 specs[count++].specific = 0;
7458 }
7459 break;
7460
7461 case IA64_RS_PMC: /* four or more registers */
7462 if (note == 3)
7463 {
7464 if (idesc->operands[!rsrc_write] == IA64_OPND_PMC_R3
7465 || (!rsrc_write && idesc->operands[1] == IA64_OPND_PMD_R3))
7466
7467 {
7468 int index = ((idesc->operands[1] == IA64_OPND_R3 && !rsrc_write)
7469 ? 1 : !rsrc_write);
7470 int regno = CURR_SLOT.opnd[index].X_add_number - REG_GR;
7471 if (regno >= 0 && regno < NELEMS (gr_values)
7472 && KNOWN (regno))
7473 {
7474 specs[count] = tmpl;
7475 specs[count++].index = gr_values[regno].value & 0xFF;
7476 }
7477 else
7478 {
7479 specs[count] = tmpl;
7480 specs[count++].specific = 0;
7481 }
7482 }
7483 }
7484 else
7485 {
7486 UNHANDLED;
7487 }
7488 break;
7489
7490 case IA64_RS_PMD: /* four or more registers */
7491 if (note == 3)
7492 {
7493 if (idesc->operands[!rsrc_write] == IA64_OPND_PMD_R3)
7494 {
7495 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_GR;
7496 if (regno >= 0 && regno < NELEMS (gr_values)
7497 && KNOWN (regno))
7498 {
7499 specs[count] = tmpl;
7500 specs[count++].index = gr_values[regno].value & 0xFF;
7501 }
7502 else
7503 {
7504 specs[count] = tmpl;
7505 specs[count++].specific = 0;
7506 }
7507 }
7508 }
7509 else
7510 {
7511 UNHANDLED;
7512 }
7513 break;
7514
7515 case IA64_RS_RR: /* eight registers */
7516 if (note == 6)
7517 {
7518 if (idesc->operands[!rsrc_write] == IA64_OPND_RR_R3)
7519 {
7520 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_GR;
7521 if (regno >= 0 && regno < NELEMS (gr_values)
7522 && KNOWN (regno))
7523 {
7524 specs[count] = tmpl;
7525 specs[count++].index = (gr_values[regno].value >> 61) & 0x7;
7526 }
7527 else
7528 {
7529 specs[count] = tmpl;
7530 specs[count++].specific = 0;
7531 }
7532 }
7533 }
7534 else if (note == 0 && !rsrc_write)
7535 {
7536 specs[count] = tmpl;
7537 specs[count++].specific = 0;
7538 }
7539 else
7540 {
7541 UNHANDLED;
7542 }
7543 break;
7544
7545 case IA64_RS_CR_IRR:
7546 if (note == 0)
7547 {
7548 /* handle mov-from-CR-IVR; it's a read that writes CR[IRR] */
7549 int regno = CURR_SLOT.opnd[1].X_add_number - REG_CR;
7550 if (rsrc_write
7551 && idesc->operands[1] == IA64_OPND_CR3
7552 && regno == CR_IVR)
7553 {
7554 for (i = 0; i < 4; i++)
7555 {
7556 specs[count] = tmpl;
7557 specs[count++].index = CR_IRR0 + i;
7558 }
7559 }
7560 }
7561 else if (note == 1)
7562 {
7563 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_CR;
7564 if (idesc->operands[!rsrc_write] == IA64_OPND_CR3
7565 && regno >= CR_IRR0
7566 && regno <= CR_IRR3)
7567 {
7568 specs[count] = tmpl;
7569 specs[count++].index = regno;
7570 }
7571 }
7572 else
7573 {
7574 UNHANDLED;
7575 }
7576 break;
7577
7578 case IA64_RS_CR_LRR:
7579 if (note != 1)
7580 {
7581 UNHANDLED;
7582 }
7583 else
7584 {
7585 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_CR;
7586 if (idesc->operands[!rsrc_write] == IA64_OPND_CR3
7587 && (regno == CR_LRR0 || regno == CR_LRR1))
7588 {
7589 specs[count] = tmpl;
7590 specs[count++].index = regno;
7591 }
7592 }
7593 break;
7594
7595 case IA64_RS_CR:
7596 if (note == 1)
7597 {
7598 if (idesc->operands[!rsrc_write] == IA64_OPND_CR3)
7599 {
7600 specs[count] = tmpl;
7601 specs[count++].index =
7602 CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_CR;
7603 }
7604 }
7605 else
7606 {
7607 UNHANDLED;
7608 }
7609 break;
7610
7611 case IA64_RS_FR:
7612 case IA64_RS_FRb:
7613 if (note != 1)
7614 {
7615 UNHANDLED;
7616 }
7617 else if (rsrc_write)
7618 {
7619 if (dep->specifier == IA64_RS_FRb
7620 && idesc->operands[0] == IA64_OPND_F1)
7621 {
7622 specs[count] = tmpl;
7623 specs[count++].index = CURR_SLOT.opnd[0].X_add_number - REG_FR;
7624 }
7625 }
7626 else
7627 {
7628 for (i = idesc->num_outputs; i < NELEMS (idesc->operands); i++)
7629 {
7630 if (idesc->operands[i] == IA64_OPND_F2
7631 || idesc->operands[i] == IA64_OPND_F3
7632 || idesc->operands[i] == IA64_OPND_F4)
7633 {
7634 specs[count] = tmpl;
7635 specs[count++].index =
7636 CURR_SLOT.opnd[i].X_add_number - REG_FR;
7637 }
7638 }
7639 }
7640 break;
7641
7642 case IA64_RS_GR:
7643 if (note == 13)
7644 {
7645 /* This reference applies only to the GR whose value is loaded with
7646 data returned from memory. */
7647 specs[count] = tmpl;
7648 specs[count++].index = CURR_SLOT.opnd[0].X_add_number - REG_GR;
7649 }
7650 else if (note == 1)
7651 {
7652 if (rsrc_write)
7653 {
7654 for (i = 0; i < idesc->num_outputs; i++)
7655 if (idesc->operands[i] == IA64_OPND_R1
7656 || idesc->operands[i] == IA64_OPND_R2
7657 || idesc->operands[i] == IA64_OPND_R3)
7658 {
7659 specs[count] = tmpl;
7660 specs[count++].index =
7661 CURR_SLOT.opnd[i].X_add_number - REG_GR;
7662 }
7663 if (idesc->flags & IA64_OPCODE_POSTINC)
7664 for (i = 0; i < NELEMS (idesc->operands); i++)
7665 if (idesc->operands[i] == IA64_OPND_MR3)
7666 {
7667 specs[count] = tmpl;
7668 specs[count++].index =
7669 CURR_SLOT.opnd[i].X_add_number - REG_GR;
7670 }
7671 }
7672 else
7673 {
7674 /* Look for anything that reads a GR. */
7675 for (i = 0; i < NELEMS (idesc->operands); i++)
7676 {
7677 if (idesc->operands[i] == IA64_OPND_MR3
7678 || idesc->operands[i] == IA64_OPND_CPUID_R3
7679 || idesc->operands[i] == IA64_OPND_DBR_R3
7680 || idesc->operands[i] == IA64_OPND_IBR_R3
7681 || idesc->operands[i] == IA64_OPND_MSR_R3
7682 || idesc->operands[i] == IA64_OPND_PKR_R3
7683 || idesc->operands[i] == IA64_OPND_PMC_R3
7684 || idesc->operands[i] == IA64_OPND_PMD_R3
7685 || idesc->operands[i] == IA64_OPND_RR_R3
7686 || ((i >= idesc->num_outputs)
7687 && (idesc->operands[i] == IA64_OPND_R1
7688 || idesc->operands[i] == IA64_OPND_R2
7689 || idesc->operands[i] == IA64_OPND_R3
7690 /* addl source register. */
7691 || idesc->operands[i] == IA64_OPND_R3_2)))
7692 {
7693 specs[count] = tmpl;
7694 specs[count++].index =
7695 CURR_SLOT.opnd[i].X_add_number - REG_GR;
7696 }
7697 }
7698 }
7699 }
7700 else
7701 {
7702 UNHANDLED;
7703 }
7704 break;
7705
7706 /* This is the same as IA64_RS_PRr, except that the register range is
7707 from 1 - 15, and there are no rotating register reads/writes here. */
7708 case IA64_RS_PR:
7709 if (note == 0)
7710 {
7711 for (i = 1; i < 16; i++)
7712 {
7713 specs[count] = tmpl;
7714 specs[count++].index = i;
7715 }
7716 }
7717 else if (note == 7)
7718 {
7719 valueT mask = 0;
7720 /* Mark only those registers indicated by the mask. */
7721 if (rsrc_write)
7722 {
7723 mask = CURR_SLOT.opnd[2].X_add_number;
7724 for (i = 1; i < 16; i++)
7725 if (mask & ((valueT) 1 << i))
7726 {
7727 specs[count] = tmpl;
7728 specs[count++].index = i;
7729 }
7730 }
7731 else
7732 {
7733 UNHANDLED;
7734 }
7735 }
7736 else if (note == 11) /* note 11 implies note 1 as well */
7737 {
7738 if (rsrc_write)
7739 {
7740 for (i = 0; i < idesc->num_outputs; i++)
7741 {
7742 if (idesc->operands[i] == IA64_OPND_P1
7743 || idesc->operands[i] == IA64_OPND_P2)
7744 {
7745 int regno = CURR_SLOT.opnd[i].X_add_number - REG_P;
7746 if (regno >= 1 && regno < 16)
7747 {
7748 specs[count] = tmpl;
7749 specs[count++].index = regno;
7750 }
7751 }
7752 }
7753 }
7754 else
7755 {
7756 UNHANDLED;
7757 }
7758 }
7759 else if (note == 12)
7760 {
7761 if (CURR_SLOT.qp_regno >= 1 && CURR_SLOT.qp_regno < 16)
7762 {
7763 specs[count] = tmpl;
7764 specs[count++].index = CURR_SLOT.qp_regno;
7765 }
7766 }
7767 else if (note == 1)
7768 {
7769 if (rsrc_write)
7770 {
7771 int p1 = CURR_SLOT.opnd[0].X_add_number - REG_P;
7772 int p2 = CURR_SLOT.opnd[1].X_add_number - REG_P;
7773 int or_andcm = strstr (idesc->name, "or.andcm") != NULL;
7774 int and_orcm = strstr (idesc->name, "and.orcm") != NULL;
7775
7776 if ((idesc->operands[0] == IA64_OPND_P1
7777 || idesc->operands[0] == IA64_OPND_P2)
7778 && p1 >= 1 && p1 < 16)
7779 {
7780 specs[count] = tmpl;
7781 specs[count].cmp_type =
7782 (or_andcm ? CMP_OR : (and_orcm ? CMP_AND : CMP_NONE));
7783 specs[count++].index = p1;
7784 }
7785 if ((idesc->operands[1] == IA64_OPND_P1
7786 || idesc->operands[1] == IA64_OPND_P2)
7787 && p2 >= 1 && p2 < 16)
7788 {
7789 specs[count] = tmpl;
7790 specs[count].cmp_type =
7791 (or_andcm ? CMP_AND : (and_orcm ? CMP_OR : CMP_NONE));
7792 specs[count++].index = p2;
7793 }
7794 }
7795 else
7796 {
7797 if (CURR_SLOT.qp_regno >= 1 && CURR_SLOT.qp_regno < 16)
7798 {
7799 specs[count] = tmpl;
7800 specs[count++].index = CURR_SLOT.qp_regno;
7801 }
7802 if (idesc->operands[1] == IA64_OPND_PR)
7803 {
7804 for (i = 1; i < 16; i++)
7805 {
7806 specs[count] = tmpl;
7807 specs[count++].index = i;
7808 }
7809 }
7810 }
7811 }
7812 else
7813 {
7814 UNHANDLED;
7815 }
7816 break;
7817
7818 /* This is the general case for PRs. IA64_RS_PR and IA64_RS_PR63 are
7819 simplified cases of this. */
7820 case IA64_RS_PRr:
7821 if (note == 0)
7822 {
7823 for (i = 16; i < 63; i++)
7824 {
7825 specs[count] = tmpl;
7826 specs[count++].index = i;
7827 }
7828 }
7829 else if (note == 7)
7830 {
7831 valueT mask = 0;
7832 /* Mark only those registers indicated by the mask. */
7833 if (rsrc_write
7834 && idesc->operands[0] == IA64_OPND_PR)
7835 {
7836 mask = CURR_SLOT.opnd[2].X_add_number;
7837 if (mask & ((valueT) 1<<16))
7838 for (i = 16; i < 63; i++)
7839 {
7840 specs[count] = tmpl;
7841 specs[count++].index = i;
7842 }
7843 }
7844 else if (rsrc_write
7845 && idesc->operands[0] == IA64_OPND_PR_ROT)
7846 {
7847 for (i = 16; i < 63; i++)
7848 {
7849 specs[count] = tmpl;
7850 specs[count++].index = i;
7851 }
7852 }
7853 else
7854 {
7855 UNHANDLED;
7856 }
7857 }
7858 else if (note == 11) /* note 11 implies note 1 as well */
7859 {
7860 if (rsrc_write)
7861 {
7862 for (i = 0; i < idesc->num_outputs; i++)
7863 {
7864 if (idesc->operands[i] == IA64_OPND_P1
7865 || idesc->operands[i] == IA64_OPND_P2)
7866 {
7867 int regno = CURR_SLOT.opnd[i].X_add_number - REG_P;
7868 if (regno >= 16 && regno < 63)
7869 {
7870 specs[count] = tmpl;
7871 specs[count++].index = regno;
7872 }
7873 }
7874 }
7875 }
7876 else
7877 {
7878 UNHANDLED;
7879 }
7880 }
7881 else if (note == 12)
7882 {
7883 if (CURR_SLOT.qp_regno >= 16 && CURR_SLOT.qp_regno < 63)
7884 {
7885 specs[count] = tmpl;
7886 specs[count++].index = CURR_SLOT.qp_regno;
7887 }
7888 }
7889 else if (note == 1)
7890 {
7891 if (rsrc_write)
7892 {
7893 int p1 = CURR_SLOT.opnd[0].X_add_number - REG_P;
7894 int p2 = CURR_SLOT.opnd[1].X_add_number - REG_P;
7895 int or_andcm = strstr (idesc->name, "or.andcm") != NULL;
7896 int and_orcm = strstr (idesc->name, "and.orcm") != NULL;
7897
7898 if ((idesc->operands[0] == IA64_OPND_P1
7899 || idesc->operands[0] == IA64_OPND_P2)
7900 && p1 >= 16 && p1 < 63)
7901 {
7902 specs[count] = tmpl;
7903 specs[count].cmp_type =
7904 (or_andcm ? CMP_OR : (and_orcm ? CMP_AND : CMP_NONE));
7905 specs[count++].index = p1;
7906 }
7907 if ((idesc->operands[1] == IA64_OPND_P1
7908 || idesc->operands[1] == IA64_OPND_P2)
7909 && p2 >= 16 && p2 < 63)
7910 {
7911 specs[count] = tmpl;
7912 specs[count].cmp_type =
7913 (or_andcm ? CMP_AND : (and_orcm ? CMP_OR : CMP_NONE));
7914 specs[count++].index = p2;
7915 }
7916 }
7917 else
7918 {
7919 if (CURR_SLOT.qp_regno >= 16 && CURR_SLOT.qp_regno < 63)
7920 {
7921 specs[count] = tmpl;
7922 specs[count++].index = CURR_SLOT.qp_regno;
7923 }
7924 if (idesc->operands[1] == IA64_OPND_PR)
7925 {
7926 for (i = 16; i < 63; i++)
7927 {
7928 specs[count] = tmpl;
7929 specs[count++].index = i;
7930 }
7931 }
7932 }
7933 }
7934 else
7935 {
7936 UNHANDLED;
7937 }
7938 break;
7939
7940 case IA64_RS_PSR:
7941 /* Verify that the instruction is using the PSR bit indicated in
7942 dep->regindex. */
7943 if (note == 0)
7944 {
7945 if (idesc->operands[!rsrc_write] == IA64_OPND_PSR_UM)
7946 {
7947 if (dep->regindex < 6)
7948 {
7949 specs[count++] = tmpl;
7950 }
7951 }
7952 else if (idesc->operands[!rsrc_write] == IA64_OPND_PSR)
7953 {
7954 if (dep->regindex < 32
7955 || dep->regindex == 35
7956 || dep->regindex == 36
7957 || (!rsrc_write && dep->regindex == PSR_CPL))
7958 {
7959 specs[count++] = tmpl;
7960 }
7961 }
7962 else if (idesc->operands[!rsrc_write] == IA64_OPND_PSR_L)
7963 {
7964 if (dep->regindex < 32
7965 || dep->regindex == 35
7966 || dep->regindex == 36
7967 || (rsrc_write && dep->regindex == PSR_CPL))
7968 {
7969 specs[count++] = tmpl;
7970 }
7971 }
7972 else
7973 {
7974 /* Several PSR bits have very specific dependencies. */
7975 switch (dep->regindex)
7976 {
7977 default:
7978 specs[count++] = tmpl;
7979 break;
7980 case PSR_IC:
7981 if (rsrc_write)
7982 {
7983 specs[count++] = tmpl;
7984 }
7985 else
7986 {
7987 /* Only certain CR accesses use PSR.ic */
7988 if (idesc->operands[0] == IA64_OPND_CR3
7989 || idesc->operands[1] == IA64_OPND_CR3)
7990 {
7991 int index =
7992 ((idesc->operands[0] == IA64_OPND_CR3)
7993 ? 0 : 1);
7994 int regno =
7995 CURR_SLOT.opnd[index].X_add_number - REG_CR;
7996
7997 switch (regno)
7998 {
7999 default:
8000 break;
8001 case CR_ITIR:
8002 case CR_IFS:
8003 case CR_IIM:
8004 case CR_IIP:
8005 case CR_IPSR:
8006 case CR_ISR:
8007 case CR_IFA:
8008 case CR_IHA:
8009 case CR_IIPA:
8010 specs[count++] = tmpl;
8011 break;
8012 }
8013 }
8014 }
8015 break;
8016 case PSR_CPL:
8017 if (rsrc_write)
8018 {
8019 specs[count++] = tmpl;
8020 }
8021 else
8022 {
8023 /* Only some AR accesses use cpl */
8024 if (idesc->operands[0] == IA64_OPND_AR3
8025 || idesc->operands[1] == IA64_OPND_AR3)
8026 {
8027 int index =
8028 ((idesc->operands[0] == IA64_OPND_AR3)
8029 ? 0 : 1);
8030 int regno =
8031 CURR_SLOT.opnd[index].X_add_number - REG_AR;
8032
8033 if (regno == AR_ITC
8034 || (index == 0
8035 && (regno == AR_ITC
8036 || regno == AR_RSC
8037 || (regno >= AR_K0
8038 && regno <= AR_K7))))
8039 {
8040 specs[count++] = tmpl;
8041 }
8042 }
8043 else
8044 {
8045 specs[count++] = tmpl;
8046 }
8047 break;
8048 }
8049 }
8050 }
8051 }
8052 else if (note == 7)
8053 {
8054 valueT mask = 0;
8055 if (idesc->operands[0] == IA64_OPND_IMMU24)
8056 {
8057 mask = CURR_SLOT.opnd[0].X_add_number;
8058 }
8059 else
8060 {
8061 UNHANDLED;
8062 }
8063 if (mask & ((valueT) 1 << dep->regindex))
8064 {
8065 specs[count++] = tmpl;
8066 }
8067 }
8068 else if (note == 8)
8069 {
8070 int min = dep->regindex == PSR_DFL ? 2 : 32;
8071 int max = dep->regindex == PSR_DFL ? 31 : 127;
8072 /* dfh is read on FR32-127; dfl is read on FR2-31 */
8073 for (i = 0; i < NELEMS (idesc->operands); i++)
8074 {
8075 if (idesc->operands[i] == IA64_OPND_F1
8076 || idesc->operands[i] == IA64_OPND_F2
8077 || idesc->operands[i] == IA64_OPND_F3
8078 || idesc->operands[i] == IA64_OPND_F4)
8079 {
8080 int reg = CURR_SLOT.opnd[i].X_add_number - REG_FR;
8081 if (reg >= min && reg <= max)
8082 {
8083 specs[count++] = tmpl;
8084 }
8085 }
8086 }
8087 }
8088 else if (note == 9)
8089 {
8090 int min = dep->regindex == PSR_MFL ? 2 : 32;
8091 int max = dep->regindex == PSR_MFL ? 31 : 127;
8092 /* mfh is read on writes to FR32-127; mfl is read on writes to
8093 FR2-31 */
8094 for (i = 0; i < idesc->num_outputs; i++)
8095 {
8096 if (idesc->operands[i] == IA64_OPND_F1)
8097 {
8098 int reg = CURR_SLOT.opnd[i].X_add_number - REG_FR;
8099 if (reg >= min && reg <= max)
8100 {
8101 specs[count++] = tmpl;
8102 }
8103 }
8104 }
8105 }
8106 else if (note == 10)
8107 {
8108 for (i = 0; i < NELEMS (idesc->operands); i++)
8109 {
8110 if (idesc->operands[i] == IA64_OPND_R1
8111 || idesc->operands[i] == IA64_OPND_R2
8112 || idesc->operands[i] == IA64_OPND_R3)
8113 {
8114 int regno = CURR_SLOT.opnd[i].X_add_number - REG_GR;
8115 if (regno >= 16 && regno <= 31)
8116 {
8117 specs[count++] = tmpl;
8118 }
8119 }
8120 }
8121 }
8122 else
8123 {
8124 UNHANDLED;
8125 }
8126 break;
8127
8128 case IA64_RS_AR_FPSR:
8129 if (idesc->operands[!rsrc_write] == IA64_OPND_AR3)
8130 {
8131 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_AR;
8132 if (regno == AR_FPSR)
8133 {
8134 specs[count++] = tmpl;
8135 }
8136 }
8137 else
8138 {
8139 specs[count++] = tmpl;
8140 }
8141 break;
8142
8143 case IA64_RS_ARX:
8144 /* Handle all AR[REG] resources */
8145 if (note == 0 || note == 1)
8146 {
8147 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_AR;
8148 if (idesc->operands[!rsrc_write] == IA64_OPND_AR3
8149 && regno == dep->regindex)
8150 {
8151 specs[count++] = tmpl;
8152 }
8153 /* other AR[REG] resources may be affected by AR accesses */
8154 else if (idesc->operands[0] == IA64_OPND_AR3)
8155 {
8156 /* AR[] writes */
8157 regno = CURR_SLOT.opnd[0].X_add_number - REG_AR;
8158 switch (dep->regindex)
8159 {
8160 default:
8161 break;
8162 case AR_BSP:
8163 case AR_RNAT:
8164 if (regno == AR_BSPSTORE)
8165 {
8166 specs[count++] = tmpl;
8167 }
8168 case AR_RSC:
8169 if (!rsrc_write &&
8170 (regno == AR_BSPSTORE
8171 || regno == AR_RNAT))
8172 {
8173 specs[count++] = tmpl;
8174 }
8175 break;
8176 }
8177 }
8178 else if (idesc->operands[1] == IA64_OPND_AR3)
8179 {
8180 /* AR[] reads */
8181 regno = CURR_SLOT.opnd[1].X_add_number - REG_AR;
8182 switch (dep->regindex)
8183 {
8184 default:
8185 break;
8186 case AR_RSC:
8187 if (regno == AR_BSPSTORE || regno == AR_RNAT)
8188 {
8189 specs[count++] = tmpl;
8190 }
8191 break;
8192 }
8193 }
8194 else
8195 {
8196 specs[count++] = tmpl;
8197 }
8198 }
8199 else
8200 {
8201 UNHANDLED;
8202 }
8203 break;
8204
8205 case IA64_RS_CRX:
8206 /* Handle all CR[REG] resources */
8207 if (note == 0 || note == 1)
8208 {
8209 if (idesc->operands[!rsrc_write] == IA64_OPND_CR3)
8210 {
8211 int regno = CURR_SLOT.opnd[!rsrc_write].X_add_number - REG_CR;
8212 if (regno == dep->regindex)
8213 {
8214 specs[count++] = tmpl;
8215 }
8216 else if (!rsrc_write)
8217 {
8218 /* Reads from CR[IVR] affect other resources. */
8219 if (regno == CR_IVR)
8220 {
8221 if ((dep->regindex >= CR_IRR0
8222 && dep->regindex <= CR_IRR3)
8223 || dep->regindex == CR_TPR)
8224 {
8225 specs[count++] = tmpl;
8226 }
8227 }
8228 }
8229 }
8230 else
8231 {
8232 specs[count++] = tmpl;
8233 }
8234 }
8235 else
8236 {
8237 UNHANDLED;
8238 }
8239 break;
8240
8241 case IA64_RS_INSERVICE:
8242 /* look for write of EOI (67) or read of IVR (65) */
8243 if ((idesc->operands[0] == IA64_OPND_CR3
8244 && CURR_SLOT.opnd[0].X_add_number - REG_CR == CR_EOI)
8245 || (idesc->operands[1] == IA64_OPND_CR3
8246 && CURR_SLOT.opnd[1].X_add_number - REG_CR == CR_IVR))
8247 {
8248 specs[count++] = tmpl;
8249 }
8250 break;
8251
8252 case IA64_RS_GR0:
8253 if (note == 1)
8254 {
8255 specs[count++] = tmpl;
8256 }
8257 else
8258 {
8259 UNHANDLED;
8260 }
8261 break;
8262
8263 case IA64_RS_CFM:
8264 if (note != 2)
8265 {
8266 specs[count++] = tmpl;
8267 }
8268 else
8269 {
8270 /* Check if any of the registers accessed are in the rotating region.
8271 mov to/from pr accesses CFM only when qp_regno is in the rotating
8272 region */
8273 for (i = 0; i < NELEMS (idesc->operands); i++)
8274 {
8275 if (idesc->operands[i] == IA64_OPND_R1
8276 || idesc->operands[i] == IA64_OPND_R2
8277 || idesc->operands[i] == IA64_OPND_R3)
8278 {
8279 int num = CURR_SLOT.opnd[i].X_add_number - REG_GR;
8280 /* Assumes that md.rot.num_regs is always valid */
8281 if (md.rot.num_regs > 0
8282 && num > 31
8283 && num < 31 + md.rot.num_regs)
8284 {
8285 specs[count] = tmpl;
8286 specs[count++].specific = 0;
8287 }
8288 }
8289 else if (idesc->operands[i] == IA64_OPND_F1
8290 || idesc->operands[i] == IA64_OPND_F2
8291 || idesc->operands[i] == IA64_OPND_F3
8292 || idesc->operands[i] == IA64_OPND_F4)
8293 {
8294 int num = CURR_SLOT.opnd[i].X_add_number - REG_FR;
8295 if (num > 31)
8296 {
8297 specs[count] = tmpl;
8298 specs[count++].specific = 0;
8299 }
8300 }
8301 else if (idesc->operands[i] == IA64_OPND_P1
8302 || idesc->operands[i] == IA64_OPND_P2)
8303 {
8304 int num = CURR_SLOT.opnd[i].X_add_number - REG_P;
8305 if (num > 15)
8306 {
8307 specs[count] = tmpl;
8308 specs[count++].specific = 0;
8309 }
8310 }
8311 }
8312 if (CURR_SLOT.qp_regno > 15)
8313 {
8314 specs[count] = tmpl;
8315 specs[count++].specific = 0;
8316 }
8317 }
8318 break;
8319
8320 /* This is the same as IA64_RS_PRr, except simplified to account for
8321 the fact that there is only one register. */
8322 case IA64_RS_PR63:
8323 if (note == 0)
8324 {
8325 specs[count++] = tmpl;
8326 }
8327 else if (note == 7)
8328 {
8329 valueT mask = 0;
8330 if (idesc->operands[2] == IA64_OPND_IMM17)
8331 mask = CURR_SLOT.opnd[2].X_add_number;
8332 if (mask & ((valueT) 1 << 63))
8333 specs[count++] = tmpl;
8334 }
8335 else if (note == 11)
8336 {
8337 if ((idesc->operands[0] == IA64_OPND_P1
8338 && CURR_SLOT.opnd[0].X_add_number - REG_P == 63)
8339 || (idesc->operands[1] == IA64_OPND_P2
8340 && CURR_SLOT.opnd[1].X_add_number - REG_P == 63))
8341 {
8342 specs[count++] = tmpl;
8343 }
8344 }
8345 else if (note == 12)
8346 {
8347 if (CURR_SLOT.qp_regno == 63)
8348 {
8349 specs[count++] = tmpl;
8350 }
8351 }
8352 else if (note == 1)
8353 {
8354 if (rsrc_write)
8355 {
8356 int p1 = CURR_SLOT.opnd[0].X_add_number - REG_P;
8357 int p2 = CURR_SLOT.opnd[1].X_add_number - REG_P;
8358 int or_andcm = strstr (idesc->name, "or.andcm") != NULL;
8359 int and_orcm = strstr (idesc->name, "and.orcm") != NULL;
8360
8361 if (p1 == 63
8362 && (idesc->operands[0] == IA64_OPND_P1
8363 || idesc->operands[0] == IA64_OPND_P2))
8364 {
8365 specs[count] = tmpl;
8366 specs[count++].cmp_type =
8367 (or_andcm ? CMP_OR : (and_orcm ? CMP_AND : CMP_NONE));
8368 }
8369 if (p2 == 63
8370 && (idesc->operands[1] == IA64_OPND_P1
8371 || idesc->operands[1] == IA64_OPND_P2))
8372 {
8373 specs[count] = tmpl;
8374 specs[count++].cmp_type =
8375 (or_andcm ? CMP_AND : (and_orcm ? CMP_OR : CMP_NONE));
8376 }
8377 }
8378 else
8379 {
8380 if (CURR_SLOT.qp_regno == 63)
8381 {
8382 specs[count++] = tmpl;
8383 }
8384 }
8385 }
8386 else
8387 {
8388 UNHANDLED;
8389 }
8390 break;
8391
8392 case IA64_RS_RSE:
8393 /* FIXME we can identify some individual RSE written resources, but RSE
8394 read resources have not yet been completely identified, so for now
8395 treat RSE as a single resource */
8396 if (strncmp (idesc->name, "mov", 3) == 0)
8397 {
8398 if (rsrc_write)
8399 {
8400 if (idesc->operands[0] == IA64_OPND_AR3
8401 && CURR_SLOT.opnd[0].X_add_number - REG_AR == AR_BSPSTORE)
8402 {
8403 specs[count] = tmpl;
8404 specs[count++].index = 0; /* IA64_RSE_BSPLOAD/RNATBITINDEX */
8405 }
8406 }
8407 else
8408 {
8409 if (idesc->operands[0] == IA64_OPND_AR3)
8410 {
8411 if (CURR_SLOT.opnd[0].X_add_number - REG_AR == AR_BSPSTORE
8412 || CURR_SLOT.opnd[0].X_add_number - REG_AR == AR_RNAT)
8413 {
8414 specs[count++] = tmpl;
8415 }
8416 }
8417 else if (idesc->operands[1] == IA64_OPND_AR3)
8418 {
8419 if (CURR_SLOT.opnd[1].X_add_number - REG_AR == AR_BSP
8420 || CURR_SLOT.opnd[1].X_add_number - REG_AR == AR_BSPSTORE
8421 || CURR_SLOT.opnd[1].X_add_number - REG_AR == AR_RNAT)
8422 {
8423 specs[count++] = tmpl;
8424 }
8425 }
8426 }
8427 }
8428 else
8429 {
8430 specs[count++] = tmpl;
8431 }
8432 break;
8433
8434 case IA64_RS_ANY:
8435 /* FIXME -- do any of these need to be non-specific? */
8436 specs[count++] = tmpl;
8437 break;
8438
8439 default:
8440 as_bad (_("Unrecognized dependency specifier %d\n"), dep->specifier);
8441 break;
8442 }
8443
8444 return count;
8445 }
8446
8447 /* Clear branch flags on marked resources. This breaks the link between the
8448 QP of the marking instruction and a subsequent branch on the same QP. */
8449
8450 static void
8451 clear_qp_branch_flag (mask)
8452 valueT mask;
8453 {
8454 int i;
8455 for (i = 0; i < regdepslen; i++)
8456 {
8457 valueT bit = ((valueT) 1 << regdeps[i].qp_regno);
8458 if ((bit & mask) != 0)
8459 {
8460 regdeps[i].link_to_qp_branch = 0;
8461 }
8462 }
8463 }
8464
8465 /* Remove any mutexes which contain any of the PRs indicated in the mask.
8466
8467 Any changes to a PR clears the mutex relations which include that PR. */
8468
8469 static void
8470 clear_qp_mutex (mask)
8471 valueT mask;
8472 {
8473 int i;
8474
8475 i = 0;
8476 while (i < qp_mutexeslen)
8477 {
8478 if ((qp_mutexes[i].prmask & mask) != 0)
8479 {
8480 if (md.debug_dv)
8481 {
8482 fprintf (stderr, " Clearing mutex relation");
8483 print_prmask (qp_mutexes[i].prmask);
8484 fprintf (stderr, "\n");
8485 }
8486 qp_mutexes[i] = qp_mutexes[--qp_mutexeslen];
8487 }
8488 else
8489 ++i;
8490 }
8491 }
8492
8493 /* Clear implies relations which contain PRs in the given masks.
8494 P1_MASK indicates the source of the implies relation, while P2_MASK
8495 indicates the implied PR. */
8496
8497 static void
8498 clear_qp_implies (p1_mask, p2_mask)
8499 valueT p1_mask;
8500 valueT p2_mask;
8501 {
8502 int i;
8503
8504 i = 0;
8505 while (i < qp_implieslen)
8506 {
8507 if ((((valueT) 1 << qp_implies[i].p1) & p1_mask) != 0
8508 || (((valueT) 1 << qp_implies[i].p2) & p2_mask) != 0)
8509 {
8510 if (md.debug_dv)
8511 fprintf (stderr, "Clearing implied relation PR%d->PR%d\n",
8512 qp_implies[i].p1, qp_implies[i].p2);
8513 qp_implies[i] = qp_implies[--qp_implieslen];
8514 }
8515 else
8516 ++i;
8517 }
8518 }
8519
8520 /* Add the PRs specified to the list of implied relations. */
8521
8522 static void
8523 add_qp_imply (p1, p2)
8524 int p1, p2;
8525 {
8526 valueT mask;
8527 valueT bit;
8528 int i;
8529
8530 /* p0 is not meaningful here. */
8531 if (p1 == 0 || p2 == 0)
8532 abort ();
8533
8534 if (p1 == p2)
8535 return;
8536
8537 /* If it exists already, ignore it. */
8538 for (i = 0; i < qp_implieslen; i++)
8539 {
8540 if (qp_implies[i].p1 == p1
8541 && qp_implies[i].p2 == p2
8542 && qp_implies[i].path == md.path
8543 && !qp_implies[i].p2_branched)
8544 return;
8545 }
8546
8547 if (qp_implieslen == qp_impliestotlen)
8548 {
8549 qp_impliestotlen += 20;
8550 qp_implies = (struct qp_imply *)
8551 xrealloc ((void *) qp_implies,
8552 qp_impliestotlen * sizeof (struct qp_imply));
8553 }
8554 if (md.debug_dv)
8555 fprintf (stderr, " Registering PR%d implies PR%d\n", p1, p2);
8556 qp_implies[qp_implieslen].p1 = p1;
8557 qp_implies[qp_implieslen].p2 = p2;
8558 qp_implies[qp_implieslen].path = md.path;
8559 qp_implies[qp_implieslen++].p2_branched = 0;
8560
8561 /* Add in the implied transitive relations; for everything that p2 implies,
8562 make p1 imply that, too; for everything that implies p1, make it imply p2
8563 as well. */
8564 for (i = 0; i < qp_implieslen; i++)
8565 {
8566 if (qp_implies[i].p1 == p2)
8567 add_qp_imply (p1, qp_implies[i].p2);
8568 if (qp_implies[i].p2 == p1)
8569 add_qp_imply (qp_implies[i].p1, p2);
8570 }
8571 /* Add in mutex relations implied by this implies relation; for each mutex
8572 relation containing p2, duplicate it and replace p2 with p1. */
8573 bit = (valueT) 1 << p1;
8574 mask = (valueT) 1 << p2;
8575 for (i = 0; i < qp_mutexeslen; i++)
8576 {
8577 if (qp_mutexes[i].prmask & mask)
8578 add_qp_mutex ((qp_mutexes[i].prmask & ~mask) | bit);
8579 }
8580 }
8581
8582 /* Add the PRs specified in the mask to the mutex list; this means that only
8583 one of the PRs can be true at any time. PR0 should never be included in
8584 the mask. */
8585
8586 static void
8587 add_qp_mutex (mask)
8588 valueT mask;
8589 {
8590 if (mask & 0x1)
8591 abort ();
8592
8593 if (qp_mutexeslen == qp_mutexestotlen)
8594 {
8595 qp_mutexestotlen += 20;
8596 qp_mutexes = (struct qpmutex *)
8597 xrealloc ((void *) qp_mutexes,
8598 qp_mutexestotlen * sizeof (struct qpmutex));
8599 }
8600 if (md.debug_dv)
8601 {
8602 fprintf (stderr, " Registering mutex on");
8603 print_prmask (mask);
8604 fprintf (stderr, "\n");
8605 }
8606 qp_mutexes[qp_mutexeslen].path = md.path;
8607 qp_mutexes[qp_mutexeslen++].prmask = mask;
8608 }
8609
8610 static void
8611 clear_register_values ()
8612 {
8613 int i;
8614 if (md.debug_dv)
8615 fprintf (stderr, " Clearing register values\n");
8616 for (i = 1; i < NELEMS (gr_values); i++)
8617 gr_values[i].known = 0;
8618 }
8619
8620 /* Keep track of register values/changes which affect DV tracking.
8621
8622 optimization note: should add a flag to classes of insns where otherwise we
8623 have to examine a group of strings to identify them. */
8624
8625 static void
8626 note_register_values (idesc)
8627 struct ia64_opcode *idesc;
8628 {
8629 valueT qp_changemask = 0;
8630 int i;
8631
8632 /* Invalidate values for registers being written to. */
8633 for (i = 0; i < idesc->num_outputs; i++)
8634 {
8635 if (idesc->operands[i] == IA64_OPND_R1
8636 || idesc->operands[i] == IA64_OPND_R2
8637 || idesc->operands[i] == IA64_OPND_R3)
8638 {
8639 int regno = CURR_SLOT.opnd[i].X_add_number - REG_GR;
8640 if (regno > 0 && regno < NELEMS (gr_values))
8641 gr_values[regno].known = 0;
8642 }
8643 else if (idesc->operands[i] == IA64_OPND_R3_2)
8644 {
8645 int regno = CURR_SLOT.opnd[i].X_add_number - REG_GR;
8646 if (regno > 0 && regno < 4)
8647 gr_values[regno].known = 0;
8648 }
8649 else if (idesc->operands[i] == IA64_OPND_P1
8650 || idesc->operands[i] == IA64_OPND_P2)
8651 {
8652 int regno = CURR_SLOT.opnd[i].X_add_number - REG_P;
8653 qp_changemask |= (valueT) 1 << regno;
8654 }
8655 else if (idesc->operands[i] == IA64_OPND_PR)
8656 {
8657 if (idesc->operands[2] & (valueT) 0x10000)
8658 qp_changemask = ~(valueT) 0x1FFFF | idesc->operands[2];
8659 else
8660 qp_changemask = idesc->operands[2];
8661 break;
8662 }
8663 else if (idesc->operands[i] == IA64_OPND_PR_ROT)
8664 {
8665 if (idesc->operands[1] & ((valueT) 1 << 43))
8666 qp_changemask = ~(valueT) 0xFFFFFFFFFFF | idesc->operands[1];
8667 else
8668 qp_changemask = idesc->operands[1];
8669 qp_changemask &= ~(valueT) 0xFFFF;
8670 break;
8671 }
8672 }
8673
8674 /* Always clear qp branch flags on any PR change. */
8675 /* FIXME there may be exceptions for certain compares. */
8676 clear_qp_branch_flag (qp_changemask);
8677
8678 /* Invalidate rotating registers on insns which affect RRBs in CFM. */
8679 if (idesc->flags & IA64_OPCODE_MOD_RRBS)
8680 {
8681 qp_changemask |= ~(valueT) 0xFFFF;
8682 if (strcmp (idesc->name, "clrrrb.pr") != 0)
8683 {
8684 for (i = 32; i < 32 + md.rot.num_regs; i++)
8685 gr_values[i].known = 0;
8686 }
8687 clear_qp_mutex (qp_changemask);
8688 clear_qp_implies (qp_changemask, qp_changemask);
8689 }
8690 /* After a call, all register values are undefined, except those marked
8691 as "safe". */
8692 else if (strncmp (idesc->name, "br.call", 6) == 0
8693 || strncmp (idesc->name, "brl.call", 7) == 0)
8694 {
8695 /* FIXME keep GR values which are marked as "safe_across_calls" */
8696 clear_register_values ();
8697 clear_qp_mutex (~qp_safe_across_calls);
8698 clear_qp_implies (~qp_safe_across_calls, ~qp_safe_across_calls);
8699 clear_qp_branch_flag (~qp_safe_across_calls);
8700 }
8701 else if (is_interruption_or_rfi (idesc)
8702 || is_taken_branch (idesc))
8703 {
8704 clear_register_values ();
8705 clear_qp_mutex (~(valueT) 0);
8706 clear_qp_implies (~(valueT) 0, ~(valueT) 0);
8707 }
8708 /* Look for mutex and implies relations. */
8709 else if ((idesc->operands[0] == IA64_OPND_P1
8710 || idesc->operands[0] == IA64_OPND_P2)
8711 && (idesc->operands[1] == IA64_OPND_P1
8712 || idesc->operands[1] == IA64_OPND_P2))
8713 {
8714 int p1 = CURR_SLOT.opnd[0].X_add_number - REG_P;
8715 int p2 = CURR_SLOT.opnd[1].X_add_number - REG_P;
8716 valueT p1mask = (valueT) 1 << p1;
8717 valueT p2mask = (valueT) 1 << p2;
8718
8719 /* If one of the PRs is PR0, we can't really do anything. */
8720 if (p1 == 0 || p2 == 0)
8721 {
8722 if (md.debug_dv)
8723 fprintf (stderr, " Ignoring PRs due to inclusion of p0\n");
8724 }
8725 /* In general, clear mutexes and implies which include P1 or P2,
8726 with the following exceptions. */
8727 else if (strstr (idesc->name, ".or.andcm") != NULL)
8728 {
8729 add_qp_mutex (p1mask | p2mask);
8730 clear_qp_implies (p2mask, p1mask);
8731 }
8732 else if (strstr (idesc->name, ".and.orcm") != NULL)
8733 {
8734 add_qp_mutex (p1mask | p2mask);
8735 clear_qp_implies (p1mask, p2mask);
8736 }
8737 else if (strstr (idesc->name, ".and") != NULL)
8738 {
8739 clear_qp_implies (0, p1mask | p2mask);
8740 }
8741 else if (strstr (idesc->name, ".or") != NULL)
8742 {
8743 clear_qp_mutex (p1mask | p2mask);
8744 clear_qp_implies (p1mask | p2mask, 0);
8745 }
8746 else
8747 {
8748 clear_qp_implies (p1mask | p2mask, p1mask | p2mask);
8749 if (strstr (idesc->name, ".unc") != NULL)
8750 {
8751 add_qp_mutex (p1mask | p2mask);
8752 if (CURR_SLOT.qp_regno != 0)
8753 {
8754 add_qp_imply (CURR_SLOT.opnd[0].X_add_number - REG_P,
8755 CURR_SLOT.qp_regno);
8756 add_qp_imply (CURR_SLOT.opnd[1].X_add_number - REG_P,
8757 CURR_SLOT.qp_regno);
8758 }
8759 }
8760 else if (CURR_SLOT.qp_regno == 0)
8761 {
8762 add_qp_mutex (p1mask | p2mask);
8763 }
8764 else
8765 {
8766 clear_qp_mutex (p1mask | p2mask);
8767 }
8768 }
8769 }
8770 /* Look for mov imm insns into GRs. */
8771 else if (idesc->operands[0] == IA64_OPND_R1
8772 && (idesc->operands[1] == IA64_OPND_IMM22
8773 || idesc->operands[1] == IA64_OPND_IMMU64)
8774 && (strcmp (idesc->name, "mov") == 0
8775 || strcmp (idesc->name, "movl") == 0))
8776 {
8777 int regno = CURR_SLOT.opnd[0].X_add_number - REG_GR;
8778 if (regno > 0 && regno < NELEMS (gr_values))
8779 {
8780 gr_values[regno].known = 1;
8781 gr_values[regno].value = CURR_SLOT.opnd[1].X_add_number;
8782 gr_values[regno].path = md.path;
8783 if (md.debug_dv)
8784 {
8785 fprintf (stderr, " Know gr%d = ", regno);
8786 fprintf_vma (stderr, gr_values[regno].value);
8787 fputs ("\n", stderr);
8788 }
8789 }
8790 }
8791 else
8792 {
8793 clear_qp_mutex (qp_changemask);
8794 clear_qp_implies (qp_changemask, qp_changemask);
8795 }
8796 }
8797
8798 /* Return whether the given predicate registers are currently mutex. */
8799
8800 static int
8801 qp_mutex (p1, p2, path)
8802 int p1;
8803 int p2;
8804 int path;
8805 {
8806 int i;
8807 valueT mask;
8808
8809 if (p1 != p2)
8810 {
8811 mask = ((valueT) 1 << p1) | (valueT) 1 << p2;
8812 for (i = 0; i < qp_mutexeslen; i++)
8813 {
8814 if (qp_mutexes[i].path >= path
8815 && (qp_mutexes[i].prmask & mask) == mask)
8816 return 1;
8817 }
8818 }
8819 return 0;
8820 }
8821
8822 /* Return whether the given resource is in the given insn's list of chks
8823 Return 1 if the conflict is absolutely determined, 2 if it's a potential
8824 conflict. */
8825
8826 static int
8827 resources_match (rs, idesc, note, qp_regno, path)
8828 struct rsrc *rs;
8829 struct ia64_opcode *idesc;
8830 int note;
8831 int qp_regno;
8832 int path;
8833 {
8834 struct rsrc specs[MAX_SPECS];
8835 int count;
8836
8837 /* If the marked resource's qp_regno and the given qp_regno are mutex,
8838 we don't need to check. One exception is note 11, which indicates that
8839 target predicates are written regardless of PR[qp]. */
8840 if (qp_mutex (rs->qp_regno, qp_regno, path)
8841 && note != 11)
8842 return 0;
8843
8844 count = specify_resource (rs->dependency, idesc, DV_CHK, specs, note, path);
8845 while (count-- > 0)
8846 {
8847 /* UNAT checking is a bit more specific than other resources */
8848 if (rs->dependency->specifier == IA64_RS_AR_UNAT
8849 && specs[count].mem_offset.hint
8850 && rs->mem_offset.hint)
8851 {
8852 if (rs->mem_offset.base == specs[count].mem_offset.base)
8853 {
8854 if (((rs->mem_offset.offset >> 3) & 0x3F) ==
8855 ((specs[count].mem_offset.offset >> 3) & 0x3F))
8856 return 1;
8857 else
8858 continue;
8859 }
8860 }
8861
8862 /* Skip apparent PR write conflicts where both writes are an AND or both
8863 writes are an OR. */
8864 if (rs->dependency->specifier == IA64_RS_PR
8865 || rs->dependency->specifier == IA64_RS_PRr
8866 || rs->dependency->specifier == IA64_RS_PR63)
8867 {
8868 if (specs[count].cmp_type != CMP_NONE
8869 && specs[count].cmp_type == rs->cmp_type)
8870 {
8871 if (md.debug_dv)
8872 fprintf (stderr, " %s on parallel compare allowed (PR%d)\n",
8873 dv_mode[rs->dependency->mode],
8874 rs->dependency->specifier != IA64_RS_PR63 ?
8875 specs[count].index : 63);
8876 continue;
8877 }
8878 if (md.debug_dv)
8879 fprintf (stderr,
8880 " %s on parallel compare conflict %s vs %s on PR%d\n",
8881 dv_mode[rs->dependency->mode],
8882 dv_cmp_type[rs->cmp_type],
8883 dv_cmp_type[specs[count].cmp_type],
8884 rs->dependency->specifier != IA64_RS_PR63 ?
8885 specs[count].index : 63);
8886
8887 }
8888
8889 /* If either resource is not specific, conservatively assume a conflict
8890 */
8891 if (!specs[count].specific || !rs->specific)
8892 return 2;
8893 else if (specs[count].index == rs->index)
8894 return 1;
8895 }
8896 #if 0
8897 if (md.debug_dv)
8898 fprintf (stderr, " No %s conflicts\n", rs->dependency->name);
8899 #endif
8900
8901 return 0;
8902 }
8903
8904 /* Indicate an instruction group break; if INSERT_STOP is non-zero, then
8905 insert a stop to create the break. Update all resource dependencies
8906 appropriately. If QP_REGNO is non-zero, only apply the break to resources
8907 which use the same QP_REGNO and have the link_to_qp_branch flag set.
8908 If SAVE_CURRENT is non-zero, don't affect resources marked by the current
8909 instruction. */
8910
8911 static void
8912 insn_group_break (insert_stop, qp_regno, save_current)
8913 int insert_stop;
8914 int qp_regno;
8915 int save_current;
8916 {
8917 int i;
8918
8919 if (insert_stop && md.num_slots_in_use > 0)
8920 PREV_SLOT.end_of_insn_group = 1;
8921
8922 if (md.debug_dv)
8923 {
8924 fprintf (stderr, " Insn group break%s",
8925 (insert_stop ? " (w/stop)" : ""));
8926 if (qp_regno != 0)
8927 fprintf (stderr, " effective for QP=%d", qp_regno);
8928 fprintf (stderr, "\n");
8929 }
8930
8931 i = 0;
8932 while (i < regdepslen)
8933 {
8934 const struct ia64_dependency *dep = regdeps[i].dependency;
8935
8936 if (qp_regno != 0
8937 && regdeps[i].qp_regno != qp_regno)
8938 {
8939 ++i;
8940 continue;
8941 }
8942
8943 if (save_current
8944 && CURR_SLOT.src_file == regdeps[i].file
8945 && CURR_SLOT.src_line == regdeps[i].line)
8946 {
8947 ++i;
8948 continue;
8949 }
8950
8951 /* clear dependencies which are automatically cleared by a stop, or
8952 those that have reached the appropriate state of insn serialization */
8953 if (dep->semantics == IA64_DVS_IMPLIED
8954 || dep->semantics == IA64_DVS_IMPLIEDF
8955 || regdeps[i].insn_srlz == STATE_SRLZ)
8956 {
8957 print_dependency ("Removing", i);
8958 regdeps[i] = regdeps[--regdepslen];
8959 }
8960 else
8961 {
8962 if (dep->semantics == IA64_DVS_DATA
8963 || dep->semantics == IA64_DVS_INSTR
8964 || dep->semantics == IA64_DVS_SPECIFIC)
8965 {
8966 if (regdeps[i].insn_srlz == STATE_NONE)
8967 regdeps[i].insn_srlz = STATE_STOP;
8968 if (regdeps[i].data_srlz == STATE_NONE)
8969 regdeps[i].data_srlz = STATE_STOP;
8970 }
8971 ++i;
8972 }
8973 }
8974 }
8975
8976 /* Add the given resource usage spec to the list of active dependencies. */
8977
8978 static void
8979 mark_resource (idesc, dep, spec, depind, path)
8980 struct ia64_opcode *idesc ATTRIBUTE_UNUSED;
8981 const struct ia64_dependency *dep ATTRIBUTE_UNUSED;
8982 struct rsrc *spec;
8983 int depind;
8984 int path;
8985 {
8986 if (regdepslen == regdepstotlen)
8987 {
8988 regdepstotlen += 20;
8989 regdeps = (struct rsrc *)
8990 xrealloc ((void *) regdeps,
8991 regdepstotlen * sizeof (struct rsrc));
8992 }
8993
8994 regdeps[regdepslen] = *spec;
8995 regdeps[regdepslen].depind = depind;
8996 regdeps[regdepslen].path = path;
8997 regdeps[regdepslen].file = CURR_SLOT.src_file;
8998 regdeps[regdepslen].line = CURR_SLOT.src_line;
8999
9000 print_dependency ("Adding", regdepslen);
9001
9002 ++regdepslen;
9003 }
9004
9005 static void
9006 print_dependency (action, depind)
9007 const char *action;
9008 int depind;
9009 {
9010 if (md.debug_dv)
9011 {
9012 fprintf (stderr, " %s %s '%s'",
9013 action, dv_mode[(regdeps[depind].dependency)->mode],
9014 (regdeps[depind].dependency)->name);
9015 if (regdeps[depind].specific && regdeps[depind].index != 0)
9016 fprintf (stderr, " (%d)", regdeps[depind].index);
9017 if (regdeps[depind].mem_offset.hint)
9018 {
9019 fputs (" ", stderr);
9020 fprintf_vma (stderr, regdeps[depind].mem_offset.base);
9021 fputs ("+", stderr);
9022 fprintf_vma (stderr, regdeps[depind].mem_offset.offset);
9023 }
9024 fprintf (stderr, "\n");
9025 }
9026 }
9027
9028 static void
9029 instruction_serialization ()
9030 {
9031 int i;
9032 if (md.debug_dv)
9033 fprintf (stderr, " Instruction serialization\n");
9034 for (i = 0; i < regdepslen; i++)
9035 if (regdeps[i].insn_srlz == STATE_STOP)
9036 regdeps[i].insn_srlz = STATE_SRLZ;
9037 }
9038
9039 static void
9040 data_serialization ()
9041 {
9042 int i = 0;
9043 if (md.debug_dv)
9044 fprintf (stderr, " Data serialization\n");
9045 while (i < regdepslen)
9046 {
9047 if (regdeps[i].data_srlz == STATE_STOP
9048 /* Note: as of 991210, all "other" dependencies are cleared by a
9049 data serialization. This might change with new tables */
9050 || (regdeps[i].dependency)->semantics == IA64_DVS_OTHER)
9051 {
9052 print_dependency ("Removing", i);
9053 regdeps[i] = regdeps[--regdepslen];
9054 }
9055 else
9056 ++i;
9057 }
9058 }
9059
9060 /* Insert stops and serializations as needed to avoid DVs. */
9061
9062 static void
9063 remove_marked_resource (rs)
9064 struct rsrc *rs;
9065 {
9066 switch (rs->dependency->semantics)
9067 {
9068 case IA64_DVS_SPECIFIC:
9069 if (md.debug_dv)
9070 fprintf (stderr, "Implementation-specific, assume worst case...\n");
9071 /* ...fall through... */
9072 case IA64_DVS_INSTR:
9073 if (md.debug_dv)
9074 fprintf (stderr, "Inserting instr serialization\n");
9075 if (rs->insn_srlz < STATE_STOP)
9076 insn_group_break (1, 0, 0);
9077 if (rs->insn_srlz < STATE_SRLZ)
9078 {
9079 int oldqp = CURR_SLOT.qp_regno;
9080 struct ia64_opcode *oldidesc = CURR_SLOT.idesc;
9081 /* Manually jam a srlz.i insn into the stream */
9082 CURR_SLOT.qp_regno = 0;
9083 CURR_SLOT.idesc = ia64_find_opcode ("srlz.i");
9084 instruction_serialization ();
9085 md.curr_slot = (md.curr_slot + 1) % NUM_SLOTS;
9086 if (++md.num_slots_in_use >= NUM_SLOTS)
9087 emit_one_bundle ();
9088 CURR_SLOT.qp_regno = oldqp;
9089 CURR_SLOT.idesc = oldidesc;
9090 }
9091 insn_group_break (1, 0, 0);
9092 break;
9093 case IA64_DVS_OTHER: /* as of rev2 (991220) of the DV tables, all
9094 "other" types of DV are eliminated
9095 by a data serialization */
9096 case IA64_DVS_DATA:
9097 if (md.debug_dv)
9098 fprintf (stderr, "Inserting data serialization\n");
9099 if (rs->data_srlz < STATE_STOP)
9100 insn_group_break (1, 0, 0);
9101 {
9102 int oldqp = CURR_SLOT.qp_regno;
9103 struct ia64_opcode *oldidesc = CURR_SLOT.idesc;
9104 /* Manually jam a srlz.d insn into the stream */
9105 CURR_SLOT.qp_regno = 0;
9106 CURR_SLOT.idesc = ia64_find_opcode ("srlz.d");
9107 data_serialization ();
9108 md.curr_slot = (md.curr_slot + 1) % NUM_SLOTS;
9109 if (++md.num_slots_in_use >= NUM_SLOTS)
9110 emit_one_bundle ();
9111 CURR_SLOT.qp_regno = oldqp;
9112 CURR_SLOT.idesc = oldidesc;
9113 }
9114 break;
9115 case IA64_DVS_IMPLIED:
9116 case IA64_DVS_IMPLIEDF:
9117 if (md.debug_dv)
9118 fprintf (stderr, "Inserting stop\n");
9119 insn_group_break (1, 0, 0);
9120 break;
9121 default:
9122 break;
9123 }
9124 }
9125
9126 /* Check the resources used by the given opcode against the current dependency
9127 list.
9128
9129 The check is run once for each execution path encountered. In this case,
9130 a unique execution path is the sequence of instructions following a code
9131 entry point, e.g. the following has three execution paths, one starting
9132 at L0, one at L1, and one at L2.
9133
9134 L0: nop
9135 L1: add
9136 L2: add
9137 br.ret
9138 */
9139
9140 static void
9141 check_dependencies (idesc)
9142 struct ia64_opcode *idesc;
9143 {
9144 const struct ia64_opcode_dependency *opdeps = idesc->dependencies;
9145 int path;
9146 int i;
9147
9148 /* Note that the number of marked resources may change within the
9149 loop if in auto mode. */
9150 i = 0;
9151 while (i < regdepslen)
9152 {
9153 struct rsrc *rs = &regdeps[i];
9154 const struct ia64_dependency *dep = rs->dependency;
9155 int chkind;
9156 int note;
9157 int start_over = 0;
9158
9159 if (dep->semantics == IA64_DVS_NONE
9160 || (chkind = depends_on (rs->depind, idesc)) == -1)
9161 {
9162 ++i;
9163 continue;
9164 }
9165
9166 note = NOTE (opdeps->chks[chkind]);
9167
9168 /* Check this resource against each execution path seen thus far. */
9169 for (path = 0; path <= md.path; path++)
9170 {
9171 int matchtype;
9172
9173 /* If the dependency wasn't on the path being checked, ignore it. */
9174 if (rs->path < path)
9175 continue;
9176
9177 /* If the QP for this insn implies a QP which has branched, don't
9178 bother checking. Ed. NOTE: I don't think this check is terribly
9179 useful; what's the point of generating code which will only be
9180 reached if its QP is zero?
9181 This code was specifically inserted to handle the following code,
9182 based on notes from Intel's DV checking code, where p1 implies p2.
9183
9184 mov r4 = 2
9185 (p2) br.cond L
9186 (p1) mov r4 = 7
9187 */
9188 if (CURR_SLOT.qp_regno != 0)
9189 {
9190 int skip = 0;
9191 int implies;
9192 for (implies = 0; implies < qp_implieslen; implies++)
9193 {
9194 if (qp_implies[implies].path >= path
9195 && qp_implies[implies].p1 == CURR_SLOT.qp_regno
9196 && qp_implies[implies].p2_branched)
9197 {
9198 skip = 1;
9199 break;
9200 }
9201 }
9202 if (skip)
9203 continue;
9204 }
9205
9206 if ((matchtype = resources_match (rs, idesc, note,
9207 CURR_SLOT.qp_regno, path)) != 0)
9208 {
9209 char msg[1024];
9210 char pathmsg[256] = "";
9211 char indexmsg[256] = "";
9212 int certain = (matchtype == 1 && CURR_SLOT.qp_regno == 0);
9213
9214 if (path != 0)
9215 sprintf (pathmsg, " when entry is at label '%s'",
9216 md.entry_labels[path - 1]);
9217 if (rs->specific && rs->index != 0)
9218 sprintf (indexmsg, ", specific resource number is %d",
9219 rs->index);
9220 sprintf (msg, "Use of '%s' %s %s dependency '%s' (%s)%s%s",
9221 idesc->name,
9222 (certain ? "violates" : "may violate"),
9223 dv_mode[dep->mode], dep->name,
9224 dv_sem[dep->semantics],
9225 pathmsg, indexmsg);
9226
9227 if (md.explicit_mode)
9228 {
9229 as_warn ("%s", msg);
9230 if (path < md.path)
9231 as_warn (_("Only the first path encountering the conflict "
9232 "is reported"));
9233 as_warn_where (rs->file, rs->line,
9234 _("This is the location of the "
9235 "conflicting usage"));
9236 /* Don't bother checking other paths, to avoid duplicating
9237 the same warning */
9238 break;
9239 }
9240 else
9241 {
9242 if (md.debug_dv)
9243 fprintf (stderr, "%s @ %s:%d\n", msg, rs->file, rs->line);
9244
9245 remove_marked_resource (rs);
9246
9247 /* since the set of dependencies has changed, start over */
9248 /* FIXME -- since we're removing dvs as we go, we
9249 probably don't really need to start over... */
9250 start_over = 1;
9251 break;
9252 }
9253 }
9254 }
9255 if (start_over)
9256 i = 0;
9257 else
9258 ++i;
9259 }
9260 }
9261
9262 /* Register new dependencies based on the given opcode. */
9263
9264 static void
9265 mark_resources (idesc)
9266 struct ia64_opcode *idesc;
9267 {
9268 int i;
9269 const struct ia64_opcode_dependency *opdeps = idesc->dependencies;
9270 int add_only_qp_reads = 0;
9271
9272 /* A conditional branch only uses its resources if it is taken; if it is
9273 taken, we stop following that path. The other branch types effectively
9274 *always* write their resources. If it's not taken, register only QP
9275 reads. */
9276 if (is_conditional_branch (idesc) || is_interruption_or_rfi (idesc))
9277 {
9278 add_only_qp_reads = 1;
9279 }
9280
9281 if (md.debug_dv)
9282 fprintf (stderr, "Registering '%s' resource usage\n", idesc->name);
9283
9284 for (i = 0; i < opdeps->nregs; i++)
9285 {
9286 const struct ia64_dependency *dep;
9287 struct rsrc specs[MAX_SPECS];
9288 int note;
9289 int path;
9290 int count;
9291
9292 dep = ia64_find_dependency (opdeps->regs[i]);
9293 note = NOTE (opdeps->regs[i]);
9294
9295 if (add_only_qp_reads
9296 && !(dep->mode == IA64_DV_WAR
9297 && (dep->specifier == IA64_RS_PR
9298 || dep->specifier == IA64_RS_PRr
9299 || dep->specifier == IA64_RS_PR63)))
9300 continue;
9301
9302 count = specify_resource (dep, idesc, DV_REG, specs, note, md.path);
9303
9304 #if 0
9305 if (md.debug_dv && !count)
9306 fprintf (stderr, " No %s %s usage found (path %d)\n",
9307 dv_mode[dep->mode], dep->name, md.path);
9308 #endif
9309
9310 while (count-- > 0)
9311 {
9312 mark_resource (idesc, dep, &specs[count],
9313 DEP (opdeps->regs[i]), md.path);
9314 }
9315
9316 /* The execution path may affect register values, which may in turn
9317 affect which indirect-access resources are accessed. */
9318 switch (dep->specifier)
9319 {
9320 default:
9321 break;
9322 case IA64_RS_CPUID:
9323 case IA64_RS_DBR:
9324 case IA64_RS_IBR:
9325 case IA64_RS_MSR:
9326 case IA64_RS_PKR:
9327 case IA64_RS_PMC:
9328 case IA64_RS_PMD:
9329 case IA64_RS_RR:
9330 for (path = 0; path < md.path; path++)
9331 {
9332 count = specify_resource (dep, idesc, DV_REG, specs, note, path);
9333 while (count-- > 0)
9334 mark_resource (idesc, dep, &specs[count],
9335 DEP (opdeps->regs[i]), path);
9336 }
9337 break;
9338 }
9339 }
9340 }
9341
9342 /* Remove dependencies when they no longer apply. */
9343
9344 static void
9345 update_dependencies (idesc)
9346 struct ia64_opcode *idesc;
9347 {
9348 int i;
9349
9350 if (strcmp (idesc->name, "srlz.i") == 0)
9351 {
9352 instruction_serialization ();
9353 }
9354 else if (strcmp (idesc->name, "srlz.d") == 0)
9355 {
9356 data_serialization ();
9357 }
9358 else if (is_interruption_or_rfi (idesc)
9359 || is_taken_branch (idesc))
9360 {
9361 /* Although technically the taken branch doesn't clear dependencies
9362 which require a srlz.[id], we don't follow the branch; the next
9363 instruction is assumed to start with a clean slate. */
9364 regdepslen = 0;
9365 md.path = 0;
9366 }
9367 else if (is_conditional_branch (idesc)
9368 && CURR_SLOT.qp_regno != 0)
9369 {
9370 int is_call = strstr (idesc->name, ".call") != NULL;
9371
9372 for (i = 0; i < qp_implieslen; i++)
9373 {
9374 /* If the conditional branch's predicate is implied by the predicate
9375 in an existing dependency, remove that dependency. */
9376 if (qp_implies[i].p2 == CURR_SLOT.qp_regno)
9377 {
9378 int depind = 0;
9379 /* Note that this implied predicate takes a branch so that if
9380 a later insn generates a DV but its predicate implies this
9381 one, we can avoid the false DV warning. */
9382 qp_implies[i].p2_branched = 1;
9383 while (depind < regdepslen)
9384 {
9385 if (regdeps[depind].qp_regno == qp_implies[i].p1)
9386 {
9387 print_dependency ("Removing", depind);
9388 regdeps[depind] = regdeps[--regdepslen];
9389 }
9390 else
9391 ++depind;
9392 }
9393 }
9394 }
9395 /* Any marked resources which have this same predicate should be
9396 cleared, provided that the QP hasn't been modified between the
9397 marking instruction and the branch. */
9398 if (is_call)
9399 {
9400 insn_group_break (0, CURR_SLOT.qp_regno, 1);
9401 }
9402 else
9403 {
9404 i = 0;
9405 while (i < regdepslen)
9406 {
9407 if (regdeps[i].qp_regno == CURR_SLOT.qp_regno
9408 && regdeps[i].link_to_qp_branch
9409 && (regdeps[i].file != CURR_SLOT.src_file
9410 || regdeps[i].line != CURR_SLOT.src_line))
9411 {
9412 /* Treat like a taken branch */
9413 print_dependency ("Removing", i);
9414 regdeps[i] = regdeps[--regdepslen];
9415 }
9416 else
9417 ++i;
9418 }
9419 }
9420 }
9421 }
9422
9423 /* Examine the current instruction for dependency violations. */
9424
9425 static int
9426 check_dv (idesc)
9427 struct ia64_opcode *idesc;
9428 {
9429 if (md.debug_dv)
9430 {
9431 fprintf (stderr, "Checking %s for violations (line %d, %d/%d)\n",
9432 idesc->name, CURR_SLOT.src_line,
9433 idesc->dependencies->nchks,
9434 idesc->dependencies->nregs);
9435 }
9436
9437 /* Look through the list of currently marked resources; if the current
9438 instruction has the dependency in its chks list which uses that resource,
9439 check against the specific resources used. */
9440 check_dependencies (idesc);
9441
9442 /* Look up the instruction's regdeps (RAW writes, WAW writes, and WAR reads),
9443 then add them to the list of marked resources. */
9444 mark_resources (idesc);
9445
9446 /* There are several types of dependency semantics, and each has its own
9447 requirements for being cleared
9448
9449 Instruction serialization (insns separated by interruption, rfi, or
9450 writer + srlz.i + reader, all in separate groups) clears DVS_INSTR.
9451
9452 Data serialization (instruction serialization, or writer + srlz.d +
9453 reader, where writer and srlz.d are in separate groups) clears
9454 DVS_DATA. (This also clears DVS_OTHER, but that is not guaranteed to
9455 always be the case).
9456
9457 Instruction group break (groups separated by stop, taken branch,
9458 interruption or rfi) clears DVS_IMPLIED and DVS_IMPLIEDF.
9459 */
9460 update_dependencies (idesc);
9461
9462 /* Sometimes, knowing a register value allows us to avoid giving a false DV
9463 warning. Keep track of as many as possible that are useful. */
9464 note_register_values (idesc);
9465
9466 /* We don't need or want this anymore. */
9467 md.mem_offset.hint = 0;
9468
9469 return 0;
9470 }
9471
9472 /* Translate one line of assembly. Pseudo ops and labels do not show
9473 here. */
9474 void
9475 md_assemble (str)
9476 char *str;
9477 {
9478 char *saved_input_line_pointer, *mnemonic;
9479 const struct pseudo_opcode *pdesc;
9480 struct ia64_opcode *idesc;
9481 unsigned char qp_regno;
9482 unsigned int flags;
9483 int ch;
9484
9485 saved_input_line_pointer = input_line_pointer;
9486 input_line_pointer = str;
9487
9488 /* extract the opcode (mnemonic): */
9489
9490 mnemonic = input_line_pointer;
9491 ch = get_symbol_end ();
9492 pdesc = (struct pseudo_opcode *) hash_find (md.pseudo_hash, mnemonic);
9493 if (pdesc)
9494 {
9495 *input_line_pointer = ch;
9496 (*pdesc->handler) (pdesc->arg);
9497 goto done;
9498 }
9499
9500 /* Find the instruction descriptor matching the arguments. */
9501
9502 idesc = ia64_find_opcode (mnemonic);
9503 *input_line_pointer = ch;
9504 if (!idesc)
9505 {
9506 as_bad ("Unknown opcode `%s'", mnemonic);
9507 goto done;
9508 }
9509
9510 idesc = parse_operands (idesc);
9511 if (!idesc)
9512 goto done;
9513
9514 /* Handle the dynamic ops we can handle now: */
9515 if (idesc->type == IA64_TYPE_DYN)
9516 {
9517 if (strcmp (idesc->name, "add") == 0)
9518 {
9519 if (CURR_SLOT.opnd[2].X_op == O_register
9520 && CURR_SLOT.opnd[2].X_add_number < 4)
9521 mnemonic = "addl";
9522 else
9523 mnemonic = "adds";
9524 ia64_free_opcode (idesc);
9525 idesc = ia64_find_opcode (mnemonic);
9526 #if 0
9527 know (!idesc->next);
9528 #endif
9529 }
9530 else if (strcmp (idesc->name, "mov") == 0)
9531 {
9532 enum ia64_opnd opnd1, opnd2;
9533 int rop;
9534
9535 opnd1 = idesc->operands[0];
9536 opnd2 = idesc->operands[1];
9537 if (opnd1 == IA64_OPND_AR3)
9538 rop = 0;
9539 else if (opnd2 == IA64_OPND_AR3)
9540 rop = 1;
9541 else
9542 abort ();
9543 if (CURR_SLOT.opnd[rop].X_op == O_register
9544 && ar_is_in_integer_unit (CURR_SLOT.opnd[rop].X_add_number))
9545 mnemonic = "mov.i";
9546 else
9547 mnemonic = "mov.m";
9548 ia64_free_opcode (idesc);
9549 idesc = ia64_find_opcode (mnemonic);
9550 while (idesc != NULL
9551 && (idesc->operands[0] != opnd1
9552 || idesc->operands[1] != opnd2))
9553 idesc = get_next_opcode (idesc);
9554 }
9555 }
9556
9557 qp_regno = 0;
9558 if (md.qp.X_op == O_register)
9559 {
9560 qp_regno = md.qp.X_add_number - REG_P;
9561 md.qp.X_op = O_absent;
9562 }
9563
9564 flags = idesc->flags;
9565
9566 if ((flags & IA64_OPCODE_FIRST) != 0)
9567 insn_group_break (1, 0, 0);
9568
9569 if ((flags & IA64_OPCODE_NO_PRED) != 0 && qp_regno != 0)
9570 {
9571 as_bad ("`%s' cannot be predicated", idesc->name);
9572 goto done;
9573 }
9574
9575 /* Build the instruction. */
9576 CURR_SLOT.qp_regno = qp_regno;
9577 CURR_SLOT.idesc = idesc;
9578 as_where (&CURR_SLOT.src_file, &CURR_SLOT.src_line);
9579 dwarf2_where (&CURR_SLOT.debug_line);
9580
9581 /* Add unwind entry, if there is one. */
9582 if (unwind.current_entry)
9583 {
9584 CURR_SLOT.unwind_record = unwind.current_entry;
9585 unwind.current_entry = NULL;
9586 }
9587
9588 /* Check for dependency violations. */
9589 if (md.detect_dv)
9590 check_dv (idesc);
9591
9592 md.curr_slot = (md.curr_slot + 1) % NUM_SLOTS;
9593 if (++md.num_slots_in_use >= NUM_SLOTS)
9594 emit_one_bundle ();
9595
9596 if ((flags & IA64_OPCODE_LAST) != 0)
9597 insn_group_break (1, 0, 0);
9598
9599 md.last_text_seg = now_seg;
9600
9601 done:
9602 input_line_pointer = saved_input_line_pointer;
9603 }
9604
9605 /* Called when symbol NAME cannot be found in the symbol table.
9606 Should be used for dynamic valued symbols only. */
9607
9608 symbolS *
9609 md_undefined_symbol (name)
9610 char *name ATTRIBUTE_UNUSED;
9611 {
9612 return 0;
9613 }
9614
9615 /* Called for any expression that can not be recognized. When the
9616 function is called, `input_line_pointer' will point to the start of
9617 the expression. */
9618
9619 void
9620 md_operand (e)
9621 expressionS *e;
9622 {
9623 enum pseudo_type pseudo_type;
9624 const char *name;
9625 size_t len;
9626 int ch, i;
9627
9628 switch (*input_line_pointer)
9629 {
9630 case '@':
9631 /* Find what relocation pseudo-function we're dealing with. */
9632 pseudo_type = 0;
9633 ch = *++input_line_pointer;
9634 for (i = 0; i < NELEMS (pseudo_func); ++i)
9635 if (pseudo_func[i].name && pseudo_func[i].name[0] == ch)
9636 {
9637 len = strlen (pseudo_func[i].name);
9638 if (strncmp (pseudo_func[i].name + 1,
9639 input_line_pointer + 1, len - 1) == 0
9640 && !is_part_of_name (input_line_pointer[len]))
9641 {
9642 input_line_pointer += len;
9643 pseudo_type = pseudo_func[i].type;
9644 break;
9645 }
9646 }
9647 switch (pseudo_type)
9648 {
9649 case PSEUDO_FUNC_RELOC:
9650 SKIP_WHITESPACE ();
9651 if (*input_line_pointer != '(')
9652 {
9653 as_bad ("Expected '('");
9654 goto err;
9655 }
9656 /* Skip '('. */
9657 ++input_line_pointer;
9658 expression (e);
9659 if (*input_line_pointer++ != ')')
9660 {
9661 as_bad ("Missing ')'");
9662 goto err;
9663 }
9664 if (e->X_op != O_symbol)
9665 {
9666 if (e->X_op != O_pseudo_fixup)
9667 {
9668 as_bad ("Not a symbolic expression");
9669 goto err;
9670 }
9671 if (S_GET_VALUE (e->X_op_symbol) == FUNC_FPTR_RELATIVE
9672 && i == FUNC_LT_RELATIVE)
9673 i = FUNC_LT_FPTR_RELATIVE;
9674 else
9675 {
9676 as_bad ("Illegal combination of relocation functions");
9677 goto err;
9678 }
9679 }
9680 /* Make sure gas doesn't get rid of local symbols that are used
9681 in relocs. */
9682 e->X_op = O_pseudo_fixup;
9683 e->X_op_symbol = pseudo_func[i].u.sym;
9684 break;
9685
9686 case PSEUDO_FUNC_CONST:
9687 e->X_op = O_constant;
9688 e->X_add_number = pseudo_func[i].u.ival;
9689 break;
9690
9691 case PSEUDO_FUNC_REG:
9692 e->X_op = O_register;
9693 e->X_add_number = pseudo_func[i].u.ival;
9694 break;
9695
9696 default:
9697 name = input_line_pointer - 1;
9698 get_symbol_end ();
9699 as_bad ("Unknown pseudo function `%s'", name);
9700 goto err;
9701 }
9702 break;
9703
9704 case '[':
9705 ++input_line_pointer;
9706 expression (e);
9707 if (*input_line_pointer != ']')
9708 {
9709 as_bad ("Closing bracket misssing");
9710 goto err;
9711 }
9712 else
9713 {
9714 if (e->X_op != O_register)
9715 as_bad ("Register expected as index");
9716
9717 ++input_line_pointer;
9718 e->X_op = O_index;
9719 }
9720 break;
9721
9722 default:
9723 break;
9724 }
9725 return;
9726
9727 err:
9728 ignore_rest_of_line ();
9729 }
9730
9731 /* Return 1 if it's OK to adjust a reloc by replacing the symbol with
9732 a section symbol plus some offset. For relocs involving @fptr(),
9733 directives we don't want such adjustments since we need to have the
9734 original symbol's name in the reloc. */
9735 int
9736 ia64_fix_adjustable (fix)
9737 fixS *fix;
9738 {
9739 /* Prevent all adjustments to global symbols */
9740 if (S_IS_EXTERN (fix->fx_addsy) || S_IS_WEAK (fix->fx_addsy))
9741 return 0;
9742
9743 switch (fix->fx_r_type)
9744 {
9745 case BFD_RELOC_IA64_FPTR64I:
9746 case BFD_RELOC_IA64_FPTR32MSB:
9747 case BFD_RELOC_IA64_FPTR32LSB:
9748 case BFD_RELOC_IA64_FPTR64MSB:
9749 case BFD_RELOC_IA64_FPTR64LSB:
9750 case BFD_RELOC_IA64_LTOFF_FPTR22:
9751 case BFD_RELOC_IA64_LTOFF_FPTR64I:
9752 return 0;
9753 default:
9754 break;
9755 }
9756
9757 return 1;
9758 }
9759
9760 int
9761 ia64_force_relocation (fix)
9762 fixS *fix;
9763 {
9764 switch (fix->fx_r_type)
9765 {
9766 case BFD_RELOC_IA64_FPTR64I:
9767 case BFD_RELOC_IA64_FPTR32MSB:
9768 case BFD_RELOC_IA64_FPTR32LSB:
9769 case BFD_RELOC_IA64_FPTR64MSB:
9770 case BFD_RELOC_IA64_FPTR64LSB:
9771
9772 case BFD_RELOC_IA64_LTOFF22:
9773 case BFD_RELOC_IA64_LTOFF64I:
9774 case BFD_RELOC_IA64_LTOFF_FPTR22:
9775 case BFD_RELOC_IA64_LTOFF_FPTR64I:
9776 case BFD_RELOC_IA64_PLTOFF22:
9777 case BFD_RELOC_IA64_PLTOFF64I:
9778 case BFD_RELOC_IA64_PLTOFF64MSB:
9779 case BFD_RELOC_IA64_PLTOFF64LSB:
9780 return 1;
9781
9782 default:
9783 return 0;
9784 }
9785 return 0;
9786 }
9787
9788 /* Decide from what point a pc-relative relocation is relative to,
9789 relative to the pc-relative fixup. Er, relatively speaking. */
9790 long
9791 ia64_pcrel_from_section (fix, sec)
9792 fixS *fix;
9793 segT sec;
9794 {
9795 unsigned long off = fix->fx_frag->fr_address + fix->fx_where;
9796
9797 if (bfd_get_section_flags (stdoutput, sec) & SEC_CODE)
9798 off &= ~0xfUL;
9799
9800 return off;
9801 }
9802
9803 /* This is called whenever some data item (not an instruction) needs a
9804 fixup. We pick the right reloc code depending on the byteorder
9805 currently in effect. */
9806 void
9807 ia64_cons_fix_new (f, where, nbytes, exp)
9808 fragS *f;
9809 int where;
9810 int nbytes;
9811 expressionS *exp;
9812 {
9813 bfd_reloc_code_real_type code;
9814 fixS *fix;
9815
9816 switch (nbytes)
9817 {
9818 /* There are no reloc for 8 and 16 bit quantities, but we allow
9819 them here since they will work fine as long as the expression
9820 is fully defined at the end of the pass over the source file. */
9821 case 1: code = BFD_RELOC_8; break;
9822 case 2: code = BFD_RELOC_16; break;
9823 case 4:
9824 if (target_big_endian)
9825 code = BFD_RELOC_IA64_DIR32MSB;
9826 else
9827 code = BFD_RELOC_IA64_DIR32LSB;
9828 break;
9829
9830 case 8:
9831 if (target_big_endian)
9832 code = BFD_RELOC_IA64_DIR64MSB;
9833 else
9834 code = BFD_RELOC_IA64_DIR64LSB;
9835 break;
9836
9837 case 16:
9838 if (exp->X_op == O_pseudo_fixup
9839 && exp->X_op_symbol
9840 && S_GET_VALUE (exp->X_op_symbol) == FUNC_IPLT_RELOC)
9841 {
9842 if (target_big_endian)
9843 code = BFD_RELOC_IA64_IPLTMSB;
9844 else
9845 code = BFD_RELOC_IA64_IPLTLSB;
9846
9847 exp->X_op = O_symbol;
9848 break;
9849 }
9850 /* FALLTHRU */
9851
9852 default:
9853 as_bad ("Unsupported fixup size %d", nbytes);
9854 ignore_rest_of_line ();
9855 return;
9856 }
9857 if (exp->X_op == O_pseudo_fixup)
9858 {
9859 /* ??? */
9860 exp->X_op = O_symbol;
9861 code = ia64_gen_real_reloc_type (exp->X_op_symbol, code);
9862 }
9863
9864 fix = fix_new_exp (f, where, nbytes, exp, 0, code);
9865 /* We need to store the byte order in effect in case we're going
9866 to fix an 8 or 16 bit relocation (for which there no real
9867 relocs available). See md_apply_fix(). */
9868 fix->tc_fix_data.bigendian = target_big_endian;
9869 }
9870
9871 /* Return the actual relocation we wish to associate with the pseudo
9872 reloc described by SYM and R_TYPE. SYM should be one of the
9873 symbols in the pseudo_func array, or NULL. */
9874
9875 static bfd_reloc_code_real_type
9876 ia64_gen_real_reloc_type (sym, r_type)
9877 struct symbol *sym;
9878 bfd_reloc_code_real_type r_type;
9879 {
9880 bfd_reloc_code_real_type new = 0;
9881
9882 if (sym == NULL)
9883 {
9884 return r_type;
9885 }
9886
9887 switch (S_GET_VALUE (sym))
9888 {
9889 case FUNC_FPTR_RELATIVE:
9890 switch (r_type)
9891 {
9892 case BFD_RELOC_IA64_IMM64: new = BFD_RELOC_IA64_FPTR64I; break;
9893 case BFD_RELOC_IA64_DIR32MSB: new = BFD_RELOC_IA64_FPTR32MSB; break;
9894 case BFD_RELOC_IA64_DIR32LSB: new = BFD_RELOC_IA64_FPTR32LSB; break;
9895 case BFD_RELOC_IA64_DIR64MSB: new = BFD_RELOC_IA64_FPTR64MSB; break;
9896 case BFD_RELOC_IA64_DIR64LSB: new = BFD_RELOC_IA64_FPTR64LSB; break;
9897 default: break;
9898 }
9899 break;
9900
9901 case FUNC_GP_RELATIVE:
9902 switch (r_type)
9903 {
9904 case BFD_RELOC_IA64_IMM22: new = BFD_RELOC_IA64_GPREL22; break;
9905 case BFD_RELOC_IA64_IMM64: new = BFD_RELOC_IA64_GPREL64I; break;
9906 case BFD_RELOC_IA64_DIR32MSB: new = BFD_RELOC_IA64_GPREL32MSB; break;
9907 case BFD_RELOC_IA64_DIR32LSB: new = BFD_RELOC_IA64_GPREL32LSB; break;
9908 case BFD_RELOC_IA64_DIR64MSB: new = BFD_RELOC_IA64_GPREL64MSB; break;
9909 case BFD_RELOC_IA64_DIR64LSB: new = BFD_RELOC_IA64_GPREL64LSB; break;
9910 default: break;
9911 }
9912 break;
9913
9914 case FUNC_LT_RELATIVE:
9915 switch (r_type)
9916 {
9917 case BFD_RELOC_IA64_IMM22: new = BFD_RELOC_IA64_LTOFF22; break;
9918 case BFD_RELOC_IA64_IMM64: new = BFD_RELOC_IA64_LTOFF64I; break;
9919 default: break;
9920 }
9921 break;
9922
9923 case FUNC_PC_RELATIVE:
9924 switch (r_type)
9925 {
9926 case BFD_RELOC_IA64_IMM22: new = BFD_RELOC_IA64_PCREL22; break;
9927 case BFD_RELOC_IA64_IMM64: new = BFD_RELOC_IA64_PCREL64I; break;
9928 case BFD_RELOC_IA64_DIR32MSB: new = BFD_RELOC_IA64_PCREL32MSB; break;
9929 case BFD_RELOC_IA64_DIR32LSB: new = BFD_RELOC_IA64_PCREL32LSB; break;
9930 case BFD_RELOC_IA64_DIR64MSB: new = BFD_RELOC_IA64_PCREL64MSB; break;
9931 case BFD_RELOC_IA64_DIR64LSB: new = BFD_RELOC_IA64_PCREL64LSB; break;
9932 default: break;
9933 }
9934 break;
9935
9936 case FUNC_PLT_RELATIVE:
9937 switch (r_type)
9938 {
9939 case BFD_RELOC_IA64_IMM22: new = BFD_RELOC_IA64_PLTOFF22; break;
9940 case BFD_RELOC_IA64_IMM64: new = BFD_RELOC_IA64_PLTOFF64I; break;
9941 case BFD_RELOC_IA64_DIR64MSB: new = BFD_RELOC_IA64_PLTOFF64MSB;break;
9942 case BFD_RELOC_IA64_DIR64LSB: new = BFD_RELOC_IA64_PLTOFF64LSB;break;
9943 default: break;
9944 }
9945 break;
9946
9947 case FUNC_SEC_RELATIVE:
9948 switch (r_type)
9949 {
9950 case BFD_RELOC_IA64_DIR32MSB: new = BFD_RELOC_IA64_SECREL32MSB;break;
9951 case BFD_RELOC_IA64_DIR32LSB: new = BFD_RELOC_IA64_SECREL32LSB;break;
9952 case BFD_RELOC_IA64_DIR64MSB: new = BFD_RELOC_IA64_SECREL64MSB;break;
9953 case BFD_RELOC_IA64_DIR64LSB: new = BFD_RELOC_IA64_SECREL64LSB;break;
9954 default: break;
9955 }
9956 break;
9957
9958 case FUNC_SEG_RELATIVE:
9959 switch (r_type)
9960 {
9961 case BFD_RELOC_IA64_DIR32MSB: new = BFD_RELOC_IA64_SEGREL32MSB;break;
9962 case BFD_RELOC_IA64_DIR32LSB: new = BFD_RELOC_IA64_SEGREL32LSB;break;
9963 case BFD_RELOC_IA64_DIR64MSB: new = BFD_RELOC_IA64_SEGREL64MSB;break;
9964 case BFD_RELOC_IA64_DIR64LSB: new = BFD_RELOC_IA64_SEGREL64LSB;break;
9965 default: break;
9966 }
9967 break;
9968
9969 case FUNC_LTV_RELATIVE:
9970 switch (r_type)
9971 {
9972 case BFD_RELOC_IA64_DIR32MSB: new = BFD_RELOC_IA64_LTV32MSB; break;
9973 case BFD_RELOC_IA64_DIR32LSB: new = BFD_RELOC_IA64_LTV32LSB; break;
9974 case BFD_RELOC_IA64_DIR64MSB: new = BFD_RELOC_IA64_LTV64MSB; break;
9975 case BFD_RELOC_IA64_DIR64LSB: new = BFD_RELOC_IA64_LTV64LSB; break;
9976 default: break;
9977 }
9978 break;
9979
9980 case FUNC_LT_FPTR_RELATIVE:
9981 switch (r_type)
9982 {
9983 case BFD_RELOC_IA64_IMM22:
9984 new = BFD_RELOC_IA64_LTOFF_FPTR22; break;
9985 case BFD_RELOC_IA64_IMM64:
9986 new = BFD_RELOC_IA64_LTOFF_FPTR64I; break;
9987 default:
9988 break;
9989 }
9990 break;
9991
9992 default:
9993 abort ();
9994 }
9995 /* Hmmmm. Should this ever occur? */
9996 if (new)
9997 return new;
9998 else
9999 return r_type;
10000 }
10001
10002 /* Here is where generate the appropriate reloc for pseudo relocation
10003 functions. */
10004 void
10005 ia64_validate_fix (fix)
10006 fixS *fix;
10007 {
10008 switch (fix->fx_r_type)
10009 {
10010 case BFD_RELOC_IA64_FPTR64I:
10011 case BFD_RELOC_IA64_FPTR32MSB:
10012 case BFD_RELOC_IA64_FPTR64LSB:
10013 case BFD_RELOC_IA64_LTOFF_FPTR22:
10014 case BFD_RELOC_IA64_LTOFF_FPTR64I:
10015 if (fix->fx_offset != 0)
10016 as_bad_where (fix->fx_file, fix->fx_line,
10017 "No addend allowed in @fptr() relocation");
10018 break;
10019 default:
10020 break;
10021 }
10022
10023 return;
10024 }
10025
10026 static void
10027 fix_insn (fix, odesc, value)
10028 fixS *fix;
10029 const struct ia64_operand *odesc;
10030 valueT value;
10031 {
10032 bfd_vma insn[3], t0, t1, control_bits;
10033 const char *err;
10034 char *fixpos;
10035 long slot;
10036
10037 slot = fix->fx_where & 0x3;
10038 fixpos = fix->fx_frag->fr_literal + (fix->fx_where - slot);
10039
10040 /* Bundles are always in little-endian byte order */
10041 t0 = bfd_getl64 (fixpos);
10042 t1 = bfd_getl64 (fixpos + 8);
10043 control_bits = t0 & 0x1f;
10044 insn[0] = (t0 >> 5) & 0x1ffffffffffLL;
10045 insn[1] = ((t0 >> 46) & 0x3ffff) | ((t1 & 0x7fffff) << 18);
10046 insn[2] = (t1 >> 23) & 0x1ffffffffffLL;
10047
10048 err = NULL;
10049 if (odesc - elf64_ia64_operands == IA64_OPND_IMMU64)
10050 {
10051 insn[1] = (value >> 22) & 0x1ffffffffffLL;
10052 insn[2] |= (((value & 0x7f) << 13)
10053 | (((value >> 7) & 0x1ff) << 27)
10054 | (((value >> 16) & 0x1f) << 22)
10055 | (((value >> 21) & 0x1) << 21)
10056 | (((value >> 63) & 0x1) << 36));
10057 }
10058 else if (odesc - elf64_ia64_operands == IA64_OPND_IMMU62)
10059 {
10060 if (value & ~0x3fffffffffffffffULL)
10061 err = "integer operand out of range";
10062 insn[1] = (value >> 21) & 0x1ffffffffffLL;
10063 insn[2] |= (((value & 0xfffff) << 6) | (((value >> 20) & 0x1) << 36));
10064 }
10065 else if (odesc - elf64_ia64_operands == IA64_OPND_TGT64)
10066 {
10067 value >>= 4;
10068 insn[1] = ((value >> 20) & 0x7fffffffffLL) << 2;
10069 insn[2] |= ((((value >> 59) & 0x1) << 36)
10070 | (((value >> 0) & 0xfffff) << 13));
10071 }
10072 else
10073 err = (*odesc->insert) (odesc, value, insn + slot);
10074
10075 if (err)
10076 as_bad_where (fix->fx_file, fix->fx_line, err);
10077
10078 t0 = control_bits | (insn[0] << 5) | (insn[1] << 46);
10079 t1 = ((insn[1] >> 18) & 0x7fffff) | (insn[2] << 23);
10080 number_to_chars_littleendian (fixpos + 0, t0, 8);
10081 number_to_chars_littleendian (fixpos + 8, t1, 8);
10082 }
10083
10084 /* Attempt to simplify or even eliminate a fixup. The return value is
10085 ignored; perhaps it was once meaningful, but now it is historical.
10086 To indicate that a fixup has been eliminated, set FIXP->FX_DONE.
10087
10088 If fixp->fx_addsy is non-NULL, we'll have to generate a reloc entry
10089 (if possible). */
10090 int
10091 md_apply_fix3 (fix, valuep, seg)
10092 fixS *fix;
10093 valueT *valuep;
10094 segT seg ATTRIBUTE_UNUSED;
10095 {
10096 char *fixpos;
10097 valueT value = *valuep;
10098 int adjust = 0;
10099
10100 fixpos = fix->fx_frag->fr_literal + fix->fx_where;
10101
10102 if (fix->fx_pcrel)
10103 {
10104 switch (fix->fx_r_type)
10105 {
10106 case BFD_RELOC_IA64_DIR32MSB:
10107 fix->fx_r_type = BFD_RELOC_IA64_PCREL32MSB;
10108 adjust = 1;
10109 break;
10110
10111 case BFD_RELOC_IA64_DIR32LSB:
10112 fix->fx_r_type = BFD_RELOC_IA64_PCREL32LSB;
10113 adjust = 1;
10114 break;
10115
10116 case BFD_RELOC_IA64_DIR64MSB:
10117 fix->fx_r_type = BFD_RELOC_IA64_PCREL64MSB;
10118 adjust = 1;
10119 break;
10120
10121 case BFD_RELOC_IA64_DIR64LSB:
10122 fix->fx_r_type = BFD_RELOC_IA64_PCREL64LSB;
10123 adjust = 1;
10124 break;
10125
10126 default:
10127 break;
10128 }
10129 }
10130 if (fix->fx_addsy)
10131 {
10132 if (fix->fx_r_type == (int) BFD_RELOC_UNUSED)
10133 {
10134 /* This must be a TAG13 or TAG13b operand. There are no external
10135 relocs defined for them, so we must give an error. */
10136 as_bad_where (fix->fx_file, fix->fx_line,
10137 "%s must have a constant value",
10138 elf64_ia64_operands[fix->tc_fix_data.opnd].desc);
10139 fix->fx_done = 1;
10140 return 1;
10141 }
10142
10143 /* ??? This is a hack copied from tc-i386.c to make PCREL relocs
10144 work. There should be a better way to handle this. */
10145 if (adjust)
10146 fix->fx_offset += fix->fx_where + fix->fx_frag->fr_address;
10147 }
10148 else if (fix->tc_fix_data.opnd == IA64_OPND_NIL)
10149 {
10150 if (fix->tc_fix_data.bigendian)
10151 number_to_chars_bigendian (fixpos, value, fix->fx_size);
10152 else
10153 number_to_chars_littleendian (fixpos, value, fix->fx_size);
10154 fix->fx_done = 1;
10155 return 1;
10156 }
10157 else
10158 {
10159 fix_insn (fix, elf64_ia64_operands + fix->tc_fix_data.opnd, value);
10160 fix->fx_done = 1;
10161 return 1;
10162 }
10163 return 1;
10164 }
10165
10166 /* Generate the BFD reloc to be stuck in the object file from the
10167 fixup used internally in the assembler. */
10168
10169 arelent *
10170 tc_gen_reloc (sec, fixp)
10171 asection *sec ATTRIBUTE_UNUSED;
10172 fixS *fixp;
10173 {
10174 arelent *reloc;
10175
10176 reloc = xmalloc (sizeof (*reloc));
10177 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
10178 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
10179 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
10180 reloc->addend = fixp->fx_offset;
10181 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
10182
10183 if (!reloc->howto)
10184 {
10185 as_bad_where (fixp->fx_file, fixp->fx_line,
10186 "Cannot represent %s relocation in object file",
10187 bfd_get_reloc_code_name (fixp->fx_r_type));
10188 }
10189 return reloc;
10190 }
10191
10192 /* Turn a string in input_line_pointer into a floating point constant
10193 of type TYPE, and store the appropriate bytes in *LIT. The number
10194 of LITTLENUMS emitted is stored in *SIZE. An error message is
10195 returned, or NULL on OK. */
10196
10197 #define MAX_LITTLENUMS 5
10198
10199 char *
10200 md_atof (type, lit, size)
10201 int type;
10202 char *lit;
10203 int *size;
10204 {
10205 LITTLENUM_TYPE words[MAX_LITTLENUMS];
10206 LITTLENUM_TYPE *word;
10207 char *t;
10208 int prec;
10209
10210 switch (type)
10211 {
10212 /* IEEE floats */
10213 case 'f':
10214 case 'F':
10215 case 's':
10216 case 'S':
10217 prec = 2;
10218 break;
10219
10220 case 'd':
10221 case 'D':
10222 case 'r':
10223 case 'R':
10224 prec = 4;
10225 break;
10226
10227 case 'x':
10228 case 'X':
10229 case 'p':
10230 case 'P':
10231 prec = 5;
10232 break;
10233
10234 default:
10235 *size = 0;
10236 return "Bad call to MD_ATOF()";
10237 }
10238 t = atof_ieee (input_line_pointer, type, words);
10239 if (t)
10240 input_line_pointer = t;
10241 *size = prec * sizeof (LITTLENUM_TYPE);
10242
10243 for (word = words + prec - 1; prec--;)
10244 {
10245 md_number_to_chars (lit, (long) (*word--), sizeof (LITTLENUM_TYPE));
10246 lit += sizeof (LITTLENUM_TYPE);
10247 }
10248 return 0;
10249 }
10250
10251 /* Round up a section's size to the appropriate boundary. */
10252 valueT
10253 md_section_align (seg, size)
10254 segT seg;
10255 valueT size;
10256 {
10257 int align = bfd_get_section_alignment (stdoutput, seg);
10258 valueT mask = ((valueT) 1 << align) - 1;
10259
10260 return (size + mask) & ~mask;
10261 }
10262
10263 /* Handle ia64 specific semantics of the align directive. */
10264
10265 void
10266 ia64_md_do_align (n, fill, len, max)
10267 int n ATTRIBUTE_UNUSED;
10268 const char *fill ATTRIBUTE_UNUSED;
10269 int len ATTRIBUTE_UNUSED;
10270 int max ATTRIBUTE_UNUSED;
10271 {
10272 if (subseg_text_p (now_seg))
10273 ia64_flush_insns ();
10274 }
10275
10276 /* This is called from HANDLE_ALIGN in write.c. Fill in the contents
10277 of an rs_align_code fragment. */
10278
10279 void
10280 ia64_handle_align (fragp)
10281 fragS *fragp;
10282 {
10283 /* Use mfi bundle of nops with no stop bits. */
10284 static const unsigned char be_nop[]
10285 = { 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
10286 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c};
10287 static const unsigned char le_nop[]
10288 = { 0x0c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
10289 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00};
10290
10291 int bytes;
10292 char *p;
10293
10294 if (fragp->fr_type != rs_align_code)
10295 return;
10296
10297 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
10298 p = fragp->fr_literal + fragp->fr_fix;
10299
10300 /* Make sure we are on a 16-byte boundary, in case someone has been
10301 putting data into a text section. */
10302 if (bytes & 15)
10303 {
10304 int fix = bytes & 15;
10305 memset (p, 0, fix);
10306 p += fix;
10307 bytes -= fix;
10308 fragp->fr_fix += fix;
10309 }
10310
10311 memcpy (p, (target_big_endian ? be_nop : le_nop), 16);
10312 fragp->fr_var = 16;
10313 }