1 /* m88k.c -- Assembler for the Motorola 88000
2 Contributed by Devon Bowen of Buffalo University
3 and Torbjorn Granlund of the Swedish Institute of Computer Science.
4 Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
6 Free Software Foundation, Inc.
8 This file is part of GAS, the GNU Assembler.
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include "m88k-opcode.h"
30 struct field_val_assoc
36 struct field_val_assoc cr_regs
[] =
63 struct field_val_assoc fcr_regs
[] =
81 struct field_val_assoc cmpslot
[] =
83 /* Integer Floating point */
94 {"lo", 10}, {"in", 10},
95 {"hs", 11}, {"ob", 11},
96 {"be", 12}, {"ue", 12},
97 {"nb", 13}, {"lg", 13},
98 {"he", 14}, {"ug", 14},
99 {"nh", 15}, {"ule", 15},
106 struct field_val_assoc cndmsk
[] =
120 unsigned long opcode
;
122 enum reloc_type reloc
;
125 static char *get_bf
PARAMS ((char *param
, unsigned *valp
));
126 static char *get_cmp
PARAMS ((char *param
, unsigned *valp
));
127 static char *get_cnd
PARAMS ((char *param
, unsigned *valp
));
128 static char *get_cr
PARAMS ((char *param
, unsigned *regnop
));
129 static char *get_fcr
PARAMS ((char *param
, unsigned *regnop
));
130 static char *get_imm16
PARAMS ((char *param
, struct m88k_insn
*insn
));
131 static char *get_o6
PARAMS ((char *param
, unsigned *valp
));
132 static char *get_reg
PARAMS ((char *param
, unsigned *regnop
, char reg_prefix
));
133 static char *get_vec9
PARAMS ((char *param
, unsigned *valp
));
134 static char *getval
PARAMS ((char *param
, unsigned int *valp
));
136 static char *get_pcr
PARAMS ((char *param
, struct m88k_insn
*insn
,
137 enum reloc_type reloc
));
139 static int calcop
PARAMS ((struct m88k_opcode
*format
,
140 char *param
, struct m88k_insn
*insn
));
143 static struct hash_control
*op_hash
= NULL
;
145 /* These bits should be turned off in the first address of every segment */
146 int md_seg_align
= 7;
148 /* These chars start a comment anywhere in a source file (except inside
150 const char comment_chars
[] = ";";
152 /* These chars only start a comment at the beginning of a line. */
153 const char line_comment_chars
[] = "#";
155 const char line_separator_chars
[] = "";
157 /* Chars that can be used to separate mant from exp in floating point nums */
158 const char EXP_CHARS
[] = "eE";
160 /* Chars that mean this number is a floating point constant */
161 /* as in 0f123.456 */
162 /* or 0H1.234E-12 (see exp chars above) */
163 const char FLT_CHARS
[] = "dDfF";
165 extern void float_cons (), cons (), s_globl (), s_space (),
166 s_set (), s_lcomm ();
168 const pseudo_typeS md_pseudo_table
[] =
170 {"align", s_align_bytes
, 4},
172 {"dfloat", float_cons
, 'd'},
173 {"ffloat", float_cons
, 'f'},
174 {"global", s_globl
, 0},
177 {"string", stringer
, 0},
179 /* Force set to be treated as an instruction. */
188 const char *retval
= NULL
;
191 /* Initialize hash table. */
192 op_hash
= hash_new ();
194 while (*m88k_opcodes
[i
].name
)
196 char *name
= m88k_opcodes
[i
].name
;
198 /* Hash each mnemonic and record its position. */
199 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
202 as_fatal (_("Can't hash instruction '%s':%s"),
203 m88k_opcodes
[i
].name
, retval
);
205 /* Skip to next unique mnemonic or end of list. */
206 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
211 CONST
char *md_shortopts
= "";
212 struct option md_longopts
[] = {
213 {NULL
, no_argument
, NULL
, 0}
215 size_t md_longopts_size
= sizeof (md_longopts
);
218 md_parse_option (c
, arg
)
219 int c ATTRIBUTE_UNUSED
;
220 char *arg ATTRIBUTE_UNUSED
;
226 md_show_usage (stream
)
227 FILE *stream ATTRIBUTE_UNUSED
;
235 char *param
, *thisfrag
;
237 struct m88k_opcode
*format
;
238 struct m88k_insn insn
;
242 /* Skip over instruction to find parameters. */
243 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
248 /* Try to find the instruction in the hash table. */
249 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
251 as_bad (_("Invalid mnemonic '%s'"), op
);
255 /* Try parsing this instruction into insn. */
256 insn
.exp
.X_add_symbol
= 0;
257 insn
.exp
.X_op_symbol
= 0;
258 insn
.exp
.X_add_number
= 0;
259 insn
.exp
.X_op
= O_illegal
;
260 insn
.reloc
= NO_RELOC
;
262 while (!calcop (format
, param
, &insn
))
264 /* If it doesn't parse try the next instruction. */
265 if (!strcmp (format
[0].name
, format
[1].name
))
269 as_fatal (_("Parameter syntax error"));
274 /* Grow the current frag and plop in the opcode. */
275 thisfrag
= frag_more (4);
276 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
278 /* If this instruction requires labels mark it for later. */
286 fix_new_exp (frag_now
,
287 thisfrag
- frag_now
->fr_literal
+ 2,
295 fix_new_exp (frag_now
,
296 thisfrag
- frag_now
->fr_literal
,
304 fix_new_exp (frag_now
,
305 thisfrag
- frag_now
->fr_literal
+ 2,
313 fix_new_exp (frag_now
,
314 thisfrag
- frag_now
->fr_literal
,
322 as_fatal (_("Unknown relocation type"));
328 calcop (format
, param
, insn
)
329 struct m88k_opcode
*format
;
331 struct m88k_insn
*insn
;
333 char *fmt
= format
->op_spec
;
337 char reg_prefix
= 'r';
339 insn
->opcode
= format
->opcode
;
350 insn
->opcode
|= opcode
;
351 return (*param
== 0 || *param
== '\n');
359 param
= get_reg (param
, &val
, reg_prefix
);
365 param
= get_o6 (param
, &val
);
366 opcode
|= ((val
>> 2) << 7);
374 param
= get_reg (param
, &val
, reg_prefix
);
380 param
= get_reg (param
, &val
, reg_prefix
);
386 param
= get_reg (param
, &val
, 'r');
387 opcode
|= (val
<< 16) | val
;
391 param
= get_imm16 (param
, insn
);
395 param
= get_bf (param
, &val
);
400 param
= get_pcr (param
, insn
, RELOC_PC16
);
404 param
= get_pcr (param
, insn
, RELOC_PC26
);
408 param
= get_cmp (param
, &val
);
413 param
= get_cnd (param
, &val
);
418 param
= get_cr (param
, &val
);
423 param
= get_fcr (param
, &val
);
428 param
= get_vec9 (param
, &val
);
433 /* Having this here repeats the warning somtimes.
434 But can't we stand that? */
435 as_warn (_("Use of obsolete instruction"));
442 match_name (param
, assoc_tab
, valp
)
444 struct field_val_assoc
*assoc_tab
;
453 name
= assoc_tab
[i
].name
;
456 name_len
= strlen (name
);
457 if (!strncmp (param
, name
, name_len
))
459 *valp
= assoc_tab
[i
].val
;
460 return param
+ name_len
;
466 get_reg (param
, regnop
, reg_prefix
)
477 regno
= *param
++ - '0';
488 regno
= regno
* 10 + c
;
503 else if (c
== 's' && param
[0] == 'p')
513 get_imm16 (param
, insn
)
515 struct m88k_insn
*insn
;
517 enum reloc_type reloc
= NO_RELOC
;
521 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
526 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
531 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
537 save_ptr
= input_line_pointer
;
538 input_line_pointer
= param
;
539 expression (&insn
->exp
);
540 param
= input_line_pointer
;
541 input_line_pointer
= save_ptr
;
543 val
= insn
->exp
.X_add_number
;
545 if (insn
->exp
.X_op
== O_constant
)
547 /* Insert the value now, and reset reloc to NO_RELOC. */
548 if (reloc
== NO_RELOC
)
550 /* Warn about too big expressions if not surrounded by xx16. */
552 as_warn (_("Expression truncated to 16 bits"));
555 if (reloc
== RELOC_HI16
)
558 insn
->opcode
|= val
& 0xffff;
561 else if (reloc
== NO_RELOC
)
562 /* We accept a symbol even without lo16, hi16, etc, and assume
563 lo16 was intended. */
572 get_pcr (param
, insn
, reloc
)
574 struct m88k_insn
*insn
;
575 enum reloc_type reloc
;
577 char *saveptr
, *saveparam
;
579 saveptr
= input_line_pointer
;
580 input_line_pointer
= param
;
582 expression (&insn
->exp
);
584 saveparam
= input_line_pointer
;
585 input_line_pointer
= saveptr
;
587 /* Botch: We should relocate now if O_constant. */
594 get_cmp (param
, valp
)
603 param
= match_name (param
, cmpslot
, valp
);
610 save_ptr
= input_line_pointer
;
611 input_line_pointer
= param
;
612 val
= get_absolute_expression ();
613 param
= input_line_pointer
;
614 input_line_pointer
= save_ptr
;
618 as_warn (_("Expression truncated to 5 bits"));
628 get_cnd (param
, valp
)
634 if (isdigit (*param
))
636 param
= getval (param
, &val
);
640 as_warn (_("Expression truncated to 5 bits"));
646 if (isupper (*param
))
647 *param
= tolower (*param
);
649 if (isupper (param
[1]))
650 param
[1] = tolower (param
[1]);
652 param
= match_name (param
, cndmsk
, valp
);
681 else if (c
== bc
&& depth
<= 0)
688 get_bf_offset_expression (param
, offsetp
)
694 if (isalpha (param
[0]))
696 if (isupper (param
[0]))
697 param
[0] = tolower (param
[0]);
698 if (isupper (param
[1]))
699 param
[1] = tolower (param
[1]);
701 param
= match_name (param
, cmpslot
, offsetp
);
707 input_line_pointer
= param
;
708 offset
= get_absolute_expression ();
709 param
= input_line_pointer
;
726 xp
= get_bf2 (param
, '<');
728 save_ptr
= input_line_pointer
;
729 input_line_pointer
= param
;
732 /* We did not find '<'. We have an offset (width implicitly 32). */
733 param
= get_bf_offset_expression (param
, &offset
);
734 input_line_pointer
= save_ptr
;
740 *xp
++ = 0; /* Overwrite the '<' */
741 param
= get_bf2 (xp
, '>');
744 *param
++ = 0; /* Overwrite the '>' */
746 width
= get_absolute_expression ();
747 xp
= get_bf_offset_expression (xp
, &offset
);
748 input_line_pointer
= save_ptr
;
754 *valp
= ((width
% 32) << 5) | (offset
% 32);
760 get_cr (param
, regnop
)
767 if (!strncmp (param
, "cr", 2))
771 regno
= *param
++ - '0';
782 regno
= regno
* 10 + c
;
798 param
= match_name (param
, cr_regs
, regnop
);
804 get_fcr (param
, regnop
)
811 if (!strncmp (param
, "fcr", 3))
815 regno
= *param
++ - '0';
826 regno
= regno
* 10 + c
;
842 param
= match_name (param
, fcr_regs
, regnop
);
848 get_vec9 (param
, valp
)
855 save_ptr
= input_line_pointer
;
856 input_line_pointer
= param
;
857 val
= get_absolute_expression ();
858 param
= input_line_pointer
;
859 input_line_pointer
= save_ptr
;
862 as_warn (_("Expression truncated to 9 bits"));
864 *valp
= val
% (1 << 9);
877 save_ptr
= input_line_pointer
;
878 input_line_pointer
= param
;
879 val
= get_absolute_expression ();
880 param
= input_line_pointer
;
881 input_line_pointer
= save_ptr
;
884 as_warn (_("Removed lower 2 bits of expression"));
892 (isdigit (z) ? (z) - '0' : \
893 islower (z) ? (z) - 'a' + 10 : \
894 isupper (z) ? (z) - 'A' + 10 : -1)
901 unsigned int val
= 0;
908 if (c
== 'x' || c
== 'X')
944 md_number_to_chars (buf
, val
, nbytes
)
949 number_to_chars_bigendian (buf
, val
, nbytes
);
953 md_number_to_disp (buf
, val
, nbytes
)
958 as_fatal (_("md_number_to_disp not defined"));
959 md_number_to_chars (buf
, val
, nbytes
);
963 md_number_to_field (buf
, val
, nbytes
)
968 as_fatal (_("md_number_to_field not defined"));
969 md_number_to_chars (buf
, val
, nbytes
);
972 #define MAX_LITTLENUMS 6
974 /* Turn a string in input_line_pointer into a floating point constant of type
975 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
976 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
979 md_atof (type
, litP
, sizeP
)
985 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
986 LITTLENUM_TYPE
*wordP
;
1017 return _("Bad call to MD_ATOF()");
1019 t
= atof_ieee (input_line_pointer
, type
, words
);
1021 input_line_pointer
= t
;
1023 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1024 for (wordP
= words
; prec
--;)
1026 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1027 litP
+= sizeof (LITTLENUM_TYPE
);
1032 int md_short_jump_size
= 4;
1035 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1037 addressT from_addr ATTRIBUTE_UNUSED
;
1038 addressT to_addr ATTRIBUTE_UNUSED
;
1042 ptr
[0] = (char) 0xc0;
1047 ptr
- frag
->fr_literal
,
1052 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1055 int md_long_jump_size
= 4;
1058 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1060 addressT from_addr ATTRIBUTE_UNUSED
;
1061 addressT to_addr ATTRIBUTE_UNUSED
;
1065 ptr
[0] = (char) 0xc0;
1070 ptr
- frag
->fr_literal
,
1079 md_estimate_size_before_relax (fragP
, segment_type
)
1080 fragS
*fragP ATTRIBUTE_UNUSED
;
1081 segT segment_type ATTRIBUTE_UNUSED
;
1083 as_fatal (_("Relaxation should never occur"));
1089 /* These functions are needed if we are linking with obj-coffbfd.c.
1090 That file may be replaced by a more BFD oriented version at some
1091 point. If that happens, these functions should be rexamined.
1093 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1095 /* Given a fixS structure (created by a call to fix_new, above),
1096 return the BFD relocation type to use for it. */
1099 tc_coff_fix2rtype (fixp
)
1102 switch (fixp
->fx_r_type
)
1121 /* Apply a fixS to the object file. Since COFF does not use addends
1122 in relocs, the addend is actually stored directly in the object
1126 md_apply_fix (fixp
, val
)
1132 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1133 fixp
->fx_offset
= 0;
1135 switch (fixp
->fx_r_type
)
1138 fixp
->fx_offset
= val
>> 16;
1144 fixp
->fx_offset
= val
>> 16;
1150 fixp
->fx_offset
= val
>> 16;
1161 buf
[0] |= (val
>> 26) & 0x03;
1179 /* Where a PC relative offset is calculated from. On the m88k they
1180 are calculated from just after the instruction. */
1183 md_pcrel_from (fixp
)
1186 switch (fixp
->fx_r_type
)
1189 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1191 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1198 /* Fill in rs_align_code fragments. */
1201 m88k_handle_align (fragp
)
1204 static const unsigned char nop_pattern
[] = { 0xf4, 0x00, 0x58, 0x00 };
1209 if (fragp
->fr_type
!= rs_align_code
)
1212 bytes
= fragp
->fr_next
->fr_address
- fragp
->fr_address
- fragp
->fr_fix
;
1213 p
= fragp
->fr_literal
+ fragp
->fr_fix
;
1217 int fix
= bytes
& 3;
1221 fragp
->fr_fix
+= fix
;
1224 memcpy (p
, nop_pattern
, 4);
1228 #endif /* M88KCOFF */