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, 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, 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 (int c ATTRIBUTE_UNUSED
,
70 char *arg ATTRIBUTE_UNUSED
)
81 md_show_usage (FILE * stream
)
83 fprintf (stream
, _(" FR30 specific command line options:\n"));
86 /* The target specific pseudo-ops which we support. */
87 const pseudo_typeS md_pseudo_table
[] =
97 /* Initialize the `cgen' interface. */
99 /* Set the machine number and endian. */
100 gas_cgen_cpu_desc
= fr30_cgen_cpu_open (CGEN_CPU_OPEN_MACHS
, 0,
101 CGEN_CPU_OPEN_ENDIAN
,
104 fr30_cgen_init_asm (gas_cgen_cpu_desc
);
106 /* This is a callback from cgen to gas to parse operands. */
107 cgen_set_parse_operand_fn (gas_cgen_cpu_desc
, gas_cgen_parse_operand
);
111 md_assemble (char *str
)
113 static int last_insn_had_delay_slot
= 0;
117 /* Initialize GAS's cgen interface for a new instruction. */
118 gas_cgen_init_parse ();
120 insn
.insn
= fr30_cgen_assemble_insn
121 (gas_cgen_cpu_desc
, str
, & insn
.fields
, insn
.buffer
, & errmsg
);
129 /* Doesn't really matter what we pass for RELAX_P here. */
130 gas_cgen_finish_insn (insn
.insn
, insn
.buffer
,
131 CGEN_FIELDS_BITSIZE (& insn
.fields
), 1, NULL
);
133 /* Warn about invalid insns in delay slots. */
134 if (last_insn_had_delay_slot
135 && CGEN_INSN_ATTR_VALUE (insn
.insn
, CGEN_INSN_NOT_IN_DELAY_SLOT
))
136 as_warn (_("Instruction %s not allowed in a delay slot."),
137 CGEN_INSN_NAME (insn
.insn
));
139 last_insn_had_delay_slot
140 = CGEN_INSN_ATTR_VALUE (insn
.insn
, CGEN_INSN_DELAY_SLOT
);
143 /* The syntax in the manual says constants begin with '#'.
144 We just ignore it. */
147 md_operand (expressionS
* expressionP
)
149 if (* input_line_pointer
== '#')
151 input_line_pointer
++;
152 expression (expressionP
);
157 md_section_align (segT segment
, valueT size
)
159 int align
= bfd_get_section_alignment (stdoutput
, segment
);
161 return ((size
+ (1 << align
) - 1) & (-1 << align
));
165 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
170 /* Interface to relax_segment. */
172 /* FIXME: Build table by hand, get it working, then machine generate. */
174 const relax_typeS md_relax_table
[] =
177 1) most positive reach of this state,
178 2) most negative reach of this state,
179 3) how many bytes this mode will add to the size of the current frag
180 4) which index into the table to try if we can't fit into this one. */
182 /* The first entry must be unused because an `rlx_more' value of zero ends
186 /* The displacement used by GAS is from the end of the 2 byte insn,
187 so we subtract 2 from the following. */
188 /* 16 bit insn, 8 bit disp -> 10 bit range.
189 This doesn't handle a branch in the right slot at the border:
190 the "& -4" isn't taken into account. It's not important enough to
191 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
193 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
194 /* 32 bit insn, 24 bit disp -> 26 bit range. */
195 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
196 /* Same thing, but with leading nop for alignment. */
197 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
200 /* Return an initial guess of the length by which a fragment must grow to
201 hold a branch to reach its destination.
202 Also updates fr_type/fr_subtype as necessary.
204 Called just before doing relaxation.
205 Any symbol that is now undefined will not become defined.
206 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
207 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
208 Although it may not be explicit in the frag, pretend fr_var starts with a
212 md_estimate_size_before_relax (fragS
* fragP
, segT segment
)
214 /* The only thing we have to handle here are symbols outside of the
215 current segment. They may be undefined or in a different segment in
216 which case linker scripts may place them anywhere.
217 However, we can't finish the fragment here and emit the reloc as insn
218 alignment requirements may move the insn about. */
220 if (S_GET_SEGMENT (fragP
->fr_symbol
) != segment
)
222 /* The symbol is undefined in this segment.
223 Change the relaxation subtype to the max allowable and leave
224 all further handling to md_convert_frag. */
225 fragP
->fr_subtype
= 2;
228 const CGEN_INSN
* insn
;
231 /* Update the recorded insn.
232 Fortunately we don't have to look very far.
233 FIXME: Change this to record in the instruction the next higher
234 relaxable insn to use. */
235 for (i
= 0, insn
= fragP
->fr_cgen
.insn
; i
< 4; i
++, insn
++)
237 if ((strcmp (CGEN_INSN_MNEMONIC (insn
),
238 CGEN_INSN_MNEMONIC (fragP
->fr_cgen
.insn
))
240 && CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAXED
))
246 fragP
->fr_cgen
.insn
= insn
;
251 /* Return the size of the variable part of the frag. */
252 return md_relax_table
[fragP
->fr_subtype
].rlx_length
;
255 /* *fragP has been relaxed to its final size, and now needs to have
256 the bytes inside it modified to conform to the new size.
258 Called after relaxation is finished.
259 fragP->fr_type == rs_machine_dependent.
260 fragP->fr_subtype is the subtype of what the address relaxed to. */
263 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
,
264 segT sec ATTRIBUTE_UNUSED
,
265 fragS
*fragP ATTRIBUTE_UNUSED
)
269 /* Functions concerning relocs. */
271 /* The location from which a PC relative jump should be calculated,
272 given a PC relative reloc. */
275 md_pcrel_from_section (fixS
* fixP
, segT sec
)
277 if (fixP
->fx_addsy
!= (symbolS
*) NULL
278 && (! S_IS_DEFINED (fixP
->fx_addsy
)
279 || S_GET_SEGMENT (fixP
->fx_addsy
) != sec
))
280 /* The symbol is undefined (or is defined but not in this section).
281 Let the linker figure it out. */
284 return (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & ~1;
287 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
288 Returns BFD_RELOC_NONE if no reloc type can be found.
289 *FIXP may be modified if desired. */
291 bfd_reloc_code_real_type
292 md_cgen_lookup_reloc (const CGEN_INSN
*insn ATTRIBUTE_UNUSED
,
293 const CGEN_OPERAND
*operand
,
296 switch (operand
->type
)
298 case FR30_OPERAND_LABEL9
: fixP
->fx_pcrel
= 1; return BFD_RELOC_FR30_9_PCREL
;
299 case FR30_OPERAND_LABEL12
: fixP
->fx_pcrel
= 1; return BFD_RELOC_FR30_12_PCREL
;
300 case FR30_OPERAND_DISP10
: return BFD_RELOC_FR30_10_IN_8
;
301 case FR30_OPERAND_DISP9
: return BFD_RELOC_FR30_9_IN_8
;
302 case FR30_OPERAND_DISP8
: return BFD_RELOC_FR30_8_IN_8
;
303 case FR30_OPERAND_UDISP6
: return BFD_RELOC_FR30_6_IN_4
;
304 case FR30_OPERAND_I8
: return BFD_RELOC_8
;
305 case FR30_OPERAND_I32
: return BFD_RELOC_FR30_48
;
306 case FR30_OPERAND_I20
: return BFD_RELOC_FR30_20
;
307 default : /* Avoid -Wall warning. */
311 return BFD_RELOC_NONE
;
314 /* Write a value out to the object file, using the appropriate endianness. */
317 md_number_to_chars (char * buf
, valueT val
, int n
)
319 number_to_chars_bigendian (buf
, val
, n
);
322 /* Turn a string in input_line_pointer into a floating point constant of type
323 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
324 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
327 /* Equal to MAX_PRECISION in atof-ieee.c. */
328 #define MAX_LITTLENUMS 6
331 md_atof (int type
, char * litP
, int * sizeP
)
335 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
354 /* FIXME: Some targets allow other format chars for bigger sizes here. */
358 return _("Bad call to md_atof()");
361 t
= atof_ieee (input_line_pointer
, type
, words
);
363 input_line_pointer
= t
;
364 * sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
366 for (i
= 0; i
< prec
; i
++)
368 md_number_to_chars (litP
, (valueT
) words
[i
],
369 sizeof (LITTLENUM_TYPE
));
370 litP
+= sizeof (LITTLENUM_TYPE
);
376 /* Worker function for fr30_is_colon_insn(). */
378 restore_colon (int advance_i_l_p_by
)
382 /* Restore the colon, and advance input_line_pointer to
383 the end of the new symbol. */
384 * input_line_pointer
= ':';
385 input_line_pointer
+= advance_i_l_p_by
;
386 c
= * input_line_pointer
;
387 * input_line_pointer
= 0;
392 /* Determines if the symbol starting at START and ending in
393 a colon that was at the location pointed to by INPUT_LINE_POINTER
394 (but which has now been replaced bu a NUL) is in fact an
395 LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
396 If it is, then it restores the colon, advances INPUT_LINE_POINTER
397 to the real end of the instruction/symbol, and returns the character
398 that really terminated the symbol. Otherwise it returns 0. */
400 fr30_is_colon_insn (char * start
)
402 char * i_l_p
= input_line_pointer
;
404 /* Check to see if the symbol parsed so far is 'ldi'. */
405 if ( (start
[0] != 'l' && start
[0] != 'L')
406 || (start
[1] != 'd' && start
[1] != 'D')
407 || (start
[2] != 'i' && start
[2] != 'I')
410 /* Nope - check to see a 'd' follows the colon. */
411 if ( (i_l_p
[1] == 'd' || i_l_p
[1] == 'D')
412 && (i_l_p
[2] == ' ' || i_l_p
[2] == '\t' || i_l_p
[2] == '\n'))
414 /* Yup - it might be delay slot instruction. */
416 static char * delay_insns
[] =
418 "call", "jmp", "ret", "bra", "bno",
419 "beq", "bne", "bc", "bnc", "bn",
420 "bp", "bv", "bnv", "blt", "bge",
421 "ble", "bgt", "bls", "bhi"
424 for (i
= sizeof (delay_insns
) / sizeof (delay_insns
[0]); i
--;)
426 char * insn
= delay_insns
[i
];
427 int len
= strlen (insn
);
429 if (start
[len
] != 0)
433 if (TOLOWER (start
[len
]) != insn
[len
])
437 return restore_colon (1);
441 /* Nope - it is a normal label. */
445 /* Check to see if the text following the colon is '8'. */
446 if (i_l_p
[1] == '8' && (i_l_p
[2] == ' ' || i_l_p
[2] == '\t'))
447 return restore_colon (2);
449 /* Check to see if the text following the colon is '20'. */
450 else if (i_l_p
[1] == '2' && i_l_p
[2] =='0' && (i_l_p
[3] == ' ' || i_l_p
[3] == '\t'))
451 return restore_colon (3);
453 /* Check to see if the text following the colon is '32'. */
454 else if (i_l_p
[1] == '3' && i_l_p
[2] =='2' && (i_l_p
[3] == ' ' || i_l_p
[3] == '\t'))
455 return restore_colon (3);
461 fr30_fix_adjustable (fixS
* fixP
)
463 /* We need the symbol name for the VTABLE entries. */
464 if (fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
465 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)