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
30 #include "h8300-opcode.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
44 const pseudo_typeS md_pseudo_table[] = {
51 const char EXP_CHARS[] = "eE";
53 /* Chars that mean this number is a floating point constant */
56 char FLT_CHARS[] = "rRsSfFdDxXpP";
59 const relax_typeS md_relax_table[1];
62 static struct hash_control *opcode_hash_control; /* Opcode mnemonics */
63 static struct hash_control *register_hash_control; /* Register name hash table */
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 reloc_howto_type *r16;
73 reloc_howto_type *r8ff;
74 reloc_howto_type *r8pcrel;
78 bfd_arch_info_type *ai;
79 const struct h8_opcode *opcode;
81 opcode_hash_control = hash_new();
82 for (opcode = h8_opcodes; opcode->name; opcode++) {
83 hash_insert(opcode_hash_control, opcode->name, (char *)opcode);
86 ai = bfd_lookup_arch(bfd_arch_h8300,0);
88 r16 = ai->reloc_type_lookup(ai, BFD_RELOC_16);
89 r8 = ai->reloc_type_lookup(ai, BFD_RELOC_8);
90 r8ff = ai->reloc_type_lookup(ai, BFD_RELOC_8_FFnn);
91 r8pcrel = ai->reloc_type_lookup(ai, BFD_RELOC_8_PCREL);
113 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
122 op_type r8_sord[] = {RS8, RD8};
123 op_type r16_sord[] = {RS16, RD16};
124 op_type rind_sord[] = {RSIND, RDIND};
125 op_type abs_sord[2] = {ABS16SRC, ABS16DST};
126 op_type disp_sord[] = {DISPSRC, DISPDST};
127 /* try and parse a reg name, returns number of chars consumed */
128 int DEFUN(parse_reg,(src, mode, reg, dst),
131 unsigned int *reg AND
134 if (src[0] == 's' && src[1] == 'p') {
135 *mode = r16_sord[dst];
139 if (src[0] == 'c' && src[1] == 'c' && src[2] == 'r') {
144 if (src[0] == 'f' && src[1] == 'p') {
145 *mode = r16_sord[dst];
150 if (src[1] >= '0' && src[1] <= '7') {
152 *mode = r8_sord[dst];
153 *reg = (src[1] - '0') + 8;
157 *mode = r8_sord[dst];
158 *reg = (src[1] - '0') ;
161 *mode = r16_sord[dst];
162 *reg = (src[1] - '0');
170 DEFUN(parse_exp,(s, op),
174 char *save = input_line_pointer;
177 input_line_pointer = s;
179 new = input_line_pointer;
180 input_line_pointer = save;
191 as_bad("Missing operand");
194 as_bad("Don't understand operand of type %s", segment_name (seg));
201 DEFUN(get_operand,(ptr, op, dst),
212 while (*src == ' ') src++;
213 len = parse_reg(src, &op->mode, &op->reg, dst);
223 len = parse_reg(src, &mode, &num, dst);
224 if (len == 0 || mode != r16_sord[dst]) {
225 as_bad("@- needs word register");
232 if (*src == '(' && ')') {
235 src = parse_exp(src, &op->exp);
239 op->mode = abs_sord[dst];
244 as_bad("expected @(exp, reg16)");
247 len = parse_reg(src, &mode, &op->reg, dst);
248 if (len == 0 || mode != r16_sord[dst])
250 as_bad("expected @(exp, reg16)");
252 op->mode = disp_sord[dst];
254 if (*src != ')' && '(') {
255 as_bad("expected @(exp, reg16)");
262 len = parse_reg(src, &mode, &num, dst);
269 as_bad("@Rn+ needs word register");
276 if (mode != r16_sord[dst]) {
277 as_bad("@Rn needs word register");
279 op->mode =rind_sord[dst];
285 /* must be a symbol */
286 op->mode = abs_sord[dst];
287 *ptr = parse_exp(src, &op->exp);
296 *ptr = parse_exp(src, &op->exp);
300 *ptr = parse_exp(src, &op->exp);
305 /* This is the guts of the machine-dependent assembler. STR points to a
306 machine dependent instruction. This funciton is supposed to emit
307 the frags/bytes it assembles to.
312 DEFUN(md_assemble,(str),
319 struct h8_opcode * opcode;
320 /* Drop leading whitespace */
325 /* find the op code end */
326 for (op_start = op_end = str;
327 *op_end != 0 && *op_end != ' ';
331 if (op_end == op_start) {
332 as_bad("can't find opcode ");
335 opcode = (struct h8_opcode *) hash_find(opcode_hash_control,
338 if (opcode == NULL) {
339 as_bad("unknown opcode");
348 struct h8_op operand[2];
349 char *ptr = op_end+1;
350 if (opcode->noperands)
351 get_operand(& ptr, &operand[0],0);
352 else operand[0].mode = 0;
353 if (opcode->noperands==2) {
354 if (*ptr == ',') ptr++;
355 get_operand(& ptr, &operand[1], 1);
357 else operand[1].mode = 0;
362 struct h8_opcode *this_try ;
364 for (j = 0; j < opcode->nopcodes && !found; j++) {
365 this_try = opcode + j;
366 for (i = 0; i < opcode->noperands; i++) {
367 op_type op = (this_try->args.nib[i]) & ~(B30|B31);
388 dispreg = operand[i].reg;
398 if (operand[i].mode != op) goto fail;
401 /* We have an expression, called IMM16, but we know we
402 want an 8 bit value here */
403 if (operand[i].mode != IMM16) goto fail;
404 operand[i].mode = IMM8;
409 if (operand[i].mode != IMM16) goto fail;
413 if (operand[i].mode != ABS16SRC) goto fail;
417 if (operand[i].mode != ABS16DST) goto fail;
426 as_bad("illegal operands for opcode");
429 /* Now we know what sort of opcodes etc, lets build the bytes -
430 actually we know how big the instruction will be too. So we
434 char *output = frag_more(this_try->length);
435 char *output_ptr = output;
436 op_type *nibble_ptr = this_try->data.nib;
442 while (*nibble_ptr != E) {
444 for (nibble = 0; nibble <2; nibble++) {
445 c = *nibble_ptr & ~(B30|B31);
450 switch (operand[0].exp.X_add_number) {
458 as_bad("Need #1 or #2 here");
461 /* stop it making a fix */
466 case 2: case 3: case 4: case 5: case 6:
467 case 7: case 8: case 9: case 10: case 11:
468 case 12: case 13: case 14: case 15:
482 if (operand[0].exp.X_add_symbol == 0) {
483 operand[0].mode = 0; /* stop it making a fix */
484 nib = (operand[0].exp.X_add_number);
486 else as_bad("can't have symbol for bit number");
522 nib = operand[1].reg;
529 if (*nibble_ptr & B31) nib|=0x8;
531 *output_ptr = nib << 4;
542 /* output any fixes */
543 for (i = 0; i < 2; i++)
545 switch (operand[i].mode) {
550 output - frag_now->fr_literal + 1,
552 operand[i].exp.X_add_symbol,
553 operand[i].exp.X_subtract_symbol,
554 operand[i].exp.X_add_number -1,
560 output - frag_now->fr_literal + 1,
562 operand[i].exp.X_add_symbol,
563 operand[i].exp.X_subtract_symbol,
564 operand[i].exp.X_add_number,
575 output - frag_now->fr_literal + 2,
577 operand[i].exp.X_add_symbol,
578 operand[i].exp.X_subtract_symbol,
579 operand[i].exp.X_add_number,
605 DEFUN(tc_crawl_symbol_chain, (headers),
606 object_headers *headers)
608 printf("call to tc_crawl_symbol_chain \n");
611 symbolS *DEFUN(md_undefined_symbol,(name),
618 DEFUN(tc_headers_hook,(headers),
619 object_headers *headers)
621 printf("call to tc_headers_hook \n");
628 /* Various routines to kill one day */
629 /* Equal to MAX_PRECISION in atof-ieee.c */
630 #define MAX_LITTLENUMS 6
632 /* Turn a string in input_line_pointer into a floating point constant of type
633 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
634 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
637 md_atof(type,litP,sizeP)
643 LITTLENUM_TYPE words[MAX_LITTLENUMS];
644 LITTLENUM_TYPE *wordP;
675 return "Bad call to MD_ATOF()";
677 t=atof_ieee(input_line_pointer,type,words);
679 input_line_pointer=t;
681 *sizeP=prec * sizeof(LITTLENUM_TYPE);
682 for(wordP=words;prec--;) {
683 md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
684 litP+=sizeof(LITTLENUM_TYPE);
686 return ""; /* Someone should teach Dean about null pointers */
690 md_parse_option(argP, cntP, vecP)
698 int md_short_jump_size;
700 void tc_aout_fix_to_chars () { printf("call to tc_aout_fix_to_chars \n");
702 void md_create_short_jump(ptr, from_addr, to_addr, frag, to_symbol)
709 as_fatal("failed sanity check.");
713 md_create_long_jump(ptr,from_addr,to_addr,frag,to_symbol)
715 long from_addr, to_addr;
719 as_fatal("failed sanity check.");
723 md_convert_frag(headers, fragP)
724 object_headers *headers;
727 { printf("call to md_convert_frag \n"); abort(); }
730 DEFUN(md_section_align,(seg, size),
734 return((size + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
739 md_apply_fix(fixP, val)
743 char *buf = fixP->fx_where + fixP->fx_frag->fr_literal;
745 switch(fixP->fx_size) {
765 void DEFUN(md_operand, (expressionP),expressionS *expressionP)
768 int md_long_jump_size;
770 md_estimate_size_before_relax(fragP, segment_type)
771 register fragS *fragP;
772 register segT segment_type;
773 { printf("call tomd_estimate_size_before_relax \n"); abort(); }
774 /* Put number into target byte order */
776 void DEFUN(md_number_to_chars,(ptr, use, nbytes),
782 case 4: *ptr++ = (use >> 24) & 0xff;
783 case 3: *ptr++ = (use >> 16) & 0xff;
784 case 2: *ptr++ = (use >> 8) & 0xff;
785 case 1: *ptr++ = (use >> 0) & 0xff;
791 long md_pcrel_from(fixP)
792 fixS *fixP; { abort(); }
794 void tc_coff_symbol_emit_hook() { }