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: 1999/02/03 17:08:40 $
12 * ------------------------------------------------------------------------*/
20 /* --------------------------------------------------------------------------
21 * Local function prototypes:
22 * ------------------------------------------------------------------------*/
24 static StgVar substVar ( List sub, StgVar v );
25 static StgAtom substAtom ( List sub, StgAtom a );
26 static void substBind ( List sub, StgVar bind );
27 static void substAlt ( List sub, StgCaseAlt alt );
28 static void substPrimAlt ( List sub, StgPrimAlt alt );
30 /* --------------------------------------------------------------------------
31 * Substitute variables throughout an expression - updating in place.
32 * ------------------------------------------------------------------------*/
34 static StgVar substVar( List sub, StgVar v )
36 Pair p = cellAssoc(v,sub);
44 static StgAtom substAtom ( List sub, StgAtom a )
48 return substVar(sub,a);
54 static Void substBind( List sub, StgVar bind )
56 StgRhs rhs = stgVarBody(bind);
57 switch (whatIs(rhs)) {
59 map1Over(substAtom,sub,stgConArgs(rhs));
62 stgVarBody(bind) = substExpr(sub,rhs);
67 static Void substAlt( List sub, StgCaseAlt alt )
69 stgCaseAltBody(alt) = substExpr(sub,stgCaseAltBody(alt));
72 static Void substPrimAlt( List sub, StgPrimAlt alt )
74 stgPrimAltBody(alt) = substExpr(sub,stgPrimAltBody(alt));
77 StgExpr substExpr( List sub, StgExpr e )
81 map1Proc(substBind,sub,stgLetBinds(e));
82 stgLetBody(e) = substExpr(sub,stgLetBody(e));
85 stgLambdaBody(e) = substExpr(sub,stgLambdaBody(e));
88 stgCaseScrut(e) = substExpr(sub,stgCaseScrut(e));
89 map1Proc(substAlt,sub,stgCaseAlts(e));
92 stgPrimCaseScrut(e) = substExpr(sub,stgPrimCaseScrut(e));
93 map1Proc(substPrimAlt,sub,stgPrimCaseAlts(e));
96 map1Over(substAtom,sub,stgPrimArgs(e));
99 stgAppFun(e) = substVar(sub,stgAppFun(e));
100 map1Over(substAtom,sub,stgAppArgs(e));
104 return substVar(sub,e);
106 internal("substExpr");
111 /*-------------------------------------------------------------------------*/