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