icode.h 11 KB

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