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