Back to index

cell-binutils  2.17cvs20070401
tc-fr30.c
Go to the documentation of this file.
00001 /* tc-fr30.c -- Assembler for the Fujitsu FR30.
00002    Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006
00003    Free Software Foundation, Inc.
00004 
00005    This file is part of GAS, the GNU Assembler.
00006 
00007    GAS is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2, or (at your option)
00010    any later version.
00011 
00012    GAS is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with GAS; see the file COPYING.  If not, write to
00019    the Free Software Foundation, 51 Franklin Street - Fifth Floor,
00020    Boston, MA 02110-1301, USA.  */
00021 
00022 #include "as.h"
00023 #include "safe-ctype.h"
00024 #include "subsegs.h"
00025 #include "symcat.h"
00026 #include "opcodes/fr30-desc.h"
00027 #include "opcodes/fr30-opc.h"
00028 #include "cgen.h"
00029 
00030 /* Structure to hold all of the different components describing
00031    an individual instruction.  */
00032 typedef struct
00033 {
00034   const CGEN_INSN *  insn;
00035   const CGEN_INSN *  orig_insn;
00036   CGEN_FIELDS        fields;
00037 #if CGEN_INT_INSN_P
00038   CGEN_INSN_INT         buffer [1];
00039 #define INSN_VALUE(buf) (*(buf))
00040 #else
00041   unsigned char         buffer [CGEN_MAX_INSN_SIZE];
00042 #define INSN_VALUE(buf) (buf)
00043 #endif
00044   char *             addr;
00045   fragS *            frag;
00046   int                   num_fixups;
00047   fixS *                fixups [GAS_CGEN_MAX_FIXUPS];
00048   int                   indices [MAX_OPERAND_INSTANCES];
00049 }
00050 fr30_insn;
00051 
00052 const char comment_chars[]        = ";";
00053 const char line_comment_chars[]   = "#";
00054 const char line_separator_chars[] = "|";
00055 const char EXP_CHARS[]            = "eE";
00056 const char FLT_CHARS[]            = "dD";
00057 
00058 #define FR30_SHORTOPTS ""
00059 const char * md_shortopts = FR30_SHORTOPTS;
00060 
00061 struct option md_longopts[] =
00062 {
00063   {NULL, no_argument, NULL, 0}
00064 };
00065 size_t md_longopts_size = sizeof (md_longopts);
00066 
00067 int
00068 md_parse_option (int c ATTRIBUTE_UNUSED,
00069                char *arg ATTRIBUTE_UNUSED)
00070 {
00071   switch (c)
00072     {
00073     default:
00074       return 0;
00075     }
00076   return 1;
00077 }
00078 
00079 void
00080 md_show_usage (FILE * stream)
00081 {
00082   fprintf (stream, _(" FR30 specific command line options:\n"));
00083 }
00084 
00085 /* The target specific pseudo-ops which we support.  */
00086 const pseudo_typeS md_pseudo_table[] =
00087 {
00088   { "word",   cons,         4 },
00089   { NULL,     NULL,         0 }
00090 };
00091 
00092 
00093 void
00094 md_begin (void)
00095 {
00096   /* Initialize the `cgen' interface.  */
00097 
00098   /* Set the machine number and endian.  */
00099   gas_cgen_cpu_desc = fr30_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0,
00100                                      CGEN_CPU_OPEN_ENDIAN,
00101                                      CGEN_ENDIAN_BIG,
00102                                      CGEN_CPU_OPEN_END);
00103   fr30_cgen_init_asm (gas_cgen_cpu_desc);
00104 
00105   /* This is a callback from cgen to gas to parse operands.  */
00106   cgen_set_parse_operand_fn (gas_cgen_cpu_desc, gas_cgen_parse_operand);
00107 }
00108 
00109 void
00110 md_assemble (char *str)
00111 {
00112   static int last_insn_had_delay_slot = 0;
00113   fr30_insn insn;
00114   char *errmsg;
00115 
00116   /* Initialize GAS's cgen interface for a new instruction.  */
00117   gas_cgen_init_parse ();
00118 
00119   insn.insn = fr30_cgen_assemble_insn
00120     (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
00121 
00122   if (!insn.insn)
00123     {
00124       as_bad (errmsg);
00125       return;
00126     }
00127 
00128   /* Doesn't really matter what we pass for RELAX_P here.  */
00129   gas_cgen_finish_insn (insn.insn, insn.buffer,
00130                      CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
00131 
00132   /* Warn about invalid insns in delay slots.  */
00133   if (last_insn_had_delay_slot
00134       && CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_NOT_IN_DELAY_SLOT))
00135     as_warn (_("Instruction %s not allowed in a delay slot."),
00136             CGEN_INSN_NAME (insn.insn));
00137 
00138   last_insn_had_delay_slot
00139     = CGEN_INSN_ATTR_VALUE (insn.insn, CGEN_INSN_DELAY_SLOT);
00140 }
00141 
00142 /* The syntax in the manual says constants begin with '#'.
00143    We just ignore it.  */
00144 
00145 void
00146 md_operand (expressionS * expressionP)
00147 {
00148   if (* input_line_pointer == '#')
00149     {
00150       input_line_pointer ++;
00151       expression (expressionP);
00152     }
00153 }
00154 
00155 valueT
00156 md_section_align (segT segment, valueT size)
00157 {
00158   int align = bfd_get_section_alignment (stdoutput, segment);
00159 
00160   return ((size + (1 << align) - 1) & (-1 << align));
00161 }
00162 
00163 symbolS *
00164 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
00165 {
00166   return NULL;
00167 }
00168 
00169 /* Interface to relax_segment.  */
00170 
00171 /* FIXME: Build table by hand, get it working, then machine generate.  */
00172 
00173 const relax_typeS md_relax_table[] =
00174 {
00175 /* The fields are:
00176    1) most positive reach of this state,
00177    2) most negative reach of this state,
00178    3) how many bytes this mode will add to the size of the current frag
00179    4) which index into the table to try if we can't fit into this one.  */
00180 
00181   /* The first entry must be unused because an `rlx_more' value of zero ends
00182      each list.  */
00183   {1, 1, 0, 0},
00184 
00185   /* The displacement used by GAS is from the end of the 2 byte insn,
00186      so we subtract 2 from the following.  */
00187   /* 16 bit insn, 8 bit disp -> 10 bit range.
00188      This doesn't handle a branch in the right slot at the border:
00189      the "& -4" isn't taken into account.  It's not important enough to
00190      complicate things over it, so we subtract an extra 2 (or + 2 in -ve
00191      case).  */
00192   {511 - 2 - 2, -512 - 2 + 2, 0, 2 },
00193   /* 32 bit insn, 24 bit disp -> 26 bit range.  */
00194   {0x2000000 - 1 - 2, -0x2000000 - 2, 2, 0 },
00195   /* Same thing, but with leading nop for alignment.  */
00196   {0x2000000 - 1 - 2, -0x2000000 - 2, 4, 0 }
00197 };
00198 
00199 /* Return an initial guess of the length by which a fragment must grow to
00200    hold a branch to reach its destination.
00201    Also updates fr_type/fr_subtype as necessary.
00202 
00203    Called just before doing relaxation.
00204    Any symbol that is now undefined will not become defined.
00205    The guess for fr_var is ACTUALLY the growth beyond fr_fix.
00206    Whatever we do to grow fr_fix or fr_var contributes to our returned value.
00207    Although it may not be explicit in the frag, pretend fr_var starts with a
00208    0 value.  */
00209 
00210 int
00211 md_estimate_size_before_relax (fragS * fragP, segT segment)
00212 {
00213   /* The only thing we have to handle here are symbols outside of the
00214      current segment.  They may be undefined or in a different segment in
00215      which case linker scripts may place them anywhere.
00216      However, we can't finish the fragment here and emit the reloc as insn
00217      alignment requirements may move the insn about.  */
00218 
00219   if (S_GET_SEGMENT (fragP->fr_symbol) != segment)
00220     {
00221       /* The symbol is undefined in this segment.
00222         Change the relaxation subtype to the max allowable and leave
00223         all further handling to md_convert_frag.  */
00224       fragP->fr_subtype = 2;
00225 
00226       {
00227        const CGEN_INSN * insn;
00228        int               i;
00229 
00230        /* Update the recorded insn.
00231           Fortunately we don't have to look very far.
00232           FIXME: Change this to record in the instruction the next higher
00233           relaxable insn to use.  */
00234        for (i = 0, insn = fragP->fr_cgen.insn; i < 4; i++, insn++)
00235          {
00236            if ((strcmp (CGEN_INSN_MNEMONIC (insn),
00237                       CGEN_INSN_MNEMONIC (fragP->fr_cgen.insn))
00238                == 0)
00239               && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED))
00240              break;
00241          }
00242        if (i == 4)
00243          abort ();
00244 
00245        fragP->fr_cgen.insn = insn;
00246        return 2;
00247       }
00248     }
00249 
00250   /* Return the size of the variable part of the frag.  */
00251   return md_relax_table[fragP->fr_subtype].rlx_length;
00252 }
00253 
00254 /* *fragP has been relaxed to its final size, and now needs to have
00255    the bytes inside it modified to conform to the new size.
00256 
00257    Called after relaxation is finished.
00258    fragP->fr_type == rs_machine_dependent.
00259    fragP->fr_subtype is the subtype of what the address relaxed to.  */
00260 
00261 void
00262 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
00263                segT sec ATTRIBUTE_UNUSED,
00264                fragS *fragP ATTRIBUTE_UNUSED)
00265 {
00266 }
00267 
00268 /* Functions concerning relocs.  */
00269 
00270 /* The location from which a PC relative jump should be calculated,
00271    given a PC relative reloc.  */
00272 
00273 long
00274 md_pcrel_from_section (fixS * fixP, segT sec)
00275 {
00276   if (fixP->fx_addsy != (symbolS *) NULL
00277       && (! S_IS_DEFINED (fixP->fx_addsy)
00278          || S_GET_SEGMENT (fixP->fx_addsy) != sec))
00279     /* The symbol is undefined (or is defined but not in this section).
00280        Let the linker figure it out.  */
00281     return 0;
00282 
00283   return (fixP->fx_frag->fr_address + fixP->fx_where) & ~1;
00284 }
00285 
00286 /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
00287    Returns BFD_RELOC_NONE if no reloc type can be found.
00288    *FIXP may be modified if desired.  */
00289 
00290 bfd_reloc_code_real_type
00291 md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED,
00292                     const CGEN_OPERAND *operand,
00293                     fixS *fixP)
00294 {
00295   switch (operand->type)
00296     {
00297     case FR30_OPERAND_LABEL9:  fixP->fx_pcrel = 1; return BFD_RELOC_FR30_9_PCREL;
00298     case FR30_OPERAND_LABEL12: fixP->fx_pcrel = 1; return BFD_RELOC_FR30_12_PCREL;
00299     case FR30_OPERAND_DISP10:  return BFD_RELOC_FR30_10_IN_8;
00300     case FR30_OPERAND_DISP9:   return BFD_RELOC_FR30_9_IN_8;
00301     case FR30_OPERAND_DISP8:   return BFD_RELOC_FR30_8_IN_8;
00302     case FR30_OPERAND_UDISP6:  return BFD_RELOC_FR30_6_IN_4;
00303     case FR30_OPERAND_I8:      return BFD_RELOC_8;
00304     case FR30_OPERAND_I32:     return BFD_RELOC_FR30_48;
00305     case FR30_OPERAND_I20:     return BFD_RELOC_FR30_20;
00306     default : /* Avoid -Wall warning.  */
00307       break;
00308     }
00309 
00310   return BFD_RELOC_NONE;
00311 }
00312 
00313 /* Write a value out to the object file, using the appropriate endianness.  */
00314 
00315 void
00316 md_number_to_chars (char * buf, valueT val, int n)
00317 {
00318   number_to_chars_bigendian (buf, val, n);
00319 }
00320 
00321 /* Turn a string in input_line_pointer into a floating point constant of type
00322    type, and store the appropriate bytes in *litP.  The number of LITTLENUMS
00323    emitted is stored in *sizeP .  An error message is returned, or NULL on OK.
00324 */
00325 
00326 /* Equal to MAX_PRECISION in atof-ieee.c.  */
00327 #define MAX_LITTLENUMS 6
00328 
00329 char *
00330 md_atof (int type, char * litP, int * sizeP)
00331 {
00332   int              i;
00333   int              prec;
00334   LITTLENUM_TYPE   words [MAX_LITTLENUMS];
00335   char *           t;
00336 
00337   switch (type)
00338     {
00339     case 'f':
00340     case 'F':
00341     case 's':
00342     case 'S':
00343       prec = 2;
00344       break;
00345 
00346     case 'd':
00347     case 'D':
00348     case 'r':
00349     case 'R':
00350       prec = 4;
00351       break;
00352 
00353    /* FIXME: Some targets allow other format chars for bigger sizes here.  */
00354 
00355     default:
00356       * sizeP = 0;
00357       return _("Bad call to md_atof()");
00358     }
00359 
00360   t = atof_ieee (input_line_pointer, type, words);
00361   if (t)
00362     input_line_pointer = t;
00363   * sizeP = prec * sizeof (LITTLENUM_TYPE);
00364 
00365   for (i = 0; i < prec; i++)
00366     {
00367       md_number_to_chars (litP, (valueT) words[i],
00368                        sizeof (LITTLENUM_TYPE));
00369       litP += sizeof (LITTLENUM_TYPE);
00370     }
00371 
00372   return 0;
00373 }
00374 
00375 /* Worker function for fr30_is_colon_insn().  */
00376 static char
00377 restore_colon (int advance_i_l_p_by)
00378 {
00379   char c;
00380 
00381   /* Restore the colon, and advance input_line_pointer to
00382      the end of the new symbol.  */
00383   * input_line_pointer = ':';
00384   input_line_pointer += advance_i_l_p_by;
00385   c = * input_line_pointer;
00386   * input_line_pointer = 0;
00387 
00388   return c;
00389 }
00390 
00391 /* Determines if the symbol starting at START and ending in
00392    a colon that was at the location pointed to by INPUT_LINE_POINTER
00393    (but which has now been replaced bu a NUL) is in fact an
00394    LDI:8, LDI:20, LDI:32, CALL:D. JMP:D, RET:D or Bcc:D instruction.
00395    If it is, then it restores the colon, advances INPUT_LINE_POINTER
00396    to the real end of the instruction/symbol, and returns the character
00397    that really terminated the symbol.  Otherwise it returns 0.  */
00398 char
00399 fr30_is_colon_insn (char *  start)
00400 {
00401   char * i_l_p = input_line_pointer;
00402 
00403   /* Check to see if the symbol parsed so far is 'ldi'.  */
00404   if (   (start[0] != 'l' && start[0] != 'L')
00405       || (start[1] != 'd' && start[1] != 'D')
00406       || (start[2] != 'i' && start[2] != 'I')
00407       || start[3] != 0)
00408     {
00409       /* Nope - check to see a 'd' follows the colon.  */
00410       if (   (i_l_p[1] == 'd' || i_l_p[1] == 'D')
00411          && (i_l_p[2] == ' ' || i_l_p[2] == '\t' || i_l_p[2] == '\n'))
00412        {
00413          /* Yup - it might be delay slot instruction.  */
00414          int           i;
00415          static char * delay_insns [] =
00416          {
00417            "call", "jmp", "ret", "bra", "bno",
00418            "beq",  "bne", "bc",  "bnc", "bn",
00419            "bp",   "bv",  "bnv", "blt", "bge",
00420            "ble",  "bgt", "bls", "bhi"
00421          };
00422 
00423          for (i = sizeof (delay_insns) / sizeof (delay_insns[0]); i--;)
00424            {
00425              char * insn = delay_insns[i];
00426              int    len  = strlen (insn);
00427 
00428              if (start [len] != 0)
00429               continue;
00430 
00431              while (len --)
00432               if (TOLOWER (start [len]) != insn [len])
00433                 break;
00434 
00435              if (len == -1)
00436               return restore_colon (1);
00437            }
00438        }
00439 
00440       /* Nope - it is a normal label.  */
00441       return 0;
00442     }
00443 
00444   /* Check to see if the text following the colon is '8'.  */
00445   if (i_l_p[1] == '8' && (i_l_p[2] == ' ' || i_l_p[2] == '\t'))
00446     return restore_colon (2);
00447 
00448   /* Check to see if the text following the colon is '20'.  */
00449   else if (i_l_p[1] == '2' && i_l_p[2] =='0' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
00450     return restore_colon (3);
00451 
00452   /* Check to see if the text following the colon is '32'.  */
00453   else if (i_l_p[1] == '3' && i_l_p[2] =='2' && (i_l_p[3] == ' ' || i_l_p[3] == '\t'))
00454     return restore_colon (3);
00455 
00456   return 0;
00457 }
00458 
00459 bfd_boolean
00460 fr30_fix_adjustable (fixS * fixP)
00461 {
00462   /* We need the symbol name for the VTABLE entries.  */
00463   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
00464       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
00465     return 0;
00466 
00467   return 1;
00468 }