1 /* Instruction printing code for the OpenRISC 1000
2 Copyright (C) 2002, 2005, 2007, 2012 Free Software Foundation, Inc.
3 Contributed by Damjan Lampret <lampret@opencores.org>.
4 Modified from a29k port.
6 This file is part of the GNU opcodes library.
8 This library 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 3, or (at your option)
13 It is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
29 #include "opcode/or32.h"
30 #include "safe-ctype.h"
32 #define EXTEND29(x) ((x) & (unsigned long) 0x10000000 ? ((x) | (unsigned long) 0xf0000000) : ((x)))
34 /* Now find the four bytes of INSN_CH and put them in *INSN. */
37 find_bytes_big (unsigned char *insn_ch, unsigned long *insn)
40 ((unsigned long) insn_ch[0] << 24) +
41 ((unsigned long) insn_ch[1] << 16) +
42 ((unsigned long) insn_ch[2] << 8) +
43 ((unsigned long) insn_ch[3]);
45 printf ("find_bytes_big3: %lx\n", *insn);
50 find_bytes_little (unsigned char *insn_ch, unsigned long *insn)
53 ((unsigned long) insn_ch[3] << 24) +
54 ((unsigned long) insn_ch[2] << 16) +
55 ((unsigned long) insn_ch[1] << 8) +
56 ((unsigned long) insn_ch[0]);
59 typedef void (*find_byte_func_type) (unsigned char *, unsigned long *);
62 or32_extract (char param_ch, char *enc_initial, unsigned long insn)
65 unsigned long ret = 0;
69 for (enc = enc_initial; *enc != '\0'; enc++)
72 if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
79 printf ("or32_extract: %c %x ", param_ch, param_pos);
83 for (enc = enc_initial; *enc != '\0'; )
84 if ((*enc == '0') && (*(enc + 1) == 'x'))
88 if ((param_ch == '0') || (param_ch == '1'))
90 unsigned long tmp = strtoul (enc, NULL, 16);
92 printf (" enc=%s, tmp=%lx ", enc, tmp);
96 ret |= tmp << opc_pos;
100 else if ((*enc == '0') || (*enc == '1'))
103 if (param_ch == *enc)
107 else if (*enc == param_ch)
112 printf ("\n ret=%lx opc_pos=%x, param_pos=%x\n", ret, opc_pos, param_pos);
114 ret += ((insn >> opc_pos) & 0x1) << param_pos;
117 && letter_signed (param_ch)
118 && ret >> (letter_range (param_ch) - 1))
121 printf ("\n ret=%lx opc_pos=%x, param_pos=%x\n",
122 ret, opc_pos, param_pos);
124 ret |= 0xffffffff << letter_range(param_ch);
126 printf ("\n after conversion to signed: ret=%lx\n", ret);
131 else if (ISALPHA (*enc))
136 else if (*enc == '-')
145 printf ("ret=%lx\n", ret);
151 or32_opcode_match (unsigned long insn, char *encoding)
153 unsigned long ones, zeros;
156 printf ("or32_opcode_match: %.8lx\n", insn);
158 ones = or32_extract ('1', encoding, insn);
159 zeros = or32_extract ('0', encoding, insn);
162 printf ("ones: %lx \n", ones);
163 printf ("zeros: %lx \n", zeros);
165 if ((insn & ones) != ones)
173 if ((~insn & zeros) != zeros)
187 /* Print register to INFO->STREAM. Used only by print_insn. */
190 or32_print_register (char param_ch,
193 struct disassemble_info *info)
195 int regnum = or32_extract (param_ch, encoding, insn);
198 printf ("or32_print_register: %c, %s, %lx\n", param_ch, encoding, insn);
201 (*info->fprintf_func) (info->stream, "r%d", regnum);
202 else if (param_ch == 'B')
203 (*info->fprintf_func) (info->stream, "r%d", regnum);
204 else if (param_ch == 'D')
205 (*info->fprintf_func) (info->stream, "r%d", regnum);
206 else if (regnum < 16)
207 (*info->fprintf_func) (info->stream, "r%d", regnum);
208 else if (regnum < 32)
209 (*info->fprintf_func) (info->stream, "r%d", regnum-16);
211 (*info->fprintf_func) (info->stream, "X%d", regnum);
214 /* Print immediate to INFO->STREAM. Used only by print_insn. */
217 or32_print_immediate (char param_ch,
220 struct disassemble_info *info)
222 int imm = or32_extract(param_ch, encoding, insn);
224 if (letter_signed(param_ch))
225 (*info->fprintf_func) (info->stream, "0x%x", imm);
226 /* (*info->fprintf_func) (info->stream, "%d", imm); */
228 (*info->fprintf_func) (info->stream, "0x%x", imm);
231 /* Print one instruction from MEMADDR on INFO->STREAM.
232 Return the size of the instruction (always 4 on or32). */
235 print_insn (bfd_vma memaddr, struct disassemble_info *info)
237 /* The raw instruction. */
238 unsigned char insn_ch[4];
239 /* Address. Will be sign extened 27-bit. */
241 /* The four bytes of the instruction. */
243 find_byte_func_type find_byte_func = (find_byte_func_type) info->private_data;
244 struct or32_opcode const * opcode;
248 (*info->read_memory_func) (memaddr, (bfd_byte *) &insn_ch[0], 4, info);
252 (*info->memory_error_func) (status, memaddr, info);
257 (*find_byte_func) (&insn_ch[0], &insn);
259 for (opcode = &or32_opcodes[0];
260 opcode < &or32_opcodes[or32_num_opcodes];
263 if (or32_opcode_match (insn, opcode->encoding))
267 (*info->fprintf_func) (info->stream, "%s ", opcode->name);
269 for (s = opcode->args; *s != '\0'; ++s)
277 or32_print_register (*++s, opcode->encoding, insn, info);
281 addr = or32_extract ('X', opcode->encoding, insn) << 2;
283 /* Calulate the correct address. XXX is this really correct ?? */
284 addr = memaddr + EXTEND29 (addr);
286 (*info->print_address_func)
291 if (strchr (opcode->encoding, *s))
292 or32_print_immediate (*s, opcode->encoding, insn, info);
294 (*info->fprintf_func) (info->stream, "%c", *s);
302 /* This used to be %8x for binutils. */
303 (*info->fprintf_func)
304 (info->stream, ".word 0x%08lx", insn);
308 /* Disassemble a big-endian or32 instruction. */
311 print_insn_big_or32 (bfd_vma memaddr, struct disassemble_info *info)
313 info->private_data = find_bytes_big;
315 return print_insn (memaddr, info);
318 /* Disassemble a little-endian or32 instruction. */
321 print_insn_little_or32 (bfd_vma memaddr, struct disassemble_info *info)
323 info->private_data = find_bytes_little;
324 return print_insn (memaddr, info);