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 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
6 Free Software Foundation, Inc.
8 This file is part of GAS, the GNU Assembler.
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
26 #include "safe-ctype.h"
28 #include "m88k-opcode.h"
30 struct field_val_assoc
36 struct field_val_assoc cr_regs
[] =
63 struct field_val_assoc fcr_regs
[] =
81 struct field_val_assoc cmpslot
[] =
83 /* Integer Floating point */
94 {"lo", 10}, {"in", 10},
95 {"hs", 11}, {"ob", 11},
96 {"be", 12}, {"ue", 12},
97 {"nb", 13}, {"lg", 13},
98 {"he", 14}, {"ug", 14},
99 {"nh", 15}, {"ule", 15},
106 struct field_val_assoc cndmsk
[] =
120 unsigned long opcode
;
122 enum reloc_type reloc
;
125 static char *get_bf
PARAMS ((char *param
, unsigned *valp
));
126 static char *get_cmp
PARAMS ((char *param
, unsigned *valp
));
127 static char *get_cnd
PARAMS ((char *param
, unsigned *valp
));
128 static char *get_bf2
PARAMS ((char *param
, int bc
));
129 static char *get_bf_offset_expression
PARAMS ((char *param
, unsigned *offsetp
));
130 static char *get_cr
PARAMS ((char *param
, unsigned *regnop
));
131 static char *get_fcr
PARAMS ((char *param
, unsigned *regnop
));
132 static char *get_imm16
PARAMS ((char *param
, struct m88k_insn
*insn
));
133 static char *get_o6
PARAMS ((char *param
, unsigned *valp
));
134 static char *match_name
PARAMS ((char *, struct field_val_assoc
*, unsigned *));
135 static char *get_reg
PARAMS ((char *param
, unsigned *regnop
, unsigned int reg_prefix
));
136 static char *get_vec9
PARAMS ((char *param
, unsigned *valp
));
137 static char *getval
PARAMS ((char *param
, unsigned int *valp
));
139 static char *get_pcr
PARAMS ((char *param
, struct m88k_insn
*insn
,
140 enum reloc_type reloc
));
142 static int calcop
PARAMS ((struct m88k_opcode
*format
,
143 char *param
, struct m88k_insn
*insn
));
146 static struct hash_control
*op_hash
= NULL
;
148 /* These bits should be turned off in the first address of every segment */
149 int md_seg_align
= 7;
151 /* These chars start a comment anywhere in a source file (except inside
153 const char comment_chars
[] = ";";
155 /* These chars only start a comment at the beginning of a line. */
156 const char line_comment_chars
[] = "#";
158 const char line_separator_chars
[] = "";
160 /* Chars that can be used to separate mant from exp in floating point nums */
161 const char EXP_CHARS
[] = "eE";
163 /* Chars that mean this number is a floating point constant */
164 /* as in 0f123.456 */
165 /* or 0H1.234E-12 (see exp chars above) */
166 const char FLT_CHARS
[] = "dDfF";
168 const pseudo_typeS md_pseudo_table
[] =
170 {"align", s_align_bytes
, 4},
172 {"dfloat", float_cons
, 'd'},
173 {"ffloat", float_cons
, 'f'},
176 {"string", stringer
, 0},
178 /* Force set to be treated as an instruction. */
187 const char *retval
= NULL
;
190 /* Initialize hash table. */
191 op_hash
= hash_new ();
193 while (*m88k_opcodes
[i
].name
)
195 char *name
= m88k_opcodes
[i
].name
;
197 /* Hash each mnemonic and record its position. */
198 retval
= hash_insert (op_hash
, name
, &m88k_opcodes
[i
]);
201 as_fatal (_("Can't hash instruction '%s':%s"),
202 m88k_opcodes
[i
].name
, retval
);
204 /* Skip to next unique mnemonic or end of list. */
205 for (i
++; !strcmp (m88k_opcodes
[i
].name
, name
); i
++)
210 const char *md_shortopts
= "";
211 struct option md_longopts
[] = {
212 {NULL
, no_argument
, NULL
, 0}
214 size_t md_longopts_size
= sizeof (md_longopts
);
217 md_parse_option (c
, arg
)
218 int c ATTRIBUTE_UNUSED
;
219 char *arg ATTRIBUTE_UNUSED
;
225 md_show_usage (stream
)
226 FILE *stream ATTRIBUTE_UNUSED
;
234 char *param
, *thisfrag
;
236 struct m88k_opcode
*format
;
237 struct m88k_insn insn
;
241 /* Skip over instruction to find parameters. */
242 for (param
= op
; *param
!= 0 && !ISSPACE (*param
); param
++)
247 /* Try to find the instruction in the hash table. */
248 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
250 as_bad (_("Invalid mnemonic '%s'"), op
);
254 /* Try parsing this instruction into insn. */
255 insn
.exp
.X_add_symbol
= 0;
256 insn
.exp
.X_op_symbol
= 0;
257 insn
.exp
.X_add_number
= 0;
258 insn
.exp
.X_op
= O_illegal
;
259 insn
.reloc
= NO_RELOC
;
261 while (!calcop (format
, param
, &insn
))
263 /* If it doesn't parse try the next instruction. */
264 if (!strcmp (format
[0].name
, format
[1].name
))
268 as_fatal (_("Parameter syntax error"));
273 /* Grow the current frag and plop in the opcode. */
274 thisfrag
= frag_more (4);
275 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
277 /* If this instruction requires labels mark it for later. */
285 fix_new_exp (frag_now
,
286 thisfrag
- frag_now
->fr_literal
+ 2,
294 fix_new_exp (frag_now
,
295 thisfrag
- frag_now
->fr_literal
,
303 fix_new_exp (frag_now
,
304 thisfrag
- frag_now
->fr_literal
+ 2,
312 fix_new_exp (frag_now
,
313 thisfrag
- frag_now
->fr_literal
,
321 as_fatal (_("Unknown relocation type"));
327 calcop (format
, param
, insn
)
328 struct m88k_opcode
*format
;
330 struct m88k_insn
*insn
;
332 char *fmt
= format
->op_spec
;
336 unsigned int reg_prefix
= 'r';
338 insn
->opcode
= format
->opcode
;
349 insn
->opcode
|= opcode
;
350 return (*param
== 0 || *param
== '\n');
358 param
= get_reg (param
, &val
, reg_prefix
);
364 param
= get_o6 (param
, &val
);
365 opcode
|= ((val
>> 2) << 7);
373 param
= get_reg (param
, &val
, reg_prefix
);
379 param
= get_reg (param
, &val
, reg_prefix
);
385 param
= get_reg (param
, &val
, 'r');
386 opcode
|= (val
<< 16) | val
;
390 param
= get_imm16 (param
, insn
);
394 param
= get_bf (param
, &val
);
399 param
= get_pcr (param
, insn
, RELOC_PC16
);
403 param
= get_pcr (param
, insn
, RELOC_PC26
);
407 param
= get_cmp (param
, &val
);
412 param
= get_cnd (param
, &val
);
417 param
= get_cr (param
, &val
);
422 param
= get_fcr (param
, &val
);
427 param
= get_vec9 (param
, &val
);
432 /* Having this here repeats the warning somtimes.
433 But can't we stand that? */
434 as_warn (_("Use of obsolete instruction"));
441 match_name (param
, assoc_tab
, valp
)
443 struct field_val_assoc
*assoc_tab
;
452 name
= assoc_tab
[i
].name
;
455 name_len
= strlen (name
);
456 if (!strncmp (param
, name
, name_len
))
458 *valp
= assoc_tab
[i
].val
;
459 return param
+ name_len
;
465 get_reg (param
, regnop
, reg_prefix
)
468 unsigned int reg_prefix
;
476 regno
= *param
++ - '0';
487 regno
= regno
* 10 + c
;
502 else if (c
== 's' && param
[0] == 'p')
512 get_imm16 (param
, insn
)
514 struct m88k_insn
*insn
;
516 enum reloc_type reloc
= NO_RELOC
;
520 if (!strncmp (param
, "hi16", 4) && !ISALNUM (param
[4]))
525 else if (!strncmp (param
, "lo16", 4) && !ISALNUM (param
[4]))
530 else if (!strncmp (param
, "iw16", 4) && !ISALNUM (param
[4]))
536 save_ptr
= input_line_pointer
;
537 input_line_pointer
= param
;
538 expression (&insn
->exp
);
539 param
= input_line_pointer
;
540 input_line_pointer
= save_ptr
;
542 val
= insn
->exp
.X_add_number
;
544 if (insn
->exp
.X_op
== O_constant
)
546 /* Insert the value now, and reset reloc to NO_RELOC. */
547 if (reloc
== NO_RELOC
)
549 /* Warn about too big expressions if not surrounded by xx16. */
551 as_warn (_("Expression truncated to 16 bits"));
554 if (reloc
== RELOC_HI16
)
557 insn
->opcode
|= val
& 0xffff;
560 else if (reloc
== NO_RELOC
)
561 /* We accept a symbol even without lo16, hi16, etc, and assume
562 lo16 was intended. */
571 get_pcr (param
, insn
, reloc
)
573 struct m88k_insn
*insn
;
574 enum reloc_type reloc
;
576 char *saveptr
, *saveparam
;
578 saveptr
= input_line_pointer
;
579 input_line_pointer
= param
;
581 expression (&insn
->exp
);
583 saveparam
= input_line_pointer
;
584 input_line_pointer
= saveptr
;
586 /* Botch: We should relocate now if O_constant. */
593 get_cmp (param
, valp
)
602 param
= match_name (param
, cmpslot
, valp
);
609 save_ptr
= input_line_pointer
;
610 input_line_pointer
= param
;
611 val
= get_absolute_expression ();
612 param
= input_line_pointer
;
613 input_line_pointer
= save_ptr
;
617 as_warn (_("Expression truncated to 5 bits"));
627 get_cnd (param
, valp
)
633 if (ISDIGIT (*param
))
635 param
= getval (param
, &val
);
639 as_warn (_("Expression truncated to 5 bits"));
645 param
[0] = TOLOWER (param
[0]);
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 param
[0] = TOLOWER (param
[0]);
693 param
[1] = TOLOWER (param
[1]);
695 param
= match_name (param
, cmpslot
, offsetp
);
701 input_line_pointer
= param
;
702 offset
= get_absolute_expression ();
703 param
= input_line_pointer
;
720 xp
= get_bf2 (param
, '<');
722 save_ptr
= input_line_pointer
;
723 input_line_pointer
= param
;
726 /* We did not find '<'. We have an offset (width implicitly 32). */
727 param
= get_bf_offset_expression (param
, &offset
);
728 input_line_pointer
= save_ptr
;
734 *xp
++ = 0; /* Overwrite the '<' */
735 param
= get_bf2 (xp
, '>');
738 *param
++ = 0; /* Overwrite the '>' */
740 width
= get_absolute_expression ();
741 xp
= get_bf_offset_expression (xp
, &offset
);
742 input_line_pointer
= save_ptr
;
748 *valp
= ((width
% 32) << 5) | (offset
% 32);
754 get_cr (param
, regnop
)
761 if (!strncmp (param
, "cr", 2))
765 regno
= *param
++ - '0';
776 regno
= regno
* 10 + c
;
792 param
= match_name (param
, cr_regs
, regnop
);
798 get_fcr (param
, regnop
)
805 if (!strncmp (param
, "fcr", 3))
809 regno
= *param
++ - '0';
820 regno
= regno
* 10 + c
;
836 param
= match_name (param
, fcr_regs
, regnop
);
842 get_vec9 (param
, valp
)
849 save_ptr
= input_line_pointer
;
850 input_line_pointer
= param
;
851 val
= get_absolute_expression ();
852 param
= input_line_pointer
;
853 input_line_pointer
= save_ptr
;
856 as_warn (_("Expression truncated to 9 bits"));
858 *valp
= val
% (1 << 9);
871 save_ptr
= input_line_pointer
;
872 input_line_pointer
= param
;
873 val
= get_absolute_expression ();
874 param
= input_line_pointer
;
875 input_line_pointer
= save_ptr
;
878 as_warn (_("Removed lower 2 bits of expression"));
886 (ISDIGIT (z) ? (z) - '0' : \
887 ISLOWER (z) ? (z) - 'a' + 10 : \
888 ISUPPER (z) ? (z) - 'A' + 10 : (unsigned) -1)
895 unsigned int val
= 0;
902 if (c
== 'x' || c
== 'X')
938 md_number_to_chars (buf
, val
, nbytes
)
943 number_to_chars_bigendian (buf
, val
, nbytes
);
946 #define MAX_LITTLENUMS 6
948 /* Turn a string in input_line_pointer into a floating point constant of type
949 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
950 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
953 md_atof (type
, litP
, sizeP
)
959 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
960 LITTLENUM_TYPE
*wordP
;
991 return _("Bad call to MD_ATOF()");
993 t
= atof_ieee (input_line_pointer
, type
, words
);
995 input_line_pointer
= t
;
997 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
998 for (wordP
= words
; prec
--;)
1000 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1001 litP
+= sizeof (LITTLENUM_TYPE
);
1006 int md_short_jump_size
= 4;
1009 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1011 addressT from_addr ATTRIBUTE_UNUSED
;
1012 addressT to_addr ATTRIBUTE_UNUSED
;
1016 ptr
[0] = (char) 0xc0;
1021 ptr
- frag
->fr_literal
,
1026 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1029 int md_long_jump_size
= 4;
1032 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1034 addressT from_addr ATTRIBUTE_UNUSED
;
1035 addressT to_addr ATTRIBUTE_UNUSED
;
1039 ptr
[0] = (char) 0xc0;
1044 ptr
- frag
->fr_literal
,
1053 md_estimate_size_before_relax (fragP
, segment_type
)
1054 fragS
*fragP ATTRIBUTE_UNUSED
;
1055 segT segment_type ATTRIBUTE_UNUSED
;
1057 as_fatal (_("Relaxation should never occur"));
1063 /* These functions are needed if we are linking with obj-coffbfd.c.
1064 That file may be replaced by a more BFD oriented version at some
1065 point. If that happens, these functions should be reexamined.
1067 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1069 /* Given a fixS structure (created by a call to fix_new, above),
1070 return the BFD relocation type to use for it. */
1073 tc_coff_fix2rtype (fixp
)
1076 switch (fixp
->fx_r_type
)
1095 /* Apply a fixS to the object file. Since COFF does not use addends
1096 in relocs, the addend is actually stored directly in the object
1100 md_apply_fix3 (fixP
, valP
, seg
)
1103 segT seg ATTRIBUTE_UNUSED
;
1105 long val
= * (long *) valP
;
1108 buf
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
1109 fixP
->fx_offset
= 0;
1111 switch (fixP
->fx_r_type
)
1114 fixP
->fx_offset
= val
>> 16;
1120 fixP
->fx_offset
= val
>> 16;
1126 fixP
->fx_offset
= val
>> 16;
1137 buf
[0] |= (val
>> 26) & 0x03;
1154 if (fixP
->fx_addsy
== NULL
&& fixP
->fx_pcrel
== 0)
1158 /* Where a PC relative offset is calculated from. On the m88k they
1159 are calculated from just after the instruction. */
1162 md_pcrel_from (fixp
)
1165 switch (fixp
->fx_r_type
)
1168 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1170 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1177 /* Fill in rs_align_code fragments. */
1180 m88k_handle_align (fragp
)
1183 static const unsigned char nop_pattern
[] = { 0xf4, 0x00, 0x58, 0x00 };
1188 if (fragp
->fr_type
!= rs_align_code
)
1191 bytes
= fragp
->fr_next
->fr_address
- fragp
->fr_address
- fragp
->fr_fix
;
1192 p
= fragp
->fr_literal
+ fragp
->fr_fix
;
1196 int fix
= bytes
& 3;
1200 fragp
->fr_fix
+= fix
;
1203 memcpy (p
, nop_pattern
, 4);
1207 #endif /* M88KCOFF */