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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 Written By Steve Chamberlain
29 #include "opcode/h8300.h"
33 char comment_chars[] = { ';',0 };
34 char line_separator_chars[] = { '$' ,0};
36 /* This table describes all the machine specific pseudo-ops the assembler
37 has to support. The fields are:
38 pseudo-op name without dot
39 function to call to execute this pseudo-op
40 Integer arg to pass to the function
45 const pseudo_typeS md_pseudo_table[] =
48 { "data.b", cons, 1 },
49 { "data.w", cons, 2 },
50 { "data.l", cons, 4 },
51 { "form", listing_psize, 0 },
52 { "heading", listing_title, 0},
53 { "import", s_ignore, 0},
54 { "page", listing_eject, 0},
55 { "program", s_ignore, 0},
61 const char EXP_CHARS[] = "eE";
63 /* Chars that mean this number is a floating point constant */
66 char FLT_CHARS[] = "rRsSfFdDxXpP";
69 const relax_typeS md_relax_table[1];
72 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
76 This function is called once, at assembler startup time. This should
77 set up all the tables, etc that the MD part of the assembler needs
80 /* encode the size and number into the number field
96 struct reg_entry reg_list[] = {
134 struct h8_opcode *opcode;
135 const struct reg_entry *reg;
136 char prev_buffer[100];
139 opcode_hash_control = hash_new();
142 for (opcode = h8_opcodes; opcode->name; opcode++)
144 /* Strip off any . part when inserting the opcode and only enter
145 unique codes into the hash table
147 char *src= opcode->name;
148 unsigned int len = strlen(src);
149 char *dst = malloc(len+1);
161 if (strcmp(buffer, prev_buffer))
163 hash_insert(opcode_hash_control, buffer, (char *)opcode);
164 strcpy(prev_buffer, buffer);
170 /* Find the number of operands */
171 opcode->noperands = 0;
172 while (opcode->args.nib[opcode->noperands] != E)
173 opcode->noperands ++;
174 /* Find the length of the opcode in bytes */
176 while (opcode->data.nib[opcode->length*2] != E)
190 unsigned int dispreg;
200 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
209 op_type r8_sord[] = {RS8, RD8};
210 op_type r16_sord[] = {RS16, RD16};
211 op_type rind_sord[] = {RSIND, RDIND};
212 op_type abs_sord[2] = {ABS16SRC, ABS16DST};
213 op_type disp_sord[] = {DISPSRC, DISPDST};
215 /* try and parse a reg name, returns number of chars consumed */
217 DEFUN(parse_reg,(src, mode, reg, dst),
220 unsigned int *reg AND
223 if (src[0] == 's' && src[1] == 'p')
225 *mode = r16_sord[dst];
229 if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
235 if (src[0] == 'f' && src[1] == 'p')
237 *mode = r16_sord[dst];
243 if (src[1] >= '0' && src[1] <= '7')
247 *mode = r8_sord[dst];
248 *reg = (src[1] - '0') + 8;
253 *mode = r8_sord[dst];
254 *reg = (src[1] - '0') ;
257 *mode = r16_sord[dst];
258 *reg = (src[1] - '0');
266 DEFUN(parse_exp,(s, op),
270 char *save = input_line_pointer;
273 input_line_pointer = s;
275 new = input_line_pointer;
276 input_line_pointer = save;
287 as_bad("Missing operand");
290 as_bad("Don't understand operand of type %s", segment_name (seg));
296 DEFUN(skip_colonthing,(ptr),
301 while (isdigit(*ptr))
308 /* The many forms of operand:
311 @Rn Register indirect
312 @(exp[:16], Rn) Register indirect with displacement
316 @aa:16 absolute 16 bit
319 #xx[:size] immediate data
320 @(exp:[8], pc) pc rel
321 @@aa[:8] memory indirect
326 DEFUN(get_operand,(ptr, op, dst),
338 len = parse_reg(src, &op->mode, &op->reg, dst);
350 src = parse_exp(src,&op->exp);
351 src = skip_colonthing(src);
364 len = parse_reg(src, &mode, &num, dst);
367 /* Oops, not a reg after all, must be ordinary exp */
369 /* must be a symbol */
370 op->mode = abs_sord[dst];
371 *ptr = skip_colonthing(parse_exp(src, &op->exp));
378 if (mode != r16_sord[dst])
380 as_bad("@- needs word register");
387 if (*src == '(' && ')')
391 src = parse_exp(src, &op->exp);
396 op->mode = abs_sord[dst];
400 src = skip_colonthing(src);
404 as_bad("expected @(exp, reg16)");
409 len = parse_reg(src, &mode, &op->reg, dst);
410 if (len == 0 || mode != r16_sord[dst])
412 as_bad("expected @(exp, reg16)");
415 op->mode = disp_sord[dst];
417 src = skip_colonthing(src);
419 if (*src != ')' && '(')
421 as_bad("expected @(exp, reg16)");
428 len = parse_reg(src, &mode, &num, dst);
437 as_bad("@Rn+ needs src word register");
445 if (mode != r16_sord[dst])
447 as_bad("@Rn needs word register");
451 op->mode =rind_sord[dst];
459 /* must be a symbol */
460 op->mode = abs_sord[dst];
461 *ptr = skip_colonthing(parse_exp(src, &op->exp));
471 src = parse_exp(src, &op->exp);
472 *ptr= skip_colonthing(src);
477 *ptr = parse_exp(src, &op->exp);
485 DEFUN(get_operands,(noperands,op_end, operand),
486 unsigned int noperands AND
488 struct h8_op *operand)
500 get_operand(& ptr, operand +0,0);
506 get_operand(& ptr, operand +0,0);
507 if (*ptr == ',') ptr++;
508 get_operand(& ptr, operand +1, 1);
519 /* Passed a pointer to a list of opcodes which use different
520 addressing modes, return the opcode which matches the opcodes
525 DEFUN(get_specific,(opcode, operands),
526 struct h8_opcode *opcode AND
527 struct h8_op *operands)
530 struct h8_opcode *this_try = opcode ;
532 unsigned int noperands = opcode->noperands;
534 unsigned int dispreg;
535 unsigned int this_index = opcode->idx;
536 while (this_index == opcode->idx && !found)
540 this_try = opcode ++;
541 for (i = 0; i < noperands; i++)
543 op_type op = (this_try->args.nib[i]) & ~(B30|B31);
565 operands[0].dispreg = operands[i].reg;
575 if (operands[i].mode != op) goto fail;
581 if (operands[i].mode != IMM16) goto fail;
584 if (operands[i].mode != MEMIND) goto fail;
591 if (operands[i].mode != ABS16SRC) goto fail;
596 if (operands[i].mode != ABS16DST) goto fail;
610 DEFUN(check_operand,(operand, width, string),
611 struct h8_op *operand AND
612 unsigned int width AND
615 if (operand->exp.X_add_symbol == 0
616 && operand->exp.X_subtract_symbol == 0)
619 /* No symbol involved, let's look at offset, it's dangerous if any of
620 the high bits are not 0 or ff's, find out by oring or anding with
621 the width and seeing if the answer is 0 or all fs*/
622 if ((operand->exp.X_add_number & ~width) != 0 &&
623 (operand->exp.X_add_number | width)!= (~0))
625 as_warn("operand %s0x%x out of range.", string, operand->exp.X_add_number);
631 /* Now we know what sort of opcodes it is, lets build the bytes -
634 DEFUN (build_bytes,(this_try, operand),
635 struct h8_opcode *this_try AND
636 struct h8_op *operand)
641 char *output = frag_more(this_try->length);
642 char *output_ptr = output;
643 op_type *nibble_ptr = this_try->data.nib;
649 while (*nibble_ptr != E)
652 for (nibble = 0; nibble <2; nibble++)
654 c = *nibble_ptr & ~(B30|B31);
660 switch (operand[0].exp.X_add_number)
669 as_bad("Need #1 or #2 here");
672 /* stop it making a fix */
677 case 2: case 3: case 4: case 5: case 6:
678 case 7: case 8: case 9: case 10: case 11:
679 case 12: case 13: case 14: case 15:
683 nib = operand[0].dispreg;
686 operand[0].mode = IMM8;
694 if (operand[0].exp.X_add_symbol == 0) {
695 operand[0].mode = 0; /* stop it making a fix */
696 nib = (operand[0].exp.X_add_number);
698 else as_bad("can't have symbol for bit number");
699 if (nib < 0 || nib > 7)
701 as_bad("Bit number out of range %d", nib);
710 operand[1].mode = ABS8DST;
714 operand[0].mode = ABS8SRC;
730 operand[0].mode = ABS16OR8SRC;
734 operand[0].mode = ABS16SRC;
739 operand[0].mode = DISP8;
754 nib = operand[0].reg;
761 nib = operand[1].reg;
768 if (*nibble_ptr & B31) {
773 *output_ptr = nib << 4;
784 /* output any fixes */
785 for (i = 0; i < 2; i++)
787 switch (operand[i].mode) {
792 check_operand(operand+i, 0x7f,"@");
794 if (operand[i].exp.X_add_number & 1) {
795 as_warn("branch operand has odd offset (%x)\n",
796 operand->exp.X_add_number);
800 output - frag_now->fr_literal + 1,
802 operand[i].exp.X_add_symbol,
803 operand[i].exp.X_subtract_symbol,
804 operand[i].exp.X_add_number -1,
809 check_operand(operand+i, 0xff,"#");
810 /* If there is nothing else going on we can safely
812 if (operand[i].exp.X_add_symbol == 0)
814 output[1] = operand[i].exp.X_add_number;
819 output - frag_now->fr_literal + 1,
821 operand[i].exp.X_add_symbol,
822 operand[i].exp.X_subtract_symbol,
823 operand[i].exp.X_add_number,
830 check_operand(operand+i, 0xff,"@@");
832 output - frag_now->fr_literal + 1,
834 operand[i].exp.X_add_symbol,
835 operand[i].exp.X_subtract_symbol,
836 operand[i].exp.X_add_number,
842 check_operand(operand+i, 0xff,"@");
844 output - frag_now->fr_literal + 1,
846 operand[i].exp.X_add_symbol,
847 operand[i].exp.X_subtract_symbol,
848 operand[i].exp.X_add_number,
855 check_operand(operand+i, 0xffff,"@");
858 output - frag_now->fr_literal + 2,
860 operand[i].exp.X_add_symbol,
861 operand[i].exp.X_subtract_symbol,
862 operand[i].exp.X_add_number,
868 check_operand(operand+i, 0xffff,"@");
869 if (operand[i].exp.X_add_number & 1) {
870 as_warn("branch operand has odd offset (%x)\n",
871 operand->exp.X_add_number);
874 output - frag_now->fr_literal + 2,
876 operand[i].exp.X_add_symbol,
877 operand[i].exp.X_subtract_symbol,
878 operand[i].exp.X_add_number,
889 check_operand(operand+i, 0xffff,"@");
890 if (operand[i].exp.X_add_symbol == 0)
892 /* This should be done with bfd */
893 output[3] = operand[i].exp.X_add_number & 0xff;
894 output[2] = operand[i].exp.X_add_number >> 8;
901 output - frag_now->fr_literal + 2,
903 operand[i].exp.X_add_symbol,
904 operand[i].exp.X_subtract_symbol,
905 operand[i].exp.X_add_number,
930 try and give an intelligent error message for common and simple to
935 DEFUN(clever_message, (opcode, operand),
936 struct h8_opcode *opcode AND
937 struct h8_op *operand)
939 struct h8_opcode *scan = opcode;
941 /* Find out if there was more than one possible opccode */
943 if ((opcode+1)->idx != opcode->idx)
947 /* Only one opcode of this flavour, try and guess which operand
949 for (argn = 0; argn < opcode->noperands; argn++)
951 switch (opcode->args.nib[argn])
954 if (operand[argn].mode != RD16)
956 as_bad("destination operand must be 16 bit register");
964 if (operand[argn].mode != RS8)
966 as_bad("source operand must be 8 bit register");
972 if (operand[argn].mode != ABS16DST)
974 as_bad("destination operand must be 16bit absolute address");
979 if (operand[argn].mode != RD8)
981 as_bad("destination operand must be 8 bit register");
988 if (operand[argn].mode != ABS16SRC)
990 as_bad("source operand must be 16bit absolute address");
998 as_bad("invalid operands");
1001 /* This is the guts of the machine-dependent assembler. STR points to a
1002 machine dependent instruction. This funciton is supposed to emit
1003 the frags/bytes it assembles to.
1009 DEFUN(md_assemble,(str),
1015 struct h8_op operand[2];
1016 struct h8_opcode * opcode;
1017 struct h8_opcode * prev_opcode;
1021 /* Drop leading whitespace */
1025 /* find the op code end */
1026 for (op_start = op_end = str;
1027 *op_end != 0 && *op_end != ' ';
1030 if (*op_end == '.') {
1040 if (op_end == op_start)
1042 as_bad("can't find opcode ");
1048 opcode = (struct h8_opcode *) hash_find(opcode_hash_control,
1053 as_bad("unknown opcode");
1058 input_line_pointer = get_operands(opcode->noperands, op_end,
1061 prev_opcode = opcode;
1063 opcode = get_specific(opcode, operand);
1067 /* Couldn't find an opcode which matched the operands */
1068 char *where =frag_more(2);
1071 clever_message(prev_opcode, operand);
1075 if (opcode->size && dot)
1077 if (opcode->size != *dot)
1079 as_warn("mismatch between opcode size and operand size");
1083 build_bytes(opcode, operand);
1088 DEFUN(tc_crawl_symbol_chain, (headers),
1089 object_headers *headers)
1091 printf("call to tc_crawl_symbol_chain \n");
1094 symbolS *DEFUN(md_undefined_symbol,(name),
1101 DEFUN(tc_headers_hook,(headers),
1102 object_headers *headers)
1104 printf("call to tc_headers_hook \n");
1111 /* Various routines to kill one day */
1112 /* Equal to MAX_PRECISION in atof-ieee.c */
1113 #define MAX_LITTLENUMS 6
1115 /* Turn a string in input_line_pointer into a floating point constant of type
1116 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1117 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1120 md_atof(type,litP,sizeP)
1126 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1127 LITTLENUM_TYPE *wordP;
1158 return "Bad call to MD_ATOF()";
1160 t=atof_ieee(input_line_pointer,type,words);
1162 input_line_pointer=t;
1164 *sizeP=prec * sizeof(LITTLENUM_TYPE);
1165 for(wordP=words;prec--;) {
1166 md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
1167 litP+=sizeof(LITTLENUM_TYPE);
1169 return ""; /* Someone should teach Dean about null pointers */
1173 md_parse_option(argP, cntP, vecP)
1183 int md_short_jump_size;
1185 void tc_aout_fix_to_chars () { printf("call to tc_aout_fix_to_chars \n");
1187 void md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
1194 as_fatal("failed sanity check.");
1198 md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)
1200 long from_addr, to_addr;
1204 as_fatal("failed sanity check.");
1208 md_convert_frag(headers, fragP)
1209 object_headers *headers;
1212 { printf("call to md_convert_frag \n"); abort(); }
1215 DEFUN(md_section_align,(seg, size),
1219 return((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1224 md_apply_fix(fixP, val)
1228 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1230 switch(fixP->fx_size) {
1250 void DEFUN(md_operand, (expressionP),expressionS *expressionP)
1253 int md_long_jump_size;
1255 md_estimate_size_before_relax(fragP, segment_type)
1256 register fragS *fragP;
1257 register segT segment_type;
1259 printf("call tomd_estimate_size_before_relax \n"); abort(); }
1260 /* Put number into target byte order */
1262 void DEFUN(md_number_to_chars,(ptr, use, nbytes),
1268 case 4: *ptr++ = (use >> 24) & 0xff;
1269 case 3: *ptr++ = (use >> 16) & 0xff;
1270 case 2: *ptr++ = (use >> 8) & 0xff;
1271 case 1: *ptr++ = (use >> 0) & 0xff;
1277 long md_pcrel_from(fixP)
1278 fixS *fixP; { abort(); }
1280 void tc_coff_symbol_emit_hook() { }
1283 void tc_reloc_mangle(fix_ptr, intr, base)
1285 struct internal_reloc *intr;
1289 symbolS *symbol_ptr;
1291 symbol_ptr = fix_ptr->fx_addsy;
1293 /* If this relocation is attached to a symbol then it's ok
1295 if (fix_ptr->fx_r_type == RELOC_32) {
1296 /* cons likes to create reloc32's whatever the size of the reloc..
1298 switch (fix_ptr->fx_size)
1302 intr->r_type = R_RELWORD;
1305 intr->r_type = R_RELBYTE;
1314 intr->r_type = fix_ptr->fx_r_type;
1317 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where +base;
1318 intr->r_offset = fix_ptr->fx_offset;
1321 intr->r_symndx = symbol_ptr->sy_number;
1323 intr->r_symndx = -1;
1328 /* end of tc-h8300.c */