icode.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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::MCInst
  154. {
  155. llIcode opcode; /* llIcode instruction */
  156. uint8_t numBytes; /* Number of bytes this instr */
  157. uint32_t flg; /* icode flags */
  158. uint32_t label; /* offset in image (20-bit adr) */
  159. LLOperand dst; /* destination operand */
  160. LLOperand src; /* source operand */
  161. DU flagDU; /* def/use of flags */
  162. struct { /* Case table if op==JMP && !I */
  163. int numEntries; /* # entries in case table */
  164. uint32_t *entries; /* array of offsets */
  165. } caseTbl;
  166. int hllLabNum; /* label # for hll codegen */
  167. bool conditionalJump()
  168. {
  169. return (opcode >= iJB) && (opcode < iJCXZ);
  170. }
  171. bool anyFlagSet(uint32_t x) const { return (flg & x)!=0;}
  172. bool match(llIcode op)
  173. {
  174. return (opcode==op);
  175. }
  176. bool match(llIcode op,eReg dest)
  177. {
  178. return (opcode==op)&&dst.regi==dest;
  179. }
  180. bool match(llIcode op,eReg dest,eReg src_reg)
  181. {
  182. return (opcode==op)&&(dst.regi==dest)&&(src.regi==src_reg);
  183. }
  184. bool match(eReg dest,eReg src_reg)
  185. {
  186. return (dst.regi==dest)&&(src.regi==src_reg);
  187. }
  188. bool match(eReg dest)
  189. {
  190. return (dst.regi==dest);
  191. }
  192. void set(llIcode op,uint32_t flags)
  193. {
  194. opcode = op;
  195. flg =flags;
  196. }
  197. };
  198. /* Icode definition: LOW_LEVEL and HIGH_LEVEL */
  199. struct ICODE
  200. {
  201. /* Def/Use of registers and stack variables */
  202. struct DU_ICODE
  203. {
  204. DU_ICODE()
  205. {
  206. def.reset();
  207. use.reset();
  208. lastDefRegi.reset();
  209. }
  210. std::bitset<32> def; // For Registers: position in bitset is reg index
  211. std::bitset<32> use; // For Registers: position in uint32_t is reg index
  212. std::bitset<32> lastDefRegi;// Bit set if last def of this register in BB
  213. };
  214. struct DU1
  215. {
  216. struct Use
  217. {
  218. int Reg; // used register
  219. std::vector<std::list<ICODE>::iterator> uses; // use locations [MAX_USES]
  220. void removeUser(std::list<ICODE>::iterator us)
  221. {
  222. // ic is no no longer an user
  223. auto iter=std::find(uses.begin(),uses.end(),us);
  224. if(iter==uses.end())
  225. return;
  226. uses.erase(iter);
  227. assert("Same user more then once!" && uses.end()==std::find(uses.begin(),uses.end(),us));
  228. }
  229. };
  230. int numRegsDef; /* # registers defined by this inst */
  231. uint8_t regi[3]; /* registers defined by this inst */
  232. Use idx[3];
  233. //int idx[MAX_REGS_DEF][MAX_USES]; /* inst that uses this def */
  234. bool used(int regIdx)
  235. {
  236. return not idx[regIdx].uses.empty();
  237. }
  238. int numUses(int regIdx)
  239. {
  240. return idx[regIdx].uses.size();
  241. }
  242. void recordUse(int regIdx,std::list<ICODE>::iterator location)
  243. {
  244. idx[regIdx].uses.push_back(location);
  245. }
  246. void remove(int regIdx,int use_idx)
  247. {
  248. idx[regIdx].uses.erase(idx[regIdx].uses.begin()+use_idx);
  249. }
  250. void remove(int regIdx,std::list<ICODE>::iterator ic)
  251. {
  252. Use &u(idx[regIdx]);
  253. u.removeUser(ic);
  254. }
  255. };
  256. icodeType type; /* Icode type */
  257. bool invalid; /* Has no HIGH_LEVEL equivalent */
  258. BB *inBB; /* BB to which this icode belongs */
  259. DU_ICODE du; /* Def/use regs/vars */
  260. DU1 du1; /* du chain 1 */
  261. int codeIdx; /* Index into cCode.code */
  262. struct IC { /* Different types of icodes */
  263. LLInst ll;
  264. HLTYPE hl; /* For HIGH_LEVEL icodes */
  265. };
  266. IC ic;/* intermediate code */
  267. int loc_ip; // used by CICodeRec to number ICODEs
  268. void ClrLlFlag(uint32_t flag) {ic.ll.flg &= ~flag;}
  269. void SetLlFlag(uint32_t flag) {ic.ll.flg |= flag;}
  270. uint32_t GetLlFlag() {return ic.ll.flg;}
  271. bool isLlFlag(uint32_t flg) {return (ic.ll.flg&flg)!=0;}
  272. llIcode GetLlOpcode() const { return ic.ll.opcode; }
  273. uint32_t GetLlLabel() const { return ic.ll.label;}
  274. void SetImmediateOp(uint32_t dw) {ic.ll.src.SetImmediateOp(dw);}
  275. void writeIntComment(std::ostringstream &s);
  276. void setRegDU(uint8_t regi, operDu du_in);
  277. void invalidate();
  278. void newCallHl();
  279. void writeDU(int idx);
  280. condId idType(opLoc sd);
  281. // HLL setting functions
  282. void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs); // set this icode to be an assign
  283. void setUnary(hlIcode op, COND_EXPR *exp);
  284. void setJCond(COND_EXPR *cexp);
  285. void emitGotoLabel(int indLevel);
  286. void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
  287. bool valid() {return not invalid;}
  288. public:
  289. bool removeDefRegi(uint8_t regi, int thisDefIdx, LOCAL_ID *locId);
  290. void checkHlCall();
  291. bool newStkArg(COND_EXPR *exp, llIcode opcode, Function *pproc)
  292. {
  293. return ic.hl.call.newStkArg(exp,opcode,pproc);
  294. }
  295. };
  296. // This is the icode array object.
  297. class CIcodeRec : public std::list<ICODE>
  298. {
  299. public:
  300. CIcodeRec(); // Constructor
  301. ICODE * addIcode(ICODE *pIcode);
  302. void SetInBB(int start, int end, BB* pnewBB);
  303. bool labelSrch(uint32_t target, uint32_t &pIndex);
  304. iterator labelSrch(uint32_t target);
  305. ICODE * GetIcode(int ip);
  306. };
  307. typedef CIcodeRec::iterator iICODE;
  308. typedef CIcodeRec::reverse_iterator riICODE;