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. */
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 /* This is the number to put at the beginning of the a.out file */
139 long omagic
= OMAGIC
;
141 /* These chars start a comment anywhere in a source file (except inside
143 const char comment_chars
[] = ";";
145 /* These chars only start a comment at the beginning of a line. */
146 const char line_comment_chars
[] = "#";
148 const char line_separator_chars
[] = "";
150 /* Chars that can be used to separate mant from exp in floating point nums */
151 const char EXP_CHARS
[] = "eE";
153 /* Chars that mean this number is a floating point constant */
154 /* as in 0f123.456 */
155 /* or 0H1.234E-12 (see exp chars above) */
156 const char FLT_CHARS
[] = "dDfF";
158 extern void float_cons (), cons (), s_globl (), s_line (), s_space (),
159 s_set (), s_lcomm ();
160 static void s_file ();
161 static void s_bss ();
163 const pseudo_typeS md_pseudo_table
[] =
166 {"dfloat", float_cons
, 'd'},
167 {"ffloat", float_cons
, 'f'},
168 {"global", s_globl
, 0},
172 {"string", stringer
, 0},
174 {"zero", s_space
, 0},
184 /* initialize hash table */
186 op_hash
= hash_new ();
188 as_fatal ("Could not initialize hash table");
190 /* loop until you see the end of the list */
192 while (*m88k_opcodes
[i
].name
)
194 char *name
= m88k_opcodes
[i
].name
;
196 /* hash each mnemonic and record its position */
198 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
200 if (retval
!= NULL
&& *retval
!= '\0')
201 as_fatal ("Can't hash instruction '%s':%s",
202 m88k_opcodes
[i
].name
, retval
);
204 /* skip to next unique mnemonic or end of list */
206 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
212 md_parse_option (argP
, cntP
, vecP
)
224 char *param
, *thisfrag
;
225 struct m88k_opcode
*format
;
226 struct m88k_insn insn
;
230 /* skip over instruction to find parameters */
232 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
237 /* try to find the instruction in the hash table */
239 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
241 as_fatal ("Invalid mnemonic '%s'", op
);
245 /* try parsing this instruction into insn */
247 insn
.exp
.X_add_symbol
= 0;
248 insn
.exp
.X_subtract_symbol
= 0;
249 insn
.exp
.X_add_number
= 0;
251 insn
.reloc
= NO_RELOC
;
253 while (!calcop (format
, param
, &insn
))
255 /* if it doesn't parse try the next instruction */
257 if (!strcmp (format
[0].name
, format
[1].name
))
261 as_fatal ("Parameter syntax error");
266 /* grow the current frag and plop in the opcode */
268 thisfrag
= frag_more (4);
269 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
271 /* if this instruction requires labels mark it for later */
281 thisfrag
- frag_now
->fr_literal
+ 2,
283 insn
.exp
.X_add_symbol
,
284 insn
.exp
.X_subtract_symbol
,
285 insn
.exp
.X_add_number
,
292 thisfrag
- frag_now
->fr_literal
,
294 insn
.exp
.X_add_symbol
,
295 insn
.exp
.X_subtract_symbol
,
296 insn
.exp
.X_add_number
,
303 thisfrag
- frag_now
->fr_literal
+ 2,
305 insn
.exp
.X_add_symbol
,
306 insn
.exp
.X_subtract_symbol
,
307 insn
.exp
.X_add_number
,
314 thisfrag
- frag_now
->fr_literal
,
316 insn
.exp
.X_add_symbol
,
317 insn
.exp
.X_subtract_symbol
,
318 insn
.exp
.X_add_number
,
324 as_fatal ("Unknown relocation type");
330 calcop (format
, param
, insn
)
331 struct m88k_opcode
*format
;
333 struct m88k_insn
*insn
;
335 char *fmt
= format
->op_spec
;
339 int reg_prefix
= 'r';
341 insn
->opcode
= format
->opcode
;
352 insn
->opcode
|= opcode
;
361 param
= get_reg (param
, &val
, reg_prefix
);
371 param
= get_reg (param
, &val
, reg_prefix
);
377 param
= get_reg (param
, &val
, reg_prefix
);
383 param
= get_reg (param
, &val
, 'r');
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
, reg_prefix
)
474 regno
= *param
++ - '0';
485 regno
= regno
* 10 + c
;
500 else if (c
== 's' && param
[0] == 'p')
510 get_imm16 (param
, insn
)
512 struct m88k_insn
*insn
;
514 enum reloc_type reloc
= NO_RELOC
;
519 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
524 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
529 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
535 save_ptr
= input_line_pointer
;
536 input_line_pointer
= param
;
537 seg
= expression (&insn
->exp
);
538 param
= input_line_pointer
;
539 input_line_pointer
= save_ptr
;
541 val
= insn
->exp
.X_add_number
;
543 if (seg
== SEG_ABSOLUTE
)
545 /* Insert the value now, and reset reloc to NO_RELOC. */
546 if (reloc
== NO_RELOC
)
548 /* Warn about too big expressions if not surrounded by xx16. */
550 as_warn ("Expression truncated to 16 bits");
553 if (reloc
== RELOC_HI16
)
556 insn
->opcode
|= val
& 0xffff;
559 else if (reloc
== NO_RELOC
)
560 /* We accept a symbol even without lo16, hi16, etc, and assume
561 lo16 was intended. */
570 get_pcr (param
, insn
, reloc
)
572 struct m88k_insn
*insn
;
573 enum reloc_type reloc
;
575 char *saveptr
, *saveparam
;
578 saveptr
= input_line_pointer
;
579 input_line_pointer
= param
;
581 seg
= expression (&insn
->exp
);
583 saveparam
= input_line_pointer
;
584 input_line_pointer
= saveptr
;
586 /* Botch: We should relocate now if SEG_ABSOLUTE. */
593 get_cmp (param
, valp
)
602 param
= match_name (param
, cmpslot
, valp
);
609 save_ptr
= input_line_pointer
;
610 input_line_pointer
= param
;
611 val
= get_absolute_expression ();
612 param
= input_line_pointer
;
613 input_line_pointer
= save_ptr
;
617 as_warn ("Expression truncated to 5 bits");
627 get_cnd (param
, valp
)
633 if (isdigit (*param
))
635 param
= getval (param
, &val
);
639 as_warn ("Expression truncated to 5 bits");
645 if (isupper (*param
))
646 *param
= tolower (*param
);
648 if (isupper (param
[1]))
649 param
[1] = tolower (param
[1]);
651 param
= match_name (param
, cndmsk
, valp
);
680 else if (c
== bc
&& depth
<= 0)
687 get_bf_offset_expression (param
, offsetp
)
693 if (isalpha (param
[0]))
695 if (isupper (param
[0]))
696 param
[0] = tolower (param
[0]);
697 if (isupper (param
[1]))
698 param
[1] = tolower (param
[1]);
700 param
= match_name (param
, cmpslot
, offsetp
);
706 input_line_pointer
= param
;
707 offset
= get_absolute_expression ();
708 param
= input_line_pointer
;
725 xp
= get_bf2 (param
, '<');
727 save_ptr
= input_line_pointer
;
728 input_line_pointer
= param
;
731 /* We did not find '<'. We have an offset (width implicitly 32). */
732 param
= get_bf_offset_expression (param
, &offset
);
733 input_line_pointer
= save_ptr
;
739 *xp
++ = 0; /* Overwrite the '<' */
740 param
= get_bf2 (xp
, '>');
743 *param
++ = 0; /* Overwrite the '>' */
745 width
= get_absolute_expression ();
746 xp
= get_bf_offset_expression (xp
, &offset
);
747 input_line_pointer
= save_ptr
;
753 *valp
= ((width
% 32) << 5) | (offset
% 32);
759 get_cr (param
, regnop
)
768 if (!strncmp (param
, "cr", 2))
772 regno
= *param
++ - '0';
783 regno
= regno
* 10 + c
;
799 param
= match_name (param
, cr_regs
, regnop
);
805 get_fcr (param
, regnop
)
814 if (!strncmp (param
, "fcr", 3))
818 regno
= *param
++ - '0';
829 regno
= regno
* 10 + c
;
845 param
= match_name (param
, fcr_regs
, regnop
);
851 get_vec9 (param
, valp
)
858 save_ptr
= input_line_pointer
;
859 input_line_pointer
= param
;
860 val
= get_absolute_expression ();
861 param
= input_line_pointer
;
862 input_line_pointer
= save_ptr
;
865 as_warn ("Expression truncated to 9 bits");
867 *valp
= val
% (1 << 9);
873 (isdigit (z) ? (z) - '0' : \
874 islower (z) ? (z) - 'a' + 10 : \
875 isupper (z) ? (z) - 'A' + 10 : -1)
882 unsigned int val
= 0;
889 if (c
== 'x' || c
== 'X')
925 md_number_to_chars (buf
, val
, nbytes
)
947 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
954 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
973 switch (fixP
->fx_r_type
)
998 buf
[0] |= (val
>> 26) & 0x03;
1012 as_fatal ("Bad relocation type");
1018 md_number_to_disp (buf
, val
, nbytes
)
1023 as_fatal ("md_number_to_disp not defined");
1024 md_number_to_chars (buf
, val
, nbytes
);
1028 md_number_to_field (buf
, val
, nbytes
)
1033 as_fatal ("md_number_to_field not defined");
1034 md_number_to_chars (buf
, val
, nbytes
);
1037 #define MAX_LITTLENUMS 6
1039 /* Turn a string in input_line_pointer into a floating point constant of type
1040 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1041 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1044 md_atof (type
, litP
, sizeP
)
1050 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1051 LITTLENUM_TYPE
*wordP
;
1083 return "Bad call to MD_ATOF()";
1085 t
= atof_ieee (input_line_pointer
, type
, words
);
1087 input_line_pointer
= t
;
1089 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1090 for (wordP
= words
; prec
--;)
1092 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1093 litP
+= sizeof (LITTLENUM_TYPE
);
1095 return ""; /* Someone should teach Dean about null pointers */
1098 int md_short_jump_size
= 4;
1101 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1103 addressT from_addr
, to_addr
;
1107 ptr
[0] = (char) 0xc0;
1112 ptr
- frag
->fr_literal
,
1118 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1121 int md_long_jump_size
= 4;
1124 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1126 addressT from_addr
, to_addr
;
1130 ptr
[0] = (char) 0xc0;
1135 ptr
- frag
->fr_literal
,
1145 md_estimate_size_before_relax (fragP
, segment_type
)
1149 as_fatal ("Relaxation should never occur");
1152 const relax_typeS md_relax_table
[] =
1161 * Risc relocations are completely different, so it needs
1162 * this machine dependent routine to emit them.
1165 emit_relocations (fixP
, segment_address_in_file
)
1167 relax_addressT segment_address_in_file
;
1169 struct reloc_info_m88k ri
;
1171 extern char *next_object_file_charP
;
1173 bzero ((char *) &ri
, sizeof (ri
));
1174 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
-= ri
.r_address
;
1207 ri
.r_addend
= fixP
->fx_addnumber
;
1210 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1222 int temp
, bss_align
;
1225 name
= input_line_pointer
;
1226 c
= get_symbol_end ();
1227 p
= input_line_pointer
;
1230 if (*input_line_pointer
!= ',')
1232 as_warn ("Expected comma after name");
1233 ignore_rest_of_line ();
1236 input_line_pointer
++;
1237 if ((temp
= get_absolute_expression ()) < 0)
1239 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1240 ignore_rest_of_line ();
1244 symbolP
= symbol_find_or_make (name
);
1246 if (*input_line_pointer
== ',')
1248 input_line_pointer
++;
1249 bss_align
= get_absolute_expression ();
1254 if (!S_IS_DEFINED(symbolP
)
1255 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1260 segT current_seg
= now_seg
;
1261 subsegT current_subseg
= now_subseg
;
1263 subseg_new (SEG_BSS
, 1); /* switch to bss */
1266 frag_align (bss_align
, 0);
1268 /* detach from old frag */
1269 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1270 symbolP
->sy_frag
->fr_symbol
= NULL
;
1272 symbolP
->sy_frag
= frag_now
;
1273 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1276 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1278 subseg_new (current_seg
, current_subseg
);
1283 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1286 while (!is_end_of_line
[*input_line_pointer
])
1288 input_line_pointer
++;