Remove duplicate code and provide a function for generating internally consistent...
[external/binutils.git] / gas / config / tc-v850.c
1 /* tc-v850.c -- Assembler code for the NEC V850
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4
5    This file is part of GAS, the GNU Assembler.
6
7    GAS is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GAS is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GAS; see the file COPYING.  If not, write to
19    the Free Software Foundation, 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <stdio.h>
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "subsegs.h"
26 #include "opcode/v850.h"
27 #include "dwarf2dbg.h"
28
29 /* Sign-extend a 16-bit number.  */
30 #define SEXT16(x)       ((((x) & 0xffff) ^ (~0x7fff)) + 0x8000)
31
32 /* Temporarily holds the reloc in a cons expression.  */
33 static bfd_reloc_code_real_type hold_cons_reloc = BFD_RELOC_UNUSED;
34
35 /* Set to TRUE if we want to be pedantic about signed overflows.  */
36 static bfd_boolean warn_signed_overflows   = FALSE;
37 static bfd_boolean warn_unsigned_overflows = FALSE;
38
39 /* Indicates the target BFD machine number.  */
40 static int machine = -1;
41
42 /* Indicates the target processor(s) for the assemble.  */
43 static int processor_mask = -1;
44 \f
45 /* Structure to hold information about predefined registers.  */
46 struct reg_name {
47   const char *name;
48   int value;
49 };
50
51 /* Generic assembler global variables which must be defined by all
52    targets.  */
53
54 /* Characters which always start a comment.  */
55 const char comment_chars[] = "#";
56
57 /* Characters which start a comment at the beginning of a line.  */
58 const char line_comment_chars[] = ";#";
59
60 /* Characters which may be used to separate multiple commands on a
61    single line.  */
62 const char line_separator_chars[] = ";";
63
64 /* Characters which are used to indicate an exponent in a floating
65    point number.  */
66 const char EXP_CHARS[] = "eE";
67
68 /* Characters which mean that a number is a floating point constant,
69    as in 0d1.0.  */
70 const char FLT_CHARS[] = "dD";
71 \f
72 const relax_typeS md_relax_table[] = {
73   /* Conditional branches.  */
74   {0xff,     -0x100,    2, 1},
75   {0x1fffff, -0x200000, 6, 0},
76   /* Unconditional branches.  */
77   {0xff,     -0x100,    2, 3},
78   {0x1fffff, -0x200000, 4, 0},
79 };
80
81 static int  v850_relax = 0;
82
83 /* Fixups.  */
84 #define MAX_INSN_FIXUPS (5)
85 struct v850_fixup {
86   expressionS exp;
87   int opindex;
88   bfd_reloc_code_real_type reloc;
89 };
90
91 struct v850_fixup fixups[MAX_INSN_FIXUPS];
92 static int fc;
93
94 struct v850_seg_entry
95 {
96   segT s;
97   const char *name;
98   flagword flags;
99 };
100
101 struct v850_seg_entry v850_seg_table[] =
102 {
103   { NULL, ".sdata",
104     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
105     | SEC_SMALL_DATA },
106   { NULL, ".tdata",
107     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
108   { NULL, ".zdata",
109     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
110   { NULL, ".sbss",
111     SEC_ALLOC | SEC_SMALL_DATA },
112   { NULL, ".tbss",
113     SEC_ALLOC },
114   { NULL, ".zbss",
115     SEC_ALLOC},
116   { NULL, ".rosdata",
117     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_DATA
118     | SEC_HAS_CONTENTS | SEC_SMALL_DATA },
119   { NULL, ".rozdata",
120     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_DATA
121     | SEC_HAS_CONTENTS },
122   { NULL, ".scommon",
123     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
124     | SEC_SMALL_DATA | SEC_IS_COMMON },
125   { NULL, ".tcommon",
126     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
127     | SEC_IS_COMMON },
128   { NULL, ".zcommon",
129     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS
130     | SEC_IS_COMMON },
131   { NULL, ".call_table_data",
132     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS },
133   { NULL, ".call_table_text",
134     SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY | SEC_CODE
135     | SEC_HAS_CONTENTS},
136   { NULL, ".bss",
137     SEC_ALLOC }
138 };
139
140 #define SDATA_SECTION           0
141 #define TDATA_SECTION           1
142 #define ZDATA_SECTION           2
143 #define SBSS_SECTION            3
144 #define TBSS_SECTION            4
145 #define ZBSS_SECTION            5
146 #define ROSDATA_SECTION         6
147 #define ROZDATA_SECTION         7
148 #define SCOMMON_SECTION         8
149 #define TCOMMON_SECTION         9
150 #define ZCOMMON_SECTION         10
151 #define CALL_TABLE_DATA_SECTION 11
152 #define CALL_TABLE_TEXT_SECTION 12
153 #define BSS_SECTION             13
154
155 static void do_v850_seg PARAMS ((int, subsegT));
156
157 static void
158 do_v850_seg (i, sub)
159      int i;
160      subsegT sub;
161 {
162   struct v850_seg_entry *seg = v850_seg_table + i;
163
164   obj_elf_section_change_hook ();
165   if (seg->s != NULL)
166     {
167       subseg_set (seg->s, sub);
168     }
169   else
170     {
171       seg->s = subseg_new (seg->name, sub);
172       bfd_set_section_flags (stdoutput, seg->s, seg->flags);
173       if ((seg->flags & SEC_LOAD) == 0)
174         seg_info (seg->s)->bss = 1;
175     }
176 }
177
178 static void v850_seg PARAMS ((int i));
179
180 static void
181 v850_seg (i)
182      int i;
183 {
184   subsegT sub = get_absolute_expression ();
185
186   do_v850_seg (i, sub);
187   demand_empty_rest_of_line ();
188 }
189
190 static void v850_offset PARAMS ((int));
191
192 static void
193 v850_offset (ignore)
194      int ignore ATTRIBUTE_UNUSED;
195 {
196   char *pfrag;
197   int temp = get_absolute_expression ();
198
199   pfrag = frag_var (rs_org, 1, 1, (relax_substateT)0, (symbolS *)0,
200                     (offsetT) temp, (char *) 0);
201   *pfrag = 0;
202
203   demand_empty_rest_of_line ();
204 }
205
206 /* Copied from obj_elf_common() in gas/config/obj-elf.c.  */
207
208 static void v850_comm PARAMS ((int));
209
210 static void
211 v850_comm (area)
212      int area;
213 {
214   char *name;
215   char c;
216   char *p;
217   int temp;
218   unsigned int size;
219   symbolS *symbolP;
220   int have_align;
221
222   name = input_line_pointer;
223   c = get_symbol_end ();
224
225   /* Just after name is now '\0'.  */
226   p = input_line_pointer;
227   *p = c;
228
229   SKIP_WHITESPACE ();
230
231   if (*input_line_pointer != ',')
232     {
233       as_bad (_("Expected comma after symbol-name"));
234       ignore_rest_of_line ();
235       return;
236     }
237
238   /* Skip ','.  */
239   input_line_pointer++;
240
241   if ((temp = get_absolute_expression ()) < 0)
242     {
243       /* xgettext:c-format  */
244       as_bad (_(".COMMon length (%d.) < 0! Ignored."), temp);
245       ignore_rest_of_line ();
246       return;
247     }
248
249   size = temp;
250   *p = 0;
251   symbolP = symbol_find_or_make (name);
252   *p = c;
253
254   if (S_IS_DEFINED (symbolP) && ! S_IS_COMMON (symbolP))
255     {
256       as_bad (_("Ignoring attempt to re-define symbol"));
257       ignore_rest_of_line ();
258       return;
259     }
260
261   if (S_GET_VALUE (symbolP) != 0)
262     {
263       if (S_GET_VALUE (symbolP) != size)
264         {
265           /* xgettext:c-format  */
266           as_warn (_("Length of .comm \"%s\" is already %ld. Not changed to %d."),
267                    S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
268         }
269     }
270
271   know (symbol_get_frag (symbolP) == &zero_address_frag);
272
273   if (*input_line_pointer != ',')
274     have_align = 0;
275   else
276     {
277       have_align = 1;
278       input_line_pointer++;
279       SKIP_WHITESPACE ();
280     }
281
282   if (! have_align || *input_line_pointer != '"')
283     {
284       if (! have_align)
285         temp = 0;
286       else
287         {
288           temp = get_absolute_expression ();
289
290           if (temp < 0)
291             {
292               temp = 0;
293               as_warn (_("Common alignment negative; 0 assumed"));
294             }
295         }
296
297       if (symbol_get_obj (symbolP)->local)
298         {
299           segT old_sec;
300           int old_subsec;
301           char *pfrag;
302           int align;
303           flagword applicable;
304
305           old_sec = now_seg;
306           old_subsec = now_subseg;
307
308           applicable = bfd_applicable_section_flags (stdoutput);
309
310           applicable &= SEC_ALLOC;
311
312           switch (area)
313             {
314             case SCOMMON_SECTION:
315               do_v850_seg (SBSS_SECTION, 0);
316               break;
317
318             case ZCOMMON_SECTION:
319               do_v850_seg (ZBSS_SECTION, 0);
320               break;
321
322             case TCOMMON_SECTION:
323               do_v850_seg (TBSS_SECTION, 0);
324               break;
325             }
326
327           if (temp)
328             {
329               /* Convert to a power of 2 alignment.  */
330               for (align = 0; (temp & 1) == 0; temp >>= 1, ++align)
331                 ;
332
333               if (temp != 1)
334                 {
335                   as_bad (_("Common alignment not a power of 2"));
336                   ignore_rest_of_line ();
337                   return;
338                 }
339             }
340           else
341             align = 0;
342
343           record_alignment (now_seg, align);
344
345           if (align)
346             frag_align (align, 0, 0);
347
348           switch (area)
349             {
350             case SCOMMON_SECTION:
351               if (S_GET_SEGMENT (symbolP) == v850_seg_table[SBSS_SECTION].s)
352                 symbol_get_frag (symbolP)->fr_symbol = 0;
353               break;
354
355             case ZCOMMON_SECTION:
356               if (S_GET_SEGMENT (symbolP) == v850_seg_table[ZBSS_SECTION].s)
357                 symbol_get_frag (symbolP)->fr_symbol = 0;
358               break;
359
360             case TCOMMON_SECTION:
361               if (S_GET_SEGMENT (symbolP) == v850_seg_table[TBSS_SECTION].s)
362                 symbol_get_frag (symbolP)->fr_symbol = 0;
363               break;
364
365             default:
366               abort ();
367             }
368
369           symbol_set_frag (symbolP, frag_now);
370           pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP,
371                             (offsetT) size, (char *) 0);
372           *pfrag = 0;
373           S_SET_SIZE (symbolP, size);
374
375           switch (area)
376             {
377             case SCOMMON_SECTION:
378               S_SET_SEGMENT (symbolP, v850_seg_table[SBSS_SECTION].s);
379               break;
380
381             case ZCOMMON_SECTION:
382               S_SET_SEGMENT (symbolP, v850_seg_table[ZBSS_SECTION].s);
383               break;
384
385             case TCOMMON_SECTION:
386               S_SET_SEGMENT (symbolP, v850_seg_table[TBSS_SECTION].s);
387               break;
388
389             default:
390               abort ();
391             }
392
393           S_CLEAR_EXTERNAL (symbolP);
394           obj_elf_section_change_hook ();
395           subseg_set (old_sec, old_subsec);
396         }
397       else
398         {
399           segT   old_sec;
400           int    old_subsec;
401
402         allocate_common:
403           old_sec = now_seg;
404           old_subsec = now_subseg;
405
406           S_SET_VALUE (symbolP, (valueT) size);
407           S_SET_ALIGN (symbolP, temp);
408           S_SET_EXTERNAL (symbolP);
409
410           switch (area)
411             {
412             case SCOMMON_SECTION:
413             case ZCOMMON_SECTION:
414             case TCOMMON_SECTION:
415               do_v850_seg (area, 0);
416               S_SET_SEGMENT (symbolP, v850_seg_table[area].s);
417               break;
418
419             default:
420               abort ();
421             }
422
423           obj_elf_section_change_hook ();
424           subseg_set (old_sec, old_subsec);
425         }
426     }
427   else
428     {
429       input_line_pointer++;
430
431       /* @@ Some use the dot, some don't.  Can we get some consistency??  */
432       if (*input_line_pointer == '.')
433         input_line_pointer++;
434
435       /* @@ Some say data, some say bss.  */
436       if (strncmp (input_line_pointer, "bss\"", 4)
437           && strncmp (input_line_pointer, "data\"", 5))
438         {
439           while (*--input_line_pointer != '"')
440             ;
441           input_line_pointer--;
442           goto bad_common_segment;
443         }
444       while (*input_line_pointer++ != '"')
445         ;
446       goto allocate_common;
447     }
448
449   symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
450
451   demand_empty_rest_of_line ();
452   return;
453
454   {
455   bad_common_segment:
456     p = input_line_pointer;
457     while (*p && *p != '\n')
458       p++;
459     c = *p;
460     *p = '\0';
461     as_bad (_("bad .common segment %s"), input_line_pointer + 1);
462     *p = c;
463     input_line_pointer = p;
464     ignore_rest_of_line ();
465     return;
466   }
467 }
468
469 static void set_machine PARAMS ((int));
470
471 static void
472 set_machine (number)
473      int number;
474 {
475   machine = number;
476   bfd_set_arch_mach (stdoutput, TARGET_ARCH, machine);
477
478   switch (machine)
479     {
480     case 0:               processor_mask = PROCESSOR_V850;   break;
481     case bfd_mach_v850e:  processor_mask = PROCESSOR_V850E;  break;
482     case bfd_mach_v850e1: processor_mask = PROCESSOR_V850E;  break;
483     }
484 }
485
486 static void v850_longcode PARAMS ((int));
487
488 static void
489 v850_longcode (type)
490      int type;
491 {
492   expressionS ex;
493
494   if (! v850_relax)
495     {
496       if (type == 1)
497         as_warn (".longcall pseudo-op seen when not relaxing");
498       else
499         as_warn (".longjump pseudo-op seen when not relaxing");
500     }
501
502   expression (&ex);
503
504   if (ex.X_op != O_symbol || ex.X_add_number != 0)
505     {
506       as_bad ("bad .longcall format");
507       ignore_rest_of_line ();
508
509       return;
510     }
511
512   if (type == 1)
513     fix_new_exp (frag_now, frag_now_fix (), 4, & ex, 1,
514                  BFD_RELOC_V850_LONGCALL);
515   else
516     fix_new_exp (frag_now, frag_now_fix (), 4, & ex, 1,
517                  BFD_RELOC_V850_LONGJUMP);
518
519   demand_empty_rest_of_line ();
520 }
521
522 /* The target specific pseudo-ops which we support.  */
523 const pseudo_typeS md_pseudo_table[] =
524 {
525   { "sdata",            v850_seg,               SDATA_SECTION           },
526   { "tdata",            v850_seg,               TDATA_SECTION           },
527   { "zdata",            v850_seg,               ZDATA_SECTION           },
528   { "sbss",             v850_seg,               SBSS_SECTION            },
529   { "tbss",             v850_seg,               TBSS_SECTION            },
530   { "zbss",             v850_seg,               ZBSS_SECTION            },
531   { "rosdata",          v850_seg,               ROSDATA_SECTION         },
532   { "rozdata",          v850_seg,               ROZDATA_SECTION         },
533   { "bss",              v850_seg,               BSS_SECTION             },
534   { "offset",           v850_offset,            0                       },
535   { "word",             cons,                   4                       },
536   { "zcomm",            v850_comm,              ZCOMMON_SECTION         },
537   { "scomm",            v850_comm,              SCOMMON_SECTION         },
538   { "tcomm",            v850_comm,              TCOMMON_SECTION         },
539   { "v850",             set_machine,            0                       },
540   { "call_table_data",  v850_seg,               CALL_TABLE_DATA_SECTION },
541   { "call_table_text",  v850_seg,               CALL_TABLE_TEXT_SECTION },
542   { "v850e",            set_machine,            bfd_mach_v850e          },
543   { "v850e1",           set_machine,            bfd_mach_v850e1         },
544   { "longcall",         v850_longcode,          1                       },
545   { "longjump",         v850_longcode,          2                       },
546   { NULL,               NULL,                   0                       }
547 };
548
549 /* Opcode hash table.  */
550 static struct hash_control *v850_hash;
551
552 /* This table is sorted.  Suitable for searching by a binary search.  */
553 static const struct reg_name pre_defined_registers[] =
554 {
555   { "ep",  30 },                /* ep - element ptr */
556   { "gp",   4 },                /* gp - global ptr  */
557   { "hp",   2 },                /* hp - handler stack ptr  */
558   { "lp",  31 },                /* lp - link ptr  */
559   { "r0",   0 },
560   { "r1",   1 },
561   { "r10", 10 },
562   { "r11", 11 },
563   { "r12", 12 },
564   { "r13", 13 },
565   { "r14", 14 },
566   { "r15", 15 },
567   { "r16", 16 },
568   { "r17", 17 },
569   { "r18", 18 },
570   { "r19", 19 },
571   { "r2",   2 },
572   { "r20", 20 },
573   { "r21", 21 },
574   { "r22", 22 },
575   { "r23", 23 },
576   { "r24", 24 },
577   { "r25", 25 },
578   { "r26", 26 },
579   { "r27", 27 },
580   { "r28", 28 },
581   { "r29", 29 },
582   { "r3",   3 },
583   { "r30", 30 },
584   { "r31", 31 },
585   { "r4",   4 },
586   { "r5",   5 },
587   { "r6",   6 },
588   { "r7",   7 },
589   { "r8",   8 },
590   { "r9",   9 },
591   { "sp",   3 },                /* sp - stack ptr  */
592   { "tp",   5 },                /* tp - text ptr  */
593   { "zero", 0 },
594 };
595
596 #define REG_NAME_CNT                                            \
597   (sizeof (pre_defined_registers) / sizeof (struct reg_name))
598
599 static const struct reg_name system_registers[] =
600 {
601   { "asid",  23 },
602   { "bpc",   22 },
603   { "bpav",  24 },
604   { "bpam",  25 },
605   { "bpdv",  26 },
606   { "bpdm",  27 },
607   { "ctbp",  20 },
608   { "ctpc",  16 },
609   { "ctpsw", 17 },
610   { "dbpc",  18 },
611   { "dbpsw", 19 },
612   { "dir",   21 },
613   { "ecr",    4 },
614   { "eipc",   0 },
615   { "eipsw",  1 },
616   { "fepc",   2 },
617   { "fepsw",  3 },
618   { "psw",    5 },
619 };
620
621 #define SYSREG_NAME_CNT                                         \
622   (sizeof (system_registers) / sizeof (struct reg_name))
623
624 static const struct reg_name system_list_registers[] =
625 {
626   {"PS",      5 },
627   {"SR",      0 + 1}
628 };
629
630 #define SYSREGLIST_NAME_CNT                                     \
631   (sizeof (system_list_registers) / sizeof (struct reg_name))
632
633 static const struct reg_name cc_names[] =
634 {
635   { "c",  0x1 },
636   { "e",  0x2 },
637   { "ge", 0xe },
638   { "gt", 0xf },
639   { "h",  0xb },
640   { "l",  0x1 },
641   { "le", 0x7 },
642   { "lt", 0x6 },
643   { "n",  0x4 },
644   { "nc", 0x9 },
645   { "ne", 0xa },
646   { "nh", 0x3 },
647   { "nl", 0x9 },
648   { "ns", 0xc },
649   { "nv", 0x8 },
650   { "nz", 0xa },
651   { "p",  0xc },
652   { "s",  0x4 },
653   { "sa", 0xd },
654   { "t",  0x5 },
655   { "v",  0x0 },
656   { "z",  0x2 },
657 };
658
659 #define CC_NAME_CNT                                     \
660   (sizeof (cc_names) / sizeof (struct reg_name))
661
662 /* Do a binary search of the given register table to see if NAME is a
663    valid regiter name.  Return the register number from the array on
664    success, or -1 on failure.  */
665
666 static int reg_name_search
667   PARAMS ((const struct reg_name *, int, const char *, bfd_boolean));
668
669 static int
670 reg_name_search (regs, regcount, name, accept_numbers)
671      const struct reg_name *regs;
672      int regcount;
673      const char *name;
674      bfd_boolean accept_numbers;
675 {
676   int middle, low, high;
677   int cmp;
678   symbolS *symbolP;
679
680   /* If the register name is a symbol, then evaluate it.  */
681   if ((symbolP = symbol_find (name)) != NULL)
682     {
683       /* If the symbol is an alias for another name then use that.
684          If the symbol is an alias for a number, then return the number.  */
685       if (symbol_equated_p (symbolP))
686         {
687           name
688             = S_GET_NAME (symbol_get_value_expression (symbolP)->X_add_symbol);
689         }
690       else if (accept_numbers)
691         {
692           int reg = S_GET_VALUE (symbolP);
693
694           if (reg >= 0 && reg <= 31)
695             return reg;
696         }
697
698       /* Otherwise drop through and try parsing name normally.  */
699     }
700
701   low = 0;
702   high = regcount - 1;
703
704   do
705     {
706       middle = (low + high) / 2;
707       cmp = strcasecmp (name, regs[middle].name);
708       if (cmp < 0)
709         high = middle - 1;
710       else if (cmp > 0)
711         low = middle + 1;
712       else
713         return regs[middle].value;
714     }
715   while (low <= high);
716   return -1;
717 }
718
719 /* Summary of register_name().
720
721    in: Input_line_pointer points to 1st char of operand.
722
723    out: An expressionS.
724         The operand may have been a register: in this case, X_op == O_register,
725         X_add_number is set to the register number, and truth is returned.
726         Input_line_pointer->(next non-blank) char after operand, or is in
727         its original state.  */
728
729 static bfd_boolean register_name PARAMS ((expressionS *));
730
731 static bfd_boolean
732 register_name (expressionP)
733      expressionS *expressionP;
734 {
735   int reg_number;
736   char *name;
737   char *start;
738   char c;
739
740   /* Find the spelling of the operand.  */
741   start = name = input_line_pointer;
742
743   c = get_symbol_end ();
744
745   reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT,
746                                 name, FALSE);
747
748   /* Put back the delimiting char.  */
749   *input_line_pointer = c;
750
751   /* Look to see if it's in the register table.  */
752   if (reg_number >= 0)
753     {
754       expressionP->X_op         = O_register;
755       expressionP->X_add_number = reg_number;
756
757       /* Make the rest nice.  */
758       expressionP->X_add_symbol = NULL;
759       expressionP->X_op_symbol  = NULL;
760
761       return TRUE;
762     }
763   else
764     {
765       /* Reset the line as if we had not done anything.  */
766       input_line_pointer = start;
767
768       return FALSE;
769     }
770 }
771
772 /* Summary of system_register_name().
773
774    in:  INPUT_LINE_POINTER points to 1st char of operand.
775         EXPRESSIONP points to an expression structure to be filled in.
776         ACCEPT_NUMBERS is true iff numerical register names may be used.
777         ACCEPT_LIST_NAMES is true iff the special names PS and SR may be
778         accepted.
779
780    out: An expressionS structure in expressionP.
781         The operand may have been a register: in this case, X_op == O_register,
782         X_add_number is set to the register number, and truth is returned.
783         Input_line_pointer->(next non-blank) char after operand, or is in
784         its original state.  */
785
786 static bfd_boolean system_register_name
787   PARAMS ((expressionS *, bfd_boolean, bfd_boolean));
788
789 static bfd_boolean
790 system_register_name (expressionP, accept_numbers, accept_list_names)
791      expressionS *expressionP;
792      bfd_boolean accept_numbers;
793      bfd_boolean accept_list_names;
794 {
795   int reg_number;
796   char *name;
797   char *start;
798   char c;
799
800   /* Find the spelling of the operand.  */
801   start = name = input_line_pointer;
802
803   c = get_symbol_end ();
804   reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name,
805                                 accept_numbers);
806
807   /* Put back the delimiting char.  */
808   *input_line_pointer = c;
809
810   if (reg_number < 0
811       && accept_numbers)
812     {
813       /* Reset input_line pointer.  */
814       input_line_pointer = start;
815
816       if (ISDIGIT (*input_line_pointer))
817         {
818           reg_number = strtol (input_line_pointer, &input_line_pointer, 10);
819
820           /* Make sure that the register number is allowable.  */
821           if (reg_number < 0
822               || (reg_number > 5 && reg_number < 16)
823               || reg_number > 27)
824             {
825               reg_number = -1;
826             }
827         }
828       else if (accept_list_names)
829         {
830           c = get_symbol_end ();
831           reg_number = reg_name_search (system_list_registers,
832                                         SYSREGLIST_NAME_CNT, name, FALSE);
833
834           /* Put back the delimiting char.  */
835           *input_line_pointer = c;
836         }
837     }
838
839   /* Look to see if it's in the register table.  */
840   if (reg_number >= 0)
841     {
842       expressionP->X_op         = O_register;
843       expressionP->X_add_number = reg_number;
844
845       /* Make the rest nice.  */
846       expressionP->X_add_symbol = NULL;
847       expressionP->X_op_symbol  = NULL;
848
849       return TRUE;
850     }
851   else
852     {
853       /* Reset the line as if we had not done anything.  */
854       input_line_pointer = start;
855
856       return FALSE;
857     }
858 }
859
860 /* Summary of cc_name().
861
862    in: INPUT_LINE_POINTER points to 1st char of operand.
863
864    out: An expressionS.
865         The operand may have been a register: in this case, X_op == O_register,
866         X_add_number is set to the register number, and truth is returned.
867         Input_line_pointer->(next non-blank) char after operand, or is in
868         its original state.  */
869
870 static bfd_boolean cc_name PARAMS ((expressionS *));
871
872 static bfd_boolean
873 cc_name (expressionP)
874      expressionS *expressionP;
875 {
876   int reg_number;
877   char *name;
878   char *start;
879   char c;
880
881   /* Find the spelling of the operand.  */
882   start = name = input_line_pointer;
883
884   c = get_symbol_end ();
885   reg_number = reg_name_search (cc_names, CC_NAME_CNT, name, FALSE);
886
887   /* Put back the delimiting char.  */
888   *input_line_pointer = c;
889
890   /* Look to see if it's in the register table.  */
891   if (reg_number >= 0)
892     {
893       expressionP->X_op         = O_constant;
894       expressionP->X_add_number = reg_number;
895
896       /* Make the rest nice.  */
897       expressionP->X_add_symbol = NULL;
898       expressionP->X_op_symbol  = NULL;
899
900       return TRUE;
901     }
902   else
903     {
904       /* Reset the line as if we had not done anything.  */
905       input_line_pointer = start;
906
907       return FALSE;
908     }
909 }
910
911 static void skip_white_space PARAMS ((void));
912
913 static void
914 skip_white_space ()
915 {
916   while (*input_line_pointer == ' '
917          || *input_line_pointer == '\t')
918     ++input_line_pointer;
919 }
920
921 /* Summary of parse_register_list ().
922
923    in: INPUT_LINE_POINTER  points to 1st char of a list of registers.
924        INSN                is the partially constructed instruction.
925        OPERAND             is the operand being inserted.
926
927    out: NULL if the parse completed successfully, otherwise a
928         pointer to an error message is returned.  If the parse
929         completes the correct bit fields in the instruction
930         will be filled in.
931
932    Parses register lists with the syntax:
933
934      { rX }
935      { rX, rY }
936      { rX - rY }
937      { rX - rY, rZ }
938      etc
939
940    and also parses constant expressions whoes bits indicate the
941    registers in the lists.  The LSB in the expression refers to
942    the lowest numbered permissible register in the register list,
943    and so on upwards.  System registers are considered to be very
944    high numbers.  */
945
946 static char *parse_register_list
947   PARAMS ((unsigned long *, const struct v850_operand *));
948
949 static char *
950 parse_register_list (insn, operand)
951      unsigned long *insn;
952      const struct v850_operand *operand;
953 {
954   static int type1_regs[32] = {
955     30,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
956      0,  0,  0,  0,  0, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24
957   };
958   static int type2_regs[32] = {
959     19, 18, 17, 16,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
960      0,  0,  0,  0, 30, 31, 29, 28, 23, 22, 21, 20, 27, 26, 25, 24
961   };
962   static int type3_regs[32] = {
963      3,  2,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
964      0,  0,  0,  0, 14, 15, 13, 12,  7,  6,  5,  4, 11, 10,  9,  8
965   };
966   int *regs;
967   expressionS exp;
968
969   /* Select a register array to parse.  */
970   switch (operand->shift)
971     {
972     case 0xffe00001: regs = type1_regs; break;
973     case 0xfff8000f: regs = type2_regs; break;
974     case 0xfff8001f: regs = type3_regs; break;
975     default:
976       as_bad (_("unknown operand shift: %x\n"), operand->shift);
977       return _("internal failure in parse_register_list");
978     }
979
980   skip_white_space ();
981
982   /* If the expression starts with a curly brace it is a register list.
983      Otherwise it is a constant expression, whoes bits indicate which
984      registers are to be included in the list.  */
985   if (*input_line_pointer != '{')
986     {
987       int reg;
988       int i;
989
990       expression (&exp);
991
992       if (exp.X_op != O_constant)
993         return _("constant expression or register list expected");
994
995       if (regs == type1_regs)
996         {
997           if (exp.X_add_number & 0xFFFFF000)
998             return _("high bits set in register list expression");
999
1000           for (reg = 20; reg < 32; reg++)
1001             if (exp.X_add_number & (1 << (reg - 20)))
1002               {
1003                 for (i = 0; i < 32; i++)
1004                   if (regs[i] == reg)
1005                     *insn |= (1 << i);
1006               }
1007         }
1008       else if (regs == type2_regs)
1009         {
1010           if (exp.X_add_number & 0xFFFE0000)
1011             return _("high bits set in register list expression");
1012
1013           for (reg = 1; reg < 16; reg++)
1014             if (exp.X_add_number & (1 << (reg - 1)))
1015               {
1016                 for (i = 0; i < 32; i++)
1017                   if (regs[i] == reg)
1018                     *insn |= (1 << i);
1019               }
1020
1021           if (exp.X_add_number & (1 << 15))
1022             *insn |= (1 << 3);
1023
1024           if (exp.X_add_number & (1 << 16))
1025             *insn |= (1 << 19);
1026         }
1027       else /* regs == type3_regs  */
1028         {
1029           if (exp.X_add_number & 0xFFFE0000)
1030             return _("high bits set in register list expression");
1031
1032           for (reg = 16; reg < 32; reg++)
1033             if (exp.X_add_number & (1 << (reg - 16)))
1034               {
1035                 for (i = 0; i < 32; i++)
1036                   if (regs[i] == reg)
1037                     *insn |= (1 << i);
1038               }
1039
1040           if (exp.X_add_number & (1 << 16))
1041             *insn |= (1 << 19);
1042         }
1043
1044       return NULL;
1045     }
1046
1047   input_line_pointer++;
1048
1049   /* Parse the register list until a terminator (closing curly brace or
1050      new-line) is found.  */
1051   for (;;)
1052     {
1053       if (register_name (&exp))
1054         {
1055           int i;
1056
1057           /* Locate the given register in the list, and if it is there,
1058              insert the corresponding bit into the instruction.  */
1059           for (i = 0; i < 32; i++)
1060             {
1061               if (regs[i] == exp.X_add_number)
1062                 {
1063                   *insn |= (1 << i);
1064                   break;
1065                 }
1066             }
1067
1068           if (i == 32)
1069             return _("illegal register included in list");
1070         }
1071       else if (system_register_name (&exp, TRUE, TRUE))
1072         {
1073           if (regs == type1_regs)
1074             {
1075               return _("system registers cannot be included in list");
1076             }
1077           else if (exp.X_add_number == 5)
1078             {
1079               if (regs == type2_regs)
1080                 return _("PSW cannot be included in list");
1081               else
1082                 *insn |= 0x8;
1083             }
1084           else if (exp.X_add_number < 4)
1085             *insn |= 0x80000;
1086           else
1087             return _("High value system registers cannot be included in list");
1088         }
1089       else if (*input_line_pointer == '}')
1090         {
1091           input_line_pointer++;
1092           break;
1093         }
1094       else if (*input_line_pointer == ',')
1095         {
1096           input_line_pointer++;
1097           continue;
1098         }
1099       else if (*input_line_pointer == '-')
1100         {
1101           /* We have encountered a range of registers: rX - rY.  */
1102           int j;
1103           expressionS exp2;
1104
1105           /* Skip the dash.  */
1106           ++input_line_pointer;
1107
1108           /* Get the second register in the range.  */
1109           if (! register_name (&exp2))
1110             {
1111               return _("second register should follow dash in register list");
1112               exp2.X_add_number = exp.X_add_number;
1113             }
1114
1115           /* Add the rest of the registers in the range.  */
1116           for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
1117             {
1118               int i;
1119
1120               /* Locate the given register in the list, and if it is there,
1121                  insert the corresponding bit into the instruction.  */
1122               for (i = 0; i < 32; i++)
1123                 {
1124                   if (regs[i] == j)
1125                     {
1126                       *insn |= (1 << i);
1127                       break;
1128                     }
1129                 }
1130
1131               if (i == 32)
1132                 return _("illegal register included in list");
1133             }
1134         }
1135       else
1136         break;
1137
1138       skip_white_space ();
1139     }
1140
1141   return NULL;
1142 }
1143
1144 const char *md_shortopts = "m:";
1145
1146 struct option md_longopts[] = {
1147   {NULL, no_argument, NULL, 0}
1148 };
1149
1150 size_t md_longopts_size = sizeof (md_longopts);
1151
1152 void
1153 md_show_usage (stream)
1154      FILE *stream;
1155 {
1156   fprintf (stream, _(" V850 options:\n"));
1157   fprintf (stream, _("  -mwarn-signed-overflow    Warn if signed immediate values overflow\n"));
1158   fprintf (stream, _("  -mwarn-unsigned-overflow  Warn if unsigned immediate values overflow\n"));
1159   fprintf (stream, _("  -mv850                    The code is targeted at the v850\n"));
1160   fprintf (stream, _("  -mv850e                   The code is targeted at the v850e\n"));
1161   fprintf (stream, _("  -mv850e1                  The code is targeted at the v850e1\n"));
1162   fprintf (stream, _("  -mv850any                 The code is generic, despite any processor specific instructions\n"));
1163   fprintf (stream, _("  -mrelax                   Enable relaxation\n"));
1164 }
1165
1166 int
1167 md_parse_option (c, arg)
1168      int c;
1169      char *arg;
1170 {
1171   if (c != 'm')
1172     {
1173       if (c != 'a')
1174         /* xgettext:c-format  */
1175         fprintf (stderr, _("unknown command line option: -%c%s\n"), c, arg);
1176       return 0;
1177     }
1178
1179   if (strcmp (arg, "warn-signed-overflow") == 0)
1180     {
1181       warn_signed_overflows = TRUE;
1182     }
1183   else if (strcmp (arg, "warn-unsigned-overflow") == 0)
1184     {
1185       warn_unsigned_overflows = TRUE;
1186     }
1187   else if (strcmp (arg, "v850") == 0)
1188     {
1189       machine = 0;
1190       processor_mask = PROCESSOR_V850;
1191     }
1192   else if (strcmp (arg, "v850e") == 0)
1193     {
1194       machine = bfd_mach_v850e;
1195       processor_mask = PROCESSOR_V850E;
1196     }
1197   else if (strcmp (arg, "v850e1") == 0)
1198     {
1199       machine = bfd_mach_v850e1;
1200       processor_mask = PROCESSOR_V850E1;
1201     }
1202   else if (strcmp (arg, "v850any") == 0)
1203     {
1204       /* Tell the world that this is for any v850 chip.  */
1205       machine = 0;
1206
1207       /* But support instructions for the extended versions.  */
1208       processor_mask = PROCESSOR_V850E;
1209       processor_mask |= PROCESSOR_V850E1;
1210     }
1211   else if (strcmp (arg, "relax") == 0)
1212     v850_relax = 1;
1213   else
1214     {
1215       /* xgettext:c-format  */
1216       fprintf (stderr, _("unknown command line option: -%c%s\n"), c, arg);
1217       return 0;
1218     }
1219
1220   return 1;
1221 }
1222
1223 symbolS *
1224 md_undefined_symbol (name)
1225      char *name ATTRIBUTE_UNUSED;
1226 {
1227   return 0;
1228 }
1229
1230 char *
1231 md_atof (type, litp, sizep)
1232      int type;
1233      char *litp;
1234      int *sizep;
1235 {
1236   int prec;
1237   LITTLENUM_TYPE words[4];
1238   char *t;
1239   int i;
1240
1241   switch (type)
1242     {
1243     case 'f':
1244       prec = 2;
1245       break;
1246
1247     case 'd':
1248       prec = 4;
1249       break;
1250
1251     default:
1252       *sizep = 0;
1253       return _("bad call to md_atof");
1254     }
1255
1256   t = atof_ieee (input_line_pointer, type, words);
1257   if (t)
1258     input_line_pointer = t;
1259
1260   *sizep = prec * 2;
1261
1262   for (i = prec - 1; i >= 0; i--)
1263     {
1264       md_number_to_chars (litp, (valueT) words[i], 2);
1265       litp += 2;
1266     }
1267
1268   return NULL;
1269 }
1270
1271 /* Very gross.  */
1272
1273 void
1274 md_convert_frag (abfd, sec, fragP)
1275      bfd *abfd ATTRIBUTE_UNUSED;
1276      asection *sec;
1277      fragS *fragP;
1278 {
1279   subseg_change (sec, 0);
1280
1281   /* In range conditional or unconditional branch.  */
1282   if (fragP->fr_subtype == 0 || fragP->fr_subtype == 2)
1283     {
1284       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
1285                fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
1286       fragP->fr_fix += 2;
1287     }
1288   /* Out of range conditional branch.  Emit a branch around a jump.  */
1289   else if (fragP->fr_subtype == 1)
1290     {
1291       unsigned char *buffer =
1292         (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1293
1294       /* Reverse the condition of the first branch.  */
1295       buffer[0] ^= 0x08;
1296       /* Mask off all the displacement bits.  */
1297       buffer[0] &= 0x8f;
1298       buffer[1] &= 0x07;
1299       /* Now set the displacement bits so that we branch
1300          around the unconditional branch.  */
1301       buffer[0] |= 0x30;
1302
1303       /* Now create the unconditional branch + fixup to the final
1304          target.  */
1305       md_number_to_chars (buffer + 2, 0x00000780, 4);
1306       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
1307                fragP->fr_offset, 1, BFD_RELOC_UNUSED +
1308                (int) fragP->fr_opcode + 1);
1309       fragP->fr_fix += 6;
1310     }
1311   /* Out of range unconditional branch.  Emit a jump.  */
1312   else if (fragP->fr_subtype == 3)
1313     {
1314       md_number_to_chars (fragP->fr_fix + fragP->fr_literal, 0x00000780, 4);
1315       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
1316                fragP->fr_offset, 1, BFD_RELOC_UNUSED +
1317                (int) fragP->fr_opcode + 1);
1318       fragP->fr_fix += 4;
1319     }
1320   else
1321     abort ();
1322 }
1323
1324 valueT
1325 md_section_align (seg, addr)
1326      asection *seg;
1327      valueT addr;
1328 {
1329   int align = bfd_get_section_alignment (stdoutput, seg);
1330   return ((addr + (1 << align) - 1) & (-1 << align));
1331 }
1332
1333 void
1334 md_begin ()
1335 {
1336   char *prev_name = "";
1337   const struct v850_opcode *op;
1338
1339   if (strncmp (TARGET_CPU, "v850e1", 6) == 0)
1340     {
1341       if (machine == -1)
1342         machine = bfd_mach_v850e1;
1343
1344       if (processor_mask == -1)
1345         processor_mask = PROCESSOR_V850E1;
1346     }
1347   else if (strncmp (TARGET_CPU, "v850e", 5) == 0)
1348     {
1349       if (machine == -1)
1350         machine = bfd_mach_v850e;
1351
1352       if (processor_mask == -1)
1353         processor_mask = PROCESSOR_V850E;
1354     }
1355   else if (strncmp (TARGET_CPU, "v850", 4) == 0)
1356     {
1357       if (machine == -1)
1358         machine = 0;
1359
1360       if (processor_mask == -1)
1361         processor_mask = PROCESSOR_V850;
1362     }
1363   else
1364     /* xgettext:c-format  */
1365     as_bad (_("Unable to determine default target processor from string: %s"),
1366             TARGET_CPU);
1367
1368   v850_hash = hash_new ();
1369
1370   /* Insert unique names into hash table.  The V850 instruction set
1371      has many identical opcode names that have different opcodes based
1372      on the operands.  This hash table then provides a quick index to
1373      the first opcode with a particular name in the opcode table.  */
1374   op = v850_opcodes;
1375   while (op->name)
1376     {
1377       if (strcmp (prev_name, op->name))
1378         {
1379           prev_name = (char *) op->name;
1380           hash_insert (v850_hash, op->name, (char *) op);
1381         }
1382       op++;
1383     }
1384
1385   v850_seg_table[BSS_SECTION].s = bss_section;
1386   bfd_set_arch_mach (stdoutput, TARGET_ARCH, machine);
1387 }
1388
1389 static bfd_reloc_code_real_type handle_ctoff
1390   PARAMS ((const struct v850_operand *));
1391
1392 static bfd_reloc_code_real_type
1393 handle_ctoff (operand)
1394      const struct v850_operand *operand;
1395 {
1396   if (operand == NULL)
1397     return BFD_RELOC_V850_CALLT_16_16_OFFSET;
1398
1399   if (operand->bits != 6
1400       || operand->shift != 0)
1401     {
1402       as_bad (_("ctoff() relocation used on an instruction which does not support it"));
1403       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
1404     }
1405
1406   return BFD_RELOC_V850_CALLT_6_7_OFFSET;
1407 }
1408
1409 static bfd_reloc_code_real_type handle_sdaoff
1410   PARAMS ((const struct v850_operand *));
1411
1412 static bfd_reloc_code_real_type
1413 handle_sdaoff (operand)
1414      const struct v850_operand *operand;
1415 {
1416   if (operand == NULL)
1417     return BFD_RELOC_V850_SDA_16_16_OFFSET;
1418
1419   if (operand->bits == 15 && operand->shift == 17)
1420     return BFD_RELOC_V850_SDA_15_16_OFFSET;
1421
1422   if (operand->bits == -1)
1423     return BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET;
1424
1425   if (operand->bits != 16
1426       || operand->shift != 16)
1427     {
1428       as_bad (_("sdaoff() relocation used on an instruction which does not support it"));
1429       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
1430     }
1431
1432   return BFD_RELOC_V850_SDA_16_16_OFFSET;
1433 }
1434
1435 static bfd_reloc_code_real_type handle_zdaoff
1436   PARAMS ((const struct v850_operand *));
1437
1438 static bfd_reloc_code_real_type
1439 handle_zdaoff (operand)
1440      const struct v850_operand *operand;
1441 {
1442   if (operand == NULL)
1443     return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1444
1445   if (operand->bits == 15 && operand->shift == 17)
1446     return BFD_RELOC_V850_ZDA_15_16_OFFSET;
1447
1448   if (operand->bits == -1)
1449     return BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET;
1450
1451   if (operand->bits != 16
1452       || operand->shift != 16)
1453     {
1454       as_bad (_("zdaoff() relocation used on an instruction which does not support it"));
1455       /* Used to indicate an error condition.  */
1456       return BFD_RELOC_64;
1457     }
1458
1459   return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1460 }
1461
1462 static bfd_reloc_code_real_type handle_tdaoff
1463   PARAMS ((const struct v850_operand *));
1464
1465 static bfd_reloc_code_real_type
1466 handle_tdaoff (operand)
1467      const struct v850_operand *operand;
1468 {
1469   if (operand == NULL)
1470     /* Data item, not an instruction.  */
1471     return BFD_RELOC_V850_TDA_7_7_OFFSET;
1472
1473   if (operand->bits == 6 && operand->shift == 1)
1474     /* sld.w/sst.w, operand: D8_6  */
1475     return BFD_RELOC_V850_TDA_6_8_OFFSET;
1476
1477   if (operand->bits == 4 && operand->insert != NULL)
1478     /* sld.hu, operand: D5-4  */
1479     return BFD_RELOC_V850_TDA_4_5_OFFSET;
1480
1481   if (operand->bits == 4 && operand->insert == NULL)
1482     /* sld.bu, operand: D4   */
1483     return BFD_RELOC_V850_TDA_4_4_OFFSET;
1484
1485   if (operand->bits == 16 && operand->shift == 16)
1486     /* set1 & chums, operands: D16  */
1487     return BFD_RELOC_V850_TDA_16_16_OFFSET;
1488
1489   if (operand->bits != 7)
1490     {
1491       as_bad (_("tdaoff() relocation used on an instruction which does not support it"));
1492       /* Used to indicate an error condition.  */
1493       return BFD_RELOC_64;
1494     }
1495
1496   return  operand->insert != NULL
1497     ? BFD_RELOC_V850_TDA_7_8_OFFSET     /* sld.h/sst.h, operand: D8_7  */
1498     : BFD_RELOC_V850_TDA_7_7_OFFSET;    /* sld.b/sst.b, operand: D7    */
1499 }
1500
1501 /* Warning: The code in this function relies upon the definitions
1502    in the v850_operands[] array (defined in opcodes/v850-opc.c)
1503    matching the hard coded values contained herein.  */
1504
1505 static bfd_reloc_code_real_type v850_reloc_prefix
1506   PARAMS ((const struct v850_operand *));
1507
1508 static bfd_reloc_code_real_type
1509 v850_reloc_prefix (operand)
1510      const struct v850_operand *operand;
1511 {
1512   bfd_boolean paren_skipped = FALSE;
1513
1514   /* Skip leading opening parenthesis.  */
1515   if (*input_line_pointer == '(')
1516     {
1517       ++input_line_pointer;
1518       paren_skipped = TRUE;
1519     }
1520
1521 #define CHECK_(name, reloc)                                             \
1522   if (strncmp (input_line_pointer, name "(", strlen (name) + 1) == 0)   \
1523     {                                                                   \
1524       input_line_pointer += strlen (name);                              \
1525       return reloc;                                                     \
1526     }
1527
1528   CHECK_ ("hi0",    BFD_RELOC_HI16         );
1529   CHECK_ ("hi",     BFD_RELOC_HI16_S       );
1530   CHECK_ ("lo",     BFD_RELOC_LO16         );
1531   CHECK_ ("sdaoff", handle_sdaoff (operand));
1532   CHECK_ ("zdaoff", handle_zdaoff (operand));
1533   CHECK_ ("tdaoff", handle_tdaoff (operand));
1534   CHECK_ ("hilo",   BFD_RELOC_32           );
1535   CHECK_ ("ctoff",  handle_ctoff (operand) );
1536
1537   /* Restore skipped parenthesis.  */
1538   if (paren_skipped)
1539     --input_line_pointer;
1540
1541   return BFD_RELOC_UNUSED;
1542 }
1543
1544 /* Insert an operand value into an instruction.  */
1545
1546 static unsigned long v850_insert_operand
1547   PARAMS ((unsigned long, const struct v850_operand *, offsetT, char *,
1548            unsigned int, char *));
1549
1550 static unsigned long
1551 v850_insert_operand (insn, operand, val, file, line, str)
1552      unsigned long insn;
1553      const struct v850_operand *operand;
1554      offsetT val;
1555      char *file;
1556      unsigned int line;
1557      char *str;
1558 {
1559   if (operand->insert)
1560     {
1561       const char *message = NULL;
1562
1563       insn = operand->insert (insn, val, &message);
1564       if (message != NULL)
1565         {
1566           if ((operand->flags & V850_OPERAND_SIGNED)
1567               && ! warn_signed_overflows
1568               && strstr (message, "out of range") != NULL)
1569             {
1570               /* Skip warning...  */
1571             }
1572           else if ((operand->flags & V850_OPERAND_SIGNED) == 0
1573                    && ! warn_unsigned_overflows
1574                    && strstr (message, "out of range") != NULL)
1575             {
1576               /* Skip warning...  */
1577             }
1578           else if (str)
1579             {
1580               if (file == (char *) NULL)
1581                 as_warn ("%s: %s", str, message);
1582               else
1583                 as_warn_where (file, line, "%s: %s", str, message);
1584             }
1585           else
1586             {
1587               if (file == (char *) NULL)
1588                 as_warn (message);
1589               else
1590                 as_warn_where (file, line, message);
1591             }
1592         }
1593     }
1594   else
1595     {
1596       if (operand->bits != 32)
1597         {
1598           long min, max;
1599
1600           if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1601             {
1602               if (! warn_signed_overflows)
1603                 max = (1 << operand->bits) - 1;
1604               else
1605                 max = (1 << (operand->bits - 1)) - 1;
1606
1607               min = -(1 << (operand->bits - 1));
1608             }
1609           else
1610             {
1611               max = (1 << operand->bits) - 1;
1612
1613               if (! warn_unsigned_overflows)
1614                 min = -(1 << (operand->bits - 1));
1615               else
1616                 min = 0;
1617             }
1618
1619           if (val < (offsetT) min || val > (offsetT) max)
1620             {
1621               char buf [128];
1622
1623               /* Restore min and mix to expected values for decimal ranges.  */
1624               if ((operand->flags & V850_OPERAND_SIGNED)
1625                   && ! warn_signed_overflows)
1626                 max = (1 << (operand->bits - 1)) - 1;
1627
1628               if (! (operand->flags & V850_OPERAND_SIGNED)
1629                   && ! warn_unsigned_overflows)
1630                 min = 0;
1631
1632               if (str)
1633                 sprintf (buf, "%s: ", str);
1634               else
1635                 buf[0] = 0;
1636               strcat (buf, _("operand"));
1637
1638               as_bad_value_out_of_range (buf, val, (offsetT) min, (offsetT) max, file, line);
1639             }
1640         }
1641
1642       insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1643     }
1644
1645   return insn;
1646 }
1647 \f
1648 static char copy_of_instruction[128];
1649
1650 void
1651 md_assemble (str)
1652      char *str;
1653 {
1654   char *s;
1655   char *start_of_operands;
1656   struct v850_opcode *opcode;
1657   struct v850_opcode *next_opcode;
1658   const unsigned char *opindex_ptr;
1659   int next_opindex;
1660   int relaxable = 0;
1661   unsigned long insn;
1662   unsigned long insn_size;
1663   char *f;
1664   int i;
1665   int match;
1666   bfd_boolean extra_data_after_insn = FALSE;
1667   unsigned extra_data_len = 0;
1668   unsigned long extra_data = 0;
1669   char *saved_input_line_pointer;
1670
1671   strncpy (copy_of_instruction, str, sizeof (copy_of_instruction) - 1);
1672
1673   /* Get the opcode.  */
1674   for (s = str; *s != '\0' && ! ISSPACE (*s); s++)
1675     continue;
1676
1677   if (*s != '\0')
1678     *s++ = '\0';
1679
1680   /* Find the first opcode with the proper name.  */
1681   opcode = (struct v850_opcode *) hash_find (v850_hash, str);
1682   if (opcode == NULL)
1683     {
1684       /* xgettext:c-format  */
1685       as_bad (_("Unrecognized opcode: `%s'"), str);
1686       ignore_rest_of_line ();
1687       return;
1688     }
1689
1690   str = s;
1691   while (ISSPACE (*str))
1692     ++str;
1693
1694   start_of_operands = str;
1695
1696   saved_input_line_pointer = input_line_pointer;
1697
1698   for (;;)
1699     {
1700       const char *errmsg = NULL;
1701
1702       match = 0;
1703
1704       if ((opcode->processors & processor_mask) == 0)
1705         {
1706           errmsg = _("Target processor does not support this instruction.");
1707           goto error;
1708         }
1709
1710       relaxable = 0;
1711       fc = 0;
1712       next_opindex = 0;
1713       insn = opcode->opcode;
1714       extra_data_after_insn = FALSE;
1715
1716       input_line_pointer = str = start_of_operands;
1717
1718       for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1719         {
1720           const struct v850_operand *operand;
1721           char *hold;
1722           expressionS ex;
1723           bfd_reloc_code_real_type reloc;
1724
1725           if (next_opindex == 0)
1726             {
1727               operand = &v850_operands[*opindex_ptr];
1728             }
1729           else
1730             {
1731               operand = &v850_operands[next_opindex];
1732               next_opindex = 0;
1733             }
1734
1735           errmsg = NULL;
1736
1737           while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1738             ++str;
1739
1740           if (operand->flags & V850_OPERAND_RELAX)
1741             relaxable = 1;
1742
1743           /* Gather the operand.  */
1744           hold = input_line_pointer;
1745           input_line_pointer = str;
1746
1747           /* lo(), hi(), hi0(), etc...  */
1748           if ((reloc = v850_reloc_prefix (operand)) != BFD_RELOC_UNUSED)
1749             {
1750               /* This is a fake reloc, used to indicate an error condition.  */
1751               if (reloc == BFD_RELOC_64)
1752                 {
1753                   match = 1;
1754                   goto error;
1755                 }
1756
1757               expression (&ex);
1758
1759               if (ex.X_op == O_constant)
1760                 {
1761                   switch (reloc)
1762                     {
1763                     case BFD_RELOC_V850_ZDA_16_16_OFFSET:
1764                       /* To cope with "not1 7, zdaoff(0xfffff006)[r0]"
1765                          and the like.  */
1766                       /* Fall through.  */
1767
1768                     case BFD_RELOC_LO16:
1769                       {
1770                         /* Truncate, then sign extend the value.  */
1771                         ex.X_add_number = SEXT16 (ex.X_add_number);
1772                         break;
1773                       }
1774
1775                     case BFD_RELOC_HI16:
1776                       {
1777                         /* Truncate, then sign extend the value.  */
1778                         ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
1779                         break;
1780                       }
1781
1782                     case BFD_RELOC_HI16_S:
1783                       {
1784                         /* Truncate, then sign extend the value.  */
1785                         int temp = (ex.X_add_number >> 16) & 0xffff;
1786
1787                         temp += (ex.X_add_number >> 15) & 1;
1788
1789                         ex.X_add_number = SEXT16 (temp);
1790                         break;
1791                       }
1792
1793                     case BFD_RELOC_32:
1794                       if ((operand->flags & V850E_IMMEDIATE32) == 0)
1795                         {
1796                           errmsg = _("immediate operand is too large");
1797                           goto error;
1798                         }
1799
1800                       extra_data_after_insn = TRUE;
1801                       extra_data_len        = 4;
1802                       extra_data            = 0;
1803                       break;
1804
1805                     default:
1806                       fprintf (stderr, "reloc: %d\n", reloc);
1807                       as_bad (_("AAARG -> unhandled constant reloc"));
1808                       break;
1809                     }
1810
1811                   if (fc > MAX_INSN_FIXUPS)
1812                     as_fatal (_("too many fixups"));
1813
1814                   fixups[fc].exp     = ex;
1815                   fixups[fc].opindex = *opindex_ptr;
1816                   fixups[fc].reloc   = reloc;
1817                   fc++;
1818                 }
1819               else
1820                 {
1821                   if (reloc == BFD_RELOC_32)
1822                     {
1823                       if ((operand->flags & V850E_IMMEDIATE32) == 0)
1824                         {
1825                           errmsg = _("immediate operand is too large");
1826                           goto error;
1827                         }
1828
1829                       extra_data_after_insn = TRUE;
1830                       extra_data_len        = 4;
1831                       extra_data            = ex.X_add_number;
1832                     }
1833
1834                   if (fc > MAX_INSN_FIXUPS)
1835                     as_fatal (_("too many fixups"));
1836
1837                   fixups[fc].exp     = ex;
1838                   fixups[fc].opindex = *opindex_ptr;
1839                   fixups[fc].reloc   = reloc;
1840                   fc++;
1841                 }
1842             }
1843           else
1844             {
1845               errmsg = NULL;
1846
1847               if ((operand->flags & V850_OPERAND_REG) != 0)
1848                 {
1849                   if (!register_name (&ex))
1850                     {
1851                       errmsg = _("invalid register name");
1852                     }
1853                   else if ((operand->flags & V850_NOT_R0)
1854                            && ex.X_add_number == 0)
1855                     {
1856                       errmsg = _("register r0 cannot be used here");
1857
1858                       /* Force an error message to be generated by
1859                          skipping over any following potential matches
1860                          for this opcode.  */
1861                       opcode += 3;
1862                     }
1863                 }
1864               else if ((operand->flags & V850_OPERAND_SRG) != 0)
1865                 {
1866                   if (!system_register_name (&ex, TRUE, FALSE))
1867                     {
1868                       errmsg = _("invalid system register name");
1869                     }
1870                 }
1871               else if ((operand->flags & V850_OPERAND_EP) != 0)
1872                 {
1873                   char *start = input_line_pointer;
1874                   char c = get_symbol_end ();
1875
1876                   if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
1877                     {
1878                       /* Put things back the way we found them.  */
1879                       *input_line_pointer = c;
1880                       input_line_pointer = start;
1881                       errmsg = _("expected EP register");
1882                       goto error;
1883                     }
1884
1885                   *input_line_pointer = c;
1886                   str = input_line_pointer;
1887                   input_line_pointer = hold;
1888
1889                   while (*str == ' ' || *str == ','
1890                          || *str == '[' || *str == ']')
1891                     ++str;
1892                   continue;
1893                 }
1894               else if ((operand->flags & V850_OPERAND_CC) != 0)
1895                 {
1896                   if (!cc_name (&ex))
1897                     {
1898                       errmsg = _("invalid condition code name");
1899                     }
1900                 }
1901               else if (operand->flags & V850E_PUSH_POP)
1902                 {
1903                   errmsg = parse_register_list (&insn, operand);
1904
1905                   /* The parse_register_list() function has already done
1906                      everything, so fake a dummy expression.  */
1907                   ex.X_op         = O_constant;
1908                   ex.X_add_number = 0;
1909                 }
1910               else if (operand->flags & V850E_IMMEDIATE16)
1911                 {
1912                   expression (&ex);
1913
1914                   if (ex.X_op != O_constant)
1915                     errmsg = _("constant expression expected");
1916                   else if (ex.X_add_number & 0xffff0000)
1917                     {
1918                       if (ex.X_add_number & 0xffff)
1919                         errmsg = _("constant too big to fit into instruction");
1920                       else if ((insn & 0x001fffc0) == 0x00130780)
1921                         ex.X_add_number >>= 16;
1922                       else
1923                         errmsg = _("constant too big to fit into instruction");
1924                     }
1925
1926                   extra_data_after_insn = TRUE;
1927                   extra_data_len        = 2;
1928                   extra_data            = ex.X_add_number;
1929                   ex.X_add_number       = 0;
1930                 }
1931               else if (operand->flags & V850E_IMMEDIATE32)
1932                 {
1933                   expression (&ex);
1934
1935                   if (ex.X_op != O_constant)
1936                     errmsg = _("constant expression expected");
1937
1938                   extra_data_after_insn = TRUE;
1939                   extra_data_len        = 4;
1940                   extra_data            = ex.X_add_number;
1941                   ex.X_add_number       = 0;
1942                 }
1943               else if (register_name (&ex)
1944                        && (operand->flags & V850_OPERAND_REG) == 0)
1945                 {
1946                   char c;
1947                   int exists = 0;
1948
1949                   /* It is possible that an alias has been defined that
1950                      matches a register name.  For example the code may
1951                      include a ".set ZERO, 0" directive, which matches
1952                      the register name "zero".  Attempt to reparse the
1953                      field as an expression, and only complain if we
1954                      cannot generate a constant.  */
1955
1956                   input_line_pointer = str;
1957
1958                   c = get_symbol_end ();
1959
1960                   if (symbol_find (str) != NULL)
1961                     exists = 1;
1962
1963                   *input_line_pointer = c;
1964                   input_line_pointer = str;
1965
1966                   expression (&ex);
1967
1968                   if (ex.X_op != O_constant)
1969                     {
1970                       /* If this register is actually occurring too early on
1971                          the parsing of the instruction, (because another
1972                          field is missing) then report this.  */
1973                       if (opindex_ptr[1] != 0
1974                           && (v850_operands[opindex_ptr[1]].flags
1975                               & V850_OPERAND_REG))
1976                         errmsg = _("syntax error: value is missing before the register name");
1977                       else
1978                         errmsg = _("syntax error: register not expected");
1979
1980                       /* If we created a symbol in the process of this
1981                          test then delete it now, so that it will not
1982                          be output with the real symbols...  */
1983                       if (exists == 0
1984                           && ex.X_op == O_symbol)
1985                         symbol_remove (ex.X_add_symbol,
1986                                        &symbol_rootP, &symbol_lastP);
1987                     }
1988                 }
1989               else if (system_register_name (&ex, FALSE, FALSE)
1990                        && (operand->flags & V850_OPERAND_SRG) == 0)
1991                 {
1992                   errmsg = _("syntax error: system register not expected");
1993                 }
1994               else if (cc_name (&ex)
1995                        && (operand->flags & V850_OPERAND_CC) == 0)
1996                 {
1997                   errmsg = _("syntax error: condition code not expected");
1998                 }
1999               else
2000                 {
2001                   expression (&ex);
2002                   /* Special case:
2003                      If we are assembling a MOV instruction and the immediate
2004                      value does not fit into the bits available then create a
2005                      fake error so that the next MOV instruction will be
2006                      selected.  This one has a 32 bit immediate field.  */
2007
2008                   if (((insn & 0x07e0) == 0x0200)
2009                       && operand->bits == 5 /* Do not match the CALLT instruction.  */
2010                       && ex.X_op == O_constant
2011                       && (ex.X_add_number < (-(1 << (operand->bits - 1)))
2012                           || ex.X_add_number > ((1 << (operand->bits - 1)) - 1)))
2013                     errmsg = _("immediate operand is too large");
2014                 }
2015
2016               if (errmsg)
2017                 goto error;
2018
2019 #if 0
2020               fprintf (stderr,
2021                        " insn: %x, operand %d, op: %d, add_number: %d\n",
2022                        insn, opindex_ptr - opcode->operands,
2023                        ex.X_op, ex.X_add_number);
2024 #endif
2025
2026               switch (ex.X_op)
2027                 {
2028                 case O_illegal:
2029                   errmsg = _("illegal operand");
2030                   goto error;
2031                 case O_absent:
2032                   errmsg = _("missing operand");
2033                   goto error;
2034                 case O_register:
2035                   if ((operand->flags
2036                        & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
2037                     {
2038                       errmsg = _("invalid operand");
2039                       goto error;
2040                     }
2041                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
2042                                               (char *) NULL, 0,
2043                                               copy_of_instruction);
2044                   break;
2045
2046                 case O_constant:
2047                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
2048                                               (char *) NULL, 0,
2049                                               copy_of_instruction);
2050                   break;
2051
2052                 default:
2053                   /* We need to generate a fixup for this expression.  */
2054                   if (fc >= MAX_INSN_FIXUPS)
2055                     as_fatal (_("too many fixups"));
2056
2057                   fixups[fc].exp     = ex;
2058                   fixups[fc].opindex = *opindex_ptr;
2059                   fixups[fc].reloc   = BFD_RELOC_UNUSED;
2060                   ++fc;
2061                   break;
2062                 }
2063             }
2064
2065           str = input_line_pointer;
2066           input_line_pointer = hold;
2067
2068           while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
2069                  || *str == ')')
2070             ++str;
2071         }
2072       match = 1;
2073
2074     error:
2075       if (match == 0)
2076         {
2077           next_opcode = opcode + 1;
2078           if (next_opcode->name != NULL
2079               && strcmp (next_opcode->name, opcode->name) == 0)
2080             {
2081               opcode = next_opcode;
2082
2083               /* Skip versions that are not supported by the target
2084                  processor.  */
2085               if ((opcode->processors & processor_mask) == 0)
2086                 goto error;
2087
2088               continue;
2089             }
2090
2091           as_bad ("%s: %s", copy_of_instruction, errmsg);
2092
2093           if (*input_line_pointer == ']')
2094             ++input_line_pointer;
2095
2096           ignore_rest_of_line ();
2097           input_line_pointer = saved_input_line_pointer;
2098           return;
2099         }
2100       break;
2101     }
2102
2103   while (ISSPACE (*str))
2104     ++str;
2105
2106   if (*str != '\0')
2107     /* xgettext:c-format  */
2108     as_bad (_("junk at end of line: `%s'"), str);
2109
2110   input_line_pointer = str;
2111
2112   /* Tie dwarf2 debug info to the address at the start of the insn.
2113      We can't do this after the insn has been output as the current
2114      frag may have been closed off.  eg. by frag_var.  */
2115   dwarf2_emit_insn (0);
2116
2117   /* Write out the instruction.  */
2118
2119   if (relaxable && fc > 0)
2120     {
2121       insn_size = 2;
2122       fc = 0;
2123
2124       if (!strcmp (opcode->name, "br"))
2125         {
2126           f = frag_var (rs_machine_dependent, 4, 2, 2,
2127                         fixups[0].exp.X_add_symbol,
2128                         fixups[0].exp.X_add_number,
2129                         (char *) fixups[0].opindex);
2130           md_number_to_chars (f, insn, insn_size);
2131           md_number_to_chars (f + 2, 0, 2);
2132         }
2133       else
2134         {
2135           f = frag_var (rs_machine_dependent, 6, 4, 0,
2136                         fixups[0].exp.X_add_symbol,
2137                         fixups[0].exp.X_add_number,
2138                         (char *) fixups[0].opindex);
2139           md_number_to_chars (f, insn, insn_size);
2140           md_number_to_chars (f + 2, 0, 4);
2141         }
2142     }
2143   else
2144     {
2145       /* Four byte insns have an opcode with the two high bits on.  */
2146       if ((insn & 0x0600) == 0x0600)
2147         insn_size = 4;
2148       else
2149         insn_size = 2;
2150
2151       /* Special case: 32 bit MOV.  */
2152       if ((insn & 0xffe0) == 0x0620)
2153         insn_size = 2;
2154
2155       f = frag_more (insn_size);
2156       md_number_to_chars (f, insn, insn_size);
2157
2158       if (extra_data_after_insn)
2159         {
2160           f = frag_more (extra_data_len);
2161           md_number_to_chars (f, extra_data, extra_data_len);
2162
2163           extra_data_after_insn = FALSE;
2164         }
2165     }
2166
2167   /* Create any fixups.  At this point we do not use a
2168      bfd_reloc_code_real_type, but instead just use the
2169      BFD_RELOC_UNUSED plus the operand index.  This lets us easily
2170      handle fixups for any operand type, although that is admittedly
2171      not a very exciting feature.  We pick a BFD reloc type in
2172      md_apply_fix3.  */
2173   for (i = 0; i < fc; i++)
2174     {
2175       const struct v850_operand *operand;
2176       bfd_reloc_code_real_type reloc;
2177
2178       operand = &v850_operands[fixups[i].opindex];
2179
2180       reloc = fixups[i].reloc;
2181
2182       if (reloc != BFD_RELOC_UNUSED)
2183         {
2184           reloc_howto_type *reloc_howto =
2185             bfd_reloc_type_lookup (stdoutput, reloc);
2186           int size;
2187           int address;
2188           fixS *fixP;
2189
2190           if (!reloc_howto)
2191             abort ();
2192
2193           size = bfd_get_reloc_size (reloc_howto);
2194
2195           /* XXX This will abort on an R_V850_8 reloc -
2196              is this reloc actually used?  */
2197           if (size != 2 && size != 4)
2198             abort ();
2199
2200           address = (f - frag_now->fr_literal) + insn_size - size;
2201
2202           if (reloc == BFD_RELOC_32)
2203             address += 2;
2204
2205           fixP = fix_new_exp (frag_now, address, size,
2206                               &fixups[i].exp,
2207                               reloc_howto->pc_relative,
2208                               reloc);
2209
2210           switch (reloc)
2211             {
2212             case BFD_RELOC_LO16:
2213             case BFD_RELOC_HI16:
2214             case BFD_RELOC_HI16_S:
2215               fixP->fx_no_overflow = 1;
2216               break;
2217             default:
2218               break;
2219             }
2220         }
2221       else
2222         {
2223           fix_new_exp (frag_now,
2224                        f - frag_now->fr_literal, 4,
2225                        & fixups[i].exp,
2226                        (operand->flags & V850_OPERAND_DISP) != 0,
2227                        (bfd_reloc_code_real_type) (fixups[i].opindex
2228                                                    + (int) BFD_RELOC_UNUSED));
2229         }
2230     }
2231
2232   input_line_pointer = saved_input_line_pointer;
2233 }
2234
2235 /* If while processing a fixup, a reloc really needs to be created
2236    then it is done here.  */
2237
2238 arelent *
2239 tc_gen_reloc (seg, fixp)
2240      asection *seg ATTRIBUTE_UNUSED;
2241      fixS *fixp;
2242 {
2243   arelent *reloc;
2244
2245   reloc               = (arelent *) xmalloc (sizeof (arelent));
2246   reloc->sym_ptr_ptr  = (asymbol **) xmalloc (sizeof (asymbol *));
2247   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2248   reloc->address      = fixp->fx_frag->fr_address + fixp->fx_where;
2249
2250   if (   fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2251       || fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2252       || fixp->fx_r_type == BFD_RELOC_V850_LONGCALL
2253       || fixp->fx_r_type == BFD_RELOC_V850_LONGJUMP
2254       || fixp->fx_r_type == BFD_RELOC_V850_ALIGN)
2255     reloc->addend = fixp->fx_offset;
2256   else
2257     {
2258       if (fixp->fx_r_type == BFD_RELOC_32
2259           && fixp->fx_pcrel)
2260         fixp->fx_r_type = BFD_RELOC_32_PCREL;
2261
2262       reloc->addend = fixp->fx_addnumber;
2263     }
2264
2265   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2266
2267   if (reloc->howto == (reloc_howto_type *) NULL)
2268     {
2269       as_bad_where (fixp->fx_file, fixp->fx_line,
2270                     /* xgettext:c-format  */
2271                     _("reloc %d not supported by object file format"),
2272                     (int) fixp->fx_r_type);
2273
2274       xfree (reloc);
2275
2276       return NULL;
2277     }
2278
2279   return reloc;
2280 }
2281
2282 void
2283 v850_handle_align (frag)
2284      fragS * frag;
2285 {
2286   if (v850_relax
2287       && frag->fr_type == rs_align
2288       && frag->fr_address + frag->fr_fix > 0
2289       && frag->fr_offset > 1
2290       && now_seg != bss_section
2291       && now_seg != v850_seg_table[SBSS_SECTION].s
2292       && now_seg != v850_seg_table[TBSS_SECTION].s
2293       && now_seg != v850_seg_table[ZBSS_SECTION].s)
2294     fix_new (frag, frag->fr_fix, 2, & abs_symbol, frag->fr_offset, 0,
2295              BFD_RELOC_V850_ALIGN);
2296 }
2297
2298 /* Return current size of variable part of frag.  */
2299
2300 int
2301 md_estimate_size_before_relax (fragp, seg)
2302      fragS *fragp;
2303      asection *seg ATTRIBUTE_UNUSED;
2304 {
2305   if (fragp->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
2306     abort ();
2307
2308   return md_relax_table[fragp->fr_subtype].rlx_length;
2309 }
2310
2311 long
2312 v850_pcrel_from_section (fixp, section)
2313      fixS *fixp;
2314      segT section;
2315 {
2316   /* If the symbol is undefined, or in a section other than our own,
2317      or it is weak (in which case it may well be in another section,
2318      then let the linker figure it out.  */
2319   if (fixp->fx_addsy != (symbolS *) NULL
2320       && (! S_IS_DEFINED (fixp->fx_addsy)
2321           || S_IS_WEAK (fixp->fx_addsy)
2322           || (S_GET_SEGMENT (fixp->fx_addsy) != section)))
2323     return 0;
2324
2325   return fixp->fx_frag->fr_address + fixp->fx_where;
2326 }
2327
2328 void
2329 md_apply_fix3 (fixP, valueP, seg)
2330      fixS *fixP;
2331      valueT *valueP;
2332      segT seg ATTRIBUTE_UNUSED;
2333 {
2334   valueT value = * valueP;
2335   char *where;
2336
2337   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2338       || fixP->fx_r_type == BFD_RELOC_V850_LONGCALL
2339       || fixP->fx_r_type == BFD_RELOC_V850_LONGJUMP
2340       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2341     {
2342       fixP->fx_done = 0;
2343       return;
2344     }
2345
2346   if (fixP->fx_addsy == (symbolS *) NULL)
2347     fixP->fx_addnumber = value,
2348     fixP->fx_done = 1;
2349
2350   else if (fixP->fx_pcrel)
2351     fixP->fx_addnumber = fixP->fx_offset;
2352
2353   else
2354     {
2355       value = fixP->fx_offset;
2356       if (fixP->fx_subsy != (symbolS *) NULL)
2357         {
2358           if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
2359             value -= S_GET_VALUE (fixP->fx_subsy);
2360           else
2361             {
2362               /* We don't actually support subtracting a symbol.  */
2363               as_bad_where (fixP->fx_file, fixP->fx_line,
2364                             _("expression too complex"));
2365             }
2366         }
2367       fixP->fx_addnumber = value;
2368     }
2369
2370   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
2371     {
2372       int opindex;
2373       const struct v850_operand *operand;
2374       unsigned long insn;
2375
2376       opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
2377       operand = &v850_operands[opindex];
2378
2379       /* Fetch the instruction, insert the fully resolved operand
2380          value, and stuff the instruction back again.
2381
2382          Note the instruction has been stored in little endian
2383          format!  */
2384       where = fixP->fx_frag->fr_literal + fixP->fx_where;
2385
2386       insn = bfd_getl32 ((unsigned char *) where);
2387       insn = v850_insert_operand (insn, operand, (offsetT) value,
2388                                   fixP->fx_file, fixP->fx_line, NULL);
2389       bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
2390
2391       if (fixP->fx_done)
2392         /* Nothing else to do here.  */
2393         return;
2394
2395       /* Determine a BFD reloc value based on the operand information.
2396          We are only prepared to turn a few of the operands into relocs.  */
2397
2398       if (operand->bits == 22)
2399         fixP->fx_r_type = BFD_RELOC_V850_22_PCREL;
2400       else if (operand->bits == 9)
2401         fixP->fx_r_type = BFD_RELOC_V850_9_PCREL;
2402       else
2403         {
2404 #if 0
2405           fprintf (stderr, "bits: %d, insn: %x\n", operand->bits, insn);
2406 #endif
2407
2408           as_bad_where (fixP->fx_file, fixP->fx_line,
2409                         _("unresolved expression that must be resolved"));
2410           fixP->fx_done = 1;
2411           return;
2412         }
2413     }
2414   else if (fixP->fx_done)
2415     {
2416       /* We still have to insert the value into memory!  */
2417       where = fixP->fx_frag->fr_literal + fixP->fx_where;
2418
2419       if (fixP->fx_size == 1)
2420         *where = value & 0xff;
2421       else if (fixP->fx_size == 2)
2422         bfd_putl16 (value & 0xffff, (unsigned char *) where);
2423       else if (fixP->fx_size == 4)
2424         bfd_putl32 (value, (unsigned char *) where);
2425     }
2426 }
2427 \f
2428 /* Parse a cons expression.  We have to handle hi(), lo(), etc
2429    on the v850.  */
2430
2431 void
2432 parse_cons_expression_v850 (exp)
2433      expressionS *exp;
2434 {
2435   /* See if there's a reloc prefix like hi() we have to handle.  */
2436   hold_cons_reloc = v850_reloc_prefix (NULL);
2437
2438   /* Do normal expression parsing.  */
2439   expression (exp);
2440 }
2441
2442 /* Create a fixup for a cons expression.  If parse_cons_expression_v850
2443    found a reloc prefix, then we use that reloc, else we choose an
2444    appropriate one based on the size of the expression.  */
2445
2446 void
2447 cons_fix_new_v850 (frag, where, size, exp)
2448      fragS *frag;
2449      int where;
2450      int size;
2451      expressionS *exp;
2452 {
2453   if (hold_cons_reloc == BFD_RELOC_UNUSED)
2454     {
2455       if (size == 4)
2456         hold_cons_reloc = BFD_RELOC_32;
2457       if (size == 2)
2458         hold_cons_reloc = BFD_RELOC_16;
2459       if (size == 1)
2460         hold_cons_reloc = BFD_RELOC_8;
2461     }
2462
2463   if (exp != NULL)
2464     fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
2465   else
2466     fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
2467
2468   hold_cons_reloc = BFD_RELOC_UNUSED;
2469 }
2470
2471 bfd_boolean
2472 v850_fix_adjustable (fixP)
2473      fixS *fixP;
2474 {
2475   if (fixP->fx_addsy == NULL)
2476     return 1;
2477
2478   /* Don't adjust function names.  */
2479   if (S_IS_FUNCTION (fixP->fx_addsy))
2480     return 0;
2481
2482   /* We need the symbol name for the VTABLE entries.  */
2483   if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
2484       || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2485     return 0;
2486
2487   return 1;
2488 }
2489
2490 int
2491 v850_force_relocation (fixP)
2492      struct fix *fixP;
2493 {
2494   if (fixP->fx_r_type == BFD_RELOC_V850_LONGCALL
2495       || fixP->fx_r_type == BFD_RELOC_V850_LONGJUMP)
2496     return 1;
2497
2498   if (v850_relax
2499       && (fixP->fx_pcrel
2500           || fixP->fx_r_type == BFD_RELOC_V850_ALIGN
2501           || fixP->fx_r_type == BFD_RELOC_V850_22_PCREL
2502           || fixP->fx_r_type == BFD_RELOC_V850_9_PCREL
2503           || fixP->fx_r_type >= BFD_RELOC_UNUSED))
2504     return 1;
2505
2506   return generic_force_reloc (fixP);
2507 }