1 /* -*- mode: hugs-c; -*- */
2 /* --------------------------------------------------------------------------
3 * Substitute variables in an expression
5 * Copyright (c) The University of Nottingham and Yale University, 1994-1997.
6 * All rights reserved. See NOTICE for details and conditions of use etc...
7 * Hugs version 1.4, December 1997
9 * $RCSfile: stgSubst.c,v $
11 * $Date: 1998/12/02 13:22:40 $
12 * ------------------------------------------------------------------------*/
22 /* --------------------------------------------------------------------------
23 * Local function prototypes:
24 * ------------------------------------------------------------------------*/
26 static StgVar substVar ( List sub, StgVar v );
27 static StgAtom substAtom ( List sub, StgAtom a );
28 static void substBind ( List sub, StgVar bind );
29 static void substAlt ( List sub, StgCaseAlt alt );
30 static void substPrimAlt ( List sub, StgPrimAlt alt );
32 /* --------------------------------------------------------------------------
33 * Substitute variables throughout an expression - updating in place.
34 * ------------------------------------------------------------------------*/
36 static StgVar substVar( List sub, StgVar v )
38 Pair p = cellAssoc(v,sub);
46 static StgAtom substAtom ( List sub, StgAtom a )
50 return substVar(sub,a);
56 static Void substBind( List sub, StgVar bind )
58 StgRhs rhs = stgVarBody(bind);
59 switch (whatIs(rhs)) {
61 map1Over(substAtom,sub,stgConArgs(rhs));
64 stgVarBody(bind) = substExpr(sub,rhs);
69 static Void substAlt( List sub, StgCaseAlt alt )
71 stgCaseAltBody(alt) = substExpr(sub,stgCaseAltBody(alt));
74 static Void substPrimAlt( List sub, StgPrimAlt alt )
76 stgPrimAltBody(alt) = substExpr(sub,stgPrimAltBody(alt));
79 StgExpr substExpr( List sub, StgExpr e )
83 map1Proc(substBind,sub,stgLetBinds(e));
84 stgLetBody(e) = substExpr(sub,stgLetBody(e));
87 stgLambdaBody(e) = substExpr(sub,stgLambdaBody(e));
90 stgCaseScrut(e) = substExpr(sub,stgCaseScrut(e));
91 map1Proc(substAlt,sub,stgCaseAlts(e));
94 stgPrimCaseScrut(e) = substExpr(sub,stgPrimCaseScrut(e));
95 map1Proc(substPrimAlt,sub,stgPrimCaseAlts(e));
98 map1Over(substAtom,sub,stgPrimArgs(e));
101 stgAppFun(e) = substVar(sub,stgAppFun(e));
102 map1Over(substAtom,sub,stgAppArgs(e));
106 return substVar(sub,e);
108 internal("substExpr");
113 /*-------------------------------------------------------------------------*/