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"
40 struct field_val_assoc
46 struct field_val_assoc cr_regs
[] =
73 struct field_val_assoc fcr_regs
[] =
91 struct field_val_assoc cmpslot
[] =
93 /* Integer Floating point */
114 struct field_val_assoc cndmsk
[] =
128 unsigned long opcode
;
130 enum reloc_type reloc
;
134 static struct hash_control
*op_hash
= NULL
;
136 /* These bits should be turned off in the first address of every segment */
137 int md_seg_align
= 7;
139 /* This is the number to put at the beginning of the a.out file */
140 long omagic
= OMAGIC
;
142 /* These chars start a comment anywhere in a source file (except inside
144 const char comment_chars
[] = ";";
146 /* These chars only start a comment at the beginning of a line. */
147 const char line_comment_chars
[] = "#";
149 const char line_separator_chars
[] = "";
151 /* Chars that can be used to separate mant from exp in floating point nums */
152 const char EXP_CHARS
[] = "eE";
154 /* Chars that mean this number is a floating point constant */
155 /* as in 0f123.456 */
156 /* or 0H1.234E-12 (see exp chars above) */
157 const char FLT_CHARS
[] = "dDfF";
159 extern void float_cons (), cons (), s_globl (), s_line (), s_space (),
160 s_set (), s_lcomm ();
161 static void s_file ();
162 static void s_bss ();
164 const pseudo_typeS md_pseudo_table
[] =
167 {"dfloat", float_cons
, 'd'},
168 {"ffloat", float_cons
, 'f'},
169 {"global", s_globl
, 0},
173 {"string", stringer
, 0},
175 {"zero", s_space
, 0},
185 /* initialize hash table */
187 op_hash
= hash_new ();
189 as_fatal ("Could not initialize hash table");
191 /* loop until you see the end of the list */
193 while (*m88k_opcodes
[i
].name
)
195 char *name
= m88k_opcodes
[i
].name
;
197 /* hash each mnemonic and record its position */
199 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
201 if (retval
!= NULL
&& *retval
!= '\0')
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 */
207 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
213 md_parse_option (argP
, cntP
, vecP
)
218 as_warn ("unknown option: -%s", *argP
);
225 char *param
, *thisfrag
;
226 struct m88k_opcode
*format
;
227 struct m88k_insn insn
;
231 /* skip over instruction to find parameters */
233 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
238 /* try to find the instruction in the hash table */
240 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
242 as_fatal ("Invalid mnemonic '%s'", op
);
246 /* try parsing this instruction into insn */
248 insn
.exp
.X_add_symbol
= 0;
249 insn
.exp
.X_subtract_symbol
= 0;
250 insn
.exp
.X_add_number
= 0;
252 insn
.reloc
= NO_RELOC
;
254 while (!calcop (format
, param
, &insn
))
256 /* if it doesn't parse try the next instruction */
258 if (!strcmp (format
[0].name
, format
[1].name
))
262 as_fatal ("Parameter syntax error");
267 /* grow the current frag and plop in the opcode */
269 thisfrag
= frag_more (4);
270 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
272 /* if this instruction requires labels mark it for later */
282 thisfrag
- frag_now
->fr_literal
+ 2,
284 insn
.exp
.X_add_symbol
,
285 insn
.exp
.X_subtract_symbol
,
286 insn
.exp
.X_add_number
,
293 thisfrag
- frag_now
->fr_literal
,
295 insn
.exp
.X_add_symbol
,
296 insn
.exp
.X_subtract_symbol
,
297 insn
.exp
.X_add_number
,
304 thisfrag
- frag_now
->fr_literal
+ 2,
306 insn
.exp
.X_add_symbol
,
307 insn
.exp
.X_subtract_symbol
,
308 insn
.exp
.X_add_number
,
315 thisfrag
- frag_now
->fr_literal
,
317 insn
.exp
.X_add_symbol
,
318 insn
.exp
.X_subtract_symbol
,
319 insn
.exp
.X_add_number
,
325 as_fatal ("Unknown relocation type");
331 calcop (format
, param
, insn
)
332 struct m88k_opcode
*format
;
334 struct m88k_insn
*insn
;
336 char *fmt
= format
->op_spec
;
341 insn
->opcode
= format
->opcode
;
352 insn
->opcode
|= opcode
;
361 param
= get_reg (param
, &val
);
366 param
= get_reg (param
, &val
);
371 param
= get_reg (param
, &val
);
376 param
= get_reg (param
, &val
);
377 opcode
|= (val
<< 16) | val
;
381 param
= get_imm16 (param
, insn
);
385 param
= get_bf (param
, &val
);
390 param
= get_pcr (param
, insn
, RELOC_PC16
);
394 param
= get_pcr (param
, insn
, RELOC_PC26
);
398 param
= get_cmp (param
, &val
);
403 param
= get_cnd (param
, &val
);
408 param
= get_cr (param
, &val
);
413 param
= get_fcr (param
, &val
);
418 param
= get_vec9 (param
, &val
);
423 /* Having this here repeats the warning somtimes.
424 But can't we stand that? */
425 as_warn ("Use of obsolete instruction");
432 match_name (param
, assoc_tab
, valp
)
434 struct field_val_assoc
*assoc_tab
;
443 name
= assoc_tab
[i
].name
;
446 name_len
= strlen (name
);
447 if (!strncmp (param
, name
, name_len
))
449 *valp
= assoc_tab
[i
].val
;
450 return param
+ name_len
;
456 get_reg (param
, regnop
)
466 regno
= *param
++ - '0';
477 regno
= regno
* 10 + c
;
492 else if (c
== 's' && param
[0] == 'p')
502 get_imm16 (param
, insn
)
504 struct m88k_insn
*insn
;
506 enum reloc_type reloc
= NO_RELOC
;
511 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
516 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
521 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
527 save_ptr
= input_line_pointer
;
528 input_line_pointer
= param
;
529 seg
= expression (&insn
->exp
);
530 param
= input_line_pointer
;
531 input_line_pointer
= save_ptr
;
533 val
= insn
->exp
.X_add_number
;
535 if (seg
== SEG_ABSOLUTE
)
537 /* Insert the value now, and reset reloc to NO_RELOC. */
538 if (reloc
== NO_RELOC
)
540 /* Warn about too big expressions if not surrounded by xx16. */
542 as_warn ("Expression truncated to 16 bits");
545 if (reloc
== RELOC_HI16
)
548 insn
->opcode
|= val
& 0xffff;
551 else if (reloc
== NO_RELOC
)
552 /* We accept a symbol even without lo16, hi16, etc, and assume
553 lo16 was intended. */
562 get_pcr (param
, insn
, reloc
)
564 struct m88k_insn
*insn
;
565 enum reloc_type reloc
;
567 char *saveptr
, *saveparam
;
570 saveptr
= input_line_pointer
;
571 input_line_pointer
= param
;
573 seg
= expression (&insn
->exp
);
575 saveparam
= input_line_pointer
;
576 input_line_pointer
= saveptr
;
578 /* Botch: We should relocate now if SEG_ABSOLUTE. */
585 get_cmp (param
, valp
)
594 param
= match_name (param
, cmpslot
, valp
);
601 save_ptr
= input_line_pointer
;
602 input_line_pointer
= param
;
603 val
= get_absolute_expression ();
604 param
= input_line_pointer
;
605 input_line_pointer
= save_ptr
;
609 as_warn ("Expression truncated to 5 bits");
619 get_cnd (param
, valp
)
625 if (isdigit (*param
))
627 param
= getval (param
, &val
);
631 as_warn ("Expression truncated to 5 bits");
637 if (isupper (*param
))
638 *param
= tolower (*param
);
640 if (isupper (param
[1]))
641 param
[1] = tolower (param
[1]);
643 param
= match_name (param
, cndmsk
, valp
);
672 else if (c
== bc
&& depth
<= 0)
679 get_bf_offset_expression (param
, offsetp
)
685 if (isalpha (param
[0]))
687 if (isupper (param
[0]))
688 param
[0] = tolower (param
[0]);
689 if (isupper (param
[1]))
690 param
[1] = tolower (param
[1]);
692 param
= match_name (param
, cmpslot
, offsetp
);
698 input_line_pointer
= param
;
699 offset
= get_absolute_expression ();
700 param
= input_line_pointer
;
717 xp
= get_bf2 (param
, '<');
719 save_ptr
= input_line_pointer
;
720 input_line_pointer
= param
;
723 /* We did not find '<'. We have an offset (width implicitly 32). */
724 param
= get_bf_offset_expression (param
, &offset
);
727 input_line_pointer
= save_ptr
;
731 *xp
++ = 0; /* Overwrite the '<' */
732 param
= get_bf2 (xp
, '>');
735 *param
++ = 0; /* Overwrite the '>' */
737 width
= get_absolute_expression ();
738 xp
= get_bf_offset_expression (xp
, &offset
);
739 input_line_pointer
= save_ptr
;
745 *valp
= ((width
% 32) << 5) | (offset
% 32);
751 get_cr (param
, regnop
)
760 if (!strncmp (param
, "cr", 2))
764 regno
= *param
++ - '0';
775 regno
= regno
* 10 + c
;
791 param
= match_name (param
, cr_regs
, regnop
);
797 get_fcr (param
, regnop
)
806 if (!strncmp (param
, "fcr", 3))
810 regno
= *param
++ - '0';
821 regno
= regno
* 10 + c
;
837 param
= match_name (param
, fcr_regs
, regnop
);
843 get_vec9 (param
, valp
)
850 save_ptr
= input_line_pointer
;
851 input_line_pointer
= param
;
852 val
= get_absolute_expression ();
853 param
= input_line_pointer
;
854 input_line_pointer
= save_ptr
;
857 as_warn ("Expression truncated to 9 bits");
859 *valp
= val
% (1 << 9);
865 (isdigit (z) ? (z) - '0' : \
866 islower (z) ? (z) - 'a' + 10 : \
867 isupper (z) ? (z) - 'A' + 10 : -1)
874 unsigned int val
= 0;
881 if (c
== 'x' || c
== 'X')
917 md_number_to_chars (buf
, val
, nbytes
)
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");
1010 md_number_to_disp (buf
, val
, nbytes
)
1015 as_fatal ("md_number_to_disp not defined");
1016 md_number_to_chars (buf
, val
, nbytes
);
1020 md_number_to_field (buf
, val
, nbytes
)
1025 as_fatal ("md_number_to_field not defined");
1026 md_number_to_chars (buf
, val
, nbytes
);
1029 #define MAX_LITTLENUMS 6
1031 /* Turn a string in input_line_pointer into a floating point constant of type
1032 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1033 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1036 md_atof (type
, litP
, sizeP
)
1042 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1043 LITTLENUM_TYPE
*wordP
;
1075 return "Bad call to MD_ATOF()";
1077 t
= atof_ieee (input_line_pointer
, type
, words
);
1079 input_line_pointer
= t
;
1081 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1082 for (wordP
= words
; prec
--;)
1084 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1085 litP
+= sizeof (LITTLENUM_TYPE
);
1087 return ""; /* Someone should teach Dean about null pointers */
1090 int md_short_jump_size
= 4;
1093 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1095 long from_addr
, to_addr
;
1104 ptr
- frag
->fr_literal
,
1110 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1113 int md_long_jump_size
= 4;
1116 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1118 long from_addr
, to_addr
;
1127 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
[] =
1148 md_convert_frag (fragP
)
1151 as_fatal ("Relaxation should never occur");
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
)
1176 if (fixP
->fx_r_type
>= NO_RELOC
)
1178 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1182 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1184 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1185 fixP
->fx_where
- segment_address_in_file
;
1186 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1189 ri
.r_symbolnum
= symbolP
->sy_number
;
1194 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1196 if (symbolP
&& symbolP
->sy_frag
)
1198 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1200 ri
.r_type
= fixP
->fx_r_type
;
1203 /* ri.r_addend -= fixP->fx_where; */
1204 ri
.r_addend
-= ri
.r_address
;
1208 ri
.r_addend
= fixP
->fx_addnumber
;
1211 /* md_ri_to_chars ((char *) &ri, ri); */
1212 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1224 int temp
, bss_align
;
1226 extern const char is_end_of_line
[256];
1228 name
= input_line_pointer
;
1229 c
= get_symbol_end ();
1230 p
= input_line_pointer
;
1233 if (*input_line_pointer
!= ',')
1235 as_warn ("Expected comma after name");
1236 ignore_rest_of_line ();
1239 input_line_pointer
++;
1240 if ((temp
= get_absolute_expression ()) < 0)
1242 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1243 ignore_rest_of_line ();
1247 symbolP
= symbol_find_or_make (name
);
1249 if (*input_line_pointer
== ',')
1251 input_line_pointer
++;
1252 bss_align
= get_absolute_expression ();
1257 if (!S_IS_DEFINED(symbolP
)
1258 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1263 segT current_seg
= now_seg
;
1264 subsegT current_subseg
= now_subseg
;
1266 subseg_new (SEG_BSS
, 1); /* switch to bss */
1269 frag_align (bss_align
, 0);
1271 /* detach from old frag */
1272 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1273 symbolP
->sy_frag
->fr_symbol
= NULL
;
1275 symbolP
->sy_frag
= frag_now
;
1276 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1279 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1281 subseg_new (current_seg
, current_subseg
);
1286 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1289 while (!is_end_of_line
[*input_line_pointer
])
1291 input_line_pointer
++;