gas/
[deliverable/binutils-gdb.git] / opcodes / cris-dis.c
CommitLineData
6c95a37f 1/* Disassembler code for CRIS.
aef6203b 2 Copyright 2000, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
6c95a37f
HPN
3 Contributed by Axis Communications AB, Lund, Sweden.
4 Written by Hans-Peter Nilsson.
5
6This file is part of the GNU binutils and GDB, the GNU debugger.
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
d83c6548 10Software Foundation; either version 2, or (at your option)
6c95a37f
HPN
11any later version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
f4321104 20Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
6c95a37f
HPN
21
22#include "dis-asm.h"
23#include "sysdep.h"
24#include "opcode/cris.h"
25#include "libiberty.h"
26\f
6c95a37f
HPN
27/* No instruction will be disassembled longer than this. In theory, and
28 in silicon, address prefixes can be cascaded. In practice, cascading
29 is not used by GCC, and not supported by the assembler. */
30#ifndef MAX_BYTES_PER_CRIS_INSN
31#define MAX_BYTES_PER_CRIS_INSN 8
32#endif
33
34/* Whether or not to decode prefixes, folding it into the following
35 instruction. FIXME: Make this optional later. */
36#ifndef PARSE_PREFIX
37#define PARSE_PREFIX 1
38#endif
39
78966507
HPN
40/* Sometimes we prefix all registers with this character. */
41#define REGISTER_PREFIX_CHAR '$'
42
6c95a37f
HPN
43/* Whether or not to trace the following sequence:
44 sub* X,r%d
45 bound* Y,r%d
46 adds.w [pc+r%d.w],pc
47
48 This is the assembly form of a switch-statement in C.
49 The "sub is optional. If there is none, then X will be zero.
50 X is the value of the first case,
51 Y is the number of cases (including default).
52
53 This results in case offsets printed on the form:
54 case N: -> case_address
55 where N is an estimation on the corresponding 'case' operand in C,
56 and case_address is where execution of that case continues after the
57 sequence presented above.
58
59 The old style of output was to print the offsets as instructions,
60 which made it hard to follow "case"-constructs in the disassembly,
61 and caused a lot of annoying warnings about undefined instructions.
62
63 FIXME: Make this optional later. */
64#ifndef TRACE_CASE
bd21e58e 65#define TRACE_CASE (disdata->trace_case)
6c95a37f
HPN
66#endif
67
bd21e58e
HPN
68enum cris_disass_family
69 { cris_dis_v0_v10, cris_dis_common_v10_v32, cris_dis_v32 };
70
71/* Stored in the disasm_info->private_data member. */
72struct cris_disasm_data
73{
74 /* Whether to print something less confusing if we find something
75 matching a switch-construct. */
76 bfd_boolean trace_case;
77
78 /* Whether this code is flagged as crisv32. FIXME: Should be an enum
79 that includes "compatible". */
80 enum cris_disass_family distype;
81};
82
6c95a37f
HPN
83/* Value of first element in switch. */
84static long case_offset = 0;
85
86/* How many more case-offsets to print. */
87static long case_offset_counter = 0;
88
89/* Number of case offsets. */
90static long no_of_case_offsets = 0;
91
92/* Candidate for next case_offset. */
93static long last_immediate = 0;
94
b34976b6
AM
95static int number_of_bits
96 PARAMS ((unsigned int));
97static char *format_hex
bd21e58e 98 PARAMS ((unsigned long, char *, struct cris_disasm_data *));
b34976b6
AM
99static char *format_dec
100 PARAMS ((long, char *, int));
101static char *format_reg
bd21e58e
HPN
102 PARAMS ((struct cris_disasm_data *, int, char *, bfd_boolean));
103static char *format_sup_reg
104 PARAMS ((unsigned int, char *, bfd_boolean));
b34976b6 105static int cris_constraint
bd21e58e
HPN
106 PARAMS ((const char *, unsigned int, unsigned int,
107 struct cris_disasm_data *));
b34976b6 108static unsigned bytes_to_skip
bd21e58e
HPN
109 PARAMS ((unsigned int, const struct cris_opcode *,
110 enum cris_disass_family));
b34976b6 111static char *print_flags
bd21e58e 112 PARAMS ((struct cris_disasm_data *, unsigned int, char *));
78966507
HPN
113static void print_with_operands
114 PARAMS ((const struct cris_opcode *, unsigned int, unsigned char *,
115 bfd_vma, disassemble_info *, const struct cris_opcode *,
b34976b6
AM
116 unsigned int, unsigned char *, bfd_boolean));
117static const struct cris_spec_reg *spec_reg_info
bd21e58e 118 PARAMS ((unsigned int, enum cris_disass_family));
78966507 119static int print_insn_cris_generic
b34976b6 120 PARAMS ((bfd_vma, disassemble_info *, bfd_boolean));
78966507
HPN
121static int print_insn_cris_with_register_prefix
122 PARAMS ((bfd_vma, disassemble_info *));
123static int print_insn_cris_without_register_prefix
124 PARAMS ((bfd_vma, disassemble_info *));
bd21e58e
HPN
125static int print_insn_crisv32_with_register_prefix
126 PARAMS ((bfd_vma, disassemble_info *));
127static int print_insn_crisv32_without_register_prefix
128 PARAMS ((bfd_vma, disassemble_info *));
129static int print_insn_crisv10_v32_with_register_prefix
130 PARAMS ((bfd_vma, disassemble_info *));
131static int print_insn_crisv10_v32_without_register_prefix
132 PARAMS ((bfd_vma, disassemble_info *));
133static bfd_boolean cris_parse_disassembler_options
134 PARAMS ((disassemble_info *, enum cris_disass_family));
d83c6548 135static const struct cris_opcode *get_opcode_entry
bd21e58e
HPN
136 PARAMS ((unsigned int, unsigned int, struct cris_disasm_data *));
137
138/* Parse disassembler options and store state in info. FIXME: For the
139 time being, we abuse static variables. */
140
141static bfd_boolean
142cris_parse_disassembler_options (info, distype)
143 disassemble_info *info;
144 enum cris_disass_family distype;
145{
146 struct cris_disasm_data *disdata;
147
148 info->private_data = calloc (1, sizeof (struct cris_disasm_data));
149 disdata = (struct cris_disasm_data *) info->private_data;
150 if (disdata == NULL)
151 return FALSE;
152
153 /* Default true. */
154 disdata->trace_case
155 = (info->disassembler_options == NULL
156 || (strcmp (info->disassembler_options, "nocase") != 0));
157
158 disdata->distype = distype;
159 return TRUE;
160}
6c95a37f 161
78966507 162
6c95a37f 163static const struct cris_spec_reg *
bd21e58e 164spec_reg_info (sreg, distype)
6c95a37f 165 unsigned int sreg;
bd21e58e 166 enum cris_disass_family distype;
6c95a37f
HPN
167{
168 int i;
169 for (i = 0; cris_spec_regs[i].name != NULL; i++)
170 {
171 if (cris_spec_regs[i].number == sreg)
bd21e58e
HPN
172 {
173 if (distype == cris_dis_v32)
174 switch (cris_spec_regs[i].applicable_version)
175 {
176 case cris_ver_warning:
177 case cris_ver_version_all:
178 case cris_ver_v3p:
179 case cris_ver_v8p:
180 case cris_ver_v10p:
181 case cris_ver_v32p:
182 /* No ambiguous sizes or register names with CRISv32. */
183 if (cris_spec_regs[i].warning == NULL)
184 return &cris_spec_regs[i];
185 default:
186 ;
187 }
188 else if (cris_spec_regs[i].applicable_version != cris_ver_v32p)
189 return &cris_spec_regs[i];
190 }
6c95a37f
HPN
191 }
192
193 return NULL;
194}
195
6c95a37f 196/* Return the number of bits in the argument. */
78966507 197
6c95a37f
HPN
198static int
199number_of_bits (val)
200 unsigned int val;
201{
202 int bits;
203
33822a8e 204 for (bits = 0; val != 0; val &= val - 1)
6c95a37f
HPN
205 bits++;
206
207 return bits;
208}
209
6c95a37f 210/* Get an entry in the opcode-table. */
78966507 211
6c95a37f 212static const struct cris_opcode *
bd21e58e 213get_opcode_entry (insn, prefix_insn, disdata)
6c95a37f
HPN
214 unsigned int insn;
215 unsigned int prefix_insn;
bd21e58e 216 struct cris_disasm_data *disdata;
6c95a37f
HPN
217{
218 /* For non-prefixed insns, we keep a table of pointers, indexed by the
219 insn code. Each entry is initialized when found to be NULL. */
220 static const struct cris_opcode **opc_table = NULL;
221
222 const struct cris_opcode *max_matchedp = NULL;
223 const struct cris_opcode **prefix_opc_table = NULL;
224
225 /* We hold a table for each prefix that need to be handled differently. */
226 static const struct cris_opcode **dip_prefixes = NULL;
227 static const struct cris_opcode **bdapq_m1_prefixes = NULL;
228 static const struct cris_opcode **bdapq_m2_prefixes = NULL;
229 static const struct cris_opcode **bdapq_m4_prefixes = NULL;
230 static const struct cris_opcode **rest_prefixes = NULL;
231
232 /* Allocate and clear the opcode-table. */
233 if (opc_table == NULL)
234 {
bd21e58e
HPN
235 opc_table = malloc (65536 * sizeof (opc_table[0]));
236 if (opc_table == NULL)
237 return NULL;
238
6c95a37f
HPN
239 memset (opc_table, 0, 65536 * sizeof (const struct cris_opcode *));
240
241 dip_prefixes
bd21e58e
HPN
242 = malloc (65536 * sizeof (const struct cris_opcode **));
243 if (dip_prefixes == NULL)
244 return NULL;
245
6c95a37f
HPN
246 memset (dip_prefixes, 0, 65536 * sizeof (dip_prefixes[0]));
247
248 bdapq_m1_prefixes
bd21e58e
HPN
249 = malloc (65536 * sizeof (const struct cris_opcode **));
250 if (bdapq_m1_prefixes == NULL)
251 return NULL;
252
6c95a37f
HPN
253 memset (bdapq_m1_prefixes, 0, 65536 * sizeof (bdapq_m1_prefixes[0]));
254
255 bdapq_m2_prefixes
bd21e58e
HPN
256 = malloc (65536 * sizeof (const struct cris_opcode **));
257 if (bdapq_m2_prefixes == NULL)
258 return NULL;
259
6c95a37f
HPN
260 memset (bdapq_m2_prefixes, 0, 65536 * sizeof (bdapq_m2_prefixes[0]));
261
262 bdapq_m4_prefixes
bd21e58e
HPN
263 = malloc (65536 * sizeof (const struct cris_opcode **));
264 if (bdapq_m4_prefixes == NULL)
265 return NULL;
266
6c95a37f
HPN
267 memset (bdapq_m4_prefixes, 0, 65536 * sizeof (bdapq_m4_prefixes[0]));
268
269 rest_prefixes
bd21e58e
HPN
270 = malloc (65536 * sizeof (const struct cris_opcode **));
271 if (rest_prefixes == NULL)
272 return NULL;
273
6c95a37f
HPN
274 memset (rest_prefixes, 0, 65536 * sizeof (rest_prefixes[0]));
275 }
276
277 /* Get the right table if this is a prefix.
278 This code is connected to cris_constraints in that it knows what
279 prefixes play a role in recognition of patterns; the necessary
280 state is reflected by which table is used. If constraints
281 involving match or non-match of prefix insns are changed, then this
282 probably needs changing too. */
283 if (prefix_insn != NO_CRIS_PREFIX)
284 {
285 const struct cris_opcode *popcodep
286 = (opc_table[prefix_insn] != NULL
287 ? opc_table[prefix_insn]
bd21e58e 288 : get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata));
6c95a37f
HPN
289
290 if (popcodep == NULL)
291 return NULL;
292
293 if (popcodep->match == BDAP_QUICK_OPCODE)
294 {
295 /* Since some offsets are recognized with "push" macros, we
296 have to have different tables for them. */
297 int offset = (prefix_insn & 255);
298
299 if (offset > 127)
300 offset -= 256;
301
302 switch (offset)
303 {
304 case -4:
305 prefix_opc_table = bdapq_m4_prefixes;
306 break;
307
308 case -2:
309 prefix_opc_table = bdapq_m2_prefixes;
310 break;
311
312 case -1:
313 prefix_opc_table = bdapq_m1_prefixes;
314 break;
315
316 default:
317 prefix_opc_table = rest_prefixes;
318 break;
319 }
320 }
321 else if (popcodep->match == DIP_OPCODE)
322 /* We don't allow postincrement when the prefix is DIP, so use a
323 different table for DIP. */
324 prefix_opc_table = dip_prefixes;
325 else
326 prefix_opc_table = rest_prefixes;
327 }
328
329 if (prefix_insn != NO_CRIS_PREFIX
330 && prefix_opc_table[insn] != NULL)
331 max_matchedp = prefix_opc_table[insn];
332 else if (prefix_insn == NO_CRIS_PREFIX && opc_table[insn] != NULL)
333 max_matchedp = opc_table[insn];
334 else
335 {
336 const struct cris_opcode *opcodep;
337 int max_level_of_match = -1;
338
339 for (opcodep = cris_opcodes;
340 opcodep->name != NULL;
341 opcodep++)
342 {
343 int level_of_match;
344
bd21e58e
HPN
345 if (disdata->distype == cris_dis_v32)
346 {
347 switch (opcodep->applicable_version)
348 {
349 case cris_ver_version_all:
350 break;
351
352 case cris_ver_v0_3:
353 case cris_ver_v0_10:
354 case cris_ver_v3_10:
355 case cris_ver_sim_v0_10:
356 case cris_ver_v8_10:
357 case cris_ver_v10:
358 case cris_ver_warning:
359 continue;
360
361 case cris_ver_v3p:
362 case cris_ver_v8p:
363 case cris_ver_v10p:
364 case cris_ver_v32p:
365 break;
366
367 case cris_ver_v8:
368 abort ();
369 default:
370 abort ();
371 }
372 }
373 else
374 {
375 switch (opcodep->applicable_version)
376 {
377 case cris_ver_version_all:
378 case cris_ver_v0_3:
379 case cris_ver_v3p:
380 case cris_ver_v0_10:
381 case cris_ver_v8p:
382 case cris_ver_v8_10:
383 case cris_ver_v10:
384 case cris_ver_sim_v0_10:
385 case cris_ver_v10p:
386 case cris_ver_warning:
387 break;
388
389 case cris_ver_v32p:
390 continue;
391
392 case cris_ver_v8:
393 abort ();
394 default:
395 abort ();
396 }
397 }
398
6c95a37f
HPN
399 /* We give a double lead for bits matching the template in
400 cris_opcodes. Not even, because then "move p8,r10" would
401 be given 2 bits lead over "clear.d r10". When there's a
402 tie, the first entry in the table wins. This is
403 deliberate, to avoid a more complicated recognition
404 formula. */
405 if ((opcodep->match & insn) == opcodep->match
406 && (opcodep->lose & insn) == 0
407 && ((level_of_match
408 = cris_constraint (opcodep->args,
409 insn,
bd21e58e
HPN
410 prefix_insn,
411 disdata))
6c95a37f
HPN
412 >= 0)
413 && ((level_of_match
414 += 2 * number_of_bits (opcodep->match
415 | opcodep->lose))
416 > max_level_of_match))
417 {
418 max_matchedp = opcodep;
419 max_level_of_match = level_of_match;
420
421 /* If there was a full match, never mind looking
422 further. */
423 if (level_of_match >= 2 * 16)
424 break;
425 }
426 }
427 /* Fill in the new entry.
428
429 If there are changes to the opcode-table involving prefixes, and
430 disassembly then does not work correctly, try removing the
431 else-clause below that fills in the prefix-table. If that
432 helps, you need to change the prefix_opc_table setting above, or
433 something related. */
434 if (prefix_insn == NO_CRIS_PREFIX)
435 opc_table[insn] = max_matchedp;
436 else
437 prefix_opc_table[insn] = max_matchedp;
438 }
439
440 return max_matchedp;
441}
442
6c95a37f 443/* Format number as hex with a leading "0x" into outbuffer. */
78966507 444
6c95a37f 445static char *
bd21e58e 446format_hex (number, outbuffer, disdata)
6c95a37f
HPN
447 unsigned long number;
448 char *outbuffer;
bd21e58e 449 struct cris_disasm_data *disdata;
6c95a37f 450{
47d8304e
AM
451 /* Truncate negative numbers on >32-bit hosts. */
452 number &= 0xffffffff;
6c95a37f
HPN
453
454 sprintf (outbuffer, "0x%lx", number);
455
456 /* Save this value for the "case" support. */
457 if (TRACE_CASE)
458 last_immediate = number;
459
460 return outbuffer + strlen (outbuffer);
461}
462
6c95a37f
HPN
463/* Format number as decimal into outbuffer. Parameter signedp says
464 whether the number should be formatted as signed (!= 0) or
465 unsigned (== 0). */
78966507 466
6c95a37f
HPN
467static char *
468format_dec (number, outbuffer, signedp)
469 long number;
470 char *outbuffer;
471 int signedp;
472{
473 last_immediate = number;
474 sprintf (outbuffer, signedp ? "%ld" : "%lu", number);
475
476 return outbuffer + strlen (outbuffer);
477}
478
6c95a37f 479/* Format the name of the general register regno into outbuffer. */
78966507 480
6c95a37f 481static char *
bd21e58e
HPN
482format_reg (disdata, regno, outbuffer_start, with_reg_prefix)
483 struct cris_disasm_data *disdata;
6c95a37f 484 int regno;
78966507 485 char *outbuffer_start;
b34976b6 486 bfd_boolean with_reg_prefix;
6c95a37f 487{
78966507
HPN
488 char *outbuffer = outbuffer_start;
489
490 if (with_reg_prefix)
491 *outbuffer++ = REGISTER_PREFIX_CHAR;
492
6c95a37f
HPN
493 switch (regno)
494 {
495 case 15:
bd21e58e
HPN
496 /* For v32, there is no context in which we output PC. */
497 if (disdata->distype == cris_dis_v32)
498 strcpy (outbuffer, "acr");
499 else
500 strcpy (outbuffer, "pc");
6c95a37f
HPN
501 break;
502
503 case 14:
504 strcpy (outbuffer, "sp");
505 break;
506
507 default:
508 sprintf (outbuffer, "r%d", regno);
509 break;
510 }
511
78966507 512 return outbuffer_start + strlen (outbuffer_start);
6c95a37f
HPN
513}
514
bd21e58e
HPN
515/* Format the name of a support register into outbuffer. */
516
517static char *
518format_sup_reg (regno, outbuffer_start, with_reg_prefix)
519 unsigned int regno;
520 char *outbuffer_start;
521 bfd_boolean with_reg_prefix;
522{
523 char *outbuffer = outbuffer_start;
524 int i;
525
526 if (with_reg_prefix)
527 *outbuffer++ = REGISTER_PREFIX_CHAR;
528
529 for (i = 0; cris_support_regs[i].name != NULL; i++)
530 if (cris_support_regs[i].number == regno)
531 {
532 sprintf (outbuffer, "%s", cris_support_regs[i].name);
533 return outbuffer_start + strlen (outbuffer_start);
534 }
535
536 /* There's supposed to be register names covering all numbers, though
537 some may be generic names. */
538 sprintf (outbuffer, "format_sup_reg-BUG");
539 return outbuffer_start + strlen (outbuffer_start);
540}
541
6c95a37f
HPN
542/* Return -1 if the constraints of a bitwise-matched instruction say
543 that there is no match. Otherwise return a nonnegative number
544 indicating the confidence in the match (higher is better). */
78966507 545
6c95a37f 546static int
bd21e58e 547cris_constraint (cs, insn, prefix_insn, disdata)
6c95a37f
HPN
548 const char *cs;
549 unsigned int insn;
550 unsigned int prefix_insn;
bd21e58e 551 struct cris_disasm_data *disdata;
6c95a37f
HPN
552{
553 int retval = 0;
554 int tmp;
555 int prefix_ok = 0;
556
557 const char *s;
33822a8e 558 for (s = cs; *s; s++)
6c95a37f
HPN
559 switch (*s)
560 {
561 case '!':
bd21e58e
HPN
562 /* Do not recognize "pop" if there's a prefix and then only for
563 v0..v10. */
564 if (prefix_insn != NO_CRIS_PREFIX
565 || disdata->distype != cris_dis_v0_v10)
6c95a37f
HPN
566 return -1;
567 break;
568
bd21e58e
HPN
569 case 'U':
570 /* Not recognized at disassembly. */
571 return -1;
572
6c95a37f
HPN
573 case 'M':
574 /* Size modifier for "clear", i.e. special register 0, 4 or 8.
575 Check that it is one of them. Only special register 12 could
576 be mismatched, but checking for matches is more logical than
577 checking for mismatches when there are only a few cases. */
578 tmp = ((insn >> 12) & 0xf);
579 if (tmp != 0 && tmp != 4 && tmp != 8)
580 return -1;
581 break;
582
583 case 'm':
584 if ((insn & 0x30) == 0x30)
585 return -1;
586 break;
587
588 case 'S':
589 /* A prefix operand without side-effect. */
590 if (prefix_insn != NO_CRIS_PREFIX && (insn & 0x400) == 0)
591 {
592 prefix_ok = 1;
593 break;
594 }
595 else
596 return -1;
597
598 case 's':
599 case 'y':
bd21e58e 600 case 'Y':
6c95a37f
HPN
601 /* If this is a prefixed insn with postincrement (side-effect),
602 the prefix must not be DIP. */
603 if (prefix_insn != NO_CRIS_PREFIX)
604 {
605 if (insn & 0x400)
606 {
607 const struct cris_opcode *prefix_opcodep
bd21e58e 608 = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
6c95a37f
HPN
609
610 if (prefix_opcodep->match == DIP_OPCODE)
611 return -1;
612 }
613
614 prefix_ok = 1;
615 }
616 break;
617
618 case 'B':
619 /* If we don't fall through, then the prefix is ok. */
620 prefix_ok = 1;
621
622 /* A "push" prefix. Check for valid "push" size.
623 In case of special register, it may be != 4. */
624 if (prefix_insn != NO_CRIS_PREFIX)
625 {
626 /* Match the prefix insn to BDAPQ. */
627 const struct cris_opcode *prefix_opcodep
bd21e58e 628 = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX, disdata);
6c95a37f
HPN
629
630 if (prefix_opcodep->match == BDAP_QUICK_OPCODE)
631 {
632 int pushsize = (prefix_insn & 255);
633
634 if (pushsize > 127)
635 pushsize -= 256;
636
637 if (s[1] == 'P')
638 {
639 unsigned int spec_reg = (insn >> 12) & 15;
640 const struct cris_spec_reg *sregp
bd21e58e 641 = spec_reg_info (spec_reg, disdata->distype);
6c95a37f
HPN
642
643 /* For a special-register, the "prefix size" must
644 match the size of the register. */
645 if (sregp && sregp->reg_size == (unsigned int) -pushsize)
646 break;
647 }
648 else if (s[1] == 'R')
649 {
650 if ((insn & 0x30) == 0x20 && pushsize == -4)
651 break;
652 }
653 /* FIXME: Should abort here; next constraint letter
654 *must* be 'P' or 'R'. */
655 }
656 }
657 return -1;
658
659 case 'D':
660 retval = (((insn >> 12) & 15) == (insn & 15));
661 if (!retval)
662 return -1;
663 else
664 retval += 4;
665 break;
666
667 case 'P':
668 {
669 const struct cris_spec_reg *sregp
bd21e58e 670 = spec_reg_info ((insn >> 12) & 15, disdata->distype);
6c95a37f
HPN
671
672 /* Since we match four bits, we will give a value of 4-1 = 3
673 in a match. If there is a corresponding exact match of a
674 special register in another pattern, it will get a value of
675 4, which will be higher. This should be correct in that an
676 exact pattern would match better than a general pattern.
677
678 Note that there is a reason for not returning zero; the
679 pattern for "clear" is partly matched in the bit-pattern
680 (the two lower bits must be zero), while the bit-pattern
681 for a move from a special register is matched in the
682 register constraint. */
683
684 if (sregp != NULL)
685 {
686 retval += 3;
687 break;
688 }
689 else
690 return -1;
691 }
692 }
693
694 if (prefix_insn != NO_CRIS_PREFIX && ! prefix_ok)
695 return -1;
696
697 return retval;
698}
699
6c95a37f 700/* Return the length of an instruction. */
78966507 701
6c95a37f 702static unsigned
bd21e58e 703bytes_to_skip (insn, matchedp, distype)
6c95a37f
HPN
704 unsigned int insn;
705 const struct cris_opcode *matchedp;
bd21e58e 706 enum cris_disass_family distype;
6c95a37f
HPN
707{
708 /* Each insn is a word plus "immediate" operands. */
709 unsigned to_skip = 2;
710 const char *template = matchedp->args;
711 const char *s;
712
713 for (s = template; *s; s++)
bd21e58e
HPN
714 if ((*s == 's' || *s == 'N' || *s == 'Y')
715 && (insn & 0x400) && (insn & 15) == 15)
6c95a37f
HPN
716 {
717 /* Immediate via [pc+], so we have to check the size of the
718 operand. */
719 int mode_size = 1 << ((insn >> 4) & (*template == 'z' ? 1 : 3));
720
721 if (matchedp->imm_oprnd_size == SIZE_FIX_32)
722 to_skip += 4;
723 else if (matchedp->imm_oprnd_size == SIZE_SPEC_REG)
724 {
725 const struct cris_spec_reg *sregp
bd21e58e 726 = spec_reg_info ((insn >> 12) & 15, distype);
6c95a37f
HPN
727
728 /* FIXME: Improve error handling; should have been caught
729 earlier. */
730 if (sregp == NULL)
731 return 2;
732
bd21e58e
HPN
733 /* PC is incremented by two, not one, for a byte. Except on
734 CRISv32, where constants are always DWORD-size for
735 special registers. */
736 to_skip +=
737 distype == cris_dis_v32 ? 4 : (sregp->reg_size + 1) & ~1;
6c95a37f
HPN
738 }
739 else
740 to_skip += (mode_size + 1) & ~1;
741 }
bd21e58e
HPN
742 else if (*s == 'n')
743 to_skip += 4;
6c95a37f
HPN
744 else if (*s == 'b')
745 to_skip += 2;
746
747 return to_skip;
748}
749
6c95a37f 750/* Print condition code flags. */
78966507 751
6c95a37f 752static char *
bd21e58e
HPN
753print_flags (disdata, insn, cp)
754 struct cris_disasm_data *disdata;
6c95a37f
HPN
755 unsigned int insn;
756 char *cp;
757{
758 /* Use the v8 (Etrax 100) flag definitions for disassembly.
759 The differences with v0 (Etrax 1..4) vs. Svinto are:
bd21e58e
HPN
760 v0 'd' <=> v8 'm'
761 v0 'e' <=> v8 'b'.
762 FIXME: Emit v0..v3 flag names somehow. */
763 static const char v8_fnames[] = "cvznxibm";
764 static const char v32_fnames[] = "cvznxiup";
765 const char *fnames
766 = disdata->distype == cris_dis_v32 ? v32_fnames : v8_fnames;
6c95a37f
HPN
767
768 unsigned char flagbits = (((insn >> 8) & 0xf0) | (insn & 15));
769 int i;
770
771 for (i = 0; i < 8; i++)
772 if (flagbits & (1 << i))
773 *cp++ = fnames[i];
774
775 return cp;
776}
777
6c95a37f
HPN
778/* Print out an insn with its operands, and update the info->insn_type
779 fields. The prefix_opcodep and the rest hold a prefix insn that is
780 supposed to be output as an address mode. */
78966507 781
6c95a37f
HPN
782static void
783print_with_operands (opcodep, insn, buffer, addr, info, prefix_opcodep,
78966507 784 prefix_insn, prefix_buffer, with_reg_prefix)
6c95a37f
HPN
785 const struct cris_opcode *opcodep;
786 unsigned int insn;
787 unsigned char *buffer;
788 bfd_vma addr;
789 disassemble_info *info;
790
791 /* If a prefix insn was before this insn (and is supposed to be
792 output as an address), here is a description of it. */
793 const struct cris_opcode *prefix_opcodep;
794 unsigned int prefix_insn;
795 unsigned char *prefix_buffer;
b34976b6 796 bfd_boolean with_reg_prefix;
6c95a37f
HPN
797{
798 /* Get a buffer of somewhat reasonable size where we store
799 intermediate parts of the insn. */
78966507 800 char temp[sizeof (".d [$r13=$r12-2147483648],$r10") * 2];
6c95a37f
HPN
801 char *tp = temp;
802 static const char mode_char[] = "bwd?";
803 const char *s;
804 const char *cs;
bd21e58e
HPN
805 struct cris_disasm_data *disdata
806 = (struct cris_disasm_data *) info->private_data;
6c95a37f
HPN
807
808 /* Print out the name first thing we do. */
809 (*info->fprintf_func) (info->stream, "%s", opcodep->name);
810
811 cs = opcodep->args;
812 s = cs;
813
814 /* Ignore any prefix indicator. */
815 if (*s == 'p')
816 s++;
817
818 if (*s == 'm' || *s == 'M' || *s == 'z')
819 {
820 *tp++ = '.';
821
822 /* Get the size-letter. */
823 *tp++ = *s == 'M'
824 ? (insn & 0x8000 ? 'd'
825 : insn & 0x4000 ? 'w' : 'b')
826 : mode_char[(insn >> 4) & (*s == 'z' ? 1 : 3)];
827
828 /* Ignore the size and the space character that follows. */
829 s += 2;
830 }
831
832 /* Add a space if this isn't a long-branch, because for those will add
833 the condition part of the name later. */
834 if (opcodep->match != (BRANCH_PC_LOW + BRANCH_INCR_HIGH * 256))
835 *tp++ = ' ';
836
837 /* Fill in the insn-type if deducible from the name (and there's no
838 better way). */
839 if (opcodep->name[0] == 'j')
840 {
841 if (strncmp (opcodep->name, "jsr", 3) == 0)
842 /* It's "jsr" or "jsrc". */
843 info->insn_type = dis_jsr;
844 else
845 /* Any other jump-type insn is considered a branch. */
846 info->insn_type = dis_branch;
847 }
848
849 /* We might know some more fields right now. */
850 info->branch_delay_insns = opcodep->delayed;
851
852 /* Handle operands. */
853 for (; *s; s++)
854 {
855 switch (*s)
856 {
bd21e58e
HPN
857 case 'T':
858 tp = format_sup_reg ((insn >> 12) & 15, tp, with_reg_prefix);
859 break;
860
861 case 'A':
862 if (with_reg_prefix)
863 *tp++ = REGISTER_PREFIX_CHAR;
864 *tp++ = 'a';
865 *tp++ = 'c';
866 *tp++ = 'r';
867 break;
868
869 case '[':
870 case ']':
6c95a37f
HPN
871 case ',':
872 *tp++ = *s;
873 break;
874
875 case '!':
bd21e58e
HPN
876 /* Ignore at this point; used at earlier stages to avoid
877 recognition if there's a prefix at something that in other
878 ways looks like a "pop". */
879 break;
880
881 case 'd':
882 /* Ignore. This is an optional ".d " on the large one of
883 relaxable insns. */
6c95a37f
HPN
884 break;
885
886 case 'B':
887 /* This was the prefix that made this a "push". We've already
888 handled it by recognizing it, so signal that the prefix is
889 handled by setting it to NULL. */
890 prefix_opcodep = NULL;
891 break;
892
893 case 'D':
894 case 'r':
bd21e58e 895 tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
6c95a37f
HPN
896 break;
897
898 case 'R':
bd21e58e
HPN
899 tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
900 break;
901
902 case 'n':
903 {
904 /* Like N but pc-relative to the start of the insn. */
905 unsigned long number
906 = (buffer[2] + buffer[3] * 256 + buffer[4] * 65536
907 + buffer[5] * 0x1000000 + addr);
908
909 /* Finish off and output previous formatted bytes. */
910 *tp = 0;
911 if (temp[0])
912 (*info->fprintf_func) (info->stream, "%s", temp);
913 tp = temp;
914
915 (*info->print_address_func) ((bfd_vma) number, info);
916 }
917 break;
918
919 case 'u':
920 {
921 /* Like n but the offset is bits <3:0> in the instruction. */
922 unsigned long number = (buffer[0] & 0xf) * 2 + addr;
923
924 /* Finish off and output previous formatted bytes. */
925 *tp = 0;
926 if (temp[0])
927 (*info->fprintf_func) (info->stream, "%s", temp);
928 tp = temp;
929
930 (*info->print_address_func) ((bfd_vma) number, info);
931 }
6c95a37f
HPN
932 break;
933
bd21e58e 934 case 'N':
6c95a37f 935 case 'y':
bd21e58e 936 case 'Y':
6c95a37f
HPN
937 case 'S':
938 case 's':
939 /* Any "normal" memory operand. */
940 if ((insn & 0x400) && (insn & 15) == 15)
941 {
942 /* We're looking at [pc+], i.e. we need to output an immediate
943 number, where the size can depend on different things. */
944 long number;
945 int signedp
946 = ((*cs == 'z' && (insn & 0x20))
947 || opcodep->match == BDAP_QUICK_OPCODE);
948 int nbytes;
949
950 if (opcodep->imm_oprnd_size == SIZE_FIX_32)
951 nbytes = 4;
952 else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
953 {
954 const struct cris_spec_reg *sregp
bd21e58e 955 = spec_reg_info ((insn >> 12) & 15, disdata->distype);
6c95a37f
HPN
956
957 /* A NULL return should have been as a non-match earlier,
958 so catch it as an internal error in the error-case
959 below. */
960 if (sregp == NULL)
961 /* Whatever non-valid size. */
962 nbytes = 42;
963 else
bd21e58e
HPN
964 /* PC is always incremented by a multiple of two.
965 For CRISv32, immediates are always 4 bytes for
966 special registers. */
967 nbytes = disdata->distype == cris_dis_v32
968 ? 4 : (sregp->reg_size + 1) & ~1;
6c95a37f
HPN
969 }
970 else
971 {
972 int mode_size = 1 << ((insn >> 4) & (*cs == 'z' ? 1 : 3));
973
974 if (mode_size == 1)
975 nbytes = 2;
976 else
977 nbytes = mode_size;
978 }
979
980 switch (nbytes)
981 {
982 case 1:
983 number = buffer[2];
984 if (signedp && number > 127)
985 number -= 256;
986 break;
987
988 case 2:
989 number = buffer[2] + buffer[3] * 256;
990 if (signedp && number > 32767)
991 number -= 65536;
992 break;
993
994 case 4:
995 number
996 = buffer[2] + buffer[3] * 256 + buffer[4] * 65536
997 + buffer[5] * 0x1000000;
998 break;
999
1000 default:
1001 strcpy (tp, "bug");
1002 tp += 3;
1003 number = 42;
1004 }
1005
1006 if ((*cs == 'z' && (insn & 0x20))
1007 || (opcodep->match == BDAP_QUICK_OPCODE
1008 && (nbytes <= 2 || buffer[1 + nbytes] == 0)))
1009 tp = format_dec (number, tp, signedp);
1010 else
1011 {
1012 unsigned int highbyte = (number >> 24) & 0xff;
1013
1014 /* Either output this as an address or as a number. If it's
1015 a dword with the same high-byte as the address of the
1016 insn, assume it's an address, and also if it's a non-zero
1017 non-0xff high-byte. If this is a jsr or a jump, then
1018 it's definitely an address. */
1019 if (nbytes == 4
1020 && (highbyte == ((addr >> 24) & 0xff)
1021 || (highbyte != 0 && highbyte != 0xff)
1022 || info->insn_type == dis_branch
1023 || info->insn_type == dis_jsr))
1024 {
1025 /* Finish off and output previous formatted bytes. */
1026 *tp = 0;
1027 tp = temp;
1028 if (temp[0])
1029 (*info->fprintf_func) (info->stream, "%s", temp);
1030
1031 (*info->print_address_func) ((bfd_vma) number, info);
1032
1033 info->target = number;
1034 }
1035 else
bd21e58e 1036 tp = format_hex (number, tp, disdata);
6c95a37f
HPN
1037 }
1038 }
1039 else
1040 {
1041 /* Not an immediate number. Then this is a (possibly
1042 prefixed) memory operand. */
1043 if (info->insn_type != dis_nonbranch)
1044 {
1045 int mode_size
1046 = 1 << ((insn >> 4)
1047 & (opcodep->args[0] == 'z' ? 1 : 3));
1048 int size;
1049 info->insn_type = dis_dref;
1050 info->flags |= CRIS_DIS_FLAG_MEMREF;
1051
1052 if (opcodep->imm_oprnd_size == SIZE_FIX_32)
1053 size = 4;
1054 else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
1055 {
1056 const struct cris_spec_reg *sregp
bd21e58e 1057 = spec_reg_info ((insn >> 12) & 15, disdata->distype);
6c95a37f
HPN
1058
1059 /* FIXME: Improve error handling; should have been caught
1060 earlier. */
1061 if (sregp == NULL)
1062 size = 4;
1063 else
1064 size = sregp->reg_size;
1065 }
1066 else
1067 size = mode_size;
1068
1069 info->data_size = size;
1070 }
1071
1072 *tp++ = '[';
1073
1074 if (prefix_opcodep
1075 /* We don't match dip with a postincremented field
1076 as a side-effect address mode. */
1077 && ((insn & 0x400) == 0
1078 || prefix_opcodep->match != DIP_OPCODE))
1079 {
1080 if (insn & 0x400)
1081 {
bd21e58e 1082 tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
6c95a37f
HPN
1083 *tp++ = '=';
1084 }
1085
1086
1087 /* We mainly ignore the prefix format string when the
1088 address-mode syntax is output. */
1089 switch (prefix_opcodep->match)
1090 {
1091 case DIP_OPCODE:
1092 /* It's [r], [r+] or [pc+]. */
1093 if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
1094 {
1095 /* It's [pc+]. This cannot possibly be anything
1096 but an address. */
1097 unsigned long number
1098 = prefix_buffer[2] + prefix_buffer[3] * 256
1099 + prefix_buffer[4] * 65536
1100 + prefix_buffer[5] * 0x1000000;
1101
1102 info->target = (bfd_vma) number;
1103
1104 /* Finish off and output previous formatted
1105 data. */
1106 *tp = 0;
1107 tp = temp;
1108 if (temp[0])
1109 (*info->fprintf_func) (info->stream, "%s", temp);
1110
1111 (*info->print_address_func) ((bfd_vma) number, info);
1112 }
1113 else
1114 {
1115 /* For a memref in an address, we use target2.
1116 In this case, target is zero. */
1117 info->flags
1118 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
1119 | CRIS_DIS_FLAG_MEM_TARGET2_MEM);
1120
1121 info->target2 = prefix_insn & 15;
1122
1123 *tp++ = '[';
bd21e58e 1124 tp = format_reg (disdata, prefix_insn & 15, tp,
78966507 1125 with_reg_prefix);
6c95a37f
HPN
1126 if (prefix_insn & 0x400)
1127 *tp++ = '+';
1128 *tp++ = ']';
1129 }
1130 break;
1131
1132 case BDAP_QUICK_OPCODE:
1133 {
1134 int number;
1135
1136 number = prefix_buffer[0];
1137 if (number > 127)
1138 number -= 256;
1139
1140 /* Output "reg+num" or, if num < 0, "reg-num". */
bd21e58e 1141 tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
78966507 1142 with_reg_prefix);
6c95a37f
HPN
1143 if (number >= 0)
1144 *tp++ = '+';
1145 tp = format_dec (number, tp, 1);
1146
1147 info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
1148 info->target = (prefix_insn >> 12) & 15;
1149 info->target2 = (bfd_vma) number;
1150 break;
1151 }
1152
1153 case BIAP_OPCODE:
1154 /* Output "r+R.m". */
bd21e58e
HPN
1155 tp = format_reg (disdata, prefix_insn & 15, tp,
1156 with_reg_prefix);
6c95a37f 1157 *tp++ = '+';
bd21e58e 1158 tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
78966507 1159 with_reg_prefix);
6c95a37f
HPN
1160 *tp++ = '.';
1161 *tp++ = mode_char[(prefix_insn >> 4) & 3];
1162
1163 info->flags
1164 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
1165 | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
1166
1167 | ((prefix_insn & 0x8000)
1168 ? CRIS_DIS_FLAG_MEM_TARGET2_MULT4
1169 : ((prefix_insn & 0x8000)
1170 ? CRIS_DIS_FLAG_MEM_TARGET2_MULT2 : 0)));
1171
1172 /* Is it the casejump? It's a "adds.w [pc+r%d.w],pc". */
1173 if (insn == 0xf83f && (prefix_insn & ~0xf000) == 0x55f)
1174 /* Then start interpreting data as offsets. */
1175 case_offset_counter = no_of_case_offsets;
1176 break;
1177
1178 case BDAP_INDIR_OPCODE:
1179 /* Output "r+s.m", or, if "s" is [pc+], "r+s" or
1180 "r-s". */
bd21e58e 1181 tp = format_reg (disdata, (prefix_insn >> 12) & 15, tp,
78966507 1182 with_reg_prefix);
6c95a37f
HPN
1183
1184 if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
1185 {
1186 long number;
1187 unsigned int nbytes;
1188
1189 /* It's a value. Get its size. */
1190 int mode_size = 1 << ((prefix_insn >> 4) & 3);
1191
1192 if (mode_size == 1)
1193 nbytes = 2;
1194 else
1195 nbytes = mode_size;
1196
1197 switch (nbytes)
1198 {
1199 case 1:
1200 number = prefix_buffer[2];
1201 if (number > 127)
1202 number -= 256;
1203 break;
1204
1205 case 2:
1206 number = prefix_buffer[2] + prefix_buffer[3] * 256;
1207 if (number > 32767)
1208 number -= 65536;
1209 break;
1210
1211 case 4:
1212 number
1213 = prefix_buffer[2] + prefix_buffer[3] * 256
1214 + prefix_buffer[4] * 65536
1215 + prefix_buffer[5] * 0x1000000;
1216 break;
1217
1218 default:
1219 strcpy (tp, "bug");
1220 tp += 3;
1221 number = 42;
1222 }
1223
1224 info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
1225 info->target2 = (bfd_vma) number;
1226
1227 /* If the size is dword, then assume it's an
1228 address. */
1229 if (nbytes == 4)
1230 {
1231 /* Finish off and output previous formatted
1232 bytes. */
1233 *tp++ = '+';
1234 *tp = 0;
1235 tp = temp;
1236 (*info->fprintf_func) (info->stream, "%s", temp);
1237
1238 (*info->print_address_func) ((bfd_vma) number, info);
1239 }
1240 else
1241 {
1242 if (number >= 0)
1243 *tp++ = '+';
1244 tp = format_dec (number, tp, 1);
1245 }
1246 }
1247 else
1248 {
1249 /* Output "r+[R].m" or "r+[R+].m". */
1250 *tp++ = '+';
1251 *tp++ = '[';
bd21e58e 1252 tp = format_reg (disdata, prefix_insn & 15, tp,
78966507 1253 with_reg_prefix);
6c95a37f
HPN
1254 if (prefix_insn & 0x400)
1255 *tp++ = '+';
1256 *tp++ = ']';
1257 *tp++ = '.';
1258 *tp++ = mode_char[(prefix_insn >> 4) & 3];
1259
1260 info->flags
1261 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
1262 | CRIS_DIS_FLAG_MEM_TARGET2_MEM
1263 | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
1264
1265 | (((prefix_insn >> 4) == 2)
1266 ? 0
1267 : (((prefix_insn >> 4) & 3) == 1
1268 ? CRIS_DIS_FLAG_MEM_TARGET2_MEM_WORD
1269 : CRIS_DIS_FLAG_MEM_TARGET2_MEM_BYTE)));
1270 }
1271 break;
1272
1273 default:
1274 (*info->fprintf_func) (info->stream, "?prefix-bug");
1275 }
1276
1277 /* To mark that the prefix is used, reset it. */
1278 prefix_opcodep = NULL;
1279 }
1280 else
1281 {
bd21e58e 1282 tp = format_reg (disdata, insn & 15, tp, with_reg_prefix);
6c95a37f
HPN
1283
1284 info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
1285 info->target = insn & 15;
1286
1287 if (insn & 0x400)
1288 *tp++ = '+';
1289 }
1290 *tp++ = ']';
1291 }
1292 break;
1293
1294 case 'x':
bd21e58e 1295 tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
6c95a37f
HPN
1296 *tp++ = '.';
1297 *tp++ = mode_char[(insn >> 4) & 3];
1298 break;
1299
1300 case 'I':
1301 tp = format_dec (insn & 63, tp, 0);
1302 break;
1303
1304 case 'b':
1305 {
1306 int where = buffer[2] + buffer[3] * 256;
1307
1308 if (where > 32767)
1309 where -= 65536;
1310
bd21e58e 1311 where += addr + ((disdata->distype == cris_dis_v32) ? 0 : 4);
6c95a37f
HPN
1312
1313 if (insn == BA_PC_INCR_OPCODE)
1314 info->insn_type = dis_branch;
1315 else
1316 info->insn_type = dis_condbranch;
1317
1318 info->target = (bfd_vma) where;
1319
1320 *tp = 0;
1321 tp = temp;
1322 (*info->fprintf_func) (info->stream, "%s%s ",
1323 temp, cris_cc_strings[insn >> 12]);
1324
1325 (*info->print_address_func) ((bfd_vma) where, info);
1326 }
1327 break;
1328
1329 case 'c':
1330 tp = format_dec (insn & 31, tp, 0);
1331 break;
1332
1333 case 'C':
1334 tp = format_dec (insn & 15, tp, 0);
1335 break;
1336
1337 case 'o':
1338 {
1339 long offset = insn & 0xfe;
bd21e58e 1340 bfd_vma target;
6c95a37f
HPN
1341
1342 if (insn & 1)
1343 offset |= ~0xff;
1344
1345 if (opcodep->match == BA_QUICK_OPCODE)
1346 info->insn_type = dis_branch;
1347 else
1348 info->insn_type = dis_condbranch;
1349
bd21e58e
HPN
1350 target = addr + ((disdata->distype == cris_dis_v32) ? 0 : 2) + offset;
1351 info->target = target;
6c95a37f
HPN
1352 *tp = 0;
1353 tp = temp;
1354 (*info->fprintf_func) (info->stream, "%s", temp);
bd21e58e 1355 (*info->print_address_func) (target, info);
6c95a37f
HPN
1356 }
1357 break;
1358
bd21e58e 1359 case 'Q':
6c95a37f
HPN
1360 case 'O':
1361 {
1362 long number = buffer[0];
1363
1364 if (number > 127)
1365 number = number - 256;
1366
1367 tp = format_dec (number, tp, 1);
1368 *tp++ = ',';
bd21e58e 1369 tp = format_reg (disdata, (insn >> 12) & 15, tp, with_reg_prefix);
6c95a37f
HPN
1370 }
1371 break;
1372
1373 case 'f':
bd21e58e 1374 tp = print_flags (disdata, insn, tp);
6c95a37f
HPN
1375 break;
1376
1377 case 'i':
85064c79 1378 tp = format_dec ((insn & 32) ? (insn & 31) | ~31L : insn & 31, tp, 1);
6c95a37f
HPN
1379 break;
1380
1381 case 'P':
1382 {
1383 const struct cris_spec_reg *sregp
bd21e58e 1384 = spec_reg_info ((insn >> 12) & 15, disdata->distype);
6c95a37f
HPN
1385
1386 if (sregp->name == NULL)
1387 /* Should have been caught as a non-match eariler. */
1388 *tp++ = '?';
1389 else
1390 {
78966507
HPN
1391 if (with_reg_prefix)
1392 *tp++ = REGISTER_PREFIX_CHAR;
6c95a37f
HPN
1393 strcpy (tp, sregp->name);
1394 tp += strlen (tp);
1395 }
1396 }
1397 break;
1398
1399 default:
1400 strcpy (tp, "???");
1401 tp += 3;
1402 }
1403 }
1404
1405 *tp = 0;
1406
1407 if (prefix_opcodep)
1408 (*info->fprintf_func) (info->stream, " (OOPS unused prefix \"%s: %s\")",
1409 prefix_opcodep->name, prefix_opcodep->args);
1410
1411 (*info->fprintf_func) (info->stream, "%s", temp);
1412
1413 /* Get info for matching case-tables, if we don't have any active.
1414 We assume that the last constant seen is used; either in the insn
1415 itself or in a "move.d const,rN, sub.d rN,rM"-like sequence. */
1416 if (TRACE_CASE && case_offset_counter == 0)
1417 {
1418 if (strncmp (opcodep->name, "sub", 3) == 0)
1419 case_offset = last_immediate;
1420
1421 /* It could also be an "add", if there are negative case-values. */
1422 else if (strncmp (opcodep->name, "add", 3) == 0)
1423 {
1424 /* The first case is the negated operand to the add. */
1425 case_offset = -last_immediate;
1426 }
1427 /* A bound insn will tell us the number of cases. */
1428 else if (strncmp (opcodep->name, "bound", 5) == 0)
1429 {
1430 no_of_case_offsets = last_immediate + 1;
1431 }
1432 /* A jump or jsr or branch breaks the chain of insns for a
1433 case-table, so assume default first-case again. */
1434 else if (info->insn_type == dis_jsr
1435 || info->insn_type == dis_branch
1436 || info->insn_type == dis_condbranch)
1437 case_offset = 0;
1438 }
1439}
1440
1441
1442/* Print the CRIS instruction at address memaddr on stream. Returns
78966507
HPN
1443 length of the instruction, in bytes. Prefix register names with `$' if
1444 WITH_REG_PREFIX. */
1445
1446static int
1447print_insn_cris_generic (memaddr, info, with_reg_prefix)
6c95a37f
HPN
1448 bfd_vma memaddr;
1449 disassemble_info *info;
b34976b6 1450 bfd_boolean with_reg_prefix;
6c95a37f
HPN
1451{
1452 int nbytes;
1453 unsigned int insn;
1454 const struct cris_opcode *matchedp;
1455 int advance = 0;
bd21e58e
HPN
1456 struct cris_disasm_data *disdata
1457 = (struct cris_disasm_data *) info->private_data;
6c95a37f
HPN
1458
1459 /* No instruction will be disassembled as longer than this number of
1460 bytes; stacked prefixes will not be expanded. */
1461 unsigned char buffer[MAX_BYTES_PER_CRIS_INSN];
1462 unsigned char *bufp;
d5b2f4d6 1463 int status = 0;
6c95a37f
HPN
1464 bfd_vma addr;
1465
1466 /* There will be an "out of range" error after the last instruction.
1467 Reading pairs of bytes in decreasing number, we hope that we will get
1468 at least the amount that we will consume.
1469
1470 If we can't get any data, or we do not get enough data, we print
1471 the error message. */
1472
1473 for (nbytes = MAX_BYTES_PER_CRIS_INSN; nbytes > 0; nbytes -= 2)
1474 {
1475 status = (*info->read_memory_func) (memaddr, buffer, nbytes, info);
1476 if (status == 0)
1477 break;
1478 }
1479
1480 /* If we did not get all we asked for, then clear the rest.
1481 Hopefully this makes a reproducible result in case of errors. */
1482 if (nbytes != MAX_BYTES_PER_CRIS_INSN)
1483 memset (buffer + nbytes, 0, MAX_BYTES_PER_CRIS_INSN - nbytes);
1484
1485 addr = memaddr;
1486 bufp = buffer;
1487
1488 /* Set some defaults for the insn info. */
1489 info->insn_info_valid = 1;
1490 info->branch_delay_insns = 0;
1491 info->data_size = 0;
1492 info->insn_type = dis_nonbranch;
1493 info->flags = 0;
1494 info->target = 0;
1495 info->target2 = 0;
1496
1497 /* If we got any data, disassemble it. */
1498 if (nbytes != 0)
1499 {
1500 matchedp = NULL;
1501
1502 insn = bufp[0] + bufp[1] * 256;
1503
1504 /* If we're in a case-table, don't disassemble the offsets. */
1505 if (TRACE_CASE && case_offset_counter != 0)
1506 {
1507 info->insn_type = dis_noninsn;
1508 advance += 2;
1509
1510 /* If to print data as offsets, then shortcut here. */
1511 (*info->fprintf_func) (info->stream, "case %d%s: -> ",
1512 case_offset + no_of_case_offsets
1513 - case_offset_counter,
1514 case_offset_counter == 1 ? "/default" :
1515 "");
1516
1517 (*info->print_address_func) ((bfd_vma)
1518 ((short) (insn)
1519 + (long) (addr
1520 - (no_of_case_offsets
1521 - case_offset_counter)
1522 * 2)), info);
1523 case_offset_counter--;
1524
1525 /* The default case start (without a "sub" or "add") must be
1526 zero. */
1527 if (case_offset_counter == 0)
1528 case_offset = 0;
1529 }
1530 else if (insn == 0)
1531 {
1532 /* We're often called to disassemble zeroes. While this is a
1533 valid "bcc .+2" insn, it is also useless enough and enough
1534 of a nuiscance that we will just output "bcc .+2" for it
1535 and signal it as a noninsn. */
bd21e58e
HPN
1536 (*info->fprintf_func) (info->stream,
1537 disdata->distype == cris_dis_v32
1538 ? "bcc ." : "bcc .+2");
6c95a37f
HPN
1539 info->insn_type = dis_noninsn;
1540 advance += 2;
1541 }
1542 else
1543 {
1544 const struct cris_opcode *prefix_opcodep = NULL;
1545 unsigned char *prefix_buffer = bufp;
1546 unsigned int prefix_insn = insn;
1547 int prefix_size = 0;
1548
bd21e58e 1549 matchedp = get_opcode_entry (insn, NO_CRIS_PREFIX, disdata);
6c95a37f
HPN
1550
1551 /* Check if we're supposed to write out prefixes as address
1552 modes and if this was a prefix. */
1553 if (matchedp != NULL && PARSE_PREFIX && matchedp->args[0] == 'p')
1554 {
1555 /* If it's a prefix, put it into the prefix vars and get the
1556 main insn. */
bd21e58e
HPN
1557 prefix_size = bytes_to_skip (prefix_insn, matchedp,
1558 disdata->distype);
6c95a37f
HPN
1559 prefix_opcodep = matchedp;
1560
1561 insn = bufp[prefix_size] + bufp[prefix_size + 1] * 256;
bd21e58e 1562 matchedp = get_opcode_entry (insn, prefix_insn, disdata);
6c95a37f
HPN
1563
1564 if (matchedp != NULL)
1565 {
1566 addr += prefix_size;
1567 bufp += prefix_size;
1568 advance += prefix_size;
1569 }
1570 else
1571 {
1572 /* The "main" insn wasn't valid, at least not when
1573 prefixed. Put back things enough to output the
1574 prefix insn only, as a normal insn. */
1575 matchedp = prefix_opcodep;
1576 insn = prefix_insn;
1577 prefix_opcodep = NULL;
1578 }
1579 }
1580
1581 if (matchedp == NULL)
1582 {
1583 (*info->fprintf_func) (info->stream, "??0x%lx", insn);
1584 advance += 2;
1585
1586 info->insn_type = dis_noninsn;
1587 }
1588 else
1589 {
bd21e58e 1590 advance += bytes_to_skip (insn, matchedp, disdata->distype);
6c95a37f
HPN
1591
1592 /* The info_type and assorted fields will be set according
1593 to the operands. */
1594 print_with_operands (matchedp, insn, bufp, addr, info,
1595 prefix_opcodep, prefix_insn,
78966507 1596 prefix_buffer, with_reg_prefix);
6c95a37f
HPN
1597 }
1598 }
1599 }
1600 else
1601 info->insn_type = dis_noninsn;
1602
1603 /* If we read less than MAX_BYTES_PER_CRIS_INSN, i.e. we got an error
1604 status when reading that much, and the insn decoding indicated a
1605 length exceeding what we read, there is an error. */
1606 if (status != 0 && (nbytes == 0 || advance > nbytes))
1607 {
1608 (*info->memory_error_func) (status, memaddr, info);
1609 return -1;
1610 }
1611
1612 /* Max supported insn size with one folded prefix insn. */
1613 info->bytes_per_line = MAX_BYTES_PER_CRIS_INSN;
1614
1615 /* I would like to set this to a fixed value larger than the actual
1616 number of bytes to print in order to avoid spaces between bytes,
1617 but objdump.c (2.9.1) does not like that, so we print 16-bit
1618 chunks, which is the next choice. */
1619 info->bytes_per_chunk = 2;
1620
1621 /* Printing bytes in order of increasing addresses makes sense,
1622 especially on a little-endian target.
1623 This is completely the opposite of what you think; setting this to
1624 BFD_ENDIAN_LITTLE will print bytes in order N..0 rather than the 0..N
1625 we want. */
1626 info->display_endian = BFD_ENDIAN_BIG;
1627
1628 return advance;
1629}
1630
bd21e58e 1631/* Disassemble, prefixing register names with `$'. CRIS v0..v10. */
78966507
HPN
1632
1633static int
1634print_insn_cris_with_register_prefix (vma, info)
1635 bfd_vma vma;
1636 disassemble_info *info;
1637{
bd21e58e
HPN
1638 if (info->private_data == NULL
1639 && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
1640 return -1;
1641 return print_insn_cris_generic (vma, info, TRUE);
1642}
1643
1644/* Disassemble, prefixing register names with `$'. CRIS v32. */
1645
1646static int
1647print_insn_crisv32_with_register_prefix (vma, info)
1648 bfd_vma vma;
1649 disassemble_info *info;
1650{
1651 if (info->private_data == NULL
1652 && !cris_parse_disassembler_options (info, cris_dis_v32))
1653 return -1;
1654 return print_insn_cris_generic (vma, info, TRUE);
1655}
1656
1657/* Disassemble, prefixing register names with `$'.
1658 Common v10 and v32 subset. */
1659
1660static int
1661print_insn_crisv10_v32_with_register_prefix (vma, info)
1662 bfd_vma vma;
1663 disassemble_info *info;
1664{
1665 if (info->private_data == NULL
1666 && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
1667 return -1;
b34976b6 1668 return print_insn_cris_generic (vma, info, TRUE);
78966507
HPN
1669}
1670
bd21e58e 1671/* Disassemble, no prefixes on register names. CRIS v0..v10. */
78966507
HPN
1672
1673static int
1674print_insn_cris_without_register_prefix (vma, info)
1675 bfd_vma vma;
1676 disassemble_info *info;
1677{
bd21e58e
HPN
1678 if (info->private_data == NULL
1679 && !cris_parse_disassembler_options (info, cris_dis_v0_v10))
1680 return -1;
1681 return print_insn_cris_generic (vma, info, FALSE);
1682}
1683
1684/* Disassemble, no prefixes on register names. CRIS v32. */
1685
1686static int
1687print_insn_crisv32_without_register_prefix (vma, info)
1688 bfd_vma vma;
1689 disassemble_info *info;
1690{
1691 if (info->private_data == NULL
1692 && !cris_parse_disassembler_options (info, cris_dis_v32))
1693 return -1;
1694 return print_insn_cris_generic (vma, info, FALSE);
1695}
1696
1697/* Disassemble, no prefixes on register names.
1698 Common v10 and v32 subset. */
1699
1700static int
1701print_insn_crisv10_v32_without_register_prefix (vma, info)
1702 bfd_vma vma;
1703 disassemble_info *info;
1704{
1705 if (info->private_data == NULL
1706 && !cris_parse_disassembler_options (info, cris_dis_common_v10_v32))
1707 return -1;
b34976b6 1708 return print_insn_cris_generic (vma, info, FALSE);
78966507
HPN
1709}
1710
1711/* Return a disassembler-function that prints registers with a `$' prefix,
bd21e58e
HPN
1712 or one that prints registers without a prefix.
1713 FIXME: We should improve the solution to avoid the multitude of
1714 functions seen above. */
78966507
HPN
1715
1716disassembler_ftype
1717cris_get_disassembler (abfd)
1718 bfd *abfd;
1719{
b6b0b32c
HPN
1720 /* If there's no bfd in sight, we return what is valid as input in all
1721 contexts if fed back to the assembler: disassembly *with* register
bd21e58e
HPN
1722 prefix. Unfortunately this will be totally wrong for v32. */
1723 if (abfd == NULL)
78966507
HPN
1724 return print_insn_cris_with_register_prefix;
1725
bd21e58e
HPN
1726 if (bfd_get_symbol_leading_char (abfd) == 0)
1727 {
1728 if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
1729 return print_insn_crisv32_with_register_prefix;
1730 if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
1731 return print_insn_crisv10_v32_with_register_prefix;
1732
1733 /* We default to v10. This may be specifically specified in the
1734 bfd mach, but is also the default setting. */
1735 return print_insn_cris_with_register_prefix;
1736 }
1737
1738 if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
1739 return print_insn_crisv32_without_register_prefix;
1740 if (bfd_get_mach (abfd) == bfd_mach_cris_v10_v32)
1741 return print_insn_crisv10_v32_without_register_prefix;
78966507
HPN
1742 return print_insn_cris_without_register_prefix;
1743}
1744
6c95a37f
HPN
1745/*
1746 * Local variables:
1747 * eval: (c-set-style "gnu")
1748 * indent-tabs-mode: t
1749 * End:
1750 */
This page took 0.529457 seconds and 4 git commands to generate.