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