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