Commit | Line | Data |
---|---|---|
a40cbfa3 NC |
1 | /* Disassembler interface for targets using CGEN. -*- C -*- |
2 | CGEN: Cpu tools GENerator | |
3 | ||
4 | THIS FILE IS MACHINE GENERATED WITH CGEN. | |
5 | - the resultant file is machine generated, cgen-dis.in isn't | |
6 | ||
bf143b25 | 7 | Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2005 |
98f70fc4 | 8 | Free Software Foundation, Inc. |
a40cbfa3 NC |
9 | |
10 | This file is part of the GNU Binutils and GDB, the GNU debugger. | |
11 | ||
12 | This program is free software; you can redistribute it and/or modify | |
13 | it under the terms of the GNU General Public License as published by | |
14 | the Free Software Foundation; either version 2, or (at your option) | |
15 | any later version. | |
16 | ||
17 | This program is distributed in the hope that it will be useful, | |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | GNU General Public License for more details. | |
21 | ||
22 | You should have received a copy of the GNU General Public License | |
23 | along with this program; if not, write to the Free Software Foundation, Inc., | |
f4321104 | 24 | 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
a40cbfa3 NC |
25 | |
26 | /* ??? Eventually more and more of this stuff can go to cpu-independent files. | |
27 | Keep that in mind. */ | |
28 | ||
29 | #include "sysdep.h" | |
30 | #include <stdio.h> | |
31 | #include "ansidecl.h" | |
32 | #include "dis-asm.h" | |
33 | #include "bfd.h" | |
34 | #include "symcat.h" | |
98f70fc4 | 35 | #include "libiberty.h" |
a40cbfa3 NC |
36 | #include "ip2k-desc.h" |
37 | #include "ip2k-opc.h" | |
38 | #include "opintl.h" | |
39 | ||
40 | /* Default text to print if an instruction isn't recognized. */ | |
41 | #define UNKNOWN_INSN_MSG _("*unknown*") | |
42 | ||
43 | static void print_normal | |
ffead7ae | 44 | (CGEN_CPU_DESC, void *, long, unsigned int, bfd_vma, int); |
a40cbfa3 | 45 | static void print_address |
bf143b25 | 46 | (CGEN_CPU_DESC, void *, bfd_vma, unsigned int, bfd_vma, int) ATTRIBUTE_UNUSED; |
a40cbfa3 | 47 | static void print_keyword |
bf143b25 | 48 | (CGEN_CPU_DESC, void *, CGEN_KEYWORD *, long, unsigned int) ATTRIBUTE_UNUSED; |
a40cbfa3 | 49 | static void print_insn_normal |
ffead7ae | 50 | (CGEN_CPU_DESC, void *, const CGEN_INSN *, CGEN_FIELDS *, bfd_vma, int); |
a40cbfa3 | 51 | static int print_insn |
33b71eeb | 52 | (CGEN_CPU_DESC, bfd_vma, disassemble_info *, bfd_byte *, unsigned); |
a40cbfa3 | 53 | static int default_print_insn |
bf143b25 | 54 | (CGEN_CPU_DESC, bfd_vma, disassemble_info *) ATTRIBUTE_UNUSED; |
a40cbfa3 | 55 | static int read_insn |
33b71eeb | 56 | (CGEN_CPU_DESC, bfd_vma, disassemble_info *, bfd_byte *, int, CGEN_EXTRACT_INFO *, |
ffead7ae | 57 | unsigned long *); |
a40cbfa3 NC |
58 | \f |
59 | /* -- disassembler routines inserted here */ | |
60 | ||
61 | /* -- dis.c */ | |
62 | ||
98f70fc4 AM |
63 | #define PRINT_FUNC_DECL(name) \ |
64 | static void name PARAMS ((CGEN_CPU_DESC, PTR, long, unsigned int, bfd_vma, int)) | |
65 | ||
66 | PRINT_FUNC_DECL (print_fr); | |
67 | PRINT_FUNC_DECL (print_dollarhex); | |
68 | PRINT_FUNC_DECL (print_dollarhex8); | |
98f70fc4 AM |
69 | PRINT_FUNC_DECL (print_dollarhex_addr16h); |
70 | PRINT_FUNC_DECL (print_dollarhex_addr16l); | |
71 | PRINT_FUNC_DECL (print_dollarhex_p); | |
72 | PRINT_FUNC_DECL (print_dollarhex_cj); | |
73 | PRINT_FUNC_DECL (print_decimal); | |
74 | ||
a40cbfa3 NC |
75 | static void |
76 | print_fr (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 77 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
78 | PTR dis_info; |
79 | long value; | |
98f70fc4 AM |
80 | unsigned int attrs ATTRIBUTE_UNUSED; |
81 | bfd_vma pc ATTRIBUTE_UNUSED; | |
82 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
83 | { |
84 | disassemble_info *info = (disassemble_info *) dis_info; | |
85 | const CGEN_KEYWORD_ENTRY *ke; | |
86 | extern CGEN_KEYWORD ip2k_cgen_opval_register_names; | |
87 | long offsettest; | |
88 | long offsetvalue; | |
89 | ||
90 | if ( value == 0 ) /* This is (IP) */ | |
98f70fc4 | 91 | { |
a40cbfa3 NC |
92 | (*info->fprintf_func) (info->stream, "%s", "(IP)"); |
93 | return; | |
98f70fc4 | 94 | } |
a40cbfa3 NC |
95 | |
96 | offsettest = value >> 7; | |
97 | offsetvalue = value & 0x7F; | |
98 | ||
99 | /* Check to see if first two bits are 10 -> (DP) */ | |
100 | if ( offsettest == 2 ) | |
98f70fc4 | 101 | { |
a40cbfa3 | 102 | if ( offsetvalue == 0 ) |
98f70fc4 | 103 | (*info->fprintf_func) (info->stream, "%s","(DP)"); |
a40cbfa3 | 104 | else |
98f70fc4 | 105 | (*info->fprintf_func) (info->stream, "$%x%s",offsetvalue, "(DP)"); |
a40cbfa3 | 106 | return; |
98f70fc4 | 107 | } |
a40cbfa3 NC |
108 | |
109 | /* Check to see if first two bits are 11 -> (SP) */ | |
110 | if ( offsettest == 3 ) | |
98f70fc4 | 111 | { |
a40cbfa3 | 112 | if ( offsetvalue == 0 ) |
98f70fc4 | 113 | (*info->fprintf_func) (info->stream, "%s", "(SP)"); |
a40cbfa3 | 114 | else |
98f70fc4 | 115 | (*info->fprintf_func) (info->stream, "$%x%s", offsetvalue,"(SP)"); |
a40cbfa3 | 116 | return; |
98f70fc4 | 117 | } |
a40cbfa3 NC |
118 | |
119 | /* Attempt to print as a register keyword. */ | |
120 | ke = cgen_keyword_lookup_value (& ip2k_cgen_opval_register_names, value); | |
121 | if (ke != NULL) | |
122 | { | |
123 | (*info->fprintf_func) (info->stream, "%s", ke->name); | |
124 | return; | |
125 | } | |
126 | ||
127 | /* Print as an address literal. */ | |
128 | (*info->fprintf_func) (info->stream, "$%02x", value); | |
129 | } | |
130 | ||
131 | static void | |
132 | print_dollarhex (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 133 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
134 | PTR dis_info; |
135 | long value; | |
98f70fc4 AM |
136 | unsigned int attrs ATTRIBUTE_UNUSED; |
137 | bfd_vma pc ATTRIBUTE_UNUSED; | |
138 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
139 | { |
140 | disassemble_info *info = (disassemble_info *) dis_info; | |
141 | ||
142 | (*info->fprintf_func) (info->stream, "$%x", value); | |
143 | } | |
144 | ||
145 | static void | |
146 | print_dollarhex8 (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 147 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
148 | PTR dis_info; |
149 | long value; | |
98f70fc4 AM |
150 | unsigned int attrs ATTRIBUTE_UNUSED; |
151 | bfd_vma pc ATTRIBUTE_UNUSED; | |
152 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
153 | { |
154 | disassemble_info *info = (disassemble_info *) dis_info; | |
155 | ||
156 | (*info->fprintf_func) (info->stream, "$%02x", value); | |
157 | } | |
158 | ||
a40cbfa3 NC |
159 | static void |
160 | print_dollarhex_addr16h (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 161 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
162 | PTR dis_info; |
163 | long value; | |
98f70fc4 AM |
164 | unsigned int attrs ATTRIBUTE_UNUSED; |
165 | bfd_vma pc ATTRIBUTE_UNUSED; | |
166 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
167 | { |
168 | disassemble_info *info = (disassemble_info *) dis_info; | |
169 | ||
170 | /* This is a loadh instruction. Shift the value to the left */ | |
171 | /* by 8 bits so that disassembled code will reassemble properly. */ | |
172 | value = ((value << 8) & 0xFF00); | |
173 | ||
174 | (*info->fprintf_func) (info->stream, "$%04x", value); | |
175 | } | |
176 | ||
177 | static void | |
178 | print_dollarhex_addr16l (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 179 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
180 | PTR dis_info; |
181 | long value; | |
98f70fc4 AM |
182 | unsigned int attrs ATTRIBUTE_UNUSED; |
183 | bfd_vma pc ATTRIBUTE_UNUSED; | |
184 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
185 | { |
186 | disassemble_info *info = (disassemble_info *) dis_info; | |
187 | ||
188 | (*info->fprintf_func) (info->stream, "$%04x", value); | |
189 | } | |
190 | ||
191 | static void | |
192 | print_dollarhex_p (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 193 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
194 | PTR dis_info; |
195 | long value; | |
98f70fc4 AM |
196 | unsigned int attrs ATTRIBUTE_UNUSED; |
197 | bfd_vma pc ATTRIBUTE_UNUSED; | |
198 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
199 | { |
200 | disassemble_info *info = (disassemble_info *) dis_info; | |
201 | ||
202 | value = ((value << 14) & 0x1C000); | |
203 | ;value = (value & 0x1FFFF); | |
204 | (*info->fprintf_func) (info->stream, "$%05x", value); | |
205 | } | |
206 | ||
207 | static void | |
208 | print_dollarhex_cj (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 209 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
210 | PTR dis_info; |
211 | long value; | |
98f70fc4 AM |
212 | unsigned int attrs ATTRIBUTE_UNUSED; |
213 | bfd_vma pc ATTRIBUTE_UNUSED; | |
214 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
215 | { |
216 | disassemble_info *info = (disassemble_info *) dis_info; | |
217 | ||
218 | value = ((value << 1) & 0x1FFFF); | |
219 | (*info->fprintf_func) (info->stream, "$%05x", value); | |
220 | } | |
221 | ||
222 | ||
223 | static void | |
224 | print_decimal (cd, dis_info, value, attrs, pc, length) | |
98f70fc4 | 225 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
a40cbfa3 NC |
226 | PTR dis_info; |
227 | long value; | |
98f70fc4 AM |
228 | unsigned int attrs ATTRIBUTE_UNUSED; |
229 | bfd_vma pc ATTRIBUTE_UNUSED; | |
230 | int length ATTRIBUTE_UNUSED; | |
a40cbfa3 NC |
231 | { |
232 | disassemble_info *info = (disassemble_info *) dis_info; | |
233 | ||
234 | (*info->fprintf_func) (info->stream, "%d", value); | |
235 | } | |
236 | ||
237 | ||
238 | ||
239 | /* -- */ | |
240 | ||
241 | void ip2k_cgen_print_operand | |
242 | PARAMS ((CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *, | |
243 | void const *, bfd_vma, int)); | |
244 | ||
245 | /* Main entry point for printing operands. | |
246 | XINFO is a `void *' and not a `disassemble_info *' to not put a requirement | |
247 | of dis-asm.h on cgen.h. | |
248 | ||
249 | This function is basically just a big switch statement. Earlier versions | |
250 | used tables to look up the function to use, but | |
251 | - if the table contains both assembler and disassembler functions then | |
252 | the disassembler contains much of the assembler and vice-versa, | |
253 | - there's a lot of inlining possibilities as things grow, | |
254 | - using a switch statement avoids the function call overhead. | |
255 | ||
256 | This function could be moved into `print_insn_normal', but keeping it | |
257 | separate makes clear the interface between `print_insn_normal' and each of | |
258 | the handlers. */ | |
259 | ||
260 | void | |
261 | ip2k_cgen_print_operand (cd, opindex, xinfo, fields, attrs, pc, length) | |
262 | CGEN_CPU_DESC cd; | |
263 | int opindex; | |
264 | PTR xinfo; | |
265 | CGEN_FIELDS *fields; | |
266 | void const *attrs ATTRIBUTE_UNUSED; | |
267 | bfd_vma pc; | |
268 | int length; | |
269 | { | |
270 | disassemble_info *info = (disassemble_info *) xinfo; | |
271 | ||
272 | switch (opindex) | |
273 | { | |
274 | case IP2K_OPERAND_ADDR16CJP : | |
275 | print_dollarhex_cj (cd, info, fields->f_addr16cjp, 0|(1<<CGEN_OPERAND_ABS_ADDR), pc, length); | |
276 | break; | |
277 | case IP2K_OPERAND_ADDR16H : | |
278 | print_dollarhex_addr16h (cd, info, fields->f_imm8, 0, pc, length); | |
279 | break; | |
280 | case IP2K_OPERAND_ADDR16L : | |
281 | print_dollarhex_addr16l (cd, info, fields->f_imm8, 0, pc, length); | |
282 | break; | |
283 | case IP2K_OPERAND_ADDR16P : | |
284 | print_dollarhex_p (cd, info, fields->f_page3, 0, pc, length); | |
285 | break; | |
286 | case IP2K_OPERAND_BITNO : | |
287 | print_decimal (cd, info, fields->f_bitno, 0, pc, length); | |
288 | break; | |
289 | case IP2K_OPERAND_CBIT : | |
290 | print_normal (cd, info, 0, 0, pc, length); | |
291 | break; | |
292 | case IP2K_OPERAND_DCBIT : | |
293 | print_normal (cd, info, 0, 0, pc, length); | |
294 | break; | |
295 | case IP2K_OPERAND_FR : | |
296 | print_fr (cd, info, fields->f_reg, 0|(1<<CGEN_OPERAND_ABS_ADDR), pc, length); | |
297 | break; | |
298 | case IP2K_OPERAND_LIT8 : | |
299 | print_dollarhex8 (cd, info, fields->f_imm8, 0|(1<<CGEN_OPERAND_SIGNED), pc, length); | |
300 | break; | |
301 | case IP2K_OPERAND_PABITS : | |
302 | print_normal (cd, info, 0, 0, pc, length); | |
303 | break; | |
304 | case IP2K_OPERAND_RETI3 : | |
305 | print_dollarhex (cd, info, fields->f_reti3, 0, pc, length); | |
306 | break; | |
307 | case IP2K_OPERAND_ZBIT : | |
308 | print_normal (cd, info, 0, 0, pc, length); | |
309 | break; | |
310 | ||
311 | default : | |
312 | /* xgettext:c-format */ | |
313 | fprintf (stderr, _("Unrecognized field %d while printing insn.\n"), | |
314 | opindex); | |
315 | abort (); | |
316 | } | |
317 | } | |
318 | ||
319 | cgen_print_fn * const ip2k_cgen_print_handlers[] = | |
320 | { | |
321 | print_insn_normal, | |
322 | }; | |
323 | ||
324 | ||
325 | void | |
326 | ip2k_cgen_init_dis (cd) | |
327 | CGEN_CPU_DESC cd; | |
328 | { | |
329 | ip2k_cgen_init_opcode_table (cd); | |
330 | ip2k_cgen_init_ibld_table (cd); | |
331 | cd->print_handlers = & ip2k_cgen_print_handlers[0]; | |
332 | cd->print_operand = ip2k_cgen_print_operand; | |
333 | } | |
334 | ||
335 | \f | |
336 | /* Default print handler. */ | |
337 | ||
338 | static void | |
ffead7ae MM |
339 | print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
340 | void *dis_info, | |
341 | long value, | |
342 | unsigned int attrs, | |
343 | bfd_vma pc ATTRIBUTE_UNUSED, | |
344 | int length ATTRIBUTE_UNUSED) | |
a40cbfa3 NC |
345 | { |
346 | disassemble_info *info = (disassemble_info *) dis_info; | |
347 | ||
348 | #ifdef CGEN_PRINT_NORMAL | |
349 | CGEN_PRINT_NORMAL (cd, info, value, attrs, pc, length); | |
350 | #endif | |
351 | ||
352 | /* Print the operand as directed by the attributes. */ | |
353 | if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY)) | |
354 | ; /* nothing to do */ | |
355 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED)) | |
356 | (*info->fprintf_func) (info->stream, "%ld", value); | |
357 | else | |
358 | (*info->fprintf_func) (info->stream, "0x%lx", value); | |
359 | } | |
360 | ||
361 | /* Default address handler. */ | |
362 | ||
363 | static void | |
ffead7ae MM |
364 | print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
365 | void *dis_info, | |
366 | bfd_vma value, | |
367 | unsigned int attrs, | |
368 | bfd_vma pc ATTRIBUTE_UNUSED, | |
369 | int length ATTRIBUTE_UNUSED) | |
a40cbfa3 NC |
370 | { |
371 | disassemble_info *info = (disassemble_info *) dis_info; | |
372 | ||
373 | #ifdef CGEN_PRINT_ADDRESS | |
374 | CGEN_PRINT_ADDRESS (cd, info, value, attrs, pc, length); | |
375 | #endif | |
376 | ||
377 | /* Print the operand as directed by the attributes. */ | |
378 | if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY)) | |
379 | ; /* nothing to do */ | |
380 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR)) | |
381 | (*info->print_address_func) (value, info); | |
382 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR)) | |
383 | (*info->print_address_func) (value, info); | |
384 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED)) | |
385 | (*info->fprintf_func) (info->stream, "%ld", (long) value); | |
386 | else | |
387 | (*info->fprintf_func) (info->stream, "0x%lx", (long) value); | |
388 | } | |
389 | ||
390 | /* Keyword print handler. */ | |
391 | ||
392 | static void | |
ffead7ae MM |
393 | print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
394 | void *dis_info, | |
395 | CGEN_KEYWORD *keyword_table, | |
396 | long value, | |
397 | unsigned int attrs ATTRIBUTE_UNUSED) | |
a40cbfa3 NC |
398 | { |
399 | disassemble_info *info = (disassemble_info *) dis_info; | |
400 | const CGEN_KEYWORD_ENTRY *ke; | |
401 | ||
402 | ke = cgen_keyword_lookup_value (keyword_table, value); | |
403 | if (ke != NULL) | |
404 | (*info->fprintf_func) (info->stream, "%s", ke->name); | |
405 | else | |
406 | (*info->fprintf_func) (info->stream, "???"); | |
407 | } | |
408 | \f | |
409 | /* Default insn printer. | |
410 | ||
ffead7ae | 411 | DIS_INFO is defined as `void *' so the disassembler needn't know anything |
a40cbfa3 NC |
412 | about disassemble_info. */ |
413 | ||
414 | static void | |
ffead7ae MM |
415 | print_insn_normal (CGEN_CPU_DESC cd, |
416 | void *dis_info, | |
417 | const CGEN_INSN *insn, | |
418 | CGEN_FIELDS *fields, | |
419 | bfd_vma pc, | |
420 | int length) | |
a40cbfa3 NC |
421 | { |
422 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); | |
423 | disassemble_info *info = (disassemble_info *) dis_info; | |
424 | const CGEN_SYNTAX_CHAR_TYPE *syn; | |
425 | ||
426 | CGEN_INIT_PRINT (cd); | |
427 | ||
428 | for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn) | |
429 | { | |
430 | if (CGEN_SYNTAX_MNEMONIC_P (*syn)) | |
431 | { | |
432 | (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn)); | |
433 | continue; | |
434 | } | |
435 | if (CGEN_SYNTAX_CHAR_P (*syn)) | |
436 | { | |
437 | (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn)); | |
438 | continue; | |
439 | } | |
440 | ||
441 | /* We have an operand. */ | |
442 | ip2k_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info, | |
443 | fields, CGEN_INSN_ATTRS (insn), pc, length); | |
444 | } | |
445 | } | |
446 | \f | |
447 | /* Subroutine of print_insn. Reads an insn into the given buffers and updates | |
448 | the extract info. | |
449 | Returns 0 if all is well, non-zero otherwise. */ | |
450 | ||
451 | static int | |
ffead7ae MM |
452 | read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
453 | bfd_vma pc, | |
454 | disassemble_info *info, | |
33b71eeb | 455 | bfd_byte *buf, |
ffead7ae MM |
456 | int buflen, |
457 | CGEN_EXTRACT_INFO *ex_info, | |
458 | unsigned long *insn_value) | |
a40cbfa3 NC |
459 | { |
460 | int status = (*info->read_memory_func) (pc, buf, buflen, info); | |
461 | if (status != 0) | |
462 | { | |
463 | (*info->memory_error_func) (status, pc, info); | |
464 | return -1; | |
465 | } | |
466 | ||
467 | ex_info->dis_info = info; | |
468 | ex_info->valid = (1 << buflen) - 1; | |
469 | ex_info->insn_bytes = buf; | |
470 | ||
471 | *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG); | |
472 | return 0; | |
473 | } | |
474 | ||
475 | /* Utility to print an insn. | |
476 | BUF is the base part of the insn, target byte order, BUFLEN bytes long. | |
477 | The result is the size of the insn in bytes or zero for an unknown insn | |
478 | or -1 if an error occurs fetching data (memory_error_func will have | |
479 | been called). */ | |
480 | ||
481 | static int | |
ffead7ae MM |
482 | print_insn (CGEN_CPU_DESC cd, |
483 | bfd_vma pc, | |
484 | disassemble_info *info, | |
33b71eeb | 485 | bfd_byte *buf, |
ffead7ae | 486 | unsigned int buflen) |
a40cbfa3 NC |
487 | { |
488 | CGEN_INSN_INT insn_value; | |
489 | const CGEN_INSN_LIST *insn_list; | |
490 | CGEN_EXTRACT_INFO ex_info; | |
491 | int basesize; | |
492 | ||
493 | /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */ | |
494 | basesize = cd->base_insn_bitsize < buflen * 8 ? | |
495 | cd->base_insn_bitsize : buflen * 8; | |
496 | insn_value = cgen_get_insn_value (cd, buf, basesize); | |
497 | ||
498 | ||
499 | /* Fill in ex_info fields like read_insn would. Don't actually call | |
500 | read_insn, since the incoming buffer is already read (and possibly | |
501 | modified a la m32r). */ | |
502 | ex_info.valid = (1 << buflen) - 1; | |
503 | ex_info.dis_info = info; | |
504 | ex_info.insn_bytes = buf; | |
505 | ||
506 | /* The instructions are stored in hash lists. | |
507 | Pick the first one and keep trying until we find the right one. */ | |
508 | ||
33b71eeb | 509 | insn_list = CGEN_DIS_LOOKUP_INSN (cd, (char *) buf, insn_value); |
a40cbfa3 NC |
510 | while (insn_list != NULL) |
511 | { | |
512 | const CGEN_INSN *insn = insn_list->insn; | |
513 | CGEN_FIELDS fields; | |
514 | int length; | |
515 | unsigned long insn_value_cropped; | |
516 | ||
517 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED | |
518 | /* Not needed as insn shouldn't be in hash lists if not supported. */ | |
519 | /* Supported by this cpu? */ | |
520 | if (! ip2k_cgen_insn_supported (cd, insn)) | |
521 | { | |
522 | insn_list = CGEN_DIS_NEXT_INSN (insn_list); | |
523 | continue; | |
524 | } | |
525 | #endif | |
526 | ||
527 | /* Basic bit mask must be correct. */ | |
528 | /* ??? May wish to allow target to defer this check until the extract | |
529 | handler. */ | |
530 | ||
531 | /* Base size may exceed this instruction's size. Extract the | |
532 | relevant part from the buffer. */ | |
533 | if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen && | |
534 | (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long)) | |
535 | insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn), | |
536 | info->endian == BFD_ENDIAN_BIG); | |
537 | else | |
538 | insn_value_cropped = insn_value; | |
539 | ||
540 | if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn)) | |
541 | == CGEN_INSN_BASE_VALUE (insn)) | |
542 | { | |
543 | /* Printing is handled in two passes. The first pass parses the | |
544 | machine insn and extracts the fields. The second pass prints | |
545 | them. */ | |
546 | ||
547 | /* Make sure the entire insn is loaded into insn_value, if it | |
548 | can fit. */ | |
549 | if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) && | |
550 | (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long)) | |
551 | { | |
552 | unsigned long full_insn_value; | |
553 | int rc = read_insn (cd, pc, info, buf, | |
554 | CGEN_INSN_BITSIZE (insn) / 8, | |
555 | & ex_info, & full_insn_value); | |
556 | if (rc != 0) | |
557 | return rc; | |
558 | length = CGEN_EXTRACT_FN (cd, insn) | |
559 | (cd, insn, &ex_info, full_insn_value, &fields, pc); | |
560 | } | |
561 | else | |
562 | length = CGEN_EXTRACT_FN (cd, insn) | |
563 | (cd, insn, &ex_info, insn_value_cropped, &fields, pc); | |
564 | ||
565 | /* length < 0 -> error */ | |
566 | if (length < 0) | |
567 | return length; | |
568 | if (length > 0) | |
569 | { | |
570 | CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length); | |
571 | /* length is in bits, result is in bytes */ | |
572 | return length / 8; | |
573 | } | |
574 | } | |
575 | ||
576 | insn_list = CGEN_DIS_NEXT_INSN (insn_list); | |
577 | } | |
578 | ||
579 | return 0; | |
580 | } | |
581 | ||
582 | /* Default value for CGEN_PRINT_INSN. | |
583 | The result is the size of the insn in bytes or zero for an unknown insn | |
584 | or -1 if an error occured fetching bytes. */ | |
585 | ||
586 | #ifndef CGEN_PRINT_INSN | |
587 | #define CGEN_PRINT_INSN default_print_insn | |
588 | #endif | |
589 | ||
590 | static int | |
ffead7ae | 591 | default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info) |
a40cbfa3 | 592 | { |
33b71eeb | 593 | bfd_byte buf[CGEN_MAX_INSN_SIZE]; |
a40cbfa3 NC |
594 | int buflen; |
595 | int status; | |
596 | ||
597 | /* Attempt to read the base part of the insn. */ | |
598 | buflen = cd->base_insn_bitsize / 8; | |
599 | status = (*info->read_memory_func) (pc, buf, buflen, info); | |
600 | ||
601 | /* Try again with the minimum part, if min < base. */ | |
602 | if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize)) | |
603 | { | |
604 | buflen = cd->min_insn_bitsize / 8; | |
605 | status = (*info->read_memory_func) (pc, buf, buflen, info); | |
606 | } | |
607 | ||
608 | if (status != 0) | |
609 | { | |
610 | (*info->memory_error_func) (status, pc, info); | |
611 | return -1; | |
612 | } | |
613 | ||
614 | return print_insn (cd, pc, info, buf, buflen); | |
615 | } | |
616 | ||
617 | /* Main entry point. | |
618 | Print one instruction from PC on INFO->STREAM. | |
619 | Return the size of the instruction (in bytes). */ | |
620 | ||
621 | typedef struct cpu_desc_list { | |
622 | struct cpu_desc_list *next; | |
623 | int isa; | |
624 | int mach; | |
625 | int endian; | |
626 | CGEN_CPU_DESC cd; | |
627 | } cpu_desc_list; | |
628 | ||
629 | int | |
ffead7ae | 630 | print_insn_ip2k (bfd_vma pc, disassemble_info *info) |
a40cbfa3 NC |
631 | { |
632 | static cpu_desc_list *cd_list = 0; | |
633 | cpu_desc_list *cl = 0; | |
634 | static CGEN_CPU_DESC cd = 0; | |
635 | static int prev_isa; | |
636 | static int prev_mach; | |
637 | static int prev_endian; | |
638 | int length; | |
639 | int isa,mach; | |
640 | int endian = (info->endian == BFD_ENDIAN_BIG | |
641 | ? CGEN_ENDIAN_BIG | |
642 | : CGEN_ENDIAN_LITTLE); | |
643 | enum bfd_architecture arch; | |
644 | ||
645 | /* ??? gdb will set mach but leave the architecture as "unknown" */ | |
646 | #ifndef CGEN_BFD_ARCH | |
647 | #define CGEN_BFD_ARCH bfd_arch_ip2k | |
648 | #endif | |
649 | arch = info->arch; | |
650 | if (arch == bfd_arch_unknown) | |
651 | arch = CGEN_BFD_ARCH; | |
652 | ||
653 | /* There's no standard way to compute the machine or isa number | |
654 | so we leave it to the target. */ | |
655 | #ifdef CGEN_COMPUTE_MACH | |
656 | mach = CGEN_COMPUTE_MACH (info); | |
657 | #else | |
658 | mach = info->mach; | |
659 | #endif | |
660 | ||
661 | #ifdef CGEN_COMPUTE_ISA | |
662 | isa = CGEN_COMPUTE_ISA (info); | |
663 | #else | |
664 | isa = info->insn_sets; | |
665 | #endif | |
666 | ||
667 | /* If we've switched cpu's, try to find a handle we've used before */ | |
668 | if (cd | |
669 | && (isa != prev_isa | |
670 | || mach != prev_mach | |
671 | || endian != prev_endian)) | |
672 | { | |
673 | cd = 0; | |
674 | for (cl = cd_list; cl; cl = cl->next) | |
675 | { | |
676 | if (cl->isa == isa && | |
677 | cl->mach == mach && | |
678 | cl->endian == endian) | |
679 | { | |
680 | cd = cl->cd; | |
681 | break; | |
682 | } | |
683 | } | |
684 | } | |
685 | ||
686 | /* If we haven't initialized yet, initialize the opcode table. */ | |
687 | if (! cd) | |
688 | { | |
689 | const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach); | |
690 | const char *mach_name; | |
691 | ||
692 | if (!arch_type) | |
693 | abort (); | |
694 | mach_name = arch_type->printable_name; | |
695 | ||
696 | prev_isa = isa; | |
697 | prev_mach = mach; | |
698 | prev_endian = endian; | |
699 | cd = ip2k_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa, | |
700 | CGEN_CPU_OPEN_BFDMACH, mach_name, | |
701 | CGEN_CPU_OPEN_ENDIAN, prev_endian, | |
702 | CGEN_CPU_OPEN_END); | |
703 | if (!cd) | |
704 | abort (); | |
705 | ||
706 | /* save this away for future reference */ | |
707 | cl = xmalloc (sizeof (struct cpu_desc_list)); | |
708 | cl->cd = cd; | |
709 | cl->isa = isa; | |
710 | cl->mach = mach; | |
711 | cl->endian = endian; | |
712 | cl->next = cd_list; | |
713 | cd_list = cl; | |
714 | ||
715 | ip2k_cgen_init_dis (cd); | |
716 | } | |
717 | ||
718 | /* We try to have as much common code as possible. | |
719 | But at this point some targets need to take over. */ | |
720 | /* ??? Some targets may need a hook elsewhere. Try to avoid this, | |
721 | but if not possible try to move this hook elsewhere rather than | |
722 | have two hooks. */ | |
723 | length = CGEN_PRINT_INSN (cd, pc, info); | |
724 | if (length > 0) | |
725 | return length; | |
726 | if (length < 0) | |
727 | return -1; | |
728 | ||
729 | (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG); | |
730 | return cd->default_insn_bitsize / 8; | |
731 | } |