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