graph.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*****************************************************************************
  2. * dcc project CFG related functions
  3. * (C) Cristina Cifuentes
  4. ****************************************************************************/
  5. #include "dcc.h"
  6. #include <string.h>
  7. #if __BORLAND__
  8. #include <alloc.h>
  9. #else
  10. #include <malloc.h> /* For free() */
  11. #endif
  12. #include "graph.h"
  13. //static BB * rmJMP(Function * pProc, Int marker, BB * pBB);
  14. static void mergeFallThrough(Function * pProc, BB * pBB);
  15. static void dfsNumbering(BB * pBB, std::vector<BB*> &dfsLast, Int *first, Int *last);
  16. /*****************************************************************************
  17. * createCFG - Create the basic control flow graph
  18. ****************************************************************************/
  19. void Function::createCFG()
  20. {
  21. /* Splits Icode associated with the procedure into Basic Blocks.
  22. * The links between BBs represent the control flow graph of the
  23. * procedure.
  24. * A Basic Block is defined to end on one of the following instructions:
  25. * 1) Conditional and unconditional jumps
  26. * 2) CALL(F)
  27. * 3) RET(F)
  28. * 4) On the instruction before a join (a flagged TARGET)
  29. * 5) Repeated string instructions
  30. * 6) End of procedure
  31. */
  32. Int i;
  33. Int ip, start;
  34. BB * psBB;
  35. BB * pBB;
  36. iICODE pIcode = Icode.begin();
  37. stats.numBBbef = stats.numBBaft = 0;
  38. for (ip = start = 0; pIcode!=Icode.end(); ip++, pIcode++)
  39. {
  40. /* Stick a NOWHERE_NODE on the end if we terminate
  41. * with anything other than a ret, jump or terminate */
  42. if (ip + 1 == Icode.size() &&
  43. ! (pIcode->ic.ll.flg & TERMINATES) &&
  44. pIcode->ic.ll.opcode != iJMP && pIcode->ic.ll.opcode != iJMPF &&
  45. pIcode->ic.ll.opcode != iRET && pIcode->ic.ll.opcode != iRETF)
  46. {
  47. pBB=BB::Create(start, ip, NOWHERE_NODE, 0, this);
  48. }
  49. /* Only process icodes that have valid instructions */
  50. else if ((pIcode->ic.ll.flg & NO_CODE) != NO_CODE)
  51. {
  52. switch (pIcode->ic.ll.opcode) {
  53. case iJB: case iJBE: case iJAE: case iJA:
  54. case iJL: case iJLE: case iJGE: case iJG:
  55. case iJE: case iJNE: case iJS: case iJNS:
  56. case iJO: case iJNO: case iJP: case iJNP:
  57. case iJCXZ:
  58. pBB = BB::Create(start, ip, TWO_BRANCH, 2, this);
  59. CondJumps:
  60. start = ip + 1;
  61. pBB->edges[0].ip = (dword)start;
  62. /* This is for jumps off into nowhere */
  63. if (pIcode->ic.ll.flg & NO_LABEL)
  64. {
  65. pBB->edges.pop_back();
  66. }
  67. else
  68. pBB->edges[1].ip = pIcode->ic.ll.src.op();
  69. break;
  70. case iLOOP: case iLOOPE: case iLOOPNE:
  71. pBB = BB::Create(start, ip, LOOP_NODE, 2, this);
  72. goto CondJumps;
  73. case iJMPF: case iJMP:
  74. if (pIcode->ic.ll.flg & SWITCH)
  75. {
  76. pBB = BB::Create(start, ip, MULTI_BRANCH, pIcode->ic.ll.caseTbl.numEntries, this);
  77. for (i = 0; i < pIcode->ic.ll.caseTbl.numEntries; i++)
  78. pBB->edges[i].ip = pIcode->ic.ll.caseTbl.entries[i];
  79. hasCase = TRUE;
  80. }
  81. else if ((pIcode->ic.ll.flg & (I | NO_LABEL)) == I)
  82. {
  83. pBB = BB::Create(start, ip, ONE_BRANCH, 1, this);
  84. pBB->edges[0].ip = pIcode->ic.ll.src.op();
  85. }
  86. else
  87. BB::Create(start, ip, NOWHERE_NODE, 0, this);
  88. start = ip + 1;
  89. break;
  90. case iCALLF: case iCALL:
  91. {
  92. Function * p = pIcode->ic.ll.src.proc.proc;
  93. if (p)
  94. i = ((p->flg) & TERMINATES) ? 0 : 1;
  95. else
  96. i = 1;
  97. pBB = BB::Create(start, ip, CALL_NODE, i, this);
  98. start = ip + 1;
  99. if (i)
  100. pBB->edges[0].ip = (dword)start;
  101. }
  102. break;
  103. case iRET: case iRETF:
  104. BB::Create(start, ip, RETURN_NODE, 0, this);
  105. start = ip + 1;
  106. break;
  107. default:
  108. /* Check for exit to DOS */
  109. if (pIcode->ic.ll.flg & TERMINATES)
  110. {
  111. pBB = BB::Create(start, ip, TERMINATE_NODE, 0, this);
  112. start = ip + 1;
  113. }
  114. /* Check for a fall through */
  115. else if (Icode[ip + 1].ic.ll.flg & (TARGET | CASE))
  116. {
  117. pBB = BB::Create(start, ip, FALL_NODE, 1, this);
  118. start = ip + 1;
  119. pBB->edges[0].ip = (dword)start;
  120. }
  121. break;
  122. }
  123. }
  124. }
  125. std::vector<BB *>::iterator iter=heldBBs.begin();
  126. /* Convert list of BBs into a graph */
  127. for (; iter!=heldBBs.end(); ++iter)
  128. {
  129. pBB = *iter;
  130. for (i = 0; i < pBB->edges.size(); i++)
  131. {
  132. ip = pBB->edges[i].ip;
  133. if (ip >= SYNTHESIZED_MIN)
  134. {
  135. fatalError (INVALID_SYNTHETIC_BB);
  136. return ;
  137. }
  138. auto iter2=std::find_if(heldBBs.begin(),heldBBs.end(),
  139. [ip](BB *psBB)->bool {return psBB->begin()==ip;});
  140. if(iter2==heldBBs.end())
  141. fatalError(NO_BB, ip, name.c_str());
  142. psBB = *iter2;
  143. pBB->edges[i].BBptr = psBB;
  144. psBB->inEdges.push_back(0);
  145. }
  146. }
  147. }
  148. void Function::markImpure()
  149. {
  150. SYM * psym;
  151. for(ICODE &icod : Icode)
  152. {
  153. if ( not icod.isLlFlag(SYM_USE | SYM_DEF))
  154. continue;
  155. psym = &symtab[icod.ic.ll.caseTbl.numEntries];
  156. for (int c = (Int)psym->label; c < (Int)psym->label+psym->size; c++)
  157. {
  158. if (BITMAP(c, BM_CODE))
  159. {
  160. icod.SetLlFlag(IMPURE);
  161. flg |= IMPURE;
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. /*****************************************************************************
  168. * newBB - Allocate new BB and link to end of list
  169. *****************************************************************************/
  170. /*****************************************************************************
  171. * freeCFG - Deallocates a cfg
  172. ****************************************************************************/
  173. void Function::freeCFG()
  174. {
  175. for(BB *p : heldBBs)
  176. delete p;
  177. }
  178. /*****************************************************************************
  179. * compressCFG - Remove redundancies and add in-edge information
  180. ****************************************************************************/
  181. void Function::compressCFG()
  182. {
  183. BB * pBB, *pNxt;
  184. Int ip, first=0, last, i;
  185. /* First pass over BB list removes redundant jumps of the form
  186. * (Un)Conditional -> Unconditional jump */
  187. auto iter=m_cfg.begin();
  188. for (;iter!=m_cfg.end(); ++iter)
  189. {
  190. pBB = *iter;
  191. if(pBB->inEdges.empty() || (pBB->nodeType != ONE_BRANCH && pBB->nodeType != TWO_BRANCH))
  192. continue;
  193. for (i = 0; i < pBB->edges.size(); i++)
  194. {
  195. ip = pBB->rbegin();
  196. pNxt = pBB->edges[i].BBptr->rmJMP(ip, pBB->edges[i].BBptr);
  197. if (not pBB->edges.empty()) /* Might have been clobbered */
  198. {
  199. pBB->edges[i].BBptr = pNxt;
  200. Icode[ip].SetImmediateOp((dword)pNxt->begin());
  201. }
  202. }
  203. }
  204. /* Next is a depth-first traversal merging any FALL_NODE or
  205. * ONE_BRANCH that fall through to a node with that as their only
  206. * in-edge. */
  207. m_cfg.front()->mergeFallThrough(Icode);
  208. /* Remove redundant BBs created by the above compressions
  209. * and allocate in-edge arrays as required. */
  210. stats.numBBaft = stats.numBBbef;
  211. for(auto iter=m_cfg.begin(); iter!=m_cfg.end(); ++iter)
  212. {
  213. pBB = *iter;
  214. if (pBB->inEdges.empty())
  215. {
  216. if (iter == m_cfg.begin()) /* Init it misses out on */
  217. pBB->index = UN_INIT;
  218. else
  219. {
  220. delete pBB;
  221. stats.numBBaft--;
  222. }
  223. }
  224. else
  225. {
  226. pBB->inEdgeCount = pBB->inEdges.size();
  227. }
  228. }
  229. /* Allocate storage for dfsLast[] array */
  230. numBBs = stats.numBBaft;
  231. m_dfsLast.resize(numBBs,0); // = (BB **)allocMem(numBBs * sizeof(BB *))
  232. /* Now do a dfs numbering traversal and fill in the inEdges[] array */
  233. last = numBBs - 1;
  234. m_cfg.front()->dfsNumbering(m_dfsLast, &first, &last);
  235. }
  236. /****************************************************************************
  237. * rmJMP - If BB addressed is just a JMP it is replaced with its target
  238. ***************************************************************************/
  239. BB *BB::rmJMP(Int marker, BB * pBB)
  240. {
  241. marker += DFS_JMP;
  242. while (pBB->nodeType == ONE_BRANCH && pBB->size() == 1)
  243. {
  244. if (pBB->traversed != marker)
  245. {
  246. pBB->traversed = marker;
  247. pBB->inEdges.pop_back();
  248. if (not pBB->inEdges.empty())
  249. {
  250. pBB->edges[0].BBptr->inEdges.push_back(0);
  251. }
  252. else
  253. {
  254. pBB->front().SetLlFlag(NO_CODE);
  255. pBB->front().invalidate(); //pProc->Icode.SetLlInvalid(pBB->begin(), TRUE);
  256. }
  257. pBB = pBB->edges[0].BBptr;
  258. }
  259. else
  260. {
  261. /* We are going around in circles */
  262. pBB->nodeType = NOWHERE_NODE;
  263. pBB->front().ic.ll.src.SetImmediateOp(pBB->front().loc_ip);
  264. //pBB->front().ic.ll.src.immed.op = pBB->front().loc_ip;
  265. do {
  266. pBB = pBB->edges[0].BBptr;
  267. pBB->inEdges.pop_back(); // was --numInedges
  268. if (! pBB->inEdges.empty())
  269. {
  270. pBB->front().SetLlFlag(NO_CODE);
  271. pBB->front().invalidate();
  272. // pProc->Icode.SetLlFlag(pBB->start, NO_CODE);
  273. // pProc->Icode.SetLlInvalid(pBB->start, TRUE);
  274. }
  275. } while (pBB->nodeType != NOWHERE_NODE);
  276. pBB->edges.clear();
  277. }
  278. }
  279. return pBB;
  280. }
  281. /*****************************************************************************
  282. * mergeFallThrough
  283. ****************************************************************************/
  284. void BB::mergeFallThrough( CIcodeRec &Icode)
  285. {
  286. BB * pChild;
  287. Int i;
  288. if (!this)
  289. {
  290. printf("mergeFallThrough on empty BB!\n");
  291. }
  292. while (nodeType == FALL_NODE || nodeType == ONE_BRANCH)
  293. {
  294. pChild = edges[0].BBptr;
  295. /* Jump to next instruction can always be removed */
  296. if (nodeType == ONE_BRANCH)
  297. {
  298. assert(Parent==pChild->Parent);
  299. if(back().loc_ip>pChild->front().loc_ip) // back edege
  300. break;
  301. auto iter=std::find_if(this->end2(),pChild->begin2(),[](ICODE &c)
  302. {return not c.isLlFlag(NO_CODE);});
  303. if (iter != pChild->begin2())
  304. break;
  305. back().SetLlFlag(NO_CODE);
  306. back().invalidate();
  307. nodeType = FALL_NODE;
  308. length--;
  309. }
  310. /* If there's no other edges into child can merge */
  311. if (pChild->inEdges.size() != 1)
  312. break;
  313. nodeType = pChild->nodeType;
  314. length = (pChild->start - start) + pChild->length ;
  315. pChild->front().ClrLlFlag(TARGET);
  316. edges.swap(pChild->edges);
  317. pChild->inEdges.clear();
  318. pChild->edges.clear();
  319. }
  320. traversed = DFS_MERGE;
  321. /* Process all out edges recursively */
  322. for (i = 0; i < edges.size(); i++)
  323. if (edges[i].BBptr->traversed != DFS_MERGE)
  324. edges[i].BBptr->mergeFallThrough(Icode);
  325. }
  326. /*****************************************************************************
  327. * dfsNumbering - Numbers nodes during first and last visits and determine
  328. * in-edges
  329. ****************************************************************************/
  330. void BB::dfsNumbering(std::vector<BB *> &dfsLast, Int *first, Int *last)
  331. {
  332. BB * pChild;
  333. traversed = DFS_NUM;
  334. dfsFirstNum = (*first)++;
  335. /* index is being used as an index to inEdges[]. */
  336. // for (i = 0; i < edges.size(); i++)
  337. for (auto edge : edges)
  338. {
  339. pChild = edge.BBptr;
  340. pChild->inEdges[pChild->index++] = this;
  341. /* Is this the last visit? */
  342. if (pChild->index == pChild->inEdges.size())
  343. pChild->index = UN_INIT;
  344. if (pChild->traversed != DFS_NUM)
  345. pChild->dfsNumbering(dfsLast, first, last);
  346. }
  347. dfsLastNum = *last;
  348. dfsLast[(*last)--] = this;
  349. }