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