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, 1990, 1991, 1993 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to
20 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "m88k-opcode.h"
37 struct field_val_assoc
43 struct field_val_assoc cr_regs
[] =
70 struct field_val_assoc fcr_regs
[] =
88 struct field_val_assoc cmpslot
[] =
90 /* Integer Floating point */
100 {"ls", 9}, {"ib", 9},
101 {"lo", 10}, {"in", 10},
102 {"hs", 11}, {"ob", 11},
103 {"be", 12}, {"ue", 12},
104 {"nb", 13}, {"lg", 13},
105 {"he", 14}, {"ug", 14},
106 {"nh", 15}, {"ule", 15},
113 struct field_val_assoc cndmsk
[] =
127 unsigned long opcode
;
129 enum reloc_type reloc
;
133 static struct hash_control
*op_hash
= NULL
;
135 /* These bits should be turned off in the first address of every segment */
136 int md_seg_align
= 7;
138 /* These chars start a comment anywhere in a source file (except inside
140 const char comment_chars
[] = ";";
142 /* These chars only start a comment at the beginning of a line. */
143 const char line_comment_chars
[] = "#";
145 const char line_separator_chars
[] = "";
147 /* Chars that can be used to separate mant from exp in floating point nums */
148 const char EXP_CHARS
[] = "eE";
150 /* Chars that mean this number is a floating point constant */
151 /* as in 0f123.456 */
152 /* or 0H1.234E-12 (see exp chars above) */
153 const char FLT_CHARS
[] = "dDfF";
155 extern void float_cons (), cons (), s_globl (), s_space (),
156 s_set (), s_lcomm ();
157 static void s_file ();
159 const pseudo_typeS md_pseudo_table
[] =
161 {"align", s_align_bytes
, 4},
163 {"dfloat", float_cons
, 'd'},
164 {"ffloat", float_cons
, 'f'},
165 {"global", s_globl
, 0},
168 {"string", stringer
, 0},
170 {"zero", s_space
, 0},
180 /* initialize hash table */
182 op_hash
= hash_new ();
184 as_fatal ("Could not initialize hash table");
186 /* loop until you see the end of the list */
188 while (*m88k_opcodes
[i
].name
)
190 char *name
= m88k_opcodes
[i
].name
;
192 /* hash each mnemonic and record its position */
194 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
196 if (retval
!= NULL
&& *retval
!= '\0')
197 as_fatal ("Can't hash instruction '%s':%s",
198 m88k_opcodes
[i
].name
, retval
);
200 /* skip to next unique mnemonic or end of list */
202 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
208 md_parse_option (argP
, cntP
, vecP
)
220 char *param
, *thisfrag
;
222 struct m88k_opcode
*format
;
223 struct m88k_insn insn
;
227 /* skip over instruction to find parameters */
229 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
234 /* try to find the instruction in the hash table */
236 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
238 extern struct hash_control
*po_hash
;
242 /* The m88k assembler does not use `.' before pseudo-ops, for
243 some reason. So if don't find an opcode, try for a
245 pop
= (pseudo_typeS
*) hash_find (po_hash
, op
);
249 as_bad ("Invalid mnemonic '%s'", op
);
253 /* Restore the character after the opcode. */
256 /* Now we have to hack. The pseudo-op code expects
257 input_line_pointer to point to the first non-whitespace
258 character after the pseudo-op itself. The calling code has
259 already advanced input_line_pointer to the end of the line
260 and inserted a null byte. We set things up for the pseudo-op
261 code, and then prepare to return from this function. */
262 hold
= input_line_pointer
;
264 input_line_pointer
= param
;
267 (*pop
->poc_handler
) (pop
->poc_val
);
269 input_line_pointer
= hold
;
274 /* try parsing this instruction into insn */
276 insn
.exp
.X_add_symbol
= 0;
277 insn
.exp
.X_op_symbol
= 0;
278 insn
.exp
.X_add_number
= 0;
279 insn
.exp
.X_op
= O_illegal
;
280 insn
.reloc
= NO_RELOC
;
282 while (!calcop (format
, param
, &insn
))
284 /* if it doesn't parse try the next instruction */
286 if (!strcmp (format
[0].name
, format
[1].name
))
290 as_fatal ("Parameter syntax error");
295 /* grow the current frag and plop in the opcode */
297 thisfrag
= frag_more (4);
298 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
300 /* if this instruction requires labels mark it for later */
309 fix_new_exp (frag_now
,
310 thisfrag
- frag_now
->fr_literal
+ 2,
318 fix_new_exp (frag_now
,
319 thisfrag
- frag_now
->fr_literal
,
327 fix_new_exp (frag_now
,
328 thisfrag
- frag_now
->fr_literal
+ 2,
336 fix_new_exp (frag_now
,
337 thisfrag
- frag_now
->fr_literal
,
345 as_fatal ("Unknown relocation type");
351 calcop (format
, param
, insn
)
352 struct m88k_opcode
*format
;
354 struct m88k_insn
*insn
;
356 char *fmt
= format
->op_spec
;
360 int reg_prefix
= 'r';
362 insn
->opcode
= format
->opcode
;
373 insn
->opcode
|= opcode
;
382 param
= get_reg (param
, &val
, reg_prefix
);
392 param
= get_reg (param
, &val
, reg_prefix
);
398 param
= get_reg (param
, &val
, reg_prefix
);
404 param
= get_reg (param
, &val
, 'r');
405 opcode
|= (val
<< 16) | val
;
409 param
= get_imm16 (param
, insn
);
413 param
= get_bf (param
, &val
);
418 param
= get_pcr (param
, insn
, RELOC_PC16
);
422 param
= get_pcr (param
, insn
, RELOC_PC26
);
426 param
= get_cmp (param
, &val
);
431 param
= get_cnd (param
, &val
);
436 param
= get_cr (param
, &val
);
441 param
= get_fcr (param
, &val
);
446 param
= get_vec9 (param
, &val
);
451 /* Having this here repeats the warning somtimes.
452 But can't we stand that? */
453 as_warn ("Use of obsolete instruction");
460 match_name (param
, assoc_tab
, valp
)
462 struct field_val_assoc
*assoc_tab
;
471 name
= assoc_tab
[i
].name
;
474 name_len
= strlen (name
);
475 if (!strncmp (param
, name
, name_len
))
477 *valp
= assoc_tab
[i
].val
;
478 return param
+ name_len
;
484 get_reg (param
, regnop
, reg_prefix
)
495 regno
= *param
++ - '0';
506 regno
= regno
* 10 + c
;
521 else if (c
== 's' && param
[0] == 'p')
531 get_imm16 (param
, insn
)
533 struct m88k_insn
*insn
;
535 enum reloc_type reloc
= NO_RELOC
;
539 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
544 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
549 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
555 save_ptr
= input_line_pointer
;
556 input_line_pointer
= param
;
557 expression (&insn
->exp
);
558 param
= input_line_pointer
;
559 input_line_pointer
= save_ptr
;
561 val
= insn
->exp
.X_add_number
;
563 if (insn
->exp
.X_op
== O_constant
)
565 /* Insert the value now, and reset reloc to NO_RELOC. */
566 if (reloc
== NO_RELOC
)
568 /* Warn about too big expressions if not surrounded by xx16. */
570 as_warn ("Expression truncated to 16 bits");
573 if (reloc
== RELOC_HI16
)
576 insn
->opcode
|= val
& 0xffff;
579 else if (reloc
== NO_RELOC
)
580 /* We accept a symbol even without lo16, hi16, etc, and assume
581 lo16 was intended. */
590 get_pcr (param
, insn
, reloc
)
592 struct m88k_insn
*insn
;
593 enum reloc_type reloc
;
595 char *saveptr
, *saveparam
;
597 saveptr
= input_line_pointer
;
598 input_line_pointer
= param
;
600 expression (&insn
->exp
);
602 saveparam
= input_line_pointer
;
603 input_line_pointer
= saveptr
;
605 /* Botch: We should relocate now if O_constant. */
612 get_cmp (param
, valp
)
621 param
= match_name (param
, cmpslot
, valp
);
628 save_ptr
= input_line_pointer
;
629 input_line_pointer
= param
;
630 val
= get_absolute_expression ();
631 param
= input_line_pointer
;
632 input_line_pointer
= save_ptr
;
636 as_warn ("Expression truncated to 5 bits");
646 get_cnd (param
, valp
)
652 if (isdigit (*param
))
654 param
= getval (param
, &val
);
658 as_warn ("Expression truncated to 5 bits");
664 if (isupper (*param
))
665 *param
= tolower (*param
);
667 if (isupper (param
[1]))
668 param
[1] = tolower (param
[1]);
670 param
= match_name (param
, cndmsk
, valp
);
699 else if (c
== bc
&& depth
<= 0)
706 get_bf_offset_expression (param
, offsetp
)
712 if (isalpha (param
[0]))
714 if (isupper (param
[0]))
715 param
[0] = tolower (param
[0]);
716 if (isupper (param
[1]))
717 param
[1] = tolower (param
[1]);
719 param
= match_name (param
, cmpslot
, offsetp
);
725 input_line_pointer
= param
;
726 offset
= get_absolute_expression ();
727 param
= input_line_pointer
;
744 xp
= get_bf2 (param
, '<');
746 save_ptr
= input_line_pointer
;
747 input_line_pointer
= param
;
750 /* We did not find '<'. We have an offset (width implicitly 32). */
751 param
= get_bf_offset_expression (param
, &offset
);
752 input_line_pointer
= save_ptr
;
758 *xp
++ = 0; /* Overwrite the '<' */
759 param
= get_bf2 (xp
, '>');
762 *param
++ = 0; /* Overwrite the '>' */
764 width
= get_absolute_expression ();
765 xp
= get_bf_offset_expression (xp
, &offset
);
766 input_line_pointer
= save_ptr
;
772 *valp
= ((width
% 32) << 5) | (offset
% 32);
778 get_cr (param
, regnop
)
787 if (!strncmp (param
, "cr", 2))
791 regno
= *param
++ - '0';
802 regno
= regno
* 10 + c
;
818 param
= match_name (param
, cr_regs
, regnop
);
824 get_fcr (param
, regnop
)
833 if (!strncmp (param
, "fcr", 3))
837 regno
= *param
++ - '0';
848 regno
= regno
* 10 + c
;
864 param
= match_name (param
, fcr_regs
, regnop
);
870 get_vec9 (param
, valp
)
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 ("Expression truncated to 9 bits");
886 *valp
= val
% (1 << 9);
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
)
967 /* This routine is never called. What is it for?
968 Ian Taylor, Cygnus Support 13 Jul 1993 */
971 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
978 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
997 switch (fixP
->fx_r_type
)
1022 buf
[0] |= (val
>> 26) & 0x03;
1036 as_fatal ("Bad relocation type");
1044 md_number_to_disp (buf
, val
, nbytes
)
1049 as_fatal ("md_number_to_disp not defined");
1050 md_number_to_chars (buf
, val
, nbytes
);
1054 md_number_to_field (buf
, val
, nbytes
)
1059 as_fatal ("md_number_to_field not defined");
1060 md_number_to_chars (buf
, val
, nbytes
);
1063 #define MAX_LITTLENUMS 6
1065 /* Turn a string in input_line_pointer into a floating point constant of type
1066 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1067 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1070 md_atof (type
, litP
, sizeP
)
1076 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1077 LITTLENUM_TYPE
*wordP
;
1109 return "Bad call to MD_ATOF()";
1111 t
= atof_ieee (input_line_pointer
, type
, words
);
1113 input_line_pointer
= t
;
1115 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1116 for (wordP
= words
; prec
--;)
1118 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1119 litP
+= sizeof (LITTLENUM_TYPE
);
1121 return ""; /* Someone should teach Dean about null pointers */
1124 int md_short_jump_size
= 4;
1127 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1129 addressT from_addr
, to_addr
;
1133 ptr
[0] = (char) 0xc0;
1138 ptr
- frag
->fr_literal
,
1143 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1146 int md_long_jump_size
= 4;
1149 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1151 addressT from_addr
, to_addr
;
1155 ptr
[0] = (char) 0xc0;
1160 ptr
- frag
->fr_literal
,
1169 md_estimate_size_before_relax (fragP
, segment_type
)
1173 as_fatal ("Relaxation should never occur");
1176 const relax_typeS md_relax_table
[] =
1186 /* As far as I can tell, this routine is never called. What is it
1188 Ian Taylor, Cygnus Support 13 Jul 1993 */
1192 * Risc relocations are completely different, so it needs
1193 * this machine dependent routine to emit them.
1196 emit_relocations (fixP
, segment_address_in_file
)
1198 relax_addressT segment_address_in_file
;
1200 struct reloc_info_m88k ri
;
1202 extern char *next_object_file_charP
;
1204 bzero ((char *) &ri
, sizeof (ri
));
1205 for (; fixP
; fixP
= fixP
->fx_next
)
1207 if (fixP
->fx_r_type
>= NO_RELOC
)
1209 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1213 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1215 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1216 fixP
->fx_where
- segment_address_in_file
;
1217 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1220 ri
.r_symbolnum
= symbolP
->sy_number
;
1225 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1227 if (symbolP
&& symbolP
->sy_frag
)
1229 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1231 ri
.r_type
= fixP
->fx_r_type
;
1234 ri
.r_addend
-= ri
.r_address
;
1238 ri
.r_addend
= fixP
->fx_addnumber
;
1241 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1251 /* This routine can be subsumed by s_lcomm in read.c.
1252 Ian Taylor, Cygnus Support 13 Jul 1993 */
1261 int temp
, bss_align
;
1264 name
= input_line_pointer
;
1265 c
= get_symbol_end ();
1266 p
= input_line_pointer
;
1269 if (*input_line_pointer
!= ',')
1271 as_warn ("Expected comma after name");
1272 ignore_rest_of_line ();
1275 input_line_pointer
++;
1276 if ((temp
= get_absolute_expression ()) < 0)
1278 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1279 ignore_rest_of_line ();
1283 symbolP
= symbol_find_or_make (name
);
1285 if (*input_line_pointer
== ',')
1287 input_line_pointer
++;
1288 bss_align
= get_absolute_expression ();
1293 if (!S_IS_DEFINED(symbolP
)
1294 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1299 segT current_seg
= now_seg
;
1300 subsegT current_subseg
= now_subseg
;
1302 subseg_new (SEG_BSS
, 1); /* switch to bss */
1305 frag_align (bss_align
, 0);
1307 /* detach from old frag */
1308 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1309 symbolP
->sy_frag
->fr_symbol
= NULL
;
1311 symbolP
->sy_frag
= frag_now
;
1312 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1315 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1317 subseg_new (current_seg
, current_subseg
);
1322 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1325 while (!is_end_of_line
[*input_line_pointer
])
1327 input_line_pointer
++;
1337 /* These functions are needed if we are linking with obj-coffbfd.c.
1338 That file may be replaced by a more BFD oriented version at some
1339 point. If that happens, these functions should be rexamined.
1341 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1343 /* Given a fixS structure (created by a call to fix_new, above),
1344 return the BFD relocation type to use for it. */
1347 tc_coff_fix2rtype (fixp
)
1350 switch (fixp
->fx_r_type
)
1369 /* Apply a fixS to the object file. Since COFF does not use addends
1370 in relocs, the addend is actually stored directly in the object
1374 md_apply_fix (fixp
, val
)
1380 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1382 switch (fixp
->fx_r_type
)
1405 buf
[0] |= (val
>> 26) & 0x03;
1423 /* Where a PC relative offset is calculated from. On the m88k they
1424 are calculated from just after the instruction. */
1427 md_pcrel_from (fixp
)
1430 switch (fixp
->fx_r_type
)
1433 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1435 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1442 #endif /* M88KCOFF */