xref: /openbsd-src/gnu/usr.bin/binutils-2.17/opcodes/sparc-dis.c (revision 3d8817e467ea46cf4772788d6804dd293abfb01a)
1*3d8817e4Smiod /* Print SPARC instructions.
2*3d8817e4Smiod    Copyright 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3*3d8817e4Smiod    2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4*3d8817e4Smiod 
5*3d8817e4Smiod    This program is free software; you can redistribute it and/or modify
6*3d8817e4Smiod    it under the terms of the GNU General Public License as published by
7*3d8817e4Smiod    the Free Software Foundation; either version 2 of the License, or
8*3d8817e4Smiod    (at your option) any later version.
9*3d8817e4Smiod 
10*3d8817e4Smiod    This program is distributed in the hope that it will be useful,
11*3d8817e4Smiod    but WITHOUT ANY WARRANTY; without even the implied warranty of
12*3d8817e4Smiod    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*3d8817e4Smiod    GNU General Public License for more details.
14*3d8817e4Smiod 
15*3d8817e4Smiod    You should have received a copy of the GNU General Public License
16*3d8817e4Smiod    along with this program; if not, write to the Free Software
17*3d8817e4Smiod    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18*3d8817e4Smiod    MA 02110-1301, USA.  */
19*3d8817e4Smiod 
20*3d8817e4Smiod #include <stdio.h>
21*3d8817e4Smiod 
22*3d8817e4Smiod #include "sysdep.h"
23*3d8817e4Smiod #include "opcode/sparc.h"
24*3d8817e4Smiod #include "dis-asm.h"
25*3d8817e4Smiod #include "libiberty.h"
26*3d8817e4Smiod #include "opintl.h"
27*3d8817e4Smiod 
28*3d8817e4Smiod /* Bitmask of v9 architectures.  */
29*3d8817e4Smiod #define MASK_V9 ((1 << SPARC_OPCODE_ARCH_V9) \
30*3d8817e4Smiod 		 | (1 << SPARC_OPCODE_ARCH_V9A) \
31*3d8817e4Smiod 		 | (1 << SPARC_OPCODE_ARCH_V9B))
32*3d8817e4Smiod /* 1 if INSN is for v9 only.  */
33*3d8817e4Smiod #define V9_ONLY_P(insn) (! ((insn)->architecture & ~MASK_V9))
34*3d8817e4Smiod /* 1 if INSN is for v9.  */
35*3d8817e4Smiod #define V9_P(insn) (((insn)->architecture & MASK_V9) != 0)
36*3d8817e4Smiod 
37*3d8817e4Smiod /* The sorted opcode table.  */
38*3d8817e4Smiod static const sparc_opcode **sorted_opcodes;
39*3d8817e4Smiod 
40*3d8817e4Smiod /* For faster lookup, after insns are sorted they are hashed.  */
41*3d8817e4Smiod /* ??? I think there is room for even more improvement.  */
42*3d8817e4Smiod 
43*3d8817e4Smiod #define HASH_SIZE 256
44*3d8817e4Smiod /* It is important that we only look at insn code bits as that is how the
45*3d8817e4Smiod    opcode table is hashed.  OPCODE_BITS is a table of valid bits for each
46*3d8817e4Smiod    of the main types (0,1,2,3).  */
47*3d8817e4Smiod static int opcode_bits[4] = { 0x01c00000, 0x0, 0x01f80000, 0x01f80000 };
48*3d8817e4Smiod #define HASH_INSN(INSN) \
49*3d8817e4Smiod   ((((INSN) >> 24) & 0xc0) | (((INSN) & opcode_bits[((INSN) >> 30) & 3]) >> 19))
50*3d8817e4Smiod typedef struct sparc_opcode_hash
51*3d8817e4Smiod {
52*3d8817e4Smiod   struct sparc_opcode_hash *next;
53*3d8817e4Smiod   const sparc_opcode *opcode;
54*3d8817e4Smiod } sparc_opcode_hash;
55*3d8817e4Smiod 
56*3d8817e4Smiod static sparc_opcode_hash *opcode_hash_table[HASH_SIZE];
57*3d8817e4Smiod 
58*3d8817e4Smiod /* Sign-extend a value which is N bits long.  */
59*3d8817e4Smiod #define	SEX(value, bits) \
60*3d8817e4Smiod 	((((int)(value)) << ((8 * sizeof (int)) - bits))	\
61*3d8817e4Smiod 			 >> ((8 * sizeof (int)) - bits) )
62*3d8817e4Smiod 
63*3d8817e4Smiod static  char *reg_names[] =
64*3d8817e4Smiod { "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
65*3d8817e4Smiod   "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
66*3d8817e4Smiod   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
67*3d8817e4Smiod   "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7",
68*3d8817e4Smiod   "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
69*3d8817e4Smiod   "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
70*3d8817e4Smiod   "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
71*3d8817e4Smiod   "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
72*3d8817e4Smiod   "f32", "f33", "f34", "f35", "f36", "f37", "f38", "f39",
73*3d8817e4Smiod   "f40", "f41", "f42", "f43", "f44", "f45", "f46", "f47",
74*3d8817e4Smiod   "f48", "f49", "f50", "f51", "f52", "f53", "f54", "f55",
75*3d8817e4Smiod   "f56", "f57", "f58", "f59", "f60", "f61", "f62", "f63",
76*3d8817e4Smiod /* psr, wim, tbr, fpsr, cpsr are v8 only.  */
77*3d8817e4Smiod   "y", "psr", "wim", "tbr", "pc", "npc", "fpsr", "cpsr"
78*3d8817e4Smiod };
79*3d8817e4Smiod 
80*3d8817e4Smiod #define	freg_names	(&reg_names[4 * 8])
81*3d8817e4Smiod 
82*3d8817e4Smiod /* These are ordered according to there register number in
83*3d8817e4Smiod    rdpr and wrpr insns.  */
84*3d8817e4Smiod static char *v9_priv_reg_names[] =
85*3d8817e4Smiod {
86*3d8817e4Smiod   "tpc", "tnpc", "tstate", "tt", "tick", "tba", "pstate", "tl",
87*3d8817e4Smiod   "pil", "cwp", "cansave", "canrestore", "cleanwin", "otherwin",
88*3d8817e4Smiod   "wstate", "fq", "gl"
89*3d8817e4Smiod   /* "ver" - special cased */
90*3d8817e4Smiod };
91*3d8817e4Smiod 
92*3d8817e4Smiod /* These are ordered according to there register number in
93*3d8817e4Smiod    rdhpr and wrhpr insns.  */
94*3d8817e4Smiod static char *v9_hpriv_reg_names[] =
95*3d8817e4Smiod {
96*3d8817e4Smiod   "hpstate", "htstate", "resv2", "hintp", "resv4", "htba", "hver",
97*3d8817e4Smiod   "resv7", "resv8", "resv9", "resv10", "resv11", "resv12", "resv13",
98*3d8817e4Smiod   "resv14", "resv15", "resv16", "resv17", "resv18", "resv19", "resv20",
99*3d8817e4Smiod   "resv21", "resv22", "resv23", "resv24", "resv25", "resv26", "resv27",
100*3d8817e4Smiod   "resv28", "resv29", "resv30", "hstick_cmpr"
101*3d8817e4Smiod };
102*3d8817e4Smiod 
103*3d8817e4Smiod /* These are ordered according to there register number in
104*3d8817e4Smiod    rd and wr insns (-16).  */
105*3d8817e4Smiod static char *v9a_asr_reg_names[] =
106*3d8817e4Smiod {
107*3d8817e4Smiod   "pcr", "pic", "dcr", "gsr", "set_softint", "clear_softint",
108*3d8817e4Smiod   "softint", "tick_cmpr", "sys_tick", "sys_tick_cmpr"
109*3d8817e4Smiod };
110*3d8817e4Smiod 
111*3d8817e4Smiod /* Macros used to extract instruction fields.  Not all fields have
112*3d8817e4Smiod    macros defined here, only those which are actually used.  */
113*3d8817e4Smiod 
114*3d8817e4Smiod #define X_RD(i)      (((i) >> 25) & 0x1f)
115*3d8817e4Smiod #define X_RS1(i)     (((i) >> 14) & 0x1f)
116*3d8817e4Smiod #define X_LDST_I(i)  (((i) >> 13) & 1)
117*3d8817e4Smiod #define X_ASI(i)     (((i) >> 5) & 0xff)
118*3d8817e4Smiod #define X_RS2(i)     (((i) >> 0) & 0x1f)
119*3d8817e4Smiod #define X_IMM(i,n)   (((i) >> 0) & ((1 << (n)) - 1))
120*3d8817e4Smiod #define X_SIMM(i,n)  SEX (X_IMM ((i), (n)), (n))
121*3d8817e4Smiod #define X_DISP22(i)  (((i) >> 0) & 0x3fffff)
122*3d8817e4Smiod #define X_IMM22(i)   X_DISP22 (i)
123*3d8817e4Smiod #define X_DISP30(i)  (((i) >> 0) & 0x3fffffff)
124*3d8817e4Smiod 
125*3d8817e4Smiod /* These are for v9.  */
126*3d8817e4Smiod #define X_DISP16(i)  (((((i) >> 20) & 3) << 14) | (((i) >> 0) & 0x3fff))
127*3d8817e4Smiod #define X_DISP19(i)  (((i) >> 0) & 0x7ffff)
128*3d8817e4Smiod #define X_MEMBAR(i)  ((i) & 0x7f)
129*3d8817e4Smiod 
130*3d8817e4Smiod /* Here is the union which was used to extract instruction fields
131*3d8817e4Smiod    before the shift and mask macros were written.
132*3d8817e4Smiod 
133*3d8817e4Smiod    union sparc_insn
134*3d8817e4Smiod      {
135*3d8817e4Smiod        unsigned long int code;
136*3d8817e4Smiod        struct
137*3d8817e4Smiod 	 {
138*3d8817e4Smiod 	   unsigned int anop:2;
139*3d8817e4Smiod 	   #define	op	ldst.anop
140*3d8817e4Smiod 	   unsigned int anrd:5;
141*3d8817e4Smiod 	   #define	rd	ldst.anrd
142*3d8817e4Smiod 	   unsigned int op3:6;
143*3d8817e4Smiod 	   unsigned int anrs1:5;
144*3d8817e4Smiod 	   #define	rs1	ldst.anrs1
145*3d8817e4Smiod 	   unsigned int i:1;
146*3d8817e4Smiod 	   unsigned int anasi:8;
147*3d8817e4Smiod 	   #define	asi	ldst.anasi
148*3d8817e4Smiod 	   unsigned int anrs2:5;
149*3d8817e4Smiod 	   #define	rs2	ldst.anrs2
150*3d8817e4Smiod 	   #define	shcnt	rs2
151*3d8817e4Smiod 	 } ldst;
152*3d8817e4Smiod        struct
153*3d8817e4Smiod 	 {
154*3d8817e4Smiod 	   unsigned int anop:2, anrd:5, op3:6, anrs1:5, i:1;
155*3d8817e4Smiod 	   unsigned int IMM13:13;
156*3d8817e4Smiod 	   #define	imm13	IMM13.IMM13
157*3d8817e4Smiod 	 } IMM13;
158*3d8817e4Smiod        struct
159*3d8817e4Smiod 	 {
160*3d8817e4Smiod 	   unsigned int anop:2;
161*3d8817e4Smiod 	   unsigned int a:1;
162*3d8817e4Smiod 	   unsigned int cond:4;
163*3d8817e4Smiod 	   unsigned int op2:3;
164*3d8817e4Smiod 	   unsigned int DISP22:22;
165*3d8817e4Smiod 	   #define	disp22	branch.DISP22
166*3d8817e4Smiod 	   #define	imm22	disp22
167*3d8817e4Smiod 	 } branch;
168*3d8817e4Smiod        struct
169*3d8817e4Smiod 	 {
170*3d8817e4Smiod 	   unsigned int anop:2;
171*3d8817e4Smiod 	   unsigned int a:1;
172*3d8817e4Smiod 	   unsigned int z:1;
173*3d8817e4Smiod 	   unsigned int rcond:3;
174*3d8817e4Smiod 	   unsigned int op2:3;
175*3d8817e4Smiod 	   unsigned int DISP16HI:2;
176*3d8817e4Smiod 	   unsigned int p:1;
177*3d8817e4Smiod 	   unsigned int _rs1:5;
178*3d8817e4Smiod 	   unsigned int DISP16LO:14;
179*3d8817e4Smiod 	 } branch16;
180*3d8817e4Smiod        struct
181*3d8817e4Smiod 	 {
182*3d8817e4Smiod 	   unsigned int anop:2;
183*3d8817e4Smiod 	   unsigned int adisp30:30;
184*3d8817e4Smiod 	   #define	disp30	call.adisp30
185*3d8817e4Smiod 	 } call;
186*3d8817e4Smiod      };  */
187*3d8817e4Smiod 
188*3d8817e4Smiod /* Nonzero if INSN is the opcode for a delayed branch.  */
189*3d8817e4Smiod 
190*3d8817e4Smiod static int
is_delayed_branch(unsigned long insn)191*3d8817e4Smiod is_delayed_branch (unsigned long insn)
192*3d8817e4Smiod {
193*3d8817e4Smiod   sparc_opcode_hash *op;
194*3d8817e4Smiod 
195*3d8817e4Smiod   for (op = opcode_hash_table[HASH_INSN (insn)]; op; op = op->next)
196*3d8817e4Smiod     {
197*3d8817e4Smiod       const sparc_opcode *opcode = op->opcode;
198*3d8817e4Smiod 
199*3d8817e4Smiod       if ((opcode->match & insn) == opcode->match
200*3d8817e4Smiod 	  && (opcode->lose & insn) == 0)
201*3d8817e4Smiod 	return opcode->flags & F_DELAYED;
202*3d8817e4Smiod     }
203*3d8817e4Smiod   return 0;
204*3d8817e4Smiod }
205*3d8817e4Smiod 
206*3d8817e4Smiod /* extern void qsort (); */
207*3d8817e4Smiod 
208*3d8817e4Smiod /* Records current mask of SPARC_OPCODE_ARCH_FOO values, used to pass value
209*3d8817e4Smiod    to compare_opcodes.  */
210*3d8817e4Smiod static unsigned int current_arch_mask;
211*3d8817e4Smiod 
212*3d8817e4Smiod /* Given BFD mach number, return a mask of SPARC_OPCODE_ARCH_FOO values.  */
213*3d8817e4Smiod 
214*3d8817e4Smiod static int
compute_arch_mask(unsigned long mach)215*3d8817e4Smiod compute_arch_mask (unsigned long mach)
216*3d8817e4Smiod {
217*3d8817e4Smiod   switch (mach)
218*3d8817e4Smiod     {
219*3d8817e4Smiod     case 0 :
220*3d8817e4Smiod     case bfd_mach_sparc :
221*3d8817e4Smiod       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V8);
222*3d8817e4Smiod     case bfd_mach_sparc_sparclet :
223*3d8817e4Smiod       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_SPARCLET);
224*3d8817e4Smiod     case bfd_mach_sparc_sparclite :
225*3d8817e4Smiod     case bfd_mach_sparc_sparclite_le :
226*3d8817e4Smiod       /* sparclites insns are recognized by default (because that's how
227*3d8817e4Smiod 	 they've always been treated, for better or worse).  Kludge this by
228*3d8817e4Smiod 	 indicating generic v8 is also selected.  */
229*3d8817e4Smiod       return (SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_SPARCLITE)
230*3d8817e4Smiod 	      | SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V8));
231*3d8817e4Smiod     case bfd_mach_sparc_v8plus :
232*3d8817e4Smiod     case bfd_mach_sparc_v9 :
233*3d8817e4Smiod       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9);
234*3d8817e4Smiod     case bfd_mach_sparc_v8plusa :
235*3d8817e4Smiod     case bfd_mach_sparc_v9a :
236*3d8817e4Smiod       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9A);
237*3d8817e4Smiod     case bfd_mach_sparc_v8plusb :
238*3d8817e4Smiod     case bfd_mach_sparc_v9b :
239*3d8817e4Smiod       return SPARC_OPCODE_ARCH_MASK (SPARC_OPCODE_ARCH_V9B);
240*3d8817e4Smiod     }
241*3d8817e4Smiod   abort ();
242*3d8817e4Smiod }
243*3d8817e4Smiod 
244*3d8817e4Smiod /* Compare opcodes A and B.  */
245*3d8817e4Smiod 
246*3d8817e4Smiod static int
compare_opcodes(const void * a,const void * b)247*3d8817e4Smiod compare_opcodes (const void * a, const void * b)
248*3d8817e4Smiod {
249*3d8817e4Smiod   sparc_opcode *op0 = * (sparc_opcode **) a;
250*3d8817e4Smiod   sparc_opcode *op1 = * (sparc_opcode **) b;
251*3d8817e4Smiod   unsigned long int match0 = op0->match, match1 = op1->match;
252*3d8817e4Smiod   unsigned long int lose0 = op0->lose, lose1 = op1->lose;
253*3d8817e4Smiod   register unsigned int i;
254*3d8817e4Smiod 
255*3d8817e4Smiod   /* If one (and only one) insn isn't supported by the current architecture,
256*3d8817e4Smiod      prefer the one that is.  If neither are supported, but they're both for
257*3d8817e4Smiod      the same architecture, continue processing.  Otherwise (both unsupported
258*3d8817e4Smiod      and for different architectures), prefer lower numbered arch's (fudged
259*3d8817e4Smiod      by comparing the bitmasks).  */
260*3d8817e4Smiod   if (op0->architecture & current_arch_mask)
261*3d8817e4Smiod     {
262*3d8817e4Smiod       if (! (op1->architecture & current_arch_mask))
263*3d8817e4Smiod 	return -1;
264*3d8817e4Smiod     }
265*3d8817e4Smiod   else
266*3d8817e4Smiod     {
267*3d8817e4Smiod       if (op1->architecture & current_arch_mask)
268*3d8817e4Smiod 	return 1;
269*3d8817e4Smiod       else if (op0->architecture != op1->architecture)
270*3d8817e4Smiod 	return op0->architecture - op1->architecture;
271*3d8817e4Smiod     }
272*3d8817e4Smiod 
273*3d8817e4Smiod   /* If a bit is set in both match and lose, there is something
274*3d8817e4Smiod      wrong with the opcode table.  */
275*3d8817e4Smiod   if (match0 & lose0)
276*3d8817e4Smiod     {
277*3d8817e4Smiod       fprintf
278*3d8817e4Smiod 	(stderr,
279*3d8817e4Smiod 	 /* xgettext:c-format */
280*3d8817e4Smiod 	 _("Internal error:  bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n"),
281*3d8817e4Smiod 	 op0->name, match0, lose0);
282*3d8817e4Smiod       op0->lose &= ~op0->match;
283*3d8817e4Smiod       lose0 = op0->lose;
284*3d8817e4Smiod     }
285*3d8817e4Smiod 
286*3d8817e4Smiod   if (match1 & lose1)
287*3d8817e4Smiod     {
288*3d8817e4Smiod       fprintf
289*3d8817e4Smiod 	(stderr,
290*3d8817e4Smiod 	 /* xgettext:c-format */
291*3d8817e4Smiod 	 _("Internal error: bad sparc-opcode.h: \"%s\", %#.8lx, %#.8lx\n"),
292*3d8817e4Smiod 	 op1->name, match1, lose1);
293*3d8817e4Smiod       op1->lose &= ~op1->match;
294*3d8817e4Smiod       lose1 = op1->lose;
295*3d8817e4Smiod     }
296*3d8817e4Smiod 
297*3d8817e4Smiod   /* Because the bits that are variable in one opcode are constant in
298*3d8817e4Smiod      another, it is important to order the opcodes in the right order.  */
299*3d8817e4Smiod   for (i = 0; i < 32; ++i)
300*3d8817e4Smiod     {
301*3d8817e4Smiod       unsigned long int x = 1 << i;
302*3d8817e4Smiod       int x0 = (match0 & x) != 0;
303*3d8817e4Smiod       int x1 = (match1 & x) != 0;
304*3d8817e4Smiod 
305*3d8817e4Smiod       if (x0 != x1)
306*3d8817e4Smiod 	return x1 - x0;
307*3d8817e4Smiod     }
308*3d8817e4Smiod 
309*3d8817e4Smiod   for (i = 0; i < 32; ++i)
310*3d8817e4Smiod     {
311*3d8817e4Smiod       unsigned long int x = 1 << i;
312*3d8817e4Smiod       int x0 = (lose0 & x) != 0;
313*3d8817e4Smiod       int x1 = (lose1 & x) != 0;
314*3d8817e4Smiod 
315*3d8817e4Smiod       if (x0 != x1)
316*3d8817e4Smiod 	return x1 - x0;
317*3d8817e4Smiod     }
318*3d8817e4Smiod 
319*3d8817e4Smiod   /* They are functionally equal.  So as long as the opcode table is
320*3d8817e4Smiod      valid, we can put whichever one first we want, on aesthetic grounds.  */
321*3d8817e4Smiod 
322*3d8817e4Smiod   /* Our first aesthetic ground is that aliases defer to real insns.  */
323*3d8817e4Smiod   {
324*3d8817e4Smiod     int alias_diff = (op0->flags & F_ALIAS) - (op1->flags & F_ALIAS);
325*3d8817e4Smiod 
326*3d8817e4Smiod     if (alias_diff != 0)
327*3d8817e4Smiod       /* Put the one that isn't an alias first.  */
328*3d8817e4Smiod       return alias_diff;
329*3d8817e4Smiod   }
330*3d8817e4Smiod 
331*3d8817e4Smiod   /* Except for aliases, two "identical" instructions had
332*3d8817e4Smiod      better have the same opcode.  This is a sanity check on the table.  */
333*3d8817e4Smiod   i = strcmp (op0->name, op1->name);
334*3d8817e4Smiod   if (i)
335*3d8817e4Smiod     {
336*3d8817e4Smiod       if (op0->flags & F_ALIAS) /* If they're both aliases, be arbitrary.  */
337*3d8817e4Smiod 	return i;
338*3d8817e4Smiod       else
339*3d8817e4Smiod 	fprintf (stderr,
340*3d8817e4Smiod 		 /* xgettext:c-format */
341*3d8817e4Smiod 		 _("Internal error: bad sparc-opcode.h: \"%s\" == \"%s\"\n"),
342*3d8817e4Smiod 		 op0->name, op1->name);
343*3d8817e4Smiod     }
344*3d8817e4Smiod 
345*3d8817e4Smiod   /* Fewer arguments are preferred.  */
346*3d8817e4Smiod   {
347*3d8817e4Smiod     int length_diff = strlen (op0->args) - strlen (op1->args);
348*3d8817e4Smiod 
349*3d8817e4Smiod     if (length_diff != 0)
350*3d8817e4Smiod       /* Put the one with fewer arguments first.  */
351*3d8817e4Smiod       return length_diff;
352*3d8817e4Smiod   }
353*3d8817e4Smiod 
354*3d8817e4Smiod   /* Put 1+i before i+1.  */
355*3d8817e4Smiod   {
356*3d8817e4Smiod     char *p0 = (char *) strchr (op0->args, '+');
357*3d8817e4Smiod     char *p1 = (char *) strchr (op1->args, '+');
358*3d8817e4Smiod 
359*3d8817e4Smiod     if (p0 && p1)
360*3d8817e4Smiod       {
361*3d8817e4Smiod 	/* There is a plus in both operands.  Note that a plus
362*3d8817e4Smiod 	   sign cannot be the first character in args,
363*3d8817e4Smiod 	   so the following [-1]'s are valid.  */
364*3d8817e4Smiod 	if (p0[-1] == 'i' && p1[1] == 'i')
365*3d8817e4Smiod 	  /* op0 is i+1 and op1 is 1+i, so op1 goes first.  */
366*3d8817e4Smiod 	  return 1;
367*3d8817e4Smiod 	if (p0[1] == 'i' && p1[-1] == 'i')
368*3d8817e4Smiod 	  /* op0 is 1+i and op1 is i+1, so op0 goes first.  */
369*3d8817e4Smiod 	  return -1;
370*3d8817e4Smiod       }
371*3d8817e4Smiod   }
372*3d8817e4Smiod 
373*3d8817e4Smiod   /* Put 1,i before i,1.  */
374*3d8817e4Smiod   {
375*3d8817e4Smiod     int i0 = strncmp (op0->args, "i,1", 3) == 0;
376*3d8817e4Smiod     int i1 = strncmp (op1->args, "i,1", 3) == 0;
377*3d8817e4Smiod 
378*3d8817e4Smiod     if (i0 ^ i1)
379*3d8817e4Smiod       return i0 - i1;
380*3d8817e4Smiod   }
381*3d8817e4Smiod 
382*3d8817e4Smiod   /* They are, as far as we can tell, identical.
383*3d8817e4Smiod      Since qsort may have rearranged the table partially, there is
384*3d8817e4Smiod      no way to tell which one was first in the opcode table as
385*3d8817e4Smiod      written, so just say there are equal.  */
386*3d8817e4Smiod   /* ??? This is no longer true now that we sort a vector of pointers,
387*3d8817e4Smiod      not the table itself.  */
388*3d8817e4Smiod   return 0;
389*3d8817e4Smiod }
390*3d8817e4Smiod 
391*3d8817e4Smiod /* Build a hash table from the opcode table.
392*3d8817e4Smiod    OPCODE_TABLE is a sorted list of pointers into the opcode table.  */
393*3d8817e4Smiod 
394*3d8817e4Smiod static void
build_hash_table(const sparc_opcode ** opcode_table,sparc_opcode_hash ** hash_table,int num_opcodes)395*3d8817e4Smiod build_hash_table (const sparc_opcode **opcode_table,
396*3d8817e4Smiod 		  sparc_opcode_hash **hash_table,
397*3d8817e4Smiod 		  int num_opcodes)
398*3d8817e4Smiod {
399*3d8817e4Smiod   int i;
400*3d8817e4Smiod   int hash_count[HASH_SIZE];
401*3d8817e4Smiod   static sparc_opcode_hash *hash_buf = NULL;
402*3d8817e4Smiod 
403*3d8817e4Smiod   /* Start at the end of the table and work backwards so that each
404*3d8817e4Smiod      chain is sorted.  */
405*3d8817e4Smiod 
406*3d8817e4Smiod   memset (hash_table, 0, HASH_SIZE * sizeof (hash_table[0]));
407*3d8817e4Smiod   memset (hash_count, 0, HASH_SIZE * sizeof (hash_count[0]));
408*3d8817e4Smiod   if (hash_buf != NULL)
409*3d8817e4Smiod     free (hash_buf);
410*3d8817e4Smiod   hash_buf = xmalloc (sizeof (* hash_buf) * num_opcodes);
411*3d8817e4Smiod   for (i = num_opcodes - 1; i >= 0; --i)
412*3d8817e4Smiod     {
413*3d8817e4Smiod       int hash = HASH_INSN (opcode_table[i]->match);
414*3d8817e4Smiod       sparc_opcode_hash *h = &hash_buf[i];
415*3d8817e4Smiod 
416*3d8817e4Smiod       h->next = hash_table[hash];
417*3d8817e4Smiod       h->opcode = opcode_table[i];
418*3d8817e4Smiod       hash_table[hash] = h;
419*3d8817e4Smiod       ++hash_count[hash];
420*3d8817e4Smiod     }
421*3d8817e4Smiod 
422*3d8817e4Smiod #if 0 /* for debugging */
423*3d8817e4Smiod   {
424*3d8817e4Smiod     int min_count = num_opcodes, max_count = 0;
425*3d8817e4Smiod     int total;
426*3d8817e4Smiod 
427*3d8817e4Smiod     for (i = 0; i < HASH_SIZE; ++i)
428*3d8817e4Smiod       {
429*3d8817e4Smiod         if (hash_count[i] < min_count)
430*3d8817e4Smiod 	  min_count = hash_count[i];
431*3d8817e4Smiod 	if (hash_count[i] > max_count)
432*3d8817e4Smiod 	  max_count = hash_count[i];
433*3d8817e4Smiod 	total += hash_count[i];
434*3d8817e4Smiod       }
435*3d8817e4Smiod 
436*3d8817e4Smiod     printf ("Opcode hash table stats: min %d, max %d, ave %f\n",
437*3d8817e4Smiod 	    min_count, max_count, (double) total / HASH_SIZE);
438*3d8817e4Smiod   }
439*3d8817e4Smiod #endif
440*3d8817e4Smiod }
441*3d8817e4Smiod 
442*3d8817e4Smiod /* Print one instruction from MEMADDR on INFO->STREAM.
443*3d8817e4Smiod 
444*3d8817e4Smiod    We suffix the instruction with a comment that gives the absolute
445*3d8817e4Smiod    address involved, as well as its symbolic form, if the instruction
446*3d8817e4Smiod    is preceded by a findable `sethi' and it either adds an immediate
447*3d8817e4Smiod    displacement to that register, or it is an `add' or `or' instruction
448*3d8817e4Smiod    on that register.  */
449*3d8817e4Smiod 
450*3d8817e4Smiod int
print_insn_sparc(bfd_vma memaddr,disassemble_info * info)451*3d8817e4Smiod print_insn_sparc (bfd_vma memaddr, disassemble_info *info)
452*3d8817e4Smiod {
453*3d8817e4Smiod   FILE *stream = info->stream;
454*3d8817e4Smiod   bfd_byte buffer[4];
455*3d8817e4Smiod   unsigned long insn;
456*3d8817e4Smiod   sparc_opcode_hash *op;
457*3d8817e4Smiod   /* Nonzero of opcode table has been initialized.  */
458*3d8817e4Smiod   static int opcodes_initialized = 0;
459*3d8817e4Smiod   /* bfd mach number of last call.  */
460*3d8817e4Smiod   static unsigned long current_mach = 0;
461*3d8817e4Smiod   bfd_vma (*getword) (const void *);
462*3d8817e4Smiod 
463*3d8817e4Smiod   if (!opcodes_initialized
464*3d8817e4Smiod       || info->mach != current_mach)
465*3d8817e4Smiod     {
466*3d8817e4Smiod       int i;
467*3d8817e4Smiod 
468*3d8817e4Smiod       current_arch_mask = compute_arch_mask (info->mach);
469*3d8817e4Smiod 
470*3d8817e4Smiod       if (!opcodes_initialized)
471*3d8817e4Smiod 	sorted_opcodes =
472*3d8817e4Smiod 	  xmalloc (sparc_num_opcodes * sizeof (sparc_opcode *));
473*3d8817e4Smiod       /* Reset the sorted table so we can resort it.  */
474*3d8817e4Smiod       for (i = 0; i < sparc_num_opcodes; ++i)
475*3d8817e4Smiod 	sorted_opcodes[i] = &sparc_opcodes[i];
476*3d8817e4Smiod       qsort ((char *) sorted_opcodes, sparc_num_opcodes,
477*3d8817e4Smiod 	     sizeof (sorted_opcodes[0]), compare_opcodes);
478*3d8817e4Smiod 
479*3d8817e4Smiod       build_hash_table (sorted_opcodes, opcode_hash_table, sparc_num_opcodes);
480*3d8817e4Smiod       current_mach = info->mach;
481*3d8817e4Smiod       opcodes_initialized = 1;
482*3d8817e4Smiod     }
483*3d8817e4Smiod 
484*3d8817e4Smiod   {
485*3d8817e4Smiod     int status =
486*3d8817e4Smiod       (*info->read_memory_func) (memaddr, buffer, sizeof (buffer), info);
487*3d8817e4Smiod 
488*3d8817e4Smiod     if (status != 0)
489*3d8817e4Smiod       {
490*3d8817e4Smiod 	(*info->memory_error_func) (status, memaddr, info);
491*3d8817e4Smiod 	return -1;
492*3d8817e4Smiod       }
493*3d8817e4Smiod   }
494*3d8817e4Smiod 
495*3d8817e4Smiod   /* On SPARClite variants such as DANlite (sparc86x), instructions
496*3d8817e4Smiod      are always big-endian even when the machine is in little-endian mode.  */
497*3d8817e4Smiod   if (info->endian == BFD_ENDIAN_BIG || info->mach == bfd_mach_sparc_sparclite)
498*3d8817e4Smiod     getword = bfd_getb32;
499*3d8817e4Smiod   else
500*3d8817e4Smiod     getword = bfd_getl32;
501*3d8817e4Smiod 
502*3d8817e4Smiod   insn = getword (buffer);
503*3d8817e4Smiod 
504*3d8817e4Smiod   info->insn_info_valid = 1;			/* We do return this info.  */
505*3d8817e4Smiod   info->insn_type = dis_nonbranch;		/* Assume non branch insn.  */
506*3d8817e4Smiod   info->branch_delay_insns = 0;			/* Assume no delay.  */
507*3d8817e4Smiod   info->target = 0;				/* Assume no target known.  */
508*3d8817e4Smiod 
509*3d8817e4Smiod   for (op = opcode_hash_table[HASH_INSN (insn)]; op; op = op->next)
510*3d8817e4Smiod     {
511*3d8817e4Smiod       const sparc_opcode *opcode = op->opcode;
512*3d8817e4Smiod 
513*3d8817e4Smiod       /* If the insn isn't supported by the current architecture, skip it.  */
514*3d8817e4Smiod       if (! (opcode->architecture & current_arch_mask))
515*3d8817e4Smiod 	continue;
516*3d8817e4Smiod 
517*3d8817e4Smiod       if ((opcode->match & insn) == opcode->match
518*3d8817e4Smiod 	  && (opcode->lose & insn) == 0)
519*3d8817e4Smiod 	{
520*3d8817e4Smiod 	  /* Nonzero means that we have found an instruction which has
521*3d8817e4Smiod 	     the effect of adding or or'ing the imm13 field to rs1.  */
522*3d8817e4Smiod 	  int imm_added_to_rs1 = 0;
523*3d8817e4Smiod 	  int imm_ored_to_rs1 = 0;
524*3d8817e4Smiod 
525*3d8817e4Smiod 	  /* Nonzero means that we have found a plus sign in the args
526*3d8817e4Smiod 	     field of the opcode table.  */
527*3d8817e4Smiod 	  int found_plus = 0;
528*3d8817e4Smiod 
529*3d8817e4Smiod 	  /* Nonzero means we have an annulled branch.  */
530*3d8817e4Smiod 	  int is_annulled = 0;
531*3d8817e4Smiod 
532*3d8817e4Smiod 	  /* Do we have an `add' or `or' instruction combining an
533*3d8817e4Smiod              immediate with rs1?  */
534*3d8817e4Smiod 	  if (opcode->match == 0x80102000) /* or */
535*3d8817e4Smiod 	    imm_ored_to_rs1 = 1;
536*3d8817e4Smiod 	  if (opcode->match == 0x80002000) /* add */
537*3d8817e4Smiod 	    imm_added_to_rs1 = 1;
538*3d8817e4Smiod 
539*3d8817e4Smiod 	  if (X_RS1 (insn) != X_RD (insn)
540*3d8817e4Smiod 	      && strchr (opcode->args, 'r') != 0)
541*3d8817e4Smiod 	      /* Can't do simple format if source and dest are different.  */
542*3d8817e4Smiod 	      continue;
543*3d8817e4Smiod 	  if (X_RS2 (insn) != X_RD (insn)
544*3d8817e4Smiod 	      && strchr (opcode->args, 'O') != 0)
545*3d8817e4Smiod 	      /* Can't do simple format if source and dest are different.  */
546*3d8817e4Smiod 	      continue;
547*3d8817e4Smiod 
548*3d8817e4Smiod 	  (*info->fprintf_func) (stream, opcode->name);
549*3d8817e4Smiod 
550*3d8817e4Smiod 	  {
551*3d8817e4Smiod 	    const char *s;
552*3d8817e4Smiod 
553*3d8817e4Smiod 	    if (opcode->args[0] != ',')
554*3d8817e4Smiod 	      (*info->fprintf_func) (stream, " ");
555*3d8817e4Smiod 
556*3d8817e4Smiod 	    for (s = opcode->args; *s != '\0'; ++s)
557*3d8817e4Smiod 	      {
558*3d8817e4Smiod 		while (*s == ',')
559*3d8817e4Smiod 		  {
560*3d8817e4Smiod 		    (*info->fprintf_func) (stream, ",");
561*3d8817e4Smiod 		    ++s;
562*3d8817e4Smiod 		    switch (*s)
563*3d8817e4Smiod 		      {
564*3d8817e4Smiod 		      case 'a':
565*3d8817e4Smiod 			(*info->fprintf_func) (stream, "a");
566*3d8817e4Smiod 			is_annulled = 1;
567*3d8817e4Smiod 			++s;
568*3d8817e4Smiod 			continue;
569*3d8817e4Smiod 		      case 'N':
570*3d8817e4Smiod 			(*info->fprintf_func) (stream, "pn");
571*3d8817e4Smiod 			++s;
572*3d8817e4Smiod 			continue;
573*3d8817e4Smiod 
574*3d8817e4Smiod 		      case 'T':
575*3d8817e4Smiod 			(*info->fprintf_func) (stream, "pt");
576*3d8817e4Smiod 			++s;
577*3d8817e4Smiod 			continue;
578*3d8817e4Smiod 
579*3d8817e4Smiod 		      default:
580*3d8817e4Smiod 			break;
581*3d8817e4Smiod 		      }
582*3d8817e4Smiod 		  }
583*3d8817e4Smiod 
584*3d8817e4Smiod 		(*info->fprintf_func) (stream, " ");
585*3d8817e4Smiod 
586*3d8817e4Smiod 		switch (*s)
587*3d8817e4Smiod 		  {
588*3d8817e4Smiod 		  case '+':
589*3d8817e4Smiod 		    found_plus = 1;
590*3d8817e4Smiod 		    /* Fall through.  */
591*3d8817e4Smiod 
592*3d8817e4Smiod 		  default:
593*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%c", *s);
594*3d8817e4Smiod 		    break;
595*3d8817e4Smiod 
596*3d8817e4Smiod 		  case '#':
597*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "0");
598*3d8817e4Smiod 		    break;
599*3d8817e4Smiod 
600*3d8817e4Smiod #define	reg(n)	(*info->fprintf_func) (stream, "%%%s", reg_names[n])
601*3d8817e4Smiod 		  case '1':
602*3d8817e4Smiod 		  case 'r':
603*3d8817e4Smiod 		    reg (X_RS1 (insn));
604*3d8817e4Smiod 		    break;
605*3d8817e4Smiod 
606*3d8817e4Smiod 		  case '2':
607*3d8817e4Smiod 		  case 'O':
608*3d8817e4Smiod 		    reg (X_RS2 (insn));
609*3d8817e4Smiod 		    break;
610*3d8817e4Smiod 
611*3d8817e4Smiod 		  case 'd':
612*3d8817e4Smiod 		    reg (X_RD (insn));
613*3d8817e4Smiod 		    break;
614*3d8817e4Smiod #undef	reg
615*3d8817e4Smiod 
616*3d8817e4Smiod #define	freg(n)		(*info->fprintf_func) (stream, "%%%s", freg_names[n])
617*3d8817e4Smiod #define	fregx(n)	(*info->fprintf_func) (stream, "%%%s", freg_names[((n) & ~1) | (((n) & 1) << 5)])
618*3d8817e4Smiod 		  case 'e':
619*3d8817e4Smiod 		    freg (X_RS1 (insn));
620*3d8817e4Smiod 		    break;
621*3d8817e4Smiod 		  case 'v':	/* Double/even.  */
622*3d8817e4Smiod 		  case 'V':	/* Quad/multiple of 4.  */
623*3d8817e4Smiod 		    fregx (X_RS1 (insn));
624*3d8817e4Smiod 		    break;
625*3d8817e4Smiod 
626*3d8817e4Smiod 		  case 'f':
627*3d8817e4Smiod 		    freg (X_RS2 (insn));
628*3d8817e4Smiod 		    break;
629*3d8817e4Smiod 		  case 'B':	/* Double/even.  */
630*3d8817e4Smiod 		  case 'R':	/* Quad/multiple of 4.  */
631*3d8817e4Smiod 		    fregx (X_RS2 (insn));
632*3d8817e4Smiod 		    break;
633*3d8817e4Smiod 
634*3d8817e4Smiod 		  case 'g':
635*3d8817e4Smiod 		    freg (X_RD (insn));
636*3d8817e4Smiod 		    break;
637*3d8817e4Smiod 		  case 'H':	/* Double/even.  */
638*3d8817e4Smiod 		  case 'J':	/* Quad/multiple of 4.  */
639*3d8817e4Smiod 		    fregx (X_RD (insn));
640*3d8817e4Smiod 		    break;
641*3d8817e4Smiod #undef	freg
642*3d8817e4Smiod #undef	fregx
643*3d8817e4Smiod 
644*3d8817e4Smiod #define	creg(n)	(*info->fprintf_func) (stream, "%%c%u", (unsigned int) (n))
645*3d8817e4Smiod 		  case 'b':
646*3d8817e4Smiod 		    creg (X_RS1 (insn));
647*3d8817e4Smiod 		    break;
648*3d8817e4Smiod 
649*3d8817e4Smiod 		  case 'c':
650*3d8817e4Smiod 		    creg (X_RS2 (insn));
651*3d8817e4Smiod 		    break;
652*3d8817e4Smiod 
653*3d8817e4Smiod 		  case 'D':
654*3d8817e4Smiod 		    creg (X_RD (insn));
655*3d8817e4Smiod 		    break;
656*3d8817e4Smiod #undef	creg
657*3d8817e4Smiod 
658*3d8817e4Smiod 		  case 'h':
659*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%hi(%#x)",
660*3d8817e4Smiod 					   ((unsigned) 0xFFFFFFFF
661*3d8817e4Smiod 					    & ((int) X_IMM22 (insn) << 10)));
662*3d8817e4Smiod 		    break;
663*3d8817e4Smiod 
664*3d8817e4Smiod 		  case 'i':	/* 13 bit immediate.  */
665*3d8817e4Smiod 		  case 'I':	/* 11 bit immediate.  */
666*3d8817e4Smiod 		  case 'j':	/* 10 bit immediate.  */
667*3d8817e4Smiod 		    {
668*3d8817e4Smiod 		      int imm;
669*3d8817e4Smiod 
670*3d8817e4Smiod 		      if (*s == 'i')
671*3d8817e4Smiod 		        imm = X_SIMM (insn, 13);
672*3d8817e4Smiod 		      else if (*s == 'I')
673*3d8817e4Smiod 			imm = X_SIMM (insn, 11);
674*3d8817e4Smiod 		      else
675*3d8817e4Smiod 			imm = X_SIMM (insn, 10);
676*3d8817e4Smiod 
677*3d8817e4Smiod 		      /* Check to see whether we have a 1+i, and take
678*3d8817e4Smiod 			 note of that fact.
679*3d8817e4Smiod 
680*3d8817e4Smiod 			 Note: because of the way we sort the table,
681*3d8817e4Smiod 			 we will be matching 1+i rather than i+1,
682*3d8817e4Smiod 			 so it is OK to assume that i is after +,
683*3d8817e4Smiod 			 not before it.  */
684*3d8817e4Smiod 		      if (found_plus)
685*3d8817e4Smiod 			imm_added_to_rs1 = 1;
686*3d8817e4Smiod 
687*3d8817e4Smiod 		      if (imm <= 9)
688*3d8817e4Smiod 			(*info->fprintf_func) (stream, "%d", imm);
689*3d8817e4Smiod 		      else
690*3d8817e4Smiod 			(*info->fprintf_func) (stream, "%#x", imm);
691*3d8817e4Smiod 		    }
692*3d8817e4Smiod 		    break;
693*3d8817e4Smiod 
694*3d8817e4Smiod 		  case 'X':	/* 5 bit unsigned immediate.  */
695*3d8817e4Smiod 		  case 'Y':	/* 6 bit unsigned immediate.  */
696*3d8817e4Smiod 		    {
697*3d8817e4Smiod 		      int imm = X_IMM (insn, *s == 'X' ? 5 : 6);
698*3d8817e4Smiod 
699*3d8817e4Smiod 		      if (imm <= 9)
700*3d8817e4Smiod 			(info->fprintf_func) (stream, "%d", imm);
701*3d8817e4Smiod 		      else
702*3d8817e4Smiod 			(info->fprintf_func) (stream, "%#x", (unsigned) imm);
703*3d8817e4Smiod 		    }
704*3d8817e4Smiod 		    break;
705*3d8817e4Smiod 
706*3d8817e4Smiod 		  case '3':
707*3d8817e4Smiod 		    (info->fprintf_func) (stream, "%ld", X_IMM (insn, 3));
708*3d8817e4Smiod 		    break;
709*3d8817e4Smiod 
710*3d8817e4Smiod 		  case 'K':
711*3d8817e4Smiod 		    {
712*3d8817e4Smiod 		      int mask = X_MEMBAR (insn);
713*3d8817e4Smiod 		      int bit = 0x40, printed_one = 0;
714*3d8817e4Smiod 		      const char *name;
715*3d8817e4Smiod 
716*3d8817e4Smiod 		      if (mask == 0)
717*3d8817e4Smiod 			(info->fprintf_func) (stream, "0");
718*3d8817e4Smiod 		      else
719*3d8817e4Smiod 			while (bit)
720*3d8817e4Smiod 			  {
721*3d8817e4Smiod 			    if (mask & bit)
722*3d8817e4Smiod 			      {
723*3d8817e4Smiod 				if (printed_one)
724*3d8817e4Smiod 				  (info->fprintf_func) (stream, "|");
725*3d8817e4Smiod 				name = sparc_decode_membar (bit);
726*3d8817e4Smiod 				(info->fprintf_func) (stream, "%s", name);
727*3d8817e4Smiod 				printed_one = 1;
728*3d8817e4Smiod 			      }
729*3d8817e4Smiod 			    bit >>= 1;
730*3d8817e4Smiod 			  }
731*3d8817e4Smiod 		      break;
732*3d8817e4Smiod 		    }
733*3d8817e4Smiod 
734*3d8817e4Smiod 		  case 'k':
735*3d8817e4Smiod 		    info->target = memaddr + SEX (X_DISP16 (insn), 16) * 4;
736*3d8817e4Smiod 		    (*info->print_address_func) (info->target, info);
737*3d8817e4Smiod 		    break;
738*3d8817e4Smiod 
739*3d8817e4Smiod 		  case 'G':
740*3d8817e4Smiod 		    info->target = memaddr + SEX (X_DISP19 (insn), 19) * 4;
741*3d8817e4Smiod 		    (*info->print_address_func) (info->target, info);
742*3d8817e4Smiod 		    break;
743*3d8817e4Smiod 
744*3d8817e4Smiod 		  case '6':
745*3d8817e4Smiod 		  case '7':
746*3d8817e4Smiod 		  case '8':
747*3d8817e4Smiod 		  case '9':
748*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%fcc%c", *s - '6' + '0');
749*3d8817e4Smiod 		    break;
750*3d8817e4Smiod 
751*3d8817e4Smiod 		  case 'z':
752*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%icc");
753*3d8817e4Smiod 		    break;
754*3d8817e4Smiod 
755*3d8817e4Smiod 		  case 'Z':
756*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%xcc");
757*3d8817e4Smiod 		    break;
758*3d8817e4Smiod 
759*3d8817e4Smiod 		  case 'E':
760*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%ccr");
761*3d8817e4Smiod 		    break;
762*3d8817e4Smiod 
763*3d8817e4Smiod 		  case 's':
764*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%fprs");
765*3d8817e4Smiod 		    break;
766*3d8817e4Smiod 
767*3d8817e4Smiod 		  case 'o':
768*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%asi");
769*3d8817e4Smiod 		    break;
770*3d8817e4Smiod 
771*3d8817e4Smiod 		  case 'W':
772*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%tick");
773*3d8817e4Smiod 		    break;
774*3d8817e4Smiod 
775*3d8817e4Smiod 		  case 'P':
776*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%pc");
777*3d8817e4Smiod 		    break;
778*3d8817e4Smiod 
779*3d8817e4Smiod 		  case '?':
780*3d8817e4Smiod 		    if (X_RS1 (insn) == 31)
781*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%ver");
782*3d8817e4Smiod 		    else if ((unsigned) X_RS1 (insn) < 17)
783*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%%s",
784*3d8817e4Smiod 					     v9_priv_reg_names[X_RS1 (insn)]);
785*3d8817e4Smiod 		    else
786*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%reserved");
787*3d8817e4Smiod 		    break;
788*3d8817e4Smiod 
789*3d8817e4Smiod 		  case '!':
790*3d8817e4Smiod 		    if ((unsigned) X_RD (insn) < 17)
791*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%%s",
792*3d8817e4Smiod 					     v9_priv_reg_names[X_RD (insn)]);
793*3d8817e4Smiod 		    else
794*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%reserved");
795*3d8817e4Smiod 		    break;
796*3d8817e4Smiod 
797*3d8817e4Smiod 		  case '$':
798*3d8817e4Smiod 		    if ((unsigned) X_RS1 (insn) < 32)
799*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%%s",
800*3d8817e4Smiod 					     v9_hpriv_reg_names[X_RS1 (insn)]);
801*3d8817e4Smiod 		    else
802*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%reserved");
803*3d8817e4Smiod 		    break;
804*3d8817e4Smiod 
805*3d8817e4Smiod 		  case '%':
806*3d8817e4Smiod 		    if ((unsigned) X_RD (insn) < 32)
807*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%%s",
808*3d8817e4Smiod 					     v9_hpriv_reg_names[X_RD (insn)]);
809*3d8817e4Smiod 		    else
810*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%reserved");
811*3d8817e4Smiod 		    break;
812*3d8817e4Smiod 
813*3d8817e4Smiod 		  case '/':
814*3d8817e4Smiod 		    if (X_RS1 (insn) < 16 || X_RS1 (insn) > 25)
815*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%reserved");
816*3d8817e4Smiod 		    else
817*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%%s",
818*3d8817e4Smiod 					     v9a_asr_reg_names[X_RS1 (insn)-16]);
819*3d8817e4Smiod 		    break;
820*3d8817e4Smiod 
821*3d8817e4Smiod 		  case '_':
822*3d8817e4Smiod 		    if (X_RD (insn) < 16 || X_RD (insn) > 25)
823*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%reserved");
824*3d8817e4Smiod 		    else
825*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "%%%s",
826*3d8817e4Smiod 					     v9a_asr_reg_names[X_RD (insn)-16]);
827*3d8817e4Smiod 		    break;
828*3d8817e4Smiod 
829*3d8817e4Smiod 		  case '*':
830*3d8817e4Smiod 		    {
831*3d8817e4Smiod 		      const char *name = sparc_decode_prefetch (X_RD (insn));
832*3d8817e4Smiod 
833*3d8817e4Smiod 		      if (name)
834*3d8817e4Smiod 			(*info->fprintf_func) (stream, "%s", name);
835*3d8817e4Smiod 		      else
836*3d8817e4Smiod 			(*info->fprintf_func) (stream, "%ld", X_RD (insn));
837*3d8817e4Smiod 		      break;
838*3d8817e4Smiod 		    }
839*3d8817e4Smiod 
840*3d8817e4Smiod 		  case 'M':
841*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%asr%ld", X_RS1 (insn));
842*3d8817e4Smiod 		    break;
843*3d8817e4Smiod 
844*3d8817e4Smiod 		  case 'm':
845*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%asr%ld", X_RD (insn));
846*3d8817e4Smiod 		    break;
847*3d8817e4Smiod 
848*3d8817e4Smiod 		  case 'L':
849*3d8817e4Smiod 		    info->target = memaddr + SEX (X_DISP30 (insn), 30) * 4;
850*3d8817e4Smiod 		    (*info->print_address_func) (info->target, info);
851*3d8817e4Smiod 		    break;
852*3d8817e4Smiod 
853*3d8817e4Smiod 		  case 'n':
854*3d8817e4Smiod 		    (*info->fprintf_func)
855*3d8817e4Smiod 		      (stream, "%#x", SEX (X_DISP22 (insn), 22));
856*3d8817e4Smiod 		    break;
857*3d8817e4Smiod 
858*3d8817e4Smiod 		  case 'l':
859*3d8817e4Smiod 		    info->target = memaddr + SEX (X_DISP22 (insn), 22) * 4;
860*3d8817e4Smiod 		    (*info->print_address_func) (info->target, info);
861*3d8817e4Smiod 		    break;
862*3d8817e4Smiod 
863*3d8817e4Smiod 		  case 'A':
864*3d8817e4Smiod 		    {
865*3d8817e4Smiod 		      const char *name = sparc_decode_asi (X_ASI (insn));
866*3d8817e4Smiod 
867*3d8817e4Smiod 		      if (name)
868*3d8817e4Smiod 			(*info->fprintf_func) (stream, "%s", name);
869*3d8817e4Smiod 		      else
870*3d8817e4Smiod 			(*info->fprintf_func) (stream, "(%ld)", X_ASI (insn));
871*3d8817e4Smiod 		      break;
872*3d8817e4Smiod 		    }
873*3d8817e4Smiod 
874*3d8817e4Smiod 		  case 'C':
875*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%csr");
876*3d8817e4Smiod 		    break;
877*3d8817e4Smiod 
878*3d8817e4Smiod 		  case 'F':
879*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%fsr");
880*3d8817e4Smiod 		    break;
881*3d8817e4Smiod 
882*3d8817e4Smiod 		  case 'p':
883*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%psr");
884*3d8817e4Smiod 		    break;
885*3d8817e4Smiod 
886*3d8817e4Smiod 		  case 'q':
887*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%fq");
888*3d8817e4Smiod 		    break;
889*3d8817e4Smiod 
890*3d8817e4Smiod 		  case 'Q':
891*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%cq");
892*3d8817e4Smiod 		    break;
893*3d8817e4Smiod 
894*3d8817e4Smiod 		  case 't':
895*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%tbr");
896*3d8817e4Smiod 		    break;
897*3d8817e4Smiod 
898*3d8817e4Smiod 		  case 'w':
899*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%wim");
900*3d8817e4Smiod 		    break;
901*3d8817e4Smiod 
902*3d8817e4Smiod 		  case 'x':
903*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%ld",
904*3d8817e4Smiod 					   ((X_LDST_I (insn) << 8)
905*3d8817e4Smiod 					    + X_ASI (insn)));
906*3d8817e4Smiod 		    break;
907*3d8817e4Smiod 
908*3d8817e4Smiod 		  case 'y':
909*3d8817e4Smiod 		    (*info->fprintf_func) (stream, "%%y");
910*3d8817e4Smiod 		    break;
911*3d8817e4Smiod 
912*3d8817e4Smiod 		  case 'u':
913*3d8817e4Smiod 		  case 'U':
914*3d8817e4Smiod 		    {
915*3d8817e4Smiod 		      int val = *s == 'U' ? X_RS1 (insn) : X_RD (insn);
916*3d8817e4Smiod 		      const char *name = sparc_decode_sparclet_cpreg (val);
917*3d8817e4Smiod 
918*3d8817e4Smiod 		      if (name)
919*3d8817e4Smiod 			(*info->fprintf_func) (stream, "%s", name);
920*3d8817e4Smiod 		      else
921*3d8817e4Smiod 			(*info->fprintf_func) (stream, "%%cpreg(%d)", val);
922*3d8817e4Smiod 		      break;
923*3d8817e4Smiod 		    }
924*3d8817e4Smiod 		  }
925*3d8817e4Smiod 	      }
926*3d8817e4Smiod 	  }
927*3d8817e4Smiod 
928*3d8817e4Smiod 	  /* If we are adding or or'ing something to rs1, then
929*3d8817e4Smiod 	     check to see whether the previous instruction was
930*3d8817e4Smiod 	     a sethi to the same register as in the sethi.
931*3d8817e4Smiod 	     If so, attempt to print the result of the add or
932*3d8817e4Smiod 	     or (in this context add and or do the same thing)
933*3d8817e4Smiod 	     and its symbolic value.  */
934*3d8817e4Smiod 	  if (imm_ored_to_rs1 || imm_added_to_rs1)
935*3d8817e4Smiod 	    {
936*3d8817e4Smiod 	      unsigned long prev_insn;
937*3d8817e4Smiod 	      int errcode;
938*3d8817e4Smiod 
939*3d8817e4Smiod 	      if (memaddr >= 4)
940*3d8817e4Smiod 		errcode =
941*3d8817e4Smiod 		  (*info->read_memory_func)
942*3d8817e4Smiod 		  (memaddr - 4, buffer, sizeof (buffer), info);
943*3d8817e4Smiod 	      else
944*3d8817e4Smiod 		errcode = 1;
945*3d8817e4Smiod 
946*3d8817e4Smiod 	      prev_insn = getword (buffer);
947*3d8817e4Smiod 
948*3d8817e4Smiod 	      if (errcode == 0)
949*3d8817e4Smiod 		{
950*3d8817e4Smiod 		  /* If it is a delayed branch, we need to look at the
951*3d8817e4Smiod 		     instruction before the delayed branch.  This handles
952*3d8817e4Smiod 		     sequences such as:
953*3d8817e4Smiod 
954*3d8817e4Smiod 		     sethi %o1, %hi(_foo), %o1
955*3d8817e4Smiod 		     call _printf
956*3d8817e4Smiod 		     or %o1, %lo(_foo), %o1  */
957*3d8817e4Smiod 
958*3d8817e4Smiod 		  if (is_delayed_branch (prev_insn))
959*3d8817e4Smiod 		    {
960*3d8817e4Smiod 		      if (memaddr >= 8)
961*3d8817e4Smiod 			errcode = (*info->read_memory_func)
962*3d8817e4Smiod 			  (memaddr - 8, buffer, sizeof (buffer), info);
963*3d8817e4Smiod 		      else
964*3d8817e4Smiod 			errcode = 1;
965*3d8817e4Smiod 
966*3d8817e4Smiod 		      prev_insn = getword (buffer);
967*3d8817e4Smiod 		    }
968*3d8817e4Smiod 		}
969*3d8817e4Smiod 
970*3d8817e4Smiod 	      /* If there was a problem reading memory, then assume
971*3d8817e4Smiod 		 the previous instruction was not sethi.  */
972*3d8817e4Smiod 	      if (errcode == 0)
973*3d8817e4Smiod 		{
974*3d8817e4Smiod 		  /* Is it sethi to the same register?  */
975*3d8817e4Smiod 		  if ((prev_insn & 0xc1c00000) == 0x01000000
976*3d8817e4Smiod 		      && X_RD (prev_insn) == X_RS1 (insn))
977*3d8817e4Smiod 		    {
978*3d8817e4Smiod 		      (*info->fprintf_func) (stream, "\t! ");
979*3d8817e4Smiod 		      info->target =
980*3d8817e4Smiod 			((unsigned) 0xFFFFFFFF
981*3d8817e4Smiod 			 & ((int) X_IMM22 (prev_insn) << 10));
982*3d8817e4Smiod 		      if (imm_added_to_rs1)
983*3d8817e4Smiod 			info->target += X_SIMM (insn, 13);
984*3d8817e4Smiod 		      else
985*3d8817e4Smiod 			info->target |= X_SIMM (insn, 13);
986*3d8817e4Smiod 		      (*info->print_address_func) (info->target, info);
987*3d8817e4Smiod 		      info->insn_type = dis_dref;
988*3d8817e4Smiod 		      info->data_size = 4;  /* FIXME!!! */
989*3d8817e4Smiod 		    }
990*3d8817e4Smiod 		}
991*3d8817e4Smiod 	    }
992*3d8817e4Smiod 
993*3d8817e4Smiod 	  if (opcode->flags & (F_UNBR|F_CONDBR|F_JSR))
994*3d8817e4Smiod 	    {
995*3d8817e4Smiod 		/* FIXME -- check is_annulled flag.  */
996*3d8817e4Smiod 	      if (opcode->flags & F_UNBR)
997*3d8817e4Smiod 		info->insn_type = dis_branch;
998*3d8817e4Smiod 	      if (opcode->flags & F_CONDBR)
999*3d8817e4Smiod 		info->insn_type = dis_condbranch;
1000*3d8817e4Smiod 	      if (opcode->flags & F_JSR)
1001*3d8817e4Smiod 		info->insn_type = dis_jsr;
1002*3d8817e4Smiod 	      if (opcode->flags & F_DELAYED)
1003*3d8817e4Smiod 		info->branch_delay_insns = 1;
1004*3d8817e4Smiod 	    }
1005*3d8817e4Smiod 
1006*3d8817e4Smiod 	  return sizeof (buffer);
1007*3d8817e4Smiod 	}
1008*3d8817e4Smiod     }
1009*3d8817e4Smiod 
1010*3d8817e4Smiod   info->insn_type = dis_noninsn;	/* Mark as non-valid instruction.  */
1011*3d8817e4Smiod   (*info->fprintf_func) (stream, _("unknown"));
1012*3d8817e4Smiod   return sizeof (buffer);
1013*3d8817e4Smiod }
1014