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