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