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
50 /* start-sanitize-h8s */
52 /* end-sanitize-h8s */
53 #define PSIZE (Hmode ? L_32 : L_16)
55 #define DSYMMODE (Hmode ? L_24 : L_16)
56 int bsize = L_8; /* default branch displacement */
63 /* start-sanitize-h8s */
65 /* end-sanitize-h8s */
68 /* start-sanitize-h8s */
75 /* end-sanitize-h8s */
88 const pseudo_typeS md_pseudo_table[] =
91 {"h8300h", h8300hmode, 0},
92 /* start-sanitize-h8s */
93 {"h8300s", h8300smode, 0},
94 /* end-sanitize-h8s */
95 {"sbranch", sbranch, L_8},
96 {"lbranch", sbranch, L_16},
102 {"form", listing_psize, 0},
103 {"heading", listing_title, 0},
104 {"import", s_ignore, 0},
105 {"page", listing_eject, 0},
106 {"program", s_ignore, 0},
110 const int md_reloc_size;
112 const char EXP_CHARS[] = "eE";
114 /* Chars that mean this number is a floating point constant */
117 const char FLT_CHARS[] = "rRsSfFdDxXpP";
119 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
122 This function is called once, at assembler startup time. This should
123 set up all the tables, etc that the MD part of the assembler needs
130 struct h8_opcode *opcode;
131 char prev_buffer[100];
134 opcode_hash_control = hash_new ();
137 for (opcode = h8_opcodes; opcode->name; opcode++)
139 /* Strip off any . part when inserting the opcode and only enter
140 unique codes into the hash table
142 char *src = opcode->name;
143 unsigned int len = strlen (src);
144 char *dst = malloc (len + 1);
159 if (strcmp (buffer, prev_buffer))
161 hash_insert (opcode_hash_control, buffer, (char *) opcode);
162 strcpy (prev_buffer, buffer);
168 /* Find the number of operands */
169 opcode->noperands = 0;
170 while (opcode->args.nib[opcode->noperands] != E)
172 /* Find the length of the opcode in bytes */
174 while (opcode->data.nib[opcode->length * 2] != E)
189 int opsize; /* Set when a register size is seen */
201 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
210 /* try and parse a reg name, returns number of chars consumed */
212 parse_reg (src, mode, reg, direction)
219 if (src[0] == 's' && src[1] == 'p')
221 *mode = PSIZE | REG | direction;
225 if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
231 /* start-sanitize-h8s */
232 if (src[0] == 'e' && src[1] == 'x' && src[2] == 'r')
238 /* end-sanitize-h8s */
239 if (src[0] == 'f' && src[1] == 'p')
241 *mode = PSIZE | REG | direction;
247 && src[2] >= '0' && src[2] <= '7')
249 *mode = L_32 | REG | direction;
252 as_warn ("Reg not valid for H8/300");
257 && src[1] >= '0' && src[1] <= '7')
259 *mode = L_16 | REG | direction;
260 *reg = src[1] - '0' + 8;
262 as_warn ("Reg not valid for H8/300");
268 if (src[1] >= '0' && src[1] <= '7')
272 *mode = L_8 | REG | direction;
273 *reg = (src[1] - '0') + 8;
278 *mode = L_8 | REG | direction;
279 *reg = (src[1] - '0');
282 *mode = L_16 | REG | direction;
283 *reg = (src[1] - '0');
295 char *save = input_line_pointer;
298 input_line_pointer = s;
300 if (op->X_op == O_absent)
301 as_bad ("missing operand");
302 new = input_line_pointer;
303 input_line_pointer = save;
308 skip_colonthing (ptr, exp, mode)
320 /* ff fill any 8 bit quantity */
321 /* exp->X_add_number -= 0x100;*/
330 else if (*ptr == '3')
334 else if (*ptr == '1')
338 while (isdigit (*ptr))
345 /* The many forms of operand:
348 @Rn Register indirect
349 @(exp[:16], Rn) Register indirect with displacement
353 @aa:16 absolute 16 bit
356 #xx[:size] immediate data
357 @(exp:[8], pc) pc rel
358 @@aa[:8] memory indirect
369 src = skip_colonthing (src, &op->exp, &mode);
373 /* Choose a default mode */
374 if (op->exp.X_add_number < -32768
375 || op->exp.X_add_number > 32767)
382 else if (op->exp.X_add_symbol
383 || op->exp.X_op_symbol)
395 get_operand (ptr, op, dst, direction)
408 /* start-sanitize-h8s */
409 /* Gross. Gross. ldm and stm have a format not easily handled
410 by get_operand. We deal with it explicitly here. */
411 if (src[0] == 'e' && src[1] == 'r' && isdigit(src[2])
412 && src[3] == '-' && src[4] == 'e' && src[5] == 'r' && isdigit(src[6]))
420 as_bad ("Invalid register list for ldm/stm\n");
423 as_bad ("Invalid register list for ldm/stm\n");
426 as_bad ("Invalid register list for ldm/stm\n");
430 as_bad ("Invalid register list for ldm/stm\n");
432 /* Even sicker. We encode two registers into op->reg. One
433 for the low register to save, the other for the high
434 register to save; we also set the high bit in op->reg
435 so we know this is "very special". */
436 op->reg = 0x80000000 | (high << 8) | low;
441 /* end-sanitize-h8s */
443 len = parse_reg (src, &op->mode, &op->reg, direction);
456 src = parse_exp (src, &op->exp);
458 src = skip_colonthing (src, &op->exp, &op->mode);
471 len = parse_reg (src, &mode, &num, direction);
474 /* Oops, not a reg after all, must be ordinary exp */
476 /* must be a symbol */
477 op->mode = ABS | PSIZE | direction;
478 *ptr = skip_colonthing (parse_exp (src, &op->exp),
479 &op->exp, &op->mode);
487 if ((mode & SIZE) != PSIZE)
488 as_bad ("Wrong size pointer register for architecture.");
499 /* Start off assuming a 16 bit offset */
502 src = parse_exp (src, &op->exp);
504 src = colonmod24 (op, src);
509 op->mode |= ABS | direction;
516 as_bad ("expected @(exp, reg16)");
522 len = parse_reg (src, &mode, &op->reg, direction);
523 if (len == 0 || !(mode & REG))
525 as_bad ("expected @(exp, reg16)");
528 op->mode |= DISP | direction;
531 src = skip_colonthing (src, &op->exp, &op->mode);
533 if (*src != ')' && '(')
535 as_bad ("expected @(exp, reg16)");
542 len = parse_reg (src, &mode, &num, direction);
550 if ((mode & SIZE) != PSIZE)
551 as_bad ("Wrong size pointer register for architecture.");
557 if ((mode & SIZE) != PSIZE)
558 as_bad ("Wrong size pointer register for architecture.");
560 op->mode = direction | IND | PSIZE;
568 /* must be a symbol */
570 op->mode = ABS | direction;
571 src = parse_exp (src, &op->exp);
573 *ptr = colonmod24 (op, src);
584 src = parse_exp (src, &op->exp);
585 *ptr = skip_colonthing (src, &op->exp, &op->mode);
589 /* start-sanitize-h8s */
590 else if (strncmp (src, "mach", 4) == 0
591 || strncmp (src, "macl", 4) == 0)
593 op->reg = src[3] == 'l';
598 /* end-sanitize-h8s */
601 src = parse_exp (src, &op->exp);
602 /* Trailing ':' size ? */
605 if (src[1] == '1' && src[2] == '6')
607 op->mode = PCREL | L_16;
610 else if (src[1] == '8')
612 op->mode = PCREL | L_8;
617 as_bad ("expect :8 or :16 here");
622 op->mode = PCREL | bsize;
631 get_operands (noperands, op_end, operand)
632 unsigned int noperands;
634 struct h8_op *operand;
647 get_operand (&ptr, operand + 0, 0, SRC);
651 get_operand (&ptr, operand + 1, 1, DST);
661 get_operand (&ptr, operand + 0, 0, SRC);
664 get_operand (&ptr, operand + 1, 1, DST);
675 /* Passed a pointer to a list of opcodes which use different
676 addressing modes, return the opcode which matches the opcodes
681 get_specific (opcode, operands)
682 struct h8_opcode *opcode;
683 struct h8_op *operands;
685 struct h8_opcode *this_try = opcode;
688 unsigned int this_index = opcode->idx;
690 /* There's only one ldm/stm and it's easier to just
691 get out quick for them. */
692 if (strcmp (opcode->name, "stm.l") == 0
693 || strcmp (opcode->name, "ldm.l") == 0)
696 while (this_index == opcode->idx && !found)
702 for (i = 0; i < this_try->noperands && found; i++)
704 op_type op = this_try->args.nib[i];
705 int x = operands[i].mode;
707 if ((op & (DISP | REG)) == (DISP | REG)
708 && ((x & (DISP | REG)) == (DISP | REG)))
710 dispreg = operands[i].reg;
719 x = (x & ~L_P) | (Hmode ? L_32 : L_16);
723 op = (op & ~L_P) | (Hmode ? L_32 : L_16);
728 /* The size of the reg is v important */
729 if ((op & SIZE) != (x & SIZE))
732 else if ((op & ABSJMP) && (x & ABS))
734 operands[i].mode &= ~ABS;
735 operands[i].mode |= ABSJMP;
736 /* But it may not be 24 bits long */
739 operands[i].mode &= ~SIZE;
740 operands[i].mode |= L_16;
745 else if ((op & (KBIT | DBIT)) && (x & IMM))
747 /* This is ok if the immediate value is sensible */
753 /* The size of the displacement is important */
754 if ((op & SIZE) != (x & SIZE))
758 else if ((op & (DISP | IMM | ABS))
759 && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
761 /* Promote a L_24 to L_32 if it makes us match. */
762 if ((x & L_24) && (op & L_32))
767 /* Promote an L8 to L_16 if it makes us match. */
768 if (op & ABS && op & L_8 && op & DISP)
773 else if ((x & SIZE) != 0
774 && ((op & SIZE) != (x & SIZE)))
777 /* start-sanitize-h8s */
778 else if ((op & MACREG) != (x & MACREG))
782 /* end-sanitize-h8s */
783 else if ((op & MODE) != (x & MODE))
797 check_operand (operand, width, string)
798 struct h8_op *operand;
802 if (operand->exp.X_add_symbol == 0
803 && operand->exp.X_op_symbol == 0)
806 /* No symbol involved, let's look at offset, it's dangerous if any of
807 the high bits are not 0 or ff's, find out by oring or anding with
808 the width and seeing if the answer is 0 or all fs*/
810 if ((operand->exp.X_add_number & ~width) != 0 &&
811 (operand->exp.X_add_number | width) != (~0))
814 && (operand->exp.X_add_number & 0xff00) == 0xff00)
816 /* Just ignore this one - which happens when trying to
817 fit a 16 bit address truncated into an 8 bit address
818 of something like bset. */
822 as_warn ("operand %s0x%lx out of range.", string,
823 (unsigned long) operand->exp.X_add_number);
830 /* RELAXMODE has one of 3 values:
832 0 Output a "normal" reloc, no relaxing possible for this insn/reloc
834 1 Output a relaxable 24bit absolute mov.w address relocation
835 (may relax into a 16bit absolute address).
837 2 Output a relaxable 16/24 absolute mov.b address relocation
838 (may relax into an 8bit absolute address). */
841 do_a_fix_imm (offset, operand, relaxmode)
843 struct h8_op *operand;
851 char *t = operand->mode & IMM ? "#" : "@";
853 if (operand->exp.X_add_symbol == 0)
855 char *bytes = frag_now->fr_literal + offset;
856 switch (operand->mode & SIZE)
859 check_operand (operand, 0x3, t);
860 bytes[0] |= (operand->exp.X_add_number) << 4;
863 check_operand (operand, 0x7, t);
864 bytes[0] |= (operand->exp.X_add_number) << 4;
867 check_operand (operand, 0xff, t);
868 bytes[0] = operand->exp.X_add_number;
871 check_operand (operand, 0xffff, t);
872 bytes[0] = operand->exp.X_add_number >> 8;
873 bytes[1] = operand->exp.X_add_number >> 0;
876 check_operand (operand, 0xffffff, t);
877 bytes[0] = operand->exp.X_add_number >> 16;
878 bytes[1] = operand->exp.X_add_number >> 8;
879 bytes[2] = operand->exp.X_add_number >> 0;
883 /* This should be done with bfd */
884 bytes[0] = operand->exp.X_add_number >> 24;
885 bytes[1] = operand->exp.X_add_number >> 16;
886 bytes[2] = operand->exp.X_add_number >> 8;
887 bytes[3] = operand->exp.X_add_number >> 0;
894 switch (operand->mode & SIZE)
900 where = (operand->mode & SIZE) == L_24 ? -1 : 0;
903 else if (relaxmode == 1)
909 as_bad("Can't work out size of operand.\n");
917 operand->exp.X_add_number = (short)operand->exp.X_add_number;
923 operand->exp.X_add_number = (char)operand->exp.X_add_number;
926 fix_new_exp (frag_now,
936 /* Now we know what sort of opcodes it is, lets build the bytes -
939 build_bytes (this_try, operand)
940 struct h8_opcode *this_try;
941 struct h8_op *operand;
945 char *output = frag_more (this_try->length);
946 op_type *nibble_ptr = this_try->data.nib;
948 unsigned int nibble_count = 0;
956 if (!(this_try->inbase || Hmode))
957 as_warn ("Opcode `%s' not available in H8/300 mode", this_try->name);
959 while (*nibble_ptr != E)
973 if (c & (REG | IND | INC | DEC))
975 nib = operand[d].reg;
977 else if ((c & DISPREG) == (DISPREG))
984 absat = nibble_count / 2;
987 else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
990 immat = nibble_count / 2;
999 switch (operand[0].exp.X_add_number)
1008 as_bad ("Need #1 or #2 here");
1013 switch (operand[0].exp.X_add_number)
1023 as_warn ("#4 not valid on H8/300.");
1028 as_bad ("Need #1 or #2 here");
1031 /* stop it making a fix */
1032 operand[0].mode = 0;
1037 operand[d].mode |= MEMRELAX;
1045 /* start-sanitize-h8s */
1048 nib = 2 + operand[d].reg;
1050 /* end-sanitize-h8s */
1057 /* start-sanitize-h8s */
1058 /* Disgusting. Why, oh why didn't someone ask us for advice
1059 on the assembler format. */
1060 if (strcmp (this_try->name, "stm.l") == 0
1061 || strcmp (this_try->name, "ldm.l") == 0)
1064 high = (operand[this_try->name[0] == 'l' ? 1 : 0].reg >> 8) & 0xf;
1065 low = operand[this_try->name[0] == 'l' ? 1 : 0].reg & 0xf;
1067 asnibbles[2] = high - low;
1068 asnibbles[7] = (this_try->name[0] == 'l') ? high : low;
1070 /* end-sanitize-h8s */
1072 for (i = 0; i < this_try->length; i++)
1074 output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
1077 /* Note if this is a movb instruction -- there's a special relaxation
1078 which only applies to them. */
1079 if (strcmp (this_try->name, "mov.b") == 0)
1082 /* output any fixes */
1083 for (i = 0; i < 2; i++)
1085 int x = operand[i].mode;
1087 if (x & (IMM | DISP))
1089 do_a_fix_imm (output - frag_now->fr_literal + immat,
1090 operand + i, x & MEMRELAX != 0);
1094 do_a_fix_imm (output - frag_now->fr_literal + absat,
1095 operand + i, x & MEMRELAX ? movb + 1 : 0);
1099 int size16 = x & L_16;
1100 int where = size16 ? 2 : 1;
1101 int size = size16 ? 2 : 1;
1102 int type = size16 ? R_PCRWORD : R_PCRBYTE;
1104 check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
1106 if (operand[i].exp.X_add_number & 1)
1108 as_warn ("branch operand has odd offset (%lx)\n",
1109 (unsigned long) operand->exp.X_add_number);
1112 operand[i].exp.X_add_number =
1113 (char) (operand[i].exp.X_add_number - 1);
1114 fix_new_exp (frag_now,
1115 output - frag_now->fr_literal + where,
1121 else if (x & MEMIND)
1124 check_operand (operand + i, 0xff, "@@");
1125 fix_new_exp (frag_now,
1126 output - frag_now->fr_literal + 1,
1132 else if (x & ABSJMP)
1134 /* This jmp may be a jump or a branch */
1136 check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
1137 if (operand[i].exp.X_add_number & 1)
1139 as_warn ("branch operand has odd offset (%lx)\n",
1140 (unsigned long) operand->exp.X_add_number);
1143 operand[i].exp.X_add_number = (short) operand[i].exp.X_add_number;
1144 fix_new_exp (frag_now,
1145 output - frag_now->fr_literal,
1156 try and give an intelligent error message for common and simple to
1161 clever_message (opcode, operand)
1162 struct h8_opcode *opcode;
1163 struct h8_op *operand;
1165 /* Find out if there was more than one possible opccode */
1167 if ((opcode + 1)->idx != opcode->idx)
1171 /* Only one opcode of this flavour, try and guess which operand
1173 for (argn = 0; argn < opcode->noperands; argn++)
1175 switch (opcode->args.nib[argn])
1178 if (operand[argn].mode != RD16)
1180 as_bad ("destination operand must be 16 bit register");
1188 if (operand[argn].mode != RS8)
1190 as_bad ("source operand must be 8 bit register");
1196 if (operand[argn].mode != ABS16DST)
1198 as_bad ("destination operand must be 16bit absolute address");
1203 if (operand[argn].mode != RD8)
1205 as_bad ("destination operand must be 8 bit register");
1212 if (operand[argn].mode != ABS16SRC)
1214 as_bad ("source operand must be 16bit absolute address");
1222 as_bad ("invalid operands");
1225 /* This is the guts of the machine-dependent assembler. STR points to a
1226 machine dependent instruction. This funciton is supposed to emit
1227 the frags/bytes it assembles to.
1238 struct h8_op operand[2];
1239 struct h8_opcode *opcode;
1240 struct h8_opcode *prev_opcode;
1245 /* Drop leading whitespace */
1249 /* find the op code end */
1250 for (op_start = op_end = str;
1251 *op_end != 0 && *op_end != ' ';
1265 if (op_end == op_start)
1267 as_bad ("can't find opcode ");
1273 opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
1278 as_bad ("unknown opcode");
1282 /* We use to set input_line_pointer to the result of get_operands,
1283 but that is wrong. Our caller assumes we don't change it. */
1285 (void) get_operands (opcode->noperands, op_end, operand);
1287 prev_opcode = opcode;
1289 opcode = get_specific (opcode, operand);
1293 /* Couldn't find an opcode which matched the operands */
1294 char *where = frag_more (2);
1298 clever_message (prev_opcode, operand);
1302 if (opcode->size && dot)
1304 if (opcode->size != *dot)
1306 as_warn ("mismatch between opcode size and operand size");
1310 build_bytes (opcode, operand);
1315 tc_crawl_symbol_chain (headers)
1316 object_headers * headers;
1318 printf ("call to tc_crawl_symbol_chain \n");
1322 md_undefined_symbol (name)
1329 tc_headers_hook (headers)
1330 object_headers * headers;
1332 printf ("call to tc_headers_hook \n");
1335 /* Various routines to kill one day */
1336 /* Equal to MAX_PRECISION in atof-ieee.c */
1337 #define MAX_LITTLENUMS 6
1339 /* Turn a string in input_line_pointer into a floating point constant of type
1340 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1341 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1344 md_atof (type, litP, sizeP)
1350 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1351 LITTLENUM_TYPE *wordP;
1383 return "Bad call to MD_ATOF()";
1385 t = atof_ieee (input_line_pointer, type, words);
1387 input_line_pointer = t;
1389 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1390 for (wordP = words; prec--;)
1392 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1393 litP += sizeof (LITTLENUM_TYPE);
1398 CONST char *md_shortopts = "";
1399 struct option md_longopts[] = {
1400 {NULL, no_argument, NULL, 0}
1402 size_t md_longopts_size = sizeof(md_longopts);
1405 md_parse_option (c, arg)
1413 md_show_usage (stream)
1418 int md_short_jump_size;
1421 tc_aout_fix_to_chars ()
1423 printf ("call to tc_aout_fix_to_chars \n");
1428 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1435 as_fatal ("failed sanity check.");
1439 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1441 addressT from_addr, to_addr;
1445 as_fatal ("failed sanity check.");
1449 md_convert_frag (headers, seg, fragP)
1450 object_headers *headers;
1454 printf ("call to md_convert_frag \n");
1459 md_section_align (seg, size)
1463 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1468 md_apply_fix (fixP, val)
1472 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1474 switch (fixP->fx_size)
1480 *buf++ = (val >> 8);
1484 *buf++ = (val >> 24);
1485 *buf++ = (val >> 16);
1486 *buf++ = (val >> 8);
1494 int md_long_jump_size;
1497 md_estimate_size_before_relax (fragP, segment_type)
1498 register fragS *fragP;
1499 register segT segment_type;
1501 printf ("call tomd_estimate_size_before_relax \n");
1505 /* Put number into target byte order */
1508 md_number_to_chars (ptr, use, nbytes)
1513 number_to_chars_bigendian (ptr, use, nbytes);
1516 md_pcrel_from (fixP)
1524 tc_reloc_mangle (fix_ptr, intr, base)
1526 struct internal_reloc *intr;
1530 symbolS *symbol_ptr;
1532 symbol_ptr = fix_ptr->fx_addsy;
1534 /* If this relocation is attached to a symbol then it's ok
1536 if (fix_ptr->fx_r_type == TC_CONS_RELOC)
1538 /* cons likes to create reloc32's whatever the size of the reloc..
1540 switch (fix_ptr->fx_size)
1543 intr->r_type = R_RELLONG;
1546 intr->r_type = R_RELWORD;
1549 intr->r_type = R_RELBYTE;
1559 intr->r_type = fix_ptr->fx_r_type;
1562 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1563 intr->r_offset = fix_ptr->fx_offset;
1566 intr->r_symndx = symbol_ptr->sy_number;
1568 intr->r_symndx = -1;
1573 /* end of tc-h8300.c */