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