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