Removed the BFD_RELOC_V850_16_PCREL.
[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 /* sign-extend a 16-bit number */
28 #define SEXT16(x)       ((((x) & 0xffff) ^ (~ 0x7fff)) + 0x8000)
29
30 /* Temporarily holds the reloc in a cons expression.  */
31 static bfd_reloc_code_real_type hold_cons_reloc;
32
33 /* Set to TRUE if we want to be pedantic about signed overflows.  */
34 static boolean warn_signed_overflows = FALSE;
35
36 \f
37 /* Structure to hold information about predefined registers.  */
38 struct reg_name
39 {
40   const char * name;
41   int          value;
42 };
43
44 /* Generic assembler global variables which must be defined by all targets. */
45
46 /* Characters which always start a comment. */
47 const char comment_chars[] = "#";
48
49 /* Characters which start a comment at the beginning of a line.  */
50 const char line_comment_chars[] = ";#";
51
52 /* Characters which may be used to separate multiple commands on a 
53    single line.  */
54 const char line_separator_chars[] = ";";
55
56 /* Characters which are used to indicate an exponent in a floating 
57    point number.  */
58 const char EXP_CHARS[] = "eE";
59
60 /* Characters which mean that a number is a floating point constant, 
61    as in 0d1.0.  */
62 const char FLT_CHARS[] = "dD";
63 \f
64
65 const relax_typeS md_relax_table[] = {
66   {0xff, -0x100, 2, 1},
67   {0x1fffff, -0x200000, 6, 0},
68 };
69
70
71 static segT sdata_section = NULL;
72 static segT tdata_section = NULL;
73 static segT zdata_section = NULL;
74 static segT sbss_section = NULL;
75 static segT tbss_section = NULL;
76 static segT zbss_section = NULL;
77 static segT rosdata_section = NULL;
78 static segT rozdata_section = NULL;
79
80
81 /* local functions */
82 static unsigned long v850_insert_operand
83   PARAMS ((unsigned long insn, const struct v850_operand *operand,
84            offsetT val, char *file, unsigned int line));
85
86
87 /* fixups */
88 #define MAX_INSN_FIXUPS (5)
89 struct v850_fixup
90 {
91   expressionS              exp;
92   int                      opindex;
93   bfd_reloc_code_real_type reloc;
94 };
95 struct v850_fixup fixups[MAX_INSN_FIXUPS];
96 static int fc;
97 \f
98 void
99 v850_sdata (int ignore)
100 {
101   subseg_set (sdata_section, (subsegT) get_absolute_expression ());
102   
103   demand_empty_rest_of_line ();
104 }
105
106 void
107 v850_tdata (int ignore)
108 {
109   subseg_set (tdata_section, (subsegT) get_absolute_expression ());
110   
111   demand_empty_rest_of_line ();
112 }
113
114 void
115 v850_zdata (int ignore)
116 {
117   subseg_set (zdata_section, (subsegT) get_absolute_expression ());
118   
119   demand_empty_rest_of_line ();
120 }
121
122 void
123 v850_sbss (int ignore)
124 {
125   subseg_set (sbss_section, (subsegT) get_absolute_expression ());
126   
127   demand_empty_rest_of_line ();
128 }
129
130 void
131 v850_tbss (int ignore)
132 {
133   subseg_set (tbss_section, (subsegT) get_absolute_expression ());
134   
135   demand_empty_rest_of_line ();
136 }
137
138 void
139 v850_zbss (int ignore)
140 {
141   subseg_set (zbss_section, (subsegT) get_absolute_expression ());
142   
143   demand_empty_rest_of_line ();
144 }
145
146 void
147 v850_rosdata (int ignore)
148 {
149   subseg_set (rosdata_section, (subsegT) get_absolute_expression ());
150   
151   demand_empty_rest_of_line ();
152 }
153
154 void
155 v850_rozdata (int ignore)
156 {
157   subseg_set (rozdata_section, (subsegT) get_absolute_expression ());
158   
159   demand_empty_rest_of_line ();
160 }
161
162 static void
163 v850_section (int arg)
164 {
165   char   saved_c;
166   char * ptr;
167   
168   for (ptr = input_line_pointer; * ptr != '\n' && * ptr != 0; ptr ++)
169     if (* ptr == ',' && ptr[1] == '.')
170       break;
171
172   saved_c = * ptr;
173   * ptr = ';';
174   
175   obj_elf_section (arg);
176
177   * ptr = saved_c;
178 }
179
180 void
181 v850_bss (int ignore)
182 {
183   register int temp = get_absolute_expression ();
184
185   obj_elf_section_change_hook();
186   
187   subseg_set (bss_section, (subsegT) temp);
188   
189   demand_empty_rest_of_line ();
190 }
191
192 void
193 v850_offset (int ignore)
194 {
195   int temp = get_absolute_expression ();
196   
197   temp -= frag_now_fix();
198   
199   if (temp > 0)
200     (void) frag_more (temp);
201   
202   demand_empty_rest_of_line ();
203 }
204
205 /* The target specific pseudo-ops which we support.  */
206 const pseudo_typeS md_pseudo_table[] =
207 {
208   {"sdata",   v850_sdata,   0},
209   {"tdata",   v850_tdata,   0},
210   {"zdata",   v850_zdata,   0},
211   {"sbss",    v850_sbss,    0},
212   {"tbss",    v850_tbss,    0},
213   {"zbss",    v850_zbss,    0},
214   {"rosdata", v850_rosdata, 0},
215   {"rozdata", v850_rozdata, 0},
216   {"bss",     v850_bss,     0},
217   {"offset",  v850_offset,  0},
218   {"section", v850_section, 0},
219   {"word",    cons,         4},
220   { NULL,     NULL,         0}
221 };
222
223 /* Opcode hash table.  */
224 static struct hash_control *v850_hash;
225
226 /* This table is sorted. Suitable for searching by a binary search. */
227 static const struct reg_name pre_defined_registers[] =
228 {
229   { "ep",  30 },                /* ep - element ptr */
230   { "gp",   4 },                /* gp - global ptr */
231   { "hp",   2 },                /* hp - handler stack ptr */
232   { "lp",  31 },                /* lp - link ptr */
233   { "r0",   0 },
234   { "r1",   1 },
235   { "r10", 10 },
236   { "r11", 11 },
237   { "r12", 12 },
238   { "r13", 13 },
239   { "r14", 14 },
240   { "r15", 15 },
241   { "r16", 16 },
242   { "r17", 17 },
243   { "r18", 18 },
244   { "r19", 19 },
245   { "r2",   2 },
246   { "r20", 20 },
247   { "r21", 21 },
248   { "r22", 22 },
249   { "r23", 23 },
250   { "r24", 24 },
251   { "r25", 25 },
252   { "r26", 26 },
253   { "r27", 27 },
254   { "r28", 28 },
255   { "r29", 29 },
256   { "r3",   3 },
257   { "r30", 30 },
258   { "r31", 31 },
259   { "r4",   4 },
260   { "r5",   5 },
261   { "r6",   6 },
262   { "r7",   7 },
263   { "r8",   8 },
264   { "r9",   9 },
265   { "sp",   3 },                /* sp - stack ptr */
266   { "tp",   5 },                /* tp - text ptr */
267   { "zero", 0 },
268 };
269 #define REG_NAME_CNT    (sizeof (pre_defined_registers) / sizeof (struct reg_name))
270
271
272 static const struct reg_name system_registers[] = 
273 {
274 /* start-sanitize-v850e */
275   { "ctbp",  20 },
276   { "ctpc",  16 },
277   { "ctpsw", 17 },
278   { "dbpc",  18 },
279   { "dbpsw", 19 },
280 /* end-sanitize-v850e */
281   { "ecr",    4 },
282   { "eipc",   0 },
283   { "eipsw",  1 },
284   { "fepc",   2 },
285   { "fepsw",  3 },
286   { "psw",    5 },
287 };
288 #define SYSREG_NAME_CNT (sizeof (system_registers) / sizeof (struct reg_name))
289
290 static const struct reg_name cc_names[] =
291 {
292   { "c",  0x1 },
293   { "e",  0x2 },
294   { "ge", 0xe },
295   { "gt", 0xf },
296   { "h",  0xb },
297   { "l",  0x1 },
298   { "le", 0x7 },
299   { "lt", 0x6 },
300   { "n",  0x4 },
301   { "nc", 0x9 },
302   { "ne", 0xa },
303   { "nh", 0x3 },
304   { "nl", 0x9 },
305   { "ns", 0xc },
306   { "nv", 0x8 },
307   { "nz", 0xa },
308   { "p",  0xc },
309   { "s",  0x4 },
310   { "sa", 0xd },
311   { "t",  0x5 },
312   { "v",  0x0 },
313   { "z",  0x2 },
314 };
315 #define CC_NAME_CNT     (sizeof(cc_names) / sizeof(struct reg_name))
316
317 /* reg_name_search does a binary search of the given register table
318    to see if "name" is a valid regiter name.  Returns the register
319    number from the array on success, or -1 on failure. */
320
321 static int
322 reg_name_search (regs, regcount, name)
323      const struct reg_name * regs;
324      int                     regcount;
325      const char *            name;
326 {
327   int middle, low, high;
328   int cmp;
329
330   low = 0;
331   high = regcount - 1;
332
333   do
334     {
335       middle = (low + high) / 2;
336       cmp = strcasecmp (name, regs[middle].name);
337       if (cmp < 0)
338         high = middle - 1;
339       else if (cmp > 0)
340         low = middle + 1;
341       else
342         return regs[middle].value;
343     }
344   while (low <= high);
345   return -1;
346 }
347
348
349 /* Summary of register_name().
350  *
351  * in: Input_line_pointer points to 1st char of operand.
352  *
353  * out: A expressionS.
354  *      The operand may have been a register: in this case, X_op == O_register,
355  *      X_add_number is set to the register number, and truth is returned.
356  *      Input_line_pointer->(next non-blank) char after operand, or is in
357  *      its original state.
358  */
359 static boolean
360 register_name (expressionP)
361      expressionS * expressionP;
362 {
363   int    reg_number;
364   char * name;
365   char * start;
366   char   c;
367
368   /* Find the spelling of the operand */
369   start = name = input_line_pointer;
370
371   c = get_symbol_end ();
372
373   reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
374
375   * input_line_pointer = c;     /* put back the delimiting char */
376   
377   /* look to see if it's in the register table */
378   if (reg_number >= 0) 
379     {
380       expressionP->X_op         = O_register;
381       expressionP->X_add_number = reg_number;
382
383       /* make the rest nice */
384       expressionP->X_add_symbol = NULL;
385       expressionP->X_op_symbol  = NULL;
386       
387       return true;
388     }
389   else
390     {
391       /* reset the line as if we had not done anything */
392       input_line_pointer = start;
393       
394       return false;
395     }
396 }
397
398 /* Summary of system_register_name().
399  *
400  * in: Input_line_pointer points to 1st char of operand.
401  *
402  * out: A expressionS.
403  *      The operand may have been a register: in this case, X_op == O_register,
404  *      X_add_number is set to the register number, and truth is returned.
405  *      Input_line_pointer->(next non-blank) char after operand, or is in
406  *      its original state.
407  */
408 static boolean
409 system_register_name (expressionP, accept_numbers)
410      expressionS * expressionP;
411      boolean       accept_numbers;
412 {
413   int    reg_number;
414   char * name;
415   char * start;
416   char   c;
417
418   /* Find the spelling of the operand */
419   start = name = input_line_pointer;
420
421   c = get_symbol_end ();
422   reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
423
424   * input_line_pointer = c;   /* put back the delimiting char */
425   
426   if (reg_number < 0
427       && accept_numbers)
428     {
429       input_line_pointer   = start; /* reset input_line pointer */
430
431       if (isdigit (* input_line_pointer))
432         reg_number = strtol (input_line_pointer, & input_line_pointer, 10);
433
434       /* Make sure that the register number is allowable. */
435       if (   reg_number < 0
436           || reg_number > 5
437 /* start-sanitize-v850e */
438           && reg_number < 16
439           || reg_number > 20
440 /* end-sanitize-v850e */
441              )
442         {
443           reg_number = -1;
444         }
445     }
446       
447   /* look to see if it's in the register table */
448   if (reg_number >= 0) 
449     {
450       expressionP->X_op         = O_register;
451       expressionP->X_add_number = reg_number;
452
453       /* make the rest nice */
454       expressionP->X_add_symbol = NULL;
455       expressionP->X_op_symbol  = NULL;
456
457       return true;
458     }
459   else
460     {
461       /* reset the line as if we had not done anything */
462       input_line_pointer = start;
463       
464       return false;
465     }
466 }
467
468 /* Summary of cc_name().
469  *
470  * in: Input_line_pointer points to 1st char of operand.
471  *
472  * out: A expressionS.
473  *      The operand may have been a register: in this case, X_op == O_register,
474  *      X_add_number is set to the register number, and truth is returned.
475  *      Input_line_pointer->(next non-blank) char after operand, or is in
476  *      its original state.
477  */
478 static boolean
479 cc_name (expressionP)
480      expressionS *expressionP;
481 {
482   int    reg_number;
483   char * name;
484   char * start;
485   char   c;
486
487   /* Find the spelling of the operand */
488   start = name = input_line_pointer;
489
490   c = get_symbol_end ();
491   reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
492
493   * input_line_pointer = c;   /* put back the delimiting char */
494   
495   /* look to see if it's in the register table */
496   if (reg_number >= 0) 
497     {
498       expressionP->X_op         = O_constant;
499       expressionP->X_add_number = reg_number;
500
501       /* make the rest nice */
502       expressionP->X_add_symbol = NULL;
503       expressionP->X_op_symbol  = NULL;
504
505       return true;
506     }
507   else
508     {
509       /* reset the line as if we had not done anything */
510       input_line_pointer = start;
511       
512       return false;
513     }
514 }
515
516 static void
517 skip_white_space (void)
518 {
519   while (   * input_line_pointer == ' '
520          || * input_line_pointer == '\t')
521     ++ input_line_pointer;
522 }
523
524 /* start-sanitize-v850e */
525 /* Summary of parse_register_list ().
526  *
527  * in: Input_line_pointer  points to 1st char of a list of registers.
528  *     insn                is the partially constructed instruction.
529  *     operand             is the operand being inserted.
530  *
531  * out: True if the parse completed successfully, False otherwise.
532  *      If the parse completes the correct bit fields in the
533  *      instruction will be filled in.
534  *
535  * Parses register lists with the syntax:
536  *
537  *   { rX }
538  *   { rX, rY }
539  *   { rX - rY }
540  *   { rX - rY, rZ }
541  *   etc
542  *
543  * and also parses constant epxressions whoes bits indicate the
544  * registers in the lists.  The LSB in the expression refers to
545  * the lowest numbered permissable register in the register list,
546  * and so on upwards.  System registers are considered to be very
547  * high numbers.
548  * 
549  */
550 static char *
551 parse_register_list
552 (
553   unsigned long *             insn,
554   const struct v850_operand * operand
555 )
556 {
557   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 };
558 /* start-sanitize-v850eq */
559   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 };
560   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 };
561 /* end-sanitize-v850eq */
562   int *       regs;
563   expressionS exp;
564
565
566   /* Select a register array to parse. */
567   switch (operand->shift)
568     {
569     case 0xffe00001: regs = type1_regs; break;
570 /* start-sanitize-v850eq */
571     case 0xfff8000f: regs = type2_regs; break;
572     case 0xfff8001f: regs = type3_regs; break;
573 /* end-sanitize-v850eq */
574     default:
575       as_bad ("unknown operand shift: %x\n", operand->shift );              
576       return "internal failure in parse_register_list";
577     }
578
579   skip_white_space();
580
581   /* If the expression starts with a curly brace it is a register list.
582      Otherwise it is a constant expression ,whoes bits indicate which
583      registers are to be included in the list.  */
584   
585   if (* input_line_pointer != '{')
586     {
587       int bits;
588       int reg;
589       int i;
590                 
591       expression (& exp);
592       
593       if (exp.X_op != O_constant)
594         return "constant expression or register list expected";
595
596 /* start-sanitize-v850eq */
597       if (regs == type1_regs)
598 /* end-sanitize-v850eq */
599         {
600           if (exp.X_add_number & 0xFFFFF000)
601             return "high bits set in register list expression";
602           
603           for (reg = 20; reg < 32; reg ++)
604             if (exp.X_add_number & (1 << (reg - 20)))
605               {
606                 for (i = 0; i < 32; i++)
607                   if (regs[i] == reg)
608                     * insn |= (1 << i);
609               }
610         }
611 /* start-sanitize-v850eq */
612       else if (regs == type2_regs)
613         {
614           if (exp.X_add_number & 0xFFFE0000)
615             return "high bits set in register list expression";
616           
617           for (reg = 1; reg < 16; reg ++)
618             if (exp.X_add_number & (1 << (reg - 1)))
619               {
620                 for (i = 0; i < 32; i++)
621                   if (regs[i] == reg)
622                     * insn |= (1 << i);
623               }
624
625           if (exp.X_add_number & (1 << 15))
626             * insn |= (1 << 3);
627           
628           if (exp.X_add_number & (1 << 16))
629             * insn |= (1 << 19);
630         }
631       else /* regs == type3_regs */
632         {
633           if (exp.X_add_number & 0xFFFE0000)
634             return "high bits set in register list expression";
635           
636           for (reg = 16; reg < 32; reg ++)
637             if (exp.X_add_number & (1 << (reg - 16)))
638               {
639                 for (i = 0; i < 32; i++)
640                   if (regs[i] == reg)
641                     * insn |= (1 << i);
642               }
643
644           if (exp.X_add_number & (1 << 16))
645             * insn |= (1 << 19);
646         }
647 /* end-sanitize-v850eq */
648
649       return NULL;
650     }
651
652   input_line_pointer ++;
653
654   /* Parse the register list until a terminator (closing curly brace or new-line) is found.  */
655   for (;;)
656     {
657       if (register_name (& exp))
658         {
659           int  i;
660           
661           /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction.  */
662           for (i = 0; i < 32; i++)
663             {
664               if (regs[ i ] == exp.X_add_number)
665                 {
666                   * insn |= (1 << i);
667                   break;
668                 }
669             }
670
671           if (i == 32)
672             {
673               return "illegal register included in list";
674             }
675         }
676       else if (system_register_name (& exp, true))
677         {
678           if (regs == type1_regs)
679             {
680               return "system registers cannot be included in list";
681             }
682           else if (exp.X_add_number == 5)
683             {
684               if (regs == type2_regs)
685                 return "PSW cannot be included in list";
686               else
687                 * insn |= 0x8;
688             }
689           else
690             * insn |= 0x80000;
691         }
692       else if (* input_line_pointer == '}')
693         {
694           input_line_pointer ++;
695           break;
696         }
697       else if (* input_line_pointer == ',')
698         {
699           input_line_pointer ++;
700           continue;
701         }
702       else if (* input_line_pointer == '-')
703         {
704           /* We have encountered a range of registers: rX - rY */
705           int         j;
706           expressionS exp2;
707
708           /* Skip the dash.  */
709           ++ input_line_pointer;
710
711           /* Get the second register in the range.  */
712           if (! register_name (& exp2))
713             {
714               return "second register should follow dash in register list";
715               exp2.X_add_number = exp.X_add_number;
716             }
717
718           /* Add the rest of the registers in the range.  */
719           for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
720             {
721               int  i;
722           
723               /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction.  */
724               for (i = 0; i < 32; i++)
725                 {
726                   if (regs[ i ] == j)
727                     {
728                       * insn |= (1 << i);
729                       break;
730                     }
731                 }
732
733               if (i == 32)
734                 {
735                   return "illegal register included in list";
736                 }
737             }
738         }
739       else
740         {
741           break;
742         }
743
744       skip_white_space();
745     }
746
747   return NULL;
748 }
749 /* end-sanitize-v850e */
750
751 CONST char * md_shortopts = "m:";
752
753 struct option md_longopts[] =
754 {
755   {NULL, no_argument, NULL, 0}
756 };
757 size_t md_longopts_size = sizeof md_longopts; 
758
759
760 void
761 md_show_usage (stream)
762   FILE *stream;
763 {
764   fprintf (stream, "V850 options:\n");
765   fprintf (stream, "\t-wsigned_overflow  Warn if signed immediate values overflow\n");
766
767
768 int
769 md_parse_option (c, arg)
770      int    c;
771      char * arg;
772 {
773   if (c == 'w' && strcmp (arg, "signed_overflow") == 0)
774     {
775       warn_signed_overflows = TRUE;
776       return 1;
777     }
778
779   return 0;
780 }
781
782 symbolS *
783 md_undefined_symbol (name)
784   char * name;
785 {
786   return 0;
787 }
788
789 char *
790 md_atof (type, litp, sizep)
791   int    type;
792   char * litp;
793   int *  sizep;
794 {
795   int            prec;
796   LITTLENUM_TYPE words[4];
797   char *         t;
798   int            i;
799
800   switch (type)
801     {
802     case 'f':
803       prec = 2;
804       break;
805
806     case 'd':
807       prec = 4;
808       break;
809
810     default:
811       *sizep = 0;
812       return "bad call to md_atof";
813     }
814   
815   t = atof_ieee (input_line_pointer, type, words);
816   if (t)
817     input_line_pointer = t;
818
819   *sizep = prec * 2;
820
821   for (i = prec - 1; i >= 0; i--)
822     {
823       md_number_to_chars (litp, (valueT) words[i], 2);
824       litp += 2;
825     }
826
827   return NULL;
828 }
829
830
831 /* Very gross.  */
832 void
833 md_convert_frag (abfd, sec, fragP)
834   bfd *      abfd;
835   asection * sec;
836   fragS *    fragP;
837 {
838   subseg_change (sec, 0);
839   if (fragP->fr_subtype == 0)
840     {
841       fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
842                fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
843       fragP->fr_var = 0;
844       fragP->fr_fix += 2;
845     }
846   else if (fragP->fr_subtype == 1)
847     {
848       /* Reverse the condition of the first branch.  */
849       fragP->fr_literal[0] &= 0xf7;
850       /* Mask off all the displacement bits.  */
851       fragP->fr_literal[0] &= 0x8f;
852       fragP->fr_literal[1] &= 0x07;
853       /* Now set the displacement bits so that we branch
854          around the unconditional branch.  */
855       fragP->fr_literal[0] |= 0x30;
856
857       /* Now create the unconditional branch + fixup to the final
858          target.  */
859       md_number_to_chars (&fragP->fr_literal[2], 0x00000780, 4);
860       fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
861                fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
862       fragP->fr_var = 0;
863       fragP->fr_fix += 6;
864     }
865   else
866     abort ();
867 }
868
869 valueT
870 md_section_align (seg, addr)
871      asection * seg;
872      valueT     addr;
873 {
874   int align = bfd_get_section_alignment (stdoutput, seg);
875   return ((addr + (1 << align) - 1) & (-1 << align));
876 }
877
878 void
879 md_begin ()
880 {
881   char *                              prev_name = "";
882   register const struct v850_opcode * op;
883   flagword                            applicable;
884
885   
886   v850_hash = hash_new();
887
888   /* Insert unique names into hash table.  The V850 instruction set
889      has many identical opcode names that have different opcodes based
890      on the operands.  This hash table then provides a quick index to
891      the first opcode with a particular name in the opcode table.  */
892
893   op = v850_opcodes;
894   while (op->name)
895     {
896       if (strcmp (prev_name, op->name)) 
897         {
898           prev_name = (char *) op->name;
899           hash_insert (v850_hash, op->name, (char *) op);
900         }
901       op++;
902     }
903   
904   bfd_set_arch_mach (stdoutput, TARGET_ARCH, 0);
905   /* start-sanitize-v850e */
906   bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_v850e);
907   /* end-sanitize-v850e */
908   /* start-sanitize-v850eq */
909   bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_v850eq);
910   /* end-sanitize-v850eq */
911
912   applicable = bfd_applicable_section_flags (stdoutput);
913
914   sdata_section = subseg_new (".sdata", 0);
915   bfd_set_section_flags (stdoutput, sdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
916   
917   tdata_section = subseg_new (".tdata", 0);
918   bfd_set_section_flags (stdoutput, tdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
919   
920   zdata_section = subseg_new (".zdata", 0);
921   bfd_set_section_flags (stdoutput, zdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
922   
923   sbss_section = subseg_new (".sbss", 0);
924   bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
925   
926   tbss_section = subseg_new (".tbss", 0);
927   bfd_set_section_flags (stdoutput, tbss_section, applicable & SEC_ALLOC);
928   
929   zbss_section = subseg_new (".zbss", 0);
930   bfd_set_section_flags (stdoutput, zbss_section, applicable & SEC_ALLOC);
931   
932   rosdata_section = subseg_new (".rosdata", 0);
933   bfd_set_section_flags (stdoutput, rosdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY));
934                          
935   rozdata_section = subseg_new (".rozdata", 0);
936   bfd_set_section_flags (stdoutput, rozdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY));
937 }
938
939
940 /* Warning: The code in this function relies upon the definitions
941    in the v850_operands[] array (defined in opcodes/v850-opc.c)
942    matching the hard coded values conatined herein.  */
943
944 static bfd_reloc_code_real_type
945 v850_reloc_prefix (const struct v850_operand * operand)
946 {
947   boolean paren_skipped = false;
948
949
950   /* Skip leading opening parenthesis.  */
951   if (* input_line_pointer == '(')
952     {
953       ++ input_line_pointer;
954       paren_skipped = true;
955     }
956   
957   if (strncmp (input_line_pointer, "hi0(", 4) == 0)
958     {
959       input_line_pointer += 3;
960       return BFD_RELOC_HI16;
961     }
962   if (strncmp (input_line_pointer, "hi(", 3) == 0)
963     {
964       input_line_pointer += 2;
965       return BFD_RELOC_HI16_S;
966     }
967   if (strncmp (input_line_pointer, "lo(", 3) == 0)
968     {
969       input_line_pointer += 2;
970       return BFD_RELOC_LO16;
971     }
972
973   if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
974     {
975       input_line_pointer += 6;
976       
977       if (operand == NULL)                             return BFD_RELOC_V850_SDA_16_16_OFFSET;
978       if (operand->bits == 15 && operand->shift == 17) return BFD_RELOC_V850_SDA_15_16_OFFSET;
979       /* start-sanitize-v850e */
980       if (operand->bits == -1)                         return BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET;
981       /* end-sanitize-v850e */
982       
983       assert (operand->bits == 16);
984       assert (operand->shift == 16);
985       
986       return BFD_RELOC_V850_SDA_16_16_OFFSET;
987     }
988       
989   if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
990     {
991       input_line_pointer += 6;
992       
993       if (operand == NULL)                             return BFD_RELOC_V850_ZDA_16_16_OFFSET;
994       if (operand->bits == 15 && operand->shift == 17) return BFD_RELOC_V850_ZDA_15_16_OFFSET;
995       /* start-sanitize-v850e */
996       if (operand->bits == -1)                         return BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET;
997       /* end-sanitize-v850e */
998       
999       assert (operand->bits == 16);
1000       assert (operand->shift == 16);
1001       
1002       return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1003     }
1004   
1005   if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
1006     {
1007       input_line_pointer += 6;
1008       
1009       if (operand == NULL)                               return BFD_RELOC_V850_TDA_7_7_OFFSET;
1010       if (operand->bits == 6 && operand->shift == 1)     return BFD_RELOC_V850_TDA_6_8_OFFSET;
1011       /* start-sanitize-v850e */
1012       if (operand->bits == 4 && operand->insert != NULL) return BFD_RELOC_V850_TDA_4_5_OFFSET;
1013       if (operand->bits == 4 && operand->insert == NULL) return BFD_RELOC_V850_TDA_4_4_OFFSET;
1014       /* end-sanitize-v850e */
1015       
1016       assert (operand->bits == 7);
1017       
1018       return  operand->insert != NULL ? BFD_RELOC_V850_TDA_7_8_OFFSET :  BFD_RELOC_V850_TDA_7_7_OFFSET;
1019     }
1020
1021   if (paren_skipped)
1022     /* Restore skipped character.  */
1023     -- input_line_pointer;
1024   
1025   return BFD_RELOC_UNUSED;
1026 }
1027
1028 void
1029 md_assemble (str) 
1030      char * str;
1031 {
1032   char *                    s;
1033   char *                    start_of_operands;
1034   struct v850_opcode *      opcode;
1035   struct v850_opcode *      next_opcode;
1036   const unsigned char *     opindex_ptr;
1037   int                       next_opindex;
1038   int                       relaxable;
1039   unsigned long             insn;
1040   unsigned long             insn_size;
1041   char *                    f;
1042   int                       i;
1043   int                       match;
1044   boolean                   extra_data_after_insn = false;
1045   unsigned                  extra_data_len;
1046   unsigned long             extra_data;
1047   char *                    saved_input_line_pointer;
1048   
1049   /* Get the opcode.  */
1050   for (s = str; *s != '\0' && ! isspace (*s); s++)
1051     continue;
1052   
1053   if (*s != '\0')
1054     *s++ = '\0';
1055
1056   /* find the first opcode with the proper name */
1057   opcode = (struct v850_opcode *)hash_find (v850_hash, str);
1058   if (opcode == NULL)
1059     {
1060       as_bad ("Unrecognized opcode: `%s'", str);
1061       ignore_rest_of_line ();
1062       return;
1063     }
1064
1065   str = s;
1066   while (isspace (* str))
1067     ++ str;
1068
1069   start_of_operands = str;
1070
1071   saved_input_line_pointer = input_line_pointer;
1072   
1073   for (;;)
1074     {
1075       const char * errmsg = NULL;
1076
1077       relaxable = 0;
1078       fc = 0;
1079       match = 0;
1080       next_opindex = 0;
1081       insn = opcode->opcode;
1082       extra_data_after_insn = false;
1083
1084       input_line_pointer = str = start_of_operands;
1085
1086       for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1087         {
1088           const struct v850_operand * operand;
1089           char *                      hold;
1090           expressionS                 ex;
1091           bfd_reloc_code_real_type    reloc;
1092
1093           if (next_opindex == 0)
1094             {
1095               operand = & v850_operands[ * opindex_ptr ];
1096             }
1097           else
1098             {
1099               operand      = & v850_operands[ next_opindex ];
1100               next_opindex = 0;
1101             }
1102
1103           errmsg = NULL;
1104
1105           while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1106             ++str;
1107
1108           if (operand->flags & V850_OPERAND_RELAX)
1109             relaxable = 1;
1110
1111           /* Gather the operand. */
1112           hold = input_line_pointer;
1113           input_line_pointer = str;
1114           
1115 /* fprintf (stderr, "operand: %s   index = %d, opcode = %s\n", input_line_pointer, opindex_ptr - opcode->operands, opcode->name ); */
1116
1117           /* lo(), hi(), hi0(), etc... */
1118           if ((reloc = v850_reloc_prefix (operand)) != BFD_RELOC_UNUSED)
1119             {
1120               expression (& ex);
1121
1122               if (ex.X_op == O_constant)
1123                 {
1124                   switch (reloc)
1125                     {
1126                     case BFD_RELOC_LO16:
1127                       {
1128                         /* Truncate, then sign extend the value.  */
1129                         ex.X_add_number = SEXT16 (ex.X_add_number);
1130                         break;
1131                       }
1132
1133                     case BFD_RELOC_HI16:
1134                       {
1135                         /* Truncate, then sign extend the value.  */
1136                         ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
1137                         break;
1138                       }
1139
1140                     case BFD_RELOC_HI16_S:
1141                       {
1142                         /* Truncate, then sign extend the value.  */
1143                         int temp = (ex.X_add_number >> 16) & 0xffff;
1144
1145                         temp += (ex.X_add_number >> 15) & 1;
1146
1147                         ex.X_add_number = SEXT16 (temp);
1148                         break;
1149                       }
1150
1151                     default:
1152                       as_bad ( "AAARG -> unhandled constant reloc");
1153                       break;
1154                     }
1155
1156                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
1157                                               (char *) NULL, 0);
1158                 }
1159               else
1160                 {
1161                   if (fc > MAX_INSN_FIXUPS)
1162                     as_fatal ("too many fixups");
1163
1164                   fixups[ fc ].exp     = ex;
1165                   fixups[ fc ].opindex = * opindex_ptr;
1166                   fixups[ fc ].reloc   = reloc;
1167                   fc++;
1168                 }
1169             }
1170           else
1171             {
1172               errmsg = NULL;
1173               
1174               if ((operand->flags & V850_OPERAND_REG) != 0) 
1175                 {
1176                   if (!register_name (& ex))
1177                     {
1178                       errmsg = "invalid register name";
1179                     }
1180
1181                   if ((operand->flags & V850_NOT_R0)
1182                       && ex.X_add_number == 0)
1183                     {
1184                       errmsg = "register r0 cannot be used here";
1185                     }
1186                 }
1187               else if ((operand->flags & V850_OPERAND_SRG) != 0) 
1188                 {
1189                   if (!system_register_name (& ex, true))
1190                     {
1191                       errmsg = "invalid system register name";
1192                     }
1193                 }
1194               else if ((operand->flags & V850_OPERAND_EP) != 0)
1195                 {
1196                   char * start = input_line_pointer;
1197                   char   c     = get_symbol_end ();
1198                   
1199                   if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
1200                     {
1201                       /* Put things back the way we found them.  */
1202                       *input_line_pointer = c;
1203                       input_line_pointer = start;
1204                       errmsg = "expected EP register";
1205                       goto error;
1206                     }
1207                   
1208                   *input_line_pointer = c;
1209                   str = input_line_pointer;
1210                   input_line_pointer = hold;
1211               
1212                   while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1213                     ++str;
1214                   continue;
1215                 }
1216               else if ((operand->flags & V850_OPERAND_CC) != 0) 
1217                 {
1218                   if (!cc_name (& ex))
1219                     {
1220                       errmsg = "invalid condition code name";
1221                     }
1222                 }
1223               /* start-sanitize-v850e */
1224               else if (operand->flags & V850E_PUSH_POP) 
1225                 {
1226                   errmsg = parse_register_list (& insn, operand);
1227                   
1228                   /* The parse_register_list() function has already done everything, so fake a dummy expression.  */
1229                   ex.X_op         = O_constant;
1230                   ex.X_add_number = 0;
1231                 }
1232               /* end-sanitize-v850e */
1233               /* start-sanitize-v850e */
1234               else if (operand->flags & V850E_IMMEDIATE16) 
1235                 {
1236                   expression (& ex);
1237
1238                   if (ex.X_op != O_constant)
1239                     errmsg = "constant expression expected";
1240                   else if (ex.X_add_number & 0xffff0000)
1241                     {
1242                       if (ex.X_add_number & 0xffff)
1243                         errmsg = "constant too big to fit into instruction";
1244                       else if ((insn & 0x001fffc0) == 0x00130780)
1245                         ex.X_add_number >>= 16;
1246                       else
1247                         errmsg = "constant too big to fit into instruction";
1248                     }
1249                   
1250                   extra_data_after_insn = true;
1251                   extra_data_len        = 2;
1252                   extra_data            = ex.X_add_number;
1253                   ex.X_add_number       = 0;
1254                 }
1255               /* end-sanitize-v850e */
1256               /* start-sanitize-v850e */
1257               else if (operand->flags & V850E_IMMEDIATE32) 
1258                 {
1259                   expression (& ex);
1260                   
1261                   if (ex.X_op != O_constant)
1262                     errmsg = "constant expression expected";
1263                   
1264                   extra_data_after_insn = true;
1265                   extra_data_len        = 4;
1266                   extra_data            = ex.X_add_number;
1267                   ex.X_add_number       = 0;
1268                 }
1269               /* end-sanitize-v850e */
1270               else if (register_name (&ex)
1271                        && (operand->flags & V850_OPERAND_REG) == 0)
1272                 {
1273                   errmsg = "syntax error: register not expected";
1274                 }
1275               else if (system_register_name (& ex, false)
1276                        && (operand->flags & V850_OPERAND_SRG) == 0)
1277                 {
1278                   errmsg = "syntax error: system register not expected";
1279                 }
1280               else if (cc_name (&ex)
1281                        && (operand->flags & V850_OPERAND_CC) == 0)
1282                 {
1283                   errmsg = "syntax error: condition code not expected";
1284                 }
1285               else
1286                 {
1287                   expression (& ex);
1288 /* start-sanitize-v850e */
1289                   /* Special case:
1290                      If we are assembling a MOV instruction (or a CALLT.... :-)
1291                      and the immediate value does not fit into the bits available
1292                      then create a fake error so that the next MOV instruction
1293                       will be selected.  This one has a 32 bit immediate field.  */
1294
1295                   if (((insn & 0x07e0) == 0x0200)
1296                       && ex.X_op == O_constant
1297                       && (ex.X_add_number < (- (1 << (operand->bits - 1))) || ex.X_add_number > ((1 << operand->bits) - 1)))
1298                     errmsg = "use bigger instruction";
1299 /* end-sanitize-v850e */
1300                 }
1301
1302               if (errmsg)
1303                 goto error;
1304               
1305 /* fprintf (stderr, "insn: %x, operand %d, op: %d, add_number: %d\n", insn, opindex_ptr - opcode->operands, ex.X_op, ex.X_add_number ); */
1306
1307               switch (ex.X_op) 
1308                 {
1309                 case O_illegal:
1310                   errmsg = "illegal operand";
1311                   goto error;
1312                 case O_absent:
1313                   errmsg = "missing operand";
1314                   goto error;
1315                 case O_register:
1316                   if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
1317                     {
1318                       errmsg = "invalid operand";
1319                       goto error;
1320                     }
1321                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
1322                                               (char *) NULL, 0);
1323                   break;
1324
1325                 case O_constant:
1326                   insn = v850_insert_operand (insn, operand, ex.X_add_number,
1327                                               (char *) NULL, 0);
1328                   break;
1329
1330                 default:
1331                   /* We need to generate a fixup for this expression.  */
1332                   if (fc >= MAX_INSN_FIXUPS)
1333                     as_fatal ("too many fixups");
1334
1335                   fixups[ fc ].exp     = ex;
1336                   fixups[ fc ].opindex = * opindex_ptr;
1337                   fixups[ fc ].reloc   = BFD_RELOC_UNUSED;
1338                   ++fc;
1339                   break;
1340                 }
1341             }
1342
1343           str = input_line_pointer;
1344           input_line_pointer = hold;
1345
1346           while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
1347                  || *str == ')')
1348             ++str;
1349         }
1350       match = 1;
1351
1352     error:
1353       if (match == 0)
1354         {
1355           next_opcode = opcode + 1;
1356           if (next_opcode->name != NULL && strcmp (next_opcode->name, opcode->name) == 0)
1357             {
1358               opcode = next_opcode;
1359               continue;
1360             }
1361           
1362           as_bad (errmsg);
1363           ignore_rest_of_line ();
1364           input_line_pointer = saved_input_line_pointer;
1365           return;
1366         }
1367       break;
1368     }
1369       
1370   while (isspace (*str))
1371     ++str;
1372
1373   if (*str != '\0')
1374     as_bad ("junk at end of line: `%s'", str);
1375
1376   input_line_pointer = str;
1377
1378   /* Write out the instruction.
1379
1380      Four byte insns have an opcode with the two high bits on.  */ 
1381   if (relaxable && fc > 0)
1382     {
1383       f = frag_var (rs_machine_dependent, 6, 4, 0,
1384                     fixups[0].exp.X_add_symbol,
1385                     fixups[0].exp.X_add_number,
1386                     (char *)fixups[0].opindex);
1387       insn_size = 2;
1388       md_number_to_chars (f, insn, insn_size);
1389       md_number_to_chars (f + 2, 0, 4);
1390       fc = 0;
1391     }
1392   else 
1393     {
1394       if ((insn & 0x0600) == 0x0600)
1395         insn_size = 4;
1396       else
1397         insn_size = 2;
1398
1399 /* start-sanitize-v850e */
1400       /* Special case: 32 bit MOV */
1401       if ((insn & 0xffe0) == 0x0620)
1402         insn_size = 2;
1403 /* end_sanitize-v850e */
1404       
1405       f = frag_more (insn_size);
1406       
1407       md_number_to_chars (f, insn, insn_size);
1408
1409       if (extra_data_after_insn)
1410         {
1411           char * g = frag_more (extra_data_len);
1412           
1413           md_number_to_chars (g, extra_data, extra_data_len);
1414
1415           extra_data_after_insn = false;
1416         }
1417     }
1418
1419   /* Create any fixups.  At this point we do not use a
1420      bfd_reloc_code_real_type, but instead just use the
1421      BFD_RELOC_UNUSED plus the operand index.  This lets us easily
1422      handle fixups for any operand type, although that is admittedly
1423      not a very exciting feature.  We pick a BFD reloc type in
1424      md_apply_fix.  */  
1425   for (i = 0; i < fc; i++)
1426     {
1427       const struct v850_operand * operand;
1428
1429       operand = & v850_operands[ fixups[i].opindex ];
1430       
1431       if (fixups[i].reloc != BFD_RELOC_UNUSED)
1432         {
1433           reloc_howto_type * reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1434           int                size;
1435           int                address;
1436           fixS *             fixP;
1437
1438           if (!reloc_howto)
1439             abort();
1440           
1441           size = bfd_get_reloc_size (reloc_howto);
1442
1443           if (size != 2 && size != 4) /* XXX this will abort on an R_V850_8 reloc - is this reloc actually used ? */
1444             abort();
1445
1446           address = (f - frag_now->fr_literal) + insn_size - size;
1447   
1448           fixP = fix_new_exp (frag_now, address, size,
1449                               & fixups[i].exp, 
1450                               reloc_howto->pc_relative,
1451                               fixups[i].reloc);
1452
1453           switch (fixups[i].reloc)
1454             {
1455             case BFD_RELOC_LO16:
1456             case BFD_RELOC_HI16:
1457             case BFD_RELOC_HI16_S:
1458               fixP->fx_no_overflow = 1;
1459               break;
1460             }
1461         }
1462       else
1463         {
1464           fix_new_exp (
1465                        frag_now,
1466                        f - frag_now->fr_literal, 4,
1467                        & fixups[i].exp,
1468                        1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
1469                        (bfd_reloc_code_real_type) (fixups[i].opindex + (int) BFD_RELOC_UNUSED)
1470                        );
1471         }
1472     }
1473
1474   input_line_pointer = saved_input_line_pointer;
1475 }
1476
1477
1478 /* If while processing a fixup, a reloc really needs to be created */
1479 /* then it is done here.  */
1480                  
1481 arelent *
1482 tc_gen_reloc (seg, fixp)
1483      asection * seg;
1484      fixS *     fixp;
1485 {
1486   arelent * reloc;
1487   
1488   reloc              = (arelent *) xmalloc (sizeof (arelent));
1489   reloc->sym_ptr_ptr = & fixp->fx_addsy->bsym;
1490   reloc->address     = fixp->fx_frag->fr_address + fixp->fx_where;
1491   reloc->howto       = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1492   
1493   if (reloc->howto == (reloc_howto_type *) NULL)
1494     {
1495       as_bad_where (fixp->fx_file, fixp->fx_line,
1496                     "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1497       return NULL;
1498     }
1499   
1500   reloc->addend = fixp->fx_addnumber;
1501   
1502   return reloc;
1503 }
1504
1505 /* Assume everything will fit in two bytes, then expand as necessary.  */
1506 int
1507 md_estimate_size_before_relax (fragp, seg)
1508      fragS * fragp;
1509      asection * seg;
1510 {
1511   fragp->fr_var = 4;
1512   return 2;
1513
1514
1515
1516 long
1517 md_pcrel_from (fixp)
1518      fixS * fixp;
1519 {
1520   /* If the symbol is undefined, or in a section other than our own,
1521      then let the linker figure it out.  */
1522   if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1523     {
1524       /* The symbol is undefined.  Let the linker figure it out.  */
1525       return 0;
1526     }
1527   return fixp->fx_frag->fr_address + fixp->fx_where;
1528 }
1529
1530 int
1531 md_apply_fix3 (fixp, valuep, seg)
1532      fixS *   fixp;
1533      valueT * valuep;
1534      segT     seg;
1535 {
1536   valueT value;
1537   char * where;
1538
1539   if (fixp->fx_addsy == (symbolS *) NULL)
1540     {
1541       value = * valuep;
1542       fixp->fx_done = 1;
1543     }
1544   else if (fixp->fx_pcrel)
1545     value = * valuep;
1546   else
1547     {
1548       value = fixp->fx_offset;
1549       if (fixp->fx_subsy != (symbolS *) NULL)
1550         {
1551           if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1552             value -= S_GET_VALUE (fixp->fx_subsy);
1553           else
1554             {
1555               /* We don't actually support subtracting a symbol.  */
1556               as_bad_where (fixp->fx_file, fixp->fx_line,
1557                             "expression too complex");
1558             }
1559         }
1560     }
1561
1562   if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
1563     {
1564       int                         opindex;
1565       const struct v850_operand * operand;
1566       char *                      where;
1567       unsigned long               insn;
1568
1569       opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
1570       operand = & v850_operands[ opindex ];
1571
1572       /* Fetch the instruction, insert the fully resolved operand
1573          value, and stuff the instruction back again.
1574
1575          Note the instruction has been stored in little endian
1576          format!  */
1577       where = fixp->fx_frag->fr_literal + fixp->fx_where;
1578
1579       insn = bfd_getl32 ((unsigned char *) where);
1580       insn = v850_insert_operand (insn, operand, (offsetT) value,
1581                                   fixp->fx_file, fixp->fx_line);
1582       bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
1583
1584       if (fixp->fx_done)
1585         {
1586           /* Nothing else to do here. */
1587           return 1;
1588         }
1589
1590       /* Determine a BFD reloc value based on the operand information.  
1591          We are only prepared to turn a few of the operands into relocs. */
1592
1593       if (operand->bits == 22)
1594         fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1595       else if (operand->bits == 9)
1596         fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1597       else
1598         {
1599           as_bad_where(fixp->fx_file, fixp->fx_line,
1600                        "unresolved expression that must be resolved");
1601           fixp->fx_done = 1;
1602           return 1;
1603         }
1604     }
1605   else if (fixp->fx_done)
1606     {
1607       /* We still have to insert the value into memory!  */
1608       where = fixp->fx_frag->fr_literal + fixp->fx_where;
1609       if (fixp->fx_size == 1)
1610         *where = value & 0xff;
1611       if (fixp->fx_size == 2)
1612         bfd_putl16 (value & 0xffff, (unsigned char *) where);
1613       if (fixp->fx_size == 4)
1614         bfd_putl32 (value, (unsigned char *) where);
1615     }
1616   
1617   fixp->fx_addnumber = value;
1618   return 1;
1619 }
1620
1621 \f
1622 /* Insert an operand value into an instruction.  */
1623
1624 static unsigned long
1625 v850_insert_operand (insn, operand, val, file, line)
1626      unsigned long insn;
1627      const struct v850_operand * operand;
1628      offsetT val;
1629      char *file;
1630      unsigned int line;
1631 {
1632   if (operand->bits != 32)
1633     {
1634       long min, max;
1635       offsetT test;
1636
1637       if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1638         {
1639           if (! warn_signed_overflows)
1640             max = (1 << operand->bits) - 1;
1641           else
1642             max = (1 << (operand->bits - 1)) - 1;
1643           
1644           min = - (1 << (operand->bits - 1));
1645         }
1646       else
1647         {
1648           max = (1 << operand->bits) - 1;
1649           min = 0;
1650         }
1651
1652       test = val;
1653
1654       if (test < (offsetT) min || test > (offsetT) max)
1655         {
1656           const char * err =
1657             "operand out of range (%s not between %ld and %ld)";
1658           char buf[100];
1659
1660           sprint_value (buf, test);
1661           if (file == (char *) NULL)
1662             as_warn (err, buf, min, max);
1663           else
1664             as_warn_where (file, line, err, buf, min, max);
1665         }
1666     }
1667
1668   if (operand->insert)
1669     {
1670       const char * message = NULL;
1671       
1672       insn = (*operand->insert) (insn, val, & message);
1673       if (message != NULL)
1674         {
1675           if (file == (char *) NULL)
1676             as_warn (message);
1677           else
1678             as_warn_where (file, line, message);
1679         }
1680     }
1681   else
1682     insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1683   
1684   return insn;
1685 }
1686
1687 /* Parse a cons expression.  We have to handle hi(), lo(), etc
1688    on the v850.  */
1689 void
1690 parse_cons_expression_v850 (exp)
1691   expressionS *exp;
1692 {
1693   /* See if there's a reloc prefix like hi() we have to handle.  */
1694   hold_cons_reloc = v850_reloc_prefix (NULL);
1695
1696   /* Do normal expression parsing.  */
1697   expression (exp);
1698 }
1699
1700 /* Create a fixup for a cons expression.  If parse_cons_expression_v850
1701    found a reloc prefix, then we use that reloc, else we choose an
1702    appropriate one based on the size of the expression.  */
1703 void
1704 cons_fix_new_v850 (frag, where, size, exp)
1705      fragS *frag;
1706      int where;
1707      int size;
1708      expressionS *exp;
1709 {
1710   if (hold_cons_reloc == BFD_RELOC_UNUSED)
1711     {
1712       if (size == 4)
1713         hold_cons_reloc = BFD_RELOC_32;
1714       if (size == 2)
1715         hold_cons_reloc = BFD_RELOC_16;
1716       if (size == 1)
1717         hold_cons_reloc = BFD_RELOC_8;
1718     }
1719
1720   if (exp != NULL)
1721     fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1722   else
1723     fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);
1724 }