1 /* tc-mn10300.c -- Assembler code for the Matsushita 10300
3 Copyright (C) 1996, 1997, 1998 Free Software Foundation.
5 This file is part of GAS, the GNU Assembler.
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "opcode/mn10300.h"
28 /* Structure to hold information about predefined registers. */
35 /* Generic assembler global variables which must be defined by all targets. */
37 /* Characters which always start a comment. */
38 const char comment_chars[] = "#";
40 /* Characters which start a comment at the beginning of a line. */
41 const char line_comment_chars[] = ";#";
43 /* Characters which may be used to separate multiple commands on a
45 const char line_separator_chars[] = ";";
47 /* Characters which are used to indicate an exponent in a floating
49 const char EXP_CHARS[] = "eE";
51 /* Characters which mean that a number is a floating point constant,
53 const char FLT_CHARS[] = "dD";
56 const relax_typeS md_relax_table[] = {
59 {0x7fff, -0x8000, 5, 2},
60 {0x7fffffff, -0x80000000, 7, 0},
62 /* bCC relaxing (uncommon cases) */
64 {0x7fff, -0x8000, 6, 5},
65 {0x7fffffff, -0x80000000, 8, 0},
68 {0x7fff, -0x8000, 5, 7},
69 {0x7fffffff, -0x80000000, 7, 0},
72 {0x7fff, -0x8000, 4, 9},
73 {0x7fffffff, -0x80000000, 6, 0},
77 {0x7fff, -0x8000, 3, 12},
78 {0x7fffffff, -0x80000000, 5, 0},
83 static void mn10300_insert_operand PARAMS ((unsigned long *, unsigned long *,
84 const struct mn10300_operand *,
85 offsetT, char *, unsigned,
87 static unsigned long check_operand PARAMS ((unsigned long,
88 const struct mn10300_operand *,
90 static int reg_name_search PARAMS ((const struct reg_name *, int, const char *));
91 static boolean data_register_name PARAMS ((expressionS *expressionP));
92 static boolean address_register_name PARAMS ((expressionS *expressionP));
93 static boolean other_register_name PARAMS ((expressionS *expressionP));
94 static void set_arch_mach PARAMS ((int));
96 static int current_machine;
99 #define MAX_INSN_FIXUPS (5)
104 bfd_reloc_code_real_type reloc;
106 struct mn10300_fixup fixups[MAX_INSN_FIXUPS];
109 /* We must store the value of each register operand so that we can
110 verify that certain registers do not match. */
111 int mn10300_reg_operands[MN10300_MAX_OPERANDS];
113 const char *md_shortopts = "";
114 struct option md_longopts[] = {
115 {NULL, no_argument, NULL, 0}
117 size_t md_longopts_size = sizeof(md_longopts);
119 /* The target specific pseudo-ops which we support. */
120 const pseudo_typeS md_pseudo_table[] =
122 { "am30", set_arch_mach, 300 },
123 { "mn10300", set_arch_mach, 300 },
127 /* Opcode hash table. */
128 static struct hash_control *mn10300_hash;
130 /* This table is sorted. Suitable for searching by a binary search. */
131 static const struct reg_name data_registers[] =
138 #define DATA_REG_NAME_CNT (sizeof(data_registers) / sizeof(struct reg_name))
140 static const struct reg_name address_registers[] =
147 #define ADDRESS_REG_NAME_CNT (sizeof(address_registers) / sizeof(struct reg_name))
150 static const struct reg_name other_registers[] =
156 #define OTHER_REG_NAME_CNT (sizeof(other_registers) / sizeof(struct reg_name))
158 /* reg_name_search does a binary search of the given register table
159 to see if "name" is a valid regiter name. Returns the register
160 number from the array on success, or -1 on failure. */
163 reg_name_search (regs, regcount, name)
164 const struct reg_name *regs;
168 int middle, low, high;
176 middle = (low + high) / 2;
177 cmp = strcasecmp (name, regs[middle].name);
183 return regs[middle].value;
191 /* Summary of register_name().
193 * in: Input_line_pointer points to 1st char of operand.
195 * out: A expressionS.
196 * The operand may have been a register: in this case, X_op == O_register,
197 * X_add_number is set to the register number, and truth is returned.
198 * Input_line_pointer->(next non-blank) char after operand, or is in
199 * its original state.
202 data_register_name (expressionP)
203 expressionS *expressionP;
210 /* Find the spelling of the operand */
211 start = name = input_line_pointer;
213 c = get_symbol_end ();
214 reg_number = reg_name_search (data_registers, DATA_REG_NAME_CNT, name);
216 /* look to see if it's in the register table */
219 expressionP->X_op = O_register;
220 expressionP->X_add_number = reg_number;
222 /* make the rest nice */
223 expressionP->X_add_symbol = NULL;
224 expressionP->X_op_symbol = NULL;
225 *input_line_pointer = c; /* put back the delimiting char */
230 /* reset the line as if we had not done anything */
231 *input_line_pointer = c; /* put back the delimiting char */
232 input_line_pointer = start; /* reset input_line pointer */
237 /* Summary of register_name().
239 * in: Input_line_pointer points to 1st char of operand.
241 * out: A expressionS.
242 * The operand may have been a register: in this case, X_op == O_register,
243 * X_add_number is set to the register number, and truth is returned.
244 * Input_line_pointer->(next non-blank) char after operand, or is in
245 * its original state.
248 address_register_name (expressionP)
249 expressionS *expressionP;
256 /* Find the spelling of the operand */
257 start = name = input_line_pointer;
259 c = get_symbol_end ();
260 reg_number = reg_name_search (address_registers, ADDRESS_REG_NAME_CNT, name);
262 /* look to see if it's in the register table */
265 expressionP->X_op = O_register;
266 expressionP->X_add_number = reg_number;
268 /* make the rest nice */
269 expressionP->X_add_symbol = NULL;
270 expressionP->X_op_symbol = NULL;
271 *input_line_pointer = c; /* put back the delimiting char */
276 /* reset the line as if we had not done anything */
277 *input_line_pointer = c; /* put back the delimiting char */
278 input_line_pointer = start; /* reset input_line pointer */
283 /* Summary of register_name().
285 * in: Input_line_pointer points to 1st char of operand.
287 * out: A expressionS.
288 * The operand may have been a register: in this case, X_op == O_register,
289 * X_add_number is set to the register number, and truth is returned.
290 * Input_line_pointer->(next non-blank) char after operand, or is in
291 * its original state.
294 other_register_name (expressionP)
295 expressionS *expressionP;
302 /* Find the spelling of the operand */
303 start = name = input_line_pointer;
305 c = get_symbol_end ();
306 reg_number = reg_name_search (other_registers, OTHER_REG_NAME_CNT, name);
308 /* look to see if it's in the register table */
311 expressionP->X_op = O_register;
312 expressionP->X_add_number = reg_number;
314 /* make the rest nice */
315 expressionP->X_add_symbol = NULL;
316 expressionP->X_op_symbol = NULL;
317 *input_line_pointer = c; /* put back the delimiting char */
322 /* reset the line as if we had not done anything */
323 *input_line_pointer = c; /* put back the delimiting char */
324 input_line_pointer = start; /* reset input_line pointer */
330 md_show_usage (stream)
333 fprintf(stream, _("MN10300 options:\n\
338 md_parse_option (c, arg)
346 md_undefined_symbol (name)
353 md_atof (type, litp, sizep)
359 LITTLENUM_TYPE words[4];
375 return "bad call to md_atof";
378 t = atof_ieee (input_line_pointer, type, words);
380 input_line_pointer = t;
384 for (i = prec - 1; i >= 0; i--)
386 md_number_to_chars (litp, (valueT) words[i], 2);
395 md_convert_frag (abfd, sec, fragP)
400 static unsigned long label_count = 0;
403 subseg_change (sec, 0);
404 if (fragP->fr_subtype == 0)
406 fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
407 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
411 else if (fragP->fr_subtype == 1)
413 /* Reverse the condition of the first branch. */
414 int offset = fragP->fr_fix;
415 int opcode = fragP->fr_literal[offset] & 0xff;
452 fragP->fr_literal[offset] = opcode;
454 /* Create a fixup for the reversed conditional branch. */
455 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
456 fix_new (fragP, fragP->fr_fix + 1, 1,
457 symbol_new (buf, sec, 0, fragP->fr_next),
458 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
460 /* Now create the unconditional branch + fixup to the
462 fragP->fr_literal[offset + 2] = 0xcc;
463 fix_new (fragP, fragP->fr_fix + 3, 2, fragP->fr_symbol,
464 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
468 else if (fragP->fr_subtype == 2)
470 /* Reverse the condition of the first branch. */
471 int offset = fragP->fr_fix;
472 int opcode = fragP->fr_literal[offset] & 0xff;
509 fragP->fr_literal[offset] = opcode;
511 /* Create a fixup for the reversed conditional branch. */
512 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
513 fix_new (fragP, fragP->fr_fix + 1, 1,
514 symbol_new (buf, sec, 0, fragP->fr_next),
515 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
517 /* Now create the unconditional branch + fixup to the
519 fragP->fr_literal[offset + 2] = 0xdc;
520 fix_new (fragP, fragP->fr_fix + 3, 4, fragP->fr_symbol,
521 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
525 else if (fragP->fr_subtype == 3)
527 fix_new (fragP, fragP->fr_fix + 2, 1, fragP->fr_symbol,
528 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
532 else if (fragP->fr_subtype == 4)
534 /* Reverse the condition of the first branch. */
535 int offset = fragP->fr_fix;
536 int opcode = fragP->fr_literal[offset + 1] & 0xff;
555 fragP->fr_literal[offset + 1] = opcode;
557 /* Create a fixup for the reversed conditional branch. */
558 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
559 fix_new (fragP, fragP->fr_fix + 2, 1,
560 symbol_new (buf, sec, 0, fragP->fr_next),
561 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
563 /* Now create the unconditional branch + fixup to the
565 fragP->fr_literal[offset + 3] = 0xcc;
566 fix_new (fragP, fragP->fr_fix + 4, 2, fragP->fr_symbol,
567 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
571 else if (fragP->fr_subtype == 5)
573 /* Reverse the condition of the first branch. */
574 int offset = fragP->fr_fix;
575 int opcode = fragP->fr_literal[offset + 1] & 0xff;
591 fragP->fr_literal[offset + 1] = opcode;
593 /* Create a fixup for the reversed conditional branch. */
594 sprintf (buf, ".%s_%d", FAKE_LABEL_NAME, label_count++);
595 fix_new (fragP, fragP->fr_fix + 2, 1,
596 symbol_new (buf, sec, 0, fragP->fr_next),
597 fragP->fr_offset + 2, 1, BFD_RELOC_8_PCREL);
599 /* Now create the unconditional branch + fixup to the
601 fragP->fr_literal[offset + 3] = 0xdc;
602 fix_new (fragP, fragP->fr_fix + 4, 4, fragP->fr_symbol,
603 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
607 else if (fragP->fr_subtype == 6)
609 int offset = fragP->fr_fix;
610 fragP->fr_literal[offset] = 0xcd;
611 fix_new (fragP, fragP->fr_fix + 1, 2, fragP->fr_symbol,
612 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
616 else if (fragP->fr_subtype == 7)
618 int offset = fragP->fr_fix;
619 fragP->fr_literal[offset] = 0xdd;
620 fragP->fr_literal[offset + 5] = fragP->fr_literal[offset + 3];
621 fragP->fr_literal[offset + 6] = fragP->fr_literal[offset + 4];
623 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
624 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
628 else if (fragP->fr_subtype == 8)
630 int offset = fragP->fr_fix;
631 fragP->fr_literal[offset] = 0xfa;
632 fragP->fr_literal[offset + 1] = 0xff;
633 fix_new (fragP, fragP->fr_fix + 2, 2, fragP->fr_symbol,
634 fragP->fr_offset + 2, 1, BFD_RELOC_16_PCREL);
638 else if (fragP->fr_subtype == 9)
640 int offset = fragP->fr_fix;
641 fragP->fr_literal[offset] = 0xfc;
642 fragP->fr_literal[offset + 1] = 0xff;
644 fix_new (fragP, fragP->fr_fix + 2, 4, fragP->fr_symbol,
645 fragP->fr_offset + 2, 1, BFD_RELOC_32_PCREL);
649 else if (fragP->fr_subtype == 10)
651 fragP->fr_literal[fragP->fr_fix] = 0xca;
652 fix_new (fragP, fragP->fr_fix + 1, 1, fragP->fr_symbol,
653 fragP->fr_offset + 1, 1, BFD_RELOC_8_PCREL);
657 else if (fragP->fr_subtype == 11)
659 int offset = fragP->fr_fix;
660 fragP->fr_literal[offset] = 0xcc;
662 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
663 fragP->fr_offset + 1, 1, BFD_RELOC_16_PCREL);
667 else if (fragP->fr_subtype == 12)
669 int offset = fragP->fr_fix;
670 fragP->fr_literal[offset] = 0xdc;
672 fix_new (fragP, fragP->fr_fix + 1, 4, fragP->fr_symbol,
673 fragP->fr_offset + 1, 1, BFD_RELOC_32_PCREL);
682 md_section_align (seg, addr)
686 int align = bfd_get_section_alignment (stdoutput, seg);
687 return ((addr + (1 << align) - 1) & (-1 << align));
693 char *prev_name = "";
694 register const struct mn10300_opcode *op;
696 mn10300_hash = hash_new();
698 /* Insert unique names into hash table. The MN10300 instruction set
699 has many identical opcode names that have different opcodes based
700 on the operands. This hash table then provides a quick index to
701 the first opcode with a particular name in the opcode table. */
703 op = mn10300_opcodes;
706 if (strcmp (prev_name, op->name))
708 prev_name = (char *) op->name;
709 hash_insert (mn10300_hash, op->name, (char *) op);
714 /* This is both a simplification (we don't have to write md_apply_fix)
715 and support for future optimizations (branch shortening and similar
716 stuff in the linker). */
719 /* Set the default machine type. */
720 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mn10300, 300))
721 as_warn (_("could not set architecture and machine"));
723 current_machine = 300;
731 struct mn10300_opcode *opcode;
732 struct mn10300_opcode *next_opcode;
733 const unsigned char *opindex_ptr;
734 int next_opindex, relaxable;
735 unsigned long insn, extension, size = 0;
740 /* Get the opcode. */
741 for (s = str; *s != '\0' && ! isspace (*s); s++)
746 /* find the first opcode with the proper name */
747 opcode = (struct mn10300_opcode *)hash_find (mn10300_hash, str);
750 as_bad (_("Unrecognized opcode: `%s'"), str);
755 while (isspace (*str))
758 input_line_pointer = str;
768 errmsg = _("Invalid opcode/operands");
770 /* Reset the array of register operands. */
771 memset (mn10300_reg_operands, -1, sizeof (mn10300_reg_operands));
777 insn = opcode->opcode;
780 /* If the instruction is not available on the current machine
781 then it can not possibly match. */
783 && (opcode->machine != current_machine))
786 for (op_idx = 1, opindex_ptr = opcode->operands;
788 opindex_ptr++, op_idx++)
790 const struct mn10300_operand *operand;
793 if (next_opindex == 0)
795 operand = &mn10300_operands[*opindex_ptr];
799 operand = &mn10300_operands[next_opindex];
803 while (*str == ' ' || *str == ',')
806 if (operand->flags & MN10300_OPERAND_RELAX)
809 /* Gather the operand. */
810 hold = input_line_pointer;
811 input_line_pointer = str;
813 if (operand->flags & MN10300_OPERAND_PAREN)
815 if (*input_line_pointer != ')' && *input_line_pointer != '(')
817 input_line_pointer = hold;
821 input_line_pointer++;
824 /* See if we can match the operands. */
825 else if (operand->flags & MN10300_OPERAND_DREG)
827 if (!data_register_name (&ex))
829 input_line_pointer = hold;
834 else if (operand->flags & MN10300_OPERAND_AREG)
836 if (!address_register_name (&ex))
838 input_line_pointer = hold;
843 else if (operand->flags & MN10300_OPERAND_SP)
845 char *start = input_line_pointer;
846 char c = get_symbol_end ();
848 if (strcasecmp (start, "sp") != 0)
850 *input_line_pointer = c;
851 input_line_pointer = hold;
855 *input_line_pointer = c;
858 else if (operand->flags & MN10300_OPERAND_PSW)
860 char *start = input_line_pointer;
861 char c = get_symbol_end ();
863 if (strcasecmp (start, "psw") != 0)
865 *input_line_pointer = c;
866 input_line_pointer = hold;
870 *input_line_pointer = c;
873 else if (operand->flags & MN10300_OPERAND_MDR)
875 char *start = input_line_pointer;
876 char c = get_symbol_end ();
878 if (strcasecmp (start, "mdr") != 0)
880 *input_line_pointer = c;
881 input_line_pointer = hold;
885 *input_line_pointer = c;
888 else if (operand->flags & MN10300_OPERAND_REG_LIST)
890 unsigned int value = 0;
891 if (*input_line_pointer != '[')
893 input_line_pointer = hold;
899 input_line_pointer++;
901 /* We used to reject a null register list here; however,
902 we accept it now so the compiler can emit "call" instructions
903 for all calls to named functions.
905 The linker can then fill in the appropriate bits for the
906 register list and stack size or change the instruction
907 into a "calls" if using "call" is not profitable. */
908 while (*input_line_pointer != ']')
913 if (*input_line_pointer == ',')
914 input_line_pointer++;
916 start = input_line_pointer;
917 c = get_symbol_end ();
919 if (strcasecmp (start, "d2") == 0)
922 *input_line_pointer = c;
924 else if (strcasecmp (start, "d3") == 0)
927 *input_line_pointer = c;
929 else if (strcasecmp (start, "a2") == 0)
932 *input_line_pointer = c;
934 else if (strcasecmp (start, "a3") == 0)
937 *input_line_pointer = c;
939 else if (strcasecmp (start, "other") == 0)
942 *input_line_pointer = c;
946 input_line_pointer = hold;
951 input_line_pointer++;
952 mn10300_insert_operand (&insn, &extension, operand,
953 value, (char *) NULL, 0, 0);
957 else if (data_register_name (&ex))
959 input_line_pointer = hold;
963 else if (address_register_name (&ex))
965 input_line_pointer = hold;
969 else if (other_register_name (&ex))
971 input_line_pointer = hold;
975 else if (*str == ')' || *str == '(')
977 input_line_pointer = hold;
989 errmsg = _("illegal operand");
992 errmsg = _("missing operand");
998 mask = MN10300_OPERAND_DREG | MN10300_OPERAND_AREG;
999 if ((operand->flags & mask) == 0)
1001 input_line_pointer = hold;
1006 if (opcode->format == FMT_D1 || opcode->format == FMT_S1)
1008 else if (opcode->format == FMT_D2
1009 || opcode->format == FMT_D4
1010 || opcode->format == FMT_S2
1011 || opcode->format == FMT_S4
1012 || opcode->format == FMT_S6
1013 || opcode->format == FMT_D5)
1018 mn10300_insert_operand (&insn, &extension, operand,
1019 ex.X_add_number, (char *) NULL,
1023 /* And note the register number in the register array. */
1024 mn10300_reg_operands[op_idx - 1] = ex.X_add_number;
1029 /* If this operand can be promoted, and it doesn't
1030 fit into the allocated bitfield for this insn,
1031 then promote it (ie this opcode does not match). */
1033 & (MN10300_OPERAND_PROMOTE | MN10300_OPERAND_RELAX)
1034 && ! check_operand (insn, operand, ex.X_add_number))
1036 input_line_pointer = hold;
1041 mn10300_insert_operand (&insn, &extension, operand,
1042 ex.X_add_number, (char *) NULL,
1047 /* If this operand can be promoted, then this opcode didn't
1048 match since we can't know if it needed promotion! */
1049 if (operand->flags & MN10300_OPERAND_PROMOTE)
1051 input_line_pointer = hold;
1056 /* We need to generate a fixup for this expression. */
1057 if (fc >= MAX_INSN_FIXUPS)
1058 as_fatal (_("too many fixups"));
1059 fixups[fc].exp = ex;
1060 fixups[fc].opindex = *opindex_ptr;
1061 fixups[fc].reloc = BFD_RELOC_UNUSED;
1067 str = input_line_pointer;
1068 input_line_pointer = hold;
1070 while (*str == ' ' || *str == ',')
1075 /* Make sure we used all the operands! */
1079 /* If this instruction has registers that must not match, verify
1080 that they do indeed not match. */
1081 if (opcode->no_match_operands)
1085 /* Look at each operand to see if it's marked. */
1086 for (i = 0; i < MN10300_MAX_OPERANDS; i++)
1088 if ((1 << i) & opcode->no_match_operands)
1092 /* operand I is marked. Check that it does not match any
1093 operands > I which are marked. */
1094 for (j = i + 1; j < MN10300_MAX_OPERANDS; j++)
1096 if (((1 << j) & opcode->no_match_operands)
1097 && mn10300_reg_operands[i] == mn10300_reg_operands[j])
1099 errmsg = _("Invalid register specification.");
1111 next_opcode = opcode + 1;
1112 if (!strcmp(next_opcode->name, opcode->name))
1114 opcode = next_opcode;
1118 as_bad ("%s", errmsg);
1124 while (isspace (*str))
1128 as_bad (_("junk at end of line: `%s'"), str);
1130 input_line_pointer = str;
1132 /* Determine the size of the instruction. */
1133 if (opcode->format == FMT_S0)
1136 if (opcode->format == FMT_S1 || opcode->format == FMT_D0)
1139 if (opcode->format == FMT_S2 || opcode->format == FMT_D1)
1143 if (opcode->format == FMT_S4)
1146 if (opcode->format == FMT_S6 || opcode->format == FMT_D5)
1149 if (opcode->format == FMT_D2)
1152 if (opcode->format == FMT_D4)
1155 if (relaxable && fc > 0)
1162 /* Handle bra specially. Basically treat it like jmp so
1163 that we automatically handle 8, 16 and 32 bit offsets
1164 correctly as well as jumps to an undefined address.
1166 It is also important to not treat it like other bCC
1167 instructions since the long forms of bra is different
1168 from other bCC instructions. */
1169 if (opcode->opcode == 0xca00)
1181 else if (size == 3 && opcode->opcode == 0xcc0000)
1183 /* bCC (uncommon cases) */
1187 f = frag_var (rs_machine_dependent, 8, 8 - size, type,
1188 fixups[0].exp.X_add_symbol,
1189 fixups[0].exp.X_add_number,
1190 (char *)fixups[0].opindex);
1192 /* This is pretty hokey. We basically just care about the
1193 opcode, so we have to write out the first word big endian.
1195 The exception is "call", which has two operands that we
1198 The first operand (the register list) happens to be in the
1199 first instruction word, and will be in the right place if
1200 we output the first word in big endian mode.
1202 The second operand (stack size) is in the extension word,
1203 and we want it to appear as the first character in the extension
1204 word (as it appears in memory). Luckily, writing the extension
1205 word in big endian format will do what we want. */
1206 number_to_chars_bigendian (f, insn, size > 4 ? 4 : size);
1209 number_to_chars_bigendian (f + 4, extension, 4);
1210 number_to_chars_bigendian (f + 8, 0, size - 8);
1213 number_to_chars_bigendian (f + 4, extension, size - 4);
1217 /* Allocate space for the instruction. */
1218 f = frag_more (size);
1220 /* Fill in bytes for the instruction. Note that opcode fields
1221 are written big-endian, 16 & 32bit immediates are written
1222 little endian. Egad. */
1223 if (opcode->format == FMT_S0
1224 || opcode->format == FMT_S1
1225 || opcode->format == FMT_D0
1226 || opcode->format == FMT_D1)
1228 number_to_chars_bigendian (f, insn, size);
1230 else if (opcode->format == FMT_S2
1231 && opcode->opcode != 0xdf0000
1232 && opcode->opcode != 0xde0000)
1234 /* A format S2 instruction that is _not_ "ret" and "retf". */
1235 number_to_chars_bigendian (f, (insn >> 16) & 0xff, 1);
1236 number_to_chars_littleendian (f + 1, insn & 0xffff, 2);
1238 else if (opcode->format == FMT_S2)
1240 /* This must be a ret or retf, which is written entirely in
1241 big-endian format. */
1242 number_to_chars_bigendian (f, insn, 3);
1244 else if (opcode->format == FMT_S4
1245 && opcode->opcode != 0xdc000000)
1247 /* This must be a format S4 "call" instruction. What a pain. */
1248 unsigned long temp = (insn >> 8) & 0xffff;
1249 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1250 number_to_chars_littleendian (f + 1, temp, 2);
1251 number_to_chars_bigendian (f + 3, insn & 0xff, 1);
1252 number_to_chars_bigendian (f + 4, extension & 0xff, 1);
1254 else if (opcode->format == FMT_S4)
1256 /* This must be a format S4 "jmp" instruction. */
1257 unsigned long temp = ((insn & 0xffffff) << 8) | (extension & 0xff);
1258 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1259 number_to_chars_littleendian (f + 1, temp, 4);
1261 else if (opcode->format == FMT_S6)
1263 unsigned long temp = ((insn & 0xffffff) << 8)
1264 | ((extension >> 16) & 0xff);
1265 number_to_chars_bigendian (f, (insn >> 24) & 0xff, 1);
1266 number_to_chars_littleendian (f + 1, temp, 4);
1267 number_to_chars_bigendian (f + 5, (extension >> 8) & 0xff, 1);
1268 number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1270 else if (opcode->format == FMT_D2
1271 && opcode->opcode != 0xfaf80000
1272 && opcode->opcode != 0xfaf00000
1273 && opcode->opcode != 0xfaf40000)
1275 /* A format D2 instruction where the 16bit immediate is
1276 really a single 16bit value, not two 8bit values. */
1277 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1278 number_to_chars_littleendian (f + 2, insn & 0xffff, 2);
1280 else if (opcode->format == FMT_D2)
1282 /* A format D2 instruction where the 16bit immediate
1283 is really two 8bit immediates. */
1284 number_to_chars_bigendian (f, insn, 4);
1286 else if (opcode->format == FMT_D4)
1288 unsigned long temp = ((insn & 0xffff) << 16) | (extension & 0xffff);
1289 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1290 number_to_chars_littleendian (f + 2, temp, 4);
1292 else if (opcode->format == FMT_D5)
1294 unsigned long temp = ((insn & 0xffff) << 16)
1295 | ((extension >> 8) & 0xffff);
1296 number_to_chars_bigendian (f, (insn >> 16) & 0xffff, 2);
1297 number_to_chars_littleendian (f + 2, temp, 4);
1298 number_to_chars_bigendian (f + 6, extension & 0xff, 1);
1301 /* Create any fixups. */
1302 for (i = 0; i < fc; i++)
1304 const struct mn10300_operand *operand;
1306 operand = &mn10300_operands[fixups[i].opindex];
1307 if (fixups[i].reloc != BFD_RELOC_UNUSED)
1309 reloc_howto_type *reloc_howto;
1314 reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc);
1319 size = bfd_get_reloc_size (reloc_howto);
1321 if (size < 1 || size > 4)
1325 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1326 size, &fixups[i].exp,
1327 reloc_howto->pc_relative,
1332 int reloc, pcrel, reloc_size, offset;
1335 reloc = BFD_RELOC_NONE;
1336 /* How big is the reloc? Remember SPLIT relocs are
1337 implicitly 32bits. */
1338 if ((operand->flags & MN10300_OPERAND_SPLIT) != 0)
1341 reloc_size = operand->bits;
1343 /* Is the reloc pc-relative? */
1344 pcrel = (operand->flags & MN10300_OPERAND_PCREL) != 0;
1346 /* Gross. This disgusting hack is to make sure we
1347 get the right offset for the 16/32 bit reloc in
1348 "call" instructions. Basically they're a pain
1349 because the reloc isn't at the end of the instruction. */
1350 if ((size == 5 || size == 7)
1351 && (((insn >> 24) & 0xff) == 0xcd
1352 || ((insn >> 24) & 0xff) == 0xdd))
1355 /* Similarly for certain bit instructions which don't
1356 hav their 32bit reloc at the tail of the instruction. */
1358 && (((insn >> 16) & 0xffff) == 0xfe00
1359 || ((insn >> 16) & 0xffff) == 0xfe01
1360 || ((insn >> 16) & 0xffff) == 0xfe02))
1363 offset = size - reloc_size / 8;
1365 /* Choose a proper BFD relocation type. */
1368 if (reloc_size == 32)
1369 reloc = BFD_RELOC_32_PCREL;
1370 else if (reloc_size == 16)
1371 reloc = BFD_RELOC_16_PCREL;
1372 else if (reloc_size == 8)
1373 reloc = BFD_RELOC_8_PCREL;
1379 if (reloc_size == 32)
1380 reloc = BFD_RELOC_32;
1381 else if (reloc_size == 16)
1382 reloc = BFD_RELOC_16;
1383 else if (reloc_size == 8)
1384 reloc = BFD_RELOC_8;
1389 /* Convert the size of the reloc into what fix_new_exp wants. */
1390 reloc_size = reloc_size / 8;
1391 if (reloc_size == 8)
1393 else if (reloc_size == 16)
1395 else if (reloc_size == 32)
1398 fixP = fix_new_exp (frag_now, f - frag_now->fr_literal + offset,
1399 reloc_size, &fixups[i].exp, pcrel,
1400 ((bfd_reloc_code_real_type) reloc));
1403 fixP->fx_offset += offset;
1410 /* if while processing a fixup, a reloc really needs to be created */
1411 /* then it is done here */
1414 tc_gen_reloc (seg, fixp)
1419 reloc = (arelent *) xmalloc (sizeof (arelent));
1421 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1422 if (reloc->howto == (reloc_howto_type *) NULL)
1424 as_bad_where (fixp->fx_file, fixp->fx_line,
1425 _("reloc %d not supported by object file format"),
1426 (int)fixp->fx_r_type);
1429 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1431 if (fixp->fx_addsy && fixp->fx_subsy)
1434 if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
1435 || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
1437 as_bad_where (fixp->fx_file, fixp->fx_line,
1438 "Difference of symbols in different sections is not supported");
1442 reloc->sym_ptr_ptr = &bfd_abs_symbol;
1443 reloc->addend = (S_GET_VALUE (fixp->fx_addsy)
1444 - S_GET_VALUE (fixp->fx_subsy) + fixp->fx_offset);
1448 reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym;
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;