1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/* --------------------------------------------------------------------------
* Lambda Lifter
*
* This is a very simple lambda lifter - it doesn't try to do Johnsson-style
* lambda lifting (yet).
*
* The Hugs 98 system is Copyright (c) Mark P Jones, Alastair Reid, the
* Yale Haskell Group, and the Oregon Graduate Institute of Science and
* Technology, 1994-1999, All rights reserved. It is distributed as
* free software under the license in the file "License", which is
* included in the distribution.
*
* $RCSfile: lift.c,v $
* $Revision: 1.11 $
* $Date: 2000/03/10 20:03:36 $
* ------------------------------------------------------------------------*/
#include "prelude.h"
#include "storage.h"
#include "connect.h"
#include "errors.h"
/* --------------------------------------------------------------------------
* Local function prototypes:
* ------------------------------------------------------------------------*/
static List liftedBinds = NIL;
static Bool makeInlineable = FALSE;
static Int inlineCounter = 0;
static StgExpr abstractExpr ( List vars, StgExpr e );
static inline Bool isTopLevel( StgVar v );
static List filterFreeVars( List vs );
static List liftLetBinds ( List binds, Bool topLevel );
static void liftAlt ( StgCaseAlt alt );
static void liftPrimAlt ( StgPrimAlt alt );
static void liftExpr ( StgExpr e );
/* --------------------------------------------------------------------------
* Lambda lifter
* ------------------------------------------------------------------------*/
/* abstract variables out of an expression */
static StgExpr abstractExpr( List vars, StgExpr e )
{
List args = NIL;
List sub = NIL; /* association list */
for(; nonNull(vars); vars=tl(vars)) {
StgVar var = hd(vars);
StgVar arg = mkStgVar(NIL,NIL);
stgVarRep(arg) = stgVarRep(var);
args = cons(arg,args);
sub = cons(pair(var,arg),sub);
}
return makeStgLambda(rev(args),substExpr(sub,e));
}
/* ToDo: should be conservative estimate but isn't */
/* Will a variable be floated out to top level - conservative estimate? */
static inline Bool isTopLevel( StgVar v )
{
if (isNull(stgVarBody(v))) {
return FALSE; /* only let bound vars can be floated */
} else if (stgVarInfo(v) == NONE) {
return TRUE; /* those at top level are already there */
} else {
return FALSE;
}
}
static List filterFreeVars( List vs )
{
List fvs = NIL;
if (vs == NONE) {
return NIL;
} else {
for(; nonNull(vs); vs=tl(vs)) {
StgVar v = hd(vs);
if (!isTopLevel(v)) {
fvs = cons(v,fvs);
}
}
return fvs;
}
}
static List liftLetBinds( List binds, Bool topLevel )
{
List bs = NIL;
for(; nonNull(binds); binds=tl(binds)) {
StgVar bind = hd(binds);
StgRhs rhs = stgVarBody(bind);
List fvs = filterFreeVars(stgVarInfo(bind));
switch (whatIs(rhs)) {
case STGCON:
case STGAPP:
case STGVAR:
case NAME:
bs = cons(bind,bs);
break;
default:
liftExpr(rhs);
if (nonNull(fvs)) {
StgVar v = mkStgVar(abstractExpr(fvs,rhs),NONE);
liftedBinds = cons(v,liftedBinds);
if (makeInlineable) {
Name n;
char s[16];
sprintf(s,"lam%d",inlineCounter++);
n = newName(findText(s),NIL);
name(n).stgVar = v;
stgVarBody(bind) = makeStgApp(n, fvs);
} else {
stgVarBody(bind) = makeStgApp(v, fvs);
}
}
bs = cons(bind,bs);
break;
}
}
return bs;
}
static void liftAlt( StgCaseAlt alt )
{
if (isDefaultAlt(alt))
liftExpr(stgDefaultBody(alt)); else
liftExpr(stgCaseAltBody(alt));
}
static void liftPrimAlt( StgPrimAlt alt )
{
liftExpr(stgPrimAltBody(alt));
}
static void liftExpr( StgExpr e )
{
switch (whatIs(e)) {
case LETREC:
stgLetBinds(e) = liftLetBinds(stgLetBinds(e),FALSE);
liftExpr(stgLetBody(e));
break;
case LAMBDA:
liftExpr(stgLambdaBody(e));
break;
case CASE:
liftExpr(stgCaseScrut(e));
mapProc(liftAlt,stgCaseAlts(e));
break;
case PRIMCASE:
liftExpr(stgPrimCaseScrut(e));
mapProc(liftPrimAlt,stgPrimCaseAlts(e));
break;
case STGPRIM:
break;
case STGAPP:
break;
case STGVAR:
case NAME:
break;
default:
internal("liftExpr");
}
}
/* Lift a list of top-level binds. */
List liftBinds( List binds )
{
List bs;
for(bs=binds; nonNull(bs); bs=tl(bs)) {
StgVar bind = hd(bs);
if (debugSC) {
if (lastModule() != modulePrelude) {
fprintf(stderr, "\n");
ppStg(hd(bs));
fprintf(stderr, "\n");
}
}
freeVarsBind(NIL,bind);
stgVarInfo(bind) = NONE; /* mark as top level */
}
liftedBinds = NIL;
binds = liftLetBinds(binds,TRUE);
binds = revOnto(liftedBinds,binds);
liftedBinds = NIL;
return binds;
}
/* --------------------------------------------------------------------------
* Compiler control:
* ------------------------------------------------------------------------*/
Void liftControl(what)
Int what; {
switch (what) {
case POSTPREL: break;
case PREPREL:
case RESET:
liftedBinds = NIL;
break;
case MARK:
mark(liftedBinds);
break;
}
}
/*-------------------------------------------------------------------------*/
|