Commit | Line | Data |
---|---|---|
3b16e843 NC |
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. | |
5 | ||
6 | This file is part of Binutils. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. */ | |
21 | ||
22 | #define DEBUG 0 | |
23 | ||
24 | #include "dis-asm.h" | |
25 | #include "opcode/or32.h" | |
26 | #include "safe-ctype.h" | |
27 | #include <string.h> | |
5e37cc46 | 28 | #include <stdlib.h> |
3b16e843 | 29 | |
5e37cc46 | 30 | #define EXTEND29(x) ((x) & (unsigned long) 0x10000000 ? ((x) | (unsigned long) 0xf0000000) : ((x))) |
3b16e843 NC |
31 | |
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 *)); | |
39 | ||
40 | /* Now find the four bytes of INSN_CH and put them in *INSN. */ | |
41 | ||
42 | static void | |
43 | find_bytes_big (insn_ch, insn) | |
44 | unsigned char *insn_ch; | |
45 | unsigned long *insn; | |
46 | { | |
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 | |
55 | } | |
56 | ||
57 | static void | |
58 | find_bytes_little (insn_ch, insn) | |
59 | unsigned char *insn_ch; | |
60 | unsigned long *insn; | |
61 | { | |
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]); | |
67 | } | |
68 | ||
69 | typedef void (*find_byte_func_type) | |
70 | PARAMS ((unsigned char *, unsigned long *)); | |
71 | ||
72 | static unsigned long | |
73 | or32_extract (param_ch, enc_initial, insn) | |
74 | char param_ch; | |
75 | char *enc_initial; | |
76 | unsigned long insn; | |
77 | { | |
78 | char *enc; | |
79 | unsigned long ret = 0; | |
80 | int opc_pos = 0; | |
81 | int param_pos = 0; | |
82 | ||
83 | for (enc = enc_initial; *enc != '\0'; enc++) | |
84 | if (*enc == param_ch) | |
5e37cc46 NC |
85 | { |
86 | if (enc - 2 >= enc_initial && (*(enc - 2) == '0') && (*(enc - 1) == 'x')) | |
87 | continue; | |
88 | else | |
89 | param_pos++; | |
90 | } | |
3b16e843 NC |
91 | |
92 | #if DEBUG | |
93 | printf ("or32_extract: %c %x ", param_ch, param_pos); | |
94 | #endif | |
95 | opc_pos = 32; | |
96 | ||
97 | for (enc = enc_initial; *enc != '\0'; ) | |
98 | if ((*enc == '0') && (*(enc + 1) == 'x')) | |
99 | { | |
100 | opc_pos -= 4; | |
101 | ||
102 | if ((param_ch == '0') || (param_ch == '1')) | |
103 | { | |
5e37cc46 | 104 | unsigned long tmp = strtoul (enc, NULL, 16); |
3b16e843 NC |
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; | |
111 | } | |
112 | enc += 3; | |
113 | } | |
114 | else if ((*enc == '0') || (*enc == '1')) | |
115 | { | |
116 | opc_pos--; | |
117 | if (param_ch == *enc) | |
118 | ret |= 1 << opc_pos; | |
119 | enc++; | |
120 | } | |
121 | else if (*enc == param_ch) | |
122 | { | |
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; | |
129 | ||
130 | if (!param_pos | |
131 | && letter_signed (param_ch) | |
5e37cc46 | 132 | && ret >> (letter_range (param_ch) - 1)) |
3b16e843 NC |
133 | { |
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 | |
142 | } | |
143 | enc++; | |
144 | } | |
145 | else if (ISALPHA (*enc)) | |
146 | { | |
147 | opc_pos--; | |
148 | enc++; | |
149 | } | |
150 | else if (*enc == '-') | |
151 | { | |
152 | opc_pos--; | |
153 | enc++; | |
154 | } | |
155 | else | |
156 | enc++; | |
157 | ||
158 | #if DEBUG | |
159 | printf ("ret=%x\n", ret); | |
160 | #endif | |
161 | return ret; | |
162 | } | |
163 | ||
164 | static int | |
165 | or32_opcode_match (insn, encoding) | |
166 | unsigned long insn; | |
167 | char *encoding; | |
168 | { | |
169 | unsigned long ones, zeros; | |
170 | ||
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); | |
176 | ||
177 | #if DEBUG | |
178 | printf ("ones: %x \n", ones); | |
179 | printf ("zeros: %x \n", zeros); | |
180 | #endif | |
181 | if ((insn & ones) != ones) | |
182 | { | |
183 | #if DEBUG | |
184 | printf ("ret1\n"); | |
185 | #endif | |
186 | return 0; | |
187 | } | |
188 | ||
189 | if ((~insn & zeros) != zeros) | |
190 | { | |
191 | #if DEBUG | |
192 | printf ("ret2\n"); | |
193 | #endif | |
194 | return 0; | |
195 | } | |
196 | ||
197 | #if DEBUG | |
198 | printf ("ret3\n"); | |
199 | #endif | |
200 | return 1; | |
201 | } | |
202 | ||
203 | /* Print register to INFO->STREAM. Used only by print_insn. */ | |
204 | ||
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; | |
211 | { | |
212 | int regnum = or32_extract (param_ch, encoding, insn); | |
213 | ||
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); | |
229 | } | |
230 | ||
231 | /* Print immediate to INFO->STREAM. Used only by print_insn. */ | |
232 | ||
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; | |
239 | { | |
240 | int imm = or32_extract(param_ch, encoding, insn); | |
241 | ||
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); | |
247 | } | |
248 | ||
249 | /* Print one instruction from MEMADDR on INFO->STREAM. | |
250 | Return the size of the instruction (always 4 on or32). */ | |
251 | ||
252 | static int | |
253 | print_insn (memaddr, info) | |
254 | bfd_vma memaddr; | |
255 | struct disassemble_info *info; | |
256 | { | |
257 | /* The raw instruction. */ | |
258 | unsigned char insn_ch[4]; | |
259 | /* Address. Will be sign extened 27-bit. */ | |
5e37cc46 | 260 | unsigned long addr; |
3b16e843 NC |
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; | |
56da5fed | 264 | struct or32_opcode const * opcode; |
3b16e843 NC |
265 | |
266 | { | |
267 | int status = | |
268 | (*info->read_memory_func) (memaddr, (bfd_byte *) &insn_ch[0], 4, info); | |
269 | ||
270 | if (status != 0) | |
271 | { | |
272 | (*info->memory_error_func) (status, memaddr, info); | |
273 | return -1; | |
274 | } | |
275 | } | |
276 | ||
277 | (*find_byte_func) (&insn_ch[0], &insn); | |
278 | ||
279 | for (opcode = &or32_opcodes[0]; | |
280 | opcode < &or32_opcodes[or32_num_opcodes]; | |
281 | ++opcode) | |
282 | { | |
283 | if (or32_opcode_match (insn, opcode->encoding)) | |
284 | { | |
285 | char *s; | |
286 | ||
287 | (*info->fprintf_func) (info->stream, "%s ", opcode->name); | |
288 | ||
289 | for (s = opcode->args; *s != '\0'; ++s) | |
290 | { | |
291 | switch (*s) | |
292 | { | |
293 | case '\0': | |
294 | return 4; | |
295 | ||
296 | case 'r': | |
297 | or32_print_register (*++s, opcode->encoding, insn, info); | |
298 | break; | |
299 | ||
300 | case 'X': | |
301 | addr = or32_extract ('X', opcode->encoding, insn) << 2; | |
302 | ||
303 | /* Calulate the correct address. XXX is this really correct ?? */ | |
304 | addr = memaddr + EXTEND29 (addr); | |
305 | ||
306 | (*info->print_address_func) | |
307 | (addr, info); | |
308 | break; | |
309 | ||
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); | |
315 | } | |
316 | } | |
317 | ||
318 | return 4; | |
319 | } | |
320 | } | |
321 | ||
322 | /* This used to be %8x for binutils. */ | |
323 | (*info->fprintf_func) | |
324 | (info->stream, ".word 0x%08x", insn); | |
325 | return 4; | |
326 | } | |
327 | ||
328 | /* Disassemble a big-endian or32 instruction. */ | |
329 | ||
330 | int | |
331 | print_insn_big_or32 (memaddr, info) | |
332 | bfd_vma memaddr; | |
333 | struct disassemble_info *info; | |
334 | { | |
335 | info->private_data = (PTR) find_bytes_big; | |
336 | return print_insn (memaddr, info); | |
337 | } | |
338 | ||
339 | /* Disassemble a little-endian or32 instruction. */ | |
340 | ||
341 | int | |
342 | print_insn_little_or32 (memaddr, info) | |
343 | bfd_vma memaddr; | |
344 | struct disassemble_info *info; | |
345 | { | |
346 | info->private_data = (PTR) find_bytes_little; | |
347 | return print_insn (memaddr, info); | |
348 | } |