1 /* tc-h8500.c -- Assemble code for the Hitachi H8/500
2 Copyright 1993, 1994, 1995, 1998, 2000, 2001, 2002
3 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)
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
29 #define ASSEMBLER_TABLE
30 #include "opcodes/h8500-opc.h"
31 #include "safe-ctype.h"
33 const char comment_chars[] = "!";
34 const char line_separator_chars[] = ";";
35 const char line_comment_chars[] = "!#";
37 /* This table describes all the machine specific pseudo-ops the assembler
38 has to support. The fields are:
39 pseudo-op name without dot
40 function to call to execute this pseudo-op
41 Integer arg to pass to the function
46 const pseudo_typeS md_pseudo_table[] =
52 {"form", listing_psize, 0},
53 {"heading", listing_title, 0},
54 {"import", s_ignore, 0},
55 {"page", listing_eject, 0},
56 {"program", s_ignore, 0},
60 const int md_reloc_size;
62 const char EXP_CHARS[] = "eE";
64 /* Chars that mean this number is a floating point constant */
67 const char FLT_CHARS[] = "rRsSfFdDxXpP";
69 #define C(a,b) ENCODE_RELAX(a,b)
70 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
72 #define GET_WHAT(x) ((x>>2))
76 #define UNDEF_BYTE_DISP 0
77 #define UNDEF_WORD_DISP 3
89 relax_typeS md_relax_table[C (END, 0)] = {
97 { BYTE_F, BYTE_B, 2, C (BRANCH, WORD_DISP) },
98 { WORD_F, WORD_B, 3, 0 },
103 { BYTE_F, BYTE_B, 3, C (SCB_F, WORD_DISP) },
104 { WORD_F, WORD_B, 8, 0 },
109 { BYTE_F, BYTE_B, 3, C (SCB_TST, WORD_DISP) },
110 { WORD_F, WORD_B, 10, 0 },
115 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
118 This function is called once, at assembler startup time. This should
119 set up all the tables, etc. that the MD part of the assembler needs
125 h8500_opcode_info *opcode;
126 char prev_buffer[100];
129 opcode_hash_control = hash_new ();
132 /* Insert unique names into hash table */
133 for (opcode = h8500_table; opcode->name; opcode++)
135 if (idx != opcode->idx)
137 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
143 static int rn; /* register number used by RN */
144 static int rs; /* register number used by RS */
145 static int rd; /* register number used by RD */
146 static int crb; /* byte size cr */
147 static int crw; /* word sized cr */
148 static int cr; /* unknown size cr */
150 static expressionS displacement;/* displacement expression */
151 static int displacement_size; /* and size if given */
153 static int immediate_inpage;
154 static expressionS immediate; /* immediate expression */
155 static int immediate_size; /* and size if given */
157 static expressionS absolute; /* absolute expression */
158 static int absolute_size; /* and size if given */
170 /* Try to parse a reg name. Return the number of chars consumed. */
173 parse_reg (src, mode, reg)
181 /* Cribbed from get_symbol_end(). */
182 if (!is_name_beginner (*src) || *src == '\001')
185 while (is_part_of_name (*end) || *end == '\001')
189 if (len == 2 && src[0] == 'r')
191 if (src[1] >= '0' && src[1] <= '7')
194 *reg = (src[1] - '0');
198 if (len == 2 && src[0] == 's' && src[1] == 'p')
204 if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
210 if (len == 2 && src[0] == 's' && src[1] == 'r')
216 if (len == 2 && src[0] == 'b' && src[1] == 'r')
222 if (len == 2 && src[0] == 'e' && src[1] == 'p')
228 if (len == 2 && src[0] == 'd' && src[1] == 'p')
234 if (len == 2 && src[0] == 't' && src[1] == 'p')
240 if (len == 2 && src[0] == 'f' && src[1] == 'p')
250 parse_exp (s, op, page)
258 save = input_line_pointer;
263 if (s[1] == 'p' && s[2] == 'a' && s[3] == 'g' && s[4] == 'e')
268 if (s[1] == 'h' && s[2] == 'i' && s[3] == '1' && s[4] == '6')
273 else if (s[1] == 'o' && s[2] == 'f' && s[3] == 'f')
280 input_line_pointer = s;
283 if (op->X_op == O_absent)
284 as_bad (_("missing operand"));
285 new = input_line_pointer;
286 input_line_pointer = save;
292 exp_signed, exp_unsigned, exp_sandu
296 skip_colonthing (sign, ptr, exp, def, size8, size16, size24)
299 h8500_operand_info *exp;
305 ptr = parse_exp (ptr, &exp->exp, &exp->page);
314 else if (ptr[0] == '1' & ptr[1] == '6')
319 else if (ptr[0] == '2' & ptr[1] == '4')
323 as_bad (_(":24 not valid for this opcode"));
330 as_bad (_("expect :8,:16 or :24"));
336 if (exp->page == 'p')
340 else if (exp->page == 'h')
346 /* Let's work out the size from the context */
347 int n = exp->exp.X_add_number;
349 && exp->exp.X_op == O_constant
350 && ((sign == exp_signed && (n >= -128 && n <= 127))
351 || (sign == exp_unsigned && (n >= 0 && (n <= 255)))
352 || (sign == exp_sandu && (n >= -128 && (n <= 255)))))
366 parse_reglist (src, op)
368 h8500_operand_info *op;
374 int idx = 1; /* skip ( */
376 while (src[idx] && src[idx] != ')')
378 int done = parse_reg (src + idx, &mode, &rn);
387 as_bad (_("syntax error in reg list"));
393 done = parse_reg (src + idx, &mode, &rm);
405 as_bad (_("missing final register in range"));
412 op->exp.X_add_symbol = 0;
413 op->exp.X_op_symbol = 0;
414 op->exp.X_add_number = mask;
415 op->exp.X_op = O_constant;
416 op->exp.X_unsigned = 1;
422 /* The many forms of operand:
425 @Rn Register indirect
426 @(disp[:size], Rn) Register indirect with displacement
430 #xx[:size] immediate data
435 get_operand (ptr, op, ispage)
437 h8500_operand_info *op;
445 if (src[0] == '(' && src[1] == 'r')
447 /* This is a register list */
448 *ptr = src + parse_reglist (src, op);
452 len = parse_reg (src, &op->type, &op->reg);
466 len = parse_reg (src, &mode, &num);
469 /* Oops, not a reg after all, must be ordinary exp */
471 /* must be a symbol */
472 *ptr = skip_colonthing (exp_unsigned, src,
473 op, ABS16, ABS8, ABS16, ABS24);
487 src = skip_colonthing (exp_signed, src,
488 op, RNIND_D16, RNIND_D8, RNIND_D16, 0);
492 as_bad (_("expected @(exp, Rn)"));
496 len = parse_reg (src, &mode, &op->reg);
497 if (len == 0 || mode != RN)
499 as_bad (_("expected @(exp, Rn)"));
505 as_bad (_("expected @(exp, Rn)"));
511 len = parse_reg (src, &mode, &num);
521 as_bad (_("@Rn+ needs word register"));
531 as_bad (_("@Rn needs word register"));
541 /* must be a symbol */
543 skip_colonthing (exp_unsigned, src, op,
544 ispage ? ABS24 : ABS16, ABS8, ABS16, ABS24);
552 *ptr = skip_colonthing (exp_sandu, src, op, IMM16, IMM8, IMM16, ABS24);
557 *ptr = skip_colonthing (exp_signed, src, op,
558 ispage ? ABS24 : PCREL8, PCREL8, PCREL16, ABS24);
563 get_operands (info, args, operand)
564 h8500_opcode_info *info;
566 h8500_operand_info *operand;
580 get_operand (&ptr, operand + 0, info->name[0] == 'p');
586 get_operand (&ptr, operand + 0, 0);
589 get_operand (&ptr, operand + 1, 0);
599 /* Passed a pointer to a list of opcodes which use different
600 addressing modes, return the opcode which matches the opcodes
603 int pcrel8; /* Set when we've seen a pcrel operand */
605 static h8500_opcode_info *
606 get_specific (opcode, operands)
607 h8500_opcode_info *opcode;
608 h8500_operand_info *operands;
610 h8500_opcode_info *this_try = opcode;
612 unsigned int noperands = opcode->nargs;
614 unsigned int this_index = opcode->idx;
616 while (this_index == opcode->idx && !found)
622 /* look at both operands needed by the opcodes and provided by
624 for (i = 0; i < noperands; i++)
626 h8500_operand_info *user = operands + i;
628 switch (this_try->arg_type[i])
631 /* Opcode needs (disp:8,fp) */
632 if (user->type == RNIND_D8 && user->reg == 6)
634 displacement = user->exp;
639 if (user->type == RNIND_D16)
641 displacement = user->exp;
647 if (user->type == RNIND_D8)
649 displacement = user->exp;
656 if (user->type == this_try->arg_type[i])
658 displacement = user->exp;
665 if (user->type == RNDEC && user->reg == 7)
671 if (user->type == RNINC && user->reg == 7)
677 if (user->type == ABS16)
679 absolute = user->exp;
684 if (user->type == ABS8)
686 absolute = user->exp;
691 if (user->type == ABS24)
693 absolute = user->exp;
699 if ((user->type == CRB || user->type == CR) && user->reg != 0)
706 if ((user->type == CRW || user->type == CR) && user->reg == 0)
713 if (user->type == DISP16)
715 displacement = user->exp;
720 if (user->type == DISP8)
722 displacement = user->exp;
727 if (user->type == RN && user->reg == 6)
733 if (user->type == PCREL16)
735 displacement = user->exp;
740 if (user->type == PCREL8)
742 displacement = user->exp;
749 if (user->type == IMM16
750 || user->type == IMM8)
752 immediate_inpage = user->page;
753 immediate = user->exp;
759 if (user->type == IMM8)
761 immediate_inpage = user->page;
762 immediate = user->exp;
767 if (user->type == IMM8)
769 immediate_inpage = user->page;
770 immediate = user->exp;
775 if (user->type == IMM8
776 && user->exp.X_op == O_constant
778 (user->exp.X_add_number == -2
779 || user->exp.X_add_number == -1
780 || user->exp.X_add_number == 1
781 || user->exp.X_add_number == 2))
783 immediate_inpage = user->page;
784 immediate = user->exp;
789 if (user->type == RN)
796 if (user->type == RN)
803 if (user->type == RNIND)
815 if (user->type == this_try->arg_type[i])
822 if (user->type == RN && user->reg == 7)
828 printf (_("unhandled %d\n"), this_try->arg_type[i]);
832 /* If we get here this didn't work out */
847 check (operand, low, high)
848 expressionS *operand;
852 if (operand->X_op != O_constant
853 || operand->X_add_number < low
854 || operand->X_add_number > high)
856 as_bad (_("operand must be absolute in range %d..%d"), low, high);
858 return operand->X_add_number;
862 insert (output, index, exp, reloc, pcrel)
869 fix_new_exp (frag_now,
870 output - frag_now->fr_literal + index,
871 4, /* always say size is 4, but we know better */
878 build_relaxable_instruction (opcode, operand)
879 h8500_opcode_info *opcode;
880 h8500_operand_info *operand;
882 /* All relaxable instructions start life as two bytes but can become
883 three bytes long if a lonely branch and up to 9 bytes if long
889 if (opcode->bytes[0].contents == 0x01)
893 else if (opcode->bytes[0].contents == 0x06
894 || opcode->bytes[0].contents == 0x07)
903 p = frag_var (rs_machine_dependent,
904 md_relax_table[C (type, WORD_DISP)].rlx_length,
905 len = md_relax_table[C (type, BYTE_DISP)].rlx_length,
906 C (type, UNDEF_BYTE_DISP),
907 displacement.X_add_symbol,
908 displacement.X_add_number,
911 p[0] = opcode->bytes[0].contents;
914 p[1] = opcode->bytes[1].contents | rs;
918 /* Now we know what sort of opcodes it is, let's build the bytes. */
921 build_bytes (opcode, operand)
922 h8500_opcode_info *opcode;
923 h8500_operand_info *operand;
931 build_relaxable_instruction (opcode, operand);
935 char *output = frag_more (opcode->length);
937 memset (output, 0, opcode->length);
938 for (index = 0; index < opcode->length; index++)
940 output[index] = opcode->bytes[index].contents;
942 switch (opcode->bytes[index].insert)
945 printf (_("failed for %d\n"), opcode->bytes[index].insert);
960 insert (output, index, &displacement, R_H8500_IMM16, 0);
965 insert (output, index, &displacement, R_H8500_IMM8, 0);
971 switch (immediate_inpage)
983 insert (output, index, &immediate, p, 0);
989 if (immediate_inpage)
990 insert (output, index, &immediate, R_H8500_HIGH8, 0);
992 insert (output, index, &immediate, R_H8500_IMM8, 0);
995 insert (output, index, &displacement, R_H8500_PCREL16, 1);
999 insert (output, index, &displacement, R_H8500_PCREL8, 1);
1002 output[index] |= check (&immediate, 0, 15);
1005 output[index] |= cr;
1012 output[index] |= crb;
1016 output[index] |= crw;
1020 insert (output, index, &absolute, R_H8500_IMM24, 0);
1024 insert (output, index, &absolute, R_H8500_IMM16, 0);
1028 insert (output, index, &absolute, R_H8500_IMM8, 0);
1031 switch (immediate.X_add_number)
1034 output[index] |= 0x5;
1037 output[index] |= 0x4;
1052 /* This is the guts of the machine-dependent assembler. STR points to
1053 a machine dependent instruction. This function is supposed to emit
1054 the frags/bytes it assembles to. */
1062 h8500_operand_info operand[2];
1063 h8500_opcode_info *opcode;
1064 h8500_opcode_info *prev_opcode;
1069 /* Drop leading whitespace. */
1073 /* Find the op code end. */
1074 for (op_start = op_end = str;
1075 !is_end_of_line[(unsigned char) *op_end] && *op_end != ' ';
1078 if ( /**op_end != '.'
1082 name[nlen++] = *op_end;
1087 if (op_end == op_start)
1088 as_bad (_("can't find opcode "));
1090 opcode = (h8500_opcode_info *) hash_find (opcode_hash_control, name);
1094 as_bad (_("unknown opcode"));
1098 get_operands (opcode, op_end, operand);
1099 prev_opcode = opcode;
1101 opcode = get_specific (opcode, operand);
1105 /* Couldn't find an opcode which matched the operands */
1106 char *where = frag_more (2);
1110 as_bad (_("invalid operands for opcode"));
1114 build_bytes (opcode, operand);
1118 tc_crawl_symbol_chain (headers)
1119 object_headers *headers;
1121 printf (_("call to tc_crawl_symbol_chain \n"));
1125 md_undefined_symbol (name)
1132 tc_headers_hook (headers)
1133 object_headers *headers;
1135 printf (_("call to tc_headers_hook \n"));
1138 /* Various routines to kill one day. */
1139 /* Equal to MAX_PRECISION in atof-ieee.c. */
1140 #define MAX_LITTLENUMS 6
1142 /* Turn a string in input_line_pointer into a floating point constant
1143 of type type, and store the appropriate bytes in *LITP. The number
1144 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1145 returned, or NULL on OK. */
1148 md_atof (type, litP, sizeP)
1154 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1155 LITTLENUM_TYPE *wordP;
1187 return _("Bad call to MD_ATOF()");
1189 t = atof_ieee (input_line_pointer, type, words);
1191 input_line_pointer = t;
1193 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1194 for (wordP = words; prec--;)
1196 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1197 litP += sizeof (LITTLENUM_TYPE);
1202 const char *md_shortopts = "";
1203 struct option md_longopts[] = {
1204 {NULL, no_argument, NULL, 0}
1206 size_t md_longopts_size = sizeof (md_longopts);
1209 md_parse_option (c, arg)
1217 md_show_usage (stream)
1223 tc_aout_fix_to_chars ()
1225 printf (_("call to tc_aout_fix_to_chars \n"));
1230 wordify_scb (buffer, disp_size, inst_size)
1235 int rn = buffer[1] & 0x7;
1239 case 0x0e: /* BSR */
1273 *buffer++ = 0x26; /* bne + 8 */
1279 *buffer++ = 0x27; /* bne + 8 */
1284 *buffer++ = 0xa8 | rn; /* addq -1,rn */
1286 *buffer++ = 0x04; /* cmp #0xff:8, rn */
1288 *buffer++ = 0x70 | rn;
1289 *buffer++ = 0x36; /* bne ... */
1294 /* Called after relaxing, change the frags so they know how big they
1298 md_convert_frag (headers, seg, fragP)
1299 object_headers *headers;
1305 char *buffer = fragP->fr_fix + fragP->fr_literal;
1307 switch (fragP->fr_subtype)
1309 case C (BRANCH, BYTE_DISP):
1314 case C (SCB_F, BYTE_DISP):
1315 case C (SCB_TST, BYTE_DISP):
1320 /* Branches to a known 16 bit displacement. */
1322 /* Turn on the 16bit bit. */
1323 case C (BRANCH, WORD_DISP):
1324 case C (SCB_F, WORD_DISP):
1325 case C (SCB_TST, WORD_DISP):
1326 wordify_scb (buffer, &disp_size, &inst_size);
1329 case C (BRANCH, UNDEF_WORD_DISP):
1330 case C (SCB_F, UNDEF_WORD_DISP):
1331 case C (SCB_TST, UNDEF_WORD_DISP):
1332 /* This tried to be relaxed, but didn't manage it, it now needs
1334 wordify_scb (buffer, &disp_size, &inst_size);
1338 fragP->fr_fix + inst_size,
1345 fragP->fr_fix += disp_size + inst_size;
1353 /* Get the address of the end of the instruction */
1354 int next_inst = fragP->fr_fix + fragP->fr_address + disp_size + inst_size;
1355 int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
1357 int disp = targ_addr - next_inst;
1359 md_number_to_chars (buffer + inst_size, disp, disp_size);
1360 fragP->fr_fix += disp_size + inst_size;
1365 md_section_align (seg, size)
1369 return ((size + (1 << section_alignment[(int) seg]) - 1)
1370 & (-1 << section_alignment[(int) seg]));
1375 md_apply_fix3 (fixP, valP, seg)
1378 segT seg ATTRIBUTE_UNUSED;
1380 long val = * (long *) valP;
1381 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1383 if (fixP->fx_r_type == 0)
1384 fixP->fx_r_type = fixP->fx_size == 4 ? R_H8500_IMM32 : R_H8500_IMM16;
1386 switch (fixP->fx_r_type)
1389 case R_H8500_PCREL8:
1394 case R_H8500_PCREL16:
1395 *buf++ = (val >> 8);
1401 case R_H8500_HIGH16:
1406 *buf++ = (val >> 16);
1407 *buf++ = (val >> 8);
1411 *buf++ = (val >> 24);
1412 *buf++ = (val >> 16);
1413 *buf++ = (val >> 8);
1420 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1424 /* Called just before address relaxation, return the length
1425 by which a fragment must grow to reach it's destination. */
1428 md_estimate_size_before_relax (fragP, segment_type)
1429 register fragS *fragP;
1430 register segT segment_type;
1434 switch (fragP->fr_subtype)
1439 case C (BRANCH, UNDEF_BYTE_DISP):
1440 case C (SCB_F, UNDEF_BYTE_DISP):
1441 case C (SCB_TST, UNDEF_BYTE_DISP):
1442 what = GET_WHAT (fragP->fr_subtype);
1443 /* used to be a branch to somewhere which was unknown */
1444 if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1446 /* Got a symbol and it's defined in this segment, become byte
1447 sized - maybe it will fix up. */
1448 fragP->fr_subtype = C (what, BYTE_DISP);
1452 /* Its got a segment, but its not ours, so it will always be
1454 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1458 case C (BRANCH, BYTE_DISP):
1459 case C (BRANCH, WORD_DISP):
1460 case C (BRANCH, UNDEF_WORD_DISP):
1461 case C (SCB_F, BYTE_DISP):
1462 case C (SCB_F, WORD_DISP):
1463 case C (SCB_F, UNDEF_WORD_DISP):
1464 case C (SCB_TST, BYTE_DISP):
1465 case C (SCB_TST, WORD_DISP):
1466 case C (SCB_TST, UNDEF_WORD_DISP):
1467 /* When relaxing a section for the second time, we don't need to
1468 do anything besides return the current size. */
1472 return md_relax_table[fragP->fr_subtype].rlx_length;
1475 /* Put number into target byte order. */
1478 md_number_to_chars (ptr, use, nbytes)
1483 number_to_chars_bigendian (ptr, use, nbytes);
1487 md_pcrel_from (fixP)
1490 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1494 tc_coff_symbol_emit_hook (ignore)
1500 tc_coff_fix2rtype (fix_ptr)
1503 if (fix_ptr->fx_r_type == RELOC_32)
1505 /* cons likes to create reloc32's whatever the size of the reloc..
1507 switch (fix_ptr->fx_size)
1510 return R_H8500_IMM16;
1513 return R_H8500_IMM8;
1519 return fix_ptr->fx_r_type;
1523 tc_reloc_mangle (fix_ptr, intr, base)
1525 struct internal_reloc *intr;
1529 symbolS *symbol_ptr;
1531 symbol_ptr = fix_ptr->fx_addsy;
1533 /* If this relocation is attached to a symbol then it's ok
1535 if (fix_ptr->fx_r_type == RELOC_32)
1537 /* cons likes to create reloc32's whatever the size of the reloc..
1539 switch (fix_ptr->fx_size)
1542 intr->r_type = R_IMM16;
1545 intr->r_type = R_IMM8;
1553 intr->r_type = fix_ptr->fx_r_type;
1556 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1557 intr->r_offset = fix_ptr->fx_offset;
1559 /* Turn the segment of the symbol into an offset. */
1564 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1569 segment_info[S_GET_SEGMENT (symbol_ptr)].scnhdr.s_paddr;
1571 intr->r_offset += S_GET_VALUE (symbol_ptr);
1572 intr->r_symndx = dot->sy_number;
1576 intr->r_symndx = symbol_ptr->sy_number;
1582 intr->r_symndx = -1;
1591 /* Check for :s.w */
1592 if (ISALPHA (ptr[1]) && ptr[2] == '.')
1595 if (ISALPHA (ptr[1]) && !ISALPHA (ptr[2]))
1601 tc_coff_sizemachdep (frag)
1604 return md_relax_table[frag->fr_subtype].rlx_length;