1 /* tc-arc.c -- Assembler for the ARC
2 Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
3 Contributed by Doug Evans (dje@cygnus.com).
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "opcode/arc.h"
28 extern int arc_get_mach PARAMS ((char *));
30 static arc_insn arc_insert_operand PARAMS ((arc_insn,
31 const struct arc_operand *, int,
32 const struct arc_operand_value *,
33 offsetT, char *, unsigned int));
34 static void arc_common PARAMS ((int));
35 static void arc_cpu PARAMS ((int));
36 /*static void arc_rename PARAMS ((int));*/
37 static int get_arc_exp_reloc_type PARAMS ((int, int, expressionS *,
40 const pseudo_typeS md_pseudo_table[] =
42 { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0) */
43 { "common", arc_common, 0 },
44 /*{ "hword", cons, 2 }, - already exists */
46 /*{ "xword", cons, 8 },*/
47 { "cpu", arc_cpu, 0 },
48 /*{ "rename", arc_rename, 0 },*/
52 /* This array holds the chars that always start a comment. If the
53 pre-processor is disabled, these aren't very useful */
54 const char comment_chars[] = "#;";
56 /* This array holds the chars that only start a comment at the beginning of
57 a line. If the line seems to have the form '# 123 filename'
58 .line and .file directives will appear in the pre-processed output */
59 /* Note that input_file.c hand checks for '#' at the beginning of the
60 first line of the input file. This is because the compiler outputs
61 #NO_APP at the beginning of its output. */
62 /* Also note that comments started like this one will always
63 work if '/' isn't otherwise defined. */
64 const char line_comment_chars[] = "#";
66 const char line_separator_chars[] = "";
68 /* Chars that can be used to separate mant from exp in floating point nums */
69 const char EXP_CHARS[] = "eE";
71 /* Chars that mean this number is a floating point constant */
74 const char FLT_CHARS[] = "rRsSfFdD";
77 extern int target_big_endian;
78 const char *arc_target_format = DEFAULT_TARGET_FORMAT;
79 static int byte_order = DEFAULT_BYTE_ORDER;
81 /* One of bfd_mach_arc_xxx. */
82 static int arc_mach_type = bfd_mach_arc_base;
84 /* Non-zero if the cpu type has been explicitly specified. */
85 static int mach_type_specified_p = 0;
87 /* Non-zero if opcode tables have been initialized.
88 A .cpu command must appear before any instructions. */
89 static int cpu_tables_init_p = 0;
91 static struct hash_control *arc_suffix_hash = NULL;
93 const char *md_shortopts = "";
94 struct option md_longopts[] =
96 #define OPTION_EB (OPTION_MD_BASE + 0)
97 {"EB", no_argument, NULL, OPTION_EB},
98 #define OPTION_EL (OPTION_MD_BASE + 1)
99 {"EL", no_argument, NULL, OPTION_EL},
100 { NULL, no_argument, NULL, 0 }
102 size_t md_longopts_size = sizeof (md_longopts);
107 * Invocation line includes a switch not recognized by the base assembler.
108 * See if it's a processor-specific option.
112 md_parse_option (c, arg)
119 byte_order = BIG_ENDIAN;
120 arc_target_format = "elf32-bigarc";
123 byte_order = LITTLE_ENDIAN;
124 arc_target_format = "elf32-littlearc";
133 md_show_usage (stream)
138 -EB generate big endian output\n\
139 -EL generate little endian output\n");
142 /* This function is called once, at assembler startup time. It should
143 set up all the tables, etc. that the MD part of the assembler will need.
144 Opcode selection is defered until later because we might see a .cpu
150 /* The endianness can be chosen "at the factory". */
151 target_big_endian = byte_order == BIG_ENDIAN;
153 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, arc_mach_type))
154 as_warn ("could not set architecture and machine");
156 /* Assume the base cpu. This call is necessary because we need to
157 initialize `arc_operand_map' which may be needed before we see the
159 arc_opcode_init_tables (arc_get_opcode_mach (bfd_mach_arc_base,
163 /* Initialize the various opcode and operand tables.
164 MACH is one of bfd_mach_arc_xxx. */
167 init_opcode_tables (mach)
170 register unsigned int i;
173 if ((arc_suffix_hash = hash_new ()) == NULL)
174 as_fatal ("virtual memory exhausted");
176 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
177 as_warn ("could not set architecture and machine");
179 /* This initializes a few things in arc-opc.c that we need.
180 This must be called before the various arc_xxx_supported fns. */
181 arc_opcode_init_tables (arc_get_opcode_mach (mach, target_big_endian));
183 /* Only put the first entry of each equivalently named suffix in the
186 for (i = 0; i < arc_suffixes_count; i++)
188 if (! arc_opval_supported (&arc_suffixes[i]))
190 if (strcmp (arc_suffixes[i].name, last) != 0)
191 hash_insert (arc_suffix_hash, arc_suffixes[i].name, (PTR) (arc_suffixes + i));
192 last = arc_suffixes[i].name;
195 /* Since registers don't have a prefix, we put them in the symbol table so
196 they can't be used as symbols. This also simplifies argument parsing as
197 we can let gas parse registers for us. The recorded register number is
198 the index in `arc_reg_names'. */
199 for (i = 0; i < arc_reg_names_count; i++)
201 if (! arc_opval_supported (&arc_reg_names[i]))
203 /* Use symbol_create here instead of symbol_new so we don't try to
204 output registers into the object file's symbol table. */
205 symbol_table_insert (symbol_create (arc_reg_names[i].name, reg_section,
206 i, &zero_address_frag));
209 /* Tell `s_cpu' it's too late. */
210 cpu_tables_init_p = 1;
213 /* Insert an operand value into an instruction.
214 If REG is non-NULL, it is a register number and ignore VAL. */
217 arc_insert_operand (insn, operand, mods, reg, val, file, line)
219 const struct arc_operand *operand;
221 const struct arc_operand_value *reg;
226 if (operand->bits != 32)
231 if ((operand->flags & ARC_OPERAND_SIGNED) != 0)
233 if ((operand->flags & ARC_OPERAND_SIGNOPT) != 0)
234 max = (1 << operand->bits) - 1;
236 max = (1 << (operand->bits - 1)) - 1;
237 min = - (1 << (operand->bits - 1));
241 max = (1 << operand->bits) - 1;
245 if ((operand->flags & ARC_OPERAND_NEGATIVE) != 0)
250 if (test < (offsetT) min || test > (offsetT) max)
253 "operand out of range (%s not between %ld and %ld)";
256 sprint_value (buf, test);
257 if (file == (char *) NULL)
258 as_warn (err, buf, min, max);
260 as_warn_where (file, line, err, buf, min, max);
269 insn = (*operand->insert) (insn, operand, mods, reg, (long) val, &errmsg);
270 if (errmsg != (const char *) NULL)
274 insn |= (((long) val & ((1 << operand->bits) - 1))
280 /* We need to keep a list of fixups. We can't simply generate them as
281 we go, because that would require us to first create the frag, and
282 that would screw up references to ``.''. */
286 /* index into `arc_operands' */
293 #define MAX_SUFFIXES 5
295 /* This routine is called for each instruction to be assembled. */
301 const struct arc_opcode *opcode;
304 static int init_tables_p = 0;
306 /* Opcode table initialization is deferred until here because we have to
307 wait for a possible .cpu command. */
310 init_opcode_tables (arc_mach_type);
314 /* Skip leading white space. */
315 while (isspace (*str))
318 /* The instructions are stored in lists hashed by the first letter (though
319 we needn't care how they're hashed). Get the first in the list. */
321 opcode = arc_opcode_lookup_asm (str);
323 /* Keep looking until we find a match. */
326 for ( ; opcode != NULL; opcode = ARC_OPCODE_NEXT_ASM (opcode))
328 int past_opcode_p, fc, num_suffixes;
330 struct arc_fixup fixups[MAX_FIXUPS];
331 /* Used as a sanity check. If we need a limm reloc, make sure we ask
332 for an extra 4 bytes from frag_more. */
334 const struct arc_operand_value *insn_suffixes[MAX_SUFFIXES];
336 /* Is this opcode supported by the selected cpu? */
337 if (! arc_opcode_supported (opcode))
340 /* Scan the syntax string. If it doesn't match, try the next one. */
342 arc_opcode_init_insert ();
343 insn = opcode->value;
349 /* We don't check for (*str != '\0') here because we want to parse
350 any trailing fake arguments in the syntax string. */
351 for (str = start, syn = opcode->syntax; *syn != '\0'; )
354 const struct arc_operand *operand;
356 /* Non operand chars must match exactly. */
357 if (*syn != '%' || *++syn == '%')
359 /* Handle '+' specially as we want to allow "ld r0,[sp-4]". */
360 /* ??? The syntax has changed to [sp,-4]. */
361 if (0 && *syn == '+' && *str == '-')
363 /* Skip over syn's +, but leave str's - alone.
364 That makes the case identical to "ld r0,[sp+-4]". */
367 else if (*str == *syn)
379 /* We have an operand. Pick out any modifiers. */
381 while (ARC_MOD_P (arc_operands[arc_operand_map[*syn]].flags))
383 mods |= arc_operands[arc_operand_map[*syn]].flags & ARC_MOD_BITS;
386 operand = arc_operands + arc_operand_map[*syn];
387 if (operand->fmt == 0)
388 as_fatal ("unknown syntax format character `%c'", *syn);
390 if (operand->flags & ARC_OPERAND_FAKE)
392 const char *errmsg = NULL;
395 insn = (*operand->insert) (insn, operand, mods, NULL, 0, &errmsg);
396 /* If we get an error, go on to try the next insn. */
402 /* Are we finished with suffixes? */
403 else if (!past_opcode_p)
408 const struct arc_operand_value *suf,*suffix,*suffix_end;
410 if (!(operand->flags & ARC_OPERAND_SUFFIX))
413 /* If we're at a space in the input string, we want to skip the
414 remaining suffixes. There may be some fake ones though, so
415 just go on to try the next one. */
423 if (mods & ARC_MOD_DOT)
431 /* This can happen in "b.nd foo" and we're currently looking
432 for "%q" (ie: a condition code suffix). */
440 /* Pick the suffix out and look it up via the hash table. */
441 for (t = s; *t && isalpha (*t); ++t)
445 suf = hash_find (arc_suffix_hash, s);
449 /* This can happen in "blle foo" and we're currently using
450 the template "b%q%.n %j". The "bl" insn occurs later in
451 the table so "lle" isn't an illegal suffix. */
455 /* Is it the right type? Note that the same character is used
456 several times, so we have to examine all of them. This is
457 relatively efficient as equivalent entries are kept
458 together. If it's not the right type, don't increment `str'
459 so we try the next one in the series. */
461 suffix_end = arc_suffixes + arc_suffixes_count;
463 suffix < suffix_end && strcmp (suffix->name, suf->name) == 0;
466 if (arc_operands[suffix->type].fmt == *syn)
468 /* Insert the suffix's value into the insn. */
470 insn = (*operand->insert) (insn, operand,
471 mods, NULL, suffix->value,
474 insn |= suffix->value << operand->shift;
483 ; /* Wrong type. Just go on to try next insn entry. */
486 if (num_suffixes == MAX_SUFFIXES)
487 as_bad ("too many suffixes");
489 insn_suffixes[num_suffixes++] = suffix;
493 /* This is either a register or an expression of some kind. */
497 const struct arc_operand_value *reg = NULL;
501 if (operand->flags & ARC_OPERAND_SUFFIX)
504 /* Is there anything left to parse?
505 We don't check for this at the top because we want to parse
506 any trailing fake arguments in the syntax string. */
510 /* Is this a syntax character? Eg: is there a '[' present when
511 there shouldn't be? */
513 /* '.' as in ".LLC0" */
515 /* '_' as in "_print" */
517 /* '-' as in "[fp,-4]" */
519 /* '%' as in "%ia(_func)" */
524 /* Parse the operand. */
525 hold = input_line_pointer;
526 input_line_pointer = str;
528 str = input_line_pointer;
529 input_line_pointer = hold;
531 if (exp.X_op == O_illegal)
532 as_bad ("illegal operand");
533 else if (exp.X_op == O_absent)
534 as_bad ("missing operand");
535 else if (exp.X_op == O_constant)
537 value = exp.X_add_number;
539 else if (exp.X_op == O_register)
541 reg = arc_reg_names + exp.X_add_number;
545 /* We need to generate a fixup for this expression. */
546 if (fc >= MAX_FIXUPS)
547 as_fatal ("too many fixups");
548 fixups[fc].exp = exp;
550 /* If this is a register constant (IE: one whose
551 register value gets stored as 61-63) then this
552 must be a limm. We don't support shimm relocs. */
553 /* ??? This bit could use some cleaning up.
554 Referencing the format chars like this goes
556 #define IS_REG_OPERAND(o) ((o) == 'a' || (o) == 'b' || (o) == 'c')
557 if (IS_REG_OPERAND (*syn))
561 fixups[fc].opindex = arc_operand_map['L'];
563 /* Tell insert_reg we need a limm. This is
564 needed because the value at this point is
566 /* ??? We need a cleaner interface than this. */
567 (*arc_operands[arc_operand_map['Q']].insert)
568 (insn, operand, mods, reg, 0L, &junk);
571 fixups[fc].opindex = arc_operand_map[*syn];
576 /* Insert the register or expression into the instruction. */
579 const char *errmsg = NULL;
580 insn = (*operand->insert) (insn, operand, mods,
581 reg, (long) value, &errmsg);
583 if (errmsg != (const char *) NULL)
586 /* FIXME: We want to try shimm insns for limm ones. But if
587 the constant won't fit, we must go on to try the next
588 possibility. Where do we issue warnings for constants
589 that are too big then? At present, we'll flag the insn
590 as unrecognizable! Maybe have the "bad instruction"
591 error message include our `errmsg'? */
592 if (errmsg != (const char *) NULL)
596 insn |= (value & ((1 << operand->bits) - 1)) << operand->shift;
602 /* If we're at the end of the syntax string, we're done. */
603 /* FIXME: try to move this to a separate function. */
610 /* For the moment we assume a valid `str' can only contain blanks
611 now. IE: We needn't try again with a longer version of the
612 insn and it is assumed that longer versions of insns appear
613 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
615 while (isspace (*str))
619 as_bad ("junk at end of line: `%s'", str);
621 /* Is there a limm value? */
622 limm_p = arc_opcode_limm_p (&limm);
624 /* Perform various error and warning tests. */
627 static int in_delay_slot_p = 0;
628 static int prev_insn_needs_cc_nop_p = 0;
629 /* delay slot type seen */
630 int delay_slot_type = ARC_DELAY_NONE;
631 /* conditional execution flag seen */
633 /* 1 if condition codes are being set */
635 /* 1 if conditional branch, including `b' "branch always" */
636 int cond_branch_p = opcode->flags & ARC_OPCODE_COND_BRANCH;
637 int need_cc_nop_p = 0;
639 for (i = 0; i < num_suffixes; ++i)
641 switch (arc_operands[insn_suffixes[i]->type].fmt)
644 delay_slot_type = insn_suffixes[i]->value;
647 conditional = insn_suffixes[i]->value;
655 /* Putting an insn with a limm value in a delay slot is supposed to
656 be legal, but let's warn the user anyway. Ditto for 8 byte
657 jumps with delay slots. */
658 if (in_delay_slot_p && limm_p)
659 as_warn ("8 byte instruction in delay slot");
660 if (delay_slot_type != ARC_DELAY_NONE && limm_p)
661 as_warn ("8 byte jump instruction with delay slot");
662 in_delay_slot_p = (delay_slot_type != ARC_DELAY_NONE) && !limm_p;
664 /* Warn when a conditional branch immediately follows a set of
665 the condition codes. Note that this needn't be done if the
666 insn that sets the condition codes uses a limm. */
667 if (cond_branch_p && conditional != 0 /* 0 = "always" */
668 && prev_insn_needs_cc_nop_p)
669 as_warn ("conditional branch follows set of flags");
670 prev_insn_needs_cc_nop_p = cc_set_p && !limm_p;
673 /* Write out the instruction.
674 It is important to fetch enough space in one call to `frag_more'.
675 We use (f - frag_now->fr_literal) to compute where we are and we
676 don't want frag_now to change between calls. */
680 md_number_to_chars (f, insn, 4);
681 md_number_to_chars (f + 4, limm, 4);
683 else if (limm_reloc_p)
685 /* We need a limm reloc, but the tables think we don't. */
691 md_number_to_chars (f, insn, 4);
694 /* Create any fixups. */
695 for (i = 0; i < fc; ++i)
697 int op_type, reloc_type;
699 const struct arc_operand *operand;
701 /* Create a fixup for this operand.
702 At this point we do not use a bfd_reloc_code_real_type for
703 operands residing in the insn, but instead just use the
704 operand index. This lets us easily handle fixups for any
705 operand type, although that is admittedly not a very exciting
706 feature. We pick a BFD reloc type in md_apply_fix.
708 Limm values (4 byte immediate "constants") must be treated
709 normally because they're not part of the actual insn word
710 and thus the insertion routines don't handle them. */
712 if (arc_operands[fixups[i].opindex].flags & ARC_OPERAND_LIMM)
714 op_type = fixups[i].opindex;
715 /* FIXME: can we add this data to the operand table? */
716 if (op_type == arc_operand_map['L'])
717 reloc_type = BFD_RELOC_32;
718 else if (op_type == arc_operand_map['J'])
719 reloc_type = BFD_RELOC_ARC_B26;
722 reloc_type = get_arc_exp_reloc_type (1, reloc_type,
728 op_type = get_arc_exp_reloc_type (0, fixups[i].opindex,
729 &fixups[i].exp, &exptmp);
730 reloc_type = op_type + (int) BFD_RELOC_UNUSED;
732 operand = &arc_operands[op_type];
733 fix_new_exp (frag_now,
734 ((f - frag_now->fr_literal)
735 + (operand->flags & ARC_OPERAND_LIMM ? 4 : 0)), 4,
737 (operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0,
738 (bfd_reloc_code_real_type) reloc_type);
745 /* Try the next entry. */
748 as_bad ("bad instruction `%s'", start);
751 /* ??? This was copied from tc-sparc.c, I think. Is it necessary? */
763 name = input_line_pointer;
764 c = get_symbol_end ();
765 /* just after name is now '\0' */
766 p = input_line_pointer;
769 if (*input_line_pointer != ',')
771 as_bad ("expected comma after symbol-name");
772 ignore_rest_of_line ();
775 input_line_pointer++; /* skip ',' */
776 if ((temp = get_absolute_expression ()) < 0)
778 as_bad (".COMMon length (%d.) <0! Ignored.", temp);
779 ignore_rest_of_line ();
784 symbolP = symbol_find_or_make (name);
786 if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
788 as_bad ("ignoring attempt to re-define symbol");
789 ignore_rest_of_line ();
792 if (S_GET_VALUE (symbolP) != 0)
794 if (S_GET_VALUE (symbolP) != size)
796 as_warn ("Length of .comm \"%s\" is already %ld. Not changed to %d.",
797 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
800 assert (symbolP->sy_frag == &zero_address_frag);
801 if (*input_line_pointer != ',')
803 as_bad ("expected comma after common length");
804 ignore_rest_of_line ();
807 input_line_pointer++;
809 if (*input_line_pointer != '"')
811 temp = get_absolute_expression ();
815 as_warn ("Common alignment negative; 0 assumed");
826 old_subsec = now_subseg;
828 record_alignment (bss_section, align);
829 subseg_set (bss_section, 0);
831 frag_align (align, 0, 0);
832 if (S_GET_SEGMENT (symbolP) == bss_section)
833 symbolP->sy_frag->fr_symbol = 0;
834 symbolP->sy_frag = frag_now;
835 p = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
836 (offsetT) size, (char *) 0);
838 S_SET_SEGMENT (symbolP, bss_section);
839 S_CLEAR_EXTERNAL (symbolP);
840 subseg_set (old_sec, old_subsec);
845 S_SET_VALUE (symbolP, (valueT) size);
846 S_SET_ALIGN (symbolP, temp);
847 S_SET_EXTERNAL (symbolP);
848 S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
853 input_line_pointer++;
854 /* ??? Some say data, some say bss. */
855 if (strncmp (input_line_pointer, ".bss\"", 5)
856 && strncmp (input_line_pointer, ".data\"", 6))
858 input_line_pointer--;
859 goto bad_common_segment;
861 while (*input_line_pointer++ != '"')
863 goto allocate_common;
865 demand_empty_rest_of_line ();
870 p = input_line_pointer;
871 while (*p && *p != '\n')
875 as_bad ("bad .common segment %s", input_line_pointer + 1);
877 input_line_pointer = p;
878 ignore_rest_of_line ();
883 /* Select the cpu we're assembling for. */
893 /* If an instruction has already been seen, it's too late. */
894 if (cpu_tables_init_p)
896 as_bad (".cpu command must appear before any instructions");
897 ignore_rest_of_line ();
901 cpu = input_line_pointer;
902 c = get_symbol_end ();
903 mach = arc_get_mach (cpu);
904 *input_line_pointer = c;
908 demand_empty_rest_of_line ();
910 /* The cpu may have been selected on the command line.
911 The choices must match. */
912 /* ??? This was a command line option early on. It's gone now, but
914 if (mach_type_specified_p && mach != arc_mach_type)
915 as_bad (".cpu conflicts with previous value");
918 arc_mach_type = mach;
919 mach_type_specified_p = 1;
920 if (!bfd_set_arch_mach (stdoutput, bfd_arch_arc, mach))
921 as_warn ("could not set architecture and machine");
926 as_bad ("bad .cpu op");
927 ignore_rest_of_line ();
931 /* The .rename pseudo-op. This is used by gcc to implement
932 -mmangle-cpu-libgcc. */
943 name = input_line_pointer;
944 c = get_symbol_end ();
945 sym = symbol_find_or_make (name);
946 *input_line_pointer = c;
948 if (*input_line_pointer != ',')
950 as_bad ("missing rename string");
951 ignore_rest_of_line ();
954 ++input_line_pointer;
957 name = input_line_pointer;
958 c = get_symbol_end ();
961 *input_line_pointer = c;
962 as_bad ("invalid symbol to rename to");
963 ignore_rest_of_line ();
966 new = (char *) xmalloc (strlen (name) + 1);
968 *input_line_pointer = c;
969 sym->sy_tc.real_name = new;
971 demand_empty_rest_of_line ();
975 /* Turn a string in input_line_pointer into a floating point constant of type
976 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
977 emitted is stored in *sizeP.
978 An error message is returned, or NULL on OK. */
980 /* Equal to MAX_PRECISION in atof-ieee.c */
981 #define MAX_LITTLENUMS 6
984 md_atof (type, litP, sizeP)
990 LITTLENUM_TYPE words[MAX_LITTLENUMS];
991 LITTLENUM_TYPE *wordP;
1009 return "bad call to md_atof";
1012 t = atof_ieee (input_line_pointer, type, words);
1014 input_line_pointer = t;
1015 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1016 for (wordP = words; prec--;)
1018 md_number_to_chars (litP, (valueT) (*wordP++), sizeof (LITTLENUM_TYPE));
1019 litP += sizeof (LITTLENUM_TYPE);
1025 /* Write a value out to the object file, using the appropriate
1029 md_number_to_chars (buf, val, n)
1034 if (target_big_endian)
1035 number_to_chars_bigendian (buf, val, n);
1037 number_to_chars_littleendian (buf, val, n);
1040 /* Round up a section size to the appropriate boundary. */
1043 md_section_align (segment, size)
1047 int align = bfd_get_section_alignment (stdoutput, segment);
1049 return ((size + (1 << align) - 1) & (-1 << align));
1052 /* We don't have any form of relaxing. */
1055 md_estimate_size_before_relax (fragp, seg)
1062 /* Convert a machine dependent frag. We never generate these. */
1065 md_convert_frag (abfd, sec, fragp)
1073 /* Parse an operand that is machine-specific.
1075 The ARC has a special %-op to adjust addresses so they're usable in
1076 branches. The "st" is short for the STatus register.
1077 ??? Later expand this to take a flags value too.
1079 ??? We can't create new expression types so we map the %-op's onto the
1080 existing syntax. This means that the user could use the chosen syntax
1081 to achieve the same effect. Perhaps put a special cookie in X_add_number
1082 to mark the expression as special. */
1085 md_operand (expressionP)
1086 expressionS *expressionP;
1088 char *p = input_line_pointer;
1090 if (*p == '%' && strncmp (p, "%st(", 4) == 0)
1092 input_line_pointer += 4;
1093 expression (expressionP);
1094 if (*input_line_pointer != ')')
1096 as_bad ("missing ')' in %-op");
1099 ++input_line_pointer;
1100 if (expressionP->X_op == O_symbol
1101 && expressionP->X_add_number == 0
1102 /* I think this test is unnecessary but just as a sanity check... */
1103 && expressionP->X_op_symbol == NULL)
1107 expressionP->X_op = O_right_shift;
1108 two.X_op = O_constant;
1109 two.X_add_symbol = two.X_op_symbol = NULL;
1110 two.X_add_number = 2;
1111 expressionP->X_op_symbol = make_expr_symbol (&two);
1113 /* allow %st(sym1-sym2) */
1114 else if (expressionP->X_op == O_subtract
1115 && expressionP->X_add_symbol != NULL
1116 && expressionP->X_op_symbol != NULL
1117 && expressionP->X_add_number == 0)
1121 expressionP->X_add_symbol = make_expr_symbol (expressionP);
1122 expressionP->X_op = O_right_shift;
1123 two.X_op = O_constant;
1124 two.X_add_symbol = two.X_op_symbol = NULL;
1125 two.X_add_number = 2;
1126 expressionP->X_op_symbol = make_expr_symbol (&two);
1130 as_bad ("expression too complex for %%st");
1136 /* We have no need to default values of symbols.
1137 We could catch register names here, but that is handled by inserting
1138 them all in the symbol table to begin with. */
1141 md_undefined_symbol (name)
1147 /* Functions concerning expressions. */
1149 /* Parse a .byte, .word, etc. expression.
1151 Values for the status register are specified with %st(label).
1152 `label' will be right shifted by 2. */
1155 arc_parse_cons_expression (exp, nbytes)
1162 /* Record a fixup for a cons expression. */
1165 arc_cons_fix_new (frag, where, nbytes, exp)
1176 /* This may be a special ARC reloc (eg: %st()). */
1177 reloc_type = get_arc_exp_reloc_type (1, BFD_RELOC_32, exp, &exptmp);
1178 fix_new_exp (frag, where, nbytes, &exptmp, 0, reloc_type);
1182 fix_new_exp (frag, where, nbytes, exp, 0,
1183 nbytes == 2 ? BFD_RELOC_16
1184 : nbytes == 8 ? BFD_RELOC_64
1189 /* Functions concerning relocs. */
1191 /* The location from which a PC relative jump should be calculated,
1192 given a PC relative reloc. */
1195 md_pcrel_from (fixP)
1198 if (fixP->fx_addsy != (symbolS *) NULL
1199 && ! S_IS_DEFINED (fixP->fx_addsy))
1201 /* The symbol is undefined. Let the linker figure it out. */
1205 /* Return the address of the delay slot. */
1206 return fixP->fx_frag->fr_address + fixP->fx_where + fixP->fx_size;
1209 /* Compute the reloc type of an expression.
1210 The possibly modified expression is stored in EXPNEW.
1212 This is used to convert the expressions generated by the %-op's into
1213 the appropriate operand type. It is called for both data in instructions
1214 (operands) and data outside instructions (variables, debugging info, etc.).
1216 Currently supported %-ops:
1218 %st(symbol): represented as "symbol >> 2"
1219 "st" is short for STatus as in the status register (pc)
1221 DEFAULT_TYPE is the type to use if no special processing is required.
1223 DATA_P is non-zero for data or limm values, zero for insn operands.
1224 Remember that the opcode "insertion fns" cannot be used on data, they're
1225 only for inserting operands into insns. They also can't be used for limm
1226 values as the insertion routines don't handle limm values. When called for
1227 insns we return fudged reloc types (real_value - BFD_RELOC_UNUSED). When
1228 called for data or limm values we use real reloc types. */
1231 get_arc_exp_reloc_type (data_p, default_type, exp, expnew)
1235 expressionS *expnew;
1237 /* If the expression is "symbol >> 2" we must change it to just "symbol",
1238 as fix_new_exp can't handle it. Similarily for (symbol - symbol) >> 2.
1239 That's ok though. What's really going on here is that we're using
1240 ">> 2" as a special syntax for specifying BFD_RELOC_ARC_B26. */
1242 if (exp->X_op == O_right_shift
1243 && exp->X_op_symbol != NULL
1244 && exp->X_op_symbol->sy_value.X_op == O_constant
1245 && exp->X_op_symbol->sy_value.X_add_number == 2
1246 && exp->X_add_number == 0)
1248 if (exp->X_add_symbol != NULL
1249 && (exp->X_add_symbol->sy_value.X_op == O_constant
1250 || exp->X_add_symbol->sy_value.X_op == O_symbol))
1253 expnew->X_op = O_symbol;
1254 expnew->X_op_symbol = NULL;
1255 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1257 else if (exp->X_add_symbol != NULL
1258 && exp->X_add_symbol->sy_value.X_op == O_subtract)
1260 *expnew = exp->X_add_symbol->sy_value;
1261 return data_p ? BFD_RELOC_ARC_B26 : arc_operand_map['J'];
1266 return default_type;
1269 /* Apply a fixup to the object code. This is called for all the
1270 fixups we generated by the call to fix_new_exp, above. In the call
1271 above we used a reloc code which was the largest legal reloc code
1272 plus the operand index. Here we undo that to recover the operand
1273 index. At this point all symbol values should be fully resolved,
1274 and we attempt to completely resolve the reloc. If we can not do
1275 that, we determine the correct reloc code and put it back in the fixup. */
1278 md_apply_fix3 (fixP, valueP, seg)
1283 /*char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;*/
1286 /* FIXME FIXME FIXME: The value we are passed in *valueP includes
1287 the symbol values. Since we are using BFD_ASSEMBLER, if we are
1288 doing this relocation the code in write.c is going to call
1289 bfd_perform_relocation, which is also going to use the symbol
1290 value. That means that if the reloc is fully resolved we want to
1291 use *valueP since bfd_perform_relocation is not being used.
1292 However, if the reloc is not fully resolved we do not want to use
1293 *valueP, and must use fx_offset instead. However, if the reloc
1294 is PC relative, we do want to use *valueP since it includes the
1295 result of md_pcrel_from. This is confusing. */
1297 if (fixP->fx_addsy == (symbolS *) NULL)
1302 else if (fixP->fx_pcrel)
1305 /* ELF relocations are against symbols.
1306 If this symbol is in a different section then we need to leave it for
1307 the linker to deal with. Unfortunately, md_pcrel_from can't tell,
1308 so we have to undo it's effects here. */
1309 if (S_IS_DEFINED (fixP->fx_addsy)
1310 && S_GET_SEGMENT (fixP->fx_addsy) != seg)
1311 value += md_pcrel_from (fixP);
1315 value = fixP->fx_offset;
1316 if (fixP->fx_subsy != (symbolS *) NULL)
1318 if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
1319 value -= S_GET_VALUE (fixP->fx_subsy);
1322 /* We can't actually support subtracting a symbol. */
1323 as_bad_where (fixP->fx_file, fixP->fx_line,
1324 "expression too complex");
1329 if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
1332 const struct arc_operand *operand;
1336 opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
1338 operand = &arc_operands[opindex];
1340 /* Fetch the instruction, insert the fully resolved operand
1341 value, and stuff the instruction back again. */
1342 where = fixP->fx_frag->fr_literal + fixP->fx_where;
1343 if (target_big_endian)
1344 insn = bfd_getb32 ((unsigned char *) where);
1346 insn = bfd_getl32 ((unsigned char *) where);
1347 insn = arc_insert_operand (insn, operand, -1, NULL, (offsetT) value,
1348 fixP->fx_file, fixP->fx_line);
1349 if (target_big_endian)
1350 bfd_putb32 ((bfd_vma) insn, (unsigned char *) where);
1352 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
1356 /* Nothing else to do here. */
1360 /* Determine a BFD reloc value based on the operand information.
1361 We are only prepared to turn a few of the operands into relocs.
1362 !!! Note that we can't handle limm values here. Since we're using
1363 implicit addends the addend must be inserted into the instruction,
1364 however, the opcode insertion routines currently do nothing with
1366 if (operand->fmt == 'B')
1368 assert ((operand->flags & ARC_OPERAND_RELATIVE_BRANCH) != 0
1369 && operand->bits == 20
1370 && operand->shift == 7);
1371 fixP->fx_r_type = BFD_RELOC_ARC_B22_PCREL;
1373 else if (0 && operand->fmt == 'J')
1375 assert ((operand->flags & ARC_OPERAND_ABSOLUTE_BRANCH) != 0
1376 && operand->bits == 24
1377 && operand->shift == 32);
1378 fixP->fx_r_type = BFD_RELOC_ARC_B26;
1380 else if (0 && operand->fmt == 'L')
1382 assert ((operand->flags & ARC_OPERAND_LIMM) != 0
1383 && operand->bits == 32
1384 && operand->shift == 32);
1385 fixP->fx_r_type = BFD_RELOC_32;
1389 as_bad_where (fixP->fx_file, fixP->fx_line,
1390 "unresolved expression that must be resolved");
1397 switch (fixP->fx_r_type)
1400 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1404 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1408 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1413 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1417 case BFD_RELOC_ARC_B26:
1418 /* If !fixP->fx_done then `value' is an implicit addend.
1419 We must shift it right by 2 in this case as well because the
1420 linker performs the relocation and then adds this in (as opposed
1421 to adding this in and then shifting right by 2). */
1423 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
1431 fixP->fx_addnumber = value;
1436 /* Translate internal representation of relocation info to BFD target
1440 tc_gen_reloc (section, fixP)
1446 reloc = (arelent *) xmalloc (sizeof (arelent));
1448 reloc->sym_ptr_ptr = &fixP->fx_addsy->bsym;
1449 reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
1450 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
1451 if (reloc->howto == (reloc_howto_type *) NULL)
1453 as_bad_where (fixP->fx_file, fixP->fx_line,
1454 "internal error: can't export reloc type %d (`%s')",
1455 fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
1459 assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
1461 reloc->addend = fixP->fx_addnumber;
1469 /* Set the real name if the .rename pseudo-op was used.
1470 Return 1 if the symbol should not be included in the symbol table. */
1473 arc_frob_symbol (sym)
1476 if (sym->sy_tc.real_name != (char *) NULL)
1477 S_SET_NAME (sym, sym->sy_tc.real_name);