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