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