]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - opcodes/cris-dis.c
2002-11-30 Nathanael Nerode <neroden@gcc.gnu.org>
[thirdparty/binutils-gdb.git] / opcodes / cris-dis.c
CommitLineData
6c95a37f 1/* Disassembler code for CRIS.
33822a8e 2 Copyright 2000, 2001 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
20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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
65#define TRACE_CASE 1
66#endif
67
68/* Value of first element in switch. */
69static long case_offset = 0;
70
71/* How many more case-offsets to print. */
72static long case_offset_counter = 0;
73
74/* Number of case offsets. */
75static long no_of_case_offsets = 0;
76
77/* Candidate for next case_offset. */
78static long last_immediate = 0;
79
80static int number_of_bits PARAMS ((unsigned int));
81static char *format_hex PARAMS ((unsigned long, char *));
82static char *format_dec PARAMS ((long, char *, int));
78966507 83static char *format_reg PARAMS ((int, char *, boolean));
6c95a37f
HPN
84static int cris_constraint PARAMS ((const char *, unsigned int,
85 unsigned int));
86static unsigned bytes_to_skip PARAMS ((unsigned int,
87 const struct cris_opcode *));
88static char *print_flags PARAMS ((unsigned int, char *));
78966507
HPN
89static void print_with_operands
90 PARAMS ((const struct cris_opcode *, unsigned int, unsigned char *,
91 bfd_vma, disassemble_info *, const struct cris_opcode *,
92 unsigned int, unsigned char *, boolean));
6c95a37f 93static const struct cris_spec_reg *spec_reg_info PARAMS ((unsigned int));
78966507
HPN
94static int print_insn_cris_generic
95 PARAMS ((bfd_vma, disassemble_info *, boolean));
96static int print_insn_cris_with_register_prefix
97 PARAMS ((bfd_vma, disassemble_info *));
98static int print_insn_cris_without_register_prefix
99 PARAMS ((bfd_vma, disassemble_info *));
d83c6548
AJ
100static const struct cris_opcode *get_opcode_entry
101 PARAMS ((unsigned int, unsigned int));
6c95a37f
HPN
102
103/* Return the descriptor of a special register.
104 FIXME: Depend on a CPU-version specific argument when all machinery
105 is in place. */
78966507 106
6c95a37f
HPN
107static const struct cris_spec_reg *
108spec_reg_info (sreg)
109 unsigned int sreg;
110{
111 int i;
112 for (i = 0; cris_spec_regs[i].name != NULL; i++)
113 {
114 if (cris_spec_regs[i].number == sreg)
115 return &cris_spec_regs[i];
116 }
117
118 return NULL;
119}
120
6c95a37f 121/* Return the number of bits in the argument. */
78966507 122
6c95a37f
HPN
123static int
124number_of_bits (val)
125 unsigned int val;
126{
127 int bits;
128
33822a8e 129 for (bits = 0; val != 0; val &= val - 1)
6c95a37f
HPN
130 bits++;
131
132 return bits;
133}
134
6c95a37f 135/* Get an entry in the opcode-table. */
78966507 136
6c95a37f
HPN
137static const struct cris_opcode *
138get_opcode_entry (insn, prefix_insn)
139 unsigned int insn;
140 unsigned int prefix_insn;
141{
142 /* For non-prefixed insns, we keep a table of pointers, indexed by the
143 insn code. Each entry is initialized when found to be NULL. */
144 static const struct cris_opcode **opc_table = NULL;
145
146 const struct cris_opcode *max_matchedp = NULL;
147 const struct cris_opcode **prefix_opc_table = NULL;
148
149 /* We hold a table for each prefix that need to be handled differently. */
150 static const struct cris_opcode **dip_prefixes = NULL;
151 static const struct cris_opcode **bdapq_m1_prefixes = NULL;
152 static const struct cris_opcode **bdapq_m2_prefixes = NULL;
153 static const struct cris_opcode **bdapq_m4_prefixes = NULL;
154 static const struct cris_opcode **rest_prefixes = NULL;
155
156 /* Allocate and clear the opcode-table. */
157 if (opc_table == NULL)
158 {
159 opc_table = xmalloc (65536 * sizeof (opc_table[0]));
160 memset (opc_table, 0, 65536 * sizeof (const struct cris_opcode *));
161
162 dip_prefixes
163 = xmalloc (65536 * sizeof (const struct cris_opcode **));
164 memset (dip_prefixes, 0, 65536 * sizeof (dip_prefixes[0]));
165
166 bdapq_m1_prefixes
167 = xmalloc (65536 * sizeof (const struct cris_opcode **));
168 memset (bdapq_m1_prefixes, 0, 65536 * sizeof (bdapq_m1_prefixes[0]));
169
170 bdapq_m2_prefixes
171 = xmalloc (65536 * sizeof (const struct cris_opcode **));
172 memset (bdapq_m2_prefixes, 0, 65536 * sizeof (bdapq_m2_prefixes[0]));
173
174 bdapq_m4_prefixes
175 = xmalloc (65536 * sizeof (const struct cris_opcode **));
176 memset (bdapq_m4_prefixes, 0, 65536 * sizeof (bdapq_m4_prefixes[0]));
177
178 rest_prefixes
179 = xmalloc (65536 * sizeof (const struct cris_opcode **));
180 memset (rest_prefixes, 0, 65536 * sizeof (rest_prefixes[0]));
181 }
182
183 /* Get the right table if this is a prefix.
184 This code is connected to cris_constraints in that it knows what
185 prefixes play a role in recognition of patterns; the necessary
186 state is reflected by which table is used. If constraints
187 involving match or non-match of prefix insns are changed, then this
188 probably needs changing too. */
189 if (prefix_insn != NO_CRIS_PREFIX)
190 {
191 const struct cris_opcode *popcodep
192 = (opc_table[prefix_insn] != NULL
193 ? opc_table[prefix_insn]
194 : get_opcode_entry (prefix_insn, NO_CRIS_PREFIX));
195
196 if (popcodep == NULL)
197 return NULL;
198
199 if (popcodep->match == BDAP_QUICK_OPCODE)
200 {
201 /* Since some offsets are recognized with "push" macros, we
202 have to have different tables for them. */
203 int offset = (prefix_insn & 255);
204
205 if (offset > 127)
206 offset -= 256;
207
208 switch (offset)
209 {
210 case -4:
211 prefix_opc_table = bdapq_m4_prefixes;
212 break;
213
214 case -2:
215 prefix_opc_table = bdapq_m2_prefixes;
216 break;
217
218 case -1:
219 prefix_opc_table = bdapq_m1_prefixes;
220 break;
221
222 default:
223 prefix_opc_table = rest_prefixes;
224 break;
225 }
226 }
227 else if (popcodep->match == DIP_OPCODE)
228 /* We don't allow postincrement when the prefix is DIP, so use a
229 different table for DIP. */
230 prefix_opc_table = dip_prefixes;
231 else
232 prefix_opc_table = rest_prefixes;
233 }
234
235 if (prefix_insn != NO_CRIS_PREFIX
236 && prefix_opc_table[insn] != NULL)
237 max_matchedp = prefix_opc_table[insn];
238 else if (prefix_insn == NO_CRIS_PREFIX && opc_table[insn] != NULL)
239 max_matchedp = opc_table[insn];
240 else
241 {
242 const struct cris_opcode *opcodep;
243 int max_level_of_match = -1;
244
245 for (opcodep = cris_opcodes;
246 opcodep->name != NULL;
247 opcodep++)
248 {
249 int level_of_match;
250
251 /* We give a double lead for bits matching the template in
252 cris_opcodes. Not even, because then "move p8,r10" would
253 be given 2 bits lead over "clear.d r10". When there's a
254 tie, the first entry in the table wins. This is
255 deliberate, to avoid a more complicated recognition
256 formula. */
257 if ((opcodep->match & insn) == opcodep->match
258 && (opcodep->lose & insn) == 0
259 && ((level_of_match
260 = cris_constraint (opcodep->args,
261 insn,
262 prefix_insn))
263 >= 0)
264 && ((level_of_match
265 += 2 * number_of_bits (opcodep->match
266 | opcodep->lose))
267 > max_level_of_match))
268 {
269 max_matchedp = opcodep;
270 max_level_of_match = level_of_match;
271
272 /* If there was a full match, never mind looking
273 further. */
274 if (level_of_match >= 2 * 16)
275 break;
276 }
277 }
278 /* Fill in the new entry.
279
280 If there are changes to the opcode-table involving prefixes, and
281 disassembly then does not work correctly, try removing the
282 else-clause below that fills in the prefix-table. If that
283 helps, you need to change the prefix_opc_table setting above, or
284 something related. */
285 if (prefix_insn == NO_CRIS_PREFIX)
286 opc_table[insn] = max_matchedp;
287 else
288 prefix_opc_table[insn] = max_matchedp;
289 }
290
291 return max_matchedp;
292}
293
6c95a37f 294/* Format number as hex with a leading "0x" into outbuffer. */
78966507 295
6c95a37f
HPN
296static char *
297format_hex (number, outbuffer)
298 unsigned long number;
299 char *outbuffer;
300{
301 /* Obfuscate to avoid warning on 32-bit host, but properly truncate
302 negative numbers on >32-bit hosts. */
303 if (sizeof (number) > 4)
304 number &= (1 << (sizeof (number) > 4 ? 32 : 1)) - 1;
305
306 sprintf (outbuffer, "0x%lx", number);
307
308 /* Save this value for the "case" support. */
309 if (TRACE_CASE)
310 last_immediate = number;
311
312 return outbuffer + strlen (outbuffer);
313}
314
6c95a37f
HPN
315/* Format number as decimal into outbuffer. Parameter signedp says
316 whether the number should be formatted as signed (!= 0) or
317 unsigned (== 0). */
78966507 318
6c95a37f
HPN
319static char *
320format_dec (number, outbuffer, signedp)
321 long number;
322 char *outbuffer;
323 int signedp;
324{
325 last_immediate = number;
326 sprintf (outbuffer, signedp ? "%ld" : "%lu", number);
327
328 return outbuffer + strlen (outbuffer);
329}
330
6c95a37f 331/* Format the name of the general register regno into outbuffer. */
78966507 332
6c95a37f 333static char *
78966507 334format_reg (regno, outbuffer_start, with_reg_prefix)
6c95a37f 335 int regno;
78966507
HPN
336 char *outbuffer_start;
337 boolean with_reg_prefix;
6c95a37f 338{
78966507
HPN
339 char *outbuffer = outbuffer_start;
340
341 if (with_reg_prefix)
342 *outbuffer++ = REGISTER_PREFIX_CHAR;
343
6c95a37f
HPN
344 switch (regno)
345 {
346 case 15:
347 strcpy (outbuffer, "pc");
348 break;
349
350 case 14:
351 strcpy (outbuffer, "sp");
352 break;
353
354 default:
355 sprintf (outbuffer, "r%d", regno);
356 break;
357 }
358
78966507 359 return outbuffer_start + strlen (outbuffer_start);
6c95a37f
HPN
360}
361
6c95a37f
HPN
362/* Return -1 if the constraints of a bitwise-matched instruction say
363 that there is no match. Otherwise return a nonnegative number
364 indicating the confidence in the match (higher is better). */
78966507 365
6c95a37f
HPN
366static int
367cris_constraint (cs, insn, prefix_insn)
368 const char *cs;
369 unsigned int insn;
370 unsigned int prefix_insn;
371{
372 int retval = 0;
373 int tmp;
374 int prefix_ok = 0;
375
376 const char *s;
33822a8e 377 for (s = cs; *s; s++)
6c95a37f
HPN
378 switch (*s)
379 {
380 case '!':
381 /* Do not recognize "pop" if there's a prefix. */
382 if (prefix_insn != NO_CRIS_PREFIX)
383 return -1;
384 break;
385
386 case 'M':
387 /* Size modifier for "clear", i.e. special register 0, 4 or 8.
388 Check that it is one of them. Only special register 12 could
389 be mismatched, but checking for matches is more logical than
390 checking for mismatches when there are only a few cases. */
391 tmp = ((insn >> 12) & 0xf);
392 if (tmp != 0 && tmp != 4 && tmp != 8)
393 return -1;
394 break;
395
396 case 'm':
397 if ((insn & 0x30) == 0x30)
398 return -1;
399 break;
400
401 case 'S':
402 /* A prefix operand without side-effect. */
403 if (prefix_insn != NO_CRIS_PREFIX && (insn & 0x400) == 0)
404 {
405 prefix_ok = 1;
406 break;
407 }
408 else
409 return -1;
410
411 case 's':
412 case 'y':
413 /* If this is a prefixed insn with postincrement (side-effect),
414 the prefix must not be DIP. */
415 if (prefix_insn != NO_CRIS_PREFIX)
416 {
417 if (insn & 0x400)
418 {
419 const struct cris_opcode *prefix_opcodep
420 = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX);
421
422 if (prefix_opcodep->match == DIP_OPCODE)
423 return -1;
424 }
425
426 prefix_ok = 1;
427 }
428 break;
429
430 case 'B':
431 /* If we don't fall through, then the prefix is ok. */
432 prefix_ok = 1;
433
434 /* A "push" prefix. Check for valid "push" size.
435 In case of special register, it may be != 4. */
436 if (prefix_insn != NO_CRIS_PREFIX)
437 {
438 /* Match the prefix insn to BDAPQ. */
439 const struct cris_opcode *prefix_opcodep
440 = get_opcode_entry (prefix_insn, NO_CRIS_PREFIX);
441
442 if (prefix_opcodep->match == BDAP_QUICK_OPCODE)
443 {
444 int pushsize = (prefix_insn & 255);
445
446 if (pushsize > 127)
447 pushsize -= 256;
448
449 if (s[1] == 'P')
450 {
451 unsigned int spec_reg = (insn >> 12) & 15;
452 const struct cris_spec_reg *sregp
453 = spec_reg_info (spec_reg);
454
455 /* For a special-register, the "prefix size" must
456 match the size of the register. */
457 if (sregp && sregp->reg_size == (unsigned int) -pushsize)
458 break;
459 }
460 else if (s[1] == 'R')
461 {
462 if ((insn & 0x30) == 0x20 && pushsize == -4)
463 break;
464 }
465 /* FIXME: Should abort here; next constraint letter
466 *must* be 'P' or 'R'. */
467 }
468 }
469 return -1;
470
471 case 'D':
472 retval = (((insn >> 12) & 15) == (insn & 15));
473 if (!retval)
474 return -1;
475 else
476 retval += 4;
477 break;
478
479 case 'P':
480 {
481 const struct cris_spec_reg *sregp
482 = spec_reg_info ((insn >> 12) & 15);
483
484 /* Since we match four bits, we will give a value of 4-1 = 3
485 in a match. If there is a corresponding exact match of a
486 special register in another pattern, it will get a value of
487 4, which will be higher. This should be correct in that an
488 exact pattern would match better than a general pattern.
489
490 Note that there is a reason for not returning zero; the
491 pattern for "clear" is partly matched in the bit-pattern
492 (the two lower bits must be zero), while the bit-pattern
493 for a move from a special register is matched in the
494 register constraint. */
495
496 if (sregp != NULL)
497 {
498 retval += 3;
499 break;
500 }
501 else
502 return -1;
503 }
504 }
505
506 if (prefix_insn != NO_CRIS_PREFIX && ! prefix_ok)
507 return -1;
508
509 return retval;
510}
511
6c95a37f 512/* Return the length of an instruction. */
78966507 513
6c95a37f
HPN
514static unsigned
515bytes_to_skip (insn, matchedp)
516 unsigned int insn;
517 const struct cris_opcode *matchedp;
518{
519 /* Each insn is a word plus "immediate" operands. */
520 unsigned to_skip = 2;
521 const char *template = matchedp->args;
522 const char *s;
523
524 for (s = template; *s; s++)
525 if (*s == 's' && (insn & 0x400) && (insn & 15) == 15)
526 {
527 /* Immediate via [pc+], so we have to check the size of the
528 operand. */
529 int mode_size = 1 << ((insn >> 4) & (*template == 'z' ? 1 : 3));
530
531 if (matchedp->imm_oprnd_size == SIZE_FIX_32)
532 to_skip += 4;
533 else if (matchedp->imm_oprnd_size == SIZE_SPEC_REG)
534 {
535 const struct cris_spec_reg *sregp
536 = spec_reg_info ((insn >> 12) & 15);
537
538 /* FIXME: Improve error handling; should have been caught
539 earlier. */
540 if (sregp == NULL)
541 return 2;
542
543 /* PC is incremented by two, not one, for a byte. */
544 to_skip += (sregp->reg_size + 1) & ~1;
545 }
546 else
547 to_skip += (mode_size + 1) & ~1;
548 }
549 else if (*s == 'b')
550 to_skip += 2;
551
552 return to_skip;
553}
554
6c95a37f 555/* Print condition code flags. */
78966507 556
6c95a37f
HPN
557static char *
558print_flags (insn, cp)
559 unsigned int insn;
560 char *cp;
561{
562 /* Use the v8 (Etrax 100) flag definitions for disassembly.
563 The differences with v0 (Etrax 1..4) vs. Svinto are:
564 v0 'd' <=> v8 'm'
565 v0 'e' <=> v8 'b'. */
566 static const char fnames[] = "cvznxibm";
567
568 unsigned char flagbits = (((insn >> 8) & 0xf0) | (insn & 15));
569 int i;
570
571 for (i = 0; i < 8; i++)
572 if (flagbits & (1 << i))
573 *cp++ = fnames[i];
574
575 return cp;
576}
577
6c95a37f
HPN
578/* Print out an insn with its operands, and update the info->insn_type
579 fields. The prefix_opcodep and the rest hold a prefix insn that is
580 supposed to be output as an address mode. */
78966507 581
6c95a37f
HPN
582static void
583print_with_operands (opcodep, insn, buffer, addr, info, prefix_opcodep,
78966507 584 prefix_insn, prefix_buffer, with_reg_prefix)
6c95a37f
HPN
585 const struct cris_opcode *opcodep;
586 unsigned int insn;
587 unsigned char *buffer;
588 bfd_vma addr;
589 disassemble_info *info;
590
591 /* If a prefix insn was before this insn (and is supposed to be
592 output as an address), here is a description of it. */
593 const struct cris_opcode *prefix_opcodep;
594 unsigned int prefix_insn;
595 unsigned char *prefix_buffer;
78966507 596 boolean with_reg_prefix;
6c95a37f
HPN
597{
598 /* Get a buffer of somewhat reasonable size where we store
599 intermediate parts of the insn. */
78966507 600 char temp[sizeof (".d [$r13=$r12-2147483648],$r10") * 2];
6c95a37f
HPN
601 char *tp = temp;
602 static const char mode_char[] = "bwd?";
603 const char *s;
604 const char *cs;
605
606 /* Print out the name first thing we do. */
607 (*info->fprintf_func) (info->stream, "%s", opcodep->name);
608
609 cs = opcodep->args;
610 s = cs;
611
612 /* Ignore any prefix indicator. */
613 if (*s == 'p')
614 s++;
615
616 if (*s == 'm' || *s == 'M' || *s == 'z')
617 {
618 *tp++ = '.';
619
620 /* Get the size-letter. */
621 *tp++ = *s == 'M'
622 ? (insn & 0x8000 ? 'd'
623 : insn & 0x4000 ? 'w' : 'b')
624 : mode_char[(insn >> 4) & (*s == 'z' ? 1 : 3)];
625
626 /* Ignore the size and the space character that follows. */
627 s += 2;
628 }
629
630 /* Add a space if this isn't a long-branch, because for those will add
631 the condition part of the name later. */
632 if (opcodep->match != (BRANCH_PC_LOW + BRANCH_INCR_HIGH * 256))
633 *tp++ = ' ';
634
635 /* Fill in the insn-type if deducible from the name (and there's no
636 better way). */
637 if (opcodep->name[0] == 'j')
638 {
639 if (strncmp (opcodep->name, "jsr", 3) == 0)
640 /* It's "jsr" or "jsrc". */
641 info->insn_type = dis_jsr;
642 else
643 /* Any other jump-type insn is considered a branch. */
644 info->insn_type = dis_branch;
645 }
646
647 /* We might know some more fields right now. */
648 info->branch_delay_insns = opcodep->delayed;
649
650 /* Handle operands. */
651 for (; *s; s++)
652 {
653 switch (*s)
654 {
655 case ',':
656 *tp++ = *s;
657 break;
658
659 case '!':
660 /* Ignore at this point; used at earlier stages to avoid recognition
661 if there's a prefixes at something that in other ways looks like
662 a "pop". */
663 break;
664
665 case 'B':
666 /* This was the prefix that made this a "push". We've already
667 handled it by recognizing it, so signal that the prefix is
668 handled by setting it to NULL. */
669 prefix_opcodep = NULL;
670 break;
671
672 case 'D':
673 case 'r':
78966507 674 tp = format_reg (insn & 15, tp, with_reg_prefix);
6c95a37f
HPN
675 break;
676
677 case 'R':
78966507 678 tp = format_reg ((insn >> 12) & 15, tp, with_reg_prefix);
6c95a37f
HPN
679 break;
680
681 case 'y':
682 case 'S':
683 case 's':
684 /* Any "normal" memory operand. */
685 if ((insn & 0x400) && (insn & 15) == 15)
686 {
687 /* We're looking at [pc+], i.e. we need to output an immediate
688 number, where the size can depend on different things. */
689 long number;
690 int signedp
691 = ((*cs == 'z' && (insn & 0x20))
692 || opcodep->match == BDAP_QUICK_OPCODE);
693 int nbytes;
694
695 if (opcodep->imm_oprnd_size == SIZE_FIX_32)
696 nbytes = 4;
697 else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
698 {
699 const struct cris_spec_reg *sregp
700 = spec_reg_info ((insn >> 12) & 15);
701
702 /* A NULL return should have been as a non-match earlier,
703 so catch it as an internal error in the error-case
704 below. */
705 if (sregp == NULL)
706 /* Whatever non-valid size. */
707 nbytes = 42;
708 else
709 /* PC is always incremented by a multiple of two. */
710 nbytes = (sregp->reg_size + 1) & ~1;
711 }
712 else
713 {
714 int mode_size = 1 << ((insn >> 4) & (*cs == 'z' ? 1 : 3));
715
716 if (mode_size == 1)
717 nbytes = 2;
718 else
719 nbytes = mode_size;
720 }
721
722 switch (nbytes)
723 {
724 case 1:
725 number = buffer[2];
726 if (signedp && number > 127)
727 number -= 256;
728 break;
729
730 case 2:
731 number = buffer[2] + buffer[3] * 256;
732 if (signedp && number > 32767)
733 number -= 65536;
734 break;
735
736 case 4:
737 number
738 = buffer[2] + buffer[3] * 256 + buffer[4] * 65536
739 + buffer[5] * 0x1000000;
740 break;
741
742 default:
743 strcpy (tp, "bug");
744 tp += 3;
745 number = 42;
746 }
747
748 if ((*cs == 'z' && (insn & 0x20))
749 || (opcodep->match == BDAP_QUICK_OPCODE
750 && (nbytes <= 2 || buffer[1 + nbytes] == 0)))
751 tp = format_dec (number, tp, signedp);
752 else
753 {
754 unsigned int highbyte = (number >> 24) & 0xff;
755
756 /* Either output this as an address or as a number. If it's
757 a dword with the same high-byte as the address of the
758 insn, assume it's an address, and also if it's a non-zero
759 non-0xff high-byte. If this is a jsr or a jump, then
760 it's definitely an address. */
761 if (nbytes == 4
762 && (highbyte == ((addr >> 24) & 0xff)
763 || (highbyte != 0 && highbyte != 0xff)
764 || info->insn_type == dis_branch
765 || info->insn_type == dis_jsr))
766 {
767 /* Finish off and output previous formatted bytes. */
768 *tp = 0;
769 tp = temp;
770 if (temp[0])
771 (*info->fprintf_func) (info->stream, "%s", temp);
772
773 (*info->print_address_func) ((bfd_vma) number, info);
774
775 info->target = number;
776 }
777 else
778 tp = format_hex (number, tp);
779 }
780 }
781 else
782 {
783 /* Not an immediate number. Then this is a (possibly
784 prefixed) memory operand. */
785 if (info->insn_type != dis_nonbranch)
786 {
787 int mode_size
788 = 1 << ((insn >> 4)
789 & (opcodep->args[0] == 'z' ? 1 : 3));
790 int size;
791 info->insn_type = dis_dref;
792 info->flags |= CRIS_DIS_FLAG_MEMREF;
793
794 if (opcodep->imm_oprnd_size == SIZE_FIX_32)
795 size = 4;
796 else if (opcodep->imm_oprnd_size == SIZE_SPEC_REG)
797 {
798 const struct cris_spec_reg *sregp
799 = spec_reg_info ((insn >> 12) & 15);
800
801 /* FIXME: Improve error handling; should have been caught
802 earlier. */
803 if (sregp == NULL)
804 size = 4;
805 else
806 size = sregp->reg_size;
807 }
808 else
809 size = mode_size;
810
811 info->data_size = size;
812 }
813
814 *tp++ = '[';
815
816 if (prefix_opcodep
817 /* We don't match dip with a postincremented field
818 as a side-effect address mode. */
819 && ((insn & 0x400) == 0
820 || prefix_opcodep->match != DIP_OPCODE))
821 {
822 if (insn & 0x400)
823 {
78966507 824 tp = format_reg (insn & 15, tp, with_reg_prefix);
6c95a37f
HPN
825 *tp++ = '=';
826 }
827
828
829 /* We mainly ignore the prefix format string when the
830 address-mode syntax is output. */
831 switch (prefix_opcodep->match)
832 {
833 case DIP_OPCODE:
834 /* It's [r], [r+] or [pc+]. */
835 if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
836 {
837 /* It's [pc+]. This cannot possibly be anything
838 but an address. */
839 unsigned long number
840 = prefix_buffer[2] + prefix_buffer[3] * 256
841 + prefix_buffer[4] * 65536
842 + prefix_buffer[5] * 0x1000000;
843
844 info->target = (bfd_vma) number;
845
846 /* Finish off and output previous formatted
847 data. */
848 *tp = 0;
849 tp = temp;
850 if (temp[0])
851 (*info->fprintf_func) (info->stream, "%s", temp);
852
853 (*info->print_address_func) ((bfd_vma) number, info);
854 }
855 else
856 {
857 /* For a memref in an address, we use target2.
858 In this case, target is zero. */
859 info->flags
860 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
861 | CRIS_DIS_FLAG_MEM_TARGET2_MEM);
862
863 info->target2 = prefix_insn & 15;
864
865 *tp++ = '[';
78966507
HPN
866 tp = format_reg (prefix_insn & 15, tp,
867 with_reg_prefix);
6c95a37f
HPN
868 if (prefix_insn & 0x400)
869 *tp++ = '+';
870 *tp++ = ']';
871 }
872 break;
873
874 case BDAP_QUICK_OPCODE:
875 {
876 int number;
877
878 number = prefix_buffer[0];
879 if (number > 127)
880 number -= 256;
881
882 /* Output "reg+num" or, if num < 0, "reg-num". */
78966507
HPN
883 tp = format_reg ((prefix_insn >> 12) & 15, tp,
884 with_reg_prefix);
6c95a37f
HPN
885 if (number >= 0)
886 *tp++ = '+';
887 tp = format_dec (number, tp, 1);
888
889 info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
890 info->target = (prefix_insn >> 12) & 15;
891 info->target2 = (bfd_vma) number;
892 break;
893 }
894
895 case BIAP_OPCODE:
896 /* Output "r+R.m". */
78966507 897 tp = format_reg (prefix_insn & 15, tp, with_reg_prefix);
6c95a37f 898 *tp++ = '+';
78966507
HPN
899 tp = format_reg ((prefix_insn >> 12) & 15, tp,
900 with_reg_prefix);
6c95a37f
HPN
901 *tp++ = '.';
902 *tp++ = mode_char[(prefix_insn >> 4) & 3];
903
904 info->flags
905 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
906 | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
907
908 | ((prefix_insn & 0x8000)
909 ? CRIS_DIS_FLAG_MEM_TARGET2_MULT4
910 : ((prefix_insn & 0x8000)
911 ? CRIS_DIS_FLAG_MEM_TARGET2_MULT2 : 0)));
912
913 /* Is it the casejump? It's a "adds.w [pc+r%d.w],pc". */
914 if (insn == 0xf83f && (prefix_insn & ~0xf000) == 0x55f)
915 /* Then start interpreting data as offsets. */
916 case_offset_counter = no_of_case_offsets;
917 break;
918
919 case BDAP_INDIR_OPCODE:
920 /* Output "r+s.m", or, if "s" is [pc+], "r+s" or
921 "r-s". */
78966507
HPN
922 tp = format_reg ((prefix_insn >> 12) & 15, tp,
923 with_reg_prefix);
6c95a37f
HPN
924
925 if ((prefix_insn & 0x400) && (prefix_insn & 15) == 15)
926 {
927 long number;
928 unsigned int nbytes;
929
930 /* It's a value. Get its size. */
931 int mode_size = 1 << ((prefix_insn >> 4) & 3);
932
933 if (mode_size == 1)
934 nbytes = 2;
935 else
936 nbytes = mode_size;
937
938 switch (nbytes)
939 {
940 case 1:
941 number = prefix_buffer[2];
942 if (number > 127)
943 number -= 256;
944 break;
945
946 case 2:
947 number = prefix_buffer[2] + prefix_buffer[3] * 256;
948 if (number > 32767)
949 number -= 65536;
950 break;
951
952 case 4:
953 number
954 = prefix_buffer[2] + prefix_buffer[3] * 256
955 + prefix_buffer[4] * 65536
956 + prefix_buffer[5] * 0x1000000;
957 break;
958
959 default:
960 strcpy (tp, "bug");
961 tp += 3;
962 number = 42;
963 }
964
965 info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
966 info->target2 = (bfd_vma) number;
967
968 /* If the size is dword, then assume it's an
969 address. */
970 if (nbytes == 4)
971 {
972 /* Finish off and output previous formatted
973 bytes. */
974 *tp++ = '+';
975 *tp = 0;
976 tp = temp;
977 (*info->fprintf_func) (info->stream, "%s", temp);
978
979 (*info->print_address_func) ((bfd_vma) number, info);
980 }
981 else
982 {
983 if (number >= 0)
984 *tp++ = '+';
985 tp = format_dec (number, tp, 1);
986 }
987 }
988 else
989 {
990 /* Output "r+[R].m" or "r+[R+].m". */
991 *tp++ = '+';
992 *tp++ = '[';
78966507
HPN
993 tp = format_reg (prefix_insn & 15, tp,
994 with_reg_prefix);
6c95a37f
HPN
995 if (prefix_insn & 0x400)
996 *tp++ = '+';
997 *tp++ = ']';
998 *tp++ = '.';
999 *tp++ = mode_char[(prefix_insn >> 4) & 3];
1000
1001 info->flags
1002 |= (CRIS_DIS_FLAG_MEM_TARGET2_IS_REG
1003 | CRIS_DIS_FLAG_MEM_TARGET2_MEM
1004 | CRIS_DIS_FLAG_MEM_TARGET_IS_REG
1005
1006 | (((prefix_insn >> 4) == 2)
1007 ? 0
1008 : (((prefix_insn >> 4) & 3) == 1
1009 ? CRIS_DIS_FLAG_MEM_TARGET2_MEM_WORD
1010 : CRIS_DIS_FLAG_MEM_TARGET2_MEM_BYTE)));
1011 }
1012 break;
1013
1014 default:
1015 (*info->fprintf_func) (info->stream, "?prefix-bug");
1016 }
1017
1018 /* To mark that the prefix is used, reset it. */
1019 prefix_opcodep = NULL;
1020 }
1021 else
1022 {
78966507 1023 tp = format_reg (insn & 15, tp, with_reg_prefix);
6c95a37f
HPN
1024
1025 info->flags |= CRIS_DIS_FLAG_MEM_TARGET_IS_REG;
1026 info->target = insn & 15;
1027
1028 if (insn & 0x400)
1029 *tp++ = '+';
1030 }
1031 *tp++ = ']';
1032 }
1033 break;
1034
1035 case 'x':
78966507 1036 tp = format_reg ((insn >> 12) & 15, tp, with_reg_prefix);
6c95a37f
HPN
1037 *tp++ = '.';
1038 *tp++ = mode_char[(insn >> 4) & 3];
1039 break;
1040
1041 case 'I':
1042 tp = format_dec (insn & 63, tp, 0);
1043 break;
1044
1045 case 'b':
1046 {
1047 int where = buffer[2] + buffer[3] * 256;
1048
1049 if (where > 32767)
1050 where -= 65536;
1051
1052 where += addr + 4;
1053
1054 if (insn == BA_PC_INCR_OPCODE)
1055 info->insn_type = dis_branch;
1056 else
1057 info->insn_type = dis_condbranch;
1058
1059 info->target = (bfd_vma) where;
1060
1061 *tp = 0;
1062 tp = temp;
1063 (*info->fprintf_func) (info->stream, "%s%s ",
1064 temp, cris_cc_strings[insn >> 12]);
1065
1066 (*info->print_address_func) ((bfd_vma) where, info);
1067 }
1068 break;
1069
1070 case 'c':
1071 tp = format_dec (insn & 31, tp, 0);
1072 break;
1073
1074 case 'C':
1075 tp = format_dec (insn & 15, tp, 0);
1076 break;
1077
1078 case 'o':
1079 {
1080 long offset = insn & 0xfe;
1081
1082 if (insn & 1)
1083 offset |= ~0xff;
1084
1085 if (opcodep->match == BA_QUICK_OPCODE)
1086 info->insn_type = dis_branch;
1087 else
1088 info->insn_type = dis_condbranch;
1089
1090 info->target = (bfd_vma) (addr + 2 + offset);
1091 *tp = 0;
1092 tp = temp;
1093 (*info->fprintf_func) (info->stream, "%s", temp);
1094
1095 (*info->print_address_func) ((bfd_vma) (addr + 2 + offset), info);
1096 }
1097 break;
1098
1099 case 'O':
1100 {
1101 long number = buffer[0];
1102
1103 if (number > 127)
1104 number = number - 256;
1105
1106 tp = format_dec (number, tp, 1);
1107 *tp++ = ',';
78966507 1108 tp = format_reg ((insn >> 12) & 15, tp, with_reg_prefix);
6c95a37f
HPN
1109 }
1110 break;
1111
1112 case 'f':
1113 tp = print_flags (insn, tp);
1114 break;
1115
1116 case 'i':
1117 tp = format_dec ((insn & 32) ? (insn & 31) | ~31 : insn & 31, tp, 1);
1118 break;
1119
1120 case 'P':
1121 {
1122 const struct cris_spec_reg *sregp
1123 = spec_reg_info ((insn >> 12) & 15);
1124
1125 if (sregp->name == NULL)
1126 /* Should have been caught as a non-match eariler. */
1127 *tp++ = '?';
1128 else
1129 {
78966507
HPN
1130 if (with_reg_prefix)
1131 *tp++ = REGISTER_PREFIX_CHAR;
6c95a37f
HPN
1132 strcpy (tp, sregp->name);
1133 tp += strlen (tp);
1134 }
1135 }
1136 break;
1137
1138 default:
1139 strcpy (tp, "???");
1140 tp += 3;
1141 }
1142 }
1143
1144 *tp = 0;
1145
1146 if (prefix_opcodep)
1147 (*info->fprintf_func) (info->stream, " (OOPS unused prefix \"%s: %s\")",
1148 prefix_opcodep->name, prefix_opcodep->args);
1149
1150 (*info->fprintf_func) (info->stream, "%s", temp);
1151
1152 /* Get info for matching case-tables, if we don't have any active.
1153 We assume that the last constant seen is used; either in the insn
1154 itself or in a "move.d const,rN, sub.d rN,rM"-like sequence. */
1155 if (TRACE_CASE && case_offset_counter == 0)
1156 {
1157 if (strncmp (opcodep->name, "sub", 3) == 0)
1158 case_offset = last_immediate;
1159
1160 /* It could also be an "add", if there are negative case-values. */
1161 else if (strncmp (opcodep->name, "add", 3) == 0)
1162 {
1163 /* The first case is the negated operand to the add. */
1164 case_offset = -last_immediate;
1165 }
1166 /* A bound insn will tell us the number of cases. */
1167 else if (strncmp (opcodep->name, "bound", 5) == 0)
1168 {
1169 no_of_case_offsets = last_immediate + 1;
1170 }
1171 /* A jump or jsr or branch breaks the chain of insns for a
1172 case-table, so assume default first-case again. */
1173 else if (info->insn_type == dis_jsr
1174 || info->insn_type == dis_branch
1175 || info->insn_type == dis_condbranch)
1176 case_offset = 0;
1177 }
1178}
1179
1180
1181/* Print the CRIS instruction at address memaddr on stream. Returns
78966507
HPN
1182 length of the instruction, in bytes. Prefix register names with `$' if
1183 WITH_REG_PREFIX. */
1184
1185static int
1186print_insn_cris_generic (memaddr, info, with_reg_prefix)
6c95a37f
HPN
1187 bfd_vma memaddr;
1188 disassemble_info *info;
78966507 1189 boolean with_reg_prefix;
6c95a37f
HPN
1190{
1191 int nbytes;
1192 unsigned int insn;
1193 const struct cris_opcode *matchedp;
1194 int advance = 0;
1195
1196 /* No instruction will be disassembled as longer than this number of
1197 bytes; stacked prefixes will not be expanded. */
1198 unsigned char buffer[MAX_BYTES_PER_CRIS_INSN];
1199 unsigned char *bufp;
d5b2f4d6 1200 int status = 0;
6c95a37f
HPN
1201 bfd_vma addr;
1202
1203 /* There will be an "out of range" error after the last instruction.
1204 Reading pairs of bytes in decreasing number, we hope that we will get
1205 at least the amount that we will consume.
1206
1207 If we can't get any data, or we do not get enough data, we print
1208 the error message. */
1209
1210 for (nbytes = MAX_BYTES_PER_CRIS_INSN; nbytes > 0; nbytes -= 2)
1211 {
1212 status = (*info->read_memory_func) (memaddr, buffer, nbytes, info);
1213 if (status == 0)
1214 break;
1215 }
1216
1217 /* If we did not get all we asked for, then clear the rest.
1218 Hopefully this makes a reproducible result in case of errors. */
1219 if (nbytes != MAX_BYTES_PER_CRIS_INSN)
1220 memset (buffer + nbytes, 0, MAX_BYTES_PER_CRIS_INSN - nbytes);
1221
1222 addr = memaddr;
1223 bufp = buffer;
1224
1225 /* Set some defaults for the insn info. */
1226 info->insn_info_valid = 1;
1227 info->branch_delay_insns = 0;
1228 info->data_size = 0;
1229 info->insn_type = dis_nonbranch;
1230 info->flags = 0;
1231 info->target = 0;
1232 info->target2 = 0;
1233
1234 /* If we got any data, disassemble it. */
1235 if (nbytes != 0)
1236 {
1237 matchedp = NULL;
1238
1239 insn = bufp[0] + bufp[1] * 256;
1240
1241 /* If we're in a case-table, don't disassemble the offsets. */
1242 if (TRACE_CASE && case_offset_counter != 0)
1243 {
1244 info->insn_type = dis_noninsn;
1245 advance += 2;
1246
1247 /* If to print data as offsets, then shortcut here. */
1248 (*info->fprintf_func) (info->stream, "case %d%s: -> ",
1249 case_offset + no_of_case_offsets
1250 - case_offset_counter,
1251 case_offset_counter == 1 ? "/default" :
1252 "");
1253
1254 (*info->print_address_func) ((bfd_vma)
1255 ((short) (insn)
1256 + (long) (addr
1257 - (no_of_case_offsets
1258 - case_offset_counter)
1259 * 2)), info);
1260 case_offset_counter--;
1261
1262 /* The default case start (without a "sub" or "add") must be
1263 zero. */
1264 if (case_offset_counter == 0)
1265 case_offset = 0;
1266 }
1267 else if (insn == 0)
1268 {
1269 /* We're often called to disassemble zeroes. While this is a
1270 valid "bcc .+2" insn, it is also useless enough and enough
1271 of a nuiscance that we will just output "bcc .+2" for it
1272 and signal it as a noninsn. */
1273 (*info->fprintf_func) (info->stream, "bcc .+2");
1274 info->insn_type = dis_noninsn;
1275 advance += 2;
1276 }
1277 else
1278 {
1279 const struct cris_opcode *prefix_opcodep = NULL;
1280 unsigned char *prefix_buffer = bufp;
1281 unsigned int prefix_insn = insn;
1282 int prefix_size = 0;
1283
1284 matchedp = get_opcode_entry (insn, NO_CRIS_PREFIX);
1285
1286 /* Check if we're supposed to write out prefixes as address
1287 modes and if this was a prefix. */
1288 if (matchedp != NULL && PARSE_PREFIX && matchedp->args[0] == 'p')
1289 {
1290 /* If it's a prefix, put it into the prefix vars and get the
1291 main insn. */
1292 prefix_size = bytes_to_skip (prefix_insn, matchedp);
1293 prefix_opcodep = matchedp;
1294
1295 insn = bufp[prefix_size] + bufp[prefix_size + 1] * 256;
1296 matchedp = get_opcode_entry (insn, prefix_insn);
1297
1298 if (matchedp != NULL)
1299 {
1300 addr += prefix_size;
1301 bufp += prefix_size;
1302 advance += prefix_size;
1303 }
1304 else
1305 {
1306 /* The "main" insn wasn't valid, at least not when
1307 prefixed. Put back things enough to output the
1308 prefix insn only, as a normal insn. */
1309 matchedp = prefix_opcodep;
1310 insn = prefix_insn;
1311 prefix_opcodep = NULL;
1312 }
1313 }
1314
1315 if (matchedp == NULL)
1316 {
1317 (*info->fprintf_func) (info->stream, "??0x%lx", insn);
1318 advance += 2;
1319
1320 info->insn_type = dis_noninsn;
1321 }
1322 else
1323 {
1324 advance += bytes_to_skip (insn, matchedp);
1325
1326 /* The info_type and assorted fields will be set according
1327 to the operands. */
1328 print_with_operands (matchedp, insn, bufp, addr, info,
1329 prefix_opcodep, prefix_insn,
78966507 1330 prefix_buffer, with_reg_prefix);
6c95a37f
HPN
1331 }
1332 }
1333 }
1334 else
1335 info->insn_type = dis_noninsn;
1336
1337 /* If we read less than MAX_BYTES_PER_CRIS_INSN, i.e. we got an error
1338 status when reading that much, and the insn decoding indicated a
1339 length exceeding what we read, there is an error. */
1340 if (status != 0 && (nbytes == 0 || advance > nbytes))
1341 {
1342 (*info->memory_error_func) (status, memaddr, info);
1343 return -1;
1344 }
1345
1346 /* Max supported insn size with one folded prefix insn. */
1347 info->bytes_per_line = MAX_BYTES_PER_CRIS_INSN;
1348
1349 /* I would like to set this to a fixed value larger than the actual
1350 number of bytes to print in order to avoid spaces between bytes,
1351 but objdump.c (2.9.1) does not like that, so we print 16-bit
1352 chunks, which is the next choice. */
1353 info->bytes_per_chunk = 2;
1354
1355 /* Printing bytes in order of increasing addresses makes sense,
1356 especially on a little-endian target.
1357 This is completely the opposite of what you think; setting this to
1358 BFD_ENDIAN_LITTLE will print bytes in order N..0 rather than the 0..N
1359 we want. */
1360 info->display_endian = BFD_ENDIAN_BIG;
1361
1362 return advance;
1363}
1364
78966507
HPN
1365/* Disassemble, prefixing register names with `$'. */
1366
1367static int
1368print_insn_cris_with_register_prefix (vma, info)
1369 bfd_vma vma;
1370 disassemble_info *info;
1371{
1372 return print_insn_cris_generic (vma, info, true);
1373}
1374
1375/* Disassemble, no prefixes on register names. */
1376
1377static int
1378print_insn_cris_without_register_prefix (vma, info)
1379 bfd_vma vma;
1380 disassemble_info *info;
1381{
1382 return print_insn_cris_generic (vma, info, false);
1383}
1384
1385/* Return a disassembler-function that prints registers with a `$' prefix,
1386 or one that prints registers without a prefix. */
1387
1388disassembler_ftype
1389cris_get_disassembler (abfd)
1390 bfd *abfd;
1391{
b6b0b32c
HPN
1392 /* If there's no bfd in sight, we return what is valid as input in all
1393 contexts if fed back to the assembler: disassembly *with* register
1394 prefix. */
1395 if (abfd == NULL || bfd_get_symbol_leading_char (abfd) == 0)
78966507
HPN
1396 return print_insn_cris_with_register_prefix;
1397
1398 return print_insn_cris_without_register_prefix;
1399}
1400
6c95a37f
HPN
1401/*
1402 * Local variables:
1403 * eval: (c-set-style "gnu")
1404 * indent-tabs-mode: t
1405 * End:
1406 */