1 /* tc-arc.c -- Assembler for the ARC
2 Copyright (C) 1994-2015 Free Software Foundation, Inc.
4 Contributor: Claudiu Zissulescu <claziss@synopsys.com>
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
25 #include "struc-symbol.h"
26 #include "dwarf2dbg.h"
27 #include "safe-ctype.h"
29 #include "opcode/arc.h"
32 /* Defines section. */
34 #define MAX_FLAG_NAME_LENGHT 3
35 #define MAX_INSN_FIXUPS 2
36 #define MAX_CONSTR_STR 20
39 # define pr_debug(fmt, args...) fprintf (stderr, fmt, ##args)
41 # define pr_debug(fmt, args...)
44 #define MAJOR_OPCODE(x) (((x) & 0xF8000000) >> 27)
45 #define SUB_OPCODE(x) (((x) & 0x003F0000) >> 16)
46 #define LP_INSN(x) ((MAJOR_OPCODE (x) == 0x4) && \
47 (SUB_OPCODE (x) == 0x28))
49 /* Equal to MAX_PRECISION in atof-ieee.c. */
50 #define MAX_LITTLENUMS 6
54 #define regno(x) ((x) & 0x3F)
55 #define is_ir_num(x) (((x) & ~0x3F) == 0)
56 #define is_code_density_p(op) (((op)->subclass == CD1 || (op)->subclass == CD2))
57 #define is_br_jmp_insn_p(op) (((op)->class == BRANCH || (op)->class == JUMP))
58 #define is_kernel_insn_p(op) (((op)->class == KERNEL))
60 /* Generic assembler global variables which must be defined by all
63 /* Characters which always start a comment. */
64 const char comment_chars
[] = "#;";
66 /* Characters which start a comment at the beginning of a line. */
67 const char line_comment_chars
[] = "#";
69 /* Characters which may be used to separate multiple commands on a
71 const char line_separator_chars
[] = "`";
73 /* Characters which are used to indicate an exponent in a floating
75 const char EXP_CHARS
[] = "eE";
77 /* Chars that mean this number is a floating point constant
78 As in 0f12.456 or 0d1.2345e12. */
79 const char FLT_CHARS
[] = "rRsSfFdD";
82 extern int target_big_endian
;
83 const char *arc_target_format
= DEFAULT_TARGET_FORMAT
;
84 static int byte_order
= DEFAULT_BYTE_ORDER
;
86 extern int arc_get_mach (char *);
88 /* Forward declaration. */
89 static void arc_lcomm (int);
90 static void arc_option (int);
91 static void arc_extra_reloc (int);
93 const pseudo_typeS md_pseudo_table
[] =
95 /* Make sure that .word is 32 bits. */
98 { "align", s_align_bytes
, 0 }, /* Defaulting is invalid (0). */
99 { "lcomm", arc_lcomm
, 0 },
100 { "lcommon", arc_lcomm
, 0 },
101 { "cpu", arc_option
, 0 },
103 { "tls_gd_ld", arc_extra_reloc
, BFD_RELOC_ARC_TLS_GD_LD
},
104 { "tls_gd_call", arc_extra_reloc
, BFD_RELOC_ARC_TLS_GD_CALL
},
109 const char *md_shortopts
= "";
113 OPTION_EB
= OPTION_MD_BASE
,
125 /* The following options are deprecated and provided here only for
126 compatibility reasons. */
152 struct option md_longopts
[] =
154 { "EB", no_argument
, NULL
, OPTION_EB
},
155 { "EL", no_argument
, NULL
, OPTION_EL
},
156 { "mcpu", required_argument
, NULL
, OPTION_MCPU
},
157 { "mA6", no_argument
, NULL
, OPTION_ARC600
},
158 { "mARC600", no_argument
, NULL
, OPTION_ARC600
},
159 { "mARC601", no_argument
, NULL
, OPTION_ARC601
},
160 { "mARC700", no_argument
, NULL
, OPTION_ARC700
},
161 { "mA7", no_argument
, NULL
, OPTION_ARC700
},
162 { "mEM", no_argument
, NULL
, OPTION_ARCEM
},
163 { "mHS", no_argument
, NULL
, OPTION_ARCHS
},
164 { "mcode-density", no_argument
, NULL
, OPTION_CD
},
166 /* The following options are deprecated and provided here only for
167 compatibility reasons. */
168 { "mav2em", no_argument
, NULL
, OPTION_ARCEM
},
169 { "mav2hs", no_argument
, NULL
, OPTION_ARCHS
},
170 { "muser-mode-only", no_argument
, NULL
, OPTION_USER_MODE
},
171 { "mld-extension-reg-mask", required_argument
, NULL
, OPTION_LD_EXT_MASK
},
172 { "mswap", no_argument
, NULL
, OPTION_SWAP
},
173 { "mnorm", no_argument
, NULL
, OPTION_NORM
},
174 { "mbarrel-shifter", no_argument
, NULL
, OPTION_BARREL_SHIFT
},
175 { "mbarrel_shifter", no_argument
, NULL
, OPTION_BARREL_SHIFT
},
176 { "mmin-max", no_argument
, NULL
, OPTION_MIN_MAX
},
177 { "mmin_max", no_argument
, NULL
, OPTION_MIN_MAX
},
178 { "mno-mpy", no_argument
, NULL
, OPTION_NO_MPY
},
179 { "mea", no_argument
, NULL
, OPTION_EA
},
180 { "mEA", no_argument
, NULL
, OPTION_EA
},
181 { "mmul64", no_argument
, NULL
, OPTION_MUL64
},
182 { "msimd", no_argument
, NULL
, OPTION_SIMD
},
183 { "mspfp", no_argument
, NULL
, OPTION_SPFP
},
184 { "mspfp-compact", no_argument
, NULL
, OPTION_SPFP
},
185 { "mspfp_compact", no_argument
, NULL
, OPTION_SPFP
},
186 { "mspfp-fast", no_argument
, NULL
, OPTION_SPFP
},
187 { "mspfp_fast", no_argument
, NULL
, OPTION_SPFP
},
188 { "mdpfp", no_argument
, NULL
, OPTION_DPFP
},
189 { "mdpfp-compact", no_argument
, NULL
, OPTION_DPFP
},
190 { "mdpfp_compact", no_argument
, NULL
, OPTION_DPFP
},
191 { "mdpfp-fast", no_argument
, NULL
, OPTION_DPFP
},
192 { "mdpfp_fast", no_argument
, NULL
, OPTION_DPFP
},
193 { "mmac-d16", no_argument
, NULL
, OPTION_XMAC_D16
},
194 { "mmac_d16", no_argument
, NULL
, OPTION_XMAC_D16
},
195 { "mmac-24", no_argument
, NULL
, OPTION_XMAC_24
},
196 { "mmac_24", no_argument
, NULL
, OPTION_XMAC_24
},
197 { "mdsp-packa", no_argument
, NULL
, OPTION_DSP_PACKA
},
198 { "mdsp_packa", no_argument
, NULL
, OPTION_DSP_PACKA
},
199 { "mcrc", no_argument
, NULL
, OPTION_CRC
},
200 { "mdvbf", no_argument
, NULL
, OPTION_DVBF
},
201 { "mtelephony", no_argument
, NULL
, OPTION_TELEPHONY
},
202 { "mxy", no_argument
, NULL
, OPTION_XYMEMORY
},
203 { "mlock", no_argument
, NULL
, OPTION_LOCK
},
204 { "mswape", no_argument
, NULL
, OPTION_SWAPE
},
205 { "mrtsc", no_argument
, NULL
, OPTION_RTSC
},
206 { "mfpuda", no_argument
, NULL
, OPTION_FPUDA
},
208 { NULL
, no_argument
, NULL
, 0 }
211 size_t md_longopts_size
= sizeof (md_longopts
);
213 /* Local data and data types. */
215 /* Used since new relocation types are introduced in this
216 file (DUMMY_RELOC_LITUSE_*). */
217 typedef int extended_bfd_reloc_code_real_type
;
223 extended_bfd_reloc_code_real_type reloc
;
225 /* index into arc_operands. */
226 unsigned int opindex
;
228 /* PC-relative, used by internals fixups. */
231 /* TRUE if this fixup is for LIMM operand. */
239 struct arc_fixup fixups
[MAX_INSN_FIXUPS
];
241 bfd_boolean short_insn
; /* Boolean value: TRUE if current insn is
243 bfd_boolean has_limm
; /* Boolean value: TRUE if limm field is
247 /* Structure to hold any last two instructions. */
248 static struct arc_last_insn
250 /* Saved instruction opcode. */
251 const struct arc_opcode
*opcode
;
253 /* Boolean value: TRUE if current insn is short. */
254 bfd_boolean has_limm
;
256 /* Boolean value: TRUE if current insn has delay slot. */
257 bfd_boolean has_delay_slot
;
260 /* The cpu for which we are generating code. */
261 static unsigned arc_target
= ARC_OPCODE_BASE
;
262 static const char *arc_target_name
= "<all>";
263 static unsigned arc_features
= 0x00;
265 /* The default architecture. */
266 static int arc_mach_type
= bfd_mach_arc_arcv2
;
268 /* Non-zero if the cpu type has been explicitly specified. */
269 static int mach_type_specified_p
= 0;
271 /* The hash table of instruction opcodes. */
272 static struct hash_control
*arc_opcode_hash
;
274 /* The hash table of register symbols. */
275 static struct hash_control
*arc_reg_hash
;
277 /* A table of CPU names and opcode sets. */
278 static const struct cpu_type
288 { "arc600", ARC_OPCODE_ARC600
, bfd_mach_arc_arc600
,
289 E_ARC_MACH_ARC600
, 0x00},
290 { "arc700", ARC_OPCODE_ARC700
, bfd_mach_arc_arc700
,
291 E_ARC_MACH_ARC700
, 0x00},
292 { "arcem", ARC_OPCODE_ARCv2EM
, bfd_mach_arc_arcv2
,
293 EF_ARC_CPU_ARCV2EM
, 0x00},
294 { "archs", ARC_OPCODE_ARCv2HS
, bfd_mach_arc_arcv2
,
295 EF_ARC_CPU_ARCV2HS
, ARC_CD
},
296 { "all", ARC_OPCODE_BASE
, bfd_mach_arc_arcv2
,
303 /* Name of the parsed flag. */
304 char name
[MAX_FLAG_NAME_LENGHT
+1];
306 /* The code of the parsed flag. Valid when is not zero. */
310 /* Used by the arc_reloc_op table. Order is important. */
311 #define O_gotoff O_md1 /* @gotoff relocation. */
312 #define O_gotpc O_md2 /* @gotpc relocation. */
313 #define O_plt O_md3 /* @plt relocation. */
314 #define O_sda O_md4 /* @sda relocation. */
315 #define O_pcl O_md5 /* @pcl relocation. */
316 #define O_tlsgd O_md6 /* @tlsgd relocation. */
317 #define O_tlsie O_md7 /* @tlsie relocation. */
318 #define O_tpoff9 O_md8 /* @tpoff9 relocation. */
319 #define O_tpoff O_md9 /* @tpoff relocation. */
320 #define O_dtpoff9 O_md10 /* @dtpoff9 relocation. */
321 #define O_dtpoff O_md11 /* @dtpoff relocation. */
322 #define O_last O_dtpoff
324 /* Used to define a bracket as operand in tokens. */
325 #define O_bracket O_md32
327 /* Dummy relocation, to be sorted out. */
328 #define DUMMY_RELOC_ARC_ENTRY (BFD_RELOC_UNUSED + 1)
330 #define USER_RELOC_P(R) ((R) >= O_gotoff && (R) <= O_last)
332 /* A table to map the spelling of a relocation operand into an appropriate
333 bfd_reloc_code_real_type type. The table is assumed to be ordered such
334 that op-O_literal indexes into it. */
335 #define ARC_RELOC_TABLE(op) \
336 (&arc_reloc_op[ ((!USER_RELOC_P (op)) \
338 : (int) (op) - (int) O_gotoff) ])
340 #define DEF(NAME, RELOC, REQ) \
341 { #NAME, sizeof (#NAME)-1, O_##NAME, RELOC, REQ}
343 static const struct arc_reloc_op_tag
345 /* String to lookup. */
347 /* Size of the string. */
349 /* Which operator to use. */
351 extended_bfd_reloc_code_real_type reloc
;
352 /* Allows complex relocation expression like identifier@reloc +
354 unsigned int complex_expr
: 1;
358 DEF (gotoff
, BFD_RELOC_ARC_GOTOFF
, 1),
359 DEF (gotpc
, BFD_RELOC_ARC_GOTPC32
, 0),
360 DEF (plt
, BFD_RELOC_ARC_PLT32
, 0),
361 DEF (sda
, DUMMY_RELOC_ARC_ENTRY
, 1),
362 DEF (pcl
, BFD_RELOC_ARC_PC32
, 1),
363 DEF (tlsgd
, BFD_RELOC_ARC_TLS_GD_GOT
, 0),
364 DEF (tlsie
, BFD_RELOC_ARC_TLS_IE_GOT
, 0),
365 DEF (tpoff9
, BFD_RELOC_ARC_TLS_LE_S9
, 0),
366 DEF (tpoff
, BFD_RELOC_ARC_TLS_LE_32
, 0),
367 DEF (dtpoff9
, BFD_RELOC_ARC_TLS_DTPOFF_S9
, 0),
368 DEF (dtpoff
, BFD_RELOC_ARC_TLS_DTPOFF
, 0),
371 static const int arc_num_reloc_op
372 = sizeof (arc_reloc_op
) / sizeof (*arc_reloc_op
);
374 /* Flags to set in the elf header. */
375 static flagword arc_eflag
= 0x00;
377 /* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
378 symbolS
* GOT_symbol
= 0;
380 /* Set to TRUE when we assemble instructions. */
381 static bfd_boolean assembling_insn
= FALSE
;
383 /* Functions declaration. */
385 static void assemble_tokens (const char *, expressionS
*, int,
386 struct arc_flags
*, int);
387 static const struct arc_opcode
*find_opcode_match (const struct arc_opcode
*,
388 expressionS
*, int *,
391 static void assemble_insn (const struct arc_opcode
*, const expressionS
*,
392 int, const struct arc_flags
*, int,
394 static void emit_insn (struct arc_insn
*);
395 static unsigned insert_operand (unsigned, const struct arc_operand
*,
396 offsetT
, char *, unsigned);
397 static const struct arc_opcode
*find_special_case_flag (const char *,
400 static const struct arc_opcode
*find_special_case (const char *,
403 expressionS
*, int *);
404 static const struct arc_opcode
*find_special_case_pseudo (const char *,
410 /* Functions implementation. */
412 /* Like md_number_to_chars but used for limms. The 4-byte limm value,
413 is encoded as 'middle-endian' for a little-endian target. FIXME!
414 this function is used for regular 4 byte instructions as well. */
417 md_number_to_chars_midend (char *buf
,
423 md_number_to_chars (buf
, (val
& 0xffff0000) >> 16, 2);
424 md_number_to_chars (buf
+ 2, (val
& 0xffff), 2);
428 md_number_to_chars (buf
, val
, n
);
432 /* Here ends all the ARCompact extension instruction assembling
436 arc_extra_reloc (int r_type
)
439 symbolS
*sym
, *lab
= NULL
;
441 if (*input_line_pointer
== '@')
442 input_line_pointer
++;
443 c
= get_symbol_name (&sym_name
);
444 sym
= symbol_find_or_make (sym_name
);
445 restore_line_pointer (c
);
446 if (c
== ',' && r_type
== BFD_RELOC_ARC_TLS_GD_LD
)
448 ++input_line_pointer
;
450 c
= get_symbol_name (&lab_name
);
451 lab
= symbol_find_or_make (lab_name
);
452 restore_line_pointer (c
);
455 = fix_new (frag_now
, /* Which frag? */
456 frag_now_fix (), /* Where in that frag? */
457 2, /* size: 1, 2, or 4 usually. */
458 sym
, /* X_add_symbol. */
459 0, /* X_add_number. */
460 FALSE
, /* TRUE if PC-relative relocation. */
461 r_type
/* Relocation type. */);
462 fixP
->fx_subsy
= lab
;
466 arc_lcomm_internal (int ignore ATTRIBUTE_UNUSED
,
467 symbolS
*symbolP
, addressT size
)
472 if (*input_line_pointer
== ',')
474 align
= parse_align (1);
476 if (align
== (addressT
) -1)
491 bss_alloc (symbolP
, size
, align
);
492 S_CLEAR_EXTERNAL (symbolP
);
498 arc_lcomm (int ignore
)
500 symbolS
*symbolP
= s_comm_internal (ignore
, arc_lcomm_internal
);
503 symbol_get_bfdsym (symbolP
)->flags
|= BSF_OBJECT
;
506 /* Select the cpu we're assembling for. */
509 arc_option (int ignore ATTRIBUTE_UNUSED
)
515 c
= get_symbol_name (&cpu
);
516 mach
= arc_get_mach (cpu
);
517 restore_line_pointer (c
);
522 if (!mach_type_specified_p
)
524 arc_mach_type
= mach
;
525 if (!bfd_set_arch_mach (stdoutput
, bfd_arch_arc
, mach
))
526 as_fatal ("could not set architecture and machine");
528 mach_type_specified_p
= 1;
531 if (arc_mach_type
!= mach
)
532 as_warn ("Command-line value overrides \".cpu\" directive");
534 demand_empty_rest_of_line ();
539 as_bad ("invalid identifier for \".cpu\"");
540 ignore_rest_of_line ();
543 /* Smartly print an expression. */
546 debug_exp (expressionS
*t
)
548 const char *name ATTRIBUTE_UNUSED
;
549 const char *namemd ATTRIBUTE_UNUSED
;
551 pr_debug ("debug_exp: ");
555 default: name
= "unknown"; break;
556 case O_illegal
: name
= "O_illegal"; break;
557 case O_absent
: name
= "O_absent"; break;
558 case O_constant
: name
= "O_constant"; break;
559 case O_symbol
: name
= "O_symbol"; break;
560 case O_symbol_rva
: name
= "O_symbol_rva"; break;
561 case O_register
: name
= "O_register"; break;
562 case O_big
: name
= "O_big"; break;
563 case O_uminus
: name
= "O_uminus"; break;
564 case O_bit_not
: name
= "O_bit_not"; break;
565 case O_logical_not
: name
= "O_logical_not"; break;
566 case O_multiply
: name
= "O_multiply"; break;
567 case O_divide
: name
= "O_divide"; break;
568 case O_modulus
: name
= "O_modulus"; break;
569 case O_left_shift
: name
= "O_left_shift"; break;
570 case O_right_shift
: name
= "O_right_shift"; break;
571 case O_bit_inclusive_or
: name
= "O_bit_inclusive_or"; break;
572 case O_bit_or_not
: name
= "O_bit_or_not"; break;
573 case O_bit_exclusive_or
: name
= "O_bit_exclusive_or"; break;
574 case O_bit_and
: name
= "O_bit_and"; break;
575 case O_add
: name
= "O_add"; break;
576 case O_subtract
: name
= "O_subtract"; break;
577 case O_eq
: name
= "O_eq"; break;
578 case O_ne
: name
= "O_ne"; break;
579 case O_lt
: name
= "O_lt"; break;
580 case O_le
: name
= "O_le"; break;
581 case O_ge
: name
= "O_ge"; break;
582 case O_gt
: name
= "O_gt"; break;
583 case O_logical_and
: name
= "O_logical_and"; break;
584 case O_logical_or
: name
= "O_logical_or"; break;
585 case O_index
: name
= "O_index"; break;
586 case O_bracket
: name
= "O_bracket"; break;
591 default: namemd
= "unknown"; break;
592 case O_gotoff
: namemd
= "O_gotoff"; break;
593 case O_gotpc
: namemd
= "O_gotpc"; break;
594 case O_plt
: namemd
= "O_plt"; break;
595 case O_sda
: namemd
= "O_sda"; break;
596 case O_pcl
: namemd
= "O_pcl"; break;
597 case O_tlsgd
: namemd
= "O_tlsgd"; break;
598 case O_tlsie
: namemd
= "O_tlsie"; break;
599 case O_tpoff9
: namemd
= "O_tpoff9"; break;
600 case O_tpoff
: namemd
= "O_tpoff"; break;
601 case O_dtpoff9
: namemd
= "O_dtpoff9"; break;
602 case O_dtpoff
: namemd
= "O_dtpoff"; break;
605 pr_debug ("%s (%s, %s, %d, %s)", name
,
606 (t
->X_add_symbol
) ? S_GET_NAME (t
->X_add_symbol
) : "--",
607 (t
->X_op_symbol
) ? S_GET_NAME (t
->X_op_symbol
) : "--",
608 (int) t
->X_add_number
,
609 (t
->X_md
) ? namemd
: "--");
614 /* Parse the arguments to an opcode. */
617 tokenize_arguments (char *str
,
621 char *old_input_line_pointer
;
622 bfd_boolean saw_comma
= FALSE
;
623 bfd_boolean saw_arg
= FALSE
;
629 const struct arc_reloc_op_tag
*r
;
632 memset (tok
, 0, sizeof (*tok
) * ntok
);
634 /* Save and restore input_line_pointer around this function. */
635 old_input_line_pointer
= input_line_pointer
;
636 input_line_pointer
= str
;
638 while (*input_line_pointer
)
641 switch (*input_line_pointer
)
647 input_line_pointer
++;
648 if (saw_comma
|| !saw_arg
)
655 ++input_line_pointer
;
659 tok
->X_op
= O_bracket
;
666 input_line_pointer
++;
670 tok
->X_op
= O_bracket
;
676 /* We have labels, function names and relocations, all
677 starting with @ symbol. Sort them out. */
678 if (saw_arg
&& !saw_comma
)
682 tok
->X_op
= O_symbol
;
683 tok
->X_md
= O_absent
;
685 if (*input_line_pointer
!= '@')
686 goto normalsymbol
; /* This is not a relocation. */
688 /* A relocation opernad has the following form
689 @identifier@relocation_type. The identifier is already
691 if (tok
->X_op
!= O_symbol
)
693 as_bad (_("No valid label relocation operand"));
697 /* Parse @relocation_type. */
698 memset (&tmpE
, 0, sizeof (tmpE
));
699 tmpE
.X_op
= O_symbol
;
702 if (tmpE
.X_op
!= O_symbol
)
704 as_bad (_("No relocation operand"));
707 p
= S_GET_NAME (tmpE
.X_add_symbol
);
710 /* Go through known relocation and try to find a match. */
711 r
= &arc_reloc_op
[0];
712 for (i
= arc_num_reloc_op
- 1; i
>= 0; i
--, r
++)
713 if (len
== r
->length
&& memcmp (p
, r
->name
, len
) == 0)
718 as_bad (_("Unknown relocation operand: @%s"), p
);
722 tok
->X_add_number
= tmpE
.X_add_number
;
723 if (tmpE
.X_add_number
&& !r
->complex_expr
)
725 as_bad (_("Complex relocation operand."));
729 /* Extra check for TLS: base. */
730 if (*input_line_pointer
== '@')
733 if (tok
->X_op_symbol
!= NULL
734 || tok
->X_op
!= O_symbol
)
736 as_bad (_("Unable to parse this reloc"));
739 input_line_pointer
++;
741 char c
= get_symbol_name (&sym_name
);
742 base
= symbol_find_or_make (sym_name
);
743 tok
->X_op
= O_subtract
;
744 tok
->X_op_symbol
= base
;
745 restore_line_pointer (c
);
757 /* Can be a register. */
758 ++input_line_pointer
;
762 if (saw_arg
&& !saw_comma
)
765 tok
->X_op
= O_absent
;
771 if (tok
->X_op
== O_illegal
|| tok
->X_op
== O_absent
)
783 if (saw_comma
|| brk_lvl
)
785 input_line_pointer
= old_input_line_pointer
;
791 as_bad (_("Brackets in operand field incorrect"));
793 as_bad (_("extra comma"));
795 as_bad (_("missing argument"));
797 as_bad (_("missing comma or colon"));
798 input_line_pointer
= old_input_line_pointer
;
802 /* Parse the flags to a structure. */
805 tokenize_flags (const char *str
,
806 struct arc_flags flags
[],
809 char *old_input_line_pointer
;
810 bfd_boolean saw_flg
= FALSE
;
811 bfd_boolean saw_dot
= FALSE
;
815 memset (flags
, 0, sizeof (*flags
) * nflg
);
817 /* Save and restore input_line_pointer around this function. */
818 old_input_line_pointer
= input_line_pointer
;
819 input_line_pointer
= (char *) str
;
821 while (*input_line_pointer
)
823 switch (*input_line_pointer
)
830 input_line_pointer
++;
838 if (saw_flg
&& !saw_dot
)
841 if (num_flags
>= nflg
)
844 flgnamelen
= strspn (input_line_pointer
, "abcdefghilmnopqrstvwxz");
845 if (flgnamelen
> MAX_FLAG_NAME_LENGHT
)
848 memcpy (flags
->name
, input_line_pointer
, flgnamelen
);
850 input_line_pointer
+= flgnamelen
;
860 input_line_pointer
= old_input_line_pointer
;
865 as_bad (_("extra dot"));
867 as_bad (_("unrecognized flag"));
869 as_bad (_("failed to parse flags"));
870 input_line_pointer
= old_input_line_pointer
;
874 /* The public interface to the instruction assembler. */
877 md_assemble (char *str
)
880 expressionS tok
[MAX_INSN_ARGS
];
883 struct arc_flags flags
[MAX_INSN_FLGS
];
885 /* Split off the opcode. */
886 opnamelen
= strspn (str
, "abcdefghijklmnopqrstuvwxyz_0123468");
887 opname
= xmalloc (opnamelen
+ 1);
888 memcpy (opname
, str
, opnamelen
);
889 opname
[opnamelen
] = '\0';
891 /* Signalize we are assmbling the instructions. */
892 assembling_insn
= TRUE
;
894 /* Tokenize the flags. */
895 if ((nflg
= tokenize_flags (str
+ opnamelen
, flags
, MAX_INSN_FLGS
)) == -1)
897 as_bad (_("syntax error"));
901 /* Scan up to the end of the mnemonic which must end in space or end
904 for (; *str
!= '\0'; str
++)
908 /* Tokenize the rest of the line. */
909 if ((ntok
= tokenize_arguments (str
, tok
, MAX_INSN_ARGS
)) < 0)
911 as_bad (_("syntax error"));
916 assemble_tokens (opname
, tok
, ntok
, flags
, nflg
);
917 assembling_insn
= FALSE
;
920 /* Callback to insert a register into the hash table. */
923 declare_register (char *name
, int number
)
926 symbolS
*regS
= symbol_create (name
, reg_section
,
927 number
, &zero_address_frag
);
929 err
= hash_insert (arc_reg_hash
, S_GET_NAME (regS
), (void *) regS
);
931 as_fatal ("Inserting \"%s\" into register table failed: %s",
935 /* Construct symbols for each of the general registers. */
938 declare_register_set (void)
941 for (i
= 0; i
< 32; ++i
)
945 sprintf (name
, "r%d", i
);
946 declare_register (name
, i
);
949 sprintf (name
, "r%dr%d", i
, i
+1);
950 declare_register (name
, i
);
955 /* Port-specific assembler initialization. This function is called
956 once, at assembler startup time. */
963 /* The endianness can be chosen "at the factory". */
964 target_big_endian
= byte_order
== BIG_ENDIAN
;
966 if (!bfd_set_arch_mach (stdoutput
, bfd_arch_arc
, arc_mach_type
))
967 as_warn (_("could not set architecture and machine"));
969 /* Set elf header flags. */
970 bfd_set_private_flags (stdoutput
, arc_eflag
);
972 /* Set up a hash table for the instructions. */
973 arc_opcode_hash
= hash_new ();
974 if (arc_opcode_hash
== NULL
)
975 as_fatal (_("Virtual memory exhausted"));
977 /* Initialize the hash table with the insns. */
978 for (i
= 0; i
< arc_num_opcodes
;)
980 const char *name
, *retval
;
982 name
= arc_opcodes
[i
].name
;
983 retval
= hash_insert (arc_opcode_hash
, name
, (void *) &arc_opcodes
[i
]);
985 as_fatal (_("internal error: can't hash opcode '%s': %s"),
988 while (++i
< arc_num_opcodes
989 && (arc_opcodes
[i
].name
== name
990 || !strcmp (arc_opcodes
[i
].name
, name
)))
994 /* Register declaration. */
995 arc_reg_hash
= hash_new ();
996 if (arc_reg_hash
== NULL
)
997 as_fatal (_("Virtual memory exhausted"));
999 declare_register_set ();
1000 declare_register ("gp", 26);
1001 declare_register ("fp", 27);
1002 declare_register ("sp", 28);
1003 declare_register ("ilink", 29);
1004 declare_register ("ilink1", 29);
1005 declare_register ("ilink2", 30);
1006 declare_register ("blink", 31);
1008 declare_register ("mlo", 57);
1009 declare_register ("mmid", 58);
1010 declare_register ("mhi", 59);
1012 declare_register ("acc1", 56);
1013 declare_register ("acc2", 57);
1015 declare_register ("lp_count", 60);
1016 declare_register ("pcl", 63);
1018 /* Initialize the last instructions. */
1019 memset (&arc_last_insns
[0], 0, sizeof (arc_last_insns
));
1022 /* Write a value out to the object file, using the appropriate
1026 md_number_to_chars (char *buf
,
1030 if (target_big_endian
)
1031 number_to_chars_bigendian (buf
, val
, n
);
1033 number_to_chars_littleendian (buf
, val
, n
);
1036 /* Round up a section size to the appropriate boundary. */
1039 md_section_align (segT segment
,
1042 int align
= bfd_get_section_alignment (stdoutput
, segment
);
1044 return ((size
+ (1 << align
) - 1) & (-1 << align
));
1047 /* The location from which a PC relative jump should be calculated,
1048 given a PC relative reloc. */
1051 md_pcrel_from_section (fixS
*fixP
,
1054 offsetT base
= fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
1056 pr_debug ("pcrel_from_section, fx_offset = %d\n", (int) fixP
->fx_offset
);
1058 if (fixP
->fx_addsy
!= (symbolS
*) NULL
1059 && (!S_IS_DEFINED (fixP
->fx_addsy
)
1060 || S_GET_SEGMENT (fixP
->fx_addsy
) != sec
))
1062 pr_debug ("Unknown pcrel symbol: %s\n", S_GET_NAME (fixP
->fx_addsy
));
1064 /* The symbol is undefined (or is defined but not in this section).
1065 Let the linker figure it out. */
1069 if ((int) fixP
->fx_r_type
< 0)
1071 /* These are the "internal" relocations. Align them to
1072 32 bit boundary (PCL), for the moment. */
1077 switch (fixP
->fx_r_type
)
1079 case BFD_RELOC_ARC_PC32
:
1080 /* The hardware calculates relative to the start of the
1081 insn, but this relocation is relative to location of the
1082 LIMM, compensate. TIP: the base always needs to be
1083 substracted by 4 as we do not support this type of PCrel
1084 relocation for short instructions. */
1085 base
-= fixP
->fx_where
- fixP
->fx_dot_value
;
1086 gas_assert ((fixP
->fx_where
- fixP
->fx_dot_value
) == 4);
1088 case BFD_RELOC_ARC_PLT32
:
1089 case BFD_RELOC_ARC_S25H_PCREL_PLT
:
1090 case BFD_RELOC_ARC_S21H_PCREL_PLT
:
1091 case BFD_RELOC_ARC_S25W_PCREL_PLT
:
1092 case BFD_RELOC_ARC_S21W_PCREL_PLT
:
1094 case BFD_RELOC_ARC_S21H_PCREL
:
1095 case BFD_RELOC_ARC_S25H_PCREL
:
1096 case BFD_RELOC_ARC_S13_PCREL
:
1097 case BFD_RELOC_ARC_S21W_PCREL
:
1098 case BFD_RELOC_ARC_S25W_PCREL
:
1102 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1103 _("unhandled reloc %s in md_pcrel_from_section"),
1104 bfd_get_reloc_code_name (fixP
->fx_r_type
));
1109 pr_debug ("pcrel from %x + %lx = %x, symbol: %s (%x)\n",
1110 fixP
->fx_frag
->fr_address
, fixP
->fx_where
, base
,
1111 fixP
->fx_addsy
? S_GET_NAME (fixP
->fx_addsy
) : "(null)",
1112 fixP
->fx_addsy
? S_GET_VALUE (fixP
->fx_addsy
) : 0);
1117 /* Given a BFD relocation find the coresponding operand. */
1119 static const struct arc_operand
*
1120 find_operand_for_reloc (extended_bfd_reloc_code_real_type reloc
)
1124 for (i
= 0; i
< arc_num_operands
; i
++)
1125 if (arc_operands
[i
].default_reloc
== reloc
)
1126 return &arc_operands
[i
];
1130 /* Apply a fixup to the object code. At this point all symbol values
1131 should be fully resolved, and we attempt to completely resolve the
1132 reloc. If we can not do that, we determine the correct reloc code
1133 and put it back in the fixup. To indicate that a fixup has been
1134 eliminated, set fixP->fx_done. */
1137 md_apply_fix (fixS
*fixP
,
1141 char * const fixpos
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1142 valueT value
= *valP
;
1144 symbolS
*fx_addsy
, *fx_subsy
;
1146 segT add_symbol_segment
= absolute_section
;
1147 segT sub_symbol_segment
= absolute_section
;
1148 const struct arc_operand
*operand
= NULL
;
1149 extended_bfd_reloc_code_real_type reloc
;
1151 pr_debug ("%s:%u: apply_fix: r_type=%d (%s) value=0x%lX offset=0x%lX\n",
1152 fixP
->fx_file
, fixP
->fx_line
, fixP
->fx_r_type
,
1153 ((int) fixP
->fx_r_type
< 0) ? "Internal":
1154 bfd_get_reloc_code_name (fixP
->fx_r_type
), value
,
1157 fx_addsy
= fixP
->fx_addsy
;
1158 fx_subsy
= fixP
->fx_subsy
;
1163 add_symbol_segment
= S_GET_SEGMENT (fx_addsy
);
1167 && fixP
->fx_r_type
!= BFD_RELOC_ARC_TLS_DTPOFF
1168 && fixP
->fx_r_type
!= BFD_RELOC_ARC_TLS_DTPOFF_S9
1169 && fixP
->fx_r_type
!= BFD_RELOC_ARC_TLS_GD_LD
)
1171 resolve_symbol_value (fx_subsy
);
1172 sub_symbol_segment
= S_GET_SEGMENT (fx_subsy
);
1174 if (sub_symbol_segment
== absolute_section
)
1176 /* The symbol is really a constant. */
1177 fx_offset
-= S_GET_VALUE (fx_subsy
);
1182 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1183 _("can't resolve `%s' {%s section} - `%s' {%s section}"),
1184 fx_addsy
? S_GET_NAME (fx_addsy
) : "0",
1185 segment_name (add_symbol_segment
),
1186 S_GET_NAME (fx_subsy
),
1187 segment_name (sub_symbol_segment
));
1193 && !S_IS_WEAK (fx_addsy
))
1195 if (add_symbol_segment
== seg
1198 value
+= S_GET_VALUE (fx_addsy
);
1199 value
-= md_pcrel_from_section (fixP
, seg
);
1201 fixP
->fx_pcrel
= FALSE
;
1203 else if (add_symbol_segment
== absolute_section
)
1205 value
= fixP
->fx_offset
;
1206 fx_offset
+= S_GET_VALUE (fixP
->fx_addsy
);
1208 fixP
->fx_pcrel
= FALSE
;
1213 fixP
->fx_done
= TRUE
;
1218 && ((S_IS_DEFINED (fx_addsy
)
1219 && S_GET_SEGMENT (fx_addsy
) != seg
)
1220 || S_IS_WEAK (fx_addsy
)))
1221 value
+= md_pcrel_from_section (fixP
, seg
);
1223 switch (fixP
->fx_r_type
)
1225 case BFD_RELOC_ARC_32_ME
:
1226 /* This is a pc-relative value in a LIMM. Adjust it to the
1227 address of the instruction not to the address of the
1228 LIMM. Note: it is not anylonger valid this afirmation as
1229 the linker consider ARC_PC32 a fixup to entire 64 bit
1231 fixP
->fx_offset
+= fixP
->fx_frag
->fr_address
;
1234 fixP
->fx_r_type
= BFD_RELOC_ARC_PC32
;
1236 case BFD_RELOC_ARC_PC32
:
1239 if ((int) fixP
->fx_r_type
< 0)
1240 as_fatal (_("PC relative relocation not allowed for (internal) type %d"),
1246 pr_debug ("%s:%u: apply_fix: r_type=%d (%s) value=0x%lX offset=0x%lX\n",
1247 fixP
->fx_file
, fixP
->fx_line
, fixP
->fx_r_type
,
1248 ((int) fixP
->fx_r_type
< 0) ? "Internal":
1249 bfd_get_reloc_code_name (fixP
->fx_r_type
), value
,
1255 /* Addjust the value if we have a constant. */
1258 /* For hosts with longs bigger than 32-bits make sure that the top
1259 bits of a 32-bit negative value read in by the parser are set,
1260 so that the correct comparisons are made. */
1261 if (value
& 0x80000000)
1262 value
|= (-1L << 31);
1264 reloc
= fixP
->fx_r_type
;
1272 md_number_to_chars (fixpos
, value
, fixP
->fx_size
);
1275 case BFD_RELOC_ARC_GOTPC32
:
1276 /* I cannot fix an GOTPC relocation because I need to relax it
1277 from ld rx,[pcl,@sym@gotpc] to add rx,pcl,@sym@gotpc. */
1278 as_bad (_("Unsupported operation on reloc"));
1280 case BFD_RELOC_ARC_GOTOFF
:
1281 case BFD_RELOC_ARC_32_ME
:
1282 case BFD_RELOC_ARC_PC32
:
1283 md_number_to_chars_midend (fixpos
, value
, fixP
->fx_size
);
1286 case BFD_RELOC_ARC_PLT32
:
1287 md_number_to_chars_midend (fixpos
, value
, fixP
->fx_size
);
1290 case BFD_RELOC_ARC_S25H_PCREL_PLT
:
1291 reloc
= BFD_RELOC_ARC_S25W_PCREL
;
1294 case BFD_RELOC_ARC_S21H_PCREL_PLT
:
1295 reloc
= BFD_RELOC_ARC_S21H_PCREL
;
1298 case BFD_RELOC_ARC_S25W_PCREL_PLT
:
1299 reloc
= BFD_RELOC_ARC_S25W_PCREL
;
1302 case BFD_RELOC_ARC_S21W_PCREL_PLT
:
1303 reloc
= BFD_RELOC_ARC_S21W_PCREL
;
1305 case BFD_RELOC_ARC_S25W_PCREL
:
1306 case BFD_RELOC_ARC_S21W_PCREL
:
1307 case BFD_RELOC_ARC_S21H_PCREL
:
1308 case BFD_RELOC_ARC_S25H_PCREL
:
1309 case BFD_RELOC_ARC_S13_PCREL
:
1311 operand
= find_operand_for_reloc (reloc
);
1312 gas_assert (operand
);
1315 case BFD_RELOC_ARC_TLS_DTPOFF
:
1316 case BFD_RELOC_ARC_TLS_LE_32
:
1319 gas_assert (!fixP
->fx_addsy
);
1320 gas_assert (!fixP
->fx_subsy
);
1321 md_number_to_chars_midend (fixpos
, value
, fixP
->fx_size
);
1326 value
= fixP
->fx_offset
;
1327 fixP
->fx_offset
= 0;
1330 case BFD_RELOC_ARC_TLS_GD_GOT
:
1331 case BFD_RELOC_ARC_TLS_IE_GOT
:
1332 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1333 md_number_to_chars_midend (fixpos
, value
, fixP
->fx_size
);
1336 case BFD_RELOC_ARC_TLS_GD_LD
:
1337 gas_assert (!fixP
->fx_offset
);
1340 = (S_GET_VALUE (fixP
->fx_subsy
)
1341 - fixP
->fx_frag
->fr_address
- fixP
->fx_where
);
1342 fixP
->fx_subsy
= NULL
;
1344 case BFD_RELOC_ARC_TLS_GD_CALL
:
1345 /* These two relocs are there just to allow ld to change the tls
1346 model for this symbol, by patching the code. */
1348 /* The offset - and scale, if any - will be installed by the
1350 gas_assert (!fixP
->fx_done
);
1351 S_SET_THREAD_LOCAL (fixP
->fx_addsy
);
1354 case BFD_RELOC_ARC_TLS_LE_S9
:
1355 case BFD_RELOC_ARC_TLS_DTPOFF_S9
:
1356 as_bad (_("TLS_*_S9 relocs are not supported yet"));
1361 if ((int) fixP
->fx_r_type
>= 0)
1362 as_fatal (_("unhandled relocation type %s"),
1363 bfd_get_reloc_code_name (fixP
->fx_r_type
));
1365 /* The rest of these fixups needs to be completely resolved as
1367 if (fixP
->fx_addsy
!= 0
1368 && S_GET_SEGMENT (fixP
->fx_addsy
) != absolute_section
)
1369 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1370 _("non-absolute expression in constant field"));
1372 gas_assert (-(int) fixP
->fx_r_type
< (int) arc_num_operands
);
1373 operand
= &arc_operands
[-(int) fixP
->fx_r_type
];
1378 if (target_big_endian
)
1380 switch (fixP
->fx_size
)
1383 insn
= bfd_getb32 (fixpos
);
1386 insn
= bfd_getb16 (fixpos
);
1389 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1390 _("unknown fixup size"));
1396 switch (fixP
->fx_size
)
1399 insn
= bfd_getl16 (fixpos
) << 16 | bfd_getl16 (fixpos
+ 2);
1402 insn
= bfd_getl16 (fixpos
);
1405 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1406 _("unknown fixup size"));
1410 insn
= insert_operand (insn
, operand
, (offsetT
) value
,
1411 fixP
->fx_file
, fixP
->fx_line
);
1413 md_number_to_chars_midend (fixpos
, insn
, fixP
->fx_size
);
1416 /* Prepare machine-dependent frags for relaxation.
1418 Called just before relaxation starts. Any symbol that is now undefined
1419 will not become defined.
1421 Return the correct fr_subtype in the frag.
1423 Return the initial "guess for fr_var" to caller. The guess for fr_var
1424 is *actually* the growth beyond fr_fix. Whatever we do to grow fr_fix
1425 or fr_var contributes to our returned value.
1427 Although it may not be explicit in the frag, pretend
1428 fr_var starts with a value. */
1431 md_estimate_size_before_relax (fragS
*fragP ATTRIBUTE_UNUSED
,
1432 segT segment ATTRIBUTE_UNUSED
)
1437 pr_debug ("%s:%d: md_estimate_size_before_relax: %d\n",
1438 fragP
->fr_file
, fragP
->fr_line
, growth
);
1440 as_fatal (_("md_estimate_size_before_relax\n"));
1444 /* Translate internal representation of relocation info to BFD target
1448 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
,
1452 bfd_reloc_code_real_type code
;
1454 reloc
= (arelent
*) xmalloc (sizeof (* reloc
));
1455 reloc
->sym_ptr_ptr
= (asymbol
**) xmalloc (sizeof (asymbol
*));
1456 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixP
->fx_addsy
);
1457 reloc
->address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
1459 /* Make sure none of our internal relocations make it this far.
1460 They'd better have been fully resolved by this point. */
1461 gas_assert ((int) fixP
->fx_r_type
> 0);
1463 code
= fixP
->fx_r_type
;
1465 /* if we have something like add gp, pcl,
1466 _GLOBAL_OFFSET_TABLE_@gotpc. */
1467 if (code
== BFD_RELOC_ARC_GOTPC32
1469 && fixP
->fx_addsy
== GOT_symbol
)
1470 code
= BFD_RELOC_ARC_GOTPC
;
1472 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, code
);
1473 if (reloc
->howto
== NULL
)
1475 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1476 _("cannot represent `%s' relocation in object file"),
1477 bfd_get_reloc_code_name (code
));
1481 if (!fixP
->fx_pcrel
!= !reloc
->howto
->pc_relative
)
1482 as_fatal (_("internal error? cannot generate `%s' relocation"),
1483 bfd_get_reloc_code_name (code
));
1485 gas_assert (!fixP
->fx_pcrel
== !reloc
->howto
->pc_relative
);
1487 if (code
== BFD_RELOC_ARC_TLS_DTPOFF
1488 || code
== BFD_RELOC_ARC_TLS_DTPOFF_S9
)
1491 = fixP
->fx_subsy
? symbol_get_bfdsym (fixP
->fx_subsy
) : NULL
;
1492 /* We just want to store a 24 bit index, but we have to wait
1493 till after write_contents has been called via
1494 bfd_map_over_sections before we can get the index from
1495 _bfd_elf_symbol_from_bfd_symbol. Thus, the write_relocs
1496 function is elf32-arc.c has to pick up the slack.
1497 Unfortunately, this leads to problems with hosts that have
1498 pointers wider than long (bfd_vma). There would be various
1499 ways to handle this, all error-prone :-( */
1500 reloc
->addend
= (bfd_vma
) sym
;
1501 if ((asymbol
*) reloc
->addend
!= sym
)
1503 as_bad ("Can't store pointer\n");
1508 reloc
->addend
= fixP
->fx_offset
;
1513 /* Perform post-processing of machine-dependent frags after relaxation.
1514 Called after relaxation is finished.
1515 In: Address of frag.
1516 fr_type == rs_machine_dependent.
1517 fr_subtype is what the address relaxed to.
1519 Out: Any fixS:s and constants are set up. */
1522 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
,
1523 segT segment ATTRIBUTE_UNUSED
,
1524 fragS
*fragP ATTRIBUTE_UNUSED
)
1526 pr_debug ("%s:%d: md_convert_frag, subtype: %d, fix: %d, var: %d\n",
1527 fragP
->fr_file
, fragP
->fr_line
,
1528 fragP
->fr_subtype
, fragP
->fr_fix
, fragP
->fr_var
);
1532 /* We have no need to default values of symbols. We could catch
1533 register names here, but that is handled by inserting them all in
1534 the symbol table to begin with. */
1537 md_undefined_symbol (char *name
)
1539 /* The arc abi demands that a GOT[0] should be referencible as
1540 [pc+_DYNAMIC@gotpc]. Hence we convert a _DYNAMIC@gotpc to a
1541 GOTPC reference to _GLOBAL_OFFSET_TABLE_. */
1543 && (*(name
+1) == 'G')
1544 && (strcmp (name
, GLOBAL_OFFSET_TABLE_NAME
) == 0))
1546 && (*(name
+1) == 'D')
1547 && (strcmp (name
, DYNAMIC_STRUCT_NAME
) == 0)))
1551 if (symbol_find (name
))
1552 as_bad ("GOT already in symbol table");
1554 GOT_symbol
= symbol_new (GLOBAL_OFFSET_TABLE_NAME
, undefined_section
,
1555 (valueT
) 0, &zero_address_frag
);
1562 /* Turn a string in input_line_pointer into a floating point constant
1563 of type type, and store the appropriate bytes in *litP. The number
1564 of LITTLENUMS emitted is stored in *sizeP. An error message is
1565 returned, or NULL on OK. */
1568 md_atof (int type
, char *litP
, int *sizeP
)
1570 return ieee_md_atof (type
, litP
, sizeP
, target_big_endian
);
1573 /* Called for any expression that can not be recognized. When the
1574 function is called, `input_line_pointer' will point to the start of
1578 md_operand (expressionS
*expressionP ATTRIBUTE_UNUSED
)
1580 char *p
= input_line_pointer
;
1583 input_line_pointer
++;
1584 expressionP
->X_op
= O_symbol
;
1585 expression (expressionP
);
1589 /* This function is called from the function 'expression', it attempts
1590 to parse special names (in our case register names). It fills in
1591 the expression with the identified register. It returns TRUE if
1592 it is a register and FALSE otherwise. */
1595 arc_parse_name (const char *name
,
1596 struct expressionS
*e
)
1600 if (!assembling_insn
)
1603 /* Handle only registers. */
1604 if (e
->X_op
!= O_absent
)
1607 sym
= hash_find (arc_reg_hash
, name
);
1610 e
->X_op
= O_register
;
1611 e
->X_add_number
= S_GET_VALUE (sym
);
1618 Invocation line includes a switch not recognized by the base assembler.
1619 See if it's a processor-specific option.
1621 New options (supported) are:
1623 -mcpu=<cpu name> Assemble for selected processor
1624 -EB/-mbig-endian Big-endian
1625 -EL/-mlittle-endian Little-endian
1627 The following CPU names are recognized:
1628 arc700, av2em, av2hs. */
1631 md_parse_option (int c
, char *arg ATTRIBUTE_UNUSED
)
1633 int cpu_flags
= EF_ARC_CPU_GENERIC
;
1639 return md_parse_option (OPTION_MCPU
, "arc600");
1642 return md_parse_option (OPTION_MCPU
, "arc700");
1645 return md_parse_option (OPTION_MCPU
, "arcem");
1648 return md_parse_option (OPTION_MCPU
, "archs");
1653 char *s
= alloca (strlen (arg
) + 1);
1660 *t
= TOLOWER (*arg1
++);
1664 for (i
= 0; cpu_types
[i
].name
; ++i
)
1666 if (!strcmp (cpu_types
[i
].name
, s
))
1668 arc_target
= cpu_types
[i
].flags
;
1669 arc_target_name
= cpu_types
[i
].name
;
1670 arc_features
= cpu_types
[i
].features
;
1671 arc_mach_type
= cpu_types
[i
].mach
;
1672 cpu_flags
= cpu_types
[i
].eflags
;
1674 mach_type_specified_p
= 1;
1679 if (!cpu_types
[i
].name
)
1681 as_fatal (_("unknown architecture: %s\n"), arg
);
1687 arc_target_format
= "elf32-bigarc";
1688 byte_order
= BIG_ENDIAN
;
1692 arc_target_format
= "elf32-littlearc";
1693 byte_order
= LITTLE_ENDIAN
;
1697 /* This option has an effect only on ARC EM. */
1698 if (arc_target
& ARC_OPCODE_ARCv2EM
)
1699 arc_features
|= ARC_CD
;
1702 case OPTION_USER_MODE
:
1703 case OPTION_LD_EXT_MASK
:
1706 case OPTION_BARREL_SHIFT
:
1707 case OPTION_MIN_MAX
:
1714 case OPTION_XMAC_D16
:
1715 case OPTION_XMAC_24
:
1716 case OPTION_DSP_PACKA
:
1719 case OPTION_TELEPHONY
:
1720 case OPTION_XYMEMORY
:
1725 /* Dummy options. */
1731 if (cpu_flags
!= EF_ARC_CPU_GENERIC
)
1732 arc_eflag
= (arc_eflag
& ~EF_ARC_MACH_MSK
) | cpu_flags
;
1738 md_show_usage (FILE *stream
)
1740 fprintf (stream
, _("ARC-specific assembler options:\n"));
1742 fprintf (stream
, " -mcpu=<cpu name>\t assemble for CPU <cpu name>\n");
1744 " -mcode-density\t enable code density option for ARC EM\n");
1746 fprintf (stream
, _("\
1747 -EB assemble code for a big-endian cpu\n"));
1748 fprintf (stream
, _("\
1749 -EL assemble code for a little-endian cpu\n"));
1753 preprocess_operands (const struct arc_opcode
*opcode
,
1761 const struct arc_aux_reg
*auxr
;
1763 for (i
= 0; i
< ntok
; i
++)
1765 switch (tok
[i
].X_op
)
1769 break; /* Throw and error. */
1772 if (opcode
->class != AUXREG
)
1774 /* Convert the symbol to a constant if possible. */
1775 p
= S_GET_NAME (tok
[i
].X_add_symbol
);
1778 auxr
= &arc_aux_regs
[0];
1779 for (j
= 0; j
< arc_num_aux_regs
; j
++, auxr
++)
1780 if (len
== auxr
->length
1781 && strcasecmp (auxr
->name
, p
) == 0)
1783 tok
[i
].X_op
= O_constant
;
1784 tok
[i
].X_add_number
= auxr
->address
;
1794 /* Given an opcode name, pre-tockenized set of argumenst and the
1795 opcode flags, take it all the way through emission. */
1798 assemble_tokens (const char *opname
,
1801 struct arc_flags
*pflags
,
1804 bfd_boolean found_something
= FALSE
;
1805 const struct arc_opcode
*opcode
;
1808 /* Search opcodes. */
1809 opcode
= (const struct arc_opcode
*) hash_find (arc_opcode_hash
, opname
);
1811 /* Couldn't find opcode conventional way, try special cases. */
1813 opcode
= find_special_case (opname
, &nflgs
, pflags
, tok
, &ntok
);
1817 pr_debug ("%s:%d: assemble_tokens: %s trying opcode 0x%08X\n",
1818 frag_now
->fr_file
, frag_now
->fr_line
, opcode
->name
,
1821 preprocess_operands (opcode
, tok
, ntok
);
1823 found_something
= TRUE
;
1824 opcode
= find_opcode_match (opcode
, tok
, &ntok
, pflags
, nflgs
, &cpumatch
);
1827 struct arc_insn insn
;
1828 assemble_insn (opcode
, tok
, ntok
, pflags
, nflgs
, &insn
);
1834 if (found_something
)
1837 as_bad (_("inappropriate arguments for opcode '%s'"), opname
);
1839 as_bad (_("opcode '%s' not supported for target %s"), opname
,
1843 as_bad (_("unknown opcode '%s'"), opname
);
1846 /* Used to find special case opcode. */
1848 static const struct arc_opcode
*
1849 find_special_case (const char *opname
,
1851 struct arc_flags
*pflags
,
1855 const struct arc_opcode
*opcode
;
1857 opcode
= find_special_case_pseudo (opname
, ntok
, tok
, nflgs
, pflags
);
1860 opcode
= find_special_case_flag (opname
, nflgs
, pflags
);
1865 /* Swap operand tokens. */
1868 swap_operand (expressionS
*operand_array
,
1870 unsigned destination
)
1872 expressionS cpy_operand
;
1873 expressionS
*src_operand
;
1874 expressionS
*dst_operand
;
1877 if (source
== destination
)
1880 src_operand
= &operand_array
[source
];
1881 dst_operand
= &operand_array
[destination
];
1882 size
= sizeof (expressionS
);
1884 /* Make copy of operand to swap with and swap. */
1885 memcpy (&cpy_operand
, dst_operand
, size
);
1886 memcpy (dst_operand
, src_operand
, size
);
1887 memcpy (src_operand
, &cpy_operand
, size
);
1890 /* Check if *op matches *tok type.
1891 Returns FALSE if they don't match, TRUE if they match. */
1894 pseudo_operand_match (const expressionS
*tok
,
1895 const struct arc_operand_operation
*op
)
1897 offsetT min
, max
, val
;
1899 const struct arc_operand
*operand_real
= &arc_operands
[op
->operand_idx
];
1905 if (operand_real
->bits
== 32 && (operand_real
->flags
& ARC_OPERAND_LIMM
))
1907 else if (!(operand_real
->flags
& ARC_OPERAND_IR
))
1909 val
= tok
->X_add_number
;
1910 if (operand_real
->flags
& ARC_OPERAND_SIGNED
)
1912 max
= (1 << (operand_real
->bits
- 1)) - 1;
1913 min
= -(1 << (operand_real
->bits
- 1));
1917 max
= (1 << operand_real
->bits
) - 1;
1920 if (min
<= val
&& val
<= max
)
1926 /* Handle all symbols as long immediates or signed 9. */
1927 if (operand_real
->flags
& ARC_OPERAND_LIMM
||
1928 ((operand_real
->flags
& ARC_OPERAND_SIGNED
) && operand_real
->bits
== 9))
1933 if (operand_real
->flags
& ARC_OPERAND_IR
)
1938 if (operand_real
->flags
& ARC_OPERAND_BRAKET
)
1949 /* Find pseudo instruction in array. */
1951 static const struct arc_pseudo_insn
*
1952 find_pseudo_insn (const char *opname
,
1954 const expressionS
*tok
)
1956 const struct arc_pseudo_insn
*pseudo_insn
= NULL
;
1957 const struct arc_operand_operation
*op
;
1961 for (i
= 0; i
< arc_num_pseudo_insn
; ++i
)
1963 pseudo_insn
= &arc_pseudo_insns
[i
];
1964 if (strcmp (pseudo_insn
->mnemonic_p
, opname
) == 0)
1966 op
= pseudo_insn
->operand
;
1967 for (j
= 0; j
< ntok
; ++j
)
1968 if (!pseudo_operand_match (&tok
[j
], &op
[j
]))
1971 /* Found the right instruction. */
1979 /* Assumes the expressionS *tok is of sufficient size. */
1981 static const struct arc_opcode
*
1982 find_special_case_pseudo (const char *opname
,
1986 struct arc_flags
*pflags
)
1988 const struct arc_pseudo_insn
*pseudo_insn
= NULL
;
1989 const struct arc_operand_operation
*operand_pseudo
;
1990 const struct arc_operand
*operand_real
;
1992 char construct_operand
[MAX_CONSTR_STR
];
1994 /* Find whether opname is in pseudo instruction array. */
1995 pseudo_insn
= find_pseudo_insn (opname
, *ntok
, tok
);
1997 if (pseudo_insn
== NULL
)
2000 /* Handle flag, Limited to one flag at the moment. */
2001 if (pseudo_insn
->flag_r
!= NULL
)
2002 *nflgs
+= tokenize_flags (pseudo_insn
->flag_r
, &pflags
[*nflgs
],
2003 MAX_INSN_FLGS
- *nflgs
);
2005 /* Handle operand operations. */
2006 for (i
= 0; i
< pseudo_insn
->operand_cnt
; ++i
)
2008 operand_pseudo
= &pseudo_insn
->operand
[i
];
2009 operand_real
= &arc_operands
[operand_pseudo
->operand_idx
];
2011 if (operand_real
->flags
& ARC_OPERAND_BRAKET
&&
2012 !operand_pseudo
->needs_insert
)
2015 /* Has to be inserted (i.e. this token does not exist yet). */
2016 if (operand_pseudo
->needs_insert
)
2018 if (operand_real
->flags
& ARC_OPERAND_BRAKET
)
2020 tok
[i
].X_op
= O_bracket
;
2025 /* Check if operand is a register or constant and handle it
2027 if (operand_real
->flags
& ARC_OPERAND_IR
)
2028 snprintf (construct_operand
, MAX_CONSTR_STR
, "r%d",
2029 operand_pseudo
->count
);
2031 snprintf (construct_operand
, MAX_CONSTR_STR
, "%d",
2032 operand_pseudo
->count
);
2034 tokenize_arguments (construct_operand
, &tok
[i
], 1);
2038 else if (operand_pseudo
->count
)
2040 /* Operand number has to be adjusted accordingly (by operand
2042 switch (tok
[i
].X_op
)
2045 tok
[i
].X_add_number
+= operand_pseudo
->count
;
2058 /* Swap operands if necessary. Only supports one swap at the
2060 for (i
= 0; i
< pseudo_insn
->operand_cnt
; ++i
)
2062 operand_pseudo
= &pseudo_insn
->operand
[i
];
2064 if (operand_pseudo
->swap_operand_idx
== i
)
2067 swap_operand (tok
, i
, operand_pseudo
->swap_operand_idx
);
2069 /* Prevent a swap back later by breaking out. */
2073 return (const struct arc_opcode
*)
2074 hash_find (arc_opcode_hash
, pseudo_insn
->mnemonic_r
);
2077 static const struct arc_opcode
*
2078 find_special_case_flag (const char *opname
,
2080 struct arc_flags
*pflags
)
2084 unsigned flag_idx
, flag_arr_idx
;
2085 size_t flaglen
, oplen
;
2086 const struct arc_flag_special
*arc_flag_special_opcode
;
2087 const struct arc_opcode
*opcode
;
2089 /* Search for special case instruction. */
2090 for (i
= 0; i
< arc_num_flag_special
; i
++)
2092 arc_flag_special_opcode
= &arc_flag_special_cases
[i
];
2093 oplen
= strlen (arc_flag_special_opcode
->name
);
2095 if (strncmp (opname
, arc_flag_special_opcode
->name
, oplen
) != 0)
2098 /* Found a potential special case instruction, now test for
2100 for (flag_arr_idx
= 0;; ++flag_arr_idx
)
2102 flag_idx
= arc_flag_special_opcode
->flags
[flag_arr_idx
];
2104 break; /* End of array, nothing found. */
2106 flagnm
= arc_flag_operands
[flag_idx
].name
;
2107 flaglen
= strlen (flagnm
);
2108 if (strcmp (opname
+ oplen
, flagnm
) == 0)
2110 opcode
= (const struct arc_opcode
*)
2111 hash_find (arc_opcode_hash
,
2112 arc_flag_special_opcode
->name
);
2114 if (*nflgs
+ 1 > MAX_INSN_FLGS
)
2116 memcpy (pflags
[*nflgs
].name
, flagnm
, flaglen
);
2117 pflags
[*nflgs
].name
[flaglen
] = '\0';
2126 /* Check whether a symbol involves a register. */
2129 contains_register (symbolS
*sym
)
2133 expressionS
*ex
= symbol_get_value_expression (sym
);
2134 return ((O_register
== ex
->X_op
)
2135 && !contains_register (ex
->X_add_symbol
)
2136 && !contains_register (ex
->X_op_symbol
));
2142 /* Returns the register number within a symbol. */
2145 get_register (symbolS
*sym
)
2147 if (!contains_register (sym
))
2150 expressionS
*ex
= symbol_get_value_expression (sym
);
2151 return regno (ex
->X_add_number
);
2154 /* Allocates a tok entry. */
2157 allocate_tok (expressionS
*tok
, int ntok
, int cidx
)
2159 if (ntok
> MAX_INSN_ARGS
- 2)
2160 return 0; /* No space left. */
2163 return 0; /* Incorect args. */
2165 memcpy (&tok
[ntok
+1], &tok
[ntok
], sizeof (*tok
));
2168 return 1; /* Success. */
2169 return allocate_tok (tok
, ntok
- 1, cidx
);
2172 /* Return true if a RELOC is generic. A generic reloc is PC-rel of a
2173 simple ME relocation (e.g. RELOC_ARC_32_ME, BFD_RELOC_ARC_PC32. */
2176 generic_reloc_p (extended_bfd_reloc_code_real_type reloc
)
2183 case BFD_RELOC_ARC_SDA_LDST
:
2184 case BFD_RELOC_ARC_SDA_LDST1
:
2185 case BFD_RELOC_ARC_SDA_LDST2
:
2186 case BFD_RELOC_ARC_SDA16_LD
:
2187 case BFD_RELOC_ARC_SDA16_LD1
:
2188 case BFD_RELOC_ARC_SDA16_LD2
:
2189 case BFD_RELOC_ARC_SDA16_ST2
:
2190 case BFD_RELOC_ARC_SDA32_ME
:
2198 /* Search forward through all variants of an opcode looking for a
2201 static const struct arc_opcode
*
2202 find_opcode_match (const struct arc_opcode
*first_opcode
,
2205 struct arc_flags
*first_pflag
,
2209 const struct arc_opcode
*opcode
= first_opcode
;
2211 int got_cpu_match
= 0;
2212 expressionS bktok
[MAX_INSN_ARGS
];
2216 memset (&emptyE
, 0, sizeof (emptyE
));
2217 memcpy (bktok
, tok
, MAX_INSN_ARGS
* sizeof (*tok
));
2222 const unsigned char *opidx
;
2223 const unsigned char *flgidx
;
2225 const expressionS
*t
= &emptyE
;
2227 pr_debug ("%s:%d: find_opcode_match: trying opcode 0x%08X ",
2228 frag_now
->fr_file
, frag_now
->fr_line
, opcode
->opcode
);
2230 /* Don't match opcodes that don't exist on this
2232 if (!(opcode
->cpu
& arc_target
))
2235 if (is_code_density_p (opcode
) && !(arc_features
& ARC_CD
))
2241 /* Check the operands. */
2242 for (opidx
= opcode
->operands
; *opidx
; ++opidx
)
2244 const struct arc_operand
*operand
= &arc_operands
[*opidx
];
2246 /* Only take input from real operands. */
2247 if ((operand
->flags
& ARC_OPERAND_FAKE
)
2248 && !(operand
->flags
& ARC_OPERAND_BRAKET
))
2251 /* When we expect input, make sure we have it. */
2255 /* Match operand type with expression type. */
2256 switch (operand
->flags
& ARC_OPERAND_TYPECHECK_MASK
)
2258 case ARC_OPERAND_IR
:
2259 /* Check to be a register. */
2260 if ((tok
[tokidx
].X_op
!= O_register
2261 || !is_ir_num (tok
[tokidx
].X_add_number
))
2262 && !(operand
->flags
& ARC_OPERAND_IGNORE
))
2265 /* If expect duplicate, make sure it is duplicate. */
2266 if (operand
->flags
& ARC_OPERAND_DUPLICATE
)
2268 /* Check for duplicate. */
2269 if (t
->X_op
!= O_register
2270 || !is_ir_num (t
->X_add_number
)
2271 || (regno (t
->X_add_number
) !=
2272 regno (tok
[tokidx
].X_add_number
)))
2276 /* Special handling? */
2277 if (operand
->insert
)
2279 const char *errmsg
= NULL
;
2280 (*operand
->insert
)(0,
2281 regno (tok
[tokidx
].X_add_number
),
2285 if (operand
->flags
& ARC_OPERAND_IGNORE
)
2287 /* Missing argument, create one. */
2288 if (!allocate_tok (tok
, ntok
- 1, tokidx
))
2291 tok
[tokidx
].X_op
= O_absent
;
2302 case ARC_OPERAND_BRAKET
:
2303 /* Check if bracket is also in opcode table as
2305 if (tok
[tokidx
].X_op
!= O_bracket
)
2309 case ARC_OPERAND_LIMM
:
2310 case ARC_OPERAND_SIGNED
:
2311 case ARC_OPERAND_UNSIGNED
:
2312 switch (tok
[tokidx
].X_op
)
2320 /* Got an (too) early bracket, check if it is an
2321 ignored operand. N.B. This procedure works only
2322 when bracket is the last operand! */
2323 if (!(operand
->flags
& ARC_OPERAND_IGNORE
))
2325 /* Insert the missing operand. */
2326 if (!allocate_tok (tok
, ntok
- 1, tokidx
))
2329 tok
[tokidx
].X_op
= O_absent
;
2334 /* Check the range. */
2335 if (operand
->bits
!= 32
2336 && !(operand
->flags
& ARC_OPERAND_NCHK
))
2338 offsetT min
, max
, val
;
2339 val
= tok
[tokidx
].X_add_number
;
2341 if (operand
->flags
& ARC_OPERAND_SIGNED
)
2343 max
= (1 << (operand
->bits
- 1)) - 1;
2344 min
= -(1 << (operand
->bits
- 1));
2348 max
= (1 << operand
->bits
) - 1;
2352 if (val
< min
|| val
> max
)
2355 /* Check alignmets. */
2356 if ((operand
->flags
& ARC_OPERAND_ALIGNED32
)
2360 if ((operand
->flags
& ARC_OPERAND_ALIGNED16
)
2364 else if (operand
->flags
& ARC_OPERAND_NCHK
)
2366 if (operand
->insert
)
2368 const char *errmsg
= NULL
;
2369 (*operand
->insert
)(0,
2370 tok
[tokidx
].X_add_number
,
2381 /* Check if it is register range. */
2382 if ((tok
[tokidx
].X_add_number
== 0)
2383 && contains_register (tok
[tokidx
].X_add_symbol
)
2384 && contains_register (tok
[tokidx
].X_op_symbol
))
2388 regs
= get_register (tok
[tokidx
].X_add_symbol
);
2390 regs
|= get_register (tok
[tokidx
].X_op_symbol
);
2391 if (operand
->insert
)
2393 const char *errmsg
= NULL
;
2394 (*operand
->insert
)(0,
2405 if (operand
->default_reloc
== 0)
2406 goto match_failed
; /* The operand needs relocation. */
2408 /* Relocs requiring long immediate. FIXME! make it
2409 generic and move it to a function. */
2410 switch (tok
[tokidx
].X_md
)
2419 if (!(operand
->flags
& ARC_OPERAND_LIMM
))
2422 if (!generic_reloc_p (operand
->default_reloc
))
2429 /* If expect duplicate, make sure it is duplicate. */
2430 if (operand
->flags
& ARC_OPERAND_DUPLICATE
)
2432 if (t
->X_op
== O_illegal
2433 || t
->X_op
== O_absent
2434 || t
->X_op
== O_register
2435 || (t
->X_add_number
!= tok
[tokidx
].X_add_number
))
2442 /* Everything else should have been fake. */
2450 /* Check the flags. Iterate over the valid flag classes. */
2453 for (flgidx
= opcode
->flags
; *flgidx
&& lnflg
; ++flgidx
)
2455 /* Get a valid flag class. */
2456 const struct arc_flag_class
*cl_flags
= &arc_flag_classes
[*flgidx
];
2457 const unsigned *flgopridx
;
2459 for (flgopridx
= cl_flags
->flags
; *flgopridx
; ++flgopridx
)
2461 const struct arc_flag_operand
*flg_operand
;
2462 struct arc_flags
*pflag
= first_pflag
;
2465 flg_operand
= &arc_flag_operands
[*flgopridx
];
2466 for (i
= 0; i
< nflgs
; i
++, pflag
++)
2468 /* Match against the parsed flags. */
2469 if (!strcmp (flg_operand
->name
, pflag
->name
))
2471 /*TODO: Check if it is duplicated. */
2472 pflag
->code
= *flgopridx
;
2474 break; /* goto next flag class and parsed flag. */
2479 /* Did I check all the parsed flags? */
2484 /* Possible match -- did we use all of our input? */
2494 /* Restore the original parameters. */
2495 memcpy (tok
, bktok
, MAX_INSN_ARGS
* sizeof (*tok
));
2498 while (++opcode
- arc_opcodes
< (int) arc_num_opcodes
2499 && !strcmp (opcode
->name
, first_opcode
->name
));
2502 *pcpumatch
= got_cpu_match
;
2507 /* Find the proper relocation for the given opcode. */
2509 static extended_bfd_reloc_code_real_type
2510 find_reloc (const char *name
,
2511 const char *opcodename
,
2512 const struct arc_flags
*pflags
,
2514 extended_bfd_reloc_code_real_type reloc
)
2518 bfd_boolean found_flag
;
2519 extended_bfd_reloc_code_real_type ret
= BFD_RELOC_UNUSED
;
2521 for (i
= 0; i
< arc_num_equiv_tab
; i
++)
2523 const struct arc_reloc_equiv_tab
*r
= &arc_reloc_equiv
[i
];
2525 /* Find the entry. */
2526 if (strcmp (name
, r
->name
))
2528 if (r
->mnemonic
&& (strcmp (r
->mnemonic
, opcodename
)))
2535 for (j
= 0; j
< nflg
; j
++)
2536 if (pflags
[i
].code
== r
->flagcode
)
2545 if (reloc
!= r
->oldreloc
)
2552 if (ret
== BFD_RELOC_UNUSED
)
2553 as_bad (_("Unable to find %s relocation for instruction %s"),
2558 /* Turn an opcode description and a set of arguments into
2559 an instruction and a fixup. */
2562 assemble_insn (const struct arc_opcode
*opcode
,
2563 const expressionS
*tok
,
2565 const struct arc_flags
*pflags
,
2567 struct arc_insn
*insn
)
2569 const expressionS
*reloc_exp
= NULL
;
2571 const unsigned char *argidx
;
2574 unsigned char pcrel
= 0;
2575 bfd_boolean needGOTSymbol
;
2576 bfd_boolean has_delay_slot
= FALSE
;
2577 extended_bfd_reloc_code_real_type reloc
= BFD_RELOC_UNUSED
;
2579 memset (insn
, 0, sizeof (*insn
));
2580 image
= opcode
->opcode
;
2582 pr_debug ("%s:%d: assemble_insn: %s using opcode %x\n",
2583 frag_now
->fr_file
, frag_now
->fr_line
, opcode
->name
,
2586 /* Handle operands. */
2587 for (argidx
= opcode
->operands
; *argidx
; ++argidx
)
2589 const struct arc_operand
*operand
= &arc_operands
[*argidx
];
2590 const expressionS
*t
= (const expressionS
*) 0;
2592 if ((operand
->flags
& ARC_OPERAND_FAKE
)
2593 && !(operand
->flags
& ARC_OPERAND_BRAKET
))
2596 if (operand
->flags
& ARC_OPERAND_DUPLICATE
)
2598 /* Duplicate operand, already inserted. */
2610 /* Regardless if we have a reloc or not mark the instruction
2611 limm if it is the case. */
2612 if (operand
->flags
& ARC_OPERAND_LIMM
)
2613 insn
->has_limm
= TRUE
;
2618 image
= insert_operand (image
, operand
, regno (t
->X_add_number
),
2623 image
= insert_operand (image
, operand
, t
->X_add_number
, NULL
, 0);
2625 if (operand
->flags
& ARC_OPERAND_LIMM
)
2626 insn
->limm
= t
->X_add_number
;
2630 /* Ignore brackets. */
2634 gas_assert (operand
->flags
& ARC_OPERAND_IGNORE
);
2638 /* Maybe register range. */
2639 if ((t
->X_add_number
== 0)
2640 && contains_register (t
->X_add_symbol
)
2641 && contains_register (t
->X_op_symbol
))
2645 regs
= get_register (t
->X_add_symbol
);
2647 regs
|= get_register (t
->X_op_symbol
);
2648 image
= insert_operand (image
, operand
, regs
, NULL
, 0);
2653 /* This operand needs a relocation. */
2654 needGOTSymbol
= FALSE
;
2659 needGOTSymbol
= TRUE
;
2660 reloc
= find_reloc ("plt", opcode
->name
,
2662 operand
->default_reloc
);
2667 needGOTSymbol
= TRUE
;
2668 reloc
= ARC_RELOC_TABLE (t
->X_md
)->reloc
;
2671 reloc
= ARC_RELOC_TABLE (t
->X_md
)->reloc
;
2672 if (ARC_SHORT (opcode
->mask
))
2673 as_bad_where (frag_now
->fr_file
, frag_now
->fr_line
,
2674 _("Unable to use @pcl relocation for insn %s"),
2678 reloc
= find_reloc ("sda", opcode
->name
,
2680 operand
->default_reloc
);
2684 needGOTSymbol
= TRUE
;
2689 reloc
= ARC_RELOC_TABLE (t
->X_md
)->reloc
;
2692 case O_tpoff9
: /*FIXME! Check for the conditionality of
2694 case O_dtpoff9
: /*FIXME! Check for the conditionality of
2696 as_bad (_("TLS_*_S9 relocs are not supported yet"));
2700 /* Just consider the default relocation. */
2701 reloc
= operand
->default_reloc
;
2705 if (needGOTSymbol
&& (GOT_symbol
== NULL
))
2706 GOT_symbol
= symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME
);
2713 /* sanity checks. */
2714 reloc_howto_type
*reloc_howto
2715 = bfd_reloc_type_lookup (stdoutput
,
2716 (bfd_reloc_code_real_type
) reloc
);
2717 unsigned reloc_bitsize
= reloc_howto
->bitsize
;
2718 if (reloc_howto
->rightshift
)
2719 reloc_bitsize
-= reloc_howto
->rightshift
;
2720 if (reloc_bitsize
!= operand
->bits
)
2722 as_bad (_("invalid relocation %s for field"),
2723 bfd_get_reloc_code_name (reloc
));
2728 if (insn
->nfixups
>= MAX_INSN_FIXUPS
)
2729 as_fatal (_("too many fixups"));
2731 struct arc_fixup
*fixup
;
2732 fixup
= &insn
->fixups
[insn
->nfixups
++];
2734 fixup
->reloc
= reloc
;
2735 pcrel
= (operand
->flags
& ARC_OPERAND_PCREL
) ? 1 : 0;
2736 fixup
->pcrel
= pcrel
;
2737 fixup
->islong
= (operand
->flags
& ARC_OPERAND_LIMM
) ?
2744 for (i
= 0; i
< nflg
; i
++)
2746 const struct arc_flag_operand
*flg_operand
=
2747 &arc_flag_operands
[pflags
[i
].code
];
2749 /* Check if the instruction has a delay slot. */
2750 if (!strcmp (flg_operand
->name
, "d"))
2751 has_delay_slot
= TRUE
;
2753 /* There is an exceptional case when we cannot insert a flag
2754 just as it is. The .T flag must be handled in relation with
2755 the relative address. */
2756 if (!strcmp (flg_operand
->name
, "t")
2757 || !strcmp (flg_operand
->name
, "nt"))
2759 unsigned bitYoperand
= 0;
2760 /* FIXME! move selection bbit/brcc in arc-opc.c. */
2761 if (!strcmp (flg_operand
->name
, "t"))
2762 if (!strcmp (opcode
->name
, "bbit0")
2763 || !strcmp (opcode
->name
, "bbit1"))
2764 bitYoperand
= arc_NToperand
;
2766 bitYoperand
= arc_Toperand
;
2768 if (!strcmp (opcode
->name
, "bbit0")
2769 || !strcmp (opcode
->name
, "bbit1"))
2770 bitYoperand
= arc_Toperand
;
2772 bitYoperand
= arc_NToperand
;
2774 gas_assert (reloc_exp
!= NULL
);
2775 if (reloc_exp
->X_op
== O_constant
)
2777 /* Check if we have a constant and solved it
2779 offsetT val
= reloc_exp
->X_add_number
;
2780 image
|= insert_operand (image
, &arc_operands
[bitYoperand
],
2785 struct arc_fixup
*fixup
;
2787 if (insn
->nfixups
>= MAX_INSN_FIXUPS
)
2788 as_fatal (_("too many fixups"));
2790 fixup
= &insn
->fixups
[insn
->nfixups
++];
2791 fixup
->exp
= *reloc_exp
;
2792 fixup
->reloc
= -bitYoperand
;
2793 fixup
->pcrel
= pcrel
;
2794 fixup
->islong
= FALSE
;
2798 image
|= (flg_operand
->code
& ((1 << flg_operand
->bits
) - 1))
2799 << flg_operand
->shift
;
2802 /* Short instruction? */
2803 insn
->short_insn
= ARC_SHORT (opcode
->mask
) ? TRUE
: FALSE
;
2807 /* Update last insn status. */
2808 arc_last_insns
[1] = arc_last_insns
[0];
2809 arc_last_insns
[0].opcode
= opcode
;
2810 arc_last_insns
[0].has_limm
= insn
->has_limm
;
2811 arc_last_insns
[0].has_delay_slot
= has_delay_slot
;
2813 /* Check if the current instruction is legally used. */
2814 if (arc_last_insns
[1].has_delay_slot
2815 && is_br_jmp_insn_p (arc_last_insns
[0].opcode
))
2816 as_bad_where (frag_now
->fr_file
, frag_now
->fr_line
,
2817 _("A jump/branch instruction in delay slot."));
2820 /* Actually output an instruction with its fixup. */
2823 emit_insn (struct arc_insn
*insn
)
2828 pr_debug ("Emit insn : 0x%x\n", insn
->insn
);
2829 pr_debug ("\tShort : 0x%d\n", insn
->short_insn
);
2830 pr_debug ("\tLong imm: 0x%lx\n", insn
->limm
);
2832 /* Write out the instruction. */
2833 if (insn
->short_insn
)
2838 md_number_to_chars (f
, insn
->insn
, 2);
2839 md_number_to_chars_midend (f
+ 2, insn
->limm
, 4);
2840 dwarf2_emit_insn (6);
2845 md_number_to_chars (f
, insn
->insn
, 2);
2846 dwarf2_emit_insn (2);
2854 md_number_to_chars_midend (f
, insn
->insn
, 4);
2855 md_number_to_chars_midend (f
+ 4, insn
->limm
, 4);
2856 dwarf2_emit_insn (8);
2861 md_number_to_chars_midend (f
, insn
->insn
, 4);
2862 dwarf2_emit_insn (4);
2866 /* Apply the fixups in order. */
2867 for (i
= 0; i
< insn
->nfixups
; i
++)
2869 struct arc_fixup
*fixup
= &insn
->fixups
[i
];
2870 int size
, pcrel
, offset
= 0;
2872 /*FIXME! the reloc size is wrong in the BFD file. When it will
2873 be fixed please delete me. */
2874 size
= (insn
->short_insn
&& !fixup
->islong
) ? 2 : 4;
2877 offset
= (insn
->short_insn
) ? 2 : 4;
2879 /* Some fixups are only used internally, thus no howto. */
2880 if ((int) fixup
->reloc
< 0)
2882 /*FIXME! the reloc size is wrong in the BFD file. When it
2883 will be fixed please enable me.
2884 size = (insn->short_insn && !fixup->islong) ? 2 : 4; */
2885 pcrel
= fixup
->pcrel
;
2889 reloc_howto_type
*reloc_howto
=
2890 bfd_reloc_type_lookup (stdoutput
,
2891 (bfd_reloc_code_real_type
) fixup
->reloc
);
2892 gas_assert (reloc_howto
);
2893 /*FIXME! the reloc size is wrong in the BFD file. When it
2894 will be fixed please enable me.
2895 size = bfd_get_reloc_size (reloc_howto); */
2896 pcrel
= reloc_howto
->pc_relative
;
2899 pr_debug ("%s:%d: emit_insn: new %s fixup (PCrel:%s) of size %d @ offset %d\n",
2900 frag_now
->fr_file
, frag_now
->fr_line
,
2901 (fixup
->reloc
< 0) ? "Internal" :
2902 bfd_get_reloc_code_name (fixup
->reloc
),
2905 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ offset
,
2906 size
, &fixup
->exp
, pcrel
, fixup
->reloc
);
2908 /* Check for ZOLs, and update symbol info if any. */
2909 if (LP_INSN (insn
->insn
))
2911 gas_assert (fixup
->exp
.X_add_symbol
);
2912 ARC_SET_FLAG (fixup
->exp
.X_add_symbol
, ARC_FLAG_ZOL
);
2917 /* Insert an operand value into an instruction. */
2920 insert_operand (unsigned insn
,
2921 const struct arc_operand
*operand
,
2926 offsetT min
= 0, max
= 0;
2928 if (operand
->bits
!= 32
2929 && !(operand
->flags
& ARC_OPERAND_NCHK
)
2930 && !(operand
->flags
& ARC_OPERAND_FAKE
))
2932 if (operand
->flags
& ARC_OPERAND_SIGNED
)
2934 max
= (1 << (operand
->bits
- 1)) - 1;
2935 min
= -(1 << (operand
->bits
- 1));
2939 max
= (1 << operand
->bits
) - 1;
2943 if (val
< min
|| val
> max
)
2944 as_bad_value_out_of_range (_("operand"),
2945 val
, min
, max
, file
, line
);
2948 pr_debug ("insert field: %ld <= %ld <= %ld in 0x%08x\n",
2949 min
, val
, max
, insn
);
2951 if ((operand
->flags
& ARC_OPERAND_ALIGNED32
)
2953 as_bad_where (file
, line
,
2954 _("Unaligned operand. Needs to be 32bit aligned"));
2956 if ((operand
->flags
& ARC_OPERAND_ALIGNED16
)
2958 as_bad_where (file
, line
,
2959 _("Unaligned operand. Needs to be 16bit aligned"));
2961 if (operand
->insert
)
2963 const char *errmsg
= NULL
;
2965 insn
= (*operand
->insert
) (insn
, val
, &errmsg
);
2967 as_warn_where (file
, line
, "%s", errmsg
);
2971 if (operand
->flags
& ARC_OPERAND_TRUNCATE
)
2973 if (operand
->flags
& ARC_OPERAND_ALIGNED32
)
2975 if (operand
->flags
& ARC_OPERAND_ALIGNED16
)
2978 insn
|= ((val
& ((1 << operand
->bits
) - 1)) << operand
->shift
);
2984 arc_handle_align (fragS
* fragP
)
2986 if ((fragP
)->fr_type
== rs_align_code
)
2988 char *dest
= (fragP
)->fr_literal
+ (fragP
)->fr_fix
;
2989 valueT count
= ((fragP
)->fr_next
->fr_address
2990 - (fragP
)->fr_address
- (fragP
)->fr_fix
);
2992 (fragP
)->fr_var
= 2;
2994 if (count
& 1)/* Padding in the gap till the next 2-byte
2995 boundary with 0s. */
3000 /* Writing nop_s. */
3001 md_number_to_chars (dest
, NOP_OPCODE_S
, 2);
3005 /* Here we decide which fixups can be adjusted to make them relative
3006 to the beginning of the section instead of the symbol. Basically
3007 we need to make sure that the dynamic relocations are done
3008 correctly, so in some cases we force the original symbol to be
3012 tc_arc_fix_adjustable (fixS
*fixP
)
3015 /* Prevent all adjustments to global symbols. */
3016 if (S_IS_EXTERNAL (fixP
->fx_addsy
))
3018 if (S_IS_WEAK (fixP
->fx_addsy
))
3021 /* Adjust_reloc_syms doesn't know about the GOT. */
3022 switch (fixP
->fx_r_type
)
3024 case BFD_RELOC_ARC_GOTPC32
:
3025 case BFD_RELOC_ARC_PLT32
:
3026 case BFD_RELOC_ARC_S25H_PCREL_PLT
:
3027 case BFD_RELOC_ARC_S21H_PCREL_PLT
:
3028 case BFD_RELOC_ARC_S25W_PCREL_PLT
:
3029 case BFD_RELOC_ARC_S21W_PCREL_PLT
:
3036 return 0; /* FIXME! return 1, fix it in the linker. */
3039 /* Compute the reloc type of an expression EXP. */
3042 arc_check_reloc (expressionS
*exp
,
3043 bfd_reloc_code_real_type
*r_type_p
)
3045 if (*r_type_p
== BFD_RELOC_32
3046 && exp
->X_op
== O_subtract
3047 && exp
->X_op_symbol
!= NULL
3048 && exp
->X_op_symbol
->bsym
->section
== now_seg
)
3049 *r_type_p
= BFD_RELOC_ARC_PC32
;
3053 /* Add expression EXP of SIZE bytes to offset OFF of fragment FRAG. */
3056 arc_cons_fix_new (fragS
*frag
,
3060 bfd_reloc_code_real_type r_type
)
3062 r_type
= BFD_RELOC_UNUSED
;
3067 r_type
= BFD_RELOC_8
;
3071 r_type
= BFD_RELOC_16
;
3075 r_type
= BFD_RELOC_24
;
3079 r_type
= BFD_RELOC_32
;
3080 arc_check_reloc (exp
, &r_type
);
3084 r_type
= BFD_RELOC_64
;
3088 as_bad (_("unsupported BFD relocation size %u"), size
);
3089 r_type
= BFD_RELOC_UNUSED
;
3092 fix_new_exp (frag
, off
, size
, exp
, 0, r_type
);
3095 /* The actual routine that checks the ZOL conditions. */
3098 check_zol (symbolS
*s
)
3100 switch (arc_mach_type
)
3102 case bfd_mach_arc_arcv2
:
3103 if (arc_target
& ARC_OPCODE_ARCv2EM
)
3106 if (is_br_jmp_insn_p (arc_last_insns
[0].opcode
)
3107 || arc_last_insns
[1].has_delay_slot
)
3108 as_bad (_("Jump/Branch instruction detected at the end of the ZOL label @%s"),
3112 case bfd_mach_arc_arc600
:
3114 if (is_kernel_insn_p (arc_last_insns
[0].opcode
))
3115 as_bad (_("Kernel instruction detected at the end of the ZOL label @%s"),
3118 if (arc_last_insns
[0].has_limm
3119 && is_br_jmp_insn_p (arc_last_insns
[0].opcode
))
3120 as_bad (_("A jump instruction with long immediate detected at the \
3121 end of the ZOL label @%s"), S_GET_NAME (s
));
3124 case bfd_mach_arc_arc700
:
3125 if (arc_last_insns
[0].has_delay_slot
)
3126 as_bad (_("An illegal use of delay slot detected at the end of the ZOL label @%s"),
3135 /* If ZOL end check the last two instruction for illegals. */
3137 arc_frob_label (symbolS
* sym
)
3139 if (ARC_GET_FLAG (sym
) & ARC_FLAG_ZOL
)
3142 dwarf2_emit_label (sym
);