1 /* tc-h8500.c -- Assemble code for the Renesas H8/500
2 Copyright 1993, 1994, 1995, 1998, 2000, 2001, 2002, 2003, 2005
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
22 /* Written By Steve Chamberlain <sac@cygnus.com>. */
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. */
43 const pseudo_typeS md_pseudo_table[] =
49 {"form", listing_psize, 0},
50 {"heading", listing_title, 0},
51 {"import", s_ignore, 0},
52 {"page", listing_eject, 0},
53 {"program", s_ignore, 0},
57 const int md_reloc_size;
59 const char EXP_CHARS[] = "eE";
61 /* Chars that mean this number is a floating point constant.
64 const char FLT_CHARS[] = "rRsSfFdDxXpP";
66 #define C(a,b) ENCODE_RELAX(a, b)
67 #define ENCODE_RELAX(what, length) (((what) << 2) + (length))
69 #define GET_WHAT(x) ((x >> 2))
73 #define UNDEF_BYTE_DISP 0
74 #define UNDEF_WORD_DISP 3
86 relax_typeS md_relax_table[C (END, 0)] =
95 { BYTE_F, BYTE_B, 2, C (BRANCH, WORD_DISP) },
96 { WORD_F, WORD_B, 3, 0 },
101 { BYTE_F, BYTE_B, 3, C (SCB_F, WORD_DISP) },
102 { WORD_F, WORD_B, 8, 0 },
107 { BYTE_F, BYTE_B, 3, C (SCB_TST, WORD_DISP) },
108 { WORD_F, WORD_B, 10, 0 },
113 static struct hash_control *opcode_hash_control; /* Opcode mnemonics. */
115 /* This function is called once, at assembler startup time. This should
116 set up all the tables, etc. that the MD part of the assembler needs. */
121 const h8500_opcode_info *opcode;
122 char prev_buffer[100];
125 opcode_hash_control = hash_new ();
128 /* Insert unique names into hash table. */
129 for (opcode = h8500_table; opcode->name; opcode++)
131 if (idx != opcode->idx)
133 hash_insert (opcode_hash_control, opcode->name, (char *) opcode);
139 static int rn; /* Register number used by RN. */
140 static int rs; /* Register number used by RS. */
141 static int rd; /* Register number used by RD. */
142 static int crb; /* Byte size cr. */
143 static int crw; /* Word sized cr. */
144 static int cr; /* Unknown size cr. */
146 static expressionS displacement;/* Displacement expression. */
147 static int immediate_inpage;
148 static expressionS immediate; /* Immediate expression. */
149 static expressionS absolute; /* Absolute expression. */
161 /* Try to parse a reg name. Return the number of chars consumed. */
163 static int parse_reg (char *, int *, unsigned int *);
166 parse_reg (char *src, int *mode, unsigned int *reg)
171 /* Cribbed from get_symbol_end(). */
172 if (!is_name_beginner (*src) || *src == '\001')
175 while (is_part_of_name (*end) || *end == '\001')
179 if (len == 2 && src[0] == 'r')
181 if (src[1] >= '0' && src[1] <= '7')
184 *reg = (src[1] - '0');
188 if (len == 2 && src[0] == 's' && src[1] == 'p')
194 if (len == 3 && src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
200 if (len == 2 && src[0] == 's' && src[1] == 'r')
206 if (len == 2 && src[0] == 'b' && src[1] == 'r')
212 if (len == 2 && src[0] == 'e' && src[1] == 'p')
218 if (len == 2 && src[0] == 'd' && src[1] == 'p')
224 if (len == 2 && src[0] == 't' && src[1] == 'p')
230 if (len == 2 && src[0] == 'f' && src[1] == 'p')
240 parse_exp (char *s, expressionS *op, int *page)
245 save = input_line_pointer;
250 if (s[1] == 'p' && s[2] == 'a' && s[3] == 'g' && s[4] == 'e')
255 if (s[1] == 'h' && s[2] == 'i' && s[3] == '1' && s[4] == '6')
260 else if (s[1] == 'o' && s[2] == 'f' && s[3] == 'f')
267 input_line_pointer = s;
270 if (op->X_op == O_absent)
271 as_bad (_("missing operand"));
272 new = input_line_pointer;
273 input_line_pointer = save;
279 exp_signed, exp_unsigned, exp_sandu
283 skip_colonthing (sign_type sign,
285 h8500_operand_info *exp,
291 ptr = parse_exp (ptr, &exp->exp, &exp->page);
300 else if (ptr[0] == '1' && ptr[1] == '6')
305 else if (ptr[0] == '2' && ptr[1] == '4')
309 as_bad (_(":24 not valid for this opcode"));
316 as_bad (_("expect :8,:16 or :24"));
322 if (exp->page == 'p')
324 else if (exp->page == 'h')
328 /* Let's work out the size from the context. */
329 int n = exp->exp.X_add_number;
332 && exp->exp.X_op == O_constant
333 && ((sign == exp_signed && (n >= -128 && n <= 127))
334 || (sign == exp_unsigned && (n >= 0 && (n <= 255)))
335 || (sign == exp_sandu && (n >= -128 && (n <= 255)))))
345 parse_reglist (char *src, h8500_operand_info *op)
354 while (src[idx] && src[idx] != ')')
356 int done = parse_reg (src + idx, &mode, &rn);
365 as_bad (_("syntax error in reg list"));
372 done = parse_reg (src + idx, &mode, &rm);
383 as_bad (_("missing final register in range"));
390 op->exp.X_add_symbol = 0;
391 op->exp.X_op_symbol = 0;
392 op->exp.X_add_number = mask;
393 op->exp.X_op = O_constant;
394 op->exp.X_unsigned = 1;
399 /* The many forms of operand:
402 @Rn Register indirect
403 @(disp[:size], Rn) Register indirect with displacement
407 #xx[:size] immediate data. */
410 get_operand (char **ptr,
411 h8500_operand_info *op,
420 if (src[0] == '(' && src[1] == 'r')
422 /* This is a register list */
423 *ptr = src + parse_reglist (src, op);
427 len = parse_reg (src, &op->type, &op->reg);
441 len = parse_reg (src, &mode, &num);
444 /* Oops, not a reg after all, must be ordinary exp */
446 /* must be a symbol */
447 *ptr = skip_colonthing (exp_unsigned, src,
448 op, ABS16, ABS8, ABS16, ABS24);
462 src = skip_colonthing (exp_signed, src,
463 op, RNIND_D16, RNIND_D8, RNIND_D16, 0);
467 as_bad (_("expected @(exp, Rn)"));
471 len = parse_reg (src, &mode, &op->reg);
472 if (len == 0 || mode != RN)
474 as_bad (_("expected @(exp, Rn)"));
480 as_bad (_("expected @(exp, Rn)"));
486 len = parse_reg (src, &mode, &num);
496 as_bad (_("@Rn+ needs word register"));
506 as_bad (_("@Rn needs word register"));
516 /* must be a symbol */
518 skip_colonthing (exp_unsigned, src, op,
519 ispage ? ABS24 : ABS16, ABS8, ABS16, ABS24);
527 *ptr = skip_colonthing (exp_sandu, src, op, IMM16, IMM8, IMM16, ABS24);
531 *ptr = skip_colonthing (exp_signed, src, op,
532 ispage ? ABS24 : PCREL8, PCREL8, PCREL16, ABS24);
536 get_operands (h8500_opcode_info *info,
538 h8500_operand_info *operand)
551 get_operand (&ptr, operand + 0, info->name[0] == 'p');
557 get_operand (&ptr, operand + 0, 0);
560 get_operand (&ptr, operand + 1, 0);
570 /* Passed a pointer to a list of opcodes which use different
571 addressing modes, return the opcode which matches the opcodes
574 int pcrel8; /* Set when we've seen a pcrel operand. */
576 static h8500_opcode_info *
577 get_specific (h8500_opcode_info *opcode,
578 h8500_operand_info *operands)
580 h8500_opcode_info *this_try = opcode;
582 unsigned int noperands = opcode->nargs;
583 int this_index = opcode->idx;
585 while (this_index == opcode->idx && !found)
591 /* Look at both operands needed by the opcodes and provided by
593 for (i = 0; i < noperands; i++)
595 h8500_operand_info *user = operands + i;
597 switch (this_try->arg_type[i])
600 /* Opcode needs (disp:8,fp). */
601 if (user->type == RNIND_D8 && user->reg == 6)
603 displacement = user->exp;
608 if (user->type == RNIND_D16)
610 displacement = user->exp;
616 if (user->type == RNIND_D8)
618 displacement = user->exp;
625 if (user->type == this_try->arg_type[i])
627 displacement = user->exp;
634 if (user->type == RNDEC && user->reg == 7)
639 if (user->type == RNINC && user->reg == 7)
644 if (user->type == ABS16)
646 absolute = user->exp;
651 if (user->type == ABS8)
653 absolute = user->exp;
658 if (user->type == ABS24)
660 absolute = user->exp;
666 if ((user->type == CRB || user->type == CR) && user->reg != 0)
673 if ((user->type == CRW || user->type == CR) && user->reg == 0)
680 if (user->type == DISP16)
682 displacement = user->exp;
687 if (user->type == DISP8)
689 displacement = user->exp;
694 if (user->type == RN && user->reg == 6)
699 if (user->type == PCREL16)
701 displacement = user->exp;
706 if (user->type == PCREL8)
708 displacement = user->exp;
715 if (user->type == IMM16
716 || user->type == IMM8)
718 immediate_inpage = user->page;
719 immediate = user->exp;
725 if (user->type == IMM8)
727 immediate_inpage = user->page;
728 immediate = user->exp;
733 if (user->type == IMM8)
735 immediate_inpage = user->page;
736 immediate = user->exp;
741 if (user->type == IMM8
742 && user->exp.X_op == O_constant
743 && (user->exp.X_add_number == -2
744 || user->exp.X_add_number == -1
745 || user->exp.X_add_number == 1
746 || user->exp.X_add_number == 2))
748 immediate_inpage = user->page;
749 immediate = user->exp;
754 if (user->type == RN)
761 if (user->type == RN)
768 if (user->type == RNIND)
779 if (user->type == this_try->arg_type[i])
786 if (user->type == RN && user->reg == 7)
790 printf (_("unhandled %d\n"), this_try->arg_type[i]);
794 /* If we get here this didn't work out. */
809 check (expressionS *operand,
813 if (operand->X_op != O_constant
814 || operand->X_add_number < low
815 || operand->X_add_number > high)
816 as_bad (_("operand must be absolute in range %d..%d"), low, high);
818 return operand->X_add_number;
822 insert (char *output, int index, expressionS *exp, int reloc, int pcrel)
824 fix_new_exp (frag_now,
825 output - frag_now->fr_literal + index,
826 4, /* Always say size is 4, but we know better. */
831 build_relaxable_instruction (h8500_opcode_info *opcode,
832 h8500_operand_info *operand ATTRIBUTE_UNUSED)
834 /* All relaxable instructions start life as two bytes but can become
835 three bytes long if a lonely branch and up to 9 bytes if long
841 if (opcode->bytes[0].contents == 0x01)
843 else if (opcode->bytes[0].contents == 0x06
844 || opcode->bytes[0].contents == 0x07)
849 p = frag_var (rs_machine_dependent,
850 md_relax_table[C (type, WORD_DISP)].rlx_length,
851 len = md_relax_table[C (type, BYTE_DISP)].rlx_length,
852 C (type, UNDEF_BYTE_DISP),
853 displacement.X_add_symbol,
854 displacement.X_add_number,
857 p[0] = opcode->bytes[0].contents;
859 p[1] = opcode->bytes[1].contents | rs;
862 /* Now we know what sort of opcodes it is, let's build the bytes. */
865 build_bytes (h8500_opcode_info *opcode, h8500_operand_info *operand)
872 build_relaxable_instruction (opcode, operand);
876 char *output = frag_more (opcode->length);
878 memset (output, 0, opcode->length);
879 for (index = 0; index < opcode->length; index++)
881 output[index] = opcode->bytes[index].contents;
883 switch (opcode->bytes[index].insert)
886 printf (_("failed for %d\n"), opcode->bytes[index].insert);
901 insert (output, index, &displacement, R_H8500_IMM16, 0);
906 insert (output, index, &displacement, R_H8500_IMM8, 0);
912 switch (immediate_inpage)
924 insert (output, index, &immediate, p, 0);
930 if (immediate_inpage)
931 insert (output, index, &immediate, R_H8500_HIGH8, 0);
933 insert (output, index, &immediate, R_H8500_IMM8, 0);
936 insert (output, index, &displacement, R_H8500_PCREL16, 1);
940 insert (output, index, &displacement, R_H8500_PCREL8, 1);
943 output[index] |= check (&immediate, 0, 15);
953 output[index] |= crb;
957 output[index] |= crw;
961 insert (output, index, &absolute, R_H8500_IMM24, 0);
965 insert (output, index, &absolute, R_H8500_IMM16, 0);
969 insert (output, index, &absolute, R_H8500_IMM8, 0);
972 switch (immediate.X_add_number)
975 output[index] |= 0x5;
978 output[index] |= 0x4;
993 /* This is the guts of the machine-dependent assembler. STR points to
994 a machine dependent instruction. This function is supposed to emit
995 the frags/bytes it assembles to. */
998 md_assemble (char *str)
1002 h8500_operand_info operand[2];
1003 h8500_opcode_info *opcode;
1004 h8500_opcode_info *prev_opcode;
1009 /* Drop leading whitespace. */
1013 /* Find the op code end. */
1014 for (op_start = op_end = str;
1015 !is_end_of_line[(unsigned char) *op_end] && *op_end != ' ';
1018 name[nlen++] = *op_end;
1022 if (op_end == op_start)
1023 as_bad (_("can't find opcode "));
1025 opcode = (h8500_opcode_info *) hash_find (opcode_hash_control, name);
1029 as_bad (_("unknown opcode"));
1033 get_operands (opcode, op_end, operand);
1034 prev_opcode = opcode;
1036 opcode = get_specific (opcode, operand);
1040 /* Couldn't find an opcode which matched the operands. */
1041 char *where = frag_more (2);
1045 as_bad (_("invalid operands for opcode"));
1049 build_bytes (opcode, operand);
1053 tc_crawl_symbol_chain (object_headers *headers ATTRIBUTE_UNUSED)
1055 printf (_("call to tc_crawl_symbol_chain \n"));
1059 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1065 tc_headers_hook (object_headers *headers ATTRIBUTE_UNUSED)
1067 printf (_("call to tc_headers_hook \n"));
1070 /* Various routines to kill one day. */
1071 /* Equal to MAX_PRECISION in atof-ieee.c. */
1072 #define MAX_LITTLENUMS 6
1074 /* Turn a string in input_line_pointer into a floating point constant
1075 of type type, and store the appropriate bytes in *LITP. The number
1076 of LITTLENUMS emitted is stored in *SIZEP. An error message is
1077 returned, or NULL on OK. */
1080 md_atof (int type, char *litP, int *sizeP)
1083 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1084 LITTLENUM_TYPE *wordP;
1115 return _("Bad call to MD_ATOF()");
1117 t = atof_ieee (input_line_pointer, type, words);
1119 input_line_pointer = t;
1121 *sizeP = prec * sizeof (LITTLENUM_TYPE);
1122 for (wordP = words; prec--;)
1124 md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE));
1125 litP += sizeof (LITTLENUM_TYPE);
1130 const char *md_shortopts = "";
1131 struct option md_longopts[] =
1133 {NULL, no_argument, NULL, 0}
1135 size_t md_longopts_size = sizeof (md_longopts);
1138 md_parse_option (int c ATTRIBUTE_UNUSED, char *arg ATTRIBUTE_UNUSED)
1144 md_show_usage (FILE *stream ATTRIBUTE_UNUSED)
1149 wordify_scb (char *buffer, int *disp_size, int *inst_size)
1151 int rn = buffer[1] & 0x7;
1155 case 0x0e: /* BSR */
1189 *buffer++ = 0x26; /* bne + 8 */
1195 *buffer++ = 0x27; /* bne + 8 */
1200 *buffer++ = 0xa8 | rn; /* addq -1,rn */
1202 *buffer++ = 0x04; /* cmp #0xff:8, rn */
1204 *buffer++ = 0x70 | rn;
1205 *buffer++ = 0x36; /* bne ... */
1210 /* Called after relaxing, change the frags so they know how big they
1214 md_convert_frag (object_headers *headers ATTRIBUTE_UNUSED,
1215 segT seg ATTRIBUTE_UNUSED,
1220 char *buffer = fragP->fr_fix + fragP->fr_literal;
1222 switch (fragP->fr_subtype)
1224 case C (BRANCH, BYTE_DISP):
1229 case C (SCB_F, BYTE_DISP):
1230 case C (SCB_TST, BYTE_DISP):
1235 /* Branches to a known 16 bit displacement. */
1237 /* Turn on the 16bit bit. */
1238 case C (BRANCH, WORD_DISP):
1239 case C (SCB_F, WORD_DISP):
1240 case C (SCB_TST, WORD_DISP):
1241 wordify_scb (buffer, &disp_size, &inst_size);
1244 case C (BRANCH, UNDEF_WORD_DISP):
1245 case C (SCB_F, UNDEF_WORD_DISP):
1246 case C (SCB_TST, UNDEF_WORD_DISP):
1247 /* This tried to be relaxed, but didn't manage it, it now needs
1249 wordify_scb (buffer, &disp_size, &inst_size);
1253 fragP->fr_fix + inst_size,
1260 fragP->fr_fix += disp_size + inst_size;
1268 /* Get the address of the end of the instruction. */
1269 int next_inst = fragP->fr_fix + fragP->fr_address + disp_size + inst_size;
1270 int targ_addr = (S_GET_VALUE (fragP->fr_symbol) +
1272 int disp = targ_addr - next_inst;
1274 md_number_to_chars (buffer + inst_size, disp, disp_size);
1275 fragP->fr_fix += disp_size + inst_size;
1280 md_section_align (segT seg, valueT size)
1282 return ((size + (1 << section_alignment[(int) seg]) - 1)
1283 & (-1 << section_alignment[(int) seg]));
1287 md_apply_fix3 (fixS *fixP, valueT * valP, segT seg ATTRIBUTE_UNUSED)
1289 long val = * (long *) valP;
1290 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1292 if (fixP->fx_r_type == 0)
1293 fixP->fx_r_type = fixP->fx_size == 4 ? R_H8500_IMM32 : R_H8500_IMM16;
1295 switch (fixP->fx_r_type)
1298 case R_H8500_PCREL8:
1303 case R_H8500_PCREL16:
1304 *buf++ = (val >> 8);
1310 case R_H8500_HIGH16:
1315 *buf++ = (val >> 16);
1316 *buf++ = (val >> 8);
1320 *buf++ = (val >> 24);
1321 *buf++ = (val >> 16);
1322 *buf++ = (val >> 8);
1329 if (fixP->fx_addsy == NULL && fixP->fx_pcrel == 0)
1333 /* Called just before address relaxation, return the length
1334 by which a fragment must grow to reach it's destination. */
1337 md_estimate_size_before_relax (fragS *fragP, segT segment_type)
1341 switch (fragP->fr_subtype)
1346 case C (BRANCH, UNDEF_BYTE_DISP):
1347 case C (SCB_F, UNDEF_BYTE_DISP):
1348 case C (SCB_TST, UNDEF_BYTE_DISP):
1349 what = GET_WHAT (fragP->fr_subtype);
1350 /* Used to be a branch to somewhere which was unknown. */
1351 if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type)
1353 /* Got a symbol and it's defined in this segment, become byte
1354 sized - maybe it will fix up. */
1355 fragP->fr_subtype = C (what, BYTE_DISP);
1358 /* Its got a segment, but its not ours, so it will always be
1360 fragP->fr_subtype = C (what, UNDEF_WORD_DISP);
1363 case C (BRANCH, BYTE_DISP):
1364 case C (BRANCH, WORD_DISP):
1365 case C (BRANCH, UNDEF_WORD_DISP):
1366 case C (SCB_F, BYTE_DISP):
1367 case C (SCB_F, WORD_DISP):
1368 case C (SCB_F, UNDEF_WORD_DISP):
1369 case C (SCB_TST, BYTE_DISP):
1370 case C (SCB_TST, WORD_DISP):
1371 case C (SCB_TST, UNDEF_WORD_DISP):
1372 /* When relaxing a section for the second time, we don't need to
1373 do anything besides return the current size. */
1377 return md_relax_table[fragP->fr_subtype].rlx_length;
1380 /* Put number into target byte order. */
1383 md_number_to_chars (char *ptr, valueT use, int nbytes)
1385 number_to_chars_bigendian (ptr, use, nbytes);
1389 md_pcrel_from (fixS *fixP)
1391 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
1395 tc_coff_symbol_emit_hook (symbolS *ignore ATTRIBUTE_UNUSED)
1400 tc_coff_fix2rtype (fixS *fix_ptr)
1402 if (fix_ptr->fx_r_type == RELOC_32)
1404 /* Cons likes to create reloc32's whatever the size of the reloc. */
1405 switch (fix_ptr->fx_size)
1408 return R_H8500_IMM16;
1411 return R_H8500_IMM8;
1417 return fix_ptr->fx_r_type;
1421 tc_reloc_mangle (fixS *fix_ptr,
1422 struct internal_reloc *intr,
1425 symbolS *symbol_ptr;
1427 symbol_ptr = fix_ptr->fx_addsy;
1429 /* If this relocation is attached to a symbol then it's ok
1431 if (fix_ptr->fx_r_type == RELOC_32)
1433 /* Cons likes to create reloc32's whatever the size of the reloc. */
1434 switch (fix_ptr->fx_size)
1437 intr->r_type = R_IMM16;
1440 intr->r_type = R_IMM8;
1447 intr->r_type = fix_ptr->fx_r_type;
1449 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where + base;
1450 intr->r_offset = fix_ptr->fx_offset;
1452 /* Turn the segment of the symbol into an offset. */
1457 dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot;
1460 intr->r_offset += S_GET_VALUE (symbol_ptr);
1461 intr->r_symndx = dot->sy_number;
1464 intr->r_symndx = symbol_ptr->sy_number;
1467 intr->r_symndx = -1;
1471 start_label (char *ptr)
1473 /* Check for :s.w */
1474 if (ISALPHA (ptr[1]) && ptr[2] == '.')
1477 if (ISALPHA (ptr[1]) && !ISALPHA (ptr[2]))
1483 tc_coff_sizemachdep (fragS *frag)
1485 return md_relax_table[frag->fr_subtype].rlx_length;