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
, extension
, 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
;
443 insn
= opcode
->opcode
;
445 for (op_idx
= 1, opindex_ptr
= opcode
->operands
;
447 opindex_ptr
++, op_idx
++)
449 const struct mn10300_operand
*operand
;
452 if (next_opindex
== 0)
454 operand
= &mn10300_operands
[*opindex_ptr
];
458 operand
= &mn10300_operands
[next_opindex
];
464 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
467 /* Gather the operand. */
468 hold
= input_line_pointer
;
469 input_line_pointer
= str
;
471 if (operand
->flags
& MN10300_OPERAND_PAREN
)
473 if (*input_line_pointer
!= ')' && *input_line_pointer
!= '(')
475 input_line_pointer
= hold
;
479 input_line_pointer
++;
482 /* See if we can match the operands. */
483 else if (operand
->flags
& MN10300_OPERAND_DREG
)
485 if (!data_register_name (&ex
))
487 input_line_pointer
= hold
;
492 else if (operand
->flags
& MN10300_OPERAND_AREG
)
494 if (!address_register_name (&ex
))
496 input_line_pointer
= hold
;
501 else if (operand
->flags
& MN10300_OPERAND_SP
)
503 char *start
= input_line_pointer
;
504 char c
= get_symbol_end ();
506 if (strcmp (start
, "sp") != 0)
508 *input_line_pointer
= c
;
509 input_line_pointer
= hold
;
513 *input_line_pointer
= c
;
516 else if (operand
->flags
& MN10300_OPERAND_PSW
)
518 char *start
= input_line_pointer
;
519 char c
= get_symbol_end ();
521 if (strcmp (start
, "psw") != 0)
523 *input_line_pointer
= c
;
524 input_line_pointer
= hold
;
528 *input_line_pointer
= c
;
531 else if (operand
->flags
& MN10300_OPERAND_MDR
)
533 char *start
= input_line_pointer
;
534 char c
= get_symbol_end ();
536 if (strcmp (start
, "mdr") != 0)
538 *input_line_pointer
= c
;
539 input_line_pointer
= hold
;
543 *input_line_pointer
= c
;
546 else if (data_register_name (&ex
))
548 input_line_pointer
= hold
;
552 else if (address_register_name (&ex
))
554 input_line_pointer
= hold
;
558 else if (other_register_name (&ex
))
560 input_line_pointer
= hold
;
564 else if (*str
== ')' || *str
== '(')
566 input_line_pointer
= hold
;
578 errmsg
= "illegal operand";
581 errmsg
= "missing operand";
584 if (operand
->flags
& (MN10300_OPERAND_DREG
585 | MN10300_OPERAND_AREG
) == 0)
587 input_line_pointer
= hold
;
592 if (opcode
->format
== FMT_D1
|| opcode
->format
== FMT_S1
)
594 else if (opcode
->format
== FMT_D2
|| opcode
->format
== FMT_D4
595 || opcode
->format
== FMT_S2
|| opcode
->format
== FMT_S4
596 || opcode
->format
== FMT_S6
|| opcode
->format
== FMT_D5
)
601 mn10300_insert_operand (&insn
, &extension
, operand
,
602 ex
.X_add_number
, (char *) NULL
,
608 /* If this operand can be promoted, and it doesn't
609 fit into the allocated bitfield for this insn,
610 then promote it (ie this opcode does not match). */
611 if (operand
->flags
& MN10300_OPERAND_PROMOTE
612 && ! check_operand (insn
, operand
, ex
.X_add_number
))
614 input_line_pointer
= hold
;
619 mn10300_insert_operand (&insn
, &extension
, operand
,
620 ex
.X_add_number
, (char *) NULL
,
625 /* If this operand can be promoted, then this opcode didn't
626 match since we can't know if it needed promotion! */
627 if (operand
->flags
& MN10300_OPERAND_PROMOTE
)
629 input_line_pointer
= hold
;
634 /* We need to generate a fixup for this expression. */
635 if (fc
>= MAX_INSN_FIXUPS
)
636 as_fatal ("too many fixups");
638 fixups
[fc
].opindex
= *opindex_ptr
;
639 fixups
[fc
].reloc
= BFD_RELOC_UNUSED
;
645 str
= input_line_pointer
;
646 input_line_pointer
= hold
;
648 while (*str
== ' ' || *str
== ',' || *str
== '[' || *str
== ']')
653 /* Make sure we used all the operands! */
660 next_opcode
= opcode
+ 1;
661 if (next_opcode
->opcode
!= 0 && !strcmp(next_opcode
->name
, opcode
->name
))
663 opcode
= next_opcode
;
667 as_bad ("%s", errmsg
);
673 while (isspace (*str
))
677 as_bad ("junk at end of line: `%s'", str
);
679 input_line_pointer
= str
;
681 /* Determine the size of the instruction. */
682 if (opcode
->format
== FMT_S0
)
685 if (opcode
->format
== FMT_S1
|| opcode
->format
== FMT_D0
)
688 if (opcode
->format
== FMT_S2
|| opcode
->format
== FMT_D1
)
691 if (opcode
->format
== FMT_S4
)
694 if (opcode
->format
== FMT_S6
|| opcode
->format
== FMT_D5
)
697 if (opcode
->format
== FMT_D2
)
700 if (opcode
->format
== FMT_D4
)
703 /* Write out the instruction. */
705 f
= frag_more (size
);
706 number_to_chars_bigendian (f
, insn
, size
> 4 ? 4 : size
);
708 number_to_chars_bigendian (f
+ 4, extension
, size
- 4);
712 /* if while processing a fixup, a reloc really needs to be created */
713 /* then it is done here */
716 tc_gen_reloc (seg
, fixp
)
721 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
722 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
723 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
724 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
725 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
727 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
728 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
731 reloc
->addend
= fixp
->fx_addnumber
;
732 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
737 md_estimate_size_before_relax (fragp
, seg
)
748 if (fixp
->fx_addsy
!= (symbolS
*) NULL
&& ! S_IS_DEFINED (fixp
->fx_addsy
))
750 /* The symbol is undefined. Let the linker figure it out. */
753 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
757 md_apply_fix3 (fixp
, valuep
, seg
)
768 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
773 else if (fixp
->fx_pcrel
)
777 value
= fixp
->fx_offset
;
778 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
780 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
781 value
-= S_GET_VALUE (fixp
->fx_subsy
);
784 /* We don't actually support subtracting a symbol. */
785 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
786 "expression too complex");
791 /* printf("md_apply_fix: value=0x%x type=%d\n", value, fixp->fx_r_type); */
793 if ((int) fixp
->fx_r_type
>= (int) BFD_RELOC_UNUSED
)
796 const struct mn10300_operand
*operand
;
798 unsigned long insn
, extension
;
800 opindex
= (int) fixp
->fx_r_type
- (int) BFD_RELOC_UNUSED
;
801 operand
= &mn10300_operands
[opindex
];
803 /* Fetch the instruction, insert the fully resolved operand
804 value, and stuff the instruction back again.
806 Note the instruction has been stored in little endian
808 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
810 insn
= bfd_getl32((unsigned char *) where
);
812 mn10300_insert_operand (&insn
, &extension
, operand
,
813 (offsetT
) value
, fixp
->fx_file
,
815 bfd_putl32((bfd_vma
) insn
, (unsigned char *) where
);
819 /* Nothing else to do here. */
823 /* Determine a BFD reloc value based on the operand information.
824 We are only prepared to turn a few of the operands into relocs. */
827 as_bad_where(fixp
->fx_file
, fixp
->fx_line
,
828 "unresolved expression that must be resolved");
833 else if (fixp
->fx_done
)
835 /* We still have to insert the value into memory! */
836 where
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
837 if (fixp
->fx_size
== 1)
838 *where
= value
& 0xff;
839 if (fixp
->fx_size
== 2)
840 bfd_putl16(value
& 0xffff, (unsigned char *) where
);
841 if (fixp
->fx_size
== 4)
842 bfd_putl32(value
, (unsigned char *) where
);
845 fixp
->fx_addnumber
= value
;
849 /* Insert an operand value into an instruction. */
852 mn10300_insert_operand (insnp
, extensionp
, operand
, val
, file
, line
, shift
)
853 unsigned long *insnp
;
854 unsigned long *extensionp
;
855 const struct mn10300_operand
*operand
;
861 if (operand
->bits
!= 32)
866 if ((operand
->flags
& MN10300_OPERAND_SIGNED
) != 0)
868 max
= (1 << (operand
->bits
- 1)) - 1;
869 min
= - (1 << (operand
->bits
- 1));
873 max
= (1 << operand
->bits
) - 1;
880 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
883 "operand out of range (%s not between %ld and %ld)";
886 sprint_value (buf
, test
);
887 if (file
== (char *) NULL
)
888 as_warn (err
, buf
, min
, max
);
890 as_warn_where (file
, line
, err
, buf
, min
, max
);
894 if ((operand
->flags
& MN10300_OPERAND_SPLIT
) != 0)
896 *insnp
|= (val
>> 16) & 0xffff;
897 *extensionp
|= (val
& 0xffff) << operand
->shift
;
899 else if ((operand
->flags
& MN10300_OPERAND_EXTENDED
) == 0)
901 *insnp
|= (((long) val
& ((1 << operand
->bits
) - 1))
902 << (operand
->shift
+ shift
));
904 if ((operand
->flags
& MN10300_OPERAND_REPEATED
) != 0)
905 *insnp
|= (((long) val
& ((1 << operand
->bits
) - 1))
906 << (operand
->shift
+ shift
+ 2));
910 *extensionp
|= (((long) val
& ((1 << operand
->bits
) - 1))
911 << (operand
->shift
+ shift
));
913 if ((operand
->flags
& MN10300_OPERAND_REPEATED
) != 0)
914 *extensionp
|= (((long) val
& ((1 << operand
->bits
) - 1))
915 << (operand
->shift
+ shift
+ 2));
920 check_operand (insn
, operand
, val
)
922 const struct mn10300_operand
*operand
;
925 if (operand
->bits
!= 32)
930 if ((operand
->flags
& MN10300_OPERAND_SIGNED
) != 0)
932 max
= (1 << (operand
->bits
- 1)) - 1;
933 min
= - (1 << (operand
->bits
- 1));
937 max
= (1 << operand
->bits
) - 1;
944 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)