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