1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2 Copyright (C) 1998 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. */
29 /* Linked list of symbols that are debugging symbols to be
30 defined as the beginning of the current instruction. */
31 typedef struct sym_link
33 struct sym_link * next;
37 static sym_linkS * debug_sym_link = (sym_linkS *) NULL;
39 /* Structure to hold all of the different components describing
40 an individual instruction. */
43 const CGEN_INSN * insn;
44 const CGEN_INSN * orig_insn;
47 CGEN_INSN_INT buffer [1];
48 #define INSN_VALUE(buf) (*(buf))
50 unsigned char buffer [CGEN_MAX_INSN_SIZE];
51 #define INSN_VALUE(buf) (buf)
56 fixS * fixups [GAS_CGEN_MAX_FIXUPS];
57 int indices [MAX_OPERAND_INSTANCES];
58 sym_linkS * debug_sym_link;
62 const char comment_chars[] = ";";
63 const char line_comment_chars[] = "#";
64 const char line_separator_chars[] = "";
65 const char EXP_CHARS[] = "eE";
66 const char FLT_CHARS[] = "dD";
68 #define FR30_SHORTOPTS ""
69 const char * md_shortopts = FR30_SHORTOPTS;
71 struct option md_longopts[] =
73 {NULL, no_argument, NULL, 0}
75 size_t md_longopts_size = sizeof (md_longopts);
78 md_parse_option (c, arg)
91 md_show_usage (stream)
94 fprintf (stream, _(" FR30 specific command line options:\n"));
97 /* The target specific pseudo-ops which we support. */
98 const pseudo_typeS md_pseudo_table[] =
112 /* Initialize the `cgen' interface. */
114 /* Set the machine number and endian. */
115 gas_cgen_opcode_desc = fr30_cgen_opcode_open (bfd_mach_fr30, CGEN_ENDIAN_BIG);
116 fr30_cgen_init_asm (gas_cgen_opcode_desc);
118 /* This is a callback from cgen to gas to parse operands. */
119 cgen_set_parse_operand_fn (gas_cgen_opcode_desc, gas_cgen_parse_operand);
130 /* Initialize GAS's cgen interface for a new instruction. */
131 gas_cgen_init_parse ();
133 insn.debug_sym_link = debug_sym_link;
134 debug_sym_link = (sym_linkS *)0;
136 insn.insn = fr30_cgen_assemble_insn
137 (gas_cgen_opcode_desc, str, & insn.fields, insn.buffer, & errmsg);
145 /* Doesn't really matter what we pass for RELAX_P here. */
146 gas_cgen_finish_insn (insn.insn, insn.buffer,
147 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
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 (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 += fragP->fr_symbol->sy_frag->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) & -4L;
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 (CGEN_OPERAND_TYPE (operand))
456 case FR30_OPERAND_PC : return BFD_RELOC_FR30_12_PCREL;
457 case FR30_OPERAND_RI :
458 case FR30_OPERAND_RJ :
459 case FR30_OPERAND_NBIT :
460 case FR30_OPERAND_VBIT :
461 case FR30_OPERAND_ZBIT :
462 case FR30_OPERAND_CBIT :
463 default : /* avoid -Wall warning */
467 return BFD_RELOC_NONE;
471 /* Return BFD reloc type from opinfo field in a fixS.
472 It's tricky using fx_r_type in fr30_frob_file because the values
473 are BFD_RELOC_UNUSED + operand number. */
474 #define FX_OPINFO_R_TYPE(f) ((f)->tc_fix_data.opinfo)
476 /* See whether we need to force a relocation into the output file.
477 This is used to force out switch and PC relative relocations when
481 fr30_force_relocation (fix)
484 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
485 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
488 return fix->fx_pcrel;
491 /* Write a value out to the object file, using the appropriate endianness. */
494 md_number_to_chars (buf, val, n)
499 number_to_chars_bigendian (buf, val, n);
502 /* Turn a string in input_line_pointer into a floating point constant of type
503 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
504 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
507 /* Equal to MAX_PRECISION in atof-ieee.c */
508 #define MAX_LITTLENUMS 6
511 md_atof (type, litP, sizeP)
518 LITTLENUM_TYPE words [MAX_LITTLENUMS];
538 /* FIXME: Some targets allow other format chars for bigger sizes here. */
542 return _("Bad call to md_atof()");
545 t = atof_ieee (input_line_pointer, type, words);
547 input_line_pointer = t;
548 * sizeP = prec * sizeof (LITTLENUM_TYPE);
550 for (i = 0; i < prec; i++)
552 md_number_to_chars (litP, (valueT) words[i],
553 sizeof (LITTLENUM_TYPE));
554 litP += sizeof (LITTLENUM_TYPE);