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