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 /* This is the number to put at the beginning of the a.out file */
139 long omagic
= OMAGIC
;
141 /* These chars start a comment anywhere in a source file (except inside
143 const char comment_chars
[] = ";";
145 /* These chars only start a comment at the beginning of a line. */
146 const char line_comment_chars
[] = "#";
148 const char line_separator_chars
[] = "";
150 /* Chars that can be used to separate mant from exp in floating point nums */
151 const char EXP_CHARS
[] = "eE";
153 /* Chars that mean this number is a floating point constant */
154 /* as in 0f123.456 */
155 /* or 0H1.234E-12 (see exp chars above) */
156 const char FLT_CHARS
[] = "dDfF";
158 extern void float_cons (), cons (), s_globl (), s_space (),
159 s_set (), s_lcomm ();
160 static void s_file ();
162 const pseudo_typeS md_pseudo_table
[] =
164 {"align", s_align_bytes
, 4},
166 {"dfloat", float_cons
, 'd'},
167 {"ffloat", float_cons
, 'f'},
168 {"global", s_globl
, 0},
171 {"string", stringer
, 0},
173 {"zero", s_space
, 0},
183 /* initialize hash table */
185 op_hash
= hash_new ();
187 as_fatal ("Could not initialize hash table");
189 /* loop until you see the end of the list */
191 while (*m88k_opcodes
[i
].name
)
193 char *name
= m88k_opcodes
[i
].name
;
195 /* hash each mnemonic and record its position */
197 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
199 if (retval
!= NULL
&& *retval
!= '\0')
200 as_fatal ("Can't hash instruction '%s':%s",
201 m88k_opcodes
[i
].name
, retval
);
203 /* skip to next unique mnemonic or end of list */
205 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
211 md_parse_option (argP
, cntP
, vecP
)
223 char *param
, *thisfrag
;
225 struct m88k_opcode
*format
;
226 struct m88k_insn insn
;
230 /* skip over instruction to find parameters */
232 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
237 /* try to find the instruction in the hash table */
239 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
241 extern struct hash_control
*po_hash
;
245 /* The m88k assembler does not use `.' before pseudo-ops, for
246 some reason. So if don't find an opcode, try for a
248 pop
= (pseudo_typeS
*) hash_find (po_hash
, op
);
252 as_bad ("Invalid mnemonic '%s'", op
);
256 /* Restore the character after the opcode. */
259 /* Now we have to hack. The pseudo-op code expects
260 input_line_pointer to point to the first non-whitespace
261 character after the pseudo-op itself. The calling code has
262 already advanced input_line_pointer to the end of the line
263 and inserted a null byte. We set things up for the pseudo-op
264 code, and then prepare to return from this function. */
265 hold
= input_line_pointer
;
267 input_line_pointer
= param
;
270 (*pop
->poc_handler
) (pop
->poc_val
);
272 input_line_pointer
= hold
;
277 /* try parsing this instruction into insn */
279 insn
.exp
.X_add_symbol
= 0;
280 insn
.exp
.X_subtract_symbol
= 0;
281 insn
.exp
.X_add_number
= 0;
283 insn
.reloc
= NO_RELOC
;
285 while (!calcop (format
, param
, &insn
))
287 /* if it doesn't parse try the next instruction */
289 if (!strcmp (format
[0].name
, format
[1].name
))
293 as_fatal ("Parameter syntax error");
298 /* grow the current frag and plop in the opcode */
300 thisfrag
= frag_more (4);
301 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
303 /* if this instruction requires labels mark it for later */
313 thisfrag
- frag_now
->fr_literal
+ 2,
315 insn
.exp
.X_add_symbol
,
316 insn
.exp
.X_subtract_symbol
,
317 insn
.exp
.X_add_number
,
324 thisfrag
- frag_now
->fr_literal
,
326 insn
.exp
.X_add_symbol
,
327 insn
.exp
.X_subtract_symbol
,
328 insn
.exp
.X_add_number
,
335 thisfrag
- frag_now
->fr_literal
+ 2,
337 insn
.exp
.X_add_symbol
,
338 insn
.exp
.X_subtract_symbol
,
339 insn
.exp
.X_add_number
,
346 thisfrag
- frag_now
->fr_literal
,
348 insn
.exp
.X_add_symbol
,
349 insn
.exp
.X_subtract_symbol
,
350 insn
.exp
.X_add_number
,
356 as_fatal ("Unknown relocation type");
362 calcop (format
, param
, insn
)
363 struct m88k_opcode
*format
;
365 struct m88k_insn
*insn
;
367 char *fmt
= format
->op_spec
;
371 int reg_prefix
= 'r';
373 insn
->opcode
= format
->opcode
;
384 insn
->opcode
|= opcode
;
393 param
= get_reg (param
, &val
, reg_prefix
);
403 param
= get_reg (param
, &val
, reg_prefix
);
409 param
= get_reg (param
, &val
, reg_prefix
);
415 param
= get_reg (param
, &val
, 'r');
416 opcode
|= (val
<< 16) | val
;
420 param
= get_imm16 (param
, insn
);
424 param
= get_bf (param
, &val
);
429 param
= get_pcr (param
, insn
, RELOC_PC16
);
433 param
= get_pcr (param
, insn
, RELOC_PC26
);
437 param
= get_cmp (param
, &val
);
442 param
= get_cnd (param
, &val
);
447 param
= get_cr (param
, &val
);
452 param
= get_fcr (param
, &val
);
457 param
= get_vec9 (param
, &val
);
462 /* Having this here repeats the warning somtimes.
463 But can't we stand that? */
464 as_warn ("Use of obsolete instruction");
471 match_name (param
, assoc_tab
, valp
)
473 struct field_val_assoc
*assoc_tab
;
482 name
= assoc_tab
[i
].name
;
485 name_len
= strlen (name
);
486 if (!strncmp (param
, name
, name_len
))
488 *valp
= assoc_tab
[i
].val
;
489 return param
+ name_len
;
495 get_reg (param
, regnop
, reg_prefix
)
506 regno
= *param
++ - '0';
517 regno
= regno
* 10 + c
;
532 else if (c
== 's' && param
[0] == 'p')
542 get_imm16 (param
, insn
)
544 struct m88k_insn
*insn
;
546 enum reloc_type reloc
= NO_RELOC
;
551 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
556 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
561 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
567 save_ptr
= input_line_pointer
;
568 input_line_pointer
= param
;
569 seg
= expression (&insn
->exp
);
570 param
= input_line_pointer
;
571 input_line_pointer
= save_ptr
;
573 val
= insn
->exp
.X_add_number
;
575 if (seg
== SEG_ABSOLUTE
)
577 /* Insert the value now, and reset reloc to NO_RELOC. */
578 if (reloc
== NO_RELOC
)
580 /* Warn about too big expressions if not surrounded by xx16. */
582 as_warn ("Expression truncated to 16 bits");
585 if (reloc
== RELOC_HI16
)
588 insn
->opcode
|= val
& 0xffff;
591 else if (reloc
== NO_RELOC
)
592 /* We accept a symbol even without lo16, hi16, etc, and assume
593 lo16 was intended. */
602 get_pcr (param
, insn
, reloc
)
604 struct m88k_insn
*insn
;
605 enum reloc_type reloc
;
607 char *saveptr
, *saveparam
;
610 saveptr
= input_line_pointer
;
611 input_line_pointer
= param
;
613 seg
= expression (&insn
->exp
);
615 saveparam
= input_line_pointer
;
616 input_line_pointer
= saveptr
;
618 /* Botch: We should relocate now if SEG_ABSOLUTE. */
625 get_cmp (param
, valp
)
634 param
= match_name (param
, cmpslot
, valp
);
641 save_ptr
= input_line_pointer
;
642 input_line_pointer
= param
;
643 val
= get_absolute_expression ();
644 param
= input_line_pointer
;
645 input_line_pointer
= save_ptr
;
649 as_warn ("Expression truncated to 5 bits");
659 get_cnd (param
, valp
)
665 if (isdigit (*param
))
667 param
= getval (param
, &val
);
671 as_warn ("Expression truncated to 5 bits");
677 if (isupper (*param
))
678 *param
= tolower (*param
);
680 if (isupper (param
[1]))
681 param
[1] = tolower (param
[1]);
683 param
= match_name (param
, cndmsk
, valp
);
712 else if (c
== bc
&& depth
<= 0)
719 get_bf_offset_expression (param
, offsetp
)
725 if (isalpha (param
[0]))
727 if (isupper (param
[0]))
728 param
[0] = tolower (param
[0]);
729 if (isupper (param
[1]))
730 param
[1] = tolower (param
[1]);
732 param
= match_name (param
, cmpslot
, offsetp
);
738 input_line_pointer
= param
;
739 offset
= get_absolute_expression ();
740 param
= input_line_pointer
;
757 xp
= get_bf2 (param
, '<');
759 save_ptr
= input_line_pointer
;
760 input_line_pointer
= param
;
763 /* We did not find '<'. We have an offset (width implicitly 32). */
764 param
= get_bf_offset_expression (param
, &offset
);
765 input_line_pointer
= save_ptr
;
771 *xp
++ = 0; /* Overwrite the '<' */
772 param
= get_bf2 (xp
, '>');
775 *param
++ = 0; /* Overwrite the '>' */
777 width
= get_absolute_expression ();
778 xp
= get_bf_offset_expression (xp
, &offset
);
779 input_line_pointer
= save_ptr
;
785 *valp
= ((width
% 32) << 5) | (offset
% 32);
791 get_cr (param
, regnop
)
800 if (!strncmp (param
, "cr", 2))
804 regno
= *param
++ - '0';
815 regno
= regno
* 10 + c
;
831 param
= match_name (param
, cr_regs
, regnop
);
837 get_fcr (param
, regnop
)
846 if (!strncmp (param
, "fcr", 3))
850 regno
= *param
++ - '0';
861 regno
= regno
* 10 + c
;
877 param
= match_name (param
, fcr_regs
, regnop
);
883 get_vec9 (param
, valp
)
890 save_ptr
= input_line_pointer
;
891 input_line_pointer
= param
;
892 val
= get_absolute_expression ();
893 param
= input_line_pointer
;
894 input_line_pointer
= save_ptr
;
897 as_warn ("Expression truncated to 9 bits");
899 *valp
= val
% (1 << 9);
905 (isdigit (z) ? (z) - '0' : \
906 islower (z) ? (z) - 'a' + 10 : \
907 isupper (z) ? (z) - 'A' + 10 : -1)
914 unsigned int val
= 0;
921 if (c
== 'x' || c
== 'X')
957 md_number_to_chars (buf
, val
, nbytes
)
980 /* This routine is never called. What is it for?
981 Ian Taylor, Cygnus Support 13 Jul 1993 */
984 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
991 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
1010 switch (fixP
->fx_r_type
)
1035 buf
[0] |= (val
>> 26) & 0x03;
1049 as_fatal ("Bad relocation type");
1057 md_number_to_disp (buf
, val
, nbytes
)
1062 as_fatal ("md_number_to_disp not defined");
1063 md_number_to_chars (buf
, val
, nbytes
);
1067 md_number_to_field (buf
, val
, nbytes
)
1072 as_fatal ("md_number_to_field not defined");
1073 md_number_to_chars (buf
, val
, nbytes
);
1076 #define MAX_LITTLENUMS 6
1078 /* Turn a string in input_line_pointer into a floating point constant of type
1079 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1080 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1083 md_atof (type
, litP
, sizeP
)
1089 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1090 LITTLENUM_TYPE
*wordP
;
1122 return "Bad call to MD_ATOF()";
1124 t
= atof_ieee (input_line_pointer
, type
, words
);
1126 input_line_pointer
= t
;
1128 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1129 for (wordP
= words
; prec
--;)
1131 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1132 litP
+= sizeof (LITTLENUM_TYPE
);
1134 return ""; /* Someone should teach Dean about null pointers */
1137 int md_short_jump_size
= 4;
1140 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1142 addressT from_addr
, to_addr
;
1146 ptr
[0] = (char) 0xc0;
1151 ptr
- frag
->fr_literal
,
1157 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1160 int md_long_jump_size
= 4;
1163 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1165 addressT from_addr
, to_addr
;
1169 ptr
[0] = (char) 0xc0;
1174 ptr
- frag
->fr_literal
,
1184 md_estimate_size_before_relax (fragP
, segment_type
)
1188 as_fatal ("Relaxation should never occur");
1191 const relax_typeS md_relax_table
[] =
1201 /* As far as I can tell, this routine is never called. What is it
1203 Ian Taylor, Cygnus Support 13 Jul 1993 */
1207 * Risc relocations are completely different, so it needs
1208 * this machine dependent routine to emit them.
1211 emit_relocations (fixP
, segment_address_in_file
)
1213 relax_addressT segment_address_in_file
;
1215 struct reloc_info_m88k ri
;
1217 extern char *next_object_file_charP
;
1219 bzero ((char *) &ri
, sizeof (ri
));
1220 for (; fixP
; fixP
= fixP
->fx_next
)
1222 if (fixP
->fx_r_type
>= NO_RELOC
)
1224 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1228 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1230 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1231 fixP
->fx_where
- segment_address_in_file
;
1232 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1235 ri
.r_symbolnum
= symbolP
->sy_number
;
1240 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1242 if (symbolP
&& symbolP
->sy_frag
)
1244 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1246 ri
.r_type
= fixP
->fx_r_type
;
1249 ri
.r_addend
-= ri
.r_address
;
1253 ri
.r_addend
= fixP
->fx_addnumber
;
1256 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1266 /* This routine can be subsumed by s_lcomm in read.c.
1267 Ian Taylor, Cygnus Support 13 Jul 1993 */
1276 int temp
, bss_align
;
1279 name
= input_line_pointer
;
1280 c
= get_symbol_end ();
1281 p
= input_line_pointer
;
1284 if (*input_line_pointer
!= ',')
1286 as_warn ("Expected comma after name");
1287 ignore_rest_of_line ();
1290 input_line_pointer
++;
1291 if ((temp
= get_absolute_expression ()) < 0)
1293 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1294 ignore_rest_of_line ();
1298 symbolP
= symbol_find_or_make (name
);
1300 if (*input_line_pointer
== ',')
1302 input_line_pointer
++;
1303 bss_align
= get_absolute_expression ();
1308 if (!S_IS_DEFINED(symbolP
)
1309 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1314 segT current_seg
= now_seg
;
1315 subsegT current_subseg
= now_subseg
;
1317 subseg_new (SEG_BSS
, 1); /* switch to bss */
1320 frag_align (bss_align
, 0);
1322 /* detach from old frag */
1323 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1324 symbolP
->sy_frag
->fr_symbol
= NULL
;
1326 symbolP
->sy_frag
= frag_now
;
1327 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1330 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1332 subseg_new (current_seg
, current_subseg
);
1337 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1340 while (!is_end_of_line
[*input_line_pointer
])
1342 input_line_pointer
++;
1352 /* These functions are needed if we are linking with obj-coffbfd.c.
1353 That file may be replaced by a more BFD oriented version at some
1354 point. If that happens, these functions should be rexamined.
1356 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1358 /* Given a fixS structure (created by a call to fix_new, above),
1359 return the BFD relocation type to use for it. */
1362 tc_coff_fix2rtype (fixp
)
1365 switch (fixp
->fx_r_type
)
1384 /* Apply a fixS to the object file. Since COFF does not use addends
1385 in relocs, the addend is actually stored directly in the object
1389 md_apply_fix (fixp
, val
)
1395 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1397 switch (fixp
->fx_r_type
)
1420 buf
[0] |= (val
>> 26) & 0x03;
1438 /* Where a PC relative offset is calculated from. On the m88k they
1439 are calculated from just after the instruction. */
1442 md_pcrel_from (fixp
)
1445 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
+ 4;
1448 #endif /* M88KCOFF */