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 const char comment_chars
[] = ";";
153 /* These chars only start a comment at the beginning of a line. */
154 const char line_comment_chars
[] = "#";
156 const char line_separator_chars
[] = "";
158 /* Chars that can be used to separate mant from exp in floating point nums */
159 const char EXP_CHARS
[] = "eE";
161 /* Chars that mean this number is a floating point constant */
162 /* as in 0f123.456 */
163 /* or 0H1.234E-12 (see exp chars above) */
164 const char FLT_CHARS
[] = "dDfF";
166 extern void float_cons (), cons (), s_globl (), s_line (),
167 s_space (), s_set (), stringer (), s_lcomm ();
168 static void s_file ();
169 static void s_bss ();
171 const pseudo_typeS md_pseudo_table
[] =
174 {"dfloat", float_cons
, 'd'},
175 {"ffloat", float_cons
, 'f'},
176 {"global", s_globl
, 0},
180 {"string", stringer
, 0},
182 {"zero", s_space
, 0},
192 /* initialize hash table */
194 op_hash
= hash_new ();
196 as_fatal ("Could not initialize hash table");
198 /* loop until you see the end of the list */
200 while (*m88k_opcodes
[i
].name
)
202 char *name
= m88k_opcodes
[i
].name
;
204 /* hash each mnemonic and record its position */
206 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
208 if (retval
!= NULL
&& *retval
!= '\0')
209 as_fatal ("Can't hash instruction '%s':%s",
210 m88k_opcodes
[i
].name
, retval
);
212 /* skip to next unique mnemonic or end of list */
214 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
220 md_parse_option (argP
, cntP
, vecP
)
225 as_warn ("unknown option: -%s", *argP
);
232 char *param
, *thisfrag
;
233 struct m88k_opcode
*format
;
234 struct m88k_insn insn
;
238 /* skip over instruction to find parameters */
240 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
245 /* try to find the instruction in the hash table */
247 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
249 as_fatal ("Invalid mnemonic '%s'", op
);
253 /* try parsing this instruction into insn */
255 insn
.exp
.X_add_symbol
= 0;
256 insn
.exp
.X_subtract_symbol
= 0;
257 insn
.exp
.X_add_number
= 0;
259 insn
.reloc
= NO_RELOC
;
261 while (!calcop (format
, param
, &insn
))
263 /* if it doesn't parse try the next instruction */
265 if (!strcmp (format
[0].name
, format
[1].name
))
269 as_fatal ("Parameter syntax error");
274 /* grow the current frag and plop in the opcode */
276 thisfrag
= frag_more (4);
277 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
279 /* if this instruction requires labels mark it for later */
289 thisfrag
- frag_now
->fr_literal
+ 2,
291 insn
.exp
.X_add_symbol
,
292 insn
.exp
.X_subtract_symbol
,
293 insn
.exp
.X_add_number
,
300 thisfrag
- frag_now
->fr_literal
,
302 insn
.exp
.X_add_symbol
,
303 insn
.exp
.X_subtract_symbol
,
304 insn
.exp
.X_add_number
,
311 thisfrag
- frag_now
->fr_literal
+ 2,
313 insn
.exp
.X_add_symbol
,
314 insn
.exp
.X_subtract_symbol
,
315 insn
.exp
.X_add_number
,
322 thisfrag
- frag_now
->fr_literal
,
324 insn
.exp
.X_add_symbol
,
325 insn
.exp
.X_subtract_symbol
,
326 insn
.exp
.X_add_number
,
332 as_fatal ("Unknown relocation type");
338 calcop (format
, param
, insn
)
339 struct m88k_opcode
*format
;
341 struct m88k_insn
*insn
;
343 char *fmt
= format
->op_spec
;
348 insn
->opcode
= format
->opcode
;
359 insn
->opcode
|= opcode
;
368 param
= get_reg (param
, &val
);
373 param
= get_reg (param
, &val
);
378 param
= get_reg (param
, &val
);
383 param
= get_reg (param
, &val
);
384 opcode
|= (val
<< 16) | val
;
388 param
= get_imm16 (param
, insn
);
392 param
= get_bf (param
, &val
);
397 param
= get_pcr (param
, insn
, RELOC_PC16
);
401 param
= get_pcr (param
, insn
, RELOC_PC26
);
405 param
= get_cmp (param
, &val
);
410 param
= get_cnd (param
, &val
);
415 param
= get_cr (param
, &val
);
420 param
= get_fcr (param
, &val
);
425 param
= get_vec9 (param
, &val
);
430 /* Having this here repeats the warning somtimes.
431 But can't we stand that? */
432 as_warn ("Use of obsolete instruction");
439 match_name (param
, assoc_tab
, valp
)
441 struct field_val_assoc
*assoc_tab
;
450 name
= assoc_tab
[i
].name
;
453 name_len
= strlen (name
);
454 if (!strncmp (param
, name
, name_len
))
456 *valp
= assoc_tab
[i
].val
;
457 return param
+ name_len
;
463 get_reg (param
, regnop
)
473 regno
= *param
++ - '0';
484 regno
= regno
* 10 + c
;
499 else if (c
== 's' && param
[0] == 'p')
509 get_imm16 (param
, insn
)
511 struct m88k_insn
*insn
;
513 enum reloc_type reloc
= NO_RELOC
;
518 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
523 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
528 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
534 save_ptr
= input_line_pointer
;
535 input_line_pointer
= param
;
536 seg
= expression (&insn
->exp
);
537 param
= input_line_pointer
;
538 input_line_pointer
= save_ptr
;
540 val
= insn
->exp
.X_add_number
;
542 if (seg
== SEG_ABSOLUTE
)
544 /* Insert the value now, and reset reloc to NO_RELOC. */
545 if (reloc
== NO_RELOC
)
547 /* Warn about too big expressions if not surrounded by xx16. */
549 as_warn ("Expression truncated to 16 bits");
552 if (reloc
== RELOC_HI16
)
555 insn
->opcode
|= val
& 0xffff;
558 else if (reloc
== NO_RELOC
)
559 /* We accept a symbol even without lo16, hi16, etc, and assume
560 lo16 was intended. */
569 get_pcr (param
, insn
, reloc
)
571 struct m88k_insn
*insn
;
572 enum reloc_type reloc
;
574 char *saveptr
, *saveparam
;
577 saveptr
= input_line_pointer
;
578 input_line_pointer
= param
;
580 seg
= expression (&insn
->exp
);
582 saveparam
= input_line_pointer
;
583 input_line_pointer
= saveptr
;
585 /* Botch: We should relocate now if SEG_ABSOLUTE. */
592 get_cmp (param
, valp
)
601 param
= match_name (param
, cmpslot
, valp
);
608 save_ptr
= input_line_pointer
;
609 input_line_pointer
= param
;
610 val
= get_absolute_expression ();
611 param
= input_line_pointer
;
612 input_line_pointer
= save_ptr
;
616 as_warn ("Expression truncated to 5 bits");
626 get_cnd (param
, valp
)
632 if (isdigit (*param
))
634 param
= getval (param
, &val
);
638 as_warn ("Expression truncated to 5 bits");
644 if (isupper (*param
))
645 *param
= tolower (*param
);
647 if (isupper (param
[1]))
648 param
[1] = tolower (param
[1]);
650 param
= match_name (param
, cndmsk
, valp
);
679 else if (c
== bc
&& depth
<= 0)
686 get_bf_offset_expression (param
, offsetp
)
692 if (isalpha (param
[0]))
694 if (isupper (param
[0]))
695 param
[0] = tolower (param
[0]);
696 if (isupper (param
[1]))
697 param
[1] = tolower (param
[1]);
699 param
= match_name (param
, cmpslot
, offsetp
);
705 input_line_pointer
= param
;
706 offset
= get_absolute_expression ();
707 param
= input_line_pointer
;
724 xp
= get_bf2 (param
, '<');
726 save_ptr
= input_line_pointer
;
727 input_line_pointer
= param
;
730 /* We did not find '<'. We have an offset (width implicitly 32). */
731 param
= get_bf_offset_expression (param
, &offset
);
734 input_line_pointer
= save_ptr
;
738 *xp
++ = 0; /* Overwrite the '<' */
739 param
= get_bf2 (xp
, '>');
742 *param
++ = 0; /* Overwrite the '>' */
744 width
= get_absolute_expression ();
745 xp
= get_bf_offset_expression (xp
, &offset
);
746 input_line_pointer
= save_ptr
;
752 *valp
= ((width
% 32) << 5) | (offset
% 32);
758 get_cr (param
, regnop
)
767 if (!strncmp (param
, "cr", 2))
771 regno
= *param
++ - '0';
782 regno
= regno
* 10 + c
;
798 param
= match_name (param
, cr_regs
, regnop
);
804 get_fcr (param
, regnop
)
813 if (!strncmp (param
, "fcr", 3))
817 regno
= *param
++ - '0';
828 regno
= regno
* 10 + c
;
844 param
= match_name (param
, fcr_regs
, regnop
);
850 get_vec9 (param
, valp
)
857 save_ptr
= input_line_pointer
;
858 input_line_pointer
= param
;
859 val
= get_absolute_expression ();
860 param
= input_line_pointer
;
861 input_line_pointer
= save_ptr
;
864 as_warn ("Expression truncated to 9 bits");
866 *valp
= val
% (1 << 9);
872 (isdigit (z) ? (z) - '0' : \
873 islower (z) ? (z) - 'a' + 10 : \
874 isupper (z) ? (z) - 'A' + 10 : -1)
881 unsigned int val
= 0;
888 if (c
== 'x' || c
== 'X')
924 md_number_to_chars (buf
, val
, nbytes
)
946 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
953 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
972 switch (fixP
->fx_r_type
)
997 buf
[0] |= (val
>> 26) & 0x03;
1011 as_fatal ("Bad relocation type");
1017 md_number_to_disp (buf
, val
, nbytes
)
1022 as_fatal ("md_number_to_disp not defined");
1023 md_number_to_chars (buf
, val
, nbytes
);
1027 md_number_to_field (buf
, val
, nbytes
)
1032 as_fatal ("md_number_to_field not defined");
1033 md_number_to_chars (buf
, val
, nbytes
);
1036 #define MAX_LITTLENUMS 6
1038 /* Turn a string in input_line_pointer into a floating point constant of type
1039 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1040 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1043 md_atof (type
, litP
, sizeP
)
1049 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1050 LITTLENUM_TYPE
*wordP
;
1082 return "Bad call to MD_ATOF()";
1084 t
=atof_ieee (input_line_pointer
, type
, words
);
1086 input_line_pointer
=t
;
1088 *sizeP
=prec
* sizeof (LITTLENUM_TYPE
);
1089 for (wordP
=words
;prec
--;)
1091 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1092 litP
+=sizeof (LITTLENUM_TYPE
);
1094 return ""; /* Someone should teach Dean about null pointers */
1097 int md_short_jump_size
= 4;
1100 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1102 long from_addr
, to_addr
;
1106 ptr
[0] = 0xc0; ptr
[1] = 0x00; ptr
[2] = 0x00; ptr
[3] = 0x00;
1108 ptr
- frag
->fr_literal
,
1114 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1117 int md_long_jump_size
= 4;
1120 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1122 long from_addr
, to_addr
;
1126 ptr
[0] = 0xc0; ptr
[1] = 0x00; ptr
[2] = 0x00; ptr
[3] = 0x00;
1128 ptr
- frag
->fr_literal
,
1138 md_estimate_size_before_relax (fragP
, segment_type
)
1142 as_fatal ("Relaxation should never occur");
1145 const relax_typeS md_relax_table
[] = {0};
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
) {
1175 if (fixP
->fx_r_type
>= NO_RELOC
) {
1176 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1180 if ((symbolP
= fixP
->fx_addsy
) != NULL
) {
1181 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1182 fixP
->fx_where
- segment_address_in_file
;
1183 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
) {
1185 ri
.r_symbolnum
= symbolP
->sy_number
;
1188 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1190 if (symbolP
&& symbolP
->sy_frag
) {
1191 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1193 ri
.r_type
= fixP
->fx_r_type
;
1194 if (fixP
->fx_pcrel
) {
1195 /* ri.r_addend -= fixP->fx_where; */
1196 ri
.r_addend
-= ri
.r_address
;
1198 ri
.r_addend
= fixP
->fx_addnumber
;
1201 /* md_ri_to_chars ((char *) &ri, ri); */
1202 append (&next_object_file_charP
, (char *)& ri
, sizeof (ri
));
1214 int temp
, bss_align
= 1;
1216 extern const char is_end_of_line
[256];
1218 name
= input_line_pointer
;
1219 c
= get_symbol_end();
1220 p
= input_line_pointer
;
1223 if ( * input_line_pointer
!= ',' )
1225 as_warn("Expected comma after name");
1226 ignore_rest_of_line();
1229 input_line_pointer
++;
1230 if ((temp
= get_absolute_expression()) < 0)
1232 as_warn("BSS length (%d.) <0! Ignored.", temp
);
1233 ignore_rest_of_line();
1237 symbolP
= symbol_find_or_make(name
);
1239 if (*input_line_pointer
== ',')
1241 input_line_pointer
++;
1242 bss_align
= get_absolute_expression();
1243 while (local_bss_counter
% bss_align
!= 0)
1244 local_bss_counter
++;
1247 if (symbolP
->sy_other
== 0
1248 && symbolP
->sy_desc
== 0
1249 && ((symbolP
->sy_type
== N_BSS
1250 && symbolP
->sy_value
== local_bss_counter
)
1251 || ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
1252 && symbolP
->sy_value
== 0)))
1254 symbolP
->sy_value
= local_bss_counter
;
1255 symbolP
->sy_type
= N_BSS
;
1256 symbolP
->sy_frag
= & bss_address_frag
;
1257 local_bss_counter
+= temp
;
1261 as_warn( "Ignoring attempt to re-define symbol from %d. to %d.",
1262 symbolP
->sy_value
, local_bss_counter
);
1264 while (!is_end_of_line
[*input_line_pointer
])
1266 input_line_pointer
++;