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},
64 static unsigned long v850_insert_operand
65 PARAMS ((unsigned long insn, const struct v850_operand *operand,
66 offsetT val, char *file, unsigned int line));
67 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
68 static boolean register_name PARAMS ((expressionS *expressionP));
69 static boolean system_register_name PARAMS ((expressionS *expressionP));
70 static boolean cc_name PARAMS ((expressionS *expressionP));
71 static bfd_reloc_code_real_type v850_reloc_prefix PARAMS ((void));
75 #define MAX_INSN_FIXUPS (5)
80 bfd_reloc_code_real_type reloc;
82 struct v850_fixup fixups[MAX_INSN_FIXUPS];
85 const char *md_shortopts = "";
86 struct option md_longopts[] = {
87 {NULL, no_argument, NULL, 0}
89 size_t md_longopts_size = sizeof(md_longopts);
91 /* The target specific pseudo-ops which we support. */
92 const pseudo_typeS md_pseudo_table[] =
98 /* Opcode hash table. */
99 static struct hash_control *v850_hash;
101 /* This table is sorted. Suitable for searching by a binary search. */
102 static const struct reg_name pre_defined_registers[] =
104 { "ep", 30 }, /* ep - element ptr */
105 { "gp", 4 }, /* gp - global ptr */
106 { "lp", 31 }, /* lp - link ptr */
139 { "sp", 3 }, /* sp - stack ptr */
140 { "tp", 5 }, /* tp - text ptr */
143 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
146 static const struct reg_name system_registers[] =
155 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
157 static const struct reg_name cc_names[] =
180 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
182 /* reg_name_search does a binary search of the given register table
183 to see if "name" is a valid regiter name. Returns the register
184 number from the array on success, or -1 on failure. */
187 reg_name_search (regs, regcount, name)
188 const struct reg_name *regs;
192 int middle, low, high;
200 middle = (low + high) / 2;
201 cmp = strcasecmp (name, regs[middle].name);
207 return regs[middle].value;
214 /* Summary of register_name().
216 * in: Input_line_pointer points to 1st char of operand.
218 * out: A expressionS.
219 * The operand may have been a register: in this case, X_op == O_register,
220 * X_add_number is set to the register number, and truth is returned.
221 * Input_line_pointer->(next non-blank) char after operand, or is in
222 * its original state.
225 register_name (expressionP)
226 expressionS *expressionP;
233 /* Find the spelling of the operand */
234 start = name = input_line_pointer;
236 c = get_symbol_end ();
237 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
239 /* look to see if it's in the register table */
242 expressionP->X_op = O_register;
243 expressionP->X_add_number = reg_number;
245 /* make the rest nice */
246 expressionP->X_add_symbol = NULL;
247 expressionP->X_op_symbol = NULL;
248 *input_line_pointer = c; /* put back the delimiting char */
253 /* reset the line as if we had not done anything */
254 *input_line_pointer = c; /* put back the delimiting char */
255 input_line_pointer = start; /* reset input_line pointer */
260 /* Summary of system_register_name().
262 * in: Input_line_pointer points to 1st char of operand.
264 * out: A expressionS.
265 * The operand may have been a register: in this case, X_op == O_register,
266 * X_add_number is set to the register number, and truth is returned.
267 * Input_line_pointer->(next non-blank) char after operand, or is in
268 * its original state.
271 system_register_name (expressionP)
272 expressionS *expressionP;
279 /* Find the spelling of the operand */
280 start = name = input_line_pointer;
282 c = get_symbol_end ();
283 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
285 /* look to see if it's in the register table */
288 expressionP->X_op = O_register;
289 expressionP->X_add_number = reg_number;
291 /* make the rest nice */
292 expressionP->X_add_symbol = NULL;
293 expressionP->X_op_symbol = NULL;
294 *input_line_pointer = c; /* put back the delimiting char */
299 /* reset the line as if we had not done anything */
300 *input_line_pointer = c; /* put back the delimiting char */
301 input_line_pointer = start; /* reset input_line pointer */
306 /* Summary of cc_name().
308 * in: Input_line_pointer points to 1st char of operand.
310 * out: A expressionS.
311 * The operand may have been a register: in this case, X_op == O_register,
312 * X_add_number is set to the register number, and truth is returned.
313 * Input_line_pointer->(next non-blank) char after operand, or is in
314 * its original state.
317 cc_name (expressionP)
318 expressionS *expressionP;
325 /* Find the spelling of the operand */
326 start = name = input_line_pointer;
328 c = get_symbol_end ();
329 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
331 /* look to see if it's in the register table */
334 expressionP->X_op = O_constant;
335 expressionP->X_add_number = reg_number;
337 /* make the rest nice */
338 expressionP->X_add_symbol = NULL;
339 expressionP->X_op_symbol = NULL;
340 *input_line_pointer = c; /* put back the delimiting char */
345 /* reset the line as if we had not done anything */
346 *input_line_pointer = c; /* put back the delimiting char */
347 input_line_pointer = start; /* reset input_line pointer */
353 md_show_usage (stream)
356 fprintf(stream, "V850 options:\n\
361 md_parse_option (c, arg)
369 md_undefined_symbol (name)
376 md_atof (type, litp, sizep)
382 LITTLENUM_TYPE words[4];
398 return "bad call to md_atof";
401 t = atof_ieee (input_line_pointer, type, words);
403 input_line_pointer = t;
407 for (i = prec - 1; i >= 0; i--)
409 md_number_to_chars (litp, (valueT) words[i], 2);
419 md_convert_frag (abfd, sec, fragP)
424 subseg_change (sec, 0);
425 if (fragP->fr_subtype == 0)
427 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
428 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
432 else if (fragP->fr_subtype == 1)
434 /* Reverse the condition of the first branch. */
435 fragP->fr_literal[0] &= 0xf7;
436 /* Mask off all the displacement bits. */
437 fragP->fr_literal[0] &= 0x8f;
438 fragP->fr_literal[1] &= 0x07;
439 /* Now set the displacement bits so that we branch
440 around the unconditional branch. */
441 fragP->fr_literal[0] |= 0x30;
443 /* Now create the unconditional branch + fixup to the final
445 md_number_to_chars (&fragP->fr_literal[2], 0x00000780, 4);
446 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
447 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
456 md_section_align (seg, addr)
460 int align = bfd_get_section_alignment (stdoutput, seg);
461 return ((addr + (1 << align) - 1) & (-1 << align));
467 char *prev_name = "";
468 register const struct v850_opcode *op;
470 v850_hash = hash_new();
472 /* Insert unique names into hash table. The V850 instruction set
473 has many identical opcode names that have different opcodes based
474 on the operands. This hash table then provides a quick index to
475 the first opcode with a particular name in the opcode table. */
480 if (strcmp (prev_name, op->name))
482 prev_name = (char *) op->name;
483 hash_insert (v850_hash, op->name, (char *) op);
489 static bfd_reloc_code_real_type
492 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
494 input_line_pointer += 3;
495 return BFD_RELOC_HI16;
497 if (strncmp(input_line_pointer, "hi(", 3) == 0)
499 input_line_pointer += 2;
500 return BFD_RELOC_HI16_S;
502 if (strncmp (input_line_pointer, "lo(", 3) == 0)
504 input_line_pointer += 2;
505 return BFD_RELOC_LO16;
508 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
510 input_line_pointer += 6;
511 return BFD_RELOC_V850_SDA_OFFSET;
514 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
516 input_line_pointer += 6;
517 return BFD_RELOC_V850_ZDA_OFFSET;
520 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
522 input_line_pointer += 6;
523 return BFD_RELOC_V850_TDA_OFFSET;
527 if (strncmp(input_line_pointer, "(hi0(", 5) == 0)
529 input_line_pointer += 4;
530 return BFD_RELOC_HI16;
532 if (strncmp(input_line_pointer, "(hi(", 4) == 0)
534 input_line_pointer += 3;
535 return BFD_RELOC_HI16_S;
537 if (strncmp (input_line_pointer, "(lo(", 4) == 0)
539 input_line_pointer += 3;
540 return BFD_RELOC_LO16;
543 if (strncmp (input_line_pointer, "(sdaoff(", 8) == 0)
545 input_line_pointer += 7;
546 return BFD_RELOC_V850_SDA_OFFSET;
549 if (strncmp (input_line_pointer, "(zdaoff(", 8) == 0)
551 input_line_pointer += 7;
552 return BFD_RELOC_V850_ZDA_OFFSET;
555 if (strncmp (input_line_pointer, "(tdaoff(", 8) == 0)
557 input_line_pointer += 7;
558 return BFD_RELOC_V850_TDA_OFFSET;
561 return BFD_RELOC_UNUSED;
569 struct v850_opcode *opcode;
570 struct v850_opcode *next_opcode;
571 const unsigned char *opindex_ptr;
572 int next_opindex, relaxable;
573 unsigned long insn, insn_size;
577 bfd_reloc_code_real_type reloc;
579 /* Get the opcode. */
580 for (s = str; *s != '\0' && ! isspace (*s); s++)
585 /* find the first opcode with the proper name */
586 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
589 as_bad ("Unrecognized opcode: `%s'", str);
594 while (isspace (*str))
597 input_line_pointer = str;
601 const char *errmsg = NULL;
607 insn = opcode->opcode;
608 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
610 const struct v850_operand *operand;
614 if (next_opindex == 0)
616 operand = &v850_operands[*opindex_ptr];
620 operand = &v850_operands[next_opindex];
626 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
629 if (operand->flags & V850_OPERAND_RELAX)
632 /* Gather the operand. */
633 hold = input_line_pointer;
634 input_line_pointer = str;
637 /* lo(), hi(), hi0(), etc... */
638 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
642 if (ex.X_op == O_constant)
648 /* Truncate, then sign extend the value. */
649 int temp = ex.X_add_number & 0xffff;
651 /* XXX Assumes 32bit ints! */
652 temp = (temp << 16) >> 16;
653 ex.X_add_number = temp;
658 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff);
661 case BFD_RELOC_HI16_S:
662 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff)
663 + ((ex.X_add_number >> 15) & 1);
670 insn = v850_insert_operand (insn, operand, ex.X_add_number,
675 if (fc > MAX_INSN_FIXUPS)
676 as_fatal ("too many fixups");
678 /* Adjust any offsets for sst.{h,w}/sld.{h,w} instructions */
679 if (operand->flags & V850_OPERAND_ADJUST_SHORT_MEMORY)
680 ex.X_add_number >>= 1;
683 fixups[fc].opindex = *opindex_ptr;
684 fixups[fc].reloc = reloc;
690 if ((operand->flags & V850_OPERAND_REG) != 0)
692 if (!register_name(&ex))
694 errmsg = "invalid register name";
698 else if ((operand->flags & V850_OPERAND_SRG) != 0)
700 if (!system_register_name(&ex))
702 errmsg = "invalid system register name";
706 else if ((operand->flags & V850_OPERAND_EP) != 0)
708 char *start = input_line_pointer;
709 char c = get_symbol_end ();
710 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
712 /* Put things back the way we found them. */
713 *input_line_pointer = c;
714 input_line_pointer = start;
715 errmsg = "expected EP register";
718 *input_line_pointer = c;
719 str = input_line_pointer;
720 input_line_pointer = hold;
722 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
726 else if ((operand->flags & V850_OPERAND_CC) != 0)
730 errmsg = "invalid condition code name";
734 else if (register_name (&ex)
735 && (operand->flags & V850_OPERAND_REG) == 0)
737 errmsg = "syntax error: register not expected";
740 else if (system_register_name (&ex)
741 && (operand->flags & V850_OPERAND_SRG) == 0)
743 errmsg = "syntax error: system register not expected";
746 else if (cc_name (&ex)
747 && (operand->flags & V850_OPERAND_CC) == 0)
749 errmsg = "syntax error: condition code not expected";
760 errmsg = "illegal operand";
763 errmsg = "missing operand";
766 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
768 errmsg = "invalid operand";
772 insn = v850_insert_operand (insn, operand, ex.X_add_number,
777 insn = v850_insert_operand (insn, operand, ex.X_add_number,
782 /* We need to generate a fixup for this expression. */
783 if (fc >= MAX_INSN_FIXUPS)
784 as_fatal ("too many fixups");
786 fixups[fc].opindex = *opindex_ptr;
787 fixups[fc].reloc = BFD_RELOC_UNUSED;
794 str = input_line_pointer;
795 input_line_pointer = hold;
797 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
806 next_opcode = opcode + 1;
807 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
809 opcode = next_opcode;
813 as_bad ("%s", errmsg);
819 while (isspace (*str))
823 as_bad ("junk at end of line: `%s'", str);
825 input_line_pointer = str;
827 /* Write out the instruction.
829 Four byte insns have an opcode with the two high bits on. */
830 if (relaxable && fc > 0)
832 f = frag_var (rs_machine_dependent, 6, 4, 0,
833 fixups[0].exp.X_add_symbol,
834 fixups[0].exp.X_add_number,
835 (char *)fixups[0].opindex);
837 md_number_to_chars (f, insn, insn_size);
838 md_number_to_chars (f + 2, 0, 4);
841 else if ((insn & 0x0600) == 0x0600)
844 f = frag_more (insn_size);
845 md_number_to_chars (f, insn, insn_size);
850 f = frag_more (insn_size);
851 md_number_to_chars (f, insn, insn_size);
854 /* Create any fixups. At this point we do not use a
855 bfd_reloc_code_real_type, but instead just use the
856 BFD_RELOC_UNUSED plus the operand index. This lets us easily
857 handle fixups for any operand type, although that is admittedly
858 not a very exciting feature. We pick a BFD reloc type in
860 for (i = 0; i < fc; i++)
862 const struct v850_operand *operand;
864 operand = &v850_operands[fixups[i].opindex];
865 if (fixups[i].reloc != BFD_RELOC_UNUSED)
867 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
875 size = bfd_get_reloc_size (reloc_howto);
877 /* The "size" of a TDA_OFFSET reloc varies depending
878 on what kind of instruction it's used in! */
879 if (reloc_howto->type == 11 && insn_size > 2)
882 if (size < 1 || size > 4)
886 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
888 reloc_howto->pc_relative,
893 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
895 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
896 ((bfd_reloc_code_real_type)
897 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
903 /* if while processing a fixup, a reloc really needs to be created */
904 /* then it is done here */
907 tc_gen_reloc (seg, fixp)
912 reloc = (arelent *) xmalloc (sizeof (arelent));
913 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
914 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
915 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
916 if (reloc->howto == (reloc_howto_type *) NULL)
918 as_bad_where (fixp->fx_file, fixp->fx_line,
919 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
922 reloc->addend = fixp->fx_addnumber;
923 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
927 /* Assume everything will fit in two bytes, then expand as necessary. */
929 md_estimate_size_before_relax (fragp, seg)
942 /* If the symbol is undefined, or in a section other than our own,
943 then let the linker figure it out. */
944 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
946 /* The symbol is undefined. Let the linker figure it out. */
949 return fixp->fx_frag->fr_address + fixp->fx_where;
953 md_apply_fix3 (fixp, valuep, seg)
961 if (fixp->fx_addsy == (symbolS *) NULL)
966 else if (fixp->fx_pcrel)
970 value = fixp->fx_offset;
971 if (fixp->fx_subsy != (symbolS *) NULL)
973 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
974 value -= S_GET_VALUE (fixp->fx_subsy);
977 /* We don't actually support subtracting a symbol. */
978 as_bad_where (fixp->fx_file, fixp->fx_line,
979 "expression too complex");
984 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
986 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
989 const struct v850_operand *operand;
993 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
994 operand = &v850_operands[opindex];
996 /* Fetch the instruction, insert the fully resolved operand
997 value, and stuff the instruction back again.
999 Note the instruction has been stored in little endian
1001 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1003 insn = bfd_getl32((unsigned char *) where);
1004 insn = v850_insert_operand (insn, operand, (offsetT) value,
1005 fixp->fx_file, fixp->fx_line);
1006 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
1010 /* Nothing else to do here. */
1014 /* Determine a BFD reloc value based on the operand information.
1015 We are only prepared to turn a few of the operands into relocs. */
1017 if (operand->bits == 22)
1018 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1019 else if (operand->bits == 9)
1020 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1023 as_bad_where(fixp->fx_file, fixp->fx_line,
1024 "unresolved expression that must be resolved");
1029 else if (fixp->fx_done)
1031 /* We still have to insert the value into memory! */
1032 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1033 if (fixp->fx_size == 1)
1034 *where = value & 0xff;
1035 if (fixp->fx_size == 2)
1036 bfd_putl16(value & 0xffff, (unsigned char *) where);
1037 if (fixp->fx_size == 4)
1038 bfd_putl32(value, (unsigned char *) where);
1041 fixp->fx_addnumber = value;
1046 /* Insert an operand value into an instruction. */
1048 static unsigned long
1049 v850_insert_operand (insn, operand, val, file, line)
1051 const struct v850_operand *operand;
1056 if (operand->bits != 16)
1061 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1063 max = (1 << (operand->bits - 1)) - 1;
1064 min = - (1 << (operand->bits - 1));
1068 max = (1 << operand->bits) - 1;
1075 if (test < (offsetT) min || test > (offsetT) max)
1078 "operand out of range (%s not between %ld and %ld)";
1081 sprint_value (buf, test);
1082 if (file == (char *) NULL)
1083 as_warn (err, buf, min, max);
1085 as_warn_where (file, line, err, buf, min, max);
1089 if (operand->insert)
1091 const char *message = NULL;
1092 insn = (*operand->insert) (insn, val, &message);
1093 if (message != NULL)
1095 if (file == (char *) NULL)
1098 as_warn_where (file, line, message);
1102 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1106 /* Parse a cons expression. We have to handle hi(), lo(), etc
1109 parse_cons_expression_v850 (exp)
1112 /* See if there's a reloc prefix like hi() we have to handle. */
1113 hold_cons_reloc = v850_reloc_prefix ();
1115 /* Do normal expression parsing. */
1119 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1120 found a reloc prefix, then we use that reloc, else we choose an
1121 appropriate one based on the size of the expression. */
1123 cons_fix_new_v850 (frag, where, size, exp)
1129 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1132 hold_cons_reloc = BFD_RELOC_32;
1134 hold_cons_reloc = BFD_RELOC_16;
1136 hold_cons_reloc = BFD_RELOC_8;
1140 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1142 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);