Commit | Line | Data |
---|---|---|
252b5132 RH |
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 | ||
98f70fc4 AM |
7 | Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 |
8 | Free Software Foundation, Inc. | |
252b5132 RH |
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., | |
24 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
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" |
252b5132 RH |
36 | #include "m32r-desc.h" |
37 | #include "m32r-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); |
252b5132 | 45 | static void print_address |
ffead7ae | 46 | (CGEN_CPU_DESC, void *, bfd_vma, unsigned int, bfd_vma, int); |
252b5132 | 47 | static void print_keyword |
ffead7ae | 48 | (CGEN_CPU_DESC, void *, CGEN_KEYWORD *, long, unsigned int); |
252b5132 | 49 | static void print_insn_normal |
ffead7ae | 50 | (CGEN_CPU_DESC, void *, const CGEN_INSN *, CGEN_FIELDS *, bfd_vma, int); |
fc05c67f | 51 | static int print_insn |
ffead7ae | 52 | (CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, unsigned); |
0e2ee3ca | 53 | static int default_print_insn |
ffead7ae | 54 | (CGEN_CPU_DESC, bfd_vma, disassemble_info *); |
fc05c67f | 55 | static int read_insn |
ffead7ae MM |
56 | (CGEN_CPU_DESC, bfd_vma, disassemble_info *, char *, int, CGEN_EXTRACT_INFO *, |
57 | unsigned long *); | |
252b5132 RH |
58 | \f |
59 | /* -- disassembler routines inserted here */ | |
60 | ||
61 | /* -- dis.c */ | |
0e2ee3ca NC |
62 | static void print_hash PARAMS ((CGEN_CPU_DESC, PTR, long, unsigned, bfd_vma, int)); |
63 | static int my_print_insn PARAMS ((CGEN_CPU_DESC, bfd_vma, disassemble_info *)); | |
252b5132 RH |
64 | |
65 | /* Immediate values are prefixed with '#'. */ | |
66 | ||
0e2ee3ca NC |
67 | #define CGEN_PRINT_NORMAL(cd, info, value, attrs, pc, length) \ |
68 | do \ | |
69 | { \ | |
70 | if (CGEN_BOOL_ATTR ((attrs), CGEN_OPERAND_HASH_PREFIX)) \ | |
71 | (*info->fprintf_func) (info->stream, "#"); \ | |
72 | } \ | |
73 | while (0) | |
252b5132 RH |
74 | |
75 | /* Handle '#' prefixes as operands. */ | |
76 | ||
77 | static void | |
78 | print_hash (cd, dis_info, value, attrs, pc, length) | |
9a2e995d | 79 | CGEN_CPU_DESC cd ATTRIBUTE_UNUSED; |
252b5132 | 80 | PTR dis_info; |
9a2e995d GH |
81 | long value ATTRIBUTE_UNUSED; |
82 | unsigned int attrs ATTRIBUTE_UNUSED; | |
83 | bfd_vma pc ATTRIBUTE_UNUSED; | |
84 | int length ATTRIBUTE_UNUSED; | |
252b5132 RH |
85 | { |
86 | disassemble_info *info = (disassemble_info *) dis_info; | |
87 | (*info->fprintf_func) (info->stream, "#"); | |
88 | } | |
89 | ||
0e2ee3ca | 90 | #undef CGEN_PRINT_INSN |
252b5132 RH |
91 | #define CGEN_PRINT_INSN my_print_insn |
92 | ||
93 | static int | |
94 | my_print_insn (cd, pc, info) | |
95 | CGEN_CPU_DESC cd; | |
96 | bfd_vma pc; | |
97 | disassemble_info *info; | |
98 | { | |
99 | char buffer[CGEN_MAX_INSN_SIZE]; | |
100 | char *buf = buffer; | |
101 | int status; | |
102 | int buflen = (pc & 3) == 0 ? 4 : 2; | |
88845958 NC |
103 | int big_p = CGEN_CPU_INSN_ENDIAN (cd) == CGEN_ENDIAN_BIG; |
104 | char *x; | |
252b5132 RH |
105 | |
106 | /* Read the base part of the insn. */ | |
107 | ||
44d86481 BE |
108 | status = (*info->read_memory_func) (pc - ((!big_p && (pc & 3) != 0) ? 2 : 0), |
109 | buf, buflen, info); | |
252b5132 RH |
110 | if (status != 0) |
111 | { | |
112 | (*info->memory_error_func) (status, pc, info); | |
113 | return -1; | |
114 | } | |
115 | ||
116 | /* 32 bit insn? */ | |
88845958 NC |
117 | x = (big_p ? &buf[0] : &buf[3]); |
118 | if ((pc & 3) == 0 && (*x & 0x80) != 0) | |
252b5132 RH |
119 | return print_insn (cd, pc, info, buf, buflen); |
120 | ||
121 | /* Print the first insn. */ | |
122 | if ((pc & 3) == 0) | |
123 | { | |
44d86481 | 124 | buf += (big_p ? 0 : 2); |
252b5132 RH |
125 | if (print_insn (cd, pc, info, buf, 2) == 0) |
126 | (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG); | |
44d86481 | 127 | buf += (big_p ? 2 : -2); |
252b5132 RH |
128 | } |
129 | ||
88845958 NC |
130 | x = (big_p ? &buf[0] : &buf[1]); |
131 | if (*x & 0x80) | |
252b5132 RH |
132 | { |
133 | /* Parallel. */ | |
134 | (*info->fprintf_func) (info->stream, " || "); | |
88845958 | 135 | *x &= 0x7f; |
252b5132 RH |
136 | } |
137 | else | |
138 | (*info->fprintf_func) (info->stream, " -> "); | |
139 | ||
140 | /* The "& 3" is to pass a consistent address. | |
141 | Parallel insns arguably both begin on the word boundary. | |
142 | Also, branch insns are calculated relative to the word boundary. */ | |
143 | if (print_insn (cd, pc & ~ (bfd_vma) 3, info, buf, 2) == 0) | |
144 | (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG); | |
145 | ||
146 | return (pc & 3) ? 2 : 4; | |
147 | } | |
148 | ||
149 | /* -- */ | |
150 | ||
0e2ee3ca NC |
151 | void m32r_cgen_print_operand |
152 | PARAMS ((CGEN_CPU_DESC, int, PTR, CGEN_FIELDS *, | |
153 | void const *, bfd_vma, int)); | |
154 | ||
252b5132 RH |
155 | /* Main entry point for printing operands. |
156 | XINFO is a `void *' and not a `disassemble_info *' to not put a requirement | |
157 | of dis-asm.h on cgen.h. | |
158 | ||
159 | This function is basically just a big switch statement. Earlier versions | |
160 | used tables to look up the function to use, but | |
161 | - if the table contains both assembler and disassembler functions then | |
162 | the disassembler contains much of the assembler and vice-versa, | |
163 | - there's a lot of inlining possibilities as things grow, | |
164 | - using a switch statement avoids the function call overhead. | |
165 | ||
166 | This function could be moved into `print_insn_normal', but keeping it | |
167 | separate makes clear the interface between `print_insn_normal' and each of | |
9a2e995d | 168 | the handlers. */ |
252b5132 RH |
169 | |
170 | void | |
171 | m32r_cgen_print_operand (cd, opindex, xinfo, fields, attrs, pc, length) | |
172 | CGEN_CPU_DESC cd; | |
173 | int opindex; | |
174 | PTR xinfo; | |
175 | CGEN_FIELDS *fields; | |
fc05c67f | 176 | void const *attrs ATTRIBUTE_UNUSED; |
252b5132 RH |
177 | bfd_vma pc; |
178 | int length; | |
179 | { | |
180 | disassemble_info *info = (disassemble_info *) xinfo; | |
181 | ||
182 | switch (opindex) | |
183 | { | |
1fa60b5d DE |
184 | case M32R_OPERAND_ACC : |
185 | print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_acc, 0); | |
186 | break; | |
187 | case M32R_OPERAND_ACCD : | |
188 | print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_accd, 0); | |
189 | break; | |
190 | case M32R_OPERAND_ACCS : | |
191 | print_keyword (cd, info, & m32r_cgen_opval_h_accums, fields->f_accs, 0); | |
192 | break; | |
252b5132 RH |
193 | case M32R_OPERAND_DCR : |
194 | print_keyword (cd, info, & m32r_cgen_opval_cr_names, fields->f_r1, 0); | |
195 | break; | |
196 | case M32R_OPERAND_DISP16 : | |
197 | print_address (cd, info, fields->f_disp16, 0|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length); | |
198 | break; | |
199 | case M32R_OPERAND_DISP24 : | |
200 | print_address (cd, info, fields->f_disp24, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length); | |
201 | break; | |
202 | case M32R_OPERAND_DISP8 : | |
203 | print_address (cd, info, fields->f_disp8, 0|(1<<CGEN_OPERAND_RELAX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_PCREL_ADDR), pc, length); | |
204 | break; | |
205 | case M32R_OPERAND_DR : | |
206 | print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r1, 0); | |
207 | break; | |
208 | case M32R_OPERAND_HASH : | |
eb1b03df | 209 | print_hash (cd, info, 0, 0|(1<<CGEN_OPERAND_SIGNED), pc, length); |
252b5132 RH |
210 | break; |
211 | case M32R_OPERAND_HI16 : | |
212 | print_normal (cd, info, fields->f_hi16, 0|(1<<CGEN_OPERAND_SIGN_OPT), pc, length); | |
213 | break; | |
1fa60b5d DE |
214 | case M32R_OPERAND_IMM1 : |
215 | print_normal (cd, info, fields->f_imm1, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
216 | break; | |
252b5132 RH |
217 | case M32R_OPERAND_SCR : |
218 | print_keyword (cd, info, & m32r_cgen_opval_cr_names, fields->f_r2, 0); | |
219 | break; | |
220 | case M32R_OPERAND_SIMM16 : | |
221 | print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
222 | break; | |
223 | case M32R_OPERAND_SIMM8 : | |
224 | print_normal (cd, info, fields->f_simm8, 0|(1<<CGEN_OPERAND_SIGNED)|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
225 | break; | |
226 | case M32R_OPERAND_SLO16 : | |
227 | print_normal (cd, info, fields->f_simm16, 0|(1<<CGEN_OPERAND_SIGNED), pc, length); | |
228 | break; | |
229 | case M32R_OPERAND_SR : | |
230 | print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r2, 0); | |
231 | break; | |
232 | case M32R_OPERAND_SRC1 : | |
233 | print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r1, 0); | |
234 | break; | |
235 | case M32R_OPERAND_SRC2 : | |
236 | print_keyword (cd, info, & m32r_cgen_opval_gr_names, fields->f_r2, 0); | |
237 | break; | |
238 | case M32R_OPERAND_UIMM16 : | |
239 | print_normal (cd, info, fields->f_uimm16, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
240 | break; | |
241 | case M32R_OPERAND_UIMM24 : | |
242 | print_address (cd, info, fields->f_uimm24, 0|(1<<CGEN_OPERAND_HASH_PREFIX)|(1<<CGEN_OPERAND_RELOC)|(1<<CGEN_OPERAND_ABS_ADDR), pc, length); | |
243 | break; | |
88845958 NC |
244 | case M32R_OPERAND_UIMM3 : |
245 | print_normal (cd, info, fields->f_uimm3, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
246 | break; | |
252b5132 RH |
247 | case M32R_OPERAND_UIMM4 : |
248 | print_normal (cd, info, fields->f_uimm4, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
249 | break; | |
250 | case M32R_OPERAND_UIMM5 : | |
251 | print_normal (cd, info, fields->f_uimm5, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
252 | break; | |
88845958 NC |
253 | case M32R_OPERAND_UIMM8 : |
254 | print_normal (cd, info, fields->f_uimm8, 0|(1<<CGEN_OPERAND_HASH_PREFIX), pc, length); | |
255 | break; | |
252b5132 RH |
256 | case M32R_OPERAND_ULO16 : |
257 | print_normal (cd, info, fields->f_uimm16, 0, pc, length); | |
258 | break; | |
259 | ||
260 | default : | |
261 | /* xgettext:c-format */ | |
262 | fprintf (stderr, _("Unrecognized field %d while printing insn.\n"), | |
263 | opindex); | |
264 | abort (); | |
265 | } | |
266 | } | |
267 | ||
268 | cgen_print_fn * const m32r_cgen_print_handlers[] = | |
269 | { | |
270 | print_insn_normal, | |
271 | }; | |
272 | ||
273 | ||
274 | void | |
275 | m32r_cgen_init_dis (cd) | |
276 | CGEN_CPU_DESC cd; | |
277 | { | |
278 | m32r_cgen_init_opcode_table (cd); | |
279 | m32r_cgen_init_ibld_table (cd); | |
280 | cd->print_handlers = & m32r_cgen_print_handlers[0]; | |
281 | cd->print_operand = m32r_cgen_print_operand; | |
282 | } | |
283 | ||
284 | \f | |
285 | /* Default print handler. */ | |
286 | ||
287 | static void | |
ffead7ae MM |
288 | print_normal (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
289 | void *dis_info, | |
290 | long value, | |
291 | unsigned int attrs, | |
292 | bfd_vma pc ATTRIBUTE_UNUSED, | |
293 | int length ATTRIBUTE_UNUSED) | |
252b5132 RH |
294 | { |
295 | disassemble_info *info = (disassemble_info *) dis_info; | |
296 | ||
297 | #ifdef CGEN_PRINT_NORMAL | |
298 | CGEN_PRINT_NORMAL (cd, info, value, attrs, pc, length); | |
299 | #endif | |
300 | ||
301 | /* Print the operand as directed by the attributes. */ | |
302 | if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY)) | |
303 | ; /* nothing to do */ | |
304 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED)) | |
305 | (*info->fprintf_func) (info->stream, "%ld", value); | |
306 | else | |
307 | (*info->fprintf_func) (info->stream, "0x%lx", value); | |
308 | } | |
309 | ||
310 | /* Default address handler. */ | |
311 | ||
312 | static void | |
ffead7ae MM |
313 | print_address (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
314 | void *dis_info, | |
315 | bfd_vma value, | |
316 | unsigned int attrs, | |
317 | bfd_vma pc ATTRIBUTE_UNUSED, | |
318 | int length ATTRIBUTE_UNUSED) | |
252b5132 RH |
319 | { |
320 | disassemble_info *info = (disassemble_info *) dis_info; | |
321 | ||
322 | #ifdef CGEN_PRINT_ADDRESS | |
323 | CGEN_PRINT_ADDRESS (cd, info, value, attrs, pc, length); | |
324 | #endif | |
325 | ||
326 | /* Print the operand as directed by the attributes. */ | |
327 | if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SEM_ONLY)) | |
328 | ; /* nothing to do */ | |
329 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_PCREL_ADDR)) | |
330 | (*info->print_address_func) (value, info); | |
331 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_ABS_ADDR)) | |
332 | (*info->print_address_func) (value, info); | |
333 | else if (CGEN_BOOL_ATTR (attrs, CGEN_OPERAND_SIGNED)) | |
334 | (*info->fprintf_func) (info->stream, "%ld", (long) value); | |
335 | else | |
336 | (*info->fprintf_func) (info->stream, "0x%lx", (long) value); | |
337 | } | |
338 | ||
339 | /* Keyword print handler. */ | |
340 | ||
341 | static void | |
ffead7ae MM |
342 | print_keyword (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
343 | void *dis_info, | |
344 | CGEN_KEYWORD *keyword_table, | |
345 | long value, | |
346 | unsigned int attrs ATTRIBUTE_UNUSED) | |
252b5132 RH |
347 | { |
348 | disassemble_info *info = (disassemble_info *) dis_info; | |
349 | const CGEN_KEYWORD_ENTRY *ke; | |
350 | ||
351 | ke = cgen_keyword_lookup_value (keyword_table, value); | |
352 | if (ke != NULL) | |
353 | (*info->fprintf_func) (info->stream, "%s", ke->name); | |
354 | else | |
355 | (*info->fprintf_func) (info->stream, "???"); | |
356 | } | |
357 | \f | |
358 | /* Default insn printer. | |
359 | ||
ffead7ae | 360 | DIS_INFO is defined as `void *' so the disassembler needn't know anything |
252b5132 RH |
361 | about disassemble_info. */ |
362 | ||
363 | static void | |
ffead7ae MM |
364 | print_insn_normal (CGEN_CPU_DESC cd, |
365 | void *dis_info, | |
366 | const CGEN_INSN *insn, | |
367 | CGEN_FIELDS *fields, | |
368 | bfd_vma pc, | |
369 | int length) | |
252b5132 RH |
370 | { |
371 | const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn); | |
372 | disassemble_info *info = (disassemble_info *) dis_info; | |
b3466c39 | 373 | const CGEN_SYNTAX_CHAR_TYPE *syn; |
252b5132 RH |
374 | |
375 | CGEN_INIT_PRINT (cd); | |
376 | ||
377 | for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn) | |
378 | { | |
379 | if (CGEN_SYNTAX_MNEMONIC_P (*syn)) | |
380 | { | |
381 | (*info->fprintf_func) (info->stream, "%s", CGEN_INSN_MNEMONIC (insn)); | |
382 | continue; | |
383 | } | |
384 | if (CGEN_SYNTAX_CHAR_P (*syn)) | |
385 | { | |
386 | (*info->fprintf_func) (info->stream, "%c", CGEN_SYNTAX_CHAR (*syn)); | |
387 | continue; | |
388 | } | |
389 | ||
390 | /* We have an operand. */ | |
391 | m32r_cgen_print_operand (cd, CGEN_SYNTAX_FIELD (*syn), info, | |
392 | fields, CGEN_INSN_ATTRS (insn), pc, length); | |
393 | } | |
394 | } | |
395 | \f | |
6bb95a0f DB |
396 | /* Subroutine of print_insn. Reads an insn into the given buffers and updates |
397 | the extract info. | |
398 | Returns 0 if all is well, non-zero otherwise. */ | |
0e2ee3ca | 399 | |
252b5132 | 400 | static int |
ffead7ae MM |
401 | read_insn (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED, |
402 | bfd_vma pc, | |
403 | disassemble_info *info, | |
404 | char *buf, | |
405 | int buflen, | |
406 | CGEN_EXTRACT_INFO *ex_info, | |
407 | unsigned long *insn_value) | |
252b5132 | 408 | { |
6bb95a0f DB |
409 | int status = (*info->read_memory_func) (pc, buf, buflen, info); |
410 | if (status != 0) | |
411 | { | |
412 | (*info->memory_error_func) (status, pc, info); | |
413 | return -1; | |
414 | } | |
252b5132 | 415 | |
6bb95a0f DB |
416 | ex_info->dis_info = info; |
417 | ex_info->valid = (1 << buflen) - 1; | |
418 | ex_info->insn_bytes = buf; | |
252b5132 | 419 | |
b3466c39 | 420 | *insn_value = bfd_get_bits (buf, buflen * 8, info->endian == BFD_ENDIAN_BIG); |
6bb95a0f DB |
421 | return 0; |
422 | } | |
423 | ||
424 | /* Utility to print an insn. | |
425 | BUF is the base part of the insn, target byte order, BUFLEN bytes long. | |
426 | The result is the size of the insn in bytes or zero for an unknown insn | |
427 | or -1 if an error occurs fetching data (memory_error_func will have | |
428 | been called). */ | |
429 | ||
430 | static int | |
ffead7ae MM |
431 | print_insn (CGEN_CPU_DESC cd, |
432 | bfd_vma pc, | |
433 | disassemble_info *info, | |
434 | char *buf, | |
435 | unsigned int buflen) | |
6bb95a0f | 436 | { |
fc7bc883 | 437 | CGEN_INSN_INT insn_value; |
6bb95a0f DB |
438 | const CGEN_INSN_LIST *insn_list; |
439 | CGEN_EXTRACT_INFO ex_info; | |
2e1ef6b4 | 440 | int basesize; |
b3466c39 | 441 | |
52646233 | 442 | /* Extract base part of instruction, just in case CGEN_DIS_* uses it. */ |
2e1ef6b4 DB |
443 | basesize = cd->base_insn_bitsize < buflen * 8 ? |
444 | cd->base_insn_bitsize : buflen * 8; | |
445 | insn_value = cgen_get_insn_value (cd, buf, basesize); | |
446 | ||
52646233 FCE |
447 | |
448 | /* Fill in ex_info fields like read_insn would. Don't actually call | |
449 | read_insn, since the incoming buffer is already read (and possibly | |
450 | modified a la m32r). */ | |
451 | ex_info.valid = (1 << buflen) - 1; | |
452 | ex_info.dis_info = info; | |
453 | ex_info.insn_bytes = buf; | |
6bb95a0f | 454 | |
252b5132 RH |
455 | /* The instructions are stored in hash lists. |
456 | Pick the first one and keep trying until we find the right one. */ | |
457 | ||
458 | insn_list = CGEN_DIS_LOOKUP_INSN (cd, buf, insn_value); | |
459 | while (insn_list != NULL) | |
460 | { | |
461 | const CGEN_INSN *insn = insn_list->insn; | |
462 | CGEN_FIELDS fields; | |
463 | int length; | |
52646233 | 464 | unsigned long insn_value_cropped; |
252b5132 | 465 | |
cfcdbe97 | 466 | #ifdef CGEN_VALIDATE_INSN_SUPPORTED |
0e2ee3ca | 467 | /* Not needed as insn shouldn't be in hash lists if not supported. */ |
252b5132 RH |
468 | /* Supported by this cpu? */ |
469 | if (! m32r_cgen_insn_supported (cd, insn)) | |
cfcdbe97 AH |
470 | { |
471 | insn_list = CGEN_DIS_NEXT_INSN (insn_list); | |
472 | continue; | |
473 | } | |
252b5132 RH |
474 | #endif |
475 | ||
476 | /* Basic bit mask must be correct. */ | |
477 | /* ??? May wish to allow target to defer this check until the extract | |
478 | handler. */ | |
52646233 FCE |
479 | |
480 | /* Base size may exceed this instruction's size. Extract the | |
481 | relevant part from the buffer. */ | |
0e2ee3ca NC |
482 | if ((unsigned) (CGEN_INSN_BITSIZE (insn) / 8) < buflen && |
483 | (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long)) | |
52646233 FCE |
484 | insn_value_cropped = bfd_get_bits (buf, CGEN_INSN_BITSIZE (insn), |
485 | info->endian == BFD_ENDIAN_BIG); | |
486 | else | |
487 | insn_value_cropped = insn_value; | |
488 | ||
489 | if ((insn_value_cropped & CGEN_INSN_BASE_MASK (insn)) | |
252b5132 RH |
490 | == CGEN_INSN_BASE_VALUE (insn)) |
491 | { | |
492 | /* Printing is handled in two passes. The first pass parses the | |
493 | machine insn and extracts the fields. The second pass prints | |
494 | them. */ | |
495 | ||
54faae25 NC |
496 | /* Make sure the entire insn is loaded into insn_value, if it |
497 | can fit. */ | |
0e2ee3ca NC |
498 | if (((unsigned) CGEN_INSN_BITSIZE (insn) > cd->base_insn_bitsize) && |
499 | (unsigned) (CGEN_INSN_BITSIZE (insn) / 8) <= sizeof (unsigned long)) | |
6bb95a0f DB |
500 | { |
501 | unsigned long full_insn_value; | |
502 | int rc = read_insn (cd, pc, info, buf, | |
503 | CGEN_INSN_BITSIZE (insn) / 8, | |
504 | & ex_info, & full_insn_value); | |
505 | if (rc != 0) | |
506 | return rc; | |
507 | length = CGEN_EXTRACT_FN (cd, insn) | |
508 | (cd, insn, &ex_info, full_insn_value, &fields, pc); | |
509 | } | |
510 | else | |
54faae25 | 511 | length = CGEN_EXTRACT_FN (cd, insn) |
fc7bc883 | 512 | (cd, insn, &ex_info, insn_value_cropped, &fields, pc); |
b3466c39 | 513 | |
252b5132 RH |
514 | /* length < 0 -> error */ |
515 | if (length < 0) | |
516 | return length; | |
517 | if (length > 0) | |
518 | { | |
519 | CGEN_PRINT_FN (cd, insn) (cd, info, insn, &fields, pc, length); | |
520 | /* length is in bits, result is in bytes */ | |
521 | return length / 8; | |
522 | } | |
523 | } | |
524 | ||
525 | insn_list = CGEN_DIS_NEXT_INSN (insn_list); | |
526 | } | |
527 | ||
528 | return 0; | |
529 | } | |
530 | ||
531 | /* Default value for CGEN_PRINT_INSN. | |
532 | The result is the size of the insn in bytes or zero for an unknown insn | |
533 | or -1 if an error occured fetching bytes. */ | |
534 | ||
535 | #ifndef CGEN_PRINT_INSN | |
536 | #define CGEN_PRINT_INSN default_print_insn | |
0e2ee3ca | 537 | #endif |
252b5132 RH |
538 | |
539 | static int | |
ffead7ae | 540 | default_print_insn (CGEN_CPU_DESC cd, bfd_vma pc, disassemble_info *info) |
252b5132 RH |
541 | { |
542 | char buf[CGEN_MAX_INSN_SIZE]; | |
fc7bc883 | 543 | int buflen; |
252b5132 RH |
544 | int status; |
545 | ||
fc7bc883 RH |
546 | /* Attempt to read the base part of the insn. */ |
547 | buflen = cd->base_insn_bitsize / 8; | |
548 | status = (*info->read_memory_func) (pc, buf, buflen, info); | |
549 | ||
550 | /* Try again with the minimum part, if min < base. */ | |
551 | if (status != 0 && (cd->min_insn_bitsize < cd->base_insn_bitsize)) | |
552 | { | |
553 | buflen = cd->min_insn_bitsize / 8; | |
554 | status = (*info->read_memory_func) (pc, buf, buflen, info); | |
555 | } | |
252b5132 | 556 | |
252b5132 RH |
557 | if (status != 0) |
558 | { | |
559 | (*info->memory_error_func) (status, pc, info); | |
560 | return -1; | |
561 | } | |
562 | ||
fc7bc883 | 563 | return print_insn (cd, pc, info, buf, buflen); |
252b5132 RH |
564 | } |
565 | ||
566 | /* Main entry point. | |
567 | Print one instruction from PC on INFO->STREAM. | |
568 | Return the size of the instruction (in bytes). */ | |
569 | ||
a978a3e5 NC |
570 | typedef struct cpu_desc_list { |
571 | struct cpu_desc_list *next; | |
572 | int isa; | |
573 | int mach; | |
574 | int endian; | |
575 | CGEN_CPU_DESC cd; | |
576 | } cpu_desc_list; | |
577 | ||
252b5132 | 578 | int |
ffead7ae | 579 | print_insn_m32r (bfd_vma pc, disassemble_info *info) |
252b5132 | 580 | { |
a978a3e5 NC |
581 | static cpu_desc_list *cd_list = 0; |
582 | cpu_desc_list *cl = 0; | |
252b5132 | 583 | static CGEN_CPU_DESC cd = 0; |
6bb95a0f DB |
584 | static int prev_isa; |
585 | static int prev_mach; | |
586 | static int prev_endian; | |
252b5132 RH |
587 | int length; |
588 | int isa,mach; | |
589 | int endian = (info->endian == BFD_ENDIAN_BIG | |
590 | ? CGEN_ENDIAN_BIG | |
591 | : CGEN_ENDIAN_LITTLE); | |
592 | enum bfd_architecture arch; | |
593 | ||
594 | /* ??? gdb will set mach but leave the architecture as "unknown" */ | |
595 | #ifndef CGEN_BFD_ARCH | |
596 | #define CGEN_BFD_ARCH bfd_arch_m32r | |
597 | #endif | |
598 | arch = info->arch; | |
599 | if (arch == bfd_arch_unknown) | |
600 | arch = CGEN_BFD_ARCH; | |
27fca2d8 PM |
601 | |
602 | /* There's no standard way to compute the machine or isa number | |
252b5132 | 603 | so we leave it to the target. */ |
27fca2d8 PM |
604 | #ifdef CGEN_COMPUTE_MACH |
605 | mach = CGEN_COMPUTE_MACH (info); | |
606 | #else | |
607 | mach = info->mach; | |
608 | #endif | |
609 | ||
252b5132 RH |
610 | #ifdef CGEN_COMPUTE_ISA |
611 | isa = CGEN_COMPUTE_ISA (info); | |
612 | #else | |
a978a3e5 | 613 | isa = info->insn_sets; |
252b5132 RH |
614 | #endif |
615 | ||
a978a3e5 | 616 | /* If we've switched cpu's, try to find a handle we've used before */ |
252b5132 RH |
617 | if (cd |
618 | && (isa != prev_isa | |
619 | || mach != prev_mach | |
620 | || endian != prev_endian)) | |
621 | { | |
252b5132 | 622 | cd = 0; |
a978a3e5 NC |
623 | for (cl = cd_list; cl; cl = cl->next) |
624 | { | |
625 | if (cl->isa == isa && | |
626 | cl->mach == mach && | |
627 | cl->endian == endian) | |
628 | { | |
629 | cd = cl->cd; | |
630 | break; | |
631 | } | |
632 | } | |
633 | } | |
252b5132 RH |
634 | |
635 | /* If we haven't initialized yet, initialize the opcode table. */ | |
636 | if (! cd) | |
637 | { | |
638 | const bfd_arch_info_type *arch_type = bfd_lookup_arch (arch, mach); | |
639 | const char *mach_name; | |
640 | ||
641 | if (!arch_type) | |
642 | abort (); | |
643 | mach_name = arch_type->printable_name; | |
644 | ||
645 | prev_isa = isa; | |
646 | prev_mach = mach; | |
647 | prev_endian = endian; | |
648 | cd = m32r_cgen_cpu_open (CGEN_CPU_OPEN_ISAS, prev_isa, | |
649 | CGEN_CPU_OPEN_BFDMACH, mach_name, | |
650 | CGEN_CPU_OPEN_ENDIAN, prev_endian, | |
651 | CGEN_CPU_OPEN_END); | |
652 | if (!cd) | |
653 | abort (); | |
a978a3e5 NC |
654 | |
655 | /* save this away for future reference */ | |
656 | cl = xmalloc (sizeof (struct cpu_desc_list)); | |
657 | cl->cd = cd; | |
658 | cl->isa = isa; | |
659 | cl->mach = mach; | |
660 | cl->endian = endian; | |
661 | cl->next = cd_list; | |
662 | cd_list = cl; | |
663 | ||
252b5132 RH |
664 | m32r_cgen_init_dis (cd); |
665 | } | |
666 | ||
667 | /* We try to have as much common code as possible. | |
668 | But at this point some targets need to take over. */ | |
669 | /* ??? Some targets may need a hook elsewhere. Try to avoid this, | |
670 | but if not possible try to move this hook elsewhere rather than | |
671 | have two hooks. */ | |
672 | length = CGEN_PRINT_INSN (cd, pc, info); | |
673 | if (length > 0) | |
674 | return length; | |
675 | if (length < 0) | |
676 | return -1; | |
677 | ||
678 | (*info->fprintf_func) (info->stream, UNKNOWN_INSN_MSG); | |
679 | return cd->default_insn_bitsize / 8; | |
680 | } |