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 /* Structure to hold information about predefined registers. */
40 /* Generic assembler global variables which must be defined by all targets. */
42 /* Characters which always start a comment. */
43 const char comment_chars[] = "#";
45 /* Characters which start a comment at the beginning of a line. */
46 const char line_comment_chars[] = ";#";
48 /* Characters which may be used to separate multiple commands on a
50 const char line_separator_chars[] = ";";
52 /* Characters which are used to indicate an exponent in a floating
54 const char EXP_CHARS[] = "eE";
56 /* Characters which mean that a number is a floating point constant,
58 const char FLT_CHARS[] = "dD";
61 const relax_typeS md_relax_table[] = {
63 {0x1fffff, -0x200000, 6, 0},
68 static unsigned long v850_insert_operand
69 PARAMS ((unsigned long insn, const struct v850_operand *operand,
70 offsetT val, char *file, unsigned int line));
74 #define MAX_INSN_FIXUPS (5)
79 bfd_reloc_code_real_type reloc;
81 struct v850_fixup fixups[MAX_INSN_FIXUPS];
85 v850_section (int arg)
90 for (ptr = input_line_pointer; * ptr != '\n' && * ptr != 0; ptr ++)
91 if (* ptr == ',' && ptr[1] == '.')
97 obj_elf_section (arg);
103 v850_bss (int ignore)
105 register int temp = get_absolute_expression ();
107 subseg_set (bss_section, (subsegT) temp);
109 demand_empty_rest_of_line ();
113 v850_offset (int ignore)
115 register int temp = get_absolute_expression ();
117 subseg_set (now_seg, (subsegT) temp);
119 demand_empty_rest_of_line ();
122 /* The target specific pseudo-ops which we support. */
123 const pseudo_typeS md_pseudo_table[] =
125 {"bss", v850_bss, 0},
126 {"offset", v850_offset, 0},
127 {"section", v850_section, 0},
132 /* Opcode hash table. */
133 static struct hash_control *v850_hash;
135 /* This table is sorted. Suitable for searching by a binary search. */
136 static const struct reg_name pre_defined_registers[] =
138 { "ep", 30 }, /* ep - element ptr */
139 { "gp", 4 }, /* gp - global ptr */
140 { "hp", 2 }, /* hp - handler stack ptr */
141 { "lp", 31 }, /* lp - link ptr */
174 { "sp", 3 }, /* sp - stack ptr */
175 { "tp", 5 }, /* tp - text ptr */
178 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct reg_name))
181 static const struct reg_name system_registers[] =
183 /* start-sanitize-v850e */
189 /* end-sanitize-v850e */
197 #define SYSREG_NAME_CNT (sizeof (system_registers) / sizeof (struct reg_name))
199 static const struct reg_name cc_names[] =
224 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
226 /* reg_name_search does a binary search of the given register table
227 to see if "name" is a valid regiter name. Returns the register
228 number from the array on success, or -1 on failure. */
231 reg_name_search (regs, regcount, name)
232 const struct reg_name *regs;
236 int middle, low, high;
244 middle = (low + high) / 2;
245 cmp = strcasecmp (name, regs[middle].name);
251 return regs[middle].value;
258 /* Summary of register_name().
260 * in: Input_line_pointer points to 1st char of operand.
262 * out: A expressionS.
263 * The operand may have been a register: in this case, X_op == O_register,
264 * X_add_number is set to the register number, and truth is returned.
265 * Input_line_pointer->(next non-blank) char after operand, or is in
266 * its original state.
269 register_name (expressionP)
270 expressionS *expressionP;
277 /* Find the spelling of the operand */
278 start = name = input_line_pointer;
280 c = get_symbol_end ();
282 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
284 * input_line_pointer = c; /* put back the delimiting char */
286 /* look to see if it's in the register table */
289 expressionP->X_op = O_register;
290 expressionP->X_add_number = reg_number;
292 /* make the rest nice */
293 expressionP->X_add_symbol = NULL;
294 expressionP->X_op_symbol = NULL;
300 /* reset the line as if we had not done anything */
301 input_line_pointer = start;
307 /* Summary of system_register_name().
309 * in: Input_line_pointer points to 1st char of operand.
311 * out: A expressionS.
312 * The operand may have been a register: in this case, X_op == O_register,
313 * X_add_number is set to the register number, and truth is returned.
314 * Input_line_pointer->(next non-blank) char after operand, or is in
315 * its original state.
318 system_register_name (expressionP, accept_numbers)
319 expressionS * expressionP;
320 boolean accept_numbers;
327 /* Find the spelling of the operand */
328 start = name = input_line_pointer;
330 c = get_symbol_end ();
331 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
333 * input_line_pointer = c; /* put back the delimiting char */
338 input_line_pointer = start; /* reset input_line pointer */
340 if (isdigit (* input_line_pointer))
341 reg_number = strtol (input_line_pointer, & input_line_pointer, 10);
343 /* Make sure that the register number is allowable. */
346 /* start-sanitize-v850e */
349 /* end-sanitize-v850e */
356 /* look to see if it's in the register table */
359 expressionP->X_op = O_register;
360 expressionP->X_add_number = reg_number;
362 /* make the rest nice */
363 expressionP->X_add_symbol = NULL;
364 expressionP->X_op_symbol = NULL;
370 /* reset the line as if we had not done anything */
371 input_line_pointer = start;
377 /* Summary of cc_name().
379 * in: Input_line_pointer points to 1st char of operand.
381 * out: A expressionS.
382 * The operand may have been a register: in this case, X_op == O_register,
383 * X_add_number is set to the register number, and truth is returned.
384 * Input_line_pointer->(next non-blank) char after operand, or is in
385 * its original state.
388 cc_name (expressionP)
389 expressionS *expressionP;
396 /* Find the spelling of the operand */
397 start = name = input_line_pointer;
399 c = get_symbol_end ();
400 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
402 * input_line_pointer = c; /* put back the delimiting char */
404 /* look to see if it's in the register table */
407 expressionP->X_op = O_constant;
408 expressionP->X_add_number = reg_number;
410 /* make the rest nice */
411 expressionP->X_add_symbol = NULL;
412 expressionP->X_op_symbol = NULL;
418 /* reset the line as if we had not done anything */
419 input_line_pointer = start;
426 skip_white_space (void)
428 while ( * input_line_pointer == ' '
429 || * input_line_pointer == '\t')
430 ++ input_line_pointer;
433 /* start-sanitize-v850e */
434 /* Summary of parse_register_list ().
436 * in: Input_line_pointer points to 1st char of a list of registers.
437 * insn is the partially constructed instruction.
438 * operand is the operand being inserted.
440 * out: True if the parse completed successfully, False otherwise.
441 * If the parse completes the correct bit fields in the
442 * instruction will be filled in.
444 * Parses register lists with the syntax:
452 * and also parses constant epxressions whoes bits indicate the
453 * registers in the lists. The LSB in the expression refers to
454 * the lowest numbered permissable register in the register list,
455 * and so on upwards. System registers are considered to be very
462 unsigned long * insn,
463 const struct v850_operand * operand
466 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 };
467 /* start-sanitize-v850eq */
468 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 };
469 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 };
470 /* end-sanitize-v850eq */
475 /* Select a register array to parse. */
476 switch (operand->shift)
478 case 0xffe00001: regs = type1_regs; break;
479 /* start-sanitize-v850eq */
480 case 0xfff8000f: regs = type2_regs; break;
481 case 0xfff8001f: regs = type3_regs; break;
482 /* end-sanitize-v850eq */
484 as_bad ("unknown operand shift: %x\n", operand->shift );
485 return "internal failure in parse_register_list";
490 /* If the expression starts with a curly brace it is a register list.
491 Otherwise it is a constant expression ,whoes bits indicate which
492 registers are to be included in the list. */
494 if (* input_line_pointer != '{')
502 if (exp.X_op != O_constant)
503 return "constant expression or register list expected";
505 /* start-sanitize-v850eq */
506 if (regs == type1_regs)
507 /* end-sanitize-v850eq */
509 if (exp.X_add_number & 0xFFFFF000)
510 return "high bits set in register list expression";
512 for (reg = 20; reg < 32; reg ++)
513 if (exp.X_add_number & (1 << (reg - 20)))
515 for (i = 0; i < 32; i++)
520 /* start-sanitize-v850eq */
521 else if (regs == type2_regs)
523 if (exp.X_add_number & 0xFFFE0000)
524 return "high bits set in register list expression";
526 for (reg = 1; reg < 16; reg ++)
527 if (exp.X_add_number & (1 << (reg - 1)))
529 for (i = 0; i < 32; i++)
534 if (exp.X_add_number & (1 << 15))
537 if (exp.X_add_number & (1 << 16))
540 else /* regs == type3_regs */
542 if (exp.X_add_number & 0xFFFE0000)
543 return "high bits set in register list expression";
545 for (reg = 16; reg < 32; reg ++)
546 if (exp.X_add_number & (1 << (reg - 16)))
548 for (i = 0; i < 32; i++)
553 if (exp.X_add_number & (1 << 16))
556 /* end-sanitize-v850eq */
561 input_line_pointer ++;
563 /* Parse the register list until a terminator (closing curly brace or new-line) is found. */
566 if (register_name (& exp))
570 /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction. */
571 for (i = 0; i < 32; i++)
573 if (regs[ i ] == exp.X_add_number)
582 return "illegal register included in list";
585 else if (system_register_name (& exp, true))
587 if (regs == type1_regs)
589 return "system registers cannot be included in list";
591 else if (exp.X_add_number == 5)
593 if (regs == type2_regs)
594 return "PSW cannot be included in list";
601 else if (* input_line_pointer == '}')
603 input_line_pointer ++;
606 else if (* input_line_pointer == ',')
608 input_line_pointer ++;
611 else if (* input_line_pointer == '-')
613 /* We have encountered a range of registers: rX - rY */
618 ++ input_line_pointer;
620 /* Get the second register in the range. */
621 if (! register_name (& exp2))
623 return "second register should follow dash in register list";
624 exp2.X_add_number = exp.X_add_number;
627 /* Add the rest of the registers in the range. */
628 for (j = exp.X_add_number + 1; j <= exp2.X_add_number; j++)
632 /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction. */
633 for (i = 0; i < 32; i++)
644 return "illegal register included in list";
658 /* end-sanitize-v850e */
660 CONST char * md_shortopts = "m:";
662 struct option md_longopts[] =
664 {NULL, no_argument, NULL, 0}
666 size_t md_longopts_size = sizeof md_longopts;
670 md_show_usage (stream)
673 fprintf (stream, "V850 options:\n");
674 fprintf (stream, "\tnone at present\n");
678 md_parse_option (c, arg)
686 md_undefined_symbol (name)
693 md_atof (type, litp, sizep)
699 LITTLENUM_TYPE words[4];
715 return "bad call to md_atof";
718 t = atof_ieee (input_line_pointer, type, words);
720 input_line_pointer = t;
724 for (i = prec - 1; i >= 0; i--)
726 md_number_to_chars (litp, (valueT) words[i], 2);
736 md_convert_frag (abfd, sec, fragP)
741 subseg_change (sec, 0);
742 if (fragP->fr_subtype == 0)
744 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
745 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
749 else if (fragP->fr_subtype == 1)
751 /* Reverse the condition of the first branch. */
752 fragP->fr_literal[0] &= 0xf7;
753 /* Mask off all the displacement bits. */
754 fragP->fr_literal[0] &= 0x8f;
755 fragP->fr_literal[1] &= 0x07;
756 /* Now set the displacement bits so that we branch
757 around the unconditional branch. */
758 fragP->fr_literal[0] |= 0x30;
760 /* Now create the unconditional branch + fixup to the final
762 md_number_to_chars (&fragP->fr_literal[2], 0x00000780, 4);
763 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
764 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
773 md_section_align (seg, addr)
777 int align = bfd_get_section_alignment (stdoutput, seg);
778 return ((addr + (1 << align) - 1) & (-1 << align));
784 char *prev_name = "";
785 register const struct v850_opcode *op;
787 v850_hash = hash_new();
789 /* Insert unique names into hash table. The V850 instruction set
790 has many identical opcode names that have different opcodes based
791 on the operands. This hash table then provides a quick index to
792 the first opcode with a particular name in the opcode table. */
797 if (strcmp (prev_name, op->name))
799 prev_name = (char *) op->name;
800 hash_insert (v850_hash, op->name, (char *) op);
805 bfd_set_arch_mach (stdoutput, TARGET_ARCH, 0);
806 fprintf (stderr, "set to %d\n", 0 );
807 /* start-sanitize-v850e */
808 bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_v850e);
809 fprintf (stderr, "set to %d\n", bfd_mach_v850e);
810 /* end-sanitize-v850e */
811 /* start-sanitize-v850eq */
812 bfd_set_arch_mach (stdoutput, TARGET_ARCH, bfd_mach_v850eq);
813 fprintf (stderr, "set to %d\n", bfd_mach_v850eq);
814 /* end-sanitize-v850eq */
817 static bfd_reloc_code_real_type
820 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
822 input_line_pointer += 3;
823 return BFD_RELOC_HI16;
825 if (strncmp(input_line_pointer, "hi(", 3) == 0)
827 input_line_pointer += 2;
828 return BFD_RELOC_HI16_S;
830 if (strncmp (input_line_pointer, "lo(", 3) == 0)
832 input_line_pointer += 2;
833 return BFD_RELOC_LO16;
836 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
838 input_line_pointer += 6;
839 return BFD_RELOC_V850_SDA_OFFSET;
842 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
844 input_line_pointer += 6;
845 return BFD_RELOC_V850_ZDA_OFFSET;
848 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
850 input_line_pointer += 6;
851 return BFD_RELOC_V850_TDA_OFFSET;
855 if (strncmp(input_line_pointer, "(hi0(", 5) == 0)
857 input_line_pointer += 4;
858 return BFD_RELOC_HI16;
860 if (strncmp(input_line_pointer, "(hi(", 4) == 0)
862 input_line_pointer += 3;
863 return BFD_RELOC_HI16_S;
865 if (strncmp (input_line_pointer, "(lo(", 4) == 0)
867 input_line_pointer += 3;
868 return BFD_RELOC_LO16;
871 if (strncmp (input_line_pointer, "(sdaoff(", 8) == 0)
873 input_line_pointer += 7;
874 return BFD_RELOC_V850_SDA_OFFSET;
877 if (strncmp (input_line_pointer, "(zdaoff(", 8) == 0)
879 input_line_pointer += 7;
880 return BFD_RELOC_V850_ZDA_OFFSET;
883 if (strncmp (input_line_pointer, "(tdaoff(", 8) == 0)
885 input_line_pointer += 7;
886 return BFD_RELOC_V850_TDA_OFFSET;
889 return BFD_RELOC_UNUSED;
897 char * start_of_operands;
898 struct v850_opcode * opcode;
899 struct v850_opcode * next_opcode;
900 const unsigned char * opindex_ptr;
904 unsigned long insn_size;
908 bfd_reloc_code_real_type reloc;
909 boolean extra_data_after_insn = false;
910 unsigned extra_data_len;
911 unsigned long extra_data;
912 char * saved_input_line_pointer;
915 /* Get the opcode. */
916 for (s = str; *s != '\0' && ! isspace (*s); s++)
922 /* find the first opcode with the proper name */
923 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
926 as_bad ("Unrecognized opcode: `%s'", str);
927 ignore_rest_of_line ();
932 while (isspace (* str))
935 start_of_operands = str;
937 saved_input_line_pointer = input_line_pointer;
941 const char * errmsg = NULL;
947 insn = opcode->opcode;
948 extra_data_after_insn = false;
950 input_line_pointer = str = start_of_operands;
952 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
954 const struct v850_operand * operand;
958 if (next_opindex == 0)
960 operand = & v850_operands[ * opindex_ptr ];
964 operand = & v850_operands[ next_opindex ];
970 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
973 if (operand->flags & V850_OPERAND_RELAX)
976 /* Gather the operand. */
977 hold = input_line_pointer;
978 input_line_pointer = str;
980 /*fprintf (stderr, "operand: %s index = %d, opcode = %s\n", input_line_pointer, opindex_ptr - opcode->operands, opcode->name );*/
982 /* lo(), hi(), hi0(), etc... */
983 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
987 if (ex.X_op == O_constant)
993 /* Truncate, then sign extend the value. */
994 ex.X_add_number = SEXT16 (ex.X_add_number);
1000 /* Truncate, then sign extend the value. */
1001 ex.X_add_number = SEXT16 (ex.X_add_number >> 16);
1005 case BFD_RELOC_HI16_S:
1007 /* Truncate, then sign extend the value. */
1008 int temp = (ex.X_add_number >> 16) & 0xffff;
1010 temp += (ex.X_add_number >> 15) & 1;
1012 ex.X_add_number = SEXT16 (temp);
1020 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1025 if (fc > MAX_INSN_FIXUPS)
1026 as_fatal ("too many fixups");
1028 /* Adjust any offsets for sst.{h,w}/sld.{h,w} instructions */
1029 if (operand->flags & V850_OPERAND_ADJUST_SHORT_MEMORY)
1030 ex.X_add_number >>= 1;
1032 fixups[fc].exp = ex;
1033 fixups[fc].opindex = *opindex_ptr;
1034 fixups[fc].reloc = reloc;
1042 if ((operand->flags & V850_OPERAND_REG) != 0)
1044 if (!register_name (& ex))
1046 errmsg = "invalid register name";
1049 if ((operand->flags & V850_NOT_R0)
1050 && ex.X_add_number == 0)
1052 errmsg = "register r0 cannot be used here";
1055 else if ((operand->flags & V850_OPERAND_SRG) != 0)
1057 if (!system_register_name (& ex, true))
1059 errmsg = "UGG invalid system register name";
1062 else if ((operand->flags & V850_OPERAND_EP) != 0)
1064 char * start = input_line_pointer;
1065 char c = get_symbol_end ();
1067 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
1069 /* Put things back the way we found them. */
1070 *input_line_pointer = c;
1071 input_line_pointer = start;
1072 errmsg = "expected EP register";
1076 *input_line_pointer = c;
1077 str = input_line_pointer;
1078 input_line_pointer = hold;
1080 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
1084 else if ((operand->flags & V850_OPERAND_CC) != 0)
1086 if (!cc_name (& ex))
1088 errmsg = "invalid condition code name";
1091 /* start-sanitize-v850e */
1092 else if (operand->flags & V850E_PUSH_POP)
1094 errmsg = parse_register_list (& insn, operand);
1096 /* The parse_register_list() function has already done everything, so fake a dummy expression. */
1097 ex.X_op = O_constant;
1098 ex.X_add_number = 0;
1100 /* end-sanitize-v850e */
1101 /* start-sanitize-v850e */
1102 else if (operand->flags & V850E_IMMEDIATE16)
1106 if (ex.X_op != O_constant)
1107 errmsg = "constant expression expected";
1108 else if (ex.X_add_number & 0xffff0000)
1110 if (ex.X_add_number & 0xffff)
1111 errmsg = "constant too big to fit into instruction";
1112 else if ((insn & 0x001fffc0) == 0x00130780)
1113 ex.X_add_number >>= 16;
1115 errmsg = "constant too big to fit into instruction";
1118 extra_data_after_insn = true;
1120 extra_data = ex.X_add_number;
1121 ex.X_add_number = 0;
1123 /* end-sanitize-v850e */
1124 /* start-sanitize-v850e */
1125 else if (operand->flags & V850E_IMMEDIATE32)
1129 if (ex.X_op != O_constant)
1130 errmsg = "constant expression expected";
1132 extra_data_after_insn = true;
1134 extra_data = ex.X_add_number;
1135 ex.X_add_number = 0;
1137 /* end-sanitize-v850e */
1138 else if (register_name (&ex)
1139 && (operand->flags & V850_OPERAND_REG) == 0)
1141 errmsg = "syntax error: register not expected";
1143 else if (system_register_name (& ex, false)
1144 && (operand->flags & V850_OPERAND_SRG) == 0)
1146 errmsg = "syntax error: system register not expected";
1148 else if (cc_name (&ex)
1149 && (operand->flags & V850_OPERAND_CC) == 0)
1151 errmsg = "syntax error: condition code not expected";
1156 /* start-sanitize-v850e */
1158 If we are assembling a MOV instruction (or a CALLT.... :-)
1159 and the immediate value does not fit into the bits available
1160 then create a fake error so that the next MOV instruction
1161 will be selected. This one has a 32 bit immediate field. */
1163 if (((insn & 0x07e0) == 0x0200)
1164 && ex.X_op == O_constant
1165 && (ex.X_add_number < (- (1 << (operand->bits - 1))) || ex.X_add_number > ((1 << operand->bits) - 1)))
1166 errmsg = "use bigger instruction";
1167 /* end-sanitize-v850e */
1173 /*fprintf (stderr, "insn: %x, operand %d, op: %d, add_number: %d\n", insn, opindex_ptr - opcode->operands, ex.X_op, ex.X_add_number );*/
1178 errmsg = "illegal operand";
1181 errmsg = "missing operand";
1184 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
1186 errmsg = "invalid operand";
1189 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1194 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1199 /* We need to generate a fixup for this expression. */
1200 if (fc >= MAX_INSN_FIXUPS)
1201 as_fatal ("too many fixups");
1202 fixups[fc].exp = ex;
1203 fixups[fc].opindex = *opindex_ptr;
1204 fixups[fc].reloc = BFD_RELOC_UNUSED;
1210 str = input_line_pointer;
1211 input_line_pointer = hold;
1213 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
1222 next_opcode = opcode + 1;
1223 if (next_opcode->name != NULL && strcmp (next_opcode->name, opcode->name) == 0)
1225 opcode = next_opcode;
1229 as_bad ("%s", errmsg);
1230 ignore_rest_of_line ();
1231 input_line_pointer = saved_input_line_pointer;
1237 while (isspace (*str))
1241 as_bad ("junk at end of line: `%s'", str);
1243 input_line_pointer = str;
1245 /* Write out the instruction.
1247 Four byte insns have an opcode with the two high bits on. */
1248 if (relaxable && fc > 0)
1250 f = frag_var (rs_machine_dependent, 6, 4, 0,
1251 fixups[0].exp.X_add_symbol,
1252 fixups[0].exp.X_add_number,
1253 (char *)fixups[0].opindex);
1255 md_number_to_chars (f, insn, insn_size);
1256 md_number_to_chars (f + 2, 0, 4);
1261 if ((insn & 0x0600) == 0x0600)
1266 /* start-sanitize-v850e */
1267 /* Special case: 32 bit MOV */
1268 if ((insn & 0xffe0) == 0x0620)
1270 /* end_sanitize-v850e */
1272 f = frag_more (insn_size);
1274 md_number_to_chars (f, insn, insn_size);
1276 if (extra_data_after_insn)
1278 f = frag_more (extra_data_len);
1279 md_number_to_chars (f, extra_data, extra_data_len);
1281 extra_data_after_insn = false;
1285 /* Create any fixups. At this point we do not use a
1286 bfd_reloc_code_real_type, but instead just use the
1287 BFD_RELOC_UNUSED plus the operand index. This lets us easily
1288 handle fixups for any operand type, although that is admittedly
1289 not a very exciting feature. We pick a BFD reloc type in
1291 for (i = 0; i < fc; i++)
1293 const struct v850_operand *operand;
1295 operand = &v850_operands[fixups[i].opindex];
1296 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1298 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1306 size = bfd_get_reloc_size (reloc_howto);
1308 /* The "size" of a TDA_OFFSET reloc varies depending
1309 on what kind of instruction it's used in! */
1310 if (reloc_howto->type == 11 && insn_size > 2)
1313 if (size < 1 || size > 4)
1317 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
1319 reloc_howto->pc_relative,
1322 switch (fixups[i].reloc)
1324 case BFD_RELOC_LO16:
1325 case BFD_RELOC_HI16:
1326 case BFD_RELOC_HI16_S:
1327 fixP->fx_no_overflow = 1;
1333 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1335 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
1336 ((bfd_reloc_code_real_type)
1337 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1341 input_line_pointer = saved_input_line_pointer;
1345 /* if while processing a fixup, a reloc really needs to be created */
1346 /* then it is done here */
1349 tc_gen_reloc (seg, fixp)
1354 reloc = (arelent *) xmalloc (sizeof (arelent));
1355 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1356 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1357 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1358 if (reloc->howto == (reloc_howto_type *) NULL)
1360 as_bad_where (fixp->fx_file, fixp->fx_line,
1361 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1364 reloc->addend = fixp->fx_addnumber;
1365 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
1369 /* Assume everything will fit in two bytes, then expand as necessary. */
1371 md_estimate_size_before_relax (fragp, seg)
1381 md_pcrel_from (fixp)
1384 /* If the symbol is undefined, or in a section other than our own,
1385 then let the linker figure it out. */
1386 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1388 /* The symbol is undefined. Let the linker figure it out. */
1391 return fixp->fx_frag->fr_address + fixp->fx_where;
1395 md_apply_fix3 (fixp, valuep, seg)
1403 if (fixp->fx_addsy == (symbolS *) NULL)
1408 else if (fixp->fx_pcrel)
1412 value = fixp->fx_offset;
1413 if (fixp->fx_subsy != (symbolS *) NULL)
1415 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1416 value -= S_GET_VALUE (fixp->fx_subsy);
1419 /* We don't actually support subtracting a symbol. */
1420 as_bad_where (fixp->fx_file, fixp->fx_line,
1421 "expression too complex");
1426 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
1428 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
1431 const struct v850_operand *operand;
1435 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
1436 operand = &v850_operands[opindex];
1438 /* Fetch the instruction, insert the fully resolved operand
1439 value, and stuff the instruction back again.
1441 Note the instruction has been stored in little endian
1443 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1445 insn = bfd_getl32((unsigned char *) where);
1446 insn = v850_insert_operand (insn, operand, (offsetT) value,
1447 fixp->fx_file, fixp->fx_line);
1448 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
1452 /* Nothing else to do here. */
1456 /* Determine a BFD reloc value based on the operand information.
1457 We are only prepared to turn a few of the operands into relocs. */
1459 if (operand->bits == 22)
1460 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1461 else if (operand->bits == 9)
1462 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1463 else if (operand->bits == 16)
1464 fixp->fx_r_type = BFD_RELOC_V850_16_PCREL;
1467 as_bad_where(fixp->fx_file, fixp->fx_line,
1468 "unresolved expression that must be resolved");
1473 else if (fixp->fx_done)
1475 /* We still have to insert the value into memory! */
1476 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1477 if (fixp->fx_size == 1)
1478 *where = value & 0xff;
1479 if (fixp->fx_size == 2)
1480 bfd_putl16(value & 0xffff, (unsigned char *) where);
1481 if (fixp->fx_size == 4)
1482 bfd_putl32(value, (unsigned char *) where);
1485 fixp->fx_addnumber = value;
1490 /* Insert an operand value into an instruction. */
1492 static unsigned long
1493 v850_insert_operand (insn, operand, val, file, line)
1495 const struct v850_operand * operand;
1500 if (operand->bits != 32)
1505 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1507 max = (1 << (operand->bits - 1)) - 1;
1508 min = - (1 << (operand->bits - 1));
1512 max = (1 << operand->bits) - 1;
1518 if (test < (offsetT) min || test > (offsetT) max)
1521 "operand out of range (%s not between %ld and %ld)";
1524 sprint_value (buf, test);
1525 if (file == (char *) NULL)
1526 as_warn (err, buf, min, max);
1528 as_warn_where (file, line, err, buf, min, max);
1532 if (operand->insert)
1534 const char * message = NULL;
1536 insn = (*operand->insert) (insn, val, & message);
1537 if (message != NULL)
1539 if (file == (char *) NULL)
1542 as_warn_where (file, line, message);
1546 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1551 /* Parse a cons expression. We have to handle hi(), lo(), etc
1554 parse_cons_expression_v850 (exp)
1557 /* See if there's a reloc prefix like hi() we have to handle. */
1558 hold_cons_reloc = v850_reloc_prefix ();
1560 /* Do normal expression parsing. */
1564 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1565 found a reloc prefix, then we use that reloc, else we choose an
1566 appropriate one based on the size of the expression. */
1568 cons_fix_new_v850 (frag, where, size, exp)
1574 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1577 hold_cons_reloc = BFD_RELOC_32;
1579 hold_cons_reloc = BFD_RELOC_16;
1581 hold_cons_reloc = BFD_RELOC_8;
1585 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1587 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);