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
30 #define h8_opcodes ops
31 #include "opcode/h8300.h"
34 const char comment_chars
[] =
36 const char line_separator_chars
[] =
38 const char line_comment_chars
[] = "#";
40 /* This table describes all the machine specific pseudo-ops the assembler
41 has to support. The fields are:
42 pseudo-op name without dot
43 function to call to execute this pseudo-op
44 Integer arg to pass to the function
50 #define PSIZE (Hmode ? L_32 : L_16)
52 #define DSYMMODE (Hmode ? L_24 : L_16)
53 int bsize
= L_8
; /* default branch displacement */
70 const pseudo_typeS md_pseudo_table
[] =
73 {"h8300h", h8300hmode
, 0},
74 {"sbranch", sbranch
, L_8
},
75 {"lbranch", sbranch
, L_16
},
81 {"form", listing_psize
, 0},
82 {"heading", listing_title
, 0},
83 {"import", s_ignore
, 0},
84 {"page", listing_eject
, 0},
85 {"program", s_ignore
, 0},
89 const int md_reloc_size
;
91 const char EXP_CHARS
[] = "eE";
93 /* Chars that mean this number is a floating point constant */
96 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
99 const relax_typeS md_relax_table
[1];
102 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
105 This function is called once, at assembler startup time. This should
106 set up all the tables, etc that the MD part of the assembler needs
113 struct h8_opcode
*opcode
;
114 const struct reg_entry
*reg
;
115 char prev_buffer
[100];
118 opcode_hash_control
= hash_new ();
121 for (opcode
= h8_opcodes
; opcode
->name
; opcode
++)
123 /* Strip off any . part when inserting the opcode and only enter
124 unique codes into the hash table
126 char *src
= opcode
->name
;
127 unsigned int len
= strlen (src
);
128 char *dst
= malloc (len
+ 1);
143 if (strcmp (buffer
, prev_buffer
))
145 hash_insert (opcode_hash_control
, buffer
, (char *) opcode
);
146 strcpy (prev_buffer
, buffer
);
152 /* Find the number of operands */
153 opcode
->noperands
= 0;
154 while (opcode
->args
.nib
[opcode
->noperands
] != E
)
156 /* Find the length of the opcode in bytes */
158 while (opcode
->data
.nib
[opcode
->length
* 2] != E
)
172 int opsize
; /* Set when a register size is seen */
184 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
193 /* try and parse a reg name, returns number of chars consumed */
195 parse_reg (src
, mode
, reg
, direction
)
202 if (src
[0] == 's' && src
[1] == 'p')
204 *mode
= PSIZE
| REG
| direction
;
208 if (src
[0] == 'c' && src
[1] == 'c' && src
[2] == 'r')
214 if (src
[0] == 'f' && src
[1] == 'p')
216 *mode
= PSIZE
| REG
| direction
;
222 && src
[2] >= '0' && src
[2] <= '7')
224 *mode
= L_32
| REG
| direction
;
227 as_warn ("Reg only legal for H8/300-H");
232 && src
[1] >= '0' && src
[1] <= '7')
234 *mode
= L_16
| REG
| direction
;
235 *reg
= src
[1] - '0' + 8;
237 as_warn ("Reg only legal for H8/300-H");
243 if (src
[1] >= '0' && src
[1] <= '7')
247 *mode
= L_8
| REG
| direction
;
248 *reg
= (src
[1] - '0') + 8;
253 *mode
= L_8
| REG
| direction
;
254 *reg
= (src
[1] - '0');
257 *mode
= L_16
| REG
| direction
;
258 *reg
= (src
[1] - '0');
266 DEFUN (parse_exp
, (s
, op
),
270 char *save
= input_line_pointer
;
274 input_line_pointer
= s
;
276 new = input_line_pointer
;
277 input_line_pointer
= save
;
278 if (SEG_NORMAL (seg
))
289 as_bad ("Missing operand");
292 as_bad ("Don't understand operand of type %s", segment_name (seg
));
298 skip_colonthing (ptr
, exp
, mode
)
309 /* ff fill any 8 bit quantity */
310 exp
->X_add_number
|= 0xff00;
319 else if (*ptr
== '1')
323 while (isdigit (*ptr
))
330 /* The many forms of operand:
333 @Rn Register indirect
334 @(exp[:16], Rn) Register indirect with displacement
338 @aa:16 absolute 16 bit
341 #xx[:size] immediate data
342 @(exp:[8], pc) pc rel
343 @@aa[:8] memory indirect
354 src
= skip_colonthing (src
, &op
->exp
, &mode
);
358 /* Choose a default mode */
359 if (op
->exp
.X_add_number
< -32768
360 || op
->exp
.X_add_number
> 32767)
367 else if (op
->exp
.X_add_symbol
368 || op
->exp
.X_subtract_symbol
)
380 get_operand (ptr
, op
, dst
, direction
)
394 len
= parse_reg (src
, &op
->mode
, &op
->reg
, direction
);
407 src
= parse_exp (src
, &op
->exp
);
409 src
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
422 len
= parse_reg (src
, &mode
, &num
, direction
);
425 /* Oops, not a reg after all, must be ordinary exp */
427 /* must be a symbol */
428 op
->mode
= ABS
| PSIZE
| direction
;
429 *ptr
= skip_colonthing (parse_exp (src
, &op
->exp
),
430 &op
->exp
, &op
->mode
);
438 if ((mode
& SIZE
) != PSIZE
)
439 as_bad ("Wrong size pointer register for architecture.");
450 /* Start off assuming a 16 bit offset */
453 src
= parse_exp (src
, &op
->exp
);
455 src
= colonmod24 (op
, src
);
460 op
->mode
= DISP
| direction
;
467 as_bad ("expected @(exp, reg16)");
473 len
= parse_reg (src
, &mode
, &op
->reg
, direction
);
474 if (len
== 0 || !(mode
& REG
))
476 as_bad ("expected @(exp, reg16)");
479 op
->mode
|= DISP
| direction
;
482 src
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
484 if (*src
!= ')' && '(')
486 as_bad ("expected @(exp, reg16)");
493 len
= parse_reg (src
, &mode
, &num
, direction
);
501 if ((mode
& SIZE
) != PSIZE
)
502 as_bad ("Wrong size pointer register for architecture.");
508 if ((mode
& SIZE
) != PSIZE
)
509 as_bad ("Wrong size pointer register for architecture.");
511 op
->mode
= direction
| IND
| PSIZE
;
519 /* must be a symbol */
521 op
->mode
= ABS
| direction
;
522 src
= parse_exp (src
, &op
->exp
);
524 *ptr
= colonmod24 (op
, src
);
535 src
= parse_exp (src
, &op
->exp
);
536 *ptr
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
542 src
= parse_exp (src
, &op
->exp
);
543 /* Trailing ':' size ? */
546 if (src
[1] == '1' && src
[2] == '6')
548 op
->mode
= PCREL
| L_16
;
551 else if (src
[1] == '8')
553 op
->mode
= PCREL
| L_8
;
558 as_bad ("expect :8 or :16 here");
563 op
->mode
= PCREL
| bsize
;
572 DEFUN (get_operands
, (noperands
, op_end
, operand
),
573 unsigned int noperands AND
575 struct h8_op
*operand
)
588 get_operand (&ptr
, operand
+ 0, 0, SRC
);
592 get_operand (&ptr
, operand
+ 1, 1, DST
);
602 get_operand (&ptr
, operand
+ 0, 0, SRC
);
605 get_operand (&ptr
, operand
+ 1, 1, DST
);
616 /* Passed a pointer to a list of opcodes which use different
617 addressing modes, return the opcode which matches the opcodes
622 get_specific (opcode
, operands
)
623 struct h8_opcode
*opcode
;
624 struct h8_op
*operands
;
626 struct h8_opcode
*this_try
= opcode
;
629 unsigned int this_index
= opcode
->idx
;
631 while (this_index
== opcode
->idx
&& !found
)
637 for (i
= 0; i
< this_try
->noperands
&& found
; i
++)
639 op_type op
= this_try
->args
.nib
[i
];
640 int x
= operands
[i
].mode
;
642 if ((op
& (DISP
| REG
)) == (DISP
| REG
)
643 && ((x
& DISP
| REG
) == (DISP
| REG
)))
645 dispreg
= operands
[i
].reg
;
654 x
= (x
& ~L_P
) | (Hmode
? L_32
: L_16
);
658 op
= (op
& ~L_P
) | (Hmode
? L_32
: L_16
);
663 /* The size of the reg is v important */
664 if ((op
& SIZE
) != (x
& SIZE
))
667 else if ((op
& ABSJMP
) && (x
& ABS
))
669 operands
[i
].mode
&= ~ABS
;
670 operands
[i
].mode
|= ABSJMP
;
671 /* But it may not be 24 bits long */
674 operands
[i
].mode
&= ~SIZE
;
675 operands
[i
].mode
|= L_16
;
680 else if ((op
& (KBIT
| DBIT
)) && (x
& IMM
))
682 /* This is ok if the immediate value is sensible */
688 /* The size of the displacement is important */
689 if ((op
& SIZE
) != (x
& SIZE
))
693 else if ((op
& (DISP
| IMM
| ABS
))
694 && (op
& (DISP
| IMM
| ABS
)) == (x
& (DISP
| IMM
| ABS
)))
696 /* Got a diplacement,will fit if no size or same size as try */
698 && ((op
& SIZE
) != (x
& SIZE
)))
701 else if ((op
& ABSMOV
) && (x
& ABS
))
705 else if ((op
& MODE
) != (x
& MODE
))
719 DEFUN (check_operand
, (operand
, width
, string
),
720 struct h8_op
*operand AND
721 unsigned int width AND
724 if (operand
->exp
.X_add_symbol
== 0
725 && operand
->exp
.X_subtract_symbol
== 0)
728 /* No symbol involved, let's look at offset, it's dangerous if any of
729 the high bits are not 0 or ff's, find out by oring or anding with
730 the width and seeing if the answer is 0 or all fs*/
731 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
732 (operand
->exp
.X_add_number
| width
) != (~0))
734 as_warn ("operand %s0x%x out of range.", string
, operand
->exp
.X_add_number
);
741 do_a_fix_imm (offset
, operand
, relaxing
)
743 struct h8_op
*operand
;
751 char *t
= operand
->mode
& IMM
? "#" : "@";
753 if (operand
->exp
.X_add_symbol
== 0)
755 char *bytes
= frag_now
->fr_literal
+ offset
;
756 switch (operand
->mode
& SIZE
)
759 check_operand (operand
, 0x7, t
);
760 bytes
[0] |= (operand
->exp
.X_add_number
) << 4;
763 check_operand (operand
, 0xff, t
);
764 bytes
[0] = operand
->exp
.X_add_number
;
767 check_operand (operand
, 0xffff, t
);
768 bytes
[0] = operand
->exp
.X_add_number
>> 8;
769 bytes
[1] = operand
->exp
.X_add_number
>> 0;
772 check_operand (operand
, 0xffffff, t
);
773 bytes
[0] = operand
->exp
.X_add_number
>> 16;
774 bytes
[1] = operand
->exp
.X_add_number
>> 8;
775 bytes
[2] = operand
->exp
.X_add_number
>> 0;
779 /* This should be done with bfd */
780 bytes
[0] = operand
->exp
.X_add_number
>> 24;
781 bytes
[1] = operand
->exp
.X_add_number
>> 16;
782 bytes
[2] = operand
->exp
.X_add_number
>> 8;
783 bytes
[3] = operand
->exp
.X_add_number
>> 0;
790 switch (operand
->mode
& SIZE
)
797 idx
= relaxing
? R_MOVLB1
: R_RELLONG
;
808 idx
= relaxing
? R_MOVB1
: R_RELWORD
;
820 operand
->exp
.X_add_symbol
,
821 operand
->exp
.X_subtract_symbol
,
822 (short) (operand
->exp
.X_add_number
),
829 /* Now we know what sort of opcodes it is, lets build the bytes -
832 build_bytes (this_try
, operand
)
833 struct h8_opcode
*this_try
;
834 struct h8_op
*operand
;
838 char *output
= frag_more (this_try
->length
);
839 char *output_ptr
= output
;
840 op_type
*nibble_ptr
= this_try
->data
.nib
;
844 unsigned int nibble_count
= 0;
851 if (!(this_try
->inbase
|| Hmode
))
853 as_warn ("Opcode `%s' only available on H8/300-H", this_try
->name
);
856 while (*nibble_ptr
!= E
)
870 if (c
& (REG
| IND
| INC
| DEC
))
872 nib
= operand
[d
].reg
;
874 else if ((c
& DISPREG
) == (DISPREG
))
881 operand
[d
].mode
&= ~ABS
;
882 operand
[d
].mode
|= ABSMOV
;
883 immat
= nibble_count
/ 2;
886 else if (c
& (IMM
| PCREL
| ABS
| ABSJMP
| DISP
))
889 immat
= nibble_count
/ 2;
898 switch (operand
[0].exp
.X_add_number
)
907 as_bad ("Need #1 or #2 here");
912 switch (operand
[0].exp
.X_add_number
)
922 as_warn ("#4 only valid in h8/300 mode.");
927 as_bad ("Need #1 or #2 here");
930 /* stop it making a fix */
944 for (i
= 0; i
< this_try
->length
; i
++)
946 output
[i
] = (asnibbles
[i
* 2] << 4) | asnibbles
[i
* 2 + 1];
949 /* output any fixes */
950 for (i
= 0; i
< 2; i
++)
952 int x
= operand
[i
].mode
;
954 if (x
& (IMM
| ABS
| DISP
))
956 do_a_fix_imm (output
- frag_now
->fr_literal
+ immat
, operand
+ i
,0);
960 int size16
= x
& L_16
;
961 int where
= size16
? 2 : 1;
962 int size
= size16
? 2 : 1;
963 int type
= size16
? R_PCRWORD
: R_PCRBYTE
;
965 check_operand (operand
+ i
, size16
? 0x7fff : 0x7f, "@");
967 if (operand
[i
].exp
.X_add_number
& 1)
969 as_warn ("branch operand has odd offset (%x)\n",
970 operand
->exp
.X_add_number
);
974 output
- frag_now
->fr_literal
+ where
,
976 operand
[i
].exp
.X_add_symbol
,
977 operand
[i
].exp
.X_subtract_symbol
,
978 (char) (operand
[i
].exp
.X_add_number
- 1),
985 check_operand (operand
+ i
, 0xff, "@@");
987 output
- frag_now
->fr_literal
+ 1,
989 operand
[i
].exp
.X_add_symbol
,
990 operand
[i
].exp
.X_subtract_symbol
,
991 operand
[i
].exp
.X_add_number
,
998 /* This mov is either absolute long or thru a memory loc */
999 do_a_fix_imm (output
- frag_now
->fr_literal
+ immat
, operand
+ i
,1);
1002 else if (x
& ABSJMP
)
1004 /* This jmp may be a jump or a branch */
1006 check_operand (operand
+ i
, Hmode
? 0xfffff : 0xffff, "@");
1007 if (operand
[i
].exp
.X_add_number
& 1)
1009 as_warn ("branch operand has odd offset (%x)\n",
1010 operand
->exp
.X_add_number
);
1013 output
- frag_now
->fr_literal
,
1015 operand
[i
].exp
.X_add_symbol
,
1016 operand
[i
].exp
.X_subtract_symbol
,
1017 (short) (operand
[i
].exp
.X_add_number
),
1026 try and give an intelligent error message for common and simple to
1031 DEFUN (clever_message
, (opcode
, operand
),
1032 struct h8_opcode
*opcode AND
1033 struct h8_op
*operand
)
1035 struct h8_opcode
*scan
= opcode
;
1037 /* Find out if there was more than one possible opccode */
1039 if ((opcode
+ 1)->idx
!= opcode
->idx
)
1043 /* Only one opcode of this flavour, try and guess which operand
1045 for (argn
= 0; argn
< opcode
->noperands
; argn
++)
1047 switch (opcode
->args
.nib
[argn
])
1050 if (operand
[argn
].mode
!= RD16
)
1052 as_bad ("destination operand must be 16 bit register");
1060 if (operand
[argn
].mode
!= RS8
)
1062 as_bad ("source operand must be 8 bit register");
1068 if (operand
[argn
].mode
!= ABS16DST
)
1070 as_bad ("destination operand must be 16bit absolute address");
1075 if (operand
[argn
].mode
!= RD8
)
1077 as_bad ("destination operand must be 8 bit register");
1084 if (operand
[argn
].mode
!= ABS16SRC
)
1086 as_bad ("source operand must be 16bit absolute address");
1094 as_bad ("invalid operands");
1097 /* This is the guts of the machine-dependent assembler. STR points to a
1098 machine dependent instruction. This funciton is supposed to emit
1099 the frags/bytes it assembles to.
1105 DEFUN (md_assemble
, (str
),
1111 struct h8_op operand
[2];
1112 struct h8_opcode
*opcode
;
1113 struct h8_opcode
*prev_opcode
;
1118 /* Drop leading whitespace */
1122 /* find the op code end */
1123 for (op_start
= op_end
= str
;
1124 *op_end
!= 0 && *op_end
!= ' ';
1138 if (op_end
== op_start
)
1140 as_bad ("can't find opcode ");
1146 opcode
= (struct h8_opcode
*) hash_find (opcode_hash_control
,
1151 as_bad ("unknown opcode");
1156 input_line_pointer
= get_operands (opcode
->noperands
, op_end
,
1159 prev_opcode
= opcode
;
1161 opcode
= get_specific (opcode
, operand
);
1165 /* Couldn't find an opcode which matched the operands */
1166 char *where
= frag_more (2);
1170 clever_message (prev_opcode
, operand
);
1174 if (opcode
->size
&& dot
)
1176 if (opcode
->size
!= *dot
)
1178 as_warn ("mismatch between opcode size and operand size");
1182 build_bytes (opcode
, operand
);
1187 DEFUN (tc_crawl_symbol_chain
, (headers
),
1188 object_headers
* headers
)
1190 printf ("call to tc_crawl_symbol_chain \n");
1194 DEFUN (md_undefined_symbol
, (name
),
1201 DEFUN (tc_headers_hook
, (headers
),
1202 object_headers
* headers
)
1204 printf ("call to tc_headers_hook \n");
1212 /* Various routines to kill one day */
1213 /* Equal to MAX_PRECISION in atof-ieee.c */
1214 #define MAX_LITTLENUMS 6
1216 /* Turn a string in input_line_pointer into a floating point constant of type
1217 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1218 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1221 md_atof (type
, litP
, sizeP
)
1227 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1228 LITTLENUM_TYPE
*wordP
;
1260 return "Bad call to MD_ATOF()";
1262 t
= atof_ieee (input_line_pointer
, type
, words
);
1264 input_line_pointer
= t
;
1266 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1267 for (wordP
= words
; prec
--;)
1269 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1270 litP
+= sizeof (LITTLENUM_TYPE
);
1276 md_parse_option (argP
, cntP
, vecP
)
1286 int md_short_jump_size
;
1289 tc_aout_fix_to_chars ()
1291 printf ("call to tc_aout_fix_to_chars \n");
1296 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1303 as_fatal ("failed sanity check.");
1307 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1309 addressT from_addr
, to_addr
;
1313 as_fatal ("failed sanity check.");
1317 md_convert_frag (headers
, fragP
)
1318 object_headers
*headers
;
1322 printf ("call to md_convert_frag \n");
1327 DEFUN (md_section_align
, (seg
, size
),
1331 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1) & (-1 << section_alignment
[(int) seg
]));
1336 md_apply_fix (fixP
, val
)
1340 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1342 switch (fixP
->fx_size
)
1348 *buf
++ = (val
>> 8);
1352 *buf
++ = (val
>> 24);
1353 *buf
++ = (val
>> 16);
1354 *buf
++ = (val
>> 8);
1364 DEFUN (md_operand
, (expressionP
), expressionS
* expressionP
)
1368 int md_long_jump_size
;
1370 md_estimate_size_before_relax (fragP
, segment_type
)
1371 register fragS
*fragP
;
1372 register segT segment_type
;
1374 printf ("call tomd_estimate_size_before_relax \n");
1378 /* Put number into target byte order */
1381 DEFUN (md_number_to_chars
, (ptr
, use
, nbytes
),
1389 *ptr
++ = (use
>> 24) & 0xff;
1391 *ptr
++ = (use
>> 16) & 0xff;
1393 *ptr
++ = (use
>> 8) & 0xff;
1395 *ptr
++ = (use
>> 0) & 0xff;
1402 md_pcrel_from (fixP
)
1409 tc_coff_symbol_emit_hook ()
1415 tc_reloc_mangle (fix_ptr
, intr
, base
)
1417 struct internal_reloc
*intr
;
1421 symbolS
*symbol_ptr
;
1423 symbol_ptr
= fix_ptr
->fx_addsy
;
1425 /* If this relocation is attached to a symbol then it's ok
1427 if (fix_ptr
->fx_r_type
== RELOC_32
)
1429 /* cons likes to create reloc32's whatever the size of the reloc..
1431 switch (fix_ptr
->fx_size
)
1435 intr
->r_type
= R_RELWORD
;
1438 intr
->r_type
= R_RELBYTE
;
1448 intr
->r_type
= fix_ptr
->fx_r_type
;
1451 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1452 intr
->r_offset
= fix_ptr
->fx_offset
;
1455 intr
->r_symndx
= symbol_ptr
->sy_number
;
1457 intr
->r_symndx
= -1;
1462 tc_coff_sizemachdep ()
1467 /* end of tc-h8300.c */