procs.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. #define indSize 61 /* size of indentation buffer; max 20 */
  12. static char indentBuf[indSize] =
  13. " ";
  14. static char *indent (Int indLevel) // Indentation according to the depth of the statement
  15. {
  16. return (&indentBuf[indSize-(indLevel*3)-1]);
  17. }
  18. /* Inserts an outEdge at the current callGraph pointer if the newProc does
  19. * not exist. */
  20. void CALL_GRAPH::insertArc (ilFunction newProc)
  21. {
  22. CALL_GRAPH *pcg;
  23. Int i;
  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", indent(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(ICODE *picode, ICODE *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. uint8_t regL, regH; /* Registers involved in arguments */
  88. /* Flag ticode as having register arguments */
  89. tproc = ticode->ic.hl.oper.call.proc;
  90. tproc->flg |= REG_ARGS;
  91. /* Get registers and index into target procedure's local list */
  92. ps = ticode->ic.hl.oper.call.args;
  93. ts = &tproc->args;
  94. lhs = picode->ic.hl.oper.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, Icode.begin() /*0*/);
  109. }
  110. /* Check if register argument already on the formal argument list */
  111. regExist = FALSE;
  112. for (i = 0; i < ts->sym.size(); i++)
  113. {
  114. if (type == REGISTER)
  115. {
  116. if ((ts->sym[i].regs != NULL) &&
  117. (ts->sym[i].regs->expr.ident.idNode.regiIdx == tidx))
  118. {
  119. regExist = TRUE;
  120. i = ts->sym.size();
  121. }
  122. }
  123. else if (type == LONG_VAR)
  124. {
  125. if ((ts->sym[i].regs != NULL) &&
  126. (ts->sym[i].regs->expr.ident.idNode.longIdx == tidx))
  127. {
  128. regExist = TRUE;
  129. i = ts->sym.size();
  130. }
  131. }
  132. }
  133. /* Do ts (formal arguments) */
  134. if (regExist == FALSE)
  135. {
  136. STKSYM newsym;
  137. sprintf (newsym.name, "arg%ld", ts->sym.size());
  138. if (type == REGISTER)
  139. {
  140. if (regL < rAL)
  141. {
  142. newsym.type = TYPE_WORD_SIGN;
  143. newsym.regs = COND_EXPR::idRegIdx(tidx, WORD_REG);
  144. }
  145. else
  146. {
  147. newsym.type = TYPE_BYTE_SIGN;
  148. newsym.regs = COND_EXPR::idRegIdx(tidx, BYTE_REG);
  149. }
  150. sprintf (tproc->localId.id_arr[tidx].name, "arg%ld", ts->sym.size());
  151. }
  152. else if (type == LONG_VAR)
  153. {
  154. newsym.regs = COND_EXPR::idLongIdx (tidx);
  155. newsym.type = TYPE_LONG_SIGN;
  156. sprintf (tproc->localId.id_arr[tidx].name, "arg%ld", ts->sym.size());
  157. tproc->localId.propLongId (regL, regH,
  158. tproc->localId.id_arr[tidx].name);
  159. }
  160. ts->sym.push_back(newsym);
  161. ts->numArgs++;
  162. }
  163. /* Do ps (actual arguments) */
  164. STKSYM newsym;
  165. sprintf (newsym.name, "arg%ld", ps->sym.size());
  166. newsym.actual = picode->ic.hl.oper.asgn.rhs;
  167. newsym.regs = lhs;
  168. /* Mask off high and low register(s) in picode */
  169. switch (type) {
  170. case REGISTER:
  171. id = &localId.id_arr[lhs->expr.ident.idNode.regiIdx];
  172. picode->du.def &= maskDuReg[id->id.regi];
  173. if (id->id.regi < rAL)
  174. newsym.type = TYPE_WORD_SIGN;
  175. else
  176. newsym.type = TYPE_BYTE_SIGN;
  177. break;
  178. case LONG_VAR:
  179. id = &localId.id_arr[lhs->expr.ident.idNode.longIdx];
  180. picode->du.def &= maskDuReg[id->id.longId.h];
  181. picode->du.def &= maskDuReg[id->id.longId.l];
  182. newsym.type = TYPE_LONG_SIGN;
  183. break;
  184. }
  185. ps->sym.push_back(newsym);
  186. ps->numArgs++;
  187. }
  188. /* Allocates num arguments in the actual argument list of the current
  189. * icode picode. */
  190. /** NOTE: this function is not used ****/
  191. void allocStkArgs (ICODE *picode, Int num)
  192. {
  193. STKFRAME * ps;
  194. ps = picode->ic.hl.oper.call.args;
  195. ps->numArgs = num;
  196. ps->sym.resize(num);
  197. }
  198. /* Inserts the new expression (ie. the actual parameter) on the argument
  199. * list.
  200. * Returns: TRUE if it was a near call that made use of a segment register.
  201. * FALSE elsewhere */
  202. boolT newStkArg (ICODE *picode, COND_EXPR *exp, llIcode opcode, Function * pproc)
  203. {
  204. STKFRAME * ps;
  205. byte regi;
  206. /* Check for far procedure call, in which case, references to segment
  207. * registers are not be considered another parameter (i.e. they are
  208. * long references to another segment) */
  209. if (exp)
  210. {
  211. if ((exp->type == IDENTIFIER) && (exp->expr.ident.idType == REGISTER))
  212. {
  213. regi = pproc->localId.id_arr[exp->expr.ident.idNode.regiIdx].id.regi;
  214. if ((regi >= rES) && (regi <= rDS))
  215. if (opcode == iCALLF)
  216. return false;
  217. else
  218. return true;
  219. }
  220. }
  221. /* Place register argument on the argument list */
  222. ps = picode->ic.hl.oper.call.args;
  223. STKSYM newsym;
  224. newsym.actual = exp;
  225. ps->sym.push_back(newsym);
  226. ps->numArgs++;
  227. return false;
  228. }
  229. /* Places the actual argument exp in the position given by pos in the
  230. * argument list of picode. */
  231. void placeStkArg (ICODE *picode, COND_EXPR *exp, Int pos)
  232. { STKFRAME * ps;
  233. ps = picode->ic.hl.oper.call.args;
  234. ps->sym[pos].actual = exp;
  235. sprintf (ps->sym[pos].name, "arg%ld", pos);
  236. }
  237. /* Checks to determine whether the expression (actual argument) has the
  238. * same type as the given type (from the procedure's formal list). If not,
  239. * the actual argument gets modified */
  240. void adjustActArgType (COND_EXPR *exp, hlType forType, Function * pproc)
  241. {
  242. hlType actType;
  243. Int offset, offL;
  244. if (exp == NULL)
  245. return;
  246. actType = expType (exp, pproc);
  247. if (((actType == forType) || (exp->type != IDENTIFIER)))
  248. return;
  249. switch (forType)
  250. {
  251. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  252. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  253. case TYPE_WORD_UNSIGN: case TYPE_LONG_SIGN:
  254. case TYPE_LONG_UNSIGN: case TYPE_RECORD:
  255. break;
  256. case TYPE_PTR:
  257. case TYPE_CONST:
  258. break;
  259. case TYPE_STR:
  260. switch (actType) {
  261. case TYPE_CONST:
  262. /* It's an offset into image where a string is
  263. * found. Point to the string. */
  264. offL = exp->expr.ident.idNode.kte.kte;
  265. if (prog.fCOM)
  266. offset = (pproc->state.r[rDS]<<4) + offL + 0x100;
  267. else
  268. offset = (pproc->state.r[rDS]<<4) + offL;
  269. exp->expr.ident.idNode.strIdx = offset;
  270. exp->expr.ident.idType = STRING;
  271. break;
  272. case TYPE_PTR:
  273. /* It's a pointer to a char rather than a pointer to
  274. * an integer */
  275. /***HERE - modify the type ****/
  276. break;
  277. case TYPE_WORD_SIGN:
  278. break;
  279. } /* eos */
  280. break;
  281. }
  282. }
  283. /* Determines whether the formal argument has the same type as the given
  284. * type (type of the actual argument). If not, the formal argument is
  285. * changed its type */
  286. void STKFRAME::adjustForArgType(Int numArg_, hlType actType_)
  287. {
  288. hlType forType;
  289. STKSYM * psym, * nsym;
  290. Int off, i;
  291. /* Find stack offset for this argument */
  292. off = m_minOff;
  293. for (i = 0; i < numArg_; i++)
  294. off += sym[i].size;
  295. /* Find formal argument */
  296. if (numArg_ < sym.size())
  297. {
  298. psym = &sym[numArg_];
  299. i = numArg_;
  300. while ((i < sym.size()) && (psym->off != off))
  301. {
  302. psym++;
  303. i++;
  304. }
  305. if (numArg_ == sym.size())
  306. return;
  307. }
  308. /* If formal argument does not exist, do not create new ones, just
  309. * ignore actual argument */
  310. else
  311. return;
  312. forType = psym->type;
  313. if (forType != actType_)
  314. {
  315. switch (actType_) {
  316. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  317. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  318. case TYPE_WORD_UNSIGN: case TYPE_RECORD:
  319. break;
  320. case TYPE_LONG_UNSIGN: case TYPE_LONG_SIGN:
  321. if ((forType == TYPE_WORD_UNSIGN) ||
  322. (forType == TYPE_WORD_SIGN) ||
  323. (forType == TYPE_UNKNOWN))
  324. {
  325. /* Merge low and high */
  326. psym->type = actType_;
  327. psym->size = 4;
  328. nsym = psym + 1;
  329. sprintf (nsym->macro, "HI");
  330. sprintf (psym->macro, "LO");
  331. nsym->hasMacro = TRUE;
  332. psym->hasMacro = TRUE;
  333. sprintf (nsym->name, "%s", psym->name);
  334. nsym->invalid = TRUE;
  335. numArgs--;
  336. }
  337. break;
  338. case TYPE_PTR:
  339. case TYPE_CONST:
  340. case TYPE_STR:
  341. break;
  342. } /* eos */
  343. }
  344. }