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