2001-08-31 Eric Christopher <echristo@redhat.com>
[binutils.git] / gas / config / tc-arc.c
blob285cdf6a01d2f9aef09302021284e4ccd79a8de0
1 /* tc-arc.c -- Assembler for the ARC
2 Copyright 1994, 1995, 1997, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Contributed by Doug Evans (dje@cygnus.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 2, or (at your option)
11 any later version.
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 #include <stdio.h>
24 #include <ctype.h>
25 #include "libiberty.h"
26 #include "as.h"
27 #include "subsegs.h"
28 #include "opcode/arc.h"
29 #include "../opcodes/arc-ext.h"
30 #include "elf/arc.h"
31 #include "dwarf2dbg.h"
33 extern int arc_get_mach PARAMS ((char *));
34 extern int arc_operand_type PARAMS ((int));
35 extern int arc_insn_not_jl PARAMS ((arc_insn));
36 extern int arc_limm_fixup_adjust PARAMS ((arc_insn));
37 extern int arc_get_noshortcut_flag PARAMS ((void));
38 extern int arc_set_ext_seg PARAMS ((void));
39 extern void arc_code_symbol PARAMS ((expressionS *));
41 static arc_insn arc_insert_operand PARAMS ((arc_insn,
42 const struct arc_operand *, int,
43 const struct arc_operand_value *,
44 offsetT, char *, unsigned int));
45 static void arc_common PARAMS ((int));
46 static void arc_extinst PARAMS ((int));
47 static void arc_extoper PARAMS ((int));
48 static void arc_option PARAMS ((int));
49 static int get_arc_exp_reloc_type PARAMS ((int, int, expressionS *,
50 expressionS *));
52 const struct suffix_classes {
53 char *name;
54 int len;
55 } suffixclass[] = {
56 { "SUFFIX_COND|SUFFIX_FLAG",23 },
57 { "SUFFIX_FLAG", 11 },
58 { "SUFFIX_COND", 11 },
59 { "SUFFIX_NONE", 11 }
62 #define MAXSUFFIXCLASS (sizeof (suffixclass) / sizeof (struct suffix_classes))
64 const struct syntax_classes {
65 char *name;
66 int len;
67 int class;
68 } syntaxclass[] = {
69 { "SYNTAX_3OP|OP1_MUST_BE_IMM", 26, SYNTAX_3OP|OP1_MUST_BE_IMM|SYNTAX_VALID },
70 { "OP1_MUST_BE_IMM|SYNTAX_3OP", 26, OP1_MUST_BE_IMM|SYNTAX_3OP|SYNTAX_VALID },
71 { "SYNTAX_2OP|OP1_IMM_IMPLIED", 26, SYNTAX_2OP|OP1_IMM_IMPLIED|SYNTAX_VALID },
72 { "OP1_IMM_IMPLIED|SYNTAX_2OP", 26, OP1_IMM_IMPLIED|SYNTAX_2OP|SYNTAX_VALID },
73 { "SYNTAX_3OP", 10, SYNTAX_3OP|SYNTAX_VALID },
74 { "SYNTAX_2OP", 10, SYNTAX_2OP|SYNTAX_VALID }
77 #define MAXSYNTAXCLASS (sizeof (syntaxclass) / sizeof (struct syntax_classes))
79 const pseudo_typeS md_pseudo_table[] = {
80 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */
81 { "comm", arc_common, 0 },
82 { "common", arc_common, 0 },
83 { "lcomm", arc_common, 1 },
84 { "lcommon", arc_common, 1 },
85 { "2byte", cons, 2 },
86 { "half", cons, 2 },
87 { "short", cons, 2 },
88 { "3byte", cons, 3 },
89 { "4byte", cons, 4 },
90 { "word", cons, 4 },
91 { "option", arc_option, 0 },
92 { "cpu", arc_option, 0 },
93 { "block", s_space, 0 },
94 { "file", dwarf2_directive_file, 0 },
95 { "loc", dwarf2_directive_loc, 0 },
96 { "extcondcode", arc_extoper, 0 },
97 { "extcoreregister", arc_extoper, 1 },
98 { "extauxregister", arc_extoper, 2 },
99 { "extinstruction", arc_extinst, 0 },
100 { NULL, 0, 0 },
103 /* This array holds the chars that always start a comment. If the
104 pre-processor is disabled, these aren't very useful. */
105 const char comment_chars[] = "#;";
107 /* This array holds the chars that only start a comment at the beginning of
108 a line. If the line seems to have the form '# 123 filename'
109 .line and .file directives will appear in the pre-processed output */
110 /* Note that input_file.c hand checks for '#' at the beginning of the
111 first line of the input file. This is because the compiler outputs
112 #NO_APP at the beginning of its output. */
113 /* Also note that comments started like this one will always
114 work if '/' isn't otherwise defined. */
115 const char line_comment_chars[] = "#";
117 const char line_separator_chars[] = "";
119 /* Chars that can be used to separate mant from exp in floating point nums. */
120 const char EXP_CHARS[] = "eE";
122 /* Chars that mean this number is a floating point constant
123 As in 0f12.456 or 0d1.2345e12. */
124 const char FLT_CHARS[] = "rRsSfFdD";
126 /* Byte order. */
127 extern int target_big_endian;
128 const char *arc_target_format = DEFAULT_TARGET_FORMAT;
129 static int byte_order = DEFAULT_BYTE_ORDER;
131 static segT arcext_section;
133 /* One of bfd_mach_arc_n. */
134 static int arc_mach_type = bfd_mach_arc_6;
136 /* Non-zero if the cpu type has been explicitly specified. */
137 static int mach_type_specified_p = 0;
139 /* Non-zero if opcode tables have been initialized.
140 A .option command must appear before any instructions. */
141 static int cpu_tables_init_p = 0;
143 static struct hash_control *arc_suffix_hash = NULL;
145 const char *md_shortopts = "";
146 struct option md_longopts[] = {
147 #define OPTION_EB (OPTION_MD_BASE + 0)
148 { "EB", no_argument, NULL, OPTION_EB },
149 #define OPTION_EL (OPTION_MD_BASE + 1)
150 { "EL", no_argument, NULL, OPTION_EL },
151 #define OPTION_ARC5 (OPTION_MD_BASE + 2)
152 { "marc5", no_argument, NULL, OPTION_ARC5 },
153 { "pre-v6", no_argument, NULL, OPTION_ARC5 },
154 #define OPTION_ARC6 (OPTION_MD_BASE + 3)
155 { "marc6", no_argument, NULL, OPTION_ARC6 },
156 #define OPTION_ARC7 (OPTION_MD_BASE + 4)
157 { "marc7", no_argument, NULL, OPTION_ARC7 },
158 #define OPTION_ARC8 (OPTION_MD_BASE + 5)
159 { "marc8", no_argument, NULL, OPTION_ARC8 },
160 #define OPTION_ARC (OPTION_MD_BASE + 6)
161 { "marc", no_argument, NULL, OPTION_ARC },
162 { NULL, no_argument, NULL, 0 }
164 size_t md_longopts_size = sizeof (md_longopts);
166 #define IS_SYMBOL_OPERAND(o) \
167 ((o) == 'b' || (o) == 'c' || (o) == 's' || (o) == 'o' || (o) == 'O')
169 struct arc_operand_value *get_ext_suffix (char *s);
171 /* Invocation line includes a switch not recognized by the base assembler.
172 See if it's a processor-specific option. */
175 md_parse_option (c, arg)
176 int c;
177 char *arg ATTRIBUTE_UNUSED;
179 switch (c)
181 case OPTION_ARC5:
182 arc_mach_type = bfd_mach_arc_5;
183 break;
184 case OPTION_ARC:
185 case OPTION_ARC6:
186 arc_mach_type = bfd_mach_arc_6;
187 break;
188 case OPTION_ARC7:
189 arc_mach_type = bfd_mach_arc_7;
190 break;
191 case OPTION_ARC8:
192 arc_mach_type = bfd_mach_arc_8;
193 break;
194 case OPTION_EB:
195 byte_order = BIG_ENDIAN;
196 arc_target_format = "elf32-bigarc";
197 break;
198 case OPTION_EL:
199 byte_order = LITTLE_ENDIAN;
200 arc_target_format = "elf32-littlearc";
201 break;
202 default:
203 return 0;
205 return 1;
208 void
209 md_show_usage (stream)
210 FILE *stream;
212 fprintf (stream, "\
213 ARC Options:\n\
214 -marc[5|6|7|8] select processor variant (default arc%d)\n\
215 -EB assemble code for a big endian cpu\n\
216 -EL assemble code for a little endian cpu\n", arc_mach_type + 5);
219 /* This function is called once, at assembler startup time. It should
220 set up all the tables, etc. that the MD part of the assembler will need.
221 Opcode selection is deferred until later because we might see a .option
222 command. */
224 void
225 md_begin ()
227 /* The endianness can be chosen "at the factory". */
228 target_big_endian = byte_order == BIG_ENDIAN;
230 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, arc_mach_type))
231 as_warn ("could not set architecture and machine");
233 /* This call is necessary because we need to initialize `arc_operand_map'
234 which may be needed before we see the first insn. */
235 arc_opcode_init_tables (arc_get_opcode_mach (arc_mach_type,
236 target_big_endian));
239 /* Initialize the various opcode and operand tables.
240 MACH is one of bfd_mach_arc_xxx. */
241 static void
242 init_opcode_tables (mach)
243 int mach;
245 int i;
246 char *last;
248 if ((arc_suffix_hash = hash_new ()) == NULL)
249 as_fatal ("virtual memory exhausted");
251 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
252 as_warn ("could not set architecture and machine");
254 /* This initializes a few things in arc-opc.c that we need.
255 This must be called before the various arc_xxx_supported fns. */
256 arc_opcode_init_tables (arc_get_opcode_mach (mach, target_big_endian));
258 /* Only put the first entry of each equivalently named suffix in the
259 table. */
260 last = "";
261 for (i = 0; i < arc_suffixes_count; i++)
263 if (strcmp (arc_suffixes[i].name, last) != 0)
264 hash_insert (arc_suffix_hash, arc_suffixes[i].name, (PTR) (arc_suffixes + i));
265 last = arc_suffixes[i].name;
268 /* Since registers don't have a prefix, we put them in the symbol table so
269 they can't be used as symbols. This also simplifies argument parsing as
270 we can let gas parse registers for us. The recorded register number is
271 the address of the register's entry in arc_reg_names.
273 If the register name is already in the table, then the existing
274 definition is assumed to be from an .ExtCoreRegister pseudo-op. */
276 for (i = 0; i < arc_reg_names_count; i++)
278 if (symbol_find (arc_reg_names[i].name))
279 continue;
280 /* Use symbol_create here instead of symbol_new so we don't try to
281 output registers into the object file's symbol table. */
282 symbol_table_insert (symbol_create (arc_reg_names[i].name,
283 reg_section,
284 (int) &arc_reg_names[i],
285 &zero_address_frag));
288 /* Tell `.option' it's too late. */
289 cpu_tables_init_p = 1;
292 /* Insert an operand value into an instruction.
293 If REG is non-NULL, it is a register number and ignore VAL. */
295 static arc_insn
296 arc_insert_operand (insn, operand, mods, reg, val, file, line)
297 arc_insn insn;
298 const struct arc_operand *operand;
299 int mods;
300 const struct arc_operand_value *reg;
301 offsetT val;
302 char *file;
303 unsigned int line;
305 if (operand->bits != 32)
307 long min, max;
308 offsetT test;
310 if ((operand->flags & ARC_OPERAND_SIGNED) != 0)
312 if ((operand->flags & ARC_OPERAND_SIGNOPT) != 0)
313 max = (1 << operand->bits) - 1;
314 else
315 max = (1 << (operand->bits - 1)) - 1;
316 min = - (1 << (operand->bits - 1));
318 else
320 max = (1 << operand->bits) - 1;
321 min = 0;
324 if ((operand->flags & ARC_OPERAND_NEGATIVE) != 0)
325 test = - val;
326 else
327 test = val;
329 if (test < (offsetT) min || test > (offsetT) max)
331 const char *err =
332 "operand out of range (%s not between %ld and %ld)";
333 char buf[100];
335 sprint_value (buf, test);
336 if (file == (char *) NULL)
337 as_warn (err, buf, min, max);
338 else
339 as_warn_where (file, line, err, buf, min, max);
343 if (operand->insert)
345 const char *errmsg;
347 errmsg = NULL;
348 insn = (*operand->insert) (insn, operand, mods, reg, (long) val, &errmsg);
349 if (errmsg != (const char *) NULL)
350 as_warn (errmsg);
352 else
353 insn |= (((long) val & ((1 << operand->bits) - 1))
354 << operand->shift);
356 return insn;
359 /* We need to keep a list of fixups. We can't simply generate them as
360 we go, because that would require us to first create the frag, and
361 that would screw up references to ``.''. */
363 struct arc_fixup {
364 /* index into `arc_operands' */
365 int opindex;
366 expressionS exp;
369 #define MAX_FIXUPS 5
371 #define MAX_SUFFIXES 5
373 /* This routine is called for each instruction to be assembled. */
375 void
376 md_assemble (str)
377 char *str;
379 const struct arc_opcode *opcode;
380 const struct arc_opcode *std_opcode;
381 struct arc_opcode *ext_opcode;
382 char *start;
383 const char *last_errmsg = 0;
384 arc_insn insn;
385 static int init_tables_p = 0;
387 /* Opcode table initialization is deferred until here because we have to
388 wait for a possible .option command. */
389 if (!init_tables_p)
391 init_opcode_tables (arc_mach_type);
392 init_tables_p = 1;
395 /* Skip leading white space. */
396 while (isspace (*str))
397 str++;
399 /* The instructions are stored in lists hashed by the first letter (though
400 we needn't care how they're hashed). Get the first in the list. */
402 ext_opcode = arc_ext_opcodes;
403 std_opcode = arc_opcode_lookup_asm (str);
405 /* Keep looking until we find a match. */
407 start = str;
408 for (opcode = (ext_opcode ? ext_opcode : std_opcode);
409 opcode != NULL;
410 opcode = (ARC_OPCODE_NEXT_ASM (opcode)
411 ? ARC_OPCODE_NEXT_ASM (opcode)
412 : (ext_opcode ? ext_opcode = NULL, std_opcode : NULL)))
414 int past_opcode_p, fc, num_suffixes;
415 int fix_up_at = 0;
416 char *syn;
417 struct arc_fixup fixups[MAX_FIXUPS];
418 /* Used as a sanity check. If we need a limm reloc, make sure we ask
419 for an extra 4 bytes from frag_more. */
420 int limm_reloc_p;
421 int ext_suffix_p;
422 const struct arc_operand_value *insn_suffixes[MAX_SUFFIXES];
424 /* Is this opcode supported by the selected cpu? */
425 if (! arc_opcode_supported (opcode))
426 continue;
428 /* Scan the syntax string. If it doesn't match, try the next one. */
430 arc_opcode_init_insert ();
431 insn = opcode->value;
432 fc = 0;
433 past_opcode_p = 0;
434 num_suffixes = 0;
435 limm_reloc_p = 0;
436 ext_suffix_p = 0;
438 /* We don't check for (*str != '\0') here because we want to parse
439 any trailing fake arguments in the syntax string. */
440 for (str = start, syn = opcode->syntax; *syn != '\0';)
442 int mods;
443 const struct arc_operand *operand;
445 /* Non operand chars must match exactly. */
446 if (*syn != '%' || *++syn == '%')
448 /* Handle '+' specially as we want to allow "ld r0,[sp-4]". */
449 /* ??? The syntax has changed to [sp,-4]. */
450 if (0 && *syn == '+' && *str == '-')
452 /* Skip over syn's +, but leave str's - alone.
453 That makes the case identical to "ld r0,[sp+-4]". */
454 ++syn;
456 else if (*str == *syn)
458 if (*syn == ' ')
459 past_opcode_p = 1;
460 ++syn;
461 ++str;
463 else
464 break;
465 continue;
468 /* We have an operand. Pick out any modifiers. */
469 mods = 0;
470 while (ARC_MOD_P (arc_operands[arc_operand_map[(int) *syn]].flags))
472 mods |= arc_operands[arc_operand_map[(int) *syn]].flags & ARC_MOD_BITS;
473 ++syn;
475 operand = arc_operands + arc_operand_map[(int) *syn];
476 if (operand->fmt == 0)
477 as_fatal ("unknown syntax format character `%c'", *syn);
479 if (operand->flags & ARC_OPERAND_FAKE)
481 const char *errmsg = NULL;
482 if (operand->insert)
484 insn = (*operand->insert) (insn, operand, mods, NULL, 0, &errmsg);
485 if (errmsg != (const char *) NULL)
487 last_errmsg = errmsg;
488 if (operand->flags & ARC_OPERAND_ERROR)
490 as_bad (errmsg);
491 return;
493 else if (operand->flags & ARC_OPERAND_WARN)
494 as_warn (errmsg);
495 break;
497 if (limm_reloc_p
498 && (operand->flags && operand->flags & ARC_OPERAND_LIMM)
499 && (operand->flags &
500 (ARC_OPERAND_ABSOLUTE_BRANCH | ARC_OPERAND_ADDRESS)))
502 fixups[fix_up_at].opindex = arc_operand_map[operand->fmt];
505 ++syn;
507 /* Are we finished with suffixes? */
508 else if (!past_opcode_p)
510 int found;
511 char c;
512 char *s, *t;
513 const struct arc_operand_value *suf, *suffix_end;
514 const struct arc_operand_value *suffix = NULL;
516 if (!(operand->flags & ARC_OPERAND_SUFFIX))
517 abort ();
519 /* If we're at a space in the input string, we want to skip the
520 remaining suffixes. There may be some fake ones though, so
521 just go on to try the next one. */
522 if (*str == ' ')
524 ++syn;
525 continue;
528 s = str;
529 if (mods & ARC_MOD_DOT)
531 if (*s != '.')
532 break;
533 ++s;
535 else
537 /* This can happen in "b.nd foo" and we're currently looking
538 for "%q" (ie: a condition code suffix). */
539 if (*s == '.')
541 ++syn;
542 continue;
546 /* Pick the suffix out and look it up via the hash table. */
547 for (t = s; *t && isalnum (*t); ++t)
548 continue;
549 c = *t;
550 *t = '\0';
551 if ((suf = get_ext_suffix (s)))
552 ext_suffix_p = 1;
553 else
554 suf = hash_find (arc_suffix_hash, s);
555 if (!suf)
557 /* This can happen in "blle foo" and we're currently using
558 the template "b%q%.n %j". The "bl" insn occurs later in
559 the table so "lle" isn't an illegal suffix. */
560 *t = c;
561 break;
564 /* Is it the right type? Note that the same character is used
565 several times, so we have to examine all of them. This is
566 relatively efficient as equivalent entries are kept
567 together. If it's not the right type, don't increment `str'
568 so we try the next one in the series. */
569 found = 0;
570 if (ext_suffix_p && arc_operands[suf->type].fmt == *syn)
572 /* Insert the suffix's value into the insn. */
573 *t = c;
574 if (operand->insert)
575 insn = (*operand->insert) (insn, operand,
576 mods, NULL, suf->value,
577 NULL);
578 else
579 insn |= suf->value << operand->shift;
581 str = t;
582 found = 1;
584 else
586 *t = c;
587 suffix_end = arc_suffixes + arc_suffixes_count;
588 for (suffix = suf;
589 suffix < suffix_end && strcmp (suffix->name, suf->name) == 0;
590 ++suffix)
592 if (arc_operands[suffix->type].fmt == *syn)
594 /* Insert the suffix's value into the insn. */
595 if (operand->insert)
596 insn = (*operand->insert) (insn, operand,
597 mods, NULL, suffix->value,
598 NULL);
599 else
600 insn |= suffix->value << operand->shift;
602 str = t;
603 found = 1;
604 break;
608 ++syn;
609 if (!found)
610 /* Wrong type. Just go on to try next insn entry. */
612 else
614 if (num_suffixes == MAX_SUFFIXES)
615 as_bad ("too many suffixes");
616 else
617 insn_suffixes[num_suffixes++] = suffix;
620 else
621 /* This is either a register or an expression of some kind. */
623 char *hold;
624 const struct arc_operand_value *reg = NULL;
625 long value = 0;
626 expressionS exp;
628 if (operand->flags & ARC_OPERAND_SUFFIX)
629 abort ();
631 /* Is there anything left to parse?
632 We don't check for this at the top because we want to parse
633 any trailing fake arguments in the syntax string. */
634 if (is_end_of_line[(unsigned char) *str])
635 break;
637 /* Parse the operand. */
638 hold = input_line_pointer;
639 input_line_pointer = str;
640 expression (&exp);
641 str = input_line_pointer;
642 input_line_pointer = hold;
644 if (exp.X_op == O_illegal)
645 as_bad ("illegal operand");
646 else if (exp.X_op == O_absent)
647 as_bad ("missing operand");
648 else if (exp.X_op == O_constant)
650 value = exp.X_add_number;
652 else if (exp.X_op == O_register)
654 reg = (struct arc_operand_value *) exp.X_add_number;
656 #define IS_REG_DEST_OPERAND(o) ((o) == 'a')
657 else if (IS_REG_DEST_OPERAND (*syn))
658 as_bad ("symbol as destination register");
659 else
661 if (!strncmp (str, "@h30", 4))
663 arc_code_symbol (&exp);
664 str += 4;
666 /* We need to generate a fixup for this expression. */
667 if (fc >= MAX_FIXUPS)
668 as_fatal ("too many fixups");
669 fixups[fc].exp = exp;
670 /* We don't support shimm relocs. break here to force
671 the assembler to output a limm. */
672 #define IS_REG_SHIMM_OFFSET(o) ((o) == 'd')
673 if (IS_REG_SHIMM_OFFSET (*syn))
674 break;
675 /* If this is a register constant (IE: one whose
676 register value gets stored as 61-63) then this
677 must be a limm. */
678 /* ??? This bit could use some cleaning up.
679 Referencing the format chars like this goes
680 against style. */
681 if (IS_SYMBOL_OPERAND (*syn))
683 const char *junk;
684 limm_reloc_p = 1;
685 /* Save this, we don't yet know what reloc to use. */
686 fix_up_at = fc;
687 /* Tell insert_reg we need a limm. This is
688 needed because the value at this point is
689 zero, a shimm. */
690 /* ??? We need a cleaner interface than this. */
691 (*arc_operands[arc_operand_map['Q']].insert)
692 (insn, operand, mods, reg, 0L, &junk);
694 else
695 fixups[fc].opindex = arc_operand_map[(int) *syn];
696 ++fc;
697 value = 0;
700 /* Insert the register or expression into the instruction. */
701 if (operand->insert)
703 const char *errmsg = NULL;
704 insn = (*operand->insert) (insn, operand, mods,
705 reg, (long) value, &errmsg);
706 if (errmsg != (const char *) NULL)
708 last_errmsg = errmsg;
709 if (operand->flags & ARC_OPERAND_ERROR)
711 as_bad (errmsg);
712 return;
714 else if (operand->flags & ARC_OPERAND_WARN)
715 as_warn (errmsg);
716 break;
719 else
720 insn |= (value & ((1 << operand->bits) - 1)) << operand->shift;
722 ++syn;
726 /* If we're at the end of the syntax string, we're done. */
727 /* FIXME: try to move this to a separate function. */
728 if (*syn == '\0')
730 int i;
731 char *f;
732 long limm, limm_p;
734 /* For the moment we assume a valid `str' can only contain blanks
735 now. IE: We needn't try again with a longer version of the
736 insn and it is assumed that longer versions of insns appear
737 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
739 while (isspace (*str))
740 ++str;
742 if (!is_end_of_line[(unsigned char) *str])
743 as_bad ("junk at end of line: `%s'", str);
745 /* Is there a limm value? */
746 limm_p = arc_opcode_limm_p (&limm);
748 /* Perform various error and warning tests. */
751 static int in_delay_slot_p = 0;
752 static int prev_insn_needs_cc_nop_p = 0;
753 /* delay slot type seen */
754 int delay_slot_type = ARC_DELAY_NONE;
755 /* conditional execution flag seen */
756 int conditional = 0;
757 /* 1 if condition codes are being set */
758 int cc_set_p = 0;
759 /* 1 if conditional branch, including `b' "branch always" */
760 int cond_branch_p = opcode->flags & ARC_OPCODE_COND_BRANCH;
762 for (i = 0; i < num_suffixes; ++i)
764 switch (arc_operands[insn_suffixes[i]->type].fmt)
766 case 'n':
767 delay_slot_type = insn_suffixes[i]->value;
768 break;
769 case 'q':
770 conditional = insn_suffixes[i]->value;
771 break;
772 case 'f':
773 cc_set_p = 1;
774 break;
778 /* Putting an insn with a limm value in a delay slot is supposed to
779 be legal, but let's warn the user anyway. Ditto for 8 byte
780 jumps with delay slots. */
781 if (in_delay_slot_p && limm_p)
782 as_warn ("8 byte instruction in delay slot");
783 if (delay_slot_type != ARC_DELAY_NONE
784 && limm_p && arc_insn_not_jl (insn)) /* except for jl addr */
785 as_warn ("8 byte jump instruction with delay slot");
786 in_delay_slot_p = (delay_slot_type != ARC_DELAY_NONE) && !limm_p;
788 /* Warn when a conditional branch immediately follows a set of
789 the condition codes. Note that this needn't be done if the
790 insn that sets the condition codes uses a limm. */
791 if (cond_branch_p && conditional != 0 /* 0 = "always" */
792 && prev_insn_needs_cc_nop_p && arc_mach_type == bfd_mach_arc_5)
793 as_warn ("conditional branch follows set of flags");
794 prev_insn_needs_cc_nop_p =
795 /* FIXME: ??? not required:
796 (delay_slot_type != ARC_DELAY_NONE) && */
797 cc_set_p && !limm_p;
800 /* Write out the instruction.
801 It is important to fetch enough space in one call to `frag_more'.
802 We use (f - frag_now->fr_literal) to compute where we are and we
803 don't want frag_now to change between calls. */
804 if (limm_p)
806 f = frag_more (8);
807 md_number_to_chars (f, insn, 4);
808 md_number_to_chars (f + 4, limm, 4);
809 dwarf2_emit_insn (8);
811 else if (limm_reloc_p)
813 /* We need a limm reloc, but the tables think we don't. */
814 abort ();
816 else
818 f = frag_more (4);
819 md_number_to_chars (f, insn, 4);
820 dwarf2_emit_insn (4);
823 /* Create any fixups. */
824 for (i = 0; i < fc; ++i)
826 int op_type, reloc_type;
827 expressionS exptmp;
828 const struct arc_operand *operand;
830 /* Create a fixup for this operand.
831 At this point we do not use a bfd_reloc_code_real_type for
832 operands residing in the insn, but instead just use the
833 operand index. This lets us easily handle fixups for any
834 operand type, although that is admittedly not a very exciting
835 feature. We pick a BFD reloc type in md_apply_fix.
837 Limm values (4 byte immediate "constants") must be treated
838 normally because they're not part of the actual insn word
839 and thus the insertion routines don't handle them. */
841 if (arc_operands[fixups[i].opindex].flags & ARC_OPERAND_LIMM)
843 /* Modify the fixup addend as required by the cpu. */
844 fixups[i].exp.X_add_number += arc_limm_fixup_adjust (insn);
845 op_type = fixups[i].opindex;
846 /* FIXME: can we add this data to the operand table? */
847 if (op_type == arc_operand_map['L']
848 || op_type == arc_operand_map['s']
849 || op_type == arc_operand_map['o']
850 || op_type == arc_operand_map['O'])
851 reloc_type = BFD_RELOC_32;
852 else if (op_type == arc_operand_map['J'])
853 reloc_type = BFD_RELOC_ARC_B26;
854 else
855 abort ();
856 reloc_type = get_arc_exp_reloc_type (1, reloc_type,
857 &fixups[i].exp,
858 &exptmp);
860 else
862 op_type = get_arc_exp_reloc_type (0, fixups[i].opindex,
863 &fixups[i].exp, &exptmp);
864 reloc_type = op_type + (int) BFD_RELOC_UNUSED;
866 operand = &arc_operands[op_type];
867 fix_new_exp (frag_now,
868 ((f - frag_now->fr_literal)
869 + (operand->flags & ARC_OPERAND_LIMM ? 4 : 0)), 4,
870 &exptmp,
871 (operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0,
872 (bfd_reloc_code_real_type) reloc_type);
875 /* All done. */
876 return;
879 /* Try the next entry. */
882 if (NULL == last_errmsg)
883 as_bad ("bad instruction `%s'", start);
884 else
885 as_bad (last_errmsg);
888 static void
889 arc_extoper (opertype)
890 int opertype;
892 char *name;
893 char *mode;
894 char c;
895 char *p;
896 int imode = 0;
897 int number;
898 struct arc_ext_operand_value *ext_oper;
899 symbolS *symbolP;
901 segT old_sec;
902 int old_subsec;
904 name = input_line_pointer;
905 c = get_symbol_end ();
906 name = xstrdup (name);
907 if (NULL == name)
909 ignore_rest_of_line ();
910 return;
913 p = name;
914 while (*p)
916 if (isupper (*p))
917 *p = tolower (*p);
918 p++;
921 /* just after name is now '\0' */
922 p = input_line_pointer;
923 *p = c;
924 SKIP_WHITESPACE ();
926 if (*input_line_pointer != ',')
928 as_bad ("expected comma after operand name");
929 ignore_rest_of_line ();
930 free (name);
931 return;
934 input_line_pointer++; /* skip ',' */
935 number = get_absolute_expression ();
937 if (number < 0)
939 as_bad ("negative operand number %d", number);
940 ignore_rest_of_line ();
941 free (name);
942 return;
945 if (opertype)
947 SKIP_WHITESPACE ();
949 if (*input_line_pointer != ',')
951 as_bad ("expected comma after register-number");
952 ignore_rest_of_line ();
953 free (name);
954 return;
957 input_line_pointer++; /* skip ',' */
958 mode = input_line_pointer;
960 if (!strncmp (mode, "r|w", 3))
962 imode = 0;
963 input_line_pointer += 3;
965 else
967 if (!strncmp (mode, "r", 1))
969 imode = ARC_REGISTER_READONLY;
970 input_line_pointer += 1;
972 else
974 if (strncmp (mode, "w", 1))
976 as_bad ("invalid mode");
977 ignore_rest_of_line ();
978 free (name);
979 return;
981 else
983 imode = ARC_REGISTER_WRITEONLY;
984 input_line_pointer += 1;
988 SKIP_WHITESPACE ();
989 if (1 == opertype)
991 if (*input_line_pointer != ',')
993 as_bad ("expected comma after register-mode");
994 ignore_rest_of_line ();
995 free (name);
996 return;
999 input_line_pointer++; /* skip ',' */
1001 if (!strncmp (input_line_pointer, "cannot_shortcut", 15))
1003 imode |= arc_get_noshortcut_flag ();
1004 input_line_pointer += 15;
1006 else
1008 if (strncmp (input_line_pointer, "can_shortcut", 12))
1010 as_bad ("shortcut designator invalid");
1011 ignore_rest_of_line ();
1012 free (name);
1013 return;
1015 else
1017 input_line_pointer += 12;
1023 if ((opertype == 1) && number > 60)
1025 as_bad ("core register value (%d) too large", number);
1026 ignore_rest_of_line ();
1027 free (name);
1028 return;
1031 if ((opertype == 0) && number > 31)
1033 as_bad ("condition code value (%d) too large", number);
1034 ignore_rest_of_line ();
1035 free (name);
1036 return;
1039 ext_oper = (struct arc_ext_operand_value *) \
1040 xmalloc (sizeof (struct arc_ext_operand_value));
1042 if (opertype)
1044 /* If the symbol already exists, point it at the new definition. */
1045 if ((symbolP = symbol_find (name)))
1047 if (S_GET_SEGMENT (symbolP) == reg_section)
1048 S_SET_VALUE (symbolP, (int) &ext_oper->operand);
1049 else
1051 as_bad ("attempt to override symbol: %s", name);
1052 ignore_rest_of_line ();
1053 free (name);
1054 free (ext_oper);
1055 return;
1058 else
1060 /* If its not there, add it. */
1061 symbol_table_insert (symbol_create (name, reg_section,
1062 (int) &ext_oper->operand, &zero_address_frag));
1066 ext_oper->operand.name = name;
1067 ext_oper->operand.value = number;
1068 ext_oper->operand.type = arc_operand_type (opertype);
1069 ext_oper->operand.flags = imode;
1071 ext_oper->next = arc_ext_operands;
1072 arc_ext_operands = ext_oper;
1074 /* OK, now that we know what this operand is, put a description in
1075 the arc extension section of the output file. */
1077 old_sec = now_seg;
1078 old_subsec = now_subseg;
1080 arc_set_ext_seg ();
1082 switch (opertype)
1084 case 0:
1085 p = frag_more (1);
1086 *p = 3 + strlen (name) + 1;
1087 p = frag_more (1);
1088 *p = EXT_COND_CODE;
1089 p = frag_more (1);
1090 *p = number;
1091 p = frag_more (strlen (name) + 1);
1092 strcpy (p, name);
1093 break;
1094 case 1:
1095 p = frag_more (1);
1096 *p = 3 + strlen (name) + 1;
1097 p = frag_more (1);
1098 *p = EXT_CORE_REGISTER;
1099 p = frag_more (1);
1100 *p = number;
1101 p = frag_more (strlen (name) + 1);
1102 strcpy (p, name);
1103 break;
1104 case 2:
1105 p = frag_more (1);
1106 *p = 6 + strlen (name) + 1;
1107 p = frag_more (1);
1108 *p = EXT_AUX_REGISTER;
1109 p = frag_more (1);
1110 *p = number >> 24 & 0xff;
1111 p = frag_more (1);
1112 *p = number >> 16 & 0xff;
1113 p = frag_more (1);
1114 *p = number >> 8 & 0xff;
1115 p = frag_more (1);
1116 *p = number & 0xff;
1117 p = frag_more (strlen (name) + 1);
1118 strcpy (p, name);
1119 break;
1120 default:
1121 as_bad ("invalid opertype");
1122 ignore_rest_of_line ();
1123 free (name);
1124 return;
1125 break;
1128 subseg_set (old_sec, old_subsec);
1130 /* Enter all registers into the symbol table. */
1132 demand_empty_rest_of_line ();
1135 static void
1136 arc_extinst (ignore)
1137 int ignore ATTRIBUTE_UNUSED;
1139 unsigned char syntax[129];
1140 char *name;
1141 char *p;
1142 char c;
1143 int suffixcode = -1;
1144 int opcode, subopcode;
1145 int i;
1146 int class = 0;
1147 int name_len;
1148 struct arc_opcode *ext_op;
1150 segT old_sec;
1151 int old_subsec;
1153 name = input_line_pointer;
1154 c = get_symbol_end ();
1155 name = xstrdup (name);
1156 if (NULL == name)
1158 ignore_rest_of_line ();
1159 return;
1161 strcpy (syntax, name);
1162 name_len = strlen (name);
1164 /* just after name is now '\0' */
1165 p = input_line_pointer;
1166 *p = c;
1168 SKIP_WHITESPACE ();
1170 if (*input_line_pointer != ',')
1172 as_bad ("expected comma after operand name");
1173 ignore_rest_of_line ();
1174 return;
1177 input_line_pointer++; /* skip ',' */
1178 opcode = get_absolute_expression ();
1180 SKIP_WHITESPACE ();
1182 if (*input_line_pointer != ',')
1184 as_bad ("expected comma after opcode");
1185 ignore_rest_of_line ();
1186 return;
1189 input_line_pointer++; /* skip ',' */
1190 subopcode = get_absolute_expression ();
1192 if (subopcode < 0)
1194 as_bad ("negative subopcode %d", subopcode);
1195 ignore_rest_of_line ();
1196 return;
1199 if (subopcode)
1201 if (3 != opcode)
1203 as_bad ("subcode value found when opcode not equal 0x03");
1204 ignore_rest_of_line ();
1205 return;
1207 else
1209 if (subopcode < 0x09 || subopcode == 0x3f)
1211 as_bad ("invalid subopcode %d", subopcode);
1212 ignore_rest_of_line ();
1213 return;
1218 SKIP_WHITESPACE ();
1220 if (*input_line_pointer != ',')
1222 as_bad ("expected comma after subopcode");
1223 ignore_rest_of_line ();
1224 return;
1227 input_line_pointer++; /* skip ',' */
1229 for (i = 0; i < (int) MAXSUFFIXCLASS; i++)
1231 if (!strncmp (suffixclass[i].name,input_line_pointer, suffixclass[i].len))
1233 suffixcode = i;
1234 input_line_pointer += suffixclass[i].len;
1235 break;
1239 if (-1 == suffixcode)
1241 as_bad ("invalid suffix class");
1242 ignore_rest_of_line ();
1243 return;
1246 SKIP_WHITESPACE ();
1248 if (*input_line_pointer != ',')
1250 as_bad ("expected comma after suffix class");
1251 ignore_rest_of_line ();
1252 return;
1255 input_line_pointer++; /* skip ',' */
1257 for (i = 0; i < (int) MAXSYNTAXCLASS; i++)
1259 if (!strncmp (syntaxclass[i].name,input_line_pointer, syntaxclass[i].len))
1261 class = syntaxclass[i].class;
1262 input_line_pointer += syntaxclass[i].len;
1263 break;
1267 if (0 == (SYNTAX_VALID & class))
1269 as_bad ("invalid syntax class");
1270 ignore_rest_of_line ();
1271 return;
1274 if ((0x3 == opcode) & (class & SYNTAX_3OP))
1276 as_bad ("opcode 0x3 and SYNTAX_3OP invalid");
1277 ignore_rest_of_line ();
1278 return;
1281 switch (suffixcode)
1283 case 0:
1284 strcat (syntax, "%.q%.f ");
1285 break;
1286 case 1:
1287 strcat (syntax, "%.f ");
1288 break;
1289 case 2:
1290 strcat (syntax, "%.q ");
1291 break;
1292 case 3:
1293 strcat (syntax, " ");
1294 break;
1295 default:
1296 as_bad ("unknown suffix class");
1297 ignore_rest_of_line ();
1298 return;
1299 break;
1302 strcat (syntax, ((opcode == 0x3) ? "%a,%b" : ((class & SYNTAX_3OP) ? "%a,%b,%c" : "%b,%c")));
1303 if (suffixcode < 2)
1304 strcat (syntax, "%F");
1305 strcat (syntax, "%S%L");
1307 ext_op = (struct arc_opcode *) xmalloc (sizeof (struct arc_opcode));
1308 if (NULL == ext_op)
1310 ignore_rest_of_line ();
1311 return;
1314 ext_op->syntax = xstrdup (syntax);
1315 if (NULL == ext_op->syntax)
1317 ignore_rest_of_line ();
1318 return;
1321 ext_op->mask = I (-1) | ((0x3 == opcode) ? C (-1) : 0);
1322 ext_op->value = I (opcode) | ((0x3 == opcode) ? C (subopcode) : 0);
1323 ext_op->flags = class;
1324 ext_op->next_asm = arc_ext_opcodes;
1325 ext_op->next_dis = arc_ext_opcodes;
1326 arc_ext_opcodes = ext_op;
1328 /* OK, now that we know what this inst is, put a description in the
1329 arc extension section of the output file. */
1331 old_sec = now_seg;
1332 old_subsec = now_subseg;
1334 arc_set_ext_seg ();
1336 p = frag_more (1);
1337 *p = 5 + name_len + 1;
1338 p = frag_more (1);
1339 *p = EXT_INSTRUCTION;
1340 p = frag_more (1);
1341 *p = opcode;
1342 p = frag_more (1);
1343 *p = subopcode;
1344 p = frag_more (1);
1345 *p = (class & (OP1_MUST_BE_IMM | OP1_IMM_IMPLIED) ? IGNORE_FIRST_OPD : 0);
1346 p = frag_more (name_len);
1347 strncpy (p, syntax, name_len);
1348 p = frag_more (1);
1349 *p = '\0';
1351 subseg_set (old_sec, old_subsec);
1353 demand_empty_rest_of_line ();
1357 arc_set_ext_seg ()
1359 if (!arcext_section)
1361 arcext_section = subseg_new (".arcextmap", 0);
1362 bfd_set_section_flags (stdoutput, arcext_section,
1363 SEC_READONLY | SEC_HAS_CONTENTS);
1365 else
1366 subseg_set (arcext_section, 0);
1367 return 1;
1370 static void
1371 arc_common (localScope)
1372 int localScope;
1374 char *name;
1375 char c;
1376 char *p;
1377 int align, size;
1378 symbolS *symbolP;
1380 name = input_line_pointer;
1381 c = get_symbol_end ();
1382 /* just after name is now '\0' */
1383 p = input_line_pointer;
1384 *p = c;
1385 SKIP_WHITESPACE ();
1387 if (*input_line_pointer != ',')
1389 as_bad ("expected comma after symbol name");
1390 ignore_rest_of_line ();
1391 return;
1394 input_line_pointer++; /* skip ',' */
1395 size = get_absolute_expression ();
1397 if (size < 0)
1399 as_bad ("negative symbol length");
1400 ignore_rest_of_line ();
1401 return;
1404 *p = 0;
1405 symbolP = symbol_find_or_make (name);
1406 *p = c;
1408 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
1410 as_bad ("ignoring attempt to re-define symbol");
1411 ignore_rest_of_line ();
1412 return;
1414 if (((int) S_GET_VALUE (symbolP) != 0) \
1415 && ((int) S_GET_VALUE (symbolP) != size))
1417 as_warn ("length of symbol \"%s\" already %ld, ignoring %d",
1418 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
1420 assert (symbolP->sy_frag == &zero_address_frag);
1422 /* Now parse the alignment field. This field is optional for
1423 local and global symbols. Default alignment is zero. */
1424 if (*input_line_pointer == ',')
1426 input_line_pointer++;
1427 align = get_absolute_expression ();
1428 if (align < 0)
1430 align = 0;
1431 as_warn ("assuming symbol alignment of zero");
1434 else
1435 align = 0;
1437 if (localScope != 0)
1439 segT old_sec;
1440 int old_subsec;
1441 char *pfrag;
1443 old_sec = now_seg;
1444 old_subsec = now_subseg;
1445 record_alignment (bss_section, align);
1446 subseg_set (bss_section, 0); /* ??? subseg_set (bss_section, 1); ??? */
1448 if (align)
1449 /* Do alignment. */
1450 frag_align (align, 0, 0);
1452 /* Detach from old frag. */
1453 if (S_GET_SEGMENT (symbolP) == bss_section)
1454 symbolP->sy_frag->fr_symbol = NULL;
1456 symbolP->sy_frag = frag_now;
1457 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
1458 (offsetT) size, (char *) 0);
1459 *pfrag = 0;
1461 S_SET_SIZE (symbolP, size);
1462 S_SET_SEGMENT (symbolP, bss_section);
1463 S_CLEAR_EXTERNAL (symbolP);
1464 symbolP->local = 1;
1465 subseg_set (old_sec, old_subsec);
1467 else
1469 S_SET_VALUE (symbolP, (valueT) size);
1470 S_SET_ALIGN (symbolP, align);
1471 S_SET_EXTERNAL (symbolP);
1472 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
1475 symbolP->bsym->flags |= BSF_OBJECT;
1477 demand_empty_rest_of_line ();
1478 return;
1481 /* Select the cpu we're assembling for. */
1483 static void
1484 arc_option (ignore)
1485 int ignore ATTRIBUTE_UNUSED;
1487 int mach;
1488 char c;
1489 char *cpu;
1491 cpu = input_line_pointer;
1492 c = get_symbol_end ();
1493 mach = arc_get_mach (cpu);
1494 *input_line_pointer = c;
1496 /* If an instruction has already been seen, it's too late. */
1497 if (cpu_tables_init_p)
1499 as_bad ("\".option\" directive must appear before any instructions");
1500 ignore_rest_of_line ();
1501 return;
1504 if (mach == -1)
1505 goto bad_cpu;
1507 if (mach_type_specified_p && mach != arc_mach_type)
1509 as_bad ("\".option\" directive conflicts with initial definition");
1510 ignore_rest_of_line ();
1511 return;
1513 else
1515 /* The cpu may have been selected on the command line. */
1516 if (mach != arc_mach_type)
1517 as_warn ("\".option\" directive overrides command-line (default) value");
1518 arc_mach_type = mach;
1519 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
1520 as_fatal ("could not set architecture and machine");
1521 mach_type_specified_p = 1;
1523 demand_empty_rest_of_line ();
1524 return;
1526 bad_cpu:
1527 as_bad ("invalid identifier for \".option\"");
1528 ignore_rest_of_line ();
1531 /* Turn a string in input_line_pointer into a floating point constant
1532 of type TYPE, and store the appropriate bytes in *LITP. The number
1533 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1534 returned, or NULL on OK. */
1536 /* Equal to MAX_PRECISION in atof-ieee.c */
1537 #define MAX_LITTLENUMS 6
1539 char *
1540 md_atof (type, litP, sizeP)
1541 char type;
1542 char *litP;
1543 int *sizeP;
1545 int prec;
1546 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1547 LITTLENUM_TYPE *wordP;
1548 char *t;
1549 char *atof_ieee ();
1551 switch (type)
1553 case 'f':
1554 case 'F':
1555 prec = 2;
1556 break;
1558 case 'd':
1559 case 'D':
1560 prec = 4;
1561 break;
1563 default:
1564 *sizeP = 0;
1565 return "bad call to md_atof";
1568 t = atof_ieee (input_line_pointer, type, words);
1569 if (t)
1570 input_line_pointer = t;
1571 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1572 for (wordP = words; prec--;)
1574 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
1575 litP += sizeof (LITTLENUM_TYPE);
1578 return NULL;
1581 /* Write a value out to the object file, using the appropriate
1582 endianness. */
1584 void
1585 md_number_to_chars (buf, val, n)
1586 char *buf;
1587 valueT val;
1588 int n;
1590 if (target_big_endian)
1591 number_to_chars_bigendian (buf, val, n);
1592 else
1593 number_to_chars_littleendian (buf, val, n);
1596 /* Round up a section size to the appropriate boundary. */
1598 valueT
1599 md_section_align (segment, size)
1600 segT segment;
1601 valueT size;
1603 int align = bfd_get_section_alignment (stdoutput, segment);
1605 return ((size + (1 << align) - 1) & (-1 << align));
1608 /* We don't have any form of relaxing. */
1611 md_estimate_size_before_relax (fragp, seg)
1612 fragS *fragp ATTRIBUTE_UNUSED;
1613 asection *seg ATTRIBUTE_UNUSED;
1615 as_fatal (_("md_estimate_size_before_relax\n"));
1616 return 1;
1619 /* Convert a machine dependent frag. We never generate these. */
1621 void
1622 md_convert_frag (abfd, sec, fragp)
1623 bfd *abfd ATTRIBUTE_UNUSED;
1624 asection *sec ATTRIBUTE_UNUSED;
1625 fragS *fragp ATTRIBUTE_UNUSED;
1627 as_fatal (_("md_convert_frag\n"));
1630 void
1631 arc_code_symbol (expressionP)
1632 expressionS *expressionP;
1634 if (expressionP->X_op == O_symbol && expressionP->X_add_number == 0
1635 /* I think this test is unnecessary but just as a sanity check... */
1636 && expressionP->X_op_symbol == NULL)
1638 expressionS two;
1639 expressionP->X_op = O_right_shift;
1640 expressionP->X_add_symbol->sy_value.X_op = O_constant;
1641 two.X_op = O_constant;
1642 two.X_add_symbol = two.X_op_symbol = NULL;
1643 two.X_add_number = 2;
1644 expressionP->X_op_symbol = make_expr_symbol (&two);
1646 /* Allow %st(sym1-sym2) */
1647 else if (expressionP->X_op == O_subtract
1648 && expressionP->X_add_symbol != NULL
1649 && expressionP->X_op_symbol != NULL
1650 && expressionP->X_add_number == 0)
1652 expressionS two;
1653 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1654 expressionP->X_op = O_right_shift;
1655 two.X_op = O_constant;
1656 two.X_add_symbol = two.X_op_symbol = NULL;
1657 two.X_add_number = 2;
1658 expressionP->X_op_symbol = make_expr_symbol (&two);
1660 else
1662 as_bad ("expression too complex code symbol");
1663 return;
1667 /* Parse an operand that is machine-specific.
1669 The ARC has a special %-op to adjust addresses so they're usable in
1670 branches. The "st" is short for the STatus register.
1671 ??? Later expand this to take a flags value too.
1673 ??? We can't create new expression types so we map the %-op's onto the
1674 existing syntax. This means that the user could use the chosen syntax
1675 to achieve the same effect. */
1677 void
1678 md_operand (expressionP)
1679 expressionS *expressionP;
1681 char *p = input_line_pointer;
1683 if (*p == '%')
1684 if (strncmp (p, "%st(", 4) == 0)
1686 input_line_pointer += 4;
1687 expression (expressionP);
1688 if (*input_line_pointer != ')')
1690 as_bad ("missing ')' in %%-op");
1691 return;
1693 ++input_line_pointer;
1694 arc_code_symbol (expressionP);
1696 else
1698 /* It could be a register. */
1699 int i, l;
1700 struct arc_ext_operand_value *ext_oper = arc_ext_operands;
1701 p++;
1703 while (ext_oper)
1705 l = strlen (ext_oper->operand.name);
1706 if (!strncmp (p, ext_oper->operand.name, l) && !isalnum (*(p + l)))
1708 input_line_pointer += l + 1;
1709 expressionP->X_op = O_register;
1710 expressionP->X_add_number = (int) &ext_oper->operand;
1711 return;
1713 ext_oper = ext_oper->next;
1715 for (i = 0; i < arc_reg_names_count; i++)
1717 l = strlen (arc_reg_names[i].name);
1718 if (!strncmp (p, arc_reg_names[i].name, l) && !isalnum (*(p + l)))
1720 input_line_pointer += l + 1;
1721 expressionP->X_op = O_register;
1722 expressionP->X_add_number = (int) &arc_reg_names[i];
1723 break;
1729 /* We have no need to default values of symbols.
1730 We could catch register names here, but that is handled by inserting
1731 them all in the symbol table to begin with. */
1733 symbolS *
1734 md_undefined_symbol (name)
1735 char *name ATTRIBUTE_UNUSED;
1737 return 0;
1740 /* Functions concerning expressions. */
1742 /* Parse a .byte, .word, etc. expression.
1744 Values for the status register are specified with %st(label).
1745 `label' will be right shifted by 2. */
1747 void
1748 arc_parse_cons_expression (exp, nbytes)
1749 expressionS *exp;
1750 unsigned int nbytes ATTRIBUTE_UNUSED;
1752 char *p = input_line_pointer;
1753 int code_symbol_fix = 0;
1755 for (; ! is_end_of_line[(unsigned char) *p]; p++)
1756 if (*p == '@' && !strncmp (p, "@h30", 4))
1758 code_symbol_fix = 1;
1759 strcpy (p, "; ");
1761 expr (0, exp);
1762 if (code_symbol_fix)
1764 arc_code_symbol (exp);
1765 input_line_pointer = p;
1769 /* Record a fixup for a cons expression. */
1771 void
1772 arc_cons_fix_new (frag, where, nbytes, exp)
1773 fragS *frag;
1774 int where;
1775 int nbytes;
1776 expressionS *exp;
1778 if (nbytes == 4)
1780 int reloc_type;
1781 expressionS exptmp;
1783 /* This may be a special ARC reloc (eg: %st()). */
1784 reloc_type = get_arc_exp_reloc_type (1, BFD_RELOC_32, exp, &exptmp);
1785 fix_new_exp (frag, where, nbytes, &exptmp, 0, reloc_type);
1787 else
1789 fix_new_exp (frag, where, nbytes, exp, 0,
1790 nbytes == 2 ? BFD_RELOC_16
1791 : nbytes == 8 ? BFD_RELOC_64
1792 : BFD_RELOC_32);
1796 /* Functions concerning relocs. */
1798 /* The location from which a PC relative jump should be calculated,
1799 given a PC relative reloc. */
1801 long
1802 md_pcrel_from (fixP)
1803 fixS *fixP;
1805 if (fixP->fx_addsy != (symbolS *) NULL
1806 && ! S_IS_DEFINED (fixP->fx_addsy))
1808 /* The symbol is undefined. Let the linker figure it out. */
1809 return 0;
1812 /* Return the address of the delay slot. */
1813 return fixP->fx_frag->fr_address + fixP->fx_where + fixP->fx_size;
1816 /* Compute the reloc type of an expression.
1817 The possibly modified expression is stored in EXPNEW.
1819 This is used to convert the expressions generated by the %-op's into
1820 the appropriate operand type. It is called for both data in instructions
1821 (operands) and data outside instructions (variables, debugging info, etc.).
1823 Currently supported %-ops:
1825 %st(symbol): represented as "symbol >> 2"
1826 "st" is short for STatus as in the status register (pc)
1828 DEFAULT_TYPE is the type to use if no special processing is required.
1830 DATA_P is non-zero for data or limm values, zero for insn operands.
1831 Remember that the opcode "insertion fns" cannot be used on data, they're
1832 only for inserting operands into insns. They also can't be used for limm
1833 values as the insertion routines don't handle limm values. When called for
1834 insns we return fudged reloc types (real_value - BFD_RELOC_UNUSED). When
1835 called for data or limm values we use real reloc types. */
1837 static int
1838 get_arc_exp_reloc_type (data_p, default_type, exp, expnew)
1839 int data_p;
1840 int default_type;
1841 expressionS *exp;
1842 expressionS *expnew;
1844 /* If the expression is "symbol >> 2" we must change it to just "symbol",
1845 as fix_new_exp can't handle it. Similarily for (symbol - symbol) >> 2.
1846 That's ok though. What's really going on here is that we're using
1847 ">> 2" as a special syntax for specifying BFD_RELOC_ARC_B26. */
1849 if (exp->X_op == O_right_shift
1850 && exp->X_op_symbol != NULL
1851 && exp->X_op_symbol->sy_value.X_op == O_constant
1852 && exp->X_op_symbol->sy_value.X_add_number == 2
1853 && exp->X_add_number == 0)
1855 if (exp->X_add_symbol != NULL
1856 && (exp->X_add_symbol->sy_value.X_op == O_constant
1857 || exp->X_add_symbol->sy_value.X_op == O_symbol))
1859 *expnew = *exp;
1860 expnew->X_op = O_symbol;
1861 expnew->X_op_symbol = NULL;
1862 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1864 else if (exp->X_add_symbol != NULL
1865 && exp->X_add_symbol->sy_value.X_op == O_subtract)
1867 *expnew = exp->X_add_symbol->sy_value;
1868 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1872 *expnew = *exp;
1873 return default_type;
1876 /* Apply a fixup to the object code. This is called for all the
1877 fixups we generated by the call to fix_new_exp, above. In the call
1878 above we used a reloc code which was the largest legal reloc code
1879 plus the operand index. Here we undo that to recover the operand
1880 index. At this point all symbol values should be fully resolved,
1881 and we attempt to completely resolve the reloc. If we can not do
1882 that, we determine the correct reloc code and put it back in the fixup. */
1885 md_apply_fix3 (fixP, valueP, seg)
1886 fixS *fixP;
1887 valueT *valueP;
1888 segT seg;
1890 #if 0
1891 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1892 #endif
1893 valueT value;
1895 /* FIXME FIXME FIXME: The value we are passed in *valueP includes
1896 the symbol values. Since we are using BFD_ASSEMBLER, if we are
1897 doing this relocation the code in write.c is going to call
1898 bfd_perform_relocation, which is also going to use the symbol
1899 value. That means that if the reloc is fully resolved we want to
1900 use *valueP since bfd_perform_relocation is not being used.
1901 However, if the reloc is not fully resolved we do not want to use
1902 *valueP, and must use fx_offset instead. However, if the reloc
1903 is PC relative, we do want to use *valueP since it includes the
1904 result of md_pcrel_from. This is confusing. */
1906 if (fixP->fx_addsy == (symbolS *) NULL)
1908 value = *valueP;
1909 fixP->fx_done = 1;
1911 else if (fixP->fx_pcrel)
1913 value = *valueP;
1914 /* ELF relocations are against symbols.
1915 If this symbol is in a different section then we need to leave it for
1916 the linker to deal with. Unfortunately, md_pcrel_from can't tell,
1917 so we have to undo it's effects here. */
1918 if (S_IS_DEFINED (fixP->fx_addsy)
1919 && S_GET_SEGMENT (fixP->fx_addsy) != seg)
1920 value += md_pcrel_from (fixP);
1922 else
1924 value = fixP->fx_offset;
1925 if (fixP->fx_subsy != (symbolS *) NULL)
1927 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
1928 value -= S_GET_VALUE (fixP->fx_subsy);
1929 else
1931 /* We can't actually support subtracting a symbol. */
1932 as_bad_where (fixP->fx_file, fixP->fx_line,
1933 "expression too complex");
1938 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
1940 int opindex;
1941 const struct arc_operand *operand;
1942 char *where;
1943 arc_insn insn;
1945 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
1947 operand = &arc_operands[opindex];
1949 /* Fetch the instruction, insert the fully resolved operand
1950 value, and stuff the instruction back again. */
1951 where = fixP->fx_frag->fr_literal + fixP->fx_where;
1952 if (target_big_endian)
1953 insn = bfd_getb32 ((unsigned char *) where);
1954 else
1955 insn = bfd_getl32 ((unsigned char *) where);
1956 insn = arc_insert_operand (insn, operand, -1, NULL, (offsetT) value,
1957 fixP->fx_file, fixP->fx_line);
1958 if (target_big_endian)
1959 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1960 else
1961 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
1963 if (fixP->fx_done)
1965 /* Nothing else to do here. */
1966 return 1;
1969 /* Determine a BFD reloc value based on the operand information.
1970 We are only prepared to turn a few of the operands into relocs.
1971 !!! Note that we can't handle limm values here. Since we're using
1972 implicit addends the addend must be inserted into the instruction,
1973 however, the opcode insertion routines currently do nothing with
1974 limm values. */
1975 if (operand->fmt == 'B')
1977 assert ((operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0
1978 && operand->bits == 20
1979 && operand->shift == 7);
1980 fixP->fx_r_type = BFD_RELOC_ARC_B22_PCREL;
1982 else if (operand->fmt == 'J')
1984 assert ((operand->flags & ARC_OPERAND_ABSOLUTE_BRANCH) != 0
1985 && operand->bits == 24
1986 && operand->shift == 32);
1987 fixP->fx_r_type = BFD_RELOC_ARC_B26;
1989 else if (operand->fmt == 'L')
1991 assert ((operand->flags & ARC_OPERAND_LIMM) != 0
1992 && operand->bits == 32
1993 && operand->shift == 32);
1994 fixP->fx_r_type = BFD_RELOC_32;
1996 else
1998 as_bad_where (fixP->fx_file, fixP->fx_line,
1999 "unresolved expression that must be resolved");
2000 fixP->fx_done = 1;
2001 return 1;
2004 else
2006 switch (fixP->fx_r_type)
2008 case BFD_RELOC_8:
2009 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2010 value, 1);
2011 break;
2012 case BFD_RELOC_16:
2013 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2014 value, 2);
2015 break;
2016 case BFD_RELOC_32:
2017 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2018 value, 4);
2019 break;
2020 #if 0
2021 case BFD_RELOC_64:
2022 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2023 value, 8);
2024 break;
2025 #endif
2026 case BFD_RELOC_ARC_B26:
2027 /* If !fixP->fx_done then `value' is an implicit addend.
2028 We must shift it right by 2 in this case as well because the
2029 linker performs the relocation and then adds this in (as opposed
2030 to adding this in and then shifting right by 2). */
2031 value >>= 2;
2032 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
2033 value, 4);
2034 break;
2035 default:
2036 abort ();
2040 fixP->fx_addnumber = value;
2042 return 1;
2045 /* Translate internal representation of relocation info to BFD target
2046 format. */
2048 arelent *
2049 tc_gen_reloc (section, fixP)
2050 asection *section ATTRIBUTE_UNUSED;
2051 fixS *fixP;
2053 arelent *reloc;
2055 reloc = (arelent *) xmalloc (sizeof (arelent));
2057 reloc->sym_ptr_ptr = &fixP->fx_addsy->bsym;
2058 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
2059 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
2060 if (reloc->howto == (reloc_howto_type *) NULL)
2062 as_bad_where (fixP->fx_file, fixP->fx_line,
2063 "internal error: can't export reloc type %d (`%s')",
2064 fixP->fx_r_type,
2065 bfd_get_reloc_code_name (fixP->fx_r_type));
2066 return NULL;
2069 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
2071 /* Set addend to account for PC being advanced one insn before the
2072 target address is computed, drop fx_addnumber as it is handled
2073 elsewhere mlm */
2075 reloc->addend = (fixP->fx_pcrel ? -4 : 0);
2077 return reloc;