* Makefile.am (DISTCLEANFILES): Change cgen-opc.h to cgen-desc.h.
[binutils-gdb.git] / gas / config / tc-fr30.c
1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2 Copyright (C) 1998, 1999 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 "opcodes/fr30-desc.h"
27 #include "opcodes/fr30-opc.h"
28 #include "cgen.h"
29
30 /* Structure to hold all of the different components describing
31 an individual instruction. */
32 typedef struct
33 {
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];
49 }
50 fr30_insn;
51
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";
57 \f
58 #define FR30_SHORTOPTS ""
59 const char * md_shortopts = FR30_SHORTOPTS;
60
61 struct option md_longopts[] =
62 {
63 {NULL, no_argument, NULL, 0}
64 };
65 size_t md_longopts_size = sizeof (md_longopts);
66
67 int
68 md_parse_option (c, arg)
69 int c;
70 char * arg;
71 {
72 switch (c)
73 {
74 default:
75 return 0;
76 }
77 return 1;
78 }
79
80 void
81 md_show_usage (stream)
82 FILE * stream;
83 {
84 fprintf (stream, _(" FR30 specific command line options:\n"));
85 }
86
87 /* The target specific pseudo-ops which we support. */
88 const pseudo_typeS md_pseudo_table[] =
89 {
90 { "word", cons, 4 },
91 { NULL, NULL, 0 }
92 };
93
94 \f
95 void
96 md_begin ()
97 {
98 flagword applicable;
99 segT seg;
100 subsegT subseg;
101
102 /* Initialize the `cgen' interface. */
103
104 /* Set the machine number and endian. */
105 gas_cgen_cpu_desc = fr30_cgen_cpu_open (bfd_mach_fr30, CGEN_ENDIAN_BIG);
106 fr30_cgen_init_asm (gas_cgen_cpu_desc);
107
108 /* This is a callback from cgen to gas to parse operands. */
109 cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
110 }
111
112 void
113 md_assemble (str)
114 char * str;
115 {
116 static int last_insn_had_delay_slot = 0;
117 fr30_insn insn;
118 char * errmsg;
119 char * str2 = NULL;
120
121 /* Initialize GAS's cgen interface for a new instruction. */
122 gas_cgen_init_parse ();
123
124 insn.insn = fr30_cgen_assemble_insn
125 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
126
127 if (!insn.insn)
128 {
129 as_bad (errmsg);
130 return;
131 }
132
133 /* Doesn't really matter what we pass for RELAX_P here. */
134 gas_cgen_finish_insn (insn.insn, insn.buffer,
135 CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
136
137 /* Warn about invalid insns in delay slots. */
138 if (last_insn_had_delay_slot
139 && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_NOT_IN_DELAY_SLOT))
140 as_warn (_("Instruction %s not allowed in a delay slot."),
141 CGEN_INSN_NAME (insn.insn));
142
143 last_insn_had_delay_slot
144 = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
145 }
146
147 /* The syntax in the manual says constants begin with '#'.
148 We just ignore it. */
149
150 void
151 md_operand (expressionP)
152 expressionS * expressionP;
153 {
154 if (* input_line_pointer == '#')
155 {
156 input_line_pointer ++;
157 expression (expressionP);
158 }
159 }
160
161 valueT
162 md_section_align (segment, size)
163 segT segment;
164 valueT size;
165 {
166 int align = bfd_get_section_alignment (stdoutput, segment);
167 return ((size + (1 << align) - 1) & (-1 << align));
168 }
169
170 symbolS *
171 md_undefined_symbol (name)
172 char * name;
173 {
174 return 0;
175 }
176 \f
177 /* Interface to relax_segment. */
178
179 /* FIXME: Build table by hand, get it working, then machine generate. */
180
181 const relax_typeS md_relax_table[] =
182 {
183 /* The fields are:
184 1) most positive reach of this state,
185 2) most negative reach of this state,
186 3) how many bytes this mode will add to the size of the current frag
187 4) which index into the table to try if we can't fit into this one. */
188
189 /* The first entry must be unused because an `rlx_more' value of zero ends
190 each list. */
191 {1, 1, 0, 0},
192
193 /* The displacement used by GAS is from the end of the 2 byte insn,
194 so we subtract 2 from the following. */
195 /* 16 bit insn, 8 bit disp -> 10 bit range.
196 This doesn't handle a branch in the right slot at the border:
197 the "& -4" isn't taken into account. It's not important enough to
198 complicate things over it, so we subtract an extra 2 (or + 2 in -ve
199 case). */
200 {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
201 /* 32 bit insn, 24 bit disp -> 26 bit range. */
202 {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
203 /* Same thing, but with leading nop for alignment. */
204 {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
205 };
206
207 long
208 fr30_relax_frag (fragP, stretch)
209 fragS * fragP;
210 long stretch;
211 {
212 /* Address of branch insn. */
213 long address = fragP->fr_address + fragP->fr_fix - 2;
214 long growth = 0;
215
216 /* Keep 32 bit insns aligned on 32 bit boundaries. */
217 if (fragP->fr_subtype == 2)
218 {
219 if ((address & 3) != 0)
220 {
221 fragP->fr_subtype = 3;
222 growth = 2;
223 }
224 }
225 else if (fragP->fr_subtype == 3)
226 {
227 if ((address & 3) == 0)
228 {
229 fragP->fr_subtype = 2;
230 growth = -2;
231 }
232 }
233 else
234 {
235 growth = relax_frag (fragP, stretch);
236
237 /* Long jump on odd halfword boundary? */
238 if (fragP->fr_subtype == 2 && (address & 3) != 0)
239 {
240 fragP->fr_subtype = 3;
241 growth += 2;
242 }
243 }
244
245 return growth;
246 }
247
248 /* Return an initial guess of the length by which a fragment must grow to
249 hold a branch to reach its destination.
250 Also updates fr_type/fr_subtype as necessary.
251
252 Called just before doing relaxation.
253 Any symbol that is now undefined will not become defined.
254 The guess for fr_var is ACTUALLY the growth beyond fr_fix.
255 Whatever we do to grow fr_fix or fr_var contributes to our returned value.
256 Although it may not be explicit in the frag, pretend fr_var starts with a
257 0 value. */
258
259 int
260 md_estimate_size_before_relax (fragP, segment)
261 fragS * fragP;
262 segT segment;
263 {
264 int old_fr_fix = fragP->fr_fix;
265
266 /* The only thing we have to handle here are symbols outside of the
267 current segment. They may be undefined or in a different segment in
268 which case linker scripts may place them anywhere.
269 However, we can't finish the fragment here and emit the reloc as insn
270 alignment requirements may move the insn about. */
271
272 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
273 {
274 /* The symbol is undefined in this segment.
275 Change the relaxation subtype to the max allowable and leave
276 all further handling to md_convert_frag. */
277 fragP->fr_subtype = 2;
278
279 #if 0 /* Can't use this, but leave in for illustration. */
280 /* Change 16 bit insn to 32 bit insn. */
281 fragP->fr_opcode[0] |= 0x80;
282
283 /* Increase known (fixed) size of fragment. */
284 fragP->fr_fix += 2;
285
286 /* Create a relocation for it. */
287 fix_new (fragP, old_fr_fix, 4,
288 fragP->fr_symbol,
289 fragP->fr_offset, 1 /* pcrel */,
290 /* FIXME: Can't use a real BFD reloc here.
291 gas_cgen_md_apply_fix3 can't handle it. */
292 BFD_RELOC_FR30_26_PCREL);
293
294 /* Mark this fragment as finished. */
295 frag_wane (fragP);
296 #else
297 {
298 const CGEN_INSN * insn;
299 int i;
300
301 /* Update the recorded insn.
302 Fortunately we don't have to look very far.
303 FIXME: Change this to record in the instruction the next higher
304 relaxable insn to use. */
305 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
306 {
307 if ((strcmp (CGEN_INSN_MNEMONIC (insn),
308 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
309 == 0)
310 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX))
311 break;
312 }
313 if (i == 4)
314 abort ();
315
316 fragP->fr_cgen.insn = insn;
317 return 2;
318 }
319 #endif
320 }
321
322 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
323 }
324
325 /* *fragP has been relaxed to its final size, and now needs to have
326 the bytes inside it modified to conform to the new size.
327
328 Called after relaxation is finished.
329 fragP->fr_type == rs_machine_dependent.
330 fragP->fr_subtype is the subtype of what the address relaxed to. */
331
332 void
333 md_convert_frag (abfd, sec, fragP)
334 bfd * abfd;
335 segT sec;
336 fragS * fragP;
337 {
338 #if 0
339 char * opcode;
340 char * displacement;
341 int target_address;
342 int opcode_address;
343 int extension;
344 int addend;
345
346 opcode = fragP->fr_opcode;
347
348 /* Address opcode resides at in file space. */
349 opcode_address = fragP->fr_address + fragP->fr_fix - 2;
350
351 switch (fragP->fr_subtype)
352 {
353 case 1 :
354 extension = 0;
355 displacement = & opcode[1];
356 break;
357 case 2 :
358 opcode[0] |= 0x80;
359 extension = 2;
360 displacement = & opcode[1];
361 break;
362 case 3 :
363 opcode[2] = opcode[0] | 0x80;
364 md_number_to_chars (opcode, PAR_NOP_INSN, 2);
365 opcode_address += 2;
366 extension = 4;
367 displacement = & opcode[3];
368 break;
369 default :
370 abort ();
371 }
372
373 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
374 {
375 /* symbol must be resolved by linker */
376 if (fragP->fr_offset & 3)
377 as_warn (_("Addend to unresolved symbol not on word boundary."));
378 addend = fragP->fr_offset >> 2;
379 }
380 else
381 {
382 /* Address we want to reach in file space. */
383 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
384 target_address += fragP->fr_symbol->sy_frag->fr_address;
385 addend = (target_address - (opcode_address & -4)) >> 2;
386 }
387
388 /* Create a relocation for symbols that must be resolved by the linker.
389 Otherwise output the completed insn. */
390
391 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
392 {
393 assert (fragP->fr_subtype != 1);
394 assert (fragP->fr_cgen.insn != 0);
395 gas_cgen_record_fixup (fragP,
396 /* Offset of branch insn in frag. */
397 fragP->fr_fix + extension - 4,
398 fragP->fr_cgen.insn,
399 4 /*length*/,
400 /* FIXME: quick hack */
401 #if 0
402 CGEN_OPERAND_ENTRY (fragP->fr_cgen.opindex),
403 #else
404 CGEN_OPERAND_ENTRY (FR30_OPERAND_DISP24),
405 #endif
406 fragP->fr_cgen.opinfo,
407 fragP->fr_symbol, fragP->fr_offset);
408 }
409
410 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
411
412 md_number_to_chars (displacement, (valueT) addend,
413 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
414
415 fragP->fr_fix += extension;
416 #endif
417 }
418 \f
419 /* Functions concerning relocs. */
420
421 /* The location from which a PC relative jump should be calculated,
422 given a PC relative reloc. */
423
424 long
425 md_pcrel_from_section (fixP, sec)
426 fixS * fixP;
427 segT sec;
428 {
429 if (fixP->fx_addsy != (symbolS *) NULL
430 && (! S_IS_DEFINED (fixP->fx_addsy)
431 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
432 {
433 /* The symbol is undefined (or is defined but not in this section).
434 Let the linker figure it out. */
435 return 0;
436 }
437
438 return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
439 }
440
441 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
442 Returns BFD_RELOC_NONE if no reloc type can be found.
443 *FIXP may be modified if desired. */
444
445 bfd_reloc_code_real_type
446 md_cgen_lookup_reloc (insn, operand, fixP)
447 const CGEN_INSN * insn;
448 const CGEN_OPERAND * operand;
449 fixS * fixP;
450 {
451 switch (CGEN_OPERAND_TYPE (gas_cgen_cpu_desc, operand))
452 {
453 case FR30_OPERAND_LABEL9: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL;
454 case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL;
455 case FR30_OPERAND_DISP10: return BFD_RELOC_FR30_10_IN_8;
456 case FR30_OPERAND_DISP9: return BFD_RELOC_FR30_9_IN_8;
457 case FR30_OPERAND_DISP8: return BFD_RELOC_FR30_8_IN_8;
458 case FR30_OPERAND_UDISP6: return BFD_RELOC_FR30_6_IN_4;
459 case FR30_OPERAND_I8: return BFD_RELOC_8;
460 case FR30_OPERAND_I32: return BFD_RELOC_FR30_48;
461 case FR30_OPERAND_I20: return BFD_RELOC_FR30_20;
462 default : /* avoid -Wall warning */
463 break;
464 }
465
466 return BFD_RELOC_NONE;
467 }
468
469
470 /* Return BFD reloc type from opinfo field in a fixS.
471 It's tricky using fx_r_type in fr30_frob_file because the values
472 are BFD_RELOC_UNUSED + operand number. */
473 #define FX_OPINFO_R_TYPE(f) ((f)->tc_fix_data.opinfo)
474
475 /* See whether we need to force a relocation into the output file.
476 This is used to force out switch and PC relative relocations when
477 relaxing. */
478
479 int
480 fr30_force_relocation (fix)
481 fixS * fix;
482 {
483 if ( fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
484 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
485 return 1;
486
487 return 0;
488 }
489 \f
490 /* Write a value out to the object file, using the appropriate endianness. */
491
492 void
493 md_number_to_chars (buf, val, n)
494 char * buf;
495 valueT val;
496 int n;
497 {
498 number_to_chars_bigendian (buf, val, n);
499 }
500
501 /* Turn a string in input_line_pointer into a floating point constant of type
502 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
503 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
504 */
505
506 /* Equal to MAX_PRECISION in atof-ieee.c */
507 #define MAX_LITTLENUMS 6
508
509 char *
510 md_atof (type, litP, sizeP)
511 char type;
512 char * litP;
513 int * sizeP;
514 {
515 int i;
516 int prec;
517 LITTLENUM_TYPE words [MAX_LITTLENUMS];
518 char * t;
519 char * atof_ieee ();
520
521 switch (type)
522 {
523 case 'f':
524 case 'F':
525 case 's':
526 case 'S':
527 prec = 2;
528 break;
529
530 case 'd':
531 case 'D':
532 case 'r':
533 case 'R':
534 prec = 4;
535 break;
536
537 /* FIXME: Some targets allow other format chars for bigger sizes here. */
538
539 default:
540 * sizeP = 0;
541 return _("Bad call to md_atof()");
542 }
543
544 t = atof_ieee (input_line_pointer, type, words);
545 if (t)
546 input_line_pointer = t;
547 * sizeP = prec * sizeof (LITTLENUM_TYPE);
548
549 for (i = 0; i < prec; i++)
550 {
551 md_number_to_chars (litP, (valueT) words[i],
552 sizeof (LITTLENUM_TYPE));
553 litP += sizeof (LITTLENUM_TYPE);
554 }
555
556 return 0;
557 }
558
559 /* Worker function for fr30_is_colon_insn(). */
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, advances 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",
605 "bp", "bv", "bnv", "blt", "bge",
606 "ble", "bgt", "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 if (tolower (start [len]) != insn [len])
619 break;
620
621 if (len == -1)
622 return restore_colon (1);
623 }
624 }
625
626 /* Nope - it is a normal label. */
627 return 0;
628 }
629
630 /* Check to see if the text following the colon is '8' */
631 if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
632 return restore_colon (2);
633
634 /* Check to see if the text following the colon is '20' */
635 else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
636 return restore_colon (3);
637
638 /* Check to see if the text following the colon is '32' */
639 else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
640 return restore_colon (3);
641
642 return 0;
643 }
644
645 boolean
646 fr30_fix_adjustable (fixP)
647 fixS * fixP;
648 {
649 if (fixP->fx_addsy == NULL)
650 return 1;
651
652 #if 0
653 /* Prevent all adjustments to global symbols. */
654 if (S_IS_EXTERN (fixP->fx_addsy))
655 return 0;
656
657 if (S_IS_WEAK (fixP->fx_addsy))
658 return 0;
659 #endif
660
661 /* We need the symbol name for the VTABLE entries */
662 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
663 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
664 return 0;
665
666 return 1;
667 }