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