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