1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2005
3 Free Software Foundation, Inc.
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,
20 Boston, MA 02111-1307, USA. */
24 #include "safe-ctype.h"
27 #include "opcodes/fr30-desc.h"
28 #include "opcodes/fr30-opc.h"
31 /* Structure to hold all of the different components describing
32 an individual instruction. */
35 const CGEN_INSN
* insn
;
36 const CGEN_INSN
* orig_insn
;
39 CGEN_INSN_INT buffer
[1];
40 #define INSN_VALUE(buf) (*(buf))
42 unsigned char buffer
[CGEN_MAX_INSN_SIZE
];
43 #define INSN_VALUE(buf) (buf)
48 fixS
* fixups
[GAS_CGEN_MAX_FIXUPS
];
49 int indices
[MAX_OPERAND_INSTANCES
];
53 const char comment_chars
[] = ";";
54 const char line_comment_chars
[] = "#";
55 const char line_separator_chars
[] = "|";
56 const char EXP_CHARS
[] = "eE";
57 const char FLT_CHARS
[] = "dD";
59 #define FR30_SHORTOPTS ""
60 const char * md_shortopts
= FR30_SHORTOPTS
;
62 struct option md_longopts
[] =
64 {NULL
, no_argument
, NULL
, 0}
66 size_t md_longopts_size
= sizeof (md_longopts
);
69 md_parse_option (c
, arg
)
70 int c ATTRIBUTE_UNUSED
;
71 char *arg ATTRIBUTE_UNUSED
;
82 md_show_usage (stream
)
85 fprintf (stream
, _(" FR30 specific command line options:\n"));
88 /* The target specific pseudo-ops which we support. */
89 const pseudo_typeS md_pseudo_table
[] =
99 /* Initialize the `cgen' interface. */
101 /* Set the machine number and endian. */
102 gas_cgen_cpu_desc
= fr30_cgen_cpu_open (CGEN_CPU_OPEN_MACHS
, 0,
103 CGEN_CPU_OPEN_ENDIAN
,
106 fr30_cgen_init_asm (gas_cgen_cpu_desc
);
108 /* This is a callback from cgen to gas to parse operands. */
109 cgen_set_parse_operand_fn (gas_cgen_cpu_desc
, gas_cgen_parse_operand
);
116 static int last_insn_had_delay_slot
= 0;
120 /* Initialize GAS's cgen interface for a new instruction. */
121 gas_cgen_init_parse ();
123 insn
.insn
= fr30_cgen_assemble_insn
124 (gas_cgen_cpu_desc
, str
, & insn
.fields
, insn
.buffer
, & errmsg
);
132 /* Doesn't really matter what we pass for RELAX_P here. */
133 gas_cgen_finish_insn (insn
.insn
, insn
.buffer
,
134 CGEN_FIELDS_BITSIZE (& insn
.fields
), 1, NULL
);
136 /* Warn about invalid insns in delay slots. */
137 if (last_insn_had_delay_slot
138 && CGEN_INSN_ATTR_VALUE (insn
.insn
, CGEN_INSN_NOT_IN_DELAY_SLOT
))
139 as_warn (_("Instruction %s not allowed in a delay slot."),
140 CGEN_INSN_NAME (insn
.insn
));
142 last_insn_had_delay_slot
143 = CGEN_INSN_ATTR_VALUE (insn
.insn
, CGEN_INSN_DELAY_SLOT
);
146 /* The syntax in the manual says constants begin with '#'.
147 We just ignore it. */
150 md_operand (expressionP
)
151 expressionS
* expressionP
;
153 if (* input_line_pointer
== '#')
155 input_line_pointer
++;
156 expression (expressionP
);
161 md_section_align (segment
, size
)
165 int align
= bfd_get_section_alignment (stdoutput
, segment
);
166 return ((size
+ (1 << align
) - 1) & (-1 << align
));
170 md_undefined_symbol (name
)
171 char *name ATTRIBUTE_UNUSED
;
176 /* Interface to relax_segment. */
178 /* FIXME: Build table by hand, get it working, then machine generate. */
180 const relax_typeS md_relax_table
[] =
183 1) most positive reach of this state,
184 2) most negative reach of this state,
185 3) how many bytes this mode will add to the size of the current frag
186 4) which index into the table to try if we can't fit into this one. */
188 /* The first entry must be unused because an `rlx_more' value of zero ends
192 /* The displacement used by GAS is from the end of the 2 byte insn,
193 so we subtract 2 from the following. */
194 /* 16 bit insn, 8 bit disp -> 10 bit range.
195 This doesn't handle a branch in the right slot at the border:
196 the "& -4" isn't taken into account. It's not important enough to
197 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
199 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
200 /* 32 bit insn, 24 bit disp -> 26 bit range. */
201 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
202 /* Same thing, but with leading nop for alignment. */
203 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
206 /* Return an initial guess of the length by which a fragment must grow to
207 hold a branch to reach its destination.
208 Also updates fr_type/fr_subtype as necessary.
210 Called just before doing relaxation.
211 Any symbol that is now undefined will not become defined.
212 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
213 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
214 Although it may not be explicit in the frag, pretend fr_var starts with a
218 md_estimate_size_before_relax (fragP
, segment
)
222 /* The only thing we have to handle here are symbols outside of the
223 current segment. They may be undefined or in a different segment in
224 which case linker scripts may place them anywhere.
225 However, we can't finish the fragment here and emit the reloc as insn
226 alignment requirements may move the insn about. */
228 if (S_GET_SEGMENT (fragP
->fr_symbol
) != segment
)
230 /* The symbol is undefined in this segment.
231 Change the relaxation subtype to the max allowable and leave
232 all further handling to md_convert_frag. */
233 fragP
->fr_subtype
= 2;
236 const CGEN_INSN
* insn
;
239 /* Update the recorded insn.
240 Fortunately we don't have to look very far.
241 FIXME: Change this to record in the instruction the next higher
242 relaxable insn to use. */
243 for (i
= 0, insn
= fragP
->fr_cgen
.insn
; i
< 4; i
++, insn
++)
245 if ((strcmp (CGEN_INSN_MNEMONIC (insn
),
246 CGEN_INSN_MNEMONIC (fragP
->fr_cgen
.insn
))
248 && CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
))
254 fragP
->fr_cgen
.insn
= insn
;
259 /* Return the size of the variable part of the frag. */
260 return md_relax_table
[fragP
->fr_subtype
].rlx_length
;
263 /* *fragP has been relaxed to its final size, and now needs to have
264 the bytes inside it modified to conform to the new size.
266 Called after relaxation is finished.
267 fragP->fr_type == rs_machine_dependent.
268 fragP->fr_subtype is the subtype of what the address relaxed to. */
271 md_convert_frag (abfd
, sec
, fragP
)
272 bfd
*abfd ATTRIBUTE_UNUSED
;
273 segT sec ATTRIBUTE_UNUSED
;
274 fragS
*fragP ATTRIBUTE_UNUSED
;
278 /* Functions concerning relocs. */
280 /* The location from which a PC relative jump should be calculated,
281 given a PC relative reloc. */
284 md_pcrel_from_section (fixP
, sec
)
288 if (fixP
->fx_addsy
!= (symbolS
*) NULL
289 && (! S_IS_DEFINED (fixP
->fx_addsy
)
290 || S_GET_SEGMENT (fixP
->fx_addsy
) != sec
))
292 /* The symbol is undefined (or is defined but not in this section).
293 Let the linker figure it out. */
297 return (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & ~1;
300 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
301 Returns BFD_RELOC_NONE if no reloc type can be found.
302 *FIXP may be modified if desired. */
304 bfd_reloc_code_real_type
305 md_cgen_lookup_reloc (insn
, operand
, fixP
)
306 const CGEN_INSN
*insn ATTRIBUTE_UNUSED
;
307 const CGEN_OPERAND
*operand
;
310 switch (operand
->type
)
312 case FR30_OPERAND_LABEL9
: fixP
->fx_pcrel
= 1; return BFD_RELOC_FR30_9_PCREL
;
313 case FR30_OPERAND_LABEL12
: fixP
->fx_pcrel
= 1; return BFD_RELOC_FR30_12_PCREL
;
314 case FR30_OPERAND_DISP10
: return BFD_RELOC_FR30_10_IN_8
;
315 case FR30_OPERAND_DISP9
: return BFD_RELOC_FR30_9_IN_8
;
316 case FR30_OPERAND_DISP8
: return BFD_RELOC_FR30_8_IN_8
;
317 case FR30_OPERAND_UDISP6
: return BFD_RELOC_FR30_6_IN_4
;
318 case FR30_OPERAND_I8
: return BFD_RELOC_8
;
319 case FR30_OPERAND_I32
: return BFD_RELOC_FR30_48
;
320 case FR30_OPERAND_I20
: return BFD_RELOC_FR30_20
;
321 default : /* avoid -Wall warning */
325 return BFD_RELOC_NONE
;
328 /* Write a value out to the object file, using the appropriate endianness. */
331 md_number_to_chars (buf
, val
, n
)
336 number_to_chars_bigendian (buf
, val
, n
);
339 /* Turn a string in input_line_pointer into a floating point constant of type
340 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
341 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
344 /* Equal to MAX_PRECISION in atof-ieee.c */
345 #define MAX_LITTLENUMS 6
348 md_atof (type
, litP
, sizeP
)
355 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
374 /* FIXME: Some targets allow other format chars for bigger sizes here. */
378 return _("Bad call to md_atof()");
381 t
= atof_ieee (input_line_pointer
, type
, words
);
383 input_line_pointer
= t
;
384 * sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
386 for (i
= 0; i
< prec
; i
++)
388 md_number_to_chars (litP
, (valueT
) words
[i
],
389 sizeof (LITTLENUM_TYPE
));
390 litP
+= sizeof (LITTLENUM_TYPE
);
396 /* Worker function for fr30_is_colon_insn(). */
397 static char restore_colon
PARAMS ((int));
400 restore_colon (advance_i_l_p_by
)
401 int advance_i_l_p_by
;
405 /* Restore the colon, and advance input_line_pointer to
406 the end of the new symbol. */
407 * input_line_pointer
= ':';
408 input_line_pointer
+= advance_i_l_p_by
;
409 c
= * input_line_pointer
;
410 * input_line_pointer
= 0;
415 /* Determines if the symbol starting at START and ending in
416 a colon that was at the location pointed to by INPUT_LINE_POINTER
417 (but which has now been replaced bu a NUL) is in fact an
418 LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
419 If it is, then it restores the colon, advances INPUT_LINE_POINTER
420 to the real end of the instruction/symbol, and returns the character
421 that really terminated the symbol. Otherwise it returns 0. */
423 fr30_is_colon_insn (start
)
426 char * i_l_p
= input_line_pointer
;
428 /* Check to see if the symbol parsed so far is 'ldi' */
429 if ( (start
[0] != 'l' && start
[0] != 'L')
430 || (start
[1] != 'd' && start
[1] != 'D')
431 || (start
[2] != 'i' && start
[2] != 'I')
434 /* Nope - check to see a 'd' follows the colon. */
435 if ( (i_l_p
[1] == 'd' || i_l_p
[1] == 'D')
436 && (i_l_p
[2] == ' ' || i_l_p
[2] == '\t' || i_l_p
[2] == '\n'))
438 /* Yup - it might be delay slot instruction. */
440 static char * delay_insns
[] =
442 "call", "jmp", "ret", "bra", "bno",
443 "beq", "bne", "bc", "bnc", "bn",
444 "bp", "bv", "bnv", "blt", "bge",
445 "ble", "bgt", "bls", "bhi"
448 for (i
= sizeof (delay_insns
) / sizeof (delay_insns
[0]); i
--;)
450 char * insn
= delay_insns
[i
];
451 int len
= strlen (insn
);
453 if (start
[len
] != 0)
457 if (TOLOWER (start
[len
]) != insn
[len
])
461 return restore_colon (1);
465 /* Nope - it is a normal label. */
469 /* Check to see if the text following the colon is '8' */
470 if (i_l_p
[1] == '8' && (i_l_p
[2] == ' ' || i_l_p
[2] == '\t'))
471 return restore_colon (2);
473 /* Check to see if the text following the colon is '20' */
474 else if (i_l_p
[1] == '2' && i_l_p
[2] =='0' && (i_l_p
[3] == ' ' || i_l_p
[3] == '\t'))
475 return restore_colon (3);
477 /* Check to see if the text following the colon is '32' */
478 else if (i_l_p
[1] == '3' && i_l_p
[2] =='2' && (i_l_p
[3] == ' ' || i_l_p
[3] == '\t'))
479 return restore_colon (3);
485 fr30_fix_adjustable (fixP
)
488 /* We need the symbol name for the VTABLE entries */
489 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
490 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)