icode.h 12 KB

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