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