1 /* Disassemble Motorola M*Core instructions.
2 Copyright (C) 1993-2020 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. */
23 #include "libiberty.h"
27 #include "mcore-opc.h"
28 #include "disassemble.h"
30 /* Mask for each mcore_opclass: */
31 static const unsigned short imsk
[] = {
65 /* OPSR */ 0xFFF8, /* psrset/psrclr */
67 /* JC */ 0, /* JC,JU,JL don't appear in object */
72 /* OB2 */ 0 /* OB2 won't appear in object. */
75 static const char *grname
[] = {
76 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
77 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
80 static const char X
[] = "??";
82 static const char *crname
[] = {
83 "psr", "vbr", "epsr", "fpsr", "epc", "fpc", "ss0", "ss1",
84 "ss2", "ss3", "ss4", "gcr", "gsr", X
, X
, X
,
85 X
, X
, X
, X
, X
, X
, X
, X
,
86 X
, X
, X
, X
, X
, X
, X
, X
89 static const unsigned isiz
[] = { 2, 0, 1, 0 };
92 print_insn_mcore (bfd_vma memaddr
,
93 struct disassemble_info
*info
)
95 unsigned char ibytes
[4];
96 fprintf_ftype print_func
= info
->fprintf_func
;
97 void *stream
= info
->stream
;
102 info
->bytes_per_chunk
= 2;
104 status
= info
->read_memory_func (memaddr
, ibytes
, 2, info
);
108 info
->memory_error_func (status
, memaddr
, info
);
112 if (info
->endian
== BFD_ENDIAN_BIG
)
113 inst
= (ibytes
[0] << 8) | ibytes
[1];
114 else if (info
->endian
== BFD_ENDIAN_LITTLE
)
115 inst
= (ibytes
[1] << 8) | ibytes
[0];
119 /* Just a linear search of the table. */
120 for (i
= 0; i
< ARRAY_SIZE (mcore_table
); i
++)
121 if (mcore_table
[i
].inst
== (inst
& imsk
[mcore_table
[i
].opclass
]))
124 if (i
== ARRAY_SIZE (mcore_table
))
125 (*print_func
) (stream
, ".short 0x%04x", inst
);
128 const char *name
= grname
[inst
& 0x0F];
130 (*print_func
) (stream
, "%s", mcore_table
[i
].name
);
132 switch (mcore_table
[i
].opclass
)
138 (*print_func
) (stream
, "\t%d", inst
& 0x3);
144 (*print_func
) (stream
, "\t%s", name
);
148 (*print_func
) (stream
, "\t%s, %s", name
, crname
[(inst
>> 4) & 0x1F]);
152 (*print_func
) (stream
, "\t%s, r1", name
);
157 (*print_func
) (stream
, "\t%s, %s", name
, grname
[(inst
>> 4) & 0xF]);
161 (*print_func
) (stream
, "\tr1, %s", name
);
165 (*print_func
) (stream
, "\t%s, %d", name
, ((inst
>> 4) & 0x1F) + 1);
169 (*print_func
) (stream
, "\t%s-r15, (r0)", name
);
173 (*print_func
) (stream
, "\tr4-r7, (%s)", name
);
185 (*print_func
) (stream
, "\t%s, %d", name
, (inst
>> 4) & 0x1F);
189 (*print_func
) (stream
, "\t%s, %d", name
, (inst
>> 4) & 0x7F);
193 (*print_func
) (stream
, "\t%s, (%s, %d)", grname
[(inst
>> 8) & 0xF],
194 name
, ((inst
>> 4) & 0xF) << isiz
[(inst
>> 13) & 3]);
199 uint32_t val
= ((inst
& 0x3FF) ^ 0x400) - 0x400;
201 val
= memaddr
+ 2 + (val
<< 1);
202 (*print_func
) (stream
, "\t0x%x", val
);
204 if (strcmp (mcore_table
[i
].name
, "bsr") == 0)
206 /* For bsr, we'll try to get a symbol for the target. */
207 if (info
->print_address_func
&& val
!= 0)
209 (*print_func
) (stream
, "\t// ");
210 info
->print_address_func (val
, info
);
218 uint32_t val
= inst
& 0x000F;
219 (*print_func
) (stream
, "\t%s, 0x%x",
220 grname
[(inst
>> 4) & 0xF],
221 (uint32_t) (memaddr
- (val
<< 1)));
229 val
= (memaddr
+ 2 + ((inst
& 0xFF) << 2)) & ~3;
231 /* We are not reading an instruction, so allow
232 reads to extend beyond the next symbol. */
234 status
= info
->read_memory_func (val
, ibytes
, 4, info
);
237 info
->memory_error_func (status
, memaddr
, info
);
241 if (info
->endian
== BFD_ENDIAN_LITTLE
)
242 val
= (((unsigned) ibytes
[3] << 24) | (ibytes
[2] << 16)
243 | (ibytes
[1] << 8) | (ibytes
[0]));
245 val
= (((unsigned) ibytes
[0] << 24) | (ibytes
[1] << 16)
246 | (ibytes
[2] << 8) | (ibytes
[3]));
248 /* Removed [] around literal value to match ABI syntax 12/95. */
249 (*print_func
) (stream
, "\t%s, 0x%X", grname
[(inst
>> 8) & 0xF], val
);
252 (*print_func
) (stream
, "\t// from address pool at 0x%x",
253 (uint32_t) (memaddr
+ 2
254 + ((inst
& 0xFF) << 2)) & ~3);
262 val
= (memaddr
+ 2 + ((inst
& 0xFF) << 2)) & ~3;
264 /* We are not reading an instruction, so allow
265 reads to extend beyond the next symbol. */
267 status
= info
->read_memory_func (val
, ibytes
, 4, info
);
270 info
->memory_error_func (status
, memaddr
, info
);
274 if (info
->endian
== BFD_ENDIAN_LITTLE
)
275 val
= (((unsigned) ibytes
[3] << 24) | (ibytes
[2] << 16)
276 | (ibytes
[1] << 8) | (ibytes
[0]));
278 val
= (((unsigned) ibytes
[0] << 24) | (ibytes
[1] << 16)
279 | (ibytes
[2] << 8) | (ibytes
[3]));
281 /* Removed [] around literal value to match ABI syntax 12/95. */
282 (*print_func
) (stream
, "\t0x%X", val
);
283 /* For jmpi/jsri, we'll try to get a symbol for the target. */
284 if (info
->print_address_func
&& val
!= 0)
286 (*print_func
) (stream
, "\t// ");
287 info
->print_address_func (val
, info
);
291 (*print_func
) (stream
, "\t// from address pool at 0x%x",
292 (uint32_t) (memaddr
+ 2
293 + ((inst
& 0xFF) << 2)) & ~3);
300 static char *fields
[] = {
301 "af", "ie", "fe", "fe,ie",
302 "ee", "ee,ie", "ee,fe", "ee,fe,ie"
305 (*print_func
) (stream
, "\t%s", fields
[inst
& 0x7]);
310 /* If the disassembler lags the instruction set. */
311 (*print_func
) (stream
, "\tundecoded operands, inst is 0x%04x", inst
);
316 /* Say how many bytes we consumed. */