--- ray/src/common/caldefn.c 2023/09/26 18:33:14 2.36 +++ ray/src/common/caldefn.c 2024/06/20 21:21:24 2.39 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: caldefn.c,v 2.36 2023/09/26 18:33:14 greg Exp $"; +static const char RCSid[] = "$Id: caldefn.c,v 2.39 2024/06/20 21:21:24 greg Exp $"; #endif /* * Store variable definitions. @@ -58,6 +58,8 @@ static VARDEF *htpos; /* ...dfirst() and */ static EPNODE *ochpos; /* ...dnext */ static EPNODE *outchan; +static int optimized = 0; /* are we optimized? */ + EPNODE *curfunc = NULL; @@ -175,14 +177,14 @@ dclear( /* delete variable definitions of name */ char *name ) { - EPNODE *ep; + VARDEF *vp; + EPNODE *dp; - while ((ep = dpop(name)) != NULL) { - if (ep->type == ':') { - dpush(name, ep); /* don't clear constants */ - return; - } - epfree(ep); + while ((vp = varlookup(name)) != NULL && + (dp = vp->def) != NULL && dp->type == '=') { + vp->def = dp->sibling; + epfree(dp,1); + varfree(vp); } } @@ -195,7 +197,7 @@ dremove( /* delete all definitions of name */ EPNODE *ep; while ((ep = dpop(name)) != NULL) - epfree(ep); + epfree(ep,1); } @@ -318,7 +320,7 @@ qualname( /* get qualified name */ while (*++cpp && *cpp != CNTXMARK) ; } - while (*cpp) { /* copy context to static buffer */ + while (*cpp) { /* add remaining context to name */ if (cp >= nambuf+RMAXWORD) goto toolong; *cp++ = *cpp++; @@ -356,6 +358,22 @@ chanout( /* set output channels */ void +doptimize(int activate) /* optimize current and future definitions? */ +{ + EPNODE *ep; + + if (activate && optimized) + return; /* already going */ + + if (!(optimized = activate)) + return; /* switching off */ + + for (ep = dfirst(); ep != NULL; ep = dnext()) + epoptimize(ep); +} + + +void dcleanup( /* clear definitions (0->vars,1->output,2->consts) */ int lvl ) @@ -376,8 +394,7 @@ dcleanup( /* clear definitions (0->vars,1->output,2-> while (outchan != NULL) { ep = outchan; outchan = ep->sibling; - ep->sibling = NULL; - epfree(ep); + epfree(ep,1); } } @@ -560,7 +577,7 @@ addchan( /* add an output channel assignment */ sp->sibling = ep; else { sp->sibling = ep->sibling; - epfree(ep); + epfree(ep,1); } return; } @@ -587,9 +604,13 @@ getstatement(void) /* get next statement */ if (esupport&E_OUTCHAN && nextc == '$') { /* channel assignment */ ep = getchan(); + if (optimized) + epoptimize(ep); /* optimize new chan expr */ addchan(ep); } else { /* ordinary definition */ ep = getdefn(); + if (optimized) + epoptimize(ep); /* optimize new statement */ qname = qualname(dfn_name(ep), 0); if (esupport&E_REDEFW && (vdef = varlookup(qname)) != NULL) { if (vdef->def != NULL && epcmp(ep, vdef->def)) {