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