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