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 /* Force set to be treated as an instruction. */
184 const char *retval
= NULL
;
187 /* initialize hash table */
189 op_hash
= hash_new ();
191 /* loop until you see the end of the list */
193 while (*m88k_opcodes
[i
].name
)
195 char *name
= m88k_opcodes
[i
].name
;
197 /* hash each mnemonic and record its position */
199 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
202 as_fatal ("Can't hash instruction '%s':%s",
203 m88k_opcodes
[i
].name
, retval
);
205 /* skip to next unique mnemonic or end of list */
207 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
213 md_parse_option (argP
, cntP
, vecP
)
225 char *param
, *thisfrag
;
227 struct m88k_opcode
*format
;
228 struct m88k_insn insn
;
232 /* skip over instruction to find parameters */
234 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
239 /* try to find the instruction in the hash table */
241 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
243 as_bad ("Invalid mnemonic '%s'", op
);
247 /* try parsing this instruction into insn */
249 insn
.exp
.X_add_symbol
= 0;
250 insn
.exp
.X_op_symbol
= 0;
251 insn
.exp
.X_add_number
= 0;
252 insn
.exp
.X_op
= O_illegal
;
253 insn
.reloc
= NO_RELOC
;
255 while (!calcop (format
, param
, &insn
))
257 /* if it doesn't parse try the next instruction */
259 if (!strcmp (format
[0].name
, format
[1].name
))
263 as_fatal ("Parameter syntax error");
268 /* grow the current frag and plop in the opcode */
270 thisfrag
= frag_more (4);
271 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
273 /* if this instruction requires labels mark it for later */
282 fix_new_exp (frag_now
,
283 thisfrag
- frag_now
->fr_literal
+ 2,
291 fix_new_exp (frag_now
,
292 thisfrag
- frag_now
->fr_literal
,
300 fix_new_exp (frag_now
,
301 thisfrag
- frag_now
->fr_literal
+ 2,
309 fix_new_exp (frag_now
,
310 thisfrag
- frag_now
->fr_literal
,
318 as_fatal ("Unknown relocation type");
324 calcop (format
, param
, insn
)
325 struct m88k_opcode
*format
;
327 struct m88k_insn
*insn
;
329 char *fmt
= format
->op_spec
;
333 int reg_prefix
= 'r';
335 insn
->opcode
= format
->opcode
;
346 insn
->opcode
|= opcode
;
347 return (*param
== 0 || *param
== '\n');
355 param
= get_reg (param
, &val
, reg_prefix
);
361 param
= get_o6 (param
, &val
);
362 opcode
|= ((val
>> 2) << 7);
370 param
= get_reg (param
, &val
, reg_prefix
);
376 param
= get_reg (param
, &val
, reg_prefix
);
382 param
= get_reg (param
, &val
, 'r');
383 opcode
|= (val
<< 16) | val
;
387 param
= get_imm16 (param
, insn
);
391 param
= get_bf (param
, &val
);
396 param
= get_pcr (param
, insn
, RELOC_PC16
);
400 param
= get_pcr (param
, insn
, RELOC_PC26
);
404 param
= get_cmp (param
, &val
);
409 param
= get_cnd (param
, &val
);
414 param
= get_cr (param
, &val
);
419 param
= get_fcr (param
, &val
);
424 param
= get_vec9 (param
, &val
);
429 /* Having this here repeats the warning somtimes.
430 But can't we stand that? */
431 as_warn ("Use of obsolete instruction");
438 match_name (param
, assoc_tab
, valp
)
440 struct field_val_assoc
*assoc_tab
;
449 name
= assoc_tab
[i
].name
;
452 name_len
= strlen (name
);
453 if (!strncmp (param
, name
, name_len
))
455 *valp
= assoc_tab
[i
].val
;
456 return param
+ name_len
;
462 get_reg (param
, regnop
, reg_prefix
)
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
;
517 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
522 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
527 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
533 save_ptr
= input_line_pointer
;
534 input_line_pointer
= param
;
535 expression (&insn
->exp
);
536 param
= input_line_pointer
;
537 input_line_pointer
= save_ptr
;
539 val
= insn
->exp
.X_add_number
;
541 if (insn
->exp
.X_op
== O_constant
)
543 /* Insert the value now, and reset reloc to NO_RELOC. */
544 if (reloc
== NO_RELOC
)
546 /* Warn about too big expressions if not surrounded by xx16. */
548 as_warn ("Expression truncated to 16 bits");
551 if (reloc
== RELOC_HI16
)
554 insn
->opcode
|= val
& 0xffff;
557 else if (reloc
== NO_RELOC
)
558 /* We accept a symbol even without lo16, hi16, etc, and assume
559 lo16 was intended. */
568 get_pcr (param
, insn
, reloc
)
570 struct m88k_insn
*insn
;
571 enum reloc_type reloc
;
573 char *saveptr
, *saveparam
;
575 saveptr
= input_line_pointer
;
576 input_line_pointer
= param
;
578 expression (&insn
->exp
);
580 saveparam
= input_line_pointer
;
581 input_line_pointer
= saveptr
;
583 /* Botch: We should relocate now if O_constant. */
590 get_cmp (param
, valp
)
599 param
= match_name (param
, cmpslot
, valp
);
606 save_ptr
= input_line_pointer
;
607 input_line_pointer
= param
;
608 val
= get_absolute_expression ();
609 param
= input_line_pointer
;
610 input_line_pointer
= save_ptr
;
614 as_warn ("Expression truncated to 5 bits");
624 get_cnd (param
, valp
)
630 if (isdigit (*param
))
632 param
= getval (param
, &val
);
636 as_warn ("Expression truncated to 5 bits");
642 if (isupper (*param
))
643 *param
= tolower (*param
);
645 if (isupper (param
[1]))
646 param
[1] = tolower (param
[1]);
648 param
= match_name (param
, cndmsk
, valp
);
677 else if (c
== bc
&& depth
<= 0)
684 get_bf_offset_expression (param
, offsetp
)
690 if (isalpha (param
[0]))
692 if (isupper (param
[0]))
693 param
[0] = tolower (param
[0]);
694 if (isupper (param
[1]))
695 param
[1] = tolower (param
[1]);
697 param
= match_name (param
, cmpslot
, offsetp
);
703 input_line_pointer
= param
;
704 offset
= get_absolute_expression ();
705 param
= input_line_pointer
;
722 xp
= get_bf2 (param
, '<');
724 save_ptr
= input_line_pointer
;
725 input_line_pointer
= param
;
728 /* We did not find '<'. We have an offset (width implicitly 32). */
729 param
= get_bf_offset_expression (param
, &offset
);
730 input_line_pointer
= save_ptr
;
736 *xp
++ = 0; /* Overwrite the '<' */
737 param
= get_bf2 (xp
, '>');
740 *param
++ = 0; /* Overwrite the '>' */
742 width
= get_absolute_expression ();
743 xp
= get_bf_offset_expression (xp
, &offset
);
744 input_line_pointer
= save_ptr
;
750 *valp
= ((width
% 32) << 5) | (offset
% 32);
756 get_cr (param
, regnop
)
763 if (!strncmp (param
, "cr", 2))
767 regno
= *param
++ - '0';
778 regno
= regno
* 10 + c
;
794 param
= match_name (param
, cr_regs
, regnop
);
800 get_fcr (param
, regnop
)
807 if (!strncmp (param
, "fcr", 3))
811 regno
= *param
++ - '0';
822 regno
= regno
* 10 + c
;
838 param
= match_name (param
, fcr_regs
, regnop
);
844 get_vec9 (param
, valp
)
851 save_ptr
= input_line_pointer
;
852 input_line_pointer
= param
;
853 val
= get_absolute_expression ();
854 param
= input_line_pointer
;
855 input_line_pointer
= save_ptr
;
858 as_warn ("Expression truncated to 9 bits");
860 *valp
= val
% (1 << 9);
873 save_ptr
= input_line_pointer
;
874 input_line_pointer
= param
;
875 val
= get_absolute_expression ();
876 param
= input_line_pointer
;
877 input_line_pointer
= save_ptr
;
880 as_warn ("Removed lower 2 bits of expression");
888 (isdigit (z) ? (z) - '0' : \
889 islower (z) ? (z) - 'a' + 10 : \
890 isupper (z) ? (z) - 'A' + 10 : -1)
897 unsigned int val
= 0;
904 if (c
== 'x' || c
== 'X')
940 md_number_to_chars (buf
, val
, nbytes
)
945 number_to_chars_bigendian (buf
, val
, nbytes
);
950 /* This routine is never called. What is it for?
951 Ian Taylor, Cygnus Support 13 Jul 1993 */
954 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
961 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
980 switch (fixP
->fx_r_type
)
1005 buf
[0] |= (val
>> 26) & 0x03;
1019 as_fatal ("Bad relocation type");
1027 md_number_to_disp (buf
, val
, nbytes
)
1032 as_fatal ("md_number_to_disp not defined");
1033 md_number_to_chars (buf
, val
, nbytes
);
1037 md_number_to_field (buf
, val
, nbytes
)
1042 as_fatal ("md_number_to_field not defined");
1043 md_number_to_chars (buf
, val
, nbytes
);
1046 #define MAX_LITTLENUMS 6
1048 /* Turn a string in input_line_pointer into a floating point constant of type
1049 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1050 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1053 md_atof (type
, litP
, sizeP
)
1059 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1060 LITTLENUM_TYPE
*wordP
;
1092 return "Bad call to MD_ATOF()";
1094 t
= atof_ieee (input_line_pointer
, type
, words
);
1096 input_line_pointer
= t
;
1098 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1099 for (wordP
= words
; prec
--;)
1101 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1102 litP
+= sizeof (LITTLENUM_TYPE
);
1107 int md_short_jump_size
= 4;
1110 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1112 addressT from_addr
, to_addr
;
1116 ptr
[0] = (char) 0xc0;
1121 ptr
- frag
->fr_literal
,
1126 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1129 int md_long_jump_size
= 4;
1132 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1134 addressT from_addr
, to_addr
;
1138 ptr
[0] = (char) 0xc0;
1143 ptr
- frag
->fr_literal
,
1152 md_estimate_size_before_relax (fragP
, segment_type
)
1156 as_fatal ("Relaxation should never occur");
1160 const relax_typeS md_relax_table
[] =
1165 /* As far as I can tell, this routine is never called. What is it
1167 Ian Taylor, Cygnus Support 13 Jul 1993 */
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
)
1186 if (fixP
->fx_r_type
>= NO_RELOC
)
1188 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1192 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1194 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1195 fixP
->fx_where
- segment_address_in_file
;
1196 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1199 ri
.r_symbolnum
= symbolP
->sy_number
;
1204 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1206 if (symbolP
&& symbolP
->sy_frag
)
1208 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1210 ri
.r_type
= fixP
->fx_r_type
;
1213 ri
.r_addend
-= ri
.r_address
;
1217 ri
.r_addend
= fixP
->fx_addnumber
;
1220 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1229 /* This routine can be subsumed by s_lcomm in read.c.
1230 Ian Taylor, Cygnus Support 13 Jul 1993 */
1239 int temp
, bss_align
;
1242 name
= input_line_pointer
;
1243 c
= get_symbol_end ();
1244 p
= input_line_pointer
;
1247 if (*input_line_pointer
!= ',')
1249 as_warn ("Expected comma after name");
1250 ignore_rest_of_line ();
1253 input_line_pointer
++;
1254 if ((temp
= get_absolute_expression ()) < 0)
1256 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1257 ignore_rest_of_line ();
1261 symbolP
= symbol_find_or_make (name
);
1263 if (*input_line_pointer
== ',')
1265 input_line_pointer
++;
1266 bss_align
= get_absolute_expression ();
1271 if (!S_IS_DEFINED(symbolP
)
1272 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1277 segT current_seg
= now_seg
;
1278 subsegT current_subseg
= now_subseg
;
1280 subseg_set (SEG_BSS
, 1); /* switch to bss */
1283 frag_align (bss_align
, 0);
1285 /* detach from old frag */
1286 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1287 symbolP
->sy_frag
->fr_symbol
= NULL
;
1289 symbolP
->sy_frag
= frag_now
;
1290 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1293 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1295 subseg_set (current_seg
, current_subseg
);
1300 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1303 while (!is_end_of_line
[*input_line_pointer
])
1305 input_line_pointer
++;
1313 /* These functions are needed if we are linking with obj-coffbfd.c.
1314 That file may be replaced by a more BFD oriented version at some
1315 point. If that happens, these functions should be rexamined.
1317 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1319 /* Given a fixS structure (created by a call to fix_new, above),
1320 return the BFD relocation type to use for it. */
1323 tc_coff_fix2rtype (fixp
)
1326 switch (fixp
->fx_r_type
)
1345 /* Apply a fixS to the object file. Since COFF does not use addends
1346 in relocs, the addend is actually stored directly in the object
1350 md_apply_fix (fixp
, val
)
1356 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1357 fixp
->fx_offset
= 0;
1359 switch (fixp
->fx_r_type
)
1362 fixp
->fx_offset
= val
>> 16;
1368 fixp
->fx_offset
= val
>> 16;
1374 fixp
->fx_offset
= val
>> 16;
1385 buf
[0] |= (val
>> 26) & 0x03;
1403 /* Where a PC relative offset is calculated from. On the m88k they
1404 are calculated from just after the instruction. */
1407 md_pcrel_from (fixp
)
1410 switch (fixp
->fx_r_type
)
1413 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1415 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1422 #endif /* M88KCOFF */