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