icode.h 11 KB

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