icode.h 14 KB

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