1 /* Disassemble Motorola M*Core instructions.
2 Copyright (C) 1993-2016 Free Software Foundation, Inc.
4 This file is part of the GNU opcodes library.
6 This library 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 3, or (at your option)
11 It is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
26 #include "mcore-opc.h"
29 /* Mask for each mcore_opclass: */
30 static const unsigned short imsk
[] = {
64 /* OPSR */ 0xFFF8, /* psrset/psrclr */
66 /* JC */ 0, /* JC,JU,JL don't appear in object */
71 /* OB2 */ 0 /* OB2 won't appear in object. */
74 static const char *grname
[] = {
75 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
76 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
79 static const char X
[] = "??";
81 static const char *crname
[] = {
82 "psr", "vbr", "epsr", "fpsr", "epc", "fpc", "ss0", "ss1",
83 "ss2", "ss3", "ss4", "gcr", "gsr", X
, X
, X
,
84 X
, X
, X
, X
, X
, X
, X
, X
,
85 X
, X
, X
, X
, X
, X
, X
, X
88 static const unsigned isiz
[] = { 2, 0, 1, 0 };
91 print_insn_mcore (bfd_vma memaddr
,
92 struct disassemble_info
*info
)
94 unsigned char ibytes
[4];
95 fprintf_ftype print_func
= info
->fprintf_func
;
96 void *stream
= info
->stream
;
98 const mcore_opcode_info
*op
;
101 info
->bytes_per_chunk
= 2;
103 status
= info
->read_memory_func (memaddr
, ibytes
, 2, info
);
107 info
->memory_error_func (status
, memaddr
, info
);
111 if (info
->endian
== BFD_ENDIAN_BIG
)
112 inst
= (ibytes
[0] << 8) | ibytes
[1];
113 else if (info
->endian
== BFD_ENDIAN_LITTLE
)
114 inst
= (ibytes
[1] << 8) | ibytes
[0];
118 /* Just a linear search of the table. */
119 for (op
= mcore_table
; op
->name
!= 0; op
++)
120 if (op
->inst
== (inst
& imsk
[op
->opclass
]))
124 (*print_func
) (stream
, ".short 0x%04x", inst
);
127 const char *name
= grname
[inst
& 0x0F];
129 (*print_func
) (stream
, "%s", op
->name
);
137 (*print_func
) (stream
, "\t%d", inst
& 0x3);
143 (*print_func
) (stream
, "\t%s", name
);
147 (*print_func
) (stream
, "\t%s, %s", name
, crname
[(inst
>> 4) & 0x1F]);
151 (*print_func
) (stream
, "\t%s, r1", name
);
156 (*print_func
) (stream
, "\t%s, %s", name
, grname
[(inst
>> 4) & 0xF]);
160 (*print_func
) (stream
, "\tr1, %s", name
);
164 (*print_func
) (stream
, "\t%s, %d", name
, ((inst
>> 4) & 0x1F) + 1);
168 (*print_func
) (stream
, "\t%s-r15, (r0)", name
);
172 (*print_func
) (stream
, "\tr4-r7, (%s)", name
);
184 (*print_func
) (stream
, "\t%s, %d", name
, (inst
>> 4) & 0x1F);
188 (*print_func
) (stream
, "\t%s, %d", name
, (inst
>> 4) & 0x7F);
192 (*print_func
) (stream
, "\t%s, (%s, %d)", grname
[(inst
>> 8) & 0xF],
193 name
, ((inst
>> 4) & 0xF) << isiz
[(inst
>> 13) & 3]);
198 long val
= inst
& 0x3FF;
203 (*print_func
) (stream
, "\t0x%lx", (long)(memaddr
+ 2 + (val
<< 1)));
205 if (strcmp (op
->name
, "bsr") == 0)
207 /* For bsr, we'll try to get a symbol for the target. */
208 val
= memaddr
+ 2 + (val
<< 1);
210 if (info
->print_address_func
&& val
!= 0)
212 (*print_func
) (stream
, "\t// ");
213 info
->print_address_func (val
, info
);
222 val
= (inst
& 0x000F);
223 (*print_func
) (stream
, "\t%s, 0x%lx",
224 grname
[(inst
>> 4) & 0xF],
225 (long) (memaddr
- (val
<< 1)));
233 val
= (memaddr
+ 2 + ((inst
& 0xFF) << 2)) & 0xFFFFFFFC;
235 /* We are not reading an instruction, so allow
236 reads to extend beyond the next symbol. */
238 status
= info
->read_memory_func (val
, ibytes
, 4, info
);
241 info
->memory_error_func (status
, memaddr
, info
);
245 if (info
->endian
== BFD_ENDIAN_LITTLE
)
246 val
= (ibytes
[3] << 24) | (ibytes
[2] << 16)
247 | (ibytes
[1] << 8) | (ibytes
[0]);
249 val
= (ibytes
[0] << 24) | (ibytes
[1] << 16)
250 | (ibytes
[2] << 8) | (ibytes
[3]);
252 /* Removed [] around literal value to match ABI syntax 12/95. */
253 (*print_func
) (stream
, "\t%s, 0x%lX", grname
[(inst
>> 8) & 0xF], val
);
256 (*print_func
) (stream
, "\t// from address pool at 0x%lx",
258 + ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
266 val
= (memaddr
+ 2 + ((inst
& 0xFF) << 2)) & 0xFFFFFFFC;
268 /* We are not reading an instruction, so allow
269 reads to extend beyond the next symbol. */
271 status
= info
->read_memory_func (val
, ibytes
, 4, info
);
274 info
->memory_error_func (status
, memaddr
, info
);
278 if (info
->endian
== BFD_ENDIAN_LITTLE
)
279 val
= (ibytes
[3] << 24) | (ibytes
[2] << 16)
280 | (ibytes
[1] << 8) | (ibytes
[0]);
282 val
= (ibytes
[0] << 24) | (ibytes
[1] << 16)
283 | (ibytes
[2] << 8) | (ibytes
[3]);
285 /* Removed [] around literal value to match ABI syntax 12/95. */
286 (*print_func
) (stream
, "\t0x%lX", val
);
287 /* For jmpi/jsri, we'll try to get a symbol for the target. */
288 if (info
->print_address_func
&& val
!= 0)
290 (*print_func
) (stream
, "\t// ");
291 info
->print_address_func (val
, info
);
295 (*print_func
) (stream
, "\t// from address pool at 0x%lx",
297 + ((inst
& 0xFF) << 2)) & 0xFFFFFFFC);
304 static char *fields
[] = {
305 "af", "ie", "fe", "fe,ie",
306 "ee", "ee,ie", "ee,fe", "ee,fe,ie"
309 (*print_func
) (stream
, "\t%s", fields
[inst
& 0x7]);
314 /* If the disassembler lags the instruction set. */
315 (*print_func
) (stream
, "\tundecoded operands, inst is 0x%04x", inst
);
320 /* Say how many bytes we consumed. */