Add support for delay slot insns.
[binutils-gdb.git] / gas / config / tc-fr30.c
1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2 Copyright (C) 1998 Free Software Foundation.
3
4 This file is part of GAS, the GNU Assembler.
5
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.
10
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.
15
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. */
20
21 #include <stdio.h>
22 #include <ctype.h>
23 #include "as.h"
24 #include "subsegs.h"
25 #include "symcat.h"
26 #include "cgen-opc.h"
27 #include "cgen.h"
28
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
32 {
33 struct sym_link * next;
34 symbolS * symbol;
35 } sym_linkS;
36
37 static sym_linkS * debug_sym_link = (sym_linkS *) NULL;
38
39 /* Structure to hold all of the different components describing
40 an individual instruction. */
41 typedef struct
42 {
43 const CGEN_INSN * insn;
44 const CGEN_INSN * orig_insn;
45 CGEN_FIELDS fields;
46 #if CGEN_INT_INSN_P
47 CGEN_INSN_INT buffer [1];
48 #define INSN_VALUE(buf) (*(buf))
49 #else
50 unsigned char buffer [CGEN_MAX_INSN_SIZE];
51 #define INSN_VALUE(buf) (buf)
52 #endif
53 char * addr;
54 fragS * frag;
55 int num_fixups;
56 fixS * fixups [GAS_CGEN_MAX_FIXUPS];
57 int indices [MAX_OPERAND_INSTANCES];
58 sym_linkS * debug_sym_link;
59 }
60 fr30_insn;
61
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";
67 \f
68 #define FR30_SHORTOPTS ""
69 const char * md_shortopts = FR30_SHORTOPTS;
70
71 struct option md_longopts[] =
72 {
73 {NULL, no_argument, NULL, 0}
74 };
75 size_t md_longopts_size = sizeof (md_longopts);
76
77 int
78 md_parse_option (c, arg)
79 int c;
80 char * arg;
81 {
82 switch (c)
83 {
84 default:
85 return 0;
86 }
87 return 1;
88 }
89
90 void
91 md_show_usage (stream)
92 FILE * stream;
93 {
94 fprintf (stream, _(" FR30 specific command line options:\n"));
95 }
96
97 /* The target specific pseudo-ops which we support. */
98 const pseudo_typeS md_pseudo_table[] =
99 {
100 { "word", cons, 4 },
101 { NULL, NULL, 0 }
102 };
103
104 \f
105 void
106 md_begin ()
107 {
108 flagword applicable;
109 segT seg;
110 subsegT subseg;
111
112 /* Initialize the `cgen' interface. */
113
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);
117
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);
120 }
121
122 void
123 md_assemble (str)
124 char * str;
125 {
126 fr30_insn insn;
127 char * errmsg;
128 char * str2 = NULL;
129
130 /* Initialize GAS's cgen interface for a new instruction. */
131 gas_cgen_init_parse ();
132
133 insn.debug_sym_link = debug_sym_link;
134 debug_sym_link = (sym_linkS *)0;
135
136 insn.insn = fr30_cgen_assemble_insn
137 (gas_cgen_opcode_desc, str, & insn.fields, insn.buffer, & errmsg);
138
139 if (!insn.insn)
140 {
141 as_bad (errmsg);
142 return;
143 }
144
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);
148 }
149
150 /* The syntax in the manual says constants begin with '#'.
151 We just ignore it. */
152
153 void
154 md_operand (expressionP)
155 expressionS * expressionP;
156 {
157 if (* input_line_pointer == '#')
158 {
159 input_line_pointer ++;
160 expression (expressionP);
161 }
162 }
163
164 valueT
165 md_section_align (segment, size)
166 segT segment;
167 valueT size;
168 {
169 int align = bfd_get_section_alignment (stdoutput, segment);
170 return ((size + (1 << align) - 1) & (-1 << align));
171 }
172
173 symbolS *
174 md_undefined_symbol (name)
175 char * name;
176 {
177 return 0;
178 }
179 \f
180 /* Interface to relax_segment. */
181
182 /* FIXME: Build table by hand, get it working, then machine generate. */
183
184 const relax_typeS md_relax_table[] =
185 {
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. */
191
192 /* The first entry must be unused because an `rlx_more' value of zero ends
193 each list. */
194 {1, 1, 0, 0},
195
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 }
208 };
209
210 long
211 fr30_relax_frag (fragP, stretch)
212 fragS * fragP;
213 long stretch;
214 {
215 /* Address of branch insn. */
216 long address = fragP->fr_address + fragP->fr_fix - 2;
217 long growth = 0;
218
219 /* Keep 32 bit insns aligned on 32 bit boundaries. */
220 if (fragP->fr_subtype == 2)
221 {
222 if ((address & 3) != 0)
223 {
224 fragP->fr_subtype = 3;
225 growth = 2;
226 }
227 }
228 else if (fragP->fr_subtype == 3)
229 {
230 if ((address & 3) == 0)
231 {
232 fragP->fr_subtype = 2;
233 growth = -2;
234 }
235 }
236 else
237 {
238 growth = relax_frag (fragP, stretch);
239
240 /* Long jump on odd halfword boundary? */
241 if (fragP->fr_subtype == 2 && (address & 3) != 0)
242 {
243 fragP->fr_subtype = 3;
244 growth += 2;
245 }
246 }
247
248 return growth;
249 }
250
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.
254
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
260 0 value. */
261
262 int
263 md_estimate_size_before_relax (fragP, segment)
264 fragS * fragP;
265 segT segment;
266 {
267 int old_fr_fix = fragP->fr_fix;
268
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. */
274
275 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
276 {
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;
281
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;
285
286 /* Increase known (fixed) size of fragment. */
287 fragP->fr_fix += 2;
288
289 /* Create a relocation for it. */
290 fix_new (fragP, old_fr_fix, 4,
291 fragP->fr_symbol,
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);
296
297 /* Mark this fragment as finished. */
298 frag_wane (fragP);
299 #else
300 {
301 const CGEN_INSN * insn;
302 int i;
303
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++)
309 {
310 if ((strcmp (CGEN_INSN_MNEMONIC (insn),
311 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
312 == 0)
313 && CGEN_INSN_ATTR (insn, CGEN_INSN_RELAX))
314 break;
315 }
316 if (i == 4)
317 abort ();
318
319 fragP->fr_cgen.insn = insn;
320 return 2;
321 }
322 #endif
323 }
324
325 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
326 }
327
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.
330
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. */
334
335 void
336 md_convert_frag (abfd, sec, fragP)
337 bfd * abfd;
338 segT sec;
339 fragS * fragP;
340 {
341 #if 0
342 char * opcode;
343 char * displacement;
344 int target_address;
345 int opcode_address;
346 int extension;
347 int addend;
348
349 opcode = fragP->fr_opcode;
350
351 /* Address opcode resides at in file space. */
352 opcode_address = fragP->fr_address + fragP->fr_fix - 2;
353
354 switch (fragP->fr_subtype)
355 {
356 case 1 :
357 extension = 0;
358 displacement = & opcode[1];
359 break;
360 case 2 :
361 opcode[0] |= 0x80;
362 extension = 2;
363 displacement = & opcode[1];
364 break;
365 case 3 :
366 opcode[2] = opcode[0] | 0x80;
367 md_number_to_chars (opcode, PAR_NOP_INSN, 2);
368 opcode_address += 2;
369 extension = 4;
370 displacement = & opcode[3];
371 break;
372 default :
373 abort ();
374 }
375
376 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
377 {
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;
382 }
383 else
384 {
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;
389 }
390
391 /* Create a relocation for symbols that must be resolved by the linker.
392 Otherwise output the completed insn. */
393
394 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
395 {
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,
401 fragP->fr_cgen.insn,
402 4 /*length*/,
403 /* FIXME: quick hack */
404 #if 0
405 CGEN_OPERAND_ENTRY (fragP->fr_cgen.opindex),
406 #else
407 CGEN_OPERAND_ENTRY (FR30_OPERAND_DISP24),
408 #endif
409 fragP->fr_cgen.opinfo,
410 fragP->fr_symbol, fragP->fr_offset);
411 }
412
413 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
414
415 md_number_to_chars (displacement, (valueT) addend,
416 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
417
418 fragP->fr_fix += extension;
419 #endif
420 }
421 \f
422 /* Functions concerning relocs. */
423
424 /* The location from which a PC relative jump should be calculated,
425 given a PC relative reloc. */
426
427 long
428 md_pcrel_from_section (fixP, sec)
429 fixS * fixP;
430 segT sec;
431 {
432 if (fixP->fx_addsy != (symbolS *) NULL
433 && (! S_IS_DEFINED (fixP->fx_addsy)
434 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
435 {
436 /* The symbol is undefined (or is defined but not in this section).
437 Let the linker figure it out. */
438 return 0;
439 }
440
441 return (fixP->fx_frag->fr_address + fixP->fx_where) & -4L;
442 }
443
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. */
447
448 bfd_reloc_code_real_type
449 md_cgen_lookup_reloc (insn, operand, fixP)
450 const CGEN_INSN * insn;
451 const CGEN_OPERAND * operand;
452 fixS * fixP;
453 {
454 switch (CGEN_OPERAND_TYPE (operand))
455 {
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 */
464 break;
465 }
466
467 return BFD_RELOC_NONE;
468 }
469
470
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)
475
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
478 relaxing. */
479
480 int
481 fr30_force_relocation (fix)
482 fixS * fix;
483 {
484 if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
485 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
486 return 1;
487
488 return fix->fx_pcrel;
489 }
490 \f
491 /* Write a value out to the object file, using the appropriate endianness. */
492
493 void
494 md_number_to_chars (buf, val, n)
495 char * buf;
496 valueT val;
497 int n;
498 {
499 number_to_chars_bigendian (buf, val, n);
500 }
501
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.
505 */
506
507 /* Equal to MAX_PRECISION in atof-ieee.c */
508 #define MAX_LITTLENUMS 6
509
510 char *
511 md_atof (type, litP, sizeP)
512 char type;
513 char * litP;
514 int * sizeP;
515 {
516 int i;
517 int prec;
518 LITTLENUM_TYPE words [MAX_LITTLENUMS];
519 char * t;
520 char * atof_ieee ();
521
522 switch (type)
523 {
524 case 'f':
525 case 'F':
526 case 's':
527 case 'S':
528 prec = 2;
529 break;
530
531 case 'd':
532 case 'D':
533 case 'r':
534 case 'R':
535 prec = 4;
536 break;
537
538 /* FIXME: Some targets allow other format chars for bigger sizes here. */
539
540 default:
541 * sizeP = 0;
542 return _("Bad call to md_atof()");
543 }
544
545 t = atof_ieee (input_line_pointer, type, words);
546 if (t)
547 input_line_pointer = t;
548 * sizeP = prec * sizeof (LITTLENUM_TYPE);
549
550 for (i = 0; i < prec; i++)
551 {
552 md_number_to_chars (litP, (valueT) words[i],
553 sizeof (LITTLENUM_TYPE));
554 litP += sizeof (LITTLENUM_TYPE);
555 }
556
557 return 0;
558 }
559
560 static char
561 restore_colon (advance_i_l_p_by)
562 int advance_i_l_p_by;
563 {
564 char c;
565
566 /* Restore the colon, and advance input_line_pointer to
567 the end of the new symbol. */
568 * input_line_pointer = ':';
569 input_line_pointer += advance_i_l_p_by;
570 c = * input_line_pointer;
571 * input_line_pointer = 0;
572
573 return c;
574 }
575
576 /* Determines if the symbol starting at START and ending in
577 a colon that was at the location pointed to by INPUT_LINE_POINTER
578 (but which has now been replaced bu a NUL) is in fact an
579 LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
580 If it is, then it restores the colon, adbvances INPUT_LINE_POINTER
581 to the real end of the instruction/symbol, and returns the character
582 that really terminated the symbol. Otherwise it returns 0. */
583 char
584 fr30_is_colon_insn (start)
585 char * start;
586 {
587 char * i_l_p = input_line_pointer;
588
589 /* Check to see if the symbol parsed so far is 'ldi' */
590 if ( (start[0] != 'l' && start[0] != 'L')
591 || (start[1] != 'd' && start[1] != 'D')
592 || (start[2] != 'i' && start[2] != 'I')
593 || start[3] != 0)
594 {
595 /* Nope - check to see a 'd' follows the colon. */
596 if ( (i_l_p[1] == 'd' || i_l_p[1] == 'D')
597 && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n'))
598 {
599 /* Yup - it might be delay slot instruction. */
600 int i;
601 static char * delay_insns [] =
602 {
603 "call", "jmp", "ret", "bra", "bno",
604 "beq", "bne", "bc", "bnc", "bn", "bp",
605 "bv", "bnv", "blt", "bge", "ble", "bgt",
606 "bls", "bhi"
607 };
608
609 for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;)
610 {
611 char * insn = delay_insns[i];
612 int len = strlen (insn);
613
614 if (start [len] != 0)
615 continue;
616
617 while (len --)
618 {
619 if (tolower (start [len]) != insn [len])
620 break;
621 }
622
623 if (len == -1)
624 return restore_colon (1);
625 }
626 }
627
628 /* Nope - it is a normal label. */
629 return 0;
630 }
631
632 /* Check to see if the text following the colon is '8' */
633 if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
634 {
635 return restore_colon (2);
636 }
637
638 /* Check to see if the text following the colon is '20' */
639 else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
640 {
641 return restore_colon (3);
642 }
643
644 /* Check to see if the text following the colon is '32' */
645 else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
646 {
647 return restore_colon (3);
648 }
649
650 return 0;
651 }