Coverage Report

Created: 2026-07-25 10:20

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