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