2000-09-25 Kazu Hirata <kazu@hxi.com>
[external/binutils.git] / gas / config / tc-fr30.c
1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2    Copyright (C) 1998, 1999, 2000 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 (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 (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_VALUE (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 += symbol_get_frag (fragP->fr_symbol)->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) & ~1;
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 (operand->type)
455     {
456     case FR30_OPERAND_LABEL9:  fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL;
457     case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL;
458     case FR30_OPERAND_DISP10:  return BFD_RELOC_FR30_10_IN_8;
459     case FR30_OPERAND_DISP9:   return BFD_RELOC_FR30_9_IN_8;
460     case FR30_OPERAND_DISP8:   return BFD_RELOC_FR30_8_IN_8;
461     case FR30_OPERAND_UDISP6:  return BFD_RELOC_FR30_6_IN_4;
462     case FR30_OPERAND_I8:      return BFD_RELOC_8;
463     case FR30_OPERAND_I32:     return BFD_RELOC_FR30_48;
464     case FR30_OPERAND_I20:     return BFD_RELOC_FR30_20;
465     default : /* avoid -Wall warning */
466       break;
467     }
468
469   return BFD_RELOC_NONE;
470 }
471
472 /* See whether we need to force a relocation into the output file.
473    This is used to force out switch and PC relative relocations when
474    relaxing.  */
475
476 int
477 fr30_force_relocation (fix)
478      fixS * fix;
479 {
480   if (   fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
481       || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
482     return 1;
483
484   return 0;
485 }
486 \f
487 /* Write a value out to the object file, using the appropriate endianness.  */
488
489 void
490 md_number_to_chars (buf, val, n)
491      char * buf;
492      valueT val;
493      int    n;
494 {
495   number_to_chars_bigendian (buf, val, n);
496 }
497
498 /* Turn a string in input_line_pointer into a floating point constant of type
499    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
500    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
501 */
502
503 /* Equal to MAX_PRECISION in atof-ieee.c */
504 #define MAX_LITTLENUMS 6
505
506 char *
507 md_atof (type, litP, sizeP)
508      char   type;
509      char * litP;
510      int *  sizeP;
511 {
512   int              i;
513   int              prec;
514   LITTLENUM_TYPE   words [MAX_LITTLENUMS];
515   char *           t;
516   char *           atof_ieee ();
517
518   switch (type)
519     {
520     case 'f':
521     case 'F':
522     case 's':
523     case 'S':
524       prec = 2;
525       break;
526
527     case 'd':
528     case 'D':
529     case 'r':
530     case 'R':
531       prec = 4;
532       break;
533
534    /* FIXME: Some targets allow other format chars for bigger sizes here.  */
535
536     default:
537       * sizeP = 0;
538       return _("Bad call to md_atof()");
539     }
540
541   t = atof_ieee (input_line_pointer, type, words);
542   if (t)
543     input_line_pointer = t;
544   * sizeP = prec * sizeof (LITTLENUM_TYPE);
545
546   for (i = 0; i < prec; i++)
547     {
548       md_number_to_chars (litP, (valueT) words[i],
549                           sizeof (LITTLENUM_TYPE));
550       litP += sizeof (LITTLENUM_TYPE);
551     }
552
553   return 0;
554 }
555
556 /* Worker function for fr30_is_colon_insn().  */
557 static char
558 restore_colon (advance_i_l_p_by)
559      int advance_i_l_p_by;
560 {
561   char c;
562
563   /* Restore the colon, and advance input_line_pointer to
564      the end of the new symbol.  */
565   * input_line_pointer = ':';
566   input_line_pointer += advance_i_l_p_by;
567   c = * input_line_pointer;
568   * input_line_pointer = 0;
569
570   return c;
571 }
572
573 /* Determines if the symbol starting at START and ending in
574    a colon that was at the location pointed to by INPUT_LINE_POINTER
575    (but which has now been replaced bu a NUL) is in fact an
576    LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
577    If it is, then it restores the colon, advances INPUT_LINE_POINTER
578    to the real end of the instruction/symbol, and returns the character
579    that really terminated the symbol.  Otherwise it returns 0.  */
580 char
581 fr30_is_colon_insn (start)
582      char *  start;
583 {
584   char * i_l_p = input_line_pointer;
585
586   /* Check to see if the symbol parsed so far is 'ldi'  */
587   if (   (start[0] != 'l' && start[0] != 'L')
588       || (start[1] != 'd' && start[1] != 'D')
589       || (start[2] != 'i' && start[2] != 'I')
590       || start[3] != 0)
591     {
592       /* Nope - check to see a 'd' follows the colon.  */
593       if (   (i_l_p[1] == 'd' || i_l_p[1] == 'D')
594           && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n'))
595         {
596           /* Yup - it might be delay slot instruction.  */
597           int           i;
598           static char * delay_insns [] =
599           {
600             "call", "jmp", "ret", "bra", "bno",
601             "beq",  "bne", "bc",  "bnc", "bn",
602             "bp",   "bv",  "bnv", "blt", "bge",
603             "ble",  "bgt", "bls", "bhi"
604           };
605
606           for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;)
607             {
608               char * insn = delay_insns[i];
609               int    len  = strlen (insn);
610
611               if (start [len] != 0)
612                 continue;
613
614               while (len --)
615                 if (tolower (start [len]) != insn [len])
616                   break;
617
618               if (len == -1)
619                 return restore_colon (1);
620             }
621         }
622
623       /* Nope - it is a normal label.  */
624       return 0;
625     }
626
627   /* Check to see if the text following the colon is '8' */
628   if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
629     return restore_colon (2);
630
631   /* Check to see if the text following the colon is '20' */
632   else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
633     return restore_colon (3);
634
635   /* Check to see if the text following the colon is '32' */
636   else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
637     return restore_colon (3);
638
639   return 0;
640 }
641
642 boolean
643 fr30_fix_adjustable (fixP)
644    fixS * fixP;
645 {
646   if (fixP->fx_addsy == NULL)
647     return 1;
648
649 #if 0
650   /* Prevent all adjustments to global symbols.  */
651   if (S_IS_EXTERN (fixP->fx_addsy))
652     return 0;
653
654   if (S_IS_WEAK (fixP->fx_addsy))
655     return 0;
656 #endif
657
658   /* We need the symbol name for the VTABLE entries */
659   if (   fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
660       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
661     return 0;
662
663   return 1;
664 }