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