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