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