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