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 {"zero", s_space
, 0},
177 /* 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
++)
214 md_parse_option (argP
, cntP
, vecP
)
226 char *param
, *thisfrag
;
228 struct m88k_opcode
*format
;
229 struct m88k_insn insn
;
233 /* skip over instruction to find parameters */
235 for (param
= op
; *param
!= 0 && !isspace (*param
); param
++)
240 /* try to find the instruction in the hash table */
242 if ((format
= (struct m88k_opcode
*) hash_find (op_hash
, op
)) == NULL
)
244 as_bad ("Invalid mnemonic '%s'", op
);
248 /* try parsing this instruction into insn */
250 insn
.exp
.X_add_symbol
= 0;
251 insn
.exp
.X_op_symbol
= 0;
252 insn
.exp
.X_add_number
= 0;
253 insn
.exp
.X_op
= O_illegal
;
254 insn
.reloc
= NO_RELOC
;
256 while (!calcop (format
, param
, &insn
))
258 /* if it doesn't parse try the next instruction */
260 if (!strcmp (format
[0].name
, format
[1].name
))
264 as_fatal ("Parameter syntax error");
269 /* grow the current frag and plop in the opcode */
271 thisfrag
= frag_more (4);
272 md_number_to_chars (thisfrag
, insn
.opcode
, 4);
274 /* if this instruction requires labels mark it for later */
283 fix_new_exp (frag_now
,
284 thisfrag
- frag_now
->fr_literal
+ 2,
292 fix_new_exp (frag_now
,
293 thisfrag
- frag_now
->fr_literal
,
301 fix_new_exp (frag_now
,
302 thisfrag
- frag_now
->fr_literal
+ 2,
310 fix_new_exp (frag_now
,
311 thisfrag
- frag_now
->fr_literal
,
319 as_fatal ("Unknown relocation type");
325 calcop (format
, param
, insn
)
326 struct m88k_opcode
*format
;
328 struct m88k_insn
*insn
;
330 char *fmt
= format
->op_spec
;
334 int reg_prefix
= 'r';
336 insn
->opcode
= format
->opcode
;
347 insn
->opcode
|= opcode
;
348 return (*param
== 0 || *param
== '\n');
356 param
= get_reg (param
, &val
, reg_prefix
);
362 param
= get_o6 (param
, &val
);
363 opcode
|= ((val
>> 2) << 7);
371 param
= get_reg (param
, &val
, reg_prefix
);
377 param
= get_reg (param
, &val
, reg_prefix
);
383 param
= get_reg (param
, &val
, 'r');
384 opcode
|= (val
<< 16) | val
;
388 param
= get_imm16 (param
, insn
);
392 param
= get_bf (param
, &val
);
397 param
= get_pcr (param
, insn
, RELOC_PC16
);
401 param
= get_pcr (param
, insn
, RELOC_PC26
);
405 param
= get_cmp (param
, &val
);
410 param
= get_cnd (param
, &val
);
415 param
= get_cr (param
, &val
);
420 param
= get_fcr (param
, &val
);
425 param
= get_vec9 (param
, &val
);
430 /* Having this here repeats the warning somtimes.
431 But can't we stand that? */
432 as_warn ("Use of obsolete instruction");
439 match_name (param
, assoc_tab
, valp
)
441 struct field_val_assoc
*assoc_tab
;
450 name
= assoc_tab
[i
].name
;
453 name_len
= strlen (name
);
454 if (!strncmp (param
, name
, name_len
))
456 *valp
= assoc_tab
[i
].val
;
457 return param
+ name_len
;
463 get_reg (param
, regnop
, reg_prefix
)
474 regno
= *param
++ - '0';
485 regno
= regno
* 10 + c
;
500 else if (c
== 's' && param
[0] == 'p')
510 get_imm16 (param
, insn
)
512 struct m88k_insn
*insn
;
514 enum reloc_type reloc
= NO_RELOC
;
518 if (!strncmp (param
, "hi16", 4) && !isalnum (param
[4]))
523 else if (!strncmp (param
, "lo16", 4) && !isalnum (param
[4]))
528 else if (!strncmp (param
, "iw16", 4) && !isalnum (param
[4]))
534 save_ptr
= input_line_pointer
;
535 input_line_pointer
= param
;
536 expression (&insn
->exp
);
537 param
= input_line_pointer
;
538 input_line_pointer
= save_ptr
;
540 val
= insn
->exp
.X_add_number
;
542 if (insn
->exp
.X_op
== O_constant
)
544 /* Insert the value now, and reset reloc to NO_RELOC. */
545 if (reloc
== NO_RELOC
)
547 /* Warn about too big expressions if not surrounded by xx16. */
549 as_warn ("Expression truncated to 16 bits");
552 if (reloc
== RELOC_HI16
)
555 insn
->opcode
|= val
& 0xffff;
558 else if (reloc
== NO_RELOC
)
559 /* We accept a symbol even without lo16, hi16, etc, and assume
560 lo16 was intended. */
569 get_pcr (param
, insn
, reloc
)
571 struct m88k_insn
*insn
;
572 enum reloc_type reloc
;
574 char *saveptr
, *saveparam
;
576 saveptr
= input_line_pointer
;
577 input_line_pointer
= param
;
579 expression (&insn
->exp
);
581 saveparam
= input_line_pointer
;
582 input_line_pointer
= saveptr
;
584 /* Botch: We should relocate now if O_constant. */
591 get_cmp (param
, valp
)
600 param
= match_name (param
, cmpslot
, valp
);
607 save_ptr
= input_line_pointer
;
608 input_line_pointer
= param
;
609 val
= get_absolute_expression ();
610 param
= input_line_pointer
;
611 input_line_pointer
= save_ptr
;
615 as_warn ("Expression truncated to 5 bits");
625 get_cnd (param
, valp
)
631 if (isdigit (*param
))
633 param
= getval (param
, &val
);
637 as_warn ("Expression truncated to 5 bits");
643 if (isupper (*param
))
644 *param
= tolower (*param
);
646 if (isupper (param
[1]))
647 param
[1] = tolower (param
[1]);
649 param
= match_name (param
, cndmsk
, valp
);
678 else if (c
== bc
&& depth
<= 0)
685 get_bf_offset_expression (param
, offsetp
)
691 if (isalpha (param
[0]))
693 if (isupper (param
[0]))
694 param
[0] = tolower (param
[0]);
695 if (isupper (param
[1]))
696 param
[1] = tolower (param
[1]);
698 param
= match_name (param
, cmpslot
, offsetp
);
704 input_line_pointer
= param
;
705 offset
= get_absolute_expression ();
706 param
= input_line_pointer
;
723 xp
= get_bf2 (param
, '<');
725 save_ptr
= input_line_pointer
;
726 input_line_pointer
= param
;
729 /* We did not find '<'. We have an offset (width implicitly 32). */
730 param
= get_bf_offset_expression (param
, &offset
);
731 input_line_pointer
= save_ptr
;
737 *xp
++ = 0; /* Overwrite the '<' */
738 param
= get_bf2 (xp
, '>');
741 *param
++ = 0; /* Overwrite the '>' */
743 width
= get_absolute_expression ();
744 xp
= get_bf_offset_expression (xp
, &offset
);
745 input_line_pointer
= save_ptr
;
751 *valp
= ((width
% 32) << 5) | (offset
% 32);
757 get_cr (param
, regnop
)
764 if (!strncmp (param
, "cr", 2))
768 regno
= *param
++ - '0';
779 regno
= regno
* 10 + c
;
795 param
= match_name (param
, cr_regs
, regnop
);
801 get_fcr (param
, regnop
)
808 if (!strncmp (param
, "fcr", 3))
812 regno
= *param
++ - '0';
823 regno
= regno
* 10 + c
;
839 param
= match_name (param
, fcr_regs
, regnop
);
845 get_vec9 (param
, valp
)
852 save_ptr
= input_line_pointer
;
853 input_line_pointer
= param
;
854 val
= get_absolute_expression ();
855 param
= input_line_pointer
;
856 input_line_pointer
= save_ptr
;
859 as_warn ("Expression truncated to 9 bits");
861 *valp
= val
% (1 << 9);
874 save_ptr
= input_line_pointer
;
875 input_line_pointer
= param
;
876 val
= get_absolute_expression ();
877 param
= input_line_pointer
;
878 input_line_pointer
= save_ptr
;
881 as_warn ("Removed lower 2 bits of expression");
889 (isdigit (z) ? (z) - '0' : \
890 islower (z) ? (z) - 'a' + 10 : \
891 isupper (z) ? (z) - 'A' + 10 : -1)
898 unsigned int val
= 0;
905 if (c
== 'x' || c
== 'X')
941 md_number_to_chars (buf
, val
, nbytes
)
946 number_to_chars_bigendian (buf
, val
, nbytes
);
951 /* This routine is never called. What is it for?
952 Ian Taylor, Cygnus Support 13 Jul 1993 */
955 md_number_to_imm (buf
, val
, nbytes
, fixP
, seg_type
)
962 if (seg_type
!= N_TEXT
|| fixP
->fx_r_type
== NO_RELOC
)
981 switch (fixP
->fx_r_type
)
1006 buf
[0] |= (val
>> 26) & 0x03;
1020 as_fatal ("Bad relocation type");
1028 md_number_to_disp (buf
, val
, nbytes
)
1033 as_fatal ("md_number_to_disp not defined");
1034 md_number_to_chars (buf
, val
, nbytes
);
1038 md_number_to_field (buf
, val
, nbytes
)
1043 as_fatal ("md_number_to_field not defined");
1044 md_number_to_chars (buf
, val
, nbytes
);
1047 #define MAX_LITTLENUMS 6
1049 /* Turn a string in input_line_pointer into a floating point constant of type
1050 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1051 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1054 md_atof (type
, litP
, sizeP
)
1060 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1061 LITTLENUM_TYPE
*wordP
;
1093 return "Bad call to MD_ATOF()";
1095 t
= atof_ieee (input_line_pointer
, type
, words
);
1097 input_line_pointer
= t
;
1099 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1100 for (wordP
= words
; prec
--;)
1102 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1103 litP
+= sizeof (LITTLENUM_TYPE
);
1108 int md_short_jump_size
= 4;
1111 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1113 addressT from_addr
, to_addr
;
1117 ptr
[0] = (char) 0xc0;
1122 ptr
- frag
->fr_literal
,
1127 RELOC_PC26
); /* Botch: Shouldn't this be RELOC_PC16? */
1130 int md_long_jump_size
= 4;
1133 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1135 addressT from_addr
, to_addr
;
1139 ptr
[0] = (char) 0xc0;
1144 ptr
- frag
->fr_literal
,
1153 md_estimate_size_before_relax (fragP
, segment_type
)
1157 as_fatal ("Relaxation should never occur");
1161 const relax_typeS md_relax_table
[] =
1166 /* As far as I can tell, this routine is never called. What is it
1168 Ian Taylor, Cygnus Support 13 Jul 1993 */
1172 * Risc relocations are completely different, so it needs
1173 * this machine dependent routine to emit them.
1176 emit_relocations (fixP
, segment_address_in_file
)
1178 relax_addressT segment_address_in_file
;
1180 struct reloc_info_m88k ri
;
1182 extern char *next_object_file_charP
;
1184 bzero ((char *) &ri
, sizeof (ri
));
1185 for (; fixP
; fixP
= fixP
->fx_next
)
1187 if (fixP
->fx_r_type
>= NO_RELOC
)
1189 fprintf (stderr
, "fixP->fx_r_type = %d\n", fixP
->fx_r_type
);
1193 if ((symbolP
= fixP
->fx_addsy
) != NULL
)
1195 ri
.r_address
= fixP
->fx_frag
->fr_address
+
1196 fixP
->fx_where
- segment_address_in_file
;
1197 if ((symbolP
->sy_type
& N_TYPE
) == N_UNDF
)
1200 ri
.r_symbolnum
= symbolP
->sy_number
;
1205 ri
.r_symbolnum
= symbolP
->sy_type
& N_TYPE
;
1207 if (symbolP
&& symbolP
->sy_frag
)
1209 ri
.r_addend
= symbolP
->sy_frag
->fr_address
;
1211 ri
.r_type
= fixP
->fx_r_type
;
1214 ri
.r_addend
-= ri
.r_address
;
1218 ri
.r_addend
= fixP
->fx_addnumber
;
1221 append (&next_object_file_charP
, (char *) &ri
, sizeof (ri
));
1230 /* This routine can be subsumed by s_lcomm in read.c.
1231 Ian Taylor, Cygnus Support 13 Jul 1993 */
1240 int temp
, bss_align
;
1243 name
= input_line_pointer
;
1244 c
= get_symbol_end ();
1245 p
= input_line_pointer
;
1248 if (*input_line_pointer
!= ',')
1250 as_warn ("Expected comma after name");
1251 ignore_rest_of_line ();
1254 input_line_pointer
++;
1255 if ((temp
= get_absolute_expression ()) < 0)
1257 as_warn ("BSS length (%d.) <0! Ignored.", temp
);
1258 ignore_rest_of_line ();
1262 symbolP
= symbol_find_or_make (name
);
1264 if (*input_line_pointer
== ',')
1266 input_line_pointer
++;
1267 bss_align
= get_absolute_expression ();
1272 if (!S_IS_DEFINED(symbolP
)
1273 || S_GET_SEGMENT(symbolP
) == SEG_BSS
)
1278 segT current_seg
= now_seg
;
1279 subsegT current_subseg
= now_subseg
;
1281 subseg_set (SEG_BSS
, 1); /* switch to bss */
1284 frag_align (bss_align
, 0);
1286 /* detach from old frag */
1287 if (symbolP
->sy_type
== N_BSS
&& symbolP
->sy_frag
!= NULL
)
1288 symbolP
->sy_frag
->fr_symbol
= NULL
;
1290 symbolP
->sy_frag
= frag_now
;
1291 p
= frag_var (rs_org
, 1, 1, (relax_substateT
)0, symbolP
,
1294 S_SET_SEGMENT (symbolP
, SEG_BSS
);
1296 subseg_set (current_seg
, current_subseg
);
1301 as_warn ("Ignoring attempt to re-define symbol %s.", name
);
1304 while (!is_end_of_line
[*input_line_pointer
])
1306 input_line_pointer
++;
1314 /* These functions are needed if we are linking with obj-coffbfd.c.
1315 That file may be replaced by a more BFD oriented version at some
1316 point. If that happens, these functions should be rexamined.
1318 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
1320 /* Given a fixS structure (created by a call to fix_new, above),
1321 return the BFD relocation type to use for it. */
1324 tc_coff_fix2rtype (fixp
)
1327 switch (fixp
->fx_r_type
)
1346 /* Apply a fixS to the object file. Since COFF does not use addends
1347 in relocs, the addend is actually stored directly in the object
1351 md_apply_fix (fixp
, val
)
1357 buf
= fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
1358 fixp
->fx_offset
= 0;
1360 switch (fixp
->fx_r_type
)
1363 fixp
->fx_offset
= val
>> 16;
1369 fixp
->fx_offset
= val
>> 16;
1375 fixp
->fx_offset
= val
>> 16;
1386 buf
[0] |= (val
>> 26) & 0x03;
1404 /* Where a PC relative offset is calculated from. On the m88k they
1405 are calculated from just after the instruction. */
1408 md_pcrel_from (fixp
)
1411 switch (fixp
->fx_r_type
)
1414 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
- 2;
1416 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1423 #endif /* M88KCOFF */