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
;
273 input_line_pointer
= s
;
274 if (expression (op
) == O_absent
)
275 as_bad ("missing operand");
276 new = input_line_pointer
;
277 input_line_pointer
= save
;
282 skip_colonthing (ptr
, exp
, mode
)
293 /* ff fill any 8 bit quantity */
294 exp
->X_add_number
|= 0xff00;
303 else if (*ptr
== '1')
307 while (isdigit (*ptr
))
314 /* The many forms of operand:
317 @Rn Register indirect
318 @(exp[:16], Rn) Register indirect with displacement
322 @aa:16 absolute 16 bit
325 #xx[:size] immediate data
326 @(exp:[8], pc) pc rel
327 @@aa[:8] memory indirect
338 src
= skip_colonthing (src
, &op
->exp
, &mode
);
342 /* Choose a default mode */
343 if (op
->exp
.X_add_number
< -32768
344 || op
->exp
.X_add_number
> 32767)
351 else if (op
->exp
.X_add_symbol
352 || op
->exp
.X_op_symbol
)
364 get_operand (ptr
, op
, dst
, direction
)
378 len
= parse_reg (src
, &op
->mode
, &op
->reg
, direction
);
391 src
= parse_exp (src
, &op
->exp
);
393 src
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
406 len
= parse_reg (src
, &mode
, &num
, direction
);
409 /* Oops, not a reg after all, must be ordinary exp */
411 /* must be a symbol */
412 op
->mode
= ABS
| PSIZE
| direction
;
413 *ptr
= skip_colonthing (parse_exp (src
, &op
->exp
),
414 &op
->exp
, &op
->mode
);
422 if ((mode
& SIZE
) != PSIZE
)
423 as_bad ("Wrong size pointer register for architecture.");
434 /* Start off assuming a 16 bit offset */
437 src
= parse_exp (src
, &op
->exp
);
439 src
= colonmod24 (op
, src
);
444 op
->mode
= DISP
| direction
;
451 as_bad ("expected @(exp, reg16)");
457 len
= parse_reg (src
, &mode
, &op
->reg
, direction
);
458 if (len
== 0 || !(mode
& REG
))
460 as_bad ("expected @(exp, reg16)");
463 op
->mode
|= DISP
| direction
;
466 src
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
468 if (*src
!= ')' && '(')
470 as_bad ("expected @(exp, reg16)");
477 len
= parse_reg (src
, &mode
, &num
, direction
);
485 if ((mode
& SIZE
) != PSIZE
)
486 as_bad ("Wrong size pointer register for architecture.");
492 if ((mode
& SIZE
) != PSIZE
)
493 as_bad ("Wrong size pointer register for architecture.");
495 op
->mode
= direction
| IND
| PSIZE
;
503 /* must be a symbol */
505 op
->mode
= ABS
| direction
;
506 src
= parse_exp (src
, &op
->exp
);
508 *ptr
= colonmod24 (op
, src
);
519 src
= parse_exp (src
, &op
->exp
);
520 *ptr
= skip_colonthing (src
, &op
->exp
, &op
->mode
);
526 src
= parse_exp (src
, &op
->exp
);
527 /* Trailing ':' size ? */
530 if (src
[1] == '1' && src
[2] == '6')
532 op
->mode
= PCREL
| L_16
;
535 else if (src
[1] == '8')
537 op
->mode
= PCREL
| L_8
;
542 as_bad ("expect :8 or :16 here");
547 op
->mode
= PCREL
| bsize
;
556 DEFUN (get_operands
, (noperands
, op_end
, operand
),
557 unsigned int noperands AND
559 struct h8_op
*operand
)
572 get_operand (&ptr
, operand
+ 0, 0, SRC
);
576 get_operand (&ptr
, operand
+ 1, 1, DST
);
586 get_operand (&ptr
, operand
+ 0, 0, SRC
);
589 get_operand (&ptr
, operand
+ 1, 1, DST
);
600 /* Passed a pointer to a list of opcodes which use different
601 addressing modes, return the opcode which matches the opcodes
606 get_specific (opcode
, operands
)
607 struct h8_opcode
*opcode
;
608 struct h8_op
*operands
;
610 struct h8_opcode
*this_try
= opcode
;
613 unsigned int this_index
= opcode
->idx
;
615 while (this_index
== opcode
->idx
&& !found
)
621 for (i
= 0; i
< this_try
->noperands
&& found
; i
++)
623 op_type op
= this_try
->args
.nib
[i
];
624 int x
= operands
[i
].mode
;
626 if ((op
& (DISP
| REG
)) == (DISP
| REG
)
627 && ((x
& DISP
| REG
) == (DISP
| REG
)))
629 dispreg
= operands
[i
].reg
;
638 x
= (x
& ~L_P
) | (Hmode
? L_32
: L_16
);
642 op
= (op
& ~L_P
) | (Hmode
? L_32
: L_16
);
647 /* The size of the reg is v important */
648 if ((op
& SIZE
) != (x
& SIZE
))
651 else if ((op
& ABSJMP
) && (x
& ABS
))
653 operands
[i
].mode
&= ~ABS
;
654 operands
[i
].mode
|= ABSJMP
;
655 /* But it may not be 24 bits long */
658 operands
[i
].mode
&= ~SIZE
;
659 operands
[i
].mode
|= L_16
;
664 else if ((op
& (KBIT
| DBIT
)) && (x
& IMM
))
666 /* This is ok if the immediate value is sensible */
672 /* The size of the displacement is important */
673 if ((op
& SIZE
) != (x
& SIZE
))
677 else if ((op
& (DISP
| IMM
| ABS
))
678 && (op
& (DISP
| IMM
| ABS
)) == (x
& (DISP
| IMM
| ABS
)))
680 /* Got a diplacement,will fit if no size or same size as try */
682 && ((op
& SIZE
) != (x
& SIZE
)))
685 else if ((op
& ABSMOV
) && (x
& ABS
))
689 else if ((op
& MODE
) != (x
& MODE
))
703 DEFUN (check_operand
, (operand
, width
, string
),
704 struct h8_op
*operand AND
705 unsigned int width AND
708 if (operand
->exp
.X_add_symbol
== 0
709 && operand
->exp
.X_op_symbol
== 0)
712 /* No symbol involved, let's look at offset, it's dangerous if any of
713 the high bits are not 0 or ff's, find out by oring or anding with
714 the width and seeing if the answer is 0 or all fs*/
715 if ((operand
->exp
.X_add_number
& ~width
) != 0 &&
716 (operand
->exp
.X_add_number
| width
) != (~0))
718 as_warn ("operand %s0x%x out of range.", string
, operand
->exp
.X_add_number
);
725 do_a_fix_imm (offset
, operand
, relaxing
)
727 struct h8_op
*operand
;
735 char *t
= operand
->mode
& IMM
? "#" : "@";
737 if (operand
->exp
.X_add_symbol
== 0)
739 char *bytes
= frag_now
->fr_literal
+ offset
;
740 switch (operand
->mode
& SIZE
)
743 check_operand (operand
, 0x7, t
);
744 bytes
[0] |= (operand
->exp
.X_add_number
) << 4;
747 check_operand (operand
, 0xff, t
);
748 bytes
[0] = operand
->exp
.X_add_number
;
751 check_operand (operand
, 0xffff, t
);
752 bytes
[0] = operand
->exp
.X_add_number
>> 8;
753 bytes
[1] = operand
->exp
.X_add_number
>> 0;
756 check_operand (operand
, 0xffffff, t
);
757 bytes
[0] = operand
->exp
.X_add_number
>> 16;
758 bytes
[1] = operand
->exp
.X_add_number
>> 8;
759 bytes
[2] = operand
->exp
.X_add_number
>> 0;
763 /* This should be done with bfd */
764 bytes
[0] = operand
->exp
.X_add_number
>> 24;
765 bytes
[1] = operand
->exp
.X_add_number
>> 16;
766 bytes
[2] = operand
->exp
.X_add_number
>> 8;
767 bytes
[3] = operand
->exp
.X_add_number
>> 0;
774 switch (operand
->mode
& SIZE
)
781 idx
= relaxing
? R_MOVLB1
: R_RELLONG
;
792 idx
= relaxing
? R_MOVB1
: R_RELWORD
;
801 operand
->exp
.X_add_number
= (short) operand
->exp
.X_add_number
;
802 fix_new_exp (frag_now
,
812 /* Now we know what sort of opcodes it is, lets build the bytes -
815 build_bytes (this_try
, operand
)
816 struct h8_opcode
*this_try
;
817 struct h8_op
*operand
;
821 char *output
= frag_more (this_try
->length
);
822 char *output_ptr
= output
;
823 op_type
*nibble_ptr
= this_try
->data
.nib
;
827 unsigned int nibble_count
= 0;
834 if (!(this_try
->inbase
|| Hmode
))
836 as_warn ("Opcode `%s' only available on H8/300-H", this_try
->name
);
839 while (*nibble_ptr
!= E
)
853 if (c
& (REG
| IND
| INC
| DEC
))
855 nib
= operand
[d
].reg
;
857 else if ((c
& DISPREG
) == (DISPREG
))
864 operand
[d
].mode
&= ~ABS
;
865 operand
[d
].mode
|= ABSMOV
;
866 immat
= nibble_count
/ 2;
869 else if (c
& (IMM
| PCREL
| ABS
| ABSJMP
| DISP
))
872 immat
= nibble_count
/ 2;
881 switch (operand
[0].exp
.X_add_number
)
890 as_bad ("Need #1 or #2 here");
895 switch (operand
[0].exp
.X_add_number
)
905 as_warn ("#4 only valid in h8/300 mode.");
910 as_bad ("Need #1 or #2 here");
913 /* stop it making a fix */
927 for (i
= 0; i
< this_try
->length
; i
++)
929 output
[i
] = (asnibbles
[i
* 2] << 4) | asnibbles
[i
* 2 + 1];
932 /* output any fixes */
933 for (i
= 0; i
< 2; i
++)
935 int x
= operand
[i
].mode
;
937 if (x
& (IMM
| ABS
| DISP
))
939 do_a_fix_imm (output
- frag_now
->fr_literal
+ immat
, operand
+ i
,0);
943 int size16
= x
& L_16
;
944 int where
= size16
? 2 : 1;
945 int size
= size16
? 2 : 1;
946 int type
= size16
? R_PCRWORD
: R_PCRBYTE
;
948 check_operand (operand
+ i
, size16
? 0x7fff : 0x7f, "@");
950 if (operand
[i
].exp
.X_add_number
& 1)
952 as_warn ("branch operand has odd offset (%x)\n",
953 operand
->exp
.X_add_number
);
956 operand
[i
].exp
.X_add_number
=
957 (char) (operand
[i
].exp
.X_add_number
- 1);
958 fix_new_exp (frag_now
,
959 output
- frag_now
->fr_literal
+ where
,
968 check_operand (operand
+ i
, 0xff, "@@");
969 fix_new_exp (frag_now
,
970 output
- frag_now
->fr_literal
+ 1,
979 /* This mov is either absolute long or thru a memory loc */
980 do_a_fix_imm (output
- frag_now
->fr_literal
+ immat
, operand
+ i
,1);
985 /* This jmp may be a jump or a branch */
987 check_operand (operand
+ i
, Hmode
? 0xfffff : 0xffff, "@");
988 if (operand
[i
].exp
.X_add_number
& 1)
990 as_warn ("branch operand has odd offset (%x)\n",
991 operand
->exp
.X_add_number
);
993 operand
[i
].exp
.X_add_number
= (short) operand
[i
].exp
.X_add_number
;
994 fix_new_exp (frag_now
,
995 output
- frag_now
->fr_literal
,
1006 try and give an intelligent error message for common and simple to
1011 DEFUN (clever_message
, (opcode
, operand
),
1012 struct h8_opcode
*opcode AND
1013 struct h8_op
*operand
)
1015 struct h8_opcode
*scan
= opcode
;
1017 /* Find out if there was more than one possible opccode */
1019 if ((opcode
+ 1)->idx
!= opcode
->idx
)
1023 /* Only one opcode of this flavour, try and guess which operand
1025 for (argn
= 0; argn
< opcode
->noperands
; argn
++)
1027 switch (opcode
->args
.nib
[argn
])
1030 if (operand
[argn
].mode
!= RD16
)
1032 as_bad ("destination operand must be 16 bit register");
1040 if (operand
[argn
].mode
!= RS8
)
1042 as_bad ("source operand must be 8 bit register");
1048 if (operand
[argn
].mode
!= ABS16DST
)
1050 as_bad ("destination operand must be 16bit absolute address");
1055 if (operand
[argn
].mode
!= RD8
)
1057 as_bad ("destination operand must be 8 bit register");
1064 if (operand
[argn
].mode
!= ABS16SRC
)
1066 as_bad ("source operand must be 16bit absolute address");
1074 as_bad ("invalid operands");
1077 /* This is the guts of the machine-dependent assembler. STR points to a
1078 machine dependent instruction. This funciton is supposed to emit
1079 the frags/bytes it assembles to.
1085 DEFUN (md_assemble
, (str
),
1091 struct h8_op operand
[2];
1092 struct h8_opcode
*opcode
;
1093 struct h8_opcode
*prev_opcode
;
1098 /* Drop leading whitespace */
1102 /* find the op code end */
1103 for (op_start
= op_end
= str
;
1104 *op_end
!= 0 && *op_end
!= ' ';
1118 if (op_end
== op_start
)
1120 as_bad ("can't find opcode ");
1126 opcode
= (struct h8_opcode
*) hash_find (opcode_hash_control
,
1131 as_bad ("unknown opcode");
1136 input_line_pointer
= get_operands (opcode
->noperands
, op_end
,
1139 prev_opcode
= opcode
;
1141 opcode
= get_specific (opcode
, operand
);
1145 /* Couldn't find an opcode which matched the operands */
1146 char *where
= frag_more (2);
1150 clever_message (prev_opcode
, operand
);
1154 if (opcode
->size
&& dot
)
1156 if (opcode
->size
!= *dot
)
1158 as_warn ("mismatch between opcode size and operand size");
1162 build_bytes (opcode
, operand
);
1167 DEFUN (tc_crawl_symbol_chain
, (headers
),
1168 object_headers
* headers
)
1170 printf ("call to tc_crawl_symbol_chain \n");
1174 DEFUN (md_undefined_symbol
, (name
),
1181 DEFUN (tc_headers_hook
, (headers
),
1182 object_headers
* headers
)
1184 printf ("call to tc_headers_hook \n");
1192 /* Various routines to kill one day */
1193 /* Equal to MAX_PRECISION in atof-ieee.c */
1194 #define MAX_LITTLENUMS 6
1196 /* Turn a string in input_line_pointer into a floating point constant of type
1197 type, and store the appropriate bytes in *litP. The number of LITTLENUMS
1198 emitted is stored in *sizeP . An error message is returned, or NULL on OK.
1201 md_atof (type
, litP
, sizeP
)
1207 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1208 LITTLENUM_TYPE
*wordP
;
1240 return "Bad call to MD_ATOF()";
1242 t
= atof_ieee (input_line_pointer
, type
, words
);
1244 input_line_pointer
= t
;
1246 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
1247 for (wordP
= words
; prec
--;)
1249 md_number_to_chars (litP
, (long) (*wordP
++), sizeof (LITTLENUM_TYPE
));
1250 litP
+= sizeof (LITTLENUM_TYPE
);
1256 md_parse_option (argP
, cntP
, vecP
)
1266 int md_short_jump_size
;
1269 tc_aout_fix_to_chars ()
1271 printf ("call to tc_aout_fix_to_chars \n");
1276 md_create_short_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1283 as_fatal ("failed sanity check.");
1287 md_create_long_jump (ptr
, from_addr
, to_addr
, frag
, to_symbol
)
1289 addressT from_addr
, to_addr
;
1293 as_fatal ("failed sanity check.");
1297 md_convert_frag (headers
, fragP
)
1298 object_headers
*headers
;
1302 printf ("call to md_convert_frag \n");
1306 valueT
md_section_align (seg
, size
)
1310 return ((size
+ (1 << section_alignment
[(int) seg
]) - 1) & (-1 << section_alignment
[(int) seg
]));
1315 md_apply_fix (fixP
, val
)
1319 char *buf
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
1321 switch (fixP
->fx_size
)
1327 *buf
++ = (val
>> 8);
1331 *buf
++ = (val
>> 24);
1332 *buf
++ = (val
>> 16);
1333 *buf
++ = (val
>> 8);
1343 DEFUN (md_operand
, (expressionP
), expressionS
* expressionP
)
1347 int md_long_jump_size
;
1349 md_estimate_size_before_relax (fragP
, segment_type
)
1350 register fragS
*fragP
;
1351 register segT segment_type
;
1353 printf ("call tomd_estimate_size_before_relax \n");
1357 /* Put number into target byte order */
1360 DEFUN (md_number_to_chars
, (ptr
, use
, nbytes
),
1368 *ptr
++ = (use
>> 24) & 0xff;
1370 *ptr
++ = (use
>> 16) & 0xff;
1372 *ptr
++ = (use
>> 8) & 0xff;
1374 *ptr
++ = (use
>> 0) & 0xff;
1381 md_pcrel_from (fixP
)
1388 tc_coff_symbol_emit_hook ()
1394 tc_reloc_mangle (fix_ptr
, intr
, base
)
1396 struct internal_reloc
*intr
;
1400 symbolS
*symbol_ptr
;
1402 symbol_ptr
= fix_ptr
->fx_addsy
;
1404 /* If this relocation is attached to a symbol then it's ok
1406 if (fix_ptr
->fx_r_type
== RELOC_32
)
1408 /* cons likes to create reloc32's whatever the size of the reloc..
1410 switch (fix_ptr
->fx_size
)
1414 intr
->r_type
= R_RELWORD
;
1417 intr
->r_type
= R_RELBYTE
;
1427 intr
->r_type
= fix_ptr
->fx_r_type
;
1430 intr
->r_vaddr
= fix_ptr
->fx_frag
->fr_address
+ fix_ptr
->fx_where
+ base
;
1431 intr
->r_offset
= fix_ptr
->fx_offset
;
1434 intr
->r_symndx
= symbol_ptr
->sy_number
;
1436 intr
->r_symndx
= -1;
1441 tc_coff_sizemachdep ()
1446 /* end of tc-h8300.c */