dataflow.cpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*****************************************************************************
  2. * Project: dcc
  3. * File: dataflow.c
  4. * Purpose: Data flow analysis module.
  5. * (C) Cristina Cifuentes
  6. ****************************************************************************/
  7. #include "dcc.h"
  8. #include <string.h>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <stdio.h>
  12. struct ExpStack
  13. {
  14. typedef std::list<COND_EXPR *> EXP_STK;
  15. EXP_STK expStk; /* local expression stack */
  16. void init();
  17. void push(COND_EXPR *);
  18. COND_EXPR * pop();
  19. int numElem();
  20. boolT empty();
  21. };
  22. /***************************************************************************
  23. * Expression stack functions
  24. **************************************************************************/
  25. /* Reinitalizes the expression stack (expStk) to NULL, by freeing all the
  26. * space allocated (if any). */
  27. void ExpStack::init()
  28. {
  29. expStk.clear();
  30. }
  31. /* Pushes the given expression onto the local stack (expStk). */
  32. void ExpStack::push(COND_EXPR *expr)
  33. {
  34. expStk.push_back(expr);
  35. }
  36. /* Returns the element on the top of the local expression stack (expStk),
  37. * and deallocates the space allocated by this node.
  38. * If there are no elements on the stack, returns NULL. */
  39. COND_EXPR *ExpStack::pop()
  40. {
  41. if(expStk.empty())
  42. return 0;
  43. COND_EXPR *topExp = expStk.back();
  44. expStk.pop_back();
  45. return topExp;
  46. }
  47. /* Returns the number of elements available in the expression stack */
  48. int ExpStack::numElem()
  49. {
  50. return expStk.size();
  51. }
  52. /* Returns whether the expression stack is empty or not */
  53. boolT ExpStack::empty()
  54. {
  55. return expStk.empty();
  56. }
  57. using namespace std;
  58. ExpStack g_exp_stk;
  59. /* Returns the index of the local variable or parameter at offset off, if it
  60. * is in the stack frame provided. */
  61. int STKFRAME::getLocVar(int off)
  62. {
  63. int i;
  64. for (i = 0; i < sym.size(); i++)
  65. if (sym[i].off == off)
  66. break;
  67. return (i);
  68. }
  69. /* Returns a string with the source operand of Icode */
  70. static COND_EXPR *srcIdent (const ICODE &Icode, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
  71. {
  72. if (Icode.ll()->isLlFlag(I)) /* immediate operand */
  73. {
  74. if (Icode.ll()->isLlFlag(B))
  75. return COND_EXPR::idKte (Icode.ll()->src.op(), 1);
  76. return COND_EXPR::idKte (Icode.ll()->src.op(), 2);
  77. }
  78. // otherwise
  79. return COND_EXPR::id (Icode, SRC, pProc, i, duIcode, du);
  80. }
  81. /* Returns the destination operand */
  82. static COND_EXPR *dstIdent (const ICODE & Icode, Function * pProc, iICODE i, ICODE & duIcode, operDu du)
  83. {
  84. COND_EXPR *n;
  85. n = COND_EXPR::id (Icode, DST, pProc, i, duIcode, du);
  86. /** Is it needed? (pIcode->ll()->flg) & NO_SRC_B **/
  87. return (n);
  88. }
  89. /* Eliminates all condition codes and generates new hlIcode instructions */
  90. void Function::elimCondCodes ()
  91. {
  92. int i;
  93. uint8_t use; /* Used flags bit vector */
  94. uint8_t def; /* Defined flags bit vector */
  95. boolT notSup; /* Use/def combination not supported */
  96. COND_EXPR *rhs; /* Source operand */
  97. COND_EXPR *lhs; /* Destination operand */
  98. COND_EXPR *exp; /* Boolean expression */
  99. BB * pBB; /* Pointer to BBs in dfs last ordering */
  100. riICODE useAt; /* Instruction that used flag */
  101. riICODE defAt; /* Instruction that defined flag */
  102. for (i = 0; i < numBBs; i++)
  103. {
  104. pBB = m_dfsLast[i];
  105. if (pBB->flg & INVALID_BB)
  106. continue; /* Do not process invalid BBs */
  107. for (useAt = pBB->rbegin2(); useAt != pBB->rend2(); useAt++)
  108. {
  109. llIcode useAtOp = useAt->ll()->GetLlOpcode();
  110. if ((useAt->type == LOW_LEVEL) && (useAt->valid()) && (use = useAt->ll()->flagDU.u))
  111. {
  112. /* Find definition within the same basic block */
  113. defAt=useAt;
  114. ++defAt;
  115. for (; defAt != pBB->rend2(); defAt++)
  116. {
  117. def = defAt->ll()->flagDU.d;
  118. if ((use & def) != use)
  119. continue;
  120. notSup = FALSE;
  121. if ((useAtOp >= iJB) && (useAtOp <= iJNS))
  122. {
  123. iICODE befDefAt = (++riICODE(defAt)).base();
  124. switch (defAt->ll()->GetLlOpcode())
  125. {
  126. case iCMP:
  127. rhs = srcIdent (*defAt, this, befDefAt,*useAt, eUSE);
  128. lhs = dstIdent (*defAt, this, befDefAt,*useAt, eUSE);
  129. break;
  130. case iOR:
  131. lhs = defAt->hl()->asgn.lhs->clone();
  132. useAt->copyDU(*defAt, eUSE, eDEF);
  133. if (defAt->ll()->isLlFlag(B))
  134. rhs = COND_EXPR::idKte (0, 1);
  135. else
  136. rhs = COND_EXPR::idKte (0, 2);
  137. break;
  138. case iTEST:
  139. rhs = srcIdent (*defAt,this, befDefAt,*useAt, eUSE);
  140. lhs = dstIdent (*defAt,this, befDefAt,*useAt, eUSE);
  141. lhs = COND_EXPR::boolOp (lhs, rhs, AND);
  142. if (defAt->ll()->isLlFlag(B))
  143. rhs = COND_EXPR::idKte (0, 1);
  144. else
  145. rhs = COND_EXPR::idKte (0, 2);
  146. break;
  147. default:
  148. notSup = TRUE;
  149. std::cout << hex<<defAt->loc_ip;
  150. reportError (JX_NOT_DEF, defAt->ll()->GetLlOpcode());
  151. flg |= PROC_ASM; /* generate asm */
  152. }
  153. if (! notSup)
  154. {
  155. exp = COND_EXPR::boolOp (lhs, rhs,condOpJCond[useAtOp-iJB]);
  156. useAt->setJCond(exp);
  157. }
  158. }
  159. else if (useAtOp == iJCXZ)
  160. {
  161. lhs = COND_EXPR::idReg (rCX, 0, &localId);
  162. useAt->setRegDU (rCX, eUSE);
  163. rhs = COND_EXPR::idKte (0, 2);
  164. exp = COND_EXPR::boolOp (lhs, rhs, EQUAL);
  165. useAt->setJCond(exp);
  166. }
  167. // else if (useAt->GetLlOpcode() == iRCL)
  168. // {
  169. // ICODE &a(*defAt);
  170. // ICODE &b(*useAt);
  171. // if(a.GetLlOpcode() == iRCL)
  172. // {
  173. // if ((b.ll()->flg & NO_SRC) != NO_SRC) /* if there is src op */
  174. // rhs = COND_EXPR::id (*useAt, SRC, this, Icode.end(), *useAt, NONE);
  175. // lhs = COND_EXPR::id (*useAt, DST, this, Icode.end(), *useAt, USE_DEF);
  176. // rhs = COND_EXPR::boolOp (lhs, rhs, SHL);
  177. // useAt->setAsgn(lhs->clone(), rhs);
  178. // printf("RCL\n");
  179. // }
  180. // }
  181. else
  182. {
  183. ICODE &a(*defAt);
  184. ICODE &b(*useAt);
  185. reportError (NOT_DEF_USE,a.ll()->GetLlOpcode(),b.ll()->GetLlOpcode());
  186. flg |= PROC_ASM; /* generate asm */
  187. }
  188. break;
  189. }
  190. /* Check for extended basic block */
  191. if ((pBB->size() == 1) &&(useAtOp >= iJB) && (useAtOp <= iJNS))
  192. {
  193. ICODE & prev(pBB->back()); /* For extended basic blocks - previous icode inst */
  194. if (prev.hl()->opcode == HLI_JCOND)
  195. {
  196. exp = prev.hl()->expr()->clone();
  197. exp->changeBoolOp (condOpJCond[useAtOp-iJB]);
  198. useAt->copyDU(prev, eUSE, eUSE);
  199. useAt->setJCond(exp);
  200. }
  201. }
  202. /* Error - definition not found for use of a cond code */
  203. else if (defAt == pBB->rend2())
  204. {
  205. reportError(DEF_NOT_FOUND,useAtOp);
  206. //fatalError (DEF_NOT_FOUND, Icode.GetLlOpcode(useAt-1));
  207. }
  208. }
  209. }
  210. }
  211. }
  212. /** Generates the LiveUse() and Def() sets for each basic block in the graph.
  213. * Note: these sets are constant and could have been constructed during
  214. * the construction of the graph, but since the code hasn't been
  215. * analyzed yet for idioms, the procedure preamble misleads the
  216. * analysis (eg: push si, would include si in LiveUse; although it
  217. * is not really meant to be a register that is used before defined). */
  218. void Function::genLiveKtes ()
  219. {
  220. int i;
  221. BB * pbb;
  222. bitset<32> liveUse, def;
  223. for (i = 0; i < numBBs; i++)
  224. {
  225. liveUse.reset();
  226. def.reset();
  227. pbb = m_dfsLast[i];
  228. if (pbb->flg & INVALID_BB)
  229. continue; // skip invalid BBs
  230. for (auto j = pbb->begin2(); j != pbb->end2(); j++)
  231. {
  232. if ((j->type == HIGH_LEVEL) && (j->invalid == FALSE))
  233. {
  234. liveUse |= (j->du.use & ~def);
  235. def |= j->du.def;
  236. }
  237. }
  238. pbb->liveUse = liveUse;
  239. pbb->def = def;
  240. }
  241. }
  242. /* Generates the liveIn() and liveOut() sets for each basic block via an
  243. * iterative approach.
  244. * Propagates register usage information to the procedure call. */
  245. void Function::liveRegAnalysis (std::bitset<32> &in_liveOut)
  246. {
  247. int i, j;
  248. BB * pbb=0; /* pointer to current basic block */
  249. Function * pcallee; /* invoked subroutine */
  250. //ICODE *ticode /* icode that invokes a subroutine */
  251. ;
  252. std::bitset<32> prevLiveOut, /* previous live out */
  253. prevLiveIn; /* previous live in */
  254. boolT change; /* is there change in the live sets?*/
  255. /* liveOut for this procedure */
  256. liveOut = in_liveOut;
  257. change = true;
  258. while (change)
  259. {
  260. /* Process nodes in reverse postorder order */
  261. change = false;
  262. //for (i = numBBs; i > 0; i--)
  263. for(auto iBB=m_dfsLast.rbegin(); iBB!=m_dfsLast.rend(); ++iBB)
  264. {
  265. pbb = *iBB;//m_dfsLast[i-1];
  266. if (pbb->flg & INVALID_BB) /* Do not process invalid BBs */
  267. continue;
  268. /* Get current liveIn() and liveOut() sets */
  269. prevLiveIn = pbb->liveIn;
  270. prevLiveOut = pbb->liveOut;
  271. /* liveOut(b) = U LiveIn(s); where s is successor(b)
  272. * liveOut(b) = {liveOut}; when b is a HLI_RET node */
  273. if (pbb->edges.empty()) /* HLI_RET node */
  274. {
  275. pbb->liveOut = in_liveOut;
  276. /* Get return expression of function */
  277. if (flg & PROC_IS_FUNC)
  278. {
  279. auto picode = pbb->rbegin2(); /* icode of function return */
  280. if (picode->hl()->opcode == HLI_RET)
  281. {
  282. //pbb->back().loc_ip
  283. picode->hl()->expr(COND_EXPR::idID (&retVal, &localId, (++pbb->rbegin2()).base()));
  284. picode->du.use = in_liveOut;
  285. }
  286. }
  287. }
  288. else /* Check successors */
  289. {
  290. for(TYPEADR_TYPE &e : pbb->edges)
  291. pbb->liveOut |= e.BBptr->liveIn;
  292. /* propagate to invoked procedure */
  293. if (pbb->nodeType == CALL_NODE)
  294. {
  295. ICODE &ticode(pbb->back());
  296. pcallee = ticode.hl()->call.proc;
  297. /* user/runtime routine */
  298. if (! (pcallee->flg & PROC_ISLIB))
  299. {
  300. if (pcallee->liveAnal == FALSE) /* hasn't been processed */
  301. pcallee->dataFlow (pbb->liveOut);
  302. pbb->liveOut = pcallee->liveIn;
  303. }
  304. else /* library routine */
  305. {
  306. if ( (pcallee->flg & PROC_IS_FUNC) && /* returns a value */
  307. (pcallee->liveOut & pbb->edges[0].BBptr->liveIn).any()
  308. )
  309. pbb->liveOut = pcallee->liveOut;
  310. else
  311. pbb->liveOut = 0;
  312. }
  313. if ((! (pcallee->flg & PROC_ISLIB)) || (pbb->liveOut != 0))
  314. {
  315. switch (pcallee->retVal.type) {
  316. case TYPE_LONG_SIGN: case TYPE_LONG_UNSIGN:
  317. ticode.du1.numRegsDef = 2;
  318. break;
  319. case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN:
  320. case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
  321. ticode.du1.numRegsDef = 1;
  322. break;
  323. default:
  324. fprintf(stderr,"Function::liveRegAnalysis : Unknown return type %d\n",pcallee->retVal.type);
  325. } /*eos*/
  326. /* Propagate def/use results to calling icode */
  327. ticode.du.use = pcallee->liveIn;
  328. ticode.du.def = pcallee->liveOut;
  329. }
  330. }
  331. }
  332. /* liveIn(b) = liveUse(b) U (liveOut(b) - def(b) */
  333. pbb->liveIn = pbb->liveUse | (pbb->liveOut & ~pbb->def);
  334. /* Check if live sets have been modified */
  335. if ((prevLiveIn != pbb->liveIn) || (prevLiveOut != pbb->liveOut))
  336. change = true;
  337. }
  338. }
  339. /* Propagate liveIn(b) to procedure header */
  340. if (pbb->liveIn != 0) /* uses registers */
  341. liveIn = pbb->liveIn;
  342. /* Remove any references to register variables */
  343. if (flg & SI_REGVAR)
  344. {
  345. liveIn &= maskDuReg[rSI];
  346. pbb->liveIn &= maskDuReg[rSI];
  347. }
  348. if (flg & DI_REGVAR)
  349. {
  350. liveIn &= maskDuReg[rDI];
  351. pbb->liveIn &= maskDuReg[rDI];
  352. }
  353. }
  354. void BB::genDU1()
  355. {
  356. uint8_t regi; /* Register that was defined */
  357. int k, defRegIdx, useIdx;
  358. iICODE picode, ticode,lastInst;
  359. BB *tbb; /* Target basic block */
  360. bool res;
  361. //COND_EXPR *e
  362. /* Process each register definition of a HIGH_LEVEL icode instruction.
  363. * Note that register variables should not be considered registers.
  364. */
  365. assert(0!=Parent);
  366. lastInst = this->end2();
  367. for (picode = this->begin2(); picode != lastInst; picode++)
  368. {
  369. if (picode->type != HIGH_LEVEL)
  370. continue;
  371. regi = 0;
  372. defRegIdx = 0;
  373. // foreach defined register
  374. bitset<32> processed=0;
  375. for (k = 0; k < INDEXBASE; k++)
  376. {
  377. if (not picode->du.def.test(k))
  378. continue;
  379. //printf("Processing reg")
  380. processed |= duReg[k];
  381. regi = (uint8_t)(k + 1); /* defined register */
  382. picode->du1.regi[defRegIdx] = regi;
  383. /* Check remaining instructions of the BB for all uses
  384. * of register regi, before any definitions of the
  385. * register */
  386. if ((regi == rDI) && (flg & DI_REGVAR))
  387. continue;
  388. if ((regi == rSI) && (flg & SI_REGVAR))
  389. continue;
  390. if (distance(picode,lastInst)>1) /* several instructions */
  391. {
  392. useIdx = 0;
  393. for (auto ricode = ++iICODE(picode); ricode != lastInst; ricode++)
  394. {
  395. ticode=ricode;
  396. if (ricode->type != HIGH_LEVEL) // Only check uses of HIGH_LEVEL icodes
  397. continue;
  398. /* if used, get icode index */
  399. if ((ricode->du.use & duReg[regi]).any())
  400. picode->du1.recordUse(defRegIdx,ricode);
  401. /* if defined, stop finding uses for this reg */
  402. if ((ricode->du.def & duReg[regi]).any())
  403. break;
  404. }
  405. /* Check if last definition of this register */
  406. if ((not (ticode->du.def & duReg[regi]).any()) and (this->liveOut & duReg[regi]).any())
  407. picode->du.lastDefRegi |= duReg[regi];
  408. }
  409. else /* only 1 instruction in this basic block */
  410. {
  411. /* Check if last definition of this register */
  412. if ((this->liveOut & duReg[regi]).any())
  413. picode->du.lastDefRegi |= duReg[regi];
  414. }
  415. /* Find target icode for HLI_CALL icodes to procedures
  416. * that are functions. The target icode is in the
  417. * next basic block (unoptimized code) or somewhere else
  418. * on optimized code. */
  419. if ((picode->hl()->opcode == HLI_CALL) &&
  420. (picode->hl()->call.proc->flg & PROC_IS_FUNC))
  421. {
  422. tbb = this->edges[0].BBptr;
  423. for (ticode = tbb->begin2(); ticode != tbb->end2(); ticode++)
  424. {
  425. if (ticode->type != HIGH_LEVEL)
  426. continue;
  427. /* if used, get icode index */
  428. if ((ticode->du.use & duReg[regi]).any())
  429. picode->du1.recordUse(defRegIdx,ticode);
  430. /* if defined, stop finding uses for this reg */
  431. if ((ticode->du.def & duReg[regi]).any())
  432. break;
  433. }
  434. /* if not used in this basic block, check if the
  435. * register is live out, if so, make it the last
  436. * definition of this register */
  437. if ( picode->du1.used(defRegIdx) && (tbb->liveOut & duReg[regi]).any())
  438. picode->du.lastDefRegi |= duReg[regi];
  439. }
  440. /* If not used within this bb or in successors of this
  441. * bb (ie. not in liveOut), then register is useless,
  442. * thus remove it. Also check that this is not a return
  443. * from a library function (routines such as printf
  444. * return an integer, which is normally not taken into
  445. * account by the programmer). */
  446. if (picode->valid() and not picode->du1.used(defRegIdx) and
  447. (not (picode->du.lastDefRegi & duReg[regi]).any()) &&
  448. (not ((picode->hl()->opcode == HLI_CALL) &&
  449. (picode->hl()->call.proc->flg & PROC_ISLIB))))
  450. {
  451. if (! (this->liveOut & duReg[regi]).any()) /* not liveOut */
  452. {
  453. res = picode->removeDefRegi (regi, defRegIdx+1,&Parent->localId);
  454. if (res != true)
  455. {
  456. defRegIdx++;
  457. continue;
  458. }
  459. /* Backpatch any uses of this instruction, within
  460. * the same BB, if the instruction was invalidated */
  461. for (auto ticode = riICODE(picode); ticode != this->rend2(); ticode++)
  462. {
  463. ticode->du1.remove(0,picode);
  464. }
  465. }
  466. else /* liveOut */
  467. picode->du.lastDefRegi |= duReg[regi];
  468. }
  469. defRegIdx++;
  470. /* Check if all defined registers have been processed */
  471. if ((defRegIdx >= picode->du1.numRegsDef) || (defRegIdx == MAX_REGS_DEF))
  472. break;
  473. }
  474. }
  475. }
  476. /* Generates the du chain of each instruction in a basic block */
  477. void Function::genDU1 ()
  478. {
  479. uint8_t regi; /* Register that was defined */
  480. int i, k, defRegIdx, useIdx;
  481. iICODE picode, ticode,lastInst;/* Current and target bb */
  482. BB * pbb, *tbb; /* Current and target basic block */
  483. bool res;
  484. //COND_EXPR *exp, *lhs;
  485. /* Traverse tree in dfsLast order */
  486. assert(m_dfsLast.size()==numBBs);
  487. for(BB *pbb : m_dfsLast)
  488. {
  489. if (pbb->flg & INVALID_BB)
  490. continue;
  491. pbb->genDU1();
  492. }
  493. }
  494. /* Substitutes the rhs (or lhs if rhs not possible) of ticode for the rhs
  495. * of picode. */
  496. static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, iICODE picode,
  497. iICODE ticode, LOCAL_ID *locsym, int &numHlIcodes)
  498. {
  499. boolT res;
  500. if (rhs == NULL) /* In case expression popped is NULL */
  501. return;
  502. /* Insert on rhs of ticode, if possible */
  503. res = insertSubTreeReg (rhs, &ticode->hl()->asgn.rhs,
  504. locsym->id_arr[lhs->expr.ident.idNode.regiIdx].id.regi,
  505. locsym);
  506. if (res)
  507. {
  508. picode->invalidate();
  509. numHlIcodes--;
  510. }
  511. else
  512. {
  513. /* Try to insert it on lhs of ticode*/
  514. res = insertSubTreeReg (rhs, &ticode->hl()->asgn.lhs,
  515. locsym->id_arr[lhs->expr.ident.idNode.regiIdx].id.regi,
  516. locsym);
  517. if (res)
  518. {
  519. picode->invalidate();
  520. numHlIcodes--;
  521. }
  522. }
  523. }
  524. /* Substitutes the rhs (or lhs if rhs not possible) of ticode for the
  525. * expression exp given */
  526. static void forwardSubsLong (int longIdx, COND_EXPR *exp, iICODE picode,
  527. iICODE ticode, int *numHlIcodes)
  528. {
  529. bool res;
  530. if (exp == NULL) /* In case expression popped is NULL */
  531. return;
  532. /* Insert on rhs of ticode, if possible */
  533. res = insertSubTreeLongReg (exp, &ticode->hl()->asgn.rhs, longIdx);
  534. if (res)
  535. {
  536. picode->invalidate();
  537. (*numHlIcodes)--;
  538. }
  539. else
  540. {
  541. /* Try to insert it on lhs of ticode*/
  542. res = insertSubTreeLongReg (exp, &ticode->hl()->asgn.lhs, longIdx);
  543. if (res)
  544. {
  545. picode->invalidate();
  546. (*numHlIcodes)--;
  547. }
  548. }
  549. }
  550. /* Returns whether the elements of the expression rhs are all x-clear from
  551. * instruction f up to instruction t. */
  552. static boolT xClear (COND_EXPR *rhs, iICODE f, iICODE t, iICODE lastBBinst, Function * pproc)
  553. {
  554. iICODE i;
  555. boolT res;
  556. uint8_t regi;
  557. if (rhs == NULL)
  558. return false;
  559. switch (rhs->type)
  560. {
  561. case IDENTIFIER:
  562. if (rhs->expr.ident.idType == REGISTER)
  563. {
  564. regi= pproc->localId.id_arr[rhs->expr.ident.idNode.regiIdx].id.regi;
  565. for (i = ++iICODE(f); (i != lastBBinst) && (i!=t); i++)
  566. if ((i->type == HIGH_LEVEL) && ( not i->invalid ))
  567. {
  568. if ((i->du.def & duReg[regi]).any())
  569. return false;
  570. }
  571. if (i != lastBBinst)
  572. return true;
  573. return false;
  574. }
  575. else
  576. return true;
  577. /* else if (rhs->expr.ident.idType == LONG_VAR)
  578. {
  579. missing all other identifiers ****
  580. } */
  581. case BOOLEAN_OP:
  582. res = xClear (rhs->expr.boolExpr.rhs, f, t, lastBBinst, pproc);
  583. if (res == FALSE)
  584. return false;
  585. return (xClear (rhs->expr.boolExpr.lhs, f, t, lastBBinst, pproc));
  586. case NEGATION:
  587. case ADDRESSOF:
  588. case DEREFERENCE:
  589. return (xClear (rhs->expr.unaryExp, f, t, lastBBinst, pproc));
  590. } /* eos */
  591. return false;
  592. }
  593. /* Checks the type of the formal argument as against to the actual argument,
  594. * whenever possible, and then places the actual argument on the procedure's
  595. * argument list. */
  596. static void processCArg (Function * pp, Function * pProc, ICODE * picode, int numArgs, int *k)
  597. {
  598. COND_EXPR *exp;
  599. boolT res;
  600. /* if (numArgs == 0)
  601. return; */
  602. exp = g_exp_stk.pop();
  603. if (pp->flg & PROC_ISLIB) /* library function */
  604. {
  605. if (pp->args.numArgs > 0)
  606. if (pp->flg & PROC_VARARG)
  607. {
  608. if (numArgs < pp->args.sym.size())
  609. adjustActArgType (exp, pp->args.sym[numArgs].type, pProc);
  610. }
  611. else
  612. adjustActArgType (exp, pp->args.sym[numArgs].type, pProc);
  613. res = picode->newStkArg (exp, picode->ll()->opcode, pProc);
  614. }
  615. else /* user function */
  616. {
  617. if (pp->args.numArgs > 0)
  618. pp->args.adjustForArgType (numArgs, expType (exp, pProc));
  619. res = picode->newStkArg (exp, picode->ll()->opcode, pProc);
  620. }
  621. /* Do not update the size of k if the expression was a segment register
  622. * in a near call */
  623. if (res == FALSE)
  624. *k += hlTypeSize (exp, pProc);
  625. }
  626. /** Eliminates extraneous intermediate icode instructions when finding
  627. * expressions. Generates new hlIcodes in the form of expression trees.
  628. * For HLI_CALL hlIcodes, places the arguments in the argument list. */
  629. void Function::findExps()
  630. {
  631. int i, k, numHlIcodes;
  632. iICODE lastInst,
  633. picode, // Current icode */
  634. ticode; // Target icode */
  635. BB * pbb; // Current and next basic block */
  636. boolT res;
  637. COND_EXPR *exp, // expression pointer - for HLI_POP and HLI_CALL */
  638. *lhs; // exp ptr for return value of a HLI_CALL */
  639. //STKFRAME * args; // pointer to arguments - for HLI_CALL */
  640. uint8_t regi; // register(s) to be forward substituted */
  641. ID *retVal; // function return value
  642. /* Initialize expression stack */
  643. g_exp_stk.init();
  644. /* Traverse tree in dfsLast order */
  645. for (i = 0; i < numBBs; i++)
  646. {
  647. /* Process one BB */
  648. pbb = m_dfsLast[i];
  649. if (pbb->flg & INVALID_BB)
  650. continue;
  651. lastInst = pbb->end2();
  652. numHlIcodes = 0;
  653. for (picode = pbb->begin2(); picode != lastInst; picode++)
  654. {
  655. if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE))
  656. {
  657. numHlIcodes++;
  658. if (picode->du1.numRegsDef == 1) /* uint8_t/uint16_t regs */
  659. {
  660. /* Check for only one use of this register. If this is
  661. * the last definition of the register in this BB, check
  662. * that it is not liveOut from this basic block */
  663. if (picode->du1.numUses(0)==1)
  664. {
  665. /* Check that this register is not liveOut, if it
  666. * is the last definition of the register */
  667. regi = picode->du1.regi[0];
  668. /* Check if we can forward substitute this register */
  669. switch (picode->hl()->opcode)
  670. {
  671. case HLI_ASSIGN:
  672. /* Replace rhs of current icode into target
  673. * icode expression */
  674. ticode = picode->du1.idx[0].uses.front();
  675. if ((picode->du.lastDefRegi & duReg[regi]).any() &&
  676. ((ticode->hl()->opcode != HLI_CALL) &&
  677. (ticode->hl()->opcode != HLI_RET)))
  678. continue;
  679. if (xClear (picode->hl()->asgn.rhs, picode,
  680. picode->du1.idx[0].uses[0], lastInst, this))
  681. {
  682. switch (ticode->hl()->opcode) {
  683. case HLI_ASSIGN:
  684. forwardSubs (picode->hl()->asgn.lhs,
  685. picode->hl()->asgn.rhs,
  686. picode, ticode, &localId,
  687. numHlIcodes);
  688. break;
  689. case HLI_JCOND: case HLI_PUSH: case HLI_RET:
  690. res = insertSubTreeReg (
  691. picode->hl()->asgn.rhs,
  692. &ticode->hl()->exp.v,
  693. localId.id_arr[picode->hl()->asgn.lhs->expr.ident.idNode.regiIdx].id.regi,
  694. &localId);
  695. if (res)
  696. {
  697. picode->invalidate();
  698. numHlIcodes--;
  699. }
  700. break;
  701. case HLI_CALL: /* register arguments */
  702. newRegArg (picode, ticode);
  703. picode->invalidate();
  704. numHlIcodes--;
  705. break;
  706. } /* eos */
  707. }
  708. break;
  709. case HLI_POP:
  710. ticode = picode->du1.idx[0].uses.front();
  711. if ((picode->du.lastDefRegi & duReg[regi]).any() &&
  712. ((ticode->hl()->opcode != HLI_CALL) &&
  713. (ticode->hl()->opcode != HLI_RET)))
  714. continue;
  715. exp = g_exp_stk.pop(); /* pop last exp pushed */
  716. switch (ticode->hl()->opcode) {
  717. case HLI_ASSIGN:
  718. forwardSubs (picode->hl()->expr(), exp,
  719. picode, ticode, &localId,
  720. numHlIcodes);
  721. break;
  722. case HLI_JCOND: case HLI_PUSH: case HLI_RET:
  723. res = insertSubTreeReg (exp,
  724. &ticode->hl()->exp.v,
  725. localId.id_arr[picode->hl()->expr()->expr.ident.idNode.regiIdx].id.regi,
  726. &localId);
  727. if (res)
  728. {
  729. picode->invalidate();
  730. numHlIcodes--;
  731. }
  732. break;
  733. /****case HLI_CALL: /* register arguments
  734. newRegArg (pProc, picode, ticode);
  735. picode->invalidate();
  736. numHlIcodes--;
  737. break; */
  738. } /* eos */
  739. break;
  740. case HLI_CALL:
  741. ticode = picode->du1.idx[0].uses.front();
  742. switch (ticode->hl()->opcode) {
  743. case HLI_ASSIGN:
  744. exp = COND_EXPR::idFunc (
  745. picode->hl()->call.proc,
  746. picode->hl()->call.args);
  747. res = insertSubTreeReg (exp,
  748. &ticode->hl()->asgn.rhs,
  749. picode->hl()->call.proc->retVal.id.regi,
  750. &localId);
  751. if (! res)
  752. insertSubTreeReg (exp,
  753. &ticode->hl()->asgn.lhs,
  754. picode->hl()->call.proc->retVal.id.regi,
  755. &localId);
  756. /*** TODO: HERE missing: 2 regs ****/
  757. picode->invalidate();
  758. numHlIcodes--;
  759. break;
  760. case HLI_PUSH: case HLI_RET:
  761. ticode->hl()->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) );
  762. picode->invalidate();
  763. numHlIcodes--;
  764. break;
  765. case HLI_JCOND:
  766. exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
  767. retVal = &picode->hl()->call.proc->retVal,
  768. res = insertSubTreeReg (exp,
  769. &ticode->hl()->exp.v,
  770. retVal->id.regi, &localId);
  771. if (res) /* was substituted */
  772. {
  773. picode->invalidate();
  774. numHlIcodes--;
  775. }
  776. else /* cannot substitute function */
  777. {
  778. //picode->loc_ip
  779. lhs = COND_EXPR::idID(retVal,&localId,picode);
  780. picode->setAsgn(lhs, exp);
  781. }
  782. break;
  783. } /* eos */
  784. break;
  785. } /* eos */
  786. }
  787. }
  788. else if (picode->du1.numRegsDef == 2) /* long regs */
  789. {
  790. /* Check for only one use of these registers */
  791. if ((picode->du1.numUses(0) == 1) and (picode->du1.numUses(1) == 1))
  792. {
  793. switch (picode->hl()->opcode) {
  794. case HLI_ASSIGN:
  795. /* Replace rhs of current icode into target
  796. * icode expression */
  797. if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0])
  798. {
  799. ticode = picode->du1.idx[0].uses.front();
  800. if ((picode->du.lastDefRegi & duReg[regi]).any() &&
  801. ((ticode->hl()->opcode != HLI_CALL) &&
  802. (ticode->hl()->opcode != HLI_RET)))
  803. continue;
  804. switch (ticode->hl()->opcode) {
  805. case HLI_ASSIGN:
  806. forwardSubsLong (picode->hl()->asgn.lhs->expr.ident.idNode.longIdx,
  807. picode->hl()->asgn.rhs, picode,ticode,
  808. &numHlIcodes);
  809. break;
  810. case HLI_JCOND: case HLI_PUSH: case HLI_RET:
  811. res = insertSubTreeLongReg (
  812. picode->hl()->asgn.rhs,
  813. &ticode->hl()->exp.v,
  814. picode->hl()->asgn.lhs->expr.ident.idNode.longIdx);
  815. if (res)
  816. {
  817. picode->invalidate();
  818. numHlIcodes--;
  819. }
  820. break;
  821. case HLI_CALL: /* register arguments */
  822. newRegArg ( picode, ticode);
  823. picode->invalidate();
  824. numHlIcodes--;
  825. break;
  826. } /* eos */
  827. }
  828. break;
  829. case HLI_POP:
  830. if (picode->du1.idx[0].uses[0] == picode->du1.idx[1].uses[0])
  831. {
  832. ticode = picode->du1.idx[0].uses.front();
  833. if ((picode->du.lastDefRegi & duReg[regi]).any() &&
  834. ((ticode->hl()->opcode != HLI_CALL) &&
  835. (ticode->hl()->opcode != HLI_RET)))
  836. continue;
  837. exp = g_exp_stk.pop(); /* pop last exp pushed */
  838. switch (ticode->hl()->opcode) {
  839. case HLI_ASSIGN:
  840. forwardSubsLong (picode->hl()->expr()->expr.ident.idNode.longIdx,
  841. exp, picode, ticode, &numHlIcodes);
  842. break;
  843. case HLI_JCOND: case HLI_PUSH:
  844. res = insertSubTreeLongReg (exp,
  845. &ticode->hl()->exp.v,
  846. picode->hl()->asgn.lhs->expr.ident.idNode.longIdx);
  847. if (res)
  848. {
  849. picode->invalidate();
  850. numHlIcodes--;
  851. }
  852. break;
  853. case HLI_CALL: /*** missing ***/
  854. break;
  855. } /* eos */
  856. }
  857. break;
  858. case HLI_CALL: /* check for function return */
  859. ticode = picode->du1.idx[0].uses.front();
  860. switch (ticode->hl()->opcode)
  861. {
  862. case HLI_ASSIGN:
  863. exp = COND_EXPR::idFunc (
  864. picode->hl()->call.proc,
  865. picode->hl()->call.args);
  866. ticode->hl()->asgn.lhs =
  867. COND_EXPR::idLong(&localId, DST, ticode,HIGH_FIRST, picode, eDEF, 1);
  868. ticode->hl()->asgn.rhs = exp;
  869. picode->invalidate();
  870. numHlIcodes--;
  871. break;
  872. case HLI_PUSH: case HLI_RET:
  873. ticode->hl()->expr( COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args) );
  874. picode->invalidate();
  875. numHlIcodes--;
  876. break;
  877. case HLI_JCOND:
  878. exp = COND_EXPR::idFunc ( picode->hl()->call.proc, picode->hl()->call.args);
  879. retVal = &picode->hl()->call.proc->retVal;
  880. res = insertSubTreeLongReg (exp,
  881. &ticode->hl()->exp.v,
  882. localId.newLongReg ( retVal->type, retVal->id.longId.h,
  883. retVal->id.longId.l, picode));
  884. if (res) /* was substituted */
  885. {
  886. picode->invalidate();
  887. numHlIcodes--;
  888. }
  889. else /* cannot substitute function */
  890. {
  891. lhs = COND_EXPR::idID(retVal,&localId,picode/*picode->loc_ip*/);
  892. picode->setAsgn(lhs, exp);
  893. }
  894. break;
  895. } /* eos */
  896. } /* eos */
  897. }
  898. }
  899. /* HLI_PUSH doesn't define any registers, only uses registers.
  900. * Push the associated expression to the register on the local
  901. * expression stack */
  902. else if (picode->hl()->opcode == HLI_PUSH)
  903. {
  904. g_exp_stk.push(picode->hl()->expr());
  905. picode->invalidate();
  906. numHlIcodes--;
  907. }
  908. /* For HLI_CALL instructions that use arguments from the stack,
  909. * pop them from the expression stack and place them on the
  910. * procedure's argument list */
  911. if ((picode->hl()->opcode == HLI_CALL) &&
  912. ! (picode->hl()->call.proc->flg & REG_ARGS))
  913. { Function * pp;
  914. int cb, numArgs;
  915. boolT res;
  916. pp = picode->hl()->call.proc;
  917. if (pp->flg & CALL_PASCAL)
  918. {
  919. cb = pp->cbParam; /* fixed # arguments */
  920. for (k = 0, numArgs = 0; k < cb; numArgs++)
  921. {
  922. exp = g_exp_stk.pop();
  923. if (pp->flg & PROC_ISLIB) /* library function */
  924. {
  925. if (pp->args.numArgs > 0)
  926. adjustActArgType(exp, pp->args.sym[numArgs].type, this);
  927. res = picode->newStkArg (exp, picode->ll()->opcode, this);
  928. }
  929. else /* user function */
  930. {
  931. if (pp->args.numArgs >0)
  932. pp->args.adjustForArgType (numArgs,expType (exp, this));
  933. res = picode->newStkArg (exp,picode->ll()->opcode, this);
  934. }
  935. if (res == FALSE)
  936. k += hlTypeSize (exp, this);
  937. }
  938. }
  939. else /* CALL_C */
  940. {
  941. cb = picode->hl()->call.args->cb;
  942. numArgs = 0;
  943. if (cb)
  944. for (k = 0; k < cb; numArgs++)
  945. processCArg (pp, this, &(*picode), numArgs, &k);
  946. else if ((cb == 0) && picode->ll()->isLlFlag(REST_STK))
  947. while (! g_exp_stk.empty())
  948. {
  949. processCArg (pp, this, &(*picode), numArgs, &k);
  950. numArgs++;
  951. }
  952. }
  953. }
  954. /* If we could not substitute the result of a function,
  955. * assign it to the corresponding registers */
  956. if ((picode->hl()->opcode == HLI_CALL) &&
  957. ((picode->hl()->call.proc->flg & PROC_ISLIB) !=
  958. PROC_ISLIB) && (not picode->du1.used(0)) &&
  959. (picode->du1.numRegsDef > 0))
  960. {
  961. exp = COND_EXPR::idFunc (picode->hl()->call.proc, picode->hl()->call.args);
  962. lhs = COND_EXPR::idID (&picode->hl()->call.proc->retVal, &localId, picode);
  963. picode->setAsgn(lhs, exp);
  964. }
  965. }
  966. }
  967. /* Store number of high-level icodes in current basic block */
  968. pbb->numHlIcodes = numHlIcodes;
  969. }
  970. }
  971. /** Invokes procedures related with data flow analysis. Works on a procedure
  972. * at a time basis.
  973. * Note: indirect recursion in liveRegAnalysis is possible. */
  974. void Function::dataFlow(std::bitset<32> &liveOut)
  975. {
  976. boolT isAx, isBx, isCx, isDx;
  977. int idx;
  978. /* Remove references to register variables */
  979. if (flg & SI_REGVAR)
  980. liveOut &= maskDuReg[rSI];
  981. if (flg & DI_REGVAR)
  982. liveOut &= maskDuReg[rDI];
  983. /* Function - return value register(s) */
  984. if (liveOut != 0)
  985. {
  986. flg |= PROC_IS_FUNC;
  987. isAx = liveOut.test(rAX - rAX);
  988. isBx = liveOut.test(rBX - rAX);
  989. isCx = liveOut.test(rCX - rAX);
  990. isDx = liveOut.test(rDX - rAX);
  991. if (isAx && isDx) /* long or pointer */
  992. {
  993. retVal.type = TYPE_LONG_SIGN;
  994. retVal.loc = REG_FRAME;
  995. retVal.id.longId.h = rDX;
  996. retVal.id.longId.l = rAX;
  997. idx = localId.newLongReg(TYPE_LONG_SIGN, rDX, rAX, Icode.begin()/*0*/);
  998. localId.propLongId (rAX, rDX, "\0");
  999. }
  1000. else if (isAx || isBx || isCx || isDx) /* uint16_t */
  1001. {
  1002. retVal.type = TYPE_WORD_SIGN;
  1003. retVal.loc = REG_FRAME;
  1004. if (isAx)
  1005. retVal.id.regi = rAX;
  1006. else if (isBx)
  1007. retVal.id.regi = rBX;
  1008. else if (isCx)
  1009. retVal.id.regi = rCX;
  1010. else
  1011. retVal.id.regi = rDX;
  1012. idx = localId.newByteWordReg(TYPE_WORD_SIGN,retVal.id.regi);
  1013. }
  1014. }
  1015. /* Data flow analysis */
  1016. liveAnal = TRUE;
  1017. elimCondCodes();
  1018. genLiveKtes();
  1019. liveRegAnalysis (liveOut); /* calls dataFlow() recursively */
  1020. if (! (flg & PROC_ASM)) /* can generate C for pProc */
  1021. {
  1022. genDU1 (); /* generate def/use level 1 chain */
  1023. findExps (); /* forward substitution algorithm */
  1024. }
  1025. }