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