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 (C) 1989, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
5 Free Software Foundation, Inc.
7 This file is part of GAS, the GNU Assembler.
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
27 #include "m88k-opcode.h"
29 struct field_val_assoc
35 struct field_val_assoc cr_regs
[] =
62 struct field_val_assoc fcr_regs
[] =
80 struct field_val_assoc cmpslot
[] =
82 /* Integer Floating point */
93 {"lo", 10}, {"in", 10},
94 {"hs", 11}, {"ob", 11},
95 {"be", 12}, {"ue", 12},
96 {"nb", 13}, {"lg", 13},
97 {"he", 14}, {"ug", 14},
98 {"nh", 15}, {"ule", 15},
105 struct field_val_assoc cndmsk
[] =
119 unsigned long opcode
;
121 enum reloc_type reloc
;
124 static char *get_bf
PARAMS ((char *param
, unsigned *valp
));
125 static char *get_cmp
PARAMS ((char *param
, unsigned *valp
));
126 static char *get_cnd
PARAMS ((char *param
, unsigned *valp
));
127 static char *get_cr
PARAMS ((char *param
, unsigned *regnop
));
128 static char *get_fcr
PARAMS ((char *param
, unsigned *regnop
));
129 static char *get_imm16
PARAMS ((char *param
, struct m88k_insn
*insn
));
130 static char *get_o6
PARAMS ((char *param
, unsigned *valp
));
131 static char *get_reg
PARAMS ((char *param
, unsigned *regnop
, int reg_prefix
));
132 static char *get_vec9
PARAMS ((char *param
, unsigned *valp
));
133 static char *getval
PARAMS ((char *param
, unsigned int *valp
));
135 static char *get_pcr
PARAMS ((char *param
, struct m88k_insn
*insn
,
136 enum reloc_type reloc
));
138 static int calcop
PARAMS ((struct m88k_opcode
*format
,
139 char *param
, struct m88k_insn
*insn
));
142 static struct hash_control
*op_hash
= NULL
;
144 /* These bits should be turned off in the first address of every segment */
145 int md_seg_align
= 7;
147 /* These chars start a comment anywhere in a source file (except inside
149 const char comment_chars
[] = ";";
151 /* These chars only start a comment at the beginning of a line. */
152 const char line_comment_chars
[] = "#";
154 const char line_separator_chars
[] = "";
156 /* Chars that can be used to separate mant from exp in floating point nums */
157 const char EXP_CHARS
[] = "eE";
159 /* Chars that mean this number is a floating point constant */
160 /* as in 0f123.456 */
161 /* or 0H1.234E-12 (see exp chars above) */
162 const char FLT_CHARS
[] = "dDfF";
164 extern void float_cons (), cons (), s_globl (), s_space (),
165 s_set (), s_lcomm ();
167 const pseudo_typeS md_pseudo_table
[] =
169 {"align", s_align_bytes
, 4},
171 {"dfloat", float_cons
, 'd'},
172 {"ffloat", float_cons
, 'f'},
173 {"global", s_globl
, 0},
176 {"string", stringer
, 0},
178 /* Force set to be treated as an instruction. */
187 const char *retval
= NULL
;
190 /* initialize hash table */
192 op_hash
= hash_new ();
194 /* loop until you see the end of the list */
196 while (*m88k_opcodes
[i
].name
)
198 char *name
= m88k_opcodes
[i
].name
;
200 /* hash each mnemonic and record its position */
202 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
205 as_fatal (_("Can't hash instruction '%s':%s"),
206 m88k_opcodes
[i
].name
, retval
);
208 /* skip to next unique mnemonic or end of list */
210 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
215 CONST
char *md_shortopts
= "";
216 struct option md_longopts
[] = {
217 {NULL
, no_argument
, NULL
, 0}
219 size_t md_longopts_size
= sizeof(md_longopts
);
222 md_parse_option (c
, arg
)
230 md_show_usage (stream
)
239 char *param
, *thisfrag
;
241 struct m88k_opcode
*format
;
242 struct m88k_insn insn
;
246 /* skip over instruction to find parameters */
248 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
253 /* try to find the instruction in the hash table */
255 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
257 as_bad (_("Invalid mnemonic '%s'"), op
);
261 /* try parsing this instruction into insn */
263 insn
.exp
.X_add_symbol
= 0;
264 insn
.exp
.X_op_symbol
= 0;
265 insn
.exp
.X_add_number
= 0;
266 insn
.exp
.X_op
= O_illegal
;
267 insn
.reloc
= NO_RELOC
;
269 while (!calcop (format
, param
, &insn
))
271 /* if it doesn't parse try the next instruction */
273 if (!strcmp (format
[0].name
, format
[1].name
))
277 as_fatal (_("Parameter syntax error"));
282 /* grow the current frag and plop in the opcode */
284 thisfrag
= frag_more (4);
285 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
287 /* if this instruction requires labels mark it for later */
296 fix_new_exp (frag_now
,
297 thisfrag
- frag_now
->fr_literal
+ 2,
305 fix_new_exp (frag_now
,
306 thisfrag
- frag_now
->fr_literal
,
314 fix_new_exp (frag_now
,
315 thisfrag
- frag_now
->fr_literal
+ 2,
323 fix_new_exp (frag_now
,
324 thisfrag
- frag_now
->fr_literal
,
332 as_fatal (_("Unknown relocation type"));
338 calcop (format
, param
, insn
)
339 struct m88k_opcode
*format
;
341 struct m88k_insn
*insn
;
343 char *fmt
= format
->op_spec
;
347 int reg_prefix
= 'r';
349 insn
->opcode
= format
->opcode
;
360 insn
->opcode
|= opcode
;
361 return (*param
== 0 || *param
== '\n');
369 param
= get_reg (param
, &val
, reg_prefix
);
375 param
= get_o6 (param
, &val
);
376 opcode
|= ((val
>> 2) << 7);
384 param
= get_reg (param
, &val
, reg_prefix
);
390 param
= get_reg (param
, &val
, reg_prefix
);
396 param
= get_reg (param
, &val
, 'r');
397 opcode
|= (val
<< 16) | val
;
401 param
= get_imm16 (param
, insn
);
405 param
= get_bf (param
, &val
);
410 param
= get_pcr (param
, insn
, RELOC_PC16
);
414 param
= get_pcr (param
, insn
, RELOC_PC26
);
418 param
= get_cmp (param
, &val
);
423 param
= get_cnd (param
, &val
);
428 param
= get_cr (param
, &val
);
433 param
= get_fcr (param
, &val
);
438 param
= get_vec9 (param
, &val
);
443 /* Having this here repeats the warning somtimes.
444 But can't we stand that? */
445 as_warn (_("Use of obsolete instruction"));
452 match_name (param
, assoc_tab
, valp
)
454 struct field_val_assoc
*assoc_tab
;
463 name
= assoc_tab
[i
].name
;
466 name_len
= strlen (name
);
467 if (!strncmp (param
, name
, name_len
))
469 *valp
= assoc_tab
[i
].val
;
470 return param
+ name_len
;
476 get_reg (param
, regnop
, reg_prefix
)
487 regno
= *param
++ - '0';
498 regno
= regno
* 10 + c
;
513 else if (c
== 's' && param
[0] == 'p')
523 get_imm16 (param
, insn
)
525 struct m88k_insn
*insn
;
527 enum reloc_type reloc
= NO_RELOC
;
531 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
536 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
541 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
547 save_ptr
= input_line_pointer
;
548 input_line_pointer
= param
;
549 expression (&insn
->exp
);
550 param
= input_line_pointer
;
551 input_line_pointer
= save_ptr
;
553 val
= insn
->exp
.X_add_number
;
555 if (insn
->exp
.X_op
== O_constant
)
557 /* Insert the value now, and reset reloc to NO_RELOC. */
558 if (reloc
== NO_RELOC
)
560 /* Warn about too big expressions if not surrounded by xx16. */
562 as_warn (_("Expression truncated to 16 bits"));
565 if (reloc
== RELOC_HI16
)
568 insn
->opcode
|= val
& 0xffff;
571 else if (reloc
== NO_RELOC
)
572 /* We accept a symbol even without lo16, hi16, etc, and assume
573 lo16 was intended. */
582 get_pcr (param
, insn
, reloc
)
584 struct m88k_insn
*insn
;
585 enum reloc_type reloc
;
587 char *saveptr
, *saveparam
;
589 saveptr
= input_line_pointer
;
590 input_line_pointer
= param
;
592 expression (&insn
->exp
);
594 saveparam
= input_line_pointer
;
595 input_line_pointer
= saveptr
;
597 /* Botch: We should relocate now if O_constant. */
604 get_cmp (param
, valp
)
613 param
= match_name (param
, cmpslot
, valp
);
620 save_ptr
= input_line_pointer
;
621 input_line_pointer
= param
;
622 val
= get_absolute_expression ();
623 param
= input_line_pointer
;
624 input_line_pointer
= save_ptr
;
628 as_warn (_("Expression truncated to 5 bits"));
638 get_cnd (param
, valp
)
644 if (isdigit (*param
))
646 param
= getval (param
, &val
);
650 as_warn (_("Expression truncated to 5 bits"));
656 if (isupper (*param
))
657 *param
= tolower (*param
);
659 if (isupper (param
[1]))
660 param
[1] = tolower (param
[1]);
662 param
= match_name (param
, cndmsk
, valp
);
691 else if (c
== bc
&& depth
<= 0)
698 get_bf_offset_expression (param
, offsetp
)
704 if (isalpha (param
[0]))
706 if (isupper (param
[0]))
707 param
[0] = tolower (param
[0]);
708 if (isupper (param
[1]))
709 param
[1] = tolower (param
[1]);
711 param
= match_name (param
, cmpslot
, offsetp
);
717 input_line_pointer
= param
;
718 offset
= get_absolute_expression ();
719 param
= input_line_pointer
;
736 xp
= get_bf2 (param
, '<');
738 save_ptr
= input_line_pointer
;
739 input_line_pointer
= param
;
742 /* We did not find '<'. We have an offset (width implicitly 32). */
743 param
= get_bf_offset_expression (param
, &offset
);
744 input_line_pointer
= save_ptr
;
750 *xp
++ = 0; /* Overwrite the '<' */
751 param
= get_bf2 (xp
, '>');
754 *param
++ = 0; /* Overwrite the '>' */
756 width
= get_absolute_expression ();
757 xp
= get_bf_offset_expression (xp
, &offset
);
758 input_line_pointer
= save_ptr
;
764 *valp
= ((width
% 32) << 5) | (offset
% 32);
770 get_cr (param
, regnop
)
777 if (!strncmp (param
, "cr", 2))
781 regno
= *param
++ - '0';
792 regno
= regno
* 10 + c
;
808 param
= match_name (param
, cr_regs
, regnop
);
814 get_fcr (param
, regnop
)
821 if (!strncmp (param
, "fcr", 3))
825 regno
= *param
++ - '0';
836 regno
= regno
* 10 + c
;
852 param
= match_name (param
, fcr_regs
, regnop
);
858 get_vec9 (param
, valp
)
865 save_ptr
= input_line_pointer
;
866 input_line_pointer
= param
;
867 val
= get_absolute_expression ();
868 param
= input_line_pointer
;
869 input_line_pointer
= save_ptr
;
872 as_warn (_("Expression truncated to 9 bits"));
874 *valp
= val
% (1 << 9);
887 save_ptr
= input_line_pointer
;
888 input_line_pointer
= param
;
889 val
= get_absolute_expression ();
890 param
= input_line_pointer
;
891 input_line_pointer
= save_ptr
;
894 as_warn (_("Removed lower 2 bits of expression"));
902 (isdigit (z) ? (z) - '0' : \
903 islower (z) ? (z) - 'a' + 10 : \
904 isupper (z) ? (z) - 'A' + 10 : -1)
911 unsigned int val
= 0;
918 if (c
== 'x' || c
== 'X')
954 md_number_to_chars (buf
, val
, nbytes
)
959 number_to_chars_bigendian (buf
, val
, nbytes
);
964 /* This routine is never called. What is it for?
965 Ian Taylor, Cygnus Support 13 Jul 1993 */
968 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
975 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
994 switch (fixP
->fx_r_type
)
1019 buf
[0] |= (val
>> 26) & 0x03;
1033 as_fatal (_("Bad relocation type"));
1041 md_number_to_disp (buf
, val
, nbytes
)
1046 as_fatal (_("md_number_to_disp not defined"));
1047 md_number_to_chars (buf
, val
, nbytes
);
1051 md_number_to_field (buf
, val
, nbytes
)
1056 as_fatal (_("md_number_to_field not defined"));
1057 md_number_to_chars (buf
, val
, nbytes
);
1060 #define MAX_LITTLENUMS 6
1062 /* Turn a string in input_line_pointer into a floating point constant of type
1063 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1064 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1067 md_atof (type
, litP
, sizeP
)
1073 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1074 LITTLENUM_TYPE
*wordP
;
1105 return _("Bad call to MD_ATOF()");
1107 t
= atof_ieee (input_line_pointer
, type
, words
);
1109 input_line_pointer
= t
;
1111 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1112 for (wordP
= words
; prec
--;)
1114 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1115 litP
+= sizeof (LITTLENUM_TYPE
);
1120 int md_short_jump_size
= 4;
1123 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1125 addressT from_addr
, to_addr
;
1129 ptr
[0] = (char) 0xc0;
1134 ptr
- frag
->fr_literal
,
1139 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1142 int md_long_jump_size
= 4;
1145 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1147 addressT from_addr
, to_addr
;
1151 ptr
[0] = (char) 0xc0;
1156 ptr
- frag
->fr_literal
,
1165 md_estimate_size_before_relax (fragP
, segment_type
)
1169 as_fatal (_("Relaxation should never occur"));
1175 /* As far as I can tell, this routine is never called. What is it
1177 Ian Taylor, Cygnus Support 13 Jul 1993 */
1180 * Risc relocations are completely different, so it needs
1181 * this machine dependent routine to emit them.
1184 emit_relocations (fixP
, segment_address_in_file
)
1186 relax_addressT segment_address_in_file
;
1188 struct reloc_info_m88k ri
;
1190 extern char *next_object_file_charP
;
1192 bzero ((char *) &ri
, sizeof (ri
));
1193 for (; fixP
; fixP
= fixP
->fx_next
)
1195 if (fixP
->fx_r_type
>= NO_RELOC
)
1197 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1201 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1203 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1204 fixP
->fx_where
- segment_address_in_file
;
1205 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1208 ri
.r_symbolnum
= symbolP
->sy_number
;
1213 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1215 if (symbolP
&& symbol_get_frag (symbolP
))
1217 ri
.r_addend
= symbol_get_frag (symbolP
)->fr_address
;
1219 ri
.r_type
= fixP
->fx_r_type
;
1222 ri
.r_addend
-= ri
.r_address
;
1226 ri
.r_addend
= fixP
->fx_addnumber
;
1229 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1238 /* This routine can be subsumed by s_lcomm in read.c.
1239 Ian Taylor, Cygnus Support 13 Jul 1993 */
1247 int temp
, bss_align
;
1250 name
= input_line_pointer
;
1251 c
= get_symbol_end ();
1252 p
= input_line_pointer
;
1255 if (*input_line_pointer
!= ',')
1257 as_warn (_("Expected comma after name"));
1258 ignore_rest_of_line ();
1261 input_line_pointer
++;
1262 if ((temp
= get_absolute_expression ()) < 0)
1264 as_warn (_("BSS length (%d.) <0! Ignored."), temp
);
1265 ignore_rest_of_line ();
1269 symbolP
= symbol_find_or_make (name
);
1271 if (*input_line_pointer
== ',')
1273 input_line_pointer
++;
1274 bss_align
= get_absolute_expression ();
1279 if (!S_IS_DEFINED(symbolP
)
1280 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1285 segT current_seg
= now_seg
;
1286 subsegT current_subseg
= now_subseg
;
1288 subseg_set (SEG_BSS
, 1); /* switch to bss */
1291 frag_align (bss_align
, 0, 0);
1293 /* detach from old frag */
1294 if (symbolP
->sy_type
== N_BSS
&& symbol_get_frag (symbolP
) != NULL
)
1295 symbol_get_frag (symbolP
)->fr_symbol
= NULL
;
1297 symbol_set_frag (symbolP
, frag_now
);
1298 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1299 (offsetT
) temp
, (char *)0);
1301 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1303 subseg_set (current_seg
, current_subseg
);
1308 as_warn (_("Ignoring attempt to re-define symbol %s."), name
);
1311 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
1313 input_line_pointer
++;
1321 /* These functions are needed if we are linking with obj-coffbfd.c.
1322 That file may be replaced by a more BFD oriented version at some
1323 point. If that happens, these functions should be rexamined.
1325 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1327 /* Given a fixS structure (created by a call to fix_new, above),
1328 return the BFD relocation type to use for it. */
1331 tc_coff_fix2rtype (fixp
)
1334 switch (fixp
->fx_r_type
)
1353 /* Apply a fixS to the object file. Since COFF does not use addends
1354 in relocs, the addend is actually stored directly in the object
1358 md_apply_fix (fixp
, val
)
1364 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1365 fixp
->fx_offset
= 0;
1367 switch (fixp
->fx_r_type
)
1370 fixp
->fx_offset
= val
>> 16;
1376 fixp
->fx_offset
= val
>> 16;
1382 fixp
->fx_offset
= val
>> 16;
1393 buf
[0] |= (val
>> 26) & 0x03;
1411 /* Where a PC relative offset is calculated from. On the m88k they
1412 are calculated from just after the instruction. */
1415 md_pcrel_from (fixp
)
1418 switch (fixp
->fx_r_type
)
1421 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1423 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1430 /* When we align the .init section, insert the correct NOP pattern. */
1433 m88k_do_align (n
, fill
, max
, len
)
1440 && strcmp (obj_segment_name (now_seg
), ".init") == 0)
1442 static const unsigned char nop_pattern
[] = { 0xf4, 0x00, 0x58, 0x00 };
1443 frag_align_pattern (n
, nop_pattern
, sizeof (nop_pattern
), max
);
1449 #endif /* M88KCOFF */