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