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