MBlazeDisassembler.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. //===-- MBlazeDisassembler.cpp - Disassembler for MicroBlaze -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file is part of the MBlaze Disassembler. It contains code to translate
  11. // the data produced by the decoder into MCInsts.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "MBlaze.h"
  15. #include "MBlazeDisassembler.h"
  16. #include "llvm/MC/EDInstInfo.h"
  17. #include "llvm/MC/MCDisassembler.h"
  18. #include "llvm/MC/MCInst.h"
  19. #include "llvm/MC/MCInstrDesc.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/MemoryObject.h"
  22. #include "llvm/Support/TargetRegistry.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. // #include "MBlazeGenDecoderTables.inc"
  25. // #include "MBlazeGenRegisterNames.inc"
  26. #include "MBlazeGenEDInfo.inc"
  27. namespace llvm {
  28. extern const MCInstrDesc MBlazeInsts[];
  29. }
  30. using namespace llvm;
  31. const unsigned UNSUPPORTED = -1;
  32. static const unsigned mblazeBinary2Opcode[] = {
  33. MBlaze::ADD, MBlaze::RSUB, MBlaze::ADDC, MBlaze::RSUBC, //00,01,02,03
  34. MBlaze::ADDK, MBlaze::RSUBK, MBlaze::ADDKC, MBlaze::RSUBKC, //04,05,06,07
  35. MBlaze::ADDI, MBlaze::RSUBI, MBlaze::ADDIC, MBlaze::RSUBIC, //08,09,0A,0B
  36. MBlaze::ADDIK, MBlaze::RSUBIK, MBlaze::ADDIKC, MBlaze::RSUBIKC, //0C,0D,0E,0F
  37. MBlaze::MUL, MBlaze::BSRL, MBlaze::IDIV, MBlaze::GETD, //10,11,12,13
  38. UNSUPPORTED, UNSUPPORTED, MBlaze::FADD, UNSUPPORTED, //14,15,16,17
  39. MBlaze::MULI, MBlaze::BSRLI, UNSUPPORTED, MBlaze::GET, //18,19,1A,1B
  40. UNSUPPORTED, UNSUPPORTED, UNSUPPORTED, UNSUPPORTED, //1C,1D,1E,1F
  41. MBlaze::OR, MBlaze::AND, MBlaze::XOR, MBlaze::ANDN, //20,21,22,23
  42. MBlaze::SEXT8, MBlaze::MFS, MBlaze::BR, MBlaze::BEQ, //24,25,26,27
  43. MBlaze::ORI, MBlaze::ANDI, MBlaze::XORI, MBlaze::ANDNI, //28,29,2A,2B
  44. MBlaze::IMM, MBlaze::RTSD, MBlaze::BRI, MBlaze::BEQI, //2C,2D,2E,2F
  45. MBlaze::LBU, MBlaze::LHU, MBlaze::LW, UNSUPPORTED, //30,31,32,33
  46. MBlaze::SB, MBlaze::SH, MBlaze::SW, UNSUPPORTED, //34,35,36,37
  47. MBlaze::LBUI, MBlaze::LHUI, MBlaze::LWI, UNSUPPORTED, //38,39,3A,3B
  48. MBlaze::SBI, MBlaze::SHI, MBlaze::SWI, UNSUPPORTED, //3C,3D,3E,3F
  49. };
  50. static unsigned getRD(uint32_t insn) {
  51. if (!isMBlazeRegister((insn>>21)&0x1F))
  52. return UNSUPPORTED;
  53. return getMBlazeRegisterFromNumbering((insn>>21)&0x1F);
  54. }
  55. static unsigned getRA(uint32_t insn) {
  56. if (!getMBlazeRegisterFromNumbering((insn>>16)&0x1F))
  57. return UNSUPPORTED;
  58. return getMBlazeRegisterFromNumbering((insn>>16)&0x1F);
  59. }
  60. static unsigned getRB(uint32_t insn) {
  61. if (!getMBlazeRegisterFromNumbering((insn>>11)&0x1F))
  62. return UNSUPPORTED;
  63. return getMBlazeRegisterFromNumbering((insn>>11)&0x1F);
  64. }
  65. static int64_t getRS(uint32_t insn) {
  66. if (!isSpecialMBlazeRegister(insn&0x3FFF))
  67. return UNSUPPORTED;
  68. return getSpecialMBlazeRegisterFromNumbering(insn&0x3FFF);
  69. }
  70. static int64_t getIMM(uint32_t insn) {
  71. int16_t val = (insn & 0xFFFF);
  72. return val;
  73. }
  74. static int64_t getSHT(uint32_t insn) {
  75. int16_t val = (insn & 0x1F);
  76. return val;
  77. }
  78. static unsigned getFLAGS(int32_t insn) {
  79. return (insn & 0x7FF);
  80. }
  81. static int64_t getFSL(uint32_t insn) {
  82. int16_t val = (insn & 0xF);
  83. return val;
  84. }
  85. static unsigned decodeMUL(uint32_t insn) {
  86. switch (getFLAGS(insn)) {
  87. default: return UNSUPPORTED;
  88. case 0: return MBlaze::MUL;
  89. case 1: return MBlaze::MULH;
  90. case 2: return MBlaze::MULHSU;
  91. case 3: return MBlaze::MULHU;
  92. }
  93. }
  94. static unsigned decodeSEXT(uint32_t insn) {
  95. switch (insn&0x7FF) {
  96. default: return UNSUPPORTED;
  97. case 0x60: return MBlaze::SEXT8;
  98. case 0x68: return MBlaze::WIC;
  99. case 0x64: return MBlaze::WDC;
  100. case 0x66: return MBlaze::WDCC;
  101. case 0x74: return MBlaze::WDCF;
  102. case 0x61: return MBlaze::SEXT16;
  103. case 0x41: return MBlaze::SRL;
  104. case 0x21: return MBlaze::SRC;
  105. case 0x01: return MBlaze::SRA;
  106. case 0xE0: return MBlaze::CLZ;
  107. }
  108. }
  109. static unsigned decodeBEQ(uint32_t insn) {
  110. switch ((insn>>21)&0x1F) {
  111. default: return UNSUPPORTED;
  112. case 0x00: return MBlaze::BEQ;
  113. case 0x10: return MBlaze::BEQD;
  114. case 0x05: return MBlaze::BGE;
  115. case 0x15: return MBlaze::BGED;
  116. case 0x04: return MBlaze::BGT;
  117. case 0x14: return MBlaze::BGTD;
  118. case 0x03: return MBlaze::BLE;
  119. case 0x13: return MBlaze::BLED;
  120. case 0x02: return MBlaze::BLT;
  121. case 0x12: return MBlaze::BLTD;
  122. case 0x01: return MBlaze::BNE;
  123. case 0x11: return MBlaze::BNED;
  124. }
  125. }
  126. static unsigned decodeBEQI(uint32_t insn) {
  127. switch ((insn>>21)&0x1F) {
  128. default: return UNSUPPORTED;
  129. case 0x00: return MBlaze::BEQI;
  130. case 0x10: return MBlaze::BEQID;
  131. case 0x05: return MBlaze::BGEI;
  132. case 0x15: return MBlaze::BGEID;
  133. case 0x04: return MBlaze::BGTI;
  134. case 0x14: return MBlaze::BGTID;
  135. case 0x03: return MBlaze::BLEI;
  136. case 0x13: return MBlaze::BLEID;
  137. case 0x02: return MBlaze::BLTI;
  138. case 0x12: return MBlaze::BLTID;
  139. case 0x01: return MBlaze::BNEI;
  140. case 0x11: return MBlaze::BNEID;
  141. }
  142. }
  143. static unsigned decodeBR(uint32_t insn) {
  144. switch ((insn>>16)&0x1F) {
  145. default: return UNSUPPORTED;
  146. case 0x00: return MBlaze::BR;
  147. case 0x08: return MBlaze::BRA;
  148. case 0x0C: return MBlaze::BRK;
  149. case 0x10: return MBlaze::BRD;
  150. case 0x14: return MBlaze::BRLD;
  151. case 0x18: return MBlaze::BRAD;
  152. case 0x1C: return MBlaze::BRALD;
  153. }
  154. }
  155. static unsigned decodeBRI(uint32_t insn) {
  156. switch (insn&0x3FFFFFF) {
  157. default: break;
  158. case 0x0020004: return MBlaze::IDMEMBAR;
  159. case 0x0220004: return MBlaze::DMEMBAR;
  160. case 0x0420004: return MBlaze::IMEMBAR;
  161. }
  162. switch ((insn>>16)&0x1F) {
  163. default: return UNSUPPORTED;
  164. case 0x00: return MBlaze::BRI;
  165. case 0x08: return MBlaze::BRAI;
  166. case 0x0C: return MBlaze::BRKI;
  167. case 0x10: return MBlaze::BRID;
  168. case 0x14: return MBlaze::BRLID;
  169. case 0x18: return MBlaze::BRAID;
  170. case 0x1C: return MBlaze::BRALID;
  171. }
  172. }
  173. static unsigned decodeBSRL(uint32_t insn) {
  174. switch ((insn>>9)&0x3) {
  175. default: return UNSUPPORTED;
  176. case 0x2: return MBlaze::BSLL;
  177. case 0x1: return MBlaze::BSRA;
  178. case 0x0: return MBlaze::BSRL;
  179. }
  180. }
  181. static unsigned decodeBSRLI(uint32_t insn) {
  182. switch ((insn>>9)&0x3) {
  183. default: return UNSUPPORTED;
  184. case 0x2: return MBlaze::BSLLI;
  185. case 0x1: return MBlaze::BSRAI;
  186. case 0x0: return MBlaze::BSRLI;
  187. }
  188. }
  189. static unsigned decodeRSUBK(uint32_t insn) {
  190. switch (getFLAGS(insn)) {
  191. default: return UNSUPPORTED;
  192. case 0x0: return MBlaze::RSUBK;
  193. case 0x1: return MBlaze::CMP;
  194. case 0x3: return MBlaze::CMPU;
  195. }
  196. }
  197. static unsigned decodeFADD(uint32_t insn) {
  198. switch (getFLAGS(insn)) {
  199. default: return UNSUPPORTED;
  200. case 0x000: return MBlaze::FADD;
  201. case 0x080: return MBlaze::FRSUB;
  202. case 0x100: return MBlaze::FMUL;
  203. case 0x180: return MBlaze::FDIV;
  204. case 0x200: return MBlaze::FCMP_UN;
  205. case 0x210: return MBlaze::FCMP_LT;
  206. case 0x220: return MBlaze::FCMP_EQ;
  207. case 0x230: return MBlaze::FCMP_LE;
  208. case 0x240: return MBlaze::FCMP_GT;
  209. case 0x250: return MBlaze::FCMP_NE;
  210. case 0x260: return MBlaze::FCMP_GE;
  211. case 0x280: return MBlaze::FLT;
  212. case 0x300: return MBlaze::FINT;
  213. case 0x380: return MBlaze::FSQRT;
  214. }
  215. }
  216. static unsigned decodeGET(uint32_t insn) {
  217. switch ((insn>>10)&0x3F) {
  218. default: return UNSUPPORTED;
  219. case 0x00: return MBlaze::GET;
  220. case 0x01: return MBlaze::EGET;
  221. case 0x02: return MBlaze::AGET;
  222. case 0x03: return MBlaze::EAGET;
  223. case 0x04: return MBlaze::TGET;
  224. case 0x05: return MBlaze::TEGET;
  225. case 0x06: return MBlaze::TAGET;
  226. case 0x07: return MBlaze::TEAGET;
  227. case 0x08: return MBlaze::CGET;
  228. case 0x09: return MBlaze::ECGET;
  229. case 0x0A: return MBlaze::CAGET;
  230. case 0x0B: return MBlaze::ECAGET;
  231. case 0x0C: return MBlaze::TCGET;
  232. case 0x0D: return MBlaze::TECGET;
  233. case 0x0E: return MBlaze::TCAGET;
  234. case 0x0F: return MBlaze::TECAGET;
  235. case 0x10: return MBlaze::NGET;
  236. case 0x11: return MBlaze::NEGET;
  237. case 0x12: return MBlaze::NAGET;
  238. case 0x13: return MBlaze::NEAGET;
  239. case 0x14: return MBlaze::TNGET;
  240. case 0x15: return MBlaze::TNEGET;
  241. case 0x16: return MBlaze::TNAGET;
  242. case 0x17: return MBlaze::TNEAGET;
  243. case 0x18: return MBlaze::NCGET;
  244. case 0x19: return MBlaze::NECGET;
  245. case 0x1A: return MBlaze::NCAGET;
  246. case 0x1B: return MBlaze::NECAGET;
  247. case 0x1C: return MBlaze::TNCGET;
  248. case 0x1D: return MBlaze::TNECGET;
  249. case 0x1E: return MBlaze::TNCAGET;
  250. case 0x1F: return MBlaze::TNECAGET;
  251. case 0x20: return MBlaze::PUT;
  252. case 0x22: return MBlaze::APUT;
  253. case 0x24: return MBlaze::TPUT;
  254. case 0x26: return MBlaze::TAPUT;
  255. case 0x28: return MBlaze::CPUT;
  256. case 0x2A: return MBlaze::CAPUT;
  257. case 0x2C: return MBlaze::TCPUT;
  258. case 0x2E: return MBlaze::TCAPUT;
  259. case 0x30: return MBlaze::NPUT;
  260. case 0x32: return MBlaze::NAPUT;
  261. case 0x34: return MBlaze::TNPUT;
  262. case 0x36: return MBlaze::TNAPUT;
  263. case 0x38: return MBlaze::NCPUT;
  264. case 0x3A: return MBlaze::NCAPUT;
  265. case 0x3C: return MBlaze::TNCPUT;
  266. case 0x3E: return MBlaze::TNCAPUT;
  267. }
  268. }
  269. static unsigned decodeGETD(uint32_t insn) {
  270. switch ((insn>>5)&0x3F) {
  271. default: return UNSUPPORTED;
  272. case 0x00: return MBlaze::GETD;
  273. case 0x01: return MBlaze::EGETD;
  274. case 0x02: return MBlaze::AGETD;
  275. case 0x03: return MBlaze::EAGETD;
  276. case 0x04: return MBlaze::TGETD;
  277. case 0x05: return MBlaze::TEGETD;
  278. case 0x06: return MBlaze::TAGETD;
  279. case 0x07: return MBlaze::TEAGETD;
  280. case 0x08: return MBlaze::CGETD;
  281. case 0x09: return MBlaze::ECGETD;
  282. case 0x0A: return MBlaze::CAGETD;
  283. case 0x0B: return MBlaze::ECAGETD;
  284. case 0x0C: return MBlaze::TCGETD;
  285. case 0x0D: return MBlaze::TECGETD;
  286. case 0x0E: return MBlaze::TCAGETD;
  287. case 0x0F: return MBlaze::TECAGETD;
  288. case 0x10: return MBlaze::NGETD;
  289. case 0x11: return MBlaze::NEGETD;
  290. case 0x12: return MBlaze::NAGETD;
  291. case 0x13: return MBlaze::NEAGETD;
  292. case 0x14: return MBlaze::TNGETD;
  293. case 0x15: return MBlaze::TNEGETD;
  294. case 0x16: return MBlaze::TNAGETD;
  295. case 0x17: return MBlaze::TNEAGETD;
  296. case 0x18: return MBlaze::NCGETD;
  297. case 0x19: return MBlaze::NECGETD;
  298. case 0x1A: return MBlaze::NCAGETD;
  299. case 0x1B: return MBlaze::NECAGETD;
  300. case 0x1C: return MBlaze::TNCGETD;
  301. case 0x1D: return MBlaze::TNECGETD;
  302. case 0x1E: return MBlaze::TNCAGETD;
  303. case 0x1F: return MBlaze::TNECAGETD;
  304. case 0x20: return MBlaze::PUTD;
  305. case 0x22: return MBlaze::APUTD;
  306. case 0x24: return MBlaze::TPUTD;
  307. case 0x26: return MBlaze::TAPUTD;
  308. case 0x28: return MBlaze::CPUTD;
  309. case 0x2A: return MBlaze::CAPUTD;
  310. case 0x2C: return MBlaze::TCPUTD;
  311. case 0x2E: return MBlaze::TCAPUTD;
  312. case 0x30: return MBlaze::NPUTD;
  313. case 0x32: return MBlaze::NAPUTD;
  314. case 0x34: return MBlaze::TNPUTD;
  315. case 0x36: return MBlaze::TNAPUTD;
  316. case 0x38: return MBlaze::NCPUTD;
  317. case 0x3A: return MBlaze::NCAPUTD;
  318. case 0x3C: return MBlaze::TNCPUTD;
  319. case 0x3E: return MBlaze::TNCAPUTD;
  320. }
  321. }
  322. static unsigned decodeIDIV(uint32_t insn) {
  323. switch (insn&0x3) {
  324. default: return UNSUPPORTED;
  325. case 0x0: return MBlaze::IDIV;
  326. case 0x2: return MBlaze::IDIVU;
  327. }
  328. }
  329. static unsigned decodeLBU(uint32_t insn) {
  330. switch ((insn>>9)&0x1) {
  331. default: return UNSUPPORTED;
  332. case 0x0: return MBlaze::LBU;
  333. case 0x1: return MBlaze::LBUR;
  334. }
  335. }
  336. static unsigned decodeLHU(uint32_t insn) {
  337. switch ((insn>>9)&0x1) {
  338. default: return UNSUPPORTED;
  339. case 0x0: return MBlaze::LHU;
  340. case 0x1: return MBlaze::LHUR;
  341. }
  342. }
  343. static unsigned decodeLW(uint32_t insn) {
  344. switch ((insn>>9)&0x3) {
  345. default: return UNSUPPORTED;
  346. case 0x0: return MBlaze::LW;
  347. case 0x1: return MBlaze::LWR;
  348. case 0x2: return MBlaze::LWX;
  349. }
  350. }
  351. static unsigned decodeSB(uint32_t insn) {
  352. switch ((insn>>9)&0x1) {
  353. default: return UNSUPPORTED;
  354. case 0x0: return MBlaze::SB;
  355. case 0x1: return MBlaze::SBR;
  356. }
  357. }
  358. static unsigned decodeSH(uint32_t insn) {
  359. switch ((insn>>9)&0x1) {
  360. default: return UNSUPPORTED;
  361. case 0x0: return MBlaze::SH;
  362. case 0x1: return MBlaze::SHR;
  363. }
  364. }
  365. static unsigned decodeSW(uint32_t insn) {
  366. switch ((insn>>9)&0x3) {
  367. default: return UNSUPPORTED;
  368. case 0x0: return MBlaze::SW;
  369. case 0x1: return MBlaze::SWR;
  370. case 0x2: return MBlaze::SWX;
  371. }
  372. }
  373. static unsigned decodeMFS(uint32_t insn) {
  374. switch ((insn>>15)&0x1) {
  375. default: return UNSUPPORTED;
  376. case 0x0:
  377. switch ((insn>>16)&0x1) {
  378. default: return UNSUPPORTED;
  379. case 0x0: return MBlaze::MSRSET;
  380. case 0x1: return MBlaze::MSRCLR;
  381. }
  382. case 0x1:
  383. switch ((insn>>14)&0x1) {
  384. default: return UNSUPPORTED;
  385. case 0x0: return MBlaze::MFS;
  386. case 0x1: return MBlaze::MTS;
  387. }
  388. }
  389. }
  390. static unsigned decodeOR(uint32_t insn) {
  391. switch (getFLAGS(insn)) {
  392. default: return UNSUPPORTED;
  393. case 0x000: return MBlaze::OR;
  394. case 0x400: return MBlaze::PCMPBF;
  395. }
  396. }
  397. static unsigned decodeXOR(uint32_t insn) {
  398. switch (getFLAGS(insn)) {
  399. default: return UNSUPPORTED;
  400. case 0x000: return MBlaze::XOR;
  401. case 0x400: return MBlaze::PCMPEQ;
  402. }
  403. }
  404. static unsigned decodeANDN(uint32_t insn) {
  405. switch (getFLAGS(insn)) {
  406. default: return UNSUPPORTED;
  407. case 0x000: return MBlaze::ANDN;
  408. case 0x400: return MBlaze::PCMPNE;
  409. }
  410. }
  411. static unsigned decodeRTSD(uint32_t insn) {
  412. switch ((insn>>21)&0x1F) {
  413. default: return UNSUPPORTED;
  414. case 0x10: return MBlaze::RTSD;
  415. case 0x11: return MBlaze::RTID;
  416. case 0x12: return MBlaze::RTBD;
  417. case 0x14: return MBlaze::RTED;
  418. }
  419. }
  420. static unsigned getOPCODE(uint32_t insn) {
  421. unsigned opcode = mblazeBinary2Opcode[ (insn>>26)&0x3F ];
  422. switch (opcode) {
  423. case MBlaze::MUL: return decodeMUL(insn);
  424. case MBlaze::SEXT8: return decodeSEXT(insn);
  425. case MBlaze::BEQ: return decodeBEQ(insn);
  426. case MBlaze::BEQI: return decodeBEQI(insn);
  427. case MBlaze::BR: return decodeBR(insn);
  428. case MBlaze::BRI: return decodeBRI(insn);
  429. case MBlaze::BSRL: return decodeBSRL(insn);
  430. case MBlaze::BSRLI: return decodeBSRLI(insn);
  431. case MBlaze::RSUBK: return decodeRSUBK(insn);
  432. case MBlaze::FADD: return decodeFADD(insn);
  433. case MBlaze::GET: return decodeGET(insn);
  434. case MBlaze::GETD: return decodeGETD(insn);
  435. case MBlaze::IDIV: return decodeIDIV(insn);
  436. case MBlaze::LBU: return decodeLBU(insn);
  437. case MBlaze::LHU: return decodeLHU(insn);
  438. case MBlaze::LW: return decodeLW(insn);
  439. case MBlaze::SB: return decodeSB(insn);
  440. case MBlaze::SH: return decodeSH(insn);
  441. case MBlaze::SW: return decodeSW(insn);
  442. case MBlaze::MFS: return decodeMFS(insn);
  443. case MBlaze::OR: return decodeOR(insn);
  444. case MBlaze::XOR: return decodeXOR(insn);
  445. case MBlaze::ANDN: return decodeANDN(insn);
  446. case MBlaze::RTSD: return decodeRTSD(insn);
  447. default: return opcode;
  448. }
  449. }
  450. const EDInstInfo *MBlazeDisassembler::getEDInfo() const {
  451. return instInfoMBlaze;
  452. }
  453. //
  454. // Public interface for the disassembler
  455. //
  456. MCDisassembler::DecodeStatus MBlazeDisassembler::getInstruction(MCInst &instr,
  457. uint64_t &size,
  458. const MemoryObject &region,
  459. uint64_t address,
  460. raw_ostream &vStream,
  461. raw_ostream &cStream) const {
  462. // The machine instruction.
  463. uint32_t insn;
  464. uint64_t read;
  465. uint8_t bytes[4];
  466. // By default we consume 1 byte on failure
  467. size = 1;
  468. // We want to read exactly 4 bytes of data.
  469. if (region.readBytes(address, 4, (uint8_t*)bytes, &read) == -1 || read < 4)
  470. return Fail;
  471. // Encoded as a big-endian 32-bit word in the stream.
  472. insn = (bytes[0]<<24) | (bytes[1]<<16) | (bytes[2]<< 8) | (bytes[3]<<0);
  473. // Get the MCInst opcode from the binary instruction and make sure
  474. // that it is a valid instruction.
  475. unsigned opcode = getOPCODE(insn);
  476. if (opcode == UNSUPPORTED)
  477. return Fail;
  478. instr.setOpcode(opcode);
  479. unsigned RD = getRD(insn);
  480. unsigned RA = getRA(insn);
  481. unsigned RB = getRB(insn);
  482. unsigned RS = getRS(insn);
  483. uint64_t tsFlags = MBlazeInsts[opcode].TSFlags;
  484. switch ((tsFlags & MBlazeII::FormMask)) {
  485. default:
  486. return Fail;
  487. case MBlazeII::FC:
  488. break;
  489. case MBlazeII::FRRRR:
  490. if (RD == UNSUPPORTED || RA == UNSUPPORTED || RB == UNSUPPORTED)
  491. return Fail;
  492. instr.addOperand(MCOperand::CreateReg(RD));
  493. instr.addOperand(MCOperand::CreateReg(RB));
  494. instr.addOperand(MCOperand::CreateReg(RA));
  495. break;
  496. case MBlazeII::FRRR:
  497. if (RD == UNSUPPORTED || RA == UNSUPPORTED || RB == UNSUPPORTED)
  498. return Fail;
  499. instr.addOperand(MCOperand::CreateReg(RD));
  500. instr.addOperand(MCOperand::CreateReg(RA));
  501. instr.addOperand(MCOperand::CreateReg(RB));
  502. break;
  503. case MBlazeII::FRR:
  504. if (RD == UNSUPPORTED || RA == UNSUPPORTED)
  505. return Fail;
  506. instr.addOperand(MCOperand::CreateReg(RD));
  507. instr.addOperand(MCOperand::CreateReg(RA));
  508. break;
  509. case MBlazeII::FRI:
  510. switch (opcode) {
  511. default:
  512. return Fail;
  513. case MBlaze::MFS:
  514. if (RD == UNSUPPORTED)
  515. return Fail;
  516. instr.addOperand(MCOperand::CreateReg(RD));
  517. instr.addOperand(MCOperand::CreateImm(insn&0x3FFF));
  518. break;
  519. case MBlaze::MTS:
  520. if (RA == UNSUPPORTED)
  521. return Fail;
  522. instr.addOperand(MCOperand::CreateImm(insn&0x3FFF));
  523. instr.addOperand(MCOperand::CreateReg(RA));
  524. break;
  525. case MBlaze::MSRSET:
  526. case MBlaze::MSRCLR:
  527. if (RD == UNSUPPORTED)
  528. return Fail;
  529. instr.addOperand(MCOperand::CreateReg(RD));
  530. instr.addOperand(MCOperand::CreateImm(insn&0x7FFF));
  531. break;
  532. }
  533. break;
  534. case MBlazeII::FRRI:
  535. if (RD == UNSUPPORTED || RA == UNSUPPORTED)
  536. return Fail;
  537. instr.addOperand(MCOperand::CreateReg(RD));
  538. instr.addOperand(MCOperand::CreateReg(RA));
  539. switch (opcode) {
  540. default:
  541. instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
  542. break;
  543. case MBlaze::BSRLI:
  544. case MBlaze::BSRAI:
  545. case MBlaze::BSLLI:
  546. instr.addOperand(MCOperand::CreateImm(insn&0x1F));
  547. break;
  548. }
  549. break;
  550. case MBlazeII::FCRR:
  551. if (RA == UNSUPPORTED || RB == UNSUPPORTED)
  552. return Fail;
  553. instr.addOperand(MCOperand::CreateReg(RA));
  554. instr.addOperand(MCOperand::CreateReg(RB));
  555. break;
  556. case MBlazeII::FCRI:
  557. if (RA == UNSUPPORTED)
  558. return Fail;
  559. instr.addOperand(MCOperand::CreateReg(RA));
  560. instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
  561. break;
  562. case MBlazeII::FRCR:
  563. if (RD == UNSUPPORTED || RB == UNSUPPORTED)
  564. return Fail;
  565. instr.addOperand(MCOperand::CreateReg(RD));
  566. instr.addOperand(MCOperand::CreateReg(RB));
  567. break;
  568. case MBlazeII::FRCI:
  569. if (RD == UNSUPPORTED)
  570. return Fail;
  571. instr.addOperand(MCOperand::CreateReg(RD));
  572. instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
  573. break;
  574. case MBlazeII::FCCR:
  575. if (RB == UNSUPPORTED)
  576. return Fail;
  577. instr.addOperand(MCOperand::CreateReg(RB));
  578. break;
  579. case MBlazeII::FCCI:
  580. instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
  581. break;
  582. case MBlazeII::FRRCI:
  583. if (RD == UNSUPPORTED || RA == UNSUPPORTED)
  584. return Fail;
  585. instr.addOperand(MCOperand::CreateReg(RD));
  586. instr.addOperand(MCOperand::CreateReg(RA));
  587. instr.addOperand(MCOperand::CreateImm(getSHT(insn)));
  588. break;
  589. case MBlazeII::FRRC:
  590. if (RD == UNSUPPORTED || RA == UNSUPPORTED)
  591. return Fail;
  592. instr.addOperand(MCOperand::CreateReg(RD));
  593. instr.addOperand(MCOperand::CreateReg(RA));
  594. break;
  595. case MBlazeII::FRCX:
  596. if (RD == UNSUPPORTED)
  597. return Fail;
  598. instr.addOperand(MCOperand::CreateReg(RD));
  599. instr.addOperand(MCOperand::CreateImm(getFSL(insn)));
  600. break;
  601. case MBlazeII::FRCS:
  602. if (RD == UNSUPPORTED || RS == UNSUPPORTED)
  603. return Fail;
  604. instr.addOperand(MCOperand::CreateReg(RD));
  605. instr.addOperand(MCOperand::CreateReg(RS));
  606. break;
  607. case MBlazeII::FCRCS:
  608. if (RS == UNSUPPORTED || RA == UNSUPPORTED)
  609. return Fail;
  610. instr.addOperand(MCOperand::CreateReg(RS));
  611. instr.addOperand(MCOperand::CreateReg(RA));
  612. break;
  613. case MBlazeII::FCRCX:
  614. if (RA == UNSUPPORTED)
  615. return Fail;
  616. instr.addOperand(MCOperand::CreateReg(RA));
  617. instr.addOperand(MCOperand::CreateImm(getFSL(insn)));
  618. break;
  619. case MBlazeII::FCX:
  620. instr.addOperand(MCOperand::CreateImm(getFSL(insn)));
  621. break;
  622. case MBlazeII::FCR:
  623. if (RB == UNSUPPORTED)
  624. return Fail;
  625. instr.addOperand(MCOperand::CreateReg(RB));
  626. break;
  627. case MBlazeII::FRIR:
  628. if (RD == UNSUPPORTED || RA == UNSUPPORTED)
  629. return Fail;
  630. instr.addOperand(MCOperand::CreateReg(RD));
  631. instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
  632. instr.addOperand(MCOperand::CreateReg(RA));
  633. break;
  634. }
  635. // We always consume 4 bytes of data on success
  636. size = 4;
  637. return Success;
  638. }
  639. static MCDisassembler *createMBlazeDisassembler(const Target &T,
  640. const MCSubtargetInfo &STI) {
  641. return new MBlazeDisassembler(STI);
  642. }
  643. extern "C" void LLVMInitializeMBlazeDisassembler() {
  644. // Register the disassembler.
  645. TargetRegistry::RegisterMCDisassembler(TheMBlazeTarget,
  646. createMBlazeDisassembler);
  647. }