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
);
356 if (len
== 0 || mode
!= r16_sord
[dst
])
358 as_bad("@- needs word register");
365 if (*src
== '(' && ')')
369 src
= parse_exp(src
, &op
->exp
);
374 op
->mode
= abs_sord
[dst
];
378 src
= skip_colonthing(src
);
382 as_bad("expected @(exp, reg16)");
385 len
= parse_reg(src
, &mode
, &op
->reg
, dst
);
386 if (len
== 0 || mode
!= r16_sord
[dst
])
388 as_bad("expected @(exp, reg16)");
390 op
->mode
= disp_sord
[dst
];
392 src
= skip_colonthing(src
);
394 if (*src
!= ')' && '(')
396 as_bad("expected @(exp, reg16)");
403 len
= parse_reg(src
, &mode
, &num
, dst
);
412 as_bad("@Rn+ needs src word register");
419 if (mode
!= r16_sord
[dst
])
421 as_bad("@Rn needs word register");
423 op
->mode
=rind_sord
[dst
];
430 /* must be a symbol */
431 op
->mode
= abs_sord
[dst
];
432 *ptr
= skip_colonthing(parse_exp(src
, &op
->exp
));
442 src
= parse_exp(src
, &op
->exp
);
443 *ptr
= skip_colonthing(src
);
448 *ptr
= parse_exp(src
, &op
->exp
);
456 DEFUN(get_operands
,(noperands
,op_end
, operand
),
457 unsigned int noperands AND
459 struct h8_op
*operand
)
471 get_operand(& ptr
, operand
+0,0);
477 get_operand(& ptr
, operand
+0,0);
478 if (*ptr
== ',') ptr
++;
479 get_operand(& ptr
, operand
+1, 1);
490 /* Passed a pointer to a list of opcodes which use different
491 addressing modes, return the opcode which matches the opcodes
496 DEFUN(get_specific
,(opcode
, operands
),
497 struct h8_opcode
*opcode AND
498 struct h8_op
*operands
)
501 struct h8_opcode
*this_try
= opcode
;
503 unsigned int noperands
= opcode
->noperands
;
505 unsigned int dispreg
;
506 unsigned int this_index
= opcode
->idx
;
507 while (this_index
== opcode
->idx
&& !found
)
511 this_try
= opcode
++;
512 for (i
= 0; i
< noperands
; i
++)
514 op_type op
= (this_try
->args
.nib
[i
]) & ~(B30
|B31
);
536 operands
[0].dispreg
= operands
[i
].reg
;
546 if (operands
[i
].mode
!= op
) goto fail
;
552 if (operands
[i
].mode
!= IMM16
) goto fail
;
555 if (operands
[i
].mode
!= MEMIND
) goto fail
;
562 if (operands
[i
].mode
!= ABS16SRC
) goto fail
;
567 if (operands
[i
].mode
!= ABS16DST
) goto fail
;
581 DEFUN(check_operand
,(operand
, width
, string
),
582 struct h8_op
*operand AND
583 unsigned int width AND
586 if (operand
->exp
.X_add_symbol
== 0
587 && operand
->exp
.X_subtract_symbol
== 0)
590 /* No symbol involved, let's look at offset, it's dangerous if any of
591 the high bits are not 0 or ff's, find out by oring or anding with
592 the width and seeing if the answer is 0 or all fs*/
593 if ((operand
->exp
.X_add_number
| width
) != ~0 &&
594 (operand
->exp
.X_add_number
& ~width
)!= 0)
596 as_warn("operand %s0x%x out of range.", string
, operand
->exp
.X_add_number
);
602 /* Now we know what sort of opcodes it is, lets build the bytes -
605 DEFUN (build_bytes
,(this_try
, operand
),
606 struct h8_opcode
*this_try AND
607 struct h8_op
*operand
)
612 char *output
= frag_more(this_try
->length
);
613 char *output_ptr
= output
;
614 op_type
*nibble_ptr
= this_try
->data
.nib
;
620 while (*nibble_ptr
!= E
)
623 for (nibble
= 0; nibble
<2; nibble
++)
625 c
= *nibble_ptr
& ~(B30
|B31
);
631 switch (operand
[0].exp
.X_add_number
)
640 as_bad("Need #1 or #2 here");
643 /* stop it making a fix */
648 case 2: case 3: case 4: case 5: case 6:
649 case 7: case 8: case 9: case 10: case 11:
650 case 12: case 13: case 14: case 15:
654 nib
= operand
[0].dispreg
;
657 operand
[0].mode
= IMM8
;
665 if (operand
[0].exp
.X_add_symbol
== 0) {
666 operand
[0].mode
= 0; /* stop it making a fix */
667 nib
= (operand
[0].exp
.X_add_number
);
669 else as_bad("can't have symbol for bit number");
670 if (nib
< 0 || nib
> 7)
672 as_bad("Bit number out of range %d", nib
);
681 operand
[1].mode
= ABS8DST
;
685 operand
[0].mode
= ABS8SRC
;
701 operand
[0].mode
= ABS16OR8SRC
;
705 operand
[0].mode
= ABS16SRC
;
710 operand
[0].mode
= DISP8
;
725 nib
= operand
[0].reg
;
732 nib
= operand
[1].reg
;
739 if (*nibble_ptr
& B31
) {
744 *output_ptr
= nib
<< 4;
755 /* output any fixes */
756 for (i
= 0; i
< 2; i
++)
758 switch (operand
[i
].mode
) {
763 check_operand(operand
+i
, 0x7f,"@");
766 output
- frag_now
->fr_literal
+ 1,
768 operand
[i
].exp
.X_add_symbol
,
769 operand
[i
].exp
.X_subtract_symbol
,
770 operand
[i
].exp
.X_add_number
-1,
775 check_operand(operand
+i
, 0xff,"#");
776 /* If there is nothing else going on we can safely
778 if (operand
[i
].exp
.X_add_symbol
== 0)
780 output
[1] = operand
[i
].exp
.X_add_number
;
785 output
- frag_now
->fr_literal
+ 1,
787 operand
[i
].exp
.X_add_symbol
,
788 operand
[i
].exp
.X_subtract_symbol
,
789 operand
[i
].exp
.X_add_number
,
796 check_operand(operand
+i
, 0xff,"@@");
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
,
808 check_operand(operand
+i
, 0xff,"@");
810 output
- frag_now
->fr_literal
+ 1,
812 operand
[i
].exp
.X_add_symbol
,
813 operand
[i
].exp
.X_subtract_symbol
,
814 operand
[i
].exp
.X_add_number
,
821 check_operand(operand
+i
, 0xffff,"@");
824 output
- frag_now
->fr_literal
+ 2,
826 operand
[i
].exp
.X_add_symbol
,
827 operand
[i
].exp
.X_subtract_symbol
,
828 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
,
852 check_operand(operand
+i
, 0xffff,"@");
853 if (operand
[i
].exp
.X_add_symbol
== 0)
855 /* This should be done with bfd */
856 output
[3] = operand
[i
].exp
.X_add_number
& 0xff;
857 output
[2] = operand
[i
].exp
.X_add_number
>> 8;
864 output
- frag_now
->fr_literal
+ 2,
866 operand
[i
].exp
.X_add_symbol
,
867 operand
[i
].exp
.X_subtract_symbol
,
868 operand
[i
].exp
.X_add_number
,
892 /* This is the guts of the machine-dependent assembler. STR points to a
893 machine dependent instruction. This funciton is supposed to emit
894 the frags/bytes it assembles to.
899 DEFUN(md_assemble
,(str
),
905 struct h8_op operand
[2];
906 struct h8_opcode
* opcode
;
909 /* Drop leading whitespace */
913 /* find the op code end */
914 for (op_start
= op_end
= str
;
915 *op_end
!= 0 && *op_end
!= ' ';
918 if (*op_end
== '.') {
928 if (op_end
== op_start
)
930 as_bad("can't find opcode ");
936 opcode
= (struct h8_opcode
*) hash_find(opcode_hash_control
,
941 as_bad("unknown opcode");
946 input_line_pointer
= get_operands(opcode
->noperands
, op_end
,
949 opcode
= get_specific(opcode
, operand
);
953 /* Allocate 2 bytes for the insn anyway */
954 char *where
=frag_more(2);
959 as_bad("illegal operands for opcode");
962 if (opcode
->size
&& dot
)
964 if (opcode
->size
!= *dot
)
966 as_warn("mismatch between opcode size and operand size");
970 build_bytes(opcode
, operand
);
975 DEFUN(tc_crawl_symbol_chain
, (headers
),
976 object_headers
*headers
)
978 printf("call to tc_crawl_symbol_chain \n");
981 symbolS
*DEFUN(md_undefined_symbol
,(name
),
988 DEFUN(tc_headers_hook
,(headers
),
989 object_headers
*headers
)
991 printf("call to tc_headers_hook \n");
998 /* Various routines to kill one day */
999 /* Equal to MAX_PRECISION in atof-ieee.c */
1000 #define MAX_LITTLENUMS 6
1002 /* Turn a string in input_line_pointer into a floating point constant of type
1003 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1004 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1007 md_atof(type
,litP
,sizeP
)
1013 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1014 LITTLENUM_TYPE
*wordP
;
1045 return "Bad call to MD_ATOF()";
1047 t
=atof_ieee(input_line_pointer
,type
,words
);
1049 input_line_pointer
=t
;
1051 *sizeP
=prec
* sizeof(LITTLENUM_TYPE
);
1052 for(wordP
=words
;prec
--;) {
1053 md_number_to_chars(litP
,(long)(*wordP
++),sizeof(LITTLENUM_TYPE
));
1054 litP
+=sizeof(LITTLENUM_TYPE
);
1056 return ""; /* Someone should teach Dean about null pointers */
1060 md_parse_option(argP
, cntP
, vecP
)
1070 int md_short_jump_size
;
1072 void tc_aout_fix_to_chars () { printf("call to tc_aout_fix_to_chars \n");
1074 void md_create_short_jump(ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1081 as_fatal("failed sanity check.");
1085 md_create_long_jump(ptr
,from_addr
,to_addr
,frag
,to_symbol
)
1087 long from_addr
, to_addr
;
1091 as_fatal("failed sanity check.");
1095 md_convert_frag(headers
, fragP
)
1096 object_headers
*headers
;
1099 { printf("call to md_convert_frag \n"); abort(); }
1102 DEFUN(md_section_align
,(seg
, size
),
1106 return((size
+ (1 << section_alignment
[(int) seg
]) - 1) & (-1 << section_alignment
[(int) seg
]));
1111 md_apply_fix(fixP
, val
)
1115 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1117 switch(fixP
->fx_size
) {
1137 void DEFUN(md_operand
, (expressionP
),expressionS
*expressionP
)
1140 int md_long_jump_size
;
1142 md_estimate_size_before_relax(fragP
, segment_type
)
1143 register fragS
*fragP
;
1144 register segT segment_type
;
1146 printf("call tomd_estimate_size_before_relax \n"); abort(); }
1147 /* Put number into target byte order */
1149 void DEFUN(md_number_to_chars
,(ptr
, use
, nbytes
),
1155 case 4: *ptr
++ = (use
>> 24) & 0xff;
1156 case 3: *ptr
++ = (use
>> 16) & 0xff;
1157 case 2: *ptr
++ = (use
>> 8) & 0xff;
1158 case 1: *ptr
++ = (use
>> 0) & 0xff;
1164 long md_pcrel_from(fixP
)
1165 fixS
*fixP
; { abort(); }
1167 void tc_coff_symbol_emit_hook() { }
1170 void tc_reloc_mangle(fix_ptr
, intr
, base
)
1172 struct internal_reloc
*intr
;
1176 symbolS
*symbol_ptr
;
1178 symbol_ptr
= fix_ptr
->fx_addsy
;
1180 /* If this relocation is attached to a symbol then it's ok
1182 if (fix_ptr
->fx_r_type
== RELOC_32
) {
1183 /* cons likes to create reloc32's whatever the size of the reloc..
1185 switch (fix_ptr
->fx_size
)
1189 intr
->r_type
= R_RELWORD
;
1192 intr
->r_type
= R_RELBYTE
;
1201 intr
->r_type
= fix_ptr
->fx_r_type
;
1204 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+base
;
1205 intr
->r_offset
= fix_ptr
->fx_offset
;
1208 intr
->r_symndx
= symbol_ptr
->sy_number
;
1210 intr
->r_symndx
= -1;