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