1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2 Copyright (C) 1998, 1999 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 #include "opcodes/fr30-desc.h"
27 #include "opcodes/fr30-opc.h"
30 /* Structure to hold all of the different components describing
31 an individual instruction. */
34 const CGEN_INSN
* insn
;
35 const CGEN_INSN
* orig_insn
;
38 CGEN_INSN_INT buffer
[1];
39 #define INSN_VALUE(buf) (*(buf))
41 unsigned char buffer
[CGEN_MAX_INSN_SIZE
];
42 #define INSN_VALUE(buf) (buf)
47 fixS
* fixups
[GAS_CGEN_MAX_FIXUPS
];
48 int indices
[MAX_OPERAND_INSTANCES
];
52 const char comment_chars
[] = ";";
53 const char line_comment_chars
[] = "#";
54 const char line_separator_chars
[] = "|";
55 const char EXP_CHARS
[] = "eE";
56 const char FLT_CHARS
[] = "dD";
58 #define FR30_SHORTOPTS ""
59 const char * md_shortopts
= FR30_SHORTOPTS
;
61 struct option md_longopts
[] =
63 {NULL
, no_argument
, NULL
, 0}
65 size_t md_longopts_size
= sizeof (md_longopts
);
68 md_parse_option (c
, arg
)
81 md_show_usage (stream
)
84 fprintf (stream
, _(" FR30 specific command line options:\n"));
87 /* The target specific pseudo-ops which we support. */
88 const pseudo_typeS md_pseudo_table
[] =
102 /* Initialize the `cgen' interface. */
104 /* Set the machine number and endian. */
105 gas_cgen_cpu_desc
= fr30_cgen_cpu_open (CGEN_CPU_OPEN_MACHS
, 0,
106 CGEN_CPU_OPEN_ENDIAN
,
109 fr30_cgen_init_asm (gas_cgen_cpu_desc
);
111 /* This is a callback from cgen to gas to parse operands. */
112 cgen_set_parse_operand_fn (gas_cgen_cpu_desc
, gas_cgen_parse_operand
);
119 static int last_insn_had_delay_slot
= 0;
124 /* Initialize GAS's cgen interface for a new instruction. */
125 gas_cgen_init_parse ();
127 insn
.insn
= fr30_cgen_assemble_insn
128 (gas_cgen_cpu_desc
, str
, & insn
.fields
, insn
.buffer
, & errmsg
);
136 /* Doesn't really matter what we pass for RELAX_P here. */
137 gas_cgen_finish_insn (insn
.insn
, insn
.buffer
,
138 CGEN_FIELDS_BITSIZE (& insn
.fields
), 1, NULL
);
140 /* Warn about invalid insns in delay slots. */
141 if (last_insn_had_delay_slot
142 && CGEN_INSN_ATTR_VALUE (insn
.insn
, CGEN_INSN_NOT_IN_DELAY_SLOT
))
143 as_warn (_("Instruction %s not allowed in a delay slot."),
144 CGEN_INSN_NAME (insn
.insn
));
146 last_insn_had_delay_slot
147 = CGEN_INSN_ATTR_VALUE (insn
.insn
, CGEN_INSN_DELAY_SLOT
);
150 /* The syntax in the manual says constants begin with '#'.
151 We just ignore it. */
154 md_operand (expressionP
)
155 expressionS
* expressionP
;
157 if (* input_line_pointer
== '#')
159 input_line_pointer
++;
160 expression (expressionP
);
165 md_section_align (segment
, size
)
169 int align
= bfd_get_section_alignment (stdoutput
, segment
);
170 return ((size
+ (1 << align
) - 1) & (-1 << align
));
174 md_undefined_symbol (name
)
180 /* Interface to relax_segment. */
182 /* FIXME: Build table by hand, get it working, then machine generate. */
184 const relax_typeS md_relax_table
[] =
187 1) most positive reach of this state,
188 2) most negative reach of this state,
189 3) how many bytes this mode will add to the size of the current frag
190 4) which index into the table to try if we can't fit into this one. */
192 /* The first entry must be unused because an `rlx_more' value of zero ends
196 /* The displacement used by GAS is from the end of the 2 byte insn,
197 so we subtract 2 from the following. */
198 /* 16 bit insn, 8 bit disp -> 10 bit range.
199 This doesn't handle a branch in the right slot at the border:
200 the "& -4" isn't taken into account. It's not important enough to
201 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
203 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
204 /* 32 bit insn, 24 bit disp -> 26 bit range. */
205 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
206 /* Same thing, but with leading nop for alignment. */
207 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
211 fr30_relax_frag (fragP
, stretch
)
215 /* Address of branch insn. */
216 long address
= fragP
->fr_address
+ fragP
->fr_fix
- 2;
219 /* Keep 32 bit insns aligned on 32 bit boundaries. */
220 if (fragP
->fr_subtype
== 2)
222 if ((address
& 3) != 0)
224 fragP
->fr_subtype
= 3;
228 else if (fragP
->fr_subtype
== 3)
230 if ((address
& 3) == 0)
232 fragP
->fr_subtype
= 2;
238 growth
= relax_frag (fragP
, stretch
);
240 /* Long jump on odd halfword boundary? */
241 if (fragP
->fr_subtype
== 2 && (address
& 3) != 0)
243 fragP
->fr_subtype
= 3;
251 /* Return an initial guess of the length by which a fragment must grow to
252 hold a branch to reach its destination.
253 Also updates fr_type/fr_subtype as necessary.
255 Called just before doing relaxation.
256 Any symbol that is now undefined will not become defined.
257 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
258 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
259 Although it may not be explicit in the frag, pretend fr_var starts with a
263 md_estimate_size_before_relax (fragP
, segment
)
267 int old_fr_fix
= fragP
->fr_fix
;
269 /* The only thing we have to handle here are symbols outside of the
270 current segment. They may be undefined or in a different segment in
271 which case linker scripts may place them anywhere.
272 However, we can't finish the fragment here and emit the reloc as insn
273 alignment requirements may move the insn about. */
275 if (S_GET_SEGMENT (fragP
->fr_symbol
) != segment
)
277 /* The symbol is undefined in this segment.
278 Change the relaxation subtype to the max allowable and leave
279 all further handling to md_convert_frag. */
280 fragP
->fr_subtype
= 2;
282 #if 0 /* Can't use this, but leave in for illustration. */
283 /* Change 16 bit insn to 32 bit insn. */
284 fragP
->fr_opcode
[0] |= 0x80;
286 /* Increase known (fixed) size of fragment. */
289 /* Create a relocation for it. */
290 fix_new (fragP
, old_fr_fix
, 4,
292 fragP
->fr_offset
, 1 /* pcrel */,
293 /* FIXME: Can't use a real BFD reloc here.
294 gas_cgen_md_apply_fix3 can't handle it. */
295 BFD_RELOC_FR30_26_PCREL
);
297 /* Mark this fragment as finished. */
301 const CGEN_INSN
* insn
;
304 /* Update the recorded insn.
305 Fortunately we don't have to look very far.
306 FIXME: Change this to record in the instruction the next higher
307 relaxable insn to use. */
308 for (i
= 0, insn
= fragP
->fr_cgen
.insn
; i
< 4; i
++, insn
++)
310 if ((strcmp (CGEN_INSN_MNEMONIC (insn
),
311 CGEN_INSN_MNEMONIC (fragP
->fr_cgen
.insn
))
313 && CGEN_INSN_ATTR_VALUE (insn
, CGEN_INSN_RELAX
))
319 fragP
->fr_cgen
.insn
= insn
;
325 return (fragP
->fr_var
+ fragP
->fr_fix
- old_fr_fix
);
328 /* *fragP has been relaxed to its final size, and now needs to have
329 the bytes inside it modified to conform to the new size.
331 Called after relaxation is finished.
332 fragP->fr_type == rs_machine_dependent.
333 fragP->fr_subtype is the subtype of what the address relaxed to. */
336 md_convert_frag (abfd
, sec
, fragP
)
349 opcode
= fragP
->fr_opcode
;
351 /* Address opcode resides at in file space. */
352 opcode_address
= fragP
->fr_address
+ fragP
->fr_fix
- 2;
354 switch (fragP
->fr_subtype
)
358 displacement
= & opcode
[1];
363 displacement
= & opcode
[1];
366 opcode
[2] = opcode
[0] | 0x80;
367 md_number_to_chars (opcode
, PAR_NOP_INSN
, 2);
370 displacement
= & opcode
[3];
376 if (S_GET_SEGMENT (fragP
->fr_symbol
) != sec
)
378 /* symbol must be resolved by linker */
379 if (fragP
->fr_offset
& 3)
380 as_warn (_("Addend to unresolved symbol not on word boundary."));
381 addend
= fragP
->fr_offset
>> 2;
385 /* Address we want to reach in file space. */
386 target_address
= S_GET_VALUE (fragP
->fr_symbol
) + fragP
->fr_offset
;
387 target_address
+= symbol_get_frag (fragP
->fr_symbol
)->fr_address
;
388 addend
= (target_address
- (opcode_address
& -4)) >> 2;
391 /* Create a relocation for symbols that must be resolved by the linker.
392 Otherwise output the completed insn. */
394 if (S_GET_SEGMENT (fragP
->fr_symbol
) != sec
)
396 assert (fragP
->fr_subtype
!= 1);
397 assert (fragP
->fr_cgen
.insn
!= 0);
398 gas_cgen_record_fixup (fragP
,
399 /* Offset of branch insn in frag. */
400 fragP
->fr_fix
+ extension
- 4,
403 /* FIXME: quick hack */
405 CGEN_OPERAND_ENTRY (fragP
->fr_cgen
.opindex
),
407 CGEN_OPERAND_ENTRY (FR30_OPERAND_DISP24
),
409 fragP
->fr_cgen
.opinfo
,
410 fragP
->fr_symbol
, fragP
->fr_offset
);
413 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
415 md_number_to_chars (displacement
, (valueT
) addend
,
416 SIZE_FROM_RELAX_STATE (fragP
->fr_subtype
));
418 fragP
->fr_fix
+= extension
;
422 /* Functions concerning relocs. */
424 /* The location from which a PC relative jump should be calculated,
425 given a PC relative reloc. */
428 md_pcrel_from_section (fixP
, sec
)
432 if (fixP
->fx_addsy
!= (symbolS
*) NULL
433 && (! S_IS_DEFINED (fixP
->fx_addsy
)
434 || S_GET_SEGMENT (fixP
->fx_addsy
) != sec
))
436 /* The symbol is undefined (or is defined but not in this section).
437 Let the linker figure it out. */
441 return (fixP
->fx_frag
->fr_address
+ fixP
->fx_where
) & ~1;
444 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
445 Returns BFD_RELOC_NONE if no reloc type can be found.
446 *FIXP may be modified if desired. */
448 bfd_reloc_code_real_type
449 md_cgen_lookup_reloc (insn
, operand
, fixP
)
450 const CGEN_INSN
* insn
;
451 const CGEN_OPERAND
* operand
;
454 switch (operand
->type
)
456 case FR30_OPERAND_LABEL9
: fixP
->fx_pcrel
= 1; return BFD_RELOC_FR30_9_PCREL
;
457 case FR30_OPERAND_LABEL12
: fixP
->fx_pcrel
= 1; return BFD_RELOC_FR30_12_PCREL
;
458 case FR30_OPERAND_DISP10
: return BFD_RELOC_FR30_10_IN_8
;
459 case FR30_OPERAND_DISP9
: return BFD_RELOC_FR30_9_IN_8
;
460 case FR30_OPERAND_DISP8
: return BFD_RELOC_FR30_8_IN_8
;
461 case FR30_OPERAND_UDISP6
: return BFD_RELOC_FR30_6_IN_4
;
462 case FR30_OPERAND_I8
: return BFD_RELOC_8
;
463 case FR30_OPERAND_I32
: return BFD_RELOC_FR30_48
;
464 case FR30_OPERAND_I20
: return BFD_RELOC_FR30_20
;
465 default : /* avoid -Wall warning */
469 return BFD_RELOC_NONE
;
472 /* See whether we need to force a relocation into the output file.
473 This is used to force out switch and PC relative relocations when
477 fr30_force_relocation (fix
)
480 if ( fix
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
481 || fix
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
487 /* Write a value out to the object file, using the appropriate endianness. */
490 md_number_to_chars (buf
, val
, n
)
495 number_to_chars_bigendian (buf
, val
, n
);
498 /* Turn a string in input_line_pointer into a floating point constant of type
499 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
500 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
503 /* Equal to MAX_PRECISION in atof-ieee.c */
504 #define MAX_LITTLENUMS 6
507 md_atof (type
, litP
, sizeP
)
514 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
534 /* FIXME: Some targets allow other format chars for bigger sizes here. */
538 return _("Bad call to md_atof()");
541 t
= atof_ieee (input_line_pointer
, type
, words
);
543 input_line_pointer
= t
;
544 * sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
546 for (i
= 0; i
< prec
; i
++)
548 md_number_to_chars (litP
, (valueT
) words
[i
],
549 sizeof (LITTLENUM_TYPE
));
550 litP
+= sizeof (LITTLENUM_TYPE
);
556 /* Worker function for fr30_is_colon_insn(). */
558 restore_colon (advance_i_l_p_by
)
559 int advance_i_l_p_by
;
563 /* Restore the colon, and advance input_line_pointer to
564 the end of the new symbol. */
565 * input_line_pointer
= ':';
566 input_line_pointer
+= advance_i_l_p_by
;
567 c
= * input_line_pointer
;
568 * input_line_pointer
= 0;
573 /* Determines if the symbol starting at START and ending in
574 a colon that was at the location pointed to by INPUT_LINE_POINTER
575 (but which has now been replaced bu a NUL) is in fact an
576 LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
577 If it is, then it restores the colon, advances INPUT_LINE_POINTER
578 to the real end of the instruction/symbol, and returns the character
579 that really terminated the symbol. Otherwise it returns 0. */
581 fr30_is_colon_insn (start
)
584 char * i_l_p
= input_line_pointer
;
586 /* Check to see if the symbol parsed so far is 'ldi' */
587 if ( (start
[0] != 'l' && start
[0] != 'L')
588 || (start
[1] != 'd' && start
[1] != 'D')
589 || (start
[2] != 'i' && start
[2] != 'I')
592 /* Nope - check to see a 'd' follows the colon. */
593 if ( (i_l_p
[1] == 'd' || i_l_p
[1] == 'D')
594 && (i_l_p
[2] == ' ' || i_l_p
[2] == '\t' || i_l_p
[2] == '\n'))
596 /* Yup - it might be delay slot instruction. */
598 static char * delay_insns
[] =
600 "call", "jmp", "ret", "bra", "bno",
601 "beq", "bne", "bc", "bnc", "bn",
602 "bp", "bv", "bnv", "blt", "bge",
603 "ble", "bgt", "bls", "bhi"
606 for (i
= sizeof (delay_insns
) / sizeof (delay_insns
[0]); i
--;)
608 char * insn
= delay_insns
[i
];
609 int len
= strlen (insn
);
611 if (start
[len
] != 0)
615 if (tolower (start
[len
]) != insn
[len
])
619 return restore_colon (1);
623 /* Nope - it is a normal label. */
627 /* Check to see if the text following the colon is '8' */
628 if (i_l_p
[1] == '8' && (i_l_p
[2] == ' ' || i_l_p
[2] == '\t'))
629 return restore_colon (2);
631 /* Check to see if the text following the colon is '20' */
632 else if (i_l_p
[1] == '2' && i_l_p
[2] =='0' && (i_l_p
[3] == ' ' || i_l_p
[3] == '\t'))
633 return restore_colon (3);
635 /* Check to see if the text following the colon is '32' */
636 else if (i_l_p
[1] == '3' && i_l_p
[2] =='2' && (i_l_p
[3] == ' ' || i_l_p
[3] == '\t'))
637 return restore_colon (3);
643 fr30_fix_adjustable (fixP
)
646 if (fixP
->fx_addsy
== NULL
)
650 /* Prevent all adjustments to global symbols. */
651 if (S_IS_EXTERN (fixP
->fx_addsy
))
654 if (S_IS_WEAK (fixP
->fx_addsy
))
658 /* We need the symbol name for the VTABLE entries */
659 if ( fixP
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
660 || fixP
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)