procs.cpp 12 KB

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