* config/tc-mips.c (s_gpdword): New function.
[binutils.git] / gas / config / tc-fr30.c
blob54a12bfb34621b739d8e2096b5658ee2248ede53
1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2 Copyright 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
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)
9 any later version.
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. */
21 #include <stdio.h>
22 #include "as.h"
23 #include "safe-ctype.h"
24 #include "subsegs.h"
25 #include "symcat.h"
26 #include "opcodes/fr30-desc.h"
27 #include "opcodes/fr30-opc.h"
28 #include "cgen.h"
30 /* Structure to hold all of the different components describing
31 an individual instruction. */
32 typedef struct
34 const CGEN_INSN * insn;
35 const CGEN_INSN * orig_insn;
36 CGEN_FIELDS fields;
37 #if CGEN_INT_INSN_P
38 CGEN_INSN_INT buffer [1];
39 #define INSN_VALUE(buf) (*(buf))
40 #else
41 unsigned char buffer [CGEN_MAX_INSN_SIZE];
42 #define INSN_VALUE(buf) (buf)
43 #endif
44 char * addr;
45 fragS * frag;
46 int num_fixups;
47 fixS * fixups [GAS_CGEN_MAX_FIXUPS];
48 int indices [MAX_OPERAND_INSTANCES];
50 fr30_insn;
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);
67 int
68 md_parse_option (c, arg)
69 int c;
70 char * arg;
72 switch (c)
74 default:
75 return 0;
77 return 1;
80 void
81 md_show_usage (stream)
82 FILE * 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[] =
90 { "word", cons, 4 },
91 { NULL, NULL, 0 }
95 void
96 md_begin ()
98 flagword applicable;
99 segT seg;
100 subsegT subseg;
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,
107 CGEN_ENDIAN_BIG,
108 CGEN_CPU_OPEN_END);
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);
115 void
116 md_assemble (str)
117 char * str;
119 static int last_insn_had_delay_slot = 0;
120 fr30_insn insn;
121 char * errmsg;
122 char * str2 = NULL;
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);
130 if (!insn.insn)
132 as_bad (errmsg);
133 return;
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. */
153 void
154 md_operand (expressionP)
155 expressionS * expressionP;
157 if (* input_line_pointer == '#')
159 input_line_pointer ++;
160 expression (expressionP);
164 valueT
165 md_section_align (segment, size)
166 segT segment;
167 valueT size;
169 int align = bfd_get_section_alignment (stdoutput, segment);
170 return ((size + (1 << align) - 1) & (-1 << align));
173 symbolS *
174 md_undefined_symbol (name)
175 char * name;
177 return 0;
180 /* Interface to relax_segment. */
182 /* FIXME: Build table by hand, get it working, then machine generate. */
184 const relax_typeS md_relax_table[] =
186 /* The fields are:
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
193 each list. */
194 {1, 1, 0, 0},
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
202 case). */
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 }
210 long
211 fr30_relax_frag (segment, fragP, stretch)
212 segT segment;
213 fragS * fragP;
214 long stretch;
216 /* Address of branch insn. */
217 long address = fragP->fr_address + fragP->fr_fix - 2;
218 long growth = 0;
220 /* Keep 32 bit insns aligned on 32 bit boundaries. */
221 if (fragP->fr_subtype == 2)
223 if ((address & 3) != 0)
225 fragP->fr_subtype = 3;
226 growth = 2;
229 else if (fragP->fr_subtype == 3)
231 if ((address & 3) == 0)
233 fragP->fr_subtype = 2;
234 growth = -2;
237 else
239 growth = relax_frag (segment, fragP, stretch);
241 /* Long jump on odd halfword boundary? */
242 if (fragP->fr_subtype == 2 && (address & 3) != 0)
244 fragP->fr_subtype = 3;
245 growth += 2;
249 return growth;
252 /* Return an initial guess of the length by which a fragment must grow to
253 hold a branch to reach its destination.
254 Also updates fr_type/fr_subtype as necessary.
256 Called just before doing relaxation.
257 Any symbol that is now undefined will not become defined.
258 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
259 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
260 Although it may not be explicit in the frag, pretend fr_var starts with a
261 0 value. */
264 md_estimate_size_before_relax (fragP, segment)
265 fragS * fragP;
266 segT segment;
268 /* The only thing we have to handle here are symbols outside of the
269 current segment. They may be undefined or in a different segment in
270 which case linker scripts may place them anywhere.
271 However, we can't finish the fragment here and emit the reloc as insn
272 alignment requirements may move the insn about. */
274 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
276 int old_fr_fix = fragP->fr_fix;
278 /* The symbol is undefined in this segment.
279 Change the relaxation subtype to the max allowable and leave
280 all further handling to md_convert_frag. */
281 fragP->fr_subtype = 2;
283 #if 0 /* Can't use this, but leave in for illustration. */
284 /* Change 16 bit insn to 32 bit insn. */
285 fragP->fr_opcode[0] |= 0x80;
287 /* Increase known (fixed) size of fragment. */
288 fragP->fr_fix += 2;
290 /* Create a relocation for it. */
291 fix_new (fragP, old_fr_fix, 4,
292 fragP->fr_symbol,
293 fragP->fr_offset, 1 /* pcrel */,
294 /* FIXME: Can't use a real BFD reloc here.
295 gas_cgen_md_apply_fix3 can't handle it. */
296 BFD_RELOC_FR30_26_PCREL);
298 /* Mark this fragment as finished. */
299 frag_wane (fragP);
300 return fragP->fr_fix - old_fr_fix;
301 #else
303 const CGEN_INSN * insn;
304 int i;
306 /* Update the recorded insn.
307 Fortunately we don't have to look very far.
308 FIXME: Change this to record in the instruction the next higher
309 relaxable insn to use. */
310 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
312 if ((strcmp (CGEN_INSN_MNEMONIC (insn),
313 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
314 == 0)
315 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX))
316 break;
318 if (i == 4)
319 abort ();
321 fragP->fr_cgen.insn = insn;
322 return 2;
324 #endif
327 /* Return the size of the variable part of the frag. */
328 return md_relax_table[fragP->fr_subtype].rlx_length;
331 /* *fragP has been relaxed to its final size, and now needs to have
332 the bytes inside it modified to conform to the new size.
334 Called after relaxation is finished.
335 fragP->fr_type == rs_machine_dependent.
336 fragP->fr_subtype is the subtype of what the address relaxed to. */
338 void
339 md_convert_frag (abfd, sec, fragP)
340 bfd * abfd;
341 segT sec;
342 fragS * fragP;
344 #if 0
345 char * opcode;
346 char * displacement;
347 int target_address;
348 int opcode_address;
349 int extension;
350 int addend;
352 opcode = fragP->fr_opcode;
354 /* Address opcode resides at in file space. */
355 opcode_address = fragP->fr_address + fragP->fr_fix - 2;
357 switch (fragP->fr_subtype)
359 case 1 :
360 extension = 0;
361 displacement = & opcode[1];
362 break;
363 case 2 :
364 opcode[0] |= 0x80;
365 extension = 2;
366 displacement = & opcode[1];
367 break;
368 case 3 :
369 opcode[2] = opcode[0] | 0x80;
370 md_number_to_chars (opcode, PAR_NOP_INSN, 2);
371 opcode_address += 2;
372 extension = 4;
373 displacement = & opcode[3];
374 break;
375 default :
376 abort ();
379 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
381 /* symbol must be resolved by linker */
382 if (fragP->fr_offset & 3)
383 as_warn (_("Addend to unresolved symbol not on word boundary."));
384 addend = fragP->fr_offset >> 2;
386 else
388 /* Address we want to reach in file space. */
389 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
390 addend = (target_address - (opcode_address & -4)) >> 2;
393 /* Create a relocation for symbols that must be resolved by the linker.
394 Otherwise output the completed insn. */
396 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
398 assert (fragP->fr_subtype != 1);
399 assert (fragP->fr_cgen.insn != 0);
400 gas_cgen_record_fixup (fragP,
401 /* Offset of branch insn in frag. */
402 fragP->fr_fix + extension - 4,
403 fragP->fr_cgen.insn,
404 4 /*length*/,
405 /* FIXME: quick hack */
406 #if 0
407 CGEN_OPERAND_ENTRY (fragP->fr_cgen.opindex),
408 #else
409 CGEN_OPERAND_ENTRY (FR30_OPERAND_DISP24),
410 #endif
411 fragP->fr_cgen.opinfo,
412 fragP->fr_symbol, fragP->fr_offset);
415 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
417 md_number_to_chars (displacement, (valueT) addend,
418 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
420 fragP->fr_fix += extension;
421 #endif
424 /* Functions concerning relocs. */
426 /* The location from which a PC relative jump should be calculated,
427 given a PC relative reloc. */
429 long
430 md_pcrel_from_section (fixP, sec)
431 fixS * fixP;
432 segT sec;
434 if (fixP->fx_addsy != (symbolS *) NULL
435 && (! S_IS_DEFINED (fixP->fx_addsy)
436 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
438 /* The symbol is undefined (or is defined but not in this section).
439 Let the linker figure it out. */
440 return 0;
443 return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
446 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
447 Returns BFD_RELOC_NONE if no reloc type can be found.
448 *FIXP may be modified if desired. */
450 bfd_reloc_code_real_type
451 md_cgen_lookup_reloc (insn, operand, fixP)
452 const CGEN_INSN * insn;
453 const CGEN_OPERAND * operand;
454 fixS * fixP;
456 switch (operand->type)
458 case FR30_OPERAND_LABEL9: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL;
459 case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL;
460 case FR30_OPERAND_DISP10: return BFD_RELOC_FR30_10_IN_8;
461 case FR30_OPERAND_DISP9: return BFD_RELOC_FR30_9_IN_8;
462 case FR30_OPERAND_DISP8: return BFD_RELOC_FR30_8_IN_8;
463 case FR30_OPERAND_UDISP6: return BFD_RELOC_FR30_6_IN_4;
464 case FR30_OPERAND_I8: return BFD_RELOC_8;
465 case FR30_OPERAND_I32: return BFD_RELOC_FR30_48;
466 case FR30_OPERAND_I20: return BFD_RELOC_FR30_20;
467 default : /* avoid -Wall warning */
468 break;
471 return BFD_RELOC_NONE;
474 /* See whether we need to force a relocation into the output file.
475 This is used to force out switch and PC relative relocations when
476 relaxing. */
479 fr30_force_relocation (fix)
480 fixS * fix;
482 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
483 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
484 return 1;
486 return S_FORCE_RELOC (fix->fx_addsy);
489 /* Write a value out to the object file, using the appropriate endianness. */
491 void
492 md_number_to_chars (buf, val, n)
493 char * buf;
494 valueT val;
495 int n;
497 number_to_chars_bigendian (buf, val, n);
500 /* Turn a string in input_line_pointer into a floating point constant of type
501 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
502 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
505 /* Equal to MAX_PRECISION in atof-ieee.c */
506 #define MAX_LITTLENUMS 6
508 char *
509 md_atof (type, litP, sizeP)
510 char type;
511 char * litP;
512 int * sizeP;
514 int i;
515 int prec;
516 LITTLENUM_TYPE words [MAX_LITTLENUMS];
517 char * t;
518 char * atof_ieee ();
520 switch (type)
522 case 'f':
523 case 'F':
524 case 's':
525 case 'S':
526 prec = 2;
527 break;
529 case 'd':
530 case 'D':
531 case 'r':
532 case 'R':
533 prec = 4;
534 break;
536 /* FIXME: Some targets allow other format chars for bigger sizes here. */
538 default:
539 * sizeP = 0;
540 return _("Bad call to md_atof()");
543 t = atof_ieee (input_line_pointer, type, words);
544 if (t)
545 input_line_pointer = t;
546 * sizeP = prec * sizeof (LITTLENUM_TYPE);
548 for (i = 0; i < prec; i++)
550 md_number_to_chars (litP, (valueT) words[i],
551 sizeof (LITTLENUM_TYPE));
552 litP += sizeof (LITTLENUM_TYPE);
555 return 0;
558 /* Worker function for fr30_is_colon_insn(). */
559 static char
560 restore_colon (advance_i_l_p_by)
561 int advance_i_l_p_by;
563 char c;
565 /* Restore the colon, and advance input_line_pointer to
566 the end of the new symbol. */
567 * input_line_pointer = ':';
568 input_line_pointer += advance_i_l_p_by;
569 c = * input_line_pointer;
570 * input_line_pointer = 0;
572 return c;
575 /* Determines if the symbol starting at START and ending in
576 a colon that was at the location pointed to by INPUT_LINE_POINTER
577 (but which has now been replaced bu a NUL) is in fact an
578 LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
579 If it is, then it restores the colon, advances INPUT_LINE_POINTER
580 to the real end of the instruction/symbol, and returns the character
581 that really terminated the symbol. Otherwise it returns 0. */
582 char
583 fr30_is_colon_insn (start)
584 char * start;
586 char * i_l_p = input_line_pointer;
588 /* Check to see if the symbol parsed so far is 'ldi' */
589 if ( (start[0] != 'l' && start[0] != 'L')
590 || (start[1] != 'd' && start[1] != 'D')
591 || (start[2] != 'i' && start[2] != 'I')
592 || start[3] != 0)
594 /* Nope - check to see a 'd' follows the colon. */
595 if ( (i_l_p[1] == 'd' || i_l_p[1] == 'D')
596 && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n'))
598 /* Yup - it might be delay slot instruction. */
599 int i;
600 static char * delay_insns [] =
602 "call", "jmp", "ret", "bra", "bno",
603 "beq", "bne", "bc", "bnc", "bn",
604 "bp", "bv", "bnv", "blt", "bge",
605 "ble", "bgt", "bls", "bhi"
608 for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;)
610 char * insn = delay_insns[i];
611 int len = strlen (insn);
613 if (start [len] != 0)
614 continue;
616 while (len --)
617 if (TOLOWER (start [len]) != insn [len])
618 break;
620 if (len == -1)
621 return restore_colon (1);
625 /* Nope - it is a normal label. */
626 return 0;
629 /* Check to see if the text following the colon is '8' */
630 if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
631 return restore_colon (2);
633 /* Check to see if the text following the colon is '20' */
634 else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
635 return restore_colon (3);
637 /* Check to see if the text following the colon is '32' */
638 else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
639 return restore_colon (3);
641 return 0;
644 boolean
645 fr30_fix_adjustable (fixP)
646 fixS * fixP;
648 /* We need the symbol name for the VTABLE entries */
649 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
650 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
651 return 0;
653 return 1;