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 as_bad ("Invalid mnemonic '%s'", op
);
242 /* try parsing this instruction into insn */
244 insn
.exp
.X_add_symbol
= 0;
245 insn
.exp
.X_op_symbol
= 0;
246 insn
.exp
.X_add_number
= 0;
247 insn
.exp
.X_op
= O_illegal
;
248 insn
.reloc
= NO_RELOC
;
250 while (!calcop (format
, param
, &insn
))
252 /* if it doesn't parse try the next instruction */
254 if (!strcmp (format
[0].name
, format
[1].name
))
258 as_fatal ("Parameter syntax error");
263 /* grow the current frag and plop in the opcode */
265 thisfrag
= frag_more (4);
266 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
268 /* if this instruction requires labels mark it for later */
277 fix_new_exp (frag_now
,
278 thisfrag
- frag_now
->fr_literal
+ 2,
286 fix_new_exp (frag_now
,
287 thisfrag
- frag_now
->fr_literal
,
295 fix_new_exp (frag_now
,
296 thisfrag
- frag_now
->fr_literal
+ 2,
304 fix_new_exp (frag_now
,
305 thisfrag
- frag_now
->fr_literal
,
313 as_fatal ("Unknown relocation type");
319 calcop (format
, param
, insn
)
320 struct m88k_opcode
*format
;
322 struct m88k_insn
*insn
;
324 char *fmt
= format
->op_spec
;
328 int reg_prefix
= 'r';
330 insn
->opcode
= format
->opcode
;
341 insn
->opcode
|= opcode
;
350 param
= get_reg (param
, &val
, reg_prefix
);
360 param
= get_reg (param
, &val
, reg_prefix
);
366 param
= get_reg (param
, &val
, reg_prefix
);
372 param
= get_reg (param
, &val
, 'r');
373 opcode
|= (val
<< 16) | val
;
377 param
= get_imm16 (param
, insn
);
381 param
= get_bf (param
, &val
);
386 param
= get_pcr (param
, insn
, RELOC_PC16
);
390 param
= get_pcr (param
, insn
, RELOC_PC26
);
394 param
= get_cmp (param
, &val
);
399 param
= get_cnd (param
, &val
);
404 param
= get_cr (param
, &val
);
409 param
= get_fcr (param
, &val
);
414 param
= get_vec9 (param
, &val
);
419 /* Having this here repeats the warning somtimes.
420 But can't we stand that? */
421 as_warn ("Use of obsolete instruction");
428 match_name (param
, assoc_tab
, valp
)
430 struct field_val_assoc
*assoc_tab
;
439 name
= assoc_tab
[i
].name
;
442 name_len
= strlen (name
);
443 if (!strncmp (param
, name
, name_len
))
445 *valp
= assoc_tab
[i
].val
;
446 return param
+ name_len
;
452 get_reg (param
, regnop
, reg_prefix
)
463 regno
= *param
++ - '0';
474 regno
= regno
* 10 + c
;
489 else if (c
== 's' && param
[0] == 'p')
499 get_imm16 (param
, insn
)
501 struct m88k_insn
*insn
;
503 enum reloc_type reloc
= NO_RELOC
;
507 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
512 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
517 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
523 save_ptr
= input_line_pointer
;
524 input_line_pointer
= param
;
525 expression (&insn
->exp
);
526 param
= input_line_pointer
;
527 input_line_pointer
= save_ptr
;
529 val
= insn
->exp
.X_add_number
;
531 if (insn
->exp
.X_op
== O_constant
)
533 /* Insert the value now, and reset reloc to NO_RELOC. */
534 if (reloc
== NO_RELOC
)
536 /* Warn about too big expressions if not surrounded by xx16. */
538 as_warn ("Expression truncated to 16 bits");
541 if (reloc
== RELOC_HI16
)
544 insn
->opcode
|= val
& 0xffff;
547 else if (reloc
== NO_RELOC
)
548 /* We accept a symbol even without lo16, hi16, etc, and assume
549 lo16 was intended. */
558 get_pcr (param
, insn
, reloc
)
560 struct m88k_insn
*insn
;
561 enum reloc_type reloc
;
563 char *saveptr
, *saveparam
;
565 saveptr
= input_line_pointer
;
566 input_line_pointer
= param
;
568 expression (&insn
->exp
);
570 saveparam
= input_line_pointer
;
571 input_line_pointer
= saveptr
;
573 /* Botch: We should relocate now if O_constant. */
580 get_cmp (param
, valp
)
589 param
= match_name (param
, cmpslot
, valp
);
596 save_ptr
= input_line_pointer
;
597 input_line_pointer
= param
;
598 val
= get_absolute_expression ();
599 param
= input_line_pointer
;
600 input_line_pointer
= save_ptr
;
604 as_warn ("Expression truncated to 5 bits");
614 get_cnd (param
, valp
)
620 if (isdigit (*param
))
622 param
= getval (param
, &val
);
626 as_warn ("Expression truncated to 5 bits");
632 if (isupper (*param
))
633 *param
= tolower (*param
);
635 if (isupper (param
[1]))
636 param
[1] = tolower (param
[1]);
638 param
= match_name (param
, cndmsk
, valp
);
667 else if (c
== bc
&& depth
<= 0)
674 get_bf_offset_expression (param
, offsetp
)
680 if (isalpha (param
[0]))
682 if (isupper (param
[0]))
683 param
[0] = tolower (param
[0]);
684 if (isupper (param
[1]))
685 param
[1] = tolower (param
[1]);
687 param
= match_name (param
, cmpslot
, offsetp
);
693 input_line_pointer
= param
;
694 offset
= get_absolute_expression ();
695 param
= input_line_pointer
;
712 xp
= get_bf2 (param
, '<');
714 save_ptr
= input_line_pointer
;
715 input_line_pointer
= param
;
718 /* We did not find '<'. We have an offset (width implicitly 32). */
719 param
= get_bf_offset_expression (param
, &offset
);
720 input_line_pointer
= save_ptr
;
726 *xp
++ = 0; /* Overwrite the '<' */
727 param
= get_bf2 (xp
, '>');
730 *param
++ = 0; /* Overwrite the '>' */
732 width
= get_absolute_expression ();
733 xp
= get_bf_offset_expression (xp
, &offset
);
734 input_line_pointer
= save_ptr
;
740 *valp
= ((width
% 32) << 5) | (offset
% 32);
746 get_cr (param
, regnop
)
755 if (!strncmp (param
, "cr", 2))
759 regno
= *param
++ - '0';
770 regno
= regno
* 10 + c
;
786 param
= match_name (param
, cr_regs
, regnop
);
792 get_fcr (param
, regnop
)
801 if (!strncmp (param
, "fcr", 3))
805 regno
= *param
++ - '0';
816 regno
= regno
* 10 + c
;
832 param
= match_name (param
, fcr_regs
, regnop
);
838 get_vec9 (param
, valp
)
845 save_ptr
= input_line_pointer
;
846 input_line_pointer
= param
;
847 val
= get_absolute_expression ();
848 param
= input_line_pointer
;
849 input_line_pointer
= save_ptr
;
852 as_warn ("Expression truncated to 9 bits");
854 *valp
= val
% (1 << 9);
860 (isdigit (z) ? (z) - '0' : \
861 islower (z) ? (z) - 'a' + 10 : \
862 isupper (z) ? (z) - 'A' + 10 : -1)
869 unsigned int val
= 0;
876 if (c
== 'x' || c
== 'X')
912 md_number_to_chars (buf
, val
, nbytes
)
935 /* This routine is never called. What is it for?
936 Ian Taylor, Cygnus Support 13 Jul 1993 */
939 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
946 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
965 switch (fixP
->fx_r_type
)
990 buf
[0] |= (val
>> 26) & 0x03;
1004 as_fatal ("Bad relocation type");
1012 md_number_to_disp (buf
, val
, nbytes
)
1017 as_fatal ("md_number_to_disp not defined");
1018 md_number_to_chars (buf
, val
, nbytes
);
1022 md_number_to_field (buf
, val
, nbytes
)
1027 as_fatal ("md_number_to_field not defined");
1028 md_number_to_chars (buf
, val
, nbytes
);
1031 #define MAX_LITTLENUMS 6
1033 /* Turn a string in input_line_pointer into a floating point constant of type
1034 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1035 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1038 md_atof (type
, litP
, sizeP
)
1044 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1045 LITTLENUM_TYPE
*wordP
;
1077 return "Bad call to MD_ATOF()";
1079 t
= atof_ieee (input_line_pointer
, type
, words
);
1081 input_line_pointer
= t
;
1083 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1084 for (wordP
= words
; prec
--;)
1086 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1087 litP
+= sizeof (LITTLENUM_TYPE
);
1089 return ""; /* Someone should teach Dean about null pointers */
1092 int md_short_jump_size
= 4;
1095 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1097 addressT from_addr
, to_addr
;
1101 ptr
[0] = (char) 0xc0;
1106 ptr
- frag
->fr_literal
,
1111 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1114 int md_long_jump_size
= 4;
1117 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1119 addressT from_addr
, to_addr
;
1123 ptr
[0] = (char) 0xc0;
1128 ptr
- frag
->fr_literal
,
1137 md_estimate_size_before_relax (fragP
, segment_type
)
1141 as_fatal ("Relaxation should never occur");
1144 const relax_typeS md_relax_table
[] =
1154 /* As far as I can tell, this routine is never called. What is it
1156 Ian Taylor, Cygnus Support 13 Jul 1993 */
1160 * Risc relocations are completely different, so it needs
1161 * this machine dependent routine to emit them.
1164 emit_relocations (fixP
, segment_address_in_file
)
1166 relax_addressT segment_address_in_file
;
1168 struct reloc_info_m88k ri
;
1170 extern char *next_object_file_charP
;
1172 bzero ((char *) &ri
, sizeof (ri
));
1173 for (; fixP
; fixP
= fixP
->fx_next
)
1175 if (fixP
->fx_r_type
>= NO_RELOC
)
1177 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1181 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1183 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1184 fixP
->fx_where
- segment_address_in_file
;
1185 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1188 ri
.r_symbolnum
= symbolP
->sy_number
;
1193 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1195 if (symbolP
&& symbolP
->sy_frag
)
1197 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1199 ri
.r_type
= fixP
->fx_r_type
;
1202 ri
.r_addend
-= ri
.r_address
;
1206 ri
.r_addend
= fixP
->fx_addnumber
;
1209 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1219 /* This routine can be subsumed by s_lcomm in read.c.
1220 Ian Taylor, Cygnus Support 13 Jul 1993 */
1229 int temp
, bss_align
;
1232 name
= input_line_pointer
;
1233 c
= get_symbol_end ();
1234 p
= input_line_pointer
;
1237 if (*input_line_pointer
!= ',')
1239 as_warn ("Expected comma after name");
1240 ignore_rest_of_line ();
1243 input_line_pointer
++;
1244 if ((temp
= get_absolute_expression ()) < 0)
1246 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1247 ignore_rest_of_line ();
1251 symbolP
= symbol_find_or_make (name
);
1253 if (*input_line_pointer
== ',')
1255 input_line_pointer
++;
1256 bss_align
= get_absolute_expression ();
1261 if (!S_IS_DEFINED(symbolP
)
1262 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1267 segT current_seg
= now_seg
;
1268 subsegT current_subseg
= now_subseg
;
1270 subseg_new (SEG_BSS
, 1); /* switch to bss */
1273 frag_align (bss_align
, 0);
1275 /* detach from old frag */
1276 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1277 symbolP
->sy_frag
->fr_symbol
= NULL
;
1279 symbolP
->sy_frag
= frag_now
;
1280 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1283 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1285 subseg_new (current_seg
, current_subseg
);
1290 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1293 while (!is_end_of_line
[*input_line_pointer
])
1295 input_line_pointer
++;
1305 /* These functions are needed if we are linking with obj-coffbfd.c.
1306 That file may be replaced by a more BFD oriented version at some
1307 point. If that happens, these functions should be rexamined.
1309 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1311 /* Given a fixS structure (created by a call to fix_new, above),
1312 return the BFD relocation type to use for it. */
1315 tc_coff_fix2rtype (fixp
)
1318 switch (fixp
->fx_r_type
)
1337 /* Apply a fixS to the object file. Since COFF does not use addends
1338 in relocs, the addend is actually stored directly in the object
1342 md_apply_fix (fixp
, val
)
1348 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1349 fixp
->fx_offset
= 0;
1351 switch (fixp
->fx_r_type
)
1354 fixp
->fx_offset
= val
>> 16;
1360 fixp
->fx_offset
= val
>> 16;
1366 fixp
->fx_offset
= val
>> 16;
1377 buf
[0] |= (val
>> 26) & 0x03;
1395 /* Where a PC relative offset is calculated from. On the m88k they
1396 are calculated from just after the instruction. */
1399 md_pcrel_from (fixp
)
1402 switch (fixp
->fx_r_type
)
1405 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1407 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1414 #endif /* M88KCOFF */