icode.h 11 KB

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