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