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 */
75 const pseudo_typeS md_pseudo_table
[] =
78 {"h8300h", h8300hmode
, 0},
79 {"sbranch", sbranch
, L_8
},
80 {"lbranch", sbranch
, L_16
},
86 {"form", listing_psize
, 0},
87 {"heading", listing_title
, 0},
88 {"import", s_ignore
, 0},
89 {"page", listing_eject
, 0},
90 {"program", s_ignore
, 0},
94 const int md_reloc_size
;
96 const char EXP_CHARS
[] = "eE";
98 /* Chars that mean this number is a floating point constant */
101 const char FLT_CHARS
[] = "rRsSfFdDxXpP";
104 const relax_typeS md_relax_table
[1];
107 static struct hash_control
*opcode_hash_control
; /* Opcode mnemonics */
110 This function is called once, at assembler startup time. This should
111 set up all the tables, etc that the MD part of the assembler needs
118 struct h8_opcode
*opcode
;
119 const struct reg_entry
*reg
;
120 char prev_buffer
[100];
123 opcode_hash_control
= hash_new ();
126 for (opcode
= h8_opcodes
; opcode
->name
; opcode
++)
128 /* Strip off any . part when inserting the opcode and only enter
129 unique codes into the hash table
131 char *src
= opcode
->name
;
132 unsigned int len
= strlen (src
);
133 char *dst
= malloc (len
+ 1);
148 if (strcmp (buffer
, prev_buffer
))
150 hash_insert (opcode_hash_control
, buffer
, (char *) opcode
);
151 strcpy (prev_buffer
, buffer
);
157 /* Find the number of operands */
158 opcode
->noperands
= 0;
159 while (opcode
->args
.nib
[opcode
->noperands
] != E
)
161 /* Find the length of the opcode in bytes */
163 while (opcode
->data
.nib
[opcode
->length
* 2] != E
)
177 int opsize
; /* Set when a register size is seen */
189 WREG r0,r1,r2,r3,r4,r5,r6,r7,fp,sp
198 /* try and parse a reg name, returns number of chars consumed */
200 parse_reg (src
, mode
, reg
, direction
)
207 if (src
[0] == 's' && src
[1] == 'p')
209 *mode
= PSIZE
| REG
| direction
;
213 if (src
[0] == 'c' && src
[1] == 'c' && src
[2] == 'r')
219 if (src
[0] == 'f' && src
[1] == 'p')
221 *mode
= PSIZE
| REG
| direction
;
227 && src
[2] >= '0' && src
[2] <= '7')
229 *mode
= L_32
| REG
| direction
;
232 as_warn ("Reg only legal for H8/300-H");
237 && src
[1] >= '0' && src
[1] <= '7')
239 *mode
= L_16
| REG
| direction
;
240 *reg
= src
[1] - '0' + 8;
242 as_warn ("Reg only legal for H8/300-H");
248 if (src
[1] >= '0' && src
[1] <= '7')
252 *mode
= L_8
| REG
| direction
;
253 *reg
= (src
[1] - '0') + 8;
258 *mode
= L_8
| REG
| direction
;
259 *reg
= (src
[1] - '0');
262 *mode
= L_16
| REG
| direction
;
263 *reg
= (src
[1] - '0');
275 char *save
= input_line_pointer
;
278 input_line_pointer
= s
;
280 if (op
->X_op
== O_absent
)
281 as_bad ("missing operand");
282 new = input_line_pointer
;
283 input_line_pointer
= save
;
288 skip_colonthing (ptr
, exp
, mode
)
299 /* ff fill any 8 bit quantity */
300 /* exp->X_add_number -= 0x100;*/
309 else if (*ptr
== '1')
313 while (isdigit (*ptr
))
320 /* The many forms of operand:
323 @Rn Register indirect
324 @(exp[:16], Rn) Register indirect with displacement
328 @aa:16 absolute 16 bit
331 #xx[:size] immediate data
332 @(exp:[8], pc) pc rel
333 @@aa[:8] memory indirect
344 src
= skip_colonthing (src
, &op
->exp
, &mode
);
348 /* Choose a default mode */
349 if (op
->exp
.X_add_number
< -32768
350 || op
->exp
.X_add_number
> 32767)
357 else if (op
->exp
.X_add_symbol
358 || op
->exp
.X_op_symbol
)
370 get_operand (ptr
, op
, dst
, direction
)
384 len
= parse_reg (src
, &op
->mode
, &op
->reg
, direction
);
397 src
= parse_exp (src
, &op
->exp
);
399 src
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
412 len
= parse_reg (src
, &mode
, &num
, direction
);
415 /* Oops, not a reg after all, must be ordinary exp */
417 /* must be a symbol */
418 op
->mode
= ABS
| PSIZE
| direction
;
419 *ptr
= skip_colonthing (parse_exp (src
, &op
->exp
),
420 &op
->exp
, &op
->mode
);
428 if ((mode
& SIZE
) != PSIZE
)
429 as_bad ("Wrong size pointer register for architecture.");
440 /* Start off assuming a 16 bit offset */
443 src
= parse_exp (src
, &op
->exp
);
445 src
= colonmod24 (op
, src
);
450 op
->mode
|= ABS
| direction
;
457 as_bad ("expected @(exp, reg16)");
463 len
= parse_reg (src
, &mode
, &op
->reg
, direction
);
464 if (len
== 0 || !(mode
& REG
))
466 as_bad ("expected @(exp, reg16)");
469 op
->mode
|= DISP
| direction
;
472 src
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
474 if (*src
!= ')' && '(')
476 as_bad ("expected @(exp, reg16)");
483 len
= parse_reg (src
, &mode
, &num
, direction
);
491 if ((mode
& SIZE
) != PSIZE
)
492 as_bad ("Wrong size pointer register for architecture.");
498 if ((mode
& SIZE
) != PSIZE
)
499 as_bad ("Wrong size pointer register for architecture.");
501 op
->mode
= direction
| IND
| PSIZE
;
509 /* must be a symbol */
511 op
->mode
= ABS
| direction
;
512 src
= parse_exp (src
, &op
->exp
);
514 *ptr
= colonmod24 (op
, src
);
525 src
= parse_exp (src
, &op
->exp
);
526 *ptr
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
532 src
= parse_exp (src
, &op
->exp
);
533 /* Trailing ':' size ? */
536 if (src
[1] == '1' && src
[2] == '6')
538 op
->mode
= PCREL
| L_16
;
541 else if (src
[1] == '8')
543 op
->mode
= PCREL
| L_8
;
548 as_bad ("expect :8 or :16 here");
553 op
->mode
= PCREL
| bsize
;
562 get_operands (noperands
, op_end
, operand
)
563 unsigned int noperands
;
565 struct h8_op
*operand
;
578 get_operand (&ptr
, operand
+ 0, 0, SRC
);
582 get_operand (&ptr
, operand
+ 1, 1, DST
);
592 get_operand (&ptr
, operand
+ 0, 0, SRC
);
595 get_operand (&ptr
, operand
+ 1, 1, DST
);
606 /* Passed a pointer to a list of opcodes which use different
607 addressing modes, return the opcode which matches the opcodes
612 get_specific (opcode
, operands
)
613 struct h8_opcode
*opcode
;
614 struct h8_op
*operands
;
616 struct h8_opcode
*this_try
= opcode
;
619 unsigned int this_index
= opcode
->idx
;
621 while (this_index
== opcode
->idx
&& !found
)
627 for (i
= 0; i
< this_try
->noperands
&& found
; i
++)
629 op_type op
= this_try
->args
.nib
[i
];
630 int x
= operands
[i
].mode
;
632 if ((op
& (DISP
| REG
)) == (DISP
| REG
)
633 && ((x
& DISP
| REG
) == (DISP
| REG
)))
635 dispreg
= operands
[i
].reg
;
644 x
= (x
& ~L_P
) | (Hmode
? L_32
: L_16
);
648 op
= (op
& ~L_P
) | (Hmode
? L_32
: L_16
);
653 /* The size of the reg is v important */
654 if ((op
& SIZE
) != (x
& SIZE
))
657 else if ((op
& ABSJMP
) && (x
& ABS
))
659 operands
[i
].mode
&= ~ABS
;
660 operands
[i
].mode
|= ABSJMP
;
661 /* But it may not be 24 bits long */
664 operands
[i
].mode
&= ~SIZE
;
665 operands
[i
].mode
|= L_16
;
670 else if ((op
& (KBIT
| DBIT
)) && (x
& IMM
))
672 /* This is ok if the immediate value is sensible */
678 /* The size of the displacement is important */
679 if ((op
& SIZE
) != (x
& SIZE
))
683 else if ((op
& (DISP
| IMM
| ABS
))
684 && (op
& (DISP
| IMM
| ABS
)) == (x
& (DISP
| IMM
| ABS
)))
686 /* Got a diplacement,will fit if no size or same size as try */
687 if (op
& ABS
&& op
& L_8
)
689 /* We want an 8 bit abs here, but one which looks like 16 bits will do fine */
695 && ((op
& SIZE
) != (x
& SIZE
)))
699 else if ((op
& ABSMOV
) && (x
& ABS
))
705 else if ((op
& MODE
) != (x
& MODE
))
719 check_operand (operand
, width
, string
)
720 struct h8_op
*operand
;
724 if (operand
->exp
.X_add_symbol
== 0
725 && operand
->exp
.X_op_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*/
732 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
733 (operand
->exp
.X_add_number
| width
) != (~0))
736 && (operand
->exp
.X_add_number
& 0xff00) == 0xff00)
738 /* Just ignore this one - which happens when trying to
739 fit a 16 bit address truncated into an 8 bit address of something like bset */
743 as_warn ("operand %s0x%x out of range.", string
, operand
->exp
.X_add_number
);
751 do_a_fix_imm (offset
, operand
, relaxing
)
753 struct h8_op
*operand
;
761 char *t
= operand
->mode
& IMM
? "#" : "@";
763 if (operand
->exp
.X_add_symbol
== 0)
765 char *bytes
= frag_now
->fr_literal
+ offset
;
766 switch (operand
->mode
& SIZE
)
769 check_operand (operand
, 0x3, t
);
770 bytes
[0] |= (operand
->exp
.X_add_number
) << 4;
773 check_operand (operand
, 0x7, t
);
774 bytes
[0] |= (operand
->exp
.X_add_number
) << 4;
777 check_operand (operand
, 0xff, t
);
778 bytes
[0] = operand
->exp
.X_add_number
;
781 check_operand (operand
, 0xffff, t
);
782 bytes
[0] = operand
->exp
.X_add_number
>> 8;
783 bytes
[1] = operand
->exp
.X_add_number
>> 0;
786 check_operand (operand
, 0xffffff, t
);
787 bytes
[0] = operand
->exp
.X_add_number
>> 16;
788 bytes
[1] = operand
->exp
.X_add_number
>> 8;
789 bytes
[2] = operand
->exp
.X_add_number
>> 0;
793 /* This should be done with bfd */
794 bytes
[0] = operand
->exp
.X_add_number
>> 24;
795 bytes
[1] = operand
->exp
.X_add_number
>> 16;
796 bytes
[2] = operand
->exp
.X_add_number
>> 8;
797 bytes
[3] = operand
->exp
.X_add_number
>> 0;
804 switch (operand
->mode
& SIZE
)
810 idx
= relaxing
? R_MOVLB1
: R_RELLONG
;
813 as_bad("Can't work out size of operand.\n");
822 idx
= relaxing
? R_MOVB1
: R_RELWORD
;
830 /* Sign extend any expression */
831 operand
->exp
.X_add_number
= (short)operand
->exp
.X_add_number
;
832 fix_new_exp (frag_now
,
842 /* Now we know what sort of opcodes it is, lets build the bytes -
845 build_bytes (this_try
, operand
)
846 struct h8_opcode
*this_try
;
847 struct h8_op
*operand
;
851 char *output
= frag_more (this_try
->length
);
852 char *output_ptr
= output
;
853 op_type
*nibble_ptr
= this_try
->data
.nib
;
857 unsigned int nibble_count
= 0;
864 if (!(this_try
->inbase
|| Hmode
))
866 as_warn ("Opcode `%s' only available on H8/300-H", this_try
->name
);
869 while (*nibble_ptr
!= E
)
883 if (c
& (REG
| IND
| INC
| DEC
))
885 nib
= operand
[d
].reg
;
887 else if ((c
& DISPREG
) == (DISPREG
))
894 operand
[d
].mode
&= ~ABS
;
895 operand
[d
].mode
|= ABSMOV
;
896 immat
= nibble_count
/ 2;
902 absat
= nibble_count
/ 2;
905 else if (c
& (IMM
| PCREL
| ABS
| ABSJMP
| DISP
))
908 immat
= nibble_count
/ 2;
917 switch (operand
[0].exp
.X_add_number
)
926 as_bad ("Need #1 or #2 here");
931 switch (operand
[0].exp
.X_add_number
)
941 as_warn ("#4 only valid in h8/300 mode.");
946 as_bad ("Need #1 or #2 here");
949 /* stop it making a fix */
963 for (i
= 0; i
< this_try
->length
; i
++)
965 output
[i
] = (asnibbles
[i
* 2] << 4) | asnibbles
[i
* 2 + 1];
968 /* output any fixes */
969 for (i
= 0; i
< 2; i
++)
971 int x
= operand
[i
].mode
;
973 if (x
& (IMM
| DISP
))
975 do_a_fix_imm (output
- frag_now
->fr_literal
+ immat
, operand
+ i
, 0);
979 do_a_fix_imm (output
- frag_now
->fr_literal
+ absat
, operand
+ i
, 0);
983 int size16
= x
& L_16
;
984 int where
= size16
? 2 : 1;
985 int size
= size16
? 2 : 1;
986 int type
= size16
? R_PCRWORD
: R_PCRBYTE
;
988 check_operand (operand
+ i
, size16
? 0x7fff : 0x7f, "@");
990 if (operand
[i
].exp
.X_add_number
& 1)
992 as_warn ("branch operand has odd offset (%x)\n",
993 operand
->exp
.X_add_number
);
996 operand
[i
].exp
.X_add_number
=
997 (char) (operand
[i
].exp
.X_add_number
- 1);
998 fix_new_exp (frag_now
,
999 output
- frag_now
->fr_literal
+ where
,
1005 else if (x
& MEMIND
)
1008 check_operand (operand
+ i
, 0xff, "@@");
1009 fix_new_exp (frag_now
,
1010 output
- frag_now
->fr_literal
+ 1,
1017 else if (x
& ABSMOV
)
1019 /* This mov is either absolute long or thru a memory loc */
1020 do_a_fix_imm (output
- frag_now
->fr_literal
+ immat
, operand
+ i
, 1);
1023 else if (x
& ABSJMP
)
1025 /* This jmp may be a jump or a branch */
1027 check_operand (operand
+ i
, Hmode
? 0xffffff : 0xffff, "@");
1028 if (operand
[i
].exp
.X_add_number
& 1)
1030 as_warn ("branch operand has odd offset (%x)\n",
1031 operand
->exp
.X_add_number
);
1034 operand
[i
].exp
.X_add_number
= (short) operand
[i
].exp
.X_add_number
;
1035 fix_new_exp (frag_now
,
1036 output
- frag_now
->fr_literal
,
1047 try and give an intelligent error message for common and simple to
1052 clever_message (opcode
, operand
)
1053 struct h8_opcode
*opcode
;
1054 struct h8_op
*operand
;
1056 struct h8_opcode
*scan
= opcode
;
1058 /* Find out if there was more than one possible opccode */
1060 if ((opcode
+ 1)->idx
!= opcode
->idx
)
1064 /* Only one opcode of this flavour, try and guess which operand
1066 for (argn
= 0; argn
< opcode
->noperands
; argn
++)
1068 switch (opcode
->args
.nib
[argn
])
1071 if (operand
[argn
].mode
!= RD16
)
1073 as_bad ("destination operand must be 16 bit register");
1081 if (operand
[argn
].mode
!= RS8
)
1083 as_bad ("source operand must be 8 bit register");
1089 if (operand
[argn
].mode
!= ABS16DST
)
1091 as_bad ("destination operand must be 16bit absolute address");
1096 if (operand
[argn
].mode
!= RD8
)
1098 as_bad ("destination operand must be 8 bit register");
1105 if (operand
[argn
].mode
!= ABS16SRC
)
1107 as_bad ("source operand must be 16bit absolute address");
1115 as_bad ("invalid operands");
1118 /* This is the guts of the machine-dependent assembler. STR points to a
1119 machine dependent instruction. This funciton is supposed to emit
1120 the frags/bytes it assembles to.
1132 struct h8_op operand
[2];
1133 struct h8_opcode
*opcode
;
1134 struct h8_opcode
*prev_opcode
;
1139 /* Drop leading whitespace */
1143 /* find the op code end */
1144 for (op_start
= op_end
= str
;
1145 *op_end
!= 0 && *op_end
!= ' ';
1159 if (op_end
== op_start
)
1161 as_bad ("can't find opcode ");
1167 opcode
= (struct h8_opcode
*) hash_find (opcode_hash_control
,
1172 as_bad ("unknown opcode");
1176 /* We use to set input_line_pointer to the result of get_operands,
1177 but that is wrong. Our caller assumes we don't change it. */
1179 (void) get_operands (opcode
->noperands
, op_end
, operand
);
1181 prev_opcode
= opcode
;
1183 opcode
= get_specific (opcode
, operand
);
1187 /* Couldn't find an opcode which matched the operands */
1188 char *where
= frag_more (2);
1192 clever_message (prev_opcode
, operand
);
1196 if (opcode
->size
&& dot
)
1198 if (opcode
->size
!= *dot
)
1200 as_warn ("mismatch between opcode size and operand size");
1204 build_bytes (opcode
, operand
);
1209 tc_crawl_symbol_chain (headers
)
1210 object_headers
* headers
;
1212 printf ("call to tc_crawl_symbol_chain \n");
1216 md_undefined_symbol (name
)
1223 tc_headers_hook (headers
)
1224 object_headers
* headers
;
1226 printf ("call to tc_headers_hook \n");
1229 /* Various routines to kill one day */
1230 /* Equal to MAX_PRECISION in atof-ieee.c */
1231 #define MAX_LITTLENUMS 6
1233 /* Turn a string in input_line_pointer into a floating point constant of type
1234 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1235 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1238 md_atof (type
, litP
, sizeP
)
1244 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1245 LITTLENUM_TYPE
*wordP
;
1277 return "Bad call to MD_ATOF()";
1279 t
= atof_ieee (input_line_pointer
, type
, words
);
1281 input_line_pointer
= t
;
1283 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1284 for (wordP
= words
; prec
--;)
1286 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1287 litP
+= sizeof (LITTLENUM_TYPE
);
1292 CONST
char *md_shortopts
= "";
1293 struct option md_longopts
[] = {
1294 {NULL
, no_argument
, NULL
, 0}
1296 size_t md_longopts_size
= sizeof(md_longopts
);
1299 md_parse_option (c
, arg
)
1307 md_show_usage (stream
)
1312 int md_short_jump_size
;
1315 tc_aout_fix_to_chars ()
1317 printf ("call to tc_aout_fix_to_chars \n");
1322 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1329 as_fatal ("failed sanity check.");
1333 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1335 addressT from_addr
, to_addr
;
1339 as_fatal ("failed sanity check.");
1343 md_convert_frag (headers
, fragP
)
1344 object_headers
*headers
;
1348 printf ("call to md_convert_frag \n");
1353 md_section_align (seg
, size
)
1357 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1) & (-1 << section_alignment
[(int) seg
]));
1362 md_apply_fix (fixP
, val
)
1366 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1368 switch (fixP
->fx_size
)
1374 *buf
++ = (val
>> 8);
1378 *buf
++ = (val
>> 24);
1379 *buf
++ = (val
>> 16);
1380 *buf
++ = (val
>> 8);
1390 md_operand (expressionP
)
1391 expressionS
* expressionP
;
1395 int md_long_jump_size
;
1398 md_estimate_size_before_relax (fragP
, segment_type
)
1399 register fragS
*fragP
;
1400 register segT segment_type
;
1402 printf ("call tomd_estimate_size_before_relax \n");
1406 /* Put number into target byte order */
1409 md_number_to_chars (ptr
, use
, nbytes
)
1417 *ptr
++ = (use
>> 24) & 0xff;
1419 *ptr
++ = (use
>> 16) & 0xff;
1421 *ptr
++ = (use
>> 8) & 0xff;
1423 *ptr
++ = (use
>> 0) & 0xff;
1430 md_pcrel_from (fixP
)
1438 tc_reloc_mangle (fix_ptr
, intr
, base
)
1440 struct internal_reloc
*intr
;
1444 symbolS
*symbol_ptr
;
1446 symbol_ptr
= fix_ptr
->fx_addsy
;
1448 /* If this relocation is attached to a symbol then it's ok
1450 if (fix_ptr
->fx_r_type
== RELOC_32
)
1452 /* cons likes to create reloc32's whatever the size of the reloc..
1454 switch (fix_ptr
->fx_size
)
1458 intr
->r_type
= R_RELWORD
;
1461 intr
->r_type
= R_RELBYTE
;
1471 intr
->r_type
= fix_ptr
->fx_r_type
;
1474 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1475 intr
->r_offset
= fix_ptr
->fx_offset
;
1478 intr
->r_symndx
= symbol_ptr
->sy_number
;
1480 intr
->r_symndx
= -1;
1485 tc_coff_sizemachdep ()
1490 /* end of tc-h8300.c */