1*3d8817e4Smiod /* Disassembler for the i860.
2*3d8817e4Smiod Copyright 2000, 2003 Free Software Foundation, Inc.
3*3d8817e4Smiod
4*3d8817e4Smiod Contributed by Jason Eckhardt <jle@cygnus.com>.
5*3d8817e4Smiod
6*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
7*3d8817e4Smiod it under the terms of the GNU General Public License as published by
8*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
9*3d8817e4Smiod (at your option) any later version.
10*3d8817e4Smiod
11*3d8817e4Smiod This program is distributed in the hope that it will be useful,
12*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
13*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*3d8817e4Smiod GNU General Public License for more details.
15*3d8817e4Smiod
16*3d8817e4Smiod You should have received a copy of the GNU General Public License
17*3d8817e4Smiod along with this program; if not, write to the Free Software
18*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
19*3d8817e4Smiod
20*3d8817e4Smiod #include "dis-asm.h"
21*3d8817e4Smiod #include "opcode/i860.h"
22*3d8817e4Smiod
23*3d8817e4Smiod /* Later we should probably choose the prefix based on which OS flavor. */
24*3d8817e4Smiod #define I860_REG_PREFIX "%"
25*3d8817e4Smiod
26*3d8817e4Smiod /* Integer register names (encoded as 0..31 in the instruction). */
27*3d8817e4Smiod static const char *const grnames[] =
28*3d8817e4Smiod {"r0", "r1", "sp", "fp", "r4", "r5", "r6", "r7",
29*3d8817e4Smiod "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
30*3d8817e4Smiod "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
31*3d8817e4Smiod "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31"};
32*3d8817e4Smiod
33*3d8817e4Smiod /* FP register names (encoded as 0..31 in the instruction). */
34*3d8817e4Smiod static const char *const frnames[] =
35*3d8817e4Smiod {"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
36*3d8817e4Smiod "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
37*3d8817e4Smiod "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
38*3d8817e4Smiod "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"};
39*3d8817e4Smiod
40*3d8817e4Smiod /* Control/status register names (encoded as 0..11 in the instruction).
41*3d8817e4Smiod Registers bear, ccr, p0, p1, p2 and p3 are XP only. */
42*3d8817e4Smiod static const char *const crnames[] =
43*3d8817e4Smiod {"fir", "psr", "dirbase", "db", "fsr", "epsr", "bear", "ccr",
44*3d8817e4Smiod "p0", "p1", "p2", "p3", "--", "--", "--", "--" };
45*3d8817e4Smiod
46*3d8817e4Smiod
47*3d8817e4Smiod
48*3d8817e4Smiod /* True if opcode is xor, xorh, and, andh, or, orh, andnot, andnoth. */
49*3d8817e4Smiod #define BITWISE_OP(op) ((op) == 0x30 || (op) == 0x31 \
50*3d8817e4Smiod || (op) == 0x34 || (op) == 0x35 \
51*3d8817e4Smiod || (op) == 0x38 || (op) == 0x39 \
52*3d8817e4Smiod || (op) == 0x3c || (op) == 0x3d \
53*3d8817e4Smiod || (op) == 0x33 || (op) == 0x37 \
54*3d8817e4Smiod || (op) == 0x3b || (op) == 0x3f)
55*3d8817e4Smiod
56*3d8817e4Smiod
57*3d8817e4Smiod /* Sign extend N-bit number. */
58*3d8817e4Smiod static int
sign_ext(unsigned int x,int n)59*3d8817e4Smiod sign_ext (unsigned int x, int n)
60*3d8817e4Smiod {
61*3d8817e4Smiod int t;
62*3d8817e4Smiod t = x >> (n - 1);
63*3d8817e4Smiod t = ((-t) << n) | x;
64*3d8817e4Smiod return t;
65*3d8817e4Smiod }
66*3d8817e4Smiod
67*3d8817e4Smiod
68*3d8817e4Smiod /* Print a PC-relative branch offset. VAL is the sign extended value
69*3d8817e4Smiod from the branch instruction. */
70*3d8817e4Smiod static void
print_br_address(disassemble_info * info,bfd_vma memaddr,long val)71*3d8817e4Smiod print_br_address (disassemble_info *info, bfd_vma memaddr, long val)
72*3d8817e4Smiod {
73*3d8817e4Smiod
74*3d8817e4Smiod long adj = (long)memaddr + 4 + (val << 2);
75*3d8817e4Smiod
76*3d8817e4Smiod (*info->fprintf_func) (info->stream, "0x%08lx", adj);
77*3d8817e4Smiod
78*3d8817e4Smiod /* Attempt to obtain a symbol for the target address. */
79*3d8817e4Smiod
80*3d8817e4Smiod if (info->print_address_func && adj != 0)
81*3d8817e4Smiod {
82*3d8817e4Smiod (*info->fprintf_func) (info->stream, "\t// ");
83*3d8817e4Smiod (*info->print_address_func) (adj, info);
84*3d8817e4Smiod }
85*3d8817e4Smiod }
86*3d8817e4Smiod
87*3d8817e4Smiod
88*3d8817e4Smiod /* Print one instruction. */
89*3d8817e4Smiod int
print_insn_i860(bfd_vma memaddr,disassemble_info * info)90*3d8817e4Smiod print_insn_i860 (bfd_vma memaddr, disassemble_info *info)
91*3d8817e4Smiod {
92*3d8817e4Smiod bfd_byte buff[4];
93*3d8817e4Smiod unsigned int insn, i;
94*3d8817e4Smiod int status;
95*3d8817e4Smiod const struct i860_opcode *opcode = 0;
96*3d8817e4Smiod
97*3d8817e4Smiod status = (*info->read_memory_func) (memaddr, buff, sizeof (buff), info);
98*3d8817e4Smiod if (status != 0)
99*3d8817e4Smiod {
100*3d8817e4Smiod (*info->memory_error_func) (status, memaddr, info);
101*3d8817e4Smiod return -1;
102*3d8817e4Smiod }
103*3d8817e4Smiod
104*3d8817e4Smiod /* Note that i860 instructions are always accessed as little endian
105*3d8817e4Smiod data, regardless of the endian mode of the i860. */
106*3d8817e4Smiod insn = bfd_getl32 (buff);
107*3d8817e4Smiod
108*3d8817e4Smiod status = 0;
109*3d8817e4Smiod i = 0;
110*3d8817e4Smiod while (i860_opcodes[i].name != NULL)
111*3d8817e4Smiod {
112*3d8817e4Smiod opcode = &i860_opcodes[i];
113*3d8817e4Smiod if ((insn & opcode->match) == opcode->match
114*3d8817e4Smiod && (insn & opcode->lose) == 0)
115*3d8817e4Smiod {
116*3d8817e4Smiod status = 1;
117*3d8817e4Smiod break;
118*3d8817e4Smiod }
119*3d8817e4Smiod ++i;
120*3d8817e4Smiod }
121*3d8817e4Smiod
122*3d8817e4Smiod if (status == 0)
123*3d8817e4Smiod {
124*3d8817e4Smiod /* Instruction not in opcode table. */
125*3d8817e4Smiod (*info->fprintf_func) (info->stream, ".long %#08x", insn);
126*3d8817e4Smiod }
127*3d8817e4Smiod else
128*3d8817e4Smiod {
129*3d8817e4Smiod const char *s;
130*3d8817e4Smiod int val;
131*3d8817e4Smiod
132*3d8817e4Smiod /* If this a flop (or a shrd) and its dual bit is set,
133*3d8817e4Smiod prefix with 'd.'. */
134*3d8817e4Smiod if (((insn & 0xfc000000) == 0x48000000
135*3d8817e4Smiod || (insn & 0xfc000000) == 0xb0000000)
136*3d8817e4Smiod && (insn & 0x200))
137*3d8817e4Smiod (*info->fprintf_func) (info->stream, "d.%s\t", opcode->name);
138*3d8817e4Smiod else
139*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s\t", opcode->name);
140*3d8817e4Smiod
141*3d8817e4Smiod for (s = opcode->args; *s; s++)
142*3d8817e4Smiod {
143*3d8817e4Smiod switch (*s)
144*3d8817e4Smiod {
145*3d8817e4Smiod /* Integer register (src1). */
146*3d8817e4Smiod case '1':
147*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%s", I860_REG_PREFIX,
148*3d8817e4Smiod grnames[(insn >> 11) & 0x1f]);
149*3d8817e4Smiod break;
150*3d8817e4Smiod
151*3d8817e4Smiod /* Integer register (src2). */
152*3d8817e4Smiod case '2':
153*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%s", I860_REG_PREFIX,
154*3d8817e4Smiod grnames[(insn >> 21) & 0x1f]);
155*3d8817e4Smiod break;
156*3d8817e4Smiod
157*3d8817e4Smiod /* Integer destination register. */
158*3d8817e4Smiod case 'd':
159*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%s", I860_REG_PREFIX,
160*3d8817e4Smiod grnames[(insn >> 16) & 0x1f]);
161*3d8817e4Smiod break;
162*3d8817e4Smiod
163*3d8817e4Smiod /* Floating-point register (src1). */
164*3d8817e4Smiod case 'e':
165*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%s", I860_REG_PREFIX,
166*3d8817e4Smiod frnames[(insn >> 11) & 0x1f]);
167*3d8817e4Smiod break;
168*3d8817e4Smiod
169*3d8817e4Smiod /* Floating-point register (src2). */
170*3d8817e4Smiod case 'f':
171*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%s", I860_REG_PREFIX,
172*3d8817e4Smiod frnames[(insn >> 21) & 0x1f]);
173*3d8817e4Smiod break;
174*3d8817e4Smiod
175*3d8817e4Smiod /* Floating-point destination register. */
176*3d8817e4Smiod case 'g':
177*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%s", I860_REG_PREFIX,
178*3d8817e4Smiod frnames[(insn >> 16) & 0x1f]);
179*3d8817e4Smiod break;
180*3d8817e4Smiod
181*3d8817e4Smiod /* Control register. */
182*3d8817e4Smiod case 'c':
183*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s%s", I860_REG_PREFIX,
184*3d8817e4Smiod crnames[(insn >> 21) & 0xf]);
185*3d8817e4Smiod break;
186*3d8817e4Smiod
187*3d8817e4Smiod /* 16-bit immediate (sign extend, except for bitwise ops). */
188*3d8817e4Smiod case 'i':
189*3d8817e4Smiod if (BITWISE_OP ((insn & 0xfc000000) >> 26))
190*3d8817e4Smiod (*info->fprintf_func) (info->stream, "0x%04x",
191*3d8817e4Smiod (unsigned int) (insn & 0xffff));
192*3d8817e4Smiod else
193*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
194*3d8817e4Smiod sign_ext ((insn & 0xffff), 16));
195*3d8817e4Smiod break;
196*3d8817e4Smiod
197*3d8817e4Smiod /* 16-bit immediate, aligned (2^0, ld.b). */
198*3d8817e4Smiod case 'I':
199*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
200*3d8817e4Smiod sign_ext ((insn & 0xffff), 16));
201*3d8817e4Smiod break;
202*3d8817e4Smiod
203*3d8817e4Smiod /* 16-bit immediate, aligned (2^1, ld.s). */
204*3d8817e4Smiod case 'J':
205*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
206*3d8817e4Smiod sign_ext ((insn & 0xfffe), 16));
207*3d8817e4Smiod break;
208*3d8817e4Smiod
209*3d8817e4Smiod /* 16-bit immediate, aligned (2^2, ld.l, {p}fld.l, fst.l). */
210*3d8817e4Smiod case 'K':
211*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
212*3d8817e4Smiod sign_ext ((insn & 0xfffc), 16));
213*3d8817e4Smiod break;
214*3d8817e4Smiod
215*3d8817e4Smiod /* 16-bit immediate, aligned (2^3, {p}fld.d, fst.d). */
216*3d8817e4Smiod case 'L':
217*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
218*3d8817e4Smiod sign_ext ((insn & 0xfff8), 16));
219*3d8817e4Smiod break;
220*3d8817e4Smiod
221*3d8817e4Smiod /* 16-bit immediate, aligned (2^4, {p}fld.q, fst.q). */
222*3d8817e4Smiod case 'M':
223*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
224*3d8817e4Smiod sign_ext ((insn & 0xfff0), 16));
225*3d8817e4Smiod break;
226*3d8817e4Smiod
227*3d8817e4Smiod /* 5-bit immediate (zero extend). */
228*3d8817e4Smiod case '5':
229*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
230*3d8817e4Smiod ((insn >> 11) & 0x1f));
231*3d8817e4Smiod break;
232*3d8817e4Smiod
233*3d8817e4Smiod /* Split 16 bit immediate (20..16:10..0). */
234*3d8817e4Smiod case 's':
235*3d8817e4Smiod val = ((insn >> 5) & 0xf800) | (insn & 0x07ff);
236*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
237*3d8817e4Smiod sign_ext (val, 16));
238*3d8817e4Smiod break;
239*3d8817e4Smiod
240*3d8817e4Smiod /* Split 16 bit immediate, aligned. (2^0, st.b). */
241*3d8817e4Smiod case 'S':
242*3d8817e4Smiod val = ((insn >> 5) & 0xf800) | (insn & 0x07ff);
243*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
244*3d8817e4Smiod sign_ext (val, 16));
245*3d8817e4Smiod break;
246*3d8817e4Smiod
247*3d8817e4Smiod /* Split 16 bit immediate, aligned. (2^1, st.s). */
248*3d8817e4Smiod case 'T':
249*3d8817e4Smiod val = ((insn >> 5) & 0xf800) | (insn & 0x07fe);
250*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
251*3d8817e4Smiod sign_ext (val, 16));
252*3d8817e4Smiod break;
253*3d8817e4Smiod
254*3d8817e4Smiod /* Split 16 bit immediate, aligned. (2^2, st.l). */
255*3d8817e4Smiod case 'U':
256*3d8817e4Smiod val = ((insn >> 5) & 0xf800) | (insn & 0x07fc);
257*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%d",
258*3d8817e4Smiod sign_ext (val, 16));
259*3d8817e4Smiod break;
260*3d8817e4Smiod
261*3d8817e4Smiod /* 26-bit PC relative immediate (lbroff). */
262*3d8817e4Smiod case 'l':
263*3d8817e4Smiod val = sign_ext ((insn & 0x03ffffff), 26);
264*3d8817e4Smiod print_br_address (info, memaddr, val);
265*3d8817e4Smiod break;
266*3d8817e4Smiod
267*3d8817e4Smiod /* 16-bit PC relative immediate (sbroff). */
268*3d8817e4Smiod case 'r':
269*3d8817e4Smiod val = sign_ext ((((insn >> 5) & 0xf800) | (insn & 0x07ff)), 16);
270*3d8817e4Smiod print_br_address (info, memaddr, val);
271*3d8817e4Smiod break;
272*3d8817e4Smiod
273*3d8817e4Smiod default:
274*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%c", *s);
275*3d8817e4Smiod break;
276*3d8817e4Smiod }
277*3d8817e4Smiod }
278*3d8817e4Smiod }
279*3d8817e4Smiod
280*3d8817e4Smiod return sizeof (insn);
281*3d8817e4Smiod }
282*3d8817e4Smiod
283