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