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"
29 #define EXTEND29(x) ((x) & 0x10000000 ? ((x) | 0xf0000000) : ((x)))
31 static void find_bytes_big PARAMS ((unsigned char *, unsigned long *));
32 static void find_bytes_little PARAMS ((unsigned char *, unsigned long *));
33 static unsigned long or32_extract PARAMS ((char, char *, unsigned long));
34 static int or32_opcode_match PARAMS ((unsigned long, char *));
35 static void or32_print_register PARAMS ((char, char *, unsigned long, struct disassemble_info *));
36 static void or32_print_immediate PARAMS ((char, char *, unsigned long, struct disassemble_info *));
37 static int print_insn PARAMS ((bfd_vma, struct disassemble_info *));
39 /* Now find the four bytes of INSN_CH and put them in *INSN. */
42 find_bytes_big (insn_ch, insn)
43 unsigned char *insn_ch;
47 ((unsigned long) insn_ch[0] << 24) +
48 ((unsigned long) insn_ch[1] << 16) +
49 ((unsigned long) insn_ch[2] << 8) +
50 ((unsigned long) insn_ch[3]);
52 printf ("find_bytes_big3: %x\n", *insn);
57 find_bytes_little (insn_ch, insn)
58 unsigned char *insn_ch;
62 ((unsigned long) insn_ch[3] << 24) +
63 ((unsigned long) insn_ch[2] << 16) +
64 ((unsigned long) insn_ch[1] << 8) +
65 ((unsigned long) insn_ch[0]);
68 typedef void (*find_byte_func_type)
69 PARAMS ((unsigned char *, unsigned long *));
72 or32_extract (param_ch, enc_initial, insn)
78 unsigned long ret = 0;
82 for (enc = enc_initial; *enc != '\0'; enc++)
84 if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
90 printf ("or32_extract: %c %x ", param_ch, param_pos);
94 for (enc = enc_initial; *enc != '\0'; )
95 if ((*enc == '0') && (*(enc + 1) == 'x'))
99 if ((param_ch == '0') || (param_ch == '1'))
101 unsigned long tmp = strtol (enc, NULL, 16);
103 printf (" enc=%s, tmp=%x ", enc, tmp);
107 ret |= tmp << opc_pos;
111 else if ((*enc == '0') || (*enc == '1'))
114 if (param_ch == *enc)
118 else if (*enc == param_ch)
123 printf ("\n ret=%x opc_pos=%x, param_pos=%x\n", ret, opc_pos, param_pos);
125 ret += ((insn >> opc_pos) & 0x1) << param_pos;
128 && letter_signed (param_ch)
129 && ret >> letter_range (param_ch) - 1)
132 printf ("\n ret=%x opc_pos=%x, param_pos=%x\n",
133 ret, opc_pos, param_pos);
135 ret |= 0xffffffff << letter_range(param_ch);
137 printf ("\n after conversion to signed: ret=%x\n", ret);
142 else if (ISALPHA (*enc))
147 else if (*enc == '-')
156 printf ("ret=%x\n", ret);
162 or32_opcode_match (insn, encoding)
166 unsigned long ones, zeros;
169 printf ("or32_opcode_match: %.8lx\n", insn);
171 ones = or32_extract ('1', encoding, insn);
172 zeros = or32_extract ('0', encoding, insn);
175 printf ("ones: %x \n", ones);
176 printf ("zeros: %x \n", zeros);
178 if ((insn & ones) != ones)
186 if ((~insn & zeros) != zeros)
200 /* Print register to INFO->STREAM. Used only by print_insn. */
203 or32_print_register (param_ch, encoding, insn, info)
207 struct disassemble_info *info;
209 int regnum = or32_extract (param_ch, encoding, insn);
212 printf ("or32_print_register: %c, %s, %x\n", param_ch, encoding, insn);
215 (*info->fprintf_func) (info->stream, "r%d", regnum);
216 else if (param_ch == 'B')
217 (*info->fprintf_func) (info->stream, "r%d", regnum);
218 else if (param_ch == 'D')
219 (*info->fprintf_func) (info->stream, "r%d", regnum);
220 else if (regnum < 16)
221 (*info->fprintf_func) (info->stream, "r%d", regnum);
222 else if (regnum < 32)
223 (*info->fprintf_func) (info->stream, "r%d", regnum-16);
225 (*info->fprintf_func) (info->stream, "X%d", regnum);
228 /* Print immediate to INFO->STREAM. Used only by print_insn. */
231 or32_print_immediate (param_ch, encoding, insn, info)
235 struct disassemble_info *info;
237 int imm = or32_extract(param_ch, encoding, insn);
239 if (letter_signed(param_ch))
240 (*info->fprintf_func) (info->stream, "0x%x", imm);
241 /* (*info->fprintf_func) (info->stream, "%d", imm); */
243 (*info->fprintf_func) (info->stream, "0x%x", imm);
246 /* Print one instruction from MEMADDR on INFO->STREAM.
247 Return the size of the instruction (always 4 on or32). */
250 print_insn (memaddr, info)
252 struct disassemble_info *info;
254 /* The raw instruction. */
255 unsigned char insn_ch[4];
256 /* Address. Will be sign extened 27-bit. */
258 /* The four bytes of the instruction. */
260 find_byte_func_type find_byte_func = (find_byte_func_type)info->private_data;
261 struct or32_opcode CONST * opcode;
265 (*info->read_memory_func) (memaddr, (bfd_byte *) &insn_ch[0], 4, info);
269 (*info->memory_error_func) (status, memaddr, info);
274 (*find_byte_func) (&insn_ch[0], &insn);
276 for (opcode = &or32_opcodes[0];
277 opcode < &or32_opcodes[or32_num_opcodes];
280 if (or32_opcode_match (insn, opcode->encoding))
284 (*info->fprintf_func) (info->stream, "%s ", opcode->name);
286 for (s = opcode->args; *s != '\0'; ++s)
294 or32_print_register (*++s, opcode->encoding, insn, info);
298 addr = or32_extract ('X', opcode->encoding, insn) << 2;
300 /* Calulate the correct address. XXX is this really correct ?? */
301 addr = memaddr + EXTEND29 (addr);
303 (*info->print_address_func)
308 if (strchr (opcode->encoding, *s))
309 or32_print_immediate (*s, opcode->encoding, insn, info);
311 (*info->fprintf_func) (info->stream, "%c", *s);
319 /* This used to be %8x for binutils. */
320 (*info->fprintf_func)
321 (info->stream, ".word 0x%08x", insn);
325 /* Disassemble a big-endian or32 instruction. */
328 print_insn_big_or32 (memaddr, info)
330 struct disassemble_info *info;
332 info->private_data = (PTR) find_bytes_big;
333 return print_insn (memaddr, info);
336 /* Disassemble a little-endian or32 instruction. */
339 print_insn_little_or32 (memaddr, info)
341 struct disassemble_info *info;
343 info->private_data = (PTR) find_bytes_little;
344 return print_insn (memaddr, info);