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