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,
5 2000, 2001, 2002, 2003, 2005
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;
126 static struct hash_control *op_hash = NULL;
128 /* These bits should be turned off in the first address of every segment. */
129 int md_seg_align = 7;
131 /* These chars start a comment anywhere in a source file (except inside
133 const char comment_chars[] = ";";
135 /* These chars only start a comment at the beginning of a line. */
136 const char line_comment_chars[] = "#";
138 const char line_separator_chars[] = "";
140 /* Chars that can be used to separate mant from exp in floating point nums. */
141 const char EXP_CHARS[] = "eE";
143 /* Chars that mean this number is a floating point constant.
145 or 0H1.234E-12 (see exp chars above). */
146 const char FLT_CHARS[] = "dDfF";
151 const char *retval = NULL;
154 /* Initialize hash table. */
155 op_hash = hash_new ();
157 while (*m88k_opcodes[i].name)
159 char *name = m88k_opcodes[i].name;
161 /* Hash each mnemonic and record its position. */
162 retval = hash_insert (op_hash, name, &m88k_opcodes[i]);
165 as_fatal (_("Can't hash instruction '%s':%s"),
166 m88k_opcodes[i].name, retval);
168 /* Skip to next unique mnemonic or end of list. */
169 for (i++; !strcmp (m88k_opcodes[i].name, name); i++)
174 const char *md_shortopts = "";
175 struct option md_longopts[] =
177 {NULL, no_argument, NULL, 0}
179 size_t md_longopts_size = sizeof (md_longopts);
182 md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
188 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
193 get_o6 (char *param, unsigned *valp)
198 save_ptr = input_line_pointer;
199 input_line_pointer = param;
200 val = get_absolute_expression ();
201 param = input_line_pointer;
202 input_line_pointer = save_ptr;
205 as_warn (_("Removed lower 2 bits of expression"));
213 get_vec9 (char *param, unsigned *valp)
218 save_ptr = input_line_pointer;
219 input_line_pointer = param;
220 val = get_absolute_expression ();
221 param = input_line_pointer;
222 input_line_pointer = save_ptr;
225 as_warn (_("Expression truncated to 9 bits"));
227 *valp = val % (1 << 9);
233 get_bf2 (char *param, int bc)
247 else if (c == bc && depth <= 0)
254 match_name (char *param,
255 struct field_val_assoc *assoc_tab,
264 name = assoc_tab[i].name;
267 name_len = strlen (name);
268 if (!strncmp (param, name, name_len))
270 *valp = assoc_tab[i].val;
271 return param + name_len;
277 get_bf_offset_expression (char *param, unsigned *offsetp)
281 if (ISALPHA (param[0]))
283 param[0] = TOLOWER (param[0]);
284 param[1] = TOLOWER (param[1]);
286 param = match_name (param, cmpslot, offsetp);
292 input_line_pointer = param;
293 offset = get_absolute_expression ();
294 param = input_line_pointer;
302 get_bf (char *param, unsigned *valp)
309 xp = get_bf2 (param, '<');
311 save_ptr = input_line_pointer;
312 input_line_pointer = param;
315 /* We did not find '<'. We have an offset (width implicitly 32). */
316 param = get_bf_offset_expression (param, &offset);
317 input_line_pointer = save_ptr;
323 *xp++ = 0; /* Overwrite the '<'. */
324 param = get_bf2 (xp, '>');
327 *param++ = 0; /* Overwrite the '>'. */
329 width = get_absolute_expression ();
330 xp = get_bf_offset_expression (xp, &offset);
331 input_line_pointer = save_ptr;
337 *valp = ((width % 32) << 5) | (offset % 32);
343 get_cr (char *param, unsigned *regnop)
348 if (!strncmp (param, "cr", 2))
352 regno = *param++ - '0';
363 regno = regno * 10 + c;
379 param = match_name (param, cr_regs, regnop);
385 get_fcr (char *param, unsigned *regnop)
390 if (!strncmp (param, "fcr", 3))
394 regno = *param++ - '0';
405 regno = regno * 10 + c;
421 param = match_name (param, fcr_regs, regnop);
427 (ISDIGIT (z) ? (z) - '0' : \
428 ISLOWER (z) ? (z) - 'a' + 10 : \
429 ISUPPER (z) ? (z) - 'A' + 10 : (unsigned) -1)
432 getval (char *param, unsigned int *valp)
434 unsigned int val = 0;
441 if (c == 'x' || c == 'X')
477 get_cnd (char *param, unsigned *valp)
481 if (ISDIGIT (*param))
483 param = getval (param, &val);
487 as_warn (_("Expression truncated to 5 bits"));
493 param[0] = TOLOWER (param[0]);
494 param[1] = TOLOWER (param[1]);
496 param = match_name (param, cndmsk, valp);
509 get_reg (char *param, unsigned *regnop, unsigned int reg_prefix)
517 regno = *param++ - '0';
528 regno = regno * 10 + c;
543 else if (c == 's' && param[0] == 'p')
553 get_imm16 (char *param, struct m88k_insn *insn)
555 enum reloc_type reloc = NO_RELOC;
559 if (!strncmp (param, "hi16", 4) && !ISALNUM (param[4]))
564 else if (!strncmp (param, "lo16", 4) && !ISALNUM (param[4]))
569 else if (!strncmp (param, "iw16", 4) && !ISALNUM (param[4]))
575 save_ptr = input_line_pointer;
576 input_line_pointer = param;
577 expression (&insn->exp);
578 param = input_line_pointer;
579 input_line_pointer = save_ptr;
581 val = insn->exp.X_add_number;
583 if (insn->exp.X_op == O_constant)
585 /* Insert the value now, and reset reloc to NO_RELOC. */
586 if (reloc == NO_RELOC)
588 /* Warn about too big expressions if not surrounded by xx16. */
590 as_warn (_("Expression truncated to 16 bits"));
593 if (reloc == RELOC_HI16)
596 insn->opcode |= val & 0xffff;
599 else if (reloc == NO_RELOC)
600 /* We accept a symbol even without lo16, hi16, etc, and assume
601 lo16 was intended. */
610 get_pcr (char *param, struct m88k_insn *insn, enum reloc_type reloc)
612 char *saveptr, *saveparam;
614 saveptr = input_line_pointer;
615 input_line_pointer = param;
617 expression (&insn->exp);
619 saveparam = input_line_pointer;
620 input_line_pointer = saveptr;
622 /* Botch: We should relocate now if O_constant. */
629 get_cmp (char *param, unsigned *valp)
636 param = match_name (param, cmpslot, valp);
643 save_ptr = input_line_pointer;
644 input_line_pointer = param;
645 val = get_absolute_expression ();
646 param = input_line_pointer;
647 input_line_pointer = save_ptr;
651 as_warn (_("Expression truncated to 5 bits"));
661 calcop (struct m88k_opcode *format,
663 struct m88k_insn *insn)
665 char *fmt = format->op_spec;
669 unsigned int reg_prefix = 'r';
671 insn->opcode = format->opcode;
682 insn->opcode |= opcode;
683 return (*param == 0 || *param == '\n');
691 param = get_reg (param, &val, reg_prefix);
697 param = get_o6 (param, &val);
698 opcode |= ((val >> 2) << 7);
706 param = get_reg (param, &val, reg_prefix);
712 param = get_reg (param, &val, reg_prefix);
718 param = get_reg (param, &val, 'r');
719 opcode |= (val << 16) | val;
723 param = get_imm16 (param, insn);
727 param = get_bf (param, &val);
732 param = get_pcr (param, insn, RELOC_PC16);
736 param = get_pcr (param, insn, RELOC_PC26);
740 param = get_cmp (param, &val);
745 param = get_cnd (param, &val);
750 param = get_cr (param, &val);
755 param = get_fcr (param, &val);
760 param = get_vec9 (param, &val);
765 /* Having this here repeats the warning somtimes.
766 But can't we stand that? */
767 as_warn (_("Use of obsolete instruction"));
774 md_assemble (char *op)
776 char *param, *thisfrag;
778 struct m88k_opcode *format;
779 struct m88k_insn insn;
783 /* Skip over instruction to find parameters. */
784 for (param = op; *param != 0 && !ISSPACE (*param); param++)
789 /* Try to find the instruction in the hash table. */
790 if ((format = (struct m88k_opcode *) hash_find (op_hash, op)) == NULL)
792 as_bad (_("Invalid mnemonic '%s'"), op);
796 /* Try parsing this instruction into insn. */
797 insn.exp.X_add_symbol = 0;
798 insn.exp.X_op_symbol = 0;
799 insn.exp.X_add_number = 0;
800 insn.exp.X_op = O_illegal;
801 insn.reloc = NO_RELOC;
803 while (!calcop (format, param, &insn))
805 /* If it doesn't parse try the next instruction. */
806 if (!strcmp (format[0].name, format[1].name))
810 as_fatal (_("Parameter syntax error"));
815 /* Grow the current frag and plop in the opcode. */
816 thisfrag = frag_more (4);
817 md_number_to_chars (thisfrag, insn.opcode, 4);
819 /* If this instruction requires labels mark it for later. */
827 fix_new_exp (frag_now,
828 thisfrag - frag_now->fr_literal + 2,
836 fix_new_exp (frag_now,
837 thisfrag - frag_now->fr_literal,
845 fix_new_exp (frag_now,
846 thisfrag - frag_now->fr_literal + 2,
854 fix_new_exp (frag_now,
855 thisfrag - frag_now->fr_literal,
863 as_fatal (_("Unknown relocation type"));
869 md_number_to_chars (char *buf, valueT val, int nbytes)
871 number_to_chars_bigendian (buf, val, nbytes);
874 #define MAX_LITTLENUMS 6
876 /* Turn a string in input_line_pointer into a floating point constant of type
877 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
878 emitted is stored in *sizeP . An error message is returned, or NULL on OK. */
881 md_atof (int type, char *litP, int *sizeP)
884 LITTLENUM_TYPE words[MAX_LITTLENUMS];
885 LITTLENUM_TYPE *wordP;
916 return _("Bad call to MD_ATOF()");
918 t = atof_ieee (input_line_pointer, type, words);
920 input_line_pointer = t;
922 *sizeP = prec * sizeof (LITTLENUM_TYPE);
923 for (wordP = words; prec--;)
925 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
926 litP += sizeof (LITTLENUM_TYPE);
931 int md_short_jump_size = 4;
934 md_create_short_jump (char *ptr,
935 addressT from_addr ATTRIBUTE_UNUSED,
936 addressT to_addr ATTRIBUTE_UNUSED,
940 ptr[0] = (char) 0xc0;
945 ptr - frag->fr_literal,
950 RELOC_PC26); /* Botch: Shouldn't this be RELOC_PC16? */
953 int md_long_jump_size = 4;
956 md_create_long_jump (char *ptr,
957 addressT from_addr ATTRIBUTE_UNUSED,
958 addressT to_addr ATTRIBUTE_UNUSED,
962 ptr[0] = (char) 0xc0;
967 ptr - frag->fr_literal,
976 md_estimate_size_before_relax (fragS *fragP ATTRIBUTE_UNUSED,
977 segT segment_type ATTRIBUTE_UNUSED)
979 as_fatal (_("Relaxation should never occur"));
985 /* These functions are needed if we are linking with obj-coffbfd.c.
986 That file may be replaced by a more BFD oriented version at some
987 point. If that happens, these functions should be reexamined.
989 Ian Lance Taylor, Cygnus Support, 13 July 1993. */
991 /* Given a fixS structure (created by a call to fix_new, above),
992 return the BFD relocation type to use for it. */
995 tc_coff_fix2rtype (fixS *fixp)
997 switch (fixp->fx_r_type)
1016 /* Apply a fixS to the object file. Since COFF does not use addends
1017 in relocs, the addend is actually stored directly in the object
1021 md_apply_fix3 (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1023 long val = * (long *) valP;
1026 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
1027 fixP->fx_offset = 0;
1029 switch (fixP->fx_r_type)
1032 fixP->fx_offset = val >> 16;
1038 fixP->fx_offset = val >> 16;
1044 fixP->fx_offset = val >> 16;
1055 buf[0] |= (val >> 26) & 0x03;
1072 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1076 /* Where a PC relative offset is calculated from. On the m88k they
1077 are calculated from just after the instruction. */
1080 md_pcrel_from (fixS *fixp)
1082 switch (fixp->fx_r_type)
1085 return fixp->fx_frag->fr_address + fixp->fx_where - 2;
1087 return fixp->fx_frag->fr_address + fixp->fx_where;
1093 /* Fill in rs_align_code fragments. */
1096 m88k_handle_align (fragS *fragp)
1098 static const unsigned char nop_pattern[] = { 0xf4, 0x00, 0x58, 0x00 };
1103 if (fragp->fr_type != rs_align_code)
1106 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
1107 p = fragp->fr_literal + fragp->fr_fix;
1111 int fix = bytes & 3;
1115 fragp->fr_fix += fix;
1118 memcpy (p, nop_pattern, 4);
1122 #endif /* M88KCOFF */
1124 const pseudo_typeS md_pseudo_table[] =
1126 {"align", s_align_bytes, 4},
1128 {"dfloat", float_cons, 'd'},
1129 {"ffloat", float_cons, 'f'},
1131 {"bss", s_lcomm, 1},
1132 {"string", stringer, 0},
1134 /* Force set to be treated as an instruction. */