1 /* tc-mn10300.c -- Assembler code for the Matsushita 10300
2 Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 #include "opcode/mn10300.h"
27 /* Structure to hold information about predefined registers. */
34 /* Generic assembler global variables which must be defined by all targets. */
36 /* Characters which always start a comment. */
37 const char comment_chars[] = "#";
39 /* Characters which start a comment at the beginning of a line. */
40 const char line_comment_chars[] = ";#";
42 /* Characters which may be used to separate multiple commands on a
44 const char line_separator_chars[] = ";";
46 /* Characters which are used to indicate an exponent in a floating
48 const char EXP_CHARS[] = "eE";
50 /* Characters which mean that a number is a floating point constant,
52 const char FLT_CHARS[] = "dD";
55 const relax_typeS md_relax_table[] = {
58 {0x7fff, -0x8000, 5, 2},
59 {0x7fffffff, -0x80000000, 7, 0},
61 /* bCC relaxing (uncommon cases) */
63 {0x7fff, -0x8000, 6, 5},
64 {0x7fffffff, -0x80000000, 8, 0},
67 {0x7fff, -0x8000, 5, 7},
68 {0x7fffffff, -0x80000000, 7, 0},
71 {0x7fff, -0x8000, 4, 9},
72 {0x7fffffff, -0x80000000, 6, 0},
76 {0x7fff, -0x8000, 3, 12},
77 {0x7fffffff, -0x80000000, 5, 0},
82 static void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
83 const struct mn10300_operand *,
84 offsetT, char *, unsigned,
86 static unsigned long check_operand PARAMS ((unsigned long,
87 const struct mn10300_operand *,
89 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
90 static boolean data_register_name PARAMS ((expressionS *expressionP));
91 static boolean address_register_name PARAMS ((expressionS *expressionP));
92 static boolean other_register_name PARAMS ((expressionS *expressionP));
93 static void set_arch_mach PARAMS ((int));
95 static int current_machine;
98 #define MAX_INSN_FIXUPS (5)
103 bfd_reloc_code_real_type reloc;
105 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
108 /* We must store the value of each register operand so that we can
109 verify that certain registers do not match. */
110 int mn10300_reg_operands[MN10300_MAX_OPERANDS];
112 const char *md_shortopts = "";
113 struct option md_longopts[] = {
114 {NULL, no_argument, NULL, 0}
116 size_t md_longopts_size = sizeof(md_longopts);
118 /* The target specific pseudo-ops which we support. */
119 const pseudo_typeS md_pseudo_table[] =
121 { "am30", set_arch_mach, 300 },
122 { "mn10300", set_arch_mach, 300 },
126 /* Opcode hash table. */
127 static struct hash_control *mn10300_hash;
129 /* This table is sorted. Suitable for searching by a binary search. */
130 static const struct reg_name data_registers[] =
137 #define DATA_REG_NAME_CNT (sizeof(data_registers) / sizeof(struct reg_name))
139 static const struct reg_name address_registers[] =
146 #define ADDRESS_REG_NAME_CNT (sizeof(address_registers) / sizeof(struct reg_name))
149 static const struct reg_name other_registers[] =
155 #define OTHER_REG_NAME_CNT (sizeof(other_registers) / sizeof(struct reg_name))
157 /* reg_name_search does a binary search of the given register table
158 to see if "name" is a valid regiter name. Returns the register
159 number from the array on success, or -1 on failure. */
162 reg_name_search (regs, regcount, name)
163 const struct reg_name *regs;
167 int middle, low, high;
175 middle = (low + high) / 2;
176 cmp = strcasecmp (name, regs[middle].name);
182 return regs[middle].value;
190 /* Summary of register_name().
192 * in: Input_line_pointer points to 1st char of operand.
194 * out: A expressionS.
195 * The operand may have been a register: in this case, X_op == O_register,
196 * X_add_number is set to the register number, and truth is returned.
197 * Input_line_pointer->(next non-blank) char after operand, or is in
198 * its original state.
201 data_register_name (expressionP)
202 expressionS *expressionP;
209 /* Find the spelling of the operand */
210 start = name = input_line_pointer;
212 c = get_symbol_end ();
213 reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
215 /* look to see if it's in the register table */
218 expressionP->X_op = O_register;
219 expressionP->X_add_number = reg_number;
221 /* make the rest nice */
222 expressionP->X_add_symbol = NULL;
223 expressionP->X_op_symbol = NULL;
224 *input_line_pointer = c; /* put back the delimiting char */
229 /* reset the line as if we had not done anything */
230 *input_line_pointer = c; /* put back the delimiting char */
231 input_line_pointer = start; /* reset input_line pointer */
236 /* Summary of register_name().
238 * in: Input_line_pointer points to 1st char of operand.
240 * out: A expressionS.
241 * The operand may have been a register: in this case, X_op == O_register,
242 * X_add_number is set to the register number, and truth is returned.
243 * Input_line_pointer->(next non-blank) char after operand, or is in
244 * its original state.
247 address_register_name (expressionP)
248 expressionS *expressionP;
255 /* Find the spelling of the operand */
256 start = name = input_line_pointer;
258 c = get_symbol_end ();
259 reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
261 /* look to see if it's in the register table */
264 expressionP->X_op = O_register;
265 expressionP->X_add_number = reg_number;
267 /* make the rest nice */
268 expressionP->X_add_symbol = NULL;
269 expressionP->X_op_symbol = NULL;
270 *input_line_pointer = c; /* put back the delimiting char */
275 /* reset the line as if we had not done anything */
276 *input_line_pointer = c; /* put back the delimiting char */
277 input_line_pointer = start; /* reset input_line pointer */
282 /* Summary of register_name().
284 * in: Input_line_pointer points to 1st char of operand.
286 * out: A expressionS.
287 * The operand may have been a register: in this case, X_op == O_register,
288 * X_add_number is set to the register number, and truth is returned.
289 * Input_line_pointer->(next non-blank) char after operand, or is in
290 * its original state.
293 other_register_name (expressionP)
294 expressionS *expressionP;
301 /* Find the spelling of the operand */
302 start = name = input_line_pointer;
304 c = get_symbol_end ();
305 reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
307 /* look to see if it's in the register table */
310 expressionP->X_op = O_register;
311 expressionP->X_add_number = reg_number;
313 /* make the rest nice */
314 expressionP->X_add_symbol = NULL;
315 expressionP->X_op_symbol = NULL;
316 *input_line_pointer = c; /* put back the delimiting char */
321 /* reset the line as if we had not done anything */
322 *input_line_pointer = c; /* put back the delimiting char */
323 input_line_pointer = start; /* reset input_line pointer */
329 md_show_usage (stream)
332 fprintf(stream, _("MN10300 options:\n\
337 md_parse_option (c, arg)
345 md_undefined_symbol (name)
352 md_atof (type, litp, sizep)
358 LITTLENUM_TYPE words[4];
374 return "bad call to md_atof";
377 t = atof_ieee (input_line_pointer, type, words);
379 input_line_pointer = t;
383 for (i = prec - 1; i >= 0; i--)
385 md_number_to_chars (litp, (valueT) words[i], 2);
394 md_convert_frag (abfd, sec, fragP)
399 static unsigned long label_count = 0;
402 subseg_change (sec, 0);
403 if (fragP->fr_subtype == 0)
405 fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
406 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
410 else if (fragP->fr_subtype == 1)
412 /* Reverse the condition of the first branch. */
413 int offset = fragP->fr_fix;
414 int opcode = fragP->fr_literal[offset] & 0xff;
451 fragP->fr_literal[offset] = opcode;
453 /* Create a fixup for the reversed conditional branch. */
454 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
455 fix_new (fragP, fragP->fr_fix + 1, 1,
456 symbol_new (buf, sec, 0, fragP->fr_next),
457 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
459 /* Now create the unconditional branch + fixup to the
461 fragP->fr_literal[offset + 2] = 0xcc;
462 fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
463 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
467 else if (fragP->fr_subtype == 2)
469 /* Reverse the condition of the first branch. */
470 int offset = fragP->fr_fix;
471 int opcode = fragP->fr_literal[offset] & 0xff;
508 fragP->fr_literal[offset] = opcode;
510 /* Create a fixup for the reversed conditional branch. */
511 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
512 fix_new (fragP, fragP->fr_fix + 1, 1,
513 symbol_new (buf, sec, 0, fragP->fr_next),
514 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
516 /* Now create the unconditional branch + fixup to the
518 fragP->fr_literal[offset + 2] = 0xdc;
519 fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
520 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
524 else if (fragP->fr_subtype == 3)
526 fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
527 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
531 else if (fragP->fr_subtype == 4)
533 /* Reverse the condition of the first branch. */
534 int offset = fragP->fr_fix;
535 int opcode = fragP->fr_literal[offset + 1] & 0xff;
554 fragP->fr_literal[offset + 1] = opcode;
556 /* Create a fixup for the reversed conditional branch. */
557 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
558 fix_new (fragP, fragP->fr_fix + 2, 1,
559 symbol_new (buf, sec, 0, fragP->fr_next),
560 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
562 /* Now create the unconditional branch + fixup to the
564 fragP->fr_literal[offset + 3] = 0xcc;
565 fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
566 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
570 else if (fragP->fr_subtype == 5)
572 /* Reverse the condition of the first branch. */
573 int offset = fragP->fr_fix;
574 int opcode = fragP->fr_literal[offset + 1] & 0xff;
590 fragP->fr_literal[offset + 1] = opcode;
592 /* Create a fixup for the reversed conditional branch. */
593 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
594 fix_new (fragP, fragP->fr_fix + 2, 1,
595 symbol_new (buf, sec, 0, fragP->fr_next),
596 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
598 /* Now create the unconditional branch + fixup to the
600 fragP->fr_literal[offset + 3] = 0xdc;
601 fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
602 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
606 else if (fragP->fr_subtype == 6)
608 int offset = fragP->fr_fix;
609 fragP->fr_literal[offset] = 0xcd;
610 fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
611 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
615 else if (fragP->fr_subtype == 7)
617 int offset = fragP->fr_fix;
618 fragP->fr_literal[offset] = 0xdd;
619 fragP->fr_literal[offset + 5] = fragP->fr_literal[offset + 3];
620 fragP->fr_literal[offset + 6] = fragP->fr_literal[offset + 4];
622 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
623 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
627 else if (fragP->fr_subtype == 8)
629 int offset = fragP->fr_fix;
630 fragP->fr_literal[offset] = 0xfa;
631 fragP->fr_literal[offset + 1] = 0xff;
632 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
633 fragP->fr_offset + 2, 1, BFD_RELOC_16_PCREL);
637 else if (fragP->fr_subtype == 9)
639 int offset = fragP->fr_fix;
640 fragP->fr_literal[offset] = 0xfc;
641 fragP->fr_literal[offset + 1] = 0xff;
643 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
644 fragP->fr_offset + 2, 1, BFD_RELOC_32_PCREL);
648 else if (fragP->fr_subtype == 10)
650 fragP->fr_literal[fragP->fr_fix] = 0xca;
651 fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
652 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
656 else if (fragP->fr_subtype == 11)
658 int offset = fragP->fr_fix;
659 fragP->fr_literal[offset] = 0xcc;
661 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
662 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
666 else if (fragP->fr_subtype == 12)
668 int offset = fragP->fr_fix;
669 fragP->fr_literal[offset] = 0xdc;
671 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
672 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
681 md_section_align (seg, addr)
685 int align = bfd_get_section_alignment (stdoutput, seg);
686 return ((addr + (1 << align) - 1) & (-1 << align));
692 char *prev_name = "";
693 register const struct mn10300_opcode *op;
695 mn10300_hash = hash_new();
697 /* Insert unique names into hash table. The MN10300 instruction set
698 has many identical opcode names that have different opcodes based
699 on the operands. This hash table then provides a quick index to
700 the first opcode with a particular name in the opcode table. */
702 op = mn10300_opcodes;
705 if (strcmp (prev_name, op->name))
707 prev_name = (char *) op->name;
708 hash_insert (mn10300_hash, op->name, (char *) op);
713 /* This is both a simplification (we don't have to write md_apply_fix)
714 and support for future optimizations (branch shortening and similar
715 stuff in the linker). */
718 /* Set the default machine type. */
719 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, 300))
720 as_warn (_("could not set architecture and machine"));
722 current_machine = 300;
730 struct mn10300_opcode *opcode;
731 struct mn10300_opcode *next_opcode;
732 const unsigned char *opindex_ptr;
733 int next_opindex, relaxable;
734 unsigned long insn, extension, size = 0;
739 /* Get the opcode. */
740 for (s = str; *s != '\0' && ! isspace (*s); s++)
745 /* find the first opcode with the proper name */
746 opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
749 as_bad (_("Unrecognized opcode: `%s'"), str);
754 while (isspace (*str))
757 input_line_pointer = str;
767 errmsg = _("Invalid opcode/operands");
769 /* Reset the array of register operands. */
770 memset (mn10300_reg_operands, -1, sizeof (mn10300_reg_operands));
776 insn = opcode->opcode;
779 /* If the instruction is not available on the current machine
780 then it can not possibly match. */
782 && (opcode->machine != current_machine))
785 for (op_idx = 1, opindex_ptr = opcode->operands;
787 opindex_ptr++, op_idx++)
789 const struct mn10300_operand *operand;
792 if (next_opindex == 0)
794 operand = &mn10300_operands[*opindex_ptr];
798 operand = &mn10300_operands[next_opindex];
802 while (*str == ' ' || *str == ',')
805 if (operand->flags & MN10300_OPERAND_RELAX)
808 /* Gather the operand. */
809 hold = input_line_pointer;
810 input_line_pointer = str;
812 if (operand->flags & MN10300_OPERAND_PAREN)
814 if (*input_line_pointer != ')' && *input_line_pointer != '(')
816 input_line_pointer = hold;
820 input_line_pointer++;
823 /* See if we can match the operands. */
824 else if (operand->flags & MN10300_OPERAND_DREG)
826 if (!data_register_name (&ex))
828 input_line_pointer = hold;
833 else if (operand->flags & MN10300_OPERAND_AREG)
835 if (!address_register_name (&ex))
837 input_line_pointer = hold;
842 else if (operand->flags & MN10300_OPERAND_SP)
844 char *start = input_line_pointer;
845 char c = get_symbol_end ();
847 if (strcasecmp (start, "sp") != 0)
849 *input_line_pointer = c;
850 input_line_pointer = hold;
854 *input_line_pointer = c;
857 else if (operand->flags & MN10300_OPERAND_PSW)
859 char *start = input_line_pointer;
860 char c = get_symbol_end ();
862 if (strcasecmp (start, "psw") != 0)
864 *input_line_pointer = c;
865 input_line_pointer = hold;
869 *input_line_pointer = c;
872 else if (operand->flags & MN10300_OPERAND_MDR)
874 char *start = input_line_pointer;
875 char c = get_symbol_end ();
877 if (strcasecmp (start, "mdr") != 0)
879 *input_line_pointer = c;
880 input_line_pointer = hold;
884 *input_line_pointer = c;
887 else if (operand->flags & MN10300_OPERAND_REG_LIST)
889 unsigned int value = 0;
890 if (*input_line_pointer != '[')
892 input_line_pointer = hold;
898 input_line_pointer++;
900 /* We used to reject a null register list here; however,
901 we accept it now so the compiler can emit "call" instructions
902 for all calls to named functions.
904 The linker can then fill in the appropriate bits for the
905 register list and stack size or change the instruction
906 into a "calls" if using "call" is not profitable. */
907 while (*input_line_pointer != ']')
912 if (*input_line_pointer == ',')
913 input_line_pointer++;
915 start = input_line_pointer;
916 c = get_symbol_end ();
918 if (strcasecmp (start, "d2") == 0)
921 *input_line_pointer = c;
923 else if (strcasecmp (start, "d3") == 0)
926 *input_line_pointer = c;
928 else if (strcasecmp (start, "a2") == 0)
931 *input_line_pointer = c;
933 else if (strcasecmp (start, "a3") == 0)
936 *input_line_pointer = c;
938 else if (strcasecmp (start, "other") == 0)
941 *input_line_pointer = c;
945 input_line_pointer = hold;
950 input_line_pointer++;
951 mn10300_insert_operand (&insn, &extension, operand,
952 value, (char *) NULL, 0, 0);
956 else if (data_register_name (&ex))
958 input_line_pointer = hold;
962 else if (address_register_name (&ex))
964 input_line_pointer = hold;
968 else if (other_register_name (&ex))
970 input_line_pointer = hold;
974 else if (*str == ')' || *str == '(')
976 input_line_pointer = hold;
988 errmsg = _("illegal operand");
991 errmsg = _("missing operand");
997 mask = MN10300_OPERAND_DREG | MN10300_OPERAND_AREG;
998 if ((operand->flags & mask) == 0)
1000 input_line_pointer = hold;
1005 if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
1007 else if (opcode->format == FMT_D2
1008 || opcode->format == FMT_D4
1009 || opcode->format == FMT_S2
1010 || opcode->format == FMT_S4
1011 || opcode->format == FMT_S6
1012 || opcode->format == FMT_D5)
1017 mn10300_insert_operand (&insn, &extension, operand,
1018 ex.X_add_number, (char *) NULL,
1022 /* And note the register number in the register array. */
1023 mn10300_reg_operands[op_idx - 1] = ex.X_add_number;
1028 /* If this operand can be promoted, and it doesn't
1029 fit into the allocated bitfield for this insn,
1030 then promote it (ie this opcode does not match). */
1032 & (MN10300_OPERAND_PROMOTE | MN10300_OPERAND_RELAX)
1033 && ! check_operand (insn, operand, ex.X_add_number))
1035 input_line_pointer = hold;
1040 mn10300_insert_operand (&insn, &extension, operand,
1041 ex.X_add_number, (char *) NULL,
1046 /* If this operand can be promoted, then this opcode didn't
1047 match since we can't know if it needed promotion! */
1048 if (operand->flags & MN10300_OPERAND_PROMOTE)
1050 input_line_pointer = hold;
1055 /* We need to generate a fixup for this expression. */
1056 if (fc >= MAX_INSN_FIXUPS)
1057 as_fatal (_("too many fixups"));
1058 fixups[fc].exp = ex;
1059 fixups[fc].opindex = *opindex_ptr;
1060 fixups[fc].reloc = BFD_RELOC_UNUSED;
1066 str = input_line_pointer;
1067 input_line_pointer = hold;
1069 while (*str == ' ' || *str == ',')
1074 /* Make sure we used all the operands! */
1078 /* If this instruction has registers that must not match, verify
1079 that they do indeed not match. */
1080 if (opcode->no_match_operands)
1084 /* Look at each operand to see if it's marked. */
1085 for (i = 0; i < MN10300_MAX_OPERANDS; i++)
1087 if ((1 << i) & opcode->no_match_operands)
1091 /* operand I is marked. Check that it does not match any
1092 operands > I which are marked. */
1093 for (j = i + 1; j < MN10300_MAX_OPERANDS; j++)
1095 if (((1 << j) & opcode->no_match_operands)
1096 && mn10300_reg_operands[i] == mn10300_reg_operands[j])
1098 errmsg = _("Invalid register specification.");
1110 next_opcode = opcode + 1;
1111 if (!strcmp(next_opcode->name, opcode->name))
1113 opcode = next_opcode;
1117 as_bad ("%s", errmsg);
1123 while (isspace (*str))
1127 as_bad (_("junk at end of line: `%s'"), str);
1129 input_line_pointer = str;
1131 /* Determine the size of the instruction. */
1132 if (opcode->format == FMT_S0)
1135 if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
1138 if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
1142 if (opcode->format == FMT_S4)
1145 if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
1148 if (opcode->format == FMT_D2)
1151 if (opcode->format == FMT_D4)
1154 if (relaxable && fc > 0)
1161 /* Handle bra specially. Basically treat it like jmp so
1162 that we automatically handle 8, 16 and 32 bit offsets
1163 correctly as well as jumps to an undefined address.
1165 It is also important to not treat it like other bCC
1166 instructions since the long forms of bra is different
1167 from other bCC instructions. */
1168 if (opcode->opcode == 0xca00)
1180 else if (size == 3 && opcode->opcode == 0xcc0000)
1182 /* bCC (uncommon cases) */
1186 f = frag_var (rs_machine_dependent, 8, 8 - size, type,
1187 fixups[0].exp.X_add_symbol,
1188 fixups[0].exp.X_add_number,
1189 (char *)fixups[0].opindex);
1191 /* This is pretty hokey. We basically just care about the
1192 opcode, so we have to write out the first word big endian.
1194 The exception is "call", which has two operands that we
1197 The first operand (the register list) happens to be in the
1198 first instruction word, and will be in the right place if
1199 we output the first word in big endian mode.
1201 The second operand (stack size) is in the extension word,
1202 and we want it to appear as the first character in the extension
1203 word (as it appears in memory). Luckily, writing the extension
1204 word in big endian format will do what we want. */
1205 number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
1208 number_to_chars_bigendian (f + 4, extension, 4);
1209 number_to_chars_bigendian (f + 8, 0, size - 8);
1212 number_to_chars_bigendian (f + 4, extension, size - 4);
1216 /* Allocate space for the instruction. */
1217 f = frag_more (size);
1219 /* Fill in bytes for the instruction. Note that opcode fields
1220 are written big-endian, 16 & 32bit immediates are written
1221 little endian. Egad. */
1222 if (opcode->format == FMT_S0
1223 || opcode->format == FMT_S1
1224 || opcode->format == FMT_D0
1225 || opcode->format == FMT_D1)
1227 number_to_chars_bigendian (f, insn, size);
1229 else if (opcode->format == FMT_S2
1230 && opcode->opcode != 0xdf0000
1231 && opcode->opcode != 0xde0000)
1233 /* A format S2 instruction that is _not_ "ret" and "retf". */
1234 number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
1235 number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
1237 else if (opcode->format == FMT_S2)
1239 /* This must be a ret or retf, which is written entirely in
1240 big-endian format. */
1241 number_to_chars_bigendian (f, insn, 3);
1243 else if (opcode->format == FMT_S4
1244 && opcode->opcode != 0xdc000000)
1246 /* This must be a format S4 "call" instruction. What a pain. */
1247 unsigned long temp = (insn >> 8) & 0xffff;
1248 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1249 number_to_chars_littleendian (f + 1, temp, 2);
1250 number_to_chars_bigendian (f + 3, insn & 0xff, 1);
1251 number_to_chars_bigendian (f + 4, extension & 0xff, 1);
1253 else if (opcode->format == FMT_S4)
1255 /* This must be a format S4 "jmp" instruction. */
1256 unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
1257 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1258 number_to_chars_littleendian (f + 1, temp, 4);
1260 else if (opcode->format == FMT_S6)
1262 unsigned long temp = ((insn & 0xffffff) << 8)
1263 | ((extension >> 16) & 0xff);
1264 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1265 number_to_chars_littleendian (f + 1, temp, 4);
1266 number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
1267 number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1269 else if (opcode->format == FMT_D2
1270 && opcode->opcode != 0xfaf80000
1271 && opcode->opcode != 0xfaf00000
1272 && opcode->opcode != 0xfaf40000)
1274 /* A format D2 instruction where the 16bit immediate is
1275 really a single 16bit value, not two 8bit values. */
1276 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1277 number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
1279 else if (opcode->format == FMT_D2)
1281 /* A format D2 instruction where the 16bit immediate
1282 is really two 8bit immediates. */
1283 number_to_chars_bigendian (f, insn, 4);
1285 else if (opcode->format == FMT_D4)
1287 unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
1288 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1289 number_to_chars_littleendian (f + 2, temp, 4);
1291 else if (opcode->format == FMT_D5)
1293 unsigned long temp = ((insn & 0xffff) << 16)
1294 | ((extension >> 8) & 0xffff);
1295 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1296 number_to_chars_littleendian (f + 2, temp, 4);
1297 number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1300 /* Create any fixups. */
1301 for (i = 0; i < fc; i++)
1303 const struct mn10300_operand *operand;
1305 operand = &mn10300_operands[fixups[i].opindex];
1306 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1308 reloc_howto_type *reloc_howto;
1313 reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1318 size = bfd_get_reloc_size (reloc_howto);
1320 if (size < 1 || size > 4)
1324 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1325 size, &fixups[i].exp,
1326 reloc_howto->pc_relative,
1331 int reloc, pcrel, reloc_size, offset;
1334 reloc = BFD_RELOC_NONE;
1335 /* How big is the reloc? Remember SPLIT relocs are
1336 implicitly 32bits. */
1337 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1340 reloc_size = operand->bits;
1342 /* Is the reloc pc-relative? */
1343 pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
1345 /* Gross. This disgusting hack is to make sure we
1346 get the right offset for the 16/32 bit reloc in
1347 "call" instructions. Basically they're a pain
1348 because the reloc isn't at the end of the instruction. */
1349 if ((size == 5 || size == 7)
1350 && (((insn >> 24) & 0xff) == 0xcd
1351 || ((insn >> 24) & 0xff) == 0xdd))
1354 /* Similarly for certain bit instructions which don't
1355 hav their 32bit reloc at the tail of the instruction. */
1357 && (((insn >> 16) & 0xffff) == 0xfe00
1358 || ((insn >> 16) & 0xffff) == 0xfe01
1359 || ((insn >> 16) & 0xffff) == 0xfe02))
1362 offset = size - reloc_size / 8;
1364 /* Choose a proper BFD relocation type. */
1367 if (reloc_size == 32)
1368 reloc = BFD_RELOC_32_PCREL;
1369 else if (reloc_size == 16)
1370 reloc = BFD_RELOC_16_PCREL;
1371 else if (reloc_size == 8)
1372 reloc = BFD_RELOC_8_PCREL;
1378 if (reloc_size == 32)
1379 reloc = BFD_RELOC_32;
1380 else if (reloc_size == 16)
1381 reloc = BFD_RELOC_16;
1382 else if (reloc_size == 8)
1383 reloc = BFD_RELOC_8;
1388 /* Convert the size of the reloc into what fix_new_exp wants. */
1389 reloc_size = reloc_size / 8;
1390 if (reloc_size == 8)
1392 else if (reloc_size == 16)
1394 else if (reloc_size == 32)
1397 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1398 reloc_size, &fixups[i].exp, pcrel,
1399 ((bfd_reloc_code_real_type) reloc));
1402 fixP->fx_offset += offset;
1409 /* if while processing a fixup, a reloc really needs to be created */
1410 /* then it is done here */
1413 tc_gen_reloc (seg, fixp)
1418 reloc = (arelent *) xmalloc (sizeof (arelent));
1420 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1421 if (reloc->howto == (reloc_howto_type *) NULL)
1423 as_bad_where (fixp->fx_file, fixp->fx_line,
1424 _("reloc %d not supported by object file format"),
1425 (int)fixp->fx_r_type);
1428 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1430 if (fixp->fx_addsy && fixp->fx_subsy)
1433 if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
1434 || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
1436 as_bad_where (fixp->fx_file, fixp->fx_line,
1437 "Difference of symbols in different sections is not supported");
1441 reloc->sym_ptr_ptr = &bfd_abs_symbol;
1442 reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
1443 - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
1447 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof( asymbol *));
1448 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1449 reloc->addend = fixp->fx_offset;
1455 md_estimate_size_before_relax (fragp, seg)
1459 if (fragp->fr_subtype == 0)
1461 if (fragp->fr_subtype == 3)
1463 if (fragp->fr_subtype == 6)
1465 if (!S_IS_DEFINED (fragp->fr_symbol)
1466 || seg != S_GET_SEGMENT (fragp->fr_symbol))
1468 fragp->fr_subtype = 7;
1474 if (fragp->fr_subtype == 8)
1476 if (!S_IS_DEFINED (fragp->fr_symbol)
1477 || seg != S_GET_SEGMENT (fragp->fr_symbol))
1479 fragp->fr_subtype = 9;
1485 if (fragp->fr_subtype == 10)
1487 if (!S_IS_DEFINED (fragp->fr_symbol)
1488 || seg != S_GET_SEGMENT (fragp->fr_symbol))
1490 fragp->fr_subtype = 12;
1499 md_pcrel_from (fixp)
1502 return fixp->fx_frag->fr_address;
1504 if (fixp->fx_addsy != (symbolS *) NULL && ! S_IS_DEFINED (fixp->fx_addsy))
1506 /* The symbol is undefined. Let the linker figure it out. */
1509 return fixp->fx_frag->fr_address + fixp->fx_where;
1514 md_apply_fix3 (fixp, valuep, seg)
1519 /* We shouldn't ever get here because linkrelax is nonzero. */
1525 /* Insert an operand value into an instruction. */
1528 mn10300_insert_operand (insnp, extensionp, operand, val, file, line, shift)
1529 unsigned long *insnp;
1530 unsigned long *extensionp;
1531 const struct mn10300_operand *operand;
1537 /* No need to check 32bit operands for a bit. Note that
1538 MN10300_OPERAND_SPLIT is an implicit 32bit operand. */
1539 if (operand->bits != 32
1540 && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1546 bits = operand->bits;
1548 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1550 max = (1 << (bits - 1)) - 1;
1551 min = - (1 << (bits - 1));
1555 max = (1 << bits) - 1;
1562 if (test < (offsetT) min || test > (offsetT) max)
1565 _("operand out of range (%s not between %ld and %ld)");
1568 sprint_value (buf, test);
1569 if (file == (char *) NULL)
1570 as_warn (err, buf, min, max);
1572 as_warn_where (file, line, err, buf, min, max);
1576 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1578 *insnp |= (val >> (32 - operand->bits)) & ((1 << operand->bits) - 1);
1579 *extensionp |= ((val & ((1 << (32 - operand->bits)) - 1))
1582 else if ((operand->flags & MN10300_OPERAND_EXTENDED) == 0)
1584 *insnp |= (((long) val & ((1 << operand->bits) - 1))
1585 << (operand->shift + shift));
1587 if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1588 *insnp |= (((long) val & ((1 << operand->bits) - 1))
1589 << (operand->shift + shift + operand->bits));
1593 *extensionp |= (((long) val & ((1 << operand->bits) - 1))
1594 << (operand->shift + shift));
1596 if ((operand->flags & MN10300_OPERAND_REPEATED) != 0)
1597 *extensionp |= (((long) val & ((1 << operand->bits) - 1))
1598 << (operand->shift + shift + operand->bits));
1602 static unsigned long
1603 check_operand (insn, operand, val)
1605 const struct mn10300_operand *operand;
1608 /* No need to check 32bit operands for a bit. Note that
1609 MN10300_OPERAND_SPLIT is an implicit 32bit operand. */
1610 if (operand->bits != 32
1611 && (operand->flags & MN10300_OPERAND_SPLIT) == 0)
1617 bits = operand->bits;
1619 if ((operand->flags & MN10300_OPERAND_SIGNED) != 0)
1621 max = (1 << (bits - 1)) - 1;
1622 min = - (1 << (bits - 1));
1626 max = (1 << bits) - 1;
1633 if (test < (offsetT) min || test > (offsetT) max)
1642 set_arch_mach (mach)
1645 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, mach))
1646 as_warn (_("could not set architecture and machine"));
1648 current_machine = mach;