* config/tc-cris.c (md_estimate_size_before_relax) <case
[deliverable/binutils-gdb.git] / gas / config / tc-fr30.c
1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2 Copyright 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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 (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);
110
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);
113 }
114
115 void
116 md_assemble (str)
117 char * str;
118 {
119 static int last_insn_had_delay_slot = 0;
120 fr30_insn insn;
121 char * errmsg;
122 char * str2 = NULL;
123
124 /* Initialize GAS's cgen interface for a new instruction. */
125 gas_cgen_init_parse ();
126
127 insn.insn = fr30_cgen_assemble_insn
128 (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
129
130 if (!insn.insn)
131 {
132 as_bad (errmsg);
133 return;
134 }
135
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);
139
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));
145
146 last_insn_had_delay_slot
147 = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
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 (segment, fragP, stretch)
212 segT segment;
213 fragS * fragP;
214 long stretch;
215 {
216 /* Address of branch insn. */
217 long address = fragP->fr_address + fragP->fr_fix - 2;
218 long growth = 0;
219
220 /* Keep 32 bit insns aligned on 32 bit boundaries. */
221 if (fragP->fr_subtype == 2)
222 {
223 if ((address & 3) != 0)
224 {
225 fragP->fr_subtype = 3;
226 growth = 2;
227 }
228 }
229 else if (fragP->fr_subtype == 3)
230 {
231 if ((address & 3) == 0)
232 {
233 fragP->fr_subtype = 2;
234 growth = -2;
235 }
236 }
237 else
238 {
239 growth = relax_frag (segment, fragP, stretch);
240
241 /* Long jump on odd halfword boundary? */
242 if (fragP->fr_subtype == 2 && (address & 3) != 0)
243 {
244 fragP->fr_subtype = 3;
245 growth += 2;
246 }
247 }
248
249 return growth;
250 }
251
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.
255
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. */
262
263 int
264 md_estimate_size_before_relax (fragP, segment)
265 fragS * fragP;
266 segT segment;
267 {
268 int old_fr_fix = fragP->fr_fix;
269
270 /* The only thing we have to handle here are symbols outside of the
271 current segment. They may be undefined or in a different segment in
272 which case linker scripts may place them anywhere.
273 However, we can't finish the fragment here and emit the reloc as insn
274 alignment requirements may move the insn about. */
275
276 if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
277 {
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;
282
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;
286
287 /* Increase known (fixed) size of fragment. */
288 fragP->fr_fix += 2;
289
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);
297
298 /* Mark this fragment as finished. */
299 frag_wane (fragP);
300 #else
301 {
302 const CGEN_INSN * insn;
303 int i;
304
305 /* Update the recorded insn.
306 Fortunately we don't have to look very far.
307 FIXME: Change this to record in the instruction the next higher
308 relaxable insn to use. */
309 for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
310 {
311 if ((strcmp (CGEN_INSN_MNEMONIC (insn),
312 CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
313 == 0)
314 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX))
315 break;
316 }
317 if (i == 4)
318 abort ();
319
320 fragP->fr_cgen.insn = insn;
321 return 2;
322 }
323 #endif
324 }
325
326 return (fragP->fr_var + fragP->fr_fix - old_fr_fix);
327 }
328
329 /* *fragP has been relaxed to its final size, and now needs to have
330 the bytes inside it modified to conform to the new size.
331
332 Called after relaxation is finished.
333 fragP->fr_type == rs_machine_dependent.
334 fragP->fr_subtype is the subtype of what the address relaxed to. */
335
336 void
337 md_convert_frag (abfd, sec, fragP)
338 bfd * abfd;
339 segT sec;
340 fragS * fragP;
341 {
342 #if 0
343 char * opcode;
344 char * displacement;
345 int target_address;
346 int opcode_address;
347 int extension;
348 int addend;
349
350 opcode = fragP->fr_opcode;
351
352 /* Address opcode resides at in file space. */
353 opcode_address = fragP->fr_address + fragP->fr_fix - 2;
354
355 switch (fragP->fr_subtype)
356 {
357 case 1 :
358 extension = 0;
359 displacement = & opcode[1];
360 break;
361 case 2 :
362 opcode[0] |= 0x80;
363 extension = 2;
364 displacement = & opcode[1];
365 break;
366 case 3 :
367 opcode[2] = opcode[0] | 0x80;
368 md_number_to_chars (opcode, PAR_NOP_INSN, 2);
369 opcode_address += 2;
370 extension = 4;
371 displacement = & opcode[3];
372 break;
373 default :
374 abort ();
375 }
376
377 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
378 {
379 /* symbol must be resolved by linker */
380 if (fragP->fr_offset & 3)
381 as_warn (_("Addend to unresolved symbol not on word boundary."));
382 addend = fragP->fr_offset >> 2;
383 }
384 else
385 {
386 /* Address we want to reach in file space. */
387 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
388 target_address += symbol_get_frag (fragP->fr_symbol)->fr_address;
389 addend = (target_address - (opcode_address & -4)) >> 2;
390 }
391
392 /* Create a relocation for symbols that must be resolved by the linker.
393 Otherwise output the completed insn. */
394
395 if (S_GET_SEGMENT (fragP->fr_symbol) != sec)
396 {
397 assert (fragP->fr_subtype != 1);
398 assert (fragP->fr_cgen.insn != 0);
399 gas_cgen_record_fixup (fragP,
400 /* Offset of branch insn in frag. */
401 fragP->fr_fix + extension - 4,
402 fragP->fr_cgen.insn,
403 4 /*length*/,
404 /* FIXME: quick hack */
405 #if 0
406 CGEN_OPERAND_ENTRY (fragP->fr_cgen.opindex),
407 #else
408 CGEN_OPERAND_ENTRY (FR30_OPERAND_DISP24),
409 #endif
410 fragP->fr_cgen.opinfo,
411 fragP->fr_symbol, fragP->fr_offset);
412 }
413
414 #define SIZE_FROM_RELAX_STATE(n) ((n) == 1 ? 1 : 3)
415
416 md_number_to_chars (displacement, (valueT) addend,
417 SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
418
419 fragP->fr_fix += extension;
420 #endif
421 }
422 \f
423 /* Functions concerning relocs. */
424
425 /* The location from which a PC relative jump should be calculated,
426 given a PC relative reloc. */
427
428 long
429 md_pcrel_from_section (fixP, sec)
430 fixS * fixP;
431 segT sec;
432 {
433 if (fixP->fx_addsy != (symbolS *) NULL
434 && (! S_IS_DEFINED (fixP->fx_addsy)
435 || S_GET_SEGMENT (fixP->fx_addsy) != sec))
436 {
437 /* The symbol is undefined (or is defined but not in this section).
438 Let the linker figure it out. */
439 return 0;
440 }
441
442 return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
443 }
444
445 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
446 Returns BFD_RELOC_NONE if no reloc type can be found.
447 *FIXP may be modified if desired. */
448
449 bfd_reloc_code_real_type
450 md_cgen_lookup_reloc (insn, operand, fixP)
451 const CGEN_INSN * insn;
452 const CGEN_OPERAND * operand;
453 fixS * fixP;
454 {
455 switch (operand->type)
456 {
457 case FR30_OPERAND_LABEL9: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL;
458 case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL;
459 case FR30_OPERAND_DISP10: return BFD_RELOC_FR30_10_IN_8;
460 case FR30_OPERAND_DISP9: return BFD_RELOC_FR30_9_IN_8;
461 case FR30_OPERAND_DISP8: return BFD_RELOC_FR30_8_IN_8;
462 case FR30_OPERAND_UDISP6: return BFD_RELOC_FR30_6_IN_4;
463 case FR30_OPERAND_I8: return BFD_RELOC_8;
464 case FR30_OPERAND_I32: return BFD_RELOC_FR30_48;
465 case FR30_OPERAND_I20: return BFD_RELOC_FR30_20;
466 default : /* avoid -Wall warning */
467 break;
468 }
469
470 return BFD_RELOC_NONE;
471 }
472
473 /* See whether we need to force a relocation into the output file.
474 This is used to force out switch and PC relative relocations when
475 relaxing. */
476
477 int
478 fr30_force_relocation (fix)
479 fixS * fix;
480 {
481 if ( fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
482 || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
483 return 1;
484
485 return 0;
486 }
487 \f
488 /* Write a value out to the object file, using the appropriate endianness. */
489
490 void
491 md_number_to_chars (buf, val, n)
492 char * buf;
493 valueT val;
494 int n;
495 {
496 number_to_chars_bigendian (buf, val, n);
497 }
498
499 /* Turn a string in input_line_pointer into a floating point constant of type
500 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
501 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
502 */
503
504 /* Equal to MAX_PRECISION in atof-ieee.c */
505 #define MAX_LITTLENUMS 6
506
507 char *
508 md_atof (type, litP, sizeP)
509 char type;
510 char * litP;
511 int * sizeP;
512 {
513 int i;
514 int prec;
515 LITTLENUM_TYPE words [MAX_LITTLENUMS];
516 char * t;
517 char * atof_ieee ();
518
519 switch (type)
520 {
521 case 'f':
522 case 'F':
523 case 's':
524 case 'S':
525 prec = 2;
526 break;
527
528 case 'd':
529 case 'D':
530 case 'r':
531 case 'R':
532 prec = 4;
533 break;
534
535 /* FIXME: Some targets allow other format chars for bigger sizes here. */
536
537 default:
538 * sizeP = 0;
539 return _("Bad call to md_atof()");
540 }
541
542 t = atof_ieee (input_line_pointer, type, words);
543 if (t)
544 input_line_pointer = t;
545 * sizeP = prec * sizeof (LITTLENUM_TYPE);
546
547 for (i = 0; i < prec; i++)
548 {
549 md_number_to_chars (litP, (valueT) words[i],
550 sizeof (LITTLENUM_TYPE));
551 litP += sizeof (LITTLENUM_TYPE);
552 }
553
554 return 0;
555 }
556
557 /* Worker function for fr30_is_colon_insn(). */
558 static char
559 restore_colon (advance_i_l_p_by)
560 int advance_i_l_p_by;
561 {
562 char c;
563
564 /* Restore the colon, and advance input_line_pointer to
565 the end of the new symbol. */
566 * input_line_pointer = ':';
567 input_line_pointer += advance_i_l_p_by;
568 c = * input_line_pointer;
569 * input_line_pointer = 0;
570
571 return c;
572 }
573
574 /* Determines if the symbol starting at START and ending in
575 a colon that was at the location pointed to by INPUT_LINE_POINTER
576 (but which has now been replaced bu a NUL) is in fact an
577 LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
578 If it is, then it restores the colon, advances INPUT_LINE_POINTER
579 to the real end of the instruction/symbol, and returns the character
580 that really terminated the symbol. Otherwise it returns 0. */
581 char
582 fr30_is_colon_insn (start)
583 char * start;
584 {
585 char * i_l_p = input_line_pointer;
586
587 /* Check to see if the symbol parsed so far is 'ldi' */
588 if ( (start[0] != 'l' && start[0] != 'L')
589 || (start[1] != 'd' && start[1] != 'D')
590 || (start[2] != 'i' && start[2] != 'I')
591 || start[3] != 0)
592 {
593 /* Nope - check to see a 'd' follows the colon. */
594 if ( (i_l_p[1] == 'd' || i_l_p[1] == 'D')
595 && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n'))
596 {
597 /* Yup - it might be delay slot instruction. */
598 int i;
599 static char * delay_insns [] =
600 {
601 "call", "jmp", "ret", "bra", "bno",
602 "beq", "bne", "bc", "bnc", "bn",
603 "bp", "bv", "bnv", "blt", "bge",
604 "ble", "bgt", "bls", "bhi"
605 };
606
607 for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;)
608 {
609 char * insn = delay_insns[i];
610 int len = strlen (insn);
611
612 if (start [len] != 0)
613 continue;
614
615 while (len --)
616 if (tolower (start [len]) != insn [len])
617 break;
618
619 if (len == -1)
620 return restore_colon (1);
621 }
622 }
623
624 /* Nope - it is a normal label. */
625 return 0;
626 }
627
628 /* Check to see if the text following the colon is '8' */
629 if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
630 return restore_colon (2);
631
632 /* Check to see if the text following the colon is '20' */
633 else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
634 return restore_colon (3);
635
636 /* Check to see if the text following the colon is '32' */
637 else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
638 return restore_colon (3);
639
640 return 0;
641 }
642
643 boolean
644 fr30_fix_adjustable (fixP)
645 fixS * fixP;
646 {
647 if (fixP->fx_addsy == NULL)
648 return 1;
649
650 #if 0
651 /* Prevent all adjustments to global symbols. */
652 if (S_IS_EXTERN (fixP->fx_addsy))
653 return 0;
654
655 if (S_IS_WEAK (fixP->fx_addsy))
656 return 0;
657 #endif
658
659 /* We need the symbol name for the VTABLE entries */
660 if ( fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
661 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
662 return 0;
663
664 return 1;
665 }
This page took 0.043259 seconds and 4 git commands to generate.