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 /* Structure to hold information about predefined registers. */
35 /* Generic assembler global variables which must be defined by all targets. */
37 /* Characters which always start a comment. */
38 const char comment_chars[] = "#";
40 /* Characters which start a comment at the beginning of a line. */
41 const char line_comment_chars[] = ";#";
43 /* Characters which may be used to separate multiple commands on a
45 const char line_separator_chars[] = ";";
47 /* Characters which are used to indicate an exponent in a floating
49 const char EXP_CHARS[] = "eE";
51 /* Characters which mean that a number is a floating point constant,
53 const char FLT_CHARS[] = "dD";
57 static unsigned long v850_insert_operand
58 PARAMS ((unsigned long insn, const struct v850_operand *operand,
59 offsetT val, char *file, unsigned int line));
60 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
61 static boolean register_name PARAMS ((expressionS *expressionP));
62 static boolean system_register_name PARAMS ((expressionS *expressionP));
63 static boolean cc_name PARAMS ((expressionS *expressionP));
64 static bfd_reloc_code_real_type v850_reloc_prefix PARAMS ((void));
68 #define MAX_INSN_FIXUPS (5)
73 bfd_reloc_code_real_type reloc;
75 struct v850_fixup fixups[MAX_INSN_FIXUPS];
78 const char *md_shortopts = "";
79 struct option md_longopts[] = {
80 {NULL, no_argument, NULL, 0}
82 size_t md_longopts_size = sizeof(md_longopts);
84 /* The target specific pseudo-ops which we support. */
85 const pseudo_typeS md_pseudo_table[] =
90 /* Opcode hash table. */
91 static struct hash_control *v850_hash;
93 /* This table is sorted. Suitable for searching by a binary search. */
94 static const struct reg_name pre_defined_registers[] =
96 { "ep", 30 }, /* ep - element ptr */
97 { "gp", 4 }, /* gp - global ptr */
98 { "lp", 31 }, /* lp - link ptr */
131 { "sp", 3 }, /* sp - stack ptr */
132 { "tp", 5 }, /* tp - text ptr */
135 #define REG_NAME_CNT (sizeof(pre_defined_registers) / sizeof(struct reg_name))
138 static const struct reg_name system_registers[] =
147 #define SYSREG_NAME_CNT (sizeof(system_registers) / sizeof(struct reg_name))
149 static const struct reg_name cc_names[] =
172 #define CC_NAME_CNT (sizeof(cc_names) / sizeof(struct reg_name))
174 /* reg_name_search does a binary search of the given register table
175 to see if "name" is a valid regiter name. Returns the register
176 number from the array on success, or -1 on failure. */
179 reg_name_search (regs, regcount, name)
180 const struct reg_name *regs;
184 int middle, low, high;
192 middle = (low + high) / 2;
193 cmp = strcasecmp (name, regs[middle].name);
199 return regs[middle].value;
206 /* Summary of register_name().
208 * in: Input_line_pointer points to 1st char of operand.
210 * out: A expressionS.
211 * The operand may have been a register: in this case, X_op == O_register,
212 * X_add_number is set to the register number, and truth is returned.
213 * Input_line_pointer->(next non-blank) char after operand, or is in
214 * its original state.
217 register_name (expressionP)
218 expressionS *expressionP;
225 /* Find the spelling of the operand */
226 start = name = input_line_pointer;
228 c = get_symbol_end ();
229 reg_number = reg_name_search (pre_defined_registers, REG_NAME_CNT, name);
231 /* look to see if it's in the register table */
234 expressionP->X_op = O_register;
235 expressionP->X_add_number = reg_number;
237 /* make the rest nice */
238 expressionP->X_add_symbol = NULL;
239 expressionP->X_op_symbol = NULL;
240 *input_line_pointer = c; /* put back the delimiting char */
245 /* reset the line as if we had not done anything */
246 *input_line_pointer = c; /* put back the delimiting char */
247 input_line_pointer = start; /* reset input_line pointer */
252 /* Summary of system_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 system_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 (system_registers, SYSREG_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 cc_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 cc_name (expressionP)
310 expressionS *expressionP;
317 /* Find the spelling of the operand */
318 start = name = input_line_pointer;
320 c = get_symbol_end ();
321 reg_number = reg_name_search (cc_names, CC_NAME_CNT, name);
323 /* look to see if it's in the register table */
326 expressionP->X_op = O_constant;
327 expressionP->X_add_number = reg_number;
329 /* make the rest nice */
330 expressionP->X_add_symbol = NULL;
331 expressionP->X_op_symbol = NULL;
332 *input_line_pointer = c; /* put back the delimiting char */
337 /* reset the line as if we had not done anything */
338 *input_line_pointer = c; /* put back the delimiting char */
339 input_line_pointer = start; /* reset input_line pointer */
345 md_show_usage (stream)
348 fprintf(stream, "V850 options:\n\
353 md_parse_option (c, arg)
361 md_undefined_symbol (name)
368 md_atof (type, litp, sizep)
374 LITTLENUM_TYPE words[4];
390 return "bad call to md_atof";
393 t = atof_ieee (input_line_pointer, type, words);
395 input_line_pointer = t;
399 for (i = prec - 1; i >= 0; i--)
401 md_number_to_chars (litp, (valueT) words[i], 2);
410 md_convert_frag (abfd, sec, fragP)
415 /* printf ("call to md_convert_frag \n"); */
420 md_section_align (seg, addr)
424 int align = bfd_get_section_alignment (stdoutput, seg);
425 return ((addr + (1 << align) - 1) & (-1 << align));
431 char *prev_name = "";
432 register const struct v850_opcode *op;
434 v850_hash = hash_new();
436 /* Insert unique names into hash table. The V850 instruction set
437 has many identical opcode names that have different opcodes based
438 on the operands. This hash table then provides a quick index to
439 the first opcode with a particular name in the opcode table. */
444 if (strcmp (prev_name, op->name))
446 prev_name = (char *) op->name;
447 hash_insert (v850_hash, op->name, (char *) op);
453 static bfd_reloc_code_real_type
456 if (strncmp(input_line_pointer, "hi0(", 4) == 0)
458 input_line_pointer += 4;
459 return BFD_RELOC_HI16;
461 if (strncmp(input_line_pointer, "hi(", 3) == 0)
463 input_line_pointer += 3;
464 return BFD_RELOC_HI16_S;
466 if (strncmp (input_line_pointer, "lo(", 3) == 0)
468 input_line_pointer += 3;
469 return BFD_RELOC_LO16;
472 if (strncmp (input_line_pointer, "sdaoff(", 7) == 0)
474 input_line_pointer += 7;
475 return BFD_RELOC_V850_SDA_OFFSET;
478 if (strncmp (input_line_pointer, "zdaoff(", 7) == 0)
480 input_line_pointer += 7;
481 return BFD_RELOC_V850_ZDA_OFFSET;
484 if (strncmp (input_line_pointer, "tdaoff(", 7) == 0)
486 input_line_pointer += 7;
487 return BFD_RELOC_V850_TDA_OFFSET;
490 /* FIXME: implement sda, tda, zda here */
492 return BFD_RELOC_UNUSED;
500 struct v850_opcode *opcode;
501 struct v850_opcode *next_opcode;
502 const unsigned char *opindex_ptr;
504 unsigned long insn, size;
508 bfd_reloc_code_real_type reloc;
510 /* Get the opcode. */
511 for (s = str; *s != '\0' && ! isspace (*s); s++)
516 /* find the first opcode with the proper name */
517 opcode = (struct v850_opcode *)hash_find (v850_hash, str);
520 as_bad ("Unrecognized opcode: `%s'", str);
525 while (isspace (*str))
528 input_line_pointer = str;
532 const char *errmsg = NULL;
537 insn = opcode->opcode;
538 for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++)
540 const struct v850_operand *operand;
544 if (next_opindex == 0)
546 operand = &v850_operands[*opindex_ptr];
550 operand = &v850_operands[next_opindex];
556 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
559 /* Gather the operand. */
560 hold = input_line_pointer;
561 input_line_pointer = str;
564 /* lo(), hi(), hi0(), etc... */
565 if ((reloc = v850_reloc_prefix()) != BFD_RELOC_UNUSED)
569 if (*input_line_pointer++ != ')')
571 errmsg = "syntax error: expected `)'";
575 if (ex.X_op == O_constant)
580 ex.X_add_number &= 0xffff;
584 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff);
587 case BFD_RELOC_HI16_S:
588 ex.X_add_number = ((ex.X_add_number >> 16) & 0xffff)
589 + ((ex.X_add_number >> 15) & 1);
596 insn = v850_insert_operand (insn, operand, ex.X_add_number,
601 if (fc > MAX_INSN_FIXUPS)
602 as_fatal ("too many fixups");
605 fixups[fc].opindex = *opindex_ptr;
606 fixups[fc].reloc = reloc;
612 if ((operand->flags & V850_OPERAND_REG) != 0)
614 if (!register_name(&ex))
616 errmsg = "invalid register name";
620 else if ((operand->flags & V850_OPERAND_SRG) != 0)
622 if (!system_register_name(&ex))
624 errmsg = "invalid system register name";
628 else if ((operand->flags & V850_OPERAND_EP) != 0)
630 char *start = input_line_pointer;
631 char c = get_symbol_end ();
632 if (strcmp (start, "ep") != 0 && strcmp (start, "r30") != 0)
634 /* Put things back the way we found them. */
635 *input_line_pointer = c;
636 input_line_pointer = start;
637 errmsg = "expected EP register";
640 *input_line_pointer = c;
641 str = input_line_pointer;
642 input_line_pointer = hold;
644 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
648 else if ((operand->flags & V850_OPERAND_CC) != 0)
652 errmsg = "invalid condition code name";
656 else if (register_name (&ex)
657 && (operand->flags & V850_OPERAND_REG) == 0)
659 errmsg = "syntax error: register not expected";
662 else if (system_register_name (&ex)
663 && (operand->flags & V850_OPERAND_SRG) == 0)
665 errmsg = "syntax error: system register not expected";
668 else if (cc_name (&ex)
669 && (operand->flags & V850_OPERAND_CC) == 0)
671 errmsg = "syntax error: condition code not expected";
682 errmsg = "illegal operand";
685 errmsg = "missing operand";
688 if ((operand->flags & (V850_OPERAND_REG | V850_OPERAND_SRG)) == 0)
690 errmsg = "invalid operand";
694 insn = v850_insert_operand (insn, operand, ex.X_add_number,
699 insn = v850_insert_operand (insn, operand, ex.X_add_number,
704 /* We need to generate a fixup for this expression. */
705 if (fc >= MAX_INSN_FIXUPS)
706 as_fatal ("too many fixups");
708 fixups[fc].opindex = *opindex_ptr;
709 fixups[fc].reloc = BFD_RELOC_UNUSED;
716 str = input_line_pointer;
717 input_line_pointer = hold;
719 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
727 next_opcode = opcode + 1;
728 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
730 opcode = next_opcode;
734 as_bad ("%s", errmsg);
740 while (isspace (*str))
744 as_bad ("junk at end of line: `%s'", str);
746 input_line_pointer = str;
748 /* Write out the instruction.
750 Four byte insns have an opcode with the two high bits on. */
751 if ((insn & 0x0600) == 0x0600)
755 f = frag_more (size);
756 md_number_to_chars (f, insn, size);
758 /* Create any fixups. At this point we do not use a
759 bfd_reloc_code_real_type, but instead just use the
760 BFD_RELOC_UNUSED plus the operand index. This lets us easily
761 handle fixups for any operand type, although that is admittedly
762 not a very exciting feature. We pick a BFD reloc type in
764 for (i = 0; i < fc; i++)
766 const struct v850_operand *operand;
768 operand = &v850_operands[fixups[i].opindex];
769 if (fixups[i].reloc != BFD_RELOC_UNUSED)
771 reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
779 size = bfd_get_reloc_size (reloc_howto);
782 if (size < 1 || size > 4)
785 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset, size,
787 reloc_howto->pc_relative,
792 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
794 1 /* FIXME: V850_OPERAND_RELATIVE ??? */,
795 ((bfd_reloc_code_real_type)
796 (fixups[i].opindex + (int) BFD_RELOC_UNUSED)));
802 /* if while processing a fixup, a reloc really needs to be created */
803 /* then it is done here */
806 tc_gen_reloc (seg, fixp)
811 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
812 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
813 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
814 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
815 if (reloc->howto == (reloc_howto_type *) NULL)
817 as_bad_where (fixp->fx_file, fixp->fx_line,
818 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
821 reloc->addend = fixp->fx_addnumber;
822 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
827 md_estimate_size_before_relax (fragp, seg)
838 /* If the symbol is undefined, or in a section other than our own,
839 then let the linker figure it out. */
840 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
842 /* The symbol is undefined. Let the linker figure it out. */
845 return fixp->fx_frag->fr_address + fixp->fx_where;
849 md_apply_fix3 (fixp, valuep, seg)
857 if (fixp->fx_addsy == (symbolS *) NULL)
862 else if (fixp->fx_pcrel)
866 value = fixp->fx_offset;
867 if (fixp->fx_subsy != (symbolS *) NULL)
869 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
870 value -= S_GET_VALUE (fixp->fx_subsy);
873 /* We don't actually support subtracting a symbol. */
874 as_bad_where (fixp->fx_file, fixp->fx_line,
875 "expression too complex");
880 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
882 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
885 const struct v850_operand *operand;
889 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
890 operand = &v850_operands[opindex];
892 /* Fetch the instruction, insert the fully resolved operand
893 value, and stuff the instruction back again.
895 Note the instruction has been stored in little endian
897 where = fixp->fx_frag->fr_literal + fixp->fx_where;
899 insn = bfd_getl32((unsigned char *) where);
900 insn = v850_insert_operand (insn, operand, (offsetT) value,
901 fixp->fx_file, fixp->fx_line);
902 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
906 /* Nothing else to do here. */
910 /* Determine a BFD reloc value based on the operand information.
911 We are only prepared to turn a few of the operands into relocs. */
913 if (operand->bits == 22)
914 fixp->fx_r_type = BFD_RELOC_V850_22_PCREL;
915 else if (operand->bits == 9)
916 fixp->fx_r_type = BFD_RELOC_V850_9_PCREL;
919 as_bad_where(fixp->fx_file, fixp->fx_line,
920 "unresolved expression that must be resolved");
925 else if (fixp->fx_done)
927 /* We still have to insert the value into memory! */
928 where = fixp->fx_frag->fr_literal + fixp->fx_where;
929 if (fixp->fx_size == 1)
930 *where = value & 0xff;
931 if (fixp->fx_size == 2)
932 bfd_putl16(value & 0xffff, (unsigned char *) where);
933 if (fixp->fx_size == 4)
934 bfd_putl32(value, (unsigned char *) where);
937 fixp->fx_addnumber = value;
942 /* Insert an operand value into an instruction. */
945 v850_insert_operand (insn, operand, val, file, line)
947 const struct v850_operand *operand;
952 if (operand->bits != 16)
957 if ((operand->flags & V850_OPERAND_SIGNED) != 0)
959 max = (1 << (operand->bits - 1)) - 1;
960 min = - (1 << (operand->bits - 1));
964 max = (1 << operand->bits) - 1;
971 if (test < (offsetT) min || test > (offsetT) max)
974 "operand out of range (%s not between %ld and %ld)";
977 sprint_value (buf, test);
978 if (file == (char *) NULL)
979 as_warn (err, buf, min, max);
981 as_warn_where (file, line, err, buf, min, max);
987 const char *message = NULL;
988 insn = (*operand->insert) (insn, val, &message);
991 if (file == (char *) NULL)
994 as_warn_where (file, line, message);
998 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);