1 /* tc-v850.c -- Assembler code for the NEC V850
2 Copyright (C) 1996, 1997 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
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)
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.
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. */
25 #include "opcode/v850.h"
27 /* sign-extend a 16-bit number */
28 #define SEXT16(x) ((((x) & 0xffff) ^ (~ 0x7fff)) + 0x8000)
30 /* Temporarily holds the reloc in a cons expression. */
31 static bfd_reloc_code_real_type hold_cons_reloc;
33 /* Set to TRUE if we want to be pedantic about signed overflows. */
34 static boolean warn_signed_overflows = FALSE;
37 /* Structure to hold information about predefined registers. */
44 /* Generic assembler global variables which must be defined by all targets. */
46 /* Characters which always start a comment. */
47 const char comment_chars[] = "#";
49 /* Characters which start a comment at the beginning of a line. */
50 const char line_comment_chars[] = ";#";
52 /* Characters which may be used to separate multiple commands on a
54 const char line_separator_chars[] = ";";
56 /* Characters which are used to indicate an exponent in a floating
58 const char EXP_CHARS[] = "eE";
60 /* Characters which mean that a number is a floating point constant,
62 const char FLT_CHARS[] = "dD";
65 const relax_typeS md_relax_table[] = {
67 {0x1fffff, -0x200000, 6, 0},
71 static segT sdata_section = NULL;
72 static segT tdata_section = NULL;
73 static segT zdata_section = NULL;
74 static segT sbss_section = NULL;
75 static segT tbss_section = NULL;
76 static segT zbss_section = NULL;
77 static segT rosdata_section = NULL;
78 static segT rozdata_section = NULL;
82 static unsigned long v850_insert_operand
83 PARAMS ((unsigned long insn, const struct v850_operand *operand,
84 offsetT val, char *file, unsigned int line));
88 #define MAX_INSN_FIXUPS (5)
93 bfd_reloc_code_real_type reloc;
95 struct v850_fixup fixups[MAX_INSN_FIXUPS];
99 v850_sdata (int ignore)
101 subseg_set (sdata_section, (subsegT) get_absolute_expression ());
103 demand_empty_rest_of_line ();
107 v850_tdata (int ignore)
109 subseg_set (tdata_section, (subsegT) get_absolute_expression ());
111 demand_empty_rest_of_line ();
115 v850_zdata (int ignore)
117 subseg_set (zdata_section, (subsegT) get_absolute_expression ());
119 demand_empty_rest_of_line ();
123 v850_sbss (int ignore)
125 subseg_set (sbss_section, (subsegT) get_absolute_expression ());
127 demand_empty_rest_of_line ();
131 v850_tbss (int ignore)
133 subseg_set (tbss_section, (subsegT) get_absolute_expression ());
135 demand_empty_rest_of_line ();
139 v850_zbss (int ignore)
141 subseg_set (zbss_section, (subsegT) get_absolute_expression ());
143 demand_empty_rest_of_line ();
147 v850_rosdata (int ignore)
149 subseg_set (rosdata_section, (subsegT) get_absolute_expression ());
151 demand_empty_rest_of_line ();
155 v850_rozdata (int ignore)
157 subseg_set (rozdata_section, (subsegT) get_absolute_expression ());
159 demand_empty_rest_of_line ();
163 v850_section (int arg)
168 for (ptr = input_line_pointer; * ptr != '\n' && * ptr != 0; ptr ++)
169 if (* ptr == ',' && ptr[1] == '.')
175 obj_elf_section (arg);
181 v850_bss (int ignore)
183 register int temp = get_absolute_expression ();
185 obj_elf_section_change_hook();
187 subseg_set (bss_section, (subsegT) temp);
189 demand_empty_rest_of_line ();
193 v850_offset (int ignore)
195 int temp = get_absolute_expression ();
197 temp -= frag_now_fix();
200 (void) frag_more (temp);
202 demand_empty_rest_of_line ();
205 /* The target specific pseudo-ops which we support. */
206 const pseudo_typeS md_pseudo_table[] =
208 {"sdata", v850_sdata, 0},
209 {"tdata", v850_tdata, 0},
210 {"zdata", v850_zdata, 0},
211 {"sbss", v850_sbss, 0},
212 {"tbss", v850_tbss, 0},
213 {"zbss", v850_zbss, 0},
214 {"rosdata", v850_rosdata, 0},
215 {"rozdata", v850_rozdata, 0},
216 {"bss", v850_bss, 0},
217 {"offset", v850_offset, 0},
218 {"section", v850_section, 0},
223 /* Opcode hash table. */
224 static struct hash_control *v850_hash;
226 /* This table is sorted. Suitable for searching by a binary search. */
227 static const struct reg_name pre_defined_registers[] =
229 { "ep", 30 }, /* ep - element ptr */
230 { "gp", 4 }, /* gp - global ptr */
231 { "hp", 2 }, /* hp - handler stack ptr */
232 { "lp", 31 }, /* lp - link ptr */
265 { "sp", 3 }, /* sp - stack ptr */
266 { "tp", 5 }, /* tp - text ptr */
269 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct reg_name))
272 static const struct reg_name system_registers[] =
274 /* start-sanitize-v850e */
280 /* end-sanitize-v850e */
288 #define SYSREG_NAME_CNT (sizeof (system_registers) / sizeof (struct reg_name))
290 static const struct reg_name cc_names[] =
315 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
317 /* reg_name_search does a binary search of the given register table
318 to see if "name" is a valid regiter name. Returns the register
319 number from the array on success, or -1 on failure. */
322 reg_name_search (regs, regcount, name)
323 const struct reg_name * regs;
327 int middle, low, high;
335 middle = (low + high) / 2;
336 cmp = strcasecmp (name, regs[middle].name);
342 return regs[middle].value;
349 /* Summary of register_name().
351 * in: Input_line_pointer points to 1st char of operand.
353 * out: A expressionS.
354 * The operand may have been a register: in this case, X_op == O_register,
355 * X_add_number is set to the register number, and truth is returned.
356 * Input_line_pointer->(next non-blank) char after operand, or is in
357 * its original state.
360 register_name (expressionP)
361 expressionS * expressionP;
368 /* Find the spelling of the operand */
369 start = name = input_line_pointer;
371 c = get_symbol_end ();
373 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
375 * input_line_pointer = c; /* put back the delimiting char */
377 /* look to see if it's in the register table */
380 expressionP->X_op = O_register;
381 expressionP->X_add_number = reg_number;
383 /* make the rest nice */
384 expressionP->X_add_symbol = NULL;
385 expressionP->X_op_symbol = NULL;
391 /* reset the line as if we had not done anything */
392 input_line_pointer = start;
398 /* Summary of system_register_name().
400 * in: Input_line_pointer points to 1st char of operand.
402 * out: A expressionS.
403 * The operand may have been a register: in this case, X_op == O_register,
404 * X_add_number is set to the register number, and truth is returned.
405 * Input_line_pointer->(next non-blank) char after operand, or is in
406 * its original state.
409 system_register_name (expressionP, accept_numbers)
410 expressionS * expressionP;
411 boolean accept_numbers;
418 /* Find the spelling of the operand */
419 start = name = input_line_pointer;
421 c = get_symbol_end ();
422 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
424 * input_line_pointer = c; /* put back the delimiting char */
429 input_line_pointer = start; /* reset input_line pointer */
431 if (isdigit (* input_line_pointer))
432 reg_number = strtol (input_line_pointer, & input_line_pointer, 10);
434 /* Make sure that the register number is allowable. */
437 /* start-sanitize-v850e */
440 /* end-sanitize-v850e */
447 /* look to see if it's in the register table */
450 expressionP->X_op = O_register;
451 expressionP->X_add_number = reg_number;
453 /* make the rest nice */
454 expressionP->X_add_symbol = NULL;
455 expressionP->X_op_symbol = NULL;
461 /* reset the line as if we had not done anything */
462 input_line_pointer = start;
468 /* Summary of cc_name().
470 * in: Input_line_pointer points to 1st char of operand.
472 * out: A expressionS.
473 * The operand may have been a register: in this case, X_op == O_register,
474 * X_add_number is set to the register number, and truth is returned.
475 * Input_line_pointer->(next non-blank) char after operand, or is in
476 * its original state.
479 cc_name (expressionP)
480 expressionS *expressionP;
487 /* Find the spelling of the operand */
488 start = name = input_line_pointer;
490 c = get_symbol_end ();
491 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
493 * input_line_pointer = c; /* put back the delimiting char */
495 /* look to see if it's in the register table */
498 expressionP->X_op = O_constant;
499 expressionP->X_add_number = reg_number;
501 /* make the rest nice */
502 expressionP->X_add_symbol = NULL;
503 expressionP->X_op_symbol = NULL;
509 /* reset the line as if we had not done anything */
510 input_line_pointer = start;
517 skip_white_space (void)
519 while ( * input_line_pointer == ' '
520 || * input_line_pointer == '\t')
521 ++ input_line_pointer;
524 /* start-sanitize-v850e */
525 /* Summary of parse_register_list ().
527 * in: Input_line_pointer points to 1st char of a list of registers.
528 * insn is the partially constructed instruction.
529 * operand is the operand being inserted.
531 * out: True if the parse completed successfully, False otherwise.
532 * If the parse completes the correct bit fields in the
533 * instruction will be filled in.
535 * Parses register lists with the syntax:
543 * and also parses constant epxressions whoes bits indicate the
544 * registers in the lists. The LSB in the expression refers to
545 * the lowest numbered permissable register in the register list,
546 * and so on upwards. System registers are considered to be very
553 unsigned long * insn,
554 const struct v850_operand * operand
557 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 };
558 /* start-sanitize-v850eq */
559 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 };
560 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 };
561 /* end-sanitize-v850eq */
566 /* Select a register array to parse. */
567 switch (operand->shift)
569 case 0xffe00001: regs = type1_regs; break;
570 /* start-sanitize-v850eq */
571 case 0xfff8000f: regs = type2_regs; break;
572 case 0xfff8001f: regs = type3_regs; break;
573 /* end-sanitize-v850eq */
575 as_bad ("unknown operand shift: %x\n", operand->shift );
576 return "internal failure in parse_register_list";
581 /* If the expression starts with a curly brace it is a register list.
582 Otherwise it is a constant expression ,whoes bits indicate which
583 registers are to be included in the list. */
585 if (* input_line_pointer != '{')
593 if (exp.X_op != O_constant)
594 return "constant expression or register list expected";
596 /* start-sanitize-v850eq */
597 if (regs == type1_regs)
598 /* end-sanitize-v850eq */
600 if (exp.X_add_number & 0xFFFFF000)
601 return "high bits set in register list expression";
603 for (reg = 20; reg < 32; reg ++)
604 if (exp.X_add_number & (1 << (reg - 20)))
606 for (i = 0; i < 32; i++)
611 /* start-sanitize-v850eq */
612 else if (regs == type2_regs)
614 if (exp.X_add_number & 0xFFFE0000)
615 return "high bits set in register list expression";
617 for (reg = 1; reg < 16; reg ++)
618 if (exp.X_add_number & (1 << (reg - 1)))
620 for (i = 0; i < 32; i++)
625 if (exp.X_add_number & (1 << 15))
628 if (exp.X_add_number & (1 << 16))
631 else /* regs == type3_regs */
633 if (exp.X_add_number & 0xFFFE0000)
634 return "high bits set in register list expression";
636 for (reg = 16; reg < 32; reg ++)
637 if (exp.X_add_number & (1 << (reg - 16)))
639 for (i = 0; i < 32; i++)
644 if (exp.X_add_number & (1 << 16))
647 /* end-sanitize-v850eq */
652 input_line_pointer ++;
654 /* Parse the register list until a terminator (closing curly brace or new-line) is found. */
657 if (register_name (& exp))
661 /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction. */
662 for (i = 0; i < 32; i++)
664 if (regs[ i ] == exp.X_add_number)
673 return "illegal register included in list";
676 else if (system_register_name (& exp, true))
678 if (regs == type1_regs)
680 return "system registers cannot be included in list";
682 else if (exp.X_add_number == 5)
684 if (regs == type2_regs)
685 return "PSW cannot be included in list";
692 else if (* input_line_pointer == '}')
694 input_line_pointer ++;
697 else if (* input_line_pointer == ',')
699 input_line_pointer ++;
702 else if (* input_line_pointer == '-')
704 /* We have encountered a range of registers: rX - rY */
709 ++ input_line_pointer;
711 /* Get the second register in the range. */
712 if (! register_name (& exp2))
714 return "second register should follow dash in register list";
715 exp2.X_add_number = exp.X_add_number;
718 /* Add the rest of the registers in the range. */
719 for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
723 /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction. */
724 for (i = 0; i < 32; i++)
735 return "illegal register included in list";
749 /* end-sanitize-v850e */
751 CONST char * md_shortopts = "m:";
753 struct option md_longopts[] =
755 {NULL, no_argument, NULL, 0}
757 size_t md_longopts_size = sizeof md_longopts;
761 md_show_usage (stream)
764 fprintf (stream, "V850 options:\n");
765 fprintf (stream, "\t-wsigned_overflow Warn if signed immediate values overflow\n");
769 md_parse_option (c, arg)
773 if (c == 'w' && strcmp (arg, "signed_overflow") == 0)
775 warn_signed_overflows = TRUE;
783 md_undefined_symbol (name)
790 md_atof (type, litp, sizep)
796 LITTLENUM_TYPE words[4];
812 return "bad call to md_atof";
815 t = atof_ieee (input_line_pointer, type, words);
817 input_line_pointer = t;
821 for (i = prec - 1; i >= 0; i--)
823 md_number_to_chars (litp, (valueT) words[i], 2);
833 md_convert_frag (abfd, sec, fragP)
838 subseg_change (sec, 0);
839 if (fragP->fr_subtype == 0)
841 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
842 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
846 else if (fragP->fr_subtype == 1)
848 /* Reverse the condition of the first branch. */
849 fragP->fr_literal[0] &= 0xf7;
850 /* Mask off all the displacement bits. */
851 fragP->fr_literal[0] &= 0x8f;
852 fragP->fr_literal[1] &= 0x07;
853 /* Now set the displacement bits so that we branch
854 around the unconditional branch. */
855 fragP->fr_literal[0] |= 0x30;
857 /* Now create the unconditional branch + fixup to the final
859 md_number_to_chars (&fragP->fr_literal[2], 0x00000780, 4);
860 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
861 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
870 md_section_align (seg, addr)
874 int align = bfd_get_section_alignment (stdoutput, seg);
875 return ((addr + (1 << align) - 1) & (-1 << align));
881 char * prev_name = "";
882 register const struct v850_opcode * op;
886 v850_hash = hash_new();
888 /* Insert unique names into hash table. The V850 instruction set
889 has many identical opcode names that have different opcodes based
890 on the operands. This hash table then provides a quick index to
891 the first opcode with a particular name in the opcode table. */
896 if (strcmp (prev_name, op->name))
898 prev_name = (char *) op->name;
899 hash_insert (v850_hash, op->name, (char *) op);
904 bfd_set_arch_mach (stdoutput, TARGET_ARCH, 0);
905 /* start-sanitize-v850e */
906 bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_v850e);
907 /* end-sanitize-v850e */
908 /* start-sanitize-v850eq */
909 bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_v850eq);
910 /* end-sanitize-v850eq */
912 applicable = bfd_applicable_section_flags (stdoutput);
914 sdata_section = subseg_new (".sdata", 0);
915 bfd_set_section_flags (stdoutput, sdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
917 tdata_section = subseg_new (".tdata", 0);
918 bfd_set_section_flags (stdoutput, tdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
920 zdata_section = subseg_new (".zdata", 0);
921 bfd_set_section_flags (stdoutput, zdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA | SEC_HAS_CONTENTS));
923 sbss_section = subseg_new (".sbss", 0);
924 bfd_set_section_flags (stdoutput, sbss_section, applicable & SEC_ALLOC);
926 tbss_section = subseg_new (".tbss", 0);
927 bfd_set_section_flags (stdoutput, tbss_section, applicable & SEC_ALLOC);
929 zbss_section = subseg_new (".zbss", 0);
930 bfd_set_section_flags (stdoutput, zbss_section, applicable & SEC_ALLOC);
932 rosdata_section = subseg_new (".rosdata", 0);
933 bfd_set_section_flags (stdoutput, rosdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY));
935 rozdata_section = subseg_new (".rozdata", 0);
936 bfd_set_section_flags (stdoutput, rozdata_section, applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY));
940 /* Warning: The code in this function relies upon the definitions
941 in the v850_operands[] array (defined in opcodes/v850-opc.c)
942 matching the hard coded values conatined herein. */
944 static bfd_reloc_code_real_type
945 v850_reloc_prefix (const struct v850_operand * operand)
947 boolean paren_skipped = false;
950 /* Skip leading opening parenthesis. */
951 if (* input_line_pointer == '(')
953 ++ input_line_pointer;
954 paren_skipped = true;
957 if (strncmp (input_line_pointer, "hi0(", 4) == 0)
959 input_line_pointer += 3;
960 return BFD_RELOC_HI16;
962 if (strncmp (input_line_pointer, "hi(", 3) == 0)
964 input_line_pointer += 2;
965 return BFD_RELOC_HI16_S;
967 if (strncmp (input_line_pointer, "lo(", 3) == 0)
969 input_line_pointer += 2;
970 return BFD_RELOC_LO16;
973 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
975 input_line_pointer += 6;
977 if (operand == NULL) return BFD_RELOC_V850_SDA_16_16_OFFSET;
978 if (operand->bits == 15 && operand->shift == 17) return BFD_RELOC_V850_SDA_15_16_OFFSET;
979 /* start-sanitize-v850e */
980 if (operand->bits == -1) return BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET;
981 /* end-sanitize-v850e */
983 assert (operand->bits == 16);
984 assert (operand->shift == 16);
986 return BFD_RELOC_V850_SDA_16_16_OFFSET;
989 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
991 input_line_pointer += 6;
993 if (operand == NULL) return BFD_RELOC_V850_ZDA_16_16_OFFSET;
994 if (operand->bits == 15 && operand->shift == 17) return BFD_RELOC_V850_ZDA_15_16_OFFSET;
995 /* start-sanitize-v850e */
996 if (operand->bits == -1) return BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET;
997 /* end-sanitize-v850e */
999 assert (operand->bits == 16);
1000 assert (operand->shift == 16);
1002 return BFD_RELOC_V850_ZDA_16_16_OFFSET;
1005 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
1007 input_line_pointer += 6;
1009 if (operand == NULL) return BFD_RELOC_V850_TDA_7_7_OFFSET;
1010 if (operand->bits == 6 && operand->shift == 1) return BFD_RELOC_V850_TDA_6_8_OFFSET;
1011 /* start-sanitize-v850e */
1012 if (operand->bits == 4 && operand->insert != NULL) return BFD_RELOC_V850_TDA_4_5_OFFSET;
1013 if (operand->bits == 4 && operand->insert == NULL) return BFD_RELOC_V850_TDA_4_4_OFFSET;
1014 /* end-sanitize-v850e */
1016 assert (operand->bits == 7);
1018 return operand->insert != NULL ? BFD_RELOC_V850_TDA_7_8_OFFSET : BFD_RELOC_V850_TDA_7_7_OFFSET;
1022 /* Restore skipped character. */
1023 -- input_line_pointer;
1025 return BFD_RELOC_UNUSED;
1033 char * start_of_operands;
1034 struct v850_opcode * opcode;
1035 struct v850_opcode * next_opcode;
1036 const unsigned char * opindex_ptr;
1040 unsigned long insn_size;
1044 boolean extra_data_after_insn = false;
1045 unsigned extra_data_len;
1046 unsigned long extra_data;
1047 char * saved_input_line_pointer;
1049 /* Get the opcode. */
1050 for (s = str; *s != '\0' && ! isspace (*s); s++)
1056 /* find the first opcode with the proper name */
1057 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
1060 as_bad ("Unrecognized opcode: `%s'", str);
1061 ignore_rest_of_line ();
1066 while (isspace (* str))
1069 start_of_operands = str;
1071 saved_input_line_pointer = input_line_pointer;
1075 const char * errmsg = NULL;
1081 insn = opcode->opcode;
1082 extra_data_after_insn = false;
1084 input_line_pointer = str = start_of_operands;
1086 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
1088 const struct v850_operand * operand;
1091 bfd_reloc_code_real_type reloc;
1093 if (next_opindex == 0)
1095 operand = & v850_operands[ * opindex_ptr ];
1099 operand = & v850_operands[ next_opindex ];
1105 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1108 if (operand->flags & V850_OPERAND_RELAX)
1111 /* Gather the operand. */
1112 hold = input_line_pointer;
1113 input_line_pointer = str;
1115 /* fprintf (stderr, "operand: %s index = %d, opcode = %s\n", input_line_pointer, opindex_ptr - opcode->operands, opcode->name ); */
1117 /* lo(), hi(), hi0(), etc... */
1118 if ((reloc = v850_reloc_prefix (operand)) != BFD_RELOC_UNUSED)
1122 if (ex.X_op == O_constant)
1126 case BFD_RELOC_LO16:
1128 /* Truncate, then sign extend the value. */
1129 ex.X_add_number = SEXT16 (ex.X_add_number);
1133 case BFD_RELOC_HI16:
1135 /* Truncate, then sign extend the value. */
1136 ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
1140 case BFD_RELOC_HI16_S:
1142 /* Truncate, then sign extend the value. */
1143 int temp = (ex.X_add_number >> 16) & 0xffff;
1145 temp += (ex.X_add_number >> 15) & 1;
1147 ex.X_add_number = SEXT16 (temp);
1152 as_bad ( "AAARG -> unhandled constant reloc");
1156 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1161 if (fc > MAX_INSN_FIXUPS)
1162 as_fatal ("too many fixups");
1164 fixups[ fc ].exp = ex;
1165 fixups[ fc ].opindex = * opindex_ptr;
1166 fixups[ fc ].reloc = reloc;
1174 if ((operand->flags & V850_OPERAND_REG) != 0)
1176 if (!register_name (& ex))
1178 errmsg = "invalid register name";
1181 if ((operand->flags & V850_NOT_R0)
1182 && ex.X_add_number == 0)
1184 errmsg = "register r0 cannot be used here";
1187 else if ((operand->flags & V850_OPERAND_SRG) != 0)
1189 if (!system_register_name (& ex, true))
1191 errmsg = "invalid system register name";
1194 else if ((operand->flags & V850_OPERAND_EP) != 0)
1196 char * start = input_line_pointer;
1197 char c = get_symbol_end ();
1199 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
1201 /* Put things back the way we found them. */
1202 *input_line_pointer = c;
1203 input_line_pointer = start;
1204 errmsg = "expected EP register";
1208 *input_line_pointer = c;
1209 str = input_line_pointer;
1210 input_line_pointer = hold;
1212 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1216 else if ((operand->flags & V850_OPERAND_CC) != 0)
1218 if (!cc_name (& ex))
1220 errmsg = "invalid condition code name";
1223 /* start-sanitize-v850e */
1224 else if (operand->flags & V850E_PUSH_POP)
1226 errmsg = parse_register_list (& insn, operand);
1228 /* The parse_register_list() function has already done everything, so fake a dummy expression. */
1229 ex.X_op = O_constant;
1230 ex.X_add_number = 0;
1232 /* end-sanitize-v850e */
1233 /* start-sanitize-v850e */
1234 else if (operand->flags & V850E_IMMEDIATE16)
1238 if (ex.X_op != O_constant)
1239 errmsg = "constant expression expected";
1240 else if (ex.X_add_number & 0xffff0000)
1242 if (ex.X_add_number & 0xffff)
1243 errmsg = "constant too big to fit into instruction";
1244 else if ((insn & 0x001fffc0) == 0x00130780)
1245 ex.X_add_number >>= 16;
1247 errmsg = "constant too big to fit into instruction";
1250 extra_data_after_insn = true;
1252 extra_data = ex.X_add_number;
1253 ex.X_add_number = 0;
1255 /* end-sanitize-v850e */
1256 /* start-sanitize-v850e */
1257 else if (operand->flags & V850E_IMMEDIATE32)
1261 if (ex.X_op != O_constant)
1262 errmsg = "constant expression expected";
1264 extra_data_after_insn = true;
1266 extra_data = ex.X_add_number;
1267 ex.X_add_number = 0;
1269 /* end-sanitize-v850e */
1270 else if (register_name (&ex)
1271 && (operand->flags & V850_OPERAND_REG) == 0)
1273 errmsg = "syntax error: register not expected";
1275 else if (system_register_name (& ex, false)
1276 && (operand->flags & V850_OPERAND_SRG) == 0)
1278 errmsg = "syntax error: system register not expected";
1280 else if (cc_name (&ex)
1281 && (operand->flags & V850_OPERAND_CC) == 0)
1283 errmsg = "syntax error: condition code not expected";
1288 /* start-sanitize-v850e */
1290 If we are assembling a MOV instruction (or a CALLT.... :-)
1291 and the immediate value does not fit into the bits available
1292 then create a fake error so that the next MOV instruction
1293 will be selected. This one has a 32 bit immediate field. */
1295 if (((insn & 0x07e0) == 0x0200)
1296 && ex.X_op == O_constant
1297 && (ex.X_add_number < (- (1 << (operand->bits - 1))) || ex.X_add_number > ((1 << operand->bits) - 1)))
1298 errmsg = "use bigger instruction";
1299 /* end-sanitize-v850e */
1305 /* fprintf (stderr, "insn: %x, operand %d, op: %d, add_number: %d\n", insn, opindex_ptr - opcode->operands, ex.X_op, ex.X_add_number ); */
1310 errmsg = "illegal operand";
1313 errmsg = "missing operand";
1316 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
1318 errmsg = "invalid operand";
1321 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1326 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1331 /* We need to generate a fixup for this expression. */
1332 if (fc >= MAX_INSN_FIXUPS)
1333 as_fatal ("too many fixups");
1335 fixups[ fc ].exp = ex;
1336 fixups[ fc ].opindex = * opindex_ptr;
1337 fixups[ fc ].reloc = BFD_RELOC_UNUSED;
1343 str = input_line_pointer;
1344 input_line_pointer = hold;
1346 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
1355 next_opcode = opcode + 1;
1356 if (next_opcode->name != NULL && strcmp (next_opcode->name, opcode->name) == 0)
1358 opcode = next_opcode;
1363 ignore_rest_of_line ();
1364 input_line_pointer = saved_input_line_pointer;
1370 while (isspace (*str))
1374 as_bad ("junk at end of line: `%s'", str);
1376 input_line_pointer = str;
1378 /* Write out the instruction.
1380 Four byte insns have an opcode with the two high bits on. */
1381 if (relaxable && fc > 0)
1383 f = frag_var (rs_machine_dependent, 6, 4, 0,
1384 fixups[0].exp.X_add_symbol,
1385 fixups[0].exp.X_add_number,
1386 (char *)fixups[0].opindex);
1388 md_number_to_chars (f, insn, insn_size);
1389 md_number_to_chars (f + 2, 0, 4);
1394 if ((insn & 0x0600) == 0x0600)
1399 /* start-sanitize-v850e */
1400 /* Special case: 32 bit MOV */
1401 if ((insn & 0xffe0) == 0x0620)
1403 /* end_sanitize-v850e */
1405 f = frag_more (insn_size);
1407 md_number_to_chars (f, insn, insn_size);
1409 if (extra_data_after_insn)
1411 char * g = frag_more (extra_data_len);
1413 md_number_to_chars (g, extra_data, extra_data_len);
1415 extra_data_after_insn = false;
1419 /* Create any fixups. At this point we do not use a
1420 bfd_reloc_code_real_type, but instead just use the
1421 BFD_RELOC_UNUSED plus the operand index. This lets us easily
1422 handle fixups for any operand type, although that is admittedly
1423 not a very exciting feature. We pick a BFD reloc type in
1425 for (i = 0; i < fc; i++)
1427 const struct v850_operand * operand;
1429 operand = & v850_operands[ fixups[i].opindex ];
1431 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1433 reloc_howto_type * reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1441 size = bfd_get_reloc_size (reloc_howto);
1443 if (size != 2 && size != 4) /* XXX this will abort on an R_V850_8 reloc - is this reloc actually used ? */
1446 address = (f - frag_now->fr_literal) + insn_size - size;
1448 fixP = fix_new_exp (frag_now, address, size,
1450 reloc_howto->pc_relative,
1453 switch (fixups[i].reloc)
1455 case BFD_RELOC_LO16:
1456 case BFD_RELOC_HI16:
1457 case BFD_RELOC_HI16_S:
1458 fixP->fx_no_overflow = 1;
1466 f - frag_now->fr_literal, 4,
1468 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
1469 (bfd_reloc_code_real_type) (fixups[i].opindex + (int) BFD_RELOC_UNUSED)
1474 input_line_pointer = saved_input_line_pointer;
1478 /* If while processing a fixup, a reloc really needs to be created */
1479 /* then it is done here. */
1482 tc_gen_reloc (seg, fixp)
1488 reloc = (arelent *) xmalloc (sizeof (arelent));
1489 reloc->sym_ptr_ptr = & fixp->fx_addsy->bsym;
1490 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1491 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1493 if (reloc->howto == (reloc_howto_type *) NULL)
1495 as_bad_where (fixp->fx_file, fixp->fx_line,
1496 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1500 reloc->addend = fixp->fx_addnumber;
1505 /* Assume everything will fit in two bytes, then expand as necessary. */
1507 md_estimate_size_before_relax (fragp, seg)
1517 md_pcrel_from (fixp)
1520 /* If the symbol is undefined, or in a section other than our own,
1521 then let the linker figure it out. */
1522 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1524 /* The symbol is undefined. Let the linker figure it out. */
1527 return fixp->fx_frag->fr_address + fixp->fx_where;
1531 md_apply_fix3 (fixp, valuep, seg)
1539 if (fixp->fx_addsy == (symbolS *) NULL)
1544 else if (fixp->fx_pcrel)
1548 value = fixp->fx_offset;
1549 if (fixp->fx_subsy != (symbolS *) NULL)
1551 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1552 value -= S_GET_VALUE (fixp->fx_subsy);
1555 /* We don't actually support subtracting a symbol. */
1556 as_bad_where (fixp->fx_file, fixp->fx_line,
1557 "expression too complex");
1562 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
1565 const struct v850_operand * operand;
1569 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
1570 operand = & v850_operands[ opindex ];
1572 /* Fetch the instruction, insert the fully resolved operand
1573 value, and stuff the instruction back again.
1575 Note the instruction has been stored in little endian
1577 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1579 insn = bfd_getl32 ((unsigned char *) where);
1580 insn = v850_insert_operand (insn, operand, (offsetT) value,
1581 fixp->fx_file, fixp->fx_line);
1582 bfd_putl32 ((bfd_vma) insn, (unsigned char *) where);
1586 /* Nothing else to do here. */
1590 /* Determine a BFD reloc value based on the operand information.
1591 We are only prepared to turn a few of the operands into relocs. */
1593 if (operand->bits == 22)
1594 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1595 else if (operand->bits == 9)
1596 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1599 as_bad_where(fixp->fx_file, fixp->fx_line,
1600 "unresolved expression that must be resolved");
1605 else if (fixp->fx_done)
1607 /* We still have to insert the value into memory! */
1608 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1609 if (fixp->fx_size == 1)
1610 *where = value & 0xff;
1611 if (fixp->fx_size == 2)
1612 bfd_putl16 (value & 0xffff, (unsigned char *) where);
1613 if (fixp->fx_size == 4)
1614 bfd_putl32 (value, (unsigned char *) where);
1617 fixp->fx_addnumber = value;
1622 /* Insert an operand value into an instruction. */
1624 static unsigned long
1625 v850_insert_operand (insn, operand, val, file, line)
1627 const struct v850_operand * operand;
1632 if (operand->bits != 32)
1637 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1639 if (! warn_signed_overflows)
1640 max = (1 << operand->bits) - 1;
1642 max = (1 << (operand->bits - 1)) - 1;
1644 min = - (1 << (operand->bits - 1));
1648 max = (1 << operand->bits) - 1;
1654 if (test < (offsetT) min || test > (offsetT) max)
1657 "operand out of range (%s not between %ld and %ld)";
1660 sprint_value (buf, test);
1661 if (file == (char *) NULL)
1662 as_warn (err, buf, min, max);
1664 as_warn_where (file, line, err, buf, min, max);
1668 if (operand->insert)
1670 const char * message = NULL;
1672 insn = (*operand->insert) (insn, val, & message);
1673 if (message != NULL)
1675 if (file == (char *) NULL)
1678 as_warn_where (file, line, message);
1682 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1687 /* Parse a cons expression. We have to handle hi(), lo(), etc
1690 parse_cons_expression_v850 (exp)
1693 /* See if there's a reloc prefix like hi() we have to handle. */
1694 hold_cons_reloc = v850_reloc_prefix (NULL);
1696 /* Do normal expression parsing. */
1700 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1701 found a reloc prefix, then we use that reloc, else we choose an
1702 appropriate one based on the size of the expression. */
1704 cons_fix_new_v850 (frag, where, size, exp)
1710 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1713 hold_cons_reloc = BFD_RELOC_32;
1715 hold_cons_reloc = BFD_RELOC_16;
1717 hold_cons_reloc = BFD_RELOC_8;
1721 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1723 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);