icode.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 <llvm/Value.h>
  16. #include <llvm/Instruction.h>
  17. #include <boost/range.hpp>
  18. #include "libdis.h"
  19. #include "Enums.h"
  20. #include "state.h" // State depends on INDEXBASE, but later need STATE
  21. //enum condId;
  22. struct LOCAL_ID;
  23. struct BB;
  24. struct Function;
  25. struct STKFRAME;
  26. class CIcodeRec;
  27. struct ICODE;
  28. struct bundle;
  29. typedef std::list<ICODE>::iterator iICODE;
  30. typedef std::list<ICODE>::reverse_iterator riICODE;
  31. typedef boost::iterator_range<iICODE> rCODE;
  32. extern std::bitset<32> duReg[30];
  33. /* uint8_t and uint16_t registers */
  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 4 /* 2 regs def'd for long-reg vars */
  42. struct COND_EXPR;
  43. struct HlTypeSupport
  44. {
  45. //hlIcode opcode; /* hlIcode opcode */
  46. virtual bool removeRegFromLong(eReg regi, LOCAL_ID *locId)=0;
  47. virtual std::string writeOut(Function *pProc, int *numLoc)=0;
  48. protected:
  49. void performLongRemoval (eReg regi, LOCAL_ID *locId, COND_EXPR *tree);
  50. };
  51. struct CallType : public HlTypeSupport
  52. {
  53. //for HLI_CALL
  54. Function * proc;
  55. STKFRAME * args; // actual arguments
  56. void allocStkArgs (int num);
  57. bool newStkArg(COND_EXPR *exp, llIcode opcode, Function *pproc);
  58. void placeStkArg(COND_EXPR *exp, int pos);
  59. virtual COND_EXPR * toId();
  60. public:
  61. bool removeRegFromLong(eReg /*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. AssignType() : lhs(0),rhs(0) {}
  74. bool removeRegFromLong(eReg regi, LOCAL_ID *locId)
  75. {
  76. performLongRemoval(regi,locId,lhs);
  77. return true;
  78. }
  79. std::string writeOut(Function *pProc, int *numLoc);
  80. };
  81. struct ExpType : public HlTypeSupport
  82. {
  83. /* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
  84. COND_EXPR *v;
  85. ExpType() : v(0) {}
  86. bool removeRegFromLong(eReg regi, LOCAL_ID *locId)
  87. {
  88. performLongRemoval(regi,locId,v);
  89. return true;
  90. }
  91. std::string writeOut(Function *pProc, int *numLoc);
  92. };
  93. struct HLTYPE
  94. {
  95. protected:
  96. public:
  97. ExpType exp; /* for HLI_JCOND, HLI_RET, HLI_PUSH, HLI_POP*/
  98. hlIcode opcode; /* hlIcode opcode */
  99. AssignType asgn;
  100. CallType call;
  101. HlTypeSupport *get();
  102. void expr(COND_EXPR *e)
  103. {
  104. assert(e);
  105. exp.v=e;
  106. }
  107. void replaceExpr(COND_EXPR *e);
  108. COND_EXPR * expr() { return exp.v;}
  109. const COND_EXPR * expr() const { return exp.v;}
  110. void set(hlIcode i,COND_EXPR *e)
  111. {
  112. if(i!=HLI_RET)
  113. assert(e);
  114. assert(exp.v==0);
  115. opcode=i;
  116. exp.v=e;
  117. }
  118. void set(COND_EXPR *l,COND_EXPR *r)
  119. {
  120. assert(l);
  121. assert(r);
  122. opcode = HLI_ASSIGN;
  123. assert((asgn.lhs==0) and (asgn.rhs==0)); //prevent memory leaks
  124. asgn.lhs=l;
  125. asgn.rhs=r;
  126. }
  127. HLTYPE(hlIcode op=HLI_INVALID) : opcode(op)
  128. {}
  129. HLTYPE & operator=(const HLTYPE &l)
  130. {
  131. exp=l.exp;
  132. opcode=l.opcode;
  133. asgn=l.asgn;
  134. call=l.call;
  135. return *this;
  136. }
  137. public:
  138. std::string write1HlIcode(Function *pProc, int *numLoc);
  139. void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs);
  140. } ;
  141. /* LOW_LEVEL icode operand record */
  142. struct LLOperand
  143. {
  144. llvm::MCOperand llvm_op;
  145. eReg seg; /* CS, DS, ES, SS */
  146. eReg segOver; /* CS, DS, ES, SS if segment override */
  147. int16_t segValue; /* Value of segment seg during analysis */
  148. eReg regi; /* 0 < regs < INDEXBASE <= index modes */
  149. int16_t off; /* memory address offset */
  150. uint32_t opz; /* idx of immed src op */
  151. //union {/* Source operand if (flg & I) */
  152. struct { /* Call & # actual arg bytes */
  153. Function *proc; /* pointer to target proc (for CALL(F))*/
  154. int cb; /* # actual arg bytes */
  155. } proc;
  156. LLOperand() : seg(rUNDEF),segOver(rUNDEF),segValue(0),regi(rUNDEF),off(0),opz(0)
  157. {
  158. proc.proc=0;
  159. proc.cb=0;
  160. }
  161. int64_t getImm2() const {return opz;}
  162. void SetImmediateOp(uint32_t dw)
  163. {
  164. opz=dw;
  165. }
  166. eReg getReg2() {return regi;}
  167. bool isReg() const;
  168. static LLOperand CreateImm2(int64_t Val)
  169. {
  170. LLOperand Op;
  171. //Op.Kind = kImmediate;
  172. //Op.ImmVal = Val;
  173. Op.opz = Val;
  174. return Op;
  175. }
  176. static LLOperand CreateReg2(unsigned Val)
  177. {
  178. LLOperand Op;
  179. // Op.Kind = kRegister;
  180. // Op.RegVal = Reg;
  181. Op.regi = (eReg)Val;
  182. return Op;
  183. }
  184. void addProcInformation(int param_count,uint32_t call_conv);
  185. };
  186. struct LLInst : public llvm::MCInst //: public llvm::ilist_node<LLInst>
  187. {
  188. protected:
  189. uint32_t flg; /* icode flags */
  190. // LLOperand &get(int idx)
  191. // {
  192. // assert(idx<size());
  193. // return getOperand(idx);
  194. // }
  195. LLOperand m_src; /* source operand */
  196. public:
  197. int codeIdx; /* Index into cCode.code */
  198. uint8_t numBytes; /* Number of bytes this instr */
  199. uint32_t label; /* offset in image (20-bit adr) */
  200. LLOperand dst; /* destination operand */
  201. DU flagDU; /* def/use of flags */
  202. int caseEntry;
  203. std::vector<uint32_t> caseTbl2;
  204. int hllLabNum; /* label # for hll codegen */
  205. bool conditionalJump()
  206. {
  207. return (getOpcode() >= iJB) && (getOpcode() < iJCXZ);
  208. }
  209. bool testFlags(uint32_t x) const { return (flg & x)!=0;}
  210. void setFlags(uint32_t flag) {flg |= flag;}
  211. void clrFlags(uint32_t flag)
  212. {
  213. if(getOpcode()==iMOD)
  214. {
  215. assert(false);
  216. }
  217. flg &= ~flag;
  218. }
  219. uint32_t getFlag() const {return flg;}
  220. //llIcode getOpcode() const { return opcode; }
  221. uint32_t GetLlLabel() const { return label;}
  222. void SetImmediateOp(uint32_t dw) {m_src.SetImmediateOp(dw);}
  223. bool match(llIcode op)
  224. {
  225. return (getOpcode()==op);
  226. }
  227. bool match(llIcode op,eReg dest)
  228. {
  229. return (getOpcode()==op)&&dst.regi==dest;
  230. }
  231. bool match(llIcode op,eReg dest,uint32_t flgs)
  232. {
  233. return (getOpcode()==op) and (dst.regi==dest) and testFlags(flgs);
  234. }
  235. bool match(llIcode op,eReg dest,eReg src_reg)
  236. {
  237. return (getOpcode()==op)&&(dst.regi==dest)&&(m_src.regi==src_reg);
  238. }
  239. bool match(eReg dest,eReg src_reg)
  240. {
  241. return (dst.regi==dest)&&(m_src.regi==src_reg);
  242. }
  243. bool match(eReg dest)
  244. {
  245. return (dst.regi==dest);
  246. }
  247. bool match(llIcode op,uint32_t flgs)
  248. {
  249. return (getOpcode()==op) and testFlags(flgs);
  250. }
  251. void set(llIcode op,uint32_t flags)
  252. {
  253. setOpcode(op);
  254. flg =flags;
  255. }
  256. void emitGotoLabel(int indLevel);
  257. void findJumpTargets(CIcodeRec &_pc);
  258. void writeIntComment(std::ostringstream &s);
  259. void dis1Line(int loc_ip, int pass);
  260. std::ostringstream &strSrc(std::ostringstream &os,bool skip_comma=false);
  261. void flops(std::ostringstream &out);
  262. bool isJmpInst();
  263. HLTYPE toHighLevel(COND_EXPR *lhs, COND_EXPR *rhs, Function *func);
  264. HLTYPE createCall();
  265. LLInst(ICODE *container) : flg(0),codeIdx(0),numBytes(0),m_link(container)
  266. {
  267. setOpcode(0);
  268. }
  269. const LLOperand &src() const {return m_src;}
  270. LLOperand &src() {return m_src;}
  271. void replaceSrc(const LLOperand &with)
  272. {
  273. m_src = with;
  274. }
  275. void replaceSrc(eReg r)
  276. {
  277. m_src = LLOperand::CreateReg2(r);
  278. }
  279. void replaceSrc(int64_t r)
  280. {
  281. m_src = LLOperand::CreateImm2(r);
  282. }
  283. void replaceDst(const LLOperand &with)
  284. {
  285. dst = with;
  286. }
  287. void replaceDst(eReg r)
  288. {
  289. dst = LLOperand::CreateReg2(r);
  290. }
  291. ICODE *m_link;
  292. condId idType(opLoc sd) const;
  293. const LLOperand * get(opLoc sd) const { return (sd == SRC) ? &src() : &dst; }
  294. LLOperand * get(opLoc sd) { return (sd == SRC) ? &src() : &dst; }
  295. };
  296. /* Icode definition: LOW_LEVEL and HIGH_LEVEL */
  297. struct ICODE
  298. {
  299. // use llvm names at least
  300. typedef BB MachineBasicBlock;
  301. protected:
  302. LLInst m_ll;
  303. HLTYPE m_hl;
  304. MachineBasicBlock * Parent; /* BB to which this icode belongs */
  305. bool invalid; /* Has no HIGH_LEVEL equivalent */
  306. public:
  307. x86_insn_t insn;
  308. template<int FLAG>
  309. struct FlagFilter
  310. {
  311. bool operator()(ICODE *ic) {return ic->ll()->testFlags(FLAG);}
  312. bool operator()(ICODE &ic) {return ic.ll()->testFlags(FLAG);}
  313. };
  314. template<int TYPE>
  315. struct TypeFilter
  316. {
  317. bool operator()(ICODE *ic) {return ic->type==HIGH_LEVEL;}
  318. bool operator()(ICODE &ic) {return ic.type==HIGH_LEVEL;}
  319. };
  320. template<int TYPE>
  321. struct TypeAndValidFilter
  322. {
  323. bool operator()(ICODE *ic) {return (ic->type==HIGH_LEVEL)&&(ic->valid());}
  324. bool operator()(ICODE &ic) {return (ic.type==HIGH_LEVEL)&&ic.valid();}
  325. };
  326. static TypeFilter<HIGH_LEVEL> select_high_level;
  327. static TypeAndValidFilter<HIGH_LEVEL> select_valid_high_level;
  328. /* Def/Use of registers and stack variables */
  329. struct DU_ICODE
  330. {
  331. DU_ICODE()
  332. {
  333. def.reset();
  334. use.reset();
  335. lastDefRegi.reset();
  336. }
  337. std::bitset<32> def; // For Registers: position in bitset is reg index
  338. std::bitset<32> use; // For Registers: position in uint32_t is reg index
  339. std::bitset<32> lastDefRegi;// Bit set if last def of this register in BB
  340. void addDefinedAndUsed(eReg r)
  341. {
  342. def |= duReg[r];
  343. use |= duReg[r];
  344. }
  345. };
  346. struct DU1
  347. {
  348. struct Use
  349. {
  350. int Reg; // used register
  351. std::vector<std::list<ICODE>::iterator> uses; // use locations [MAX_USES]
  352. void removeUser(std::list<ICODE>::iterator us)
  353. {
  354. // ic is no no longer an user
  355. auto iter=std::find(uses.begin(),uses.end(),us);
  356. if(iter==uses.end())
  357. return;
  358. uses.erase(iter);
  359. assert("Same user more then once!" && uses.end()==std::find(uses.begin(),uses.end(),us));
  360. }
  361. };
  362. int numRegsDef; /* # registers defined by this inst */
  363. uint8_t regi[MAX_REGS_DEF+1]; /* registers defined by this inst */
  364. Use idx[MAX_REGS_DEF+1];
  365. //int idx[MAX_REGS_DEF][MAX_USES]; /* inst that uses this def */
  366. bool used(int regIdx)
  367. {
  368. return not idx[regIdx].uses.empty();
  369. }
  370. int numUses(int regIdx)
  371. {
  372. return idx[regIdx].uses.size();
  373. }
  374. void recordUse(int regIdx,std::list<ICODE>::iterator location)
  375. {
  376. idx[regIdx].uses.push_back(location);
  377. }
  378. void remove(int regIdx,int use_idx)
  379. {
  380. idx[regIdx].uses.erase(idx[regIdx].uses.begin()+use_idx);
  381. }
  382. void remove(int regIdx,std::list<ICODE>::iterator ic)
  383. {
  384. Use &u(idx[regIdx]);
  385. u.removeUser(ic);
  386. }
  387. DU1() : numRegsDef(0)
  388. {
  389. }
  390. };
  391. icodeType type; /* Icode type */
  392. DU_ICODE du; /* Def/use regs/vars */
  393. DU1 du1; /* du chain 1 */
  394. int loc_ip; // used by CICodeRec to number ICODEs
  395. LLInst * ll() { return &m_ll;}
  396. const LLInst * ll() const { return &m_ll;}
  397. HLTYPE * hl() { return &m_hl;}
  398. const HLTYPE * hl() const { return &m_hl;}
  399. void hl(const HLTYPE &v) { m_hl=v;}
  400. void setRegDU(eReg regi, operDu du_in);
  401. void invalidate();
  402. void newCallHl();
  403. void writeDU();
  404. condId idType(opLoc sd);
  405. // HLL setting functions
  406. // set this icode to be an assign
  407. void setAsgn(COND_EXPR *lhs, COND_EXPR *rhs)
  408. {
  409. type=HIGH_LEVEL;
  410. hl()->setAsgn(lhs,rhs);
  411. }
  412. void setUnary(hlIcode op, COND_EXPR *_exp);
  413. void setJCond(COND_EXPR *cexp);
  414. void emitGotoLabel(int indLevel);
  415. void copyDU(const ICODE &duIcode, operDu _du, operDu duDu);
  416. bool valid() {return not invalid;}
  417. void setParent(MachineBasicBlock *P) { Parent = P; }
  418. public:
  419. bool removeDefRegi(eReg regi, int thisDefIdx, LOCAL_ID *locId);
  420. void checkHlCall();
  421. bool newStkArg(COND_EXPR *exp, llIcode opcode, Function *pproc)
  422. {
  423. return hl()->call.newStkArg(exp,opcode,pproc);
  424. }
  425. ICODE() : m_ll(this),Parent(0),invalid(false),type(NOT_SCANNED),loc_ip(0)
  426. {
  427. }
  428. public:
  429. const MachineBasicBlock* getParent() const { return Parent; }
  430. MachineBasicBlock* getParent() { return Parent; }
  431. //unsigned getNumOperands() const { return (unsigned)Operands.size(); }
  432. };
  433. /** Map n low level instructions to m high level instructions
  434. */
  435. struct MappingLLtoML
  436. {
  437. typedef llvm::iplist<llvm::Instruction> InstListType;
  438. typedef boost::iterator_range<iICODE> rSourceRange;
  439. typedef boost::iterator_range<InstListType::iterator> rTargetRange;
  440. rSourceRange m_low_level;
  441. rTargetRange m_middle_level;
  442. };
  443. // This is the icode array object.
  444. class CIcodeRec : public std::list<ICODE>
  445. {
  446. public:
  447. CIcodeRec(); // Constructor
  448. ICODE * addIcode(ICODE *pIcode);
  449. void SetInBB(rCODE &rang, BB* pnewBB);
  450. bool labelSrch(uint32_t target, uint32_t &pIndex);
  451. iterator labelSrch(uint32_t target);
  452. ICODE * GetIcode(size_t ip);
  453. bool alreadyDecoded(uint32_t target);
  454. };