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
++)
212 CONST
char *md_shortopts
= "";
213 struct option md_longopts
[] = {
214 {NULL
, no_argument
, NULL
, 0}
216 size_t md_longopts_size
= sizeof(md_longopts
);
219 md_parse_option (c
, arg
)
227 md_show_usage (stream
)
236 char *param
, *thisfrag
;
238 struct m88k_opcode
*format
;
239 struct m88k_insn insn
;
243 /* skip over instruction to find parameters */
245 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
250 /* try to find the instruction in the hash table */
252 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
254 as_bad ("Invalid mnemonic '%s'", op
);
258 /* try parsing this instruction into insn */
260 insn
.exp
.X_add_symbol
= 0;
261 insn
.exp
.X_op_symbol
= 0;
262 insn
.exp
.X_add_number
= 0;
263 insn
.exp
.X_op
= O_illegal
;
264 insn
.reloc
= NO_RELOC
;
266 while (!calcop (format
, param
, &insn
))
268 /* if it doesn't parse try the next instruction */
270 if (!strcmp (format
[0].name
, format
[1].name
))
274 as_fatal ("Parameter syntax error");
279 /* grow the current frag and plop in the opcode */
281 thisfrag
= frag_more (4);
282 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
284 /* if this instruction requires labels mark it for later */
293 fix_new_exp (frag_now
,
294 thisfrag
- frag_now
->fr_literal
+ 2,
302 fix_new_exp (frag_now
,
303 thisfrag
- frag_now
->fr_literal
,
311 fix_new_exp (frag_now
,
312 thisfrag
- frag_now
->fr_literal
+ 2,
320 fix_new_exp (frag_now
,
321 thisfrag
- frag_now
->fr_literal
,
329 as_fatal ("Unknown relocation type");
335 calcop (format
, param
, insn
)
336 struct m88k_opcode
*format
;
338 struct m88k_insn
*insn
;
340 char *fmt
= format
->op_spec
;
344 int reg_prefix
= 'r';
346 insn
->opcode
= format
->opcode
;
357 insn
->opcode
|= opcode
;
358 return (*param
== 0 || *param
== '\n');
366 param
= get_reg (param
, &val
, reg_prefix
);
372 param
= get_o6 (param
, &val
);
373 opcode
|= ((val
>> 2) << 7);
381 param
= get_reg (param
, &val
, reg_prefix
);
387 param
= get_reg (param
, &val
, reg_prefix
);
393 param
= get_reg (param
, &val
, 'r');
394 opcode
|= (val
<< 16) | val
;
398 param
= get_imm16 (param
, insn
);
402 param
= get_bf (param
, &val
);
407 param
= get_pcr (param
, insn
, RELOC_PC16
);
411 param
= get_pcr (param
, insn
, RELOC_PC26
);
415 param
= get_cmp (param
, &val
);
420 param
= get_cnd (param
, &val
);
425 param
= get_cr (param
, &val
);
430 param
= get_fcr (param
, &val
);
435 param
= get_vec9 (param
, &val
);
440 /* Having this here repeats the warning somtimes.
441 But can't we stand that? */
442 as_warn ("Use of obsolete instruction");
449 match_name (param
, assoc_tab
, valp
)
451 struct field_val_assoc
*assoc_tab
;
460 name
= assoc_tab
[i
].name
;
463 name_len
= strlen (name
);
464 if (!strncmp (param
, name
, name_len
))
466 *valp
= assoc_tab
[i
].val
;
467 return param
+ name_len
;
473 get_reg (param
, regnop
, reg_prefix
)
484 regno
= *param
++ - '0';
495 regno
= regno
* 10 + c
;
510 else if (c
== 's' && param
[0] == 'p')
520 get_imm16 (param
, insn
)
522 struct m88k_insn
*insn
;
524 enum reloc_type reloc
= NO_RELOC
;
528 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
533 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
538 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
544 save_ptr
= input_line_pointer
;
545 input_line_pointer
= param
;
546 expression (&insn
->exp
);
547 param
= input_line_pointer
;
548 input_line_pointer
= save_ptr
;
550 val
= insn
->exp
.X_add_number
;
552 if (insn
->exp
.X_op
== O_constant
)
554 /* Insert the value now, and reset reloc to NO_RELOC. */
555 if (reloc
== NO_RELOC
)
557 /* Warn about too big expressions if not surrounded by xx16. */
559 as_warn ("Expression truncated to 16 bits");
562 if (reloc
== RELOC_HI16
)
565 insn
->opcode
|= val
& 0xffff;
568 else if (reloc
== NO_RELOC
)
569 /* We accept a symbol even without lo16, hi16, etc, and assume
570 lo16 was intended. */
579 get_pcr (param
, insn
, reloc
)
581 struct m88k_insn
*insn
;
582 enum reloc_type reloc
;
584 char *saveptr
, *saveparam
;
586 saveptr
= input_line_pointer
;
587 input_line_pointer
= param
;
589 expression (&insn
->exp
);
591 saveparam
= input_line_pointer
;
592 input_line_pointer
= saveptr
;
594 /* Botch: We should relocate now if O_constant. */
601 get_cmp (param
, valp
)
610 param
= match_name (param
, cmpslot
, valp
);
617 save_ptr
= input_line_pointer
;
618 input_line_pointer
= param
;
619 val
= get_absolute_expression ();
620 param
= input_line_pointer
;
621 input_line_pointer
= save_ptr
;
625 as_warn ("Expression truncated to 5 bits");
635 get_cnd (param
, valp
)
641 if (isdigit (*param
))
643 param
= getval (param
, &val
);
647 as_warn ("Expression truncated to 5 bits");
653 if (isupper (*param
))
654 *param
= tolower (*param
);
656 if (isupper (param
[1]))
657 param
[1] = tolower (param
[1]);
659 param
= match_name (param
, cndmsk
, valp
);
688 else if (c
== bc
&& depth
<= 0)
695 get_bf_offset_expression (param
, offsetp
)
701 if (isalpha (param
[0]))
703 if (isupper (param
[0]))
704 param
[0] = tolower (param
[0]);
705 if (isupper (param
[1]))
706 param
[1] = tolower (param
[1]);
708 param
= match_name (param
, cmpslot
, offsetp
);
714 input_line_pointer
= param
;
715 offset
= get_absolute_expression ();
716 param
= input_line_pointer
;
733 xp
= get_bf2 (param
, '<');
735 save_ptr
= input_line_pointer
;
736 input_line_pointer
= param
;
739 /* We did not find '<'. We have an offset (width implicitly 32). */
740 param
= get_bf_offset_expression (param
, &offset
);
741 input_line_pointer
= save_ptr
;
747 *xp
++ = 0; /* Overwrite the '<' */
748 param
= get_bf2 (xp
, '>');
751 *param
++ = 0; /* Overwrite the '>' */
753 width
= get_absolute_expression ();
754 xp
= get_bf_offset_expression (xp
, &offset
);
755 input_line_pointer
= save_ptr
;
761 *valp
= ((width
% 32) << 5) | (offset
% 32);
767 get_cr (param
, regnop
)
774 if (!strncmp (param
, "cr", 2))
778 regno
= *param
++ - '0';
789 regno
= regno
* 10 + c
;
805 param
= match_name (param
, cr_regs
, regnop
);
811 get_fcr (param
, regnop
)
818 if (!strncmp (param
, "fcr", 3))
822 regno
= *param
++ - '0';
833 regno
= regno
* 10 + c
;
849 param
= match_name (param
, fcr_regs
, regnop
);
855 get_vec9 (param
, valp
)
862 save_ptr
= input_line_pointer
;
863 input_line_pointer
= param
;
864 val
= get_absolute_expression ();
865 param
= input_line_pointer
;
866 input_line_pointer
= save_ptr
;
869 as_warn ("Expression truncated to 9 bits");
871 *valp
= val
% (1 << 9);
884 save_ptr
= input_line_pointer
;
885 input_line_pointer
= param
;
886 val
= get_absolute_expression ();
887 param
= input_line_pointer
;
888 input_line_pointer
= save_ptr
;
891 as_warn ("Removed lower 2 bits of expression");
899 (isdigit (z) ? (z) - '0' : \
900 islower (z) ? (z) - 'a' + 10 : \
901 isupper (z) ? (z) - 'A' + 10 : -1)
908 unsigned int val
= 0;
915 if (c
== 'x' || c
== 'X')
951 md_number_to_chars (buf
, val
, nbytes
)
956 number_to_chars_bigendian (buf
, val
, nbytes
);
961 /* This routine is never called. What is it for?
962 Ian Taylor, Cygnus Support 13 Jul 1993 */
965 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
972 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
991 switch (fixP
->fx_r_type
)
1016 buf
[0] |= (val
>> 26) & 0x03;
1030 as_fatal ("Bad relocation type");
1038 md_number_to_disp (buf
, val
, nbytes
)
1043 as_fatal ("md_number_to_disp not defined");
1044 md_number_to_chars (buf
, val
, nbytes
);
1048 md_number_to_field (buf
, val
, nbytes
)
1053 as_fatal ("md_number_to_field not defined");
1054 md_number_to_chars (buf
, val
, nbytes
);
1057 #define MAX_LITTLENUMS 6
1059 /* Turn a string in input_line_pointer into a floating point constant of type
1060 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1061 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1064 md_atof (type
, litP
, sizeP
)
1070 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1071 LITTLENUM_TYPE
*wordP
;
1103 return "Bad call to MD_ATOF()";
1105 t
= atof_ieee (input_line_pointer
, type
, words
);
1107 input_line_pointer
= t
;
1109 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1110 for (wordP
= words
; prec
--;)
1112 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1113 litP
+= sizeof (LITTLENUM_TYPE
);
1118 int md_short_jump_size
= 4;
1121 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1123 addressT from_addr
, to_addr
;
1127 ptr
[0] = (char) 0xc0;
1132 ptr
- frag
->fr_literal
,
1137 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1140 int md_long_jump_size
= 4;
1143 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1145 addressT from_addr
, to_addr
;
1149 ptr
[0] = (char) 0xc0;
1154 ptr
- frag
->fr_literal
,
1163 md_estimate_size_before_relax (fragP
, segment_type
)
1167 as_fatal ("Relaxation should never occur");
1171 const relax_typeS md_relax_table
[] =
1176 /* As far as I can tell, this routine is never called. What is it
1178 Ian Taylor, Cygnus Support 13 Jul 1993 */
1182 * Risc relocations are completely different, so it needs
1183 * this machine dependent routine to emit them.
1186 emit_relocations (fixP
, segment_address_in_file
)
1188 relax_addressT segment_address_in_file
;
1190 struct reloc_info_m88k ri
;
1192 extern char *next_object_file_charP
;
1194 bzero ((char *) &ri
, sizeof (ri
));
1195 for (; fixP
; fixP
= fixP
->fx_next
)
1197 if (fixP
->fx_r_type
>= NO_RELOC
)
1199 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1203 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1205 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1206 fixP
->fx_where
- segment_address_in_file
;
1207 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1210 ri
.r_symbolnum
= symbolP
->sy_number
;
1215 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1217 if (symbolP
&& symbolP
->sy_frag
)
1219 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1221 ri
.r_type
= fixP
->fx_r_type
;
1224 ri
.r_addend
-= ri
.r_address
;
1228 ri
.r_addend
= fixP
->fx_addnumber
;
1231 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1240 /* This routine can be subsumed by s_lcomm in read.c.
1241 Ian Taylor, Cygnus Support 13 Jul 1993 */
1250 int temp
, bss_align
;
1253 name
= input_line_pointer
;
1254 c
= get_symbol_end ();
1255 p
= input_line_pointer
;
1258 if (*input_line_pointer
!= ',')
1260 as_warn ("Expected comma after name");
1261 ignore_rest_of_line ();
1264 input_line_pointer
++;
1265 if ((temp
= get_absolute_expression ()) < 0)
1267 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1268 ignore_rest_of_line ();
1272 symbolP
= symbol_find_or_make (name
);
1274 if (*input_line_pointer
== ',')
1276 input_line_pointer
++;
1277 bss_align
= get_absolute_expression ();
1282 if (!S_IS_DEFINED(symbolP
)
1283 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1288 segT current_seg
= now_seg
;
1289 subsegT current_subseg
= now_subseg
;
1291 subseg_set (SEG_BSS
, 1); /* switch to bss */
1294 frag_align (bss_align
, 0);
1296 /* detach from old frag */
1297 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1298 symbolP
->sy_frag
->fr_symbol
= NULL
;
1300 symbolP
->sy_frag
= frag_now
;
1301 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1304 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1306 subseg_set (current_seg
, current_subseg
);
1311 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1314 while (!is_end_of_line
[*input_line_pointer
])
1316 input_line_pointer
++;
1324 /* These functions are needed if we are linking with obj-coffbfd.c.
1325 That file may be replaced by a more BFD oriented version at some
1326 point. If that happens, these functions should be rexamined.
1328 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1330 /* Given a fixS structure (created by a call to fix_new, above),
1331 return the BFD relocation type to use for it. */
1334 tc_coff_fix2rtype (fixp
)
1337 switch (fixp
->fx_r_type
)
1356 /* Apply a fixS to the object file. Since COFF does not use addends
1357 in relocs, the addend is actually stored directly in the object
1361 md_apply_fix (fixp
, val
)
1367 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1368 fixp
->fx_offset
= 0;
1370 switch (fixp
->fx_r_type
)
1373 fixp
->fx_offset
= val
>> 16;
1379 fixp
->fx_offset
= val
>> 16;
1385 fixp
->fx_offset
= val
>> 16;
1396 buf
[0] |= (val
>> 26) & 0x03;
1414 /* Where a PC relative offset is calculated from. On the m88k they
1415 are calculated from just after the instruction. */
1418 md_pcrel_from (fixp
)
1421 switch (fixp
->fx_r_type
)
1424 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1426 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1433 #endif /* M88KCOFF */