procs.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. AstIdent *lhs;
  78. STKFRAME * call_args_stackframe, *target_stackframe;
  79. const ID *id;
  80. int tidx;
  81. bool regExist=false;
  82. condId type;
  83. Function * tproc;
  84. eReg regL = rUNDEF;
  85. eReg regH; /* Registers involved in arguments */
  86. /* Flag ticode as having register arguments */
  87. tproc = ticode->hl()->call.proc;
  88. tproc->flg |= REG_ARGS;
  89. /* Get registers and index into target procedure's local list */
  90. call_args_stackframe = ticode->hl()->call.args;
  91. target_stackframe = &tproc->args;
  92. lhs = dynamic_cast<AstIdent *>(picode->hl()->asgn.lhs());
  93. RegisterNode *lhs_reg = dynamic_cast<RegisterNode *>(lhs);
  94. assert(lhs);
  95. type = lhs->ident.type();
  96. if (lhs_reg)
  97. {
  98. regL = id_arr[lhs_reg->regiIdx].id.regi;
  99. assert(regL!=rUNDEF);
  100. if (regL < rAL)
  101. tidx = tproc->localId.newByteWordReg(TYPE_WORD_SIGN, regL);
  102. else
  103. tidx = tproc->localId.newByteWordReg(TYPE_BYTE_SIGN, regL);
  104. /* Check if register argument already on the formal argument list */
  105. for(STKSYM &tgt_sym : *target_stackframe)
  106. {
  107. RegisterNode *tgt_sym_regs = dynamic_cast<RegisterNode *>(tgt_sym.regs);
  108. if( tgt_sym_regs == NULL ) // both REGISTER and LONG_VAR require this precondition
  109. continue;
  110. if ( tgt_sym_regs->regiIdx == tidx )
  111. {
  112. regExist = true;
  113. break;
  114. }
  115. }
  116. }
  117. else if (type == LONG_VAR)
  118. {
  119. int longIdx = lhs->ident.idNode.longIdx;
  120. LONGID_TYPE regHL = id_arr[longIdx].longId();
  121. regH=regHL.h();
  122. regL=regHL.l();
  123. tidx = tproc->localId.newLongReg(TYPE_LONG_SIGN, regHL, tproc->Icode.begin() /*0*/);
  124. /* Check if register argument already on the formal argument list */
  125. for(STKSYM &tgt_sym : *target_stackframe)
  126. {
  127. if( tgt_sym.regs == NULL ) // both REGISTER and LONG_VAR require this precondition
  128. continue;
  129. if ( tgt_sym.regs->ident.idNode.longIdx == tidx )
  130. {
  131. regExist = true;
  132. break;
  133. }
  134. }
  135. }
  136. else
  137. ;//regExist = false;
  138. /* Do ts (formal arguments) */
  139. if (regExist == false)
  140. {
  141. STKSYM newsym;
  142. newsym.setArgName(target_stackframe->size());
  143. if (type == REGISTER)
  144. {
  145. if (regL < rAL)
  146. {
  147. newsym.type = TYPE_WORD_SIGN;
  148. newsym.regs = new RegisterNode(tidx, WORD_REG);
  149. }
  150. else
  151. {
  152. newsym.type = TYPE_BYTE_SIGN;
  153. newsym.regs = new RegisterNode(tidx, BYTE_REG);
  154. }
  155. tproc->localId.id_arr[tidx].name = newsym.name;
  156. }
  157. else if (type == LONG_VAR)
  158. {
  159. newsym.regs = AstIdent::LongIdx (tidx);
  160. newsym.type = TYPE_LONG_SIGN;
  161. assert(regL!=rUNDEF);
  162. tproc->localId.id_arr[tidx].name = newsym.name;
  163. tproc->localId.propLongId (regL, regH, tproc->localId.id_arr[tidx].name.c_str());
  164. }
  165. target_stackframe->push_back(newsym);
  166. target_stackframe->numArgs++;
  167. }
  168. /* Do ps (actual arguments) */
  169. STKSYM newsym;
  170. newsym.setArgName(call_args_stackframe->size());
  171. newsym.actual = picode->hl()->asgn.rhs;
  172. newsym.regs = lhs;
  173. /* Mask off high and low register(s) in picode */
  174. switch (type) {
  175. case REGISTER:
  176. id = &id_arr[lhs_reg->regiIdx];
  177. picode->du.def.clrReg(id->id.regi);
  178. if (id->id.regi < rAL)
  179. newsym.type = TYPE_WORD_SIGN;
  180. else
  181. newsym.type = TYPE_BYTE_SIGN;
  182. break;
  183. case LONG_VAR:
  184. id = &id_arr[lhs->ident.idNode.longIdx];
  185. picode->du.def.clrReg(id->longId().h());
  186. picode->du.def.clrReg(id->longId().l());
  187. newsym.type = TYPE_LONG_SIGN;
  188. break;
  189. default:
  190. fprintf(stderr,"LOCAL_ID::newRegArg unhandled type %d in masking low\n",type);
  191. }
  192. call_args_stackframe->push_back(newsym);
  193. call_args_stackframe->numArgs++;
  194. }
  195. /** Inserts the new expression (ie. the actual parameter) on the argument
  196. * list.
  197. * @return true if it was a near call that made use of a segment register.
  198. * false elsewhere
  199. */
  200. bool CallType::newStkArg(Expr *exp, llIcode opcode, Function * pproc)
  201. {
  202. RegisterNode *expr = dynamic_cast<RegisterNode *>(exp);
  203. uint8_t regi;
  204. /* Check for far procedure call, in which case, references to segment
  205. * registers are not be considered another parameter (i.e. they are
  206. * long references to another segment) */
  207. if (expr)
  208. {
  209. regi = pproc->localId.id_arr[expr->regiIdx].id.regi;
  210. if ((regi >= rES) && (regi <= rDS))
  211. {
  212. if (opcode == iCALLF)
  213. return false;
  214. else
  215. return true;
  216. }
  217. }
  218. /* Place register argument on the argument list */
  219. STKSYM newsym;
  220. newsym.actual = exp;
  221. args->push_back(newsym);
  222. args->numArgs++;
  223. return false;
  224. }
  225. /* Places the actual argument exp in the position given by pos in the
  226. * argument list of picode. */
  227. void CallType::placeStkArg (Expr *exp, int pos)
  228. {
  229. (*args)[pos].actual = exp;
  230. (*args)[pos].setArgName(pos);
  231. }
  232. Expr *CallType::toAst()
  233. {
  234. return new FuncNode( proc, args);
  235. }
  236. /* Checks to determine whether the expression (actual argument) has the
  237. * same type as the given type (from the procedure's formal list). If not,
  238. * the actual argument gets modified */
  239. Expr *Function::adjustActArgType (Expr *_exp, hlType forType)
  240. {
  241. AstIdent *expr = dynamic_cast<AstIdent *>(_exp);
  242. PROG &prog(Project::get()->prog);
  243. hlType actType;
  244. int offset, offL;
  245. if (expr == NULL)
  246. return _exp;
  247. actType = expr-> expType (this);
  248. if (actType == forType)
  249. return _exp;
  250. switch (forType)
  251. {
  252. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  253. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  254. case TYPE_WORD_UNSIGN: case TYPE_LONG_SIGN:
  255. case TYPE_LONG_UNSIGN: case TYPE_RECORD:
  256. break;
  257. case TYPE_PTR:
  258. case TYPE_CONST:
  259. break;
  260. case TYPE_STR:
  261. switch (actType) {
  262. case TYPE_CONST:
  263. /* It's an offset into image where a string is found. Point to the string. */
  264. {
  265. Constant *c=dynamic_cast<Constant *>(expr);
  266. assert(c);
  267. offL = c->kte.kte;
  268. if (prog.fCOM)
  269. offset = (state.r[rDS]<<4) + offL + 0x100;
  270. else
  271. offset = (state.r[rDS]<<4) + offL;
  272. expr->ident.idNode.strIdx = offset;
  273. expr->ident.type(STRING);
  274. delete c;
  275. return AstIdent::String(offset);
  276. }
  277. case TYPE_PTR:
  278. /* It's a pointer to a char rather than a pointer to
  279. * an integer */
  280. /***HERE - modify the type ****/
  281. break;
  282. case TYPE_WORD_SIGN:
  283. break;
  284. default:
  285. fprintf(stderr,"adjustForArgType unhandled actType_ %d \n",actType);
  286. } /* eos */
  287. break;
  288. default:
  289. fprintf(stderr,"adjustForArgType unhandled forType %d \n",forType);
  290. }
  291. return _exp;
  292. }
  293. /* Determines whether the formal argument has the same type as the given
  294. * type (type of the actual argument). If not, the formal argument is
  295. * changed its type */
  296. void STKFRAME::adjustForArgType(size_t numArg_, hlType actType_)
  297. {
  298. hlType forType;
  299. STKSYM * psym, * nsym;
  300. int off;
  301. /* If formal argument does not exist, do not create new ones, just
  302. * ignore actual argument
  303. */
  304. if(numArg_>size())
  305. return;
  306. /* Find stack offset for this argument */
  307. off = m_minOff;
  308. size_t i=0;
  309. for(STKSYM &s : *this) // walk formal arguments upto numArg_
  310. {
  311. if(i>=numArg_)
  312. break;
  313. off+=s.size;
  314. i++;
  315. }
  316. /* Find formal argument */
  317. //psym = &at(numArg_);
  318. //i = numArg_;
  319. //auto iter=std::find_if(sym.begin(),sym.end(),[off](STKSYM &s)->bool {s.off==off;});
  320. auto iter=std::find_if(begin()+numArg_,end(),[off](STKSYM &s)->bool {return s.label==off;});
  321. if(iter==end()) // symbol not found
  322. return;
  323. psym = &(*iter);
  324. forType = psym->type;
  325. if (forType != actType_)
  326. {
  327. switch (actType_) {
  328. case TYPE_UNKNOWN: case TYPE_BYTE_SIGN:
  329. case TYPE_BYTE_UNSIGN: case TYPE_WORD_SIGN:
  330. case TYPE_WORD_UNSIGN: case TYPE_RECORD:
  331. break;
  332. case TYPE_LONG_UNSIGN: case TYPE_LONG_SIGN:
  333. if ((forType == TYPE_WORD_UNSIGN) ||
  334. (forType == TYPE_WORD_SIGN) ||
  335. (forType == TYPE_UNKNOWN))
  336. {
  337. /* Merge low and high */
  338. psym->type = actType_;
  339. psym->size = 4;
  340. nsym = psym + 1;
  341. nsym->macro = "HI";
  342. psym->macro = "LO";
  343. nsym->hasMacro = true;
  344. psym->hasMacro = true;
  345. nsym->name = psym->name;
  346. nsym->invalid = true;
  347. numArgs--;
  348. }
  349. break;
  350. case TYPE_PTR:
  351. case TYPE_CONST:
  352. case TYPE_STR:
  353. break;
  354. default:
  355. fprintf(stderr,"STKFRAME::adjustForArgType unhandled actType_ %d \n",actType_);
  356. } /* eos */
  357. }
  358. }