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