1 /* Instruction printing code for the OpenRISC 1000
2 Copyright (C) 2002 Free Software Foundation, Inc.
3 Contributed by Damjan Lampret <lampret@opencores.org>.
4 Modified from a29k port.
6 This file is part of Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "opcode/or32.h"
26 #include "safe-ctype.h"
30 #define EXTEND29(x) ((x) & (unsigned long) 0x10000000 ? ((x) | (unsigned long) 0xf0000000) : ((x)))
32 static void find_bytes_big
PARAMS ((unsigned char *, unsigned long *));
33 static void find_bytes_little
PARAMS ((unsigned char *, unsigned long *));
34 static unsigned long or32_extract
PARAMS ((char, char *, unsigned long));
35 static int or32_opcode_match
PARAMS ((unsigned long, char *));
36 static void or32_print_register
PARAMS ((char, char *, unsigned long, struct disassemble_info
*));
37 static void or32_print_immediate
PARAMS ((char, char *, unsigned long, struct disassemble_info
*));
38 static int print_insn
PARAMS ((bfd_vma
, struct disassemble_info
*));
40 /* Now find the four bytes of INSN_CH and put them in *INSN. */
43 find_bytes_big (insn_ch
, insn
)
44 unsigned char *insn_ch
;
48 ((unsigned long) insn_ch
[0] << 24) +
49 ((unsigned long) insn_ch
[1] << 16) +
50 ((unsigned long) insn_ch
[2] << 8) +
51 ((unsigned long) insn_ch
[3]);
53 printf ("find_bytes_big3: %x\n", *insn
);
58 find_bytes_little (insn_ch
, insn
)
59 unsigned char *insn_ch
;
63 ((unsigned long) insn_ch
[3] << 24) +
64 ((unsigned long) insn_ch
[2] << 16) +
65 ((unsigned long) insn_ch
[1] << 8) +
66 ((unsigned long) insn_ch
[0]);
69 typedef void (*find_byte_func_type
)
70 PARAMS ((unsigned char *, unsigned long *));
73 or32_extract (param_ch
, enc_initial
, insn
)
79 unsigned long ret
= 0;
83 for (enc
= enc_initial
; *enc
!= '\0'; enc
++)
86 if (enc
- 2 >= enc_initial
&& (*(enc
- 2) == '0') && (*(enc
- 1) == 'x'))
93 printf ("or32_extract: %c %x ", param_ch
, param_pos
);
97 for (enc
= enc_initial
; *enc
!= '\0'; )
98 if ((*enc
== '0') && (*(enc
+ 1) == 'x'))
102 if ((param_ch
== '0') || (param_ch
== '1'))
104 unsigned long tmp
= strtoul (enc
, NULL
, 16);
106 printf (" enc=%s, tmp=%x ", enc
, tmp
);
110 ret
|= tmp
<< opc_pos
;
114 else if ((*enc
== '0') || (*enc
== '1'))
117 if (param_ch
== *enc
)
121 else if (*enc
== param_ch
)
126 printf ("\n ret=%x opc_pos=%x, param_pos=%x\n", ret
, opc_pos
, param_pos
);
128 ret
+= ((insn
>> opc_pos
) & 0x1) << param_pos
;
131 && letter_signed (param_ch
)
132 && ret
>> (letter_range (param_ch
) - 1))
135 printf ("\n ret=%x opc_pos=%x, param_pos=%x\n",
136 ret
, opc_pos
, param_pos
);
138 ret
|= 0xffffffff << letter_range(param_ch
);
140 printf ("\n after conversion to signed: ret=%x\n", ret
);
145 else if (ISALPHA (*enc
))
150 else if (*enc
== '-')
159 printf ("ret=%x\n", ret
);
165 or32_opcode_match (insn
, encoding
)
169 unsigned long ones
, zeros
;
172 printf ("or32_opcode_match: %.8lx\n", insn
);
174 ones
= or32_extract ('1', encoding
, insn
);
175 zeros
= or32_extract ('0', encoding
, insn
);
178 printf ("ones: %x \n", ones
);
179 printf ("zeros: %x \n", zeros
);
181 if ((insn
& ones
) != ones
)
189 if ((~insn
& zeros
) != zeros
)
203 /* Print register to INFO->STREAM. Used only by print_insn. */
206 or32_print_register (param_ch
, encoding
, insn
, info
)
210 struct disassemble_info
*info
;
212 int regnum
= or32_extract (param_ch
, encoding
, insn
);
215 printf ("or32_print_register: %c, %s, %x\n", param_ch
, encoding
, insn
);
218 (*info
->fprintf_func
) (info
->stream
, "r%d", regnum
);
219 else if (param_ch
== 'B')
220 (*info
->fprintf_func
) (info
->stream
, "r%d", regnum
);
221 else if (param_ch
== 'D')
222 (*info
->fprintf_func
) (info
->stream
, "r%d", regnum
);
223 else if (regnum
< 16)
224 (*info
->fprintf_func
) (info
->stream
, "r%d", regnum
);
225 else if (regnum
< 32)
226 (*info
->fprintf_func
) (info
->stream
, "r%d", regnum
-16);
228 (*info
->fprintf_func
) (info
->stream
, "X%d", regnum
);
231 /* Print immediate to INFO->STREAM. Used only by print_insn. */
234 or32_print_immediate (param_ch
, encoding
, insn
, info
)
238 struct disassemble_info
*info
;
240 int imm
= or32_extract(param_ch
, encoding
, insn
);
242 if (letter_signed(param_ch
))
243 (*info
->fprintf_func
) (info
->stream
, "0x%x", imm
);
244 /* (*info->fprintf_func) (info->stream, "%d", imm); */
246 (*info
->fprintf_func
) (info
->stream
, "0x%x", imm
);
249 /* Print one instruction from MEMADDR on INFO->STREAM.
250 Return the size of the instruction (always 4 on or32). */
253 print_insn (memaddr
, info
)
255 struct disassemble_info
*info
;
257 /* The raw instruction. */
258 unsigned char insn_ch
[4];
259 /* Address. Will be sign extened 27-bit. */
261 /* The four bytes of the instruction. */
263 find_byte_func_type find_byte_func
= (find_byte_func_type
)info
->private_data
;
264 struct or32_opcode CONST
* opcode
;
268 (*info
->read_memory_func
) (memaddr
, (bfd_byte
*) &insn_ch
[0], 4, info
);
272 (*info
->memory_error_func
) (status
, memaddr
, info
);
277 (*find_byte_func
) (&insn_ch
[0], &insn
);
279 for (opcode
= &or32_opcodes
[0];
280 opcode
< &or32_opcodes
[or32_num_opcodes
];
283 if (or32_opcode_match (insn
, opcode
->encoding
))
287 (*info
->fprintf_func
) (info
->stream
, "%s ", opcode
->name
);
289 for (s
= opcode
->args
; *s
!= '\0'; ++s
)
297 or32_print_register (*++s
, opcode
->encoding
, insn
, info
);
301 addr
= or32_extract ('X', opcode
->encoding
, insn
) << 2;
303 /* Calulate the correct address. XXX is this really correct ?? */
304 addr
= memaddr
+ EXTEND29 (addr
);
306 (*info
->print_address_func
)
311 if (strchr (opcode
->encoding
, *s
))
312 or32_print_immediate (*s
, opcode
->encoding
, insn
, info
);
314 (*info
->fprintf_func
) (info
->stream
, "%c", *s
);
322 /* This used to be %8x for binutils. */
323 (*info
->fprintf_func
)
324 (info
->stream
, ".word 0x%08x", insn
);
328 /* Disassemble a big-endian or32 instruction. */
331 print_insn_big_or32 (memaddr
, info
)
333 struct disassemble_info
*info
;
335 info
->private_data
= (PTR
) find_bytes_big
;
336 return print_insn (memaddr
, info
);
339 /* Disassemble a little-endian or32 instruction. */
342 print_insn_little_or32 (memaddr
, info
)
344 struct disassemble_info
*info
;
346 info
->private_data
= (PTR
) find_bytes_little
;
347 return print_insn (memaddr
, info
);