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, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "m88k-opcode.h"
26 struct field_val_assoc
32 struct field_val_assoc cr_regs
[] =
59 struct field_val_assoc fcr_regs
[] =
77 struct field_val_assoc cmpslot
[] =
79 /* Integer Floating point */
90 {"lo", 10}, {"in", 10},
91 {"hs", 11}, {"ob", 11},
92 {"be", 12}, {"ue", 12},
93 {"nb", 13}, {"lg", 13},
94 {"he", 14}, {"ug", 14},
95 {"nh", 15}, {"ule", 15},
102 struct field_val_assoc cndmsk
[] =
116 unsigned long opcode
;
118 enum reloc_type reloc
;
121 static char *get_bf
PARAMS ((char *param
, unsigned *valp
));
122 static char *get_cmp
PARAMS ((char *param
, unsigned *valp
));
123 static char *get_cnd
PARAMS ((char *param
, unsigned *valp
));
124 static char *get_cr
PARAMS ((char *param
, unsigned *regnop
));
125 static char *get_fcr
PARAMS ((char *param
, unsigned *regnop
));
126 static char *get_imm16
PARAMS ((char *param
, struct m88k_insn
*insn
));
127 static char *get_o6
PARAMS ((char *param
, unsigned *valp
));
128 static char *get_reg
PARAMS ((char *param
, unsigned *regnop
, int reg_prefix
));
129 static char *get_vec9
PARAMS ((char *param
, unsigned *valp
));
130 static char *getval
PARAMS ((char *param
, unsigned int *valp
));
132 static char *get_pcr
PARAMS ((char *param
, struct m88k_insn
*insn
,
133 enum reloc_type reloc
));
135 static int calcop
PARAMS ((struct m88k_opcode
*format
,
136 char *param
, struct m88k_insn
*insn
));
140 static struct hash_control
*op_hash
= NULL
;
142 /* These bits should be turned off in the first address of every segment */
143 int md_seg_align
= 7;
145 /* These chars start a comment anywhere in a source file (except inside
147 const char comment_chars
[] = ";";
149 /* These chars only start a comment at the beginning of a line. */
150 const char line_comment_chars
[] = "#";
152 const char line_separator_chars
[] = "";
154 /* Chars that can be used to separate mant from exp in floating point nums */
155 const char EXP_CHARS
[] = "eE";
157 /* Chars that mean this number is a floating point constant */
158 /* as in 0f123.456 */
159 /* or 0H1.234E-12 (see exp chars above) */
160 const char FLT_CHARS
[] = "dDfF";
162 extern void float_cons (), cons (), s_globl (), s_space (),
163 s_set (), s_lcomm ();
165 const pseudo_typeS md_pseudo_table
[] =
167 {"align", s_align_bytes
, 4},
169 {"dfloat", float_cons
, 'd'},
170 {"ffloat", float_cons
, 'f'},
171 {"global", s_globl
, 0},
174 {"string", stringer
, 0},
176 {"zero", s_space
, 0},
177 /* Force set to be treated as an instruction. */
185 const char *retval
= NULL
;
188 /* initialize hash table */
190 op_hash
= hash_new ();
192 as_fatal ("Could not initialize hash table");
194 /* loop until you see the end of the list */
196 while (*m88k_opcodes
[i
].name
)
198 char *name
= m88k_opcodes
[i
].name
;
200 /* hash each mnemonic and record its position */
202 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
204 if (retval
!= NULL
&& *retval
!= '\0')
205 as_fatal ("Can't hash instruction '%s':%s",
206 m88k_opcodes
[i
].name
, retval
);
208 /* skip to next unique mnemonic or end of list */
210 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
216 md_parse_option (argP
, cntP
, vecP
)
228 char *param
, *thisfrag
;
230 struct m88k_opcode
*format
;
231 struct m88k_insn insn
;
235 /* skip over instruction to find parameters */
237 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
242 /* try to find the instruction in the hash table */
244 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
246 as_bad ("Invalid mnemonic '%s'", op
);
250 /* try parsing this instruction into insn */
252 insn
.exp
.X_add_symbol
= 0;
253 insn
.exp
.X_op_symbol
= 0;
254 insn
.exp
.X_add_number
= 0;
255 insn
.exp
.X_op
= O_illegal
;
256 insn
.reloc
= NO_RELOC
;
258 while (!calcop (format
, param
, &insn
))
260 /* if it doesn't parse try the next instruction */
262 if (!strcmp (format
[0].name
, format
[1].name
))
266 as_fatal ("Parameter syntax error");
271 /* grow the current frag and plop in the opcode */
273 thisfrag
= frag_more (4);
274 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
276 /* if this instruction requires labels mark it for later */
285 fix_new_exp (frag_now
,
286 thisfrag
- frag_now
->fr_literal
+ 2,
294 fix_new_exp (frag_now
,
295 thisfrag
- frag_now
->fr_literal
,
303 fix_new_exp (frag_now
,
304 thisfrag
- frag_now
->fr_literal
+ 2,
312 fix_new_exp (frag_now
,
313 thisfrag
- frag_now
->fr_literal
,
321 as_fatal ("Unknown relocation type");
327 calcop (format
, param
, insn
)
328 struct m88k_opcode
*format
;
330 struct m88k_insn
*insn
;
332 char *fmt
= format
->op_spec
;
336 int reg_prefix
= 'r';
338 insn
->opcode
= format
->opcode
;
349 insn
->opcode
|= opcode
;
350 return (*param
== 0 || *param
== '\n');
358 param
= get_reg (param
, &val
, reg_prefix
);
364 param
= get_o6 (param
, &val
);
365 opcode
|= ((val
>> 2) << 7);
373 param
= get_reg (param
, &val
, reg_prefix
);
379 param
= get_reg (param
, &val
, reg_prefix
);
385 param
= get_reg (param
, &val
, 'r');
386 opcode
|= (val
<< 16) | val
;
390 param
= get_imm16 (param
, insn
);
394 param
= get_bf (param
, &val
);
399 param
= get_pcr (param
, insn
, RELOC_PC16
);
403 param
= get_pcr (param
, insn
, RELOC_PC26
);
407 param
= get_cmp (param
, &val
);
412 param
= get_cnd (param
, &val
);
417 param
= get_cr (param
, &val
);
422 param
= get_fcr (param
, &val
);
427 param
= get_vec9 (param
, &val
);
432 /* Having this here repeats the warning somtimes.
433 But can't we stand that? */
434 as_warn ("Use of obsolete instruction");
441 match_name (param
, assoc_tab
, valp
)
443 struct field_val_assoc
*assoc_tab
;
452 name
= assoc_tab
[i
].name
;
455 name_len
= strlen (name
);
456 if (!strncmp (param
, name
, name_len
))
458 *valp
= assoc_tab
[i
].val
;
459 return param
+ name_len
;
465 get_reg (param
, regnop
, reg_prefix
)
476 regno
= *param
++ - '0';
487 regno
= regno
* 10 + c
;
502 else if (c
== 's' && param
[0] == 'p')
512 get_imm16 (param
, insn
)
514 struct m88k_insn
*insn
;
516 enum reloc_type reloc
= NO_RELOC
;
520 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
525 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
530 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
536 save_ptr
= input_line_pointer
;
537 input_line_pointer
= param
;
538 expression (&insn
->exp
);
539 param
= input_line_pointer
;
540 input_line_pointer
= save_ptr
;
542 val
= insn
->exp
.X_add_number
;
544 if (insn
->exp
.X_op
== O_constant
)
546 /* Insert the value now, and reset reloc to NO_RELOC. */
547 if (reloc
== NO_RELOC
)
549 /* Warn about too big expressions if not surrounded by xx16. */
551 as_warn ("Expression truncated to 16 bits");
554 if (reloc
== RELOC_HI16
)
557 insn
->opcode
|= val
& 0xffff;
560 else if (reloc
== NO_RELOC
)
561 /* We accept a symbol even without lo16, hi16, etc, and assume
562 lo16 was intended. */
571 get_pcr (param
, insn
, reloc
)
573 struct m88k_insn
*insn
;
574 enum reloc_type reloc
;
576 char *saveptr
, *saveparam
;
578 saveptr
= input_line_pointer
;
579 input_line_pointer
= param
;
581 expression (&insn
->exp
);
583 saveparam
= input_line_pointer
;
584 input_line_pointer
= saveptr
;
586 /* Botch: We should relocate now if O_constant. */
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
)
766 if (!strncmp (param
, "cr", 2))
770 regno
= *param
++ - '0';
781 regno
= regno
* 10 + c
;
797 param
= match_name (param
, cr_regs
, regnop
);
803 get_fcr (param
, regnop
)
810 if (!strncmp (param
, "fcr", 3))
814 regno
= *param
++ - '0';
825 regno
= regno
* 10 + c
;
841 param
= match_name (param
, fcr_regs
, regnop
);
847 get_vec9 (param
, valp
)
854 save_ptr
= input_line_pointer
;
855 input_line_pointer
= param
;
856 val
= get_absolute_expression ();
857 param
= input_line_pointer
;
858 input_line_pointer
= save_ptr
;
861 as_warn ("Expression truncated to 9 bits");
863 *valp
= val
% (1 << 9);
876 save_ptr
= input_line_pointer
;
877 input_line_pointer
= param
;
878 val
= get_absolute_expression ();
879 param
= input_line_pointer
;
880 input_line_pointer
= save_ptr
;
883 as_warn ("Removed lower 2 bits of expression");
891 (isdigit (z) ? (z) - '0' : \
892 islower (z) ? (z) - 'a' + 10 : \
893 isupper (z) ? (z) - 'A' + 10 : -1)
900 unsigned int val
= 0;
907 if (c
== 'x' || c
== 'X')
943 md_number_to_chars (buf
, val
, nbytes
)
966 /* This routine is never called. What is it for?
967 Ian Taylor, Cygnus Support 13 Jul 1993 */
970 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
977 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
996 switch (fixP
->fx_r_type
)
1021 buf
[0] |= (val
>> 26) & 0x03;
1035 as_fatal ("Bad relocation type");
1043 md_number_to_disp (buf
, val
, nbytes
)
1048 as_fatal ("md_number_to_disp not defined");
1049 md_number_to_chars (buf
, val
, nbytes
);
1053 md_number_to_field (buf
, val
, nbytes
)
1058 as_fatal ("md_number_to_field not defined");
1059 md_number_to_chars (buf
, val
, nbytes
);
1062 #define MAX_LITTLENUMS 6
1064 /* Turn a string in input_line_pointer into a floating point constant of type
1065 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1066 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1069 md_atof (type
, litP
, sizeP
)
1075 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1076 LITTLENUM_TYPE
*wordP
;
1108 return "Bad call to MD_ATOF()";
1110 t
= atof_ieee (input_line_pointer
, type
, words
);
1112 input_line_pointer
= t
;
1114 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1115 for (wordP
= words
; prec
--;)
1117 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1118 litP
+= sizeof (LITTLENUM_TYPE
);
1120 return ""; /* Someone should teach Dean about null pointers */
1123 int md_short_jump_size
= 4;
1126 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1128 addressT from_addr
, to_addr
;
1132 ptr
[0] = (char) 0xc0;
1137 ptr
- frag
->fr_literal
,
1142 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1145 int md_long_jump_size
= 4;
1148 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1150 addressT from_addr
, to_addr
;
1154 ptr
[0] = (char) 0xc0;
1159 ptr
- frag
->fr_literal
,
1168 md_estimate_size_before_relax (fragP
, segment_type
)
1172 as_fatal ("Relaxation should never occur");
1176 const relax_typeS md_relax_table
[] =
1186 /* As far as I can tell, this routine is never called. What is it
1188 Ian Taylor, Cygnus Support 13 Jul 1993 */
1192 * Risc relocations are completely different, so it needs
1193 * this machine dependent routine to emit them.
1196 emit_relocations (fixP
, segment_address_in_file
)
1198 relax_addressT segment_address_in_file
;
1200 struct reloc_info_m88k ri
;
1202 extern char *next_object_file_charP
;
1204 bzero ((char *) &ri
, sizeof (ri
));
1205 for (; fixP
; fixP
= fixP
->fx_next
)
1207 if (fixP
->fx_r_type
>= NO_RELOC
)
1209 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1213 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1215 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1216 fixP
->fx_where
- segment_address_in_file
;
1217 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1220 ri
.r_symbolnum
= symbolP
->sy_number
;
1225 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1227 if (symbolP
&& symbolP
->sy_frag
)
1229 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1231 ri
.r_type
= fixP
->fx_r_type
;
1234 ri
.r_addend
-= ri
.r_address
;
1238 ri
.r_addend
= fixP
->fx_addnumber
;
1241 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1251 /* This routine can be subsumed by s_lcomm in read.c.
1252 Ian Taylor, Cygnus Support 13 Jul 1993 */
1261 int temp
, bss_align
;
1264 name
= input_line_pointer
;
1265 c
= get_symbol_end ();
1266 p
= input_line_pointer
;
1269 if (*input_line_pointer
!= ',')
1271 as_warn ("Expected comma after name");
1272 ignore_rest_of_line ();
1275 input_line_pointer
++;
1276 if ((temp
= get_absolute_expression ()) < 0)
1278 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1279 ignore_rest_of_line ();
1283 symbolP
= symbol_find_or_make (name
);
1285 if (*input_line_pointer
== ',')
1287 input_line_pointer
++;
1288 bss_align
= get_absolute_expression ();
1293 if (!S_IS_DEFINED(symbolP
)
1294 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1299 segT current_seg
= now_seg
;
1300 subsegT current_subseg
= now_subseg
;
1302 subseg_set (SEG_BSS
, 1); /* switch to bss */
1305 frag_align (bss_align
, 0);
1307 /* detach from old frag */
1308 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1309 symbolP
->sy_frag
->fr_symbol
= NULL
;
1311 symbolP
->sy_frag
= frag_now
;
1312 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1315 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1317 subseg_set (current_seg
, current_subseg
);
1322 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1325 while (!is_end_of_line
[*input_line_pointer
])
1327 input_line_pointer
++;
1337 /* These functions are needed if we are linking with obj-coffbfd.c.
1338 That file may be replaced by a more BFD oriented version at some
1339 point. If that happens, these functions should be rexamined.
1341 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1343 /* Given a fixS structure (created by a call to fix_new, above),
1344 return the BFD relocation type to use for it. */
1347 tc_coff_fix2rtype (fixp
)
1350 switch (fixp
->fx_r_type
)
1369 /* Apply a fixS to the object file. Since COFF does not use addends
1370 in relocs, the addend is actually stored directly in the object
1374 md_apply_fix (fixp
, val
)
1380 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1381 fixp
->fx_offset
= 0;
1383 switch (fixp
->fx_r_type
)
1386 fixp
->fx_offset
= val
>> 16;
1392 fixp
->fx_offset
= val
>> 16;
1398 fixp
->fx_offset
= val
>> 16;
1409 buf
[0] |= (val
>> 26) & 0x03;
1427 /* Where a PC relative offset is calculated from. On the m88k they
1428 are calculated from just after the instruction. */
1431 md_pcrel_from (fixp
)
1434 switch (fixp
->fx_r_type
)
1437 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1439 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1446 #endif /* M88KCOFF */