1 /* tc-arc.c -- Assembler for the ARC
2 Copyright (C) 1994, 1995 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, 675 Mass Ave, Cambridge, MA 02139, 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 int delay_slot_type
PARAMS ((arc_insn
));
35 static void arc_common
PARAMS ((int));
36 static void arc_cpu
PARAMS ((int));
37 /*static void arc_rename PARAMS ((int));*/
38 static int get_arc_exp_reloc_type
PARAMS ((int, int, expressionS
*,
41 const pseudo_typeS md_pseudo_table
[] =
43 { "align", s_align_bytes
, 0 }, /* Defaulting is invalid (0) */
44 { "common", arc_common
, 0 },
45 /*{ "hword", cons, 2 }, - already exists */
47 /*{ "xword", cons, 8 },*/
48 { "cpu", arc_cpu
, 0 },
49 /*{ "rename", arc_rename, 0 },*/
53 /* This array holds the chars that always start a comment. If the
54 pre-processor is disabled, these aren't very useful */
55 const char comment_chars
[] = "#;";
57 /* This array holds the chars that only start a comment at the beginning of
58 a line. If the line seems to have the form '# 123 filename'
59 .line and .file directives will appear in the pre-processed output */
60 /* Note that input_file.c hand checks for '#' at the beginning of the
61 first line of the input file. This is because the compiler outputs
62 #NO_APP at the beginning of its output. */
63 /* Also note that comments started like this one will always
64 work if '/' isn't otherwise defined. */
65 const char line_comment_chars
[] = "#";
67 const char line_separator_chars
[] = "";
69 /* Chars that can be used to separate mant from exp in floating point nums */
70 const char EXP_CHARS
[] = "eE";
72 /* Chars that mean this number is a floating point constant */
75 const char FLT_CHARS
[] = "rRsSfFdD";
78 extern int target_big_endian
;
79 const char *arc_target_format
= DEFAULT_TARGET_FORMAT
;
80 static int byte_order
;
82 /* One of bfd_mach_arc_xxx. */
83 static int arc_mach_type
= bfd_mach_arc_base
;
85 /* Non-zero if the cpu type has been explicitly specified. */
86 static int mach_type_specified_p
= 0;
88 /* Non-zero if opcode tables have been initialized.
89 A .cpu command must appear before any instructions. */
90 static int cpu_tables_init_p
= 0;
92 static struct hash_control
*arc_suffix_hash
= NULL
;
94 const char *md_shortopts
= "";
95 struct option md_longopts
[] =
97 #define OPTION_EB (OPTION_MD_BASE + 0)
98 {"EB", no_argument
, NULL
, OPTION_EB
},
99 #define OPTION_EL (OPTION_MD_BASE + 1)
100 {"EL", no_argument
, NULL
, OPTION_EL
},
101 { NULL
, no_argument
, NULL
, 0 }
103 size_t md_longopts_size
= sizeof (md_longopts
);
108 * Invocation line includes a switch not recognized by the base assembler.
109 * See if it's a processor-specific option.
113 md_parse_option (c
, arg
)
120 byte_order
= BIG_ENDIAN
;
121 arc_target_format
= "elf32-bigarc";
124 byte_order
= LITTLE_ENDIAN
;
125 arc_target_format
= "elf32-littlearc";
134 md_show_usage (stream
)
139 -EB generate big endian output\n\
140 -EL generate little endian output\n");
143 /* This function is called once, at assembler startup time. It should
144 set up all the tables, etc. that the MD part of the assembler will need.
145 Opcode selection is defered until later because we might see a .cpu
151 /* The endianness can be chosen "at the factory". */
152 target_big_endian
= byte_order
== BIG_ENDIAN
;
154 if (!bfd_set_arch_mach (stdoutput
, bfd_arch_arc
, arc_mach_type
))
155 as_warn ("could not set architecture and machine");
157 /* Assume the base cpu. This call is necessary because we need to
158 initialize `arc_operand_map' which may be needed before we see the
160 arc_opcode_init_tables (arc_get_opcode_mach (bfd_mach_arc_base
,
164 /* Initialize the various opcode and operand tables.
165 MACH is one of bfd_mach_arc_xxx. */
168 init_opcode_tables (mach
)
171 register unsigned int i
;
174 if ((arc_suffix_hash
= hash_new ()) == NULL
)
175 as_fatal ("virtual memory exhausted");
177 if (!bfd_set_arch_mach (stdoutput
, bfd_arch_arc
, mach
))
178 as_warn ("could not set architecture and machine");
180 /* This initializes a few things in arc-opc.c that we need.
181 This must be called before the various arc_xxx_supported fns. */
182 arc_opcode_init_tables (arc_get_opcode_mach (mach
, target_big_endian
));
184 /* Only put the first entry of each equivalently named suffix in the
187 for (i
= 0; i
< arc_suffixes_count
; i
++)
189 if (! arc_opval_supported (&arc_suffixes
[i
]))
191 if (strcmp (arc_suffixes
[i
].name
, last
) != 0)
192 hash_insert (arc_suffix_hash
, arc_suffixes
[i
].name
, (PTR
) (arc_suffixes
+ i
));
193 last
= arc_suffixes
[i
].name
;
196 /* Since registers don't have a prefix, we put them in the symbol table so
197 they can't be used as symbols. This also simplifies argument parsing as
198 we can let gas parse registers for us. The recorded register number is
199 the index in `arc_reg_names'. */
200 for (i
= 0; i
< arc_reg_names_count
; i
++)
202 if (! arc_opval_supported (&arc_reg_names
[i
]))
204 /* Use symbol_create here instead of symbol_new so we don't try to
205 output registers into the object file's symbol table. */
206 symbol_table_insert (symbol_create (arc_reg_names
[i
].name
, reg_section
,
207 i
, &zero_address_frag
));
210 /* Tell `s_cpu' it's too late. */
211 cpu_tables_init_p
= 1;
214 /* Insert an operand value into an instruction.
215 If REG is non-NULL, it is a register number and ignore VAL. */
218 arc_insert_operand (insn
, operand
, mods
, reg
, val
, file
, line
)
220 const struct arc_operand
*operand
;
222 const struct arc_operand_value
*reg
;
227 if (operand
->bits
!= 32)
232 if ((operand
->flags
& ARC_OPERAND_SIGNED
) != 0)
234 if ((operand
->flags
& ARC_OPERAND_SIGNOPT
) != 0)
235 max
= (1 << operand
->bits
) - 1;
237 max
= (1 << (operand
->bits
- 1)) - 1;
238 min
= - (1 << (operand
->bits
- 1));
242 max
= (1 << operand
->bits
) - 1;
246 if ((operand
->flags
& ARC_OPERAND_NEGATIVE
) != 0)
251 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
254 "operand out of range (%s not between %ld and %ld)";
257 sprint_value (buf
, test
);
258 if (file
== (char *) NULL
)
259 as_warn (err
, buf
, min
, max
);
261 as_warn_where (file
, line
, err
, buf
, min
, max
);
270 insn
= (*operand
->insert
) (insn
, operand
, mods
, reg
, (long) val
, &errmsg
);
271 if (errmsg
!= (const char *) NULL
)
275 insn
|= (((long) val
& ((1 << operand
->bits
) - 1))
281 /* Return the delay slot type for INSN (or -1 if it isn't a branch). */
282 /* ??? This is a quick hack. May wish to later generalize this concept
283 to record all suffixes the insn contains (for various warnings, etc.). */
286 delay_slot_type (arc_insn insn
)
288 if ((insn
>> 27) >= 4 && (insn
>> 27) <= 7)
289 return (insn
& 0x60) >> 5;
293 /* We need to keep a list of fixups. We can't simply generate them as
294 we go, because that would require us to first create the frag, and
295 that would screw up references to ``.''. */
299 /* index into `arc_operands' */
304 #define MAX_INSN_FIXUPS 5
306 /* This routine is called for each instruction to be assembled. */
312 const struct arc_opcode
*opcode
;
315 static int init_tables_p
= 0;
317 /* Opcode table initialization is deferred until here because we have to
318 wait for a possible .cpu command. */
321 init_opcode_tables (arc_mach_type
);
325 /* Skip leading white space. */
326 while (isspace (*str
))
329 /* The instructions are stored in lists hashed by the first letter (though
330 we needn't care how they're hashed). Get the first in the list. */
332 opcode
= arc_opcode_asm_list (str
);
334 /* Keep looking until we find a match. */
337 for ( ; opcode
!= NULL
; opcode
= ARC_OPCODE_NEXT_ASM (opcode
))
341 struct arc_fixup fixups
[MAX_INSN_FIXUPS
];
344 /* Is this opcode supported by the selected cpu? */
345 if (! arc_opcode_supported (opcode
))
348 /* Scan the syntax string. If it doesn't match, try the next one. */
350 arc_opcode_init_insert ();
351 insn
= opcode
->value
;
355 /* Used as a sanity check. If we need a limm reloc, make sure we ask
356 for an extra 4 bytes from frag_more. */
359 /* We don't check for (*str != '\0') here because we want to parse
360 any trailing fake arguments in the syntax string. */
361 for (str
= start
, syn
= opcode
->syntax
; *syn
!= '\0'; )
364 const struct arc_operand
*operand
;
366 /* Non operand chars must match exactly. */
367 if (*syn
!= '%' || *++syn
== '%')
369 /* Handle '+' specially as we want to allow "ld r0,[sp-4]". */
370 /* ??? The syntax has changed to [sp,-4]. */
371 if (0 && *syn
== '+' && *str
== '-')
373 /* Skip over syn's +, but leave str's - alone.
374 That makes the case identical to "ld r0,[sp+-4]". */
377 else if (*str
== *syn
)
389 /* We have an operand. Pick out any modifiers. */
391 while (ARC_MOD_P (arc_operands
[arc_operand_map
[*syn
]].flags
))
393 mods
|= arc_operands
[arc_operand_map
[*syn
]].flags
& ARC_MOD_BITS
;
396 operand
= arc_operands
+ arc_operand_map
[*syn
];
397 if (operand
->fmt
== 0)
398 as_fatal ("unknown syntax format character `%c'", *syn
);
400 if (operand
->flags
& ARC_OPERAND_FAKE
)
402 const char *errmsg
= NULL
;
405 insn
= (*operand
->insert
) (insn
, operand
, mods
, NULL
, 0, &errmsg
);
406 /* If we get an error, go on to try the next insn. */
412 /* Are we finished with suffixes? */
413 else if (!past_opcode_p
)
418 const struct arc_operand_value
*suf
,*suffix
,*suffix_end
;
420 if (!(operand
->flags
& ARC_OPERAND_SUFFIX
))
423 /* If we're at a space in the input string, we want to skip the
424 remaining suffixes. There may be some fake ones though, so
425 just go on to try the next one. */
433 if (mods
& ARC_MOD_DOT
)
441 /* This can happen in "b.nd foo" and we're currently looking
442 for "%q" (ie: a condition code suffix). */
450 /* Pick the suffix out and look it up via the hash table. */
451 for (t
= s
; *t
&& isalpha (*t
); ++t
)
455 suf
= hash_find (arc_suffix_hash
, s
);
459 /* This can happen in "blle foo" and we're currently using
460 the template "b%q%.n %j". The "bl" insn occurs later in
461 the table so "lle" isn't an illegal suffix. */
465 /* Is it the right type? Note that the same character is used
466 several times, so we have to examine all of them. This is
467 relatively efficient as equivalent entries are kept
468 together. If it's not the right type, don't increment `str'
469 so we try the next one in the series. */
471 suffix_end
= arc_suffixes
+ arc_suffixes_count
;
473 suffix
< suffix_end
&& strcmp (suffix
->name
, suf
->name
) == 0;
476 if (arc_operands
[suffix
->type
].fmt
== *syn
)
478 /* Insert the suffix's value into the insn. */
480 insn
= (*operand
->insert
) (insn
, operand
,
481 mods
, NULL
, suffix
->value
,
484 insn
|= suffix
->value
<< operand
->shift
;
493 /* There's nothing to do except go on to try the next one.
494 ??? This test can be deleted when we're done. */
498 /* This is either a register or an expression of some kind. */
502 const struct arc_operand_value
*reg
= NULL
;
506 if (operand
->flags
& ARC_OPERAND_SUFFIX
)
509 /* Is there anything left to parse?
510 We don't check for this at the top because we want to parse
511 any trailing fake arguments in the syntax string. */
515 /* Is this a syntax character? Eg: is there a '[' present when
516 there shouldn't be? */
518 /* '.' as in ".LLC0" */
520 /* '_' as in "_print" */
522 /* '-' as in "[fp,-4]" */
524 /* '%' as in "%ia(_func)" */
529 /* Parse the operand. */
530 hold
= input_line_pointer
;
531 input_line_pointer
= str
;
533 str
= input_line_pointer
;
534 input_line_pointer
= hold
;
536 if (exp
.X_op
== O_illegal
)
537 as_bad ("illegal operand");
538 else if (exp
.X_op
== O_absent
)
539 as_bad ("missing operand");
540 else if (exp
.X_op
== O_constant
)
542 value
= exp
.X_add_number
;
544 else if (exp
.X_op
== O_register
)
546 reg
= arc_reg_names
+ exp
.X_add_number
;
550 /* We need to generate a fixup for this expression. */
551 if (fc
>= MAX_INSN_FIXUPS
)
552 as_fatal ("too many fixups");
553 fixups
[fc
].exp
= exp
;
555 /* If this is a register constant (IE: one whose
556 register value gets stored as 61-63) then this
557 must be a limm. We don't support shimm relocs. */
558 /* ??? This bit could use some cleaning up.
559 Referencing the format chars like this goes
561 #define IS_REG_OPERAND(o) ((o) == 'a' || (o) == 'b' || (o) == 'c')
562 if (IS_REG_OPERAND (*syn
))
566 fixups
[fc
].opindex
= arc_operand_map
['L'];
568 /* Tell insert_reg we need a limm. This is
569 needed because the value at this point is
571 /* ??? We need a cleaner interface than this. */
572 (*arc_operands
[arc_operand_map
['Q']].insert
)
573 (insn
, operand
, mods
, reg
, 0L, &junk
);
576 fixups
[fc
].opindex
= arc_operand_map
[*syn
];
581 /* Insert the register or expression into the instruction. */
584 const char *errmsg
= NULL
;
585 insn
= (*operand
->insert
) (insn
, operand
, mods
,
586 reg
, (long) value
, &errmsg
);
588 if (errmsg
!= (const char *) NULL
)
591 /* FIXME: We want to try shimm insns for limm ones. But if
592 the constant won't fit, we must go on to try the next
593 possibility. Where do we issue warnings for constants
594 that are too big then? At present, we'll flag the insn
595 as unrecognizable! Maybe have the "bad instruction"
596 error message include our `errmsg'? */
597 if (errmsg
!= (const char *) NULL
)
601 insn
|= (value
& ((1 << operand
->bits
) - 1)) << operand
->shift
;
607 /* If we're at the end of the syntax string, we're done. */
614 /* For the moment we assume a valid `str' can only contain blanks
615 now. IE: We needn't try again with a longer version of the
616 insn and it is assumed that longer versions of insns appear
617 before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
619 while (isspace (*str
))
623 as_bad ("junk at end of line: `%s'", str
);
625 /* Is there a limm value? */
626 limm_p
= arc_opcode_limm_p (&limm
);
628 /* Putting an insn with a limm value in a delay slot is supposed to
629 be legal, but let's warn the user anyway. Ditto for 8 byte jumps
632 static int in_delay_slot_p
= 0;
633 int this_insn_delay_slot_type
= delay_slot_type (insn
);
635 if (in_delay_slot_p
&& limm_p
)
636 as_warn ("8 byte instruction in delay slot");
637 if (this_insn_delay_slot_type
> 0 && limm_p
)
638 as_warn ("8 byte jump instruction with delay slot");
639 in_delay_slot_p
= (this_insn_delay_slot_type
> 0) && !limm_p
;
642 /* Write out the instruction.
643 It is important to fetch enough space in one call to `frag_more'.
644 We use (f - frag_now->fr_literal) to compute where we are and we
645 don't want frag_now to change between calls. */
649 md_number_to_chars (f
, insn
, 4);
650 md_number_to_chars (f
+ 4, limm
, 4);
652 else if (limm_reloc_p
)
654 /* We need a limm reloc, but the tables think we don't. */
660 md_number_to_chars (f
, insn
, 4);
663 /* Create any fixups. */
664 for (i
= 0; i
< fc
; ++i
)
666 int op_type
, reloc_type
;
668 const struct arc_operand
*operand
;
670 /* Create a fixup for this operand.
671 At this point we do not use a bfd_reloc_code_real_type for
672 operands residing in the insn, but instead just use the
673 operand index. This lets us easily handle fixups for any
674 operand type, although that is admittedly not a very exciting
675 feature. We pick a BFD reloc type in md_apply_fix.
677 Limm values (4 byte immediate "constants") must be treated
678 normally because they're not part of the actual insn word
679 and thus the insertion routines don't handle them. */
681 if (arc_operands
[fixups
[i
].opindex
].flags
& ARC_OPERAND_LIMM
)
683 op_type
= fixups
[i
].opindex
;
684 /* FIXME: can we add this data to the operand table? */
685 if (op_type
== arc_operand_map
['L'])
686 reloc_type
= BFD_RELOC_32
;
687 else if (op_type
== arc_operand_map
['J'])
688 reloc_type
= BFD_RELOC_ARC_B26
;
691 reloc_type
= get_arc_exp_reloc_type (1, reloc_type
,
697 op_type
= get_arc_exp_reloc_type (0, fixups
[i
].opindex
,
698 &fixups
[i
].exp
, &exptmp
);
699 reloc_type
= op_type
+ (int) BFD_RELOC_UNUSED
;
701 operand
= &arc_operands
[op_type
];
702 fix_new_exp (frag_now
,
703 ((f
- frag_now
->fr_literal
)
704 + (operand
->flags
& ARC_OPERAND_LIMM
? 4 : 0)), 4,
706 (operand
->flags
& ARC_OPERAND_RELATIVE_BRANCH
) != 0,
707 (bfd_reloc_code_real_type
) reloc_type
);
714 /* Try the next entry. */
717 as_bad ("bad instruction `%s'", start
);
720 /* ??? This was copied from tc-sparc.c, I think. Is it necessary? */
732 name
= input_line_pointer
;
733 c
= get_symbol_end ();
734 /* just after name is now '\0' */
735 p
= input_line_pointer
;
738 if (*input_line_pointer
!= ',')
740 as_bad ("expected comma after symbol-name");
741 ignore_rest_of_line ();
744 input_line_pointer
++; /* skip ',' */
745 if ((temp
= get_absolute_expression ()) < 0)
747 as_bad (".COMMon length (%d.) <0! Ignored.", temp
);
748 ignore_rest_of_line ();
753 symbolP
= symbol_find_or_make (name
);
755 if (S_IS_DEFINED (symbolP
))
757 as_bad ("ignoring attempt to re-define symbol");
758 ignore_rest_of_line ();
761 if (S_GET_VALUE (symbolP
) != 0)
763 if (S_GET_VALUE (symbolP
) != size
)
765 as_warn ("Length of .comm \"%s\" is already %ld. Not changed to %d.",
766 S_GET_NAME (symbolP
), (long) S_GET_VALUE (symbolP
), size
);
769 assert (symbolP
->sy_frag
== &zero_address_frag
);
770 if (*input_line_pointer
!= ',')
772 as_bad ("expected comma after common length");
773 ignore_rest_of_line ();
776 input_line_pointer
++;
778 if (*input_line_pointer
!= '"')
780 temp
= get_absolute_expression ();
784 as_warn ("Common alignment negative; 0 assumed");
795 old_subsec
= now_subseg
;
797 record_alignment (bss_section
, align
);
798 subseg_set (bss_section
, 0);
800 frag_align (align
, 0);
801 if (S_GET_SEGMENT (symbolP
) == bss_section
)
802 symbolP
->sy_frag
->fr_symbol
= 0;
803 symbolP
->sy_frag
= frag_now
;
804 p
= frag_var (rs_org
, 1, 1, (relax_substateT
) 0, symbolP
, size
,
807 S_SET_SEGMENT (symbolP
, bss_section
);
808 S_CLEAR_EXTERNAL (symbolP
);
809 subseg_set (old_sec
, old_subsec
);
814 S_SET_VALUE (symbolP
, (valueT
) size
);
815 S_SET_ALIGN (symbolP
, temp
);
816 S_SET_EXTERNAL (symbolP
);
817 /* should be common, but this is how gas does it for now */
818 S_SET_SEGMENT (symbolP
, bfd_und_section_ptr
);
823 input_line_pointer
++;
824 /* ??? Some say data, some say bss. */
825 if (strncmp (input_line_pointer
, ".bss\"", 5)
826 && strncmp (input_line_pointer
, ".data\"", 6))
828 input_line_pointer
--;
829 goto bad_common_segment
;
831 while (*input_line_pointer
++ != '"')
833 goto allocate_common
;
835 demand_empty_rest_of_line ();
840 p
= input_line_pointer
;
841 while (*p
&& *p
!= '\n')
845 as_bad ("bad .common segment %s", input_line_pointer
+ 1);
847 input_line_pointer
= p
;
848 ignore_rest_of_line ();
853 /* Select the cpu we're assembling for. */
863 /* If an instruction has already been seen, it's too late. */
864 if (cpu_tables_init_p
)
866 as_bad (".cpu command must appear before any instructions");
867 ignore_rest_of_line ();
871 cpu
= input_line_pointer
;
872 c
= get_symbol_end ();
873 mach
= arc_get_mach (cpu
);
874 *input_line_pointer
= c
;
878 demand_empty_rest_of_line ();
880 /* The cpu may have been selected on the command line.
881 The choices must match. */
882 /* ??? This was a command line option early on. It's gone now, but
884 if (mach_type_specified_p
&& mach
!= arc_mach_type
)
885 as_bad (".cpu conflicts with previous value");
888 arc_mach_type
= mach
;
889 mach_type_specified_p
= 1;
890 if (!bfd_set_arch_mach (stdoutput
, bfd_arch_arc
, mach
))
891 as_warn ("could not set architecture and machine");
896 as_bad ("bad .cpu op");
897 ignore_rest_of_line ();
901 /* The .rename pseudo-op. This is used by gcc to implement
902 -mmangle-cpu-libgcc. */
913 name
= input_line_pointer
;
914 c
= get_symbol_end ();
915 sym
= symbol_find_or_make (name
);
916 *input_line_pointer
= c
;
918 if (*input_line_pointer
!= ',')
920 as_bad ("missing rename string");
921 ignore_rest_of_line ();
924 ++input_line_pointer
;
927 name
= input_line_pointer
;
928 c
= get_symbol_end ();
931 *input_line_pointer
= c
;
932 as_bad ("invalid symbol to rename to");
933 ignore_rest_of_line ();
936 new = (char *) xmalloc (strlen (name
) + 1);
938 *input_line_pointer
= c
;
939 sym
->sy_tc
.real_name
= new;
941 demand_empty_rest_of_line ();
945 /* Turn a string in input_line_pointer into a floating point constant of type
946 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
947 emitted is stored in *sizeP.
948 An error message is returned, or NULL on OK. */
950 /* Equal to MAX_PRECISION in atof-ieee.c */
951 #define MAX_LITTLENUMS 6
954 md_atof (type
, litP
, sizeP
)
960 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
961 LITTLENUM_TYPE
*wordP
;
979 return "bad call to md_atof";
982 t
= atof_ieee (input_line_pointer
, type
, words
);
984 input_line_pointer
= t
;
985 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
986 for (wordP
= words
; prec
--;)
988 md_number_to_chars (litP
, (valueT
) (*wordP
++), sizeof (LITTLENUM_TYPE
));
989 litP
+= sizeof (LITTLENUM_TYPE
);
995 /* Write a value out to the object file, using the appropriate
999 md_number_to_chars (buf
, val
, n
)
1004 if (target_big_endian
)
1005 number_to_chars_bigendian (buf
, val
, n
);
1007 number_to_chars_littleendian (buf
, val
, n
);
1010 /* Round up a section size to the appropriate boundary. */
1013 md_section_align (segment
, size
)
1017 int align
= bfd_get_section_alignment (stdoutput
, segment
);
1019 return ((size
+ (1 << align
) - 1) & (-1 << align
));
1022 /* We don't have any form of relaxing. */
1025 md_estimate_size_before_relax (fragp
, seg
)
1032 /* Convert a machine dependent frag. We never generate these. */
1035 md_convert_frag (abfd
, sec
, fragp
)
1043 /* Parse an operand that is machine-specific.
1045 The ARC has a special %-op to adjust addresses so they're usable in
1046 branches. The "st" is short for the STatus register.
1047 ??? Later expand this to take a flags value too.
1049 ??? We can't create new expression types so we map the %-op's onto the
1050 existing syntax. This means that the user could use the chosen syntax
1051 to achieve the same effect. Perhaps put a special cookie in X_add_number
1052 to mark the expression as special. */
1055 md_operand (expressionP
)
1056 expressionS
*expressionP
;
1058 char *p
= input_line_pointer
;
1060 if (*p
== '%' && strncmp (p
, "%st(", 4) == 0)
1062 input_line_pointer
+= 4;
1063 expression (expressionP
);
1064 if (*input_line_pointer
!= ')')
1066 as_bad ("missing ')' in %-op");
1069 ++input_line_pointer
;
1070 if (expressionP
->X_op
== O_symbol
1071 && expressionP
->X_add_number
== 0
1072 /* I think this test is unnecessary but just as a sanity check... */
1073 && expressionP
->X_op_symbol
== NULL
)
1077 expressionP
->X_op
= O_right_shift
;
1078 two
.X_op
= O_constant
;
1079 two
.X_add_symbol
= two
.X_op_symbol
= NULL
;
1080 two
.X_add_number
= 2;
1081 expressionP
->X_op_symbol
= make_expr_symbol (&two
);
1083 /* allow %st(sym1-sym2) */
1084 else if (expressionP
->X_op
== O_subtract
1085 && expressionP
->X_add_symbol
!= NULL
1086 && expressionP
->X_op_symbol
!= NULL
1087 && expressionP
->X_add_number
== 0)
1091 expressionP
->X_add_symbol
= make_expr_symbol (expressionP
);
1092 expressionP
->X_op
= O_right_shift
;
1093 two
.X_op
= O_constant
;
1094 two
.X_add_symbol
= two
.X_op_symbol
= NULL
;
1095 two
.X_add_number
= 2;
1096 expressionP
->X_op_symbol
= make_expr_symbol (&two
);
1100 as_bad ("expression too complex for %%st");
1106 /* We have no need to default values of symbols.
1107 We could catch register names here, but that is handled by inserting
1108 them all in the symbol table to begin with. */
1111 md_undefined_symbol (name
)
1117 /* Functions concerning expressions. */
1119 /* Parse a .byte, .word, etc. expression.
1121 Values for the status register are specified with %st(label).
1122 `label' will be right shifted by 2. */
1125 arc_parse_cons_expression (exp
, nbytes
)
1132 /* Record a fixup for a cons expression. */
1135 arc_cons_fix_new (frag
, where
, nbytes
, exp
)
1146 /* This may be a special ARC reloc (eg: %st()). */
1147 reloc_type
= get_arc_exp_reloc_type (1, BFD_RELOC_32
, exp
, &exptmp
);
1148 fix_new_exp (frag
, where
, nbytes
, &exptmp
, 0, reloc_type
);
1152 fix_new_exp (frag
, where
, nbytes
, exp
, 0,
1153 nbytes
== 2 ? BFD_RELOC_16
1154 : nbytes
== 8 ? BFD_RELOC_64
1159 /* Functions concerning relocs. */
1161 /* The location from which a PC relative jump should be calculated,
1162 given a PC relative reloc. */
1165 md_pcrel_from (fixP
)
1168 if (fixP
->fx_addsy
!= (symbolS
*) NULL
1169 && ! S_IS_DEFINED (fixP
->fx_addsy
))
1170 /* Return offset from PC to delay slot. Offsets are from there. */
1173 /* Return the address of the delay slot. */
1174 return fixP
->fx_frag
->fr_address
+ fixP
->fx_where
+ fixP
->fx_size
;
1177 /* Compute the reloc type of an expression.
1178 The possibly modified expression is stored in EXPNEW.
1180 This is used to convert the expressions generated by the %-op's into
1181 the appropriate operand type. It is called for both data in instructions
1182 (operands) and data outside instructions (variables, debugging info, etc.).
1184 Currently supported %-ops:
1186 %st(symbol): represented as "symbol >> 2"
1187 "st" is short for STatus as in the status register (pc)
1189 DEFAULT_TYPE is the type to use if no special processing is required.
1191 DATA_P is non-zero for data or limm values, zero for insn operands.
1192 Remember that the opcode "insertion fns" cannot be used on data, they're
1193 only for inserting operands into insns. They also can't be used for limm
1194 values as the insertion routines don't handle limm values. When called for
1195 insns we return fudged reloc types (real_value - BFD_RELOC_UNUSED). When
1196 called for data or limm values we use real reloc types. */
1199 get_arc_exp_reloc_type (data_p
, default_type
, exp
, expnew
)
1203 expressionS
*expnew
;
1205 /* If the expression is "symbol >> 2" we must change it to just "symbol",
1206 as fix_new_exp can't handle it. Similarily for (symbol - symbol) >> 2.
1207 That's ok though. What's really going on here is that we're using
1208 ">> 2" as a special syntax for specifying BFD_RELOC_ARC_B26. */
1210 if (exp
->X_op
== O_right_shift
1211 && exp
->X_op_symbol
!= NULL
1212 && exp
->X_op_symbol
->sy_value
.X_op
== O_constant
1213 && exp
->X_op_symbol
->sy_value
.X_add_number
== 2
1214 && exp
->X_add_number
== 0)
1216 if (exp
->X_add_symbol
!= NULL
1217 && (exp
->X_add_symbol
->sy_value
.X_op
== O_constant
1218 || exp
->X_add_symbol
->sy_value
.X_op
== O_symbol
))
1221 expnew
->X_op
= O_symbol
;
1222 expnew
->X_op_symbol
= NULL
;
1223 return data_p
? BFD_RELOC_ARC_B26
: arc_operand_map
['J'];
1225 else if (exp
->X_add_symbol
!= NULL
1226 && exp
->X_add_symbol
->sy_value
.X_op
== O_subtract
)
1228 *expnew
= exp
->X_add_symbol
->sy_value
;
1229 return data_p
? BFD_RELOC_ARC_B26
: arc_operand_map
['J'];
1234 return default_type
;
1237 /* Apply a fixup to the object code. This is called for all the
1238 fixups we generated by the call to fix_new_exp, above. In the call
1239 above we used a reloc code which was the largest legal reloc code
1240 plus the operand index. Here we undo that to recover the operand
1241 index. At this point all symbol values should be fully resolved,
1242 and we attempt to completely resolve the reloc. If we can not do
1243 that, we determine the correct reloc code and put it back in the fixup. */
1246 md_apply_fix (fixP
, valueP
)
1250 /*char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;*/
1253 /* FIXME FIXME FIXME: The value we are passed in *valueP includes
1254 the symbol values. Since we are using BFD_ASSEMBLER, if we are
1255 doing this relocation the code in write.c is going to call
1256 bfd_perform_relocation, which is also going to use the symbol
1257 value. That means that if the reloc is fully resolved we want to
1258 use *valueP since bfd_perform_relocation is not being used.
1259 However, if the reloc is not fully resolved we do not want to use
1260 *valueP, and must use fx_offset instead. However, if the reloc
1261 is PC relative, we do want to use *valueP since it includes the
1262 result of md_pcrel_from. This is confusing. */
1264 if (fixP
->fx_addsy
== (symbolS
*) NULL
)
1269 else if (fixP
->fx_pcrel
)
1273 value
= fixP
->fx_offset
;
1274 if (fixP
->fx_subsy
!= (symbolS
*) NULL
)
1276 if (S_GET_SEGMENT (fixP
->fx_subsy
) == absolute_section
)
1277 value
-= S_GET_VALUE (fixP
->fx_subsy
);
1280 /* We can't actually support subtracting a symbol. */
1281 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1282 "expression too complex");
1287 if ((int) fixP
->fx_r_type
>= (int) BFD_RELOC_UNUSED
)
1290 const struct arc_operand
*operand
;
1294 opindex
= (int) fixP
->fx_r_type
- (int) BFD_RELOC_UNUSED
;
1296 operand
= &arc_operands
[opindex
];
1298 /* Fetch the instruction, insert the fully resolved operand
1299 value, and stuff the instruction back again. */
1300 where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1301 if (target_big_endian
)
1302 insn
= bfd_getb32 ((unsigned char *) where
);
1304 insn
= bfd_getl32 ((unsigned char *) where
);
1305 insn
= arc_insert_operand (insn
, operand
, -1, NULL
, (offsetT
) value
,
1306 fixP
->fx_file
, fixP
->fx_line
);
1307 if (target_big_endian
)
1308 bfd_putb32 ((bfd_vma
) insn
, (unsigned char *) where
);
1310 bfd_putl32 ((bfd_vma
) insn
, (unsigned char *) where
);
1314 /* Nothing else to do here. */
1318 /* Determine a BFD reloc value based on the operand information.
1319 We are only prepared to turn a few of the operands into relocs.
1320 !!! Note that we can't handle limm values here. Since we're using
1321 implicit addends the addend must be inserted into the instruction,
1322 however, the opcode insertion routines currently do nothing with
1324 if (operand
->fmt
== 'B')
1326 assert ((operand
->flags
& ARC_OPERAND_RELATIVE_BRANCH
) != 0
1327 && operand
->bits
== 20
1328 && operand
->shift
== 7);
1329 fixP
->fx_r_type
= BFD_RELOC_ARC_B22_PCREL
;
1331 else if (0 && operand
->fmt
== 'J')
1333 assert ((operand
->flags
& ARC_OPERAND_ABSOLUTE_BRANCH
) != 0
1334 && operand
->bits
== 24
1335 && operand
->shift
== 32);
1336 fixP
->fx_r_type
= BFD_RELOC_ARC_B26
;
1338 else if (0 && operand
->fmt
== 'L')
1340 assert ((operand
->flags
& ARC_OPERAND_LIMM
) != 0
1341 && operand
->bits
== 32
1342 && operand
->shift
== 32);
1343 fixP
->fx_r_type
= BFD_RELOC_32
;
1347 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1348 "unresolved expression that must be resolved");
1355 switch (fixP
->fx_r_type
)
1358 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1362 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1366 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1371 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1375 case BFD_RELOC_ARC_B26
:
1376 /* If !fixP->fx_done then `value' is an implicit addend.
1377 We must shift it right by 2 in this case as well because the
1378 linker performs the relocation and then adds this in (as opposed
1379 to adding this in and then shifting right by 2). */
1381 md_number_to_chars (fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
,
1389 fixP
->fx_addnumber
= value
;
1394 /* Translate internal representation of relocation info to BFD target
1398 tc_gen_reloc (section
, fixP
)
1404 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
1406 reloc
->sym_ptr_ptr
= &fixP
->fx_addsy
->bsym
;
1407 reloc
->address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
1408 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
1409 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
1411 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
1412 "internal error: can't export reloc type %d (`%s')",
1413 fixP
->fx_r_type
, bfd_get_reloc_code_name (fixP
->fx_r_type
));
1416 reloc
->addend
= fixP
->fx_addnumber
;
1418 assert (!fixP
->fx_pcrel
== !reloc
->howto
->pc_relative
);
1426 /* Set the real name if the .rename pseudo-op was used.
1427 Return 1 if the symbol should not be included in the symbol table. */
1430 arc_frob_symbol (sym
)
1433 if (sym
->sy_tc
.real_name
!= (char *) NULL
)
1434 S_SET_NAME (sym
, sym
->sy_tc
.real_name
);