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. */
185 const char *retval
= NULL
;
188 /* initialize hash table */
190 op_hash
= hash_new ();
192 /* loop until you see the end of the list */
194 while (*m88k_opcodes
[i
].name
)
196 char *name
= m88k_opcodes
[i
].name
;
198 /* hash each mnemonic and record its position */
200 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
203 as_fatal ("Can't hash instruction '%s':%s",
204 m88k_opcodes
[i
].name
, retval
);
206 /* skip to next unique mnemonic or end of list */
208 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
213 CONST
char *md_shortopts
= "";
214 struct option md_longopts
[] = {
215 {NULL
, no_argument
, NULL
, 0}
217 size_t md_longopts_size
= sizeof(md_longopts
);
220 md_parse_option (c
, arg
)
228 md_show_usage (stream
)
237 char *param
, *thisfrag
;
239 struct m88k_opcode
*format
;
240 struct m88k_insn insn
;
244 /* skip over instruction to find parameters */
246 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
251 /* try to find the instruction in the hash table */
253 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
255 as_bad ("Invalid mnemonic '%s'", op
);
259 /* try parsing this instruction into insn */
261 insn
.exp
.X_add_symbol
= 0;
262 insn
.exp
.X_op_symbol
= 0;
263 insn
.exp
.X_add_number
= 0;
264 insn
.exp
.X_op
= O_illegal
;
265 insn
.reloc
= NO_RELOC
;
267 while (!calcop (format
, param
, &insn
))
269 /* if it doesn't parse try the next instruction */
271 if (!strcmp (format
[0].name
, format
[1].name
))
275 as_fatal ("Parameter syntax error");
280 /* grow the current frag and plop in the opcode */
282 thisfrag
= frag_more (4);
283 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
285 /* if this instruction requires labels mark it for later */
294 fix_new_exp (frag_now
,
295 thisfrag
- frag_now
->fr_literal
+ 2,
303 fix_new_exp (frag_now
,
304 thisfrag
- frag_now
->fr_literal
,
312 fix_new_exp (frag_now
,
313 thisfrag
- frag_now
->fr_literal
+ 2,
321 fix_new_exp (frag_now
,
322 thisfrag
- frag_now
->fr_literal
,
330 as_fatal ("Unknown relocation type");
336 calcop (format
, param
, insn
)
337 struct m88k_opcode
*format
;
339 struct m88k_insn
*insn
;
341 char *fmt
= format
->op_spec
;
345 int reg_prefix
= 'r';
347 insn
->opcode
= format
->opcode
;
358 insn
->opcode
|= opcode
;
359 return (*param
== 0 || *param
== '\n');
367 param
= get_reg (param
, &val
, reg_prefix
);
373 param
= get_o6 (param
, &val
);
374 opcode
|= ((val
>> 2) << 7);
382 param
= get_reg (param
, &val
, reg_prefix
);
388 param
= get_reg (param
, &val
, reg_prefix
);
394 param
= get_reg (param
, &val
, 'r');
395 opcode
|= (val
<< 16) | val
;
399 param
= get_imm16 (param
, insn
);
403 param
= get_bf (param
, &val
);
408 param
= get_pcr (param
, insn
, RELOC_PC16
);
412 param
= get_pcr (param
, insn
, RELOC_PC26
);
416 param
= get_cmp (param
, &val
);
421 param
= get_cnd (param
, &val
);
426 param
= get_cr (param
, &val
);
431 param
= get_fcr (param
, &val
);
436 param
= get_vec9 (param
, &val
);
441 /* Having this here repeats the warning somtimes.
442 But can't we stand that? */
443 as_warn ("Use of obsolete instruction");
450 match_name (param
, assoc_tab
, valp
)
452 struct field_val_assoc
*assoc_tab
;
461 name
= assoc_tab
[i
].name
;
464 name_len
= strlen (name
);
465 if (!strncmp (param
, name
, name_len
))
467 *valp
= assoc_tab
[i
].val
;
468 return param
+ name_len
;
474 get_reg (param
, regnop
, reg_prefix
)
485 regno
= *param
++ - '0';
496 regno
= regno
* 10 + c
;
511 else if (c
== 's' && param
[0] == 'p')
521 get_imm16 (param
, insn
)
523 struct m88k_insn
*insn
;
525 enum reloc_type reloc
= NO_RELOC
;
529 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
534 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
539 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
545 save_ptr
= input_line_pointer
;
546 input_line_pointer
= param
;
547 expression (&insn
->exp
);
548 param
= input_line_pointer
;
549 input_line_pointer
= save_ptr
;
551 val
= insn
->exp
.X_add_number
;
553 if (insn
->exp
.X_op
== O_constant
)
555 /* Insert the value now, and reset reloc to NO_RELOC. */
556 if (reloc
== NO_RELOC
)
558 /* Warn about too big expressions if not surrounded by xx16. */
560 as_warn ("Expression truncated to 16 bits");
563 if (reloc
== RELOC_HI16
)
566 insn
->opcode
|= val
& 0xffff;
569 else if (reloc
== NO_RELOC
)
570 /* We accept a symbol even without lo16, hi16, etc, and assume
571 lo16 was intended. */
580 get_pcr (param
, insn
, reloc
)
582 struct m88k_insn
*insn
;
583 enum reloc_type reloc
;
585 char *saveptr
, *saveparam
;
587 saveptr
= input_line_pointer
;
588 input_line_pointer
= param
;
590 expression (&insn
->exp
);
592 saveparam
= input_line_pointer
;
593 input_line_pointer
= saveptr
;
595 /* Botch: We should relocate now if O_constant. */
602 get_cmp (param
, valp
)
611 param
= match_name (param
, cmpslot
, valp
);
618 save_ptr
= input_line_pointer
;
619 input_line_pointer
= param
;
620 val
= get_absolute_expression ();
621 param
= input_line_pointer
;
622 input_line_pointer
= save_ptr
;
626 as_warn ("Expression truncated to 5 bits");
636 get_cnd (param
, valp
)
642 if (isdigit (*param
))
644 param
= getval (param
, &val
);
648 as_warn ("Expression truncated to 5 bits");
654 if (isupper (*param
))
655 *param
= tolower (*param
);
657 if (isupper (param
[1]))
658 param
[1] = tolower (param
[1]);
660 param
= match_name (param
, cndmsk
, valp
);
689 else if (c
== bc
&& depth
<= 0)
696 get_bf_offset_expression (param
, offsetp
)
702 if (isalpha (param
[0]))
704 if (isupper (param
[0]))
705 param
[0] = tolower (param
[0]);
706 if (isupper (param
[1]))
707 param
[1] = tolower (param
[1]);
709 param
= match_name (param
, cmpslot
, offsetp
);
715 input_line_pointer
= param
;
716 offset
= get_absolute_expression ();
717 param
= input_line_pointer
;
734 xp
= get_bf2 (param
, '<');
736 save_ptr
= input_line_pointer
;
737 input_line_pointer
= param
;
740 /* We did not find '<'. We have an offset (width implicitly 32). */
741 param
= get_bf_offset_expression (param
, &offset
);
742 input_line_pointer
= save_ptr
;
748 *xp
++ = 0; /* Overwrite the '<' */
749 param
= get_bf2 (xp
, '>');
752 *param
++ = 0; /* Overwrite the '>' */
754 width
= get_absolute_expression ();
755 xp
= get_bf_offset_expression (xp
, &offset
);
756 input_line_pointer
= save_ptr
;
762 *valp
= ((width
% 32) << 5) | (offset
% 32);
768 get_cr (param
, regnop
)
775 if (!strncmp (param
, "cr", 2))
779 regno
= *param
++ - '0';
790 regno
= regno
* 10 + c
;
806 param
= match_name (param
, cr_regs
, regnop
);
812 get_fcr (param
, regnop
)
819 if (!strncmp (param
, "fcr", 3))
823 regno
= *param
++ - '0';
834 regno
= regno
* 10 + c
;
850 param
= match_name (param
, fcr_regs
, regnop
);
856 get_vec9 (param
, valp
)
863 save_ptr
= input_line_pointer
;
864 input_line_pointer
= param
;
865 val
= get_absolute_expression ();
866 param
= input_line_pointer
;
867 input_line_pointer
= save_ptr
;
870 as_warn ("Expression truncated to 9 bits");
872 *valp
= val
% (1 << 9);
885 save_ptr
= input_line_pointer
;
886 input_line_pointer
= param
;
887 val
= get_absolute_expression ();
888 param
= input_line_pointer
;
889 input_line_pointer
= save_ptr
;
892 as_warn ("Removed lower 2 bits of expression");
900 (isdigit (z) ? (z) - '0' : \
901 islower (z) ? (z) - 'a' + 10 : \
902 isupper (z) ? (z) - 'A' + 10 : -1)
909 unsigned int val
= 0;
916 if (c
== 'x' || c
== 'X')
952 md_number_to_chars (buf
, val
, nbytes
)
957 number_to_chars_bigendian (buf
, val
, nbytes
);
962 /* This routine is never called. What is it for?
963 Ian Taylor, Cygnus Support 13 Jul 1993 */
966 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
973 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
992 switch (fixP
->fx_r_type
)
1017 buf
[0] |= (val
>> 26) & 0x03;
1031 as_fatal ("Bad relocation type");
1039 md_number_to_disp (buf
, val
, nbytes
)
1044 as_fatal ("md_number_to_disp not defined");
1045 md_number_to_chars (buf
, val
, nbytes
);
1049 md_number_to_field (buf
, val
, nbytes
)
1054 as_fatal ("md_number_to_field not defined");
1055 md_number_to_chars (buf
, val
, nbytes
);
1058 #define MAX_LITTLENUMS 6
1060 /* Turn a string in input_line_pointer into a floating point constant of type
1061 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1062 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1065 md_atof (type
, litP
, sizeP
)
1071 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1072 LITTLENUM_TYPE
*wordP
;
1104 return "Bad call to MD_ATOF()";
1106 t
= atof_ieee (input_line_pointer
, type
, words
);
1108 input_line_pointer
= t
;
1110 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1111 for (wordP
= words
; prec
--;)
1113 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1114 litP
+= sizeof (LITTLENUM_TYPE
);
1119 int md_short_jump_size
= 4;
1122 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1124 addressT from_addr
, to_addr
;
1128 ptr
[0] = (char) 0xc0;
1133 ptr
- frag
->fr_literal
,
1138 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1141 int md_long_jump_size
= 4;
1144 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1146 addressT from_addr
, to_addr
;
1150 ptr
[0] = (char) 0xc0;
1155 ptr
- frag
->fr_literal
,
1164 md_estimate_size_before_relax (fragP
, segment_type
)
1168 as_fatal ("Relaxation should never occur");
1174 /* As far as I can tell, this routine is never called. What is it
1176 Ian Taylor, Cygnus Support 13 Jul 1993 */
1180 * Risc relocations are completely different, so it needs
1181 * this machine dependent routine to emit them.
1184 emit_relocations (fixP
, segment_address_in_file
)
1186 relax_addressT segment_address_in_file
;
1188 struct reloc_info_m88k ri
;
1190 extern char *next_object_file_charP
;
1192 bzero ((char *) &ri
, sizeof (ri
));
1193 for (; fixP
; fixP
= fixP
->fx_next
)
1195 if (fixP
->fx_r_type
>= NO_RELOC
)
1197 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1201 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1203 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1204 fixP
->fx_where
- segment_address_in_file
;
1205 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1208 ri
.r_symbolnum
= symbolP
->sy_number
;
1213 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1215 if (symbolP
&& symbolP
->sy_frag
)
1217 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1219 ri
.r_type
= fixP
->fx_r_type
;
1222 ri
.r_addend
-= ri
.r_address
;
1226 ri
.r_addend
= fixP
->fx_addnumber
;
1229 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1238 /* This routine can be subsumed by s_lcomm in read.c.
1239 Ian Taylor, Cygnus Support 13 Jul 1993 */
1248 int temp
, bss_align
;
1251 name
= input_line_pointer
;
1252 c
= get_symbol_end ();
1253 p
= input_line_pointer
;
1256 if (*input_line_pointer
!= ',')
1258 as_warn ("Expected comma after name");
1259 ignore_rest_of_line ();
1262 input_line_pointer
++;
1263 if ((temp
= get_absolute_expression ()) < 0)
1265 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1266 ignore_rest_of_line ();
1270 symbolP
= symbol_find_or_make (name
);
1272 if (*input_line_pointer
== ',')
1274 input_line_pointer
++;
1275 bss_align
= get_absolute_expression ();
1280 if (!S_IS_DEFINED(symbolP
)
1281 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1286 segT current_seg
= now_seg
;
1287 subsegT current_subseg
= now_subseg
;
1289 subseg_set (SEG_BSS
, 1); /* switch to bss */
1292 frag_align (bss_align
, 0);
1294 /* detach from old frag */
1295 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1296 symbolP
->sy_frag
->fr_symbol
= NULL
;
1298 symbolP
->sy_frag
= frag_now
;
1299 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1302 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1304 subseg_set (current_seg
, current_subseg
);
1309 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1312 while (!is_end_of_line
[*input_line_pointer
])
1314 input_line_pointer
++;
1322 /* These functions are needed if we are linking with obj-coffbfd.c.
1323 That file may be replaced by a more BFD oriented version at some
1324 point. If that happens, these functions should be rexamined.
1326 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1328 /* Given a fixS structure (created by a call to fix_new, above),
1329 return the BFD relocation type to use for it. */
1332 tc_coff_fix2rtype (fixp
)
1335 switch (fixp
->fx_r_type
)
1354 /* Apply a fixS to the object file. Since COFF does not use addends
1355 in relocs, the addend is actually stored directly in the object
1359 md_apply_fix (fixp
, val
)
1365 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1366 fixp
->fx_offset
= 0;
1368 switch (fixp
->fx_r_type
)
1371 fixp
->fx_offset
= val
>> 16;
1377 fixp
->fx_offset
= val
>> 16;
1383 fixp
->fx_offset
= val
>> 16;
1394 buf
[0] |= (val
>> 26) & 0x03;
1412 /* Where a PC relative offset is calculated from. On the m88k they
1413 are calculated from just after the instruction. */
1416 md_pcrel_from (fixp
)
1419 switch (fixp
->fx_r_type
)
1422 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1424 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1431 #endif /* M88KCOFF */