icode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*****************************************************************************
  2. * I-code related definitions
  3. * (C) Cristina Cifuentes
  4. ****************************************************************************/
  5. #pragma once
  6. #include <vector>
  7. #include <llvm/MC/MCInst.h>
  8. #include <llvm/MC/MCAsmInfo.h>
  9. #include "Enums.h"
  10. //enum condId;
  11. struct LOCAL_ID;
  12. /* LOW_LEVEL icode flags */
  13. enum eLLFlags
  14. {
  15. B =0x0000001, /* Byte operands (value implicitly used) */
  16. I =0x0000002, /* Immed. source */
  17. NOT_HLL =0x0000004, /* Not HLL inst. */
  18. FLOAT_OP =0x0000008, /* ESC or WAIT */
  19. SEG_IMMED =0x0000010, /* Number is relocated segment value */
  20. IMPURE =0x0000020, /* Instruction modifies code */
  21. WORD_OFF =0x0000040, /* Inst has word offset ie.could be address */
  22. TERMINATES =0x0000080, /* Instruction terminates program */
  23. CASE =0x0000100, /* Label as case part of switch */
  24. SWITCH =0x0000200, /* Treat indirect JMP as switch stmt */
  25. TARGET =0x0000400, /* Jump target */
  26. SYNTHETIC =0x0000800, /* Synthetic jump instruction */
  27. NO_LABEL =0x0001000, /* Immed. jump cannot be linked to a label */
  28. NO_CODE =0x0002000, /* Hole in Icode array */
  29. SYM_USE =0x0004000, /* Instruction uses a symbol */
  30. SYM_DEF =0x0008000, /* Instruction defines a symbol */
  31. NO_SRC =0x0010000, /* Opcode takes no source */
  32. NO_OPS =0x0020000, /* Opcode takes no operands */
  33. IM_OPS =0x0040000, /* Opcode takes implicit operands */
  34. SRC_B =0x0080000, /* Source operand is byte (dest is word) */
  35. #define NO_SRC_B 0xF7FFFF /* Masks off SRC_B */
  36. HLL_LABEL =0x0100000, /* Icode has a high level language label */
  37. IM_DST =0x0200000, /* Implicit DST for opcode (SIGNEX) */
  38. IM_SRC =0x0400000, /* Implicit SRC for opcode (dx:ax) */
  39. IM_TMP_DST =0x0800000, /* Implicit rTMP DST for opcode (DIV/IDIV) */
  40. JMP_ICODE =0x1000000, /* Jmp dest immed.op converted to icode index */
  41. JX_LOOP =0x2000000, /* Cond jump is part of loop conditional exp */
  42. REST_STK =0x4000000 /* Stack needs to be restored after CALL */
  43. };
  44. /* Parser flags */
  45. #define TO_REG 0x000100 /* rm is source */
  46. #define S_EXT 0x000200 /* sign extend */
  47. #define OP386 0x000400 /* 386 op-code */
  48. #define NSP 0x000800 /* NOT_HLL if SP is src or dst */
  49. #define ICODEMASK 0xFF00FF /* Masks off parser flags */
  50. /* LOW_LEVEL icode, DU flag bits */
  51. enum eDuFlags
  52. {
  53. Cf=1,
  54. Sf=2,
  55. Zf=4,
  56. Df=8
  57. };
  58. /* Machine registers */
  59. #define rAX 1 /* These are numbered relative to real 8086 */
  60. #define rCX 2
  61. #define rDX 3
  62. #define rBX 4
  63. #define rSP 5
  64. #define rBP 6
  65. #define rSI 7
  66. #define rDI 8
  67. #define rES 9
  68. #define rCS 10
  69. #define rSS 11
  70. #define rDS 12
  71. #define rAL 13
  72. #define rCL 14
  73. #define rDL 15
  74. #define rBL 16
  75. #define rAH 17
  76. #define rCH 18
  77. #define rDH 19
  78. #define rBH 20
  79. #define rTMP 21 /* temp register for DIV/IDIV/MOD */
  80. #define INDEXBASE 22 /* Indexed modes go from INDEXBASE to
  81. * INDEXBASE+7 */
  82. /* Byte and Word registers */
  83. static const char *const byteReg[9] = {"al", "cl", "dl", "bl",
  84. "ah", "ch", "dh", "bh", "tmp" };
  85. static const char *const wordReg[21] = {"ax", "cx", "dx", "bx", "sp", "bp",
  86. "si", "di", "es", "cs", "ss", "ds",
  87. "", "", "", "", "", "", "", "", "tmp"};
  88. #include "state.h" // State depends on INDEXBASE, but later need STATE
  89. /* Types of icodes */
  90. enum icodeType
  91. {
  92. NOT_SCANNED = 0, /* not even scanned yet */
  93. LOW_LEVEL, /* low-level icode */
  94. HIGH_LEVEL /* high-level icode */
  95. };
  96. /* LOW_LEVEL icode opcodes */
  97. enum llIcode
  98. {
  99. iCBW, /* 0 */
  100. iAAA,
  101. iAAD,
  102. iAAM,
  103. iAAS,
  104. iADC,
  105. iADD,
  106. iAND,
  107. iBOUND,
  108. iCALL,
  109. iCALLF, /* 10 */
  110. iCLC,
  111. iCLD,
  112. iCLI,
  113. iCMC,
  114. iCMP,
  115. iCMPS,
  116. iREPNE_CMPS,
  117. iREPE_CMPS,
  118. iDAA,
  119. iDAS, /* 20 */
  120. iDEC,
  121. iDIV,
  122. iENTER,
  123. iESC,
  124. iHLT,
  125. iIDIV,
  126. iIMUL,
  127. iIN,
  128. iINC,
  129. iINS, /* 30 */
  130. iREP_INS,
  131. iINT,
  132. iIRET,
  133. iJB,
  134. iJBE,
  135. iJAE,
  136. iJA,
  137. iJE,
  138. iJNE,
  139. iJL, /* 40 */
  140. iJGE,
  141. iJLE,
  142. iJG,
  143. iJS,
  144. iJNS,
  145. iJO,
  146. iJNO,
  147. iJP,
  148. iJNP,
  149. iJCXZ, /* 50 */
  150. iJMP,
  151. iJMPF,
  152. iLAHF,
  153. iLDS,
  154. iLEA,
  155. iLEAVE,
  156. iLES,
  157. iLOCK,
  158. iLODS,
  159. iREP_LODS, /* 60 */
  160. iLOOP,
  161. iLOOPE,
  162. iLOOPNE,
  163. iMOV, /* 64 */
  164. iMOVS,
  165. iREP_MOVS,
  166. iMUL, /* 67 */
  167. iNEG,
  168. iNOT,
  169. iOR, /* 70 */
  170. iOUT,
  171. iOUTS,
  172. iREP_OUTS,
  173. iPOP,
  174. iPOPA,
  175. iPOPF,
  176. iPUSH,
  177. iPUSHA,
  178. iPUSHF,
  179. iRCL, /* 80 */
  180. iRCR,
  181. iROL,
  182. iROR,
  183. iRET, /* 84 */
  184. iRETF,
  185. iSAHF,
  186. iSAR,
  187. iSHL,
  188. iSHR,
  189. iSBB, /* 90 */
  190. iSCAS,
  191. iREPNE_SCAS,
  192. iREPE_SCAS,
  193. iSIGNEX,
  194. iSTC,
  195. iSTD,
  196. iSTI,
  197. iSTOS,
  198. iREP_STOS,
  199. iSUB, /* 100 */
  200. iTEST,
  201. iWAIT,
  202. iXCHG,
  203. iXLAT,
  204. iXOR,
  205. iINTO,
  206. iNOP,
  207. iREPNE,
  208. iREPE,
  209. iMOD /* 110 */
  210. };
  211. struct BB;
  212. struct Function;
  213. struct STKFRAME;
  214. /* HIGH_LEVEL icodes opcodes */
  215. typedef enum {
  216. HLI_ASSIGN, /* := */
  217. HLI_CALL, /* Call procedure */
  218. HLI_JCOND, /* Conditional jump */
  219. HLI_RET, /* Return from procedure */
  220. /* pseudo high-level icodes */
  221. HLI_POP, /* Pop expression */
  222. HLI_PUSH /* Push expression */
  223. } hlIcode;
  224. /* Def/use of flags - low 4 bits represent flags */
  225. struct DU
  226. {
  227. byte d;
  228. byte u;
  229. };
  230. /* Def/Use of registers and stack variables */
  231. struct DU_ICODE
  232. {
  233. dword def; /* For Registers: position in dword is reg index*/
  234. dword lastDefRegi;/* Bit set if last def of this register in BB */
  235. dword use; /* For Registers: position in dword is reg index*/
  236. };
  237. /* Definition-use chain for level 1 (within a basic block) */
  238. #define MAX_REGS_DEF 2 /* 2 regs def'd for long-reg vars */
  239. #define MAX_USES 5
  240. struct COND_EXPR;
  241. struct HLTYPE
  242. {
  243. hlIcode opcode; /* hlIcode opcode */
  244. union { /* different operands */
  245. struct { /* for HLI_ASSIGN */
  246. COND_EXPR *lhs;
  247. COND_EXPR *rhs;
  248. } asgn;
  249. COND_EXPR *exp; /* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
  250. struct { /* for HLI_CALL */
  251. Function *proc;
  252. STKFRAME *args; /* actual arguments */
  253. } call;
  254. } oper; /* operand */
  255. } ;
  256. /* LOW_LEVEL icode operand record */
  257. struct LLOpcode : public llvm::MCOperand
  258. {
  259. byte seg; /* CS, DS, ES, SS */
  260. int16 segValue; /* Value of segment seg during analysis */
  261. byte segOver; /* CS, DS, ES, SS if segment override */
  262. byte regi; /* 0 < regs < INDEXBASE <= index modes */
  263. int16 off; /* memory address offset */
  264. dword opz; /* idx of immed src op */
  265. //union {/* Source operand if (flg & I) */
  266. //dword opz; /* idx of immed src op */
  267. struct { /* Call & # actual arg bytes */
  268. Function *proc; /* pointer to target proc (for CALL(F))*/
  269. int cb; /* # actual arg bytes */
  270. } proc;
  271. //} immed;
  272. dword op() const {return opz;}
  273. void SetImmediateOp(dword dw) {opz=dw;}
  274. };
  275. struct LLInst : public llvm::MCInst
  276. {
  277. llIcode opcode; /* llIcode instruction */
  278. byte numBytes; /* Number of bytes this instr */
  279. flags32 flg; /* icode flags */
  280. dword label; /* offset in image (20-bit adr) */
  281. LLOpcode dst; /* destination operand */
  282. LLOpcode src; /* source operand */
  283. DU flagDU; /* def/use of flags */
  284. struct { /* Case table if op==JMP && !I */
  285. Int numEntries; /* # entries in case table */
  286. dword *entries; /* array of offsets */
  287. } caseTbl;
  288. Int hllLabNum; /* label # for hll codegen */
  289. bool conditionalJump()
  290. {
  291. return (opcode >= iJB) && (opcode < iJCXZ);
  292. }
  293. bool anyFlagSet(uint32_t x) const { return (flg & x)!=0;}
  294. };
  295. /* Icode definition: LOW_LEVEL and HIGH_LEVEL */
  296. struct ICODE
  297. {
  298. struct DU1
  299. {
  300. Int numRegsDef; /* # registers defined by this inst */
  301. byte regi[MAX_REGS_DEF]; /* registers defined by this inst */
  302. Int idx[MAX_REGS_DEF][MAX_USES]; /* inst that uses this def */
  303. };
  304. icodeType type; /* Icode type */
  305. boolT invalid; /* Has no HIGH_LEVEL equivalent */
  306. BB *inBB; /* BB to which this icode belongs */
  307. DU_ICODE du; /* Def/use regs/vars */
  308. DU1 du1; /* du chain 1 */
  309. Int codeIdx; /* Index into cCode.code */
  310. struct IC { /* Different types of icodes */
  311. LLInst ll;
  312. HLTYPE hl; /* For HIGH_LEVEL icodes */
  313. };
  314. IC ic;/* intermediate code */
  315. int loc_ip; // used by CICodeRec to number ICODEs
  316. void ClrLlFlag(dword flag) {ic.ll.flg &= ~flag;}
  317. void SetLlFlag(dword flag) {ic.ll.flg |= flag;}
  318. dword GetLlFlag() {return ic.ll.flg;}
  319. bool isLlFlag(dword flg) {return (ic.ll.flg&flg)!=0;}
  320. llIcode GetLlOpcode() const { return ic.ll.opcode; }
  321. dword GetLlLabel() const { return ic.ll.label;}
  322. void SetImmediateOp(dword dw) {ic.ll.src.SetImmediateOp(dw);}
  323. void writeIntComment(std::ostringstream &s);
  324. void setRegDU(byte regi, operDu du_in);
  325. void invalidate();
  326. void newCallHl();
  327. void writeDU(Int idx);
  328. condId idType(opLoc sd);
  329. // HLL setting functions
  330. void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs); // set this icode to be an assign
  331. void setUnary(hlIcode op, COND_EXPR *exp);
  332. void setJCond(COND_EXPR *cexp);
  333. void emitGotoLabel(Int indLevel);
  334. void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
  335. public:
  336. boolT removeDefRegi(byte regi, Int thisDefIdx, LOCAL_ID *locId);
  337. void checkHlCall();
  338. };
  339. // This is the icode array object.
  340. class CIcodeRec : public std::vector<ICODE>
  341. {
  342. public:
  343. CIcodeRec(); // Constructor
  344. ICODE * addIcode(ICODE *pIcode);
  345. void SetInBB(int start, int end, BB* pnewBB);
  346. bool labelSrch(dword target, dword &pIndex);
  347. ICODE * GetIcode(int ip);
  348. };
  349. typedef CIcodeRec::iterator iICODE;
  350. typedef CIcodeRec::reverse_iterator riICODE;