procs.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. bool 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. bool 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 * call_args_stackframe, *target_stackframe;
  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. call_args_stackframe = ticode->hl()->call.args;
  93. target_stackframe = &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(STKSYM &tgt_sym : *target_stackframe)
  114. {
  115. if (type == REGISTER)
  116. {
  117. if ((tgt_sym.regs != NULL) &&
  118. (tgt_sym.regs->expr.ident.idNode.regiIdx == tidx))
  119. {
  120. regExist = true;
  121. }
  122. }
  123. else if (type == LONG_VAR)
  124. {
  125. if ((tgt_sym.regs != NULL) &&
  126. (tgt_sym.regs->expr.ident.idNode.longIdx == tidx))
  127. {
  128. regExist = true;
  129. }
  130. }
  131. if(regExist == true)
  132. break;
  133. }
  134. /* Do ts (formal arguments) */
  135. if (regExist == false)
  136. {
  137. STKSYM newsym;
  138. newsym.setArgName(target_stackframe->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. tproc->localId.id_arr[tidx].name = newsym.name;
  152. }
  153. else if (type == LONG_VAR)
  154. {
  155. newsym.regs = COND_EXPR::idLongIdx (tidx);
  156. newsym.type = TYPE_LONG_SIGN;
  157. tproc->localId.id_arr[tidx].name = newsym.name;
  158. tproc->localId.propLongId (regL, regH, tproc->localId.id_arr[tidx].name.c_str());
  159. }
  160. target_stackframe->push_back(newsym);
  161. target_stackframe->numArgs++;
  162. }
  163. /* Do ps (actual arguments) */
  164. STKSYM newsym;
  165. newsym.setArgName(call_args_stackframe->size());
  166. newsym.actual = picode->hl()->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. call_args_stackframe->push_back(newsym);
  186. call_args_stackframe->numArgs++;
  187. }
  188. /** Inserts the new expression (ie. the actual parameter) on the argument
  189. * list.
  190. * @return true if it was a near call that made use of a segment register.
  191. * false elsewhere
  192. */
  193. bool CallType::newStkArg(COND_EXPR *exp, llIcode opcode, Function * pproc)
  194. {
  195. uint8_t regi;
  196. /* Check for far procedure call, in which case, references to segment
  197. * registers are not be considered another parameter (i.e. they are
  198. * long references to another segment) */
  199. if (exp)
  200. {
  201. if ((exp->type == IDENTIFIER) && (exp->expr.ident.idType == REGISTER))
  202. {
  203. regi = pproc->localId.id_arr[exp->expr.ident.idNode.regiIdx].id.regi;
  204. if ((regi >= rES) && (regi <= rDS))
  205. if (opcode == iCALLF)
  206. return false;
  207. else
  208. return true;
  209. }
  210. }
  211. /* Place register argument on the argument list */
  212. STKSYM newsym;
  213. newsym.actual = exp;
  214. args->push_back(newsym);
  215. args->numArgs++;
  216. return false;
  217. }
  218. /* Places the actual argument exp in the position given by pos in the
  219. * argument list of picode. */
  220. void CallType::placeStkArg (COND_EXPR *exp, int pos)
  221. {
  222. (*args)[pos].actual = exp;
  223. (*args)[pos].setArgName(pos);
  224. }
  225. /* Checks to determine whether the expression (actual argument) has the
  226. * same type as the given type (from the procedure's formal list). If not,
  227. * the actual argument gets modified */
  228. void adjustActArgType (COND_EXPR *exp, hlType forType, Function * pproc)
  229. {
  230. hlType actType;
  231. int offset, offL;
  232. if (exp == NULL)
  233. return;
  234. actType = expType (exp, pproc);
  235. if (((actType == forType) || (exp->type != IDENTIFIER)))
  236. return;
  237. switch (forType)
  238. {
  239. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  240. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  241. case TYPE_WORD_UNSIGN: case TYPE_LONG_SIGN:
  242. case TYPE_LONG_UNSIGN: case TYPE_RECORD:
  243. break;
  244. case TYPE_PTR:
  245. case TYPE_CONST:
  246. break;
  247. case TYPE_STR:
  248. switch (actType) {
  249. case TYPE_CONST:
  250. /* It's an offset into image where a string is
  251. * found. Point to the string. */
  252. offL = exp->expr.ident.idNode.kte.kte;
  253. if (prog.fCOM)
  254. offset = (pproc->state.r[rDS]<<4) + offL + 0x100;
  255. else
  256. offset = (pproc->state.r[rDS]<<4) + offL;
  257. exp->expr.ident.idNode.strIdx = offset;
  258. exp->expr.ident.idType = STRING;
  259. break;
  260. case TYPE_PTR:
  261. /* It's a pointer to a char rather than a pointer to
  262. * an integer */
  263. /***HERE - modify the type ****/
  264. break;
  265. case TYPE_WORD_SIGN:
  266. break;
  267. } /* eos */
  268. break;
  269. }
  270. }
  271. /* Determines whether the formal argument has the same type as the given
  272. * type (type of the actual argument). If not, the formal argument is
  273. * changed its type */
  274. void STKFRAME::adjustForArgType(int numArg_, hlType actType_)
  275. {
  276. hlType forType;
  277. STKSYM * psym, * nsym;
  278. int off, i;
  279. /* If formal argument does not exist, do not create new ones, just
  280. * ignore actual argument
  281. */
  282. if(numArg_>size())
  283. return;
  284. /* Find stack offset for this argument */
  285. off = m_minOff;
  286. i=0;
  287. for(STKSYM &s : *this) // walk formal arguments upto numArg_
  288. {
  289. if(i>=numArg_)
  290. break;
  291. off+=s.size;
  292. i++;
  293. }
  294. /* Find formal argument */
  295. //psym = &at(numArg_);
  296. //i = numArg_;
  297. //auto iter=std::find_if(sym.begin(),sym.end(),[off](STKSYM &s)->bool {s.off==off;});
  298. auto iter=std::find_if(begin()+numArg_,end(),[off](STKSYM &s)->bool {s.label==off;});
  299. if(iter==end()) // symbol not found
  300. return;
  301. psym = &(*iter);
  302. forType = psym->type;
  303. if (forType != actType_)
  304. {
  305. switch (actType_) {
  306. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  307. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  308. case TYPE_WORD_UNSIGN: case TYPE_RECORD:
  309. break;
  310. case TYPE_LONG_UNSIGN: case TYPE_LONG_SIGN:
  311. if ((forType == TYPE_WORD_UNSIGN) ||
  312. (forType == TYPE_WORD_SIGN) ||
  313. (forType == TYPE_UNKNOWN))
  314. {
  315. /* Merge low and high */
  316. psym->type = actType_;
  317. psym->size = 4;
  318. nsym = psym + 1;
  319. nsym->macro = "HI";
  320. psym->macro = "LO";
  321. nsym->hasMacro = true;
  322. psym->hasMacro = true;
  323. nsym->name = psym->name;
  324. nsym->invalid = true;
  325. numArgs--;
  326. }
  327. break;
  328. case TYPE_PTR:
  329. case TYPE_CONST:
  330. case TYPE_STR:
  331. break;
  332. } /* eos */
  333. }
  334. }