dis_ppc.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. ----------------------------------------------------------------------------
  2. -- LuaJIT PPC disassembler module.
  3. --
  4. -- Copyright (C) 2005-2014 Mike Pall. All rights reserved.
  5. -- Released under the MIT/X license. See Copyright Notice in luajit.h
  6. ----------------------------------------------------------------------------
  7. -- This is a helper module used by the LuaJIT machine code dumper module.
  8. --
  9. -- It disassembles all common, non-privileged 32/64 bit PowerPC instructions
  10. -- plus the e500 SPE instructions and some Cell/Xenon extensions.
  11. --
  12. -- NYI: VMX, VMX128
  13. ------------------------------------------------------------------------------
  14. local type = type
  15. local sub, byte, format = string.sub, string.byte, string.format
  16. local match, gmatch, gsub = string.match, string.gmatch, string.gsub
  17. local concat = table.concat
  18. local bit = require("bit")
  19. local band, bor, tohex = bit.band, bit.bor, bit.tohex
  20. local lshift, rshift, arshift = bit.lshift, bit.rshift, bit.arshift
  21. ------------------------------------------------------------------------------
  22. -- Primary and extended opcode maps
  23. ------------------------------------------------------------------------------
  24. local map_crops = {
  25. shift = 1, mask = 1023,
  26. [0] = "mcrfXX",
  27. [33] = "crnor|crnotCCC=", [129] = "crandcCCC",
  28. [193] = "crxor|crclrCCC%", [225] = "crnandCCC",
  29. [257] = "crandCCC", [289] = "creqv|crsetCCC%",
  30. [417] = "crorcCCC", [449] = "cror|crmoveCCC=",
  31. [16] = "b_lrKB", [528] = "b_ctrKB",
  32. [150] = "isync",
  33. }
  34. local map_rlwinm = setmetatable({
  35. shift = 0, mask = -1,
  36. },
  37. { __index = function(t, x)
  38. local rot = band(rshift(x, 11), 31)
  39. local mb = band(rshift(x, 6), 31)
  40. local me = band(rshift(x, 1), 31)
  41. if mb == 0 and me == 31-rot then
  42. return "slwiRR~A."
  43. elseif me == 31 and mb == 32-rot then
  44. return "srwiRR~-A."
  45. else
  46. return "rlwinmRR~AAA."
  47. end
  48. end
  49. })
  50. local map_rld = {
  51. shift = 2, mask = 7,
  52. [0] = "rldiclRR~HM.", "rldicrRR~HM.", "rldicRR~HM.", "rldimiRR~HM.",
  53. {
  54. shift = 1, mask = 1,
  55. [0] = "rldclRR~RM.", "rldcrRR~RM.",
  56. },
  57. }
  58. local map_ext = setmetatable({
  59. shift = 1, mask = 1023,
  60. [0] = "cmp_YLRR", [32] = "cmpl_YLRR",
  61. [4] = "twARR", [68] = "tdARR",
  62. [8] = "subfcRRR.", [40] = "subfRRR.",
  63. [104] = "negRR.", [136] = "subfeRRR.",
  64. [200] = "subfzeRR.", [232] = "subfmeRR.",
  65. [520] = "subfcoRRR.", [552] = "subfoRRR.",
  66. [616] = "negoRR.", [648] = "subfeoRRR.",
  67. [712] = "subfzeoRR.", [744] = "subfmeoRR.",
  68. [9] = "mulhduRRR.", [73] = "mulhdRRR.", [233] = "mulldRRR.",
  69. [457] = "divduRRR.", [489] = "divdRRR.",
  70. [745] = "mulldoRRR.",
  71. [969] = "divduoRRR.", [1001] = "divdoRRR.",
  72. [10] = "addcRRR.", [138] = "addeRRR.",
  73. [202] = "addzeRR.", [234] = "addmeRR.", [266] = "addRRR.",
  74. [522] = "addcoRRR.", [650] = "addeoRRR.",
  75. [714] = "addzeoRR.", [746] = "addmeoRR.", [778] = "addoRRR.",
  76. [11] = "mulhwuRRR.", [75] = "mulhwRRR.", [235] = "mullwRRR.",
  77. [459] = "divwuRRR.", [491] = "divwRRR.",
  78. [747] = "mullwoRRR.",
  79. [971] = "divwouRRR.", [1003] = "divwoRRR.",
  80. [15] = "iselltRRR", [47] = "iselgtRRR", [79] = "iseleqRRR",
  81. [144] = { shift = 20, mask = 1, [0] = "mtcrfRZ~", "mtocrfRZ~", },
  82. [19] = { shift = 20, mask = 1, [0] = "mfcrR", "mfocrfRZ", },
  83. [371] = { shift = 11, mask = 1023, [392] = "mftbR", [424] = "mftbuR", },
  84. [339] = {
  85. shift = 11, mask = 1023,
  86. [32] = "mferR", [256] = "mflrR", [288] = "mfctrR", [16] = "mfspefscrR",
  87. },
  88. [467] = {
  89. shift = 11, mask = 1023,
  90. [32] = "mtxerR", [256] = "mtlrR", [288] = "mtctrR", [16] = "mtspefscrR",
  91. },
  92. [20] = "lwarxRR0R", [84] = "ldarxRR0R",
  93. [21] = "ldxRR0R", [53] = "lduxRRR",
  94. [149] = "stdxRR0R", [181] = "stduxRRR",
  95. [341] = "lwaxRR0R", [373] = "lwauxRRR",
  96. [23] = "lwzxRR0R", [55] = "lwzuxRRR",
  97. [87] = "lbzxRR0R", [119] = "lbzuxRRR",
  98. [151] = "stwxRR0R", [183] = "stwuxRRR",
  99. [215] = "stbxRR0R", [247] = "stbuxRRR",
  100. [279] = "lhzxRR0R", [311] = "lhzuxRRR",
  101. [343] = "lhaxRR0R", [375] = "lhauxRRR",
  102. [407] = "sthxRR0R", [439] = "sthuxRRR",
  103. [54] = "dcbst-R0R", [86] = "dcbf-R0R",
  104. [150] = "stwcxRR0R.", [214] = "stdcxRR0R.",
  105. [246] = "dcbtst-R0R", [278] = "dcbt-R0R",
  106. [310] = "eciwxRR0R", [438] = "ecowxRR0R",
  107. [470] = "dcbi-RR",
  108. [598] = {
  109. shift = 21, mask = 3,
  110. [0] = "sync", "lwsync", "ptesync",
  111. },
  112. [758] = "dcba-RR",
  113. [854] = "eieio", [982] = "icbi-R0R", [1014] = "dcbz-R0R",
  114. [26] = "cntlzwRR~", [58] = "cntlzdRR~",
  115. [122] = "popcntbRR~",
  116. [154] = "prtywRR~", [186] = "prtydRR~",
  117. [28] = "andRR~R.", [60] = "andcRR~R.", [124] = "nor|notRR~R=.",
  118. [284] = "eqvRR~R.", [316] = "xorRR~R.",
  119. [412] = "orcRR~R.", [444] = "or|mrRR~R=.", [476] = "nandRR~R.",
  120. [508] = "cmpbRR~R",
  121. [512] = "mcrxrX",
  122. [532] = "ldbrxRR0R", [660] = "stdbrxRR0R",
  123. [533] = "lswxRR0R", [597] = "lswiRR0A",
  124. [661] = "stswxRR0R", [725] = "stswiRR0A",
  125. [534] = "lwbrxRR0R", [662] = "stwbrxRR0R",
  126. [790] = "lhbrxRR0R", [918] = "sthbrxRR0R",
  127. [535] = "lfsxFR0R", [567] = "lfsuxFRR",
  128. [599] = "lfdxFR0R", [631] = "lfduxFRR",
  129. [663] = "stfsxFR0R", [695] = "stfsuxFRR",
  130. [727] = "stfdxFR0R", [759] = "stfduxFR0R",
  131. [855] = "lfiwaxFR0R",
  132. [983] = "stfiwxFR0R",
  133. [24] = "slwRR~R.",
  134. [27] = "sldRR~R.", [536] = "srwRR~R.",
  135. [792] = "srawRR~R.", [824] = "srawiRR~A.",
  136. [794] = "sradRR~R.", [826] = "sradiRR~H.", [827] = "sradiRR~H.",
  137. [922] = "extshRR~.", [954] = "extsbRR~.", [986] = "extswRR~.",
  138. [539] = "srdRR~R.",
  139. },
  140. { __index = function(t, x)
  141. if band(x, 31) == 15 then return "iselRRRC" end
  142. end
  143. })
  144. local map_ld = {
  145. shift = 0, mask = 3,
  146. [0] = "ldRRE", "lduRRE", "lwaRRE",
  147. }
  148. local map_std = {
  149. shift = 0, mask = 3,
  150. [0] = "stdRRE", "stduRRE",
  151. }
  152. local map_fps = {
  153. shift = 5, mask = 1,
  154. {
  155. shift = 1, mask = 15,
  156. [0] = false, false, "fdivsFFF.", false,
  157. "fsubsFFF.", "faddsFFF.", "fsqrtsF-F.", false,
  158. "fresF-F.", "fmulsFF-F.", "frsqrtesF-F.", false,
  159. "fmsubsFFFF~.", "fmaddsFFFF~.", "fnmsubsFFFF~.", "fnmaddsFFFF~.",
  160. }
  161. }
  162. local map_fpd = {
  163. shift = 5, mask = 1,
  164. [0] = {
  165. shift = 1, mask = 1023,
  166. [0] = "fcmpuXFF", [32] = "fcmpoXFF", [64] = "mcrfsXX",
  167. [38] = "mtfsb1A.", [70] = "mtfsb0A.", [134] = "mtfsfiA>>-A>",
  168. [8] = "fcpsgnFFF.", [40] = "fnegF-F.", [72] = "fmrF-F.",
  169. [136] = "fnabsF-F.", [264] = "fabsF-F.",
  170. [12] = "frspF-F.",
  171. [14] = "fctiwF-F.", [15] = "fctiwzF-F.",
  172. [583] = "mffsF.", [711] = "mtfsfZF.",
  173. [392] = "frinF-F.", [424] = "frizF-F.",
  174. [456] = "fripF-F.", [488] = "frimF-F.",
  175. [814] = "fctidF-F.", [815] = "fctidzF-F.", [846] = "fcfidF-F.",
  176. },
  177. {
  178. shift = 1, mask = 15,
  179. [0] = false, false, "fdivFFF.", false,
  180. "fsubFFF.", "faddFFF.", "fsqrtF-F.", "fselFFFF~.",
  181. "freF-F.", "fmulFF-F.", "frsqrteF-F.", false,
  182. "fmsubFFFF~.", "fmaddFFFF~.", "fnmsubFFFF~.", "fnmaddFFFF~.",
  183. }
  184. }
  185. local map_spe = {
  186. shift = 0, mask = 2047,
  187. [512] = "evaddwRRR", [514] = "evaddiwRAR~",
  188. [516] = "evsubwRRR~", [518] = "evsubiwRAR~",
  189. [520] = "evabsRR", [521] = "evnegRR",
  190. [522] = "evextsbRR", [523] = "evextshRR", [524] = "evrndwRR",
  191. [525] = "evcntlzwRR", [526] = "evcntlswRR",
  192. [527] = "brincRRR",
  193. [529] = "evandRRR", [530] = "evandcRRR", [534] = "evxorRRR",
  194. [535] = "evor|evmrRRR=", [536] = "evnor|evnotRRR=",
  195. [537] = "eveqvRRR", [539] = "evorcRRR", [542] = "evnandRRR",
  196. [544] = "evsrwuRRR", [545] = "evsrwsRRR",
  197. [546] = "evsrwiuRRA", [547] = "evsrwisRRA",
  198. [548] = "evslwRRR", [550] = "evslwiRRA",
  199. [552] = "evrlwRRR", [553] = "evsplatiRS",
  200. [554] = "evrlwiRRA", [555] = "evsplatfiRS",
  201. [556] = "evmergehiRRR", [557] = "evmergeloRRR",
  202. [558] = "evmergehiloRRR", [559] = "evmergelohiRRR",
  203. [560] = "evcmpgtuYRR", [561] = "evcmpgtsYRR",
  204. [562] = "evcmpltuYRR", [563] = "evcmpltsYRR",
  205. [564] = "evcmpeqYRR",
  206. [632] = "evselRRR", [633] = "evselRRRW",
  207. [634] = "evselRRRW", [635] = "evselRRRW",
  208. [636] = "evselRRRW", [637] = "evselRRRW",
  209. [638] = "evselRRRW", [639] = "evselRRRW",
  210. [640] = "evfsaddRRR", [641] = "evfssubRRR",
  211. [644] = "evfsabsRR", [645] = "evfsnabsRR", [646] = "evfsnegRR",
  212. [648] = "evfsmulRRR", [649] = "evfsdivRRR",
  213. [652] = "evfscmpgtYRR", [653] = "evfscmpltYRR", [654] = "evfscmpeqYRR",
  214. [656] = "evfscfuiR-R", [657] = "evfscfsiR-R",
  215. [658] = "evfscfufR-R", [659] = "evfscfsfR-R",
  216. [660] = "evfsctuiR-R", [661] = "evfsctsiR-R",
  217. [662] = "evfsctufR-R", [663] = "evfsctsfR-R",
  218. [664] = "evfsctuizR-R", [666] = "evfsctsizR-R",
  219. [668] = "evfststgtYRR", [669] = "evfststltYRR", [670] = "evfststeqYRR",
  220. [704] = "efsaddRRR", [705] = "efssubRRR",
  221. [708] = "efsabsRR", [709] = "efsnabsRR", [710] = "efsnegRR",
  222. [712] = "efsmulRRR", [713] = "efsdivRRR",
  223. [716] = "efscmpgtYRR", [717] = "efscmpltYRR", [718] = "efscmpeqYRR",
  224. [719] = "efscfdR-R",
  225. [720] = "efscfuiR-R", [721] = "efscfsiR-R",
  226. [722] = "efscfufR-R", [723] = "efscfsfR-R",
  227. [724] = "efsctuiR-R", [725] = "efsctsiR-R",
  228. [726] = "efsctufR-R", [727] = "efsctsfR-R",
  229. [728] = "efsctuizR-R", [730] = "efsctsizR-R",
  230. [732] = "efststgtYRR", [733] = "efststltYRR", [734] = "efststeqYRR",
  231. [736] = "efdaddRRR", [737] = "efdsubRRR",
  232. [738] = "efdcfuidR-R", [739] = "efdcfsidR-R",
  233. [740] = "efdabsRR", [741] = "efdnabsRR", [742] = "efdnegRR",
  234. [744] = "efdmulRRR", [745] = "efddivRRR",
  235. [746] = "efdctuidzR-R", [747] = "efdctsidzR-R",
  236. [748] = "efdcmpgtYRR", [749] = "efdcmpltYRR", [750] = "efdcmpeqYRR",
  237. [751] = "efdcfsR-R",
  238. [752] = "efdcfuiR-R", [753] = "efdcfsiR-R",
  239. [754] = "efdcfufR-R", [755] = "efdcfsfR-R",
  240. [756] = "efdctuiR-R", [757] = "efdctsiR-R",
  241. [758] = "efdctufR-R", [759] = "efdctsfR-R",
  242. [760] = "efdctuizR-R", [762] = "efdctsizR-R",
  243. [764] = "efdtstgtYRR", [765] = "efdtstltYRR", [766] = "efdtsteqYRR",
  244. [768] = "evlddxRR0R", [769] = "evlddRR8",
  245. [770] = "evldwxRR0R", [771] = "evldwRR8",
  246. [772] = "evldhxRR0R", [773] = "evldhRR8",
  247. [776] = "evlhhesplatxRR0R", [777] = "evlhhesplatRR2",
  248. [780] = "evlhhousplatxRR0R", [781] = "evlhhousplatRR2",
  249. [782] = "evlhhossplatxRR0R", [783] = "evlhhossplatRR2",
  250. [784] = "evlwhexRR0R", [785] = "evlwheRR4",
  251. [788] = "evlwhouxRR0R", [789] = "evlwhouRR4",
  252. [790] = "evlwhosxRR0R", [791] = "evlwhosRR4",
  253. [792] = "evlwwsplatxRR0R", [793] = "evlwwsplatRR4",
  254. [796] = "evlwhsplatxRR0R", [797] = "evlwhsplatRR4",
  255. [800] = "evstddxRR0R", [801] = "evstddRR8",
  256. [802] = "evstdwxRR0R", [803] = "evstdwRR8",
  257. [804] = "evstdhxRR0R", [805] = "evstdhRR8",
  258. [816] = "evstwhexRR0R", [817] = "evstwheRR4",
  259. [820] = "evstwhoxRR0R", [821] = "evstwhoRR4",
  260. [824] = "evstwwexRR0R", [825] = "evstwweRR4",
  261. [828] = "evstwwoxRR0R", [829] = "evstwwoRR4",
  262. [1027] = "evmhessfRRR", [1031] = "evmhossfRRR", [1032] = "evmheumiRRR",
  263. [1033] = "evmhesmiRRR", [1035] = "evmhesmfRRR", [1036] = "evmhoumiRRR",
  264. [1037] = "evmhosmiRRR", [1039] = "evmhosmfRRR", [1059] = "evmhessfaRRR",
  265. [1063] = "evmhossfaRRR", [1064] = "evmheumiaRRR", [1065] = "evmhesmiaRRR",
  266. [1067] = "evmhesmfaRRR", [1068] = "evmhoumiaRRR", [1069] = "evmhosmiaRRR",
  267. [1071] = "evmhosmfaRRR", [1095] = "evmwhssfRRR", [1096] = "evmwlumiRRR",
  268. [1100] = "evmwhumiRRR", [1101] = "evmwhsmiRRR", [1103] = "evmwhsmfRRR",
  269. [1107] = "evmwssfRRR", [1112] = "evmwumiRRR", [1113] = "evmwsmiRRR",
  270. [1115] = "evmwsmfRRR", [1127] = "evmwhssfaRRR", [1128] = "evmwlumiaRRR",
  271. [1132] = "evmwhumiaRRR", [1133] = "evmwhsmiaRRR", [1135] = "evmwhsmfaRRR",
  272. [1139] = "evmwssfaRRR", [1144] = "evmwumiaRRR", [1145] = "evmwsmiaRRR",
  273. [1147] = "evmwsmfaRRR",
  274. [1216] = "evaddusiaawRR", [1217] = "evaddssiaawRR",
  275. [1218] = "evsubfusiaawRR", [1219] = "evsubfssiaawRR",
  276. [1220] = "evmraRR",
  277. [1222] = "evdivwsRRR", [1223] = "evdivwuRRR",
  278. [1224] = "evaddumiaawRR", [1225] = "evaddsmiaawRR",
  279. [1226] = "evsubfumiaawRR", [1227] = "evsubfsmiaawRR",
  280. [1280] = "evmheusiaawRRR", [1281] = "evmhessiaawRRR",
  281. [1283] = "evmhessfaawRRR", [1284] = "evmhousiaawRRR",
  282. [1285] = "evmhossiaawRRR", [1287] = "evmhossfaawRRR",
  283. [1288] = "evmheumiaawRRR", [1289] = "evmhesmiaawRRR",
  284. [1291] = "evmhesmfaawRRR", [1292] = "evmhoumiaawRRR",
  285. [1293] = "evmhosmiaawRRR", [1295] = "evmhosmfaawRRR",
  286. [1320] = "evmhegumiaaRRR", [1321] = "evmhegsmiaaRRR",
  287. [1323] = "evmhegsmfaaRRR", [1324] = "evmhogumiaaRRR",
  288. [1325] = "evmhogsmiaaRRR", [1327] = "evmhogsmfaaRRR",
  289. [1344] = "evmwlusiaawRRR", [1345] = "evmwlssiaawRRR",
  290. [1352] = "evmwlumiaawRRR", [1353] = "evmwlsmiaawRRR",
  291. [1363] = "evmwssfaaRRR", [1368] = "evmwumiaaRRR",
  292. [1369] = "evmwsmiaaRRR", [1371] = "evmwsmfaaRRR",
  293. [1408] = "evmheusianwRRR", [1409] = "evmhessianwRRR",
  294. [1411] = "evmhessfanwRRR", [1412] = "evmhousianwRRR",
  295. [1413] = "evmhossianwRRR", [1415] = "evmhossfanwRRR",
  296. [1416] = "evmheumianwRRR", [1417] = "evmhesmianwRRR",
  297. [1419] = "evmhesmfanwRRR", [1420] = "evmhoumianwRRR",
  298. [1421] = "evmhosmianwRRR", [1423] = "evmhosmfanwRRR",
  299. [1448] = "evmhegumianRRR", [1449] = "evmhegsmianRRR",
  300. [1451] = "evmhegsmfanRRR", [1452] = "evmhogumianRRR",
  301. [1453] = "evmhogsmianRRR", [1455] = "evmhogsmfanRRR",
  302. [1472] = "evmwlusianwRRR", [1473] = "evmwlssianwRRR",
  303. [1480] = "evmwlumianwRRR", [1481] = "evmwlsmianwRRR",
  304. [1491] = "evmwssfanRRR", [1496] = "evmwumianRRR",
  305. [1497] = "evmwsmianRRR", [1499] = "evmwsmfanRRR",
  306. }
  307. local map_pri = {
  308. [0] = false, false, "tdiARI", "twiARI",
  309. map_spe, false, false, "mulliRRI",
  310. "subficRRI", false, "cmpl_iYLRU", "cmp_iYLRI",
  311. "addicRRI", "addic.RRI", "addi|liRR0I", "addis|lisRR0I",
  312. "b_KBJ", "sc", "bKJ", map_crops,
  313. "rlwimiRR~AAA.", map_rlwinm, false, "rlwnmRR~RAA.",
  314. "oriNRR~U", "orisRR~U", "xoriRR~U", "xorisRR~U",
  315. "andi.RR~U", "andis.RR~U", map_rld, map_ext,
  316. "lwzRRD", "lwzuRRD", "lbzRRD", "lbzuRRD",
  317. "stwRRD", "stwuRRD", "stbRRD", "stbuRRD",
  318. "lhzRRD", "lhzuRRD", "lhaRRD", "lhauRRD",
  319. "sthRRD", "sthuRRD", "lmwRRD", "stmwRRD",
  320. "lfsFRD", "lfsuFRD", "lfdFRD", "lfduFRD",
  321. "stfsFRD", "stfsuFRD", "stfdFRD", "stfduFRD",
  322. false, false, map_ld, map_fps,
  323. false, false, map_std, map_fpd,
  324. }
  325. ------------------------------------------------------------------------------
  326. local map_gpr = {
  327. [0] = "r0", "sp", "r2", "r3", "r4", "r5", "r6", "r7",
  328. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  329. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  330. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  331. }
  332. local map_cond = { [0] = "lt", "gt", "eq", "so", "ge", "le", "ne", "ns", }
  333. -- Format a condition bit.
  334. local function condfmt(cond)
  335. if cond <= 3 then
  336. return map_cond[band(cond, 3)]
  337. else
  338. return format("4*cr%d+%s", rshift(cond, 2), map_cond[band(cond, 3)])
  339. end
  340. end
  341. ------------------------------------------------------------------------------
  342. -- Output a nicely formatted line with an opcode and operands.
  343. local function putop(ctx, text, operands)
  344. local pos = ctx.pos
  345. local extra = ""
  346. if ctx.rel then
  347. local sym = ctx.symtab[ctx.rel]
  348. if sym then extra = "\t->"..sym end
  349. end
  350. if ctx.hexdump > 0 then
  351. ctx.out(format("%08x %s %-7s %s%s\n",
  352. ctx.addr+pos, tohex(ctx.op), text, concat(operands, ", "), extra))
  353. else
  354. ctx.out(format("%08x %-7s %s%s\n",
  355. ctx.addr+pos, text, concat(operands, ", "), extra))
  356. end
  357. ctx.pos = pos + 4
  358. end
  359. -- Fallback for unknown opcodes.
  360. local function unknown(ctx)
  361. return putop(ctx, ".long", { "0x"..tohex(ctx.op) })
  362. end
  363. -- Disassemble a single instruction.
  364. local function disass_ins(ctx)
  365. local pos = ctx.pos
  366. local b0, b1, b2, b3 = byte(ctx.code, pos+1, pos+4)
  367. local op = bor(lshift(b0, 24), lshift(b1, 16), lshift(b2, 8), b3)
  368. local operands = {}
  369. local last = nil
  370. local rs = 21
  371. ctx.op = op
  372. ctx.rel = nil
  373. local opat = map_pri[rshift(b0, 2)]
  374. while type(opat) ~= "string" do
  375. if not opat then return unknown(ctx) end
  376. opat = opat[band(rshift(op, opat.shift), opat.mask)]
  377. end
  378. local name, pat = match(opat, "^([a-z0-9_.]*)(.*)")
  379. local altname, pat2 = match(pat, "|([a-z0-9_.]*)(.*)")
  380. if altname then pat = pat2 end
  381. for p in gmatch(pat, ".") do
  382. local x = nil
  383. if p == "R" then
  384. x = map_gpr[band(rshift(op, rs), 31)]
  385. rs = rs - 5
  386. elseif p == "F" then
  387. x = "f"..band(rshift(op, rs), 31)
  388. rs = rs - 5
  389. elseif p == "A" then
  390. x = band(rshift(op, rs), 31)
  391. rs = rs - 5
  392. elseif p == "S" then
  393. x = arshift(lshift(op, 27-rs), 27)
  394. rs = rs - 5
  395. elseif p == "I" then
  396. x = arshift(lshift(op, 16), 16)
  397. elseif p == "U" then
  398. x = band(op, 0xffff)
  399. elseif p == "D" or p == "E" then
  400. local disp = arshift(lshift(op, 16), 16)
  401. if p == "E" then disp = band(disp, -4) end
  402. if last == "r0" then last = "0" end
  403. operands[#operands] = format("%d(%s)", disp, last)
  404. elseif p >= "2" and p <= "8" then
  405. local disp = band(rshift(op, rs), 31) * p
  406. if last == "r0" then last = "0" end
  407. operands[#operands] = format("%d(%s)", disp, last)
  408. elseif p == "H" then
  409. x = band(rshift(op, rs), 31) + lshift(band(op, 2), 4)
  410. rs = rs - 5
  411. elseif p == "M" then
  412. x = band(rshift(op, rs), 31) + band(op, 0x20)
  413. elseif p == "C" then
  414. x = condfmt(band(rshift(op, rs), 31))
  415. rs = rs - 5
  416. elseif p == "B" then
  417. local bo = rshift(op, 21)
  418. local cond = band(rshift(op, 16), 31)
  419. local cn = ""
  420. rs = rs - 10
  421. if band(bo, 4) == 0 then
  422. cn = band(bo, 2) == 0 and "dnz" or "dz"
  423. if band(bo, 0x10) == 0 then
  424. cn = cn..(band(bo, 8) == 0 and "f" or "t")
  425. end
  426. if band(bo, 0x10) == 0 then x = condfmt(cond) end
  427. name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+")
  428. elseif band(bo, 0x10) == 0 then
  429. cn = map_cond[band(cond, 3) + (band(bo, 8) == 0 and 4 or 0)]
  430. if cond > 3 then x = "cr"..rshift(cond, 2) end
  431. name = name..(band(bo, 1) == band(rshift(op, 15), 1) and "-" or "+")
  432. end
  433. name = gsub(name, "_", cn)
  434. elseif p == "J" then
  435. x = arshift(lshift(op, 27-rs), 29-rs)*4
  436. if band(op, 2) == 0 then x = ctx.addr + pos + x end
  437. ctx.rel = x
  438. x = "0x"..tohex(x)
  439. elseif p == "K" then
  440. if band(op, 1) ~= 0 then name = name.."l" end
  441. if band(op, 2) ~= 0 then name = name.."a" end
  442. elseif p == "X" or p == "Y" then
  443. x = band(rshift(op, rs+2), 7)
  444. if x == 0 and p == "Y" then x = nil else x = "cr"..x end
  445. rs = rs - 5
  446. elseif p == "W" then
  447. x = "cr"..band(op, 7)
  448. elseif p == "Z" then
  449. x = band(rshift(op, rs-4), 255)
  450. rs = rs - 10
  451. elseif p == ">" then
  452. operands[#operands] = rshift(operands[#operands], 1)
  453. elseif p == "0" then
  454. if last == "r0" then
  455. operands[#operands] = nil
  456. if altname then name = altname end
  457. end
  458. elseif p == "L" then
  459. name = gsub(name, "_", band(op, 0x00200000) ~= 0 and "d" or "w")
  460. elseif p == "." then
  461. if band(op, 1) == 1 then name = name.."." end
  462. elseif p == "N" then
  463. if op == 0x60000000 then name = "nop"; break end
  464. elseif p == "~" then
  465. local n = #operands
  466. operands[n-1], operands[n] = operands[n], operands[n-1]
  467. elseif p == "=" then
  468. local n = #operands
  469. if last == operands[n-1] then
  470. operands[n] = nil
  471. name = altname
  472. end
  473. elseif p == "%" then
  474. local n = #operands
  475. if last == operands[n-1] and last == operands[n-2] then
  476. operands[n] = nil
  477. operands[n-1] = nil
  478. name = altname
  479. end
  480. elseif p == "-" then
  481. rs = rs - 5
  482. else
  483. assert(false)
  484. end
  485. if x then operands[#operands+1] = x; last = x end
  486. end
  487. return putop(ctx, name, operands)
  488. end
  489. ------------------------------------------------------------------------------
  490. -- Disassemble a block of code.
  491. local function disass_block(ctx, ofs, len)
  492. if not ofs then ofs = 0 end
  493. local stop = len and ofs+len or #ctx.code
  494. stop = stop - stop % 4
  495. ctx.pos = ofs - ofs % 4
  496. ctx.rel = nil
  497. while ctx.pos < stop do disass_ins(ctx) end
  498. end
  499. -- Extended API: create a disassembler context. Then call ctx:disass(ofs, len).
  500. local function create_(code, addr, out)
  501. local ctx = {}
  502. ctx.code = code
  503. ctx.addr = addr or 0
  504. ctx.out = out or io.write
  505. ctx.symtab = {}
  506. ctx.disass = disass_block
  507. ctx.hexdump = 8
  508. return ctx
  509. end
  510. -- Simple API: disassemble code (a string) at address and output via out.
  511. local function disass_(code, addr, out)
  512. create_(code, addr, out):disass()
  513. end
  514. -- Return register name for RID.
  515. local function regname_(r)
  516. if r < 32 then return map_gpr[r] end
  517. return "f"..(r-32)
  518. end
  519. -- Public module functions.
  520. module(...)
  521. create = create_
  522. disass = disass_
  523. regname = regname_