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