1 /* disassemble sparc instructions for objdump
2 Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc.
5 This file is part of the binutils.
7 The binutils are free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 The binutils are distributed in the hope that they will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with the binutils; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "opcode/sparc.h"
26 extern int print_address();
28 static char *reg_names
[] =
29 { "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
30 "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
31 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
32 "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
33 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
34 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
35 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
36 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
37 "y", "psr", "wim", "tbr", "pc", "npc", "fpsr", "cpsr" };
39 #define freg_names (®_names[4 * 8])
43 unsigned long int code
;
62 unsigned int _OP
:2, _RD
:5, op3
:6, _RS1
:5, i
:1;
63 unsigned int IMM13
:13;
64 #define imm13 IMM13.IMM13
72 unsigned int DISP22
:22;
73 #define disp22 branch.DISP22
78 unsigned int _OP
:2, _RD
:5, op3
:6, _RS1
:5;
79 unsigned int DISP14
:14;
80 #define disp14 DISP14.DISP14
89 unsigned int DISP21
:21;
90 #define disp21 branch2.DISP21
98 unsigned int _DISP30
:30;
99 #define disp30 call._DISP30
103 /* Nonzero if INSN is the opcode for a delayed branch. */
105 is_delayed_branch (insn
)
106 union sparc_insn insn
;
110 for (i
= 0; i
< NUMOPCODES
; ++i
)
112 const struct sparc_opcode
*opcode
= &sparc_opcodes
[i
];
113 if ((opcode
->match
& insn
.code
) == opcode
->match
114 && (opcode
->lose
& insn
.code
) == 0
115 && (opcode
->flags
&F_DELAYED
))
121 static int opcodes_sorted
= 0;
123 /* Print one instruction from MEMADDR on STREAM. */
125 print_insn_sparc (memaddr
, buffer
, stream
)
131 union sparc_insn insn
;
133 register unsigned int i
;
137 static int compare_opcodes ();
138 qsort ((char *) sparc_opcodes
, NUMOPCODES
,
139 sizeof (sparc_opcodes
[0]), compare_opcodes
);
143 memcpy(&insn
,buffer
, sizeof (insn
));
145 for (i
= 0; i
< NUMOPCODES
; ++i
)
147 const struct sparc_opcode
*opcode
= &sparc_opcodes
[i
];
148 if ((opcode
->match
& insn
.code
) == opcode
->match
149 && (opcode
->lose
& insn
.code
) == 0)
151 /* Nonzero means that we have found an instruction which has
152 the effect of adding or or'ing the imm13 field to rs1. */
153 int imm_added_to_rs1
= 0;
155 /* Nonzero means that we have found a plus sign in the args
156 field of the opcode table. */
159 /* Do we have an 'or' instruction where rs1 is the same
160 as rsd, and which has the i bit set? */
161 if (opcode
->match
== 0x80102000
162 && insn
.rs1
== insn
.rd
)
163 imm_added_to_rs1
= 1;
165 if (index (opcode
->args
, 'S') != 0)
166 /* Reject the special case for `set'.
167 The real `sethi' will match. */
169 if (insn
.rs1
!= insn
.rd
170 && index (opcode
->args
, 'r') != 0)
171 /* Can't do simple format if source and dest are different. */
174 fputs (opcode
->name
, stream
);
177 register const char *s
;
179 if (opcode
->args
[0] != ',')
181 for (s
= opcode
->args
; *s
!= '\0'; ++s
)
207 } /* switch on arg */
208 } /* while there are comma started args */
217 /* note fall-through */
219 fprintf (stream
, "%c", *s
);
226 #define reg(n) fprintf (stream, "%%%s", reg_names[n])
241 #define freg(n) fprintf (stream, "%%%s", freg_names[n])
243 case 'v': /* double/even */
244 case 'V': /* quad/multiple of 4 */
249 case 'B': /* double/even */
250 case 'R': /* quad/multiple of 4 */
255 /* Somebody who know needs to define rs3.
257 case 'u': * double/even *
258 case 'U': * quad/multiple of 4 *
265 case 'H': /* double/even */
266 case 'J': /* quad/multiple of 4 */
271 #define creg(n) fprintf (stream, "%%c%u", (unsigned int) (n))
286 fprintf (stream
, "%%hi(%#x)",
287 (unsigned int) insn
.imm22
<< 10);
292 /* We cannot trust the compiler to sign-extend
293 when extracting the bitfield, hence the shifts. */
294 int imm
= ((int) insn
.imm13
<< 19) >> 19;
296 /* Check to see whether we have a 1+i, and take
299 Note: because of the way we sort the table,
300 we will be matching 1+i rather than i+1,
301 so it is OK to assume that i is after +,
304 imm_added_to_rs1
= 1;
307 fprintf (stream
, "%d", imm
);
309 fprintf (stream
, "%#x", (unsigned) imm
);
315 print_address ((bfd_vma
)
317 + (((int) insn
.disp14
<< 18) >> 18) * 4),
322 print_address ((bfd_vma
)
324 /* We use only 19 of the 21 bits. */
325 + (((int) insn
.disp21
<< 13) >> 13) * 4),
330 fputs ("%amr", stream
);
337 fprintf (stream
, "fcc%c", *s
- '6' + '0');
341 fputs ("icc", stream
);
345 fputs ("xcc", stream
);
350 fprintf(stream
, "%%asr%d", insn
.rs1
);
354 fprintf(stream
, "%%asr%d", insn
.rd
);
358 print_address ((bfd_vma
) memaddr
+ insn
.disp30
* 4,
363 if ((insn
.code
>> 22) == 0)
364 /* Special case for `unimp'. Don't try to turn
365 it's operand into a function offset. */
366 fprintf (stream
, "%#x",
367 (unsigned) (((int) insn
.disp22
<< 10) >> 10));
369 /* We cannot trust the compiler to sign-extend
370 when extracting the bitfield, hence the shifts. */
371 print_address ((bfd_vma
)
373 + (((int) insn
.disp22
<< 10) >> 10) * 4),
378 fprintf (stream
, "(%d)", (int) insn
.asi
);
382 fputs ("%csr", stream
);
386 fputs ("%fsr", stream
);
390 fputs ("%psr", stream
);
394 fputs ("%fq", stream
);
398 fputs ("%cq", stream
);
402 fputs ("%tbr", stream
);
406 fputs ("%wim", stream
);
410 fputs ("%y", stream
);
416 /* If we are adding or or'ing something to rs1, then
417 check to see whether the previous instruction was
418 a sethi to the same register as in the sethi.
419 If so, attempt to print the result of the add or
420 or (in this context add and or do the same thing)
421 and its symbolic value. */
422 if (imm_added_to_rs1
)
424 union sparc_insn prev_insn
;
427 memcpy(&prev_insn
, buffer
-4, sizeof (prev_insn
));
431 /* If it is a delayed branch, we need to look at the
432 instruction before the delayed branch. This handles
435 sethi %o1, %hi(_foo), %o1
437 or %o1, %lo(_foo), %o1
440 if (is_delayed_branch (prev_insn
))
441 memcpy(&prev_insn
, buffer
- 8, sizeof(prev_insn
));
445 /* If there was a problem reading memory, then assume
446 the previous instruction was not sethi. */
449 /* Is it sethi to the same register? */
450 if ((prev_insn
.code
& 0xc1c00000) == 0x01000000
451 && prev_insn
.rd
== insn
.rs1
)
453 fprintf (stream
, "\t! ");
454 /* We cannot trust the compiler to sign-extend
455 when extracting the bitfield, hence the shifts. */
456 print_address (((int) prev_insn
.imm22
<< 10)
457 | (insn
.imm13
<< 19) >> 19, stream
);
462 return sizeof (insn
);
466 fprintf (stream
, "%#8x", insn
.code
);
467 return sizeof (insn
);
471 /* Compare opcodes A and B. */
474 compare_opcodes (a
, b
)
477 struct sparc_opcode
*op0
= (struct sparc_opcode
*) a
;
478 struct sparc_opcode
*op1
= (struct sparc_opcode
*) b
;
479 unsigned long int match0
= op0
->match
, match1
= op1
->match
;
480 unsigned long int lose0
= op0
->lose
, lose1
= op1
->lose
;
481 register unsigned int i
;
483 /* If a bit is set in both match and lose, there is something
484 wrong with the opcode table. */
487 fprintf (stderr
, "Internal error: bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
488 op0
->name
, match0
, lose0
);
489 op0
->lose
&= ~op0
->match
;
495 fprintf (stderr
, "Internal error: bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n",
496 op1
->name
, match1
, lose1
);
497 op1
->lose
&= ~op1
->match
;
501 /* Because the bits that are variable in one opcode are constant in
502 another, it is important to order the opcodes in the right order. */
503 for (i
= 0; i
< 32; ++i
)
505 unsigned long int x
= 1 << i
;
506 int x0
= (match0
& x
) != 0;
507 int x1
= (match1
& x
) != 0;
513 for (i
= 0; i
< 32; ++i
)
515 unsigned long int x
= 1 << i
;
516 int x0
= (lose0
& x
) != 0;
517 int x1
= (lose1
& x
) != 0;
523 /* They are functionally equal. So as long as the opcode table is
524 valid, we can put whichever one first we want, on aesthetic grounds. */
526 int length_diff
= strlen (op0
->args
) - strlen (op1
->args
);
527 if (length_diff
!= 0)
528 /* Put the one with fewer arguments first. */
532 /* Put 1+i before i+1. */
534 char *p0
= (char *) index(op0
->args
, '+');
535 char *p1
= (char *) index(op1
->args
, '+');
539 /* There is a plus in both operands. Note that a plus
540 sign cannot be the first character in args,
541 so the following [-1]'s are valid. */
542 if (p0
[-1] == 'i' && p1
[1] == 'i')
543 /* op0 is i+1 and op1 is 1+i, so op1 goes first. */
545 if (p0
[1] == 'i' && p1
[-1] == 'i')
546 /* op0 is 1+i and op1 is i+1, so op0 goes first. */
551 /* They are, as far as we can tell, identical.
552 Since qsort may have rearranged the table partially, there is
553 no way to tell which one was first in the opcode table as
554 written, so just say there are equal. */