Display instruction when an insertion error is encountered.
[external/binutils.git] / gas / config / tc-v850.c
1 /* tc-v850.c -- Assembler code for the NEC V850
2    Copyright (C) 1996, 1997 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, name, FALSE);
685
686   * input_line_pointer = c;     /* put back the delimiting char */
687   
688   /* look to see if it's in the register table */
689   if (reg_number >= 0) 
690     {
691       expressionP->X_op         = O_register;
692       expressionP->X_add_number = reg_number;
693
694       /* make the rest nice */
695       expressionP->X_add_symbol = NULL;
696       expressionP->X_op_symbol  = NULL;
697       
698       return true;
699     }
700   else
701     {
702       /* reset the line as if we had not done anything */
703       input_line_pointer = start;
704       
705       return false;
706     }
707 }
708
709 /* Summary of system_register_name().
710  *
711  * in:  Input_line_pointer points to 1st char of operand.
712  *      expressionP points to an expression structure to be filled in.
713  *      accept_numbers is true iff numerical register names may be used.
714  * start-sanitize-v850e
715  *      accept_list_names is true iff the special names PS and SR may be 
716  *      accepted.
717  * end-sanitize-v850e
718  *
719  * out: A expressionS structure in expressionP.
720  *      The operand may have been a register: in this case, X_op == O_register,
721  *      X_add_number is set to the register number, and truth is returned.
722  *      Input_line_pointer->(next non-blank) char after operand, or is in
723  *      its original state.
724  */
725 static boolean
726 system_register_name (expressionP, accept_numbers
727                       /* start-sanitize-v850e */
728                       , accept_list_names
729                       /* end-sanitize-v850e */
730                       )
731      expressionS * expressionP;
732      boolean       accept_numbers;
733 /* start-sanitize-v850e */
734      boolean       accept_list_names;
735 /* end-sanitize-v850e */
736 {
737   int    reg_number;
738   char * name;
739   char * start;
740   char   c;
741
742   /* Find the spelling of the operand */
743   start = name = input_line_pointer;
744
745   c = get_symbol_end ();
746   reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name, accept_numbers);
747
748   * input_line_pointer = c;   /* put back the delimiting char */
749   
750   if (reg_number < 0
751       && accept_numbers)
752     {
753       input_line_pointer   = start; /* reset input_line pointer */
754
755       if (isdigit (* input_line_pointer))
756         {
757           reg_number = strtol (input_line_pointer, & input_line_pointer, 10);
758
759           /* Make sure that the register number is allowable. */
760           if (   reg_number < 0
761                  || reg_number > 5
762 /* start-sanitize-v850e */
763                  && reg_number < 16
764                  || reg_number > 20
765 /* end-sanitize-v850e */
766                  )
767             {
768               reg_number = -1;
769             }
770         }
771 /* start-sanitize-v850e */      
772       else if (accept_list_names)
773         {
774           c = get_symbol_end ();
775           reg_number = reg_name_search (system_list_registers, SYSREGLIST_NAME_CNT, name, FALSE);
776
777           * input_line_pointer = c;   /* put back the delimiting char */
778         }
779 /* end-sanitize-v850e */      
780     }
781       
782   /* look to see if it's in the register table */
783   if (reg_number >= 0) 
784     {
785       expressionP->X_op         = O_register;
786       expressionP->X_add_number = reg_number;
787
788       /* make the rest nice */
789       expressionP->X_add_symbol = NULL;
790       expressionP->X_op_symbol  = NULL;
791
792       return true;
793     }
794   else
795     {
796       /* reset the line as if we had not done anything */
797       input_line_pointer = start;
798       
799       return false;
800     }
801 }
802
803 /* Summary of cc_name().
804  *
805  * in: Input_line_pointer points to 1st char of operand.
806  *
807  * out: A expressionS.
808  *      The operand may have been a register: in this case, X_op == O_register,
809  *      X_add_number is set to the register number, and truth is returned.
810  *      Input_line_pointer->(next non-blank) char after operand, or is in
811  *      its original state.
812  */
813 static boolean
814 cc_name (expressionP)
815      expressionS * expressionP;
816 {
817   int    reg_number;
818   char * name;
819   char * start;
820   char   c;
821
822   /* Find the spelling of the operand */
823   start = name = input_line_pointer;
824
825   c = get_symbol_end ();
826   reg_number = reg_name_search (cc_names, CC_NAME_CNT, name, FALSE);
827
828   * input_line_pointer = c;   /* put back the delimiting char */
829   
830   /* look to see if it's in the register table */
831   if (reg_number >= 0) 
832     {
833       expressionP->X_op         = O_constant;
834       expressionP->X_add_number = reg_number;
835
836       /* make the rest nice */
837       expressionP->X_add_symbol = NULL;
838       expressionP->X_op_symbol  = NULL;
839
840       return true;
841     }
842   else
843     {
844       /* reset the line as if we had not done anything */
845       input_line_pointer = start;
846       
847       return false;
848     }
849 }
850
851 static void
852 skip_white_space (void)
853 {
854   while (   * input_line_pointer == ' '
855          || * input_line_pointer == '\t')
856     ++ input_line_pointer;
857 }
858
859 /* start-sanitize-v850e */
860 /* Summary of parse_register_list ().
861  *
862  * in: Input_line_pointer  points to 1st char of a list of registers.
863  *     insn                is the partially constructed instruction.
864  *     operand             is the operand being inserted.
865  *
866  * out: NULL if the parse completed successfully, otherwise a
867  *      pointer to an error message is returned.  If the parse
868  *      completes the correct bit fields in the instruction
869  *      will be filled in.
870  *
871  * Parses register lists with the syntax:
872  *
873  *   { rX }
874  *   { rX, rY }
875  *   { rX - rY }
876  *   { rX - rY, rZ }
877  *   etc
878  *
879  * and also parses constant epxressions whoes bits indicate the
880  * registers in the lists.  The LSB in the expression refers to
881  * the lowest numbered permissable register in the register list,
882  * and so on upwards.  System registers are considered to be very
883  * high numbers.
884  * 
885  */
886 static char *
887 parse_register_list
888 (
889   unsigned long *             insn,
890   const struct v850_operand * operand
891 )
892 {
893   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 };
894   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 };
895   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 };
896   int *       regs;
897   expressionS exp;
898
899
900   /* Select a register array to parse. */
901   switch (operand->shift)
902     {
903     case 0xffe00001: regs = type1_regs; break;
904     case 0xfff8000f: regs = type2_regs; break;
905     case 0xfff8001f: regs = type3_regs; break;
906     default:
907       as_bad ("unknown operand shift: %x\n", operand->shift );              
908       return "internal failure in parse_register_list";
909     }
910
911   skip_white_space();
912
913   /* If the expression starts with a curly brace it is a register list.
914      Otherwise it is a constant expression, whoes bits indicate which
915      registers are to be included in the list.  */
916   
917   if (* input_line_pointer != '{')
918     {
919       int bits;
920       int reg;
921       int i;
922                 
923       expression (& exp);
924       
925       if (exp.X_op != O_constant)
926         return "constant expression or register list expected";
927
928       if (regs == type1_regs)
929         {
930           if (exp.X_add_number & 0xFFFFF000)
931             return "high bits set in register list expression";
932           
933           for (reg = 20; reg < 32; reg ++)
934             if (exp.X_add_number & (1 << (reg - 20)))
935               {
936                 for (i = 0; i < 32; i++)
937                   if (regs[i] == reg)
938                     * insn |= (1 << i);
939               }
940         }
941       else if (regs == type2_regs)
942         {
943           if (exp.X_add_number & 0xFFFE0000)
944             return "high bits set in register list expression";
945           
946           for (reg = 1; reg < 16; reg ++)
947             if (exp.X_add_number & (1 << (reg - 1)))
948               {
949                 for (i = 0; i < 32; i++)
950                   if (regs[i] == reg)
951                     * insn |= (1 << i);
952               }
953
954           if (exp.X_add_number & (1 << 15))
955             * insn |= (1 << 3);
956           
957           if (exp.X_add_number & (1 << 16))
958             * insn |= (1 << 19);
959         }
960       else /* regs == type3_regs */
961         {
962           if (exp.X_add_number & 0xFFFE0000)
963             return "high bits set in register list expression";
964           
965           for (reg = 16; reg < 32; reg ++)
966             if (exp.X_add_number & (1 << (reg - 16)))
967               {
968                 for (i = 0; i < 32; i++)
969                   if (regs[i] == reg)
970                     * insn |= (1 << i);
971               }
972
973           if (exp.X_add_number & (1 << 16))
974             * insn |= (1 << 19);
975         }
976
977       return NULL;
978     }
979
980   input_line_pointer ++;
981
982   /* Parse the register list until a terminator (closing curly brace or new-line) is found.  */
983   for (;;)
984     {
985       if (register_name (& exp))
986         {
987           int  i;
988           
989           /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction.  */
990           for (i = 0; i < 32; i++)
991             {
992               if (regs[ i ] == exp.X_add_number)
993                 {
994                   * insn |= (1 << i);
995                   break;
996                 }
997             }
998
999           if (i == 32)
1000             {
1001               return "illegal register included in list";
1002             }
1003         }
1004       else if (system_register_name (& exp, true, true))
1005         {
1006           if (regs == type1_regs)
1007             {
1008               return "system registers cannot be included in list";
1009             }
1010           else if (exp.X_add_number == 5)
1011             {
1012               if (regs == type2_regs)
1013                 return "PSW cannot be included in list";
1014               else
1015                 * insn |= 0x8;
1016             }
1017           else if (exp.X_add_number < 4)
1018             * insn |= 0x80000;
1019           else
1020             return "High value system registers cannot be included in list";
1021         }
1022       else if (* input_line_pointer == '}')
1023         {
1024           input_line_pointer ++;
1025           break;
1026         }
1027       else if (* input_line_pointer == ',')
1028         {
1029           input_line_pointer ++;
1030           continue;
1031         }
1032       else if (* input_line_pointer == '-')
1033         {
1034           /* We have encountered a range of registers: rX - rY */
1035           int         j;
1036           expressionS exp2;
1037
1038           /* Skip the dash.  */
1039           ++ input_line_pointer;
1040
1041           /* Get the second register in the range.  */
1042           if (! register_name (& exp2))
1043             {
1044               return "second register should follow dash in register list";
1045               exp2.X_add_number = exp.X_add_number;
1046             }
1047
1048           /* Add the rest of the registers in the range.  */
1049           for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
1050             {
1051               int  i;
1052           
1053               /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction.  */
1054               for (i = 0; i < 32; i++)
1055                 {
1056                   if (regs[ i ] == j)
1057                     {
1058                       * insn |= (1 << i);
1059                       break;
1060                     }
1061                 }
1062
1063               if (i == 32)
1064                 {
1065                   return "illegal register included in list";
1066                 }
1067             }
1068         }
1069       else
1070         {
1071           break;
1072         }
1073
1074       skip_white_space ();
1075     }
1076
1077   return NULL;
1078 }
1079 /* end-sanitize-v850e */
1080
1081 CONST char * md_shortopts = "m:";
1082
1083 struct option md_longopts[] =
1084 {
1085   {NULL, no_argument, NULL, 0}
1086 };
1087 size_t md_longopts_size = sizeof md_longopts; 
1088
1089
1090 void
1091 md_show_usage (stream)
1092   FILE * stream;
1093 {
1094   fprintf (stream, "V850 options:\n");
1095   fprintf (stream, "\t-mwarn-signed-overflow    Warn if signed immediate values overflow\n");
1096   fprintf (stream, "\t-mwarn-unsigned-overflow  Warn if unsigned immediate values overflow\n");
1097   fprintf (stream, "\t-mv850                    The code is targeted at the v850\n");
1098 /* start-sanitize-v850e */
1099   fprintf (stream, "\t-mv850e                   The code is targeted at the v850e\n");
1100   fprintf (stream, "\t-mv850ea                  The code is targeted at the v850ea\n");
1101 /* end-sanitize-v850e */
1102
1103
1104 int
1105 md_parse_option (c, arg)
1106      int    c;
1107      char * arg;
1108 {
1109   if (c != 'm')
1110     {
1111       fprintf (stderr, "unknown command line option: -%c%s\n", c, arg);
1112       return 0;
1113     }
1114
1115   if (strcmp (arg, "warn-signed-overflow") == 0)
1116     {
1117       warn_signed_overflows = TRUE;
1118     }
1119   else if (strcmp (arg, "warn-unsigned-overflow") == 0)
1120     {
1121       warn_unsigned_overflows = TRUE;
1122     }
1123   else if (strcmp (arg, "v850") == 0)
1124     {
1125       machine = 0;
1126       processor_mask = PROCESSOR_V850;
1127     }
1128 /* start-sanitize-v850e */
1129   else if (strcmp (arg, "v850e") == 0)
1130     {
1131       machine = bfd_mach_v850e;
1132       processor_mask = PROCESSOR_V850E;
1133     }
1134   else if (strcmp (arg, "v850ea") == 0)
1135     {
1136       machine = bfd_mach_v850ea;
1137       processor_mask = PROCESSOR_V850EA;
1138     }
1139 /* end-sanitize-v850e */
1140   else
1141     {
1142       fprintf (stderr, "unknown command line option: -%c%s\n", c, arg);
1143       return 0;
1144     }
1145   
1146   return 1;
1147 }
1148
1149 symbolS *
1150 md_undefined_symbol (name)
1151   char * name;
1152 {
1153   return 0;
1154 }
1155
1156 char *
1157 md_atof (type, litp, sizep)
1158   int    type;
1159   char * litp;
1160   int *  sizep;
1161 {
1162   int            prec;
1163   LITTLENUM_TYPE words[4];
1164   char *         t;
1165   int            i;
1166
1167   switch (type)
1168     {
1169     case 'f':
1170       prec = 2;
1171       break;
1172
1173     case 'd':
1174       prec = 4;
1175       break;
1176
1177     default:
1178       *sizep = 0;
1179       return "bad call to md_atof";
1180     }
1181   
1182   t = atof_ieee (input_line_pointer, type, words);
1183   if (t)
1184     input_line_pointer = t;
1185
1186   *sizep = prec * 2;
1187
1188   for (i = prec - 1; i >= 0; i--)
1189     {
1190       md_number_to_chars (litp, (valueT) words[i], 2);
1191       litp += 2;
1192     }
1193
1194   return NULL;
1195 }
1196
1197
1198 /* Very gross.  */
1199 void
1200 md_convert_frag (abfd, sec, fragP)
1201   bfd *      abfd;
1202   asection * sec;
1203   fragS *    fragP;
1204 {
1205   subseg_change (sec, 0);
1206   
1207   /* In range conditional or unconditional branch.  */
1208   if (fragP->fr_subtype == 0 || fragP->fr_subtype == 2)
1209     {
1210       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
1211                fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
1212       fragP->fr_var = 0;
1213       fragP->fr_fix += 2;
1214     }
1215   /* Out of range conditional branch.  Emit a branch around a jump.  */
1216   else if (fragP->fr_subtype == 1)
1217     {
1218       unsigned char *buffer = 
1219         (unsigned char *) (fragP->fr_fix + fragP->fr_literal);
1220
1221       /* Reverse the condition of the first branch.  */
1222       buffer[0] ^= 0x08;
1223       /* Mask off all the displacement bits.  */
1224       buffer[0] &= 0x8f;
1225       buffer[1] &= 0x07;
1226       /* Now set the displacement bits so that we branch
1227          around the unconditional branch.  */
1228       buffer[0] |= 0x30;
1229
1230       /* Now create the unconditional branch + fixup to the final
1231          target.  */
1232       md_number_to_chars (buffer + 2, 0x00000780, 4);
1233       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
1234                fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
1235       fragP->fr_var = 0;
1236       fragP->fr_fix += 6;
1237     }
1238   /* Out of range unconditional branch.  Emit a jump.  */
1239   else if (fragP->fr_subtype == 3)
1240     {
1241       md_number_to_chars (fragP->fr_fix + fragP->fr_literal, 0x00000780, 4);
1242       fix_new (fragP, fragP->fr_fix, 4, fragP->fr_symbol,
1243                fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
1244       fragP->fr_var = 0;
1245       fragP->fr_fix += 4;
1246     }
1247   else
1248     abort ();
1249 }
1250
1251 valueT
1252 md_section_align (seg, addr)
1253      asection * seg;
1254      valueT     addr;
1255 {
1256   int align = bfd_get_section_alignment (stdoutput, seg);
1257   return ((addr + (1 << align) - 1) & (-1 << align));
1258 }
1259
1260 void
1261 md_begin ()
1262 {
1263   char *                              prev_name = "";
1264   register const struct v850_opcode * op;
1265   flagword                            applicable;
1266
1267 /* start-sanitize-v850e */
1268   if (strncmp (TARGET_CPU, "v850ea", 6) == 0)
1269     {
1270       if (machine == -1)
1271         machine = bfd_mach_v850ea;
1272       
1273       if (processor_mask == -1)
1274         processor_mask = PROCESSOR_V850EA;
1275     }
1276   else if (strncmp (TARGET_CPU, "v850e", 5) == 0)
1277     {
1278       if (machine == -1)
1279         machine        = bfd_mach_v850e;
1280       
1281       if (processor_mask == -1)
1282         processor_mask = PROCESSOR_V850E;
1283     }
1284   else
1285 /* end-sanitize-v850e */
1286   if (strncmp (TARGET_CPU, "v850", 4) == 0)
1287     {
1288       if (machine == -1)
1289         machine        = 0;
1290       
1291       if (processor_mask == -1)
1292         processor_mask = PROCESSOR_V850;
1293     }
1294   else
1295     as_bad ("Unable to determine default target processor from string: %s", 
1296             TARGET_CPU);
1297
1298   v850_hash = hash_new();
1299
1300   /* Insert unique names into hash table.  The V850 instruction set
1301      has many identical opcode names that have different opcodes based
1302      on the operands.  This hash table then provides a quick index to
1303      the first opcode with a particular name in the opcode table.  */
1304
1305   op = v850_opcodes;
1306   while (op->name)
1307     {
1308       if (strcmp (prev_name, op->name)) 
1309         {
1310           prev_name = (char *) op->name;
1311           hash_insert (v850_hash, op->name, (char *) op);
1312         }
1313       op++;
1314     }
1315
1316   bfd_set_arch_mach (stdoutput, TARGET_ARCH, machine);
1317
1318   applicable = bfd_applicable_section_flags (stdoutput);
1319
1320   sdata_section = subseg_new (".sdata", 0);
1321   bfd_set_section_flags (stdoutput, sdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
1322   
1323   tdata_section = subseg_new (".tdata", 0);
1324   bfd_set_section_flags (stdoutput, tdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
1325   
1326   zdata_section = subseg_new (".zdata", 0);
1327   bfd_set_section_flags (stdoutput, zdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
1328   
1329   sbss_section = subseg_new (".sbss", 0);
1330   bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
1331   seg_info (sbss_section)->bss = 1;
1332   
1333   tbss_section = subseg_new (".tbss", 0);
1334   bfd_set_section_flags (stdoutput, tbss_section, applicable & SEC_ALLOC);
1335   seg_info (tbss_section)->bss = 1;
1336   
1337   zbss_section = subseg_new (".zbss", 0);
1338   bfd_set_section_flags (stdoutput, zbss_section, applicable & SEC_ALLOC);
1339   seg_info (zbss_section)->bss = 1;
1340   
1341   rosdata_section = subseg_new (".rosdata", 0);
1342   bfd_set_section_flags (stdoutput, rosdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY));
1343                          
1344   rozdata_section = subseg_new (".rozdata", 0);
1345   bfd_set_section_flags (stdoutput, rozdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY));
1346
1347   scommon_section = subseg_new (".scommon", 0);
1348   bfd_set_section_flags (stdoutput, scommon_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS | SEC_IS_COMMON));
1349
1350   zcommon_section = subseg_new (".zcommon", 0);
1351   bfd_set_section_flags (stdoutput, zcommon_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS | SEC_IS_COMMON));
1352
1353   tcommon_section = subseg_new (".tcommon", 0);
1354   bfd_set_section_flags (stdoutput, tcommon_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS | SEC_IS_COMMON));
1355
1356 /* start-sanitize-v850e */
1357   call_table_data_section = subseg_new (".call_table_data", 0);
1358   bfd_set_section_flags (stdoutput, call_table_data_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
1359   
1360   call_table_text_section = subseg_new (".call_table_text", 0);
1361   bfd_set_section_flags (stdoutput, call_table_text_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE));
1362 /* end-sanitize-v850e */
1363 }
1364
1365
1366 /* start-sanitize-v850e */
1367 static bfd_reloc_code_real_type
1368 handle_ctoff (const struct v850_operand * operand)
1369 {
1370   if (operand == NULL)
1371     return BFD_RELOC_V850_CALLT_16_16_OFFSET;
1372
1373   if (   operand->bits  != 6
1374       || operand->shift != 0)
1375     {
1376       as_bad ("ctoff() relocation used on an instruction which does not support it");
1377       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
1378     }
1379       
1380   return BFD_RELOC_V850_CALLT_6_7_OFFSET;
1381 }
1382 /* end-sanitize-v850e */
1383
1384 static bfd_reloc_code_real_type
1385 handle_sdaoff (const struct v850_operand * operand)
1386 {
1387   if (operand == NULL)                             return BFD_RELOC_V850_SDA_16_16_OFFSET;
1388   if (operand->bits == 15 && operand->shift == 17) return BFD_RELOC_V850_SDA_15_16_OFFSET;
1389   /* start-sanitize-v850e */
1390   if (operand->bits == -1)                         return BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET;
1391   /* end-sanitize-v850e */
1392   
1393   if (   operand->bits  != 16
1394       || operand->shift != 16)
1395     {
1396       as_bad ("sdaoff() relocation used on an instruction which does not support it");
1397       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
1398     }
1399   
1400   return BFD_RELOC_V850_SDA_16_16_OFFSET;
1401 }
1402
1403 static bfd_reloc_code_real_type
1404 handle_zdaoff (const struct v850_operand * operand)
1405 {
1406   if (operand == NULL)                             return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1407   if (operand->bits == 15 && operand->shift == 17) return BFD_RELOC_V850_ZDA_15_16_OFFSET;
1408   /* start-sanitize-v850e */
1409   if (operand->bits == -1)                         return BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET;
1410   /* end-sanitize-v850e */
1411
1412   if (   operand->bits  != 16
1413       || operand->shift != 16)
1414     {
1415       as_bad ("zdaoff() relocation used on an instruction which does not support it");
1416       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
1417     }
1418   
1419   return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1420 }
1421
1422 static bfd_reloc_code_real_type
1423 handle_tdaoff (const struct v850_operand * operand)
1424 {
1425   if (operand == NULL)                               return BFD_RELOC_V850_TDA_7_7_OFFSET;  /* data item, not an instruction.  */
1426   if (operand->bits == 6 && operand->shift == 1)     return BFD_RELOC_V850_TDA_6_8_OFFSET;  /* sld.w/sst.w, operand: D8_6  */
1427   /* start-sanitize-v850e */
1428   if (operand->bits == 4 && operand->insert != NULL) return BFD_RELOC_V850_TDA_4_5_OFFSET;  /* sld.hu, operand: D5-4 */
1429   if (operand->bits == 4 && operand->insert == NULL) return BFD_RELOC_V850_TDA_4_4_OFFSET;  /* sld.bu, operand: D4   */
1430   /* end-sanitize-v850e */
1431   if (operand->bits == 16 && operand->shift == 16)   return BFD_RELOC_V850_TDA_16_16_OFFSET; /* set1 & chums, operands: D16 */
1432   
1433   if (operand->bits != 7)
1434     {
1435       as_bad ("tdaoff() relocation used on an instruction which does not support it");
1436       return BFD_RELOC_64;  /* Used to indicate an error condition.  */
1437     }
1438   
1439   return  operand->insert != NULL
1440     ? BFD_RELOC_V850_TDA_7_8_OFFSET     /* sld.h/sst.h, operand: D8_7 */
1441     : BFD_RELOC_V850_TDA_7_7_OFFSET;    /* sld.b/sst.b, opreand: D7   */
1442 }
1443
1444 /* Warning: The code in this function relies upon the definitions
1445    in the v850_operands[] array (defined in opcodes/v850-opc.c)
1446    matching the hard coded values contained herein.  */
1447
1448 static bfd_reloc_code_real_type
1449 v850_reloc_prefix (const struct v850_operand * operand)
1450 {
1451   boolean paren_skipped = false;
1452
1453
1454   /* Skip leading opening parenthesis.  */
1455   if (* input_line_pointer == '(')
1456     {
1457       ++ input_line_pointer;
1458       paren_skipped = true;
1459     }
1460
1461 #define CHECK_(name, reloc)                                             \
1462   if (strncmp (input_line_pointer, name##"(", strlen (name) + 1) == 0)  \
1463     {                                                                   \
1464       input_line_pointer += strlen (name);                              \
1465       return reloc;                                                     \
1466     }
1467   
1468   CHECK_ ("hi0",    BFD_RELOC_HI16);
1469   CHECK_ ("hi",     BFD_RELOC_HI16_S);
1470   CHECK_ ("lo",     BFD_RELOC_LO16);
1471   CHECK_ ("sdaoff", handle_sdaoff (operand));
1472   CHECK_ ("zdaoff", handle_zdaoff (operand));
1473   CHECK_ ("tdaoff", handle_tdaoff (operand));
1474
1475 /* start-sanitize-v850e */
1476   CHECK_ ("hilo",   BFD_RELOC_32);
1477   CHECK_ ("ctoff",  handle_ctoff (operand));
1478 /* end-sanitize-v850e */
1479   
1480   /* Restore skipped parenthesis.  */
1481   if (paren_skipped)
1482     -- input_line_pointer;
1483   
1484   return BFD_RELOC_UNUSED;
1485 }
1486
1487 /* Insert an operand value into an instruction.  */
1488
1489 static unsigned long
1490 v850_insert_operand (insn, operand, val, file, line, str)
1491      unsigned long               insn;
1492      const struct v850_operand * operand;
1493      offsetT                     val;
1494      char *                      file;
1495      unsigned int                line;
1496      char *                      str;
1497 {
1498   if (operand->insert)
1499     {
1500       const char * message = NULL;
1501       
1502       insn = operand->insert (insn, val, & message);
1503       if (message != NULL)
1504         {
1505           if ((operand->flags & V850_OPERAND_SIGNED)
1506               && ! warn_signed_overflows
1507               && strstr (message, "out of range") != NULL)
1508             {
1509               /* skip warning... */
1510             }
1511           else if ((operand->flags & V850_OPERAND_SIGNED) == 0
1512                    && ! warn_unsigned_overflows
1513                    && strstr (message, "out of range") != NULL)
1514             {
1515               /* skip warning... */
1516             }
1517           else if (str)
1518             {
1519               if (file == (char *) NULL)
1520                 as_warn ("%s: %s", str, message);
1521               else
1522                 as_warn_where (file, line, "%s: %s", str, message);
1523             }
1524           else
1525             {
1526               if (file == (char *) NULL)
1527                 as_warn (message);
1528               else
1529                 as_warn_where (file, line, message);
1530             }
1531         }
1532     }
1533   else
1534     {
1535       if (operand->bits != 32)
1536         {
1537           long    min, max;
1538           offsetT test;
1539
1540           if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1541             {
1542               if (! warn_signed_overflows)
1543                 max = (1 << operand->bits) - 1;
1544               else
1545                 max = (1 << (operand->bits - 1)) - 1;
1546               
1547               min = - (1 << (operand->bits - 1));
1548             }
1549           else
1550             {
1551               max = (1 << operand->bits) - 1;
1552               
1553               if (! warn_unsigned_overflows)
1554                 min = - (1 << (operand->bits - 1));
1555               else
1556                 min = 0;
1557             }
1558           
1559           if (val < (offsetT) min || val > (offsetT) max)
1560             {
1561               const char * err = "operand out of range (%s not between %ld and %ld)";
1562               char         buf[100];
1563               
1564               /* Restore min and mix to expected values for decimal ranges.  */
1565               if ((operand->flags & V850_OPERAND_SIGNED) && ! warn_signed_overflows)
1566                 max = (1 << (operand->bits - 1)) - 1;
1567
1568               if (! (operand->flags & V850_OPERAND_SIGNED)
1569                   && ! warn_unsigned_overflows)
1570                 min = 0;
1571
1572               if (str)
1573                 {
1574                   sprintf (buf, "%s: ", str);
1575                   
1576                   sprint_value (buf + strlen (buf), val);
1577                 }
1578               else
1579                 sprint_value (buf, val);
1580               
1581               if (file == (char *) NULL)
1582                 as_warn (err, buf, min, max);
1583               else
1584                 as_warn_where (file, line, err, buf, min, max);
1585             }
1586         }
1587
1588       insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1589     }
1590   
1591   return insn;
1592 }
1593
1594 \f
1595 static char                 copy_of_instruction [128];
1596
1597 void
1598 md_assemble (str) 
1599      char * str;
1600 {
1601   char *                    s;
1602   char *                    start_of_operands;
1603   struct v850_opcode *      opcode;
1604   struct v850_opcode *      next_opcode;
1605   const unsigned char *     opindex_ptr;
1606   int                       next_opindex;
1607   int                       relaxable;
1608   unsigned long             insn;
1609   unsigned long             insn_size;
1610   char *                    f;
1611   int                       i;
1612   int                       match;
1613   boolean                   extra_data_after_insn = false;
1614   unsigned                  extra_data_len;
1615   unsigned long             extra_data;
1616   char *                    saved_input_line_pointer;
1617
1618   
1619   strncpy (copy_of_instruction, str, sizeof (copy_of_instruction) - 1);
1620   
1621   /* Get the opcode.  */
1622   for (s = str; *s != '\0' && ! isspace (*s); s++)
1623     continue;
1624   
1625   if (*s != '\0')
1626     *s++ = '\0';
1627
1628   /* find the first opcode with the proper name */
1629   opcode = (struct v850_opcode *) hash_find (v850_hash, str);
1630   if (opcode == NULL)
1631     {
1632       as_bad ("Unrecognized opcode: `%s'", str);
1633       ignore_rest_of_line ();
1634       return;
1635     }
1636
1637   str = s;
1638   while (isspace (* str))
1639     ++ str;
1640
1641   start_of_operands = str;
1642
1643   saved_input_line_pointer = input_line_pointer;
1644   
1645   for (;;)
1646     {
1647       const char * errmsg = NULL;
1648
1649       match = 0;
1650       
1651       if ((opcode->processors & processor_mask) == 0)
1652         {
1653           errmsg = "Target processor does not support this instruction.";
1654           goto error;
1655         }
1656       
1657       relaxable = 0;
1658       fc = 0;
1659       next_opindex = 0;
1660       insn = opcode->opcode;
1661       extra_data_after_insn = false;
1662
1663       input_line_pointer = str = start_of_operands;
1664
1665       for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1666         {
1667           const struct v850_operand * operand;
1668           char *                      hold;
1669           expressionS                 ex;
1670           bfd_reloc_code_real_type    reloc;
1671
1672           if (next_opindex == 0)
1673             {
1674               operand = & v850_operands[ * opindex_ptr ];
1675             }
1676           else
1677             {
1678               operand      = & v850_operands[ next_opindex ];
1679               next_opindex = 0;
1680             }
1681
1682           errmsg = NULL;
1683
1684           while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1685             ++ str;
1686
1687           if (operand->flags & V850_OPERAND_RELAX)
1688             relaxable = 1;
1689
1690           /* Gather the operand. */
1691           hold = input_line_pointer;
1692           input_line_pointer = str;
1693           
1694           /* lo(), hi(), hi0(), etc... */
1695           if ((reloc = v850_reloc_prefix (operand)) != BFD_RELOC_UNUSED)
1696             {
1697               if (reloc == BFD_RELOC_64) /* This is a fake reloc, used to indicate an error condition.  */
1698                 {
1699                   match = 1;
1700                   goto error;
1701                 }
1702                  
1703               expression (& ex);
1704
1705               if (ex.X_op == O_constant)
1706                 {
1707                   switch (reloc)
1708                     {
1709                     case BFD_RELOC_V850_ZDA_16_16_OFFSET:
1710                       /* To cope with "not1 7, zdaoff(0xfffff006)[r0]"  and the like.  */
1711                       /* Fall through.  */
1712                       
1713                     case BFD_RELOC_LO16:
1714                       {
1715                         /* Truncate, then sign extend the value.  */
1716                         ex.X_add_number = SEXT16 (ex.X_add_number);
1717                         break;
1718                       }
1719
1720                     case BFD_RELOC_HI16:
1721                       {
1722                         /* Truncate, then sign extend the value.  */
1723                         ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
1724                         break;
1725                       }
1726
1727                     case BFD_RELOC_HI16_S:
1728                       {
1729                         /* Truncate, then sign extend the value.  */
1730                         int temp = (ex.X_add_number >> 16) & 0xffff;
1731
1732                         temp += (ex.X_add_number >> 15) & 1;
1733
1734                         ex.X_add_number = SEXT16 (temp);
1735                         break;
1736                       }
1737                     
1738 /* start-sanitize-v850e */
1739                     case BFD_RELOC_32:
1740                       if ((operand->flags & V850E_IMMEDIATE32) == 0)
1741                         {
1742                           errmsg = "immediate operand is too large";
1743                           goto error;
1744                         }
1745                       
1746                       extra_data_after_insn = true;
1747                       extra_data_len        = 4;
1748                       extra_data            = ex.X_add_number;
1749                       ex.X_add_number       = 0;
1750                       break;
1751 /* end-sanitize-v850e */
1752                       
1753                     default:
1754                       fprintf (stderr, "reloc: %d\n", reloc);
1755                       as_bad ("AAARG -> unhandled constant reloc");
1756                       break;
1757                     }
1758
1759                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
1760                                               (char *) NULL, 0, copy_of_instruction);
1761                 }
1762               else
1763                 {
1764 /* start-sanitize-v850e */
1765                   if (reloc == BFD_RELOC_32)
1766                     {
1767                       if ((operand->flags & V850E_IMMEDIATE32) == 0)
1768                         {
1769                           errmsg = "immediate operand is too large";
1770                           goto error;
1771                         }
1772                       
1773                       extra_data_after_insn = true;
1774                       extra_data_len        = 4;
1775                       extra_data            = ex.X_add_number;
1776                     }
1777 /* end-sanitize-v850e */
1778                       
1779                   if (fc > MAX_INSN_FIXUPS)
1780                     as_fatal ("too many fixups");
1781
1782                   fixups[ fc ].exp     = ex;
1783                   fixups[ fc ].opindex = * opindex_ptr;
1784                   fixups[ fc ].reloc   = reloc;
1785                   fc++;
1786                 }
1787             }
1788           else
1789             {
1790               errmsg = NULL;
1791               
1792               if ((operand->flags & V850_OPERAND_REG) != 0) 
1793                 {
1794                   if (!register_name (& ex))
1795                     {
1796                       errmsg = "invalid register name";
1797                     }
1798
1799                   if ((operand->flags & V850_NOT_R0)
1800                       && ex.X_add_number == 0)
1801                     {
1802                       errmsg = "register r0 cannot be used here";
1803                       
1804                       /* Force an error message to be generated by
1805                          skipping over any following potential matches
1806                          for this opcode.  */
1807                       opcode += 3;
1808
1809                       if (* input_line_pointer == ']')
1810                         ++ input_line_pointer;
1811                     }
1812                 }
1813               else if ((operand->flags & V850_OPERAND_SRG) != 0) 
1814                 {
1815                   if (!system_register_name (& ex, true
1816                                              /* start-sanitize-v850e */
1817                                              , false
1818                                              /* end-sanitize-v850e */
1819                                              ))
1820                     {
1821                       errmsg = "invalid system register name";
1822                     }
1823                 }
1824               else if ((operand->flags & V850_OPERAND_EP) != 0)
1825                 {
1826                   char * start = input_line_pointer;
1827                   char   c     = get_symbol_end ();
1828                   
1829                   if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
1830                     {
1831                       /* Put things back the way we found them.  */
1832                       *input_line_pointer = c;
1833                       input_line_pointer = start;
1834                       errmsg = "expected EP register";
1835                       goto error;
1836                     }
1837                   
1838                   *input_line_pointer = c;
1839                   str = input_line_pointer;
1840                   input_line_pointer = hold;
1841               
1842                   while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1843                     ++str;
1844                   continue;
1845                 }
1846               else if ((operand->flags & V850_OPERAND_CC) != 0) 
1847                 {
1848                   if (!cc_name (& ex))
1849                     {
1850                       errmsg = "invalid condition code name";
1851                     }
1852                 }
1853 /* start-sanitize-v850e */
1854               else if (operand->flags & V850E_PUSH_POP) 
1855                 {
1856                   errmsg = parse_register_list (& insn, operand);
1857                   
1858                   /* The parse_register_list() function has already done everything, so fake a dummy expression.  */
1859                   ex.X_op         = O_constant;
1860                   ex.X_add_number = 0;
1861                 }
1862               else if (operand->flags & V850E_IMMEDIATE16) 
1863                 {
1864                   expression (& ex);
1865
1866                   if (ex.X_op != O_constant)
1867                     errmsg = "constant expression expected";
1868                   else if (ex.X_add_number & 0xffff0000)
1869                     {
1870                       if (ex.X_add_number & 0xffff)
1871                         errmsg = "constant too big to fit into instruction";
1872                       else if ((insn & 0x001fffc0) == 0x00130780)
1873                         ex.X_add_number >>= 16;
1874                       else
1875                         errmsg = "constant too big to fit into instruction";
1876                     }
1877                   
1878                   extra_data_after_insn = true;
1879                   extra_data_len        = 2;
1880                   extra_data            = ex.X_add_number;
1881                   ex.X_add_number       = 0;
1882                 }
1883               else if (operand->flags & V850E_IMMEDIATE32) 
1884                 {
1885                   expression (& ex);
1886                   
1887                   if (ex.X_op != O_constant)
1888                     errmsg = "constant expression expected";
1889                   
1890                   extra_data_after_insn = true;
1891                   extra_data_len        = 4;
1892                   extra_data            = ex.X_add_number;
1893                   ex.X_add_number       = 0;
1894                 }
1895 /* end-sanitize-v850e */
1896               else if (register_name (& ex)
1897                        && (operand->flags & V850_OPERAND_REG) == 0)
1898                 {
1899                   /* It is possible that an alias has been defined that
1900                      matches a register name.  For example the code may
1901                      include a ".set ZERO, 0" directive, which matches
1902                      the register name "zero".  Attempt to reparse the
1903                      field as an expression, and only complain if we
1904                      cannot generate a constant.  */
1905                   
1906                   input_line_pointer = str;
1907                   
1908                   expression (& ex);
1909
1910                   if (ex.X_op != O_constant)
1911                     errmsg = "syntax error: register not expected";
1912                 }
1913               else if (system_register_name (& ex, false
1914                                              /* start-sanitize-v850e */
1915                                              , false
1916                                              /* end-sanitize-v850e */
1917                                              )
1918                        && (operand->flags & V850_OPERAND_SRG) == 0)
1919                 {
1920                   errmsg = "syntax error: system register not expected";
1921                 }
1922               else if (cc_name (&ex)
1923                        && (operand->flags & V850_OPERAND_CC) == 0)
1924                 {
1925                   errmsg = "syntax error: condition code not expected";
1926                 }
1927               else
1928                 {
1929                   expression (& ex);
1930 /* start-sanitize-v850e */
1931                   /* Special case:
1932                      If we are assembling a MOV instruction (or a CALLT.... :-)
1933                      and the immediate value does not fit into the bits available
1934                      then create a fake error so that the next MOV instruction
1935                       will be selected.  This one has a 32 bit immediate field.  */
1936
1937                   if (((insn & 0x07e0) == 0x0200)
1938                       && ex.X_op == O_constant
1939                       && (ex.X_add_number < (- (1 << (operand->bits - 1))) || ex.X_add_number > ((1 << operand->bits) - 1)))
1940                     errmsg = "immediate constant is too large";
1941 /* end-sanitize-v850e */
1942                 }
1943
1944               if (errmsg)
1945                 goto error;
1946               
1947 /* fprintf (stderr, " insn: %x, operand %d, op: %d, add_number: %d\n", insn, opindex_ptr - opcode->operands, ex.X_op, ex.X_add_number); */
1948
1949               switch (ex.X_op) 
1950                 {
1951                 case O_illegal:
1952                   errmsg = "illegal operand";
1953                   goto error;
1954                 case O_absent:
1955                   errmsg = "missing operand";
1956                   goto error;
1957                 case O_register:
1958                   if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
1959                     {
1960                       errmsg = "invalid operand";
1961                       goto error;
1962                     }
1963                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
1964                                               (char *) NULL, 0, copy_of_instruction);
1965                   break;
1966
1967                 case O_constant:
1968                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
1969                                               (char *) NULL, 0, copy_of_instruction);
1970                   break;
1971
1972                 default:
1973                   /* We need to generate a fixup for this expression.  */
1974                   if (fc >= MAX_INSN_FIXUPS)
1975                     as_fatal ("too many fixups");
1976
1977                   fixups[ fc ].exp     = ex;
1978                   fixups[ fc ].opindex = * opindex_ptr;
1979                   fixups[ fc ].reloc   = BFD_RELOC_UNUSED;
1980                   ++fc;
1981                   break;
1982                 }
1983             }
1984
1985           str = input_line_pointer;
1986           input_line_pointer = hold;
1987
1988           while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
1989                  || *str == ')')
1990             ++str;
1991         }
1992       match = 1;
1993
1994     error:
1995       if (match == 0)
1996         {
1997           next_opcode = opcode + 1;
1998           if (next_opcode->name != NULL && strcmp (next_opcode->name, opcode->name) == 0)
1999             {
2000               opcode = next_opcode;
2001               continue;
2002             }
2003           
2004           as_bad (errmsg);
2005           ignore_rest_of_line ();
2006           input_line_pointer = saved_input_line_pointer;
2007           return;
2008         }
2009       break;
2010     }
2011       
2012   while (isspace (*str))
2013     ++str;
2014
2015   if (*str != '\0')
2016     as_bad ("junk at end of line: `%s'", str);
2017
2018   input_line_pointer = str;
2019
2020   /* Write out the instruction. */
2021   
2022   if (relaxable && fc > 0)
2023     {
2024       insn_size = 2;
2025       fc = 0;
2026
2027       if (!strcmp (opcode->name, "br"))
2028         {
2029           f = frag_var (rs_machine_dependent, 4, 2, 2,
2030                         fixups[0].exp.X_add_symbol,
2031                         fixups[0].exp.X_add_number,
2032                         (char *)fixups[0].opindex);
2033           md_number_to_chars (f, insn, insn_size);
2034           md_number_to_chars (f + 2, 0, 2);
2035         }
2036       else
2037         {
2038           f = frag_var (rs_machine_dependent, 6, 4, 0,
2039                         fixups[0].exp.X_add_symbol,
2040                         fixups[0].exp.X_add_number,
2041                         (char *)fixups[0].opindex);
2042           md_number_to_chars (f, insn, insn_size);
2043           md_number_to_chars (f + 2, 0, 4);
2044         }
2045     }
2046   else 
2047     {
2048       /* Four byte insns have an opcode with the two high bits on.  */
2049       if ((insn & 0x0600) == 0x0600)
2050         insn_size = 4;
2051       else
2052         insn_size = 2;
2053
2054 /* start-sanitize-v850e */
2055       /* Special case: 32 bit MOV */
2056       if ((insn & 0xffe0) == 0x0620)
2057         insn_size = 2;
2058 /* end-sanitize-v850e */
2059       
2060       f = frag_more (insn_size);
2061       
2062       md_number_to_chars (f, insn, insn_size);
2063
2064       if (extra_data_after_insn)
2065         {
2066           f = frag_more (extra_data_len);
2067           
2068           md_number_to_chars (f, extra_data, extra_data_len);
2069
2070           extra_data_after_insn = false;
2071         }
2072     }
2073
2074   /* Create any fixups.  At this point we do not use a
2075      bfd_reloc_code_real_type, but instead just use the
2076      BFD_RELOC_UNUSED plus the operand index.  This lets us easily
2077      handle fixups for any operand type, although that is admittedly
2078      not a very exciting feature.  We pick a BFD reloc type in
2079      md_apply_fix.  */  
2080   for (i = 0; i < fc; i++)
2081     {
2082       const struct v850_operand * operand;
2083       bfd_reloc_code_real_type    reloc;
2084       
2085       operand = & v850_operands[ fixups[i].opindex ];
2086
2087       reloc = fixups[i].reloc;
2088       
2089       if (reloc != BFD_RELOC_UNUSED)
2090         {
2091           reloc_howto_type * reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc);
2092           int                size;
2093           int                address;
2094           fixS *             fixP;
2095
2096           if (!reloc_howto)
2097             abort();
2098           
2099           size = bfd_get_reloc_size (reloc_howto);
2100
2101           if (size != 2 && size != 4) /* XXX this will abort on an R_V850_8 reloc - is this reloc actually used ? */
2102             abort();
2103
2104           address = (f - frag_now->fr_literal) + insn_size - size;
2105
2106           if (reloc == BFD_RELOC_32)
2107             {
2108               address += 2;
2109             }
2110           
2111           fixP = fix_new_exp (frag_now, address, size,
2112                               & fixups[i].exp, 
2113                               reloc_howto->pc_relative,
2114                               reloc);
2115
2116           switch (reloc)
2117             {
2118             case BFD_RELOC_LO16:
2119             case BFD_RELOC_HI16:
2120             case BFD_RELOC_HI16_S:
2121               fixP->fx_no_overflow = 1;
2122               break;
2123             }
2124         }
2125       else
2126         {
2127           fix_new_exp (
2128                        frag_now,
2129                        f - frag_now->fr_literal, 4,
2130                        & fixups[i].exp,
2131                        1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
2132                        (bfd_reloc_code_real_type) (fixups[i].opindex + (int) BFD_RELOC_UNUSED)
2133                        );
2134         }
2135     }
2136
2137   input_line_pointer = saved_input_line_pointer;
2138 }
2139
2140
2141 /* If while processing a fixup, a reloc really needs to be created */
2142 /* then it is done here.  */
2143                  
2144 arelent *
2145 tc_gen_reloc (seg, fixp)
2146      asection * seg;
2147      fixS *     fixp;
2148 {
2149   arelent * reloc;
2150   
2151   reloc              = (arelent *) xmalloc (sizeof (arelent));
2152   reloc->sym_ptr_ptr = & fixp->fx_addsy->bsym;
2153   reloc->address     = fixp->fx_frag->fr_address + fixp->fx_where;
2154   reloc->howto       = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2155
2156   if (reloc->howto == (reloc_howto_type *) NULL)
2157     {
2158       as_bad_where (fixp->fx_file, fixp->fx_line,
2159                     "reloc %d not supported by object file format", (int)fixp->fx_r_type);
2160
2161       xfree (reloc);
2162       
2163       return NULL;
2164     }
2165   
2166   reloc->addend = fixp->fx_addnumber;
2167   
2168   return reloc;
2169 }
2170
2171 /* Assume everything will fit in two bytes, then expand as necessary.  */
2172 int
2173 md_estimate_size_before_relax (fragp, seg)
2174      fragS * fragp;
2175      asection * seg;
2176 {
2177   if (fragp->fr_subtype == 0)
2178     fragp->fr_var = 4;
2179   else if (fragp->fr_subtype == 2)
2180     fragp->fr_var = 2;
2181   else
2182     abort ();
2183   return 2;
2184
2185
2186 long
2187 md_pcrel_from (fixp)
2188      fixS * fixp;
2189 {
2190   /* If the symbol is undefined, or in a section other than our own,
2191      then let the linker figure it out.  */
2192   if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
2193     {
2194       /* The symbol is undefined.  Let the linker figure it out.  */
2195       return 0;
2196     }
2197   return fixp->fx_frag->fr_address + fixp->fx_where;
2198 }
2199
2200 int
2201 md_apply_fix3 (fixp, valuep, seg)
2202      fixS *   fixp;
2203      valueT * valuep;
2204      segT     seg;
2205 {
2206   valueT value;
2207   char * where;
2208
2209   if (fixp->fx_addsy == (symbolS *) NULL)
2210     {
2211       value = * valuep;
2212       fixp->fx_done = 1;
2213     }
2214   else if (fixp->fx_pcrel)
2215     value = * valuep;
2216   else
2217     {
2218       value = fixp->fx_offset;
2219       if (fixp->fx_subsy != (symbolS *) NULL)
2220         {
2221           if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
2222             value -= S_GET_VALUE (fixp->fx_subsy);
2223           else
2224             {
2225               /* We don't actually support subtracting a symbol.  */
2226               as_bad_where (fixp->fx_file, fixp->fx_line,
2227                             "expression too complex");
2228             }
2229         }
2230     }
2231
2232   if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
2233     {
2234       int                         opindex;
2235       const struct v850_operand * operand;
2236       unsigned long               insn;
2237
2238       opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
2239       operand = & v850_operands[ opindex ];
2240
2241       /* Fetch the instruction, insert the fully resolved operand
2242          value, and stuff the instruction back again.
2243
2244          Note the instruction has been stored in little endian
2245          format!  */
2246       where = fixp->fx_frag->fr_literal + fixp->fx_where;
2247
2248       insn = bfd_getl32 ((unsigned char *) where);
2249       insn = v850_insert_operand (insn, operand, (offsetT) value,
2250                                   fixp->fx_file, fixp->fx_line, NULL);
2251       bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
2252
2253       if (fixp->fx_done)
2254         {
2255           /* Nothing else to do here. */
2256           return 1;
2257         }
2258
2259       /* Determine a BFD reloc value based on the operand information.  
2260          We are only prepared to turn a few of the operands into relocs. */
2261
2262       if (operand->bits == 22)
2263         fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
2264       else if (operand->bits == 9)
2265         fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
2266       else
2267         {
2268           /* fprintf (stderr, "bits: %d, insn: %x\n", operand->bits, insn); */
2269           
2270           as_bad_where(fixp->fx_file, fixp->fx_line,
2271                        "unresolved expression that must be resolved");
2272           fixp->fx_done = 1;
2273           return 1;
2274         }
2275     }
2276   else if (fixp->fx_done)
2277     {
2278       /* We still have to insert the value into memory!  */
2279       where = fixp->fx_frag->fr_literal + fixp->fx_where;
2280
2281       if (fixp->fx_size == 1)
2282         *where = value & 0xff;
2283       else if (fixp->fx_size == 2)
2284         bfd_putl16 (value & 0xffff, (unsigned char *) where);
2285       else if (fixp->fx_size == 4)
2286         bfd_putl32 (value, (unsigned char *) where);
2287     }
2288   
2289   fixp->fx_addnumber = value;
2290   return 1;
2291 }
2292
2293 \f
2294 /* Parse a cons expression.  We have to handle hi(), lo(), etc
2295    on the v850.  */
2296 void
2297 parse_cons_expression_v850 (exp)
2298   expressionS *exp;
2299 {
2300   /* See if there's a reloc prefix like hi() we have to handle.  */
2301   hold_cons_reloc = v850_reloc_prefix (NULL);
2302
2303   /* Do normal expression parsing.  */
2304   expression (exp);
2305 }
2306
2307 /* Create a fixup for a cons expression.  If parse_cons_expression_v850
2308    found a reloc prefix, then we use that reloc, else we choose an
2309    appropriate one based on the size of the expression.  */
2310 void
2311 cons_fix_new_v850 (frag, where, size, exp)
2312      fragS *frag;
2313      int where;
2314      int size;
2315      expressionS *exp;
2316 {
2317   if (hold_cons_reloc == BFD_RELOC_UNUSED)
2318     {
2319       if (size == 4)
2320         hold_cons_reloc = BFD_RELOC_32;
2321       if (size == 2)
2322         hold_cons_reloc = BFD_RELOC_16;
2323       if (size == 1)
2324         hold_cons_reloc = BFD_RELOC_8;
2325     }
2326
2327   if (exp != NULL)
2328     fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
2329   else
2330     fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
2331 }