1*3d8817e4Smiod /* Instruction printing code for the OpenRISC 1000
2*3d8817e4Smiod Copyright (C) 2002, 2005 Free Software Foundation, Inc.
3*3d8817e4Smiod Contributed by Damjan Lampret <lampret@opencores.org>.
4*3d8817e4Smiod Modified from a29k port.
5*3d8817e4Smiod
6*3d8817e4Smiod This file is part of Binutils.
7*3d8817e4Smiod
8*3d8817e4Smiod This program is free software; you can redistribute it and/or modify
9*3d8817e4Smiod it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod the Free Software Foundation; either version 2 of the License, or
11*3d8817e4Smiod (at your option) any later version.
12*3d8817e4Smiod
13*3d8817e4Smiod This program is distributed in the hope that it will be useful,
14*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*3d8817e4Smiod GNU General Public License for more details.
17*3d8817e4Smiod
18*3d8817e4Smiod You should have received a copy of the GNU General Public License
19*3d8817e4Smiod along with this program; if not, write to the Free Software
20*3d8817e4Smiod Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*3d8817e4Smiod MA 02110-1301, USA. */
22*3d8817e4Smiod
23*3d8817e4Smiod #define DEBUG 0
24*3d8817e4Smiod
25*3d8817e4Smiod #include "dis-asm.h"
26*3d8817e4Smiod #include "opcode/or32.h"
27*3d8817e4Smiod #include "safe-ctype.h"
28*3d8817e4Smiod #include <string.h>
29*3d8817e4Smiod #include <stdlib.h>
30*3d8817e4Smiod
31*3d8817e4Smiod #define EXTEND29(x) ((x) & (unsigned long) 0x10000000 ? ((x) | (unsigned long) 0xf0000000) : ((x)))
32*3d8817e4Smiod
33*3d8817e4Smiod /* Now find the four bytes of INSN_CH and put them in *INSN. */
34*3d8817e4Smiod
35*3d8817e4Smiod static void
find_bytes_big(unsigned char * insn_ch,unsigned long * insn)36*3d8817e4Smiod find_bytes_big (unsigned char *insn_ch, unsigned long *insn)
37*3d8817e4Smiod {
38*3d8817e4Smiod *insn =
39*3d8817e4Smiod ((unsigned long) insn_ch[0] << 24) +
40*3d8817e4Smiod ((unsigned long) insn_ch[1] << 16) +
41*3d8817e4Smiod ((unsigned long) insn_ch[2] << 8) +
42*3d8817e4Smiod ((unsigned long) insn_ch[3]);
43*3d8817e4Smiod #if DEBUG
44*3d8817e4Smiod printf ("find_bytes_big3: %x\n", *insn);
45*3d8817e4Smiod #endif
46*3d8817e4Smiod }
47*3d8817e4Smiod
48*3d8817e4Smiod static void
find_bytes_little(unsigned char * insn_ch,unsigned long * insn)49*3d8817e4Smiod find_bytes_little (unsigned char *insn_ch, unsigned long *insn)
50*3d8817e4Smiod {
51*3d8817e4Smiod *insn =
52*3d8817e4Smiod ((unsigned long) insn_ch[3] << 24) +
53*3d8817e4Smiod ((unsigned long) insn_ch[2] << 16) +
54*3d8817e4Smiod ((unsigned long) insn_ch[1] << 8) +
55*3d8817e4Smiod ((unsigned long) insn_ch[0]);
56*3d8817e4Smiod }
57*3d8817e4Smiod
58*3d8817e4Smiod typedef void (*find_byte_func_type) (unsigned char *, unsigned long *);
59*3d8817e4Smiod
60*3d8817e4Smiod static unsigned long
or32_extract(char param_ch,char * enc_initial,unsigned long insn)61*3d8817e4Smiod or32_extract (char param_ch, char *enc_initial, unsigned long insn)
62*3d8817e4Smiod {
63*3d8817e4Smiod char *enc;
64*3d8817e4Smiod unsigned long ret = 0;
65*3d8817e4Smiod int opc_pos = 0;
66*3d8817e4Smiod int param_pos = 0;
67*3d8817e4Smiod
68*3d8817e4Smiod for (enc = enc_initial; *enc != '\0'; enc++)
69*3d8817e4Smiod if (*enc == param_ch)
70*3d8817e4Smiod {
71*3d8817e4Smiod if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
72*3d8817e4Smiod continue;
73*3d8817e4Smiod else
74*3d8817e4Smiod param_pos++;
75*3d8817e4Smiod }
76*3d8817e4Smiod
77*3d8817e4Smiod #if DEBUG
78*3d8817e4Smiod printf ("or32_extract: %c %x ", param_ch, param_pos);
79*3d8817e4Smiod #endif
80*3d8817e4Smiod opc_pos = 32;
81*3d8817e4Smiod
82*3d8817e4Smiod for (enc = enc_initial; *enc != '\0'; )
83*3d8817e4Smiod if ((*enc == '0') && (*(enc + 1) == 'x'))
84*3d8817e4Smiod {
85*3d8817e4Smiod opc_pos -= 4;
86*3d8817e4Smiod
87*3d8817e4Smiod if ((param_ch == '0') || (param_ch == '1'))
88*3d8817e4Smiod {
89*3d8817e4Smiod unsigned long tmp = strtoul (enc, NULL, 16);
90*3d8817e4Smiod #if DEBUG
91*3d8817e4Smiod printf (" enc=%s, tmp=%x ", enc, tmp);
92*3d8817e4Smiod #endif
93*3d8817e4Smiod if (param_ch == '0')
94*3d8817e4Smiod tmp = 15 - tmp;
95*3d8817e4Smiod ret |= tmp << opc_pos;
96*3d8817e4Smiod }
97*3d8817e4Smiod enc += 3;
98*3d8817e4Smiod }
99*3d8817e4Smiod else if ((*enc == '0') || (*enc == '1'))
100*3d8817e4Smiod {
101*3d8817e4Smiod opc_pos--;
102*3d8817e4Smiod if (param_ch == *enc)
103*3d8817e4Smiod ret |= 1 << opc_pos;
104*3d8817e4Smiod enc++;
105*3d8817e4Smiod }
106*3d8817e4Smiod else if (*enc == param_ch)
107*3d8817e4Smiod {
108*3d8817e4Smiod opc_pos--;
109*3d8817e4Smiod param_pos--;
110*3d8817e4Smiod #if DEBUG
111*3d8817e4Smiod printf ("\n ret=%x opc_pos=%x, param_pos=%x\n", ret, opc_pos, param_pos);
112*3d8817e4Smiod #endif
113*3d8817e4Smiod ret += ((insn >> opc_pos) & 0x1) << param_pos;
114*3d8817e4Smiod
115*3d8817e4Smiod if (!param_pos
116*3d8817e4Smiod && letter_signed (param_ch)
117*3d8817e4Smiod && ret >> (letter_range (param_ch) - 1))
118*3d8817e4Smiod {
119*3d8817e4Smiod #if DEBUG
120*3d8817e4Smiod printf ("\n ret=%x opc_pos=%x, param_pos=%x\n",
121*3d8817e4Smiod ret, opc_pos, param_pos);
122*3d8817e4Smiod #endif
123*3d8817e4Smiod ret |= 0xffffffff << letter_range(param_ch);
124*3d8817e4Smiod #if DEBUG
125*3d8817e4Smiod printf ("\n after conversion to signed: ret=%x\n", ret);
126*3d8817e4Smiod #endif
127*3d8817e4Smiod }
128*3d8817e4Smiod enc++;
129*3d8817e4Smiod }
130*3d8817e4Smiod else if (ISALPHA (*enc))
131*3d8817e4Smiod {
132*3d8817e4Smiod opc_pos--;
133*3d8817e4Smiod enc++;
134*3d8817e4Smiod }
135*3d8817e4Smiod else if (*enc == '-')
136*3d8817e4Smiod {
137*3d8817e4Smiod opc_pos--;
138*3d8817e4Smiod enc++;
139*3d8817e4Smiod }
140*3d8817e4Smiod else
141*3d8817e4Smiod enc++;
142*3d8817e4Smiod
143*3d8817e4Smiod #if DEBUG
144*3d8817e4Smiod printf ("ret=%x\n", ret);
145*3d8817e4Smiod #endif
146*3d8817e4Smiod return ret;
147*3d8817e4Smiod }
148*3d8817e4Smiod
149*3d8817e4Smiod static int
or32_opcode_match(unsigned long insn,char * encoding)150*3d8817e4Smiod or32_opcode_match (unsigned long insn, char *encoding)
151*3d8817e4Smiod {
152*3d8817e4Smiod unsigned long ones, zeros;
153*3d8817e4Smiod
154*3d8817e4Smiod #if DEBUG
155*3d8817e4Smiod printf ("or32_opcode_match: %.8lx\n", insn);
156*3d8817e4Smiod #endif
157*3d8817e4Smiod ones = or32_extract ('1', encoding, insn);
158*3d8817e4Smiod zeros = or32_extract ('0', encoding, insn);
159*3d8817e4Smiod
160*3d8817e4Smiod #if DEBUG
161*3d8817e4Smiod printf ("ones: %x \n", ones);
162*3d8817e4Smiod printf ("zeros: %x \n", zeros);
163*3d8817e4Smiod #endif
164*3d8817e4Smiod if ((insn & ones) != ones)
165*3d8817e4Smiod {
166*3d8817e4Smiod #if DEBUG
167*3d8817e4Smiod printf ("ret1\n");
168*3d8817e4Smiod #endif
169*3d8817e4Smiod return 0;
170*3d8817e4Smiod }
171*3d8817e4Smiod
172*3d8817e4Smiod if ((~insn & zeros) != zeros)
173*3d8817e4Smiod {
174*3d8817e4Smiod #if DEBUG
175*3d8817e4Smiod printf ("ret2\n");
176*3d8817e4Smiod #endif
177*3d8817e4Smiod return 0;
178*3d8817e4Smiod }
179*3d8817e4Smiod
180*3d8817e4Smiod #if DEBUG
181*3d8817e4Smiod printf ("ret3\n");
182*3d8817e4Smiod #endif
183*3d8817e4Smiod return 1;
184*3d8817e4Smiod }
185*3d8817e4Smiod
186*3d8817e4Smiod /* Print register to INFO->STREAM. Used only by print_insn. */
187*3d8817e4Smiod
188*3d8817e4Smiod static void
or32_print_register(char param_ch,char * encoding,unsigned long insn,struct disassemble_info * info)189*3d8817e4Smiod or32_print_register (char param_ch,
190*3d8817e4Smiod char *encoding,
191*3d8817e4Smiod unsigned long insn,
192*3d8817e4Smiod struct disassemble_info *info)
193*3d8817e4Smiod {
194*3d8817e4Smiod int regnum = or32_extract (param_ch, encoding, insn);
195*3d8817e4Smiod
196*3d8817e4Smiod #if DEBUG
197*3d8817e4Smiod printf ("or32_print_register: %c, %s, %x\n", param_ch, encoding, insn);
198*3d8817e4Smiod #endif
199*3d8817e4Smiod if (param_ch == 'A')
200*3d8817e4Smiod (*info->fprintf_func) (info->stream, "r%d", regnum);
201*3d8817e4Smiod else if (param_ch == 'B')
202*3d8817e4Smiod (*info->fprintf_func) (info->stream, "r%d", regnum);
203*3d8817e4Smiod else if (param_ch == 'D')
204*3d8817e4Smiod (*info->fprintf_func) (info->stream, "r%d", regnum);
205*3d8817e4Smiod else if (regnum < 16)
206*3d8817e4Smiod (*info->fprintf_func) (info->stream, "r%d", regnum);
207*3d8817e4Smiod else if (regnum < 32)
208*3d8817e4Smiod (*info->fprintf_func) (info->stream, "r%d", regnum-16);
209*3d8817e4Smiod else
210*3d8817e4Smiod (*info->fprintf_func) (info->stream, "X%d", regnum);
211*3d8817e4Smiod }
212*3d8817e4Smiod
213*3d8817e4Smiod /* Print immediate to INFO->STREAM. Used only by print_insn. */
214*3d8817e4Smiod
215*3d8817e4Smiod static void
or32_print_immediate(char param_ch,char * encoding,unsigned long insn,struct disassemble_info * info)216*3d8817e4Smiod or32_print_immediate (char param_ch,
217*3d8817e4Smiod char *encoding,
218*3d8817e4Smiod unsigned long insn,
219*3d8817e4Smiod struct disassemble_info *info)
220*3d8817e4Smiod {
221*3d8817e4Smiod int imm = or32_extract(param_ch, encoding, insn);
222*3d8817e4Smiod
223*3d8817e4Smiod if (letter_signed(param_ch))
224*3d8817e4Smiod (*info->fprintf_func) (info->stream, "0x%x", imm);
225*3d8817e4Smiod /* (*info->fprintf_func) (info->stream, "%d", imm); */
226*3d8817e4Smiod else
227*3d8817e4Smiod (*info->fprintf_func) (info->stream, "0x%x", imm);
228*3d8817e4Smiod }
229*3d8817e4Smiod
230*3d8817e4Smiod /* Print one instruction from MEMADDR on INFO->STREAM.
231*3d8817e4Smiod Return the size of the instruction (always 4 on or32). */
232*3d8817e4Smiod
233*3d8817e4Smiod static int
print_insn(bfd_vma memaddr,struct disassemble_info * info)234*3d8817e4Smiod print_insn (bfd_vma memaddr, struct disassemble_info *info)
235*3d8817e4Smiod {
236*3d8817e4Smiod /* The raw instruction. */
237*3d8817e4Smiod unsigned char insn_ch[4];
238*3d8817e4Smiod /* Address. Will be sign extened 27-bit. */
239*3d8817e4Smiod unsigned long addr;
240*3d8817e4Smiod /* The four bytes of the instruction. */
241*3d8817e4Smiod unsigned long insn;
242*3d8817e4Smiod find_byte_func_type find_byte_func = (find_byte_func_type) info->private_data;
243*3d8817e4Smiod struct or32_opcode const * opcode;
244*3d8817e4Smiod
245*3d8817e4Smiod {
246*3d8817e4Smiod int status =
247*3d8817e4Smiod (*info->read_memory_func) (memaddr, (bfd_byte *) &insn_ch[0], 4, info);
248*3d8817e4Smiod
249*3d8817e4Smiod if (status != 0)
250*3d8817e4Smiod {
251*3d8817e4Smiod (*info->memory_error_func) (status, memaddr, info);
252*3d8817e4Smiod return -1;
253*3d8817e4Smiod }
254*3d8817e4Smiod }
255*3d8817e4Smiod
256*3d8817e4Smiod (*find_byte_func) (&insn_ch[0], &insn);
257*3d8817e4Smiod
258*3d8817e4Smiod for (opcode = &or32_opcodes[0];
259*3d8817e4Smiod opcode < &or32_opcodes[or32_num_opcodes];
260*3d8817e4Smiod ++opcode)
261*3d8817e4Smiod {
262*3d8817e4Smiod if (or32_opcode_match (insn, opcode->encoding))
263*3d8817e4Smiod {
264*3d8817e4Smiod char *s;
265*3d8817e4Smiod
266*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%s ", opcode->name);
267*3d8817e4Smiod
268*3d8817e4Smiod for (s = opcode->args; *s != '\0'; ++s)
269*3d8817e4Smiod {
270*3d8817e4Smiod switch (*s)
271*3d8817e4Smiod {
272*3d8817e4Smiod case '\0':
273*3d8817e4Smiod return 4;
274*3d8817e4Smiod
275*3d8817e4Smiod case 'r':
276*3d8817e4Smiod or32_print_register (*++s, opcode->encoding, insn, info);
277*3d8817e4Smiod break;
278*3d8817e4Smiod
279*3d8817e4Smiod case 'X':
280*3d8817e4Smiod addr = or32_extract ('X', opcode->encoding, insn) << 2;
281*3d8817e4Smiod
282*3d8817e4Smiod /* Calulate the correct address. XXX is this really correct ?? */
283*3d8817e4Smiod addr = memaddr + EXTEND29 (addr);
284*3d8817e4Smiod
285*3d8817e4Smiod (*info->print_address_func)
286*3d8817e4Smiod (addr, info);
287*3d8817e4Smiod break;
288*3d8817e4Smiod
289*3d8817e4Smiod default:
290*3d8817e4Smiod if (strchr (opcode->encoding, *s))
291*3d8817e4Smiod or32_print_immediate (*s, opcode->encoding, insn, info);
292*3d8817e4Smiod else
293*3d8817e4Smiod (*info->fprintf_func) (info->stream, "%c", *s);
294*3d8817e4Smiod }
295*3d8817e4Smiod }
296*3d8817e4Smiod
297*3d8817e4Smiod return 4;
298*3d8817e4Smiod }
299*3d8817e4Smiod }
300*3d8817e4Smiod
301*3d8817e4Smiod /* This used to be %8x for binutils. */
302*3d8817e4Smiod (*info->fprintf_func)
303*3d8817e4Smiod (info->stream, ".word 0x%08lx", insn);
304*3d8817e4Smiod return 4;
305*3d8817e4Smiod }
306*3d8817e4Smiod
307*3d8817e4Smiod /* Disassemble a big-endian or32 instruction. */
308*3d8817e4Smiod
309*3d8817e4Smiod int
print_insn_big_or32(bfd_vma memaddr,struct disassemble_info * info)310*3d8817e4Smiod print_insn_big_or32 (bfd_vma memaddr, struct disassemble_info *info)
311*3d8817e4Smiod {
312*3d8817e4Smiod info->private_data = find_bytes_big;
313*3d8817e4Smiod
314*3d8817e4Smiod return print_insn (memaddr, info);
315*3d8817e4Smiod }
316*3d8817e4Smiod
317*3d8817e4Smiod /* Disassemble a little-endian or32 instruction. */
318*3d8817e4Smiod
319*3d8817e4Smiod int
print_insn_little_or32(bfd_vma memaddr,struct disassemble_info * info)320*3d8817e4Smiod print_insn_little_or32 (bfd_vma memaddr, struct disassemble_info *info)
321*3d8817e4Smiod {
322*3d8817e4Smiod info->private_data = find_bytes_little;
323*3d8817e4Smiod return print_insn (memaddr, info);
324*3d8817e4Smiod }
325