2 /* --------------------------------------------------------------------------
3 * Free variable analysis
5 * The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
6 * Yale Haskell Group, and the Oregon Graduate Institute of Science and
7 * Technology, 1994-1999, All rights reserved. It is distributed as
8 * free software under the license in the file "License", which is
9 * included in the distribution.
11 * $RCSfile: free.c,v $
13 * $Date: 2000/04/27 16:35:29 $
14 * ------------------------------------------------------------------------*/
16 #include "hugsbasictypes.h"
22 /* --------------------------------------------------------------------------
24 * ------------------------------------------------------------------------*/
26 static List freeVarsAlt ( List, StgCaseAlt );
27 static List freeVarsPrimAlt ( List, StgPrimAlt );
28 static List freeVarsExpr ( List, StgExpr );
29 static List freeVarsAtom ( List, StgAtom );
30 static List freeVarsVar ( List, StgVar );
32 /* --------------------------------------------------------------------------
33 * Free variable analysis
34 * ------------------------------------------------------------------------*/
36 static List freeVarsAtom( List acc, StgAtom a)
40 return freeVarsVar(acc,a);
41 /* Note that NAMEs have no free vars. */
47 static List freeVarsVar( List acc, StgVar v)
49 if (cellIsMember(v,acc)) {
56 List freeVarsBind( List acc, StgVar v )
58 StgRhs rhs = stgVarBody(v);
60 switch (whatIs(rhs)) {
62 mapAccum(freeVarsAtom,fvs,stgConArgs(rhs));
65 fvs = freeVarsExpr(fvs,rhs);
68 /* fvs = rev(fvs); */ /* todo might cause less stack rearrangement? */
70 mapAccum(freeVarsVar,acc,fvs); /* copy onto acc */
74 static List freeVarsAlt( List acc, StgCaseAlt alt )
76 if (isDefaultAlt(alt)) {
77 acc = freeVarsExpr(acc,stgDefaultBody(alt));
78 return deleteCell(acc,stgDefaultVar(alt));
80 acc = freeVarsExpr(acc,stgCaseAltBody(alt));
81 return diffList(acc,stgCaseAltVars(alt));
85 static List freeVarsPrimAlt( List acc, StgPrimAlt alt )
87 List vs = stgPrimAltVars(alt);
88 acc = freeVarsExpr(acc,stgPrimAltBody(alt));
89 return diffList(acc,vs);
92 static List freeVarsExpr( List acc, StgExpr e )
95 printf( "freeVarsExpr: " );ppStgExpr(e);printf("\n");
99 mapAccum(freeVarsBind,acc,stgLetBinds(e));
100 return diffList(freeVarsExpr(acc,stgLetBody(e)),stgLetBinds(e));
102 return diffList(freeVarsExpr(acc,stgLambdaBody(e)),stgLambdaArgs(e));
104 mapAccum(freeVarsAlt,acc,stgCaseAlts(e));
105 return freeVarsExpr(acc,stgCaseScrut(e));
107 mapAccum(freeVarsPrimAlt,acc,stgPrimCaseAlts(e));
108 return freeVarsExpr(acc,stgPrimCaseScrut(e));
110 mapAccum(freeVarsAtom,acc,stgPrimArgs(e));
111 /* primop is not a var */
114 /* Doing fun first causes slightly less stack rearrangement. */
115 acc = freeVarsExpr(acc,stgAppFun(e));
116 mapAccum(freeVarsAtom,acc,stgAppArgs(e));
119 return freeVarsVar(acc, e);
122 return acc; /* Names are never free vars */
127 internal("freeVarsExpr");
131 /*-------------------------------------------------------------------------*/