* elf64-ppc.c (ppc64_elf_edit_opd): When deleting relocs, adjust
[binutils.git] / opcodes / or32-dis.c
blobd5f467944d23c1cf7fbd7907e359c702da6c4ca3
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. */
22 #define DEBUG 0
24 #include "dis-asm.h"
25 #include "opcode/or32.h"
26 #include "safe-ctype.h"
27 #include <string.h>
28 #include <stdlib.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. */
42 static void
43 find_bytes_big (insn_ch, insn)
44 unsigned char *insn_ch;
45 unsigned long *insn;
47 *insn =
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]);
52 #if DEBUG
53 printf ("find_bytes_big3: %x\n", *insn);
54 #endif
57 static void
58 find_bytes_little (insn_ch, insn)
59 unsigned char *insn_ch;
60 unsigned long *insn;
62 *insn =
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 *));
72 static unsigned long
73 or32_extract (param_ch, enc_initial, insn)
74 char param_ch;
75 char *enc_initial;
76 unsigned long insn;
78 char *enc;
79 unsigned long ret = 0;
80 int opc_pos = 0;
81 int param_pos = 0;
83 for (enc = enc_initial; *enc != '\0'; enc++)
84 if (*enc == param_ch)
86 if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x'))
87 continue;
88 else
89 param_pos++;
92 #if DEBUG
93 printf ("or32_extract: %c %x ", param_ch, param_pos);
94 #endif
95 opc_pos = 32;
97 for (enc = enc_initial; *enc != '\0'; )
98 if ((*enc == '0') && (*(enc + 1) == 'x'))
100 opc_pos -= 4;
102 if ((param_ch == '0') || (param_ch == '1'))
104 unsigned long tmp = strtoul (enc, NULL, 16);
105 #if DEBUG
106 printf (" enc=%s, tmp=%x ", enc, tmp);
107 #endif
108 if (param_ch == '0')
109 tmp = 15 - tmp;
110 ret |= tmp << opc_pos;
112 enc += 3;
114 else if ((*enc == '0') || (*enc == '1'))
116 opc_pos--;
117 if (param_ch == *enc)
118 ret |= 1 << opc_pos;
119 enc++;
121 else if (*enc == param_ch)
123 opc_pos--;
124 param_pos--;
125 #if DEBUG
126 printf ("\n ret=%x opc_pos=%x, param_pos=%x\n", ret, opc_pos, param_pos);
127 #endif
128 ret += ((insn >> opc_pos) & 0x1) << param_pos;
130 if (!param_pos
131 && letter_signed (param_ch)
132 && ret >> (letter_range (param_ch) - 1))
134 #if DEBUG
135 printf ("\n ret=%x opc_pos=%x, param_pos=%x\n",
136 ret, opc_pos, param_pos);
137 #endif
138 ret |= 0xffffffff << letter_range(param_ch);
139 #if DEBUG
140 printf ("\n after conversion to signed: ret=%x\n", ret);
141 #endif
143 enc++;
145 else if (ISALPHA (*enc))
147 opc_pos--;
148 enc++;
150 else if (*enc == '-')
152 opc_pos--;
153 enc++;
155 else
156 enc++;
158 #if DEBUG
159 printf ("ret=%x\n", ret);
160 #endif
161 return ret;
164 static int
165 or32_opcode_match (insn, encoding)
166 unsigned long insn;
167 char *encoding;
169 unsigned long ones, zeros;
171 #if DEBUG
172 printf ("or32_opcode_match: %.8lx\n", insn);
173 #endif
174 ones = or32_extract ('1', encoding, insn);
175 zeros = or32_extract ('0', encoding, insn);
177 #if DEBUG
178 printf ("ones: %x \n", ones);
179 printf ("zeros: %x \n", zeros);
180 #endif
181 if ((insn & ones) != ones)
183 #if DEBUG
184 printf ("ret1\n");
185 #endif
186 return 0;
189 if ((~insn & zeros) != zeros)
191 #if DEBUG
192 printf ("ret2\n");
193 #endif
194 return 0;
197 #if DEBUG
198 printf ("ret3\n");
199 #endif
200 return 1;
203 /* Print register to INFO->STREAM. Used only by print_insn. */
205 static void
206 or32_print_register (param_ch, encoding, insn, info)
207 char param_ch;
208 char *encoding;
209 unsigned long insn;
210 struct disassemble_info *info;
212 int regnum = or32_extract (param_ch, encoding, insn);
214 #if DEBUG
215 printf ("or32_print_register: %c, %s, %x\n", param_ch, encoding, insn);
216 #endif
217 if (param_ch == 'A')
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);
227 else
228 (*info->fprintf_func) (info->stream, "X%d", regnum);
231 /* Print immediate to INFO->STREAM. Used only by print_insn. */
233 static void
234 or32_print_immediate (param_ch, encoding, insn, info)
235 char param_ch;
236 char *encoding;
237 unsigned long insn;
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); */
245 else
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). */
252 static int
253 print_insn (memaddr, info)
254 bfd_vma memaddr;
255 struct disassemble_info *info;
257 /* The raw instruction. */
258 unsigned char insn_ch[4];
259 /* Address. Will be sign extened 27-bit. */
260 unsigned long addr;
261 /* The four bytes of the instruction. */
262 unsigned long insn;
263 find_byte_func_type find_byte_func = (find_byte_func_type)info->private_data;
264 struct or32_opcode const * opcode;
267 int status =
268 (*info->read_memory_func) (memaddr, (bfd_byte *) &insn_ch[0], 4, info);
270 if (status != 0)
272 (*info->memory_error_func) (status, memaddr, info);
273 return -1;
277 (*find_byte_func) (&insn_ch[0], &insn);
279 for (opcode = &or32_opcodes[0];
280 opcode < &or32_opcodes[or32_num_opcodes];
281 ++opcode)
283 if (or32_opcode_match (insn, opcode->encoding))
285 char *s;
287 (*info->fprintf_func) (info->stream, "%s ", opcode->name);
289 for (s = opcode->args; *s != '\0'; ++s)
291 switch (*s)
293 case '\0':
294 return 4;
296 case 'r':
297 or32_print_register (*++s, opcode->encoding, insn, info);
298 break;
300 case 'X':
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)
307 (addr, info);
308 break;
310 default:
311 if (strchr (opcode->encoding, *s))
312 or32_print_immediate (*s, opcode->encoding, insn, info);
313 else
314 (*info->fprintf_func) (info->stream, "%c", *s);
318 return 4;
322 /* This used to be %8x for binutils. */
323 (*info->fprintf_func)
324 (info->stream, ".word 0x%08x", insn);
325 return 4;
328 /* Disassemble a big-endian or32 instruction. */
331 print_insn_big_or32 (memaddr, info)
332 bfd_vma memaddr;
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)
343 bfd_vma memaddr;
344 struct disassemble_info *info;
346 info->private_data = (PTR) find_bytes_little;
347 return print_insn (memaddr, info);