icode.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*****************************************************************************
  2. * I-code related definitions
  3. * (C) Cristina Cifuentes
  4. ****************************************************************************/
  5. #pragma once
  6. #include <vector>
  7. #include <list>
  8. #include <bitset>
  9. #include <llvm/ADT/ilist.h>
  10. #include <llvm/ADT/ilist_node.h>
  11. #include <llvm/MC/MCInst.h>
  12. #include <llvm/MC/MCAsmInfo.h>
  13. #include "Enums.h"
  14. //enum condId;
  15. struct LOCAL_ID;
  16. /* LOW_LEVEL icode, DU flag bits */
  17. enum eDuFlags
  18. {
  19. Cf=1,
  20. Sf=2,
  21. Zf=4,
  22. Df=8
  23. };
  24. /* uint8_t and uint16_t registers */
  25. static const char *const byteReg[9] = {"al", "cl", "dl", "bl",
  26. "ah", "ch", "dh", "bh", "tmp" };
  27. static const char *const wordReg[21] = {"ax", "cx", "dx", "bx", "sp", "bp",
  28. "si", "di", "es", "cs", "ss", "ds",
  29. "", "", "", "", "", "", "", "", "tmp"};
  30. #include "state.h" // State depends on INDEXBASE, but later need STATE
  31. struct BB;
  32. struct Function;
  33. struct STKFRAME;
  34. /* Def/use of flags - low 4 bits represent flags */
  35. struct DU
  36. {
  37. uint8_t d;
  38. uint8_t u;
  39. };
  40. /* Definition-use chain for level 1 (within a basic block) */
  41. #define MAX_REGS_DEF 2 /* 2 regs def'd for long-reg vars */
  42. //#define MAX_USES 5
  43. struct COND_EXPR;
  44. struct HlTypeSupport
  45. {
  46. //hlIcode opcode; /* hlIcode opcode */
  47. virtual bool removeRegFromLong(uint8_t regi, LOCAL_ID *locId)=0;
  48. virtual std::string writeOut(Function *pProc, int *numLoc)=0;
  49. protected:
  50. void performLongRemoval (uint8_t regi, LOCAL_ID *locId, COND_EXPR *tree);
  51. };
  52. struct CallType : public HlTypeSupport
  53. {
  54. //for HLI_CALL
  55. Function * proc;
  56. STKFRAME * args; // actual arguments
  57. void allocStkArgs (int num);
  58. bool newStkArg(COND_EXPR *exp, llIcode opcode, Function *pproc);
  59. void placeStkArg(COND_EXPR *exp, int pos);
  60. public:
  61. bool removeRegFromLong(uint8_t regi, LOCAL_ID *locId)
  62. {
  63. printf("CallType : removeRegFromLong not supproted");
  64. return false;
  65. }
  66. std::string writeOut(Function *pProc, int *numLoc);
  67. };
  68. struct AssignType : public HlTypeSupport
  69. {
  70. /* for HLI_ASSIGN */
  71. COND_EXPR *lhs;
  72. COND_EXPR *rhs;
  73. bool removeRegFromLong(uint8_t regi, LOCAL_ID *locId)
  74. {
  75. performLongRemoval(regi,locId,lhs);
  76. return true;
  77. }
  78. std::string writeOut(Function *pProc, int *numLoc);
  79. };
  80. struct ExpType : public HlTypeSupport
  81. {
  82. /* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
  83. COND_EXPR *v;
  84. bool removeRegFromLong(uint8_t regi, LOCAL_ID *locId)
  85. {
  86. performLongRemoval(regi,locId,v);
  87. return true;
  88. }
  89. std::string writeOut(Function *pProc, int *numLoc);
  90. };
  91. struct HLTYPE
  92. {
  93. hlIcode opcode; /* hlIcode opcode */
  94. ExpType exp; /* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
  95. AssignType asgn;
  96. CallType call;
  97. HlTypeSupport *get()
  98. {
  99. switch(opcode)
  100. {
  101. case HLI_ASSIGN: return &asgn;
  102. case HLI_RET:
  103. case HLI_POP:
  104. case HLI_PUSH: return &exp;
  105. case HLI_CALL: return &call;
  106. default:
  107. return 0;
  108. }
  109. }
  110. void expr(COND_EXPR *e)
  111. {
  112. assert(e);
  113. exp.v=e;
  114. }
  115. COND_EXPR * expr() { return exp.v;}
  116. void set(hlIcode i,COND_EXPR *e)
  117. {
  118. if(i!=HLI_RET)
  119. assert(e);
  120. assert(exp.v==0);
  121. opcode=i;
  122. exp.v=e;
  123. }
  124. void set(COND_EXPR *l,COND_EXPR *r)
  125. {
  126. assert(l);
  127. assert(r);
  128. opcode = HLI_ASSIGN;
  129. assert((asgn.lhs==0) and (asgn.rhs==0)); //prevent memory leaks
  130. asgn.lhs=l;
  131. asgn.rhs=r;
  132. }
  133. public:
  134. std::string write1HlIcode(Function *pProc, int *numLoc);
  135. } ;
  136. /* LOW_LEVEL icode operand record */
  137. struct LLOperand //: public llvm::MCOperand
  138. {
  139. uint8_t seg; /* CS, DS, ES, SS */
  140. int16_t segValue; /* Value of segment seg during analysis */
  141. uint8_t segOver; /* CS, DS, ES, SS if segment override */
  142. uint8_t regi; /* 0 < regs < INDEXBASE <= index modes */
  143. int16_t off; /* memory address offset */
  144. uint32_t opz; /* idx of immed src op */
  145. //union {/* Source operand if (flg & I) */
  146. struct { /* Call & # actual arg bytes */
  147. Function *proc; /* pointer to target proc (for CALL(F))*/
  148. int cb; /* # actual arg bytes */
  149. } proc;
  150. uint32_t op() const {return opz;}
  151. void SetImmediateOp(uint32_t dw) {opz=dw;}
  152. };
  153. struct LLInst : public llvm::ilist_node<LLInst>
  154. {
  155. protected:
  156. uint32_t flg; /* icode flags */
  157. public:
  158. int codeIdx; /* Index into cCode.code */
  159. llIcode opcode; /* llIcode instruction */
  160. uint8_t numBytes; /* Number of bytes this instr */
  161. uint32_t label; /* offset in image (20-bit adr) */
  162. LLOperand dst; /* destination operand */
  163. LLOperand src; /* source operand */
  164. DU flagDU; /* def/use of flags */
  165. struct { /* Case table if op==JMP && !I */
  166. int numEntries; /* # entries in case table */
  167. uint32_t *entries; /* array of offsets */
  168. } caseTbl;
  169. int hllLabNum; /* label # for hll codegen */
  170. bool conditionalJump()
  171. {
  172. return (opcode >= iJB) && (opcode < iJCXZ);
  173. }
  174. bool testFlags(uint32_t x) const { return (flg & x)!=0;}
  175. void setFlags(uint32_t flag) {flg |= flag;}
  176. void clrFlags(uint32_t flag) {flg &= ~flag;}
  177. uint32_t getFlag() const {return flg;}
  178. llIcode GetLlOpcode() const { return opcode; }
  179. uint32_t GetLlLabel() const { return label;}
  180. void SetImmediateOp(uint32_t dw) {src.SetImmediateOp(dw);}
  181. bool match(llIcode op)
  182. {
  183. return (opcode==op);
  184. }
  185. bool match(llIcode op,eReg dest)
  186. {
  187. return (opcode==op)&&dst.regi==dest;
  188. }
  189. bool match(llIcode op,eReg dest,uint32_t flgs)
  190. {
  191. return (opcode==op) and (dst.regi==dest) and testFlags(flgs);
  192. }
  193. bool match(llIcode op,eReg dest,eReg src_reg)
  194. {
  195. return (opcode==op)&&(dst.regi==dest)&&(src.regi==src_reg);
  196. }
  197. bool match(eReg dest,eReg src_reg)
  198. {
  199. return (dst.regi==dest)&&(src.regi==src_reg);
  200. }
  201. bool match(eReg dest)
  202. {
  203. return (dst.regi==dest);
  204. }
  205. bool match(llIcode op,uint32_t flgs)
  206. {
  207. return (opcode==op) and testFlags(flgs);
  208. }
  209. void set(llIcode op,uint32_t flags)
  210. {
  211. opcode = op;
  212. flg =flags;
  213. }
  214. void emitGotoLabel(int indLevel);
  215. };
  216. /* Icode definition: LOW_LEVEL and HIGH_LEVEL */
  217. struct ICODE
  218. {
  219. protected:
  220. LLInst m_ll;
  221. HLTYPE m_hl;
  222. public:
  223. /* Def/Use of registers and stack variables */
  224. struct DU_ICODE
  225. {
  226. DU_ICODE()
  227. {
  228. def.reset();
  229. use.reset();
  230. lastDefRegi.reset();
  231. }
  232. std::bitset<32> def; // For Registers: position in bitset is reg index
  233. std::bitset<32> use; // For Registers: position in uint32_t is reg index
  234. std::bitset<32> lastDefRegi;// Bit set if last def of this register in BB
  235. };
  236. struct DU1
  237. {
  238. struct Use
  239. {
  240. int Reg; // used register
  241. std::vector<std::list<ICODE>::iterator> uses; // use locations [MAX_USES]
  242. void removeUser(std::list<ICODE>::iterator us)
  243. {
  244. // ic is no no longer an user
  245. auto iter=std::find(uses.begin(),uses.end(),us);
  246. if(iter==uses.end())
  247. return;
  248. uses.erase(iter);
  249. assert("Same user more then once!" && uses.end()==std::find(uses.begin(),uses.end(),us));
  250. }
  251. };
  252. int numRegsDef; /* # registers defined by this inst */
  253. uint8_t regi[3]; /* registers defined by this inst */
  254. Use idx[3];
  255. //int idx[MAX_REGS_DEF][MAX_USES]; /* inst that uses this def */
  256. bool used(int regIdx)
  257. {
  258. return not idx[regIdx].uses.empty();
  259. }
  260. int numUses(int regIdx)
  261. {
  262. return idx[regIdx].uses.size();
  263. }
  264. void recordUse(int regIdx,std::list<ICODE>::iterator location)
  265. {
  266. idx[regIdx].uses.push_back(location);
  267. }
  268. void remove(int regIdx,int use_idx)
  269. {
  270. idx[regIdx].uses.erase(idx[regIdx].uses.begin()+use_idx);
  271. }
  272. void remove(int regIdx,std::list<ICODE>::iterator ic)
  273. {
  274. Use &u(idx[regIdx]);
  275. u.removeUser(ic);
  276. }
  277. };
  278. icodeType type; /* Icode type */
  279. bool invalid; /* Has no HIGH_LEVEL equivalent */
  280. BB *inBB; /* BB to which this icode belongs */
  281. DU_ICODE du; /* Def/use regs/vars */
  282. DU1 du1; /* du chain 1 */
  283. LLInst * ll() { return &m_ll;}
  284. const LLInst * ll() const { return &m_ll;}
  285. HLTYPE * hl() { return &m_hl;}
  286. const HLTYPE * hl() const { return &m_hl;}
  287. int loc_ip; // used by CICodeRec to number ICODEs
  288. void writeIntComment(std::ostringstream &s);
  289. void setRegDU(uint8_t regi, operDu du_in);
  290. void invalidate();
  291. void newCallHl();
  292. void writeDU(int idx);
  293. condId idType(opLoc sd);
  294. // HLL setting functions
  295. void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs); // set this icode to be an assign
  296. void setUnary(hlIcode op, COND_EXPR *exp);
  297. void setJCond(COND_EXPR *cexp);
  298. void emitGotoLabel(int indLevel);
  299. void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
  300. bool valid() {return not invalid;}
  301. public:
  302. bool removeDefRegi(uint8_t regi, int thisDefIdx, LOCAL_ID *locId);
  303. void checkHlCall();
  304. bool newStkArg(COND_EXPR *exp, llIcode opcode, Function *pproc)
  305. {
  306. return hl()->call.newStkArg(exp,opcode,pproc);
  307. }
  308. };
  309. // This is the icode array object.
  310. class CIcodeRec : public std::list<ICODE>
  311. {
  312. public:
  313. CIcodeRec(); // Constructor
  314. ICODE * addIcode(ICODE *pIcode);
  315. void SetInBB(int start, int end, BB* pnewBB);
  316. bool labelSrch(uint32_t target, uint32_t &pIndex);
  317. iterator labelSrch(uint32_t target);
  318. ICODE * GetIcode(int ip);
  319. };
  320. typedef CIcodeRec::iterator iICODE;
  321. typedef CIcodeRec::reverse_iterator riICODE;