icode.h 12 KB

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