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
;
668 if (opcode
->format
== 1)
671 if (opcode
->format
== 2 || opcode
->format
== 6)
674 if (opcode
->format
== 3 || opcode
->format
== 7)
677 if (opcode
->format
== 4)
680 if (opcode
->format
== 5)
683 if (opcode
->format
== 8)
686 if (opcode
->format
== 9)
689 if (opcode
->format
== 10)
693 /* Write out the instruction. */
695 f
= frag_more (size
);
698 md_number_to_chars (f
, insn
, size
);
702 /* if while processing a fixup, a reloc really needs to be created */
703 /* then it is done here */
706 tc_gen_reloc (seg
, fixp
)
711 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
712 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
713 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
714 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
715 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
717 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
718 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
721 reloc
->addend
= fixp
->fx_addnumber
;
722 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
727 md_estimate_size_before_relax (fragp
, seg
)
738 if (fixp
->fx_addsy
!= (symbolS
*) NULL
&& ! S_IS_DEFINED (fixp
->fx_addsy
))
740 /* The symbol is undefined. Let the linker figure it out. */
743 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
747 md_apply_fix3 (fixp
, valuep
, seg
)
758 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
763 else if (fixp
->fx_pcrel
)
767 value
= fixp
->fx_offset
;
768 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
770 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
771 value
-= S_GET_VALUE (fixp
->fx_subsy
);
774 /* We don't actually support subtracting a symbol. */
775 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
776 "expression too complex");
781 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
783 if ((int) fixp
->fx_r_type
>= (int) BFD_RELOC_UNUSED
)
786 const struct mn10300_operand
*operand
;
790 opindex
= (int) fixp
->fx_r_type
- (int) BFD_RELOC_UNUSED
;
791 operand
= &mn10300_operands
[opindex
];
793 /* Fetch the instruction, insert the fully resolved operand
794 value, and stuff the instruction back again.
796 Note the instruction has been stored in little endian
798 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
800 insn
= bfd_getl32((unsigned char *) where
);
801 insn
= mn10300_insert_operand (insn
, operand
, (offsetT
) value
,
802 fixp
->fx_file
, fixp
->fx_line
);
803 bfd_putl32((bfd_vma
) insn
, (unsigned char *) where
);
807 /* Nothing else to do here. */
811 /* Determine a BFD reloc value based on the operand information.
812 We are only prepared to turn a few of the operands into relocs. */
815 as_bad_where(fixp
->fx_file
, fixp
->fx_line
,
816 "unresolved expression that must be resolved");
821 else if (fixp
->fx_done
)
823 /* We still have to insert the value into memory! */
824 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
825 if (fixp
->fx_size
== 1)
826 *where
= value
& 0xff;
827 if (fixp
->fx_size
== 2)
828 bfd_putl16(value
& 0xffff, (unsigned char *) where
);
829 if (fixp
->fx_size
== 4)
830 bfd_putl32(value
, (unsigned char *) where
);
833 fixp
->fx_addnumber
= value
;
837 /* Insert an operand value into an instruction. */
840 mn10300_insert_operand (insn
, operand
, val
, file
, line
)
842 const struct mn10300_operand
*operand
;
847 if (operand
->bits
!= 32)
852 if ((operand
->flags
& MN10300_OPERAND_SIGNED
) != 0)
854 max
= (1 << (operand
->bits
- 1)) - 1;
855 min
= - (1 << (operand
->bits
- 1));
859 max
= (1 << operand
->bits
) - 1;
866 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
869 "operand out of range (%s not between %ld and %ld)";
872 sprint_value (buf
, test
);
873 if (file
== (char *) NULL
)
874 as_warn (err
, buf
, min
, max
);
876 as_warn_where (file
, line
, err
, buf
, min
, max
);
880 insn
|= (((long) val
& ((1 << operand
->bits
) - 1)) << operand
->shift
);
885 check_operand (insn
, operand
, val
)
887 const struct mn10300_operand
*operand
;
890 if (operand
->bits
!= 32)
895 if ((operand
->flags
& MN10300_OPERAND_SIGNED
) != 0)
897 max
= (1 << (operand
->bits
- 1)) - 1;
898 min
= - (1 << (operand
->bits
- 1));
902 max
= (1 << operand
->bits
) - 1;
909 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)