* config/tc-m68hc11.c (elf_flags): Set default ABI to gcc default
[external/binutils.git] / gas / config / tc-m68hc11.c
1 /* tc-m68hc11.c -- Assembler code for the Motorola 68HC11 & 68HC12.
2    Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3    Written by Stephane Carrez (stcarrez@nerim.fr)
4
5    This file is part of GAS, the GNU Assembler.
6
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)
10    any later version.
11
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.
16
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.  */
21
22 #include <stdio.h>
23 #include "as.h"
24 #include "safe-ctype.h"
25 #include "subsegs.h"
26 #include "opcode/m68hc11.h"
27 #include "dwarf2dbg.h"
28 #include "elf/m68hc11.h"
29
30 const char comment_chars[] = ";!";
31 const char line_comment_chars[] = "#*";
32 const char line_separator_chars[] = "";
33
34 const char EXP_CHARS[] = "eE";
35 const char FLT_CHARS[] = "dD";
36
37 #define STATE_CONDITIONAL_BRANCH        (1)
38 #define STATE_PC_RELATIVE               (2)
39 #define STATE_INDEXED_OFFSET            (3)
40 #define STATE_XBCC_BRANCH               (4)
41 #define STATE_CONDITIONAL_BRANCH_6812   (5)
42
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 */
50
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)
55
56 #define IS_OPCODE(C1,C2)        (((C1) & 0x0FF) == ((C2) & 0x0FF))
57
58 /* This table describes how you change sizes for the various types of variable
59    size expressions.  This version only supports two kinds.  */
60
61 /* The fields are:
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.  */
66
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.  */
71   {1, 1, 0, 0},
72
73   /* Relax for bcc <L>.
74      These insns are translated into b!cc +3 jmp L.  */
75   {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD)},
76   {0, 0, 3, 0},
77   {1, 1, 0, 0},
78   {1, 1, 0, 0},
79
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)},
83   {0, 0, 1, 0},
84   {1, 1, 0, 0},
85   {1, 1, 0, 0},
86
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)},
90   {0, 0, 2, 0},
91   {1, 1, 0, 0},
92
93   /* Relax for dbeq/ibeq/tbeq r,<L>:
94      These insns are translated into db!cc +3 jmp L.  */
95   {(255), (-256), 0, ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD)},
96   {0, 0, 3, 0},
97   {1, 1, 0, 0},
98   {1, 1, 0, 0},
99
100   /* Relax for bcc <L> on 68HC12.
101      These insns are translated into lbcc <L>.  */
102   {(127), (-128), 0, ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD)},
103   {0, 0, 2, 0},
104   {1, 1, 0, 0},
105   {1, 1, 0, 0},
106
107 };
108
109 /* 68HC11 and 68HC12 registers.  They are numbered according to the 68HC12.  */
110 typedef enum register_id {
111   REG_NONE = -1,
112   REG_A = 0,
113   REG_B = 1,
114   REG_CCR = 2,
115   REG_D = 4,
116   REG_X = 5,
117   REG_Y = 6,
118   REG_SP = 7,
119   REG_PC = 8
120 } register_id;
121
122 typedef struct operand {
123   expressionS exp;
124   register_id reg1;
125   register_id reg2;
126   int mode;
127 } operand;
128
129 struct m68hc11_opcode_def {
130   long format;
131   int min_operands;
132   int max_operands;
133   int nb_modes;
134   int used;
135   struct m68hc11_opcode *opcode;
136 };
137
138 static struct m68hc11_opcode_def *m68hc11_opcode_defs = 0;
139 static int m68hc11_nb_opcode_defs = 0;
140
141 typedef struct alias {
142   const char *name;
143   const char *alias;
144 } alias;
145
146 static alias alias_opcodes[] = {
147   {"cpd", "cmpd"},
148   {"cpx", "cmpx"},
149   {"cpy", "cmpy"},
150   {0, 0}
151 };
152
153 /* Local functions.  */
154 static register_id reg_name_search PARAMS ((char *));
155 static register_id register_name PARAMS ((void));
156 static int cmp_opcode PARAMS ((struct m68hc11_opcode *,
157                                struct m68hc11_opcode *));
158 static char *print_opcode_format PARAMS ((struct m68hc11_opcode *, int));
159 static char *skip_whites PARAMS ((char *));
160 static int check_range PARAMS ((long, int));
161 static void print_opcode_list PARAMS ((void));
162 static void get_default_target PARAMS ((void));
163 static void print_insn_format PARAMS ((char *));
164 static int get_operand PARAMS ((operand *, int, long));
165 static void fixup8 PARAMS ((expressionS *, int, int));
166 static void fixup16 PARAMS ((expressionS *, int, int));
167 static void fixup24 PARAMS ((expressionS *, int, int));
168 static unsigned char convert_branch PARAMS ((unsigned char));
169 static char *m68hc11_new_insn PARAMS ((int));
170 static void build_dbranch_insn PARAMS ((struct m68hc11_opcode *,
171                                         operand *, int, int));
172 static int build_indexed_byte PARAMS ((operand *, int, int));
173 static int build_reg_mode PARAMS ((operand *, int));
174
175 static struct m68hc11_opcode *find
176   PARAMS ((struct m68hc11_opcode_def *, operand *, int));
177 static struct m68hc11_opcode *find_opcode
178   PARAMS ((struct m68hc11_opcode_def *, operand *, int *));
179 static void build_jump_insn
180   PARAMS ((struct m68hc11_opcode *, operand *, int, int));
181 static void build_insn
182   PARAMS ((struct m68hc11_opcode *, operand *, int));
183 static int relaxable_symbol PARAMS ((symbolS *));
184
185 /* Pseudo op to indicate a relax group.  */
186 static void s_m68hc11_relax PARAMS((int));
187
188 /* Pseudo op to control the ELF flags.  */
189 static void s_m68hc11_mode PARAMS ((int));
190
191 /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
192    are using 'rtc' for returning.  It is necessary to use 'call'
193    to invoke them.  This is also used by the debugger to correctly
194    find the stack frame.  */
195 static void s_m68hc11_mark_symbol PARAMS ((int));
196
197 /* Controls whether relative branches can be turned into long branches.
198    When the relative offset is too large, the insn are changed:
199     bra -> jmp
200     bsr -> jsr
201     bcc -> b!cc +3
202            jmp L
203     dbcc -> db!cc +3
204             jmp L
205
206   Setting the flag forbidds this.  */
207 static short flag_fixed_branchs = 0;
208
209 /* Force to use long jumps (absolute) instead of relative branches.  */
210 static short flag_force_long_jumps = 0;
211
212 /* Change the direct addressing mode into an absolute addressing mode
213    when the insn does not support direct addressing.
214    For example, "clr *ZD0" is normally not possible and is changed
215    into "clr ZDO".  */
216 static short flag_strict_direct_addressing = 1;
217
218 /* When an opcode has invalid operand, print out the syntax of the opcode
219    to stderr.  */
220 static short flag_print_insn_syntax = 0;
221
222 /* Dumps the list of instructions with syntax and then exit:
223    1 -> Only dumps the list (sorted by name)
224    2 -> Generate an example (or test) that can be compiled.  */
225 static short flag_print_opcodes = 0;
226
227 /* Opcode hash table.  */
228 static struct hash_control *m68hc11_hash;
229
230 /* Current cpu (either cpu6811 or cpu6812).  This is determined automagically
231    by 'get_default_target' by looking at default BFD vector.  This is overriden
232    with the -m<cpu> option.  */
233 static int current_architecture = 0;
234
235 /* Default cpu determined by 'get_default_target'.  */
236 static const char *default_cpu;
237
238 /* Number of opcodes in the sorted table (filtered by current cpu).  */
239 static int num_opcodes;
240
241 /* The opcodes sorted by name and filtered by current cpu.  */
242 static struct m68hc11_opcode *m68hc11_sorted_opcodes;
243
244 /* ELF flags to set in the output file header.  */
245 static int elf_flags = E_M68HC11_F64;
246
247 /* These are the machine dependent pseudo-ops.  These are included so
248    the assembler can work on the output from the SUN C compiler, which
249    generates these.  */
250
251 /* This table describes all the machine specific pseudo-ops the assembler
252    has to support.  The fields are:
253    pseudo-op name without dot
254    function to call to execute this pseudo-op
255    Integer arg to pass to the function.  */
256 const pseudo_typeS md_pseudo_table[] = {
257   /* The following pseudo-ops are supported for MRI compatibility.  */
258   {"fcb", cons, 1},
259   {"fdb", cons, 2},
260   {"fcc", stringer, 1},
261   {"rmb", s_space, 0},
262
263   /* Dwarf2 support for Gcc.  */
264   {"file", (void (*) PARAMS ((int))) dwarf2_directive_file, 0},
265   {"loc", dwarf2_directive_loc, 0},
266
267   /* Motorola ALIS.  */
268   {"xrefb", s_ignore, 0}, /* Same as xref  */
269
270   /* Gcc driven relaxation.  */
271   {"relax", s_m68hc11_relax, 0},
272
273   /* .mode instruction (ala SH).  */
274   {"mode", s_m68hc11_mode, 0},
275
276   /* .far instruction.  */
277   {"far", s_m68hc11_mark_symbol, STO_M68HC12_FAR},
278
279   /* .interrupt instruction.  */
280   {"interrupt", s_m68hc11_mark_symbol, STO_M68HC12_INTERRUPT},
281
282   {0, 0, 0}
283 };
284 \f
285 /* Options and initialization.  */
286
287 const char *md_shortopts = "Sm:";
288
289 struct option md_longopts[] = {
290 #define OPTION_FORCE_LONG_BRANCH (OPTION_MD_BASE)
291   {"force-long-branchs", no_argument, NULL, OPTION_FORCE_LONG_BRANCH},
292
293 #define OPTION_SHORT_BRANCHS     (OPTION_MD_BASE + 1)
294   {"short-branchs", no_argument, NULL, OPTION_SHORT_BRANCHS},
295
296 #define OPTION_STRICT_DIRECT_MODE  (OPTION_MD_BASE + 2)
297   {"strict-direct-mode", no_argument, NULL, OPTION_STRICT_DIRECT_MODE},
298
299 #define OPTION_PRINT_INSN_SYNTAX  (OPTION_MD_BASE + 3)
300   {"print-insn-syntax", no_argument, NULL, OPTION_PRINT_INSN_SYNTAX},
301
302 #define OPTION_PRINT_OPCODES  (OPTION_MD_BASE + 4)
303   {"print-opcodes", no_argument, NULL, OPTION_PRINT_OPCODES},
304
305 #define OPTION_GENERATE_EXAMPLE  (OPTION_MD_BASE + 5)
306   {"generate-example", no_argument, NULL, OPTION_GENERATE_EXAMPLE},
307
308 #define OPTION_MSHORT  (OPTION_MD_BASE + 6)
309   {"mshort", no_argument, NULL, OPTION_MSHORT},
310
311 #define OPTION_MLONG  (OPTION_MD_BASE + 7)
312   {"mlong", no_argument, NULL, OPTION_MLONG},
313
314 #define OPTION_MSHORT_DOUBLE  (OPTION_MD_BASE + 8)
315   {"mshort-double", no_argument, NULL, OPTION_MSHORT_DOUBLE},
316
317 #define OPTION_MLONG_DOUBLE  (OPTION_MD_BASE + 9)
318   {"mlong-double", no_argument, NULL, OPTION_MLONG_DOUBLE},
319
320   {NULL, no_argument, NULL, 0}
321 };
322 size_t md_longopts_size = sizeof (md_longopts);
323
324 /* Get the target cpu for the assembler.  This is based on the configure
325    options and on the -m68hc11/-m68hc12 option.  If no option is specified,
326    we must get the default.  */
327 const char *
328 m68hc11_arch_format ()
329 {
330   get_default_target ();
331   if (current_architecture & cpu6811)
332     return "elf32-m68hc11";
333   else
334     return "elf32-m68hc12";
335 }
336
337 enum bfd_architecture
338 m68hc11_arch ()
339 {
340   get_default_target ();
341   if (current_architecture & cpu6811)
342     return bfd_arch_m68hc11;
343   else
344     return bfd_arch_m68hc12;
345 }
346
347 int
348 m68hc11_mach ()
349 {
350   return 0;
351 }
352
353 /* Listing header selected according to cpu.  */
354 const char *
355 m68hc11_listing_header ()
356 {
357   if (current_architecture & cpu6811)
358     return "M68HC11 GAS ";
359   else
360     return "M68HC12 GAS ";
361 }
362
363 void
364 md_show_usage (stream)
365      FILE *stream;
366 {
367   get_default_target ();
368   fprintf (stream, _("\
369 Motorola 68HC11/68HC12 options:\n\
370   -m68hc11 | -m68hc12     specify the processor [default %s]\n\
371   -mshort                 use 16-bit int ABI (default)\n\
372   -mlong                  use 32-bit int ABI\n\
373   -mshort-double          use 32-bit double ABI\n\
374   -mlong-double           use 64-bit double ABI (default)\n\
375   --force-long-branchs    always turn relative branchs into absolute ones\n\
376   -S,--short-branchs      do not turn relative branchs into absolute ones\n\
377                           when the offset is out of range\n\
378   --strict-direct-mode    do not turn the direct mode into extended mode\n\
379                           when the instruction does not support direct mode\n\
380   --print-insn-syntax     print the syntax of instruction in case of error\n\
381   --print-opcodes         print the list of instructions with syntax\n\
382   --generate-example      generate an example of each instruction\n\
383                           (used for testing)\n"), default_cpu);
384
385 }
386
387 /* Try to identify the default target based on the BFD library.  */
388 static void
389 get_default_target ()
390 {
391   const bfd_target *target;
392   bfd abfd;
393
394   if (current_architecture != 0)
395     return;
396
397   default_cpu = "unknown";
398   target = bfd_find_target (0, &abfd);
399   if (target && target->name)
400     {
401       if (strcmp (target->name, "elf32-m68hc12") == 0)
402         {
403           current_architecture = cpu6812;
404           default_cpu = "m68hc12";
405         }
406       else if (strcmp (target->name, "elf32-m68hc11") == 0)
407         {
408           current_architecture = cpu6811;
409           default_cpu = "m68hc11";
410         }
411       else
412         {
413           as_bad (_("Default target `%s' is not supported."), target->name);
414         }
415     }
416 }
417
418 void
419 m68hc11_print_statistics (file)
420      FILE *file;
421 {
422   int i;
423   struct m68hc11_opcode_def *opc;
424
425   hash_print_statistics (file, "opcode table", m68hc11_hash);
426
427   opc = m68hc11_opcode_defs;
428   if (opc == 0 || m68hc11_nb_opcode_defs == 0)
429     return;
430
431   /* Dump the opcode statistics table.  */
432   fprintf (file, _("Name   # Modes  Min ops  Max ops  Modes mask  # Used\n"));
433   for (i = 0; i < m68hc11_nb_opcode_defs; i++, opc++)
434     {
435       fprintf (file, "%-7.7s  %5d  %7d  %7d  0x%08lx  %7d\n",
436                opc->opcode->name,
437                opc->nb_modes,
438                opc->min_operands, opc->max_operands, opc->format, opc->used);
439     }
440 }
441
442 int
443 md_parse_option (c, arg)
444      int c;
445      char *arg;
446 {
447   get_default_target ();
448   switch (c)
449     {
450       /* -S means keep external to 2 bit offset rather than 16 bit one.  */
451     case OPTION_SHORT_BRANCHS:
452     case 'S':
453       flag_fixed_branchs = 1;
454       break;
455
456     case OPTION_FORCE_LONG_BRANCH:
457       flag_force_long_jumps = 1;
458       break;
459
460     case OPTION_PRINT_INSN_SYNTAX:
461       flag_print_insn_syntax = 1;
462       break;
463
464     case OPTION_PRINT_OPCODES:
465       flag_print_opcodes = 1;
466       break;
467
468     case OPTION_STRICT_DIRECT_MODE:
469       flag_strict_direct_addressing = 0;
470       break;
471
472     case OPTION_GENERATE_EXAMPLE:
473       flag_print_opcodes = 2;
474       break;
475
476     case OPTION_MSHORT:
477       elf_flags &= ~E_M68HC11_I32;
478       break;
479
480     case OPTION_MLONG:
481       elf_flags |= E_M68HC11_I32;
482       break;
483
484     case OPTION_MSHORT_DOUBLE:
485       elf_flags &= ~E_M68HC11_F64;
486       break;
487
488     case OPTION_MLONG_DOUBLE:
489       elf_flags |= E_M68HC11_F64;
490       break;
491
492     case 'm':
493       if (strcasecmp (arg, "68hc11") == 0)
494         current_architecture = cpu6811;
495       else if (strcasecmp (arg, "68hc12") == 0)
496         current_architecture = cpu6812;
497       else
498         as_bad (_("Option `%s' is not recognized."), arg);
499       break;
500
501     default:
502       return 0;
503     }
504
505   return 1;
506 }
507 \f
508 symbolS *
509 md_undefined_symbol (name)
510      char *name ATTRIBUTE_UNUSED;
511 {
512   return 0;
513 }
514
515 /* Equal to MAX_PRECISION in atof-ieee.c.  */
516 #define MAX_LITTLENUMS 6
517
518 /* Turn a string in input_line_pointer into a floating point constant
519    of type TYPE, and store the appropriate bytes in *LITP.  The number
520    of LITTLENUMS emitted is stored in *SIZEP.  An error message is
521    returned, or NULL on OK.  */
522 char *
523 md_atof (type, litP, sizeP)
524      char type;
525      char *litP;
526      int *sizeP;
527 {
528   int prec;
529   LITTLENUM_TYPE words[MAX_LITTLENUMS];
530   LITTLENUM_TYPE *wordP;
531   char *t;
532
533   switch (type)
534     {
535     case 'f':
536     case 'F':
537     case 's':
538     case 'S':
539       prec = 2;
540       break;
541
542     case 'd':
543     case 'D':
544     case 'r':
545     case 'R':
546       prec = 4;
547       break;
548
549     case 'x':
550     case 'X':
551       prec = 6;
552       break;
553
554     case 'p':
555     case 'P':
556       prec = 6;
557       break;
558
559     default:
560       *sizeP = 0;
561       return _("Bad call to MD_ATOF()");
562     }
563   t = atof_ieee (input_line_pointer, type, words);
564   if (t)
565     input_line_pointer = t;
566
567   *sizeP = prec * sizeof (LITTLENUM_TYPE);
568   for (wordP = words; prec--;)
569     {
570       md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
571       litP += sizeof (LITTLENUM_TYPE);
572     }
573   return 0;
574 }
575
576 valueT
577 md_section_align (seg, addr)
578      asection *seg;
579      valueT addr;
580 {
581   int align = bfd_get_section_alignment (stdoutput, seg);
582   return ((addr + (1 << align) - 1) & (-1 << align));
583 }
584
585 static int
586 cmp_opcode (op1, op2)
587      struct m68hc11_opcode *op1;
588      struct m68hc11_opcode *op2;
589 {
590   return strcmp (op1->name, op2->name);
591 }
592
593 #define IS_CALL_SYMBOL(MODE) \
594 (((MODE) & (M6812_OP_PAGE|M6811_OP_IND16)) \
595   == ((M6812_OP_PAGE|M6811_OP_IND16)))
596
597 /* Initialize the assembler.  Create the opcode hash table
598    (sorted on the names) with the M6811 opcode table
599    (from opcode library).  */
600 void
601 md_begin ()
602 {
603   char *prev_name = "";
604   struct m68hc11_opcode *opcodes;
605   struct m68hc11_opcode_def *opc = 0;
606   int i, j;
607
608   get_default_target ();
609
610   m68hc11_hash = hash_new ();
611
612   /* Get a writable copy of the opcode table and sort it on the names.  */
613   opcodes = (struct m68hc11_opcode *) xmalloc (m68hc11_num_opcodes *
614                                                sizeof (struct
615                                                        m68hc11_opcode));
616   m68hc11_sorted_opcodes = opcodes;
617   num_opcodes = 0;
618   for (i = 0; i < m68hc11_num_opcodes; i++)
619     {
620       if (m68hc11_opcodes[i].arch & current_architecture)
621         {
622           opcodes[num_opcodes] = m68hc11_opcodes[i];
623           if (opcodes[num_opcodes].name[0] == 'b'
624               && opcodes[num_opcodes].format & M6811_OP_JUMP_REL
625               && !(opcodes[num_opcodes].format & M6811_OP_BITMASK))
626             {
627               num_opcodes++;
628               opcodes[num_opcodes] = m68hc11_opcodes[i];
629             }
630           num_opcodes++;
631           for (j = 0; alias_opcodes[j].name != 0; j++)
632             if (strcmp (m68hc11_opcodes[i].name, alias_opcodes[j].name) == 0)
633               {
634                 opcodes[num_opcodes] = m68hc11_opcodes[i];
635                 opcodes[num_opcodes].name = alias_opcodes[j].alias;
636                 num_opcodes++;
637                 break;
638               }
639         }
640     }
641   qsort (opcodes, num_opcodes, sizeof (struct m68hc11_opcode), cmp_opcode);
642
643   opc = (struct m68hc11_opcode_def *)
644     xmalloc (num_opcodes * sizeof (struct m68hc11_opcode_def));
645   m68hc11_opcode_defs = opc--;
646
647   /* Insert unique names into hash table.  The M6811 instruction set
648      has several identical opcode names that have different opcodes based
649      on the operands.  This hash table then provides a quick index to
650      the first opcode with a particular name in the opcode table.  */
651   for (i = 0; i < num_opcodes; i++, opcodes++)
652     {
653       int expect;
654
655       if (strcmp (prev_name, opcodes->name))
656         {
657           prev_name = (char *) opcodes->name;
658
659           opc++;
660           opc->format = 0;
661           opc->min_operands = 100;
662           opc->max_operands = 0;
663           opc->nb_modes = 0;
664           opc->opcode = opcodes;
665           opc->used = 0;
666           hash_insert (m68hc11_hash, opcodes->name, (char *) opc);
667         }
668       opc->nb_modes++;
669       opc->format |= opcodes->format;
670
671       /* See how many operands this opcode needs.  */
672       expect = 0;
673       if (opcodes->format & M6811_OP_MASK)
674         expect++;
675       if (opcodes->format & M6811_OP_BITMASK)
676         expect++;
677       if (opcodes->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
678         expect++;
679       if (opcodes->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
680         expect++;
681       /* Special case for call instruction.  */
682       if ((opcodes->format & M6812_OP_PAGE)
683           && !(opcodes->format & M6811_OP_IND16))
684         expect++;
685
686       if (expect < opc->min_operands)
687         opc->min_operands = expect;
688       if (IS_CALL_SYMBOL (opcodes->format))
689          expect++;
690       if (expect > opc->max_operands)
691         opc->max_operands = expect;
692     }
693   opc++;
694   m68hc11_nb_opcode_defs = opc - m68hc11_opcode_defs;
695
696   if (flag_print_opcodes)
697     {
698       print_opcode_list ();
699       exit (EXIT_SUCCESS);
700     }
701 }
702
703 void
704 m68hc11_init_after_args ()
705 {
706 }
707 \f
708 /* Builtin help.  */
709
710 /* Return a string that represents the operand format for the instruction.
711    When example is true, this generates an example of operand.  This is used
712    to give an example and also to generate a test.  */
713 static char *
714 print_opcode_format (opcode, example)
715      struct m68hc11_opcode *opcode;
716      int example;
717 {
718   static char buf[128];
719   int format = opcode->format;
720   char *p;
721
722   p = buf;
723   buf[0] = 0;
724   if (format & M6811_OP_IMM8)
725     {
726       if (example)
727         sprintf (p, "#%d", rand () & 0x0FF);
728       else
729         strcpy (p, _("#<imm8>"));
730       p = &p[strlen (p)];
731     }
732
733   if (format & M6811_OP_IMM16)
734     {
735       if (example)
736         sprintf (p, "#%d", rand () & 0x0FFFF);
737       else
738         strcpy (p, _("#<imm16>"));
739       p = &p[strlen (p)];
740     }
741
742   if (format & M6811_OP_IX)
743     {
744       if (example)
745         sprintf (p, "%d,X", rand () & 0x0FF);
746       else
747         strcpy (p, _("<imm8>,X"));
748       p = &p[strlen (p)];
749     }
750
751   if (format & M6811_OP_IY)
752     {
753       if (example)
754         sprintf (p, "%d,X", rand () & 0x0FF);
755       else
756         strcpy (p, _("<imm8>,X"));
757       p = &p[strlen (p)];
758     }
759
760   if (format & M6812_OP_IDX)
761     {
762       if (example)
763         sprintf (p, "%d,X", rand () & 0x0FF);
764       else
765         strcpy (p, "n,r");
766       p = &p[strlen (p)];
767     }
768
769   if (format & M6812_OP_PAGE)
770     {
771       if (example)
772         sprintf (p, ", %d", rand () & 0x0FF);
773       else
774         strcpy (p, ", <page>");
775       p = &p[strlen (p)];
776     }
777
778   if (format & M6811_OP_DIRECT)
779     {
780       if (example)
781         sprintf (p, "*Z%d", rand () & 0x0FF);
782       else
783         strcpy (p, _("*<abs8>"));
784       p = &p[strlen (p)];
785     }
786
787   if (format & M6811_OP_BITMASK)
788     {
789       if (buf[0])
790         *p++ = ' ';
791
792       if (example)
793         sprintf (p, "#$%02x", rand () & 0x0FF);
794       else
795         strcpy (p, _("#<mask>"));
796
797       p = &p[strlen (p)];
798       if (format & M6811_OP_JUMP_REL)
799         *p++ = ' ';
800     }
801
802   if (format & M6811_OP_IND16)
803     {
804       if (example)
805         sprintf (p, _("symbol%d"), rand () & 0x0FF);
806       else
807         strcpy (p, _("<abs>"));
808
809       p = &p[strlen (p)];
810     }
811
812   if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
813     {
814       if (example)
815         {
816           if (format & M6811_OP_BITMASK)
817             {
818               sprintf (p, ".+%d", rand () & 0x7F);
819             }
820           else
821             {
822               sprintf (p, "L%d", rand () & 0x0FF);
823             }
824         }
825       else
826         strcpy (p, _("<label>"));
827     }
828
829   return buf;
830 }
831
832 /* Prints the list of instructions with the possible operands.  */
833 static void
834 print_opcode_list ()
835 {
836   int i;
837   char *prev_name = "";
838   struct m68hc11_opcode *opcodes;
839   int example = flag_print_opcodes == 2;
840
841   if (example)
842     printf (_("# Example of `%s' instructions\n\t.sect .text\n_start:\n"),
843             default_cpu);
844
845   opcodes = m68hc11_sorted_opcodes;
846
847   /* Walk the list sorted on names (by md_begin).  We only report
848      one instruction per line, and we collect the different operand
849      formats.  */
850   for (i = 0; i < num_opcodes; i++, opcodes++)
851     {
852       char *fmt = print_opcode_format (opcodes, example);
853
854       if (example)
855         {
856           printf ("L%d:\t", i);
857           printf ("%s %s\n", opcodes->name, fmt);
858         }
859       else
860         {
861           if (strcmp (prev_name, opcodes->name))
862             {
863               if (i > 0)
864                 printf ("\n");
865
866               printf ("%-5.5s ", opcodes->name);
867               prev_name = (char *) opcodes->name;
868             }
869           if (fmt[0])
870             printf ("  [%s]", fmt);
871         }
872     }
873   printf ("\n");
874 }
875
876 /* Print the instruction format.  This operation is called when some
877    instruction is not correct.  Instruction format is printed as an
878    error message.  */
879 static void
880 print_insn_format (name)
881      char *name;
882 {
883   struct m68hc11_opcode_def *opc;
884   struct m68hc11_opcode *opcode;
885   char buf[128];
886
887   opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, name);
888   if (opc == NULL)
889     {
890       as_bad (_("Instruction `%s' is not recognized."), name);
891       return;
892     }
893   opcode = opc->opcode;
894
895   as_bad (_("Instruction formats for `%s':"), name);
896   do
897     {
898       char *fmt;
899
900       fmt = print_opcode_format (opcode, 0);
901       sprintf (buf, "\t%-5.5s %s", opcode->name, fmt);
902
903       as_bad ("%s", buf);
904       opcode++;
905     }
906   while (strcmp (opcode->name, name) == 0);
907 }
908 \f
909 /* Analysis of 68HC11 and 68HC12 operands.  */
910
911 /* reg_name_search() finds the register number given its name.
912    Returns the register number or REG_NONE on failure.  */
913 static register_id
914 reg_name_search (name)
915      char *name;
916 {
917   if (strcasecmp (name, "x") == 0 || strcasecmp (name, "ix") == 0)
918     return REG_X;
919   if (strcasecmp (name, "y") == 0 || strcasecmp (name, "iy") == 0)
920     return REG_Y;
921   if (strcasecmp (name, "a") == 0)
922     return REG_A;
923   if (strcasecmp (name, "b") == 0)
924     return REG_B;
925   if (strcasecmp (name, "d") == 0)
926     return REG_D;
927   if (strcasecmp (name, "sp") == 0)
928     return REG_SP;
929   if (strcasecmp (name, "pc") == 0)
930     return REG_PC;
931   if (strcasecmp (name, "ccr") == 0)
932     return REG_CCR;
933
934   return REG_NONE;
935 }
936
937 static char *
938 skip_whites (p)
939      char *p;
940 {
941   while (*p == ' ' || *p == '\t')
942     p++;
943
944   return p;
945 }
946
947 /* Check the string at input_line_pointer
948    to see if it is a valid register name.  */
949 static register_id
950 register_name ()
951 {
952   register_id reg_number;
953   char c, *p = input_line_pointer;
954
955   if (!is_name_beginner (*p++))
956     return REG_NONE;
957
958   while (is_part_of_name (*p++))
959     continue;
960
961   c = *--p;
962   if (c)
963     *p++ = 0;
964
965   /* Look to see if it's in the register table.  */
966   reg_number = reg_name_search (input_line_pointer);
967   if (reg_number != REG_NONE)
968     {
969       if (c)
970         *--p = c;
971
972       input_line_pointer = p;
973       return reg_number;
974     }
975   if (c)
976     *--p = c;
977
978   return reg_number;
979 }
980
981 /* Parse a string of operands and return an array of expressions.
982
983    Operand      mode[0]         mode[1]       exp[0]       exp[1]
984    #n           M6811_OP_IMM16  -             O_*
985    *<exp>       M6811_OP_DIRECT -             O_*
986    .{+-}<exp>   M6811_OP_JUMP_REL -           O_*
987    <exp>        M6811_OP_IND16  -             O_*
988    ,r N,r       M6812_OP_IDX    M6812_OP_REG  O_constant   O_register
989    n,-r         M6812_PRE_DEC   M6812_OP_REG  O_constant   O_register
990    n,+r         M6812_PRE_INC   " "
991    n,r-         M6812_POST_DEC  " "
992    n,r+         M6812_POST_INC  " "
993    A,r B,r D,r  M6811_OP_REG    M6812_OP_REG  O_register   O_register
994    [D,r]        M6811_OP_D_IDX  M6812_OP_REG  O_register   O_register
995    [n,r]        M6811_OP_D_IDX_2 M6812_OP_REG  O_constant   O_register  */
996 static int
997 get_operand (oper, which, opmode)
998      operand *oper;
999      int which;
1000      long opmode;
1001 {
1002   char *p = input_line_pointer;
1003   int mode;
1004   register_id reg;
1005
1006   oper->exp.X_op = O_absent;
1007   oper->reg1 = REG_NONE;
1008   oper->reg2 = REG_NONE;
1009   mode = M6811_OP_NONE;
1010
1011   p = skip_whites (p);
1012
1013   if (*p == 0 || *p == '\n' || *p == '\r')
1014     {
1015       input_line_pointer = p;
1016       return 0;
1017     }
1018
1019   if (*p == '*' && (opmode & (M6811_OP_DIRECT | M6811_OP_IND16)))
1020     {
1021       mode = M6811_OP_DIRECT;
1022       p++;
1023     }
1024   else if (*p == '#')
1025     {
1026       if (!(opmode & (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK)))
1027         {
1028           as_bad (_("Immediate operand is not allowed for operand %d."),
1029                   which);
1030           return -1;
1031         }
1032
1033       mode = M6811_OP_IMM16;
1034       p++;
1035       if (strncmp (p, "%hi", 3) == 0)
1036         {
1037           p += 3;
1038           mode |= M6811_OP_HIGH_ADDR;
1039         }
1040       else if (strncmp (p, "%lo", 3) == 0)
1041         {
1042           p += 3;
1043           mode |= M6811_OP_LOW_ADDR;
1044         }
1045     }
1046   else if (*p == '.' && (p[1] == '+' || p[1] == '-'))
1047     {
1048       p++;
1049       mode = M6811_OP_JUMP_REL;
1050     }
1051   else if (*p == '[')
1052     {
1053       if (current_architecture & cpu6811)
1054         as_bad (_("Indirect indexed addressing is not valid for 68HC11."));
1055
1056       p++;
1057       mode = M6812_OP_D_IDX;
1058       p = skip_whites (p);
1059     }
1060   else if (*p == ',')           /* Special handling of ,x and ,y.  */
1061     {
1062       p++;
1063       input_line_pointer = p;
1064
1065       reg = register_name ();
1066       if (reg != REG_NONE)
1067         {
1068           oper->reg1 = reg;
1069           oper->exp.X_op = O_constant;
1070           oper->exp.X_add_number = 0;
1071           oper->mode = M6812_OP_IDX;
1072           return 1;
1073         }
1074       as_bad (_("Spurious `,' or bad indirect register addressing mode."));
1075       return -1;
1076     }
1077   input_line_pointer = p;
1078
1079   if (mode == M6811_OP_NONE || mode == M6812_OP_D_IDX)
1080     reg = register_name ();
1081   else
1082     reg = REG_NONE;
1083
1084   if (reg != REG_NONE)
1085     {
1086       p = skip_whites (input_line_pointer);
1087       if (*p == ']' && mode == M6812_OP_D_IDX)
1088         {
1089           as_bad
1090             (_("Missing second register or offset for indexed-indirect mode."));
1091           return -1;
1092         }
1093
1094       oper->reg1 = reg;
1095       oper->mode = mode | M6812_OP_REG;
1096       if (*p != ',')
1097         {
1098           if (mode == M6812_OP_D_IDX)
1099             {
1100               as_bad (_("Missing second register for indexed-indirect mode."));
1101               return -1;
1102             }
1103           return 1;
1104         }
1105
1106       p++;
1107       input_line_pointer = p;
1108       reg = register_name ();
1109       if (reg != REG_NONE)
1110         {
1111           p = skip_whites (input_line_pointer);
1112           if (mode == M6812_OP_D_IDX)
1113             {
1114               if (*p != ']')
1115                 {
1116                   as_bad (_("Missing `]' to close indexed-indirect mode."));
1117                   return -1;
1118                 }
1119               p++;
1120               oper->mode = M6812_OP_D_IDX;
1121             }
1122           input_line_pointer = p;
1123
1124           oper->reg2 = reg;
1125           return 1;
1126         }
1127       return 1;
1128     }
1129
1130   /* In MRI mode, isolate the operand because we can't distinguish
1131      operands from comments.  */
1132   if (flag_mri)
1133     {
1134       char c = 0;
1135
1136       p = skip_whites (p);
1137       while (*p && *p != ' ' && *p != '\t')
1138         p++;
1139
1140       if (*p)
1141         {
1142           c = *p;
1143           *p = 0;
1144         }
1145
1146       /* Parse as an expression.  */
1147       expression (&oper->exp);
1148
1149       if (c)
1150         {
1151           *p = c;
1152         }
1153     }
1154   else
1155     {
1156       expression (&oper->exp);
1157     }
1158
1159   if (oper->exp.X_op == O_illegal)
1160     {
1161       as_bad (_("Illegal operand."));
1162       return -1;
1163     }
1164   else if (oper->exp.X_op == O_absent)
1165     {
1166       as_bad (_("Missing operand."));
1167       return -1;
1168     }
1169
1170   p = input_line_pointer;
1171
1172   if (mode == M6811_OP_NONE || mode == M6811_OP_DIRECT
1173       || mode == M6812_OP_D_IDX)
1174     {
1175       p = skip_whites (input_line_pointer);
1176
1177       if (*p == ',')
1178         {
1179           int possible_mode = M6811_OP_NONE;
1180           char *old_input_line;
1181
1182           old_input_line = p;
1183           p++;
1184
1185           /* 68HC12 pre increment or decrement.  */
1186           if (mode == M6811_OP_NONE)
1187             {
1188               if (*p == '-')
1189                 {
1190                   possible_mode = M6812_PRE_DEC;
1191                   p++;
1192                 }
1193               else if (*p == '+')
1194                 {
1195                   possible_mode = M6812_PRE_INC;
1196                   p++;
1197                 }
1198               p = skip_whites (p);
1199             }
1200           input_line_pointer = p;
1201           reg = register_name ();
1202
1203           /* Backtrack if we have a valid constant expression and
1204              it does not correspond to the offset of the 68HC12 indexed
1205              addressing mode (as in N,x).  */
1206           if (reg == REG_NONE && mode == M6811_OP_NONE
1207               && possible_mode != M6811_OP_NONE)
1208             {
1209               oper->mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
1210               input_line_pointer = skip_whites (old_input_line);
1211               return 1;
1212             }
1213
1214           if (possible_mode != M6811_OP_NONE)
1215             mode = possible_mode;
1216
1217           if ((current_architecture & cpu6811)
1218               && possible_mode != M6811_OP_NONE)
1219             as_bad (_("Pre-increment mode is not valid for 68HC11"));
1220           /* Backtrack.  */
1221           if (which == 0 && opmode & M6812_OP_IDX_P2
1222               && reg != REG_X && reg != REG_Y
1223               && reg != REG_PC && reg != REG_SP)
1224             {
1225               reg = REG_NONE;
1226               input_line_pointer = p;
1227             }
1228
1229           if (reg == REG_NONE && mode != M6811_OP_DIRECT
1230               && !(mode == M6811_OP_NONE && opmode & M6811_OP_IND16))
1231             {
1232               as_bad (_("Wrong register in register indirect mode."));
1233               return -1;
1234             }
1235           if (mode == M6812_OP_D_IDX)
1236             {
1237               p = skip_whites (input_line_pointer);
1238               if (*p++ != ']')
1239                 {
1240                   as_bad (_("Missing `]' to close register indirect operand."));
1241                   return -1;
1242                 }
1243               input_line_pointer = p;
1244               oper->reg1 = reg;
1245               oper->mode = M6812_OP_D_IDX_2;
1246               return 1;
1247             }
1248           if (reg != REG_NONE)
1249             {
1250               oper->reg1 = reg;
1251               if (mode == M6811_OP_NONE)
1252                 {
1253                   p = input_line_pointer;
1254                   if (*p == '-')
1255                     {
1256                       mode = M6812_POST_DEC;
1257                       p++;
1258                       if (current_architecture & cpu6811)
1259                         as_bad
1260                           (_("Post-decrement mode is not valid for 68HC11."));
1261                     }
1262                   else if (*p == '+')
1263                     {
1264                       mode = M6812_POST_INC;
1265                       p++;
1266                       if (current_architecture & cpu6811)
1267                         as_bad
1268                           (_("Post-increment mode is not valid for 68HC11."));
1269                     }
1270                   else
1271                     mode = M6812_OP_IDX;
1272
1273                   input_line_pointer = p;
1274                 }
1275               else
1276                 mode |= M6812_OP_IDX;
1277
1278               oper->mode = mode;
1279               return 1;
1280             }
1281           input_line_pointer = old_input_line;
1282         }
1283
1284       if (mode == M6812_OP_D_IDX_2)
1285         {
1286           as_bad (_("Invalid indexed indirect mode."));
1287           return -1;
1288         }
1289     }
1290
1291   /* If the mode is not known until now, this is either a label
1292      or an indirect address.  */
1293   if (mode == M6811_OP_NONE)
1294     mode = M6811_OP_IND16 | M6811_OP_JUMP_REL;
1295
1296   p = input_line_pointer;
1297   while (*p == ' ' || *p == '\t')
1298     p++;
1299   input_line_pointer = p;
1300   oper->mode = mode;
1301
1302   return 1;
1303 }
1304
1305 #define M6812_AUTO_INC_DEC (M6812_PRE_INC | M6812_PRE_DEC \
1306                             | M6812_POST_INC | M6812_POST_DEC)
1307
1308 /* Checks that the number 'num' fits for a given mode.  */
1309 static int
1310 check_range (num, mode)
1311      long num;
1312      int mode;
1313 {
1314   /* Auto increment and decrement are ok for [-8..8] without 0.  */
1315   if (mode & M6812_AUTO_INC_DEC)
1316     return (num != 0 && num <= 8 && num >= -8);
1317
1318   /* The 68HC12 supports 5, 9 and 16-bit offsets.  */
1319   if (mode & (M6812_INDEXED_IND | M6812_INDEXED | M6812_OP_IDX))
1320     mode = M6811_OP_IND16;
1321
1322   if (mode & M6812_OP_JUMP_REL16)
1323     mode = M6811_OP_IND16;
1324
1325   mode &= ~M6811_OP_BRANCH;
1326   switch (mode)
1327     {
1328     case M6811_OP_IX:
1329     case M6811_OP_IY:
1330     case M6811_OP_DIRECT:
1331       return (num >= 0 && num <= 255) ? 1 : 0;
1332
1333     case M6811_OP_BITMASK:
1334     case M6811_OP_IMM8:
1335     case M6812_OP_PAGE:
1336       return (((num & 0xFFFFFF00) == 0) || ((num & 0xFFFFFF00) == 0xFFFFFF00))
1337         ? 1 : 0;
1338
1339     case M6811_OP_JUMP_REL:
1340       return (num >= -128 && num <= 127) ? 1 : 0;
1341
1342     case M6811_OP_IND16:
1343     case M6811_OP_IND16 | M6812_OP_PAGE:
1344     case M6811_OP_IMM16:
1345       return (((num & 0xFFFF0000) == 0) || ((num & 0xFFFF0000) == 0xFFFF0000))
1346         ? 1 : 0;
1347
1348     case M6812_OP_IBCC_MARKER:
1349     case M6812_OP_TBCC_MARKER:
1350     case M6812_OP_DBCC_MARKER:
1351       return (num >= -256 && num <= 255) ? 1 : 0;
1352
1353     case M6812_OP_TRAP_ID:
1354       return ((num >= 0x30 && num <= 0x39)
1355               || (num >= 0x40 && num <= 0x0ff)) ? 1 : 0;
1356
1357     default:
1358       return 0;
1359     }
1360 }
1361 \f
1362 /* Gas fixup generation.  */
1363
1364 /* Put a 1 byte expression described by 'oper'.  If this expression contains
1365    unresolved symbols, generate an 8-bit fixup.  */
1366 static void
1367 fixup8 (oper, mode, opmode)
1368      expressionS *oper;
1369      int mode;
1370      int opmode;
1371 {
1372   char *f;
1373
1374   f = frag_more (1);
1375
1376   if (oper->X_op == O_constant)
1377     {
1378       if (mode & M6812_OP_TRAP_ID
1379           && !check_range (oper->X_add_number, M6812_OP_TRAP_ID))
1380         {
1381           static char trap_id_warn_once = 0;
1382
1383           as_bad (_("Trap id `%ld' is out of range."), oper->X_add_number);
1384           if (trap_id_warn_once == 0)
1385             {
1386               trap_id_warn_once = 1;
1387               as_bad (_("Trap id must be within [0x30..0x39] or [0x40..0xff]."));
1388             }
1389         }
1390
1391       if (!(mode & M6812_OP_TRAP_ID)
1392           && !check_range (oper->X_add_number, mode))
1393         {
1394           as_bad (_("Operand out of 8-bit range: `%ld'."), oper->X_add_number);
1395         }
1396       number_to_chars_bigendian (f, oper->X_add_number & 0x0FF, 1);
1397     }
1398   else if (oper->X_op != O_register)
1399     {
1400       if (mode & M6812_OP_TRAP_ID)
1401         as_bad (_("The trap id must be a constant."));
1402
1403       if (mode == M6811_OP_JUMP_REL)
1404         {
1405           fixS *fixp;
1406
1407           fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
1408                               oper, TRUE, BFD_RELOC_8_PCREL);
1409           fixp->fx_pcrel_adjust = 1;
1410         }
1411       else
1412         {
1413           /* Now create an 8-bit fixup.  If there was some %hi or %lo
1414              modifier, generate the reloc accordingly.  */
1415           fix_new_exp (frag_now, f - frag_now->fr_literal, 1,
1416                        oper, FALSE,
1417                        ((opmode & M6811_OP_HIGH_ADDR)
1418                         ? BFD_RELOC_M68HC11_HI8
1419                         : ((opmode & M6811_OP_LOW_ADDR)
1420                            ? BFD_RELOC_M68HC11_LO8
1421                            : ((mode & M6812_OP_PAGE)
1422                               ? BFD_RELOC_M68HC11_PAGE : BFD_RELOC_8))));
1423         }
1424       number_to_chars_bigendian (f, 0, 1);
1425     }
1426   else
1427     {
1428       as_fatal (_("Operand `%x' not recognized in fixup8."), oper->X_op);
1429     }
1430 }
1431
1432 /* Put a 2 byte expression described by 'oper'.  If this expression contains
1433    unresolved symbols, generate a 16-bit fixup.  */
1434 static void
1435 fixup16 (oper, mode, opmode)
1436      expressionS *oper;
1437      int mode;
1438      int opmode ATTRIBUTE_UNUSED;
1439 {
1440   char *f;
1441
1442   f = frag_more (2);
1443
1444   if (oper->X_op == O_constant)
1445     {
1446       if (!check_range (oper->X_add_number, mode))
1447         {
1448           as_bad (_("Operand out of 16-bit range: `%ld'."),
1449                   oper->X_add_number);
1450         }
1451       number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFF, 2);
1452     }
1453   else if (oper->X_op != O_register)
1454     {
1455       fixS *fixp;
1456
1457       /* Now create a 16-bit fixup.  */
1458       fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 2,
1459                           oper,
1460                           (mode & M6812_OP_JUMP_REL16 ? TRUE : FALSE),
1461                           (mode & M6812_OP_JUMP_REL16
1462                            ? BFD_RELOC_16_PCREL
1463                            : (mode & M6812_OP_PAGE)
1464                            ? BFD_RELOC_M68HC11_LO16 : BFD_RELOC_16));
1465       number_to_chars_bigendian (f, 0, 2);
1466       if (mode & M6812_OP_JUMP_REL16)
1467         fixp->fx_pcrel_adjust = 2;
1468     }
1469   else
1470     {
1471       as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
1472     }
1473 }
1474
1475 /* Put a 3 byte expression described by 'oper'.  If this expression contains
1476    unresolved symbols, generate a 24-bit fixup.  */
1477 static void
1478 fixup24 (oper, mode, opmode)
1479      expressionS *oper;
1480      int mode;
1481      int opmode ATTRIBUTE_UNUSED;
1482 {
1483   char *f;
1484
1485   f = frag_more (3);
1486
1487   if (oper->X_op == O_constant)
1488     {
1489       if (!check_range (oper->X_add_number, mode))
1490         {
1491           as_bad (_("Operand out of 16-bit range: `%ld'."),
1492                   oper->X_add_number);
1493         }
1494       number_to_chars_bigendian (f, oper->X_add_number & 0x0FFFFFF, 3);
1495     }
1496   else if (oper->X_op != O_register)
1497     {
1498       fixS *fixp;
1499
1500       /* Now create a 24-bit fixup.  */
1501       fixp = fix_new_exp (frag_now, f - frag_now->fr_literal, 2,
1502                           oper, FALSE, BFD_RELOC_M68HC11_24);
1503       number_to_chars_bigendian (f, 0, 3);
1504     }
1505   else
1506     {
1507       as_fatal (_("Operand `%x' not recognized in fixup16."), oper->X_op);
1508     }
1509 }
1510 \f
1511 /* 68HC11 and 68HC12 code generation.  */
1512
1513 /* Translate the short branch/bsr instruction into a long branch.  */
1514 static unsigned char
1515 convert_branch (code)
1516      unsigned char code;
1517 {
1518   if (IS_OPCODE (code, M6812_BSR))
1519     return M6812_JSR;
1520   else if (IS_OPCODE (code, M6811_BSR))
1521     return M6811_JSR;
1522   else if (IS_OPCODE (code, M6811_BRA))
1523     return (current_architecture & cpu6812) ? M6812_JMP : M6811_JMP;
1524   else
1525     as_fatal (_("Unexpected branch conversion with `%x'"), code);
1526
1527   /* Keep gcc happy.  */
1528   return M6811_JSR;
1529 }
1530
1531 /* Start a new insn that contains at least 'size' bytes.  Record the
1532    line information of that insn in the dwarf2 debug sections.  */
1533 static char *
1534 m68hc11_new_insn (size)
1535      int size;
1536 {
1537   char *f;
1538
1539   f = frag_more (size);
1540
1541   dwarf2_emit_insn (size);
1542
1543   return f;
1544 }
1545
1546 /* Builds a jump instruction (bra, bcc, bsr).  */
1547 static void
1548 build_jump_insn (opcode, operands, nb_operands, jmp_mode)
1549      struct m68hc11_opcode *opcode;
1550      operand operands[];
1551      int nb_operands;
1552      int jmp_mode;
1553 {
1554   unsigned char code;
1555   char *f;
1556   unsigned long n;
1557   fragS *frag;
1558   int where;
1559
1560   /* The relative branch convertion is not supported for
1561      brclr and brset.  */
1562   assert ((opcode->format & M6811_OP_BITMASK) == 0);
1563   assert (nb_operands == 1);
1564   assert (operands[0].reg1 == REG_NONE && operands[0].reg2 == REG_NONE);
1565
1566   code = opcode->opcode;
1567
1568   n = operands[0].exp.X_add_number;
1569
1570   /* Turn into a long branch:
1571      - when force long branch option (and not for jbcc pseudos),
1572      - when jbcc and the constant is out of -128..127 range,
1573      - when branch optimization is allowed and branch out of range.  */
1574   if ((jmp_mode == 0 && flag_force_long_jumps)
1575       || (operands[0].exp.X_op == O_constant
1576           && (!check_range (n, opcode->format) &&
1577               (jmp_mode == 1 || flag_fixed_branchs == 0))))
1578     {
1579       frag = frag_now;
1580       where = frag_now_fix ();
1581
1582       fix_new (frag_now, frag_now_fix (), 1,
1583                &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
1584
1585       if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
1586         {
1587           code = convert_branch (code);
1588
1589           f = m68hc11_new_insn (1);
1590           number_to_chars_bigendian (f, code, 1);
1591         }
1592       else if (current_architecture & cpu6812)
1593         {
1594           /* 68HC12: translate the bcc into a lbcc.  */
1595           f = m68hc11_new_insn (2);
1596           number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
1597           number_to_chars_bigendian (f + 1, code, 1);
1598           fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16,
1599                    M6812_OP_JUMP_REL16);
1600           return;
1601         }
1602       else
1603         {
1604           /* 68HC11: translate the bcc into b!cc +3; jmp <L>.  */
1605           f = m68hc11_new_insn (3);
1606           code ^= 1;
1607           number_to_chars_bigendian (f, code, 1);
1608           number_to_chars_bigendian (f + 1, 3, 1);
1609           number_to_chars_bigendian (f + 2, M6811_JMP, 1);
1610         }
1611       fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
1612       return;
1613     }
1614
1615   /* Branch with a constant that must fit in 8-bits.  */
1616   if (operands[0].exp.X_op == O_constant)
1617     {
1618       if (!check_range (n, opcode->format))
1619         {
1620           as_bad (_("Operand out of range for a relative branch: `%ld'"),
1621                   n);
1622         }
1623       else if (opcode->format & M6812_OP_JUMP_REL16)
1624         {
1625           f = m68hc11_new_insn (4);
1626           number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
1627           number_to_chars_bigendian (f + 1, code, 1);
1628           number_to_chars_bigendian (f + 2, n & 0x0ffff, 2);
1629         }
1630       else
1631         {
1632           f = m68hc11_new_insn (2);
1633           number_to_chars_bigendian (f, code, 1);
1634           number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
1635         }
1636     }
1637   else if (opcode->format & M6812_OP_JUMP_REL16)
1638     {
1639       frag = frag_now;
1640       where = frag_now_fix ();
1641
1642       fix_new (frag_now, frag_now_fix (), 1,
1643                &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
1644
1645       f = m68hc11_new_insn (2);
1646       number_to_chars_bigendian (f, M6811_OPCODE_PAGE2, 1);
1647       number_to_chars_bigendian (f + 1, code, 1);
1648       fixup16 (&operands[0].exp, M6812_OP_JUMP_REL16, M6812_OP_JUMP_REL16);
1649     }
1650   else
1651     {
1652       char *opcode;
1653
1654       frag = frag_now;
1655       where = frag_now_fix ();
1656       
1657       fix_new (frag_now, frag_now_fix (), 1,
1658                &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
1659
1660       /* Branch offset must fit in 8-bits, don't do some relax.  */
1661       if (jmp_mode == 0 && flag_fixed_branchs)
1662         {
1663           opcode = m68hc11_new_insn (1);
1664           number_to_chars_bigendian (opcode, code, 1);
1665           fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
1666         }
1667
1668       /* bra/bsr made be changed into jmp/jsr.  */
1669       else if (code == M6811_BSR || code == M6811_BRA || code == M6812_BSR)
1670         {
1671           /* Allocate worst case storage.  */
1672           opcode = m68hc11_new_insn (3);
1673           number_to_chars_bigendian (opcode, code, 1);
1674           number_to_chars_bigendian (opcode + 1, 0, 1);
1675           frag_variant (rs_machine_dependent, 1, 1,
1676                         ENCODE_RELAX (STATE_PC_RELATIVE, STATE_UNDF),
1677                         operands[0].exp.X_add_symbol, (offsetT) n,
1678                         opcode);
1679         }
1680       else if (current_architecture & cpu6812)
1681         {
1682           opcode = m68hc11_new_insn (2);
1683           number_to_chars_bigendian (opcode, code, 1);
1684           number_to_chars_bigendian (opcode + 1, 0, 1);
1685           frag_var (rs_machine_dependent, 2, 2,
1686                     ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_UNDF),
1687                     operands[0].exp.X_add_symbol, (offsetT) n, opcode);
1688         }
1689       else
1690         {
1691           opcode = m68hc11_new_insn (2);
1692           number_to_chars_bigendian (opcode, code, 1);
1693           number_to_chars_bigendian (opcode + 1, 0, 1);
1694           frag_var (rs_machine_dependent, 3, 3,
1695                     ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_UNDF),
1696                     operands[0].exp.X_add_symbol, (offsetT) n, opcode);
1697         }
1698     }
1699 }
1700
1701 /* Builds a dbne/dbeq/tbne/tbeq instruction.  */
1702 static void
1703 build_dbranch_insn (opcode, operands, nb_operands, jmp_mode)
1704      struct m68hc11_opcode *opcode;
1705      operand operands[];
1706      int nb_operands;
1707      int jmp_mode;
1708 {
1709   unsigned char code;
1710   char *f;
1711   unsigned long n;
1712
1713   /* The relative branch convertion is not supported for
1714      brclr and brset.  */
1715   assert ((opcode->format & M6811_OP_BITMASK) == 0);
1716   assert (nb_operands == 2);
1717   assert (operands[0].reg1 != REG_NONE);
1718
1719   code = opcode->opcode & 0x0FF;
1720
1721   f = m68hc11_new_insn (1);
1722   number_to_chars_bigendian (f, code, 1);
1723
1724   n = operands[1].exp.X_add_number;
1725   code = operands[0].reg1;
1726
1727   if (operands[0].reg1 == REG_NONE || operands[0].reg1 == REG_CCR
1728       || operands[0].reg1 == REG_PC)
1729     as_bad (_("Invalid register for dbcc/tbcc instruction."));
1730
1731   if (opcode->format & M6812_OP_IBCC_MARKER)
1732     code |= 0x80;
1733   else if (opcode->format & M6812_OP_TBCC_MARKER)
1734     code |= 0x40;
1735
1736   if (!(opcode->format & M6812_OP_EQ_MARKER))
1737     code |= 0x20;
1738
1739   /* Turn into a long branch:
1740      - when force long branch option (and not for jbcc pseudos),
1741      - when jdbcc and the constant is out of -256..255 range,
1742      - when branch optimization is allowed and branch out of range.  */
1743   if ((jmp_mode == 0 && flag_force_long_jumps)
1744       || (operands[1].exp.X_op == O_constant
1745           && (!check_range (n, M6812_OP_IBCC_MARKER) &&
1746               (jmp_mode == 1 || flag_fixed_branchs == 0))))
1747     {
1748       f = frag_more (2);
1749       code ^= 0x20;
1750       number_to_chars_bigendian (f, code, 1);
1751       number_to_chars_bigendian (f + 1, M6812_JMP, 1);
1752       fixup16 (&operands[0].exp, M6811_OP_IND16, M6811_OP_IND16);
1753       return;
1754     }
1755
1756   /* Branch with a constant that must fit in 9-bits.  */
1757   if (operands[1].exp.X_op == O_constant)
1758     {
1759       if (!check_range (n, M6812_OP_IBCC_MARKER))
1760         {
1761           as_bad (_("Operand out of range for a relative branch: `%ld'"),
1762                   n);
1763         }
1764       else
1765         {
1766           if ((long) n < 0)
1767             code |= 0x10;
1768
1769           f = frag_more (2);
1770           number_to_chars_bigendian (f, code, 1);
1771           number_to_chars_bigendian (f + 1, n & 0x0FF, 1);
1772         }
1773     }
1774   else
1775     {
1776       /* Branch offset must fit in 8-bits, don't do some relax.  */
1777       if (jmp_mode == 0 && flag_fixed_branchs)
1778         {
1779           fixup8 (&operands[0].exp, M6811_OP_JUMP_REL, M6811_OP_JUMP_REL);
1780         }
1781
1782       else
1783         {
1784           f = frag_more (2);
1785           number_to_chars_bigendian (f, code, 1);
1786           number_to_chars_bigendian (f + 1, 0, 1);
1787           frag_var (rs_machine_dependent, 3, 3,
1788                     ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_UNDF),
1789                     operands[1].exp.X_add_symbol, (offsetT) n, f);
1790         }
1791     }
1792 }
1793
1794 #define OP_EXTENDED (M6811_OP_PAGE2 | M6811_OP_PAGE3 | M6811_OP_PAGE4)
1795
1796 /* Assemble the post index byte for 68HC12 extended addressing modes.  */
1797 static int
1798 build_indexed_byte (op, format, move_insn)
1799      operand *op;
1800      int format ATTRIBUTE_UNUSED;
1801      int move_insn;
1802 {
1803   unsigned char byte = 0;
1804   char *f;
1805   int mode;
1806   long val;
1807
1808   val = op->exp.X_add_number;
1809   mode = op->mode;
1810   if (mode & M6812_AUTO_INC_DEC)
1811     {
1812       byte = 0x20;
1813       if (mode & (M6812_POST_INC | M6812_POST_DEC))
1814         byte |= 0x10;
1815
1816       if (op->exp.X_op == O_constant)
1817         {
1818           if (!check_range (val, mode))
1819             {
1820               as_bad (_("Increment/decrement value is out of range: `%ld'."),
1821                       val);
1822             }
1823           if (mode & (M6812_POST_INC | M6812_PRE_INC))
1824             byte |= (val - 1) & 0x07;
1825           else
1826             byte |= (8 - ((val) & 7)) | 0x8;
1827         }
1828       switch (op->reg1)
1829         {
1830         case REG_NONE:
1831           as_fatal (_("Expecting a register."));
1832
1833         case REG_X:
1834           byte |= 0;
1835           break;
1836
1837         case REG_Y:
1838           byte |= 0x40;
1839           break;
1840
1841         case REG_SP:
1842           byte |= 0x80;
1843           break;
1844
1845         default:
1846           as_bad (_("Invalid register for post/pre increment."));
1847           break;
1848         }
1849
1850       f = frag_more (1);
1851       number_to_chars_bigendian (f, byte, 1);
1852       return 1;
1853     }
1854
1855   if (mode & (M6812_OP_IDX | M6812_OP_D_IDX_2))
1856     {
1857       switch (op->reg1)
1858         {
1859         case REG_X:
1860           byte = 0;
1861           break;
1862
1863         case REG_Y:
1864           byte = 1;
1865           break;
1866
1867         case REG_SP:
1868           byte = 2;
1869           break;
1870
1871         case REG_PC:
1872           byte = 3;
1873           break;
1874
1875         default:
1876           as_bad (_("Invalid register."));
1877           break;
1878         }
1879       if (op->exp.X_op == O_constant)
1880         {
1881           if (!check_range (val, M6812_OP_IDX))
1882             {
1883               as_bad (_("Offset out of 16-bit range: %ld."), val);
1884             }
1885
1886           if (move_insn && !(val >= -16 && val <= 15))
1887             {
1888               as_bad (_("Offset out of 5-bit range for movw/movb insn: %ld."),
1889                       val);
1890               return -1;
1891             }
1892
1893           if (val >= -16 && val <= 15 && !(mode & M6812_OP_D_IDX_2))
1894             {
1895               byte = byte << 6;
1896               byte |= val & 0x1f;
1897               f = frag_more (1);
1898               number_to_chars_bigendian (f, byte, 1);
1899               return 1;
1900             }
1901           else if (val >= -256 && val <= 255 && !(mode & M6812_OP_D_IDX_2))
1902             {
1903               byte = byte << 3;
1904               byte |= 0xe0;
1905               if (val < 0)
1906                 byte |= 0x1;
1907               f = frag_more (2);
1908               number_to_chars_bigendian (f, byte, 1);
1909               number_to_chars_bigendian (f + 1, val & 0x0FF, 1);
1910               return 2;
1911             }
1912           else
1913             {
1914               byte = byte << 3;
1915               if (mode & M6812_OP_D_IDX_2)
1916                 byte |= 0xe3;
1917               else
1918                 byte |= 0xe2;
1919
1920               f = frag_more (3);
1921               number_to_chars_bigendian (f, byte, 1);
1922               number_to_chars_bigendian (f + 1, val & 0x0FFFF, 2);
1923               return 3;
1924             }
1925         }
1926       if (mode & M6812_OP_D_IDX_2)
1927         {
1928           byte = (byte << 3) | 0xe3;
1929           f = frag_more (1);
1930           number_to_chars_bigendian (f, byte, 1);
1931
1932           fixup16 (&op->exp, 0, 0);
1933         }
1934       else if (op->reg1 != REG_PC)
1935         {
1936           byte = (byte << 3) | 0xe2;
1937           f = frag_more (1);
1938           number_to_chars_bigendian (f, byte, 1);
1939
1940           f = frag_more (2);
1941           fix_new_exp (frag_now, f - frag_now->fr_literal, 2,
1942                        &op->exp, FALSE, BFD_RELOC_16);
1943           number_to_chars_bigendian (f, 0, 2);
1944         }
1945       else
1946         {
1947           f = frag_more (1);
1948           number_to_chars_bigendian (f, byte, 1);
1949           frag_var (rs_machine_dependent, 2, 2,
1950                     ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_UNDF),
1951                     op->exp.X_add_symbol,
1952                     op->exp.X_add_number, f);
1953         }
1954       return 3;
1955     }
1956
1957   if (mode & (M6812_OP_REG | M6812_OP_D_IDX))
1958     {
1959       if (mode & M6812_OP_D_IDX)
1960         {
1961           if (op->reg1 != REG_D)
1962             as_bad (_("Expecting register D for indexed indirect mode."));
1963           if (move_insn)
1964             as_bad (_("Indexed indirect mode is not allowed for movb/movw."));
1965
1966           byte = 0xE7;
1967         }
1968       else
1969         {
1970           switch (op->reg1)
1971             {
1972             case REG_A:
1973               byte = 0xE4;
1974               break;
1975
1976             case REG_B:
1977               byte = 0xE5;
1978               break;
1979
1980             default:
1981               as_bad (_("Invalid accumulator register."));
1982
1983             case REG_D:
1984               byte = 0xE6;
1985               break;
1986             }
1987         }
1988       switch (op->reg2)
1989         {
1990         case REG_X:
1991           break;
1992
1993         case REG_Y:
1994           byte |= (1 << 3);
1995           break;
1996
1997         case REG_SP:
1998           byte |= (2 << 3);
1999           break;
2000
2001         case REG_PC:
2002           byte |= (3 << 3);
2003           break;
2004
2005         default:
2006           as_bad (_("Invalid indexed register."));
2007           break;
2008         }
2009       f = frag_more (1);
2010       number_to_chars_bigendian (f, byte, 1);
2011       return 1;
2012     }
2013
2014   as_fatal (_("Addressing mode not implemented yet."));
2015   return 0;
2016 }
2017
2018 /* Assemble the 68HC12 register mode byte.  */
2019 static int
2020 build_reg_mode (op, format)
2021      operand *op;
2022      int format;
2023 {
2024   unsigned char byte;
2025   char *f;
2026
2027   if (format & M6812_OP_SEX_MARKER
2028       && op->reg1 != REG_A && op->reg1 != REG_B && op->reg1 != REG_CCR)
2029     as_bad (_("Invalid source register for this instruction, use 'tfr'."));
2030   else if (op->reg1 == REG_NONE || op->reg1 == REG_PC)
2031     as_bad (_("Invalid source register."));
2032
2033   if (format & M6812_OP_SEX_MARKER
2034       && op->reg2 != REG_D
2035       && op->reg2 != REG_X && op->reg2 != REG_Y && op->reg2 != REG_SP)
2036     as_bad (_("Invalid destination register for this instruction, use 'tfr'."));
2037   else if (op->reg2 == REG_NONE || op->reg2 == REG_PC)
2038     as_bad (_("Invalid destination register."));
2039
2040   byte = (op->reg1 << 4) | (op->reg2);
2041   if (format & M6812_OP_EXG_MARKER)
2042     byte |= 0x80;
2043
2044   f = frag_more (1);
2045   number_to_chars_bigendian (f, byte, 1);
2046   return 1;
2047 }
2048
2049 /* build_insn takes a pointer to the opcode entry in the opcode table,
2050    the array of operand expressions and builds the correspding instruction.
2051    This operation only deals with non relative jumps insn (need special
2052    handling).  */
2053 static void
2054 build_insn (opcode, operands, nb_operands)
2055      struct m68hc11_opcode *opcode;
2056      operand operands[];
2057      int nb_operands ATTRIBUTE_UNUSED;
2058 {
2059   int i;
2060   char *f;
2061   long format;
2062   int move_insn = 0;
2063   fragS *frag;
2064   int where;
2065
2066   /* Put the page code instruction if there is one.  */
2067   format = opcode->format;
2068
2069   frag = frag_now;
2070   where = frag_now_fix ();
2071
2072   if (format & M6811_OP_BRANCH)
2073     fix_new (frag, where, 1,
2074              &abs_symbol, 0, 1, BFD_RELOC_M68HC11_RL_JUMP);
2075
2076   if (format & OP_EXTENDED)
2077     {
2078       int page_code;
2079
2080       f = m68hc11_new_insn (2);
2081       if (format & M6811_OP_PAGE2)
2082         page_code = M6811_OPCODE_PAGE2;
2083       else if (format & M6811_OP_PAGE3)
2084         page_code = M6811_OPCODE_PAGE3;
2085       else
2086         page_code = M6811_OPCODE_PAGE4;
2087
2088       number_to_chars_bigendian (f, page_code, 1);
2089       f++;
2090     }
2091   else
2092     f = m68hc11_new_insn (1);
2093
2094   number_to_chars_bigendian (f, opcode->opcode, 1);
2095
2096   i = 0;
2097
2098   /* The 68HC12 movb and movw instructions are special.  We have to handle
2099      them in a special way.  */
2100   if (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
2101     {
2102       move_insn = 1;
2103       if (format & M6812_OP_IDX)
2104         {
2105           build_indexed_byte (&operands[0], format, 1);
2106           i = 1;
2107           format &= ~M6812_OP_IDX;
2108         }
2109       if (format & M6812_OP_IDX_P2)
2110         {
2111           build_indexed_byte (&operands[1], format, 1);
2112           i = 0;
2113           format &= ~M6812_OP_IDX_P2;
2114         }
2115     }
2116
2117   if (format & (M6811_OP_DIRECT | M6811_OP_IMM8))
2118     {
2119       fixup8 (&operands[i].exp,
2120               format & (M6811_OP_DIRECT | M6811_OP_IMM8 | M6812_OP_TRAP_ID),
2121               operands[i].mode);
2122       i++;
2123     }
2124   else if (IS_CALL_SYMBOL (format) && nb_operands == 1)
2125     {
2126       format &= ~M6812_OP_PAGE;
2127       fixup24 (&operands[i].exp, format & M6811_OP_IND16,
2128                operands[i].mode);
2129       i++;
2130     }
2131   else if (format & (M6811_OP_IMM16 | M6811_OP_IND16))
2132     {
2133       fixup16 (&operands[i].exp,
2134                format & (M6811_OP_IMM16 | M6811_OP_IND16 | M6812_OP_PAGE),
2135                operands[i].mode);
2136       i++;
2137     }
2138   else if (format & (M6811_OP_IX | M6811_OP_IY))
2139     {
2140       if ((format & M6811_OP_IX) && (operands[0].reg1 != REG_X))
2141         as_bad (_("Invalid indexed register, expecting register X."));
2142       if ((format & M6811_OP_IY) && (operands[0].reg1 != REG_Y))
2143         as_bad (_("Invalid indexed register, expecting register Y."));
2144
2145       fixup8 (&operands[0].exp, M6811_OP_IX, operands[0].mode);
2146       i = 1;
2147     }
2148   else if (format &
2149            (M6812_OP_IDX | M6812_OP_IDX_2 | M6812_OP_IDX_1
2150             | M6812_OP_D_IDX | M6812_OP_D_IDX_2))
2151     {
2152       build_indexed_byte (&operands[i], format, move_insn);
2153       i++;
2154     }
2155   else if (format & M6812_OP_REG && current_architecture & cpu6812)
2156     {
2157       build_reg_mode (&operands[i], format);
2158       i++;
2159     }
2160   if (format & M6811_OP_BITMASK)
2161     {
2162       fixup8 (&operands[i].exp, M6811_OP_BITMASK, operands[i].mode);
2163       i++;
2164     }
2165   if (format & M6811_OP_JUMP_REL)
2166     {
2167       fixup8 (&operands[i].exp, M6811_OP_JUMP_REL, operands[i].mode);
2168     }
2169   else if (format & M6812_OP_IND16_P2)
2170     {
2171       fixup16 (&operands[1].exp, M6811_OP_IND16, operands[1].mode);
2172     }
2173   if (format & M6812_OP_PAGE)
2174     {
2175       fixup8 (&operands[i].exp, M6812_OP_PAGE, operands[i].mode);
2176     }
2177 }
2178 \f
2179 /* Opcode identification and operand analysis.  */
2180
2181 /* find() gets a pointer to an entry in the opcode table.  It must look at all
2182    opcodes with the same name and use the operands to choose the correct
2183    opcode.  Returns the opcode pointer if there was a match and 0 if none.  */
2184 static struct m68hc11_opcode *
2185 find (opc, operands, nb_operands)
2186      struct m68hc11_opcode_def *opc;
2187      operand operands[];
2188      int nb_operands;
2189 {
2190   int i, match, pos;
2191   struct m68hc11_opcode *opcode;
2192   struct m68hc11_opcode *op_indirect;
2193
2194   op_indirect = 0;
2195   opcode = opc->opcode;
2196
2197   /* Now search the opcode table table for one with operands
2198      that matches what we've got.  We're only done if the operands matched so
2199      far AND there are no more to check.  */
2200   for (pos = match = 0; match == 0 && pos < opc->nb_modes; pos++, opcode++)
2201     {
2202       int poss_indirect = 0;
2203       long format = opcode->format;
2204       int expect;
2205
2206       expect = 0;
2207       if (opcode->format & M6811_OP_MASK)
2208         expect++;
2209       if (opcode->format & M6811_OP_BITMASK)
2210         expect++;
2211       if (opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2212         expect++;
2213       if (opcode->format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2))
2214         expect++;
2215       if ((opcode->format & M6812_OP_PAGE)
2216           && (!IS_CALL_SYMBOL (opcode->format) || nb_operands == 2))
2217         expect++;
2218
2219       for (i = 0; expect == nb_operands && i < nb_operands; i++)
2220         {
2221           int mode = operands[i].mode;
2222
2223           if (mode & M6811_OP_IMM16)
2224             {
2225               if (format &
2226                   (M6811_OP_IMM8 | M6811_OP_IMM16 | M6811_OP_BITMASK))
2227                 continue;
2228               break;
2229             }
2230           if (mode == M6811_OP_DIRECT)
2231             {
2232               if (format & M6811_OP_DIRECT)
2233                 continue;
2234
2235               /* If the operand is a page 0 operand, remember a
2236                  possible <abs-16> addressing mode.  We mark
2237                  this and continue to check other operands.  */
2238               if (format & M6811_OP_IND16
2239                   && flag_strict_direct_addressing && op_indirect == 0)
2240                 {
2241                   poss_indirect = 1;
2242                   continue;
2243                 }
2244               break;
2245             }
2246           if (mode & M6811_OP_IND16)
2247             {
2248               if (i == 0 && (format & M6811_OP_IND16) != 0)
2249                 continue;
2250               if (i != 0 && (format & M6812_OP_PAGE) != 0)
2251                 continue;
2252               if (i != 0 && (format & M6812_OP_IND16_P2) != 0)
2253                 continue;
2254               if (i == 0 && (format & M6811_OP_BITMASK))
2255                 break;
2256             }
2257           if (mode & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2258             {
2259               if (format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2260                 continue;
2261             }
2262           if (mode & M6812_OP_REG)
2263             {
2264               if (i == 0
2265                   && (format & M6812_OP_REG)
2266                   && (operands[i].reg2 == REG_NONE))
2267                 continue;
2268               if (i == 0
2269                   && (format & M6812_OP_REG)
2270                   && (format & M6812_OP_REG_2)
2271                   && (operands[i].reg2 != REG_NONE))
2272                 continue;
2273               if (i == 0
2274                   && (format & M6812_OP_IDX)
2275                   && (operands[i].reg2 != REG_NONE))
2276                 continue;
2277               if (i == 0
2278                   && (format & M6812_OP_IDX)
2279                   && (format & (M6812_OP_IND16_P2 | M6812_OP_IDX_P2)))
2280                 continue;
2281               if (i == 1
2282                   && (format & M6812_OP_IDX_P2))
2283                 continue;
2284               break;
2285             }
2286           if (mode & M6812_OP_IDX)
2287             {
2288               if (format & M6811_OP_IX && operands[i].reg1 == REG_X)
2289                 continue;
2290               if (format & M6811_OP_IY && operands[i].reg1 == REG_Y)
2291                 continue;
2292               if (i == 0
2293                   && format & (M6812_OP_IDX | M6812_OP_IDX_1 | M6812_OP_IDX_2)
2294                   && (operands[i].reg1 == REG_X
2295                       || operands[i].reg1 == REG_Y
2296                       || operands[i].reg1 == REG_SP
2297                       || operands[i].reg1 == REG_PC))
2298                 continue;
2299               if (i == 1 && format & M6812_OP_IDX_P2)
2300                 continue;
2301             }
2302           if (mode & format & (M6812_OP_D_IDX | M6812_OP_D_IDX_2))
2303             {
2304               if (i == 0)
2305                 continue;
2306             }
2307           if (mode & M6812_AUTO_INC_DEC)
2308             {
2309               if (i == 0
2310                   && format & (M6812_OP_IDX | M6812_OP_IDX_1 |
2311                                M6812_OP_IDX_2))
2312                 continue;
2313               if (i == 1 && format & M6812_OP_IDX_P2)
2314                 continue;
2315             }
2316           break;
2317         }
2318       match = i == nb_operands;
2319
2320       /* Operands are ok but an operand uses page 0 addressing mode
2321          while the insn supports abs-16 mode.  Keep a reference to this
2322          insns in case there is no insn supporting page 0 addressing.  */
2323       if (match && poss_indirect)
2324         {
2325           op_indirect = opcode;
2326           match = 0;
2327         }
2328       if (match)
2329         break;
2330     }
2331
2332   /* Page 0 addressing is used but not supported by any insn.
2333      If absolute addresses are supported, we use that insn.  */
2334   if (match == 0 && op_indirect)
2335     {
2336       opcode = op_indirect;
2337       match = 1;
2338     }
2339
2340   if (!match)
2341     {
2342       return (0);
2343     }
2344
2345   return opcode;
2346 }
2347
2348 /* Find the real opcode and its associated operands.  We use a progressive
2349    approach here.  On entry, 'opc' points to the first opcode in the
2350    table that matches the opcode name in the source line.  We try to
2351    isolate an operand, find a possible match in the opcode table.
2352    We isolate another operand if no match were found.  The table 'operands'
2353    is filled while operands are recognized.
2354
2355    Returns the opcode pointer that matches the opcode name in the
2356    source line and the associated operands.  */
2357 static struct m68hc11_opcode *
2358 find_opcode (opc, operands, nb_operands)
2359      struct m68hc11_opcode_def *opc;
2360      operand operands[];
2361      int *nb_operands;
2362 {
2363   struct m68hc11_opcode *opcode;
2364   int i;
2365
2366   if (opc->max_operands == 0)
2367     {
2368       *nb_operands = 0;
2369       return opc->opcode;
2370     }
2371
2372   for (i = 0; i < opc->max_operands;)
2373     {
2374       int result;
2375
2376       result = get_operand (&operands[i], i, opc->format);
2377       if (result <= 0)
2378         return 0;
2379
2380       /* Special case where the bitmask of the bclr/brclr
2381          instructions is not introduced by #.
2382          Example: bclr 3,x $80.  */
2383       if (i == 1 && (opc->format & M6811_OP_BITMASK)
2384           && (operands[i].mode & M6811_OP_IND16))
2385         {
2386           operands[i].mode = M6811_OP_IMM16;
2387         }
2388
2389       i += result;
2390       *nb_operands = i;
2391       if (i >= opc->min_operands)
2392         {
2393           opcode = find (opc, operands, i);
2394           if (opcode && !(opcode->format & M6812_OP_PAGE))
2395              return opcode;
2396
2397           if (opcode && *input_line_pointer != ',')
2398             return opcode;
2399         }
2400
2401       if (*input_line_pointer == ',')
2402         input_line_pointer++;
2403     }
2404
2405   return 0;
2406 }
2407
2408 #define M6812_XBCC_MARKER (M6812_OP_TBCC_MARKER \
2409                            | M6812_OP_DBCC_MARKER \
2410                            | M6812_OP_IBCC_MARKER)
2411 \f
2412 /* Gas line assembler entry point.  */
2413
2414 /* This is the main entry point for the machine-dependent assembler.  str
2415    points to a machine-dependent instruction.  This function is supposed to
2416    emit the frags/bytes it assembles to.  */
2417 void
2418 md_assemble (str)
2419      char *str;
2420 {
2421   struct m68hc11_opcode_def *opc;
2422   struct m68hc11_opcode *opcode;
2423
2424   unsigned char *op_start, *save;
2425   unsigned char *op_end;
2426   char name[20];
2427   int nlen = 0;
2428   operand operands[M6811_MAX_OPERANDS];
2429   int nb_operands;
2430   int branch_optimize = 0;
2431   int alias_id = -1;
2432
2433   /* Drop leading whitespace.  */
2434   while (*str == ' ')
2435     str++;
2436
2437   /* Find the opcode end and get the opcode in 'name'.  The opcode is forced
2438      lower case (the opcode table only has lower case op-codes).  */
2439   for (op_start = op_end = (unsigned char *) (str);
2440        *op_end && nlen < 20 && !is_end_of_line[*op_end] && *op_end != ' ';
2441        op_end++)
2442     {
2443       name[nlen] = TOLOWER (op_start[nlen]);
2444       nlen++;
2445     }
2446   name[nlen] = 0;
2447
2448   if (nlen == 0)
2449     {
2450       as_bad (_("No instruction or missing opcode."));
2451       return;
2452     }
2453
2454   /* Find the opcode definition given its name.  */
2455   opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, name);
2456
2457   /* If it's not recognized, look for 'jbsr' and 'jbxx'.  These are
2458      pseudo insns for relative branch.  For these branchs, we always
2459      optimize them (turned into absolute branchs) even if --short-branchs
2460      is given.  */
2461   if (opc == NULL && name[0] == 'j' && name[1] == 'b')
2462     {
2463       opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash, &name[1]);
2464       if (opc
2465           && (!(opc->format & M6811_OP_JUMP_REL)
2466               || (opc->format & M6811_OP_BITMASK)))
2467         opc = 0;
2468       if (opc)
2469         branch_optimize = 1;
2470     }
2471
2472   /* The following test should probably be removed.  This is not conform
2473      to Motorola assembler specs.  */
2474   if (opc == NULL && flag_mri)
2475     {
2476       if (*op_end == ' ' || *op_end == '\t')
2477         {
2478           while (*op_end == ' ' || *op_end == '\t')
2479             op_end++;
2480
2481           if (nlen < 19
2482               && (*op_end &&
2483                   (is_end_of_line[op_end[1]]
2484                    || op_end[1] == ' ' || op_end[1] == '\t'
2485                    || !ISALNUM (op_end[1])))
2486               && (*op_end == 'a' || *op_end == 'b'
2487                   || *op_end == 'A' || *op_end == 'B'
2488                   || *op_end == 'd' || *op_end == 'D'
2489                   || *op_end == 'x' || *op_end == 'X'
2490                   || *op_end == 'y' || *op_end == 'Y'))
2491             {
2492               name[nlen++] = TOLOWER (*op_end++);
2493               name[nlen] = 0;
2494               opc = (struct m68hc11_opcode_def *) hash_find (m68hc11_hash,
2495                                                              name);
2496             }
2497         }
2498     }
2499
2500   /* Identify a possible instruction alias.  There are some on the
2501      68HC12 to emulate a few 68HC11 instructions.  */
2502   if (opc == NULL && (current_architecture & cpu6812))
2503     {
2504       int i;
2505
2506       for (i = 0; i < m68hc12_num_alias; i++)
2507         if (strcmp (m68hc12_alias[i].name, name) == 0)
2508           {
2509             alias_id = i;
2510             break;
2511           }
2512     }
2513   if (opc == NULL && alias_id < 0)
2514     {
2515       as_bad (_("Opcode `%s' is not recognized."), name);
2516       return;
2517     }
2518   save = input_line_pointer;
2519   input_line_pointer = op_end;
2520
2521   if (opc)
2522     {
2523       opc->used++;
2524       opcode = find_opcode (opc, operands, &nb_operands);
2525     }
2526   else
2527     opcode = 0;
2528
2529   if ((opcode || alias_id >= 0) && !flag_mri)
2530     {
2531       char *p = input_line_pointer;
2532
2533       while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
2534         p++;
2535
2536       if (*p != '\n' && *p)
2537         as_bad (_("Garbage at end of instruction: `%s'."), p);
2538     }
2539
2540   input_line_pointer = save;
2541
2542   if (alias_id >= 0)
2543     {
2544       char *f = m68hc11_new_insn (m68hc12_alias[alias_id].size);
2545
2546       number_to_chars_bigendian (f, m68hc12_alias[alias_id].code1, 1);
2547       if (m68hc12_alias[alias_id].size > 1)
2548         number_to_chars_bigendian (f + 1, m68hc12_alias[alias_id].code2, 1);
2549
2550       return;
2551     }
2552
2553   /* Opcode is known but does not have valid operands.  Print out the
2554      syntax for this opcode.  */
2555   if (opcode == 0)
2556     {
2557       if (flag_print_insn_syntax)
2558         print_insn_format (name);
2559
2560       as_bad (_("Invalid operand for `%s'"), name);
2561       return;
2562     }
2563
2564   /* Treat dbeq/ibeq/tbeq instructions in a special way.  The branch is
2565      relative and must be in the range -256..255 (9-bits).  */
2566   if ((opcode->format & M6812_XBCC_MARKER)
2567       && (opcode->format & M6811_OP_JUMP_REL))
2568     build_dbranch_insn (opcode, operands, nb_operands, branch_optimize);
2569
2570   /* Relative jumps instructions are taken care of separately.  We have to make
2571      sure that the relative branch is within the range -128..127.  If it's out
2572      of range, the instructions are changed into absolute instructions.
2573      This is not supported for the brset and brclr instructions.  */
2574   else if ((opcode->format & (M6811_OP_JUMP_REL | M6812_OP_JUMP_REL16))
2575            && !(opcode->format & M6811_OP_BITMASK))
2576     build_jump_insn (opcode, operands, nb_operands, branch_optimize);
2577   else
2578     build_insn (opcode, operands, nb_operands);
2579 }
2580
2581 \f
2582 /* Pseudo op to control the ELF flags.  */
2583 static void
2584 s_m68hc11_mode (x)
2585      int x ATTRIBUTE_UNUSED;
2586 {
2587   char *name = input_line_pointer, ch;
2588
2589   while (!is_end_of_line[(unsigned char) *input_line_pointer])
2590     input_line_pointer++;
2591   ch = *input_line_pointer;
2592   *input_line_pointer = '\0';
2593
2594   if (strcmp (name, "mshort") == 0)
2595     {
2596       elf_flags &= ~E_M68HC11_I32;
2597     }
2598   else if (strcmp (name, "mlong") == 0)
2599     {
2600       elf_flags |= E_M68HC11_I32;
2601     }
2602   else if (strcmp (name, "mshort-double") == 0)
2603     {
2604       elf_flags &= ~E_M68HC11_F64;
2605     }
2606   else if (strcmp (name, "mlong-double") == 0)
2607     {
2608       elf_flags |= E_M68HC11_F64;
2609     }
2610   else
2611     {
2612       as_warn (_("Invalid mode: %s\n"), name);
2613     }
2614   *input_line_pointer = ch;
2615   demand_empty_rest_of_line ();
2616 }
2617
2618 /* Mark the symbols with STO_M68HC12_FAR to indicate the functions
2619    are using 'rtc' for returning.  It is necessary to use 'call'
2620    to invoke them.  This is also used by the debugger to correctly
2621    find the stack frame.  */
2622 static void
2623 s_m68hc11_mark_symbol (mark)
2624      int mark;
2625 {
2626   char *name;
2627   int c;
2628   symbolS *symbolP;
2629   asymbol *bfdsym;
2630   elf_symbol_type *elfsym;
2631
2632   do
2633     {
2634       name = input_line_pointer;
2635       c = get_symbol_end ();
2636       symbolP = symbol_find_or_make (name);
2637       *input_line_pointer = c;
2638
2639       SKIP_WHITESPACE ();
2640
2641       bfdsym = symbol_get_bfdsym (symbolP);
2642       elfsym = elf_symbol_from (bfd_asymbol_bfd (bfdsym), bfdsym);
2643
2644       assert (elfsym);
2645
2646       /* Mark the symbol far (using rtc for function return).  */
2647       elfsym->internal_elf_sym.st_other |= mark;
2648
2649       if (c == ',')
2650         {
2651           input_line_pointer ++;
2652
2653           SKIP_WHITESPACE ();
2654
2655           if (*input_line_pointer == '\n')
2656             c = '\n';
2657         }
2658     }
2659   while (c == ',');
2660
2661   demand_empty_rest_of_line ();
2662 }
2663
2664 static void
2665 s_m68hc11_relax (ignore)
2666      int ignore ATTRIBUTE_UNUSED;
2667 {
2668   expressionS ex;
2669
2670   expression (&ex);
2671
2672   if (ex.X_op != O_symbol || ex.X_add_number != 0)
2673     {
2674       as_bad (_("bad .relax format"));
2675       ignore_rest_of_line ();
2676       return;
2677     }
2678
2679   fix_new_exp (frag_now, frag_now_fix (), 1, &ex, 1,
2680                BFD_RELOC_M68HC11_RL_GROUP);
2681
2682   demand_empty_rest_of_line ();
2683 }
2684
2685 \f
2686 /* Relocation, relaxation and frag conversions.  */
2687
2688 /* PC-relative offsets are relative to the start of the
2689    next instruction.  That is, the address of the offset, plus its
2690    size, since the offset is always the last part of the insn.  */
2691 long
2692 md_pcrel_from (fixP)
2693      fixS *fixP;
2694 {
2695   if (fixP->fx_r_type == BFD_RELOC_M68HC11_RL_JUMP)
2696     return 0;
2697
2698   return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
2699 }
2700
2701 /* If while processing a fixup, a reloc really needs to be created
2702    then it is done here.  */
2703 arelent *
2704 tc_gen_reloc (section, fixp)
2705      asection *section;
2706      fixS *fixp;
2707 {
2708   arelent *reloc;
2709
2710   reloc = (arelent *) xmalloc (sizeof (arelent));
2711   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2712   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2713   reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
2714   if (fixp->fx_r_type == 0)
2715     reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16);
2716   else
2717     reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
2718   if (reloc->howto == (reloc_howto_type *) NULL)
2719     {
2720       as_bad_where (fixp->fx_file, fixp->fx_line,
2721                     _("Relocation %d is not supported by object file format."),
2722                     (int) fixp->fx_r_type);
2723       return NULL;
2724     }
2725
2726   /* Since we use Rel instead of Rela, encode the vtable entry to be
2727      used in the relocation's section offset.  */
2728   if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
2729     reloc->address = fixp->fx_offset;
2730
2731   reloc->addend = 0;
2732   return reloc;
2733 }
2734
2735 void
2736 md_convert_frag (abfd, sec, fragP)
2737      bfd *abfd ATTRIBUTE_UNUSED;
2738      asection *sec ATTRIBUTE_UNUSED;
2739      fragS *fragP;
2740 {
2741   fixS *fixp;
2742   long value;
2743   long disp;
2744   char *buffer_address = fragP->fr_literal;
2745
2746   /* Address in object code of the displacement.  */
2747   register int object_address = fragP->fr_fix + fragP->fr_address;
2748
2749   buffer_address += fragP->fr_fix;
2750
2751   /* The displacement of the address, from current location.  */
2752   value = S_GET_VALUE (fragP->fr_symbol);
2753   disp = (value + fragP->fr_offset) - object_address;
2754
2755   switch (fragP->fr_subtype)
2756     {
2757     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE):
2758       fragP->fr_opcode[1] = disp;
2759       break;
2760
2761     case ENCODE_RELAX (STATE_PC_RELATIVE, STATE_WORD):
2762       /* This relax is only for bsr and bra.  */
2763       assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
2764               || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
2765               || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
2766
2767       fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
2768
2769       fix_new (fragP, fragP->fr_fix - 1, 2,
2770                fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
2771       fragP->fr_fix += 1;
2772       break;
2773
2774     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_BYTE):
2775     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_BYTE):
2776       fragP->fr_opcode[1] = disp;
2777       break;
2778
2779     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH, STATE_WORD):
2780       /* Invert branch.  */
2781       fragP->fr_opcode[0] ^= 1;
2782       fragP->fr_opcode[1] = 3;  /* Branch offset.  */
2783       buffer_address[0] = M6811_JMP;
2784       fix_new (fragP, fragP->fr_fix + 1, 2,
2785                fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
2786       fragP->fr_fix += 3;
2787       break;
2788
2789     case ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812, STATE_WORD):
2790       /* Translate branch into a long branch.  */
2791       fragP->fr_opcode[1] = fragP->fr_opcode[0];
2792       fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
2793
2794       fixp = fix_new (fragP, fragP->fr_fix, 2,
2795                       fragP->fr_symbol, fragP->fr_offset, 1,
2796                       BFD_RELOC_16_PCREL);
2797       fixp->fx_pcrel_adjust = 2;
2798       fragP->fr_fix += 2;
2799       break;
2800
2801     case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS5):
2802       fragP->fr_opcode[0] = fragP->fr_opcode[0] << 6;
2803       if ((fragP->fr_opcode[0] & 0x0ff) == 0x0c0)
2804         fragP->fr_opcode[0] |= disp & 0x1f;
2805       else
2806         fragP->fr_opcode[0] |= value & 0x1f;
2807       break;
2808
2809     case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS9):
2810       fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
2811       fragP->fr_opcode[0] |= 0xE0;
2812       fix_new (fragP, fragP->fr_fix, 1,
2813                fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_8);
2814       fragP->fr_fix += 1;
2815       break;
2816
2817     case ENCODE_RELAX (STATE_INDEXED_OFFSET, STATE_BITS16):
2818       fragP->fr_opcode[0] = (fragP->fr_opcode[0] << 3);
2819       fragP->fr_opcode[0] |= 0xe2;
2820       if ((fragP->fr_opcode[0] & 0x0ff) == 0x0fa)
2821         {
2822           fixp = fix_new (fragP, fragP->fr_fix, 2,
2823                           fragP->fr_symbol, fragP->fr_offset,
2824                           1, BFD_RELOC_16_PCREL);
2825           fixp->fx_pcrel_adjust = 2;
2826         }
2827       else
2828         {
2829           fix_new (fragP, fragP->fr_fix, 2,
2830                    fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
2831         }
2832       fragP->fr_fix += 2;
2833       break;
2834
2835     case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE):
2836       if (disp < 0)
2837         fragP->fr_opcode[0] |= 0x10;
2838
2839       fragP->fr_opcode[1] = disp & 0x0FF;
2840       break;
2841
2842     case ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_WORD):
2843       /* Invert branch.  */
2844       fragP->fr_opcode[0] ^= 0x20;
2845       fragP->fr_opcode[1] = 3;  /* Branch offset.  */
2846       buffer_address[0] = M6812_JMP;
2847       fix_new (fragP, fragP->fr_fix + 1, 2,
2848                fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_16);
2849       fragP->fr_fix += 3;
2850       break;
2851
2852     default:
2853       break;
2854     }
2855 }
2856
2857 /* On an ELF system, we can't relax a weak symbol.  The weak symbol
2858    can be overridden at final link time by a non weak symbol.  We can
2859    relax externally visible symbol because there is no shared library
2860    and such symbol can't be overridden (unless they are weak).  */
2861 static int
2862 relaxable_symbol (symbol)
2863      symbolS *symbol;
2864 {
2865   return ! S_IS_WEAK (symbol);
2866 }
2867
2868 /* Force truly undefined symbols to their maximum size, and generally set up
2869    the frag list to be relaxed.  */
2870 int
2871 md_estimate_size_before_relax (fragP, segment)
2872      fragS *fragP;
2873      asection *segment;
2874 {
2875   if (RELAX_LENGTH (fragP->fr_subtype) == STATE_UNDF)
2876     {
2877       if (S_GET_SEGMENT (fragP->fr_symbol) != segment
2878           || !relaxable_symbol (fragP->fr_symbol))
2879         {
2880           /* Non-relaxable cases.  */
2881           int old_fr_fix;
2882           char *buffer_address;
2883
2884           old_fr_fix = fragP->fr_fix;
2885           buffer_address = fragP->fr_fix + fragP->fr_literal;
2886
2887           switch (RELAX_STATE (fragP->fr_subtype))
2888             {
2889             case STATE_PC_RELATIVE:
2890
2891               /* This relax is only for bsr and bra.  */
2892               assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
2893                       || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
2894                       || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
2895
2896               if (flag_fixed_branchs)
2897                 as_bad_where (fragP->fr_file, fragP->fr_line,
2898                               _("bra or bsr with undefined symbol."));
2899
2900               /* The symbol is undefined or in a separate section.
2901                  Turn bra into a jmp and bsr into a jsr.  The insn
2902                  becomes 3 bytes long (instead of 2).  A fixup is
2903                  necessary for the unresolved symbol address.  */
2904               fragP->fr_opcode[0] = convert_branch (fragP->fr_opcode[0]);
2905
2906               fix_new (fragP, fragP->fr_fix - 1, 2, fragP->fr_symbol,
2907                        fragP->fr_offset, 0, BFD_RELOC_16);
2908               fragP->fr_fix++;
2909               break;
2910
2911             case STATE_CONDITIONAL_BRANCH:
2912               assert (current_architecture & cpu6811);
2913
2914               fragP->fr_opcode[0] ^= 1; /* Reverse sense of branch.  */
2915               fragP->fr_opcode[1] = 3;  /* Skip next jmp insn (3 bytes).  */
2916
2917               /* Don't use fr_opcode[2] because this may be
2918                  in a different frag.  */
2919               buffer_address[0] = M6811_JMP;
2920
2921               fragP->fr_fix++;
2922               fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
2923                        fragP->fr_offset, 0, BFD_RELOC_16);
2924               fragP->fr_fix += 2;
2925               break;
2926
2927             case STATE_INDEXED_OFFSET:
2928               assert (current_architecture & cpu6812);
2929
2930               /* Switch the indexed operation to 16-bit mode.  */
2931               fragP->fr_opcode[0] = fragP->fr_opcode[0] << 3;
2932               fragP->fr_opcode[0] |= 0xe2;
2933               fragP->fr_fix++;
2934               fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
2935                        fragP->fr_offset, 0, BFD_RELOC_16);
2936               fragP->fr_fix++;
2937               break;
2938
2939             case STATE_XBCC_BRANCH:
2940               assert (current_architecture & cpu6812);
2941
2942               fragP->fr_opcode[0] ^= 0x20;      /* Reverse sense of branch.  */
2943               fragP->fr_opcode[1] = 3;  /* Skip next jmp insn (3 bytes).  */
2944
2945               /* Don't use fr_opcode[2] because this may be
2946                  in a different frag.  */
2947               buffer_address[0] = M6812_JMP;
2948
2949               fragP->fr_fix++;
2950               fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
2951                        fragP->fr_offset, 0, BFD_RELOC_16);
2952               fragP->fr_fix += 2;
2953               break;
2954
2955             case STATE_CONDITIONAL_BRANCH_6812:
2956               assert (current_architecture & cpu6812);
2957
2958               /* Translate into a lbcc branch.  */
2959               fragP->fr_opcode[1] = fragP->fr_opcode[0];
2960               fragP->fr_opcode[0] = M6811_OPCODE_PAGE2;
2961
2962               fix_new (fragP, fragP->fr_fix, 2, fragP->fr_symbol,
2963                        fragP->fr_offset, 1, BFD_RELOC_16_PCREL);
2964               fragP->fr_fix += 2;
2965               break;
2966
2967             default:
2968               as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
2969             }
2970           frag_wane (fragP);
2971
2972           /* Return the growth in the fixed part of the frag.  */
2973           return fragP->fr_fix - old_fr_fix;
2974         }
2975
2976       /* Relaxable cases.  */
2977       switch (RELAX_STATE (fragP->fr_subtype))
2978         {
2979         case STATE_PC_RELATIVE:
2980           /* This relax is only for bsr and bra.  */
2981           assert (IS_OPCODE (fragP->fr_opcode[0], M6811_BSR)
2982                   || IS_OPCODE (fragP->fr_opcode[0], M6811_BRA)
2983                   || IS_OPCODE (fragP->fr_opcode[0], M6812_BSR));
2984
2985           fragP->fr_subtype = ENCODE_RELAX (STATE_PC_RELATIVE, STATE_BYTE);
2986           break;
2987
2988         case STATE_CONDITIONAL_BRANCH:
2989           assert (current_architecture & cpu6811);
2990
2991           fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH,
2992                                             STATE_BYTE);
2993           break;
2994
2995         case STATE_INDEXED_OFFSET:
2996           assert (current_architecture & cpu6812);
2997
2998           fragP->fr_subtype = ENCODE_RELAX (STATE_INDEXED_OFFSET,
2999                                             STATE_BITS5);
3000           break;
3001
3002         case STATE_XBCC_BRANCH:
3003           assert (current_architecture & cpu6812);
3004
3005           fragP->fr_subtype = ENCODE_RELAX (STATE_XBCC_BRANCH, STATE_BYTE);
3006           break;
3007
3008         case STATE_CONDITIONAL_BRANCH_6812:
3009           assert (current_architecture & cpu6812);
3010
3011           fragP->fr_subtype = ENCODE_RELAX (STATE_CONDITIONAL_BRANCH_6812,
3012                                             STATE_BYTE);
3013           break;
3014         }
3015     }
3016
3017   if (fragP->fr_subtype >= sizeof (md_relax_table) / sizeof (md_relax_table[0]))
3018     as_fatal (_("Subtype %d is not recognized."), fragP->fr_subtype);
3019
3020   /* Return the size of the variable part of the frag.  */
3021   return md_relax_table[fragP->fr_subtype].rlx_length;
3022 }
3023
3024 /* See whether we need to force a relocation into the output file.  */
3025 int
3026 tc_m68hc11_force_relocation (fixP)
3027      fixS * fixP;
3028 {
3029   switch (fixP->fx_r_type)
3030     {
3031     case BFD_RELOC_VTABLE_INHERIT:
3032     case BFD_RELOC_VTABLE_ENTRY:
3033     case BFD_RELOC_M68HC11_RL_GROUP:
3034       return 1;
3035
3036     default:
3037       break;
3038     }
3039
3040   return S_FORCE_RELOC (fixP->fx_addsy);
3041 }
3042
3043 /* Here we decide which fixups can be adjusted to make them relative
3044    to the beginning of the section instead of the symbol.  Basically
3045    we need to make sure that the linker relaxation is done
3046    correctly, so in some cases we force the original symbol to be
3047    used.  */
3048 int
3049 tc_m68hc11_fix_adjustable (fixP)
3050      fixS *fixP;
3051 {
3052   switch (fixP->fx_r_type)
3053     {
3054       /* For the linker relaxation to work correctly, these relocs
3055          need to be on the symbol itself.  */
3056     case BFD_RELOC_16:
3057     case BFD_RELOC_LO16:
3058     case BFD_RELOC_M68HC11_RL_JUMP:
3059     case BFD_RELOC_M68HC11_RL_GROUP:
3060     case BFD_RELOC_VTABLE_INHERIT:
3061     case BFD_RELOC_VTABLE_ENTRY:
3062       return 0;
3063
3064     case BFD_RELOC_32:
3065     default:
3066       return 1;
3067     }
3068 }
3069
3070 void
3071 md_apply_fix3 (fixP, valP, seg)
3072      fixS *fixP;
3073      valueT *valP;
3074      segT seg ATTRIBUTE_UNUSED;
3075 {
3076   char *where;
3077   long value = * valP;
3078   int op_type;
3079
3080   if (fixP->fx_addsy == (symbolS *) NULL)
3081     fixP->fx_done = 1;
3082
3083   /* We don't actually support subtracting a symbol.  */
3084   if (fixP->fx_subsy != (symbolS *) NULL)
3085     as_bad_where (fixP->fx_file, fixP->fx_line, _("Expression too complex."));
3086
3087   op_type = fixP->fx_r_type;
3088
3089   /* Patch the instruction with the resolved operand.  Elf relocation
3090      info will also be generated to take care of linker/loader fixups.
3091      The 68HC11 addresses only 64Kb, we are only concerned by 8 and 16-bit
3092      relocs.  BFD_RELOC_8 is basically used for .page0 access (the linker
3093      will warn for overflows).  BFD_RELOC_8_PCREL should not be generated
3094      because it's either resolved or turned out into non-relative insns (see
3095      relax table, bcc, bra, bsr transformations)
3096
3097      The BFD_RELOC_32 is necessary for the support of --gstabs.  */
3098   where = fixP->fx_frag->fr_literal + fixP->fx_where;
3099
3100   switch (fixP->fx_r_type)
3101     {
3102     case BFD_RELOC_32:
3103       bfd_putb32 ((bfd_vma) value, (unsigned char *) where);
3104       break;
3105
3106     case BFD_RELOC_24:
3107     case BFD_RELOC_M68HC11_24:
3108       bfd_putb16 ((bfd_vma) (value & 0x0ffff), (unsigned char *) where);
3109       ((bfd_byte*) where)[2] = ((value >> 16) & 0x0ff);
3110       break;
3111
3112     case BFD_RELOC_16:
3113     case BFD_RELOC_16_PCREL:
3114     case BFD_RELOC_M68HC11_LO16:
3115       bfd_putb16 ((bfd_vma) value, (unsigned char *) where);
3116       if (value < -65537 || value > 65535)
3117         as_bad_where (fixP->fx_file, fixP->fx_line,
3118                       _("Value out of 16-bit range."));
3119       break;
3120
3121     case BFD_RELOC_M68HC11_HI8:
3122       value = value >> 8;
3123       /* Fall through.  */
3124
3125     case BFD_RELOC_M68HC11_LO8:
3126     case BFD_RELOC_8:
3127     case BFD_RELOC_M68HC11_PAGE:
3128 #if 0
3129       bfd_putb8 ((bfd_vma) value, (unsigned char *) where);
3130 #endif
3131       ((bfd_byte *) where)[0] = (bfd_byte) value;
3132       break;
3133
3134     case BFD_RELOC_8_PCREL:
3135 #if 0
3136       bfd_putb8 ((bfd_vma) value, (unsigned char *) where);
3137 #endif
3138       ((bfd_byte *) where)[0] = (bfd_byte) value;
3139
3140       if (value < -128 || value > 127)
3141         as_bad_where (fixP->fx_file, fixP->fx_line,
3142                       _("Value %ld too large for 8-bit PC-relative branch."),
3143                       value);
3144       break;
3145
3146     case BFD_RELOC_M68HC11_3B:
3147       if (value <= 0 || value > 8)
3148         as_bad_where (fixP->fx_file, fixP->fx_line,
3149                       _("Auto increment/decrement offset '%ld' is out of range."),
3150                       value);
3151       if (where[0] & 0x8)
3152         value = 8 - value;
3153       else
3154         value--;
3155
3156       where[0] = where[0] | (value & 0x07);
3157       break;
3158
3159     case BFD_RELOC_M68HC11_RL_JUMP:
3160     case BFD_RELOC_M68HC11_RL_GROUP:
3161     case BFD_RELOC_VTABLE_INHERIT:
3162     case BFD_RELOC_VTABLE_ENTRY:
3163       fixP->fx_done = 0;
3164       return;
3165
3166     default:
3167       as_fatal (_("Line %d: unknown relocation type: 0x%x."),
3168                 fixP->fx_line, fixP->fx_r_type);
3169     }
3170 }
3171
3172 /* Set the ELF specific flags.  */
3173 void
3174 m68hc11_elf_final_processing ()
3175 {
3176   elf_elfheader (stdoutput)->e_flags &= ~EF_M68HC11_ABI;
3177   elf_elfheader (stdoutput)->e_flags |= elf_flags;
3178 }