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 void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
58 const struct mn10300_operand *,
59 offsetT, char *, unsigned,
61 static unsigned long check_operand PARAMS ((unsigned long,
62 const struct mn10300_operand *,
64 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
65 static boolean register_name PARAMS ((expressionS *expressionP));
66 static boolean system_register_name PARAMS ((expressionS *expressionP));
67 static boolean cc_name PARAMS ((expressionS *expressionP));
71 #define MAX_INSN_FIXUPS (5)
76 bfd_reloc_code_real_type reloc;
78 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
81 const char *md_shortopts = "";
82 struct option md_longopts[] = {
83 {NULL, no_argument, NULL, 0}
85 size_t md_longopts_size = sizeof(md_longopts);
87 /* The target specific pseudo-ops which we support. */
88 const pseudo_typeS md_pseudo_table[] =
93 /* Opcode hash table. */
94 static struct hash_control *mn10300_hash;
96 /* This table is sorted. Suitable for searching by a binary search. */
97 static const struct reg_name data_registers[] =
104 #define DATA_REG_NAME_CNT (sizeof(data_registers) / sizeof(struct reg_name))
106 static const struct reg_name address_registers[] =
113 #define ADDRESS_REG_NAME_CNT (sizeof(address_registers) / sizeof(struct reg_name))
115 static const struct reg_name other_registers[] =
121 #define OTHER_REG_NAME_CNT (sizeof(other_registers) / sizeof(struct reg_name))
123 /* reg_name_search does a binary search of the given register table
124 to see if "name" is a valid regiter name. Returns the register
125 number from the array on success, or -1 on failure. */
128 reg_name_search (regs, regcount, name)
129 const struct reg_name *regs;
133 int middle, low, high;
141 middle = (low + high) / 2;
142 cmp = strcasecmp (name, regs[middle].name);
148 return regs[middle].value;
155 /* Summary of register_name().
157 * in: Input_line_pointer points to 1st char of operand.
159 * out: A expressionS.
160 * The operand may have been a register: in this case, X_op == O_register,
161 * X_add_number is set to the register number, and truth is returned.
162 * Input_line_pointer->(next non-blank) char after operand, or is in
163 * its original state.
166 data_register_name (expressionP)
167 expressionS *expressionP;
174 /* Find the spelling of the operand */
175 start = name = input_line_pointer;
177 c = get_symbol_end ();
178 reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
180 /* look to see if it's in the register table */
183 expressionP->X_op = O_register;
184 expressionP->X_add_number = reg_number;
186 /* make the rest nice */
187 expressionP->X_add_symbol = NULL;
188 expressionP->X_op_symbol = NULL;
189 *input_line_pointer = c; /* put back the delimiting char */
194 /* reset the line as if we had not done anything */
195 *input_line_pointer = c; /* put back the delimiting char */
196 input_line_pointer = start; /* reset input_line pointer */
201 /* Summary of register_name().
203 * in: Input_line_pointer points to 1st char of operand.
205 * out: A expressionS.
206 * The operand may have been a register: in this case, X_op == O_register,
207 * X_add_number is set to the register number, and truth is returned.
208 * Input_line_pointer->(next non-blank) char after operand, or is in
209 * its original state.
212 address_register_name (expressionP)
213 expressionS *expressionP;
220 /* Find the spelling of the operand */
221 start = name = input_line_pointer;
223 c = get_symbol_end ();
224 reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
226 /* look to see if it's in the register table */
229 expressionP->X_op = O_register;
230 expressionP->X_add_number = reg_number;
232 /* make the rest nice */
233 expressionP->X_add_symbol = NULL;
234 expressionP->X_op_symbol = NULL;
235 *input_line_pointer = c; /* put back the delimiting char */
240 /* reset the line as if we had not done anything */
241 *input_line_pointer = c; /* put back the delimiting char */
242 input_line_pointer = start; /* reset input_line pointer */
247 /* Summary of register_name().
249 * in: Input_line_pointer points to 1st char of operand.
251 * out: A expressionS.
252 * The operand may have been a register: in this case, X_op == O_register,
253 * X_add_number is set to the register number, and truth is returned.
254 * Input_line_pointer->(next non-blank) char after operand, or is in
255 * its original state.
258 other_register_name (expressionP)
259 expressionS *expressionP;
266 /* Find the spelling of the operand */
267 start = name = input_line_pointer;
269 c = get_symbol_end ();
270 reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
272 /* look to see if it's in the register table */
275 expressionP->X_op = O_register;
276 expressionP->X_add_number = reg_number;
278 /* make the rest nice */
279 expressionP->X_add_symbol = NULL;
280 expressionP->X_op_symbol = NULL;
281 *input_line_pointer = c; /* put back the delimiting char */
286 /* reset the line as if we had not done anything */
287 *input_line_pointer = c; /* put back the delimiting char */
288 input_line_pointer = start; /* reset input_line pointer */
294 md_show_usage (stream)
297 fprintf(stream, "MN10300 options:\n\
302 md_parse_option (c, arg)
310 md_undefined_symbol (name)
317 md_atof (type, litp, sizep)
323 LITTLENUM_TYPE words[4];
339 return "bad call to md_atof";
342 t = atof_ieee (input_line_pointer, type, words);
344 input_line_pointer = t;
348 for (i = prec - 1; i >= 0; i--)
350 md_number_to_chars (litp, (valueT) words[i], 2);
359 md_convert_frag (abfd, sec, fragP)
364 /* printf ("call to md_convert_frag \n"); */
369 md_section_align (seg, addr)
373 int align = bfd_get_section_alignment (stdoutput, seg);
374 return ((addr + (1 << align) - 1) & (-1 << align));
380 char *prev_name = "";
381 register const struct mn10300_opcode *op;
383 mn10300_hash = hash_new();
385 /* Insert unique names into hash table. The MN10300 instruction set
386 has many identical opcode names that have different opcodes based
387 on the operands. This hash table then provides a quick index to
388 the first opcode with a particular name in the opcode table. */
390 op = mn10300_opcodes;
393 if (strcmp (prev_name, op->name))
395 prev_name = (char *) op->name;
396 hash_insert (mn10300_hash, op->name, (char *) op);
407 struct mn10300_opcode *opcode;
408 struct mn10300_opcode *next_opcode;
409 const unsigned char *opindex_ptr;
411 unsigned long insn, extension, size;
415 bfd_reloc_code_real_type reloc;
417 /* Get the opcode. */
418 for (s = str; *s != '\0' && ! isspace (*s); s++)
423 /* find the first opcode with the proper name */
424 opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
427 as_bad ("Unrecognized opcode: `%s'", str);
432 while (isspace (*str))
435 input_line_pointer = str;
439 const char *errmsg = NULL;
447 insn = opcode->opcode;
449 for (op_idx = 1, opindex_ptr = opcode->operands;
451 opindex_ptr++, op_idx++)
453 const struct mn10300_operand *operand;
456 if (next_opindex == 0)
458 operand = &mn10300_operands[*opindex_ptr];
462 operand = &mn10300_operands[next_opindex];
468 while (*str == ' ' || *str == ',')
471 /* Gather the operand. */
472 hold = input_line_pointer;
473 input_line_pointer = str;
475 if (operand->flags & MN10300_OPERAND_PAREN)
477 if (*input_line_pointer != ')' && *input_line_pointer != '(')
479 input_line_pointer = hold;
483 input_line_pointer++;
486 /* See if we can match the operands. */
487 else if (operand->flags & MN10300_OPERAND_DREG)
489 if (!data_register_name (&ex))
491 input_line_pointer = hold;
496 else if (operand->flags & MN10300_OPERAND_AREG)
498 if (!address_register_name (&ex))
500 input_line_pointer = hold;
505 else if (operand->flags & MN10300_OPERAND_SP)
507 char *start = input_line_pointer;
508 char c = get_symbol_end ();
510 if (strcmp (start, "sp") != 0)
512 *input_line_pointer = c;
513 input_line_pointer = hold;
517 *input_line_pointer = c;
520 else if (operand->flags & MN10300_OPERAND_PSW)
522 char *start = input_line_pointer;
523 char c = get_symbol_end ();
525 if (strcmp (start, "psw") != 0)
527 *input_line_pointer = c;
528 input_line_pointer = hold;
532 *input_line_pointer = c;
535 else if (operand->flags & MN10300_OPERAND_MDR)
537 char *start = input_line_pointer;
538 char c = get_symbol_end ();
540 if (strcmp (start, "mdr") != 0)
542 *input_line_pointer = c;
543 input_line_pointer = hold;
547 *input_line_pointer = c;
550 else if (operand->flags & MN10300_OPERAND_REG_LIST)
552 unsigned int value = 0;
553 if (*input_line_pointer != '[')
555 input_line_pointer = hold;
561 input_line_pointer++;
563 /* A null register list can not be specified. */
564 if (*input_line_pointer == ']')
566 input_line_pointer = hold;
571 while (*input_line_pointer != ']')
576 if (*input_line_pointer == ',')
577 input_line_pointer++;
579 start = input_line_pointer;
580 c = get_symbol_end ();
582 if (strcmp (start, "d2") == 0)
585 *input_line_pointer = c;
587 else if (strcmp (start, "d3") == 0)
590 *input_line_pointer = c;
592 else if (strcmp (start, "a2") == 0)
595 *input_line_pointer = c;
597 else if (strcmp (start, "a3") == 0)
600 *input_line_pointer = c;
602 else if (strcmp (start, "other") == 0)
605 *input_line_pointer = c;
609 input_line_pointer = hold;
614 input_line_pointer++;
615 mn10300_insert_operand (&insn, &extension, operand,
616 value, (char *) NULL, 0, 0);
620 else if (data_register_name (&ex))
622 input_line_pointer = hold;
626 else if (address_register_name (&ex))
628 input_line_pointer = hold;
632 else if (other_register_name (&ex))
634 input_line_pointer = hold;
638 else if (*str == ')' || *str == '(')
640 input_line_pointer = hold;
652 errmsg = "illegal operand";
655 errmsg = "missing operand";
658 if (operand->flags & (MN10300_OPERAND_DREG
659 | MN10300_OPERAND_AREG) == 0)
661 input_line_pointer = hold;
666 if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
668 else if (opcode->format == FMT_D2 || opcode->format == FMT_D4
669 || opcode->format == FMT_S2 || opcode->format == FMT_S4
670 || opcode->format == FMT_S6 || opcode->format == FMT_D5)
675 mn10300_insert_operand (&insn, &extension, operand,
676 ex.X_add_number, (char *) NULL,
682 /* If this operand can be promoted, and it doesn't
683 fit into the allocated bitfield for this insn,
684 then promote it (ie this opcode does not match). */
685 if (operand->flags & MN10300_OPERAND_PROMOTE
686 && ! check_operand (insn, operand, ex.X_add_number))
688 input_line_pointer = hold;
693 mn10300_insert_operand (&insn, &extension, operand,
694 ex.X_add_number, (char *) NULL,
699 /* If this operand can be promoted, then this opcode didn't
700 match since we can't know if it needed promotion! */
701 if (operand->flags & MN10300_OPERAND_PROMOTE)
703 input_line_pointer = hold;
708 /* We need to generate a fixup for this expression. */
709 if (fc >= MAX_INSN_FIXUPS)
710 as_fatal ("too many fixups");
712 fixups[fc].opindex = *opindex_ptr;
713 fixups[fc].reloc = BFD_RELOC_UNUSED;
719 str = input_line_pointer;
720 input_line_pointer = hold;
722 while (*str == ' ' || *str == ',')
727 /* Make sure we used all the operands! */
734 next_opcode = opcode + 1;
735 if (next_opcode->opcode != 0 && !strcmp(next_opcode->name, opcode->name))
737 opcode = next_opcode;
741 as_bad ("%s", errmsg);
747 while (isspace (*str))
751 as_bad ("junk at end of line: `%s'", str);
753 input_line_pointer = str;
755 /* Determine the size of the instruction. */
756 if (opcode->format == FMT_S0)
759 if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
762 if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
765 if (opcode->format == FMT_S4)
768 if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
771 if (opcode->format == FMT_D2)
774 if (opcode->format == FMT_D4)
777 /* Write out the instruction. */
779 f = frag_more (size);
780 number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
782 number_to_chars_bigendian (f + 4, extension, size - 4);
786 /* if while processing a fixup, a reloc really needs to be created */
787 /* then it is done here */
790 tc_gen_reloc (seg, fixp)
795 reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent));
796 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
797 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
798 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
799 if (reloc->howto == (reloc_howto_type *) NULL)
801 as_bad_where (fixp->fx_file, fixp->fx_line,
802 "reloc %d not supported by object file format", (int)fixp->fx_r_type);
805 reloc->addend = fixp->fx_addnumber;
806 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
811 md_estimate_size_before_relax (fragp, seg)
822 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
824 /* The symbol is undefined. Let the linker figure it out. */
827 return fixp->fx_frag->fr_address + fixp->fx_where;
831 md_apply_fix3 (fixp, valuep, seg)
842 if (fixp->fx_addsy == (symbolS *) NULL)
847 else if (fixp->fx_pcrel)
851 value = fixp->fx_offset;
852 if (fixp->fx_subsy != (symbolS *) NULL)
854 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
855 value -= S_GET_VALUE (fixp->fx_subsy);
858 /* We don't actually support subtracting a symbol. */
859 as_bad_where (fixp->fx_file, fixp->fx_line,
860 "expression too complex");
865 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
867 if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED)
870 const struct mn10300_operand *operand;
872 unsigned long insn, extension;
874 opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED;
875 operand = &mn10300_operands[opindex];
877 /* Fetch the instruction, insert the fully resolved operand
878 value, and stuff the instruction back again.
880 Note the instruction has been stored in little endian
882 where = fixp->fx_frag->fr_literal + fixp->fx_where;
884 insn = bfd_getl32((unsigned char *) where);
886 mn10300_insert_operand (&insn, &extension, operand,
887 (offsetT) value, fixp->fx_file,
889 bfd_putl32((bfd_vma) insn, (unsigned char *) where);
893 /* Nothing else to do here. */
897 /* Determine a BFD reloc value based on the operand information.
898 We are only prepared to turn a few of the operands into relocs. */
901 as_bad_where(fixp->fx_file, fixp->fx_line,
902 "unresolved expression that must be resolved");
907 else if (fixp->fx_done)
909 /* We still have to insert the value into memory! */
910 where = fixp->fx_frag->fr_literal + fixp->fx_where;
911 if (fixp->fx_size == 1)
912 *where = value & 0xff;
913 if (fixp->fx_size == 2)
914 bfd_putl16(value & 0xffff, (unsigned char *) where);
915 if (fixp->fx_size == 4)
916 bfd_putl32(value, (unsigned char *) where);
919 fixp->fx_addnumber = value;
923 /* Insert an operand value into an instruction. */
926 mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
927 unsigned long *insnp;
928 unsigned long *extensionp;
929 const struct mn10300_operand *operand;
935 /* No need to check 32bit operands for a bit. Note that
936 MN10300_OPERAND_SPLIT is an implicit 32bit operand. */
937 if (operand->bits != 32
938 && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
943 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
945 max = (1 << (operand->bits - 1)) - 1;
946 min = - (1 << (operand->bits - 1));
950 max = (1 << operand->bits) - 1;
957 if (test < (offsetT) min || test > (offsetT) max)
960 "operand out of range (%s not between %ld and %ld)";
963 sprint_value (buf, test);
964 if (file == (char *) NULL)
965 as_warn (err, buf, min, max);
967 as_warn_where (file, line, err, buf, min, max);
971 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
973 *insnp |= (val >> 32 - operand->bits) & ((1 << operand->bits) - 1);
974 *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
977 else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
979 *insnp |= (((long) val & ((1 << operand->bits) - 1))
980 << (operand->shift + shift));
982 if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
983 *insnp |= (((long) val & ((1 << operand->bits) - 1))
984 << (operand->shift + shift + 2));
988 *extensionp |= (((long) val & ((1 << operand->bits) - 1))
989 << (operand->shift + shift));
991 if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
992 *extensionp |= (((long) val & ((1 << operand->bits) - 1))
993 << (operand->shift + shift + 2));
998 check_operand (insn, operand, val)
1000 const struct mn10300_operand *operand;
1003 /* No need to check 32bit operands for a bit. Note that
1004 MN10300_OPERAND_SPLIT is an implicit 32bit operand. */
1005 if (operand->bits != 32
1006 && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1011 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1013 max = (1 << (operand->bits - 1)) - 1;
1014 min = - (1 << (operand->bits - 1));
1018 max = (1 << operand->bits) - 1;
1025 if (test < (offsetT) min || test > (offsetT) max)