* config/tc-h8300.c (get_specific): No PCREL8 encoding for bsr/bc
[binutils-gdb.git] / gas / config / tc-h8300.c
1 /* tc-h8300.c -- Assemble code for the Renesas H8/300
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
3 2001, 2002, 2003 Free Software Foundation, Inc.
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
23
24 #include <stdio.h>
25 #include "as.h"
26 #include "subsegs.h"
27 #include "bfd.h"
28
29 #ifdef BFD_ASSEMBLER
30 #include "dwarf2dbg.h"
31 #endif
32
33 #define DEFINE_TABLE
34 #define h8_opcodes ops
35 #include "opcode/h8300.h"
36 #include "safe-ctype.h"
37
38 #ifdef OBJ_ELF
39 #include "elf/h8.h"
40 #endif
41
42 const char comment_chars[] = ";";
43 const char line_comment_chars[] = "#";
44 const char line_separator_chars[] = "";
45
46 void cons PARAMS ((int));
47 void sbranch PARAMS ((int));
48 void h8300hmode PARAMS ((int));
49 void h8300smode PARAMS ((int));
50 void h8300hnmode PARAMS ((int));
51 void h8300snmode PARAMS ((int));
52 void h8300sxmode PARAMS ((int));
53 void h8300sxnmode PARAMS ((int));
54 static void pint PARAMS ((int));
55
56 int Hmode;
57 int Smode;
58 int Nmode;
59 int SXmode;
60
61 #define PSIZE (Hmode ? L_32 : L_16)
62
63 int bsize = L_8; /* Default branch displacement. */
64
65 struct h8_instruction
66 {
67 int length;
68 int noperands;
69 int idx;
70 int size;
71 const struct h8_opcode *opcode;
72 };
73
74 struct h8_instruction *h8_instructions;
75
76 void
77 h8300hmode (arg)
78 int arg ATTRIBUTE_UNUSED;
79 {
80 Hmode = 1;
81 Smode = 0;
82 #ifdef BFD_ASSEMBLER
83 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300h))
84 as_warn (_("could not set architecture and machine"));
85 #endif
86 }
87
88 void
89 h8300smode (arg)
90 int arg ATTRIBUTE_UNUSED;
91 {
92 Smode = 1;
93 Hmode = 1;
94 #ifdef BFD_ASSEMBLER
95 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300s))
96 as_warn (_("could not set architecture and machine"));
97 #endif
98 }
99
100 void
101 h8300hnmode (arg)
102 int arg ATTRIBUTE_UNUSED;
103 {
104 Hmode = 1;
105 Smode = 0;
106 Nmode = 1;
107 #ifdef BFD_ASSEMBLER
108 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300hn))
109 as_warn (_("could not set architecture and machine"));
110 #endif
111 }
112
113 void
114 h8300snmode (arg)
115 int arg ATTRIBUTE_UNUSED;
116 {
117 Smode = 1;
118 Hmode = 1;
119 Nmode = 1;
120 #ifdef BFD_ASSEMBLER
121 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sn))
122 as_warn (_("could not set architecture and machine"));
123 #endif
124 }
125
126 void
127 h8300sxmode (arg)
128 int arg ATTRIBUTE_UNUSED;
129 {
130 Smode = 1;
131 Hmode = 1;
132 SXmode = 1;
133 #ifdef BFD_ASSEMBLER
134 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sx))
135 as_warn (_("could not set architecture and machine"));
136 #endif
137 }
138
139 void
140 h8300sxnmode (arg)
141 int arg ATTRIBUTE_UNUSED;
142 {
143 Smode = 1;
144 Hmode = 1;
145 SXmode = 1;
146 Nmode = 1;
147 #ifdef BFD_ASSEMBLER
148 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300sxn))
149 as_warn (_("could not set architecture and machine"));
150 #endif
151 }
152
153 void
154 sbranch (size)
155 int size;
156 {
157 bsize = size;
158 }
159
160 static void
161 pint (arg)
162 int arg ATTRIBUTE_UNUSED;
163 {
164 cons (Hmode ? 4 : 2);
165 }
166
167 /* This table describes all the machine specific pseudo-ops the assembler
168 has to support. The fields are:
169 pseudo-op name without dot
170 function to call to execute this pseudo-op
171 Integer arg to pass to the function. */
172
173 const pseudo_typeS md_pseudo_table[] =
174 {
175 {"h8300h", h8300hmode, 0},
176 {"h8300hn", h8300hnmode, 0},
177 {"h8300s", h8300smode, 0},
178 {"h8300sn", h8300snmode, 0},
179 {"h8300sx", h8300sxmode, 0},
180 {"h8300sxn", h8300sxnmode, 0},
181 {"sbranch", sbranch, L_8},
182 {"lbranch", sbranch, L_16},
183
184 {"int", pint, 0},
185 {"data.b", cons, 1},
186 {"data.w", cons, 2},
187 {"data.l", cons, 4},
188 {"form", listing_psize, 0},
189 {"heading", listing_title, 0},
190 {"import", s_ignore, 0},
191 {"page", listing_eject, 0},
192 {"program", s_ignore, 0},
193 {0, 0, 0}
194 };
195
196 const int md_reloc_size;
197
198 const char EXP_CHARS[] = "eE";
199
200 /* Chars that mean this number is a floating point constant
201 As in 0f12.456
202 or 0d1.2345e12. */
203 const char FLT_CHARS[] = "rRsSfFdDxXpP";
204
205 static struct hash_control *opcode_hash_control; /* Opcode mnemonics. */
206
207 /* This function is called once, at assembler startup time. This
208 should set up all the tables, etc. that the MD part of the assembler
209 needs. */
210
211 void
212 md_begin ()
213 {
214 unsigned int nopcodes;
215 struct h8_opcode *p, *p1;
216 struct h8_instruction *pi;
217 char prev_buffer[100];
218 int idx = 0;
219
220 #ifdef BFD_ASSEMBLER
221 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300))
222 as_warn (_("could not set architecture and machine"));
223 #endif
224
225 opcode_hash_control = hash_new ();
226 prev_buffer[0] = 0;
227
228 nopcodes = sizeof (h8_opcodes) / sizeof (struct h8_opcode);
229
230 h8_instructions = (struct h8_instruction *)
231 xmalloc (nopcodes * sizeof (struct h8_instruction));
232
233 pi = h8_instructions;
234 p1 = h8_opcodes;
235 /* We do a minimum amount of sorting on the opcode table; this is to
236 make it easy to describe the mova instructions without unnecessary
237 code duplication.
238 Sorting only takes place inside blocks of instructions of the form
239 X/Y, so for example mova/b, mova/w and mova/l can be intermixed. */
240 while (p1)
241 {
242 struct h8_opcode *first_skipped = 0;
243 int len, cmplen = 0;
244 char *src = p1->name;
245 char *dst, *buffer;
246
247 if (p1->name == 0)
248 break;
249 /* Strip off any . part when inserting the opcode and only enter
250 unique codes into the hash table. */
251 dst = buffer = malloc (strlen (src) + 1);
252 while (*src)
253 {
254 if (*src == '.')
255 {
256 src++;
257 break;
258 }
259 if (*src == '/')
260 cmplen = src - p1->name + 1;
261 *dst++ = *src++;
262 }
263 *dst = 0;
264 len = dst - buffer;
265 if (cmplen == 0)
266 cmplen = len;
267 hash_insert (opcode_hash_control, buffer, (char *) pi);
268 strcpy (prev_buffer, buffer);
269 idx++;
270
271 for (p = p1; p->name; p++)
272 {
273 /* A negative TIME is used to indicate that we've added this opcode
274 already. */
275 if (p->time == -1)
276 continue;
277 if (strncmp (p->name, buffer, cmplen) != 0
278 || (p->name[cmplen] != '\0' && p->name[cmplen] != '.'
279 && p->name[cmplen - 1] != '/'))
280 {
281 if (first_skipped == 0)
282 first_skipped = p;
283 break;
284 }
285 if (strncmp (p->name, buffer, len) != 0)
286 {
287 if (first_skipped == 0)
288 first_skipped = p;
289 continue;
290 }
291
292 p->time = -1;
293 pi->size = p->name[len] == '.' ? p->name[len + 1] : 0;
294 pi->idx = idx;
295
296 /* Find the number of operands. */
297 pi->noperands = 0;
298 while (pi->noperands < 3 && p->args.nib[pi->noperands] != (op_type) E)
299 pi->noperands++;
300
301 /* Find the length of the opcode in bytes. */
302 pi->length = 0;
303 while (p->data.nib[pi->length * 2] != (op_type) E)
304 pi->length++;
305
306 pi->opcode = p;
307 pi++;
308 }
309 p1 = first_skipped;
310 }
311
312 /* Add entry for the NULL vector terminator. */
313 pi->length = 0;
314 pi->noperands = 0;
315 pi->idx = 0;
316 pi->size = 0;
317 pi->opcode = 0;
318
319 linkrelax = 1;
320 }
321
322 struct h8_exp
323 {
324 char *e_beg;
325 char *e_end;
326 expressionS e_exp;
327 };
328
329 struct h8_op
330 {
331 op_type mode;
332 unsigned reg;
333 expressionS exp;
334 };
335
336 static void clever_message PARAMS ((const struct h8_instruction *, struct h8_op *));
337 static void fix_operand_size PARAMS ((struct h8_op *, int));
338 static void build_bytes PARAMS ((const struct h8_instruction *, struct h8_op *));
339 static void do_a_fix_imm PARAMS ((int, int, struct h8_op *, int));
340 static void check_operand PARAMS ((struct h8_op *, unsigned int, char *));
341 static const struct h8_instruction * get_specific PARAMS ((const struct h8_instruction *, struct h8_op *, int));
342 static char *get_operands PARAMS ((unsigned, char *, struct h8_op *));
343 static void get_operand PARAMS ((char **, struct h8_op *, int));
344 static int parse_reg PARAMS ((char *, op_type *, unsigned *, int));
345 static char *skip_colonthing PARAMS ((char *, int *));
346 static char *parse_exp PARAMS ((char *, struct h8_op *));
347
348 static int constant_fits_width_p PARAMS ((struct h8_op *, unsigned int));
349 static int constant_fits_size_p PARAMS ((struct h8_op *, int, int));
350
351 /*
352 parse operands
353 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
354 r0l,r0h,..r7l,r7h
355 @WREG
356 @WREG+
357 @-WREG
358 #const
359 ccr
360 */
361
362 /* Try to parse a reg name. Return the number of chars consumed. */
363
364 static int
365 parse_reg (src, mode, reg, direction)
366 char *src;
367 op_type *mode;
368 unsigned int *reg;
369 int direction;
370 {
371 char *end;
372 int len;
373
374 /* Cribbed from get_symbol_end. */
375 if (!is_name_beginner (*src) || *src == '\001')
376 return 0;
377 end = src + 1;
378 while ((is_part_of_name (*end) && *end != '.') || *end == '\001')
379 end++;
380 len = end - src;
381
382 if (len == 2 && TOLOWER (src[0]) == 's' && TOLOWER (src[1]) == 'p')
383 {
384 *mode = PSIZE | REG | direction;
385 *reg = 7;
386 return len;
387 }
388 if (len == 3 &&
389 TOLOWER (src[0]) == 'c' &&
390 TOLOWER (src[1]) == 'c' &&
391 TOLOWER (src[2]) == 'r')
392 {
393 *mode = CCR;
394 *reg = 0;
395 return len;
396 }
397 if (len == 3 &&
398 TOLOWER (src[0]) == 'e' &&
399 TOLOWER (src[1]) == 'x' &&
400 TOLOWER (src[2]) == 'r')
401 {
402 *mode = EXR;
403 *reg = 1;
404 return len;
405 }
406 if (len == 3 &&
407 TOLOWER (src[0]) == 'v' &&
408 TOLOWER (src[1]) == 'b' &&
409 TOLOWER (src[2]) == 'r')
410 {
411 *mode = VBR;
412 *reg = 6;
413 return len;
414 }
415 if (len == 3 &&
416 TOLOWER (src[0]) == 's' &&
417 TOLOWER (src[1]) == 'b' &&
418 TOLOWER (src[2]) == 'r')
419 {
420 *mode = SBR;
421 *reg = 7;
422 return len;
423 }
424 if (len == 2 && TOLOWER (src[0]) == 'f' && TOLOWER (src[1]) == 'p')
425 {
426 *mode = PSIZE | REG | direction;
427 *reg = 6;
428 return len;
429 }
430 if (len == 3 && TOLOWER (src[0]) == 'e' && TOLOWER (src[1]) == 'r' &&
431 src[2] >= '0' && src[2] <= '7')
432 {
433 *mode = L_32 | REG | direction;
434 *reg = src[2] - '0';
435 if (!Hmode)
436 as_warn (_("Reg not valid for H8/300"));
437 return len;
438 }
439 if (len == 2 && TOLOWER (src[0]) == 'e' && src[1] >= '0' && src[1] <= '7')
440 {
441 *mode = L_16 | REG | direction;
442 *reg = src[1] - '0' + 8;
443 if (!Hmode)
444 as_warn (_("Reg not valid for H8/300"));
445 return len;
446 }
447
448 if (TOLOWER (src[0]) == 'r')
449 {
450 if (src[1] >= '0' && src[1] <= '7')
451 {
452 if (len == 3 && TOLOWER (src[2]) == 'l')
453 {
454 *mode = L_8 | REG | direction;
455 *reg = (src[1] - '0') + 8;
456 return len;
457 }
458 if (len == 3 && TOLOWER (src[2]) == 'h')
459 {
460 *mode = L_8 | REG | direction;
461 *reg = (src[1] - '0');
462 return len;
463 }
464 if (len == 2)
465 {
466 *mode = L_16 | REG | direction;
467 *reg = (src[1] - '0');
468 return len;
469 }
470 }
471 }
472
473 return 0;
474 }
475
476
477 /* Parse an immediate or address-related constant and store it in OP.
478 If the user also specifies the operand's size, store that size
479 in OP->MODE, otherwise leave it for later code to decide. */
480
481 static char *
482 parse_exp (src, op)
483 char *src;
484 struct h8_op *op;
485 {
486 char *save;
487
488 save = input_line_pointer;
489 input_line_pointer = src;
490 expression (&op->exp);
491 if (op->exp.X_op == O_absent)
492 as_bad (_("missing operand"));
493 src = input_line_pointer;
494 input_line_pointer = save;
495
496 return skip_colonthing (src, &op->mode);
497 }
498
499
500 /* If SRC starts with an explicit operand size, skip it and store the size
501 in *MODE. Leave *MODE unchanged otherwise. */
502
503 static char *
504 skip_colonthing (src, mode)
505 char *src;
506 int *mode;
507 {
508 if (*src == ':')
509 {
510 src++;
511 *mode &= ~SIZE;
512 if (src[0] == '8' && !ISDIGIT (src[1]))
513 *mode |= L_8;
514 else if (src[0] == '2' && !ISDIGIT (src[1]))
515 *mode |= L_2;
516 else if (src[0] == '3' && !ISDIGIT (src[1]))
517 *mode |= L_3;
518 else if (src[0] == '4' && !ISDIGIT (src[1]))
519 *mode |= L_4;
520 else if (src[0] == '5' && !ISDIGIT (src[1]))
521 *mode |= L_5;
522 else if (src[0] == '2' && src[1] == '4' && !ISDIGIT (src[2]))
523 *mode |= L_24;
524 else if (src[0] == '3' && src[1] == '2' && !ISDIGIT (src[2]))
525 *mode |= L_32;
526 else if (src[0] == '1' && src[1] == '6' && !ISDIGIT (src[2]))
527 *mode |= L_16;
528 else
529 as_bad (_("invalid operand size requested"));
530
531 while (ISDIGIT (*src))
532 src++;
533 }
534 return src;
535 }
536
537 /* The many forms of operand:
538
539 Rn Register direct
540 @Rn Register indirect
541 @(exp[:16], Rn) Register indirect with displacement
542 @Rn+
543 @-Rn
544 @aa:8 absolute 8 bit
545 @aa:16 absolute 16 bit
546 @aa absolute 16 bit
547
548 #xx[:size] immediate data
549 @(exp:[8], pc) pc rel
550 @@aa[:8] memory indirect. */
551
552 static int
553 constant_fits_width_p (operand, width)
554 struct h8_op *operand;
555 unsigned int width;
556 {
557 return ((operand->exp.X_add_number & ~width) == 0
558 || (operand->exp.X_add_number | width) == (unsigned)(~0));
559 }
560
561 static int
562 constant_fits_size_p (operand, size, no_symbols)
563 struct h8_op *operand;
564 int size, no_symbols;
565 {
566 offsetT num = operand->exp.X_add_number;
567 if (no_symbols
568 && (operand->exp.X_add_symbol != 0 || operand->exp.X_op_symbol != 0))
569 return 0;
570 switch (size)
571 {
572 case L_2:
573 return (num & ~3) == 0;
574 case L_3:
575 return (num & ~7) == 0;
576 case L_3NZ:
577 return num >= 1 && num < 8;
578 case L_4:
579 return (num & ~15) == 0;
580 case L_5:
581 return num >= 1 && num < 32;
582 case L_8:
583 return (num & ~0xFF) == 0 || ((unsigned)num | 0x7F) == ~0u;
584 case L_8U:
585 return (num & ~0xFF) == 0;
586 case L_16:
587 return (num & ~0xFFFF) == 0 || ((unsigned)num | 0x7FFF) == ~0u;
588 case L_16U:
589 return (num & ~0xFFFF) == 0;
590 case L_32:
591 return 1;
592 default:
593 abort ();
594 }
595 }
596
597 static void
598 get_operand (ptr, op, direction)
599 char **ptr;
600 struct h8_op *op;
601 int direction;
602 {
603 char *src = *ptr;
604 op_type mode;
605 unsigned int num;
606 unsigned int len;
607
608 op->mode = 0;
609
610 /* Check for '(' and ')' for instructions ldm and stm. */
611 if (src[0] == '(' && src[8] == ')')
612 ++ src;
613
614 /* Gross. Gross. ldm and stm have a format not easily handled
615 by get_operand. We deal with it explicitly here. */
616 if (TOLOWER (src[0]) == 'e' && TOLOWER (src[1]) == 'r' &&
617 ISDIGIT (src[2]) && src[3] == '-' &&
618 TOLOWER (src[4]) == 'e' && TOLOWER (src[5]) == 'r' && ISDIGIT (src[6]))
619 {
620 int low, high;
621
622 low = src[2] - '0';
623 high = src[6] - '0';
624
625 if (high == low)
626 as_bad (_("Invalid register list for ldm/stm\n"));
627
628 if (high < low)
629 as_bad (_("Invalid register list for ldm/stm\n"));
630
631 if (high - low > 3)
632 as_bad (_("Invalid register list for ldm/stm)\n"));
633
634 /* Even sicker. We encode two registers into op->reg. One
635 for the low register to save, the other for the high
636 register to save; we also set the high bit in op->reg
637 so we know this is "very special". */
638 op->reg = 0x80000000 | (high << 8) | low;
639 op->mode = REG;
640 if (src[7] == ')')
641 *ptr = src + 8;
642 else
643 *ptr = src + 7;
644 return;
645 }
646
647 len = parse_reg (src, &op->mode, &op->reg, direction);
648 if (len)
649 {
650 src += len;
651 if (*src == '.')
652 {
653 int size = op->mode & SIZE;
654 switch (src[1])
655 {
656 case 'l': case 'L':
657 if (size != L_32)
658 as_warn (_("mismatch between register and suffix"));
659 op->mode = (op->mode & ~MODE) | LOWREG;
660 break;
661 case 'w': case 'W':
662 if (size != L_32 && size != L_16)
663 as_warn (_("mismatch between register and suffix"));
664 op->mode = (op->mode & ~MODE) | LOWREG;
665 op->mode = (op->mode & ~SIZE) | L_16;
666 break;
667 case 'b': case 'B':
668 op->mode = (op->mode & ~MODE) | LOWREG;
669 if (size != L_32 && size != L_8)
670 as_warn (_("mismatch between register and suffix"));
671 op->mode = (op->mode & ~MODE) | LOWREG;
672 op->mode = (op->mode & ~SIZE) | L_8;
673 break;
674 default:
675 as_warn ("invalid suffix after register.");
676 break;
677 }
678 src += 2;
679 }
680 *ptr = src;
681 return;
682 }
683
684 if (*src == '@')
685 {
686 src++;
687 if (*src == '@')
688 {
689 *ptr = parse_exp (src + 1, op);
690 if (op->exp.X_add_number >= 0x100)
691 {
692 int divisor;
693
694 op->mode = VECIND;
695 /* FIXME : 2? or 4? */
696 if (op->exp.X_add_number >= 0x400)
697 as_bad (_("address too high for vector table jmp/jsr"));
698 else if (op->exp.X_add_number >= 0x200)
699 divisor = 4;
700 else
701 divisor = 2;
702
703 op->exp.X_add_number = op->exp.X_add_number / divisor - 0x80;
704 }
705 else
706 op->mode = MEMIND;
707 return;
708 }
709
710 if (*src == '-' || *src == '+')
711 {
712 len = parse_reg (src + 1, &mode, &num, direction);
713 if (len == 0)
714 {
715 /* Oops, not a reg after all, must be ordinary exp. */
716 op->mode = ABS | direction;
717 *ptr = parse_exp (src, op);
718 return;
719 }
720
721 if ((mode & SIZE) != PSIZE)
722 as_bad (_("Wrong size pointer register for architecture."));
723
724 op->mode = src[0] == '-' ? RDPREDEC : RDPREINC;
725 op->reg = num;
726 *ptr = src + 1 + len;
727 return;
728 }
729 if (*src == '(')
730 {
731 src++;
732
733 /* See if this is @(ERn.x, PC). */
734 len = parse_reg (src, &mode, &op->reg, direction);
735 if (len != 0 && (mode & MODE) == REG && src[len] == '.')
736 {
737 switch (TOLOWER (src[len + 1]))
738 {
739 case 'b':
740 mode = PCIDXB | direction;
741 break;
742 case 'w':
743 mode = PCIDXW | direction;
744 break;
745 case 'l':
746 mode = PCIDXL | direction;
747 break;
748 default:
749 mode = 0;
750 break;
751 }
752 if (mode
753 && src[len + 2] == ','
754 && TOLOWER (src[len + 3]) != 'p'
755 && TOLOWER (src[len + 4]) != 'c'
756 && src[len + 5] != ')')
757 {
758 *ptr = src + len + 6;
759 op->mode |= mode;
760 return;
761 }
762 /* Fall through into disp case - the grammar is somewhat
763 ambiguous, so we should try whether it's a DISP operand
764 after all ("ER3.L" might be a poorly named label...). */
765 }
766
767 /* Disp. */
768
769 /* Start off assuming a 16 bit offset. */
770
771 src = parse_exp (src, op);
772 if (*src == ')')
773 {
774 op->mode |= ABS | direction;
775 *ptr = src + 1;
776 return;
777 }
778
779 if (*src != ',')
780 {
781 as_bad (_("expected @(exp, reg16)"));
782 return;
783 }
784 src++;
785
786 len = parse_reg (src, &mode, &op->reg, direction);
787 if (len == 0 || (mode & MODE) != REG)
788 {
789 as_bad (_("expected @(exp, reg16)"));
790 return;
791 }
792 src += len;
793 if (src[0] == '.')
794 {
795 switch (TOLOWER (src[1]))
796 {
797 case 'b':
798 op->mode |= INDEXB | direction;
799 break;
800 case 'w':
801 op->mode |= INDEXW | direction;
802 break;
803 case 'l':
804 op->mode |= INDEXL | direction;
805 break;
806 default:
807 as_bad (_("expected .L, .W or .B for register in indexed addressing mode"));
808 }
809 src += 2;
810 op->reg &= 7;
811 }
812 else
813 op->mode |= DISP | direction;
814 src = skip_colonthing (src, &op->mode);
815
816 if (*src != ')' && '(')
817 {
818 as_bad (_("expected @(exp, reg16)"));
819 return;
820 }
821 *ptr = src + 1;
822 return;
823 }
824 len = parse_reg (src, &mode, &num, direction);
825
826 if (len)
827 {
828 src += len;
829 if (*src == '+' || *src == '-')
830 {
831 if ((mode & SIZE) != PSIZE)
832 as_bad (_("Wrong size pointer register for architecture."));
833 op->mode = *src == '+' ? RSPOSTINC : RSPOSTDEC;
834 op->reg = num;
835 src++;
836 *ptr = src;
837 return;
838 }
839 if ((mode & SIZE) != PSIZE)
840 as_bad (_("Wrong size pointer register for architecture."));
841
842 op->mode = direction | IND | PSIZE;
843 op->reg = num;
844 *ptr = src;
845
846 return;
847 }
848 else
849 {
850 /* must be a symbol */
851
852 op->mode = ABS | direction;
853 *ptr = parse_exp (src, op);
854 return;
855 }
856 }
857
858 if (*src == '#')
859 {
860 op->mode = IMM;
861 *ptr = parse_exp (src + 1, op);
862 return;
863 }
864 else if (strncmp (src, "mach", 4) == 0 ||
865 strncmp (src, "macl", 4) == 0 ||
866 strncmp (src, "MACH", 4) == 0 ||
867 strncmp (src, "MACL", 4) == 0)
868 {
869 op->reg = TOLOWER (src[3]) == 'l';
870 op->mode = MACREG;
871 *ptr = src + 4;
872 return;
873 }
874 else
875 {
876 op->mode = PCREL;
877 *ptr = parse_exp (src, op);
878 }
879 }
880
881 static char *
882 get_operands (noperands, op_end, operand)
883 unsigned int noperands;
884 char *op_end;
885 struct h8_op *operand;
886 {
887 char *ptr = op_end;
888
889 switch (noperands)
890 {
891 case 0:
892 break;
893
894 case 1:
895 ptr++;
896 get_operand (&ptr, operand + 0, SRC);
897 if (*ptr == ',')
898 {
899 ptr++;
900 get_operand (&ptr, operand + 1, DST);
901 }
902 break;
903
904 case 2:
905 ptr++;
906 get_operand (&ptr, operand + 0, SRC);
907 if (*ptr == ',')
908 ptr++;
909 get_operand (&ptr, operand + 1, DST);
910 break;
911
912 case 3:
913 ptr++;
914 get_operand (&ptr, operand + 0, SRC);
915 if (*ptr == ',')
916 ptr++;
917 get_operand (&ptr, operand + 1, DST);
918 if (*ptr == ',')
919 ptr++;
920 get_operand (&ptr, operand + 2, OP3);
921 break;
922
923 default:
924 abort ();
925 }
926
927 return ptr;
928 }
929
930 /* MOVA has special requirements. Rather than adding twice the amount of
931 addressing modes, we simply special case it a bit. */
932 static void
933 get_mova_operands (char *op_end, struct h8_op *operand)
934 {
935 char *ptr = op_end;
936
937 if (ptr[1] != '@' || ptr[2] != '(')
938 goto error;
939 ptr += 3;
940 operand[0].mode = 0;
941 ptr = parse_exp (ptr, &operand[0]);
942
943 if (*ptr !=',')
944 goto error;
945 ptr++;
946 get_operand (&ptr, operand + 1, DST);
947
948 if (*ptr =='.')
949 {
950 ptr++;
951 switch (*ptr++)
952 {
953 case 'b': case 'B':
954 operand[0].mode = (operand[0].mode & ~MODE) | INDEXB;
955 break;
956 case 'w': case 'W':
957 operand[0].mode = (operand[0].mode & ~MODE) | INDEXW;
958 break;
959 case 'l': case 'L':
960 operand[0].mode = (operand[0].mode & ~MODE) | INDEXL;
961 break;
962 default:
963 goto error;
964 }
965 }
966 else if ((operand[1].mode & MODE) == LOWREG)
967 {
968 switch (operand[1].mode & SIZE)
969 {
970 case L_8:
971 operand[0].mode = (operand[0].mode & ~MODE) | INDEXB;
972 break;
973 case L_16:
974 operand[0].mode = (operand[0].mode & ~MODE) | INDEXW;
975 break;
976 case L_32:
977 operand[0].mode = (operand[0].mode & ~MODE) | INDEXL;
978 break;
979 default:
980 goto error;
981 }
982 }
983 else
984 goto error;
985
986 if (*ptr++ != ')' || *ptr++ != ',')
987 goto error;
988 get_operand (&ptr, operand + 2, OP3);
989 /* See if we can use the short form of MOVA. */
990 if (((operand[1].mode & MODE) == REG || (operand[1].mode & MODE) == LOWREG)
991 && (operand[2].mode & MODE) == REG
992 && (operand[1].reg & 7) == (operand[2].reg & 7))
993 {
994 operand[1].mode = operand[2].mode = 0;
995 operand[0].reg = operand[2].reg & 7;
996 }
997 return;
998
999 error:
1000 as_bad (_("expected valid addressing mode for mova: \"@(disp, ea.sz),ERn\""));
1001 return;
1002 }
1003
1004 static void
1005 get_rtsl_operands (char *ptr, struct h8_op *operand)
1006 {
1007 int mode, num, num2, len, type = 0;
1008
1009 ptr++;
1010 if (*ptr == '(')
1011 {
1012 ptr++;
1013 type = 1;
1014 }
1015 len = parse_reg (ptr, &mode, &num, SRC);
1016 if (len == 0 || (mode & MODE) != REG)
1017 {
1018 as_bad (_("expected register"));
1019 return;
1020 }
1021 ptr += len;
1022 if (*ptr == '-')
1023 {
1024 len = parse_reg (++ptr, &mode, &num2, SRC);
1025 if (len == 0 || (mode & MODE) != REG)
1026 {
1027 as_bad (_("expected register"));
1028 return;
1029 }
1030 ptr += len;
1031 /* CONST_xxx are used as placeholders in the opcode table. */
1032 num = num2 - num;
1033 if (num < 0 || num > 3)
1034 {
1035 as_bad (_("invalid register list"));
1036 return;
1037 }
1038 }
1039 else
1040 num2 = num, num = 0;
1041 if (type == 1 && *ptr++ != ')')
1042 {
1043 as_bad (_("expected closing paren"));
1044 return;
1045 }
1046 operand[0].mode = RS32;
1047 operand[1].mode = RD32;
1048 operand[0].reg = num;
1049 operand[1].reg = num2;
1050 }
1051
1052 /* Passed a pointer to a list of opcodes which use different
1053 addressing modes, return the opcode which matches the opcodes
1054 provided. */
1055
1056 static const struct h8_instruction *
1057 get_specific (instruction, operands, size)
1058 const struct h8_instruction *instruction;
1059 struct h8_op *operands;
1060 int size;
1061 {
1062 const struct h8_instruction *this_try = instruction;
1063 const struct h8_instruction *found_other = 0, *found_mismatched = 0;
1064 int found = 0;
1065 int this_index = instruction->idx;
1066 int noperands = 0;
1067
1068 /* There's only one ldm/stm and it's easier to just
1069 get out quick for them. */
1070 if (OP_KIND (instruction->opcode->how) == O_LDM
1071 || OP_KIND (instruction->opcode->how) == O_STM)
1072 return this_try;
1073
1074 while (noperands < 3 && operands[noperands].mode != 0)
1075 noperands++;
1076
1077 while (this_index == instruction->idx && !found)
1078 {
1079 int this_size;
1080
1081 found = 1;
1082 this_try = instruction++;
1083 this_size = this_try->opcode->how & SN;
1084
1085 if (this_try->noperands != noperands)
1086 found = 0;
1087 else if (this_try->noperands > 0)
1088 {
1089 int i;
1090
1091 for (i = 0; i < this_try->noperands && found; i++)
1092 {
1093 op_type op = this_try->opcode->args.nib[i];
1094 int op_mode = op & MODE;
1095 int op_size = op & SIZE;
1096 int x = operands[i].mode;
1097 int x_mode = x & MODE;
1098 int x_size = x & SIZE;
1099
1100 if (op_mode == LOWREG && (x_mode == REG || x_mode == LOWREG))
1101 {
1102 if ((x_size == L_8 && (operands[i].reg & 8) == 0)
1103 || (x_size == L_16 && (operands[i].reg & 8) == 8))
1104 as_warn (_("can't use high part of register in operand %d"), i);
1105
1106 if (x_size != op_size)
1107 found = 0;
1108 }
1109 else if (op_mode == REG)
1110 {
1111 if (x_mode == LOWREG)
1112 x_mode = REG;
1113 if (x_mode != REG)
1114 found = 0;
1115
1116 if (x_size == L_P)
1117 x_size = (Hmode ? L_32 : L_16);
1118 if (op_size == L_P)
1119 op_size = (Hmode ? L_32 : L_16);
1120
1121 /* The size of the reg is v important. */
1122 if (op_size != x_size)
1123 found = 0;
1124 }
1125 else if (op_mode & CTRL) /* control register */
1126 {
1127 if (!(x_mode & CTRL))
1128 found = 0;
1129
1130 switch (x_mode)
1131 {
1132 case CCR:
1133 if (op_mode != CCR &&
1134 op_mode != CCR_EXR &&
1135 op_mode != CC_EX_VB_SB)
1136 found = 0;
1137 break;
1138 case EXR:
1139 if (op_mode != EXR &&
1140 op_mode != CCR_EXR &&
1141 op_mode != CC_EX_VB_SB)
1142 found = 0;
1143 break;
1144 case MACH:
1145 if (op_mode != MACH &&
1146 op_mode != MACREG)
1147 found = 0;
1148 break;
1149 case MACL:
1150 if (op_mode != MACL &&
1151 op_mode != MACREG)
1152 found = 0;
1153 break;
1154 case VBR:
1155 if (op_mode != VBR &&
1156 op_mode != VBR_SBR &&
1157 op_mode != CC_EX_VB_SB)
1158 found = 0;
1159 break;
1160 case SBR:
1161 if (op_mode != SBR &&
1162 op_mode != VBR_SBR &&
1163 op_mode != CC_EX_VB_SB)
1164 found = 0;
1165 break;
1166 }
1167 }
1168 else if ((op & ABSJMP) && (x_mode == ABS || x_mode == PCREL))
1169 {
1170 operands[i].mode &= ~MODE;
1171 operands[i].mode |= ABSJMP;
1172 /* But it may not be 24 bits long. */
1173 if (x_mode == ABS && !Hmode)
1174 {
1175 operands[i].mode &= ~SIZE;
1176 operands[i].mode |= L_16;
1177 }
1178 if ((operands[i].mode & SIZE) == L_32
1179 && (op_mode & SIZE) != L_32)
1180 found = 0;
1181 }
1182 else if (x_mode == IMM && op_mode != IMM)
1183 {
1184 offsetT num = operands[i].exp.X_add_number;
1185 if (op_mode == KBIT || op_mode == DBIT)
1186 /* This is ok if the immediate value is sensible. */;
1187 else if (op_mode == CONST_2)
1188 found = num == 2;
1189 else if (op_mode == CONST_4)
1190 found = num == 4;
1191 else if (op_mode == CONST_8)
1192 found = num == 8;
1193 else if (op_mode == CONST_16)
1194 found = num == 16;
1195 else
1196 found = 0;
1197 }
1198 else if (op_mode == PCREL && op_mode == x_mode)
1199 {
1200 /* movsd, bsr/bc and bsr/bs only come in PCREL16 flavour:
1201 If x_size is L_8, promote it. */
1202 if (OP_KIND (this_try->opcode->how) == O_MOVSD
1203 || OP_KIND (this_try->opcode->how) == O_BSRBC
1204 || OP_KIND (this_try->opcode->how) == O_BSRBS)
1205 if (x_size == L_8)
1206 x_size = L_16;
1207
1208 /* The size of the displacement is important. */
1209 if (op_size != x_size)
1210 found = 0;
1211 }
1212 else if ((op_mode == DISP || op_mode == IMM || op_mode == ABS
1213 || op_mode == INDEXB || op_mode == INDEXW
1214 || op_mode == INDEXL)
1215 && op_mode == x_mode)
1216 {
1217 /* Promote a L_24 to L_32 if it makes us match. */
1218 if (x_size == L_24 && op_size == L_32)
1219 {
1220 x &= ~SIZE;
1221 x |= x_size = L_32;
1222 }
1223
1224 #if 0 /* ??? */
1225 /* Promote an L8 to L_16 if it makes us match. */
1226 if ((op_mode == ABS || op_mode == DISP) && x_size == L_8)
1227 {
1228 if (op_size == L_16)
1229 x_size = L_16;
1230 }
1231 #endif
1232
1233 if (((x_size == L_16 && op_size == L_16U)
1234 || (x_size == L_8 && op_size == L_8U)
1235 || (x_size == L_3 && op_size == L_3NZ))
1236 /* We're deliberately more permissive for ABS modes. */
1237 && (op_mode == ABS
1238 || constant_fits_size_p (operands + i, op_size,
1239 op & NO_SYMBOLS)))
1240 x_size = op_size;
1241
1242 if (x_size != 0 && op_size != x_size)
1243 found = 0;
1244 else if (x_size == 0
1245 && ! constant_fits_size_p (operands + i, op_size,
1246 op & NO_SYMBOLS))
1247 found = 0;
1248 }
1249 else if (op_mode != x_mode)
1250 {
1251 found = 0;
1252 }
1253 }
1254 }
1255 if (found)
1256 {
1257 if ((this_try->opcode->available == AV_H8SX && ! SXmode)
1258 || (this_try->opcode->available == AV_H8S && ! Smode)
1259 || (this_try->opcode->available == AV_H8H && ! Hmode))
1260 found = 0, found_other = this_try;
1261 else if (this_size != size && (this_size != SN && size != SN))
1262 found_mismatched = this_try, found = 0;
1263
1264 }
1265 }
1266 if (found)
1267 return this_try;
1268 if (found_other)
1269 {
1270 as_warn (_("Opcode `%s' with these operand types not available in %s mode"),
1271 found_other->opcode->name,
1272 (! Hmode && ! Smode ? "H8/300"
1273 : SXmode ? "H8sx"
1274 : Smode ? "H8/300S"
1275 : "H8/300H"));
1276 }
1277 else if (found_mismatched)
1278 {
1279 as_warn (_("mismatch between opcode size and operand size"));
1280 return found_mismatched;
1281 }
1282 return 0;
1283 }
1284
1285 static void
1286 check_operand (operand, width, string)
1287 struct h8_op *operand;
1288 unsigned int width;
1289 char *string;
1290 {
1291 if (operand->exp.X_add_symbol == 0
1292 && operand->exp.X_op_symbol == 0)
1293 {
1294 /* No symbol involved, let's look at offset, it's dangerous if
1295 any of the high bits are not 0 or ff's, find out by oring or
1296 anding with the width and seeing if the answer is 0 or all
1297 fs. */
1298
1299 if (! constant_fits_width_p (operand, width))
1300 {
1301 if (width == 255
1302 && (operand->exp.X_add_number & 0xff00) == 0xff00)
1303 {
1304 /* Just ignore this one - which happens when trying to
1305 fit a 16 bit address truncated into an 8 bit address
1306 of something like bset. */
1307 }
1308 else if (strcmp (string, "@") == 0
1309 && width == 0xffff
1310 && (operand->exp.X_add_number & 0xff8000) == 0xff8000)
1311 {
1312 /* Just ignore this one - which happens when trying to
1313 fit a 24 bit address truncated into a 16 bit address
1314 of something like mov.w. */
1315 }
1316 else
1317 {
1318 as_warn (_("operand %s0x%lx out of range."), string,
1319 (unsigned long) operand->exp.X_add_number);
1320 }
1321 }
1322 }
1323 }
1324
1325 /* RELAXMODE has one of 3 values:
1326
1327 0 Output a "normal" reloc, no relaxing possible for this insn/reloc
1328
1329 1 Output a relaxable 24bit absolute mov.w address relocation
1330 (may relax into a 16bit absolute address).
1331
1332 2 Output a relaxable 16/24 absolute mov.b address relocation
1333 (may relax into an 8bit absolute address). */
1334
1335 static void
1336 do_a_fix_imm (offset, nibble, operand, relaxmode)
1337 int offset, nibble;
1338 struct h8_op *operand;
1339 int relaxmode;
1340 {
1341 int idx;
1342 int size;
1343 int where;
1344 char *bytes = frag_now->fr_literal + offset;
1345
1346 char *t = ((operand->mode & MODE) == IMM) ? "#" : "@";
1347
1348 if (operand->exp.X_add_symbol == 0)
1349 {
1350 switch (operand->mode & SIZE)
1351 {
1352 case L_2:
1353 check_operand (operand, 0x3, t);
1354 bytes[0] |= (operand->exp.X_add_number & 3) << (nibble ? 0 : 4);
1355 break;
1356 case L_3:
1357 case L_3NZ:
1358 check_operand (operand, 0x7, t);
1359 bytes[0] |= (operand->exp.X_add_number & 7) << (nibble ? 0 : 4);
1360 break;
1361 case L_4:
1362 check_operand (operand, 0xF, t);
1363 bytes[0] |= (operand->exp.X_add_number & 15) << (nibble ? 0 : 4);
1364 break;
1365 case L_5:
1366 check_operand (operand, 0x1F, t);
1367 bytes[0] |= operand->exp.X_add_number & 31;
1368 break;
1369 case L_8:
1370 case L_8U:
1371 check_operand (operand, 0xff, t);
1372 bytes[0] |= operand->exp.X_add_number;
1373 break;
1374 case L_16:
1375 case L_16U:
1376 check_operand (operand, 0xffff, t);
1377 bytes[0] |= operand->exp.X_add_number >> 8;
1378 bytes[1] |= operand->exp.X_add_number >> 0;
1379 break;
1380 case L_24:
1381 check_operand (operand, 0xffffff, t);
1382 bytes[0] |= operand->exp.X_add_number >> 16;
1383 bytes[1] |= operand->exp.X_add_number >> 8;
1384 bytes[2] |= operand->exp.X_add_number >> 0;
1385 break;
1386
1387 case L_32:
1388 /* This should be done with bfd. */
1389 bytes[0] |= operand->exp.X_add_number >> 24;
1390 bytes[1] |= operand->exp.X_add_number >> 16;
1391 bytes[2] |= operand->exp.X_add_number >> 8;
1392 bytes[3] |= operand->exp.X_add_number >> 0;
1393 if (relaxmode != 0)
1394 {
1395 idx = (relaxmode == 2) ? R_MOV24B1 : R_MOVL1;
1396 fix_new_exp (frag_now, offset, 4, &operand->exp, 0, idx);
1397 }
1398 break;
1399 }
1400 }
1401 else
1402 {
1403 switch (operand->mode & SIZE)
1404 {
1405 case L_24:
1406 case L_32:
1407 size = 4;
1408 where = (operand->mode & SIZE) == L_24 ? -1 : 0;
1409 if (relaxmode == 2)
1410 idx = R_MOV24B1;
1411 else if (relaxmode == 1)
1412 idx = R_MOVL1;
1413 else
1414 idx = R_RELLONG;
1415 break;
1416 default:
1417 as_bad (_("Can't work out size of operand.\n"));
1418 case L_16:
1419 case L_16U:
1420 size = 2;
1421 where = 0;
1422 if (relaxmode == 2)
1423 idx = R_MOV16B1;
1424 else
1425 idx = R_RELWORD;
1426 operand->exp.X_add_number =
1427 ((operand->exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1428 operand->exp.X_add_number |= (bytes[0] << 8) | bytes[1];
1429 break;
1430 case L_8:
1431 size = 1;
1432 where = 0;
1433 idx = R_RELBYTE;
1434 operand->exp.X_add_number =
1435 ((operand->exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1436 operand->exp.X_add_number |= bytes[0];
1437 }
1438
1439 fix_new_exp (frag_now,
1440 offset + where,
1441 size,
1442 &operand->exp,
1443 0,
1444 idx);
1445 }
1446 }
1447
1448 /* Now we know what sort of opcodes it is, let's build the bytes. */
1449
1450 static void
1451 build_bytes (this_try, operand)
1452 const struct h8_instruction *this_try;
1453 struct h8_op *operand;
1454 {
1455 int i;
1456 char *output = frag_more (this_try->length);
1457 op_type *nibble_ptr = this_try->opcode->data.nib;
1458 op_type c;
1459 unsigned int nibble_count = 0;
1460 int op_at[3];
1461 int nib = 0;
1462 int movb = 0;
1463 char asnibbles[100];
1464 char *p = asnibbles;
1465 int high, low;
1466
1467 if (!Hmode && this_try->opcode->available != AV_H8)
1468 as_warn (_("Opcode `%s' with these operand types not available in H8/300 mode"),
1469 this_try->opcode->name);
1470 else if (!Smode
1471 && this_try->opcode->available != AV_H8
1472 && this_try->opcode->available != AV_H8H)
1473 as_warn (_("Opcode `%s' with these operand types not available in H8/300H mode"),
1474 this_try->opcode->name);
1475 else if (!SXmode
1476 && this_try->opcode->available != AV_H8
1477 && this_try->opcode->available != AV_H8H
1478 && this_try->opcode->available != AV_H8S)
1479 as_warn (_("Opcode `%s' with these operand types not available in H8/300S mode"),
1480 this_try->opcode->name);
1481
1482 while (*nibble_ptr != (op_type) E)
1483 {
1484 int d;
1485
1486 nib = 0;
1487 c = *nibble_ptr++;
1488
1489 d = (c & OP3) == OP3 ? 2 : (c & DST) == DST ? 1 : 0;
1490
1491 if (c < 16)
1492 nib = c;
1493 else
1494 {
1495 int c2 = c & MODE;
1496
1497 if (c2 == REG || c2 == LOWREG
1498 || c2 == IND || c2 == PREINC || c2 == PREDEC
1499 || c2 == POSTINC || c2 == POSTDEC)
1500 {
1501 nib = operand[d].reg;
1502 if (c2 == LOWREG)
1503 nib &= 7;
1504 }
1505
1506 else if (c & CTRL) /* Control reg operand. */
1507 nib = operand[d].reg;
1508
1509 else if ((c & DISPREG) == (DISPREG))
1510 {
1511 nib = operand[d].reg;
1512 }
1513 else if (c2 == ABS)
1514 {
1515 operand[d].mode = c;
1516 op_at[d] = nibble_count;
1517 nib = 0;
1518 }
1519 else if (c2 == IMM || c2 == PCREL || c2 == ABS
1520 || (c & ABSJMP) || c2 == DISP)
1521 {
1522 operand[d].mode = c;
1523 op_at[d] = nibble_count;
1524 nib = 0;
1525 }
1526 else if ((c & IGNORE) || (c & DATA))
1527 nib = 0;
1528
1529 else if (c2 == DBIT)
1530 {
1531 switch (operand[0].exp.X_add_number)
1532 {
1533 case 1:
1534 nib = c;
1535 break;
1536 case 2:
1537 nib = 0x8 | c;
1538 break;
1539 default:
1540 as_bad (_("Need #1 or #2 here"));
1541 }
1542 }
1543 else if (c2 == KBIT)
1544 {
1545 switch (operand[0].exp.X_add_number)
1546 {
1547 case 1:
1548 nib = 0;
1549 break;
1550 case 2:
1551 nib = 8;
1552 break;
1553 case 4:
1554 if (!Hmode)
1555 as_warn (_("#4 not valid on H8/300."));
1556 nib = 9;
1557 break;
1558
1559 default:
1560 as_bad (_("Need #1 or #2 here"));
1561 break;
1562 }
1563 /* Stop it making a fix. */
1564 operand[0].mode = 0;
1565 }
1566
1567 if (c & MEMRELAX)
1568 operand[d].mode |= MEMRELAX;
1569
1570 if (c & B31)
1571 nib |= 0x8;
1572
1573 if (c & B21)
1574 nib |= 0x4;
1575
1576 if (c & B11)
1577 nib |= 0x2;
1578
1579 if (c & B01)
1580 nib |= 0x1;
1581
1582 if (c2 == MACREG)
1583 {
1584 if (operand[0].mode == MACREG)
1585 /* stmac has mac[hl] as the first operand. */
1586 nib = 2 + operand[0].reg;
1587 else
1588 /* ldmac has mac[hl] as the second operand. */
1589 nib = 2 + operand[1].reg;
1590 }
1591 }
1592 nibble_count++;
1593
1594 *p++ = nib;
1595 }
1596
1597 /* Disgusting. Why, oh why didn't someone ask us for advice
1598 on the assembler format. */
1599 if (OP_KIND (this_try->opcode->how) == O_LDM)
1600 {
1601 high = (operand[1].reg >> 8) & 0xf;
1602 low = (operand[1].reg) & 0xf;
1603 asnibbles[2] = high - low;
1604 asnibbles[7] = high;
1605 }
1606 else if (OP_KIND (this_try->opcode->how) == O_STM)
1607 {
1608 high = (operand[0].reg >> 8) & 0xf;
1609 low = (operand[0].reg) & 0xf;
1610 asnibbles[2] = high - low;
1611 asnibbles[7] = low;
1612 }
1613
1614 for (i = 0; i < this_try->length; i++)
1615 output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
1616
1617 /* Note if this is a movb instruction -- there's a special relaxation
1618 which only applies to them. */
1619 if (this_try->opcode->how == O (O_MOV, SB))
1620 movb = 1;
1621
1622 /* Output any fixes. */
1623 for (i = 0; i < this_try->noperands; i++)
1624 {
1625 int x = operand[i].mode;
1626 int x_mode = x & MODE;
1627
1628 if (x_mode == IMM || x_mode == DISP)
1629 do_a_fix_imm (output - frag_now->fr_literal + op_at[i] / 2,
1630 op_at[i] & 1, operand + i, (x & MEMRELAX) != 0);
1631
1632 else if (x_mode == ABS)
1633 do_a_fix_imm (output - frag_now->fr_literal + op_at[i] / 2,
1634 op_at[i] & 1, operand + i,
1635 (x & MEMRELAX) ? movb + 1 : 0);
1636
1637 else if (x_mode == PCREL)
1638 {
1639 int size16 = (x & SIZE) == L_16;
1640 int size = size16 ? 2 : 1;
1641 int type = size16 ? R_PCRWORD : R_PCRBYTE;
1642 fixS *fixP;
1643
1644 check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1645
1646 if (operand[i].exp.X_add_number & 1)
1647 as_warn (_("branch operand has odd offset (%lx)\n"),
1648 (unsigned long) operand->exp.X_add_number);
1649 #ifndef OBJ_ELF
1650 /* The COFF port has always been off by one, changing it
1651 now would be an incompatible change, so we leave it as-is.
1652
1653 We don't want to do this for ELF as we want to be
1654 compatible with the proposed ELF format from Hitachi. */
1655 operand[i].exp.X_add_number -= 1;
1656 #endif
1657 if (size16)
1658 {
1659 operand[i].exp.X_add_number =
1660 ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1661 }
1662 else
1663 {
1664 operand[i].exp.X_add_number =
1665 ((operand[i].exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1666 }
1667
1668 /* For BRA/S. */
1669 if (! size16)
1670 operand[i].exp.X_add_number |= output[op_at[i] / 2];
1671
1672 fixP = fix_new_exp (frag_now,
1673 output - frag_now->fr_literal + op_at[i] / 2,
1674 size,
1675 &operand[i].exp,
1676 1,
1677 type);
1678 fixP->fx_signed = 1;
1679 }
1680 else if (x_mode == MEMIND)
1681 {
1682 check_operand (operand + i, 0xff, "@@");
1683 fix_new_exp (frag_now,
1684 output - frag_now->fr_literal + 1,
1685 1,
1686 &operand[i].exp,
1687 0,
1688 R_MEM_INDIRECT);
1689 }
1690 else if (x_mode == VECIND)
1691 {
1692 check_operand (operand + i, 0x7f, "@@");
1693 /* FIXME: approximating the effect of "B31" here...
1694 This is very hackish, and ought to be done a better way. */
1695 operand[i].exp.X_add_number |= 0x80;
1696 fix_new_exp (frag_now,
1697 output - frag_now->fr_literal + 1,
1698 1,
1699 &operand[i].exp,
1700 0,
1701 R_MEM_INDIRECT);
1702 }
1703 else if (x & ABSJMP)
1704 {
1705 int where = 0;
1706 bfd_reloc_code_real_type reloc_type = R_JMPL1;
1707
1708 #ifdef OBJ_ELF
1709 /* To be compatible with the proposed H8 ELF format, we
1710 want the relocation's offset to point to the first byte
1711 that will be modified, not to the start of the instruction. */
1712
1713 if ((operand->mode & SIZE) == L_32)
1714 {
1715 where = 2;
1716 reloc_type = R_RELLONG;
1717 }
1718 else
1719 where = 1;
1720 #endif
1721
1722 /* This jmp may be a jump or a branch. */
1723
1724 check_operand (operand + i,
1725 SXmode ? 0xffffffff : Hmode ? 0xffffff : 0xffff,
1726 "@");
1727
1728 if (operand[i].exp.X_add_number & 1)
1729 as_warn (_("branch operand has odd offset (%lx)\n"),
1730 (unsigned long) operand->exp.X_add_number);
1731
1732 if (!Hmode)
1733 operand[i].exp.X_add_number =
1734 ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1735 fix_new_exp (frag_now,
1736 output - frag_now->fr_literal + where,
1737 4,
1738 &operand[i].exp,
1739 0,
1740 reloc_type);
1741 }
1742 }
1743 }
1744
1745 /* Try to give an intelligent error message for common and simple to
1746 detect errors. */
1747
1748 static void
1749 clever_message (instruction, operand)
1750 const struct h8_instruction *instruction;
1751 struct h8_op *operand;
1752 {
1753 /* Find out if there was more than one possible opcode. */
1754
1755 if ((instruction + 1)->idx != instruction->idx)
1756 {
1757 int argn;
1758
1759 /* Only one opcode of this flavour, try to guess which operand
1760 didn't match. */
1761 for (argn = 0; argn < instruction->noperands; argn++)
1762 {
1763 switch (instruction->opcode->args.nib[argn])
1764 {
1765 case RD16:
1766 if (operand[argn].mode != RD16)
1767 {
1768 as_bad (_("destination operand must be 16 bit register"));
1769 return;
1770
1771 }
1772 break;
1773
1774 case RS8:
1775 if (operand[argn].mode != RS8)
1776 {
1777 as_bad (_("source operand must be 8 bit register"));
1778 return;
1779 }
1780 break;
1781
1782 case ABS16DST:
1783 if (operand[argn].mode != ABS16DST)
1784 {
1785 as_bad (_("destination operand must be 16bit absolute address"));
1786 return;
1787 }
1788 break;
1789 case RD8:
1790 if (operand[argn].mode != RD8)
1791 {
1792 as_bad (_("destination operand must be 8 bit register"));
1793 return;
1794 }
1795 break;
1796
1797 case ABS16SRC:
1798 if (operand[argn].mode != ABS16SRC)
1799 {
1800 as_bad (_("source operand must be 16bit absolute address"));
1801 return;
1802 }
1803 break;
1804
1805 }
1806 }
1807 }
1808 as_bad (_("invalid operands"));
1809 }
1810
1811
1812 /* If OPERAND is part of an address, adjust its size and value given
1813 that it addresses SIZE bytes.
1814
1815 This function decides how big non-immediate constants are when no
1816 size was explicitly given. It also scales down the assembly-level
1817 displacement in an @(d:2,ERn) operand. */
1818
1819 static void
1820 fix_operand_size (operand, size)
1821 struct h8_op *operand;
1822 int size;
1823 {
1824 if (SXmode && (operand->mode & MODE) == DISP)
1825 {
1826 /* If the user didn't specify an operand width, see if we
1827 can use @(d:2,ERn). */
1828 if ((operand->mode & SIZE) == 0
1829 && operand->exp.X_add_symbol == 0
1830 && operand->exp.X_op_symbol == 0
1831 && (operand->exp.X_add_number == size
1832 || operand->exp.X_add_number == size * 2
1833 || operand->exp.X_add_number == size * 3))
1834 operand->mode |= L_2;
1835
1836 /* Scale down the displacement in an @(d:2,ERn) operand.
1837 X_add_number then contains the desired field value. */
1838 if ((operand->mode & SIZE) == L_2)
1839 {
1840 if (operand->exp.X_add_number % size != 0)
1841 as_warn (_("operand/size mis-match"));
1842 operand->exp.X_add_number /= size;
1843 }
1844 }
1845
1846 if ((operand->mode & SIZE) == 0)
1847 switch (operand->mode & MODE)
1848 {
1849 case DISP:
1850 case INDEXB:
1851 case INDEXW:
1852 case INDEXL:
1853 case ABS:
1854 /* Pick a 24-bit address unless we know that a 16-bit address
1855 is safe. get_specific() will relax L_24 into L_32 where
1856 necessary. */
1857 if (Hmode
1858 && (operand->exp.X_add_number < -32768
1859 || operand->exp.X_add_number > 32767
1860 || operand->exp.X_add_symbol != 0
1861 || operand->exp.X_op_symbol != 0))
1862 operand->mode |= L_24;
1863 else
1864 operand->mode |= L_16;
1865 break;
1866
1867 case PCREL:
1868 /* This condition is long standing, though somewhat suspect. */
1869 if (operand->exp.X_add_number > -128
1870 && operand->exp.X_add_number < 127)
1871 operand->mode |= L_8;
1872 else
1873 operand->mode |= L_16;
1874 break;
1875 }
1876 }
1877
1878
1879 /* This is the guts of the machine-dependent assembler. STR points to
1880 a machine dependent instruction. This function is supposed to emit
1881 the frags/bytes it assembles. */
1882
1883 void
1884 md_assemble (str)
1885 char *str;
1886 {
1887 char *op_start;
1888 char *op_end;
1889 struct h8_op operand[3];
1890 const struct h8_instruction *instruction;
1891 const struct h8_instruction *prev_instruction;
1892
1893 char *dot = 0;
1894 char *slash = 0;
1895 char c;
1896 int size, i;
1897
1898 /* Drop leading whitespace. */
1899 while (*str == ' ')
1900 str++;
1901
1902 /* Find the op code end. */
1903 for (op_start = op_end = str;
1904 *op_end != 0 && *op_end != ' ';
1905 op_end++)
1906 {
1907 if (*op_end == '.')
1908 {
1909 dot = op_end + 1;
1910 *op_end = 0;
1911 op_end += 2;
1912 break;
1913 }
1914 else if (*op_end == '/' && ! slash)
1915 slash = op_end;
1916 }
1917
1918 if (op_end == op_start)
1919 {
1920 as_bad (_("can't find opcode "));
1921 }
1922 c = *op_end;
1923
1924 *op_end = 0;
1925
1926 /* The assembler stops scanning the opcode at slashes, so it fails
1927 to make characters following them lower case. Fix them. */
1928 if (slash)
1929 while (*++slash)
1930 *slash = TOLOWER (*slash);
1931
1932 instruction = (const struct h8_instruction *)
1933 hash_find (opcode_hash_control, op_start);
1934
1935 if (instruction == NULL)
1936 {
1937 as_bad (_("unknown opcode"));
1938 return;
1939 }
1940
1941 /* We used to set input_line_pointer to the result of get_operands,
1942 but that is wrong. Our caller assumes we don't change it. */
1943
1944 operand[0].mode = 0;
1945 operand[1].mode = 0;
1946 operand[2].mode = 0;
1947
1948 if (OP_KIND (instruction->opcode->how) == O_MOVAB
1949 || OP_KIND (instruction->opcode->how) == O_MOVAW
1950 || OP_KIND (instruction->opcode->how) == O_MOVAL)
1951 get_mova_operands (op_end, operand);
1952 else if (OP_KIND (instruction->opcode->how) == O_RTEL
1953 || OP_KIND (instruction->opcode->how) == O_RTSL)
1954 get_rtsl_operands (op_end, operand);
1955 else
1956 get_operands (instruction->noperands, op_end, operand);
1957
1958 *op_end = c;
1959 prev_instruction = instruction;
1960
1961 size = SN;
1962 if (dot)
1963 {
1964 switch (TOLOWER (*dot))
1965 {
1966 case 'b':
1967 size = SB;
1968 break;
1969
1970 case 'w':
1971 size = SW;
1972 break;
1973
1974 case 'l':
1975 size = SL;
1976 break;
1977 }
1978 }
1979 if (OP_KIND (instruction->opcode->how) == O_MOVAB ||
1980 OP_KIND (instruction->opcode->how) == O_MOVAW ||
1981 OP_KIND (instruction->opcode->how) == O_MOVAL)
1982 {
1983 switch (operand[0].mode & MODE)
1984 {
1985 case INDEXB:
1986 default:
1987 fix_operand_size (&operand[1], 1);
1988 break;
1989 case INDEXW:
1990 fix_operand_size (&operand[1], 2);
1991 break;
1992 case INDEXL:
1993 fix_operand_size (&operand[1], 4);
1994 break;
1995 }
1996 }
1997 else
1998 {
1999 for (i = 0; i < 3 && operand[i].mode != 0; i++)
2000 switch (size)
2001 {
2002 case SN:
2003 case SB:
2004 default:
2005 fix_operand_size (&operand[i], 1);
2006 break;
2007 case SW:
2008 fix_operand_size (&operand[i], 2);
2009 break;
2010 case SL:
2011 fix_operand_size (&operand[i], 4);
2012 break;
2013 }
2014 }
2015
2016 instruction = get_specific (instruction, operand, size);
2017
2018 if (instruction == 0)
2019 {
2020 /* Couldn't find an opcode which matched the operands. */
2021 char *where = frag_more (2);
2022
2023 where[0] = 0x0;
2024 where[1] = 0x0;
2025 clever_message (prev_instruction, operand);
2026
2027 return;
2028 }
2029
2030 build_bytes (instruction, operand);
2031
2032 #ifdef BFD_ASSEMBLER
2033 dwarf2_emit_insn (instruction->length);
2034 #endif
2035 }
2036
2037 #ifndef BFD_ASSEMBLER
2038 void
2039 tc_crawl_symbol_chain (headers)
2040 object_headers *headers ATTRIBUTE_UNUSED;
2041 {
2042 printf (_("call to tc_crawl_symbol_chain \n"));
2043 }
2044 #endif
2045
2046 symbolS *
2047 md_undefined_symbol (name)
2048 char *name ATTRIBUTE_UNUSED;
2049 {
2050 return 0;
2051 }
2052
2053 #ifndef BFD_ASSEMBLER
2054 void
2055 tc_headers_hook (headers)
2056 object_headers *headers ATTRIBUTE_UNUSED;
2057 {
2058 printf (_("call to tc_headers_hook \n"));
2059 }
2060 #endif
2061
2062 /* Various routines to kill one day */
2063 /* Equal to MAX_PRECISION in atof-ieee.c */
2064 #define MAX_LITTLENUMS 6
2065
2066 /* Turn a string in input_line_pointer into a floating point constant
2067 of type TYPE, and store the appropriate bytes in *LITP. The number
2068 of LITTLENUMS emitted is stored in *SIZEP. An error message is
2069 returned, or NULL on OK. */
2070
2071 char *
2072 md_atof (type, litP, sizeP)
2073 char type;
2074 char *litP;
2075 int *sizeP;
2076 {
2077 int prec;
2078 LITTLENUM_TYPE words[MAX_LITTLENUMS];
2079 LITTLENUM_TYPE *wordP;
2080 char *t;
2081
2082 switch (type)
2083 {
2084 case 'f':
2085 case 'F':
2086 case 's':
2087 case 'S':
2088 prec = 2;
2089 break;
2090
2091 case 'd':
2092 case 'D':
2093 case 'r':
2094 case 'R':
2095 prec = 4;
2096 break;
2097
2098 case 'x':
2099 case 'X':
2100 prec = 6;
2101 break;
2102
2103 case 'p':
2104 case 'P':
2105 prec = 6;
2106 break;
2107
2108 default:
2109 *sizeP = 0;
2110 return _("Bad call to MD_ATOF()");
2111 }
2112 t = atof_ieee (input_line_pointer, type, words);
2113 if (t)
2114 input_line_pointer = t;
2115
2116 *sizeP = prec * sizeof (LITTLENUM_TYPE);
2117 for (wordP = words; prec--;)
2118 {
2119 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
2120 litP += sizeof (LITTLENUM_TYPE);
2121 }
2122 return 0;
2123 }
2124 \f
2125 const char *md_shortopts = "";
2126 struct option md_longopts[] = {
2127 {NULL, no_argument, NULL, 0}
2128 };
2129
2130 size_t md_longopts_size = sizeof (md_longopts);
2131
2132 int
2133 md_parse_option (c, arg)
2134 int c ATTRIBUTE_UNUSED;
2135 char *arg ATTRIBUTE_UNUSED;
2136 {
2137 return 0;
2138 }
2139
2140 void
2141 md_show_usage (stream)
2142 FILE *stream ATTRIBUTE_UNUSED;
2143 {
2144 }
2145 \f
2146 void tc_aout_fix_to_chars PARAMS ((void));
2147
2148 void
2149 tc_aout_fix_to_chars ()
2150 {
2151 printf (_("call to tc_aout_fix_to_chars \n"));
2152 abort ();
2153 }
2154
2155 void
2156 md_convert_frag (headers, seg, fragP)
2157 #ifdef BFD_ASSEMBLER
2158 bfd *headers ATTRIBUTE_UNUSED;
2159 #else
2160 object_headers *headers ATTRIBUTE_UNUSED;
2161 #endif
2162 segT seg ATTRIBUTE_UNUSED;
2163 fragS *fragP ATTRIBUTE_UNUSED;
2164 {
2165 printf (_("call to md_convert_frag \n"));
2166 abort ();
2167 }
2168
2169 #ifdef BFD_ASSEMBLER
2170 valueT
2171 md_section_align (segment, size)
2172 segT segment;
2173 valueT size;
2174 {
2175 int align = bfd_get_section_alignment (stdoutput, segment);
2176 return ((size + (1 << align) - 1) & (-1 << align));
2177 }
2178 #else
2179 valueT
2180 md_section_align (seg, size)
2181 segT seg;
2182 valueT size;
2183 {
2184 return ((size + (1 << section_alignment[(int) seg]) - 1)
2185 & (-1 << section_alignment[(int) seg]));
2186 }
2187 #endif
2188
2189
2190 void
2191 md_apply_fix3 (fixP, valP, seg)
2192 fixS *fixP;
2193 valueT *valP;
2194 segT seg ATTRIBUTE_UNUSED;
2195 {
2196 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
2197 long val = *valP;
2198
2199 switch (fixP->fx_size)
2200 {
2201 case 1:
2202 *buf++ = val;
2203 break;
2204 case 2:
2205 *buf++ = (val >> 8);
2206 *buf++ = val;
2207 break;
2208 case 4:
2209 *buf++ = (val >> 24);
2210 *buf++ = (val >> 16);
2211 *buf++ = (val >> 8);
2212 *buf++ = val;
2213 break;
2214 default:
2215 abort ();
2216 }
2217
2218 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
2219 fixP->fx_done = 1;
2220 }
2221
2222 int
2223 md_estimate_size_before_relax (fragP, segment_type)
2224 register fragS *fragP ATTRIBUTE_UNUSED;
2225 register segT segment_type ATTRIBUTE_UNUSED;
2226 {
2227 printf (_("call tomd_estimate_size_before_relax \n"));
2228 abort ();
2229 }
2230
2231 /* Put number into target byte order. */
2232 void
2233 md_number_to_chars (ptr, use, nbytes)
2234 char *ptr;
2235 valueT use;
2236 int nbytes;
2237 {
2238 number_to_chars_bigendian (ptr, use, nbytes);
2239 }
2240
2241 long
2242 md_pcrel_from (fixP)
2243 fixS *fixP ATTRIBUTE_UNUSED;
2244 {
2245 abort ();
2246 }
2247
2248 #ifndef BFD_ASSEMBLER
2249 void
2250 tc_reloc_mangle (fix_ptr, intr, base)
2251 fixS *fix_ptr;
2252 struct internal_reloc *intr;
2253 bfd_vma base;
2254
2255 {
2256 symbolS *symbol_ptr;
2257
2258 symbol_ptr = fix_ptr->fx_addsy;
2259
2260 /* If this relocation is attached to a symbol then it's ok
2261 to output it. */
2262 if (fix_ptr->fx_r_type == TC_CONS_RELOC)
2263 {
2264 /* cons likes to create reloc32's whatever the size of the reloc..
2265 */
2266 switch (fix_ptr->fx_size)
2267 {
2268 case 4:
2269 intr->r_type = R_RELLONG;
2270 break;
2271 case 2:
2272 intr->r_type = R_RELWORD;
2273 break;
2274 case 1:
2275 intr->r_type = R_RELBYTE;
2276 break;
2277 default:
2278 abort ();
2279 }
2280 }
2281 else
2282 {
2283 intr->r_type = fix_ptr->fx_r_type;
2284 }
2285
2286 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
2287 intr->r_offset = fix_ptr->fx_offset;
2288
2289 if (symbol_ptr)
2290 {
2291 if (symbol_ptr->sy_number != -1)
2292 intr->r_symndx = symbol_ptr->sy_number;
2293 else
2294 {
2295 symbolS *segsym;
2296
2297 /* This case arises when a reference is made to `.'. */
2298 segsym = seg_info (S_GET_SEGMENT (symbol_ptr))->dot;
2299 if (segsym == NULL)
2300 intr->r_symndx = -1;
2301 else
2302 {
2303 intr->r_symndx = segsym->sy_number;
2304 intr->r_offset += S_GET_VALUE (symbol_ptr);
2305 }
2306 }
2307 }
2308 else
2309 intr->r_symndx = -1;
2310 }
2311 #else /* BFD_ASSEMBLER */
2312 arelent *
2313 tc_gen_reloc (section, fixp)
2314 asection *section ATTRIBUTE_UNUSED;
2315 fixS *fixp;
2316 {
2317 arelent *rel;
2318 bfd_reloc_code_real_type r_type;
2319
2320 if (fixp->fx_addsy && fixp->fx_subsy)
2321 {
2322 if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
2323 || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
2324 {
2325 as_bad_where (fixp->fx_file, fixp->fx_line,
2326 "Difference of symbols in different sections is not supported");
2327 return NULL;
2328 }
2329 }
2330
2331 rel = (arelent *) xmalloc (sizeof (arelent));
2332 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
2333 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
2334 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
2335 rel->addend = fixp->fx_offset;
2336
2337 r_type = fixp->fx_r_type;
2338
2339 #define DEBUG 0
2340 #if DEBUG
2341 fprintf (stderr, "%s\n", bfd_get_reloc_code_name (r_type));
2342 fflush(stderr);
2343 #endif
2344 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
2345 if (rel->howto == NULL)
2346 {
2347 as_bad_where (fixp->fx_file, fixp->fx_line,
2348 _("Cannot represent relocation type %s"),
2349 bfd_get_reloc_code_name (r_type));
2350 return NULL;
2351 }
2352
2353 return rel;
2354 }
2355 #endif