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 */
125 struct field_val_assoc cndmsk
[] =
139 unsigned long opcode
;
141 enum reloc_type reloc
;
145 static struct hash_control
*op_hash
= NULL
;
147 /* These bits should be turned off in the first address of every segment */
148 int md_seg_align
= 7;
150 /* This is the number to put at the beginning of the a.out file */
151 long omagic
= OMAGIC
;
153 /* These chars start a comment anywhere in a source file (except inside
155 const char comment_chars
[] = ";";
157 /* These chars only start a comment at the beginning of a line. */
158 const char line_comment_chars
[] = "#";
160 const char line_separator_chars
[] = "";
162 /* Chars that can be used to separate mant from exp in floating point nums */
163 const char EXP_CHARS
[] = "eE";
165 /* Chars that mean this number is a floating point constant */
166 /* as in 0f123.456 */
167 /* or 0H1.234E-12 (see exp chars above) */
168 const char FLT_CHARS
[] = "dDfF";
170 extern void float_cons (), cons (), s_globl (), s_line (), s_space (),
171 s_set (), stringer (), s_lcomm ();
172 static void s_file ();
173 static void s_bss ();
175 const pseudo_typeS md_pseudo_table
[] =
178 {"dfloat", float_cons
, 'd'},
179 {"ffloat", float_cons
, 'f'},
180 {"global", s_globl
, 0},
184 {"string", stringer
, 0},
186 {"zero", s_space
, 0},
196 /* initialize hash table */
198 op_hash
= hash_new ();
200 as_fatal ("Could not initialize hash table");
202 /* loop until you see the end of the list */
204 while (*m88k_opcodes
[i
].name
)
206 char *name
= m88k_opcodes
[i
].name
;
208 /* hash each mnemonic and record its position */
210 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
212 if (retval
!= NULL
&& *retval
!= '\0')
213 as_fatal ("Can't hash instruction '%s':%s",
214 m88k_opcodes
[i
].name
, retval
);
216 /* skip to next unique mnemonic or end of list */
218 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
224 md_parse_option (argP
, cntP
, vecP
)
229 as_warn ("unknown option: -%s", *argP
);
236 char *param
, *thisfrag
;
237 struct m88k_opcode
*format
;
238 struct m88k_insn insn
;
242 /* skip over instruction to find parameters */
244 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
249 /* try to find the instruction in the hash table */
251 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
253 as_fatal ("Invalid mnemonic '%s'", op
);
257 /* try parsing this instruction into insn */
259 insn
.exp
.X_add_symbol
= 0;
260 insn
.exp
.X_subtract_symbol
= 0;
261 insn
.exp
.X_add_number
= 0;
263 insn
.reloc
= NO_RELOC
;
265 while (!calcop (format
, param
, &insn
))
267 /* if it doesn't parse try the next instruction */
269 if (!strcmp (format
[0].name
, format
[1].name
))
273 as_fatal ("Parameter syntax error");
278 /* grow the current frag and plop in the opcode */
280 thisfrag
= frag_more (4);
281 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
283 /* if this instruction requires labels mark it for later */
293 thisfrag
- frag_now
->fr_literal
+ 2,
295 insn
.exp
.X_add_symbol
,
296 insn
.exp
.X_subtract_symbol
,
297 insn
.exp
.X_add_number
,
304 thisfrag
- frag_now
->fr_literal
,
306 insn
.exp
.X_add_symbol
,
307 insn
.exp
.X_subtract_symbol
,
308 insn
.exp
.X_add_number
,
315 thisfrag
- frag_now
->fr_literal
+ 2,
317 insn
.exp
.X_add_symbol
,
318 insn
.exp
.X_subtract_symbol
,
319 insn
.exp
.X_add_number
,
326 thisfrag
- frag_now
->fr_literal
,
328 insn
.exp
.X_add_symbol
,
329 insn
.exp
.X_subtract_symbol
,
330 insn
.exp
.X_add_number
,
336 as_fatal ("Unknown relocation type");
342 calcop (format
, param
, insn
)
343 struct m88k_opcode
*format
;
345 struct m88k_insn
*insn
;
347 char *fmt
= format
->op_spec
;
352 insn
->opcode
= format
->opcode
;
363 insn
->opcode
|= opcode
;
372 param
= get_reg (param
, &val
);
377 param
= get_reg (param
, &val
);
382 param
= get_reg (param
, &val
);
387 param
= get_reg (param
, &val
);
388 opcode
|= (val
<< 16) | val
;
392 param
= get_imm16 (param
, insn
);
396 param
= get_bf (param
, &val
);
401 param
= get_pcr (param
, insn
, RELOC_PC16
);
405 param
= get_pcr (param
, insn
, RELOC_PC26
);
409 param
= get_cmp (param
, &val
);
414 param
= get_cnd (param
, &val
);
419 param
= get_cr (param
, &val
);
424 param
= get_fcr (param
, &val
);
429 param
= get_vec9 (param
, &val
);
434 /* Having this here repeats the warning somtimes.
435 But can't we stand that? */
436 as_warn ("Use of obsolete instruction");
443 match_name (param
, assoc_tab
, valp
)
445 struct field_val_assoc
*assoc_tab
;
454 name
= assoc_tab
[i
].name
;
457 name_len
= strlen (name
);
458 if (!strncmp (param
, name
, name_len
))
460 *valp
= assoc_tab
[i
].val
;
461 return param
+ name_len
;
467 get_reg (param
, regnop
)
477 regno
= *param
++ - '0';
488 regno
= regno
* 10 + c
;
503 else if (c
== 's' && param
[0] == 'p')
513 get_imm16 (param
, insn
)
515 struct m88k_insn
*insn
;
517 enum reloc_type reloc
= NO_RELOC
;
522 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
527 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
532 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
538 save_ptr
= input_line_pointer
;
539 input_line_pointer
= param
;
540 seg
= expression (&insn
->exp
);
541 param
= input_line_pointer
;
542 input_line_pointer
= save_ptr
;
544 val
= insn
->exp
.X_add_number
;
546 if (seg
== SEG_ABSOLUTE
)
548 /* Insert the value now, and reset reloc to NO_RELOC. */
549 if (reloc
== NO_RELOC
)
551 /* Warn about too big expressions if not surrounded by xx16. */
553 as_warn ("Expression truncated to 16 bits");
556 if (reloc
== RELOC_HI16
)
559 insn
->opcode
|= val
& 0xffff;
562 else if (reloc
== NO_RELOC
)
563 /* We accept a symbol even without lo16, hi16, etc, and assume
564 lo16 was intended. */
573 get_pcr (param
, insn
, reloc
)
575 struct m88k_insn
*insn
;
576 enum reloc_type reloc
;
578 char *saveptr
, *saveparam
;
581 saveptr
= input_line_pointer
;
582 input_line_pointer
= param
;
584 seg
= expression (&insn
->exp
);
586 saveparam
= input_line_pointer
;
587 input_line_pointer
= saveptr
;
589 /* Botch: We should relocate now if SEG_ABSOLUTE. */
596 get_cmp (param
, valp
)
605 param
= match_name (param
, cmpslot
, valp
);
612 save_ptr
= input_line_pointer
;
613 input_line_pointer
= param
;
614 val
= get_absolute_expression ();
615 param
= input_line_pointer
;
616 input_line_pointer
= save_ptr
;
620 as_warn ("Expression truncated to 5 bits");
630 get_cnd (param
, valp
)
636 if (isdigit (*param
))
638 param
= getval (param
, &val
);
642 as_warn ("Expression truncated to 5 bits");
648 if (isupper (*param
))
649 *param
= tolower (*param
);
651 if (isupper (param
[1]))
652 param
[1] = tolower (param
[1]);
654 param
= match_name (param
, cndmsk
, valp
);
683 else if (c
== bc
&& depth
<= 0)
690 get_bf_offset_expression (param
, offsetp
)
696 if (isalpha (param
[0]))
698 if (isupper (param
[0]))
699 param
[0] = tolower (param
[0]);
700 if (isupper (param
[1]))
701 param
[1] = tolower (param
[1]);
703 param
= match_name (param
, cmpslot
, offsetp
);
709 input_line_pointer
= param
;
710 offset
= get_absolute_expression ();
711 param
= input_line_pointer
;
728 xp
= get_bf2 (param
, '<');
730 save_ptr
= input_line_pointer
;
731 input_line_pointer
= param
;
734 /* We did not find '<'. We have an offset (width implicitly 32). */
735 param
= get_bf_offset_expression (param
, &offset
);
738 input_line_pointer
= save_ptr
;
742 *xp
++ = 0; /* Overwrite the '<' */
743 param
= get_bf2 (xp
, '>');
746 *param
++ = 0; /* Overwrite the '>' */
748 width
= get_absolute_expression ();
749 xp
= get_bf_offset_expression (xp
, &offset
);
750 input_line_pointer
= save_ptr
;
756 *valp
= ((width
% 32) << 5) | (offset
% 32);
762 get_cr (param
, regnop
)
771 if (!strncmp (param
, "cr", 2))
775 regno
= *param
++ - '0';
786 regno
= regno
* 10 + c
;
802 param
= match_name (param
, cr_regs
, regnop
);
808 get_fcr (param
, regnop
)
817 if (!strncmp (param
, "fcr", 3))
821 regno
= *param
++ - '0';
832 regno
= regno
* 10 + c
;
848 param
= match_name (param
, fcr_regs
, regnop
);
854 get_vec9 (param
, valp
)
861 save_ptr
= input_line_pointer
;
862 input_line_pointer
= param
;
863 val
= get_absolute_expression ();
864 param
= input_line_pointer
;
865 input_line_pointer
= save_ptr
;
868 as_warn ("Expression truncated to 9 bits");
870 *valp
= val
% (1 << 9);
876 (isdigit (z) ? (z) - '0' : \
877 islower (z) ? (z) - 'a' + 10 : \
878 isupper (z) ? (z) - 'A' + 10 : -1)
885 unsigned int val
= 0;
892 if (c
== 'x' || c
== 'X')
928 md_number_to_chars (buf
, val
, nbytes
)
950 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
957 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
976 switch (fixP
->fx_r_type
)
1001 buf
[0] |= (val
>> 26) & 0x03;
1015 as_fatal ("Bad relocation type");
1021 md_number_to_disp (buf
, val
, nbytes
)
1026 as_fatal ("md_number_to_disp not defined");
1027 md_number_to_chars (buf
, val
, nbytes
);
1031 md_number_to_field (buf
, val
, nbytes
)
1036 as_fatal ("md_number_to_field not defined");
1037 md_number_to_chars (buf
, val
, nbytes
);
1040 #define MAX_LITTLENUMS 6
1042 /* Turn a string in input_line_pointer into a floating point constant of type
1043 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1044 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1047 md_atof (type
, litP
, sizeP
)
1053 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1054 LITTLENUM_TYPE
*wordP
;
1086 return "Bad call to MD_ATOF()";
1088 t
= atof_ieee (input_line_pointer
, type
, words
);
1090 input_line_pointer
= t
;
1092 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1093 for (wordP
= words
; prec
--;)
1095 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1096 litP
+= sizeof (LITTLENUM_TYPE
);
1098 return ""; /* Someone should teach Dean about null pointers */
1101 int md_short_jump_size
= 4;
1104 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1106 long from_addr
, to_addr
;
1115 ptr
- frag
->fr_literal
,
1121 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1124 int md_long_jump_size
= 4;
1127 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1129 long from_addr
, to_addr
;
1138 ptr
- frag
->fr_literal
,
1148 md_estimate_size_before_relax (fragP
, segment_type
)
1152 as_fatal ("Relaxation should never occur");
1155 const relax_typeS md_relax_table
[] =
1159 md_convert_frag (fragP
)
1162 as_fatal ("Relaxation should never occur");
1171 * Risc relocations are completely different, so it needs
1172 * this machine dependent routine to emit them.
1175 emit_relocations (fixP
, segment_address_in_file
)
1177 relax_addressT segment_address_in_file
;
1179 struct reloc_info_m88k ri
;
1181 extern char *next_object_file_charP
;
1183 bzero ((char *) &ri
, sizeof (ri
));
1184 for (; fixP
; fixP
= fixP
->fx_next
)
1187 if (fixP
->fx_r_type
>= NO_RELOC
)
1189 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1193 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1195 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1196 fixP
->fx_where
- segment_address_in_file
;
1197 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1200 ri
.r_symbolnum
= symbolP
->sy_number
;
1205 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1207 if (symbolP
&& symbolP
->sy_frag
)
1209 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1211 ri
.r_type
= fixP
->fx_r_type
;
1214 /* ri.r_addend -= fixP->fx_where; */
1215 ri
.r_addend
-= ri
.r_address
;
1219 ri
.r_addend
= fixP
->fx_addnumber
;
1222 /* md_ri_to_chars ((char *) &ri, ri); */
1223 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1235 int temp
, bss_align
= 1;
1237 extern const char is_end_of_line
[256];
1239 name
= input_line_pointer
;
1240 c
= get_symbol_end ();
1241 p
= input_line_pointer
;
1244 if (*input_line_pointer
!= ',')
1246 as_warn ("Expected comma after name");
1247 ignore_rest_of_line ();
1250 input_line_pointer
++;
1251 if ((temp
= get_absolute_expression ()) < 0)
1253 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1254 ignore_rest_of_line ();
1258 symbolP
= symbol_find_or_make (name
);
1260 if (*input_line_pointer
== ',')
1262 input_line_pointer
++;
1263 bss_align
= get_absolute_expression ();
1264 while (local_bss_counter
% bss_align
!= 0)
1265 local_bss_counter
++;
1268 if (symbolP
->sy_other
== 0
1269 && symbolP
->sy_desc
== 0
1270 && ((symbolP
->sy_type
== N_BSS
1271 && symbolP
->sy_value
== local_bss_counter
)
1272 || ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
1273 && symbolP
->sy_value
== 0)))
1275 symbolP
->sy_value
= local_bss_counter
;
1276 symbolP
->sy_type
= N_BSS
;
1277 symbolP
->sy_frag
= &bss_address_frag
;
1278 local_bss_counter
+= temp
;
1282 as_warn ("Ignoring attempt to re-define symbol from %d. to %d.",
1283 symbolP
->sy_value
, local_bss_counter
);
1285 while (!is_end_of_line
[*input_line_pointer
])
1287 input_line_pointer
++;