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 #define PSIZE (Hmode ? L_32 : L_16)
52 #define DSYMMODE (Hmode ? L_24 : L_16)
53 int bsize = L_8; /* default branch displacement */
75 const pseudo_typeS md_pseudo_table[] =
78 {"h8300h", h8300hmode, 0},
79 {"sbranch", sbranch, L_8},
80 {"lbranch", sbranch, L_16},
86 {"form", listing_psize, 0},
87 {"heading", listing_title, 0},
88 {"import", s_ignore, 0},
89 {"page", listing_eject, 0},
90 {"program", s_ignore, 0},
94 const int md_reloc_size;
96 const char EXP_CHARS[] = "eE";
98 /* Chars that mean this number is a floating point constant */
101 const char FLT_CHARS[] = "rRsSfFdDxXpP";
103 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
106 This function is called once, at assembler startup time. This should
107 set up all the tables, etc that the MD part of the assembler needs
114 struct h8_opcode *opcode;
115 char prev_buffer[100];
118 opcode_hash_control = hash_new ();
121 for (opcode = h8_opcodes; opcode->name; opcode++)
123 /* Strip off any . part when inserting the opcode and only enter
124 unique codes into the hash table
126 char *src = opcode->name;
127 unsigned int len = strlen (src);
128 char *dst = malloc (len + 1);
143 if (strcmp (buffer, prev_buffer))
145 hash_insert (opcode_hash_control, buffer, (char *) opcode);
146 strcpy (prev_buffer, buffer);
152 /* Find the number of operands */
153 opcode->noperands = 0;
154 while (opcode->args.nib[opcode->noperands] != E)
156 /* Find the length of the opcode in bytes */
158 while (opcode->data.nib[opcode->length * 2] != E)
173 int opsize; /* Set when a register size is seen */
185 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
194 /* try and parse a reg name, returns number of chars consumed */
196 parse_reg (src, mode, reg, direction)
203 if (src[0] == 's' && src[1] == 'p')
205 *mode = PSIZE | REG | direction;
209 if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
215 if (src[0] == 'f' && src[1] == 'p')
217 *mode = PSIZE | REG | direction;
223 && src[2] >= '0' && src[2] <= '7')
225 *mode = L_32 | REG | direction;
228 as_warn ("Reg only legal for H8/300-H");
233 && src[1] >= '0' && src[1] <= '7')
235 *mode = L_16 | REG | direction;
236 *reg = src[1] - '0' + 8;
238 as_warn ("Reg only legal for H8/300-H");
244 if (src[1] >= '0' && src[1] <= '7')
248 *mode = L_8 | REG | direction;
249 *reg = (src[1] - '0') + 8;
254 *mode = L_8 | REG | direction;
255 *reg = (src[1] - '0');
258 *mode = L_16 | REG | direction;
259 *reg = (src[1] - '0');
271 char *save = input_line_pointer;
274 input_line_pointer = s;
276 if (op->X_op == O_absent)
277 as_bad ("missing operand");
278 new = input_line_pointer;
279 input_line_pointer = save;
284 skip_colonthing (ptr, exp, mode)
296 /* ff fill any 8 bit quantity */
297 /* exp->X_add_number -= 0x100;*/
306 else if (*ptr == '1')
310 while (isdigit (*ptr))
317 /* The many forms of operand:
320 @Rn Register indirect
321 @(exp[:16], Rn) Register indirect with displacement
325 @aa:16 absolute 16 bit
328 #xx[:size] immediate data
329 @(exp:[8], pc) pc rel
330 @@aa[:8] memory indirect
341 src = skip_colonthing (src, &op->exp, &mode);
345 /* Choose a default mode */
346 if (op->exp.X_add_number < -32768
347 || op->exp.X_add_number > 32767)
354 else if (op->exp.X_add_symbol
355 || op->exp.X_op_symbol)
367 get_operand (ptr, op, dst, direction)
380 len = parse_reg (src, &op->mode, &op->reg, direction);
393 src = parse_exp (src, &op->exp);
395 src = skip_colonthing (src, &op->exp, &op->mode);
408 len = parse_reg (src, &mode, &num, direction);
411 /* Oops, not a reg after all, must be ordinary exp */
413 /* must be a symbol */
414 op->mode = ABS | PSIZE | direction;
415 *ptr = skip_colonthing (parse_exp (src, &op->exp),
416 &op->exp, &op->mode);
424 if ((mode & SIZE) != PSIZE)
425 as_bad ("Wrong size pointer register for architecture.");
436 /* Start off assuming a 16 bit offset */
439 src = parse_exp (src, &op->exp);
441 src = colonmod24 (op, src);
446 op->mode |= ABS | direction;
453 as_bad ("expected @(exp, reg16)");
459 len = parse_reg (src, &mode, &op->reg, direction);
460 if (len == 0 || !(mode & REG))
462 as_bad ("expected @(exp, reg16)");
465 op->mode |= DISP | direction;
468 src = skip_colonthing (src, &op->exp, &op->mode);
470 if (*src != ')' && '(')
472 as_bad ("expected @(exp, reg16)");
479 len = parse_reg (src, &mode, &num, direction);
487 if ((mode & SIZE) != PSIZE)
488 as_bad ("Wrong size pointer register for architecture.");
494 if ((mode & SIZE) != PSIZE)
495 as_bad ("Wrong size pointer register for architecture.");
497 op->mode = direction | IND | PSIZE;
505 /* must be a symbol */
507 op->mode = ABS | direction;
508 src = parse_exp (src, &op->exp);
510 *ptr = colonmod24 (op, src);
521 src = parse_exp (src, &op->exp);
522 *ptr = skip_colonthing (src, &op->exp, &op->mode);
528 src = parse_exp (src, &op->exp);
529 /* Trailing ':' size ? */
532 if (src[1] == '1' && src[2] == '6')
534 op->mode = PCREL | L_16;
537 else if (src[1] == '8')
539 op->mode = PCREL | L_8;
544 as_bad ("expect :8 or :16 here");
549 op->mode = PCREL | bsize;
558 get_operands (noperands, op_end, operand)
559 unsigned int noperands;
561 struct h8_op *operand;
574 get_operand (&ptr, operand + 0, 0, SRC);
578 get_operand (&ptr, operand + 1, 1, DST);
588 get_operand (&ptr, operand + 0, 0, SRC);
591 get_operand (&ptr, operand + 1, 1, DST);
602 /* Passed a pointer to a list of opcodes which use different
603 addressing modes, return the opcode which matches the opcodes
608 get_specific (opcode, operands)
609 struct h8_opcode *opcode;
610 struct h8_op *operands;
612 struct h8_opcode *this_try = opcode;
615 unsigned int this_index = opcode->idx;
617 while (this_index == opcode->idx && !found)
623 for (i = 0; i < this_try->noperands && found; i++)
625 op_type op = this_try->args.nib[i];
626 int x = operands[i].mode;
628 if ((op & (DISP | REG)) == (DISP | REG)
629 && ((x & (DISP | REG)) == (DISP | REG)))
631 dispreg = operands[i].reg;
640 x = (x & ~L_P) | (Hmode ? L_32 : L_16);
644 op = (op & ~L_P) | (Hmode ? L_32 : L_16);
649 /* The size of the reg is v important */
650 if ((op & SIZE) != (x & SIZE))
653 else if ((op & ABSJMP) && (x & ABS))
655 operands[i].mode &= ~ABS;
656 operands[i].mode |= ABSJMP;
657 /* But it may not be 24 bits long */
660 operands[i].mode &= ~SIZE;
661 operands[i].mode |= L_16;
666 else if ((op & (KBIT | DBIT)) && (x & IMM))
668 /* This is ok if the immediate value is sensible */
674 /* The size of the displacement is important */
675 if ((op & SIZE) != (x & SIZE))
679 else if ((op & (DISP | IMM | ABS))
680 && (op & (DISP | IMM | ABS)) == (x & (DISP | IMM | ABS)))
682 /* Got a diplacement,will fit if no size or same size as try */
683 if (op & ABS && op & L_8)
685 /* We want an 8 bit abs here, but one which looks like 16 bits will do fine */
691 && ((op & SIZE) != (x & SIZE)))
694 else if ((op & MODE) != (x & MODE))
708 check_operand (operand, width, string)
709 struct h8_op *operand;
713 if (operand->exp.X_add_symbol == 0
714 && operand->exp.X_op_symbol == 0)
717 /* No symbol involved, let's look at offset, it's dangerous if any of
718 the high bits are not 0 or ff's, find out by oring or anding with
719 the width and seeing if the answer is 0 or all fs*/
721 if ((operand->exp.X_add_number & ~width) != 0 &&
722 (operand->exp.X_add_number | width) != (~0))
725 && (operand->exp.X_add_number & 0xff00) == 0xff00)
727 /* Just ignore this one - which happens when trying to
728 fit a 16 bit address truncated into an 8 bit address
729 of something like bset. */
733 as_warn ("operand %s0x%lx out of range.", string,
734 (unsigned long) operand->exp.X_add_number);
742 do_a_fix_imm (offset, operand, relaxing)
744 struct h8_op *operand;
752 char *t = operand->mode & IMM ? "#" : "@";
754 if (operand->exp.X_add_symbol == 0)
756 char *bytes = frag_now->fr_literal + offset;
757 switch (operand->mode & SIZE)
760 check_operand (operand, 0x3, t);
761 bytes[0] |= (operand->exp.X_add_number) << 4;
764 check_operand (operand, 0x7, t);
765 bytes[0] |= (operand->exp.X_add_number) << 4;
768 check_operand (operand, 0xff, t);
769 bytes[0] = operand->exp.X_add_number;
772 check_operand (operand, 0xffff, t);
773 bytes[0] = operand->exp.X_add_number >> 8;
774 bytes[1] = operand->exp.X_add_number >> 0;
777 check_operand (operand, 0xffffff, t);
778 bytes[0] = operand->exp.X_add_number >> 16;
779 bytes[1] = operand->exp.X_add_number >> 8;
780 bytes[2] = operand->exp.X_add_number >> 0;
784 /* This should be done with bfd */
785 bytes[0] = operand->exp.X_add_number >> 24;
786 bytes[1] = operand->exp.X_add_number >> 16;
787 bytes[2] = operand->exp.X_add_number >> 8;
788 bytes[3] = operand->exp.X_add_number >> 0;
795 switch (operand->mode & SIZE)
801 idx = relaxing ? R_MOVLB1 : R_RELLONG;
804 as_bad("Can't work out size of operand.\n");
813 idx = relaxing ? R_MOVB1 : R_RELWORD;
821 /* Sign extend any expression */
822 operand->exp.X_add_number = (short)operand->exp.X_add_number;
823 fix_new_exp (frag_now,
833 /* Now we know what sort of opcodes it is, lets build the bytes -
836 build_bytes (this_try, operand)
837 struct h8_opcode *this_try;
838 struct h8_op *operand;
842 char *output = frag_more (this_try->length);
843 op_type *nibble_ptr = this_try->data.nib;
845 unsigned int nibble_count = 0;
852 if (!(this_try->inbase || Hmode))
854 as_warn ("Opcode `%s' only available in this mode on H8/300-H",
858 while (*nibble_ptr != E)
872 if (c & (REG | IND | INC | DEC))
874 nib = operand[d].reg;
876 else if ((c & DISPREG) == (DISPREG))
883 absat = nibble_count / 2;
886 else if (c & (IMM | PCREL | ABS | ABSJMP | DISP))
889 immat = nibble_count / 2;
898 switch (operand[0].exp.X_add_number)
907 as_bad ("Need #1 or #2 here");
912 switch (operand[0].exp.X_add_number)
922 as_warn ("#4 only valid in h8/300 mode.");
927 as_bad ("Need #1 or #2 here");
930 /* stop it making a fix */
944 for (i = 0; i < this_try->length; i++)
946 output[i] = (asnibbles[i * 2] << 4) | asnibbles[i * 2 + 1];
949 /* output any fixes */
950 for (i = 0; i < 2; i++)
952 int x = operand[i].mode;
954 if (x & (IMM | DISP))
956 do_a_fix_imm (output - frag_now->fr_literal + immat, operand + i, 0);
960 do_a_fix_imm (output - frag_now->fr_literal + absat, operand + i, 0);
964 int size16 = x & L_16;
965 int where = size16 ? 2 : 1;
966 int size = size16 ? 2 : 1;
967 int type = size16 ? R_PCRWORD : R_PCRBYTE;
969 check_operand (operand + i, size16 ? 0x7fff : 0x7f, "@");
971 if (operand[i].exp.X_add_number & 1)
973 as_warn ("branch operand has odd offset (%lx)\n",
974 (unsigned long) operand->exp.X_add_number);
977 operand[i].exp.X_add_number =
978 (char) (operand[i].exp.X_add_number - 1);
979 fix_new_exp (frag_now,
980 output - frag_now->fr_literal + where,
989 check_operand (operand + i, 0xff, "@@");
990 fix_new_exp (frag_now,
991 output - frag_now->fr_literal + 1,
999 /* This jmp may be a jump or a branch */
1001 check_operand (operand + i, Hmode ? 0xffffff : 0xffff, "@");
1002 if (operand[i].exp.X_add_number & 1)
1004 as_warn ("branch operand has odd offset (%lx)\n",
1005 (unsigned long) operand->exp.X_add_number);
1008 operand[i].exp.X_add_number = (short) operand[i].exp.X_add_number;
1009 fix_new_exp (frag_now,
1010 output - frag_now->fr_literal,
1021 try and give an intelligent error message for common and simple to
1026 clever_message (opcode, operand)
1027 struct h8_opcode *opcode;
1028 struct h8_op *operand;
1030 /* Find out if there was more than one possible opccode */
1032 if ((opcode + 1)->idx != opcode->idx)
1036 /* Only one opcode of this flavour, try and guess which operand
1038 for (argn = 0; argn < opcode->noperands; argn++)
1040 switch (opcode->args.nib[argn])
1043 if (operand[argn].mode != RD16)
1045 as_bad ("destination operand must be 16 bit register");
1053 if (operand[argn].mode != RS8)
1055 as_bad ("source operand must be 8 bit register");
1061 if (operand[argn].mode != ABS16DST)
1063 as_bad ("destination operand must be 16bit absolute address");
1068 if (operand[argn].mode != RD8)
1070 as_bad ("destination operand must be 8 bit register");
1077 if (operand[argn].mode != ABS16SRC)
1079 as_bad ("source operand must be 16bit absolute address");
1087 as_bad ("invalid operands");
1090 /* This is the guts of the machine-dependent assembler. STR points to a
1091 machine dependent instruction. This funciton is supposed to emit
1092 the frags/bytes it assembles to.
1103 struct h8_op operand[2];
1104 struct h8_opcode *opcode;
1105 struct h8_opcode *prev_opcode;
1110 /* Drop leading whitespace */
1114 /* find the op code end */
1115 for (op_start = op_end = str;
1116 *op_end != 0 && *op_end != ' ';
1130 if (op_end == op_start)
1132 as_bad ("can't find opcode ");
1138 opcode = (struct h8_opcode *) hash_find (opcode_hash_control,
1143 as_bad ("unknown opcode");
1147 /* We use to set input_line_pointer to the result of get_operands,
1148 but that is wrong. Our caller assumes we don't change it. */
1150 (void) get_operands (opcode->noperands, op_end, operand);
1152 prev_opcode = opcode;
1154 opcode = get_specific (opcode, operand);
1158 /* Couldn't find an opcode which matched the operands */
1159 char *where = frag_more (2);
1163 clever_message (prev_opcode, operand);
1167 if (opcode->size && dot)
1169 if (opcode->size != *dot)
1171 as_warn ("mismatch between opcode size and operand size");
1175 build_bytes (opcode, operand);
1180 tc_crawl_symbol_chain (headers)
1181 object_headers * headers;
1183 printf ("call to tc_crawl_symbol_chain \n");
1187 md_undefined_symbol (name)
1194 tc_headers_hook (headers)
1195 object_headers * headers;
1197 printf ("call to tc_headers_hook \n");
1200 /* Various routines to kill one day */
1201 /* Equal to MAX_PRECISION in atof-ieee.c */
1202 #define MAX_LITTLENUMS 6
1204 /* Turn a string in input_line_pointer into a floating point constant of type
1205 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1206 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1209 md_atof (type, litP, sizeP)
1215 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1216 LITTLENUM_TYPE *wordP;
1248 return "Bad call to MD_ATOF()";
1250 t = atof_ieee (input_line_pointer, type, words);
1252 input_line_pointer = t;
1254 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1255 for (wordP = words; prec--;)
1257 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1258 litP += sizeof (LITTLENUM_TYPE);
1263 CONST char *md_shortopts = "";
1264 struct option md_longopts[] = {
1265 {NULL, no_argument, NULL, 0}
1267 size_t md_longopts_size = sizeof(md_longopts);
1270 md_parse_option (c, arg)
1278 md_show_usage (stream)
1283 int md_short_jump_size;
1286 tc_aout_fix_to_chars ()
1288 printf ("call to tc_aout_fix_to_chars \n");
1293 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1300 as_fatal ("failed sanity check.");
1304 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1306 addressT from_addr, to_addr;
1310 as_fatal ("failed sanity check.");
1314 md_convert_frag (headers, seg, fragP)
1315 object_headers *headers;
1319 printf ("call to md_convert_frag \n");
1324 md_section_align (seg, size)
1328 return ((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1333 md_apply_fix (fixP, val)
1337 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1339 switch (fixP->fx_size)
1345 *buf++ = (val >> 8);
1349 *buf++ = (val >> 24);
1350 *buf++ = (val >> 16);
1351 *buf++ = (val >> 8);
1359 int md_long_jump_size;
1362 md_estimate_size_before_relax (fragP, segment_type)
1363 register fragS *fragP;
1364 register segT segment_type;
1366 printf ("call tomd_estimate_size_before_relax \n");
1370 /* Put number into target byte order */
1373 md_number_to_chars (ptr, use, nbytes)
1378 number_to_chars_bigendian (ptr, use, nbytes);
1381 md_pcrel_from (fixP)
1389 tc_reloc_mangle (fix_ptr, intr, base)
1391 struct internal_reloc *intr;
1395 symbolS *symbol_ptr;
1397 symbol_ptr = fix_ptr->fx_addsy;
1399 /* If this relocation is attached to a symbol then it's ok
1401 if (fix_ptr->fx_r_type == TC_CONS_RELOC)
1403 /* cons likes to create reloc32's whatever the size of the reloc..
1405 switch (fix_ptr->fx_size)
1408 intr->r_type = R_RELLONG;
1411 intr->r_type = R_RELWORD;
1414 intr->r_type = R_RELBYTE;
1424 intr->r_type = fix_ptr->fx_r_type;
1427 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1428 intr->r_offset = fix_ptr->fx_offset;
1431 intr->r_symndx = symbol_ptr->sy_number;
1433 intr->r_symndx = -1;
1438 /* end of tc-h8300.c */