1 /* tc-mn10300.c -- Assembler code for the Matsushita 10300
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/mn10300.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 mn10300
58 PARAMS ((unsigned long insn, const struct mn10300_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));
67 #define MAX_INSN_FIXUPS (5)
72 bfd_reloc_code_real_type reloc;
74 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
77 const char *md_shortopts = "";
78 struct option md_longopts[] = {
79 {NULL, no_argument, NULL, 0}
81 size_t md_longopts_size = sizeof(md_longopts);
83 /* The target specific pseudo-ops which we support. */
84 const pseudo_typeS md_pseudo_table[] =
89 /* Opcode hash table. */
90 static struct hash_control *mn10300_hash;
92 /* This table is sorted. Suitable for searching by a binary search. */
93 static const struct reg_name data_registers[] =
100 #define DATA_REG_NAME_CNT (sizeof(data_registers) / sizeof(struct reg_name))
102 static const struct reg_name address_registers[] =
109 #define ADDRESS_REG_NAME_CNT (sizeof(address_registers) / sizeof(struct reg_name))
111 static const struct reg_name other_registers[] =
117 #define OTHER_REG_NAME_CNT (sizeof(other_registers) / sizeof(struct reg_name))
119 /* reg_name_search does a binary search of the given register table
120 to see if "name" is a valid regiter name. Returns the register
121 number from the array on success, or -1 on failure. */
124 reg_name_search (regs, regcount, name)
125 const struct reg_name *regs;
129 int middle, low, high;
137 middle = (low + high) / 2;
138 cmp = strcasecmp (name, regs[middle].name);
144 return regs[middle].value;
151 /* Summary of register_name().
153 * in: Input_line_pointer points to 1st char of operand.
155 * out: A expressionS.
156 * The operand may have been a register: in this case, X_op == O_register,
157 * X_add_number is set to the register number, and truth is returned.
158 * Input_line_pointer->(next non-blank) char after operand, or is in
159 * its original state.
162 data_register_name (expressionP)
163 expressionS *expressionP;
170 /* Find the spelling of the operand */
171 start = name = input_line_pointer;
173 c = get_symbol_end ();
174 reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
176 /* look to see if it's in the register table */
179 expressionP->X_op = O_register;
180 expressionP->X_add_number = reg_number;
182 /* make the rest nice */
183 expressionP->X_add_symbol = NULL;
184 expressionP->X_op_symbol = NULL;
185 *input_line_pointer = c; /* put back the delimiting char */
190 /* reset the line as if we had not done anything */
191 *input_line_pointer = c; /* put back the delimiting char */
192 input_line_pointer = start; /* reset input_line pointer */
197 /* Summary of register_name().
199 * in: Input_line_pointer points to 1st char of operand.
201 * out: A expressionS.
202 * The operand may have been a register: in this case, X_op == O_register,
203 * X_add_number is set to the register number, and truth is returned.
204 * Input_line_pointer->(next non-blank) char after operand, or is in
205 * its original state.
208 address_register_name (expressionP)
209 expressionS *expressionP;
216 /* Find the spelling of the operand */
217 start = name = input_line_pointer;
219 c = get_symbol_end ();
220 reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
222 /* look to see if it's in the register table */
225 expressionP->X_op = O_register;
226 expressionP->X_add_number = reg_number;
228 /* make the rest nice */
229 expressionP->X_add_symbol = NULL;
230 expressionP->X_op_symbol = NULL;
231 *input_line_pointer = c; /* put back the delimiting char */
236 /* reset the line as if we had not done anything */
237 *input_line_pointer = c; /* put back the delimiting char */
238 input_line_pointer = start; /* reset input_line pointer */
243 /* Summary of register_name().
245 * in: Input_line_pointer points to 1st char of operand.
247 * out: A expressionS.
248 * The operand may have been a register: in this case, X_op == O_register,
249 * X_add_number is set to the register number, and truth is returned.
250 * Input_line_pointer->(next non-blank) char after operand, or is in
251 * its original state.
254 other_register_name (expressionP)
255 expressionS *expressionP;
262 /* Find the spelling of the operand */
263 start = name = input_line_pointer;
265 c = get_symbol_end ();
266 reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
268 /* look to see if it's in the register table */
271 expressionP->X_op = O_register;
272 expressionP->X_add_number = reg_number;
274 /* make the rest nice */
275 expressionP->X_add_symbol = NULL;
276 expressionP->X_op_symbol = NULL;
277 *input_line_pointer = c; /* put back the delimiting char */
282 /* reset the line as if we had not done anything */
283 *input_line_pointer = c; /* put back the delimiting char */
284 input_line_pointer = start; /* reset input_line pointer */
290 md_show_usage (stream)
293 fprintf(stream, "MN10300 options:\n\
298 md_parse_option (c, arg)
306 md_undefined_symbol (name)
313 md_atof (type, litp, sizep)
319 LITTLENUM_TYPE words[4];
335 return "bad call to md_atof";
338 t = atof_ieee (input_line_pointer, type, words);
340 input_line_pointer = t;
344 for (i = prec - 1; i >= 0; i--)
346 md_number_to_chars (litp, (valueT) words[i], 2);
355 md_convert_frag (abfd, sec, fragP)
360 /* printf ("call to md_convert_frag \n"); */
365 md_section_align (seg, addr)
369 int align = bfd_get_section_alignment (stdoutput, seg);
370 return ((addr + (1 << align) - 1) & (-1 << align));
376 char *prev_name = "";
377 register const struct mn10300_opcode *op;
379 mn10300_hash = hash_new();
381 /* Insert unique names into hash table. The MN10300 instruction set
382 has many identical opcode names that have different opcodes based
383 on the operands. This hash table then provides a quick index to
384 the first opcode with a particular name in the opcode table. */
386 op = mn10300_opcodes;
389 if (strcmp (prev_name, op->name))
391 prev_name = (char *) op->name;
392 hash_insert (mn10300_hash, op->name, (char *) op);
403 struct mn10300_opcode *opcode;
404 struct mn10300_opcode *next_opcode;
405 const unsigned char *opindex_ptr;
407 unsigned long insn, size;
411 bfd_reloc_code_real_type reloc;
413 /* Get the opcode. */
414 for (s = str; *s != '\0' && ! isspace (*s); s++)
419 /* find the first opcode with the proper name */
420 opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
423 as_bad ("Unrecognized opcode: `%s'", str);
428 while (isspace (*str))
431 input_line_pointer = str;
435 const char *errmsg = NULL;
442 insn = opcode->opcode;
443 for (op_idx = 1, opindex_ptr = opcode->operands;
445 opindex_ptr++, op_idx++)
447 const struct mn10300_operand *operand;
450 if (next_opindex == 0)
452 operand = &mn10300_operands[*opindex_ptr];
456 operand = &mn10300_operands[next_opindex];
462 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
465 /* Gather the operand. */
466 hold = input_line_pointer;
467 input_line_pointer = str;
469 if (operand->flags & MN10300_OPERAND_PAREN)
471 if (*input_line_pointer != ')' && *input_line_pointer != '(')
473 input_line_pointer = hold;
477 input_line_pointer++;
480 /* See if we can match the operands. */
481 else if (operand->flags & MN10300_OPERAND_DREG)
483 if (!data_register_name (&ex))
485 input_line_pointer = hold;
490 else if (operand->flags & MN10300_OPERAND_AREG)
492 if (!address_register_name (&ex))
494 input_line_pointer = hold;
499 else if (operand->flags & MN10300_OPERAND_SP)
501 char *start = input_line_pointer;
502 char c = get_symbol_end ();
504 if (strcmp (start, "sp") != 0)
506 *input_line_pointer = c;
507 input_line_pointer = hold;
511 *input_line_pointer = c;
514 else if (operand->flags & MN10300_OPERAND_PSW)
516 char *start = input_line_pointer;
517 char c = get_symbol_end ();
519 if (strcmp (start, "psw") != 0)
521 *input_line_pointer = c;
522 input_line_pointer = hold;
526 *input_line_pointer = c;
529 else if (operand->flags & MN10300_OPERAND_MDR)
531 char *start = input_line_pointer;
532 char c = get_symbol_end ();
534 if (strcmp (start, "mdr") != 0)
536 *input_line_pointer = c;
537 input_line_pointer = hold;
541 *input_line_pointer = c;
544 else if (data_register_name (&ex))
546 input_line_pointer = hold;
550 else if (address_register_name (&ex))
552 input_line_pointer = hold;
556 else if (other_register_name (&ex))
558 input_line_pointer = hold;
562 else if (*str == ')' || *str == '(')
564 input_line_pointer = hold;
576 errmsg = "illegal operand";
579 errmsg = "missing operand";
582 if (operand->flags & (MN10300_OPERAND_DREG
583 | MN10300_OPERAND_AREG) == 0)
585 input_line_pointer = hold;
590 insn = mn10300_insert_operand (insn, operand, ex.X_add_number,
595 /* If this operand can be promoted, and it doesn't
596 fit into the allocated bitfield for this insn,
597 then promote it (ie this opcode does not match). */
598 if (operand->flags & MN10300_OPERAND_PROMOTE
599 && ! check_operand (insn, operand, ex.X_add_number))
601 input_line_pointer = hold;
606 insn = mn10300_insert_operand (insn, operand, ex.X_add_number,
611 /* If this operand can be promoted, then this opcode didn't
612 match since we can't know if it needed promotion! */
613 if (operand->flags & MN10300_OPERAND_PROMOTE)
615 input_line_pointer = hold;
620 /* We need to generate a fixup for this expression. */
621 if (fc >= MAX_INSN_FIXUPS)
622 as_fatal ("too many fixups");
624 fixups[fc].opindex = *opindex_ptr;
625 fixups[fc].reloc = BFD_RELOC_UNUSED;
631 str = input_line_pointer;
632 input_line_pointer = hold;
634 while (*str == ' ' || *str == ',' || *str == '[' || *str == ']')
639 /* Make sure we used all the operands! */
646 next_opcode = opcode + 1;
647 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
649 opcode = next_opcode;
653 as_bad ("%s", errmsg);
659 while (isspace (*str))
663 as_bad ("junk at end of line: `%s'", str);
665 input_line_pointer = str;
667 /* Determine the size of the instruction. */
668 if (opcode->format == FMT_S0)
671 if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
674 if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
677 if (opcode->format == FMT_S4)
680 if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
683 if (opcode->format == FMT_D2)
686 if (opcode->format == FMT_D4)
689 /* Write out the instruction. */
691 f = frag_more (size);
692 number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
694 number_to_chars_bigendian (f + 4, 0, size - 4);
698 /* if while processing a fixup, a reloc really needs to be created */
699 /* then it is done here */
702 tc_gen_reloc (seg, fixp)
707 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
708 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
709 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
710 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
711 if (reloc->howto == (reloc_howto_type *) NULL)
713 as_bad_where (fixp->fx_file, fixp->fx_line,
714 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
717 reloc->addend = fixp->fx_addnumber;
718 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
723 md_estimate_size_before_relax (fragp, seg)
734 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
736 /* The symbol is undefined. Let the linker figure it out. */
739 return fixp->fx_frag->fr_address + fixp->fx_where;
743 md_apply_fix3 (fixp, valuep, seg)
754 if (fixp->fx_addsy == (symbolS *) NULL)
759 else if (fixp->fx_pcrel)
763 value = fixp->fx_offset;
764 if (fixp->fx_subsy != (symbolS *) NULL)
766 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
767 value -= S_GET_VALUE (fixp->fx_subsy);
770 /* We don't actually support subtracting a symbol. */
771 as_bad_where (fixp->fx_file, fixp->fx_line,
772 "expression too complex");
777 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
779 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
782 const struct mn10300_operand *operand;
786 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
787 operand = &mn10300_operands[opindex];
789 /* Fetch the instruction, insert the fully resolved operand
790 value, and stuff the instruction back again.
792 Note the instruction has been stored in little endian
794 where = fixp->fx_frag->fr_literal + fixp->fx_where;
796 insn = bfd_getl32((unsigned char *) where);
797 insn = mn10300_insert_operand (insn, operand, (offsetT) value,
798 fixp->fx_file, fixp->fx_line);
799 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
803 /* Nothing else to do here. */
807 /* Determine a BFD reloc value based on the operand information.
808 We are only prepared to turn a few of the operands into relocs. */
811 as_bad_where(fixp->fx_file, fixp->fx_line,
812 "unresolved expression that must be resolved");
817 else if (fixp->fx_done)
819 /* We still have to insert the value into memory! */
820 where = fixp->fx_frag->fr_literal + fixp->fx_where;
821 if (fixp->fx_size == 1)
822 *where = value & 0xff;
823 if (fixp->fx_size == 2)
824 bfd_putl16(value & 0xffff, (unsigned char *) where);
825 if (fixp->fx_size == 4)
826 bfd_putl32(value, (unsigned char *) where);
829 fixp->fx_addnumber = value;
833 /* Insert an operand value into an instruction. */
836 mn10300_insert_operand (insn, operand, val, file, line)
838 const struct mn10300_operand *operand;
843 if (operand->bits != 32)
848 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
850 max = (1 << (operand->bits - 1)) - 1;
851 min = - (1 << (operand->bits - 1));
855 max = (1 << operand->bits) - 1;
862 if (test < (offsetT) min || test > (offsetT) max)
865 "operand out of range (%s not between %ld and %ld)";
868 sprint_value (buf, test);
869 if (file == (char *) NULL)
870 as_warn (err, buf, min, max);
872 as_warn_where (file, line, err, buf, min, max);
876 insn |= (((long) val & ((1 << operand->bits) - 1)) << operand->shift);
881 check_operand (insn, operand, val)
883 const struct mn10300_operand *operand;
886 if (operand->bits != 32)
891 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
893 max = (1 << (operand->bits - 1)) - 1;
894 min = - (1 << (operand->bits - 1));
898 max = (1 << operand->bits) - 1;
905 if (test < (offsetT) min || test > (offsetT) max)