1 /* tc-h8300.c -- Assemble code for the Hitachi H8/300
2 Copyright (C) 1991, 1992 Free Software Foundation.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 Written By Steve Chamberlain
30 #define h8_opcodes ops
31 #include "opcode/h8300.h"
34 const char comment_chars[] =
36 const char line_separator_chars[] =
38 const char line_comment_chars[] = "#";
40 /* This table describes all the machine specific pseudo-ops the assembler
41 has to support. The fields are:
42 pseudo-op name without dot
43 function to call to execute this pseudo-op
44 Integer arg to pass to the function
51 #define PSIZE (Hmode ? L_32 : L_16)
53 #define DSYMMODE (Hmode ? L_24 : L_16)
54 int bsize = L_8; /* default branch displacement */
82 const pseudo_typeS md_pseudo_table[] =
85 {"h8300h", h8300hmode, 0},
86 {"h8300s", h8300smode, 0},
87 {"sbranch", sbranch, L_8},
88 {"lbranch", sbranch, L_16},
94 {"form", listing_psize, 0},
95 {"heading", listing_title, 0},
96 {"import", s_ignore, 0},
97 {"page", listing_eject, 0},
98 {"program", s_ignore, 0},
102 const int md_reloc_size;
104 const char EXP_CHARS[] = "eE";
106 /* Chars that mean this number is a floating point constant */
109 const char FLT_CHARS[] = "rRsSfFdDxXpP";
111 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
114 This function is called once, at assembler startup time. This should
115 set up all the tables, etc that the MD part of the assembler needs
122 struct h8_opcode *opcode;
123 char prev_buffer[100];
126 opcode_hash_control = hash_new ();
129 for (opcode = h8_opcodes; opcode->name; opcode++)
131 /* Strip off any . part when inserting the opcode and only enter
132 unique codes into the hash table
134 char *src = opcode->name;
135 unsigned int len = strlen (src);
136 char *dst = malloc (len + 1);
151 if (strcmp (buffer, prev_buffer))
153 hash_insert (opcode_hash_control, buffer, (char *) opcode);
154 strcpy (prev_buffer, buffer);
160 /* Find the number of operands */
161 opcode->noperands = 0;
162 while (opcode->args.nib[opcode->noperands] != E)
164 /* Find the length of the opcode in bytes */
166 while (opcode->data.nib[opcode->length * 2] != E)
181 int opsize; /* Set when a register size is seen */
193 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
202 /* try and parse a reg name, returns number of chars consumed */
204 parse_reg (src, mode, reg, direction)
211 if (src[0] == 's' && src[1] == 'p')
213 *mode = PSIZE | REG | direction;
217 if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
223 if (src[0] == 'e' && src[1] == 'x' && src[2] == 'r')
229 if (src[0] == 'f' && src[1] == 'p')
231 *mode = PSIZE | REG | direction;
237 && src[2] >= '0' && src[2] <= '7')
239 *mode = L_32 | REG | direction;
242 as_warn ("Reg not valid for H8/300");
247 && src[1] >= '0' && src[1] <= '7')
249 *mode = L_16 | REG | direction;
250 *reg = src[1] - '0' + 8;
252 as_warn ("Reg not valid for H8/300");
258 if (src[1] >= '0' && src[1] <= '7')
262 *mode = L_8 | REG | direction;
263 *reg = (src[1] - '0') + 8;
268 *mode = L_8 | REG | direction;
269 *reg = (src[1] - '0');
272 *mode = L_16 | REG | direction;
273 *reg = (src[1] - '0');
285 char *save = input_line_pointer;
288 input_line_pointer = s;
290 if (op->X_op == O_absent)
291 as_bad ("missing operand");
292 new = input_line_pointer;
293 input_line_pointer = save;
298 skip_colonthing (ptr, exp, mode)
310 /* ff fill any 8 bit quantity */
311 /* exp->X_add_number -= 0x100;*/
320 else if (*ptr == '3')
324 else if (*ptr == '1')
328 while (isdigit (*ptr))
335 /* The many forms of operand:
338 @Rn Register indirect
339 @(exp[:16], Rn) Register indirect with displacement
343 @aa:16 absolute 16 bit
346 #xx[:size] immediate data
347 @(exp:[8], pc) pc rel
348 @@aa[:8] memory indirect
359 src = skip_colonthing (src, &op->exp, &mode);
363 /* Choose a default mode */
364 if (op->exp.X_add_number < -32768
365 || op->exp.X_add_number > 32767)
372 else if (op->exp.X_add_symbol
373 || op->exp.X_op_symbol)
385 get_operand (ptr, op, dst, direction)
398 /* Gross. Gross. ldm and stm have a format not easily handled
399 by get_operand. We deal with it explicitly here. */
400 if (src[0] == 'e' && src[1] == 'r' && isdigit(src[2])
401 && src[3] == '-' && src[4] == 'e' && src[5] == 'r' && isdigit(src[6]))
409 as_bad ("Invalid register list for ldm/stm\n");
412 as_bad ("Invalid register list for ldm/stm\n");
415 as_bad ("Invalid register list for ldm/stm\n");
419 as_bad ("Invalid register list for ldm/stm\n");
421 /* Even sicker. We encode two registers into op->reg. One
422 for the low register to save, the other for the high
423 register to save; we also set the high bit in op->reg
424 so we know this is "very special". */
425 op->reg = 0x80000000 | (high << 8) | low;
431 len = parse_reg (src, &op->mode, &op->reg, direction);
444 src = parse_exp (src, &op->exp);
446 src = skip_colonthing (src, &op->exp, &op->mode);
459 len = parse_reg (src, &mode, &num, direction);
462 /* Oops, not a reg after all, must be ordinary exp */
464 /* must be a symbol */
465 op->mode = ABS | PSIZE | direction;
466 *ptr = skip_colonthing (parse_exp (src, &op->exp),
467 &op->exp, &op->mode);
475 if ((mode & SIZE) != PSIZE)
476 as_bad ("Wrong size pointer register for architecture.");
487 /* Start off assuming a 16 bit offset */
490 src = parse_exp (src, &op->exp);
492 src = colonmod24 (op, src);
497 op->mode |= ABS | direction;
504 as_bad ("expected @(exp, reg16)");
510 len = parse_reg (src, &mode, &op->reg, direction);
511 if (len == 0 || !(mode & REG))
513 as_bad ("expected @(exp, reg16)");
516 op->mode |= DISP | direction;
519 src = skip_colonthing (src, &op->exp, &op->mode);
521 if (*src != ')' && '(')
523 as_bad ("expected @(exp, reg16)");
530 len = parse_reg (src, &mode, &num, direction);
538 if ((mode & SIZE) != PSIZE)
539 as_bad ("Wrong size pointer register for architecture.");
545 if ((mode & SIZE) != PSIZE)
546 as_bad ("Wrong size pointer register for architecture.");
548 op->mode = direction | IND | PSIZE;
556 /* must be a symbol */
558 op->mode = ABS | direction;
559 src = parse_exp (src, &op->exp);
561 *ptr = colonmod24 (op, src);
572 src = parse_exp (src, &op->exp);
573 *ptr = skip_colonthing (src, &op->exp, &op->mode);
577 else if (strncmp (src, "mach", 4) == 0
578 || strncmp (src, "macl", 4) == 0)
580 op->reg = src[3] == 'l';
587 src = parse_exp (src, &op->exp);
588 /* Trailing ':' size ? */
591 if (src[1] == '1' && src[2] == '6')
593 op->mode = PCREL | L_16;
596 else if (src[1] == '8')
598 op->mode = PCREL | L_8;
603 as_bad ("expect :8 or :16 here");
608 op->mode = PCREL | bsize;
617 get_operands (noperands, op_end, operand)
618 unsigned int noperands;
620 struct h8_op *operand;
633 get_operand (&ptr, operand + 0, 0, SRC);
637 get_operand (&ptr, operand + 1, 1, DST);
647 get_operand (&ptr, operand + 0, 0, SRC);
650 get_operand (&ptr, operand + 1, 1, DST);
661 /* Passed a pointer to a list of opcodes which use different
662 addressing modes, return the opcode which matches the opcodes
667 get_specific (opcode, operands)
668 struct h8_opcode *opcode;
669 struct h8_op *operands;
671 struct h8_opcode *this_try = opcode;
674 unsigned int this_index = opcode->idx;
676 /* There's only one ldm/stm and it's easier to just
677 get out quick for them. */
678 if (strcmp (opcode->name, "stm.l") == 0
679 || strcmp (opcode->name, "ldm.l") == 0)
682 while (this_index == opcode->idx && !found)
688 for (i = 0; i < this_try->noperands && found; i++)
690 op_type op = this_try->args.nib[i];
691 int x = operands[i].mode;
693 if ((op & (DISP | REG)) == (DISP | REG)
694 && ((x & (DISP | REG)) == (DISP | REG)))
696 dispreg = operands[i].reg;
705 x = (x & ~L_P) | (Hmode ? L_32 : L_16);
709 op = (op & ~L_P) | (Hmode ? L_32 : L_16);
714 /* The size of the reg is v important */
715 if ((op & SIZE) != (x & SIZE))
718 else if ((op & ABSJMP) && (x & ABS))
720 operands[i].mode &= ~ABS;
721 operands[i].mode |= ABSJMP;
722 /* But it may not be 24 bits long */
725 operands[i].mode &= ~SIZE;
726 operands[i].mode |= L_16;
731 else if ((op & (KBIT | DBIT)) && (x & IMM))
733 /* This is ok if the immediate value is sensible */
739 /* The size of the displacement is important */
740 if ((op & SIZE) != (x & SIZE))
744 else if ((op & (DISP | IMM | ABS))
745 && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
747 /* Promote a L_24 to L_32 if it makes us match. */
748 if ((x & L_24) && (op & L_32))
753 /* Promote an L8 to L_16 if it makes us match. */
754 if (op & ABS && op & L_8 && op & DISP)
759 else if ((x & SIZE) != 0
760 && ((op & SIZE) != (x & SIZE)))
763 else if ((op & MACREG) != (x & MACREG))
767 else if ((op & MODE) != (x & MODE))
781 check_operand (operand, width, string)
782 struct h8_op *operand;
786 if (operand->exp.X_add_symbol == 0
787 && operand->exp.X_op_symbol == 0)
790 /* No symbol involved, let's look at offset, it's dangerous if any of
791 the high bits are not 0 or ff's, find out by oring or anding with
792 the width and seeing if the answer is 0 or all fs*/
794 if ((operand->exp.X_add_number & ~width) != 0 &&
795 (operand->exp.X_add_number | width) != (~0))
798 && (operand->exp.X_add_number & 0xff00) == 0xff00)
800 /* Just ignore this one - which happens when trying to
801 fit a 16 bit address truncated into an 8 bit address
802 of something like bset. */
806 as_warn ("operand %s0x%lx out of range.", string,
807 (unsigned long) operand->exp.X_add_number);
814 /* RELAXMODE has one of 3 values:
816 0 Output a "normal" reloc, no relaxing possible for this insn/reloc
818 1 Output a relaxable 24bit absolute mov.w address relocation
819 (may relax into a 16bit absolute address).
821 2 Output a relaxable 16/24 absolute mov.b address relocation
822 (may relax into an 8bit absolute address). */
825 do_a_fix_imm (offset, operand, relaxmode)
827 struct h8_op *operand;
835 char *t = operand->mode & IMM ? "#" : "@";
837 if (operand->exp.X_add_symbol == 0)
839 char *bytes = frag_now->fr_literal + offset;
840 switch (operand->mode & SIZE)
843 check_operand (operand, 0x3, t);
844 bytes[0] |= (operand->exp.X_add_number) << 4;
847 check_operand (operand, 0x7, t);
848 bytes[0] |= (operand->exp.X_add_number) << 4;
851 check_operand (operand, 0xff, t);
852 bytes[0] = operand->exp.X_add_number;
855 check_operand (operand, 0xffff, t);
856 bytes[0] = operand->exp.X_add_number >> 8;
857 bytes[1] = operand->exp.X_add_number >> 0;
860 check_operand (operand, 0xffffff, t);
861 bytes[0] = operand->exp.X_add_number >> 16;
862 bytes[1] = operand->exp.X_add_number >> 8;
863 bytes[2] = operand->exp.X_add_number >> 0;
867 /* This should be done with bfd */
868 bytes[0] = operand->exp.X_add_number >> 24;
869 bytes[1] = operand->exp.X_add_number >> 16;
870 bytes[2] = operand->exp.X_add_number >> 8;
871 bytes[3] = operand->exp.X_add_number >> 0;
878 switch (operand->mode & SIZE)
884 where = (operand->mode & SIZE) == L_24 ? -1 : 0;
887 else if (relaxmode == 1)
893 as_bad("Can't work out size of operand.\n");
901 operand->exp.X_add_number = (short)operand->exp.X_add_number;
907 operand->exp.X_add_number = (char)operand->exp.X_add_number;
910 fix_new_exp (frag_now,
920 /* Now we know what sort of opcodes it is, lets build the bytes -
923 build_bytes (this_try, operand)
924 struct h8_opcode *this_try;
925 struct h8_op *operand;
929 char *output = frag_more (this_try->length);
930 op_type *nibble_ptr = this_try->data.nib;
932 unsigned int nibble_count = 0;
940 if (!(this_try->inbase || Hmode))
941 as_warn ("Opcode `%s' not available in H8/300 mode", this_try->name);
943 while (*nibble_ptr != E)
948 d = (c & (DST | SRC_IN_DST)) != 0;
957 if (c & (REG | IND | INC | DEC))
959 nib = operand[d].reg;
961 else if ((c & DISPREG) == (DISPREG))
968 absat = nibble_count / 2;
971 else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
974 immat = nibble_count / 2;
983 switch (operand[0].exp.X_add_number)
992 as_bad ("Need #1 or #2 here");
997 switch (operand[0].exp.X_add_number)
1007 as_warn ("#4 not valid on H8/300.");
1012 as_bad ("Need #1 or #2 here");
1015 /* stop it making a fix */
1016 operand[0].mode = 0;
1021 operand[d].mode |= MEMRELAX;
1031 nib = 2 + operand[d].reg;
1039 /* Disgusting. Why, oh why didn't someone ask us for advice
1040 on the assembler format. */
1041 if (strcmp (this_try->name, "stm.l") == 0
1042 || strcmp (this_try->name, "ldm.l") == 0)
1045 high = (operand[this_try->name[0] == 'l' ? 1 : 0].reg >> 8) & 0xf;
1046 low = operand[this_try->name[0] == 'l' ? 1 : 0].reg & 0xf;
1048 asnibbles[2] = high - low;
1049 asnibbles[7] = (this_try->name[0] == 'l') ? high : low;
1052 for (i = 0; i < this_try->length; i++)
1054 output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
1057 /* Note if this is a movb instruction -- there's a special relaxation
1058 which only applies to them. */
1059 if (strcmp (this_try->name, "mov.b") == 0)
1062 /* output any fixes */
1063 for (i = 0; i < 2; i++)
1065 int x = operand[i].mode;
1067 if (x & (IMM | DISP))
1069 do_a_fix_imm (output - frag_now->fr_literal + immat,
1070 operand + i, x & MEMRELAX != 0);
1074 do_a_fix_imm (output - frag_now->fr_literal + absat,
1075 operand + i, x & MEMRELAX ? movb + 1 : 0);
1079 int size16 = x & L_16;
1080 int where = size16 ? 2 : 1;
1081 int size = size16 ? 2 : 1;
1082 int type = size16 ? R_PCRWORD : R_PCRBYTE;
1084 check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1086 if (operand[i].exp.X_add_number & 1)
1088 as_warn ("branch operand has odd offset (%lx)\n",
1089 (unsigned long) operand->exp.X_add_number);
1092 operand[i].exp.X_add_number =
1093 (char) (operand[i].exp.X_add_number - 1);
1094 fix_new_exp (frag_now,
1095 output - frag_now->fr_literal + where,
1101 else if (x & MEMIND)
1104 check_operand (operand + i, 0xff, "@@");
1105 fix_new_exp (frag_now,
1106 output - frag_now->fr_literal + 1,
1112 else if (x & ABSJMP)
1114 /* This jmp may be a jump or a branch */
1116 check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
1117 if (operand[i].exp.X_add_number & 1)
1119 as_warn ("branch operand has odd offset (%lx)\n",
1120 (unsigned long) operand->exp.X_add_number);
1123 operand[i].exp.X_add_number = (short) operand[i].exp.X_add_number;
1124 fix_new_exp (frag_now,
1125 output - frag_now->fr_literal,
1136 try and give an intelligent error message for common and simple to
1141 clever_message (opcode, operand)
1142 struct h8_opcode *opcode;
1143 struct h8_op *operand;
1145 /* Find out if there was more than one possible opccode */
1147 if ((opcode + 1)->idx != opcode->idx)
1151 /* Only one opcode of this flavour, try and guess which operand
1153 for (argn = 0; argn < opcode->noperands; argn++)
1155 switch (opcode->args.nib[argn])
1158 if (operand[argn].mode != RD16)
1160 as_bad ("destination operand must be 16 bit register");
1168 if (operand[argn].mode != RS8)
1170 as_bad ("source operand must be 8 bit register");
1176 if (operand[argn].mode != ABS16DST)
1178 as_bad ("destination operand must be 16bit absolute address");
1183 if (operand[argn].mode != RD8)
1185 as_bad ("destination operand must be 8 bit register");
1192 if (operand[argn].mode != ABS16SRC)
1194 as_bad ("source operand must be 16bit absolute address");
1202 as_bad ("invalid operands");
1205 /* This is the guts of the machine-dependent assembler. STR points to a
1206 machine dependent instruction. This funciton is supposed to emit
1207 the frags/bytes it assembles to.
1218 struct h8_op operand[2];
1219 struct h8_opcode *opcode;
1220 struct h8_opcode *prev_opcode;
1225 /* Drop leading whitespace */
1229 /* find the op code end */
1230 for (op_start = op_end = str;
1231 *op_end != 0 && *op_end != ' ';
1245 if (op_end == op_start)
1247 as_bad ("can't find opcode ");
1253 opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
1258 as_bad ("unknown opcode");
1262 /* We use to set input_line_pointer to the result of get_operands,
1263 but that is wrong. Our caller assumes we don't change it. */
1265 (void) get_operands (opcode->noperands, op_end, operand);
1267 prev_opcode = opcode;
1269 opcode = get_specific (opcode, operand);
1273 /* Couldn't find an opcode which matched the operands */
1274 char *where = frag_more (2);
1278 clever_message (prev_opcode, operand);
1282 if (opcode->size && dot)
1284 if (opcode->size != *dot)
1286 as_warn ("mismatch between opcode size and operand size");
1290 build_bytes (opcode, operand);
1295 tc_crawl_symbol_chain (headers)
1296 object_headers * headers;
1298 printf ("call to tc_crawl_symbol_chain \n");
1302 md_undefined_symbol (name)
1309 tc_headers_hook (headers)
1310 object_headers * headers;
1312 printf ("call to tc_headers_hook \n");
1315 /* Various routines to kill one day */
1316 /* Equal to MAX_PRECISION in atof-ieee.c */
1317 #define MAX_LITTLENUMS 6
1319 /* Turn a string in input_line_pointer into a floating point constant of type
1320 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1321 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1324 md_atof (type, litP, sizeP)
1330 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1331 LITTLENUM_TYPE *wordP;
1363 return "Bad call to MD_ATOF()";
1365 t = atof_ieee (input_line_pointer, type, words);
1367 input_line_pointer = t;
1369 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1370 for (wordP = words; prec--;)
1372 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1373 litP += sizeof (LITTLENUM_TYPE);
1378 CONST char *md_shortopts = "";
1379 struct option md_longopts[] = {
1380 {NULL, no_argument, NULL, 0}
1382 size_t md_longopts_size = sizeof(md_longopts);
1385 md_parse_option (c, arg)
1393 md_show_usage (stream)
1398 int md_short_jump_size;
1401 tc_aout_fix_to_chars ()
1403 printf ("call to tc_aout_fix_to_chars \n");
1408 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1415 as_fatal ("failed sanity check.");
1419 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1421 addressT from_addr, to_addr;
1425 as_fatal ("failed sanity check.");
1429 md_convert_frag (headers, seg, fragP)
1430 object_headers *headers;
1434 printf ("call to md_convert_frag \n");
1439 md_section_align (seg, size)
1443 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1448 md_apply_fix (fixP, val)
1452 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1454 switch (fixP->fx_size)
1460 *buf++ = (val >> 8);
1464 *buf++ = (val >> 24);
1465 *buf++ = (val >> 16);
1466 *buf++ = (val >> 8);
1474 int md_long_jump_size;
1477 md_estimate_size_before_relax (fragP, segment_type)
1478 register fragS *fragP;
1479 register segT segment_type;
1481 printf ("call tomd_estimate_size_before_relax \n");
1485 /* Put number into target byte order */
1488 md_number_to_chars (ptr, use, nbytes)
1493 number_to_chars_bigendian (ptr, use, nbytes);
1496 md_pcrel_from (fixP)
1504 tc_reloc_mangle (fix_ptr, intr, base)
1506 struct internal_reloc *intr;
1510 symbolS *symbol_ptr;
1512 symbol_ptr = fix_ptr->fx_addsy;
1514 /* If this relocation is attached to a symbol then it's ok
1516 if (fix_ptr->fx_r_type == TC_CONS_RELOC)
1518 /* cons likes to create reloc32's whatever the size of the reloc..
1520 switch (fix_ptr->fx_size)
1523 intr->r_type = R_RELLONG;
1526 intr->r_type = R_RELWORD;
1529 intr->r_type = R_RELBYTE;
1539 intr->r_type = fix_ptr->fx_r_type;
1542 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1543 intr->r_offset = fix_ptr->fx_offset;
1546 intr->r_symndx = symbol_ptr->sy_number;
1548 intr->r_symndx = -1;
1553 /* end of tc-h8300.c */