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 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"
37 struct field_val_assoc
43 struct field_val_assoc cr_regs
[] =
70 struct field_val_assoc fcr_regs
[] =
88 struct field_val_assoc cmpslot
[] =
90 /* Integer Floating point */
100 {"ls", 9}, {"ib", 9},
101 {"lo", 10}, {"in", 10},
102 {"hs", 11}, {"ob", 11},
103 {"be", 12}, {"ue", 12},
104 {"nb", 13}, {"lg", 13},
105 {"he", 14}, {"ug", 14},
106 {"nh", 15}, {"ule", 15},
113 struct field_val_assoc cndmsk
[] =
127 unsigned long opcode
;
129 enum reloc_type reloc
;
133 static struct hash_control
*op_hash
= NULL
;
135 /* These bits should be turned off in the first address of every segment */
136 int md_seg_align
= 7;
138 /* These chars start a comment anywhere in a source file (except inside
140 const char comment_chars
[] = ";";
142 /* These chars only start a comment at the beginning of a line. */
143 const char line_comment_chars
[] = "#";
145 const char line_separator_chars
[] = "";
147 /* Chars that can be used to separate mant from exp in floating point nums */
148 const char EXP_CHARS
[] = "eE";
150 /* Chars that mean this number is a floating point constant */
151 /* as in 0f123.456 */
152 /* or 0H1.234E-12 (see exp chars above) */
153 const char FLT_CHARS
[] = "dDfF";
155 extern void float_cons (), cons (), s_globl (), s_space (),
156 s_set (), s_lcomm ();
157 static void s_file ();
159 const pseudo_typeS md_pseudo_table
[] =
161 {"align", s_align_bytes
, 4},
163 {"dfloat", float_cons
, 'd'},
164 {"ffloat", float_cons
, 'f'},
165 {"global", s_globl
, 0},
168 {"string", stringer
, 0},
170 {"zero", s_space
, 0},
180 /* initialize hash table */
182 op_hash
= hash_new ();
184 as_fatal ("Could not initialize hash table");
186 /* loop until you see the end of the list */
188 while (*m88k_opcodes
[i
].name
)
190 char *name
= m88k_opcodes
[i
].name
;
192 /* hash each mnemonic and record its position */
194 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
196 if (retval
!= NULL
&& *retval
!= '\0')
197 as_fatal ("Can't hash instruction '%s':%s",
198 m88k_opcodes
[i
].name
, retval
);
200 /* skip to next unique mnemonic or end of list */
202 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
208 md_parse_option (argP
, cntP
, vecP
)
220 char *param
, *thisfrag
;
222 struct m88k_opcode
*format
;
223 struct m88k_insn insn
;
227 /* skip over instruction to find parameters */
229 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
234 /* try to find the instruction in the hash table */
236 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
238 extern struct hash_control
*po_hash
;
242 /* The m88k assembler does not use `.' before pseudo-ops, for
243 some reason. So if don't find an opcode, try for a
245 pop
= (pseudo_typeS
*) hash_find (po_hash
, op
);
249 as_bad ("Invalid mnemonic '%s'", op
);
253 /* Restore the character after the opcode. */
256 /* Now we have to hack. The pseudo-op code expects
257 input_line_pointer to point to the first non-whitespace
258 character after the pseudo-op itself. The calling code has
259 already advanced input_line_pointer to the end of the line
260 and inserted a null byte. We set things up for the pseudo-op
261 code, and then prepare to return from this function. */
262 hold
= input_line_pointer
;
264 input_line_pointer
= param
;
267 (*pop
->poc_handler
) (pop
->poc_val
);
269 input_line_pointer
= hold
;
274 /* try parsing this instruction into insn */
276 insn
.exp
.X_add_symbol
= 0;
277 insn
.exp
.X_subtract_symbol
= 0;
278 insn
.exp
.X_add_number
= 0;
280 insn
.reloc
= NO_RELOC
;
282 while (!calcop (format
, param
, &insn
))
284 /* if it doesn't parse try the next instruction */
286 if (!strcmp (format
[0].name
, format
[1].name
))
290 as_fatal ("Parameter syntax error");
295 /* grow the current frag and plop in the opcode */
297 thisfrag
= frag_more (4);
298 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
300 /* if this instruction requires labels mark it for later */
310 thisfrag
- frag_now
->fr_literal
+ 2,
312 insn
.exp
.X_add_symbol
,
313 insn
.exp
.X_subtract_symbol
,
314 insn
.exp
.X_add_number
,
321 thisfrag
- frag_now
->fr_literal
,
323 insn
.exp
.X_add_symbol
,
324 insn
.exp
.X_subtract_symbol
,
325 insn
.exp
.X_add_number
,
332 thisfrag
- frag_now
->fr_literal
+ 2,
334 insn
.exp
.X_add_symbol
,
335 insn
.exp
.X_subtract_symbol
,
336 insn
.exp
.X_add_number
,
343 thisfrag
- frag_now
->fr_literal
,
345 insn
.exp
.X_add_symbol
,
346 insn
.exp
.X_subtract_symbol
,
347 insn
.exp
.X_add_number
,
353 as_fatal ("Unknown relocation type");
359 calcop (format
, param
, insn
)
360 struct m88k_opcode
*format
;
362 struct m88k_insn
*insn
;
364 char *fmt
= format
->op_spec
;
368 int reg_prefix
= 'r';
370 insn
->opcode
= format
->opcode
;
381 insn
->opcode
|= opcode
;
390 param
= get_reg (param
, &val
, reg_prefix
);
400 param
= get_reg (param
, &val
, reg_prefix
);
406 param
= get_reg (param
, &val
, reg_prefix
);
412 param
= get_reg (param
, &val
, 'r');
413 opcode
|= (val
<< 16) | val
;
417 param
= get_imm16 (param
, insn
);
421 param
= get_bf (param
, &val
);
426 param
= get_pcr (param
, insn
, RELOC_PC16
);
430 param
= get_pcr (param
, insn
, RELOC_PC26
);
434 param
= get_cmp (param
, &val
);
439 param
= get_cnd (param
, &val
);
444 param
= get_cr (param
, &val
);
449 param
= get_fcr (param
, &val
);
454 param
= get_vec9 (param
, &val
);
459 /* Having this here repeats the warning somtimes.
460 But can't we stand that? */
461 as_warn ("Use of obsolete instruction");
468 match_name (param
, assoc_tab
, valp
)
470 struct field_val_assoc
*assoc_tab
;
479 name
= assoc_tab
[i
].name
;
482 name_len
= strlen (name
);
483 if (!strncmp (param
, name
, name_len
))
485 *valp
= assoc_tab
[i
].val
;
486 return param
+ name_len
;
492 get_reg (param
, regnop
, reg_prefix
)
503 regno
= *param
++ - '0';
514 regno
= regno
* 10 + c
;
529 else if (c
== 's' && param
[0] == 'p')
539 get_imm16 (param
, insn
)
541 struct m88k_insn
*insn
;
543 enum reloc_type reloc
= NO_RELOC
;
548 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
553 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
558 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
564 save_ptr
= input_line_pointer
;
565 input_line_pointer
= param
;
566 seg
= expression (&insn
->exp
);
567 param
= input_line_pointer
;
568 input_line_pointer
= save_ptr
;
570 val
= insn
->exp
.X_add_number
;
572 if (seg
== SEG_ABSOLUTE
)
574 /* Insert the value now, and reset reloc to NO_RELOC. */
575 if (reloc
== NO_RELOC
)
577 /* Warn about too big expressions if not surrounded by xx16. */
579 as_warn ("Expression truncated to 16 bits");
582 if (reloc
== RELOC_HI16
)
585 insn
->opcode
|= val
& 0xffff;
588 else if (reloc
== NO_RELOC
)
589 /* We accept a symbol even without lo16, hi16, etc, and assume
590 lo16 was intended. */
599 get_pcr (param
, insn
, reloc
)
601 struct m88k_insn
*insn
;
602 enum reloc_type reloc
;
604 char *saveptr
, *saveparam
;
607 saveptr
= input_line_pointer
;
608 input_line_pointer
= param
;
610 seg
= expression (&insn
->exp
);
612 saveparam
= input_line_pointer
;
613 input_line_pointer
= saveptr
;
615 /* Botch: We should relocate now if SEG_ABSOLUTE. */
622 get_cmp (param
, valp
)
631 param
= match_name (param
, cmpslot
, valp
);
638 save_ptr
= input_line_pointer
;
639 input_line_pointer
= param
;
640 val
= get_absolute_expression ();
641 param
= input_line_pointer
;
642 input_line_pointer
= save_ptr
;
646 as_warn ("Expression truncated to 5 bits");
656 get_cnd (param
, valp
)
662 if (isdigit (*param
))
664 param
= getval (param
, &val
);
668 as_warn ("Expression truncated to 5 bits");
674 if (isupper (*param
))
675 *param
= tolower (*param
);
677 if (isupper (param
[1]))
678 param
[1] = tolower (param
[1]);
680 param
= match_name (param
, cndmsk
, valp
);
709 else if (c
== bc
&& depth
<= 0)
716 get_bf_offset_expression (param
, offsetp
)
722 if (isalpha (param
[0]))
724 if (isupper (param
[0]))
725 param
[0] = tolower (param
[0]);
726 if (isupper (param
[1]))
727 param
[1] = tolower (param
[1]);
729 param
= match_name (param
, cmpslot
, offsetp
);
735 input_line_pointer
= param
;
736 offset
= get_absolute_expression ();
737 param
= input_line_pointer
;
754 xp
= get_bf2 (param
, '<');
756 save_ptr
= input_line_pointer
;
757 input_line_pointer
= param
;
760 /* We did not find '<'. We have an offset (width implicitly 32). */
761 param
= get_bf_offset_expression (param
, &offset
);
762 input_line_pointer
= save_ptr
;
768 *xp
++ = 0; /* Overwrite the '<' */
769 param
= get_bf2 (xp
, '>');
772 *param
++ = 0; /* Overwrite the '>' */
774 width
= get_absolute_expression ();
775 xp
= get_bf_offset_expression (xp
, &offset
);
776 input_line_pointer
= save_ptr
;
782 *valp
= ((width
% 32) << 5) | (offset
% 32);
788 get_cr (param
, regnop
)
797 if (!strncmp (param
, "cr", 2))
801 regno
= *param
++ - '0';
812 regno
= regno
* 10 + c
;
828 param
= match_name (param
, cr_regs
, regnop
);
834 get_fcr (param
, regnop
)
843 if (!strncmp (param
, "fcr", 3))
847 regno
= *param
++ - '0';
858 regno
= regno
* 10 + c
;
874 param
= match_name (param
, fcr_regs
, regnop
);
880 get_vec9 (param
, valp
)
887 save_ptr
= input_line_pointer
;
888 input_line_pointer
= param
;
889 val
= get_absolute_expression ();
890 param
= input_line_pointer
;
891 input_line_pointer
= save_ptr
;
894 as_warn ("Expression truncated to 9 bits");
896 *valp
= val
% (1 << 9);
902 (isdigit (z) ? (z) - '0' : \
903 islower (z) ? (z) - 'a' + 10 : \
904 isupper (z) ? (z) - 'A' + 10 : -1)
911 unsigned int val
= 0;
918 if (c
== 'x' || c
== 'X')
954 md_number_to_chars (buf
, val
, nbytes
)
977 /* This routine is never called. What is it for?
978 Ian Taylor, Cygnus Support 13 Jul 1993 */
981 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
988 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
1007 switch (fixP
->fx_r_type
)
1032 buf
[0] |= (val
>> 26) & 0x03;
1046 as_fatal ("Bad relocation type");
1054 md_number_to_disp (buf
, val
, nbytes
)
1059 as_fatal ("md_number_to_disp not defined");
1060 md_number_to_chars (buf
, val
, nbytes
);
1064 md_number_to_field (buf
, val
, nbytes
)
1069 as_fatal ("md_number_to_field not defined");
1070 md_number_to_chars (buf
, val
, nbytes
);
1073 #define MAX_LITTLENUMS 6
1075 /* Turn a string in input_line_pointer into a floating point constant of type
1076 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1077 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1080 md_atof (type
, litP
, sizeP
)
1086 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1087 LITTLENUM_TYPE
*wordP
;
1119 return "Bad call to MD_ATOF()";
1121 t
= atof_ieee (input_line_pointer
, type
, words
);
1123 input_line_pointer
= t
;
1125 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1126 for (wordP
= words
; prec
--;)
1128 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1129 litP
+= sizeof (LITTLENUM_TYPE
);
1131 return ""; /* Someone should teach Dean about null pointers */
1134 int md_short_jump_size
= 4;
1137 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1139 addressT from_addr
, to_addr
;
1143 ptr
[0] = (char) 0xc0;
1148 ptr
- frag
->fr_literal
,
1154 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1157 int md_long_jump_size
= 4;
1160 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1162 addressT from_addr
, to_addr
;
1166 ptr
[0] = (char) 0xc0;
1171 ptr
- frag
->fr_literal
,
1181 md_estimate_size_before_relax (fragP
, segment_type
)
1185 as_fatal ("Relaxation should never occur");
1188 const relax_typeS md_relax_table
[] =
1198 /* As far as I can tell, this routine is never called. What is it
1200 Ian Taylor, Cygnus Support 13 Jul 1993 */
1204 * Risc relocations are completely different, so it needs
1205 * this machine dependent routine to emit them.
1208 emit_relocations (fixP
, segment_address_in_file
)
1210 relax_addressT segment_address_in_file
;
1212 struct reloc_info_m88k ri
;
1214 extern char *next_object_file_charP
;
1216 bzero ((char *) &ri
, sizeof (ri
));
1217 for (; fixP
; fixP
= fixP
->fx_next
)
1219 if (fixP
->fx_r_type
>= NO_RELOC
)
1221 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1225 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1227 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1228 fixP
->fx_where
- segment_address_in_file
;
1229 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1232 ri
.r_symbolnum
= symbolP
->sy_number
;
1237 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1239 if (symbolP
&& symbolP
->sy_frag
)
1241 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1243 ri
.r_type
= fixP
->fx_r_type
;
1246 ri
.r_addend
-= ri
.r_address
;
1250 ri
.r_addend
= fixP
->fx_addnumber
;
1253 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1263 /* This routine can be subsumed by s_lcomm in read.c.
1264 Ian Taylor, Cygnus Support 13 Jul 1993 */
1273 int temp
, bss_align
;
1276 name
= input_line_pointer
;
1277 c
= get_symbol_end ();
1278 p
= input_line_pointer
;
1281 if (*input_line_pointer
!= ',')
1283 as_warn ("Expected comma after name");
1284 ignore_rest_of_line ();
1287 input_line_pointer
++;
1288 if ((temp
= get_absolute_expression ()) < 0)
1290 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1291 ignore_rest_of_line ();
1295 symbolP
= symbol_find_or_make (name
);
1297 if (*input_line_pointer
== ',')
1299 input_line_pointer
++;
1300 bss_align
= get_absolute_expression ();
1305 if (!S_IS_DEFINED(symbolP
)
1306 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1311 segT current_seg
= now_seg
;
1312 subsegT current_subseg
= now_subseg
;
1314 subseg_new (SEG_BSS
, 1); /* switch to bss */
1317 frag_align (bss_align
, 0);
1319 /* detach from old frag */
1320 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1321 symbolP
->sy_frag
->fr_symbol
= NULL
;
1323 symbolP
->sy_frag
= frag_now
;
1324 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1327 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1329 subseg_new (current_seg
, current_subseg
);
1334 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1337 while (!is_end_of_line
[*input_line_pointer
])
1339 input_line_pointer
++;
1349 /* These functions are needed if we are linking with obj-coffbfd.c.
1350 That file may be replaced by a more BFD oriented version at some
1351 point. If that happens, these functions should be rexamined.
1353 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1355 /* Given a fixS structure (created by a call to fix_new, above),
1356 return the BFD relocation type to use for it. */
1359 tc_coff_fix2rtype (fixp
)
1362 switch (fixp
->fx_r_type
)
1381 /* Apply a fixS to the object file. Since COFF does not use addends
1382 in relocs, the addend is actually stored directly in the object
1386 md_apply_fix (fixp
, val
)
1392 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1394 switch (fixp
->fx_r_type
)
1417 buf
[0] |= (val
>> 26) & 0x03;
1435 /* Where a PC relative offset is calculated from. On the m88k they
1436 are calculated from just after the instruction. */
1439 md_pcrel_from (fixp
)
1442 switch (fixp
->fx_r_type
)
1445 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1447 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1454 #endif /* M88KCOFF */