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 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. */
23 #include "m88k-opcode.h"
26 #include "struc-symbol.h"
36 #include "struc-symbol.h"
51 struct field_val_assoc
57 struct field_val_assoc cr_regs
[] =
84 struct field_val_assoc fcr_regs
[] =
102 struct field_val_assoc cmpslot
[] =
104 /* Integer Floating point */
113 {"hi", 8}, {"ou", 8},
114 {"ls", 9}, {"ib", 9},
115 {"lo", 10}, {"in", 10},
116 {"hs", 11}, {"ob", 11},
121 struct field_val_assoc cndmsk
[] =
135 unsigned long opcode
;
137 enum reloc_type reloc
;
141 static struct hash_control
*op_hash
= NULL
;
143 /* These bits should be turned off in the first address of every segment */
144 int md_seg_align
= 7;
146 /* This is the number to put at the beginning of the a.out file */
147 long omagic
= OMAGIC
;
149 /* These chars start a comment anywhere in a source file (except inside
151 char comment_chars
[] = ";";
153 /* These chars only start a comment at the beginning of a line. */
154 char line_comment_chars
[] = "#";
156 /* Chars that can be used to separate mant from exp in floating point nums */
157 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 char FLT_CHARS
[] = "dDfF";
164 extern void float_cons (), cons (), s_globl (), s_line (),
165 s_space (), s_set (), stringer (), s_lcomm ();
166 static void s_file ();
167 static void s_bss ();
169 const pseudo_typeS md_pseudo_table
[] =
172 {"dfloat", float_cons
, 'd'},
173 {"ffloat", float_cons
, 'f'},
174 {"global", s_globl
, 0},
178 {"string", stringer
, 0},
180 {"zero", s_space
, 0},
190 /* initialize hash table */
192 op_hash
= hash_new ();
194 as_fatal ("Could not initialize hash table");
196 /* loop until you see the end of the list */
198 while (*m88k_opcodes
[i
].name
)
200 char *name
= m88k_opcodes
[i
].name
;
202 /* hash each mnemonic and record its position */
204 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
206 if (retval
!= NULL
&& *retval
!= '\0')
207 as_fatal ("Can't hash instruction '%s':%s",
208 m88k_opcodes
[i
].name
, retval
);
210 /* skip to next unique mnemonic or end of list */
212 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
218 md_parse_option (argP
, cntP
, vecP
)
223 as_warn ("unknown option: -%s", *argP
);
230 char *param
, *thisfrag
;
231 struct m88k_opcode
*format
;
232 struct m88k_insn insn
;
236 /* skip over instruction to find parameters */
238 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
243 /* try to find the instruction in the hash table */
245 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
247 as_fatal ("Invalid mnemonic '%s'", op
);
251 /* try parsing this instruction into insn */
253 insn
.exp
.X_add_symbol
= 0;
254 insn
.exp
.X_subtract_symbol
= 0;
255 insn
.exp
.X_add_number
= 0;
257 insn
.reloc
= NO_RELOC
;
259 while (!calcop (format
, param
, &insn
))
261 /* if it doesn't parse try the next instruction */
263 if (!strcmp (format
[0].name
, format
[1].name
))
267 as_fatal ("Parameter syntax error");
272 /* grow the current frag and plop in the opcode */
274 thisfrag
= frag_more (4);
275 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
277 /* if this instruction requires labels mark it for later */
287 thisfrag
- frag_now
->fr_literal
+ 2,
289 insn
.exp
.X_add_symbol
,
290 insn
.exp
.X_subtract_symbol
,
291 insn
.exp
.X_add_number
,
298 thisfrag
- frag_now
->fr_literal
,
300 insn
.exp
.X_add_symbol
,
301 insn
.exp
.X_subtract_symbol
,
302 insn
.exp
.X_add_number
,
309 thisfrag
- frag_now
->fr_literal
+ 2,
311 insn
.exp
.X_add_symbol
,
312 insn
.exp
.X_subtract_symbol
,
313 insn
.exp
.X_add_number
,
320 thisfrag
- frag_now
->fr_literal
,
322 insn
.exp
.X_add_symbol
,
323 insn
.exp
.X_subtract_symbol
,
324 insn
.exp
.X_add_number
,
330 as_fatal ("Unknown relocation type");
336 calcop (format
, param
, insn
)
337 struct m88k_opcode
*format
;
339 struct m88k_insn
*insn
;
341 char *fmt
= format
->op_spec
;
346 insn
->opcode
= format
->opcode
;
357 insn
->opcode
|= opcode
;
366 param
= get_reg (param
, &val
);
371 param
= get_reg (param
, &val
);
376 param
= get_reg (param
, &val
);
381 param
= get_reg (param
, &val
);
382 opcode
|= (val
<< 16) | val
;
386 param
= get_imm16 (param
, insn
);
390 param
= get_bf (param
, &val
);
395 param
= get_pcr (param
, insn
, RELOC_PC16
);
399 param
= get_pcr (param
, insn
, RELOC_PC26
);
403 param
= get_cmp (param
, &val
);
408 param
= get_cnd (param
, &val
);
413 param
= get_cr (param
, &val
);
418 param
= get_fcr (param
, &val
);
423 param
= get_vec9 (param
, &val
);
428 /* Having this here repeats the warning somtimes.
429 But can't we stand that? */
430 as_warn ("Use of obsolete instruction");
437 match_name (param
, assoc_tab
, valp
)
439 struct field_val_assoc
*assoc_tab
;
448 name
= assoc_tab
[i
].name
;
451 name_len
= strlen (name
);
452 if (!strncmp (param
, name
, name_len
))
454 *valp
= assoc_tab
[i
].val
;
455 return param
+ name_len
;
461 get_reg (param
, regnop
)
471 regno
= *param
++ - '0';
482 regno
= regno
* 10 + c
;
497 else if (c
== 's' && param
[0] == 'p')
507 get_imm16 (param
, insn
)
509 struct m88k_insn
*insn
;
511 enum reloc_type reloc
= NO_RELOC
;
516 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
521 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
526 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
532 save_ptr
= input_line_pointer
;
533 input_line_pointer
= param
;
534 seg
= expression (&insn
->exp
);
535 param
= input_line_pointer
;
536 input_line_pointer
= save_ptr
;
538 val
= insn
->exp
.X_add_number
;
540 if (seg
== SEG_ABSOLUTE
)
542 /* Insert the value now, and reset reloc to NO_RELOC. */
543 if (reloc
== NO_RELOC
)
545 /* Warn about too big expressions if not surrounded by xx16. */
547 as_warn ("Expression truncated to 16 bits");
550 if (reloc
== RELOC_HI16
)
553 insn
->opcode
|= val
& 0xffff;
556 else if (reloc
== NO_RELOC
)
557 /* We accept a symbol even without lo16, hi16, etc, and assume
558 lo16 was intended. */
567 get_pcr (param
, insn
, reloc
)
569 struct m88k_insn
*insn
;
570 enum reloc_type reloc
;
572 char *saveptr
, *saveparam
;
575 saveptr
= input_line_pointer
;
576 input_line_pointer
= param
;
578 seg
= expression (&insn
->exp
);
580 saveparam
= input_line_pointer
;
581 input_line_pointer
= saveptr
;
583 /* Botch: We should relocate now if SEG_ABSOLUTE. */
590 get_cmp (param
, valp
)
599 param
= match_name (param
, cmpslot
, valp
);
606 save_ptr
= input_line_pointer
;
607 input_line_pointer
= param
;
608 val
= get_absolute_expression ();
609 param
= input_line_pointer
;
610 input_line_pointer
= save_ptr
;
614 as_warn ("Expression truncated to 5 bits");
624 get_cnd (param
, valp
)
630 if (isdigit (*param
))
632 param
= getval (param
, &val
);
636 as_warn ("Expression truncated to 5 bits");
642 if (isupper (*param
))
643 *param
= tolower (*param
);
645 if (isupper (param
[1]))
646 param
[1] = tolower (param
[1]);
648 param
= match_name (param
, cndmsk
, valp
);
677 else if (c
== bc
&& depth
<= 0)
684 get_bf_offset_expression (param
, offsetp
)
690 if (isalpha (param
[0]))
692 if (isupper (param
[0]))
693 param
[0] = tolower (param
[0]);
694 if (isupper (param
[1]))
695 param
[1] = tolower (param
[1]);
697 param
= match_name (param
, cmpslot
, offsetp
);
703 input_line_pointer
= param
;
704 offset
= get_absolute_expression ();
705 param
= input_line_pointer
;
722 xp
= get_bf2 (param
, '<');
724 save_ptr
= input_line_pointer
;
725 input_line_pointer
= param
;
728 /* We did not find '<'. We have an offset (width implicitly 32). */
729 param
= get_bf_offset_expression (param
, &offset
);
732 input_line_pointer
= save_ptr
;
736 *xp
++ = 0; /* Overwrite the '<' */
737 param
= get_bf2 (xp
, '>');
740 *param
++ = 0; /* Overwrite the '>' */
742 width
= get_absolute_expression ();
743 xp
= get_bf_offset_expression (xp
, &offset
);
744 input_line_pointer
= save_ptr
;
750 *valp
= ((width
% 32) << 5) | (offset
% 32);
756 get_cr (param
, regnop
)
765 if (!strncmp (param
, "cr", 2))
769 regno
= *param
++ - '0';
780 regno
= regno
* 10 + c
;
796 param
= match_name (param
, cr_regs
, regnop
);
802 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);
870 (isdigit (z) ? (z) - '0' : \
871 islower (z) ? (z) - 'a' + 10 : \
872 isupper (z) ? (z) - 'A' + 10 : -1)
879 unsigned int val
= 0;
886 if (c
== 'x' || c
== 'X')
922 md_number_to_chars (buf
, val
, nbytes
)
944 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
951 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
970 switch (fixP
->fx_r_type
)
995 buf
[0] |= (val
>> 26) & 0x03;
1009 as_fatal ("Bad relocation type");
1015 md_number_to_disp (buf
, val
, nbytes
)
1020 as_fatal ("md_number_to_disp not defined");
1021 md_number_to_chars (buf
, val
, nbytes
);
1025 md_number_to_field (buf
, val
, nbytes
)
1030 as_fatal ("md_number_to_field not defined");
1031 md_number_to_chars (buf
, val
, nbytes
);
1034 #define MAX_LITTLENUMS 6
1036 /* Turn a string in input_line_pointer into a floating point constant of type
1037 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1038 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1041 md_atof (type
, litP
, sizeP
)
1047 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1048 LITTLENUM_TYPE
*wordP
;
1080 return "Bad call to MD_ATOF()";
1082 t
=atof_ieee (input_line_pointer
, type
, words
);
1084 input_line_pointer
=t
;
1086 *sizeP
=prec
* sizeof (LITTLENUM_TYPE
);
1087 for (wordP
=words
;prec
--;)
1089 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1090 litP
+=sizeof (LITTLENUM_TYPE
);
1092 return ""; /* Someone should teach Dean about null pointers */
1095 int md_short_jump_size
= 4;
1098 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1100 long from_addr
, to_addr
;
1104 ptr
[0] = 0xc0; ptr
[1] = 0x00; ptr
[2] = 0x00; ptr
[3] = 0x00;
1106 ptr
- frag
->fr_literal
,
1112 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1115 int md_long_jump_size
= 4;
1118 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1120 long from_addr
, to_addr
;
1124 ptr
[0] = 0xc0; ptr
[1] = 0x00; ptr
[2] = 0x00; ptr
[3] = 0x00;
1126 ptr
- frag
->fr_literal
,
1136 md_estimate_size_before_relax (fragP
, segment_type
)
1140 as_fatal ("Relaxation should never occur");
1143 const relax_typeS md_relax_table
[] = {0};
1146 md_convert_frag (fragP
)
1149 as_fatal ("Relaxation should never occur");
1158 * Risc relocations are completely different, so it needs
1159 * this machine dependent routine to emit them.
1162 emit_relocations (fixP
, segment_address_in_file
)
1164 relax_addressT segment_address_in_file
;
1166 struct reloc_info_m88k ri
;
1168 extern char *next_object_file_charP
;
1170 bzero ((char *) &ri
, sizeof (ri
));
1171 for (; fixP
; fixP
= fixP
->fx_next
) {
1173 if (fixP
->fx_r_type
>= NO_RELOC
) {
1174 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1178 if ((symbolP
= fixP
->fx_addsy
) != NULL
) {
1179 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1180 fixP
->fx_where
- segment_address_in_file
;
1181 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
) {
1183 ri
.r_symbolnum
= symbolP
->sy_number
;
1186 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1188 if (symbolP
&& symbolP
->sy_frag
) {
1189 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1191 ri
.r_type
= fixP
->fx_r_type
;
1192 if (fixP
->fx_pcrel
) {
1193 /* ri.r_addend -= fixP->fx_where; */
1194 ri
.r_addend
-= ri
.r_address
;
1196 ri
.r_addend
= fixP
->fx_addnumber
;
1199 /* md_ri_to_chars ((char *) &ri, ri); */
1200 append (&next_object_file_charP
, (char *)& ri
, sizeof (ri
));
1212 int temp
, bss_align
= 1;
1214 extern const char is_end_of_line
[256];
1216 name
= input_line_pointer
;
1217 c
= get_symbol_end();
1218 p
= input_line_pointer
;
1221 if ( * input_line_pointer
!= ',' )
1223 as_warn("Expected comma after name");
1224 ignore_rest_of_line();
1227 input_line_pointer
++;
1228 if ((temp
= get_absolute_expression()) < 0)
1230 as_warn("BSS length (%d.) <0! Ignored.", temp
);
1231 ignore_rest_of_line();
1235 symbolP
= symbol_find_or_make(name
);
1237 if (*input_line_pointer
== ',')
1239 input_line_pointer
++;
1240 bss_align
= get_absolute_expression();
1241 while (local_bss_counter
% bss_align
!= 0)
1242 local_bss_counter
++;
1245 if (symbolP
->sy_other
== 0
1246 && symbolP
->sy_desc
== 0
1247 && ((symbolP
->sy_type
== N_BSS
1248 && symbolP
->sy_value
== local_bss_counter
)
1249 || ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
1250 && symbolP
->sy_value
== 0)))
1252 symbolP
->sy_value
= local_bss_counter
;
1253 symbolP
->sy_type
= N_BSS
;
1254 symbolP
->sy_frag
= & bss_address_frag
;
1255 local_bss_counter
+= temp
;
1259 as_warn( "Ignoring attempt to re-define symbol from %d. to %d.",
1260 symbolP
->sy_value
, local_bss_counter
);
1262 while (!is_end_of_line
[*input_line_pointer
])
1264 input_line_pointer
++;