sparc-dis.c 30 KB

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