Back to index

cell-binutils  2.17cvs20070401
tc-v850.c
Go to the documentation of this file.
00001 /* tc-v850.c -- Assembler code for the NEC V850
00002    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00003    2006  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 "opcode/v850.h"
00026 #include "dwarf2dbg.h"
00027 
00028 /* Sign-extend a 16-bit number.  */
00029 #define SEXT16(x)    ((((x) & 0xffff) ^ (~0x7fff)) + 0x8000)
00030 
00031 /* Temporarily holds the reloc in a cons expression.  */
00032 static bfd_reloc_code_real_type hold_cons_reloc = BFD_RELOC_UNUSED;
00033 
00034 /* Set to TRUE if we want to be pedantic about signed overflows.  */
00035 static bfd_boolean warn_signed_overflows   = FALSE;
00036 static bfd_boolean warn_unsigned_overflows = FALSE;
00037 
00038 /* Indicates the target BFD machine number.  */
00039 static int machine = -1;
00040 
00041 /* Indicates the target processor(s) for the assemble.  */
00042 static int processor_mask = -1;
00043 
00044 /* Structure to hold information about predefined registers.  */
00045 struct reg_name
00046 {
00047   const char *name;
00048   int value;
00049 };
00050 
00051 /* Generic assembler global variables which must be defined by all
00052    targets.  */
00053 
00054 /* Characters which always start a comment.  */
00055 const char comment_chars[] = "#";
00056 
00057 /* Characters which start a comment at the beginning of a line.  */
00058 const char line_comment_chars[] = ";#";
00059 
00060 /* Characters which may be used to separate multiple commands on a
00061    single line.  */
00062 const char line_separator_chars[] = ";";
00063 
00064 /* Characters which are used to indicate an exponent in a floating
00065    point number.  */
00066 const char EXP_CHARS[] = "eE";
00067 
00068 /* Characters which mean that a number is a floating point constant,
00069    as in 0d1.0.  */
00070 const char FLT_CHARS[] = "dD";
00071 
00072 const relax_typeS md_relax_table[] =
00073 {
00074   /* Conditional branches.  */
00075   {0xff,     -0x100,    2, 1},
00076   {0x1fffff, -0x200000, 6, 0},
00077   /* Unconditional branches.  */
00078   {0xff,     -0x100,    2, 3},
00079   {0x1fffff, -0x200000, 4, 0},
00080 };
00081 
00082 static int  v850_relax = 0;
00083 
00084 /* Fixups.  */
00085 #define MAX_INSN_FIXUPS   5
00086 
00087 struct v850_fixup
00088 {
00089   expressionS exp;
00090   int opindex;
00091   bfd_reloc_code_real_type reloc;
00092 };
00093 
00094 struct v850_fixup fixups[MAX_INSN_FIXUPS];
00095 static int fc;
00096 
00097 struct v850_seg_entry
00098 {
00099   segT s;
00100   const char *name;
00101   flagword flags;
00102 };
00103 
00104 struct v850_seg_entry v850_seg_table[] =
00105 {
00106   { NULL, ".sdata",
00107     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
00108     | SEC_SMALL_DATA },
00109   { NULL, ".tdata",
00110     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
00111   { NULL, ".zdata",
00112     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
00113   { NULL, ".sbss",
00114     SEC_ALLOC | SEC_SMALL_DATA },
00115   { NULL, ".tbss",
00116     SEC_ALLOC },
00117   { NULL, ".zbss",
00118     SEC_ALLOC},
00119   { NULL, ".rosdata",
00120     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_DATA
00121     | SEC_HAS_CONTENTS | SEC_SMALL_DATA },
00122   { NULL, ".rozdata",
00123     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_DATA
00124     | SEC_HAS_CONTENTS },
00125   { NULL, ".scommon",
00126     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
00127     | SEC_SMALL_DATA | SEC_IS_COMMON },
00128   { NULL, ".tcommon",
00129     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
00130     | SEC_IS_COMMON },
00131   { NULL, ".zcommon",
00132     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
00133     | SEC_IS_COMMON },
00134   { NULL, ".call_table_data",
00135     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
00136   { NULL, ".call_table_text",
00137     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_CODE
00138     | SEC_HAS_CONTENTS},
00139   { NULL, ".bss",
00140     SEC_ALLOC }
00141 };
00142 
00143 #define SDATA_SECTION              0
00144 #define TDATA_SECTION              1
00145 #define ZDATA_SECTION              2
00146 #define SBSS_SECTION        3
00147 #define TBSS_SECTION        4
00148 #define ZBSS_SECTION        5
00149 #define ROSDATA_SECTION            6
00150 #define ROZDATA_SECTION            7
00151 #define SCOMMON_SECTION            8
00152 #define TCOMMON_SECTION            9
00153 #define ZCOMMON_SECTION            10
00154 #define CALL_TABLE_DATA_SECTION    11
00155 #define CALL_TABLE_TEXT_SECTION    12
00156 #define BSS_SECTION         13
00157 
00158 static void
00159 do_v850_seg (int i, subsegT sub)
00160 {
00161   struct v850_seg_entry *seg = v850_seg_table + i;
00162 
00163   obj_elf_section_change_hook ();
00164 
00165   if (seg->s != NULL)
00166     subseg_set (seg->s, sub);
00167   else
00168     {
00169       seg->s = subseg_new (seg->name, sub);
00170       bfd_set_section_flags (stdoutput, seg->s, seg->flags);
00171       if ((seg->flags & SEC_LOAD) == 0)
00172        seg_info (seg->s)->bss = 1;
00173     }
00174 }
00175 
00176 static void
00177 v850_seg (int i)
00178 {
00179   subsegT sub = get_absolute_expression ();
00180 
00181   do_v850_seg (i, sub);
00182   demand_empty_rest_of_line ();
00183 }
00184 
00185 static void
00186 v850_offset (int ignore ATTRIBUTE_UNUSED)
00187 {
00188   char *pfrag;
00189   int temp = get_absolute_expression ();
00190 
00191   pfrag = frag_var (rs_org, 1, 1, (relax_substateT)0, (symbolS *)0,
00192                   (offsetT) temp, (char *) 0);
00193   *pfrag = 0;
00194 
00195   demand_empty_rest_of_line ();
00196 }
00197 
00198 /* Copied from obj_elf_common() in gas/config/obj-elf.c.  */
00199 
00200 static void
00201 v850_comm (int area)
00202 {
00203   char *name;
00204   char c;
00205   char *p;
00206   int temp;
00207   unsigned int size;
00208   symbolS *symbolP;
00209   int have_align;
00210 
00211   name = input_line_pointer;
00212   c = get_symbol_end ();
00213 
00214   /* Just after name is now '\0'.  */
00215   p = input_line_pointer;
00216   *p = c;
00217 
00218   SKIP_WHITESPACE ();
00219 
00220   if (*input_line_pointer != ',')
00221     {
00222       as_bad (_("Expected comma after symbol-name"));
00223       ignore_rest_of_line ();
00224       return;
00225     }
00226 
00227   /* Skip ','.  */
00228   input_line_pointer++;
00229 
00230   if ((temp = get_absolute_expression ()) < 0)
00231     {
00232       /* xgettext:c-format  */
00233       as_bad (_(".COMMon length (%d.) < 0! Ignored."), temp);
00234       ignore_rest_of_line ();
00235       return;
00236     }
00237 
00238   size = temp;
00239   *p = 0;
00240   symbolP = symbol_find_or_make (name);
00241   *p = c;
00242 
00243   if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
00244     {
00245       as_bad (_("Ignoring attempt to re-define symbol"));
00246       ignore_rest_of_line ();
00247       return;
00248     }
00249 
00250   if (S_GET_VALUE (symbolP) != 0)
00251     {
00252       if (S_GET_VALUE (symbolP) != size)
00253        /* xgettext:c-format  */
00254        as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %d."),
00255                S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
00256     }
00257 
00258   know (symbol_get_frag (symbolP) == &zero_address_frag);
00259 
00260   if (*input_line_pointer != ',')
00261     have_align = 0;
00262   else
00263     {
00264       have_align = 1;
00265       input_line_pointer++;
00266       SKIP_WHITESPACE ();
00267     }
00268 
00269   if (! have_align || *input_line_pointer != '"')
00270     {
00271       if (! have_align)
00272        temp = 0;
00273       else
00274        {
00275          temp = get_absolute_expression ();
00276 
00277          if (temp < 0)
00278            {
00279              temp = 0;
00280              as_warn (_("Common alignment negative; 0 assumed"));
00281            }
00282        }
00283 
00284       if (symbol_get_obj (symbolP)->local)
00285        {
00286          segT old_sec;
00287          int old_subsec;
00288          char *pfrag;
00289          int align;
00290          flagword applicable;
00291 
00292          old_sec = now_seg;
00293          old_subsec = now_subseg;
00294 
00295          applicable = bfd_applicable_section_flags (stdoutput);
00296 
00297          applicable &= SEC_ALLOC;
00298 
00299          switch (area)
00300            {
00301            case SCOMMON_SECTION:
00302              do_v850_seg (SBSS_SECTION, 0);
00303              break;
00304 
00305            case ZCOMMON_SECTION:
00306              do_v850_seg (ZBSS_SECTION, 0);
00307              break;
00308 
00309            case TCOMMON_SECTION:
00310              do_v850_seg (TBSS_SECTION, 0);
00311              break;
00312            }
00313 
00314          if (temp)
00315            {
00316              /* Convert to a power of 2 alignment.  */
00317              for (align = 0; (temp & 1) == 0; temp >>= 1, ++align)
00318               ;
00319 
00320              if (temp != 1)
00321               {
00322                 as_bad (_("Common alignment not a power of 2"));
00323                 ignore_rest_of_line ();
00324                 return;
00325               }
00326            }
00327          else
00328            align = 0;
00329 
00330          record_alignment (now_seg, align);
00331 
00332          if (align)
00333            frag_align (align, 0, 0);
00334 
00335          switch (area)
00336            {
00337            case SCOMMON_SECTION:
00338              if (S_GET_SEGMENT (symbolP) == v850_seg_table[SBSS_SECTION].s)
00339               symbol_get_frag (symbolP)->fr_symbol = 0;
00340              break;
00341 
00342            case ZCOMMON_SECTION:
00343              if (S_GET_SEGMENT (symbolP) == v850_seg_table[ZBSS_SECTION].s)
00344               symbol_get_frag (symbolP)->fr_symbol = 0;
00345              break;
00346 
00347            case TCOMMON_SECTION:
00348              if (S_GET_SEGMENT (symbolP) == v850_seg_table[TBSS_SECTION].s)
00349               symbol_get_frag (symbolP)->fr_symbol = 0;
00350              break;
00351 
00352            default:
00353              abort ();
00354            }
00355 
00356          symbol_set_frag (symbolP, frag_now);
00357          pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
00358                          (offsetT) size, (char *) 0);
00359          *pfrag = 0;
00360          S_SET_SIZE (symbolP, size);
00361 
00362          switch (area)
00363            {
00364            case SCOMMON_SECTION:
00365              S_SET_SEGMENT (symbolP, v850_seg_table[SBSS_SECTION].s);
00366              break;
00367 
00368            case ZCOMMON_SECTION:
00369              S_SET_SEGMENT (symbolP, v850_seg_table[ZBSS_SECTION].s);
00370              break;
00371 
00372            case TCOMMON_SECTION:
00373              S_SET_SEGMENT (symbolP, v850_seg_table[TBSS_SECTION].s);
00374              break;
00375 
00376            default:
00377              abort ();
00378            }
00379 
00380          S_CLEAR_EXTERNAL (symbolP);
00381          obj_elf_section_change_hook ();
00382          subseg_set (old_sec, old_subsec);
00383        }
00384       else
00385        {
00386          segT   old_sec;
00387          int    old_subsec;
00388 
00389        allocate_common:
00390          old_sec = now_seg;
00391          old_subsec = now_subseg;
00392 
00393          S_SET_VALUE (symbolP, (valueT) size);
00394          S_SET_ALIGN (symbolP, temp);
00395          S_SET_EXTERNAL (symbolP);
00396 
00397          switch (area)
00398            {
00399            case SCOMMON_SECTION:
00400            case ZCOMMON_SECTION:
00401            case TCOMMON_SECTION:
00402              do_v850_seg (area, 0);
00403              S_SET_SEGMENT (symbolP, v850_seg_table[area].s);
00404              break;
00405 
00406            default:
00407              abort ();
00408            }
00409 
00410          obj_elf_section_change_hook ();
00411          subseg_set (old_sec, old_subsec);
00412        }
00413     }
00414   else
00415     {
00416       input_line_pointer++;
00417 
00418       /* @@ Some use the dot, some don't.  Can we get some consistency??  */
00419       if (*input_line_pointer == '.')
00420        input_line_pointer++;
00421 
00422       /* @@ Some say data, some say bss.  */
00423       if (strncmp (input_line_pointer, "bss\"", 4)
00424          && strncmp (input_line_pointer, "data\"", 5))
00425        {
00426          while (*--input_line_pointer != '"')
00427            ;
00428          input_line_pointer--;
00429          goto bad_common_segment;
00430        }
00431 
00432       while (*input_line_pointer++ != '"')
00433        ;
00434 
00435       goto allocate_common;
00436     }
00437 
00438   symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
00439 
00440   demand_empty_rest_of_line ();
00441   return;
00442 
00443   {
00444   bad_common_segment:
00445     p = input_line_pointer;
00446     while (*p && *p != '\n')
00447       p++;
00448     c = *p;
00449     *p = '\0';
00450     as_bad (_("bad .common segment %s"), input_line_pointer + 1);
00451     *p = c;
00452     input_line_pointer = p;
00453     ignore_rest_of_line ();
00454     return;
00455   }
00456 }
00457 
00458 static void
00459 set_machine (int number)
00460 {
00461   machine = number;
00462   bfd_set_arch_mach (stdoutput, TARGET_ARCH, machine);
00463 
00464   switch (machine)
00465     {
00466     case 0:            processor_mask = PROCESSOR_V850;   break;
00467     case bfd_mach_v850e:  processor_mask = PROCESSOR_V850E;  break;
00468     case bfd_mach_v850e1: processor_mask = PROCESSOR_V850E;  break;
00469     }
00470 }
00471 
00472 static void
00473 v850_longcode (int type)
00474 {
00475   expressionS ex;
00476 
00477   if (! v850_relax)
00478     {
00479       if (type == 1)
00480        as_warn (".longcall pseudo-op seen when not relaxing");
00481       else
00482        as_warn (".longjump pseudo-op seen when not relaxing");
00483     }
00484 
00485   expression (&ex);
00486 
00487   if (ex.X_op != O_symbol || ex.X_add_number != 0)
00488     {
00489       as_bad ("bad .longcall format");
00490       ignore_rest_of_line ();
00491 
00492       return;
00493     }
00494 
00495   if (type == 1)
00496     fix_new_exp (frag_now, frag_now_fix (), 4, & ex, 1,
00497                BFD_RELOC_V850_LONGCALL);
00498   else
00499     fix_new_exp (frag_now, frag_now_fix (), 4, & ex, 1,
00500                BFD_RELOC_V850_LONGJUMP);
00501 
00502   demand_empty_rest_of_line ();
00503 }
00504 
00505 /* The target specific pseudo-ops which we support.  */
00506 const pseudo_typeS md_pseudo_table[] =
00507 {
00508   { "sdata",         v850_seg,            SDATA_SECTION        },
00509   { "tdata",         v850_seg,            TDATA_SECTION        },
00510   { "zdata",         v850_seg,            ZDATA_SECTION        },
00511   { "sbss",          v850_seg,            SBSS_SECTION         },
00512   { "tbss",          v850_seg,            TBSS_SECTION         },
00513   { "zbss",          v850_seg,            ZBSS_SECTION         },
00514   { "rosdata",              v850_seg,            ROSDATA_SECTION      },
00515   { "rozdata",              v850_seg,            ROZDATA_SECTION      },
00516   { "bss",           v850_seg,            BSS_SECTION          },
00517   { "offset",        v850_offset,         0                    },
00518   { "word",          cons,                4                    },
00519   { "zcomm",         v850_comm,           ZCOMMON_SECTION      },
00520   { "scomm",         v850_comm,           SCOMMON_SECTION      },
00521   { "tcomm",         v850_comm,           TCOMMON_SECTION      },
00522   { "v850",          set_machine,         0                    },
00523   { "call_table_data",      v850_seg,            CALL_TABLE_DATA_SECTION     },
00524   { "call_table_text",      v850_seg,            CALL_TABLE_TEXT_SECTION     },
00525   { "v850e",         set_machine,         bfd_mach_v850e              },
00526   { "v850e1",        set_machine,         bfd_mach_v850e1      },
00527   { "longcall",             v850_longcode,              1                    },
00528   { "longjump",             v850_longcode,              2                    },
00529   { NULL,            NULL,                0                    }
00530 };
00531 
00532 /* Opcode hash table.  */
00533 static struct hash_control *v850_hash;
00534 
00535 /* This table is sorted.  Suitable for searching by a binary search.  */
00536 static const struct reg_name pre_defined_registers[] =
00537 {
00538   { "ep",  30 },            /* ep - element ptr.  */
00539   { "gp",   4 },            /* gp - global ptr.  */
00540   { "hp",   2 },            /* hp - handler stack ptr.  */
00541   { "lp",  31 },            /* lp - link ptr.  */
00542   { "r0",   0 },
00543   { "r1",   1 },
00544   { "r10", 10 },
00545   { "r11", 11 },
00546   { "r12", 12 },
00547   { "r13", 13 },
00548   { "r14", 14 },
00549   { "r15", 15 },
00550   { "r16", 16 },
00551   { "r17", 17 },
00552   { "r18", 18 },
00553   { "r19", 19 },
00554   { "r2",   2 },
00555   { "r20", 20 },
00556   { "r21", 21 },
00557   { "r22", 22 },
00558   { "r23", 23 },
00559   { "r24", 24 },
00560   { "r25", 25 },
00561   { "r26", 26 },
00562   { "r27", 27 },
00563   { "r28", 28 },
00564   { "r29", 29 },
00565   { "r3",   3 },
00566   { "r30", 30 },
00567   { "r31", 31 },
00568   { "r4",   4 },
00569   { "r5",   5 },
00570   { "r6",   6 },
00571   { "r7",   7 },
00572   { "r8",   8 },
00573   { "r9",   9 },
00574   { "sp",   3 },            /* sp - stack ptr.  */
00575   { "tp",   5 },            /* tp - text ptr.  */
00576   { "zero", 0 },
00577 };
00578 
00579 #define REG_NAME_CNT                                    \
00580   (sizeof (pre_defined_registers) / sizeof (struct reg_name))
00581 
00582 static const struct reg_name system_registers[] =
00583 {
00584   { "asid",  23 },
00585   { "bpc",   22 },
00586   { "bpav",  24 },
00587   { "bpam",  25 },
00588   { "bpdv",  26 },
00589   { "bpdm",  27 },
00590   { "ctbp",  20 },
00591   { "ctpc",  16 },
00592   { "ctpsw", 17 },
00593   { "dbpc",  18 },
00594   { "dbpsw", 19 },
00595   { "dir",   21 },
00596   { "ecr",    4 },
00597   { "eipc",   0 },
00598   { "eipsw",  1 },
00599   { "fepc",   2 },
00600   { "fepsw",  3 },
00601   { "psw",    5 },
00602 };
00603 
00604 #define SYSREG_NAME_CNT                                        \
00605   (sizeof (system_registers) / sizeof (struct reg_name))
00606 
00607 static const struct reg_name system_list_registers[] =
00608 {
00609   {"PS",      5 },
00610   {"SR",      0 + 1}
00611 };
00612 
00613 #define SYSREGLIST_NAME_CNT                             \
00614   (sizeof (system_list_registers) / sizeof (struct reg_name))
00615 
00616 static const struct reg_name cc_names[] =
00617 {
00618   { "c",  0x1 },
00619   { "e",  0x2 },
00620   { "ge", 0xe },
00621   { "gt", 0xf },
00622   { "h",  0xb },
00623   { "l",  0x1 },
00624   { "le", 0x7 },
00625   { "lt", 0x6 },
00626   { "n",  0x4 },
00627   { "nc", 0x9 },
00628   { "ne", 0xa },
00629   { "nh", 0x3 },
00630   { "nl", 0x9 },
00631   { "ns", 0xc },
00632   { "nv", 0x8 },
00633   { "nz", 0xa },
00634   { "p",  0xc },
00635   { "s",  0x4 },
00636   { "sa", 0xd },
00637   { "t",  0x5 },
00638   { "v",  0x0 },
00639   { "z",  0x2 },
00640 };
00641 
00642 #define CC_NAME_CNT                              \
00643   (sizeof (cc_names) / sizeof (struct reg_name))
00644 
00645 /* Do a binary search of the given register table to see if NAME is a
00646    valid regiter name.  Return the register number from the array on
00647    success, or -1 on failure.  */
00648 
00649 static int
00650 reg_name_search (const struct reg_name *regs,
00651                int regcount,
00652                const char *name,
00653                bfd_boolean accept_numbers)
00654 {
00655   int middle, low, high;
00656   int cmp;
00657   symbolS *symbolP;
00658 
00659   /* If the register name is a symbol, then evaluate it.  */
00660   if ((symbolP = symbol_find (name)) != NULL)
00661     {
00662       /* If the symbol is an alias for another name then use that.
00663         If the symbol is an alias for a number, then return the number.  */
00664       if (symbol_equated_p (symbolP))
00665        name
00666          = S_GET_NAME (symbol_get_value_expression (symbolP)->X_add_symbol);
00667       else if (accept_numbers)
00668        {
00669          int reg = S_GET_VALUE (symbolP);
00670 
00671          if (reg >= 0 && reg <= 31)
00672            return reg;
00673        }
00674 
00675       /* Otherwise drop through and try parsing name normally.  */
00676     }
00677 
00678   low = 0;
00679   high = regcount - 1;
00680 
00681   do
00682     {
00683       middle = (low + high) / 2;
00684       cmp = strcasecmp (name, regs[middle].name);
00685       if (cmp < 0)
00686        high = middle - 1;
00687       else if (cmp > 0)
00688        low = middle + 1;
00689       else
00690        return regs[middle].value;
00691     }
00692   while (low <= high);
00693   return -1;
00694 }
00695 
00696 /* Summary of register_name().
00697 
00698    in: Input_line_pointer points to 1st char of operand.
00699 
00700    out: An expressionS.
00701        The operand may have been a register: in this case, X_op == O_register,
00702        X_add_number is set to the register number, and truth is returned.
00703        Input_line_pointer->(next non-blank) char after operand, or is in
00704        its original state.  */
00705 
00706 static bfd_boolean
00707 register_name (expressionS *expressionP)
00708 {
00709   int reg_number;
00710   char *name;
00711   char *start;
00712   char c;
00713 
00714   /* Find the spelling of the operand.  */
00715   start = name = input_line_pointer;
00716 
00717   c = get_symbol_end ();
00718 
00719   reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT,
00720                             name, FALSE);
00721 
00722   /* Put back the delimiting char.  */
00723   *input_line_pointer = c;
00724 
00725   /* Look to see if it's in the register table.  */
00726   if (reg_number >= 0)
00727     {
00728       expressionP->X_op            = O_register;
00729       expressionP->X_add_number = reg_number;
00730 
00731       /* Make the rest nice.  */
00732       expressionP->X_add_symbol = NULL;
00733       expressionP->X_op_symbol  = NULL;
00734 
00735       return TRUE;
00736     }
00737   else
00738     {
00739       /* Reset the line as if we had not done anything.  */
00740       input_line_pointer = start;
00741 
00742       return FALSE;
00743     }
00744 }
00745 
00746 /* Summary of system_register_name().
00747 
00748    in:  INPUT_LINE_POINTER points to 1st char of operand.
00749        EXPRESSIONP points to an expression structure to be filled in.
00750        ACCEPT_NUMBERS is true iff numerical register names may be used.
00751        ACCEPT_LIST_NAMES is true iff the special names PS and SR may be
00752        accepted.
00753 
00754    out: An expressionS structure in expressionP.
00755        The operand may have been a register: in this case, X_op == O_register,
00756        X_add_number is set to the register number, and truth is returned.
00757        Input_line_pointer->(next non-blank) char after operand, or is in
00758        its original state.  */
00759 
00760 static bfd_boolean
00761 system_register_name (expressionS *expressionP,
00762                     bfd_boolean accept_numbers,
00763                     bfd_boolean accept_list_names)
00764 {
00765   int reg_number;
00766   char *name;
00767   char *start;
00768   char c;
00769 
00770   /* Find the spelling of the operand.  */
00771   start = name = input_line_pointer;
00772 
00773   c = get_symbol_end ();
00774   reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name,
00775                             accept_numbers);
00776 
00777   /* Put back the delimiting char.  */
00778   *input_line_pointer = c;
00779 
00780   if (reg_number < 0
00781       && accept_numbers)
00782     {
00783       /* Reset input_line pointer.  */
00784       input_line_pointer = start;
00785 
00786       if (ISDIGIT (*input_line_pointer))
00787        {
00788          reg_number = strtol (input_line_pointer, &input_line_pointer, 10);
00789 
00790          /* Make sure that the register number is allowable.  */
00791          if (reg_number < 0
00792              || (reg_number > 5 && reg_number < 16)
00793              || reg_number > 27)
00794            reg_number = -1;
00795        }
00796       else if (accept_list_names)
00797        {
00798          c = get_symbol_end ();
00799          reg_number = reg_name_search (system_list_registers,
00800                                    SYSREGLIST_NAME_CNT, name, FALSE);
00801 
00802          /* Put back the delimiting char.  */
00803          *input_line_pointer = c;
00804        }
00805     }
00806 
00807   /* Look to see if it's in the register table.  */
00808   if (reg_number >= 0)
00809     {
00810       expressionP->X_op            = O_register;
00811       expressionP->X_add_number = reg_number;
00812 
00813       /* Make the rest nice.  */
00814       expressionP->X_add_symbol = NULL;
00815       expressionP->X_op_symbol  = NULL;
00816 
00817       return TRUE;
00818     }
00819   else
00820     {
00821       /* Reset the line as if we had not done anything.  */
00822       input_line_pointer = start;
00823 
00824       return FALSE;
00825     }
00826 }
00827 
00828 /* Summary of cc_name().
00829 
00830    in: INPUT_LINE_POINTER points to 1st char of operand.
00831 
00832    out: An expressionS.
00833        The operand may have been a register: in this case, X_op == O_register,
00834        X_add_number is set to the register number, and truth is returned.
00835        Input_line_pointer->(next non-blank) char after operand, or is in
00836        its original state.  */
00837 
00838 static bfd_boolean
00839 cc_name (expressionS *expressionP)
00840 {
00841   int reg_number;
00842   char *name;
00843   char *start;
00844   char c;
00845 
00846   /* Find the spelling of the operand.  */
00847   start = name = input_line_pointer;
00848 
00849   c = get_symbol_end ();
00850   reg_number = reg_name_search (cc_names, CC_NAME_CNT, name, FALSE);
00851 
00852   /* Put back the delimiting char.  */
00853   *input_line_pointer = c;
00854 
00855   /* Look to see if it's in the register table.  */
00856   if (reg_number >= 0)
00857     {
00858       expressionP->X_op            = O_constant;
00859       expressionP->X_add_number = reg_number;
00860 
00861       /* Make the rest nice.  */
00862       expressionP->X_add_symbol = NULL;
00863       expressionP->X_op_symbol  = NULL;
00864 
00865       return TRUE;
00866     }
00867   else
00868     {
00869       /* Reset the line as if we had not done anything.  */
00870       input_line_pointer = start;
00871 
00872       return FALSE;
00873     }
00874 }
00875 
00876 static void
00877 skip_white_space (void)
00878 {
00879   while (*input_line_pointer == ' '
00880         || *input_line_pointer == '\t')
00881     ++input_line_pointer;
00882 }
00883 
00884 /* Summary of parse_register_list ().
00885 
00886    in: INPUT_LINE_POINTER  points to 1st char of a list of registers.
00887        INSN             is the partially constructed instruction.
00888        OPERAND                 is the operand being inserted.
00889 
00890    out: NULL if the parse completed successfully, otherwise a
00891        pointer to an error message is returned.  If the parse
00892        completes the correct bit fields in the instruction
00893        will be filled in.
00894 
00895    Parses register lists with the syntax:
00896 
00897      { rX }
00898      { rX, rY }
00899      { rX - rY }
00900      { rX - rY, rZ }
00901      etc
00902 
00903    and also parses constant expressions whoes bits indicate the
00904    registers in the lists.  The LSB in the expression refers to
00905    the lowest numbered permissible register in the register list,
00906    and so on upwards.  System registers are considered to be very
00907    high numbers.  */
00908 
00909 static char *
00910 parse_register_list (unsigned long *insn,
00911                    const struct v850_operand *operand)
00912 {
00913   static int type1_regs[32] =
00914   {
00915     30,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
00916      0,  0,  0,  0,  0, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24
00917   };
00918   static int type2_regs[32] =
00919   {
00920     19, 18, 17, 16,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
00921      0,  0,  0,  0, 30, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24
00922   };
00923   static int type3_regs[32] =
00924   {
00925      3,  2,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
00926      0,  0,  0,  0, 14, 15, 13, 12,  7,  6,  5,  4, 11, 10,  9,  8
00927   };
00928   int *regs;
00929   expressionS exp;
00930 
00931   /* Select a register array to parse.  */
00932   switch (operand->shift)
00933     {
00934     case 0xffe00001: regs = type1_regs; break;
00935     case 0xfff8000f: regs = type2_regs; break;
00936     case 0xfff8001f: regs = type3_regs; break;
00937     default:
00938       as_bad (_("unknown operand shift: %x\n"), operand->shift);
00939       return _("internal failure in parse_register_list");
00940     }
00941 
00942   skip_white_space ();
00943 
00944   /* If the expression starts with a curly brace it is a register list.
00945      Otherwise it is a constant expression, whoes bits indicate which
00946      registers are to be included in the list.  */
00947   if (*input_line_pointer != '{')
00948     {
00949       int reg;
00950       int i;
00951 
00952       expression (&exp);
00953 
00954       if (exp.X_op != O_constant)
00955        return _("constant expression or register list expected");
00956 
00957       if (regs == type1_regs)
00958        {
00959          if (exp.X_add_number & 0xFFFFF000)
00960            return _("high bits set in register list expression");
00961 
00962          for (reg = 20; reg < 32; reg++)
00963            if (exp.X_add_number & (1 << (reg - 20)))
00964              {
00965               for (i = 0; i < 32; i++)
00966                 if (regs[i] == reg)
00967                   *insn |= (1 << i);
00968              }
00969        }
00970       else if (regs == type2_regs)
00971        {
00972          if (exp.X_add_number & 0xFFFE0000)
00973            return _("high bits set in register list expression");
00974 
00975          for (reg = 1; reg < 16; reg++)
00976            if (exp.X_add_number & (1 << (reg - 1)))
00977              {
00978               for (i = 0; i < 32; i++)
00979                 if (regs[i] == reg)
00980                   *insn |= (1 << i);
00981              }
00982 
00983          if (exp.X_add_number & (1 << 15))
00984            *insn |= (1 << 3);
00985 
00986          if (exp.X_add_number & (1 << 16))
00987            *insn |= (1 << 19);
00988        }
00989       else /* regs == type3_regs  */
00990        {
00991          if (exp.X_add_number & 0xFFFE0000)
00992            return _("high bits set in register list expression");
00993 
00994          for (reg = 16; reg < 32; reg++)
00995            if (exp.X_add_number & (1 << (reg - 16)))
00996              {
00997               for (i = 0; i < 32; i++)
00998                 if (regs[i] == reg)
00999                   *insn |= (1 << i);
01000              }
01001 
01002          if (exp.X_add_number & (1 << 16))
01003            *insn |= (1 << 19);
01004        }
01005 
01006       return NULL;
01007     }
01008 
01009   input_line_pointer++;
01010 
01011   /* Parse the register list until a terminator (closing curly brace or
01012      new-line) is found.  */
01013   for (;;)
01014     {
01015       if (register_name (&exp))
01016        {
01017          int i;
01018 
01019          /* Locate the given register in the list, and if it is there,
01020             insert the corresponding bit into the instruction.  */
01021          for (i = 0; i < 32; i++)
01022            {
01023              if (regs[i] == exp.X_add_number)
01024               {
01025                 *insn |= (1 << i);
01026                 break;
01027               }
01028            }
01029 
01030          if (i == 32)
01031            return _("illegal register included in list");
01032        }
01033       else if (system_register_name (&exp, TRUE, TRUE))
01034        {
01035          if (regs == type1_regs)
01036            {
01037              return _("system registers cannot be included in list");
01038            }
01039          else if (exp.X_add_number == 5)
01040            {
01041              if (regs == type2_regs)
01042               return _("PSW cannot be included in list");
01043              else
01044               *insn |= 0x8;
01045            }
01046          else if (exp.X_add_number < 4)
01047            *insn |= 0x80000;
01048          else
01049            return _("High value system registers cannot be included in list");
01050        }
01051       else if (*input_line_pointer == '}')
01052        {
01053          input_line_pointer++;
01054          break;
01055        }
01056       else if (*input_line_pointer == ',')
01057        {
01058          input_line_pointer++;
01059          continue;
01060        }
01061       else if (*input_line_pointer == '-')
01062        {
01063          /* We have encountered a range of registers: rX - rY.  */
01064          int j;
01065          expressionS exp2;
01066 
01067          /* Skip the dash.  */
01068          ++input_line_pointer;
01069 
01070          /* Get the second register in the range.  */
01071          if (! register_name (&exp2))
01072            {
01073              return _("second register should follow dash in register list");
01074              exp2.X_add_number = exp.X_add_number;
01075            }
01076 
01077          /* Add the rest of the registers in the range.  */
01078          for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
01079            {
01080              int i;
01081 
01082              /* Locate the given register in the list, and if it is there,
01083                insert the corresponding bit into the instruction.  */
01084              for (i = 0; i < 32; i++)
01085               {
01086                 if (regs[i] == j)
01087                   {
01088                     *insn |= (1 << i);
01089                     break;
01090                   }
01091               }
01092 
01093              if (i == 32)
01094               return _("illegal register included in list");
01095            }
01096        }
01097       else
01098        break;
01099 
01100       skip_white_space ();
01101     }
01102 
01103   return NULL;
01104 }
01105 
01106 const char *md_shortopts = "m:";
01107 
01108 struct option md_longopts[] =
01109 {
01110   {NULL, no_argument, NULL, 0}
01111 };
01112 
01113 size_t md_longopts_size = sizeof (md_longopts);
01114 
01115 void
01116 md_show_usage (FILE *stream)
01117 {
01118   fprintf (stream, _(" V850 options:\n"));
01119   fprintf (stream, _("  -mwarn-signed-overflow    Warn if signed immediate values overflow\n"));
01120   fprintf (stream, _("  -mwarn-unsigned-overflow  Warn if unsigned immediate values overflow\n"));
01121   fprintf (stream, _("  -mv850                    The code is targeted at the v850\n"));
01122   fprintf (stream, _("  -mv850e                   The code is targeted at the v850e\n"));
01123   fprintf (stream, _("  -mv850e1                  The code is targeted at the v850e1\n"));
01124   fprintf (stream, _("  -mv850any                 The code is generic, despite any processor specific instructions\n"));
01125   fprintf (stream, _("  -mrelax                   Enable relaxation\n"));
01126 }
01127 
01128 int
01129 md_parse_option (int c, char *arg)
01130 {
01131   if (c != 'm')
01132     return 0;
01133 
01134   if (strcmp (arg, "warn-signed-overflow") == 0)
01135     warn_signed_overflows = TRUE;
01136 
01137   else if (strcmp (arg, "warn-unsigned-overflow") == 0)
01138     warn_unsigned_overflows = TRUE;
01139 
01140   else if (strcmp (arg, "v850") == 0)
01141     {
01142       machine = 0;
01143       processor_mask = PROCESSOR_V850;
01144     }
01145   else if (strcmp (arg, "v850e") == 0)
01146     {
01147       machine = bfd_mach_v850e;
01148       processor_mask = PROCESSOR_V850E;
01149     }
01150   else if (strcmp (arg, "v850e1") == 0)
01151     {
01152       machine = bfd_mach_v850e1;
01153       processor_mask = PROCESSOR_V850E1;
01154     }
01155   else if (strcmp (arg, "v850any") == 0)
01156     {
01157       /* Tell the world that this is for any v850 chip.  */
01158       machine = 0;
01159 
01160       /* But support instructions for the extended versions.  */
01161       processor_mask = PROCESSOR_V850E;
01162       processor_mask |= PROCESSOR_V850E1;
01163     }
01164   else if (strcmp (arg, "relax") == 0)
01165     v850_relax = 1;
01166   else
01167     return 0;
01168 
01169   return 1;
01170 }
01171 
01172 symbolS *
01173 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
01174 {
01175   return 0;
01176 }
01177 
01178 char *
01179 md_atof (int type, char *litp, int *sizep)
01180 {
01181   int prec;
01182   LITTLENUM_TYPE words[4];
01183   char *t;
01184   int i;
01185 
01186   switch (type)
01187     {
01188     case 'f':
01189       prec = 2;
01190       break;
01191 
01192     case 'd':
01193       prec = 4;
01194       break;
01195 
01196     default:
01197       *sizep = 0;
01198       return _("bad call to md_atof");
01199     }
01200 
01201   t = atof_ieee (input_line_pointer, type, words);
01202   if (t)
01203     input_line_pointer = t;
01204 
01205   *sizep = prec * 2;
01206 
01207   for (i = prec - 1; i >= 0; i--)
01208     {
01209       md_number_to_chars (litp, (valueT) words[i], 2);
01210       litp += 2;
01211     }
01212 
01213   return NULL;
01214 }
01215 
01216 /* Very gross.  */
01217 
01218 void
01219 md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
01220                asection *sec,
01221                fragS *fragP)
01222 {
01223   /* This code performs some nasty type punning between the
01224      fr_opcode field of the frag structure (a char *) and the
01225      fx_r_type field of the fix structure (a bfd_reloc_code_real_type)
01226      On a 64bit host this causes problems because these two fields
01227      are not the same size, but since we know that we are only
01228      ever storing small integers in the fields, it is safe to use
01229      a union to convert between them.  */
01230   union u
01231   {
01232     bfd_reloc_code_real_type fx_r_type;
01233     char * fr_opcode;
01234   }
01235   opcode_converter;
01236   subseg_change (sec, 0);
01237 
01238   opcode_converter.fr_opcode = fragP->fr_opcode;
01239       
01240   /* In range conditional or unconditional branch.  */
01241   if (fragP->fr_subtype == 0 || fragP->fr_subtype == 2)
01242     {
01243       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
01244               fragP->fr_offset, 1,
01245               BFD_RELOC_UNUSED + opcode_converter.fx_r_type);
01246       fragP->fr_fix += 2;
01247     }
01248   /* Out of range conditional branch.  Emit a branch around a jump.  */
01249   else if (fragP->fr_subtype == 1)
01250     {
01251       unsigned char *buffer =
01252        (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
01253 
01254       /* Reverse the condition of the first branch.  */
01255       buffer[0] ^= 0x08;
01256       /* Mask off all the displacement bits.  */
01257       buffer[0] &= 0x8f;
01258       buffer[1] &= 0x07;
01259       /* Now set the displacement bits so that we branch
01260         around the unconditional branch.  */
01261       buffer[0] |= 0x30;
01262 
01263       /* Now create the unconditional branch + fixup to the final
01264         target.  */
01265       md_number_to_chars ((char *) buffer + 2, 0x00000780, 4);
01266       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
01267               fragP->fr_offset, 1,
01268               BFD_RELOC_UNUSED + opcode_converter.fx_r_type + 1);
01269       fragP->fr_fix += 6;
01270     }
01271   /* Out of range unconditional branch.  Emit a jump.  */
01272   else if (fragP->fr_subtype == 3)
01273     {
01274       md_number_to_chars (fragP->fr_fix + fragP->fr_literal, 0x00000780, 4);
01275       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
01276               fragP->fr_offset, 1,
01277               BFD_RELOC_UNUSED + opcode_converter.fx_r_type + 1);
01278       fragP->fr_fix += 4;
01279     }
01280   else
01281     abort ();
01282 }
01283 
01284 valueT
01285 md_section_align (asection *seg, valueT addr)
01286 {
01287   int align = bfd_get_section_alignment (stdoutput, seg);
01288   return ((addr + (1 << align) - 1) & (-1 << align));
01289 }
01290 
01291 void
01292 md_begin (void)
01293 {
01294   char *prev_name = "";
01295   const struct v850_opcode *op;
01296 
01297   if (strncmp (TARGET_CPU, "v850e1", 6) == 0)
01298     {
01299       if (machine == -1)
01300        machine = bfd_mach_v850e1;
01301 
01302       if (processor_mask == -1)
01303        processor_mask = PROCESSOR_V850E1;
01304     }
01305   else if (strncmp (TARGET_CPU, "v850e", 5) == 0)
01306     {
01307       if (machine == -1)
01308        machine = bfd_mach_v850e;
01309 
01310       if (processor_mask == -1)
01311        processor_mask = PROCESSOR_V850E;
01312     }
01313   else if (strncmp (TARGET_CPU, "v850", 4) == 0)
01314     {
01315       if (machine == -1)
01316        machine = 0;
01317 
01318       if (processor_mask == -1)
01319        processor_mask = PROCESSOR_V850;
01320     }
01321   else
01322     /* xgettext:c-format  */
01323     as_bad (_("Unable to determine default target processor from string: %s"),
01324            TARGET_CPU);
01325 
01326   v850_hash = hash_new ();
01327 
01328   /* Insert unique names into hash table.  The V850 instruction set
01329      has many identical opcode names that have different opcodes based
01330      on the operands.  This hash table then provides a quick index to
01331      the first opcode with a particular name in the opcode table.  */
01332   op = v850_opcodes;
01333   while (op->name)
01334     {
01335       if (strcmp (prev_name, op->name))
01336        {
01337          prev_name = (char *) op->name;
01338          hash_insert (v850_hash, op->name, (char *) op);
01339        }
01340       op++;
01341     }
01342 
01343   v850_seg_table[BSS_SECTION].s = bss_section;
01344   bfd_set_arch_mach (stdoutput, TARGET_ARCH, machine);
01345 }
01346 
01347 static bfd_reloc_code_real_type
01348 handle_lo16 (const struct v850_operand *operand)
01349 {
01350   if (operand != NULL)
01351     {
01352       if (operand->bits == -1)
01353        return BFD_RELOC_V850_LO16_SPLIT_OFFSET;
01354 
01355       if (!(operand->bits == 16 && operand->shift == 16)
01356          && !(operand->bits == 15 && operand->shift == 17))
01357        {
01358          as_bad (_("lo() relocation used on an instruction which does "
01359                   "not support it"));
01360          return BFD_RELOC_64;  /* Used to indicate an error condition.  */
01361        }
01362     }
01363   return BFD_RELOC_LO16;
01364 }
01365 
01366 static bfd_reloc_code_real_type
01367 handle_ctoff (const struct v850_operand *operand)
01368 {
01369   if (operand == NULL)
01370     return BFD_RELOC_V850_CALLT_16_16_OFFSET;
01371 
01372   if (operand->bits != 6
01373       || operand->shift != 0)
01374     {
01375       as_bad (_("ctoff() relocation used on an instruction which does not support it"));
01376       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
01377     }
01378 
01379   return BFD_RELOC_V850_CALLT_6_7_OFFSET;
01380 }
01381 
01382 static bfd_reloc_code_real_type
01383 handle_sdaoff (const struct v850_operand *operand)
01384 {
01385   if (operand == NULL)
01386     return BFD_RELOC_V850_SDA_16_16_OFFSET;
01387 
01388   if (operand->bits == 15 && operand->shift == 17)
01389     return BFD_RELOC_V850_SDA_15_16_OFFSET;
01390 
01391   if (operand->bits == -1)
01392     return BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET;
01393 
01394   if (operand->bits != 16
01395       || operand->shift != 16)
01396     {
01397       as_bad (_("sdaoff() relocation used on an instruction which does not support it"));
01398       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
01399     }
01400 
01401   return BFD_RELOC_V850_SDA_16_16_OFFSET;
01402 }
01403 
01404 static bfd_reloc_code_real_type
01405 handle_zdaoff (const struct v850_operand *operand)
01406 {
01407   if (operand == NULL)
01408     return BFD_RELOC_V850_ZDA_16_16_OFFSET;
01409 
01410   if (operand->bits == 15 && operand->shift == 17)
01411     return BFD_RELOC_V850_ZDA_15_16_OFFSET;
01412 
01413   if (operand->bits == -1)
01414     return BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET;
01415 
01416   if (operand->bits != 16
01417       || operand->shift != 16)
01418     {
01419       as_bad (_("zdaoff() relocation used on an instruction which does not support it"));
01420       /* Used to indicate an error condition.  */
01421       return BFD_RELOC_64;
01422     }
01423 
01424   return BFD_RELOC_V850_ZDA_16_16_OFFSET;
01425 }
01426 
01427 static bfd_reloc_code_real_type
01428 handle_tdaoff (const struct v850_operand *operand)
01429 {
01430   if (operand == NULL)
01431     /* Data item, not an instruction.  */
01432     return BFD_RELOC_V850_TDA_7_7_OFFSET;
01433 
01434   if (operand->bits == 6 && operand->shift == 1)
01435     /* sld.w/sst.w, operand: D8_6.  */
01436     return BFD_RELOC_V850_TDA_6_8_OFFSET;
01437 
01438   if (operand->bits == 4 && operand->insert != NULL)
01439     /* sld.hu, operand: D5-4.  */
01440     return BFD_RELOC_V850_TDA_4_5_OFFSET;
01441 
01442   if (operand->bits == 4 && operand->insert == NULL)
01443     /* sld.bu, operand: D4.   */
01444     return BFD_RELOC_V850_TDA_4_4_OFFSET;
01445 
01446   if (operand->bits == 16 && operand->shift == 16)
01447     /* set1 & chums, operands: D16.  */
01448     return BFD_RELOC_V850_TDA_16_16_OFFSET;
01449 
01450   if (operand->bits != 7)
01451     {
01452       as_bad (_("tdaoff() relocation used on an instruction which does not support it"));
01453       /* Used to indicate an error condition.  */
01454       return BFD_RELOC_64;
01455     }
01456 
01457   return  operand->insert != NULL
01458     ? BFD_RELOC_V850_TDA_7_8_OFFSET     /* sld.h/sst.h, operand: D8_7.  */
01459     : BFD_RELOC_V850_TDA_7_7_OFFSET;    /* sld.b/sst.b, operand: D7.    */
01460 }
01461 
01462 /* Warning: The code in this function relies upon the definitions
01463    in the v850_operands[] array (defined in opcodes/v850-opc.c)
01464    matching the hard coded values contained herein.  */
01465 
01466 static bfd_reloc_code_real_type
01467 v850_reloc_prefix (const struct v850_operand *operand)
01468 {
01469   bfd_boolean paren_skipped = FALSE;
01470 
01471   /* Skip leading opening parenthesis.  */
01472   if (*input_line_pointer == '(')
01473     {
01474       ++input_line_pointer;
01475       paren_skipped = TRUE;
01476     }
01477 
01478 #define CHECK_(name, reloc)                                           \
01479   if (strncmp (input_line_pointer, name "(", strlen (name) + 1) == 0) \
01480     {                                                          \
01481       input_line_pointer += strlen (name);                            \
01482       return reloc;                                            \
01483     }
01484 
01485   CHECK_ ("hi0",    BFD_RELOC_HI16    );
01486   CHECK_ ("hi",          BFD_RELOC_HI16_S    );
01487   CHECK_ ("lo",          handle_lo16 (operand)  );
01488   CHECK_ ("sdaoff", handle_sdaoff (operand));
01489   CHECK_ ("zdaoff", handle_zdaoff (operand));
01490   CHECK_ ("tdaoff", handle_tdaoff (operand));
01491   CHECK_ ("hilo",   BFD_RELOC_32      );
01492   CHECK_ ("ctoff",  handle_ctoff (operand) );
01493 
01494   /* Restore skipped parenthesis.  */
01495   if (paren_skipped)
01496     --input_line_pointer;
01497 
01498   return BFD_RELOC_UNUSED;
01499 }
01500 
01501 /* Insert an operand value into an instruction.  */
01502 
01503 static unsigned long
01504 v850_insert_operand (unsigned long insn,
01505                    const struct v850_operand *operand,
01506                    offsetT val,
01507                    char *file,
01508                    unsigned int line,
01509                    char *str)
01510 {
01511   if (operand->insert)
01512     {
01513       const char *message = NULL;
01514 
01515       insn = operand->insert (insn, val, &message);
01516       if (message != NULL)
01517        {
01518          if ((operand->flags & V850_OPERAND_SIGNED)
01519              && ! warn_signed_overflows
01520              && strstr (message, "out of range") != NULL)
01521            {
01522              /* Skip warning...  */
01523            }
01524          else if ((operand->flags & V850_OPERAND_SIGNED) == 0
01525                  && ! warn_unsigned_overflows
01526                  && strstr (message, "out of range") != NULL)
01527            {
01528              /* Skip warning...  */
01529            }
01530          else if (str)
01531            {
01532              if (file == (char *) NULL)
01533               as_warn ("%s: %s", str, message);
01534              else
01535               as_warn_where (file, line, "%s: %s", str, message);
01536            }
01537          else
01538            {
01539              if (file == (char *) NULL)
01540               as_warn (message);
01541              else
01542               as_warn_where (file, line, message);
01543            }
01544        }
01545     }
01546   else
01547     {
01548       if (operand->bits != 32)
01549        {
01550          long min, max;
01551 
01552          if ((operand->flags & V850_OPERAND_SIGNED) != 0)
01553            {
01554              if (! warn_signed_overflows)
01555               max = (1 << operand->bits) - 1;
01556              else
01557               max = (1 << (operand->bits - 1)) - 1;
01558 
01559              min = -(1 << (operand->bits - 1));
01560            }
01561          else
01562            {
01563              max = (1 << operand->bits) - 1;
01564 
01565              if (! warn_unsigned_overflows)
01566               min = -(1 << (operand->bits - 1));
01567              else
01568               min = 0;
01569            }
01570 
01571          if (val < (offsetT) min || val > (offsetT) max)
01572            {
01573              char buf [128];
01574 
01575              /* Restore min and mix to expected values for decimal ranges.  */
01576              if ((operand->flags & V850_OPERAND_SIGNED)
01577                 && ! warn_signed_overflows)
01578               max = (1 << (operand->bits - 1)) - 1;
01579 
01580              if (! (operand->flags & V850_OPERAND_SIGNED)
01581                 && ! warn_unsigned_overflows)
01582               min = 0;
01583 
01584              if (str)
01585               sprintf (buf, "%s: ", str);
01586              else
01587               buf[0] = 0;
01588              strcat (buf, _("operand"));
01589 
01590              as_bad_value_out_of_range (buf, val, (offsetT) min, (offsetT) max, file, line);
01591            }
01592        }
01593 
01594       insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
01595     }
01596 
01597   return insn;
01598 }
01599 
01600 static char copy_of_instruction[128];
01601 
01602 void
01603 md_assemble (char *str)
01604 {
01605   char *s;
01606   char *start_of_operands;
01607   struct v850_opcode *opcode;
01608   struct v850_opcode *next_opcode;
01609   const unsigned char *opindex_ptr;
01610   int next_opindex;
01611   int relaxable = 0;
01612   unsigned long insn;
01613   unsigned long insn_size;
01614   char *f;
01615   int i;
01616   int match;
01617   bfd_boolean extra_data_after_insn = FALSE;
01618   unsigned extra_data_len = 0;
01619   unsigned long extra_data = 0;
01620   char *saved_input_line_pointer;
01621 
01622   strncpy (copy_of_instruction, str, sizeof (copy_of_instruction) - 1);
01623 
01624   /* Get the opcode.  */
01625   for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
01626     continue;
01627 
01628   if (*s != '\0')
01629     *s++ = '\0';
01630 
01631   /* Find the first opcode with the proper name.  */
01632   opcode = (struct v850_opcode *) hash_find (v850_hash, str);
01633   if (opcode == NULL)
01634     {
01635       /* xgettext:c-format  */
01636       as_bad (_("Unrecognized opcode: `%s'"), str);
01637       ignore_rest_of_line ();
01638       return;
01639     }
01640 
01641   str = s;
01642   while (ISSPACE (*str))
01643     ++str;
01644 
01645   start_of_operands = str;
01646 
01647   saved_input_line_pointer = input_line_pointer;
01648 
01649   for (;;)
01650     {
01651       const char *errmsg = NULL;
01652 
01653       match = 0;
01654 
01655       if ((opcode->processors & processor_mask) == 0)
01656        {
01657          errmsg = _("Target processor does not support this instruction.");
01658          goto error;
01659        }
01660 
01661       relaxable = 0;
01662       fc = 0;
01663       next_opindex = 0;
01664       insn = opcode->opcode;
01665       extra_data_after_insn = FALSE;
01666 
01667       input_line_pointer = str = start_of_operands;
01668 
01669       for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
01670        {
01671          const struct v850_operand *operand;
01672          char *hold;
01673          expressionS ex;
01674          bfd_reloc_code_real_type reloc;
01675 
01676          if (next_opindex == 0)
01677            operand = &v850_operands[*opindex_ptr];
01678          else
01679            {
01680              operand = &v850_operands[next_opindex];
01681              next_opindex = 0;
01682            }
01683 
01684          errmsg = NULL;
01685 
01686          while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
01687            ++str;
01688 
01689          if (operand->flags & V850_OPERAND_RELAX)
01690            relaxable = 1;
01691 
01692          /* Gather the operand.  */
01693          hold = input_line_pointer;
01694          input_line_pointer = str;
01695 
01696          /* lo(), hi(), hi0(), etc...  */
01697          if ((reloc = v850_reloc_prefix (operand)) != BFD_RELOC_UNUSED)
01698            {
01699              /* This is a fake reloc, used to indicate an error condition.  */
01700              if (reloc == BFD_RELOC_64)
01701               {
01702                 match = 1;
01703                 goto error;
01704               }
01705 
01706              expression (&ex);
01707 
01708              if (ex.X_op == O_constant)
01709               {
01710                 switch (reloc)
01711                   {
01712                   case BFD_RELOC_V850_ZDA_16_16_OFFSET:
01713                     /* To cope with "not1 7, zdaoff(0xfffff006)[r0]"
01714                       and the like.  */
01715                     /* Fall through.  */
01716 
01717                   case BFD_RELOC_LO16:
01718                   case BFD_RELOC_V850_LO16_SPLIT_OFFSET:
01719                     {
01720                      /* Truncate, then sign extend the value.  */
01721                      ex.X_add_number = SEXT16 (ex.X_add_number);
01722                      break;
01723                     }
01724 
01725                   case BFD_RELOC_HI16:
01726                     {
01727                      /* Truncate, then sign extend the value.  */
01728                      ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
01729                      break;
01730                     }
01731 
01732                   case BFD_RELOC_HI16_S:
01733                     {
01734                      /* Truncate, then sign extend the value.  */
01735                      int temp = (ex.X_add_number >> 16) & 0xffff;
01736 
01737                      temp += (ex.X_add_number >> 15) & 1;
01738 
01739                      ex.X_add_number = SEXT16 (temp);
01740                      break;
01741                     }
01742 
01743                   case BFD_RELOC_32:
01744                     if ((operand->flags & V850E_IMMEDIATE32) == 0)
01745                      {
01746                        errmsg = _("immediate operand is too large");
01747                        goto error;
01748                      }
01749 
01750                     extra_data_after_insn = TRUE;
01751                     extra_data_len     = 4;
01752                     extra_data         = 0;
01753                     break;
01754 
01755                   default:
01756                     fprintf (stderr, "reloc: %d\n", reloc);
01757                     as_bad (_("AAARG -> unhandled constant reloc"));
01758                     break;
01759                   }
01760 
01761                 if (fc > MAX_INSN_FIXUPS)
01762                   as_fatal (_("too many fixups"));
01763 
01764                 fixups[fc].exp     = ex;
01765                 fixups[fc].opindex = *opindex_ptr;
01766                 fixups[fc].reloc   = reloc;
01767                 fc++;
01768               }
01769              else
01770               {
01771                 if (reloc == BFD_RELOC_32)
01772                   {
01773                     if ((operand->flags & V850E_IMMEDIATE32) == 0)
01774                      {
01775                        errmsg = _("immediate operand is too large");
01776                        goto error;
01777                      }
01778 
01779                     extra_data_after_insn = TRUE;
01780                     extra_data_len     = 4;
01781                     extra_data         = ex.X_add_number;
01782                   }
01783 
01784                 if (fc > MAX_INSN_FIXUPS)
01785                   as_fatal (_("too many fixups"));
01786 
01787                 fixups[fc].exp     = ex;
01788                 fixups[fc].opindex = *opindex_ptr;
01789                 fixups[fc].reloc   = reloc;
01790                 fc++;
01791               }
01792            }
01793          else
01794            {
01795              errmsg = NULL;
01796 
01797              if ((operand->flags & V850_OPERAND_REG) != 0)
01798               {
01799                 if (!register_name (&ex))
01800                   errmsg = _("invalid register name");
01801                 else if ((operand->flags & V850_NOT_R0)
01802                         && ex.X_add_number == 0)
01803                   {
01804                     errmsg = _("register r0 cannot be used here");
01805 
01806                     /* Force an error message to be generated by
01807                       skipping over any following potential matches
01808                       for this opcode.  */
01809                     opcode += 3;
01810                   }
01811               }
01812              else if ((operand->flags & V850_OPERAND_SRG) != 0)
01813               {
01814                 if (!system_register_name (&ex, TRUE, FALSE))
01815                   errmsg = _("invalid system register name");
01816               }
01817              else if ((operand->flags & V850_OPERAND_EP) != 0)
01818               {
01819                 char *start = input_line_pointer;
01820                 char c = get_symbol_end ();
01821 
01822                 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
01823                   {
01824                     /* Put things back the way we found them.  */
01825                     *input_line_pointer = c;
01826                     input_line_pointer = start;
01827                     errmsg = _("expected EP register");
01828                     goto error;
01829                   }
01830 
01831                 *input_line_pointer = c;
01832                 str = input_line_pointer;
01833                 input_line_pointer = hold;
01834 
01835                 while (*str == ' ' || *str == ','
01836                       || *str == '[' || *str == ']')
01837                   ++str;
01838                 continue;
01839               }
01840              else if ((operand->flags & V850_OPERAND_CC) != 0)
01841               {
01842                 if (!cc_name (&ex))
01843                   errmsg = _("invalid condition code name");
01844               }
01845              else if (operand->flags & V850E_PUSH_POP)
01846               {
01847                 errmsg = parse_register_list (&insn, operand);
01848 
01849                 /* The parse_register_list() function has already done
01850                    everything, so fake a dummy expression.  */
01851                 ex.X_op       = O_constant;
01852                 ex.X_add_number = 0;
01853               }
01854              else if (operand->flags & V850E_IMMEDIATE16)
01855               {
01856                 expression (&ex);
01857 
01858                 if (ex.X_op != O_constant)
01859                   errmsg = _("constant expression expected");
01860                 else if (ex.X_add_number & 0xffff0000)
01861                   {
01862                     if (ex.X_add_number & 0xffff)
01863                      errmsg = _("constant too big to fit into instruction");
01864                     else if ((insn & 0x001fffc0) == 0x00130780)
01865                      ex.X_add_number >>= 16;
01866                     else
01867                      errmsg = _("constant too big to fit into instruction");
01868                   }
01869 
01870                 extra_data_after_insn = TRUE;
01871                 extra_data_len     = 2;
01872                 extra_data         = ex.X_add_number;
01873                 ex.X_add_number    = 0;
01874               }
01875              else if (operand->flags & V850E_IMMEDIATE32)
01876               {
01877                 expression (&ex);
01878 
01879                 if (ex.X_op != O_constant)
01880                   errmsg = _("constant expression expected");
01881 
01882                 extra_data_after_insn = TRUE;
01883                 extra_data_len     = 4;
01884                 extra_data         = ex.X_add_number;
01885                 ex.X_add_number    = 0;
01886               }
01887              else if (register_name (&ex)
01888                      && (operand->flags & V850_OPERAND_REG) == 0)
01889               {
01890                 char c;
01891                 int exists = 0;
01892 
01893                 /* It is possible that an alias has been defined that
01894                    matches a register name.  For example the code may
01895                    include a ".set ZERO, 0" directive, which matches
01896                    the register name "zero".  Attempt to reparse the
01897                    field as an expression, and only complain if we
01898                    cannot generate a constant.  */
01899 
01900                 input_line_pointer = str;
01901 
01902                 c = get_symbol_end ();
01903 
01904                 if (symbol_find (str) != NULL)
01905                   exists = 1;
01906 
01907                 *input_line_pointer = c;
01908                 input_line_pointer = str;
01909 
01910                 expression (&ex);
01911 
01912                 if (ex.X_op != O_constant)
01913                   {
01914                     /* If this register is actually occurring too early on
01915                       the parsing of the instruction, (because another
01916                       field is missing) then report this.  */
01917                     if (opindex_ptr[1] != 0
01918                        && (v850_operands[opindex_ptr[1]].flags
01919                            & V850_OPERAND_REG))
01920                      errmsg = _("syntax error: value is missing before the register name");
01921                     else
01922                      errmsg = _("syntax error: register not expected");
01923 
01924                     /* If we created a symbol in the process of this
01925                       test then delete it now, so that it will not
01926                       be output with the real symbols...  */
01927                     if (exists == 0
01928                        && ex.X_op == O_symbol)
01929                      symbol_remove (ex.X_add_symbol,
01930                                    &symbol_rootP, &symbol_lastP);
01931                   }
01932               }
01933              else if (system_register_name (&ex, FALSE, FALSE)
01934                      && (operand->flags & V850_OPERAND_SRG) == 0)
01935               errmsg = _("syntax error: system register not expected");
01936 
01937              else if (cc_name (&ex)
01938                      && (operand->flags & V850_OPERAND_CC) == 0)
01939               errmsg = _("syntax error: condition code not expected");
01940 
01941              else
01942               {
01943                 expression (&ex);
01944                 /* Special case:
01945                    If we are assembling a MOV instruction and the immediate
01946                    value does not fit into the bits available then create a
01947                    fake error so that the next MOV instruction will be
01948                    selected.  This one has a 32 bit immediate field.  */
01949 
01950                 if (((insn & 0x07e0) == 0x0200)
01951                     && operand->bits == 5 /* Do not match the CALLT instruction.  */
01952                     && ex.X_op == O_constant
01953                     && (ex.X_add_number < (-(1 << (operand->bits - 1)))
01954                        || ex.X_add_number > ((1 << (operand->bits - 1)) - 1)))
01955                   errmsg = _("immediate operand is too large");
01956               }
01957 
01958              if (errmsg)
01959               goto error;
01960 
01961              switch (ex.X_op)
01962               {
01963               case O_illegal:
01964                 errmsg = _("illegal operand");
01965                 goto error;
01966               case O_absent:
01967                 errmsg = _("missing operand");
01968                 goto error;
01969               case O_register:
01970                 if ((operand->flags
01971                      & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
01972                   {
01973                     errmsg = _("invalid operand");
01974                     goto error;
01975                   }
01976                 insn = v850_insert_operand (insn, operand, ex.X_add_number,
01977                                          NULL, 0, copy_of_instruction);
01978                 break;
01979 
01980               case O_constant:
01981                 insn = v850_insert_operand (insn, operand, ex.X_add_number,
01982                                          NULL, 0, copy_of_instruction);
01983                 break;
01984 
01985               default:
01986                 /* We need to generate a fixup for this expression.  */
01987                 if (fc >= MAX_INSN_FIXUPS)
01988                   as_fatal (_("too many fixups"));
01989 
01990                 fixups[fc].exp     = ex;
01991                 fixups[fc].opindex = *opindex_ptr;
01992                 fixups[fc].reloc   = BFD_RELOC_UNUSED;
01993                 ++fc;
01994                 break;
01995               }
01996            }
01997 
01998          str = input_line_pointer;
01999          input_line_pointer = hold;
02000 
02001          while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
02002                || *str == ')')
02003            ++str;
02004        }
02005       match = 1;
02006 
02007     error:
02008       if (match == 0)
02009        {
02010          next_opcode = opcode + 1;
02011          if (next_opcode->name != NULL
02012              && strcmp (next_opcode->name, opcode->name) == 0)
02013            {
02014              opcode = next_opcode;
02015 
02016              /* Skip versions that are not supported by the target
02017                processor.  */
02018              if ((opcode->processors & processor_mask) == 0)
02019               goto error;
02020 
02021              continue;
02022            }
02023 
02024          as_bad ("%s: %s", copy_of_instruction, errmsg);
02025 
02026          if (*input_line_pointer == ']')
02027            ++input_line_pointer;
02028 
02029          ignore_rest_of_line ();
02030          input_line_pointer = saved_input_line_pointer;
02031          return;
02032        }
02033       break;
02034     }
02035 
02036   while (ISSPACE (*str))
02037     ++str;
02038 
02039   if (*str != '\0')
02040     /* xgettext:c-format  */
02041     as_bad (_("junk at end of line: `%s'"), str);
02042 
02043   input_line_pointer = str;
02044 
02045   /* Tie dwarf2 debug info to the address at the start of the insn.
02046      We can't do this after the insn has been output as the current
02047      frag may have been closed off.  eg. by frag_var.  */
02048   dwarf2_emit_insn (0);
02049 
02050   /* Write out the instruction.  */
02051 
02052   if (relaxable && fc > 0)
02053     {
02054       /* On a 64-bit host the size of an 'int' is not the same
02055         as the size of a pointer, so we need a union to convert
02056         the opindex field of the fr_cgen structure into a char *
02057         so that it can be stored in the frag.  We do not have
02058         to worry about loosing accuracy as we are not going to
02059         be even close to the 32bit limit of the int.  */
02060       union
02061       {
02062        int opindex;
02063        char * ptr;
02064       }
02065       opindex_converter;
02066 
02067       opindex_converter.opindex = fixups[0].opindex;
02068       insn_size = 2;
02069       fc = 0;
02070 
02071       if (!strcmp (opcode->name, "br"))
02072        {
02073          f = frag_var (rs_machine_dependent, 4, 2, 2,
02074                      fixups[0].exp.X_add_symbol,
02075                      fixups[0].exp.X_add_number,
02076                      opindex_converter.ptr);
02077          md_number_to_chars (f, insn, insn_size);
02078          md_number_to_chars (f + 2, 0, 2);
02079        }
02080       else
02081        {
02082          f = frag_var (rs_machine_dependent, 6, 4, 0,
02083                      fixups[0].exp.X_add_symbol,
02084                      fixups[0].exp.X_add_number,
02085                      opindex_converter.ptr);
02086          md_number_to_chars (f, insn, insn_size);
02087          md_number_to_chars (f + 2, 0, 4);
02088        }
02089     }
02090   else
02091     {
02092       /* Four byte insns have an opcode with the two high bits on.  */
02093       if ((insn & 0x0600) == 0x0600)
02094        insn_size = 4;
02095       else
02096        insn_size = 2;
02097 
02098       /* Special case: 32 bit MOV.  */
02099       if ((insn & 0xffe0) == 0x0620)
02100        insn_size = 2;
02101 
02102       f = frag_more (insn_size);
02103       md_number_to_chars (f, insn, insn_size);
02104 
02105       if (extra_data_after_insn)
02106        {
02107          f = frag_more (extra_data_len);
02108          md_number_to_chars (f, extra_data, extra_data_len);
02109 
02110          extra_data_after_insn = FALSE;
02111        }
02112     }
02113 
02114   /* Create any fixups.  At this point we do not use a
02115      bfd_reloc_code_real_type, but instead just use the
02116      BFD_RELOC_UNUSED plus the operand index.  This lets us easily
02117      handle fixups for any operand type, although that is admittedly
02118      not a very exciting feature.  We pick a BFD reloc type in
02119      md_apply_fix.  */
02120   for (i = 0; i < fc; i++)
02121     {
02122       const struct v850_operand *operand;
02123       bfd_reloc_code_real_type reloc;
02124 
02125       operand = &v850_operands[fixups[i].opindex];
02126 
02127       reloc = fixups[i].reloc;
02128 
02129       if (reloc != BFD_RELOC_UNUSED)
02130        {
02131          reloc_howto_type *reloc_howto =
02132            bfd_reloc_type_lookup (stdoutput, reloc);
02133          int size;
02134          int address;
02135          fixS *fixP;
02136 
02137          if (!reloc_howto)
02138            abort ();
02139 
02140          size = bfd_get_reloc_size (reloc_howto);
02141 
02142          /* XXX This will abort on an R_V850_8 reloc -
02143             is this reloc actually used?  */
02144          if (size != 2 && size != 4)
02145            abort ();
02146 
02147          address = (f - frag_now->fr_literal) + insn_size - size;
02148 
02149          if (reloc == BFD_RELOC_32)
02150            address += 2;
02151 
02152          fixP = fix_new_exp (frag_now, address, size,
02153                            &fixups[i].exp,
02154                            reloc_howto->pc_relative,
02155                            reloc);
02156 
02157          fixP->tc_fix_data = (void *) operand;
02158 
02159          switch (reloc)
02160            {
02161            case BFD_RELOC_LO16:
02162            case BFD_RELOC_V850_LO16_SPLIT_OFFSET:
02163            case BFD_RELOC_HI16:
02164            case BFD_RELOC_HI16_S:
02165              fixP->fx_no_overflow = 1;
02166              break;
02167            default:
02168              break;
02169            }
02170        }
02171       else
02172        {
02173          fix_new_exp (frag_now,
02174                      f - frag_now->fr_literal, 4,
02175                      & fixups[i].exp,
02176                      (operand->flags & V850_OPERAND_DISP) != 0,
02177                      (bfd_reloc_code_real_type) (fixups[i].opindex
02178                                              + (int) BFD_RELOC_UNUSED));
02179        }
02180     }
02181 
02182   input_line_pointer = saved_input_line_pointer;
02183 }
02184 
02185 /* If while processing a fixup, a reloc really needs to be created
02186    then it is done here.  */
02187 
02188 arelent *
02189 tc_gen_reloc (asection *seg ATTRIBUTE_UNUSED, fixS *fixp)
02190 {
02191   arelent *reloc;
02192 
02193   reloc                    = xmalloc (sizeof (arelent));
02194   reloc->sym_ptr_ptr  = xmalloc (sizeof (asymbol *));
02195   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
02196   reloc->address      = fixp->fx_frag->fr_address + fixp->fx_where;
02197 
02198   if (   fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
02199       || fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
02200       || fixp->fx_r_type == BFD_RELOC_V850_LONGCALL
02201       || fixp->fx_r_type == BFD_RELOC_V850_LONGJUMP
02202       || fixp->fx_r_type == BFD_RELOC_V850_ALIGN)
02203     reloc->addend = fixp->fx_offset;
02204   else
02205     {
02206       if (fixp->fx_r_type == BFD_RELOC_32
02207          && fixp->fx_pcrel)
02208        fixp->fx_r_type = BFD_RELOC_32_PCREL;
02209 
02210       reloc->addend = fixp->fx_addnumber;
02211     }
02212 
02213   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
02214 
02215   if (reloc->howto == NULL)
02216     {
02217       as_bad_where (fixp->fx_file, fixp->fx_line,
02218                   /* xgettext:c-format  */
02219                   _("reloc %d not supported by object file format"),
02220                   (int) fixp->fx_r_type);
02221 
02222       xfree (reloc);
02223 
02224       return NULL;
02225     }
02226 
02227   return reloc;
02228 }
02229 
02230 void
02231 v850_handle_align (fragS * frag)
02232 {
02233   if (v850_relax
02234       && frag->fr_type == rs_align
02235       && frag->fr_address + frag->fr_fix > 0
02236       && frag->fr_offset > 1
02237       && now_seg != bss_section
02238       && now_seg != v850_seg_table[SBSS_SECTION].s
02239       && now_seg != v850_seg_table[TBSS_SECTION].s
02240       && now_seg != v850_seg_table[ZBSS_SECTION].s)
02241     fix_new (frag, frag->fr_fix, 2, & abs_symbol, frag->fr_offset, 0,
02242             BFD_RELOC_V850_ALIGN);
02243 }
02244 
02245 /* Return current size of variable part of frag.  */
02246 
02247 int
02248 md_estimate_size_before_relax (fragS *fragp, asection *seg ATTRIBUTE_UNUSED)
02249 {
02250   if (fragp->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
02251     abort ();
02252 
02253   return md_relax_table[fragp->fr_subtype].rlx_length;
02254 }
02255 
02256 long
02257 v850_pcrel_from_section (fixS *fixp, segT section)
02258 {
02259   /* If the symbol is undefined, or in a section other than our own,
02260      or it is weak (in which case it may well be in another section,
02261      then let the linker figure it out.  */
02262   if (fixp->fx_addsy != (symbolS *) NULL
02263       && (! S_IS_DEFINED (fixp->fx_addsy)
02264          || S_IS_WEAK (fixp->fx_addsy)
02265          || (S_GET_SEGMENT (fixp->fx_addsy) != section)))
02266     return 0;
02267 
02268   return fixp->fx_frag->fr_address + fixp->fx_where;
02269 }
02270 
02271 void
02272 md_apply_fix (fixS *fixP, valueT *valueP, segT seg ATTRIBUTE_UNUSED)
02273 {
02274   valueT value = * valueP;
02275   char *where;
02276 
02277   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
02278       || fixP->fx_r_type == BFD_RELOC_V850_LONGCALL
02279       || fixP->fx_r_type == BFD_RELOC_V850_LONGJUMP
02280       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
02281     {
02282       fixP->fx_done = 0;
02283       return;
02284     }
02285 
02286   if (fixP->fx_addsy == (symbolS *) NULL)
02287     fixP->fx_addnumber = value,
02288     fixP->fx_done = 1;
02289 
02290   else if (fixP->fx_pcrel)
02291     fixP->fx_addnumber = fixP->fx_offset;
02292 
02293   else
02294     {
02295       value = fixP->fx_offset;
02296       if (fixP->fx_subsy != (symbolS *) NULL)
02297        {
02298          if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
02299            value -= S_GET_VALUE (fixP->fx_subsy);
02300          else
02301            /* We don't actually support subtracting a symbol.  */
02302            as_bad_where (fixP->fx_file, fixP->fx_line,
02303                        _("expression too complex"));
02304        }
02305       fixP->fx_addnumber = value;
02306     }
02307 
02308   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
02309     {
02310       int opindex;
02311       const struct v850_operand *operand;
02312       unsigned long insn;
02313 
02314       opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
02315       operand = &v850_operands[opindex];
02316 
02317       /* Fetch the instruction, insert the fully resolved operand
02318         value, and stuff the instruction back again.
02319 
02320         Note the instruction has been stored in little endian
02321         format!  */
02322       where = fixP->fx_frag->fr_literal + fixP->fx_where;
02323 
02324       insn = bfd_getl32 ((unsigned char *) where);
02325       insn = v850_insert_operand (insn, operand, (offsetT) value,
02326                               fixP->fx_file, fixP->fx_line, NULL);
02327       bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
02328 
02329       if (fixP->fx_done)
02330        /* Nothing else to do here.  */
02331        return;
02332 
02333       /* Determine a BFD reloc value based on the operand information.
02334         We are only prepared to turn a few of the operands into relocs.  */
02335 
02336       if (operand->bits == 22)
02337        fixP->fx_r_type = BFD_RELOC_V850_22_PCREL;
02338       else if (operand->bits == 9)
02339        fixP->fx_r_type = BFD_RELOC_V850_9_PCREL;
02340       else
02341        {
02342          as_bad_where (fixP->fx_file, fixP->fx_line,
02343                      _("unresolved expression that must be resolved"));
02344          fixP->fx_done = 1;
02345          return;
02346        }
02347     }
02348   else if (fixP->fx_done)
02349     {
02350       /* We still have to insert the value into memory!  */
02351       where = fixP->fx_frag->fr_literal + fixP->fx_where;
02352 
02353       if (fixP->tc_fix_data != NULL
02354          && ((struct v850_operand *) fixP->tc_fix_data)->insert != NULL)
02355        {
02356          const char * message = NULL;
02357          struct v850_operand * operand = (struct v850_operand *) fixP->tc_fix_data;
02358          unsigned long insn;
02359 
02360          /* The variable "where" currently points at the exact point inside
02361             the insn where we need to insert the value.  But we need to
02362             extract the entire insn so we probably need to move "where"
02363             back a few bytes.  */
02364          if (fixP->fx_size == 2)
02365            where -= 2;
02366          else if (fixP->fx_size == 1)
02367            where -= 3;
02368 
02369          insn = bfd_getl32 ((unsigned char *) where);
02370 
02371          /* Use the operand's insertion procedure, if present, in order to
02372             make sure that the value is correctly stored in the insn.  */
02373          insn = operand->insert (insn, (offsetT) value, & message);
02374          /* Ignore message even if it is set.  */
02375 
02376          bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
02377        }
02378       else
02379        {
02380          if (fixP->fx_r_type == BFD_RELOC_V850_LO16_SPLIT_OFFSET)
02381            bfd_putl32 (((value << 16) & 0xfffe0000)
02382                      | ((value << 5) & 0x20)
02383                      | (bfd_getl32 (where) & ~0xfffe0020), where);
02384          else if (fixP->fx_size == 1)
02385            *where = value & 0xff;
02386          else if (fixP->fx_size == 2)
02387            bfd_putl16 (value & 0xffff, (unsigned char *) where);
02388          else if (fixP->fx_size == 4)
02389            bfd_putl32 (value, (unsigned char *) where);
02390        }
02391     }
02392 }
02393 
02394 /* Parse a cons expression.  We have to handle hi(), lo(), etc
02395    on the v850.  */
02396 
02397 void
02398 parse_cons_expression_v850 (expressionS *exp)
02399 {
02400   /* See if there's a reloc prefix like hi() we have to handle.  */
02401   hold_cons_reloc = v850_reloc_prefix (NULL);
02402 
02403   /* Do normal expression parsing.  */
02404   expression (exp);
02405 }
02406 
02407 /* Create a fixup for a cons expression.  If parse_cons_expression_v850
02408    found a reloc prefix, then we use that reloc, else we choose an
02409    appropriate one based on the size of the expression.  */
02410 
02411 void
02412 cons_fix_new_v850 (fragS *frag,
02413                  int where,
02414                  int size,
02415                  expressionS *exp)
02416 {
02417   if (hold_cons_reloc == BFD_RELOC_UNUSED)
02418     {
02419       if (size == 4)
02420        hold_cons_reloc = BFD_RELOC_32;
02421       if (size == 2)
02422        hold_cons_reloc = BFD_RELOC_16;
02423       if (size == 1)
02424        hold_cons_reloc = BFD_RELOC_8;
02425     }
02426 
02427   if (exp != NULL)
02428     fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
02429   else
02430     fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
02431 
02432   hold_cons_reloc = BFD_RELOC_UNUSED;
02433 }
02434 
02435 bfd_boolean
02436 v850_fix_adjustable (fixS *fixP)
02437 {
02438   if (fixP->fx_addsy == NULL)
02439     return 1;
02440 
02441   /* Don't adjust function names.  */
02442   if (S_IS_FUNCTION (fixP->fx_addsy))
02443     return 0;
02444 
02445   /* We need the symbol name for the VTABLE entries.  */
02446   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
02447       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
02448     return 0;
02449 
02450   return 1;
02451 }
02452 
02453 int
02454 v850_force_relocation (struct fix *fixP)
02455 {
02456   if (fixP->fx_r_type == BFD_RELOC_V850_LONGCALL
02457       || fixP->fx_r_type == BFD_RELOC_V850_LONGJUMP)
02458     return 1;
02459 
02460   if (v850_relax
02461       && (fixP->fx_pcrel
02462          || fixP->fx_r_type == BFD_RELOC_V850_ALIGN
02463          || fixP->fx_r_type == BFD_RELOC_V850_22_PCREL
02464          || fixP->fx_r_type == BFD_RELOC_V850_9_PCREL
02465          || fixP->fx_r_type >= BFD_RELOC_UNUSED))
02466     return 1;
02467 
02468   return generic_force_reloc (fixP);
02469 }