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 /* Temporarily holds the reloc in a cons expression. */
28 static bfd_reloc_code_real_type hold_cons_reloc;
30 /* Structure to hold information about predefined registers. */
37 /* Generic assembler global variables which must be defined by all targets. */
39 /* Characters which always start a comment. */
40 const char comment_chars[] = "#";
42 /* Characters which start a comment at the beginning of a line. */
43 const char line_comment_chars[] = ";#-";
45 /* Characters which may be used to separate multiple commands on a
47 const char line_separator_chars[] = ";";
49 /* Characters which are used to indicate an exponent in a floating
51 const char EXP_CHARS[] = "eE";
53 /* Characters which mean that a number is a floating point constant,
55 const char FLT_CHARS[] = "dD";
58 const relax_typeS md_relax_table[] = {
60 {0x1fffff, -0x200000, 6, 0},
65 static unsigned long v850_insert_operand
66 PARAMS ((unsigned long insn, const struct v850_operand *operand,
67 offsetT val, char *file, unsigned int line));
71 #define MAX_INSN_FIXUPS (5)
76 bfd_reloc_code_real_type reloc;
78 struct v850_fixup fixups[MAX_INSN_FIXUPS];
82 v850_section (int arg)
87 for (ptr = input_line_pointer; * ptr != '\n' && * ptr != 0; ptr ++)
94 obj_elf_section (arg);
100 v850_bss (int ignore)
102 register int temp = get_absolute_expression ();
104 subseg_set (bss_section, (subsegT) temp);
106 demand_empty_rest_of_line ();
110 v850_offset (int ignore)
112 register int temp = get_absolute_expression ();
114 subseg_set (now_seg, (subsegT) temp);
116 demand_empty_rest_of_line ();
119 /* The target specific pseudo-ops which we support. */
120 const pseudo_typeS md_pseudo_table[] =
122 {"bss", v850_bss, 0},
123 {"offset", v850_offset, 0},
124 {"section", v850_section, 0},
129 /* Opcode hash table. */
130 static struct hash_control *v850_hash;
132 /* This table is sorted. Suitable for searching by a binary search. */
133 static const struct reg_name pre_defined_registers[] =
135 { "ep", 30 }, /* ep - element ptr */
136 { "gp", 4 }, /* gp - global ptr */
137 { "lp", 31 }, /* lp - link ptr */
170 { "sp", 3 }, /* sp - stack ptr */
171 { "tp", 5 }, /* tp - text ptr */
174 #define REG_NAME_CNT (sizeof (pre_defined_registers) / sizeof (struct reg_name))
177 static const struct reg_name system_registers[] =
179 /* start-sanitize-v850e */
185 /* end-sanitize-v850e */
193 #define SYSREG_NAME_CNT (sizeof (system_registers) / sizeof (struct reg_name))
195 static const struct reg_name cc_names[] =
218 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
220 /* reg_name_search does a binary search of the given register table
221 to see if "name" is a valid regiter name. Returns the register
222 number from the array on success, or -1 on failure. */
225 reg_name_search (regs, regcount, name)
226 const struct reg_name *regs;
230 int middle, low, high;
238 middle = (low + high) / 2;
239 cmp = strcasecmp (name, regs[middle].name);
245 return regs[middle].value;
252 /* Summary of register_name().
254 * in: Input_line_pointer points to 1st char of operand.
256 * out: A expressionS.
257 * The operand may have been a register: in this case, X_op == O_register,
258 * X_add_number is set to the register number, and truth is returned.
259 * Input_line_pointer->(next non-blank) char after operand, or is in
260 * its original state.
263 register_name (expressionP)
264 expressionS *expressionP;
271 /* Find the spelling of the operand */
272 start = name = input_line_pointer;
274 c = get_symbol_end ();
275 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
277 /* look to see if it's in the register table */
280 expressionP->X_op = O_register;
281 expressionP->X_add_number = reg_number;
283 /* make the rest nice */
284 expressionP->X_add_symbol = NULL;
285 expressionP->X_op_symbol = NULL;
286 *input_line_pointer = c; /* put back the delimiting char */
291 /* reset the line as if we had not done anything */
292 *input_line_pointer = c; /* put back the delimiting char */
293 input_line_pointer = start; /* reset input_line pointer */
298 /* Summary of system_register_name().
300 * in: Input_line_pointer points to 1st char of operand.
302 * out: A expressionS.
303 * The operand may have been a register: in this case, X_op == O_register,
304 * X_add_number is set to the register number, and truth is returned.
305 * Input_line_pointer->(next non-blank) char after operand, or is in
306 * its original state.
309 system_register_name (expressionP, accept_numbers)
310 expressionS * expressionP;
311 boolean accept_numbers;
318 /* Find the spelling of the operand */
319 start = name = input_line_pointer;
321 c = get_symbol_end ();
322 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
327 * input_line_pointer = c; /* put back the delimiting char */
328 input_line_pointer = start; /* reset input_line pointer */
330 reg_number = strtol (input_line_pointer, & input_line_pointer, 10);
332 /* Make sure that the register number is allowable. */
335 /* start-sanitize-v850e */
338 /* end-sanitize-v850e */
345 c = * input_line_pointer;
349 /* look to see if it's in the register table */
352 expressionP->X_op = O_register;
353 expressionP->X_add_number = reg_number;
355 /* make the rest nice */
356 expressionP->X_add_symbol = NULL;
357 expressionP->X_op_symbol = NULL;
358 *input_line_pointer = c; /* put back the delimiting char */
363 /* reset the line as if we had not done anything */
364 *input_line_pointer = c; /* put back the delimiting char */
365 input_line_pointer = start; /* reset input_line pointer */
370 /* Summary of cc_name().
372 * in: Input_line_pointer points to 1st char of operand.
374 * out: A expressionS.
375 * The operand may have been a register: in this case, X_op == O_register,
376 * X_add_number is set to the register number, and truth is returned.
377 * Input_line_pointer->(next non-blank) char after operand, or is in
378 * its original state.
381 cc_name (expressionP)
382 expressionS *expressionP;
389 /* Find the spelling of the operand */
390 start = name = input_line_pointer;
392 c = get_symbol_end ();
393 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
395 /* look to see if it's in the register table */
398 expressionP->X_op = O_constant;
399 expressionP->X_add_number = reg_number;
401 /* make the rest nice */
402 expressionP->X_add_symbol = NULL;
403 expressionP->X_op_symbol = NULL;
404 *input_line_pointer = c; /* put back the delimiting char */
409 /* reset the line as if we had not done anything */
410 *input_line_pointer = c; /* put back the delimiting char */
411 input_line_pointer = start; /* reset input_line pointer */
416 /* start-sanitize-v850e */
417 /* Summary of parse_register_list ().
419 * in: Input_line_pointer points to 1st char of a list of registers.
420 * insn is the partially constructed instruction.
421 * operand is the operand being inserted.
423 * out: True if the parse completed successfully, False otherwise.
424 * If the parse completes the correct bit fields in the
425 * instruction will be filled in.
430 unsigned long * insn,
431 const struct v850_operand * operand
434 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 };
435 /* start-sanitize-v850eq */
436 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 };
437 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 };
438 /* end-sanitize-v850eq */
441 /* Select a register array to parse. */
442 switch (operand->shift)
444 case 0xffe00001: regs = type1_regs; break;
445 /* start-sanitize-v850eq */
446 case 0xfff8000f: regs = type2_regs; break;
447 case 0xfff8001f: regs = type3_regs; break;
448 /* end-sanitize-v850eq */
450 fprintf (stderr, "unknown operand shift: %x\n", operand->shift );
454 /* Parse the register list until a terminator (comma or new-line) is found. */
460 if (register_name (& exp))
462 /* Locate the given register in the list, and if it is there, insert the corresponding bit into the instruction. */
463 for (i = 0; i < 32; i++)
465 if (regs[ i ] == exp.X_add_number)
474 as_bad( "unable to insert register r%d into list\n", exp.X_add_number );
478 else if (system_register_name (& exp, true))
480 if (regs == type1_regs)
482 as_bad ("system registers cannot be included in this register list" );
485 else if (exp.X_add_number == 5)
487 if (regs == type2_regs)
488 as_bad ("PSW cannot be included in this register list" );
498 /* Skip white space. */
499 while (* input_line_pointer == ' ' || * input_line_pointer == '\t')
500 ++ input_line_pointer;
505 /* end-sanitize-v850e */
507 CONST char * md_shortopts = "m:";
509 struct option md_longopts[] =
511 {NULL, no_argument, NULL, 0}
513 size_t md_longopts_size = sizeof md_longopts;
517 md_show_usage (stream)
520 fprintf (stream, "V850 options:\n");
521 fprintf (stream, "\tnone at present\n");
525 md_parse_option (c, arg)
533 md_undefined_symbol (name)
540 md_atof (type, litp, sizep)
546 LITTLENUM_TYPE words[4];
562 return "bad call to md_atof";
565 t = atof_ieee (input_line_pointer, type, words);
567 input_line_pointer = t;
571 for (i = prec - 1; i >= 0; i--)
573 md_number_to_chars (litp, (valueT) words[i], 2);
583 md_convert_frag (abfd, sec, fragP)
588 subseg_change (sec, 0);
589 if (fragP->fr_subtype == 0)
591 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
592 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
596 else if (fragP->fr_subtype == 1)
598 /* Reverse the condition of the first branch. */
599 fragP->fr_literal[0] &= 0xf7;
600 /* Mask off all the displacement bits. */
601 fragP->fr_literal[0] &= 0x8f;
602 fragP->fr_literal[1] &= 0x07;
603 /* Now set the displacement bits so that we branch
604 around the unconditional branch. */
605 fragP->fr_literal[0] |= 0x30;
607 /* Now create the unconditional branch + fixup to the final
609 md_number_to_chars (&fragP->fr_literal[2], 0x00000780, 4);
610 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
611 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
620 md_section_align (seg, addr)
624 int align = bfd_get_section_alignment (stdoutput, seg);
625 return ((addr + (1 << align) - 1) & (-1 << align));
631 char *prev_name = "";
632 register const struct v850_opcode *op;
634 v850_hash = hash_new();
636 /* Insert unique names into hash table. The V850 instruction set
637 has many identical opcode names that have different opcodes based
638 on the operands. This hash table then provides a quick index to
639 the first opcode with a particular name in the opcode table. */
644 if (strcmp (prev_name, op->name))
646 prev_name = (char *) op->name;
647 hash_insert (v850_hash, op->name, (char *) op);
653 static bfd_reloc_code_real_type
656 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
658 input_line_pointer += 3;
659 return BFD_RELOC_HI16;
661 if (strncmp(input_line_pointer, "hi(", 3) == 0)
663 input_line_pointer += 2;
664 return BFD_RELOC_HI16_S;
666 if (strncmp (input_line_pointer, "lo(", 3) == 0)
668 input_line_pointer += 2;
669 return BFD_RELOC_LO16;
672 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
674 input_line_pointer += 6;
675 return BFD_RELOC_V850_SDA_OFFSET;
678 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
680 input_line_pointer += 6;
681 return BFD_RELOC_V850_ZDA_OFFSET;
684 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
686 input_line_pointer += 6;
687 return BFD_RELOC_V850_TDA_OFFSET;
691 if (strncmp(input_line_pointer, "(hi0(", 5) == 0)
693 input_line_pointer += 4;
694 return BFD_RELOC_HI16;
696 if (strncmp(input_line_pointer, "(hi(", 4) == 0)
698 input_line_pointer += 3;
699 return BFD_RELOC_HI16_S;
701 if (strncmp (input_line_pointer, "(lo(", 4) == 0)
703 input_line_pointer += 3;
704 return BFD_RELOC_LO16;
707 if (strncmp (input_line_pointer, "(sdaoff(", 8) == 0)
709 input_line_pointer += 7;
710 return BFD_RELOC_V850_SDA_OFFSET;
713 if (strncmp (input_line_pointer, "(zdaoff(", 8) == 0)
715 input_line_pointer += 7;
716 return BFD_RELOC_V850_ZDA_OFFSET;
719 if (strncmp (input_line_pointer, "(tdaoff(", 8) == 0)
721 input_line_pointer += 7;
722 return BFD_RELOC_V850_TDA_OFFSET;
725 return BFD_RELOC_UNUSED;
733 char * start_of_operands;
734 struct v850_opcode * opcode;
735 struct v850_opcode * next_opcode;
736 const unsigned char * opindex_ptr;
740 unsigned long insn_size;
744 bfd_reloc_code_real_type reloc;
745 boolean extra_data_after_insn = false;
746 unsigned extra_data_len;
747 unsigned long extra_data;
750 /* Get the opcode. */
751 for (s = str; *s != '\0' && ! isspace (*s); s++)
757 /* find the first opcode with the proper name */
758 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
761 as_bad ("Unrecognized opcode: `%s'", str);
762 ignore_rest_of_line ();
767 while (isspace (*str))
770 start_of_operands = str;
774 const char * errmsg = NULL;
780 insn = opcode->opcode;
781 extra_data_after_insn = false;
783 input_line_pointer = str = start_of_operands;
785 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
787 const struct v850_operand * operand;
791 if (next_opindex == 0)
793 operand = &v850_operands[*opindex_ptr];
797 operand = &v850_operands[next_opindex];
803 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
806 if (operand->flags & V850_OPERAND_RELAX)
809 /* Gather the operand. */
810 hold = input_line_pointer;
811 input_line_pointer = str;
813 //fprintf (stderr, "operand: %s index = %d, opcode = %s\n", input_line_pointer, opindex_ptr - opcode->operands, opcode->name );
815 /* lo(), hi(), hi0(), etc... */
816 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
820 if (ex.X_op == O_constant)
826 /* Truncate, then sign extend the value. */
827 int temp = ex.X_add_number & 0xffff;
829 /* XXX Assumes 32bit ints! */
830 temp = (temp << 16) >> 16;
831 ex.X_add_number = temp;
837 /* Truncate, then sign extend the value. */
838 int temp = (ex.X_add_number >> 16) & 0xffff;
840 /* XXX Assumes 32bit ints! */
841 temp = (temp << 16) >> 16;
842 ex.X_add_number = temp;
846 case BFD_RELOC_HI16_S:
848 /* Truncate, then sign extend the value. */
849 int temp = (ex.X_add_number >> 16) & 0xffff;
851 temp += (ex.X_add_number >> 15) & 1;
853 /* XXX Assumes 32bit ints! */
854 temp = (temp << 16) >> 16;
855 ex.X_add_number = temp;
863 insn = v850_insert_operand (insn, operand, ex.X_add_number,
868 if (fc > MAX_INSN_FIXUPS)
869 as_fatal ("too many fixups");
871 /* Adjust any offsets for sst.{h,w}/sld.{h,w} instructions */
872 if (operand->flags & V850_OPERAND_ADJUST_SHORT_MEMORY)
873 ex.X_add_number >>= 1;
876 fixups[fc].opindex = *opindex_ptr;
877 fixups[fc].reloc = reloc;
885 if ((operand->flags & V850_OPERAND_REG) != 0)
887 if (!register_name (& ex))
889 errmsg = "invalid register name";
892 else if ((operand->flags & V850_OPERAND_SRG) != 0)
894 if (!system_register_name (& ex, true))
896 errmsg = "UGG invalid system register name";
899 else if ((operand->flags & V850_OPERAND_EP) != 0)
901 char * start = input_line_pointer;
902 char c = get_symbol_end ();
904 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
906 /* Put things back the way we found them. */
907 *input_line_pointer = c;
908 input_line_pointer = start;
909 errmsg = "expected EP register";
913 *input_line_pointer = c;
914 str = input_line_pointer;
915 input_line_pointer = hold;
917 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
921 else if ((operand->flags & V850_OPERAND_CC) != 0)
925 errmsg = "invalid condition code name";
928 else if (operand->flags & V850E_PUSH_POP)
930 if (! parse_register_list (& insn, operand))
932 errmsg = "invalid register list";
935 /* The parse_register_list() function has already done everything, so fake a dummy expression. */
936 ex.X_op = O_constant;
939 else if (operand->flags & V850E_IMMEDIATE16)
943 if (ex.X_op != O_constant)
944 errmsg = "constant expression expected";
945 else if (ex.X_add_number & 0xffff0000)
947 if (ex.X_add_number & 0xffff)
948 errmsg = "constant too big to fit into instruction";
949 else if ((insn & 0x001fffc0) == 0x00130780)
950 ex.X_add_number >>= 16;
952 errmsg = "constant too big to fit into instruction";
955 extra_data_after_insn = true;
957 extra_data = ex.X_add_number;
960 else if (operand->flags & V850E_IMMEDIATE32)
964 if (ex.X_op != O_constant)
965 errmsg = "constant expression expected";
967 extra_data_after_insn = true;
969 extra_data = ex.X_add_number;
972 else if (register_name (&ex)
973 && (operand->flags & V850_OPERAND_REG) == 0)
975 errmsg = "syntax error: register not expected";
977 else if (system_register_name (& ex, false)
978 && (operand->flags & V850_OPERAND_SRG) == 0)
980 errmsg = "syntax error: system register not expected";
982 else if (cc_name (&ex)
983 && (operand->flags & V850_OPERAND_CC) == 0)
985 errmsg = "syntax error: condition code not expected";
990 /* start-sanitize-v850e */
992 If we are assembling a MOV instruction (or a CALLT.... :-)
993 and the immediate value does not fit into the bits available
994 then create a fake error so that the next MOV instruction
995 will be selected. This one has a 32 bit immediate field. */
997 if (((insn & 0x07e0) == 0x0200)
998 && ex.X_op == O_constant
999 && (ex.X_add_number < (- (1 << (operand->bits - 1))) || ex.X_add_number > ((1 << operand->bits) - 1)))
1000 errmsg = "use bigger instruction";
1001 /* end-sanitize-v850e */
1007 //fprintf (stderr, "insn: %x, operand %d, op: %d, add_number: %d\n", insn, opindex_ptr - opcode->operands, ex.X_op, ex.X_add_number );
1012 errmsg = "illegal operand";
1015 errmsg = "missing operand";
1018 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
1020 errmsg = "invalid operand";
1023 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1028 insn = v850_insert_operand (insn, operand, ex.X_add_number,
1033 /* We need to generate a fixup for this expression. */
1034 if (fc >= MAX_INSN_FIXUPS)
1035 as_fatal ("too many fixups");
1036 fixups[fc].exp = ex;
1037 fixups[fc].opindex = *opindex_ptr;
1038 fixups[fc].reloc = BFD_RELOC_UNUSED;
1044 str = input_line_pointer;
1045 input_line_pointer = hold;
1047 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
1056 next_opcode = opcode + 1;
1057 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
1059 opcode = next_opcode;
1063 as_bad ("%s", errmsg);
1064 ignore_rest_of_line ();
1070 while (isspace (*str))
1074 as_bad ("junk at end of line: `%s'", str);
1076 input_line_pointer = str;
1078 /* Write out the instruction.
1080 Four byte insns have an opcode with the two high bits on. */
1081 if (relaxable && fc > 0)
1083 f = frag_var (rs_machine_dependent, 6, 4, 0,
1084 fixups[0].exp.X_add_symbol,
1085 fixups[0].exp.X_add_number,
1086 (char *)fixups[0].opindex);
1088 md_number_to_chars (f, insn, insn_size);
1089 md_number_to_chars (f + 2, 0, 4);
1094 if ((insn & 0x0600) == 0x0600)
1099 /* start-sanitize-v850e */
1100 /* Special case: 32 bit MOV */
1101 if ((insn & 0xffe0) == 0x0620)
1103 /* end_sanitize-v850e */
1105 f = frag_more (insn_size);
1107 md_number_to_chars (f, insn, insn_size);
1109 if (extra_data_after_insn)
1111 f = frag_more (extra_data_len);
1112 md_number_to_chars (f, extra_data, extra_data_len);
1114 extra_data_after_insn = false;
1118 /* Create any fixups. At this point we do not use a
1119 bfd_reloc_code_real_type, but instead just use the
1120 BFD_RELOC_UNUSED plus the operand index. This lets us easily
1121 handle fixups for any operand type, although that is admittedly
1122 not a very exciting feature. We pick a BFD reloc type in
1124 for (i = 0; i < fc; i++)
1126 const struct v850_operand *operand;
1128 operand = &v850_operands[fixups[i].opindex];
1129 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1131 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1139 size = bfd_get_reloc_size (reloc_howto);
1141 /* The "size" of a TDA_OFFSET reloc varies depending
1142 on what kind of instruction it's used in! */
1143 if (reloc_howto->type == 11 && insn_size > 2)
1146 if (size < 1 || size > 4)
1150 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
1152 reloc_howto->pc_relative,
1155 switch (fixups[i].reloc)
1157 case BFD_RELOC_LO16:
1158 case BFD_RELOC_HI16:
1159 case BFD_RELOC_HI16_S:
1160 fixP->fx_no_overflow = 1;
1166 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1168 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
1169 ((bfd_reloc_code_real_type)
1170 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
1176 /* if while processing a fixup, a reloc really needs to be created */
1177 /* then it is done here */
1180 tc_gen_reloc (seg, fixp)
1185 reloc = (arelent *) xmalloc (sizeof (arelent));
1186 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
1187 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1188 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1189 if (reloc->howto == (reloc_howto_type *) NULL)
1191 as_bad_where (fixp->fx_file, fixp->fx_line,
1192 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
1195 reloc->addend = fixp->fx_addnumber;
1196 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
1200 /* Assume everything will fit in two bytes, then expand as necessary. */
1202 md_estimate_size_before_relax (fragp, seg)
1212 md_pcrel_from (fixp)
1215 /* If the symbol is undefined, or in a section other than our own,
1216 then let the linker figure it out. */
1217 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1219 /* The symbol is undefined. Let the linker figure it out. */
1222 return fixp->fx_frag->fr_address + fixp->fx_where;
1226 md_apply_fix3 (fixp, valuep, seg)
1234 if (fixp->fx_addsy == (symbolS *) NULL)
1239 else if (fixp->fx_pcrel)
1243 value = fixp->fx_offset;
1244 if (fixp->fx_subsy != (symbolS *) NULL)
1246 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
1247 value -= S_GET_VALUE (fixp->fx_subsy);
1250 /* We don't actually support subtracting a symbol. */
1251 as_bad_where (fixp->fx_file, fixp->fx_line,
1252 "expression too complex");
1257 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
1259 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
1262 const struct v850_operand *operand;
1266 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
1267 operand = &v850_operands[opindex];
1269 /* Fetch the instruction, insert the fully resolved operand
1270 value, and stuff the instruction back again.
1272 Note the instruction has been stored in little endian
1274 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1276 insn = bfd_getl32((unsigned char *) where);
1277 insn = v850_insert_operand (insn, operand, (offsetT) value,
1278 fixp->fx_file, fixp->fx_line);
1279 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
1283 /* Nothing else to do here. */
1287 /* Determine a BFD reloc value based on the operand information.
1288 We are only prepared to turn a few of the operands into relocs. */
1290 if (operand->bits == 22)
1291 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1292 else if (operand->bits == 9)
1293 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1294 else if (operand->bits == 16)
1295 fixp->fx_r_type = BFD_RELOC_V850_16_PCREL;
1298 as_bad_where(fixp->fx_file, fixp->fx_line,
1299 "unresolved expression that must be resolved");
1304 else if (fixp->fx_done)
1306 /* We still have to insert the value into memory! */
1307 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1308 if (fixp->fx_size == 1)
1309 *where = value & 0xff;
1310 if (fixp->fx_size == 2)
1311 bfd_putl16(value & 0xffff, (unsigned char *) where);
1312 if (fixp->fx_size == 4)
1313 bfd_putl32(value, (unsigned char *) where);
1316 fixp->fx_addnumber = value;
1321 /* Insert an operand value into an instruction. */
1323 static unsigned long
1324 v850_insert_operand (insn, operand, val, file, line)
1326 const struct v850_operand * operand;
1331 if (operand->bits != 32)
1336 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1338 max = (1 << (operand->bits - 1)) - 1;
1339 min = - (1 << (operand->bits - 1));
1343 max = (1 << operand->bits) - 1;
1349 if (test < (offsetT) min || test > (offsetT) max)
1352 "operand out of range (%s not between %ld and %ld)";
1355 sprint_value (buf, test);
1356 if (file == (char *) NULL)
1357 as_warn (err, buf, min, max);
1359 as_warn_where (file, line, err, buf, min, max);
1363 if (operand->insert)
1365 const char * message = NULL;
1367 insn = (*operand->insert) (insn, val, & message);
1368 if (message != NULL)
1370 if (file == (char *) NULL)
1373 as_warn_where (file, line, message);
1377 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1382 /* Parse a cons expression. We have to handle hi(), lo(), etc
1385 parse_cons_expression_v850 (exp)
1388 /* See if there's a reloc prefix like hi() we have to handle. */
1389 hold_cons_reloc = v850_reloc_prefix ();
1391 /* Do normal expression parsing. */
1395 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1396 found a reloc prefix, then we use that reloc, else we choose an
1397 appropriate one based on the size of the expression. */
1399 cons_fix_new_v850 (frag, where, size, exp)
1405 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1408 hold_cons_reloc = BFD_RELOC_32;
1410 hold_cons_reloc = BFD_RELOC_16;
1412 hold_cons_reloc = BFD_RELOC_8;
1416 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1418 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);