sparc-dis.c 28 KB

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