1 /* BFD library support routines for the Hitachi H8/300 architecture.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Hacked by Steve Chamberlain of Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "h8300-opcode.h"
30 static struct h8_opcode
* h8_opcodes_sorted
[256][MAXSAME
];
32 /* Run through the opcodes and sort them into order to make them easy
36 DEFUN_VOID(bfd_h8_disassemble_init
)
41 for (p
= h8_opcodes
; p
->name
; p
++) {
47 if ((int)p
->data
.nib
[0] < 16) {
48 n1
=(int) p
->data
.nib
[0] ;
50 if ((int)p
->data
.nib
[1] < 16) {
51 n2
= (int) p
->data
.nib
[1];
54 for (i
= 0; i
< MAXSAME
; i
++) {
56 if (h8_opcodes_sorted
[j
][i
] == (struct h8_opcode
*)NULL
) {
57 h8_opcodes_sorted
[j
][i
] = p
;
62 if (i
==MAXSAME
)abort();
64 /* Just make sure there are an even number of nibbles in it, and
65 that the count is the same s the length */
66 for (i
= 0; p
->data
.nib
[i
] != E
; i
++) ;
68 if (i
/2 != p
->length
) abort();
70 for (i
= 0; i
< 256; i
++)
72 if (h8_opcodes_sorted
[i
][0])
73 p
= h8_opcodes_sorted
[i
][0];
74 else h8_opcodes_sorted
[i
][0] = p
;
80 DEFUN(bfd_h8_disassemble
,(addr
, data
, stream
),
82 CONST bfd_byte
*data AND
85 /* Find the first entry in the table for this opcode */
86 CONST
static char *regnames
[] = {
87 "r0h","r1h","r2h","r3h","r4h","r5h","r6h","r7h",
88 "r0l","r1l","r2l","r3l","r4l","r5l","r6l","r7l" };
94 struct h8_opcode
**p
= h8_opcodes_sorted
[(unsigned)(data
[0])];
97 /* Find the exact opcode/arg combo */
100 unsigned int len
= 0;
104 op_enum_type looking_for
= *nib
;
105 int thisnib
= data
[len
>>1] ;
106 thisnib
= (len
& 1) ? (thisnib
& 0xf) : ((thisnib
>> 4) & 0xf);
107 if ((int)looking_for
& (int)B31
) {
108 if (((int)thisnib
& 0x8) ==0) goto fail
;
109 looking_for
= (op_enum_type
)((int)looking_for
& ~(int)B31
);
111 if ((int)looking_for
& (int)B30
) {
112 if (((int)thisnib
& 0x8) !=0) goto fail
;
113 looking_for
= (op_enum_type
)((int)looking_for
& ~(int)B30
);
115 switch (looking_for
) {
116 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
117 case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:
118 if ((int)looking_for
!= thisnib
) goto fail
;
125 abs
= (data
[len
>>1]) * 256 + data
[(len
+2)>>1];
133 abs
= thisnib
== 0x80 ? 2:1;
159 fprintf(stream
, "Dont understand \n");
170 fprintf(stream
, "%02x %02x .word\tH'%x,H'%x\n",
177 for (i
= 0; i
< q
->length
; i
++) {
178 fprintf(stream
, "%02x ", data
[i
]);
181 fprintf(stream
, " ");
184 fprintf(stream
, "%s\t",q
->name
);
185 /* Now print out the args */
187 op_enum_type
*args
= q
->args
.nib
;
191 fprintf(stream
, ",");
192 switch ((int)(*args
) & ~((int)B30
|(int)B31
)) {
196 fprintf(stream
, "#H'%x", (unsigned)abs
); break;
198 fprintf(stream
, "%s", regnames
[rd
]); break;
200 fprintf(stream
, "%s",regnames
[rs
]); break;
202 fprintf(stream
, "r%d", rd
& 0x7); break;
204 fprintf(stream
, "r%d", rs
& 0x7); break;
206 fprintf(stream
, "@r%d+", rs
& 0x7); break;
208 fprintf(stream
, "@-r%d", rd
& 0x7); break;
210 fprintf(stream
, "@r%d", rd
& 0x7); break;
212 fprintf(stream
, "@r%d",rs
& 0x7); break;
217 fprintf(stream
, "@H'%x", (unsigned)abs
); break;
219 fprintf(stream
, ".%s%d (%x)",(char)abs
>0 ? "+" :"", (char)abs
,
224 fprintf(stream
, "@(%d,r%d)", abs
, rdisp
& 0x7); break;
226 fprintf(stream
, "ccr"); break;
228 fprintf(stream
, "#%d",abs
); break;
241 unsigned int DEFUN( print_insn_h8300
,(addr
, data
, file
),
243 CONST bfd_byte
*data AND
248 bfd_h8_disassemble_init();
252 return bfd_h8_disassemble(addr
, data
, file
);
256 Relocations for the H8
259 static bfd_reloc_status_enum_type
260 DEFUN(howto16_callback
,(abfd
, reloc_entry
, symbol_in
, data
, ignore_input_section
),
262 arelent
*reloc_entry AND
263 asymbol
*symbol_in AND
264 unsigned char *data AND
265 asection
*ignore_input_section
)
268 bfd_vma addr
= reloc_entry
->address
;
269 long x
= bfd_get_16(abfd
, (bfd_byte
*)data
+ addr
);
271 HOWTO_PREPARE(relocation
, symbol_in
);
273 x
= (x
+ relocation
+ reloc_entry
->addend
);
275 bfd_put_16(abfd
, x
, (bfd_byte
*)data
+ addr
);
280 static bfd_reloc_status_enum_type
281 DEFUN(howto8_callback
,(abfd
, reloc_entry
, symbol_in
, data
, ignore_input_section
),
283 arelent
*reloc_entry AND
284 asymbol
*symbol_in AND
285 unsigned char *data AND
286 asection
*ignore_input_section
)
289 bfd_vma addr
= reloc_entry
->address
;
290 long x
= bfd_get_8(abfd
, (bfd_byte
*)data
+ addr
);
292 HOWTO_PREPARE(relocation
, symbol_in
);
294 x
= (x
+ relocation
+ reloc_entry
->addend
);
296 bfd_put_8(abfd
, x
, (bfd_byte
*)data
+ addr
);
301 static bfd_reloc_status_enum_type
302 DEFUN(howto8_FFnn_callback
,(abfd
, reloc_entry
, symbol_in
, data
, ignore_input_section
),
304 arelent
*reloc_entry AND
305 asymbol
*symbol_in AND
306 unsigned char *data AND
307 asection
*ignore_input_section
)
310 bfd_vma addr
= reloc_entry
->address
;
312 long x
= bfd_get_8(abfd
, (bfd_byte
*)data
+ addr
);
314 HOWTO_PREPARE(relocation
, symbol_in
);
316 x
= (x
+ relocation
+ reloc_entry
->addend
);
318 bfd_put_8(abfd
, x
, (bfd_byte
*)data
+ addr
);
322 static bfd_reloc_status_enum_type
323 DEFUN(howto8_pcrel_callback
,(abfd
, reloc_entry
, symbol_in
, data
, ignore_input_section
),
325 arelent
*reloc_entry AND
326 asymbol
*symbol_in AND
327 unsigned char *data AND
328 asection
*ignore_input_section
)
331 bfd_vma addr
= reloc_entry
->address
;
332 long x
= bfd_get_8(abfd
, (bfd_byte
*)data
+ addr
);
334 HOWTO_PREPARE(relocation
, symbol_in
);
336 x
= (x
+ relocation
+ reloc_entry
->addend
);
338 bfd_put_8(abfd
, x
, (bfd_byte
*)data
+ addr
);
343 static reloc_howto_type howto_16
344 = NEWHOWTO(howto16_callback
,"abs16",1,0,0);
345 static reloc_howto_type howto_8
346 = NEWHOWTO(howto8_callback
,"abs8",0,0,0);
348 static reloc_howto_type howto_8_FFnn
349 = NEWHOWTO(howto8_FFnn_callback
,"ff00+abs8",0,0,0);
351 static reloc_howto_type howto_8_pcrel
352 = NEWHOWTO(howto8_pcrel_callback
,"pcrel8",0,1,1);
356 static CONST
struct reloc_howto_struct
*
357 DEFUN(local_bfd_reloc_type_lookup
,(arch
, code
),
358 bfd_arch_info_struct_type
*arch AND
359 bfd_reloc_code_enum_type code
)
364 case BFD_RELOC_8_FFnn
:
365 return &howto_8_FFnn
;
368 case BFD_RELOC_8_PCREL
:
369 return &howto_8_pcrel
;
371 return (reloc_howto_type
*)NULL
;
374 int bfd_default_scan_num_mach();
377 DEFUN(h8300_scan
,(info
, string
),
378 CONST
struct bfd_arch_info_struct
*info AND
381 if (strcmp(string
,"h8300") == 0) return true;
382 if (strcmp(string
,"H8300") == 0) return true;
383 if (strcmp(string
,"h8/300") == 0) return true;
384 if (strcmp(string
,"H8/300") == 0) return true;
387 static bfd_arch_info_struct_type arch_info_struct
=
389 16, /* 16 bits in a word */
390 16, /* 16 bits in an address */
391 8, /* 8 bits in a byte */
393 0, /* only 1 machine */
394 "H8/300", /* arch_name */
395 "H8/300", /* printable name */
396 true, /* the default machine */
397 bfd_default_compatible
,
400 local_bfd_reloc_type_lookup
,
407 void DEFUN_VOID(bfd_h8300_arch
)
409 bfd_arch_linkin(&arch_info_struct
);