Convert unmaintained files over to ISO-C90 and fix formatting.
[platform/upstream/binutils.git] / gas / config / tc-fr30.c
1 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
2    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2005
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <stdio.h>
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "subsegs.h"
26 #include "symcat.h"
27 #include "opcodes/fr30-desc.h"
28 #include "opcodes/fr30-opc.h"
29 #include "cgen.h"
30
31 /* Structure to hold all of the different components describing
32    an individual instruction.  */
33 typedef struct
34 {
35   const CGEN_INSN *     insn;
36   const CGEN_INSN *     orig_insn;
37   CGEN_FIELDS           fields;
38 #if CGEN_INT_INSN_P
39   CGEN_INSN_INT         buffer [1];
40 #define INSN_VALUE(buf) (*(buf))
41 #else
42   unsigned char         buffer [CGEN_MAX_INSN_SIZE];
43 #define INSN_VALUE(buf) (buf)
44 #endif
45   char *                addr;
46   fragS *               frag;
47   int                   num_fixups;
48   fixS *                fixups [GAS_CGEN_MAX_FIXUPS];
49   int                   indices [MAX_OPERAND_INSTANCES];
50 }
51 fr30_insn;
52
53 const char comment_chars[]        = ";";
54 const char line_comment_chars[]   = "#";
55 const char line_separator_chars[] = "|";
56 const char EXP_CHARS[]            = "eE";
57 const char FLT_CHARS[]            = "dD";
58 \f
59 #define FR30_SHORTOPTS ""
60 const char * md_shortopts = FR30_SHORTOPTS;
61
62 struct option md_longopts[] =
63 {
64   {NULL, no_argument, NULL, 0}
65 };
66 size_t md_longopts_size = sizeof (md_longopts);
67
68 int
69 md_parse_option (int c ATTRIBUTE_UNUSED,
70                  char *arg ATTRIBUTE_UNUSED)
71 {
72   switch (c)
73     {
74     default:
75       return 0;
76     }
77   return 1;
78 }
79
80 void
81 md_show_usage (FILE * stream)
82 {
83   fprintf (stream, _(" FR30 specific command line options:\n"));
84 }
85
86 /* The target specific pseudo-ops which we support.  */
87 const pseudo_typeS md_pseudo_table[] =
88 {
89   { "word",     cons,           4 },
90   { NULL,       NULL,           0 }
91 };
92
93 \f
94 void
95 md_begin (void)
96 {
97   /* Initialize the `cgen' interface.  */
98
99   /* Set the machine number and endian.  */
100   gas_cgen_cpu_desc = fr30_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
101                                           CGEN_CPU_OPEN_ENDIAN,
102                                           CGEN_ENDIAN_BIG,
103                                           CGEN_CPU_OPEN_END);
104   fr30_cgen_init_asm (gas_cgen_cpu_desc);
105
106   /* This is a callback from cgen to gas to parse operands.  */
107   cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
108 }
109
110 void
111 md_assemble (char *str)
112 {
113   static int last_insn_had_delay_slot = 0;
114   fr30_insn insn;
115   char *errmsg;
116
117   /* Initialize GAS's cgen interface for a new instruction.  */
118   gas_cgen_init_parse ();
119
120   insn.insn = fr30_cgen_assemble_insn
121     (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
122
123   if (!insn.insn)
124     {
125       as_bad (errmsg);
126       return;
127     }
128
129   /* Doesn't really matter what we pass for RELAX_P here.  */
130   gas_cgen_finish_insn (insn.insn, insn.buffer,
131                         CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
132
133   /* Warn about invalid insns in delay slots.  */
134   if (last_insn_had_delay_slot
135       && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_NOT_IN_DELAY_SLOT))
136     as_warn (_("Instruction %s not allowed in a delay slot."),
137              CGEN_INSN_NAME (insn.insn));
138
139   last_insn_had_delay_slot
140     = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
141 }
142
143 /* The syntax in the manual says constants begin with '#'.
144    We just ignore it.  */
145
146 void
147 md_operand (expressionS * expressionP)
148 {
149   if (* input_line_pointer == '#')
150     {
151       input_line_pointer ++;
152       expression (expressionP);
153     }
154 }
155
156 valueT
157 md_section_align (segT segment, valueT size)
158 {
159   int align = bfd_get_section_alignment (stdoutput, segment);
160
161   return ((size + (1 << align) - 1) & (-1 << align));
162 }
163
164 symbolS *
165 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
166 {
167   return NULL;
168 }
169 \f
170 /* Interface to relax_segment.  */
171
172 /* FIXME: Build table by hand, get it working, then machine generate.  */
173
174 const relax_typeS md_relax_table[] =
175 {
176 /* The fields are:
177    1) most positive reach of this state,
178    2) most negative reach of this state,
179    3) how many bytes this mode will add to the size of the current frag
180    4) which index into the table to try if we can't fit into this one.  */
181
182   /* The first entry must be unused because an `rlx_more' value of zero ends
183      each list.  */
184   {1, 1, 0, 0},
185
186   /* The displacement used by GAS is from the end of the 2 byte insn,
187      so we subtract 2 from the following.  */
188   /* 16 bit insn, 8 bit disp -> 10 bit range.
189      This doesn't handle a branch in the right slot at the border:
190      the "& -4" isn't taken into account.  It's not important enough to
191      complicate things over it, so we subtract an extra 2 (or + 2 in -ve
192      case).  */
193   {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
194   /* 32 bit insn, 24 bit disp -> 26 bit range.  */
195   {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
196   /* Same thing, but with leading nop for alignment.  */
197   {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
198 };
199
200 /* Return an initial guess of the length by which a fragment must grow to
201    hold a branch to reach its destination.
202    Also updates fr_type/fr_subtype as necessary.
203
204    Called just before doing relaxation.
205    Any symbol that is now undefined will not become defined.
206    The guess for fr_var is ACTUALLY the growth beyond fr_fix.
207    Whatever we do to grow fr_fix or fr_var contributes to our returned value.
208    Although it may not be explicit in the frag, pretend fr_var starts with a
209    0 value.  */
210
211 int
212 md_estimate_size_before_relax (fragS * fragP, segT segment)
213 {
214   /* The only thing we have to handle here are symbols outside of the
215      current segment.  They may be undefined or in a different segment in
216      which case linker scripts may place them anywhere.
217      However, we can't finish the fragment here and emit the reloc as insn
218      alignment requirements may move the insn about.  */
219
220   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
221     {
222       /* The symbol is undefined in this segment.
223          Change the relaxation subtype to the max allowable and leave
224          all further handling to md_convert_frag.  */
225       fragP->fr_subtype = 2;
226
227       {
228         const CGEN_INSN * insn;
229         int               i;
230
231         /* Update the recorded insn.
232            Fortunately we don't have to look very far.
233            FIXME: Change this to record in the instruction the next higher
234            relaxable insn to use.  */
235         for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
236           {
237             if ((strcmp (CGEN_INSN_MNEMONIC (insn),
238                          CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
239                  == 0)
240                 && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
241               break;
242           }
243         if (i == 4)
244           abort ();
245
246         fragP->fr_cgen.insn = insn;
247         return 2;
248       }
249     }
250
251   /* Return the size of the variable part of the frag.  */
252   return md_relax_table[fragP->fr_subtype].rlx_length;
253 }
254
255 /* *fragP has been relaxed to its final size, and now needs to have
256    the bytes inside it modified to conform to the new size.
257
258    Called after relaxation is finished.
259    fragP->fr_type == rs_machine_dependent.
260    fragP->fr_subtype is the subtype of what the address relaxed to.  */
261
262 void
263 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
264                  segT sec ATTRIBUTE_UNUSED,
265                  fragS *fragP ATTRIBUTE_UNUSED)
266 {
267 }
268 \f
269 /* Functions concerning relocs.  */
270
271 /* The location from which a PC relative jump should be calculated,
272    given a PC relative reloc.  */
273
274 long
275 md_pcrel_from_section (fixS * fixP, segT sec)
276 {
277   if (fixP->fx_addsy != (symbolS *) NULL
278       && (! S_IS_DEFINED (fixP->fx_addsy)
279           || S_GET_SEGMENT (fixP->fx_addsy) != sec))
280     /* The symbol is undefined (or is defined but not in this section).
281        Let the linker figure it out.  */
282     return 0;
283
284   return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
285 }
286
287 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
288    Returns BFD_RELOC_NONE if no reloc type can be found.
289    *FIXP may be modified if desired.  */
290
291 bfd_reloc_code_real_type
292 md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED,
293                       const CGEN_OPERAND *operand,
294                       fixS *fixP)
295 {
296   switch (operand->type)
297     {
298     case FR30_OPERAND_LABEL9:  fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL;
299     case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL;
300     case FR30_OPERAND_DISP10:  return BFD_RELOC_FR30_10_IN_8;
301     case FR30_OPERAND_DISP9:   return BFD_RELOC_FR30_9_IN_8;
302     case FR30_OPERAND_DISP8:   return BFD_RELOC_FR30_8_IN_8;
303     case FR30_OPERAND_UDISP6:  return BFD_RELOC_FR30_6_IN_4;
304     case FR30_OPERAND_I8:      return BFD_RELOC_8;
305     case FR30_OPERAND_I32:     return BFD_RELOC_FR30_48;
306     case FR30_OPERAND_I20:     return BFD_RELOC_FR30_20;
307     default : /* Avoid -Wall warning.  */
308       break;
309     }
310
311   return BFD_RELOC_NONE;
312 }
313 \f
314 /* Write a value out to the object file, using the appropriate endianness.  */
315
316 void
317 md_number_to_chars (char * buf, valueT val, int n)
318 {
319   number_to_chars_bigendian (buf, val, n);
320 }
321
322 /* Turn a string in input_line_pointer into a floating point constant of type
323    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
324    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
325 */
326
327 /* Equal to MAX_PRECISION in atof-ieee.c.  */
328 #define MAX_LITTLENUMS 6
329
330 char *
331 md_atof (int type, char * litP, int * sizeP)
332 {
333   int              i;
334   int              prec;
335   LITTLENUM_TYPE   words [MAX_LITTLENUMS];
336   char *           t;
337
338   switch (type)
339     {
340     case 'f':
341     case 'F':
342     case 's':
343     case 'S':
344       prec = 2;
345       break;
346
347     case 'd':
348     case 'D':
349     case 'r':
350     case 'R':
351       prec = 4;
352       break;
353
354    /* FIXME: Some targets allow other format chars for bigger sizes here.  */
355
356     default:
357       * sizeP = 0;
358       return _("Bad call to md_atof()");
359     }
360
361   t = atof_ieee (input_line_pointer, type, words);
362   if (t)
363     input_line_pointer = t;
364   * sizeP = prec * sizeof (LITTLENUM_TYPE);
365
366   for (i = 0; i < prec; i++)
367     {
368       md_number_to_chars (litP, (valueT) words[i],
369                           sizeof (LITTLENUM_TYPE));
370       litP += sizeof (LITTLENUM_TYPE);
371     }
372
373   return 0;
374 }
375
376 /* Worker function for fr30_is_colon_insn().  */
377 static char
378 restore_colon (int advance_i_l_p_by)
379 {
380   char c;
381
382   /* Restore the colon, and advance input_line_pointer to
383      the end of the new symbol.  */
384   * input_line_pointer = ':';
385   input_line_pointer += advance_i_l_p_by;
386   c = * input_line_pointer;
387   * input_line_pointer = 0;
388
389   return c;
390 }
391
392 /* Determines if the symbol starting at START and ending in
393    a colon that was at the location pointed to by INPUT_LINE_POINTER
394    (but which has now been replaced bu a NUL) is in fact an
395    LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
396    If it is, then it restores the colon, advances INPUT_LINE_POINTER
397    to the real end of the instruction/symbol, and returns the character
398    that really terminated the symbol.  Otherwise it returns 0.  */
399 char
400 fr30_is_colon_insn (char *  start)
401 {
402   char * i_l_p = input_line_pointer;
403
404   /* Check to see if the symbol parsed so far is 'ldi'.  */
405   if (   (start[0] != 'l' && start[0] != 'L')
406       || (start[1] != 'd' && start[1] != 'D')
407       || (start[2] != 'i' && start[2] != 'I')
408       || start[3] != 0)
409     {
410       /* Nope - check to see a 'd' follows the colon.  */
411       if (   (i_l_p[1] == 'd' || i_l_p[1] == 'D')
412           && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n'))
413         {
414           /* Yup - it might be delay slot instruction.  */
415           int           i;
416           static char * delay_insns [] =
417           {
418             "call", "jmp", "ret", "bra", "bno",
419             "beq",  "bne", "bc",  "bnc", "bn",
420             "bp",   "bv",  "bnv", "blt", "bge",
421             "ble",  "bgt", "bls", "bhi"
422           };
423
424           for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;)
425             {
426               char * insn = delay_insns[i];
427               int    len  = strlen (insn);
428
429               if (start [len] != 0)
430                 continue;
431
432               while (len --)
433                 if (TOLOWER (start [len]) != insn [len])
434                   break;
435
436               if (len == -1)
437                 return restore_colon (1);
438             }
439         }
440
441       /* Nope - it is a normal label.  */
442       return 0;
443     }
444
445   /* Check to see if the text following the colon is '8'.  */
446   if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
447     return restore_colon (2);
448
449   /* Check to see if the text following the colon is '20'.  */
450   else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
451     return restore_colon (3);
452
453   /* Check to see if the text following the colon is '32'.  */
454   else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
455     return restore_colon (3);
456
457   return 0;
458 }
459
460 bfd_boolean
461 fr30_fix_adjustable (fixS * fixP)
462 {
463   /* We need the symbol name for the VTABLE entries.  */
464   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
465       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
466     return 0;
467
468   return 1;
469 }