* hosts/h-*.h: Configure fopen using ../include/fopen-*.h
[deliverable/binutils-gdb.git] / bfd / cpu-h8300.c
CommitLineData
e89f2fbe
SC
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.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
e89f2fbe 21#include "bfd.h"
3b4f1a5d 22#include "sysdep.h"
e89f2fbe
SC
23#include "libbfd.h"
24
25#define DEFINE_TABLE
c3eb25fc 26#include "opcode/h8300.h"
e89f2fbe
SC
27
28#define MAXSAME 14
29
30static struct h8_opcode * h8_opcodes_sorted[256][MAXSAME];
31
32/* Run through the opcodes and sort them into order to make them easy
33 to disassemble
34 */
35static void
36DEFUN_VOID(bfd_h8_disassemble_init)
37{
38 unsigned int i;
39 struct h8_opcode *p;
40
41 for (p = h8_opcodes; p->name; p++) {
42 int where = 0;
43 int n1 = 0;
44 int n2 = 0;
45 int n3 = 0;
46 int n4= 0;
47 if ((int)p->data.nib[0] < 16) {
48 n1 =(int) p->data.nib[0] ;
49 } else n1 = 0;
50 if ((int)p->data.nib[1] < 16) {
51 n2 = (int) p->data.nib[1];
52 }else n2 = 0;
53
54 for (i = 0; i < MAXSAME; i++) {
55 int j = n1 * 16 + n2;
56 if (h8_opcodes_sorted[j][i] == (struct h8_opcode *)NULL) {
57 h8_opcodes_sorted[j][i] = p;
58 break;
59 }
60 }
61
62 if (i==MAXSAME)abort();
63
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++) ;
67 if (i & 1) abort();
68 if (i/2 != p->length) abort();
69 }
70 for (i = 0; i < 256; i++)
71 {
72 if (h8_opcodes_sorted[i][0])
73 p = h8_opcodes_sorted[i][0];
74 else h8_opcodes_sorted[i][0] = p;
75 }
76}
77
b2c91bd9 78
e89f2fbe
SC
79unsigned int
80DEFUN(bfd_h8_disassemble,(addr, data, stream),
81bfd_vma addr AND
82CONST bfd_byte *data AND
83FILE *stream)
84{
85 /* Find the first entry in the table for this opcode */
b2c91bd9
SC
86 CONST static char *regnames[] = {
87 "r0h","r1h","r2h","r3h","r4h","r5h","r6h","r7h",
88 "r0l","r1l","r2l","r3l","r4l","r5l","r6l","r7l" };
89
e89f2fbe
SC
90 int rs = 0;
91 int rd = 0;
92 int rdisp = 0;
93 int abs = 0;
94 struct h8_opcode **p = h8_opcodes_sorted[(unsigned)(data[0])];
95 struct h8_opcode *q;
96
97 /* Find the exact opcode/arg combo */
98 while (*p) {
3b4f1a5d 99 op_type *nib;
e89f2fbe
SC
100 unsigned int len = 0;
101 q = *p++;
102 nib =q->data.nib;
103 while (*nib != E) {
3b4f1a5d 104 op_type looking_for = *nib;
e89f2fbe
SC
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;
3b4f1a5d 109 looking_for = (op_type)((int)looking_for & ~(int)B31);
e89f2fbe
SC
110 }
111 if ((int)looking_for & (int)B30) {
112 if (((int)thisnib & 0x8) !=0) goto fail;
3b4f1a5d 113 looking_for = (op_type)((int)looking_for & ~(int)B30);
e89f2fbe
SC
114 }
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;
119 break;
120 case ABS16SRC:
121 case ABS16DST:
122 case DISPSRC:
123 case DISPDST:
124 case IMM16:
125 abs = (data[len>>1]) * 256 + data[(len+2)>>1];
126 len+=3;
127 nib+=3;
128 break;
129 case DISPREG:
130 rdisp = thisnib;
131 break;
132 case KBIT:
3b4f1a5d 133 abs = thisnib == 0x8 ? 2:1;
e89f2fbe
SC
134 break;
135 case IMM8:
136 case ABS8SRC:
137 case ABS8DST:
0d4b8e3f 138 case MEMIND:
e89f2fbe
SC
139 case DISP8:
140 abs= data[len>>1];
141 len++;
142 nib++;
143 break;
144 case IMM3:
145 abs = thisnib ;
146 break;
147 case RS8:
148 case RS16:
149 case RSINC:
150 case RSIND:
151 rs = thisnib;
152 break;
153 case RD16:
154 case RDDEC:
155 case RD8:
156 case RDIND:
157 rd = thisnib;
158 break;
159 default:
160 fprintf(stream, "Dont understand \n");
161 goto found;
162 }
163 len++;
164 nib++;
165 }
166 goto found;
167 fail:
168 ;
169
170 }
3b4f1a5d 171 fprintf(stream, "%02x %02x .word\tH'%x,H'%x",
b2c91bd9
SC
172 data[0], data[1],
173 data[0], data[1]);
e89f2fbe
SC
174 return 2;
175 found:;
176 { int i;
177
178 for (i = 0; i < q->length; i++) {
179 fprintf(stream, "%02x ", data[i]);
180 }
181 for (; i < 6; i++) {
182 fprintf(stream, " ");
183 }
184 }
185 fprintf(stream, "%s\t",q->name);
186 /* Now print out the args */
187 {
3b4f1a5d 188 op_type *args = q->args.nib;
e89f2fbe
SC
189 int hadone = 0;
190 while (*args != E) {
191 if (hadone)
192 fprintf(stream, ",");
193 switch ((int)(*args) & ~((int)B30|(int)B31)) {
194 case IMM16:
195 case IMM8:
196 case IMM3:
0d4b8e3f
SC
197 fprintf(stream, "#0x%x", (unsigned)abs);
198 break;
e89f2fbe 199 case RD8:
0d4b8e3f
SC
200 fprintf(stream, "%s", regnames[rd]);
201 break;
e89f2fbe 202 case RS8:
0d4b8e3f
SC
203 fprintf(stream, "%s",regnames[rs]);
204 break;
e89f2fbe 205 case RD16:
0d4b8e3f
SC
206 fprintf(stream, "r%d", rd& 0x7);
207 break;
e89f2fbe 208 case RS16:
0d4b8e3f
SC
209 fprintf(stream, "r%d", rs & 0x7);
210 break;
e89f2fbe 211 case RSINC:
0d4b8e3f
SC
212 fprintf(stream, "@r%d+", rs & 0x7);
213 break;
e89f2fbe 214 case RDDEC:
0d4b8e3f
SC
215 fprintf(stream, "@-r%d", rd & 0x7);
216 break;
e89f2fbe 217 case RDIND:
0d4b8e3f
SC
218 fprintf(stream, "@r%d", rd & 0x7);
219 break;
e89f2fbe 220 case RSIND:
0d4b8e3f
SC
221 fprintf(stream, "@r%d",rs & 0x7);
222 break;
e89f2fbe
SC
223 case ABS8SRC:
224 case ABS16SRC:
225 case ABS16DST:
226 case ABS8DST:
0d4b8e3f
SC
227 fprintf(stream, "@0x%x", (unsigned)abs);
228 break;
229 case MEMIND:
230 fprintf(stream, "@@%d (%x)",abs, abs);
231 break;
e89f2fbe 232 case DISP8:
b2c91bd9
SC
233 fprintf(stream, ".%s%d (%x)",(char)abs>0 ? "+" :"", (char)abs,
234 addr + (char)abs);
235 break;
e89f2fbe
SC
236 case DISPSRC:
237 case DISPDST:
0d4b8e3f
SC
238 fprintf(stream, "@(0x%x,r%d)", abs, rdisp & 0x7);
239 break;
e89f2fbe 240 case CCR:
0d4b8e3f
SC
241 fprintf(stream, "ccr");
242 break;
e89f2fbe 243 case KBIT:
0d4b8e3f
SC
244 fprintf(stream, "#%d",abs);
245 break;
e89f2fbe
SC
246 default:
247 abort();
248 }
249 hadone = 1;
250 args++;
251 }
252 }
253 return q->length;
254}
255
256
257
3b4f1a5d 258unsigned int DEFUN(print_insn_h8300,(addr, data, file),
e89f2fbe 259bfd_vma addr AND
3b4f1a5d 260CONST char *data AND
e89f2fbe
SC
261PTR file)
262{
263 static boolean init;
264 if (!init) {
265 bfd_h8_disassemble_init();
266 init= 1;
e89f2fbe 267 }
3b4f1a5d
SC
268
269 return bfd_h8_disassemble(addr, (bfd_byte *)data, file);
e89f2fbe
SC
270}
271
272/*
273Relocations for the H8
274
275*/
3b4f1a5d 276static bfd_reloc_status_type
e89f2fbe
SC
277DEFUN(howto16_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
278bfd *abfd AND
279arelent *reloc_entry AND
280asymbol *symbol_in AND
281unsigned char *data AND
282asection *ignore_input_section)
283{
284 long relocation = 0;
285 bfd_vma addr = reloc_entry->address;
286 long x = bfd_get_16(abfd, (bfd_byte *)data + addr);
287
288 HOWTO_PREPARE(relocation, symbol_in);
289
290 x = (x + relocation + reloc_entry->addend);
291
292 bfd_put_16(abfd, x, (bfd_byte *)data + addr);
293 return bfd_reloc_ok;
294}
295
296
3b4f1a5d 297static bfd_reloc_status_type
e89f2fbe
SC
298DEFUN(howto8_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
299bfd *abfd AND
300arelent *reloc_entry AND
301asymbol *symbol_in AND
302unsigned char *data AND
303asection *ignore_input_section)
304{
305 long relocation = 0;
306 bfd_vma addr = reloc_entry->address;
307 long x = bfd_get_8(abfd, (bfd_byte *)data + addr);
308
309 HOWTO_PREPARE(relocation, symbol_in);
310
311 x = (x + relocation + reloc_entry->addend);
312
313 bfd_put_8(abfd, x, (bfd_byte *)data + addr);
314 return bfd_reloc_ok;
315}
316
317
3b4f1a5d 318static bfd_reloc_status_type
e89f2fbe
SC
319DEFUN(howto8_FFnn_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
320bfd *abfd AND
321arelent *reloc_entry AND
322asymbol *symbol_in AND
323unsigned char *data AND
324asection *ignore_input_section)
325{
326 long relocation = 0;
327 bfd_vma addr = reloc_entry->address;
328
329 long x = bfd_get_8(abfd, (bfd_byte *)data + addr);
330 abort();
331 HOWTO_PREPARE(relocation, symbol_in);
332
333 x = (x + relocation + reloc_entry->addend);
334
335 bfd_put_8(abfd, x, (bfd_byte *)data + addr);
336 return bfd_reloc_ok;
337}
338
3b4f1a5d 339static bfd_reloc_status_type
e89f2fbe
SC
340DEFUN(howto8_pcrel_callback,(abfd, reloc_entry, symbol_in, data, ignore_input_section),
341bfd *abfd AND
342arelent *reloc_entry AND
343asymbol *symbol_in AND
344unsigned char *data AND
345asection *ignore_input_section)
346{
347 long relocation = 0;
348 bfd_vma addr = reloc_entry->address;
349 long x = bfd_get_8(abfd, (bfd_byte *)data + addr);
350 abort();
351 HOWTO_PREPARE(relocation, symbol_in);
352
353 x = (x + relocation + reloc_entry->addend);
354
355 bfd_put_8(abfd, x, (bfd_byte *)data + addr);
356 return bfd_reloc_ok;
357}
358
359
360static reloc_howto_type howto_16
b2c91bd9 361 = NEWHOWTO(howto16_callback,"abs16",1,0,0);
e89f2fbe 362static reloc_howto_type howto_8
b2c91bd9 363 = NEWHOWTO(howto8_callback,"abs8",0,0,0);
e89f2fbe
SC
364
365static reloc_howto_type howto_8_FFnn
b2c91bd9 366 = NEWHOWTO(howto8_FFnn_callback,"ff00+abs8",0,0,0);
e89f2fbe
SC
367
368static reloc_howto_type howto_8_pcrel
b2c91bd9 369 = NEWHOWTO(howto8_pcrel_callback,"pcrel8",0,1,1);
e89f2fbe
SC
370
371
372
b2c91bd9
SC
373static CONST struct reloc_howto_struct *
374DEFUN(local_bfd_reloc_type_lookup,(arch, code),
3b4f1a5d
SC
375 CONST struct bfd_arch_info *arch AND
376 bfd_reloc_code_type code)
e89f2fbe
SC
377{
378 switch (code) {
379 case BFD_RELOC_16:
380 return &howto_16;
381 case BFD_RELOC_8_FFnn:
382 return &howto_8_FFnn;
383 case BFD_RELOC_8:
384 return &howto_8;
385 case BFD_RELOC_8_PCREL:
386 return &howto_8_pcrel;
387 }
388 return (reloc_howto_type *)NULL;
389}
390
391int bfd_default_scan_num_mach();
392
b2c91bd9
SC
393static boolean
394DEFUN(h8300_scan,(info, string),
3b4f1a5d 395CONST struct bfd_arch_info *info AND
b2c91bd9
SC
396CONST char *string)
397{
398 if (strcmp(string,"h8300") == 0) return true;
399 if (strcmp(string,"H8300") == 0) return true;
400 if (strcmp(string,"h8/300") == 0) return true;
401 if (strcmp(string,"H8/300") == 0) return true;
402 return false;
403}
3b4f1a5d
SC
404
405static bfd_arch_info_type arch_info_struct =
e89f2fbe
SC
406 {
407 16, /* 16 bits in a word */
408 16, /* 16 bits in an address */
409 8, /* 8 bits in a byte */
410 bfd_arch_h8300,
411 0, /* only 1 machine */
b2c91bd9 412 "H8/300", /* arch_name */
e89f2fbe
SC
413 "H8/300", /* printable name */
414 true, /* the default machine */
415 bfd_default_compatible,
b2c91bd9 416 h8300_scan,
e89f2fbe
SC
417 print_insn_h8300,
418 local_bfd_reloc_type_lookup,
419 0,
420 };
421
3b4f1a5d
SC
422void
423DEFUN_VOID(bfd_h8300_arch)
e89f2fbe
SC
424{
425 bfd_arch_linkin(&arch_info_struct);
426}
This page took 0.046745 seconds and 4 git commands to generate.