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