1 /* tc-v850.c -- Assembler code for the NEC V850
3 Copyright (C) 1996 Free Software Foundation.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "opcode/v850.h"
28 /* Temporarily holds the reloc in a cons expression. */
29 static bfd_reloc_code_real_type hold_cons_reloc;
31 /* Structure to hold information about predefined registers. */
38 /* Generic assembler global variables which must be defined by all targets. */
40 /* Characters which always start a comment. */
41 const char comment_chars[] = "#";
43 /* Characters which start a comment at the beginning of a line. */
44 const char line_comment_chars[] = ";#";
46 /* Characters which may be used to separate multiple commands on a
48 const char line_separator_chars[] = ";";
50 /* Characters which are used to indicate an exponent in a floating
52 const char EXP_CHARS[] = "eE";
54 /* Characters which mean that a number is a floating point constant,
56 const char FLT_CHARS[] = "dD";
59 const relax_typeS md_relax_table[] = {
61 {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));
68 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
69 static boolean register_name PARAMS ((expressionS *expressionP));
70 static boolean system_register_name PARAMS ((expressionS *expressionP));
71 static boolean cc_name PARAMS ((expressionS *expressionP));
72 static bfd_reloc_code_real_type v850_reloc_prefix PARAMS ((void));
76 #define MAX_INSN_FIXUPS (5)
81 bfd_reloc_code_real_type reloc;
83 struct v850_fixup fixups[MAX_INSN_FIXUPS];
86 const char *md_shortopts = "";
87 struct option md_longopts[] = {
88 {NULL, no_argument, NULL, 0}
90 size_t md_longopts_size = sizeof(md_longopts);
92 /* The target specific pseudo-ops which we support. */
93 const pseudo_typeS md_pseudo_table[] =
99 /* Opcode hash table. */
100 static struct hash_control *v850_hash;
102 /* This table is sorted. Suitable for searching by a binary search. */
103 static const struct reg_name pre_defined_registers[] =
105 { "ep", 30 }, /* ep - element ptr */
106 { "gp", 4 }, /* gp - global ptr */
107 { "lp", 31 }, /* lp - link ptr */
140 { "sp", 3 }, /* sp - stack ptr */
141 { "tp", 5 }, /* tp - text ptr */
144 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
147 static const struct reg_name system_registers[] =
156 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
158 static const struct reg_name cc_names[] =
181 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
183 /* reg_name_search does a binary search of the given register table
184 to see if "name" is a valid regiter name. Returns the register
185 number from the array on success, or -1 on failure. */
188 reg_name_search (regs, regcount, name)
189 const struct reg_name *regs;
193 int middle, low, high;
201 middle = (low + high) / 2;
202 cmp = strcasecmp (name, regs[middle].name);
208 return regs[middle].value;
215 /* Summary of register_name().
217 * in: Input_line_pointer points to 1st char of operand.
219 * out: A expressionS.
220 * The operand may have been a register: in this case, X_op == O_register,
221 * X_add_number is set to the register number, and truth is returned.
222 * Input_line_pointer->(next non-blank) char after operand, or is in
223 * its original state.
226 register_name (expressionP)
227 expressionS *expressionP;
234 /* Find the spelling of the operand */
235 start = name = input_line_pointer;
237 c = get_symbol_end ();
238 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
240 /* look to see if it's in the register table */
243 expressionP->X_op = O_register;
244 expressionP->X_add_number = reg_number;
246 /* make the rest nice */
247 expressionP->X_add_symbol = NULL;
248 expressionP->X_op_symbol = NULL;
249 *input_line_pointer = c; /* put back the delimiting char */
254 /* reset the line as if we had not done anything */
255 *input_line_pointer = c; /* put back the delimiting char */
256 input_line_pointer = start; /* reset input_line pointer */
261 /* Summary of system_register_name().
263 * in: Input_line_pointer points to 1st char of operand.
265 * out: A expressionS.
266 * The operand may have been a register: in this case, X_op == O_register,
267 * X_add_number is set to the register number, and truth is returned.
268 * Input_line_pointer->(next non-blank) char after operand, or is in
269 * its original state.
272 system_register_name (expressionP)
273 expressionS *expressionP;
280 /* Find the spelling of the operand */
281 start = name = input_line_pointer;
283 c = get_symbol_end ();
284 reg_number = reg_name_search (system_registers, SYSREG_NAME_CNT, name);
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;
295 *input_line_pointer = c; /* put back the delimiting char */
300 /* reset the line as if we had not done anything */
301 *input_line_pointer = c; /* put back the delimiting char */
302 input_line_pointer = start; /* reset input_line pointer */
307 /* Summary of cc_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 cc_name (expressionP)
319 expressionS *expressionP;
326 /* Find the spelling of the operand */
327 start = name = input_line_pointer;
329 c = get_symbol_end ();
330 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
332 /* look to see if it's in the register table */
335 expressionP->X_op = O_constant;
336 expressionP->X_add_number = reg_number;
338 /* make the rest nice */
339 expressionP->X_add_symbol = NULL;
340 expressionP->X_op_symbol = NULL;
341 *input_line_pointer = c; /* put back the delimiting char */
346 /* reset the line as if we had not done anything */
347 *input_line_pointer = c; /* put back the delimiting char */
348 input_line_pointer = start; /* reset input_line pointer */
354 md_show_usage (stream)
357 fprintf(stream, "V850 options:\n\
362 md_parse_option (c, arg)
370 md_undefined_symbol (name)
377 md_atof (type, litp, sizep)
383 LITTLENUM_TYPE words[4];
399 return "bad call to md_atof";
402 t = atof_ieee (input_line_pointer, type, words);
404 input_line_pointer = t;
408 for (i = prec - 1; i >= 0; i--)
410 md_number_to_chars (litp, (valueT) words[i], 2);
420 md_convert_frag (abfd, sec, fragP)
425 subseg_change (sec, 0);
426 if (fragP->fr_subtype == 0)
428 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
429 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode);
433 else if (fragP->fr_subtype == 1)
435 /* Reverse the condition of the first branch. */
436 fragP->fr_literal[0] &= 0xf7;
437 /* Mask off all the displacement bits. */
438 fragP->fr_literal[0] &= 0x8f;
439 fragP->fr_literal[1] &= 0x07;
440 /* Now set the displacement bits so that we branch
441 around the unconditional branch. */
442 fragP->fr_literal[0] |= 0x30;
444 /* Now create the unconditional branch + fixup to the final
446 md_number_to_chars (&fragP->fr_literal[2], 0x00000780, 4);
447 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
448 fragP->fr_offset, 1, BFD_RELOC_UNUSED + (int)fragP->fr_opcode + 1);
457 md_section_align (seg, addr)
461 int align = bfd_get_section_alignment (stdoutput, seg);
462 return ((addr + (1 << align) - 1) & (-1 << align));
468 char *prev_name = "";
469 register const struct v850_opcode *op;
471 v850_hash = hash_new();
473 /* Insert unique names into hash table. The V850 instruction set
474 has many identical opcode names that have different opcodes based
475 on the operands. This hash table then provides a quick index to
476 the first opcode with a particular name in the opcode table. */
481 if (strcmp (prev_name, op->name))
483 prev_name = (char *) op->name;
484 hash_insert (v850_hash, op->name, (char *) op);
490 static bfd_reloc_code_real_type
493 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
495 input_line_pointer += 3;
496 return BFD_RELOC_HI16;
498 if (strncmp(input_line_pointer, "hi(", 3) == 0)
500 input_line_pointer += 2;
501 return BFD_RELOC_HI16_S;
503 if (strncmp (input_line_pointer, "lo(", 3) == 0)
505 input_line_pointer += 2;
506 return BFD_RELOC_LO16;
509 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
511 input_line_pointer += 6;
512 return BFD_RELOC_V850_SDA_OFFSET;
515 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
517 input_line_pointer += 6;
518 return BFD_RELOC_V850_ZDA_OFFSET;
521 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
523 input_line_pointer += 6;
524 return BFD_RELOC_V850_TDA_OFFSET;
528 if (strncmp(input_line_pointer, "(hi0(", 5) == 0)
530 input_line_pointer += 4;
531 return BFD_RELOC_HI16;
533 if (strncmp(input_line_pointer, "(hi(", 4) == 0)
535 input_line_pointer += 3;
536 return BFD_RELOC_HI16_S;
538 if (strncmp (input_line_pointer, "(lo(", 4) == 0)
540 input_line_pointer += 3;
541 return BFD_RELOC_LO16;
544 if (strncmp (input_line_pointer, "(sdaoff(", 8) == 0)
546 input_line_pointer += 7;
547 return BFD_RELOC_V850_SDA_OFFSET;
550 if (strncmp (input_line_pointer, "(zdaoff(", 8) == 0)
552 input_line_pointer += 7;
553 return BFD_RELOC_V850_ZDA_OFFSET;
556 if (strncmp (input_line_pointer, "(tdaoff(", 8) == 0)
558 input_line_pointer += 7;
559 return BFD_RELOC_V850_TDA_OFFSET;
562 return BFD_RELOC_UNUSED;
570 struct v850_opcode *opcode;
571 struct v850_opcode *next_opcode;
572 const unsigned char *opindex_ptr;
573 int next_opindex, relaxable;
574 unsigned long insn, insn_size;
578 bfd_reloc_code_real_type reloc;
580 /* Get the opcode. */
581 for (s = str; *s != '\0' && ! isspace (*s); s++)
586 /* find the first opcode with the proper name */
587 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
590 as_bad ("Unrecognized opcode: `%s'", str);
595 while (isspace (*str))
598 input_line_pointer = str;
602 const char *errmsg = NULL;
608 insn = opcode->opcode;
609 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
611 const struct v850_operand *operand;
615 if (next_opindex == 0)
617 operand = &v850_operands[*opindex_ptr];
621 operand = &v850_operands[next_opindex];
627 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
630 if (operand->flags & V850_OPERAND_RELAX)
633 /* Gather the operand. */
634 hold = input_line_pointer;
635 input_line_pointer = str;
638 /* lo(), hi(), hi0(), etc... */
639 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
643 if (ex.X_op == O_constant)
649 /* Truncate, then sign extend the value. */
650 int temp = ex.X_add_number & 0xffff;
652 /* XXX Assumes 32bit ints! */
653 temp = (temp << 16) >> 16;
654 ex.X_add_number = temp;
659 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff);
662 case BFD_RELOC_HI16_S:
663 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff)
664 + ((ex.X_add_number >> 15) & 1);
671 insn = v850_insert_operand (insn, operand, ex.X_add_number,
676 if (fc > MAX_INSN_FIXUPS)
677 as_fatal ("too many fixups");
680 fixups[fc].opindex = *opindex_ptr;
681 fixups[fc].reloc = reloc;
687 if ((operand->flags & V850_OPERAND_REG) != 0)
689 if (!register_name(&ex))
691 errmsg = "invalid register name";
695 else if ((operand->flags & V850_OPERAND_SRG) != 0)
697 if (!system_register_name(&ex))
699 errmsg = "invalid system register name";
703 else if ((operand->flags & V850_OPERAND_EP) != 0)
705 char *start = input_line_pointer;
706 char c = get_symbol_end ();
707 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
709 /* Put things back the way we found them. */
710 *input_line_pointer = c;
711 input_line_pointer = start;
712 errmsg = "expected EP register";
715 *input_line_pointer = c;
716 str = input_line_pointer;
717 input_line_pointer = hold;
719 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
723 else if ((operand->flags & V850_OPERAND_CC) != 0)
727 errmsg = "invalid condition code name";
731 else if (register_name (&ex)
732 && (operand->flags & V850_OPERAND_REG) == 0)
734 errmsg = "syntax error: register not expected";
737 else if (system_register_name (&ex)
738 && (operand->flags & V850_OPERAND_SRG) == 0)
740 errmsg = "syntax error: system register not expected";
743 else if (cc_name (&ex)
744 && (operand->flags & V850_OPERAND_CC) == 0)
746 errmsg = "syntax error: condition code not expected";
757 errmsg = "illegal operand";
760 errmsg = "missing operand";
763 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
765 errmsg = "invalid operand";
769 insn = v850_insert_operand (insn, operand, ex.X_add_number,
774 insn = v850_insert_operand (insn, operand, ex.X_add_number,
779 /* We need to generate a fixup for this expression. */
780 if (fc >= MAX_INSN_FIXUPS)
781 as_fatal ("too many fixups");
783 fixups[fc].opindex = *opindex_ptr;
784 fixups[fc].reloc = BFD_RELOC_UNUSED;
791 str = input_line_pointer;
792 input_line_pointer = hold;
794 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']'
803 next_opcode = opcode + 1;
804 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
806 opcode = next_opcode;
810 as_bad ("%s", errmsg);
816 while (isspace (*str))
820 as_bad ("junk at end of line: `%s'", str);
822 input_line_pointer = str;
824 /* Write out the instruction.
826 Four byte insns have an opcode with the two high bits on. */
827 if (relaxable && fc > 0)
829 f = frag_var (rs_machine_dependent, 6, 4, 0,
830 fixups[0].exp.X_add_symbol, 0, (char *)fixups[0].opindex);
832 md_number_to_chars (f, insn, insn_size);
833 md_number_to_chars (f + 2, 0, 4);
836 else if ((insn & 0x0600) == 0x0600)
839 f = frag_more (insn_size);
840 md_number_to_chars (f, insn, insn_size);
845 f = frag_more (insn_size);
846 md_number_to_chars (f, insn, insn_size);
849 /* Create any fixups. At this point we do not use a
850 bfd_reloc_code_real_type, but instead just use the
851 BFD_RELOC_UNUSED plus the operand index. This lets us easily
852 handle fixups for any operand type, although that is admittedly
853 not a very exciting feature. We pick a BFD reloc type in
855 for (i = 0; i < fc; i++)
857 const struct v850_operand *operand;
859 operand = &v850_operands[fixups[i].opindex];
860 if (fixups[i].reloc != BFD_RELOC_UNUSED)
862 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
870 size = bfd_get_reloc_size (reloc_howto);
872 /* The "size" of a TDA_OFFSET reloc varies depending
873 on what kind of instruction it's used in! */
874 if (reloc_howto->type == 11 && insn_size > 2)
877 if (size < 1 || size > 4)
881 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
883 reloc_howto->pc_relative,
888 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
890 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
891 ((bfd_reloc_code_real_type)
892 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
898 /* if while processing a fixup, a reloc really needs to be created */
899 /* then it is done here */
902 tc_gen_reloc (seg, fixp)
907 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
908 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
909 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
910 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
911 if (reloc->howto == (reloc_howto_type *) NULL)
913 as_bad_where (fixp->fx_file, fixp->fx_line,
914 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
917 reloc->addend = fixp->fx_addnumber;
918 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
922 /* Assume everything will fit in two bytes, then expand as necessary. */
924 md_estimate_size_before_relax (fragp, seg)
937 /* If the symbol is undefined, or in a section other than our own,
938 then let the linker figure it out. */
939 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
941 /* The symbol is undefined. Let the linker figure it out. */
944 return fixp->fx_frag->fr_address + fixp->fx_where;
948 md_apply_fix3 (fixp, valuep, seg)
956 if (fixp->fx_addsy == (symbolS *) NULL)
961 else if (fixp->fx_pcrel)
965 value = fixp->fx_offset;
966 if (fixp->fx_subsy != (symbolS *) NULL)
968 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
969 value -= S_GET_VALUE (fixp->fx_subsy);
972 /* We don't actually support subtracting a symbol. */
973 as_bad_where (fixp->fx_file, fixp->fx_line,
974 "expression too complex");
979 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
981 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
984 const struct v850_operand *operand;
988 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
989 operand = &v850_operands[opindex];
991 /* Fetch the instruction, insert the fully resolved operand
992 value, and stuff the instruction back again.
994 Note the instruction has been stored in little endian
996 where = fixp->fx_frag->fr_literal + fixp->fx_where;
998 insn = bfd_getl32((unsigned char *) where);
999 insn = v850_insert_operand (insn, operand, (offsetT) value,
1000 fixp->fx_file, fixp->fx_line);
1001 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
1005 /* Nothing else to do here. */
1009 /* Determine a BFD reloc value based on the operand information.
1010 We are only prepared to turn a few of the operands into relocs. */
1012 if (operand->bits == 22)
1013 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
1014 else if (operand->bits == 9)
1015 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
1018 as_bad_where(fixp->fx_file, fixp->fx_line,
1019 "unresolved expression that must be resolved");
1024 else if (fixp->fx_done)
1026 /* We still have to insert the value into memory! */
1027 where = fixp->fx_frag->fr_literal + fixp->fx_where;
1028 if (fixp->fx_size == 1)
1029 *where = value & 0xff;
1030 if (fixp->fx_size == 2)
1031 bfd_putl16(value & 0xffff, (unsigned char *) where);
1032 if (fixp->fx_size == 4)
1033 bfd_putl32(value, (unsigned char *) where);
1036 fixp->fx_addnumber = value;
1041 /* Insert an operand value into an instruction. */
1043 static unsigned long
1044 v850_insert_operand (insn, operand, val, file, line)
1046 const struct v850_operand *operand;
1051 if (operand->bits != 16)
1056 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
1058 max = (1 << (operand->bits - 1)) - 1;
1059 min = - (1 << (operand->bits - 1));
1063 max = (1 << operand->bits) - 1;
1070 if (test < (offsetT) min || test > (offsetT) max)
1073 "operand out of range (%s not between %ld and %ld)";
1076 sprint_value (buf, test);
1077 if (file == (char *) NULL)
1078 as_warn (err, buf, min, max);
1080 as_warn_where (file, line, err, buf, min, max);
1084 if (operand->insert)
1086 const char *message = NULL;
1087 insn = (*operand->insert) (insn, val, &message);
1088 if (message != NULL)
1090 if (file == (char *) NULL)
1093 as_warn_where (file, line, message);
1097 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
1101 /* Parse a cons expression. We have to handle hi(), lo(), etc
1104 parse_cons_expression_v850 (exp)
1107 /* See if there's a reloc prefix like hi() we have to handle. */
1108 hold_cons_reloc = v850_reloc_prefix ();
1110 /* Do normal expression parsing. */
1114 /* Create a fixup for a cons expression. If parse_cons_expression_v850
1115 found a reloc prefix, then we use that reloc, else we choose an
1116 appropriate one based on the size of the expression. */
1118 cons_fix_new_v850 (frag, where, size, exp)
1124 if (hold_cons_reloc == BFD_RELOC_UNUSED)
1127 hold_cons_reloc = BFD_RELOC_32;
1129 hold_cons_reloc = BFD_RELOC_16;
1131 hold_cons_reloc = BFD_RELOC_8;
1135 fix_new_exp (frag, where, size, exp, 0, hold_cons_reloc);
1137 fix_new (frag, where, size, NULL, 0, 0, hold_cons_reloc);