Fix typo last change
[binutils.git] / gas / config / tc-h8300.c
blob641a7e54263966b6ac81206a817d31d49b2ca9f3
1 /* tc-h8300.c -- Assemble code for the Hitachi H8/300
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
3 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
24 #include <stdio.h>
25 #include "as.h"
26 #include "subsegs.h"
27 #include "bfd.h"
28 #define DEFINE_TABLE
29 #define h8_opcodes ops
30 #include "opcode/h8300.h"
31 #include "safe-ctype.h"
33 #ifdef OBJ_ELF
34 #include "elf/h8.h"
35 #endif
37 const char comment_chars[] = ";";
38 const char line_comment_chars[] = "#";
39 const char line_separator_chars[] = "";
41 void cons PARAMS ((int));
42 void sbranch PARAMS ((int));
43 void h8300hmode PARAMS ((int));
44 void h8300smode PARAMS ((int));
45 static void pint PARAMS ((int));
47 int Hmode;
48 int Smode;
50 #define PSIZE (Hmode ? L_32 : L_16)
51 #define DMODE (L_16)
52 #define DSYMMODE (Hmode ? L_24 : L_16)
54 int bsize = L_8; /* default branch displacement */
56 void
57 h8300hmode (arg)
58 int arg ATTRIBUTE_UNUSED;
60 Hmode = 1;
61 Smode = 0;
62 #ifdef BFD_ASSEMBLER
63 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300h))
64 as_warn (_("could not set architecture and machine"));
65 #endif
68 void
69 h8300smode (arg)
70 int arg ATTRIBUTE_UNUSED;
72 Smode = 1;
73 Hmode = 1;
74 #ifdef BFD_ASSEMBLER
75 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300s))
76 as_warn (_("could not set architecture and machine"));
77 #endif
80 void
81 sbranch (size)
82 int size;
84 bsize = size;
87 static void
88 pint (arg)
89 int arg ATTRIBUTE_UNUSED;
91 cons (Hmode ? 4 : 2);
94 /* This table describes all the machine specific pseudo-ops the assembler
95 has to support. The fields are:
96 pseudo-op name without dot
97 function to call to execute this pseudo-op
98 Integer arg to pass to the function. */
100 const pseudo_typeS md_pseudo_table[] =
102 {"h8300h", h8300hmode, 0},
103 {"h8300s", h8300smode, 0},
104 {"sbranch", sbranch, L_8},
105 {"lbranch", sbranch, L_16},
107 {"int", pint, 0},
108 {"data.b", cons, 1},
109 {"data.w", cons, 2},
110 {"data.l", cons, 4},
111 {"form", listing_psize, 0},
112 {"heading", listing_title, 0},
113 {"import", s_ignore, 0},
114 {"page", listing_eject, 0},
115 {"program", s_ignore, 0},
116 {0, 0, 0}
119 const int md_reloc_size;
121 const char EXP_CHARS[] = "eE";
123 /* Chars that mean this number is a floating point constant
124 As in 0f12.456
125 or 0d1.2345e12. */
126 const char FLT_CHARS[] = "rRsSfFdDxXpP";
128 static struct hash_control *opcode_hash_control; /* Opcode mnemonics. */
130 /* This function is called once, at assembler startup time. This
131 should set up all the tables, etc. that the MD part of the assembler
132 needs. */
134 void
135 md_begin ()
137 struct h8_opcode *opcode;
138 char prev_buffer[100];
139 int idx = 0;
141 #ifdef BFD_ASSEMBLER
142 if (!bfd_set_arch_mach (stdoutput, bfd_arch_h8300, bfd_mach_h8300))
143 as_warn (_("could not set architecture and machine"));
144 #endif
146 opcode_hash_control = hash_new ();
147 prev_buffer[0] = 0;
149 for (opcode = h8_opcodes; opcode->name; opcode++)
151 /* Strip off any . part when inserting the opcode and only enter
152 unique codes into the hash table. */
153 char *src = opcode->name;
154 unsigned int len = strlen (src);
155 char *dst = malloc (len + 1);
156 char *buffer = dst;
158 opcode->size = 0;
159 while (*src)
161 if (*src == '.')
163 src++;
164 opcode->size = *src;
165 break;
167 *dst++ = *src++;
169 *dst++ = 0;
170 if (strcmp (buffer, prev_buffer))
172 hash_insert (opcode_hash_control, buffer, (char *) opcode);
173 strcpy (prev_buffer, buffer);
174 idx++;
176 opcode->idx = idx;
178 /* Find the number of operands. */
179 opcode->noperands = 0;
180 while (opcode->args.nib[opcode->noperands] != E)
181 opcode->noperands++;
183 /* Find the length of the opcode in bytes. */
184 opcode->length = 0;
185 while (opcode->data.nib[opcode->length * 2] != E)
186 opcode->length++;
189 linkrelax = 1;
192 struct h8_exp
194 char *e_beg;
195 char *e_end;
196 expressionS e_exp;
199 int dispreg;
200 int opsize; /* Set when a register size is seen. */
202 struct h8_op
204 op_type mode;
205 unsigned reg;
206 expressionS exp;
209 static void clever_message PARAMS ((struct h8_opcode *, struct h8_op *));
210 static void build_bytes PARAMS ((struct h8_opcode *, struct h8_op *));
211 static void do_a_fix_imm PARAMS ((int, struct h8_op *, int));
212 static void check_operand PARAMS ((struct h8_op *, unsigned int, char *));
213 static struct h8_opcode * get_specific PARAMS ((struct h8_opcode *, struct h8_op *, int));
214 static char * get_operands PARAMS ((unsigned, char *, struct h8_op *));
215 static void get_operand PARAMS ((char **, struct h8_op *, unsigned, int));
216 static char * skip_colonthing PARAMS ((char *, expressionS *, int *));
217 static char * parse_exp PARAMS ((char *, expressionS *));
218 static int parse_reg PARAMS ((char *, op_type *, unsigned *, int));
219 char * colonmod24 PARAMS ((struct h8_op *, char *));
222 parse operands
223 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
224 r0l,r0h,..r7l,r7h
225 @WREG
226 @WREG+
227 @-WREG
228 #const
232 /* Try to parse a reg name. Return the number of chars consumed. */
234 static int
235 parse_reg (src, mode, reg, direction)
236 char *src;
237 op_type *mode;
238 unsigned int *reg;
239 int direction;
241 char *end;
242 int len;
244 /* Cribbed from get_symbol_end. */
245 if (!is_name_beginner (*src) || *src == '\001')
246 return 0;
247 end = src + 1;
248 while (is_part_of_name (*end) || *end == '\001')
249 end++;
250 len = end - src;
252 if (len == 2 && src[0] == 's' && src[1] == 'p')
254 *mode = PSIZE | REG | direction;
255 *reg = 7;
256 return len;
258 if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
260 *mode = CCR;
261 *reg = 0;
262 return len;
264 if (len == 3 && src[0] == 'e' && src[1] == 'x' && src[2] == 'r')
266 *mode = EXR;
267 *reg = 0;
268 return len;
270 if (len == 2 && src[0] == 'f' && src[1] == 'p')
272 *mode = PSIZE | REG | direction;
273 *reg = 6;
274 return len;
276 if (len == 3 && src[0] == 'e' && src[1] == 'r'
277 && src[2] >= '0' && src[2] <= '7')
279 *mode = L_32 | REG | direction;
280 *reg = src[2] - '0';
281 if (!Hmode)
282 as_warn (_("Reg not valid for H8/300"));
283 return len;
285 if (len == 2 && src[0] == 'e' && src[1] >= '0' && src[1] <= '7')
287 *mode = L_16 | REG | direction;
288 *reg = src[1] - '0' + 8;
289 if (!Hmode)
290 as_warn (_("Reg not valid for H8/300"));
291 return len;
294 if (src[0] == 'r')
296 if (src[1] >= '0' && src[1] <= '7')
298 if (len == 3 && src[2] == 'l')
300 *mode = L_8 | REG | direction;
301 *reg = (src[1] - '0') + 8;
302 return len;
304 if (len == 3 && src[2] == 'h')
306 *mode = L_8 | REG | direction;
307 *reg = (src[1] - '0');
308 return len;
310 if (len == 2)
312 *mode = L_16 | REG | direction;
313 *reg = (src[1] - '0');
314 return len;
319 return 0;
322 static char *
323 parse_exp (s, op)
324 char *s;
325 expressionS *op;
327 char *save = input_line_pointer;
328 char *new;
330 input_line_pointer = s;
331 expression (op);
332 if (op->X_op == O_absent)
333 as_bad (_("missing operand"));
334 new = input_line_pointer;
335 input_line_pointer = save;
336 return new;
339 static char *
340 skip_colonthing (ptr, exp, mode)
341 char *ptr;
342 expressionS *exp ATTRIBUTE_UNUSED;
343 int *mode;
345 if (*ptr == ':')
347 ptr++;
348 *mode &= ~SIZE;
349 if (*ptr == '8')
351 ptr++;
352 /* ff fill any 8 bit quantity. */
353 /* exp->X_add_number -= 0x100; */
354 *mode |= L_8;
356 else
358 if (*ptr == '2')
360 *mode |= L_24;
362 else if (*ptr == '3')
364 *mode |= L_32;
366 else if (*ptr == '1')
368 *mode |= L_16;
370 while (ISDIGIT (*ptr))
371 ptr++;
374 return ptr;
377 /* The many forms of operand:
379 Rn Register direct
380 @Rn Register indirect
381 @(exp[:16], Rn) Register indirect with displacement
382 @Rn+
383 @-Rn
384 @aa:8 absolute 8 bit
385 @aa:16 absolute 16 bit
386 @aa absolute 16 bit
388 #xx[:size] immediate data
389 @(exp:[8], pc) pc rel
390 @@aa[:8] memory indirect. */
392 char *
393 colonmod24 (op, src)
394 struct h8_op *op;
395 char *src;
397 int mode = 0;
398 src = skip_colonthing (src, &op->exp, &mode);
400 if (!mode)
402 /* Choose a default mode. */
403 if (op->exp.X_add_number < -32768
404 || op->exp.X_add_number > 32767)
406 if (Hmode)
407 mode = L_24;
408 else
409 mode = L_16;
411 else if (op->exp.X_add_symbol
412 || op->exp.X_op_symbol)
413 mode = DSYMMODE;
414 else
415 mode = DMODE;
418 op->mode |= mode;
419 return src;
422 static void
423 get_operand (ptr, op, dst, direction)
424 char **ptr;
425 struct h8_op *op;
426 unsigned int dst ATTRIBUTE_UNUSED;
427 int direction;
429 char *src = *ptr;
430 op_type mode;
431 unsigned int num;
432 unsigned int len;
434 op->mode = E;
436 /* Check for '(' and ')' for instructions ldm and stm. */
437 if (src[0] == '(' && src[8] == ')')
438 ++ src;
440 /* Gross. Gross. ldm and stm have a format not easily handled
441 by get_operand. We deal with it explicitly here. */
442 if (src[0] == 'e' && src[1] == 'r' && ISDIGIT (src[2])
443 && src[3] == '-' && src[4] == 'e' && src[5] == 'r' && ISDIGIT (src[6]))
445 int low, high;
447 low = src[2] - '0';
448 high = src[6] - '0';
450 if (high < low)
451 as_bad (_("Invalid register list for ldm/stm\n"));
453 if (low % 2)
454 as_bad (_("Invalid register list for ldm/stm\n"));
456 if (high - low > 3)
457 as_bad (_("Invalid register list for ldm/stm\n"));
459 if (high - low != 1
460 && low % 4)
461 as_bad (_("Invalid register list for ldm/stm\n"));
463 /* Even sicker. We encode two registers into op->reg. One
464 for the low register to save, the other for the high
465 register to save; we also set the high bit in op->reg
466 so we know this is "very special". */
467 op->reg = 0x80000000 | (high << 8) | low;
468 op->mode = REG;
469 if (src[7] == ')')
470 *ptr = src + 8;
471 else
472 *ptr = src + 7;
473 return;
476 len = parse_reg (src, &op->mode, &op->reg, direction);
477 if (len)
479 *ptr = src + len;
480 return;
483 if (*src == '@')
485 src++;
486 if (*src == '@')
488 src++;
489 src = parse_exp (src, &op->exp);
491 src = skip_colonthing (src, &op->exp, &op->mode);
493 *ptr = src;
495 op->mode = MEMIND;
496 return;
499 if (*src == '-')
501 src++;
502 len = parse_reg (src, &mode, &num, direction);
503 if (len == 0)
505 /* Oops, not a reg after all, must be ordinary exp. */
506 src--;
507 /* Must be a symbol. */
508 op->mode = ABS | PSIZE | direction;
509 *ptr = skip_colonthing (parse_exp (src, &op->exp),
510 &op->exp, &op->mode);
512 return;
515 if ((mode & SIZE) != PSIZE)
516 as_bad (_("Wrong size pointer register for architecture."));
517 op->mode = RDDEC;
518 op->reg = num;
519 *ptr = src + len;
520 return;
522 if (*src == '(')
524 /* Disp. */
525 src++;
527 /* Start off assuming a 16 bit offset. */
529 src = parse_exp (src, &op->exp);
531 src = colonmod24 (op, src);
533 if (*src == ')')
535 src++;
536 op->mode |= ABS | direction;
537 *ptr = src;
538 return;
541 if (*src != ',')
543 as_bad (_("expected @(exp, reg16)"));
544 return;
547 src++;
549 len = parse_reg (src, &mode, &op->reg, direction);
550 if (len == 0 || !(mode & REG))
552 as_bad (_("expected @(exp, reg16)"));
553 return;
555 op->mode |= DISP | direction;
556 dispreg = op->reg;
557 src += len;
558 src = skip_colonthing (src, &op->exp, &op->mode);
560 if (*src != ')' && '(')
562 as_bad (_("expected @(exp, reg16)"));
563 return;
565 *ptr = src + 1;
567 return;
569 len = parse_reg (src, &mode, &num, direction);
571 if (len)
573 src += len;
574 if (*src == '+')
576 src++;
577 if ((mode & SIZE) != PSIZE)
578 as_bad (_("Wrong size pointer register for architecture."));
579 op->mode = RSINC;
580 op->reg = num;
581 *ptr = src;
582 return;
584 if ((mode & SIZE) != PSIZE)
585 as_bad (_("Wrong size pointer register for architecture."));
587 op->mode = direction | IND | PSIZE;
588 op->reg = num;
589 *ptr = src;
591 return;
593 else
595 /* must be a symbol */
597 op->mode = ABS | direction;
598 src = parse_exp (src, &op->exp);
600 *ptr = colonmod24 (op, src);
602 return;
606 if (*src == '#')
608 src++;
609 op->mode = IMM;
610 src = parse_exp (src, &op->exp);
611 *ptr = skip_colonthing (src, &op->exp, &op->mode);
613 return;
615 else if (strncmp (src, "mach", 4) == 0
616 || strncmp (src, "macl", 4) == 0)
618 op->reg = src[3] == 'l';
619 op->mode = MACREG;
620 *ptr = src + 4;
621 return;
623 else
625 src = parse_exp (src, &op->exp);
626 /* Trailing ':' size ? */
627 if (*src == ':')
629 if (src[1] == '1' && src[2] == '6')
631 op->mode = PCREL | L_16;
632 src += 3;
634 else if (src[1] == '8')
636 op->mode = PCREL | L_8;
637 src += 2;
639 else
640 as_bad (_("expect :8 or :16 here"));
642 else
643 op->mode = PCREL | bsize;
645 *ptr = src;
649 static char *
650 get_operands (noperands, op_end, operand)
651 unsigned int noperands;
652 char *op_end;
653 struct h8_op *operand;
655 char *ptr = op_end;
657 switch (noperands)
659 case 0:
660 operand[0].mode = 0;
661 operand[1].mode = 0;
662 break;
664 case 1:
665 ptr++;
666 get_operand (&ptr, operand + 0, 0, SRC);
667 if (*ptr == ',')
669 ptr++;
670 get_operand (&ptr, operand + 1, 1, DST);
672 else
674 operand[1].mode = 0;
676 break;
678 case 2:
679 ptr++;
680 get_operand (&ptr, operand + 0, 0, SRC);
681 if (*ptr == ',')
682 ptr++;
683 get_operand (&ptr, operand + 1, 1, DST);
684 break;
686 default:
687 abort ();
690 return ptr;
693 /* Passed a pointer to a list of opcodes which use different
694 addressing modes, return the opcode which matches the opcodes
695 provided. */
697 static struct h8_opcode *
698 get_specific (opcode, operands, size)
699 struct h8_opcode *opcode;
700 struct h8_op *operands;
701 int size;
703 struct h8_opcode *this_try = opcode;
704 int found = 0;
705 int this_index = opcode->idx;
707 /* There's only one ldm/stm and it's easier to just
708 get out quick for them. */
709 if (strcmp (opcode->name, "stm.l") == 0
710 || strcmp (opcode->name, "ldm.l") == 0)
711 return this_try;
713 while (this_index == opcode->idx && !found)
715 found = 1;
717 this_try = opcode++;
718 if (this_try->noperands == 0)
720 int this_size;
722 this_size = this_try->how & SN;
723 if (this_size != size && (this_size != SB || size != SN))
724 found = 0;
726 else
728 int i;
730 for (i = 0; i < this_try->noperands && found; i++)
732 op_type op = this_try->args.nib[i];
733 int x = operands[i].mode;
735 if ((op & (DISP | REG)) == (DISP | REG)
736 && ((x & (DISP | REG)) == (DISP | REG)))
738 dispreg = operands[i].reg;
740 else if (op & REG)
742 if (!(x & REG))
743 found = 0;
745 if (x & L_P)
746 x = (x & ~L_P) | (Hmode ? L_32 : L_16);
747 if (op & L_P)
748 op = (op & ~L_P) | (Hmode ? L_32 : L_16);
750 opsize = op & SIZE;
752 /* The size of the reg is v important. */
753 if ((op & SIZE) != (x & SIZE))
754 found = 0;
756 else if ((op & ABSJMP) && (x & ABS))
758 operands[i].mode &= ~ABS;
759 operands[i].mode |= ABSJMP;
760 /* But it may not be 24 bits long. */
761 if (!Hmode)
763 operands[i].mode &= ~SIZE;
764 operands[i].mode |= L_16;
767 else if ((op & (KBIT | DBIT)) && (x & IMM))
769 /* This is ok if the immediate value is sensible. */
771 else if (op & PCREL)
773 /* The size of the displacement is important. */
774 if ((op & SIZE) != (x & SIZE))
775 found = 0;
777 else if ((op & (DISP | IMM | ABS))
778 && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
780 /* Promote a L_24 to L_32 if it makes us match. */
781 if ((x & L_24) && (op & L_32))
783 x &= ~L_24;
784 x |= L_32;
786 /* Promote an L8 to L_16 if it makes us match. */
787 if (op & ABS && op & L_8 && op & DISP)
789 if (x & L_16)
790 found = 1;
792 else if ((x & SIZE) != 0
793 && ((op & SIZE) != (x & SIZE)))
794 found = 0;
796 else if ((op & MACREG) != (x & MACREG))
798 found = 0;
800 else if ((op & MODE) != (x & MODE))
802 found = 0;
807 if (found)
808 return this_try;
809 else
810 return 0;
813 static void
814 check_operand (operand, width, string)
815 struct h8_op *operand;
816 unsigned int width;
817 char *string;
819 if (operand->exp.X_add_symbol == 0
820 && operand->exp.X_op_symbol == 0)
822 /* No symbol involved, let's look at offset, it's dangerous if
823 any of the high bits are not 0 or ff's, find out by oring or
824 anding with the width and seeing if the answer is 0 or all
825 fs. */
827 if ((operand->exp.X_add_number & ~width) != 0 &&
828 (operand->exp.X_add_number | width) != (unsigned)(~0))
830 if (width == 255
831 && (operand->exp.X_add_number & 0xff00) == 0xff00)
833 /* Just ignore this one - which happens when trying to
834 fit a 16 bit address truncated into an 8 bit address
835 of something like bset. */
837 else if (strcmp (string, "@") == 0
838 && width == 0xffff
839 && (operand->exp.X_add_number & 0xff8000) == 0xff8000)
841 /* Just ignore this one - which happens when trying to
842 fit a 24 bit address truncated into a 16 bit address
843 of something like mov.w. */
845 else
847 as_warn (_("operand %s0x%lx out of range."), string,
848 (unsigned long) operand->exp.X_add_number);
854 /* RELAXMODE has one of 3 values:
856 0 Output a "normal" reloc, no relaxing possible for this insn/reloc
858 1 Output a relaxable 24bit absolute mov.w address relocation
859 (may relax into a 16bit absolute address).
861 2 Output a relaxable 16/24 absolute mov.b address relocation
862 (may relax into an 8bit absolute address). */
864 static void
865 do_a_fix_imm (offset, operand, relaxmode)
866 int offset;
867 struct h8_op *operand;
868 int relaxmode;
870 int idx;
871 int size;
872 int where;
874 char *t = operand->mode & IMM ? "#" : "@";
876 if (operand->exp.X_add_symbol == 0)
878 char *bytes = frag_now->fr_literal + offset;
879 switch (operand->mode & SIZE)
881 case L_2:
882 check_operand (operand, 0x3, t);
883 bytes[0] |= (operand->exp.X_add_number) << 4;
884 break;
885 case L_3:
886 check_operand (operand, 0x7, t);
887 bytes[0] |= (operand->exp.X_add_number) << 4;
888 break;
889 case L_8:
890 check_operand (operand, 0xff, t);
891 bytes[0] = operand->exp.X_add_number;
892 break;
893 case L_16:
894 check_operand (operand, 0xffff, t);
895 bytes[0] = operand->exp.X_add_number >> 8;
896 bytes[1] = operand->exp.X_add_number >> 0;
897 break;
898 case L_24:
899 check_operand (operand, 0xffffff, t);
900 bytes[0] = operand->exp.X_add_number >> 16;
901 bytes[1] = operand->exp.X_add_number >> 8;
902 bytes[2] = operand->exp.X_add_number >> 0;
903 break;
905 case L_32:
906 /* This should be done with bfd. */
907 bytes[0] = operand->exp.X_add_number >> 24;
908 bytes[1] = operand->exp.X_add_number >> 16;
909 bytes[2] = operand->exp.X_add_number >> 8;
910 bytes[3] = operand->exp.X_add_number >> 0;
911 if (relaxmode != 0)
913 idx = (relaxmode == 2) ? R_MOV24B1 : R_MOVL1;
914 fix_new_exp (frag_now, offset, 4, &operand->exp, 0, idx);
916 break;
919 else
921 switch (operand->mode & SIZE)
923 case L_24:
924 case L_32:
925 size = 4;
926 where = (operand->mode & SIZE) == L_24 ? -1 : 0;
927 if (relaxmode == 2)
928 idx = R_MOV24B1;
929 else if (relaxmode == 1)
930 idx = R_MOVL1;
931 else
932 idx = R_RELLONG;
933 break;
934 default:
935 as_bad (_("Can't work out size of operand.\n"));
936 case L_16:
937 size = 2;
938 where = 0;
939 if (relaxmode == 2)
940 idx = R_MOV16B1;
941 else
942 idx = R_RELWORD;
943 operand->exp.X_add_number =
944 ((operand->exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
945 break;
946 case L_8:
947 size = 1;
948 where = 0;
949 idx = R_RELBYTE;
950 operand->exp.X_add_number =
951 ((operand->exp.X_add_number & 0xff) ^ 0x80) - 0x80;
954 fix_new_exp (frag_now,
955 offset + where,
956 size,
957 &operand->exp,
959 idx);
963 /* Now we know what sort of opcodes it is, let's build the bytes. */
965 static void
966 build_bytes (this_try, operand)
967 struct h8_opcode *this_try;
968 struct h8_op *operand;
970 int i;
971 char *output = frag_more (this_try->length);
972 op_type *nibble_ptr = this_try->data.nib;
973 op_type c;
974 unsigned int nibble_count = 0;
975 int absat = 0;
976 int immat = 0;
977 int nib = 0;
978 int movb = 0;
979 char asnibbles[30];
980 char *p = asnibbles;
982 if (!(this_try->inbase || Hmode))
983 as_warn (_("Opcode `%s' with these operand types not available in H8/300 mode"),
984 this_try->name);
986 while (*nibble_ptr != E)
988 int d;
989 c = *nibble_ptr++;
991 d = (c & (DST | SRC_IN_DST)) != 0;
993 if (c < 16)
994 nib = c;
995 else
997 if (c & (REG | IND | INC | DEC))
998 nib = operand[d].reg;
1000 else if ((c & DISPREG) == (DISPREG))
1001 nib = dispreg;
1003 else if (c & ABS)
1005 operand[d].mode = c;
1006 absat = nibble_count / 2;
1007 nib = 0;
1009 else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
1011 operand[d].mode = c;
1012 immat = nibble_count / 2;
1013 nib = 0;
1015 else if (c & IGNORE)
1016 nib = 0;
1018 else if (c & DBIT)
1020 switch (operand[0].exp.X_add_number)
1022 case 1:
1023 nib = c;
1024 break;
1025 case 2:
1026 nib = 0x8 | c;
1027 break;
1028 default:
1029 as_bad (_("Need #1 or #2 here"));
1032 else if (c & KBIT)
1034 switch (operand[0].exp.X_add_number)
1036 case 1:
1037 nib = 0;
1038 break;
1039 case 2:
1040 nib = 8;
1041 break;
1042 case 4:
1043 if (!Hmode)
1044 as_warn (_("#4 not valid on H8/300."));
1045 nib = 9;
1046 break;
1048 default:
1049 as_bad (_("Need #1 or #2 here"));
1050 break;
1052 /* Stop it making a fix. */
1053 operand[0].mode = 0;
1056 if (c & MEMRELAX)
1057 operand[d].mode |= MEMRELAX;
1059 if (c & B31)
1060 nib |= 0x8;
1062 if (c & MACREG)
1064 if (operand[0].mode == MACREG)
1065 /* stmac has mac[hl] as the first operand. */
1066 nib = 2 + operand[0].reg;
1067 else
1068 /* ldmac has mac[hl] as the second operand. */
1069 nib = 2 + operand[1].reg;
1072 nibble_count++;
1074 *p++ = nib;
1077 /* Disgusting. Why, oh why didn't someone ask us for advice
1078 on the assembler format. */
1079 if (strcmp (this_try->name, "stm.l") == 0
1080 || strcmp (this_try->name, "ldm.l") == 0)
1082 int high, low;
1083 high = (operand[this_try->name[0] == 'l' ? 1 : 0].reg >> 8) & 0xf;
1084 low = operand[this_try->name[0] == 'l' ? 1 : 0].reg & 0xf;
1086 asnibbles[2] = high - low;
1087 asnibbles[7] = (this_try->name[0] == 'l') ? high : low;
1090 for (i = 0; i < this_try->length; i++)
1091 output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
1093 /* Note if this is a movb instruction -- there's a special relaxation
1094 which only applies to them. */
1095 if (strcmp (this_try->name, "mov.b") == 0)
1096 movb = 1;
1098 /* Output any fixes. */
1099 for (i = 0; i < 2; i++)
1101 int x = operand[i].mode;
1103 if (x & (IMM | DISP))
1104 do_a_fix_imm (output - frag_now->fr_literal + immat,
1105 operand + i, (x & MEMRELAX) != 0);
1107 else if (x & ABS)
1108 do_a_fix_imm (output - frag_now->fr_literal + absat,
1109 operand + i, (x & MEMRELAX) ? movb + 1 : 0);
1111 else if (x & PCREL)
1113 int size16 = x & (L_16);
1114 int where = size16 ? 2 : 1;
1115 int size = size16 ? 2 : 1;
1116 int type = size16 ? R_PCRWORD : R_PCRBYTE;
1117 fixS *fixP;
1119 check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1121 if (operand[i].exp.X_add_number & 1)
1122 as_warn (_("branch operand has odd offset (%lx)\n"),
1123 (unsigned long) operand->exp.X_add_number);
1124 #ifndef OBJ_ELF
1125 /* The COFF port has always been off by one, changing it
1126 now would be an incompatible change, so we leave it as-is.
1128 We don't want to do this for ELF as we want to be
1129 compatible with the proposed ELF format from Hitachi. */
1130 operand[i].exp.X_add_number -= 1;
1131 #endif
1132 operand[i].exp.X_add_number =
1133 ((operand[i].exp.X_add_number & 0xff) ^ 0x80) - 0x80;
1135 fixP = fix_new_exp (frag_now,
1136 output - frag_now->fr_literal + where,
1137 size,
1138 &operand[i].exp,
1140 type);
1141 fixP->fx_signed = 1;
1143 else if (x & MEMIND)
1145 check_operand (operand + i, 0xff, "@@");
1146 fix_new_exp (frag_now,
1147 output - frag_now->fr_literal + 1,
1149 &operand[i].exp,
1151 R_MEM_INDIRECT);
1153 else if (x & ABSJMP)
1155 int where = 0;
1157 #ifdef OBJ_ELF
1158 /* To be compatible with the proposed H8 ELF format, we
1159 want the relocation's offset to point to the first byte
1160 that will be modified, not to the start of the instruction. */
1161 where += 1;
1162 #endif
1164 /* This jmp may be a jump or a branch. */
1166 check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
1168 if (operand[i].exp.X_add_number & 1)
1169 as_warn (_("branch operand has odd offset (%lx)\n"),
1170 (unsigned long) operand->exp.X_add_number);
1172 if (!Hmode)
1173 operand[i].exp.X_add_number =
1174 ((operand[i].exp.X_add_number & 0xffff) ^ 0x8000) - 0x8000;
1175 fix_new_exp (frag_now,
1176 output - frag_now->fr_literal + where,
1178 &operand[i].exp,
1180 R_JMPL1);
1185 /* Try to give an intelligent error message for common and simple to
1186 detect errors. */
1188 static void
1189 clever_message (opcode, operand)
1190 struct h8_opcode *opcode;
1191 struct h8_op *operand;
1193 /* Find out if there was more than one possible opcode. */
1195 if ((opcode + 1)->idx != opcode->idx)
1197 int argn;
1199 /* Only one opcode of this flavour, try to guess which operand
1200 didn't match. */
1201 for (argn = 0; argn < opcode->noperands; argn++)
1203 switch (opcode->args.nib[argn])
1205 case RD16:
1206 if (operand[argn].mode != RD16)
1208 as_bad (_("destination operand must be 16 bit register"));
1209 return;
1212 break;
1214 case RS8:
1215 if (operand[argn].mode != RS8)
1217 as_bad (_("source operand must be 8 bit register"));
1218 return;
1220 break;
1222 case ABS16DST:
1223 if (operand[argn].mode != ABS16DST)
1225 as_bad (_("destination operand must be 16bit absolute address"));
1226 return;
1228 break;
1229 case RD8:
1230 if (operand[argn].mode != RD8)
1232 as_bad (_("destination operand must be 8 bit register"));
1233 return;
1235 break;
1237 case ABS16SRC:
1238 if (operand[argn].mode != ABS16SRC)
1240 as_bad (_("source operand must be 16bit absolute address"));
1241 return;
1243 break;
1248 as_bad (_("invalid operands"));
1251 /* This is the guts of the machine-dependent assembler. STR points to
1252 a machine dependent instruction. This function is supposed to emit
1253 the frags/bytes it assembles. */
1255 void
1256 md_assemble (str)
1257 char *str;
1259 char *op_start;
1260 char *op_end;
1261 struct h8_op operand[2];
1262 struct h8_opcode *opcode;
1263 struct h8_opcode *prev_opcode;
1265 char *dot = 0;
1266 char c;
1267 int size;
1269 /* Drop leading whitespace. */
1270 while (*str == ' ')
1271 str++;
1273 /* Find the op code end. */
1274 for (op_start = op_end = str;
1275 *op_end != 0 && *op_end != ' ';
1276 op_end++)
1278 if (*op_end == '.')
1280 dot = op_end + 1;
1281 *op_end = 0;
1282 op_end += 2;
1283 break;
1287 if (op_end == op_start)
1289 as_bad (_("can't find opcode "));
1291 c = *op_end;
1293 *op_end = 0;
1295 opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
1296 op_start);
1298 if (opcode == NULL)
1300 as_bad (_("unknown opcode"));
1301 return;
1304 /* We used to set input_line_pointer to the result of get_operands,
1305 but that is wrong. Our caller assumes we don't change it. */
1307 (void) get_operands (opcode->noperands, op_end, operand);
1308 *op_end = c;
1309 prev_opcode = opcode;
1311 size = SN;
1312 if (dot)
1314 switch (*dot)
1316 case 'b':
1317 size = SB;
1318 break;
1320 case 'w':
1321 size = SW;
1322 break;
1324 case 'l':
1325 size = SL;
1326 break;
1329 opcode = get_specific (opcode, operand, size);
1331 if (opcode == 0)
1333 /* Couldn't find an opcode which matched the operands. */
1334 char *where = frag_more (2);
1336 where[0] = 0x0;
1337 where[1] = 0x0;
1338 clever_message (prev_opcode, operand);
1340 return;
1342 if (opcode->size && dot)
1344 if (opcode->size != *dot)
1346 as_warn (_("mismatch between opcode size and operand size"));
1350 build_bytes (opcode, operand);
1353 #ifndef BFD_ASSEMBLER
1354 void
1355 tc_crawl_symbol_chain (headers)
1356 object_headers *headers ATTRIBUTE_UNUSED;
1358 printf (_("call to tc_crawl_symbol_chain \n"));
1360 #endif
1362 symbolS *
1363 md_undefined_symbol (name)
1364 char *name ATTRIBUTE_UNUSED;
1366 return 0;
1369 #ifndef BFD_ASSEMBLER
1370 void
1371 tc_headers_hook (headers)
1372 object_headers *headers ATTRIBUTE_UNUSED;
1374 printf (_("call to tc_headers_hook \n"));
1376 #endif
1378 /* Various routines to kill one day */
1379 /* Equal to MAX_PRECISION in atof-ieee.c */
1380 #define MAX_LITTLENUMS 6
1382 /* Turn a string in input_line_pointer into a floating point constant
1383 of type TYPE, and store the appropriate bytes in *LITP. The number
1384 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1385 returned, or NULL on OK. */
1387 char *
1388 md_atof (type, litP, sizeP)
1389 char type;
1390 char *litP;
1391 int *sizeP;
1393 int prec;
1394 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1395 LITTLENUM_TYPE *wordP;
1396 char *t;
1398 switch (type)
1400 case 'f':
1401 case 'F':
1402 case 's':
1403 case 'S':
1404 prec = 2;
1405 break;
1407 case 'd':
1408 case 'D':
1409 case 'r':
1410 case 'R':
1411 prec = 4;
1412 break;
1414 case 'x':
1415 case 'X':
1416 prec = 6;
1417 break;
1419 case 'p':
1420 case 'P':
1421 prec = 6;
1422 break;
1424 default:
1425 *sizeP = 0;
1426 return _("Bad call to MD_ATOF()");
1428 t = atof_ieee (input_line_pointer, type, words);
1429 if (t)
1430 input_line_pointer = t;
1432 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1433 for (wordP = words; prec--;)
1435 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1436 litP += sizeof (LITTLENUM_TYPE);
1438 return 0;
1441 const char *md_shortopts = "";
1442 struct option md_longopts[] = {
1443 {NULL, no_argument, NULL, 0}
1446 size_t md_longopts_size = sizeof (md_longopts);
1449 md_parse_option (c, arg)
1450 int c ATTRIBUTE_UNUSED;
1451 char *arg ATTRIBUTE_UNUSED;
1453 return 0;
1456 void
1457 md_show_usage (stream)
1458 FILE *stream ATTRIBUTE_UNUSED;
1462 void tc_aout_fix_to_chars PARAMS ((void));
1464 void
1465 tc_aout_fix_to_chars ()
1467 printf (_("call to tc_aout_fix_to_chars \n"));
1468 abort ();
1471 void
1472 md_convert_frag (headers, seg, fragP)
1473 #ifdef BFD_ASSEMBLER
1474 bfd *headers ATTRIBUTE_UNUSED;
1475 #else
1476 object_headers *headers ATTRIBUTE_UNUSED;
1477 #endif
1478 segT seg ATTRIBUTE_UNUSED;
1479 fragS *fragP ATTRIBUTE_UNUSED;
1481 printf (_("call to md_convert_frag \n"));
1482 abort ();
1485 #ifdef BFD_ASSEMBLER
1486 valueT
1487 md_section_align (segment, size)
1488 segT segment;
1489 valueT size;
1491 int align = bfd_get_section_alignment (stdoutput, segment);
1492 return ((size + (1 << align) - 1) & (-1 << align));
1494 #else
1495 valueT
1496 md_section_align (seg, size)
1497 segT seg;
1498 valueT size;
1500 return ((size + (1 << section_alignment[(int) seg]) - 1)
1501 & (-1 << section_alignment[(int) seg]));
1503 #endif
1506 void
1507 md_apply_fix3 (fixP, valP, seg)
1508 fixS *fixP;
1509 valueT *valP;
1510 segT seg ATTRIBUTE_UNUSED;
1512 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1513 long val = * (long *) valP;
1515 switch (fixP->fx_size)
1517 case 1:
1518 *buf++ = val;
1519 break;
1520 case 2:
1521 *buf++ = (val >> 8);
1522 *buf++ = val;
1523 break;
1524 case 4:
1525 *buf++ = (val >> 24);
1526 *buf++ = (val >> 16);
1527 *buf++ = (val >> 8);
1528 *buf++ = val;
1529 break;
1530 default:
1531 abort ();
1534 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1535 fixP->fx_done = 1;
1539 md_estimate_size_before_relax (fragP, segment_type)
1540 register fragS *fragP ATTRIBUTE_UNUSED;
1541 register segT segment_type ATTRIBUTE_UNUSED;
1543 printf (_("call tomd_estimate_size_before_relax \n"));
1544 abort ();
1547 /* Put number into target byte order. */
1548 void
1549 md_number_to_chars (ptr, use, nbytes)
1550 char *ptr;
1551 valueT use;
1552 int nbytes;
1554 number_to_chars_bigendian (ptr, use, nbytes);
1557 long
1558 md_pcrel_from (fixP)
1559 fixS *fixP ATTRIBUTE_UNUSED;
1561 abort ();
1564 #ifndef BFD_ASSEMBLER
1565 void
1566 tc_reloc_mangle (fix_ptr, intr, base)
1567 fixS *fix_ptr;
1568 struct internal_reloc *intr;
1569 bfd_vma base;
1572 symbolS *symbol_ptr;
1574 symbol_ptr = fix_ptr->fx_addsy;
1576 /* If this relocation is attached to a symbol then it's ok
1577 to output it. */
1578 if (fix_ptr->fx_r_type == TC_CONS_RELOC)
1580 /* cons likes to create reloc32's whatever the size of the reloc..
1582 switch (fix_ptr->fx_size)
1584 case 4:
1585 intr->r_type = R_RELLONG;
1586 break;
1587 case 2:
1588 intr->r_type = R_RELWORD;
1589 break;
1590 case 1:
1591 intr->r_type = R_RELBYTE;
1592 break;
1593 default:
1594 abort ();
1597 else
1599 intr->r_type = fix_ptr->fx_r_type;
1602 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1603 intr->r_offset = fix_ptr->fx_offset;
1605 if (symbol_ptr)
1607 if (symbol_ptr->sy_number != -1)
1608 intr->r_symndx = symbol_ptr->sy_number;
1609 else
1611 symbolS *segsym;
1613 /* This case arises when a reference is made to `.'. */
1614 segsym = seg_info (S_GET_SEGMENT (symbol_ptr))->dot;
1615 if (segsym == NULL)
1616 intr->r_symndx = -1;
1617 else
1619 intr->r_symndx = segsym->sy_number;
1620 intr->r_offset += S_GET_VALUE (symbol_ptr);
1624 else
1625 intr->r_symndx = -1;
1627 #else /* BFD_ASSEMBLER */
1628 arelent *
1629 tc_gen_reloc (section, fixp)
1630 asection *section ATTRIBUTE_UNUSED;
1631 fixS *fixp;
1633 arelent *rel;
1634 bfd_reloc_code_real_type r_type;
1636 if (fixp->fx_addsy && fixp->fx_subsy)
1638 if ((S_GET_SEGMENT (fixp->fx_addsy) != S_GET_SEGMENT (fixp->fx_subsy))
1639 || S_GET_SEGMENT (fixp->fx_addsy) == undefined_section)
1641 as_bad_where (fixp->fx_file, fixp->fx_line,
1642 "Difference of symbols in different sections is not supported");
1643 return NULL;
1647 rel = (arelent *) xmalloc (sizeof (arelent));
1648 rel->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1649 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1650 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
1651 rel->addend = fixp->fx_offset;
1653 r_type = fixp->fx_r_type;
1655 #define DEBUG 0
1656 #if DEBUG
1657 fprintf (stderr, "%s\n", bfd_get_reloc_code_name (r_type));
1658 fflush(stderr);
1659 #endif
1660 rel->howto = bfd_reloc_type_lookup (stdoutput, r_type);
1661 if (rel->howto == NULL)
1663 as_bad_where (fixp->fx_file, fixp->fx_line,
1664 _("Cannot represent relocation type %s"),
1665 bfd_get_reloc_code_name (r_type));
1666 return NULL;
1669 return rel;
1671 #endif