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, 1993 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "m88k-opcode.h"
27 struct field_val_assoc
33 struct field_val_assoc cr_regs
[] =
60 struct field_val_assoc fcr_regs
[] =
78 struct field_val_assoc cmpslot
[] =
80 /* Integer Floating point */
91 {"lo", 10}, {"in", 10},
92 {"hs", 11}, {"ob", 11},
93 {"be", 12}, {"ue", 12},
94 {"nb", 13}, {"lg", 13},
95 {"he", 14}, {"ug", 14},
96 {"nh", 15}, {"ule", 15},
103 struct field_val_assoc cndmsk
[] =
117 unsigned long opcode
;
119 enum reloc_type reloc
;
122 static char *get_bf
PARAMS ((char *param
, unsigned *valp
));
123 static char *get_cmp
PARAMS ((char *param
, unsigned *valp
));
124 static char *get_cnd
PARAMS ((char *param
, unsigned *valp
));
125 static char *get_cr
PARAMS ((char *param
, unsigned *regnop
));
126 static char *get_fcr
PARAMS ((char *param
, unsigned *regnop
));
127 static char *get_imm16
PARAMS ((char *param
, struct m88k_insn
*insn
));
128 static char *get_o6
PARAMS ((char *param
, unsigned *valp
));
129 static char *get_reg
PARAMS ((char *param
, unsigned *regnop
, int reg_prefix
));
130 static char *get_vec9
PARAMS ((char *param
, unsigned *valp
));
131 static char *getval
PARAMS ((char *param
, unsigned int *valp
));
133 static char *get_pcr
PARAMS ((char *param
, struct m88k_insn
*insn
,
134 enum reloc_type reloc
));
136 static int calcop
PARAMS ((struct m88k_opcode
*format
,
137 char *param
, struct m88k_insn
*insn
));
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 /* These chars start a comment anywhere in a source file (except inside
148 const char comment_chars
[] = ";";
150 /* These chars only start a comment at the beginning of a line. */
151 const char line_comment_chars
[] = "#";
153 const char line_separator_chars
[] = "";
155 /* Chars that can be used to separate mant from exp in floating point nums */
156 const char EXP_CHARS
[] = "eE";
158 /* Chars that mean this number is a floating point constant */
159 /* as in 0f123.456 */
160 /* or 0H1.234E-12 (see exp chars above) */
161 const char FLT_CHARS
[] = "dDfF";
163 extern void float_cons (), cons (), s_globl (), s_space (),
164 s_set (), s_lcomm ();
166 const pseudo_typeS md_pseudo_table
[] =
168 {"align", s_align_bytes
, 4},
170 {"dfloat", float_cons
, 'd'},
171 {"ffloat", float_cons
, 'f'},
172 {"global", s_globl
, 0},
175 {"string", stringer
, 0},
177 /* Force set to be treated as an instruction. */
186 const char *retval
= NULL
;
189 /* initialize hash table */
191 op_hash
= hash_new ();
193 /* loop until you see the end of the list */
195 while (*m88k_opcodes
[i
].name
)
197 char *name
= m88k_opcodes
[i
].name
;
199 /* hash each mnemonic and record its position */
201 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
204 as_fatal ("Can't hash instruction '%s':%s",
205 m88k_opcodes
[i
].name
, retval
);
207 /* skip to next unique mnemonic or end of list */
209 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
214 CONST
char *md_shortopts
= "";
215 struct option md_longopts
[] = {
216 {NULL
, no_argument
, NULL
, 0}
218 size_t md_longopts_size
= sizeof(md_longopts
);
221 md_parse_option (c
, arg
)
229 md_show_usage (stream
)
238 char *param
, *thisfrag
;
240 struct m88k_opcode
*format
;
241 struct m88k_insn insn
;
245 /* skip over instruction to find parameters */
247 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
252 /* try to find the instruction in the hash table */
254 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
256 as_bad ("Invalid mnemonic '%s'", op
);
260 /* try parsing this instruction into insn */
262 insn
.exp
.X_add_symbol
= 0;
263 insn
.exp
.X_op_symbol
= 0;
264 insn
.exp
.X_add_number
= 0;
265 insn
.exp
.X_op
= O_illegal
;
266 insn
.reloc
= NO_RELOC
;
268 while (!calcop (format
, param
, &insn
))
270 /* if it doesn't parse try the next instruction */
272 if (!strcmp (format
[0].name
, format
[1].name
))
276 as_fatal ("Parameter syntax error");
281 /* grow the current frag and plop in the opcode */
283 thisfrag
= frag_more (4);
284 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
286 /* if this instruction requires labels mark it for later */
295 fix_new_exp (frag_now
,
296 thisfrag
- frag_now
->fr_literal
+ 2,
304 fix_new_exp (frag_now
,
305 thisfrag
- frag_now
->fr_literal
,
313 fix_new_exp (frag_now
,
314 thisfrag
- frag_now
->fr_literal
+ 2,
322 fix_new_exp (frag_now
,
323 thisfrag
- frag_now
->fr_literal
,
331 as_fatal ("Unknown relocation type");
337 calcop (format
, param
, insn
)
338 struct m88k_opcode
*format
;
340 struct m88k_insn
*insn
;
342 char *fmt
= format
->op_spec
;
346 int reg_prefix
= 'r';
348 insn
->opcode
= format
->opcode
;
359 insn
->opcode
|= opcode
;
360 return (*param
== 0 || *param
== '\n');
368 param
= get_reg (param
, &val
, reg_prefix
);
374 param
= get_o6 (param
, &val
);
375 opcode
|= ((val
>> 2) << 7);
383 param
= get_reg (param
, &val
, reg_prefix
);
389 param
= get_reg (param
, &val
, reg_prefix
);
395 param
= get_reg (param
, &val
, 'r');
396 opcode
|= (val
<< 16) | val
;
400 param
= get_imm16 (param
, insn
);
404 param
= get_bf (param
, &val
);
409 param
= get_pcr (param
, insn
, RELOC_PC16
);
413 param
= get_pcr (param
, insn
, RELOC_PC26
);
417 param
= get_cmp (param
, &val
);
422 param
= get_cnd (param
, &val
);
427 param
= get_cr (param
, &val
);
432 param
= get_fcr (param
, &val
);
437 param
= get_vec9 (param
, &val
);
442 /* Having this here repeats the warning somtimes.
443 But can't we stand that? */
444 as_warn ("Use of obsolete instruction");
451 match_name (param
, assoc_tab
, valp
)
453 struct field_val_assoc
*assoc_tab
;
462 name
= assoc_tab
[i
].name
;
465 name_len
= strlen (name
);
466 if (!strncmp (param
, name
, name_len
))
468 *valp
= assoc_tab
[i
].val
;
469 return param
+ name_len
;
475 get_reg (param
, regnop
, reg_prefix
)
486 regno
= *param
++ - '0';
497 regno
= regno
* 10 + c
;
512 else if (c
== 's' && param
[0] == 'p')
522 get_imm16 (param
, insn
)
524 struct m88k_insn
*insn
;
526 enum reloc_type reloc
= NO_RELOC
;
530 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
535 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
540 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
546 save_ptr
= input_line_pointer
;
547 input_line_pointer
= param
;
548 expression (&insn
->exp
);
549 param
= input_line_pointer
;
550 input_line_pointer
= save_ptr
;
552 val
= insn
->exp
.X_add_number
;
554 if (insn
->exp
.X_op
== O_constant
)
556 /* Insert the value now, and reset reloc to NO_RELOC. */
557 if (reloc
== NO_RELOC
)
559 /* Warn about too big expressions if not surrounded by xx16. */
561 as_warn ("Expression truncated to 16 bits");
564 if (reloc
== RELOC_HI16
)
567 insn
->opcode
|= val
& 0xffff;
570 else if (reloc
== NO_RELOC
)
571 /* We accept a symbol even without lo16, hi16, etc, and assume
572 lo16 was intended. */
581 get_pcr (param
, insn
, reloc
)
583 struct m88k_insn
*insn
;
584 enum reloc_type reloc
;
586 char *saveptr
, *saveparam
;
588 saveptr
= input_line_pointer
;
589 input_line_pointer
= param
;
591 expression (&insn
->exp
);
593 saveparam
= input_line_pointer
;
594 input_line_pointer
= saveptr
;
596 /* Botch: We should relocate now if O_constant. */
603 get_cmp (param
, valp
)
612 param
= match_name (param
, cmpslot
, valp
);
619 save_ptr
= input_line_pointer
;
620 input_line_pointer
= param
;
621 val
= get_absolute_expression ();
622 param
= input_line_pointer
;
623 input_line_pointer
= save_ptr
;
627 as_warn ("Expression truncated to 5 bits");
637 get_cnd (param
, valp
)
643 if (isdigit (*param
))
645 param
= getval (param
, &val
);
649 as_warn ("Expression truncated to 5 bits");
655 if (isupper (*param
))
656 *param
= tolower (*param
);
658 if (isupper (param
[1]))
659 param
[1] = tolower (param
[1]);
661 param
= match_name (param
, cndmsk
, valp
);
690 else if (c
== bc
&& depth
<= 0)
697 get_bf_offset_expression (param
, offsetp
)
703 if (isalpha (param
[0]))
705 if (isupper (param
[0]))
706 param
[0] = tolower (param
[0]);
707 if (isupper (param
[1]))
708 param
[1] = tolower (param
[1]);
710 param
= match_name (param
, cmpslot
, offsetp
);
716 input_line_pointer
= param
;
717 offset
= get_absolute_expression ();
718 param
= input_line_pointer
;
735 xp
= get_bf2 (param
, '<');
737 save_ptr
= input_line_pointer
;
738 input_line_pointer
= param
;
741 /* We did not find '<'. We have an offset (width implicitly 32). */
742 param
= get_bf_offset_expression (param
, &offset
);
743 input_line_pointer
= save_ptr
;
749 *xp
++ = 0; /* Overwrite the '<' */
750 param
= get_bf2 (xp
, '>');
753 *param
++ = 0; /* Overwrite the '>' */
755 width
= get_absolute_expression ();
756 xp
= get_bf_offset_expression (xp
, &offset
);
757 input_line_pointer
= save_ptr
;
763 *valp
= ((width
% 32) << 5) | (offset
% 32);
769 get_cr (param
, regnop
)
776 if (!strncmp (param
, "cr", 2))
780 regno
= *param
++ - '0';
791 regno
= regno
* 10 + c
;
807 param
= match_name (param
, cr_regs
, regnop
);
813 get_fcr (param
, regnop
)
820 if (!strncmp (param
, "fcr", 3))
824 regno
= *param
++ - '0';
835 regno
= regno
* 10 + c
;
851 param
= match_name (param
, fcr_regs
, regnop
);
857 get_vec9 (param
, valp
)
864 save_ptr
= input_line_pointer
;
865 input_line_pointer
= param
;
866 val
= get_absolute_expression ();
867 param
= input_line_pointer
;
868 input_line_pointer
= save_ptr
;
871 as_warn ("Expression truncated to 9 bits");
873 *valp
= val
% (1 << 9);
886 save_ptr
= input_line_pointer
;
887 input_line_pointer
= param
;
888 val
= get_absolute_expression ();
889 param
= input_line_pointer
;
890 input_line_pointer
= save_ptr
;
893 as_warn ("Removed lower 2 bits of expression");
901 (isdigit (z) ? (z) - '0' : \
902 islower (z) ? (z) - 'a' + 10 : \
903 isupper (z) ? (z) - 'A' + 10 : -1)
910 unsigned int val
= 0;
917 if (c
== 'x' || c
== 'X')
953 md_number_to_chars (buf
, val
, nbytes
)
958 number_to_chars_bigendian (buf
, val
, nbytes
);
963 /* This routine is never called. What is it for?
964 Ian Taylor, Cygnus Support 13 Jul 1993 */
967 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
974 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
993 switch (fixP
->fx_r_type
)
1018 buf
[0] |= (val
>> 26) & 0x03;
1032 as_fatal ("Bad relocation type");
1040 md_number_to_disp (buf
, val
, nbytes
)
1045 as_fatal ("md_number_to_disp not defined");
1046 md_number_to_chars (buf
, val
, nbytes
);
1050 md_number_to_field (buf
, val
, nbytes
)
1055 as_fatal ("md_number_to_field not defined");
1056 md_number_to_chars (buf
, val
, nbytes
);
1059 #define MAX_LITTLENUMS 6
1061 /* Turn a string in input_line_pointer into a floating point constant of type
1062 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1063 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1066 md_atof (type
, litP
, sizeP
)
1072 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1073 LITTLENUM_TYPE
*wordP
;
1105 return "Bad call to MD_ATOF()";
1107 t
= atof_ieee (input_line_pointer
, type
, words
);
1109 input_line_pointer
= t
;
1111 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1112 for (wordP
= words
; prec
--;)
1114 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1115 litP
+= sizeof (LITTLENUM_TYPE
);
1120 int md_short_jump_size
= 4;
1123 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1125 addressT from_addr
, to_addr
;
1129 ptr
[0] = (char) 0xc0;
1134 ptr
- frag
->fr_literal
,
1139 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1142 int md_long_jump_size
= 4;
1145 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1147 addressT from_addr
, to_addr
;
1151 ptr
[0] = (char) 0xc0;
1156 ptr
- frag
->fr_literal
,
1165 md_estimate_size_before_relax (fragP
, segment_type
)
1169 as_fatal ("Relaxation should never occur");
1175 /* As far as I can tell, this routine is never called. What is it
1177 Ian Taylor, Cygnus Support 13 Jul 1993 */
1181 * Risc relocations are completely different, so it needs
1182 * this machine dependent routine to emit them.
1185 emit_relocations (fixP
, segment_address_in_file
)
1187 relax_addressT segment_address_in_file
;
1189 struct reloc_info_m88k ri
;
1191 extern char *next_object_file_charP
;
1193 bzero ((char *) &ri
, sizeof (ri
));
1194 for (; fixP
; fixP
= fixP
->fx_next
)
1196 if (fixP
->fx_r_type
>= NO_RELOC
)
1198 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1202 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1204 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1205 fixP
->fx_where
- segment_address_in_file
;
1206 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1209 ri
.r_symbolnum
= symbolP
->sy_number
;
1214 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1216 if (symbolP
&& symbolP
->sy_frag
)
1218 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1220 ri
.r_type
= fixP
->fx_r_type
;
1223 ri
.r_addend
-= ri
.r_address
;
1227 ri
.r_addend
= fixP
->fx_addnumber
;
1230 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1239 /* This routine can be subsumed by s_lcomm in read.c.
1240 Ian Taylor, Cygnus Support 13 Jul 1993 */
1249 int temp
, bss_align
;
1252 name
= input_line_pointer
;
1253 c
= get_symbol_end ();
1254 p
= input_line_pointer
;
1257 if (*input_line_pointer
!= ',')
1259 as_warn ("Expected comma after name");
1260 ignore_rest_of_line ();
1263 input_line_pointer
++;
1264 if ((temp
= get_absolute_expression ()) < 0)
1266 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1267 ignore_rest_of_line ();
1271 symbolP
= symbol_find_or_make (name
);
1273 if (*input_line_pointer
== ',')
1275 input_line_pointer
++;
1276 bss_align
= get_absolute_expression ();
1281 if (!S_IS_DEFINED(symbolP
)
1282 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1287 segT current_seg
= now_seg
;
1288 subsegT current_subseg
= now_subseg
;
1290 subseg_set (SEG_BSS
, 1); /* switch to bss */
1293 frag_align (bss_align
, 0);
1295 /* detach from old frag */
1296 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1297 symbolP
->sy_frag
->fr_symbol
= NULL
;
1299 symbolP
->sy_frag
= frag_now
;
1300 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1303 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1305 subseg_set (current_seg
, current_subseg
);
1310 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1313 while (!is_end_of_line
[*input_line_pointer
])
1315 input_line_pointer
++;
1323 /* These functions are needed if we are linking with obj-coffbfd.c.
1324 That file may be replaced by a more BFD oriented version at some
1325 point. If that happens, these functions should be rexamined.
1327 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1329 /* Given a fixS structure (created by a call to fix_new, above),
1330 return the BFD relocation type to use for it. */
1333 tc_coff_fix2rtype (fixp
)
1336 switch (fixp
->fx_r_type
)
1355 /* Apply a fixS to the object file. Since COFF does not use addends
1356 in relocs, the addend is actually stored directly in the object
1360 md_apply_fix (fixp
, val
)
1366 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1367 fixp
->fx_offset
= 0;
1369 switch (fixp
->fx_r_type
)
1372 fixp
->fx_offset
= val
>> 16;
1378 fixp
->fx_offset
= val
>> 16;
1384 fixp
->fx_offset
= val
>> 16;
1395 buf
[0] |= (val
>> 26) & 0x03;
1413 /* Where a PC relative offset is calculated from. On the m88k they
1414 are calculated from just after the instruction. */
1417 md_pcrel_from (fixp
)
1420 switch (fixp
->fx_r_type
)
1423 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1425 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1432 /* When we align the .init section, insert the correct NOP pattern. */
1435 m88k_do_align (n
, fill
)
1440 && strcmp (obj_segment_name (now_seg
), ".init") == 0)
1442 static const unsigned char nop_pattern
[] = { 0xf4, 0x00, 0x58, 0x00 };
1443 frag_align_pattern (n
, nop_pattern
, sizeof (nop_pattern
));
1449 #endif /* M88KCOFF */