1 /* tc-h8300.c -- Assemble code for the Hitachi H8/300
2 Copyright (C) 1991 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[] = {
52 const char EXP_CHARS[] = "eE";
54 /* Chars that mean this number is a floating point constant */
57 char FLT_CHARS[] = "rRsSfFdDxXpP";
60 const relax_typeS md_relax_table[1];
63 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
67 This function is called once, at assembler startup time. This should
68 set up all the tables, etc that the MD part of the assembler needs
71 /* encode the size and number into the number field
87 struct reg_entry reg_list[] = {
125 struct h8_opcode *opcode;
126 const struct reg_entry *reg;
127 char prev_buffer[100];
130 opcode_hash_control = hash_new();
133 for (opcode = h8_opcodes; opcode->name; opcode++)
135 /* Strip off any . part when inserting the opcode and only enter
136 unique codes into the hash table
138 char *src= opcode->name;
139 unsigned int len = strlen(src);
140 char *dst = malloc(len+1);
152 if (strcmp(buffer, prev_buffer))
154 hash_insert(opcode_hash_control, buffer, (char *)opcode);
155 strcpy(prev_buffer, buffer);
161 /* Find the number of operands */
162 opcode->noperands = 0;
163 while (opcode->args.nib[opcode->noperands] != E)
164 opcode->noperands ++;
165 /* Find the length of the opcode in bytes */
167 while (opcode->data.nib[opcode->length*2] != E)
181 unsigned int dispreg;
191 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
200 op_type r8_sord[] = {RS8, RD8};
201 op_type r16_sord[] = {RS16, RD16};
202 op_type rind_sord[] = {RSIND, RDIND};
203 op_type abs_sord[2] = {ABS16SRC, ABS16DST};
204 op_type disp_sord[] = {DISPSRC, DISPDST};
206 /* try and parse a reg name, returns number of chars consumed */
208 DEFUN(parse_reg,(src, mode, reg, dst),
211 unsigned int *reg AND
214 if (src[0] == 's' && src[1] == 'p')
216 *mode = r16_sord[dst];
220 if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r')
226 if (src[0] == 'f' && src[1] == 'p')
228 *mode = r16_sord[dst];
234 if (src[1] >= '0' && src[1] <= '7')
238 *mode = r8_sord[dst];
239 *reg = (src[1] - '0') + 8;
244 *mode = r8_sord[dst];
245 *reg = (src[1] - '0') ;
248 *mode = r16_sord[dst];
249 *reg = (src[1] - '0');
257 DEFUN(parse_exp,(s, op),
261 char *save = input_line_pointer;
264 input_line_pointer = s;
266 new = input_line_pointer;
267 input_line_pointer = save;
278 as_bad("Missing operand");
281 as_bad("Don't understand operand of type %s", segment_name (seg));
287 DEFUN(skip_colonthing,(ptr),
292 while (isdigit(*ptr))
299 /* The many forms of operand:
302 @Rn Register indirect
303 @(exp[:16], Rn) Register indirect with displacement
307 @aa:16 absolute 16 bit
310 #xx[:size] immediate data
311 @(exp:[8], pc) pc rel
312 @@aa[:8] memory indirect
317 DEFUN(get_operand,(ptr, op, dst),
329 len = parse_reg(src, &op->mode, &op->reg, dst);
341 src = parse_exp(src,&op->exp);
342 src = skip_colonthing(src);
355 len = parse_reg(src, &mode, &num, dst);
358 /* Oops, not a reg after all, must be ordinary exp */
360 /* must be a symbol */
361 op->mode = abs_sord[dst];
362 *ptr = skip_colonthing(parse_exp(src, &op->exp));
369 if (mode != r16_sord[dst])
371 as_bad("@- needs word register");
378 if (*src == '(' && ')')
382 src = parse_exp(src, &op->exp);
387 op->mode = abs_sord[dst];
391 src = skip_colonthing(src);
395 as_bad("expected @(exp, reg16)");
398 len = parse_reg(src, &mode, &op->reg, dst);
399 if (len == 0 || mode != r16_sord[dst])
401 as_bad("expected @(exp, reg16)");
403 op->mode = disp_sord[dst];
405 src = skip_colonthing(src);
407 if (*src != ')' && '(')
409 as_bad("expected @(exp, reg16)");
416 len = parse_reg(src, &mode, &num, dst);
425 as_bad("@Rn+ needs src word register");
432 if (mode != r16_sord[dst])
434 as_bad("@Rn needs word register");
436 op->mode =rind_sord[dst];
443 /* must be a symbol */
444 op->mode = abs_sord[dst];
445 *ptr = skip_colonthing(parse_exp(src, &op->exp));
455 src = parse_exp(src, &op->exp);
456 *ptr= skip_colonthing(src);
461 *ptr = parse_exp(src, &op->exp);
469 DEFUN(get_operands,(noperands,op_end, operand),
470 unsigned int noperands AND
472 struct h8_op *operand)
484 get_operand(& ptr, operand +0,0);
490 get_operand(& ptr, operand +0,0);
491 if (*ptr == ',') ptr++;
492 get_operand(& ptr, operand +1, 1);
503 /* Passed a pointer to a list of opcodes which use different
504 addressing modes, return the opcode which matches the opcodes
509 DEFUN(get_specific,(opcode, operands),
510 struct h8_opcode *opcode AND
511 struct h8_op *operands)
514 struct h8_opcode *this_try = opcode ;
516 unsigned int noperands = opcode->noperands;
518 unsigned int dispreg;
519 unsigned int this_index = opcode->idx;
520 while (this_index == opcode->idx && !found)
524 this_try = opcode ++;
525 for (i = 0; i < noperands; i++)
527 op_type op = (this_try->args.nib[i]) & ~(B30|B31);
549 operands[0].dispreg = operands[i].reg;
559 if (operands[i].mode != op) goto fail;
565 if (operands[i].mode != IMM16) goto fail;
568 if (operands[i].mode != MEMIND) goto fail;
575 if (operands[i].mode != ABS16SRC) goto fail;
580 if (operands[i].mode != ABS16DST) goto fail;
594 DEFUN(check_operand,(operand, width, string),
595 struct h8_op *operand AND
596 unsigned int width AND
599 if (operand->exp.X_add_symbol == 0
600 && operand->exp.X_subtract_symbol == 0)
603 /* No symbol involved, let's look at offset, it's dangerous if any of
604 the high bits are not 0 or ff's, find out by oring or anding with
605 the width and seeing if the answer is 0 or all fs*/
606 if ((operand->exp.X_add_number | width) != ~0 &&
607 (operand->exp.X_add_number & ~width)!= 0)
609 as_warn("operand %s0x%x out of range.", string, operand->exp.X_add_number);
615 /* Now we know what sort of opcodes it is, lets build the bytes -
618 DEFUN (build_bytes,(this_try, operand),
619 struct h8_opcode *this_try AND
620 struct h8_op *operand)
625 char *output = frag_more(this_try->length);
626 char *output_ptr = output;
627 op_type *nibble_ptr = this_try->data.nib;
633 while (*nibble_ptr != E)
636 for (nibble = 0; nibble <2; nibble++)
638 c = *nibble_ptr & ~(B30|B31);
644 switch (operand[0].exp.X_add_number)
653 as_bad("Need #1 or #2 here");
656 /* stop it making a fix */
661 case 2: case 3: case 4: case 5: case 6:
662 case 7: case 8: case 9: case 10: case 11:
663 case 12: case 13: case 14: case 15:
667 nib = operand[0].dispreg;
670 operand[0].mode = IMM8;
678 if (operand[0].exp.X_add_symbol == 0) {
679 operand[0].mode = 0; /* stop it making a fix */
680 nib = (operand[0].exp.X_add_number);
682 else as_bad("can't have symbol for bit number");
683 if (nib < 0 || nib > 7)
685 as_bad("Bit number out of range %d", nib);
694 operand[1].mode = ABS8DST;
698 operand[0].mode = ABS8SRC;
714 operand[0].mode = ABS16OR8SRC;
718 operand[0].mode = ABS16SRC;
723 operand[0].mode = DISP8;
738 nib = operand[0].reg;
745 nib = operand[1].reg;
752 if (*nibble_ptr & B31) {
757 *output_ptr = nib << 4;
768 /* output any fixes */
769 for (i = 0; i < 2; i++)
771 switch (operand[i].mode) {
776 check_operand(operand+i, 0x7f,"@");
779 output - frag_now->fr_literal + 1,
781 operand[i].exp.X_add_symbol,
782 operand[i].exp.X_subtract_symbol,
783 operand[i].exp.X_add_number -1,
788 check_operand(operand+i, 0xff,"#");
789 /* If there is nothing else going on we can safely
791 if (operand[i].exp.X_add_symbol == 0)
793 output[1] = operand[i].exp.X_add_number;
798 output - frag_now->fr_literal + 1,
800 operand[i].exp.X_add_symbol,
801 operand[i].exp.X_subtract_symbol,
802 operand[i].exp.X_add_number,
809 check_operand(operand+i, 0xff,"@@");
811 output - frag_now->fr_literal + 1,
813 operand[i].exp.X_add_symbol,
814 operand[i].exp.X_subtract_symbol,
815 operand[i].exp.X_add_number,
821 check_operand(operand+i, 0xff,"@");
823 output - frag_now->fr_literal + 1,
825 operand[i].exp.X_add_symbol,
826 operand[i].exp.X_subtract_symbol,
827 operand[i].exp.X_add_number,
834 check_operand(operand+i, 0xffff,"@");
837 output - frag_now->fr_literal + 2,
839 operand[i].exp.X_add_symbol,
840 operand[i].exp.X_subtract_symbol,
841 operand[i].exp.X_add_number,
847 check_operand(operand+i, 0xffff,"@");
850 output - frag_now->fr_literal + 2,
852 operand[i].exp.X_add_symbol,
853 operand[i].exp.X_subtract_symbol,
854 operand[i].exp.X_add_number,
865 check_operand(operand+i, 0xffff,"@");
866 if (operand[i].exp.X_add_symbol == 0)
868 /* This should be done with bfd */
869 output[3] = operand[i].exp.X_add_number & 0xff;
870 output[2] = operand[i].exp.X_add_number >> 8;
877 output - frag_now->fr_literal + 2,
879 operand[i].exp.X_add_symbol,
880 operand[i].exp.X_subtract_symbol,
881 operand[i].exp.X_add_number,
906 try and give an intelligent error message for common and simple to
911 DEFUN(clever_message, (opcode, operand),
912 struct h8_opcode *opcode AND
913 struct h8_op *operand)
915 struct h8_opcode *scan = opcode;
917 /* Find out if there was more than one possible opccode */
919 if ((opcode+1)->idx != opcode->idx)
923 /* Only one opcode of this flavour, try and guess which operand
925 for (argn = 0; argn < opcode->noperands; argn++)
927 switch (opcode->args.nib[argn])
930 if (operand[argn].mode != RD16)
932 as_bad("destination operand must be 16 bit register");
937 if (operand[argn].mode != RS8)
939 as_bad("source operand must be 8 bit register");
943 if (operand[argn].mode != ABS16DST)
945 as_bad("destination operand must be 16bit absolute address");
950 if (operand[argn].mode != RD8)
952 as_bad("destination operand must be 8 bit register");
957 if (operand[argn].mode != ABS16SRC)
959 as_bad("source operand must be 16bit absolute address");
965 as_bad("invalid operands");
968 /* This is the guts of the machine-dependent assembler. STR points to a
969 machine dependent instruction. This funciton is supposed to emit
970 the frags/bytes it assembles to.
976 DEFUN(md_assemble,(str),
982 struct h8_op operand[2];
983 struct h8_opcode * opcode;
984 struct h8_opcode * prev_opcode;
988 /* Drop leading whitespace */
992 /* find the op code end */
993 for (op_start = op_end = str;
994 *op_end != 0 && *op_end != ' ';
997 if (*op_end == '.') {
1007 if (op_end == op_start)
1009 as_bad("can't find opcode ");
1015 opcode = (struct h8_opcode *) hash_find(opcode_hash_control,
1020 as_bad("unknown opcode");
1025 input_line_pointer = get_operands(opcode->noperands, op_end,
1028 prev_opcode = opcode;
1030 opcode = get_specific(opcode, operand);
1034 /* Couldn't find an opcode which matched the operands */
1035 char *where =frag_more(2);
1038 clever_message(prev_opcode, operand);
1042 if (opcode->size && dot)
1044 if (opcode->size != *dot)
1046 as_warn("mismatch between opcode size and operand size");
1050 build_bytes(opcode, operand);
1055 DEFUN(tc_crawl_symbol_chain, (headers),
1056 object_headers *headers)
1058 printf("call to tc_crawl_symbol_chain \n");
1061 symbolS *DEFUN(md_undefined_symbol,(name),
1068 DEFUN(tc_headers_hook,(headers),
1069 object_headers *headers)
1071 printf("call to tc_headers_hook \n");
1078 /* Various routines to kill one day */
1079 /* Equal to MAX_PRECISION in atof-ieee.c */
1080 #define MAX_LITTLENUMS 6
1082 /* Turn a string in input_line_pointer into a floating point constant of type
1083 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1084 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1087 md_atof(type,litP,sizeP)
1093 LITTLENUM_TYPE words[MAX_LITTLENUMS];
1094 LITTLENUM_TYPE *wordP;
1125 return "Bad call to MD_ATOF()";
1127 t=atof_ieee(input_line_pointer,type,words);
1129 input_line_pointer=t;
1131 *sizeP=prec * sizeof(LITTLENUM_TYPE);
1132 for(wordP=words;prec--;) {
1133 md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
1134 litP+=sizeof(LITTLENUM_TYPE);
1136 return ""; /* Someone should teach Dean about null pointers */
1140 md_parse_option(argP, cntP, vecP)
1150 int md_short_jump_size;
1152 void tc_aout_fix_to_chars () { printf("call to tc_aout_fix_to_chars \n");
1154 void md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
1161 as_fatal("failed sanity check.");
1165 md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)
1167 long from_addr, to_addr;
1171 as_fatal("failed sanity check.");
1175 md_convert_frag(headers, fragP)
1176 object_headers *headers;
1179 { printf("call to md_convert_frag \n"); abort(); }
1182 DEFUN(md_section_align,(seg, size),
1186 return((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
1191 md_apply_fix(fixP, val)
1195 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
1197 switch(fixP->fx_size) {
1217 void DEFUN(md_operand, (expressionP),expressionS *expressionP)
1220 int md_long_jump_size;
1222 md_estimate_size_before_relax(fragP, segment_type)
1223 register fragS *fragP;
1224 register segT segment_type;
1226 printf("call tomd_estimate_size_before_relax \n"); abort(); }
1227 /* Put number into target byte order */
1229 void DEFUN(md_number_to_chars,(ptr, use, nbytes),
1235 case 4: *ptr++ = (use >> 24) & 0xff;
1236 case 3: *ptr++ = (use >> 16) & 0xff;
1237 case 2: *ptr++ = (use >> 8) & 0xff;
1238 case 1: *ptr++ = (use >> 0) & 0xff;
1244 long md_pcrel_from(fixP)
1245 fixS *fixP; { abort(); }
1247 void tc_coff_symbol_emit_hook() { }
1250 void tc_reloc_mangle(fix_ptr, intr, base)
1252 struct internal_reloc *intr;
1256 symbolS *symbol_ptr;
1258 symbol_ptr = fix_ptr->fx_addsy;
1260 /* If this relocation is attached to a symbol then it's ok
1262 if (fix_ptr->fx_r_type == RELOC_32) {
1263 /* cons likes to create reloc32's whatever the size of the reloc..
1265 switch (fix_ptr->fx_size)
1269 intr->r_type = R_RELWORD;
1272 intr->r_type = R_RELBYTE;
1281 intr->r_type = fix_ptr->fx_r_type;
1284 intr->r_vaddr = fix_ptr->fx_frag->fr_address + fix_ptr->fx_where +base;
1285 intr->r_offset = fix_ptr->fx_offset;
1288 intr->r_symndx = symbol_ptr->sy_number;
1290 intr->r_symndx = -1;