1 /* tc-m68hc11.c -- Assembler code for the Motorola 68HC11 & 68HC12.
2 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Stephane Carrez (stcarrez@nerim.fr)
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. */
23 #include "safe-ctype.h"
25 #include "opcode/m68hc11.h"
26 #include "dwarf2dbg.h"
27 #include "elf/m68hc11.h"
29 const char comment_chars[] = ";!";
30 const char line_comment_chars[] = "#*";
31 const char line_separator_chars[] = "";
33 const char EXP_CHARS[] = "eE";
34 const char FLT_CHARS[] = "dD";
36 #define STATE_CONDITIONAL_BRANCH (1)
37 #define STATE_PC_RELATIVE (2)
38 #define STATE_INDEXED_OFFSET (3)
39 #define STATE_INDEXED_PCREL (4)
40 #define STATE_XBCC_BRANCH (5)
41 #define STATE_CONDITIONAL_BRANCH_6812 (6)
43 #define STATE_BYTE (0)
44 #define STATE_BITS5 (0)
45 #define STATE_WORD (1)
46 #define STATE_BITS9 (1)
47 #define STATE_LONG (2)
48 #define STATE_BITS16 (2)
49 #define STATE_UNDF (3) /* Symbol undefined in pass1 */
51 /* This macro has no side-effects. */
52 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
53 #define RELAX_STATE(s) ((s) >> 2)
54 #define RELAX_LENGTH(s) ((s) & 3)
56 #define IS_OPCODE(C1,C2) (((C1) & 0x0FF) == ((C2) & 0x0FF))
58 /* This table describes how you change sizes for the various types of variable
59 size expressions. This version only supports two kinds. */
62 How far Forward this mode will reach.
63 How far Backward this mode will reach.
64 How many bytes this mode will add to the size of the frag.
65 Which mode to go to if the offset won't fit in this one. */
67 relax_typeS md_relax_table[] = {
68 {1, 1, 0, 0}, /* First entries aren't used. */
69 {1, 1, 0, 0}, /* For no good reason except. */
70 {1, 1, 0, 0}, /* that the VAX doesn't either. */
74 These insns are translated into b!cc +3 jmp L. */
75 {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD)},
80 /* Relax for bsr <L> and bra <L>.
81 These insns are translated into jsr and jmp. */
82 {(127), (-128), 0, ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD)},
87 /* Relax for indexed offset: 5-bits, 9-bits, 16-bits. */
88 {(15), (-16), 0, ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9)},
89 {(255), (-256), 1, ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16)},
93 /* Relax for PC relative offset: 5-bits, 9-bits, 16-bits.
94 For the 9-bit case, there will be a -1 correction to take into
95 account the new byte that's why the range is -255..256. */
96 {(15), (-16), 0, ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9)},
97 {(256), (-255), 1, ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16)},
101 /* Relax for dbeq/ibeq/tbeq r,<L>:
102 These insns are translated into db!cc +3 jmp L. */
103 {(255), (-256), 0, ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD)},
108 /* Relax for bcc <L> on 68HC12.
109 These insns are translated into lbcc <L>. */
110 {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD)},
117 /* 68HC11 and 68HC12 registers. They are numbered according to the 68HC12. */
118 typedef enum register_id {
130 typedef struct operand {
137 struct m68hc11_opcode_def {
143 struct m68hc11_opcode *opcode;
146 static struct m68hc11_opcode_def *m68hc11_opcode_defs = 0;
147 static int m68hc11_nb_opcode_defs = 0;
149 typedef struct alias {
154 static alias alias_opcodes[] = {
161 /* Local functions. */
162 static register_id reg_name_search PARAMS ((char *));
163 static register_id register_name PARAMS ((void));
164 static int cmp_opcode PARAMS ((struct m68hc11_opcode *,
165 struct m68hc11_opcode *));
166 static char *print_opcode_format PARAMS ((struct m68hc11_opcode *, int));
167 static char *skip_whites PARAMS ((char *));
168 static int check_range PARAMS ((long, int));
169 static void print_opcode_list PARAMS ((void));
170 static void get_default_target PARAMS ((void));
171 static void print_insn_format PARAMS ((char *));
172 static int get_operand PARAMS ((operand *, int, long));
173 static void fixup8 PARAMS ((expressionS *, int, int));
174 static void fixup16 PARAMS ((expressionS *, int, int));
175 static void fixup24 PARAMS ((expressionS *, int, int));
176 static unsigned char convert_branch PARAMS ((unsigned char));
177 static char *m68hc11_new_insn PARAMS ((int));
178 static void build_dbranch_insn PARAMS ((struct m68hc11_opcode *,
179 operand *, int, int));
180 static int build_indexed_byte PARAMS ((operand *, int, int));
181 static int build_reg_mode PARAMS ((operand *, int));
183 static struct m68hc11_opcode *find
184 PARAMS ((struct m68hc11_opcode_def *, operand *, int));
185 static struct m68hc11_opcode *find_opcode
186 PARAMS ((struct m68hc11_opcode_def *, operand *, int *));
187 static void build_jump_insn
188 PARAMS ((struct m68hc11_opcode *, operand *, int, int));
189 static void build_insn
190 PARAMS ((struct m68hc11_opcode *, operand *, int));
191 static int relaxable_symbol PARAMS ((symbolS *));
193 /* Pseudo op to indicate a relax group. */
194 static void s_m68hc11_relax PARAMS((int));
196 /* Pseudo op to control the ELF flags. */
197 static void s_m68hc11_mode PARAMS ((int));
199 /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
200 are using 'rtc' for returning. It is necessary to use 'call'
201 to invoke them. This is also used by the debugger to correctly
202 find the stack frame. */
203 static void s_m68hc11_mark_symbol PARAMS ((int));
205 /* Controls whether relative branches can be turned into long branches.
206 When the relative offset is too large, the insn are changed:
214 Setting the flag forbidds this. */
215 static short flag_fixed_branchs = 0;
217 /* Force to use long jumps (absolute) instead of relative branches. */
218 static short flag_force_long_jumps = 0;
220 /* Change the direct addressing mode into an absolute addressing mode
221 when the insn does not support direct addressing.
222 For example, "clr *ZD0" is normally not possible and is changed
224 static short flag_strict_direct_addressing = 1;
226 /* When an opcode has invalid operand, print out the syntax of the opcode
228 static short flag_print_insn_syntax = 0;
230 /* Dumps the list of instructions with syntax and then exit:
231 1 -> Only dumps the list (sorted by name)
232 2 -> Generate an example (or test) that can be compiled. */
233 static short flag_print_opcodes = 0;
235 /* Opcode hash table. */
236 static struct hash_control *m68hc11_hash;
238 /* Current cpu (either cpu6811 or cpu6812). This is determined automagically
239 by 'get_default_target' by looking at default BFD vector. This is overriden
240 with the -m<cpu> option. */
241 static int current_architecture = 0;
243 /* Default cpu determined by 'get_default_target'. */
244 static const char *default_cpu;
246 /* Number of opcodes in the sorted table (filtered by current cpu). */
247 static int num_opcodes;
249 /* The opcodes sorted by name and filtered by current cpu. */
250 static struct m68hc11_opcode *m68hc11_sorted_opcodes;
252 /* ELF flags to set in the output file header. */
253 static int elf_flags = E_M68HC11_F64;
255 /* These are the machine dependent pseudo-ops. These are included so
256 the assembler can work on the output from the SUN C compiler, which
259 /* This table describes all the machine specific pseudo-ops the assembler
260 has to support. The fields are:
261 pseudo-op name without dot
262 function to call to execute this pseudo-op
263 Integer arg to pass to the function. */
264 const pseudo_typeS md_pseudo_table[] = {
265 /* The following pseudo-ops are supported for MRI compatibility. */
268 {"fcc", stringer, 1},
271 /* Dwarf2 support for Gcc. */
272 {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
273 {"loc", dwarf2_directive_loc, 0},
276 {"xrefb", s_ignore, 0}, /* Same as xref */
278 /* Gcc driven relaxation. */
279 {"relax", s_m68hc11_relax, 0},
281 /* .mode instruction (ala SH). */
282 {"mode", s_m68hc11_mode, 0},
284 /* .far instruction. */
285 {"far", s_m68hc11_mark_symbol, STO_M68HC12_FAR},
287 /* .interrupt instruction. */
288 {"interrupt", s_m68hc11_mark_symbol, STO_M68HC12_INTERRUPT},
293 /* Options and initialization. */
295 const char *md_shortopts = "Sm:";
297 struct option md_longopts[] = {
298 #define OPTION_FORCE_LONG_BRANCH (OPTION_MD_BASE)
299 {"force-long-branchs", no_argument, NULL, OPTION_FORCE_LONG_BRANCH},
301 #define OPTION_SHORT_BRANCHS (OPTION_MD_BASE + 1)
302 {"short-branchs", no_argument, NULL, OPTION_SHORT_BRANCHS},
304 #define OPTION_STRICT_DIRECT_MODE (OPTION_MD_BASE + 2)
305 {"strict-direct-mode", no_argument, NULL, OPTION_STRICT_DIRECT_MODE},
307 #define OPTION_PRINT_INSN_SYNTAX (OPTION_MD_BASE + 3)
308 {"print-insn-syntax", no_argument, NULL, OPTION_PRINT_INSN_SYNTAX},
310 #define OPTION_PRINT_OPCODES (OPTION_MD_BASE + 4)
311 {"print-opcodes", no_argument, NULL, OPTION_PRINT_OPCODES},
313 #define OPTION_GENERATE_EXAMPLE (OPTION_MD_BASE + 5)
314 {"generate-example", no_argument, NULL, OPTION_GENERATE_EXAMPLE},
316 #define OPTION_MSHORT (OPTION_MD_BASE + 6)
317 {"mshort", no_argument, NULL, OPTION_MSHORT},
319 #define OPTION_MLONG (OPTION_MD_BASE + 7)
320 {"mlong", no_argument, NULL, OPTION_MLONG},
322 #define OPTION_MSHORT_DOUBLE (OPTION_MD_BASE + 8)
323 {"mshort-double", no_argument, NULL, OPTION_MSHORT_DOUBLE},
325 #define OPTION_MLONG_DOUBLE (OPTION_MD_BASE + 9)
326 {"mlong-double", no_argument, NULL, OPTION_MLONG_DOUBLE},
328 {NULL, no_argument, NULL, 0}
330 size_t md_longopts_size = sizeof (md_longopts);
332 /* Get the target cpu for the assembler. This is based on the configure
333 options and on the -m68hc11/-m68hc12 option. If no option is specified,
334 we must get the default. */
336 m68hc11_arch_format ()
338 get_default_target ();
339 if (current_architecture & cpu6811)
340 return "elf32-m68hc11";
342 return "elf32-m68hc12";
345 enum bfd_architecture
348 get_default_target ();
349 if (current_architecture & cpu6811)
350 return bfd_arch_m68hc11;
352 return bfd_arch_m68hc12;
361 /* Listing header selected according to cpu. */
363 m68hc11_listing_header ()
365 if (current_architecture & cpu6811)
366 return "M68HC11 GAS ";
368 return "M68HC12 GAS ";
372 md_show_usage (stream)
375 get_default_target ();
376 fprintf (stream, _("\
377 Motorola 68HC11/68HC12/68HCS12 options:\n\
378 -m68hc11 | -m68hc12 |\n\
379 -m68hcs12 specify the processor [default %s]\n\
380 -mshort use 16-bit int ABI (default)\n\
381 -mlong use 32-bit int ABI\n\
382 -mshort-double use 32-bit double ABI\n\
383 -mlong-double use 64-bit double ABI (default)\n\
384 --force-long-branchs always turn relative branchs into absolute ones\n\
385 -S,--short-branchs do not turn relative branchs into absolute ones\n\
386 when the offset is out of range\n\
387 --strict-direct-mode do not turn the direct mode into extended mode\n\
388 when the instruction does not support direct mode\n\
389 --print-insn-syntax print the syntax of instruction in case of error\n\
390 --print-opcodes print the list of instructions with syntax\n\
391 --generate-example generate an example of each instruction\n\
392 (used for testing)\n"), default_cpu);
396 /* Try to identify the default target based on the BFD library. */
398 get_default_target ()
400 const bfd_target *target;
403 if (current_architecture != 0)
406 default_cpu = "unknown";
407 target = bfd_find_target (0, &abfd);
408 if (target && target->name)
410 if (strcmp (target->name, "elf32-m68hc12") == 0)
412 current_architecture = cpu6812;
413 default_cpu = "m68hc12";
415 else if (strcmp (target->name, "elf32-m68hc11") == 0)
417 current_architecture = cpu6811;
418 default_cpu = "m68hc11";
422 as_bad (_("Default target `%s' is not supported."), target->name);
428 m68hc11_print_statistics (file)
432 struct m68hc11_opcode_def *opc;
434 hash_print_statistics (file, "opcode table", m68hc11_hash);
436 opc = m68hc11_opcode_defs;
437 if (opc == 0 || m68hc11_nb_opcode_defs == 0)
440 /* Dump the opcode statistics table. */
441 fprintf (file, _("Name # Modes Min ops Max ops Modes mask # Used\n"));
442 for (i = 0; i < m68hc11_nb_opcode_defs; i++, opc++)
444 fprintf (file, "%-7.7s %5d %7d %7d 0x%08lx %7d\n",
447 opc->min_operands, opc->max_operands, opc->format, opc->used);
452 md_parse_option (c, arg)
456 get_default_target ();
459 /* -S means keep external to 2 bit offset rather than 16 bit one. */
460 case OPTION_SHORT_BRANCHS:
462 flag_fixed_branchs = 1;
465 case OPTION_FORCE_LONG_BRANCH:
466 flag_force_long_jumps = 1;
469 case OPTION_PRINT_INSN_SYNTAX:
470 flag_print_insn_syntax = 1;
473 case OPTION_PRINT_OPCODES:
474 flag_print_opcodes = 1;
477 case OPTION_STRICT_DIRECT_MODE:
478 flag_strict_direct_addressing = 0;
481 case OPTION_GENERATE_EXAMPLE:
482 flag_print_opcodes = 2;
486 elf_flags &= ~E_M68HC11_I32;
490 elf_flags |= E_M68HC11_I32;
493 case OPTION_MSHORT_DOUBLE:
494 elf_flags &= ~E_M68HC11_F64;
497 case OPTION_MLONG_DOUBLE:
498 elf_flags |= E_M68HC11_F64;
502 if (strcasecmp (arg, "68hc11") == 0)
503 current_architecture = cpu6811;
504 else if (strcasecmp (arg, "68hc12") == 0)
505 current_architecture = cpu6812;
506 else if (strcasecmp (arg, "68hcs12") == 0)
507 current_architecture = cpu6812 | cpu6812s;
509 as_bad (_("Option `%s' is not recognized."), arg);
520 md_undefined_symbol (name)
521 char *name ATTRIBUTE_UNUSED;
526 /* Equal to MAX_PRECISION in atof-ieee.c. */
527 #define MAX_LITTLENUMS 6
529 /* Turn a string in input_line_pointer into a floating point constant
530 of type TYPE, and store the appropriate bytes in *LITP. The number
531 of LITTLENUMS emitted is stored in *SIZEP. An error message is
532 returned, or NULL on OK. */
534 md_atof (type, litP, sizeP)
540 LITTLENUM_TYPE words[MAX_LITTLENUMS];
541 LITTLENUM_TYPE *wordP;
572 return _("Bad call to MD_ATOF()");
574 t = atof_ieee (input_line_pointer, type, words);
576 input_line_pointer = t;
578 *sizeP = prec * sizeof (LITTLENUM_TYPE);
579 for (wordP = words; prec--;)
581 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
582 litP += sizeof (LITTLENUM_TYPE);
588 md_section_align (seg, addr)
592 int align = bfd_get_section_alignment (stdoutput, seg);
593 return ((addr + (1 << align) - 1) & (-1 << align));
597 cmp_opcode (op1, op2)
598 struct m68hc11_opcode *op1;
599 struct m68hc11_opcode *op2;
601 return strcmp (op1->name, op2->name);
604 #define IS_CALL_SYMBOL(MODE) \
605 (((MODE) & (M6812_OP_PAGE|M6811_OP_IND16)) \
606 == ((M6812_OP_PAGE|M6811_OP_IND16)))
608 /* Initialize the assembler. Create the opcode hash table
609 (sorted on the names) with the M6811 opcode table
610 (from opcode library). */
614 char *prev_name = "";
615 struct m68hc11_opcode *opcodes;
616 struct m68hc11_opcode_def *opc = 0;
619 get_default_target ();
621 m68hc11_hash = hash_new ();
623 /* Get a writable copy of the opcode table and sort it on the names. */
624 opcodes = (struct m68hc11_opcode *) xmalloc (m68hc11_num_opcodes *
627 m68hc11_sorted_opcodes = opcodes;
629 for (i = 0; i < m68hc11_num_opcodes; i++)
631 if (m68hc11_opcodes[i].arch & current_architecture)
633 opcodes[num_opcodes] = m68hc11_opcodes[i];
634 if (opcodes[num_opcodes].name[0] == 'b'
635 && opcodes[num_opcodes].format & M6811_OP_JUMP_REL
636 && !(opcodes[num_opcodes].format & M6811_OP_BITMASK))
639 opcodes[num_opcodes] = m68hc11_opcodes[i];
642 for (j = 0; alias_opcodes[j].name != 0; j++)
643 if (strcmp (m68hc11_opcodes[i].name, alias_opcodes[j].name) == 0)
645 opcodes[num_opcodes] = m68hc11_opcodes[i];
646 opcodes[num_opcodes].name = alias_opcodes[j].alias;
652 qsort (opcodes, num_opcodes, sizeof (struct m68hc11_opcode),
653 (int (*) PARAMS ((const PTR, const PTR))) cmp_opcode);
655 opc = (struct m68hc11_opcode_def *)
656 xmalloc (num_opcodes * sizeof (struct m68hc11_opcode_def));
657 m68hc11_opcode_defs = opc--;
659 /* Insert unique names into hash table. The M6811 instruction set
660 has several identical opcode names that have different opcodes based
661 on the operands. This hash table then provides a quick index to
662 the first opcode with a particular name in the opcode table. */
663 for (i = 0; i < num_opcodes; i++, opcodes++)
667 if (strcmp (prev_name, opcodes->name))
669 prev_name = (char *) opcodes->name;
673 opc->min_operands = 100;
674 opc->max_operands = 0;
676 opc->opcode = opcodes;
678 hash_insert (m68hc11_hash, opcodes->name, (char *) opc);
681 opc->format |= opcodes->format;
683 /* See how many operands this opcode needs. */
685 if (opcodes->format & M6811_OP_MASK)
687 if (opcodes->format & M6811_OP_BITMASK)
689 if (opcodes->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
691 if (opcodes->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
693 /* Special case for call instruction. */
694 if ((opcodes->format & M6812_OP_PAGE)
695 && !(opcodes->format & M6811_OP_IND16))
698 if (expect < opc->min_operands)
699 opc->min_operands = expect;
700 if (IS_CALL_SYMBOL (opcodes->format))
702 if (expect > opc->max_operands)
703 opc->max_operands = expect;
706 m68hc11_nb_opcode_defs = opc - m68hc11_opcode_defs;
708 if (flag_print_opcodes)
710 print_opcode_list ();
716 m68hc11_init_after_args ()
722 /* Return a string that represents the operand format for the instruction.
723 When example is true, this generates an example of operand. This is used
724 to give an example and also to generate a test. */
726 print_opcode_format (opcode, example)
727 struct m68hc11_opcode *opcode;
730 static char buf[128];
731 int format = opcode->format;
736 if (format & M6811_OP_IMM8)
739 sprintf (p, "#%d", rand () & 0x0FF);
741 strcpy (p, _("#<imm8>"));
745 if (format & M6811_OP_IMM16)
748 sprintf (p, "#%d", rand () & 0x0FFFF);
750 strcpy (p, _("#<imm16>"));
754 if (format & M6811_OP_IX)
757 sprintf (p, "%d,X", rand () & 0x0FF);
759 strcpy (p, _("<imm8>,X"));
763 if (format & M6811_OP_IY)
766 sprintf (p, "%d,X", rand () & 0x0FF);
768 strcpy (p, _("<imm8>,X"));
772 if (format & M6812_OP_IDX)
775 sprintf (p, "%d,X", rand () & 0x0FF);
781 if (format & M6812_OP_PAGE)
784 sprintf (p, ", %d", rand () & 0x0FF);
786 strcpy (p, ", <page>");
790 if (format & M6811_OP_DIRECT)
793 sprintf (p, "*Z%d", rand () & 0x0FF);
795 strcpy (p, _("*<abs8>"));
799 if (format & M6811_OP_BITMASK)
805 sprintf (p, "#$%02x", rand () & 0x0FF);
807 strcpy (p, _("#<mask>"));
810 if (format & M6811_OP_JUMP_REL)
814 if (format & M6811_OP_IND16)
817 sprintf (p, _("symbol%d"), rand () & 0x0FF);
819 strcpy (p, _("<abs>"));
824 if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
828 if (format & M6811_OP_BITMASK)
830 sprintf (p, ".+%d", rand () & 0x7F);
834 sprintf (p, "L%d", rand () & 0x0FF);
838 strcpy (p, _("<label>"));
844 /* Prints the list of instructions with the possible operands. */
849 char *prev_name = "";
850 struct m68hc11_opcode *opcodes;
851 int example = flag_print_opcodes == 2;
854 printf (_("# Example of `%s' instructions\n\t.sect .text\n_start:\n"),
857 opcodes = m68hc11_sorted_opcodes;
859 /* Walk the list sorted on names (by md_begin). We only report
860 one instruction per line, and we collect the different operand
862 for (i = 0; i < num_opcodes; i++, opcodes++)
864 char *fmt = print_opcode_format (opcodes, example);
868 printf ("L%d:\t", i);
869 printf ("%s %s\n", opcodes->name, fmt);
873 if (strcmp (prev_name, opcodes->name))
878 printf ("%-5.5s ", opcodes->name);
879 prev_name = (char *) opcodes->name;
882 printf (" [%s]", fmt);
888 /* Print the instruction format. This operation is called when some
889 instruction is not correct. Instruction format is printed as an
892 print_insn_format (name)
895 struct m68hc11_opcode_def *opc;
896 struct m68hc11_opcode *opcode;
899 opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, name);
902 as_bad (_("Instruction `%s' is not recognized."), name);
905 opcode = opc->opcode;
907 as_bad (_("Instruction formats for `%s':"), name);
912 fmt = print_opcode_format (opcode, 0);
913 sprintf (buf, "\t%-5.5s %s", opcode->name, fmt);
918 while (strcmp (opcode->name, name) == 0);
921 /* Analysis of 68HC11 and 68HC12 operands. */
923 /* reg_name_search() finds the register number given its name.
924 Returns the register number or REG_NONE on failure. */
926 reg_name_search (name)
929 if (strcasecmp (name, "x") == 0 || strcasecmp (name, "ix") == 0)
931 if (strcasecmp (name, "y") == 0 || strcasecmp (name, "iy") == 0)
933 if (strcasecmp (name, "a") == 0)
935 if (strcasecmp (name, "b") == 0)
937 if (strcasecmp (name, "d") == 0)
939 if (strcasecmp (name, "sp") == 0)
941 if (strcasecmp (name, "pc") == 0)
943 if (strcasecmp (name, "ccr") == 0)
953 while (*p == ' ' || *p == '\t')
959 /* Check the string at input_line_pointer
960 to see if it is a valid register name. */
964 register_id reg_number;
965 char c, *p = input_line_pointer;
967 if (!is_name_beginner (*p++))
970 while (is_part_of_name (*p++))
977 /* Look to see if it's in the register table. */
978 reg_number = reg_name_search (input_line_pointer);
979 if (reg_number != REG_NONE)
984 input_line_pointer = p;
992 #define M6811_OP_CALL_ADDR 0x00800000
993 #define M6811_OP_PAGE_ADDR 0x04000000
995 /* Parse a string of operands and return an array of expressions.
997 Operand mode[0] mode[1] exp[0] exp[1]
998 #n M6811_OP_IMM16 - O_*
999 *<exp> M6811_OP_DIRECT - O_*
1000 .{+-}<exp> M6811_OP_JUMP_REL - O_*
1001 <exp> M6811_OP_IND16 - O_*
1002 ,r N,r M6812_OP_IDX M6812_OP_REG O_constant O_register
1003 n,-r M6812_PRE_DEC M6812_OP_REG O_constant O_register
1004 n,+r M6812_PRE_INC " "
1005 n,r- M6812_POST_DEC " "
1006 n,r+ M6812_POST_INC " "
1007 A,r B,r D,r M6811_OP_REG M6812_OP_REG O_register O_register
1008 [D,r] M6811_OP_D_IDX M6812_OP_REG O_register O_register
1009 [n,r] M6811_OP_D_IDX_2 M6812_OP_REG O_constant O_register */
1011 get_operand (oper, which, opmode)
1016 char *p = input_line_pointer;
1020 oper->exp.X_op = O_absent;
1021 oper->reg1 = REG_NONE;
1022 oper->reg2 = REG_NONE;
1023 mode = M6811_OP_NONE;
1025 p = skip_whites (p);
1027 if (*p == 0 || *p == '\n' || *p == '\r')
1029 input_line_pointer = p;
1033 if (*p == '*' && (opmode & (M6811_OP_DIRECT | M6811_OP_IND16)))
1035 mode = M6811_OP_DIRECT;
1040 if (!(opmode & (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK)))
1042 as_bad (_("Immediate operand is not allowed for operand %d."),
1047 mode = M6811_OP_IMM16;
1049 if (strncmp (p, "%hi", 3) == 0)
1052 mode |= M6811_OP_HIGH_ADDR;
1054 else if (strncmp (p, "%lo", 3) == 0)
1057 mode |= M6811_OP_LOW_ADDR;
1059 /* %page modifier is used to obtain only the page number
1060 of the address of a function. */
1061 else if (strncmp (p, "%page", 5) == 0)
1064 mode |= M6811_OP_PAGE_ADDR;
1067 /* %addr modifier is used to obtain the physical address part
1068 of the function (16-bit). For 68HC12 the function will be
1069 mapped in the 16K window at 0x8000 and the value will be
1070 within that window (although the function address may not fit
1071 in 16-bit). See bfd/elf32-m68hc12.c for the translation. */
1072 else if (strncmp (p, "%addr", 5) == 0)
1075 mode |= M6811_OP_CALL_ADDR;
1078 else if (*p == '.' && (p[1] == '+' || p[1] == '-'))
1081 mode = M6811_OP_JUMP_REL;
1085 if (current_architecture & cpu6811)
1086 as_bad (_("Indirect indexed addressing is not valid for 68HC11."));
1089 mode = M6812_OP_D_IDX;
1090 p = skip_whites (p);
1092 else if (*p == ',') /* Special handling of ,x and ,y. */
1095 input_line_pointer = p;
1097 reg = register_name ();
1098 if (reg != REG_NONE)
1101 oper->exp.X_op = O_constant;
1102 oper->exp.X_add_number = 0;
1103 oper->mode = M6812_OP_IDX;
1106 as_bad (_("Spurious `,' or bad indirect register addressing mode."));
1109 /* Handle 68HC12 page specification in 'call foo,%page(bar)'. */
1110 else if ((opmode & M6812_OP_PAGE) && strncmp (p, "%page", 5) == 0)
1113 mode = M6811_OP_PAGE_ADDR | M6812_OP_PAGE | M6811_OP_IND16;
1115 input_line_pointer = p;
1117 if (mode == M6811_OP_NONE || mode == M6812_OP_D_IDX)
1118 reg = register_name ();
1122 if (reg != REG_NONE)
1124 p = skip_whites (input_line_pointer);
1125 if (*p == ']' && mode == M6812_OP_D_IDX)
1128 (_("Missing second register or offset for indexed-indirect mode."));
1133 oper->mode = mode | M6812_OP_REG;
1136 if (mode == M6812_OP_D_IDX)
1138 as_bad (_("Missing second register for indexed-indirect mode."));
1145 input_line_pointer = p;
1146 reg = register_name ();
1147 if (reg != REG_NONE)
1149 p = skip_whites (input_line_pointer);
1150 if (mode == M6812_OP_D_IDX)
1154 as_bad (_("Missing `]' to close indexed-indirect mode."));
1158 oper->mode = M6812_OP_D_IDX;
1160 input_line_pointer = p;
1168 /* In MRI mode, isolate the operand because we can't distinguish
1169 operands from comments. */
1174 p = skip_whites (p);
1175 while (*p && *p != ' ' && *p != '\t')
1184 /* Parse as an expression. */
1185 expression (&oper->exp);
1194 expression (&oper->exp);
1197 if (oper->exp.X_op == O_illegal)
1199 as_bad (_("Illegal operand."));
1202 else if (oper->exp.X_op == O_absent)
1204 as_bad (_("Missing operand."));
1208 p = input_line_pointer;
1210 if (mode == M6811_OP_NONE || mode == M6811_OP_DIRECT
1211 || mode == M6812_OP_D_IDX)
1213 p = skip_whites (input_line_pointer);
1217 int possible_mode = M6811_OP_NONE;
1218 char *old_input_line;
1223 /* 68HC12 pre increment or decrement. */
1224 if (mode == M6811_OP_NONE)
1228 possible_mode = M6812_PRE_DEC;
1233 possible_mode = M6812_PRE_INC;
1236 p = skip_whites (p);
1238 input_line_pointer = p;
1239 reg = register_name ();
1241 /* Backtrack if we have a valid constant expression and
1242 it does not correspond to the offset of the 68HC12 indexed
1243 addressing mode (as in N,x). */
1244 if (reg == REG_NONE && mode == M6811_OP_NONE
1245 && possible_mode != M6811_OP_NONE)
1247 oper->mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
1248 input_line_pointer = skip_whites (old_input_line);
1252 if (possible_mode != M6811_OP_NONE)
1253 mode = possible_mode;
1255 if ((current_architecture & cpu6811)
1256 && possible_mode != M6811_OP_NONE)
1257 as_bad (_("Pre-increment mode is not valid for 68HC11"));
1259 if (which == 0 && opmode & M6812_OP_IDX_P2
1260 && reg != REG_X && reg != REG_Y
1261 && reg != REG_PC && reg != REG_SP)
1264 input_line_pointer = p;
1267 if (reg == REG_NONE && mode != M6811_OP_DIRECT
1268 && !(mode == M6811_OP_NONE && opmode & M6811_OP_IND16))
1270 as_bad (_("Wrong register in register indirect mode."));
1273 if (mode == M6812_OP_D_IDX)
1275 p = skip_whites (input_line_pointer);
1278 as_bad (_("Missing `]' to close register indirect operand."));
1281 input_line_pointer = p;
1283 oper->mode = M6812_OP_D_IDX_2;
1286 if (reg != REG_NONE)
1289 if (mode == M6811_OP_NONE)
1291 p = input_line_pointer;
1294 mode = M6812_POST_DEC;
1296 if (current_architecture & cpu6811)
1298 (_("Post-decrement mode is not valid for 68HC11."));
1302 mode = M6812_POST_INC;
1304 if (current_architecture & cpu6811)
1306 (_("Post-increment mode is not valid for 68HC11."));
1309 mode = M6812_OP_IDX;
1311 input_line_pointer = p;
1314 mode |= M6812_OP_IDX;
1319 input_line_pointer = old_input_line;
1322 if (mode == M6812_OP_D_IDX_2)
1324 as_bad (_("Invalid indexed indirect mode."));
1329 /* If the mode is not known until now, this is either a label
1330 or an indirect address. */
1331 if (mode == M6811_OP_NONE)
1332 mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
1334 p = input_line_pointer;
1335 while (*p == ' ' || *p == '\t')
1337 input_line_pointer = p;
1343 #define M6812_AUTO_INC_DEC (M6812_PRE_INC | M6812_PRE_DEC \
1344 | M6812_POST_INC | M6812_POST_DEC)
1346 /* Checks that the number 'num' fits for a given mode. */
1348 check_range (num, mode)
1352 /* Auto increment and decrement are ok for [-8..8] without 0. */
1353 if (mode & M6812_AUTO_INC_DEC)
1354 return (num != 0 && num <= 8 && num >= -8);
1356 /* The 68HC12 supports 5, 9 and 16-bit offsets. */
1357 if (mode & (M6812_INDEXED_IND | M6812_INDEXED | M6812_OP_IDX))
1358 mode = M6811_OP_IND16;
1360 if (mode & M6812_OP_JUMP_REL16)
1361 mode = M6811_OP_IND16;
1363 mode &= ~M6811_OP_BRANCH;
1368 case M6811_OP_DIRECT:
1369 return (num >= 0 && num <= 255) ? 1 : 0;
1371 case M6811_OP_BITMASK:
1374 return (((num & 0xFFFFFF00) == 0) || ((num & 0xFFFFFF00) == 0xFFFFFF00))
1377 case M6811_OP_JUMP_REL:
1378 return (num >= -128 && num <= 127) ? 1 : 0;
1380 case M6811_OP_IND16:
1381 case M6811_OP_IND16 | M6812_OP_PAGE:
1382 case M6811_OP_IMM16:
1383 return (((num & 0xFFFF0000) == 0) || ((num & 0xFFFF0000) == 0xFFFF0000))
1386 case M6812_OP_IBCC_MARKER:
1387 case M6812_OP_TBCC_MARKER:
1388 case M6812_OP_DBCC_MARKER:
1389 return (num >= -256 && num <= 255) ? 1 : 0;
1391 case M6812_OP_TRAP_ID:
1392 return ((num >= 0x30 && num <= 0x39)
1393 || (num >= 0x40 && num <= 0x0ff)) ? 1 : 0;
1400 /* Gas fixup generation. */
1402 /* Put a 1 byte expression described by 'oper'. If this expression contains
1403 unresolved symbols, generate an 8-bit fixup. */
1405 fixup8 (oper, mode, opmode)
1414 if (oper->X_op == O_constant)
1416 if (mode & M6812_OP_TRAP_ID
1417 && !check_range (oper->X_add_number, M6812_OP_TRAP_ID))
1419 static char trap_id_warn_once = 0;
1421 as_bad (_("Trap id `%ld' is out of range."), oper->X_add_number);
1422 if (trap_id_warn_once == 0)
1424 trap_id_warn_once = 1;
1425 as_bad (_("Trap id must be within [0x30..0x39] or [0x40..0xff]."));
1429 if (!(mode & M6812_OP_TRAP_ID)
1430 && !check_range (oper->X_add_number, mode))
1432 as_bad (_("Operand out of 8-bit range: `%ld'."), oper->X_add_number);
1434 number_to_chars_bigendian (f, oper->X_add_number & 0x0FF, 1);
1436 else if (oper->X_op != O_register)
1438 if (mode & M6812_OP_TRAP_ID)
1439 as_bad (_("The trap id must be a constant."));
1441 if (mode == M6811_OP_JUMP_REL)
1445 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
1446 oper, TRUE, BFD_RELOC_8_PCREL);
1447 fixp->fx_pcrel_adjust = 1;
1454 /* Now create an 8-bit fixup. If there was some %hi, %lo
1455 or %page modifier, generate the reloc accordingly. */
1456 if (opmode & M6811_OP_HIGH_ADDR)
1457 reloc = BFD_RELOC_M68HC11_HI8;
1458 else if (opmode & M6811_OP_LOW_ADDR)
1459 reloc = BFD_RELOC_M68HC11_LO8;
1460 else if (opmode & M6811_OP_PAGE_ADDR)
1461 reloc = BFD_RELOC_M68HC11_PAGE;
1463 reloc = BFD_RELOC_8;
1465 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
1466 oper, FALSE, reloc);
1467 if (reloc != BFD_RELOC_8)
1468 fixp->fx_no_overflow = 1;
1470 number_to_chars_bigendian (f, 0, 1);
1474 as_fatal (_("Operand `%x' not recognized in fixup8."), oper->X_op);
1478 /* Put a 2 byte expression described by 'oper'. If this expression contains
1479 unresolved symbols, generate a 16-bit fixup. */
1481 fixup16 (oper, mode, opmode)
1484 int opmode ATTRIBUTE_UNUSED;
1490 if (oper->X_op == O_constant)
1492 if (!check_range (oper->X_add_number, mode))
1494 as_bad (_("Operand out of 16-bit range: `%ld'."),
1495 oper->X_add_number);
1497 number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFF, 2);
1499 else if (oper->X_op != O_register)
1504 if ((opmode & M6811_OP_CALL_ADDR) && (mode & M6811_OP_IMM16))
1505 reloc = BFD_RELOC_M68HC11_LO16;
1506 else if (mode & M6812_OP_JUMP_REL16)
1507 reloc = BFD_RELOC_16_PCREL;
1508 else if (mode & M6812_OP_PAGE)
1509 reloc = BFD_RELOC_M68HC11_LO16;
1511 reloc = BFD_RELOC_16;
1513 /* Now create a 16-bit fixup. */
1514 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 2,
1516 reloc == BFD_RELOC_16_PCREL,
1518 number_to_chars_bigendian (f, 0, 2);
1519 if (reloc == BFD_RELOC_16_PCREL)
1520 fixp->fx_pcrel_adjust = 2;
1521 if (reloc == BFD_RELOC_M68HC11_LO16)
1522 fixp->fx_no_overflow = 1;
1526 as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
1530 /* Put a 3 byte expression described by 'oper'. If this expression contains
1531 unresolved symbols, generate a 24-bit fixup. */
1533 fixup24 (oper, mode, opmode)
1536 int opmode ATTRIBUTE_UNUSED;
1542 if (oper->X_op == O_constant)
1544 if (!check_range (oper->X_add_number, mode))
1546 as_bad (_("Operand out of 16-bit range: `%ld'."),
1547 oper->X_add_number);
1549 number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFFFF, 3);
1551 else if (oper->X_op != O_register)
1555 /* Now create a 24-bit fixup. */
1556 fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 2,
1557 oper, FALSE, BFD_RELOC_M68HC11_24);
1558 number_to_chars_bigendian (f, 0, 3);
1562 as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
1566 /* 68HC11 and 68HC12 code generation. */
1568 /* Translate the short branch/bsr instruction into a long branch. */
1569 static unsigned char
1570 convert_branch (code)
1573 if (IS_OPCODE (code, M6812_BSR))
1575 else if (IS_OPCODE (code, M6811_BSR))
1577 else if (IS_OPCODE (code, M6811_BRA))
1578 return (current_architecture & cpu6812) ? M6812_JMP : M6811_JMP;
1580 as_fatal (_("Unexpected branch conversion with `%x'"), code);
1582 /* Keep gcc happy. */
1586 /* Start a new insn that contains at least 'size' bytes. Record the
1587 line information of that insn in the dwarf2 debug sections. */
1589 m68hc11_new_insn (size)
1594 f = frag_more (size);
1596 dwarf2_emit_insn (size);
1601 /* Builds a jump instruction (bra, bcc, bsr). */
1603 build_jump_insn (opcode, operands, nb_operands, jmp_mode)
1604 struct m68hc11_opcode *opcode;
1615 /* The relative branch convertion is not supported for
1617 assert ((opcode->format & M6811_OP_BITMASK) == 0);
1618 assert (nb_operands == 1);
1619 assert (operands[0].reg1 == REG_NONE && operands[0].reg2 == REG_NONE);
1621 code = opcode->opcode;
1623 n = operands[0].exp.X_add_number;
1625 /* Turn into a long branch:
1626 - when force long branch option (and not for jbcc pseudos),
1627 - when jbcc and the constant is out of -128..127 range,
1628 - when branch optimization is allowed and branch out of range. */
1629 if ((jmp_mode == 0 && flag_force_long_jumps)
1630 || (operands[0].exp.X_op == O_constant
1631 && (!check_range (n, opcode->format) &&
1632 (jmp_mode == 1 || flag_fixed_branchs == 0))))
1635 where = frag_now_fix ();
1637 fix_new (frag_now, frag_now_fix (), 1,
1638 &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
1640 if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
1642 code = convert_branch (code);
1644 f = m68hc11_new_insn (1);
1645 number_to_chars_bigendian (f, code, 1);
1647 else if (current_architecture & cpu6812)
1649 /* 68HC12: translate the bcc into a lbcc. */
1650 f = m68hc11_new_insn (2);
1651 number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
1652 number_to_chars_bigendian (f + 1, code, 1);
1653 fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16,
1654 M6812_OP_JUMP_REL16);
1659 /* 68HC11: translate the bcc into b!cc +3; jmp <L>. */
1660 f = m68hc11_new_insn (3);
1662 number_to_chars_bigendian (f, code, 1);
1663 number_to_chars_bigendian (f + 1, 3, 1);
1664 number_to_chars_bigendian (f + 2, M6811_JMP, 1);
1666 fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
1670 /* Branch with a constant that must fit in 8-bits. */
1671 if (operands[0].exp.X_op == O_constant)
1673 if (!check_range (n, opcode->format))
1675 as_bad (_("Operand out of range for a relative branch: `%ld'"),
1678 else if (opcode->format & M6812_OP_JUMP_REL16)
1680 f = m68hc11_new_insn (4);
1681 number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
1682 number_to_chars_bigendian (f + 1, code, 1);
1683 number_to_chars_bigendian (f + 2, n & 0x0ffff, 2);
1687 f = m68hc11_new_insn (2);
1688 number_to_chars_bigendian (f, code, 1);
1689 number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
1692 else if (opcode->format & M6812_OP_JUMP_REL16)
1695 where = frag_now_fix ();
1697 fix_new (frag_now, frag_now_fix (), 1,
1698 &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
1700 f = m68hc11_new_insn (2);
1701 number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
1702 number_to_chars_bigendian (f + 1, code, 1);
1703 fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16, M6812_OP_JUMP_REL16);
1710 where = frag_now_fix ();
1712 fix_new (frag_now, frag_now_fix (), 1,
1713 &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
1715 /* Branch offset must fit in 8-bits, don't do some relax. */
1716 if (jmp_mode == 0 && flag_fixed_branchs)
1718 opcode = m68hc11_new_insn (1);
1719 number_to_chars_bigendian (opcode, code, 1);
1720 fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
1723 /* bra/bsr made be changed into jmp/jsr. */
1724 else if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
1726 /* Allocate worst case storage. */
1727 opcode = m68hc11_new_insn (3);
1728 number_to_chars_bigendian (opcode, code, 1);
1729 number_to_chars_bigendian (opcode + 1, 0, 1);
1730 frag_variant (rs_machine_dependent, 1, 1,
1731 ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF),
1732 operands[0].exp.X_add_symbol, (offsetT) n,
1735 else if (current_architecture & cpu6812)
1737 opcode = m68hc11_new_insn (2);
1738 number_to_chars_bigendian (opcode, code, 1);
1739 number_to_chars_bigendian (opcode + 1, 0, 1);
1740 frag_var (rs_machine_dependent, 2, 2,
1741 ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_UNDF),
1742 operands[0].exp.X_add_symbol, (offsetT) n, opcode);
1746 opcode = m68hc11_new_insn (2);
1747 number_to_chars_bigendian (opcode, code, 1);
1748 number_to_chars_bigendian (opcode + 1, 0, 1);
1749 frag_var (rs_machine_dependent, 3, 3,
1750 ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF),
1751 operands[0].exp.X_add_symbol, (offsetT) n, opcode);
1756 /* Builds a dbne/dbeq/tbne/tbeq instruction. */
1758 build_dbranch_insn (opcode, operands, nb_operands, jmp_mode)
1759 struct m68hc11_opcode *opcode;
1768 /* The relative branch convertion is not supported for
1770 assert ((opcode->format & M6811_OP_BITMASK) == 0);
1771 assert (nb_operands == 2);
1772 assert (operands[0].reg1 != REG_NONE);
1774 code = opcode->opcode & 0x0FF;
1776 f = m68hc11_new_insn (1);
1777 number_to_chars_bigendian (f, code, 1);
1779 n = operands[1].exp.X_add_number;
1780 code = operands[0].reg1;
1782 if (operands[0].reg1 == REG_NONE || operands[0].reg1 == REG_CCR
1783 || operands[0].reg1 == REG_PC)
1784 as_bad (_("Invalid register for dbcc/tbcc instruction."));
1786 if (opcode->format & M6812_OP_IBCC_MARKER)
1788 else if (opcode->format & M6812_OP_TBCC_MARKER)
1791 if (!(opcode->format & M6812_OP_EQ_MARKER))
1794 /* Turn into a long branch:
1795 - when force long branch option (and not for jbcc pseudos),
1796 - when jdbcc and the constant is out of -256..255 range,
1797 - when branch optimization is allowed and branch out of range. */
1798 if ((jmp_mode == 0 && flag_force_long_jumps)
1799 || (operands[1].exp.X_op == O_constant
1800 && (!check_range (n, M6812_OP_IBCC_MARKER) &&
1801 (jmp_mode == 1 || flag_fixed_branchs == 0))))
1805 number_to_chars_bigendian (f, code, 1);
1806 number_to_chars_bigendian (f + 1, M6812_JMP, 1);
1807 fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
1811 /* Branch with a constant that must fit in 9-bits. */
1812 if (operands[1].exp.X_op == O_constant)
1814 if (!check_range (n, M6812_OP_IBCC_MARKER))
1816 as_bad (_("Operand out of range for a relative branch: `%ld'"),
1825 number_to_chars_bigendian (f, code, 1);
1826 number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
1831 /* Branch offset must fit in 8-bits, don't do some relax. */
1832 if (jmp_mode == 0 && flag_fixed_branchs)
1834 fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
1840 number_to_chars_bigendian (f, code, 1);
1841 number_to_chars_bigendian (f + 1, 0, 1);
1842 frag_var (rs_machine_dependent, 3, 3,
1843 ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_UNDF),
1844 operands[1].exp.X_add_symbol, (offsetT) n, f);
1849 #define OP_EXTENDED (M6811_OP_PAGE2 | M6811_OP_PAGE3 | M6811_OP_PAGE4)
1851 /* Assemble the post index byte for 68HC12 extended addressing modes. */
1853 build_indexed_byte (op, format, move_insn)
1855 int format ATTRIBUTE_UNUSED;
1858 unsigned char byte = 0;
1863 val = op->exp.X_add_number;
1865 if (mode & M6812_AUTO_INC_DEC)
1868 if (mode & (M6812_POST_INC | M6812_POST_DEC))
1871 if (op->exp.X_op == O_constant)
1873 if (!check_range (val, mode))
1875 as_bad (_("Increment/decrement value is out of range: `%ld'."),
1878 if (mode & (M6812_POST_INC | M6812_PRE_INC))
1879 byte |= (val - 1) & 0x07;
1881 byte |= (8 - ((val) & 7)) | 0x8;
1886 as_fatal (_("Expecting a register."));
1901 as_bad (_("Invalid register for post/pre increment."));
1906 number_to_chars_bigendian (f, byte, 1);
1910 if (mode & (M6812_OP_IDX | M6812_OP_D_IDX_2))
1931 as_bad (_("Invalid register."));
1934 if (op->exp.X_op == O_constant)
1936 if (!check_range (val, M6812_OP_IDX))
1938 as_bad (_("Offset out of 16-bit range: %ld."), val);
1941 if (move_insn && !(val >= -16 && val <= 15))
1943 as_bad (_("Offset out of 5-bit range for movw/movb insn: %ld."),
1948 if (val >= -16 && val <= 15 && !(mode & M6812_OP_D_IDX_2))
1953 number_to_chars_bigendian (f, byte, 1);
1956 else if (val >= -256 && val <= 255 && !(mode & M6812_OP_D_IDX_2))
1963 number_to_chars_bigendian (f, byte, 1);
1964 number_to_chars_bigendian (f + 1, val & 0x0FF, 1);
1970 if (mode & M6812_OP_D_IDX_2)
1976 number_to_chars_bigendian (f, byte, 1);
1977 number_to_chars_bigendian (f + 1, val & 0x0FFFF, 2);
1981 if (mode & M6812_OP_D_IDX_2)
1983 byte = (byte << 3) | 0xe3;
1985 number_to_chars_bigendian (f, byte, 1);
1987 fixup16 (&op->exp, 0, 0);
1989 else if (op->reg1 != REG_PC)
1995 number_to_chars_bigendian (f, byte, 1);
1996 sym = op->exp.X_add_symbol;
1997 off = op->exp.X_add_number;
1998 if (op->exp.X_op != O_symbol)
2000 sym = make_expr_symbol (&op->exp);
2003 frag_var (rs_machine_dependent, 2, 2,
2004 ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_UNDF),
2010 number_to_chars_bigendian (f, byte, 1);
2011 frag_var (rs_machine_dependent, 2, 2,
2012 ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_UNDF),
2013 op->exp.X_add_symbol,
2014 op->exp.X_add_number, f);
2019 if (mode & (M6812_OP_REG | M6812_OP_D_IDX))
2021 if (mode & M6812_OP_D_IDX)
2023 if (op->reg1 != REG_D)
2024 as_bad (_("Expecting register D for indexed indirect mode."));
2026 as_bad (_("Indexed indirect mode is not allowed for movb/movw."));
2043 as_bad (_("Invalid accumulator register."));
2068 as_bad (_("Invalid indexed register."));
2072 number_to_chars_bigendian (f, byte, 1);
2076 as_fatal (_("Addressing mode not implemented yet."));
2080 /* Assemble the 68HC12 register mode byte. */
2082 build_reg_mode (op, format)
2089 if (format & M6812_OP_SEX_MARKER
2090 && op->reg1 != REG_A && op->reg1 != REG_B && op->reg1 != REG_CCR)
2091 as_bad (_("Invalid source register for this instruction, use 'tfr'."));
2092 else if (op->reg1 == REG_NONE || op->reg1 == REG_PC)
2093 as_bad (_("Invalid source register."));
2095 if (format & M6812_OP_SEX_MARKER
2096 && op->reg2 != REG_D
2097 && op->reg2 != REG_X && op->reg2 != REG_Y && op->reg2 != REG_SP)
2098 as_bad (_("Invalid destination register for this instruction, use 'tfr'."));
2099 else if (op->reg2 == REG_NONE || op->reg2 == REG_PC)
2100 as_bad (_("Invalid destination register."));
2102 byte = (op->reg1 << 4) | (op->reg2);
2103 if (format & M6812_OP_EXG_MARKER)
2107 number_to_chars_bigendian (f, byte, 1);
2111 /* build_insn takes a pointer to the opcode entry in the opcode table,
2112 the array of operand expressions and builds the correspding instruction.
2113 This operation only deals with non relative jumps insn (need special
2116 build_insn (opcode, operands, nb_operands)
2117 struct m68hc11_opcode *opcode;
2119 int nb_operands ATTRIBUTE_UNUSED;
2126 /* Put the page code instruction if there is one. */
2127 format = opcode->format;
2129 if (format & M6811_OP_BRANCH)
2130 fix_new (frag_now, frag_now_fix (), 1,
2131 &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
2133 if (format & OP_EXTENDED)
2137 f = m68hc11_new_insn (2);
2138 if (format & M6811_OP_PAGE2)
2139 page_code = M6811_OPCODE_PAGE2;
2140 else if (format & M6811_OP_PAGE3)
2141 page_code = M6811_OPCODE_PAGE3;
2143 page_code = M6811_OPCODE_PAGE4;
2145 number_to_chars_bigendian (f, page_code, 1);
2149 f = m68hc11_new_insn (1);
2151 number_to_chars_bigendian (f, opcode->opcode, 1);
2155 /* The 68HC12 movb and movw instructions are special. We have to handle
2156 them in a special way. */
2157 if (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
2160 if (format & M6812_OP_IDX)
2162 build_indexed_byte (&operands[0], format, 1);
2164 format &= ~M6812_OP_IDX;
2166 if (format & M6812_OP_IDX_P2)
2168 build_indexed_byte (&operands[1], format, 1);
2170 format &= ~M6812_OP_IDX_P2;
2174 if (format & (M6811_OP_DIRECT | M6811_OP_IMM8))
2176 fixup8 (&operands[i].exp,
2177 format & (M6811_OP_DIRECT | M6811_OP_IMM8 | M6812_OP_TRAP_ID),
2181 else if (IS_CALL_SYMBOL (format) && nb_operands == 1)
2183 format &= ~M6812_OP_PAGE;
2184 fixup24 (&operands[i].exp, format & M6811_OP_IND16,
2188 else if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
2190 fixup16 (&operands[i].exp,
2191 format & (M6811_OP_IMM16 | M6811_OP_IND16 | M6812_OP_PAGE),
2195 else if (format & (M6811_OP_IX | M6811_OP_IY))
2197 if ((format & M6811_OP_IX) && (operands[0].reg1 != REG_X))
2198 as_bad (_("Invalid indexed register, expecting register X."));
2199 if ((format & M6811_OP_IY) && (operands[0].reg1 != REG_Y))
2200 as_bad (_("Invalid indexed register, expecting register Y."));
2202 fixup8 (&operands[0].exp, M6811_OP_IX, operands[0].mode);
2206 (M6812_OP_IDX | M6812_OP_IDX_2 | M6812_OP_IDX_1
2207 | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
2209 build_indexed_byte (&operands[i], format, move_insn);
2212 else if (format & M6812_OP_REG && current_architecture & cpu6812)
2214 build_reg_mode (&operands[i], format);
2217 if (format & M6811_OP_BITMASK)
2219 fixup8 (&operands[i].exp, M6811_OP_BITMASK, operands[i].mode);
2222 if (format & M6811_OP_JUMP_REL)
2224 fixup8 (&operands[i].exp, M6811_OP_JUMP_REL, operands[i].mode);
2226 else if (format & M6812_OP_IND16_P2)
2228 fixup16 (&operands[1].exp, M6811_OP_IND16, operands[1].mode);
2230 if (format & M6812_OP_PAGE)
2232 fixup8 (&operands[i].exp, M6812_OP_PAGE, operands[i].mode);
2236 /* Opcode identification and operand analysis. */
2238 /* find() gets a pointer to an entry in the opcode table. It must look at all
2239 opcodes with the same name and use the operands to choose the correct
2240 opcode. Returns the opcode pointer if there was a match and 0 if none. */
2241 static struct m68hc11_opcode *
2242 find (opc, operands, nb_operands)
2243 struct m68hc11_opcode_def *opc;
2248 struct m68hc11_opcode *opcode;
2249 struct m68hc11_opcode *op_indirect;
2252 opcode = opc->opcode;
2254 /* Now search the opcode table table for one with operands
2255 that matches what we've got. We're only done if the operands matched so
2256 far AND there are no more to check. */
2257 for (pos = match = 0; match == 0 && pos < opc->nb_modes; pos++, opcode++)
2259 int poss_indirect = 0;
2260 long format = opcode->format;
2264 if (opcode->format & M6811_OP_MASK)
2266 if (opcode->format & M6811_OP_BITMASK)
2268 if (opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2270 if (opcode->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
2272 if ((opcode->format & M6812_OP_PAGE)
2273 && (!IS_CALL_SYMBOL (opcode->format) || nb_operands == 2))
2276 for (i = 0; expect == nb_operands && i < nb_operands; i++)
2278 int mode = operands[i].mode;
2280 if (mode & M6811_OP_IMM16)
2283 (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK))
2287 if (mode == M6811_OP_DIRECT)
2289 if (format & M6811_OP_DIRECT)
2292 /* If the operand is a page 0 operand, remember a
2293 possible <abs-16> addressing mode. We mark
2294 this and continue to check other operands. */
2295 if (format & M6811_OP_IND16
2296 && flag_strict_direct_addressing && op_indirect == 0)
2303 if (mode & M6811_OP_IND16)
2305 if (i == 0 && (format & M6811_OP_IND16) != 0)
2307 if (i != 0 && (format & M6812_OP_PAGE) != 0)
2309 if (i != 0 && (format & M6812_OP_IND16_P2) != 0)
2311 if (i == 0 && (format & M6811_OP_BITMASK))
2314 if (mode & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2316 if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2319 if (mode & M6812_OP_REG)
2322 && (format & M6812_OP_REG)
2323 && (operands[i].reg2 == REG_NONE))
2326 && (format & M6812_OP_REG)
2327 && (format & M6812_OP_REG_2)
2328 && (operands[i].reg2 != REG_NONE))
2331 && (format & M6812_OP_IDX)
2332 && (operands[i].reg2 != REG_NONE))
2335 && (format & M6812_OP_IDX)
2336 && (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)))
2339 && (format & M6812_OP_IDX_P2))
2343 if (mode & M6812_OP_IDX)
2345 if (format & M6811_OP_IX && operands[i].reg1 == REG_X)
2347 if (format & M6811_OP_IY && operands[i].reg1 == REG_Y)
2350 && format & (M6812_OP_IDX | M6812_OP_IDX_1 | M6812_OP_IDX_2)
2351 && (operands[i].reg1 == REG_X
2352 || operands[i].reg1 == REG_Y
2353 || operands[i].reg1 == REG_SP
2354 || operands[i].reg1 == REG_PC))
2356 if (i == 1 && format & M6812_OP_IDX_P2)
2359 if (mode & format & (M6812_OP_D_IDX | M6812_OP_D_IDX_2))
2364 if (mode & M6812_AUTO_INC_DEC)
2367 && format & (M6812_OP_IDX | M6812_OP_IDX_1 |
2370 if (i == 1 && format & M6812_OP_IDX_P2)
2375 match = i == nb_operands;
2377 /* Operands are ok but an operand uses page 0 addressing mode
2378 while the insn supports abs-16 mode. Keep a reference to this
2379 insns in case there is no insn supporting page 0 addressing. */
2380 if (match && poss_indirect)
2382 op_indirect = opcode;
2389 /* Page 0 addressing is used but not supported by any insn.
2390 If absolute addresses are supported, we use that insn. */
2391 if (match == 0 && op_indirect)
2393 opcode = op_indirect;
2405 /* Find the real opcode and its associated operands. We use a progressive
2406 approach here. On entry, 'opc' points to the first opcode in the
2407 table that matches the opcode name in the source line. We try to
2408 isolate an operand, find a possible match in the opcode table.
2409 We isolate another operand if no match were found. The table 'operands'
2410 is filled while operands are recognized.
2412 Returns the opcode pointer that matches the opcode name in the
2413 source line and the associated operands. */
2414 static struct m68hc11_opcode *
2415 find_opcode (opc, operands, nb_operands)
2416 struct m68hc11_opcode_def *opc;
2420 struct m68hc11_opcode *opcode;
2423 if (opc->max_operands == 0)
2429 for (i = 0; i < opc->max_operands;)
2433 result = get_operand (&operands[i], i, opc->format);
2437 /* Special case where the bitmask of the bclr/brclr
2438 instructions is not introduced by #.
2439 Example: bclr 3,x $80. */
2440 if (i == 1 && (opc->format & M6811_OP_BITMASK)
2441 && (operands[i].mode & M6811_OP_IND16))
2443 operands[i].mode = M6811_OP_IMM16;
2448 if (i >= opc->min_operands)
2450 opcode = find (opc, operands, i);
2452 /* Another special case for 'call foo,page' instructions.
2453 Since we support 'call foo' and 'call foo,page' we must look
2454 if the optional page specification is present otherwise we will
2455 assemble immediately and treat the page spec as garbage. */
2456 if (opcode && !(opcode->format & M6812_OP_PAGE))
2459 if (opcode && *input_line_pointer != ',')
2463 if (*input_line_pointer == ',')
2464 input_line_pointer++;
2470 #define M6812_XBCC_MARKER (M6812_OP_TBCC_MARKER \
2471 | M6812_OP_DBCC_MARKER \
2472 | M6812_OP_IBCC_MARKER)
2474 /* Gas line assembler entry point. */
2476 /* This is the main entry point for the machine-dependent assembler. str
2477 points to a machine-dependent instruction. This function is supposed to
2478 emit the frags/bytes it assembles to. */
2483 struct m68hc11_opcode_def *opc;
2484 struct m68hc11_opcode *opcode;
2486 unsigned char *op_start, *save;
2487 unsigned char *op_end;
2490 operand operands[M6811_MAX_OPERANDS];
2492 int branch_optimize = 0;
2495 /* Drop leading whitespace. */
2499 /* Find the opcode end and get the opcode in 'name'. The opcode is forced
2500 lower case (the opcode table only has lower case op-codes). */
2501 for (op_start = op_end = (unsigned char *) (str);
2502 *op_end && nlen < 20 && !is_end_of_line[*op_end] && *op_end != ' ';
2505 name[nlen] = TOLOWER (op_start[nlen]);
2512 as_bad (_("No instruction or missing opcode."));
2516 /* Find the opcode definition given its name. */
2517 opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, name);
2519 /* If it's not recognized, look for 'jbsr' and 'jbxx'. These are
2520 pseudo insns for relative branch. For these branchs, we always
2521 optimize them (turned into absolute branchs) even if --short-branchs
2523 if (opc == NULL && name[0] == 'j' && name[1] == 'b')
2525 opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, &name[1]);
2527 && (!(opc->format & M6811_OP_JUMP_REL)
2528 || (opc->format & M6811_OP_BITMASK)))
2531 branch_optimize = 1;
2534 /* The following test should probably be removed. This is not conform
2535 to Motorola assembler specs. */
2536 if (opc == NULL && flag_mri)
2538 if (*op_end == ' ' || *op_end == '\t')
2540 while (*op_end == ' ' || *op_end == '\t')
2545 (is_end_of_line[op_end[1]]
2546 || op_end[1] == ' ' || op_end[1] == '\t'
2547 || !ISALNUM (op_end[1])))
2548 && (*op_end == 'a' || *op_end == 'b'
2549 || *op_end == 'A' || *op_end == 'B'
2550 || *op_end == 'd' || *op_end == 'D'
2551 || *op_end == 'x' || *op_end == 'X'
2552 || *op_end == 'y' || *op_end == 'Y'))
2554 name[nlen++] = TOLOWER (*op_end++);
2556 opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash,
2562 /* Identify a possible instruction alias. There are some on the
2563 68HC12 to emulate a few 68HC11 instructions. */
2564 if (opc == NULL && (current_architecture & cpu6812))
2568 for (i = 0; i < m68hc12_num_alias; i++)
2569 if (strcmp (m68hc12_alias[i].name, name) == 0)
2575 if (opc == NULL && alias_id < 0)
2577 as_bad (_("Opcode `%s' is not recognized."), name);
2580 save = input_line_pointer;
2581 input_line_pointer = op_end;
2586 opcode = find_opcode (opc, operands, &nb_operands);
2591 if ((opcode || alias_id >= 0) && !flag_mri)
2593 char *p = input_line_pointer;
2595 while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
2598 if (*p != '\n' && *p)
2599 as_bad (_("Garbage at end of instruction: `%s'."), p);
2602 input_line_pointer = save;
2606 char *f = m68hc11_new_insn (m68hc12_alias[alias_id].size);
2608 number_to_chars_bigendian (f, m68hc12_alias[alias_id].code1, 1);
2609 if (m68hc12_alias[alias_id].size > 1)
2610 number_to_chars_bigendian (f + 1, m68hc12_alias[alias_id].code2, 1);
2615 /* Opcode is known but does not have valid operands. Print out the
2616 syntax for this opcode. */
2619 if (flag_print_insn_syntax)
2620 print_insn_format (name);
2622 as_bad (_("Invalid operand for `%s'"), name);
2626 /* Treat dbeq/ibeq/tbeq instructions in a special way. The branch is
2627 relative and must be in the range -256..255 (9-bits). */
2628 if ((opcode->format & M6812_XBCC_MARKER)
2629 && (opcode->format & M6811_OP_JUMP_REL))
2630 build_dbranch_insn (opcode, operands, nb_operands, branch_optimize);
2632 /* Relative jumps instructions are taken care of separately. We have to make
2633 sure that the relative branch is within the range -128..127. If it's out
2634 of range, the instructions are changed into absolute instructions.
2635 This is not supported for the brset and brclr instructions. */
2636 else if ((opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2637 && !(opcode->format & M6811_OP_BITMASK))
2638 build_jump_insn (opcode, operands, nb_operands, branch_optimize);
2640 build_insn (opcode, operands, nb_operands);
2644 /* Pseudo op to control the ELF flags. */
2647 int x ATTRIBUTE_UNUSED;
2649 char *name = input_line_pointer, ch;
2651 while (!is_end_of_line[(unsigned char) *input_line_pointer])
2652 input_line_pointer++;
2653 ch = *input_line_pointer;
2654 *input_line_pointer = '\0';
2656 if (strcmp (name, "mshort") == 0)
2658 elf_flags &= ~E_M68HC11_I32;
2660 else if (strcmp (name, "mlong") == 0)
2662 elf_flags |= E_M68HC11_I32;
2664 else if (strcmp (name, "mshort-double") == 0)
2666 elf_flags &= ~E_M68HC11_F64;
2668 else if (strcmp (name, "mlong-double") == 0)
2670 elf_flags |= E_M68HC11_F64;
2674 as_warn (_("Invalid mode: %s\n"), name);
2676 *input_line_pointer = ch;
2677 demand_empty_rest_of_line ();
2680 /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
2681 are using 'rtc' for returning. It is necessary to use 'call'
2682 to invoke them. This is also used by the debugger to correctly
2683 find the stack frame. */
2685 s_m68hc11_mark_symbol (mark)
2692 elf_symbol_type *elfsym;
2696 name = input_line_pointer;
2697 c = get_symbol_end ();
2698 symbolP = symbol_find_or_make (name);
2699 *input_line_pointer = c;
2703 bfdsym = symbol_get_bfdsym (symbolP);
2704 elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
2708 /* Mark the symbol far (using rtc for function return). */
2709 elfsym->internal_elf_sym.st_other |= mark;
2713 input_line_pointer ++;
2717 if (*input_line_pointer == '\n')
2723 demand_empty_rest_of_line ();
2727 s_m68hc11_relax (ignore)
2728 int ignore ATTRIBUTE_UNUSED;
2734 if (ex.X_op != O_symbol || ex.X_add_number != 0)
2736 as_bad (_("bad .relax format"));
2737 ignore_rest_of_line ();
2741 fix_new_exp (frag_now, frag_now_fix (), 1, &ex, 1,
2742 BFD_RELOC_M68HC11_RL_GROUP);
2744 demand_empty_rest_of_line ();
2748 /* Relocation, relaxation and frag conversions. */
2750 /* PC-relative offsets are relative to the start of the
2751 next instruction. That is, the address of the offset, plus its
2752 size, since the offset is always the last part of the insn. */
2754 md_pcrel_from (fixP)
2757 if (fixP->fx_r_type == BFD_RELOC_M68HC11_RL_JUMP)
2760 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
2763 /* If while processing a fixup, a reloc really needs to be created
2764 then it is done here. */
2766 tc_gen_reloc (section, fixp)
2767 asection *section ATTRIBUTE_UNUSED;
2772 reloc = (arelent *) xmalloc (sizeof (arelent));
2773 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2774 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2775 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2776 if (fixp->fx_r_type == 0)
2777 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
2779 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2780 if (reloc->howto == (reloc_howto_type *) NULL)
2782 as_bad_where (fixp->fx_file, fixp->fx_line,
2783 _("Relocation %d is not supported by object file format."),
2784 (int) fixp->fx_r_type);
2788 /* Since we use Rel instead of Rela, encode the vtable entry to be
2789 used in the relocation's section offset. */
2790 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2791 reloc->address = fixp->fx_offset;
2797 /* We need a port-specific relaxation function to cope with sym2 - sym1
2798 relative expressions with both symbols in the same segment (but not
2799 necessarily in the same frag as this insn), for example:
2800 ldab sym2-(sym1-2),pc
2802 The offset can be 5, 9 or 16 bits long. */
2805 m68hc11_relax_frag (seg, fragP, stretch)
2806 segT seg ATTRIBUTE_UNUSED;
2808 long stretch ATTRIBUTE_UNUSED;
2813 const relax_typeS *this_type;
2814 const relax_typeS *start_type;
2815 relax_substateT next_state;
2816 relax_substateT this_state;
2817 const relax_typeS *table = TC_GENERIC_RELAX_TABLE;
2819 /* We only have to cope with frags as prepared by
2820 md_estimate_size_before_relax. The STATE_BITS16 case may geet here
2821 because of the different reasons that it's not relaxable. */
2822 switch (fragP->fr_subtype)
2824 case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16):
2825 case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16):
2826 /* When we get to this state, the frag won't grow any more. */
2829 case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS5):
2830 case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS5):
2831 case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9):
2832 case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9):
2833 if (fragP->fr_symbol == NULL
2834 || S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
2835 as_fatal (_("internal inconsistency problem in %s: fr_symbol %lx"),
2836 __FUNCTION__, (long) fragP->fr_symbol);
2837 symbolP = fragP->fr_symbol;
2838 if (symbol_resolved_p (symbolP))
2839 as_fatal (_("internal inconsistency problem in %s: resolved symbol"),
2841 aim = S_GET_VALUE (symbolP);
2845 as_fatal (_("internal inconsistency problem in %s: fr_subtype %d"),
2846 __FUNCTION__, fragP->fr_subtype);
2849 /* The rest is stolen from relax_frag. There's no obvious way to
2850 share the code, but fortunately no requirement to keep in sync as
2851 long as fragP->fr_symbol does not have its segment changed. */
2853 this_state = fragP->fr_subtype;
2854 start_type = this_type = table + this_state;
2858 /* Look backwards. */
2859 for (next_state = this_type->rlx_more; next_state;)
2860 if (aim >= this_type->rlx_backward)
2864 /* Grow to next state. */
2865 this_state = next_state;
2866 this_type = table + this_state;
2867 next_state = this_type->rlx_more;
2872 /* Look forwards. */
2873 for (next_state = this_type->rlx_more; next_state;)
2874 if (aim <= this_type->rlx_forward)
2878 /* Grow to next state. */
2879 this_state = next_state;
2880 this_type = table + this_state;
2881 next_state = this_type->rlx_more;
2885 growth = this_type->rlx_length - start_type->rlx_length;
2887 fragP->fr_subtype = this_state;
2892 md_convert_frag (abfd, sec, fragP)
2893 bfd *abfd ATTRIBUTE_UNUSED;
2894 asection *sec ATTRIBUTE_UNUSED;
2900 char *buffer_address = fragP->fr_literal;
2902 /* Address in object code of the displacement. */
2903 register int object_address = fragP->fr_fix + fragP->fr_address;
2905 buffer_address += fragP->fr_fix;
2907 /* The displacement of the address, from current location. */
2908 value = S_GET_VALUE (fragP->fr_symbol);
2909 disp = (value + fragP->fr_offset) - object_address;
2911 switch (fragP->fr_subtype)
2913 case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
2914 fragP->fr_opcode[1] = disp;
2917 case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
2918 /* This relax is only for bsr and bra. */
2919 assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
2920 || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
2921 || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
2923 fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
2925 fix_new (fragP, fragP->fr_fix - 1, 2,
2926 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
2930 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
2931 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_BYTE):
2932 fragP->fr_opcode[1] = disp;
2935 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
2936 /* Invert branch. */
2937 fragP->fr_opcode[0] ^= 1;
2938 fragP->fr_opcode[1] = 3; /* Branch offset. */
2939 buffer_address[0] = M6811_JMP;
2940 fix_new (fragP, fragP->fr_fix + 1, 2,
2941 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
2945 case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD):
2946 /* Translate branch into a long branch. */
2947 fragP->fr_opcode[1] = fragP->fr_opcode[0];
2948 fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
2950 fixp = fix_new (fragP, fragP->fr_fix, 2,
2951 fragP->fr_symbol, fragP->fr_offset, 1,
2952 BFD_RELOC_16_PCREL);
2953 fixp->fx_pcrel_adjust = 2;
2957 case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS5):
2958 if (fragP->fr_symbol != 0
2959 && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
2963 case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS5):
2964 fragP->fr_opcode[0] = fragP->fr_opcode[0] << 6;
2965 fragP->fr_opcode[0] |= value & 0x1f;
2968 case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS9):
2969 /* For a PC-relative offset, use the displacement with a -1 correction
2970 to take into account the additional byte of the insn. */
2971 if (fragP->fr_symbol != 0
2972 && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
2976 case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9):
2977 fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
2978 fragP->fr_opcode[0] |= 0xE0;
2979 fragP->fr_opcode[0] |= (value >> 8) & 1;
2980 fragP->fr_opcode[1] = value;
2984 case ENCODE_RELAX (STATE_INDEXED_PCREL, STATE_BITS16):
2985 case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16):
2986 fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
2987 fragP->fr_opcode[0] |= 0xe2;
2988 if ((fragP->fr_opcode[0] & 0x0ff) == 0x0fa
2989 && fragP->fr_symbol != 0
2990 && S_GET_SEGMENT (fragP->fr_symbol) != absolute_section)
2992 fixp = fix_new (fragP, fragP->fr_fix, 2,
2993 fragP->fr_symbol, fragP->fr_offset,
2994 1, BFD_RELOC_16_PCREL);
2998 fix_new (fragP, fragP->fr_fix, 2,
2999 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
3004 case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE):
3006 fragP->fr_opcode[0] |= 0x10;
3008 fragP->fr_opcode[1] = disp & 0x0FF;
3011 case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD):
3012 /* Invert branch. */
3013 fragP->fr_opcode[0] ^= 0x20;
3014 fragP->fr_opcode[1] = 3; /* Branch offset. */
3015 buffer_address[0] = M6812_JMP;
3016 fix_new (fragP, fragP->fr_fix + 1, 2,
3017 fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
3026 /* On an ELF system, we can't relax a weak symbol. The weak symbol
3027 can be overridden at final link time by a non weak symbol. We can
3028 relax externally visible symbol because there is no shared library
3029 and such symbol can't be overridden (unless they are weak). */
3031 relaxable_symbol (symbol)
3034 return ! S_IS_WEAK (symbol);
3037 /* Force truly undefined symbols to their maximum size, and generally set up
3038 the frag list to be relaxed. */
3040 md_estimate_size_before_relax (fragP, segment)
3044 if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
3046 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
3047 || !relaxable_symbol (fragP->fr_symbol)
3048 || (segment != absolute_section
3049 && RELAX_STATE (fragP->fr_subtype) == STATE_INDEXED_OFFSET))
3051 /* Non-relaxable cases. */
3053 char *buffer_address;
3055 old_fr_fix = fragP->fr_fix;
3056 buffer_address = fragP->fr_fix + fragP->fr_literal;
3058 switch (RELAX_STATE (fragP->fr_subtype))
3060 case STATE_PC_RELATIVE:
3062 /* This relax is only for bsr and bra. */
3063 assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
3064 || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
3065 || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
3067 if (flag_fixed_branchs)
3068 as_bad_where (fragP->fr_file, fragP->fr_line,
3069 _("bra or bsr with undefined symbol."));
3071 /* The symbol is undefined or in a separate section.
3072 Turn bra into a jmp and bsr into a jsr. The insn
3073 becomes 3 bytes long (instead of 2). A fixup is
3074 necessary for the unresolved symbol address. */
3075 fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
3077 fix_new (fragP, fragP->fr_fix - 1, 2, fragP->fr_symbol,
3078 fragP->fr_offset, 0, BFD_RELOC_16);
3082 case STATE_CONDITIONAL_BRANCH:
3083 assert (current_architecture & cpu6811);
3085 fragP->fr_opcode[0] ^= 1; /* Reverse sense of branch. */
3086 fragP->fr_opcode[1] = 3; /* Skip next jmp insn (3 bytes). */
3088 /* Don't use fr_opcode[2] because this may be
3089 in a different frag. */
3090 buffer_address[0] = M6811_JMP;
3093 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3094 fragP->fr_offset, 0, BFD_RELOC_16);
3098 case STATE_INDEXED_OFFSET:
3099 assert (current_architecture & cpu6812);
3101 if (fragP->fr_symbol
3102 && S_GET_SEGMENT (fragP->fr_symbol) == absolute_section)
3104 fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_OFFSET,
3106 /* Return the size of the variable part of the frag. */
3107 return md_relax_table[fragP->fr_subtype].rlx_length;
3111 /* Switch the indexed operation to 16-bit mode. */
3112 fragP->fr_opcode[0] = fragP->fr_opcode[0] << 3;
3113 fragP->fr_opcode[0] |= 0xe2;
3114 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3115 fragP->fr_offset, 0, BFD_RELOC_16);
3120 case STATE_INDEXED_PCREL:
3121 assert (current_architecture & cpu6812);
3123 if (fragP->fr_symbol
3124 && S_GET_SEGMENT (fragP->fr_symbol) == absolute_section)
3126 fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_PCREL,
3128 /* Return the size of the variable part of the frag. */
3129 return md_relax_table[fragP->fr_subtype].rlx_length;
3135 fragP->fr_opcode[0] = fragP->fr_opcode[0] << 3;
3136 fragP->fr_opcode[0] |= 0xe2;
3137 fixp = fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3138 fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
3143 case STATE_XBCC_BRANCH:
3144 assert (current_architecture & cpu6812);
3146 fragP->fr_opcode[0] ^= 0x20; /* Reverse sense of branch. */
3147 fragP->fr_opcode[1] = 3; /* Skip next jmp insn (3 bytes). */
3149 /* Don't use fr_opcode[2] because this may be
3150 in a different frag. */
3151 buffer_address[0] = M6812_JMP;
3154 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3155 fragP->fr_offset, 0, BFD_RELOC_16);
3159 case STATE_CONDITIONAL_BRANCH_6812:
3160 assert (current_architecture & cpu6812);
3162 /* Translate into a lbcc branch. */
3163 fragP->fr_opcode[1] = fragP->fr_opcode[0];
3164 fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
3166 fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
3167 fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
3172 as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
3176 /* Return the growth in the fixed part of the frag. */
3177 return fragP->fr_fix - old_fr_fix;
3180 /* Relaxable cases. */
3181 switch (RELAX_STATE (fragP->fr_subtype))
3183 case STATE_PC_RELATIVE:
3184 /* This relax is only for bsr and bra. */
3185 assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
3186 || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
3187 || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
3189 fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
3192 case STATE_CONDITIONAL_BRANCH:
3193 assert (current_architecture & cpu6811);
3195 fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH,
3199 case STATE_INDEXED_OFFSET:
3200 assert (current_architecture & cpu6812);
3202 fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_OFFSET,
3206 case STATE_INDEXED_PCREL:
3207 assert (current_architecture & cpu6812);
3209 fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_PCREL,
3213 case STATE_XBCC_BRANCH:
3214 assert (current_architecture & cpu6812);
3216 fragP->fr_subtype = ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE);
3219 case STATE_CONDITIONAL_BRANCH_6812:
3220 assert (current_architecture & cpu6812);
3222 fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812,
3228 if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
3229 as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
3231 /* Return the size of the variable part of the frag. */
3232 return md_relax_table[fragP->fr_subtype].rlx_length;
3235 /* See whether we need to force a relocation into the output file. */
3237 tc_m68hc11_force_relocation (fixP)
3240 if (fixP->fx_r_type == BFD_RELOC_M68HC11_RL_GROUP)
3243 return generic_force_reloc (fixP);
3246 /* Here we decide which fixups can be adjusted to make them relative
3247 to the beginning of the section instead of the symbol. Basically
3248 we need to make sure that the linker relaxation is done
3249 correctly, so in some cases we force the original symbol to be
3252 tc_m68hc11_fix_adjustable (fixP)
3255 switch (fixP->fx_r_type)
3257 /* For the linker relaxation to work correctly, these relocs
3258 need to be on the symbol itself. */
3260 case BFD_RELOC_M68HC11_RL_JUMP:
3261 case BFD_RELOC_M68HC11_RL_GROUP:
3262 case BFD_RELOC_VTABLE_INHERIT:
3263 case BFD_RELOC_VTABLE_ENTRY:
3266 /* The memory bank addressing translation also needs the original
3268 case BFD_RELOC_M68HC11_LO16:
3269 case BFD_RELOC_M68HC11_PAGE:
3270 case BFD_RELOC_M68HC11_24:
3279 md_apply_fix3 (fixP, valP, seg)
3282 segT seg ATTRIBUTE_UNUSED;
3285 long value = * valP;
3288 if (fixP->fx_addsy == (symbolS *) NULL)
3291 /* We don't actually support subtracting a symbol. */
3292 if (fixP->fx_subsy != (symbolS *) NULL)
3293 as_bad_where (fixP->fx_file, fixP->fx_line, _("Expression too complex."));
3295 op_type = fixP->fx_r_type;
3297 /* Patch the instruction with the resolved operand. Elf relocation
3298 info will also be generated to take care of linker/loader fixups.
3299 The 68HC11 addresses only 64Kb, we are only concerned by 8 and 16-bit
3300 relocs. BFD_RELOC_8 is basically used for .page0 access (the linker
3301 will warn for overflows). BFD_RELOC_8_PCREL should not be generated
3302 because it's either resolved or turned out into non-relative insns (see
3303 relax table, bcc, bra, bsr transformations)
3305 The BFD_RELOC_32 is necessary for the support of --gstabs. */
3306 where = fixP->fx_frag->fr_literal + fixP->fx_where;
3308 switch (fixP->fx_r_type)
3311 bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
3315 case BFD_RELOC_M68HC11_24:
3316 bfd_putb16 ((bfd_vma) (value & 0x0ffff), (unsigned char *) where);
3317 ((bfd_byte*) where)[2] = ((value >> 16) & 0x0ff);
3321 case BFD_RELOC_16_PCREL:
3322 case BFD_RELOC_M68HC11_LO16:
3323 bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
3324 if (value < -65537 || value > 65535)
3325 as_bad_where (fixP->fx_file, fixP->fx_line,
3326 _("Value out of 16-bit range."));
3329 case BFD_RELOC_M68HC11_HI8:
3333 case BFD_RELOC_M68HC11_LO8:
3335 case BFD_RELOC_M68HC11_PAGE:
3337 bfd_putb8 ((bfd_vma) value, (unsigned char *) where);
3339 ((bfd_byte *) where)[0] = (bfd_byte) value;
3342 case BFD_RELOC_8_PCREL:
3344 bfd_putb8 ((bfd_vma) value, (unsigned char *) where);
3346 ((bfd_byte *) where)[0] = (bfd_byte) value;
3348 if (value < -128 || value > 127)
3349 as_bad_where (fixP->fx_file, fixP->fx_line,
3350 _("Value %ld too large for 8-bit PC-relative branch."),
3354 case BFD_RELOC_M68HC11_3B:
3355 if (value <= 0 || value > 8)
3356 as_bad_where (fixP->fx_file, fixP->fx_line,
3357 _("Auto increment/decrement offset '%ld' is out of range."),
3364 where[0] = where[0] | (value & 0x07);
3367 case BFD_RELOC_M68HC11_RL_JUMP:
3368 case BFD_RELOC_M68HC11_RL_GROUP:
3369 case BFD_RELOC_VTABLE_INHERIT:
3370 case BFD_RELOC_VTABLE_ENTRY:
3375 as_fatal (_("Line %d: unknown relocation type: 0x%x."),
3376 fixP->fx_line, fixP->fx_r_type);
3380 /* Set the ELF specific flags. */
3382 m68hc11_elf_final_processing ()
3384 if (current_architecture & cpu6812s)
3385 elf_flags |= EF_M68HCS12_MACH;
3386 elf_elfheader (stdoutput)->e_flags &= ~EF_M68HC11_ABI;
3387 elf_elfheader (stdoutput)->e_flags |= elf_flags;