procs.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * File: procs.c
  3. * Purpose: Functions to support Call graphs and procedures
  4. * Date: November 1993
  5. * (C) Cristina Cifuentes
  6. */
  7. #include <cstring>
  8. #include <cassert>
  9. #include "dcc.h"
  10. /* Static indentation buffer */
  11. static constexpr int indSize=81; /* size of indentation buffer; max 20 */
  12. static char indentBuf[indSize] =
  13. " ";
  14. // not static, used in icode.cpp at emitGotoLabel
  15. const char *indentStr(int indLevel) // Indentation according to the depth of the statement
  16. {
  17. return (&indentBuf[indSize-(indLevel*4)-1]);
  18. }
  19. /* Inserts an outEdge at the current callGraph pointer if the newProc does
  20. * not exist. */
  21. void CALL_GRAPH::insertArc (ilFunction newProc)
  22. {
  23. CALL_GRAPH *pcg;
  24. /* Check if procedure already exists */
  25. auto res=std::find_if(outEdges.begin(),outEdges.end(),[newProc](CALL_GRAPH *e) {return e->proc==newProc;});
  26. if(res!=outEdges.end())
  27. return;
  28. /* Include new arc */
  29. pcg = new CALL_GRAPH;
  30. pcg->proc = newProc;
  31. outEdges.push_back(pcg);
  32. }
  33. /* Inserts a (caller, callee) arc in the call graph tree. */
  34. boolT CALL_GRAPH::insertCallGraph(ilFunction caller, ilFunction callee)
  35. {
  36. int i;
  37. if (proc == caller)
  38. {
  39. insertArc (callee);
  40. return true;
  41. }
  42. else
  43. {
  44. for (i = 0; i < outEdges.size(); i++)
  45. if (outEdges[i]->insertCallGraph (caller, callee))
  46. return true;
  47. return (false);
  48. }
  49. }
  50. boolT CALL_GRAPH::insertCallGraph(Function *caller, ilFunction callee)
  51. {
  52. auto iter = std::find_if(pProcList.begin(),pProcList.end(),
  53. [caller](const Function &f)->bool {return caller==&f;});
  54. assert(iter!=pProcList.end());
  55. return insertCallGraph(iter,callee);
  56. }
  57. /* Displays the current node of the call graph, and invokes recursively on
  58. * the nodes the procedure invokes. */
  59. void CALL_GRAPH::writeNodeCallGraph(int indIdx)
  60. {
  61. int i;
  62. printf ("%s%s\n", indentStr(indIdx), proc->name.c_str());
  63. for (i = 0; i < outEdges.size(); i++)
  64. outEdges[i]->writeNodeCallGraph (indIdx + 1);
  65. }
  66. /* Writes the header and invokes recursive procedure */
  67. void CALL_GRAPH::write()
  68. {
  69. printf ("\nCall Graph:\n");
  70. writeNodeCallGraph (0);
  71. }
  72. /**************************************************************************
  73. * Routines to support arguments
  74. *************************************************************************/
  75. /* Updates the argument table by including the register(s) (ie. lhs of
  76. * picode) and the actual expression (ie. rhs of picode).
  77. * Note: register(s) are only included once in the table. */
  78. void Function::newRegArg(iICODE picode, iICODE ticode)
  79. {
  80. COND_EXPR *lhs;
  81. STKFRAME * ps, *ts;
  82. ID *id;
  83. int i, tidx;
  84. boolT regExist;
  85. condId type;
  86. Function * tproc;
  87. eReg regL, regH; /* Registers involved in arguments */
  88. /* Flag ticode as having register arguments */
  89. tproc = ticode->hl()->call.proc;
  90. tproc->flg |= REG_ARGS;
  91. /* Get registers and index into target procedure's local list */
  92. ps = ticode->hl()->call.args;
  93. ts = &tproc->args;
  94. lhs = picode->hl()->asgn.lhs;
  95. type = lhs->expr.ident.idType;
  96. if (type == REGISTER)
  97. {
  98. regL = localId.id_arr[lhs->expr.ident.idNode.regiIdx].id.regi;
  99. if (regL < rAL)
  100. tidx = tproc->localId.newByteWordReg(TYPE_WORD_SIGN, regL);
  101. else
  102. tidx = tproc->localId.newByteWordReg(TYPE_BYTE_SIGN, regL);
  103. }
  104. else if (type == LONG_VAR)
  105. {
  106. regL = localId.id_arr[lhs->expr.ident.idNode.longIdx].id.longId.l;
  107. regH = localId.id_arr[lhs->expr.ident.idNode.longIdx].id.longId.h;
  108. tidx = tproc->localId.newLongReg(TYPE_LONG_SIGN, regH, regL, tproc->Icode.begin() /*0*/);
  109. //tidx = tproc->localId.newLongReg(TYPE_LONG_SIGN, regH, regL, Icode.begin() /*0*/);
  110. }
  111. /* Check if register argument already on the formal argument list */
  112. regExist = FALSE;
  113. for (i = 0; i < ts->sym.size(); i++)
  114. {
  115. if (type == REGISTER)
  116. {
  117. if ((ts->sym[i].regs != NULL) &&
  118. (ts->sym[i].regs->expr.ident.idNode.regiIdx == tidx))
  119. {
  120. regExist = TRUE;
  121. i = ts->sym.size();
  122. }
  123. }
  124. else if (type == LONG_VAR)
  125. {
  126. if ((ts->sym[i].regs != NULL) &&
  127. (ts->sym[i].regs->expr.ident.idNode.longIdx == tidx))
  128. {
  129. regExist = TRUE;
  130. i = ts->sym.size();
  131. }
  132. }
  133. }
  134. /* Do ts (formal arguments) */
  135. if (regExist == FALSE)
  136. {
  137. STKSYM newsym;
  138. sprintf (newsym.name, "arg%ld", ts->sym.size());
  139. if (type == REGISTER)
  140. {
  141. if (regL < rAL)
  142. {
  143. newsym.type = TYPE_WORD_SIGN;
  144. newsym.regs = COND_EXPR::idRegIdx(tidx, WORD_REG);
  145. }
  146. else
  147. {
  148. newsym.type = TYPE_BYTE_SIGN;
  149. newsym.regs = COND_EXPR::idRegIdx(tidx, BYTE_REG);
  150. }
  151. sprintf (tproc->localId.id_arr[tidx].name, "arg%ld", ts->sym.size());
  152. }
  153. else if (type == LONG_VAR)
  154. {
  155. newsym.regs = COND_EXPR::idLongIdx (tidx);
  156. newsym.type = TYPE_LONG_SIGN;
  157. sprintf (tproc->localId.id_arr[tidx].name, "arg%ld", ts->sym.size());
  158. tproc->localId.propLongId (regL, regH,
  159. tproc->localId.id_arr[tidx].name);
  160. }
  161. ts->sym.push_back(newsym);
  162. ts->numArgs++;
  163. }
  164. /* Do ps (actual arguments) */
  165. STKSYM newsym;
  166. sprintf (newsym.name, "arg%ld", ps->sym.size());
  167. newsym.actual = picode->hl()->asgn.rhs;
  168. newsym.regs = lhs;
  169. /* Mask off high and low register(s) in picode */
  170. switch (type) {
  171. case REGISTER:
  172. id = &localId.id_arr[lhs->expr.ident.idNode.regiIdx];
  173. picode->du.def &= maskDuReg[id->id.regi];
  174. if (id->id.regi < rAL)
  175. newsym.type = TYPE_WORD_SIGN;
  176. else
  177. newsym.type = TYPE_BYTE_SIGN;
  178. break;
  179. case LONG_VAR:
  180. id = &localId.id_arr[lhs->expr.ident.idNode.longIdx];
  181. picode->du.def &= maskDuReg[id->id.longId.h];
  182. picode->du.def &= maskDuReg[id->id.longId.l];
  183. newsym.type = TYPE_LONG_SIGN;
  184. break;
  185. }
  186. ps->sym.push_back(newsym);
  187. ps->numArgs++;
  188. }
  189. /** Inserts the new expression (ie. the actual parameter) on the argument
  190. * list.
  191. * @return TRUE if it was a near call that made use of a segment register.
  192. * FALSE elsewhere
  193. */
  194. bool CallType::newStkArg(COND_EXPR *exp, llIcode opcode, Function * pproc)
  195. {
  196. uint8_t regi;
  197. /* Check for far procedure call, in which case, references to segment
  198. * registers are not be considered another parameter (i.e. they are
  199. * long references to another segment) */
  200. if (exp)
  201. {
  202. if ((exp->type == IDENTIFIER) && (exp->expr.ident.idType == REGISTER))
  203. {
  204. regi = pproc->localId.id_arr[exp->expr.ident.idNode.regiIdx].id.regi;
  205. if ((regi >= rES) && (regi <= rDS))
  206. if (opcode == iCALLF)
  207. return false;
  208. else
  209. return true;
  210. }
  211. }
  212. /* Place register argument on the argument list */
  213. STKSYM newsym;
  214. newsym.actual = exp;
  215. args->sym.push_back(newsym);
  216. args->numArgs++;
  217. return false;
  218. }
  219. /* Places the actual argument exp in the position given by pos in the
  220. * argument list of picode. */
  221. void CallType::placeStkArg (COND_EXPR *exp, int pos)
  222. {
  223. args->sym[pos].actual = exp;
  224. sprintf (args->sym[pos].name, "arg%ld", pos);
  225. }
  226. /* Checks to determine whether the expression (actual argument) has the
  227. * same type as the given type (from the procedure's formal list). If not,
  228. * the actual argument gets modified */
  229. void adjustActArgType (COND_EXPR *exp, hlType forType, Function * pproc)
  230. {
  231. hlType actType;
  232. int offset, offL;
  233. if (exp == NULL)
  234. return;
  235. actType = expType (exp, pproc);
  236. if (((actType == forType) || (exp->type != IDENTIFIER)))
  237. return;
  238. switch (forType)
  239. {
  240. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  241. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  242. case TYPE_WORD_UNSIGN: case TYPE_LONG_SIGN:
  243. case TYPE_LONG_UNSIGN: case TYPE_RECORD:
  244. break;
  245. case TYPE_PTR:
  246. case TYPE_CONST:
  247. break;
  248. case TYPE_STR:
  249. switch (actType) {
  250. case TYPE_CONST:
  251. /* It's an offset into image where a string is
  252. * found. Point to the string. */
  253. offL = exp->expr.ident.idNode.kte.kte;
  254. if (prog.fCOM)
  255. offset = (pproc->state.r[rDS]<<4) + offL + 0x100;
  256. else
  257. offset = (pproc->state.r[rDS]<<4) + offL;
  258. exp->expr.ident.idNode.strIdx = offset;
  259. exp->expr.ident.idType = STRING;
  260. break;
  261. case TYPE_PTR:
  262. /* It's a pointer to a char rather than a pointer to
  263. * an integer */
  264. /***HERE - modify the type ****/
  265. break;
  266. case TYPE_WORD_SIGN:
  267. break;
  268. } /* eos */
  269. break;
  270. }
  271. }
  272. /* Determines whether the formal argument has the same type as the given
  273. * type (type of the actual argument). If not, the formal argument is
  274. * changed its type */
  275. void STKFRAME::adjustForArgType(int numArg_, hlType actType_)
  276. {
  277. hlType forType;
  278. STKSYM * psym, * nsym;
  279. int off, i;
  280. /* Find stack offset for this argument */
  281. off = m_minOff;
  282. for (i = 0; i < numArg_; i++)
  283. {
  284. if(i>=sym.size())
  285. {
  286. break; //TODO: verify
  287. }
  288. off += sym[i].size;
  289. }
  290. /* Find formal argument */
  291. if (numArg_ < sym.size())
  292. {
  293. psym = &sym[numArg_];
  294. i = numArg_;
  295. //auto iter=std::find_if(sym.begin(),sym.end(),[off](STKSYM &s)->bool {s.off==off;});
  296. auto iter=std::find_if(sym.begin()+numArg_,sym.end(),[off](STKSYM &s)->bool {s.off==off;});
  297. if(iter==sym.end()) // symbol not found
  298. return;
  299. psym = &(*iter);
  300. }
  301. /* If formal argument does not exist, do not create new ones, just
  302. * ignore actual argument
  303. */
  304. else
  305. return;
  306. forType = psym->type;
  307. if (forType != actType_)
  308. {
  309. switch (actType_) {
  310. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  311. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  312. case TYPE_WORD_UNSIGN: case TYPE_RECORD:
  313. break;
  314. case TYPE_LONG_UNSIGN: case TYPE_LONG_SIGN:
  315. if ((forType == TYPE_WORD_UNSIGN) ||
  316. (forType == TYPE_WORD_SIGN) ||
  317. (forType == TYPE_UNKNOWN))
  318. {
  319. /* Merge low and high */
  320. psym->type = actType_;
  321. psym->size = 4;
  322. nsym = psym + 1;
  323. sprintf (nsym->macro, "HI");
  324. sprintf (psym->macro, "LO");
  325. nsym->hasMacro = TRUE;
  326. psym->hasMacro = TRUE;
  327. sprintf (nsym->name, "%s", psym->name);
  328. nsym->invalid = TRUE;
  329. numArgs--;
  330. }
  331. break;
  332. case TYPE_PTR:
  333. case TYPE_CONST:
  334. case TYPE_STR:
  335. break;
  336. } /* eos */
  337. }
  338. }