1 /* tc-mn10200.c -- Assembler code for the Matsushita 10200
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/mn10200.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 mn10200_insert_operand
PARAMS ((unsigned long *, unsigned long *,
58 const struct mn10200_operand
*,
59 offsetT
, char *, unsigned,
61 static unsigned long check_operand
PARAMS ((unsigned long,
62 const struct mn10200_operand
*,
64 static int reg_name_search
PARAMS ((const struct reg_name
*, int, const char *));
65 static boolean data_register_name
PARAMS ((expressionS
*expressionP
));
66 static boolean address_register_name
PARAMS ((expressionS
*expressionP
));
67 static boolean other_register_name
PARAMS ((expressionS
*expressionP
));
71 #define MAX_INSN_FIXUPS (5)
76 bfd_reloc_code_real_type reloc
;
78 struct mn10200_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
*mn10200_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
[] =
120 #define OTHER_REG_NAME_CNT (sizeof(other_registers) / sizeof(struct reg_name))
122 /* reg_name_search does a binary search of the given register table
123 to see if "name" is a valid regiter name. Returns the register
124 number from the array on success, or -1 on failure. */
127 reg_name_search (regs
, regcount
, name
)
128 const struct reg_name
*regs
;
132 int middle
, low
, high
;
140 middle
= (low
+ high
) / 2;
141 cmp
= strcasecmp (name
, regs
[middle
].name
);
147 return regs
[middle
].value
;
154 /* Summary of register_name().
156 * in: Input_line_pointer points to 1st char of operand.
158 * out: A expressionS.
159 * The operand may have been a register: in this case, X_op == O_register,
160 * X_add_number is set to the register number, and truth is returned.
161 * Input_line_pointer->(next non-blank) char after operand, or is in
162 * its original state.
165 data_register_name (expressionP
)
166 expressionS
*expressionP
;
173 /* Find the spelling of the operand */
174 start
= name
= input_line_pointer
;
176 c
= get_symbol_end ();
177 reg_number
= reg_name_search (data_registers
, DATA_REG_NAME_CNT
, name
);
179 /* look to see if it's in the register table */
182 expressionP
->X_op
= O_register
;
183 expressionP
->X_add_number
= reg_number
;
185 /* make the rest nice */
186 expressionP
->X_add_symbol
= NULL
;
187 expressionP
->X_op_symbol
= NULL
;
188 *input_line_pointer
= c
; /* put back the delimiting char */
193 /* reset the line as if we had not done anything */
194 *input_line_pointer
= c
; /* put back the delimiting char */
195 input_line_pointer
= start
; /* reset input_line pointer */
200 /* Summary of register_name().
202 * in: Input_line_pointer points to 1st char of operand.
204 * out: A expressionS.
205 * The operand may have been a register: in this case, X_op == O_register,
206 * X_add_number is set to the register number, and truth is returned.
207 * Input_line_pointer->(next non-blank) char after operand, or is in
208 * its original state.
211 address_register_name (expressionP
)
212 expressionS
*expressionP
;
219 /* Find the spelling of the operand */
220 start
= name
= input_line_pointer
;
222 c
= get_symbol_end ();
223 reg_number
= reg_name_search (address_registers
, ADDRESS_REG_NAME_CNT
, name
);
225 /* look to see if it's in the register table */
228 expressionP
->X_op
= O_register
;
229 expressionP
->X_add_number
= reg_number
;
231 /* make the rest nice */
232 expressionP
->X_add_symbol
= NULL
;
233 expressionP
->X_op_symbol
= NULL
;
234 *input_line_pointer
= c
; /* put back the delimiting char */
239 /* reset the line as if we had not done anything */
240 *input_line_pointer
= c
; /* put back the delimiting char */
241 input_line_pointer
= start
; /* reset input_line pointer */
246 /* Summary of register_name().
248 * in: Input_line_pointer points to 1st char of operand.
250 * out: A expressionS.
251 * The operand may have been a register: in this case, X_op == O_register,
252 * X_add_number is set to the register number, and truth is returned.
253 * Input_line_pointer->(next non-blank) char after operand, or is in
254 * its original state.
257 other_register_name (expressionP
)
258 expressionS
*expressionP
;
265 /* Find the spelling of the operand */
266 start
= name
= input_line_pointer
;
268 c
= get_symbol_end ();
269 reg_number
= reg_name_search (other_registers
, OTHER_REG_NAME_CNT
, name
);
271 /* look to see if it's in the register table */
274 expressionP
->X_op
= O_register
;
275 expressionP
->X_add_number
= reg_number
;
277 /* make the rest nice */
278 expressionP
->X_add_symbol
= NULL
;
279 expressionP
->X_op_symbol
= NULL
;
280 *input_line_pointer
= c
; /* put back the delimiting char */
285 /* reset the line as if we had not done anything */
286 *input_line_pointer
= c
; /* put back the delimiting char */
287 input_line_pointer
= start
; /* reset input_line pointer */
293 md_show_usage (stream
)
296 fprintf(stream
, "MN10200 options:\n\
301 md_parse_option (c
, arg
)
309 md_undefined_symbol (name
)
316 md_atof (type
, litp
, sizep
)
322 LITTLENUM_TYPE words
[4];
338 return "bad call to md_atof";
341 t
= atof_ieee (input_line_pointer
, type
, words
);
343 input_line_pointer
= t
;
347 for (i
= prec
- 1; i
>= 0; i
--)
349 md_number_to_chars (litp
, (valueT
) words
[i
], 2);
358 md_convert_frag (abfd
, sec
, fragP
)
363 /* printf ("call to md_convert_frag \n"); */
368 md_section_align (seg
, addr
)
372 int align
= bfd_get_section_alignment (stdoutput
, seg
);
373 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
379 char *prev_name
= "";
380 register const struct mn10200_opcode
*op
;
382 mn10200_hash
= hash_new();
384 /* Insert unique names into hash table. The MN10200 instruction set
385 has many identical opcode names that have different opcodes based
386 on the operands. This hash table then provides a quick index to
387 the first opcode with a particular name in the opcode table. */
389 op
= mn10200_opcodes
;
392 if (strcmp (prev_name
, op
->name
))
394 prev_name
= (char *) op
->name
;
395 hash_insert (mn10200_hash
, op
->name
, (char *) op
);
400 /* This is both a simplification (we don't have to write md_apply_fix)
401 and support for future optimizations (branch shortening and similar
402 stuff in the linker. */
411 struct mn10200_opcode
*opcode
;
412 struct mn10200_opcode
*next_opcode
;
413 const unsigned char *opindex_ptr
;
415 unsigned long insn
, extension
, size
= 0;
420 /* Get the opcode. */
421 for (s
= str
; *s
!= '\0' && ! isspace (*s
); s
++)
426 /* find the first opcode with the proper name */
427 opcode
= (struct mn10200_opcode
*)hash_find (mn10200_hash
, str
);
430 as_bad ("Unrecognized opcode: `%s'", str
);
435 while (isspace (*str
))
438 input_line_pointer
= str
;
442 const char *errmsg
= NULL
;
450 insn
= opcode
->opcode
;
452 for (op_idx
= 1, opindex_ptr
= opcode
->operands
;
454 opindex_ptr
++, op_idx
++)
456 const struct mn10200_operand
*operand
;
459 if (next_opindex
== 0)
461 operand
= &mn10200_operands
[*opindex_ptr
];
465 operand
= &mn10200_operands
[next_opindex
];
471 while (*str
== ' ' || *str
== ',')
474 /* Gather the operand. */
475 hold
= input_line_pointer
;
476 input_line_pointer
= str
;
478 if (operand
->flags
& MN10200_OPERAND_PAREN
)
480 if (*input_line_pointer
!= ')' && *input_line_pointer
!= '(')
482 input_line_pointer
= hold
;
486 input_line_pointer
++;
489 /* See if we can match the operands. */
490 else if (operand
->flags
& MN10200_OPERAND_DREG
)
492 if (!data_register_name (&ex
))
494 input_line_pointer
= hold
;
499 else if (operand
->flags
& MN10200_OPERAND_AREG
)
501 if (!address_register_name (&ex
))
503 input_line_pointer
= hold
;
508 else if (operand
->flags
& MN10200_OPERAND_PSW
)
510 char *start
= input_line_pointer
;
511 char c
= get_symbol_end ();
513 if (strcmp (start
, "psw") != 0)
515 *input_line_pointer
= c
;
516 input_line_pointer
= hold
;
520 *input_line_pointer
= c
;
523 else if (operand
->flags
& MN10200_OPERAND_MDR
)
525 char *start
= input_line_pointer
;
526 char c
= get_symbol_end ();
528 if (strcmp (start
, "mdr") != 0)
530 *input_line_pointer
= c
;
531 input_line_pointer
= hold
;
535 *input_line_pointer
= c
;
538 else if (data_register_name (&ex
))
540 input_line_pointer
= hold
;
544 else if (address_register_name (&ex
))
546 input_line_pointer
= hold
;
550 else if (other_register_name (&ex
))
552 input_line_pointer
= hold
;
556 else if (*str
== ')' || *str
== '(')
558 input_line_pointer
= hold
;
570 errmsg
= "illegal operand";
573 errmsg
= "missing operand";
577 & (MN10200_OPERAND_DREG
| MN10200_OPERAND_AREG
)) == 0)
579 input_line_pointer
= hold
;
584 if (opcode
->format
== FMT_2
|| opcode
->format
== FMT_5
)
586 else if (opcode
->format
== FMT_3
|| opcode
->format
== FMT_6
587 || opcode
->format
== FMT_7
)
592 mn10200_insert_operand (&insn
, &extension
, operand
,
593 ex
.X_add_number
, (char *) NULL
,
599 /* If this operand can be promoted, and it doesn't
600 fit into the allocated bitfield for this insn,
601 then promote it (ie this opcode does not match). */
602 if (operand
->flags
& MN10200_OPERAND_PROMOTE
603 && ! check_operand (insn
, operand
, ex
.X_add_number
))
605 input_line_pointer
= hold
;
610 mn10200_insert_operand (&insn
, &extension
, operand
,
611 ex
.X_add_number
, (char *) NULL
,
616 /* If this operand can be promoted, then this opcode didn't
617 match since we can't know if it needed promotion! */
618 if (operand
->flags
& MN10200_OPERAND_PROMOTE
)
620 input_line_pointer
= hold
;
625 /* We need to generate a fixup for this expression. */
626 if (fc
>= MAX_INSN_FIXUPS
)
627 as_fatal ("too many fixups");
629 fixups
[fc
].opindex
= *opindex_ptr
;
630 fixups
[fc
].reloc
= BFD_RELOC_UNUSED
;
636 str
= input_line_pointer
;
637 input_line_pointer
= hold
;
639 while (*str
== ' ' || *str
== ',')
644 /* Make sure we used all the operands! */
651 next_opcode
= opcode
+ 1;
652 if (next_opcode
->opcode
!= 0 && !strcmp(next_opcode
->name
, opcode
->name
))
654 opcode
= next_opcode
;
658 as_bad ("%s", errmsg
);
664 while (isspace (*str
))
668 as_bad ("junk at end of line: `%s'", str
);
670 input_line_pointer
= str
;
672 if (opcode
->format
== FMT_1
)
674 else if (opcode
->format
== FMT_2
|| opcode
->format
== FMT_4
)
676 else if (opcode
->format
== FMT_3
|| opcode
->format
== FMT_5
)
678 else if (opcode
->format
== FMT_6
)
680 else if (opcode
->format
== FMT_7
)
685 /* Write out the instruction. */
687 f
= frag_more (size
);
689 /* Oh, what a mess. The instruction is in big endian format, but
690 16 and 24bit immediates are little endian! */
691 if (opcode
->format
== FMT_3
)
693 number_to_chars_bigendian (f
, (insn
>> 16) & 0xff, 1);
694 number_to_chars_littleendian (f
+ 1, insn
& 0xffff, 2);
696 else if (opcode
->format
== FMT_6
)
698 number_to_chars_bigendian (f
, (insn
>> 16) & 0xffff, 2);
699 number_to_chars_littleendian (f
+ 2, insn
& 0xffff, 2);
701 else if (opcode
->format
== FMT_7
)
703 number_to_chars_bigendian (f
, (insn
>> 16) & 0xffff, 2);
704 number_to_chars_littleendian (f
+ 2, insn
& 0xffff, 2);
705 number_to_chars_littleendian (f
+ 4, extension
& 0xff, 1);
709 number_to_chars_bigendian (f
, insn
, size
> 4 ? 4 : size
);
713 /* Create any fixups. */
714 for (i
= 0; i
< fc
; i
++)
716 const struct mn10200_operand
*operand
;
718 operand
= &mn10200_operands
[fixups
[i
].opindex
];
719 if (fixups
[i
].reloc
!= BFD_RELOC_UNUSED
)
721 reloc_howto_type
*reloc_howto
;
726 reloc_howto
= bfd_reloc_type_lookup (stdoutput
, fixups
[i
].reloc
);
731 size
= bfd_get_reloc_size (reloc_howto
);
733 if (size
< 1 || size
> 4)
737 fixP
= fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ offset
, size
,
739 reloc_howto
->pc_relative
,
744 int reloc
, pcrel
, reloc_size
, offset
;
746 reloc
= BFD_RELOC_NONE
;
747 /* How big is the reloc? Remember SPLIT relocs are
748 implicitly 32bits. */
749 reloc_size
= operand
->bits
;
751 /* Is the reloc pc-relative? */
752 pcrel
= (operand
->flags
& MN10200_OPERAND_PCREL
) != 0;
754 /* Gross. This disgusting hack is to make sure we
755 get the right offset for the 16/32 bit reloc in
756 "call" instructions. Basically they're a pain
757 because the reloc isn't at the end of the instruction. */
758 if ((size
== 5 || size
== 7)
759 && (((insn
>> 24) & 0xff) == 0xcd
760 || ((insn
>> 24) & 0xff) == 0xdd))
763 /* Similarly for certain bit instructions which don't
764 hav their 32bit reloc at the tail of the instruction. */
766 && (((insn
>> 16) & 0xffff) == 0xfe00
767 || ((insn
>> 16) & 0xffff) == 0xfe01
768 || ((insn
>> 16) & 0xffff) == 0xfe02))
771 offset
= size
- reloc_size
/ 8;
773 /* Choose a proper BFD relocation type. */
777 reloc
= BFD_RELOC_MN10200_32_PCREL
;
779 reloc
= BFD_RELOC_MN10200_16_PCREL
;
780 else if (reloc_size
== 32)
781 reloc
= BFD_RELOC_32_PCREL
;
782 else if (reloc_size
== 16)
783 reloc
= BFD_RELOC_16_PCREL
;
784 else if (reloc_size
== 8)
785 reloc
= BFD_RELOC_8_PCREL
;
791 if (reloc_size
== 32)
792 reloc
= BFD_RELOC_MN10200_32B
;
793 else if (reloc_size
== 16)
794 reloc
= BFD_RELOC_MN10200_16B
;
795 else if (reloc_size
== 8)
801 /* Convert the size of the reloc into what fix_new_exp wants. */
802 reloc_size
= reloc_size
/ 8;
805 else if (reloc_size
== 16)
807 else if (reloc_size
== 32)
810 fix_new_exp (frag_now
, f
- frag_now
->fr_literal
+ offset
, reloc_size
,
811 &fixups
[i
].exp
, pcrel
,
812 ((bfd_reloc_code_real_type
) reloc
));
819 /* if while processing a fixup, a reloc really needs to be created */
820 /* then it is done here */
823 tc_gen_reloc (seg
, fixp
)
828 reloc
= (arelent
*) bfd_alloc_by_size_t (stdoutput
, sizeof (arelent
));
829 reloc
->sym_ptr_ptr
= &fixp
->fx_addsy
->bsym
;
830 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
831 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
832 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
834 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
835 "reloc %d not supported by object file format", (int)fixp
->fx_r_type
);
838 reloc
->addend
= fixp
->fx_offset
;
839 /* printf("tc_gen_reloc: addr=%x addend=%x\n", reloc->address, reloc->addend); */
844 md_estimate_size_before_relax (fragp
, seg
)
855 return fixp
->fx_frag
->fr_address
;
857 if (fixp
->fx_addsy
!= (symbolS
*) NULL
&& ! S_IS_DEFINED (fixp
->fx_addsy
))
859 /* The symbol is undefined. Let the linker figure it out. */
862 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
867 md_apply_fix3 (fixp
, valuep
, seg
)
872 /* We shouldn't ever get here because linkrelax is nonzero. */
878 /* Insert an operand value into an instruction. */
881 mn10200_insert_operand (insnp
, extensionp
, operand
, val
, file
, line
, shift
)
882 unsigned long *insnp
;
883 unsigned long *extensionp
;
884 const struct mn10200_operand
*operand
;
890 /* No need to check 32bit operands for a bit. */
891 if (operand
->bits
!= 32)
896 if ((operand
->flags
& MN10200_OPERAND_SIGNED
) != 0)
898 max
= (1 << (operand
->bits
- 1)) - 1;
899 min
= - (1 << (operand
->bits
- 1));
903 max
= (1 << operand
->bits
) - 1;
910 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)
913 "operand out of range (%s not between %ld and %ld)";
916 sprint_value (buf
, test
);
917 if (file
== (char *) NULL
)
918 as_warn (err
, buf
, min
, max
);
920 as_warn_where (file
, line
, err
, buf
, min
, max
);
924 if ((operand
->flags
& MN10200_OPERAND_EXTENDED
) == 0)
926 *insnp
|= (((long) val
& ((1 << operand
->bits
) - 1))
927 << (operand
->shift
+ shift
));
929 if ((operand
->flags
& MN10200_OPERAND_REPEATED
) != 0)
930 *insnp
|= (((long) val
& ((1 << operand
->bits
) - 1))
931 << (operand
->shift
+ shift
+ 2));
935 *extensionp
|= (val
>> 16) & 0xff;
936 *insnp
|= val
& 0xffff;
941 check_operand (insn
, operand
, val
)
943 const struct mn10200_operand
*operand
;
946 /* No need to check 32bit operands for a bit. */
947 if (operand
->bits
!= 32)
952 if ((operand
->flags
& MN10200_OPERAND_SIGNED
) != 0)
954 max
= (1 << (operand
->bits
- 1)) - 1;
955 min
= - (1 << (operand
->bits
- 1));
959 max
= (1 << operand
->bits
) - 1;
966 if (test
< (offsetT
) min
|| test
> (offsetT
) max
)