blob: cf46e055aefc04d22f0150ac39483be43d412d7d (
plain) (
blame)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
(define-library (csc cps)
(export
ir1->ir2)
(import (scheme base)
(only (csc gensym)
gensym
gensym->int)
(only (csc hash-map)
insert
key-not-found-error?
lookup
make-map
merge)
(only (csc ir1)
%call
%define-syntax
%if
%lambda
%letrec
%lexical-set
%library-define
%sequence
call?
constant?
if?
lambda?
letrec-gensyms
letrec-names
letrec-values
letrec?
lexical-ref-gensym
lexical-ref?
lexical-set?
library-define?
library-ref?
make-call
make-constant
make-lambda
make-lexical-ref
make-lexical-set
make-sequence
sequence?)
(only (csc ir2)
%apply
%branch
%fix
%primitive
closure-arguments
closure-body
closure-name
closure-rest
make-apply
make-atom
make-branch
make-call-closure
make-closure
make-fix
make-kargs
make-klabel
make-ktail
make-primitive)
(only (csc loop)
loop
return)
(only (csc match)
define-match-record-type
match))
(begin
; Update is a CPS expression that is used internally as part of
; CPS conversion.
; Update expressions are then removed by box-conversion.
(define-match-record-type <update>
(make-update ref atom continuation)
update?
%update
(ref update-ref)
(atom update-atom)
(continuation update-continuation))
(define (new-ref)
(make-lexical-ref 'generated-symbol (gensym)))
(define (collect-functions-and-variables expr)
(let ((names (letrec-names expr))
(gensyms (letrec-gensyms expr))
(vals (letrec-values expr)))
(loop for name in names
for gensym in gensyms
for value in vals
if (lambda? value)
collect (match value
((% %lambda args rest body)
(define continuation (new-ref))
(make-closure
(make-lexical-ref name gensym)
(cons continuation args)
rest
(to-cps
body
(lambda (z)
(make-apply continuation (list z)))))))
into functions
else
collect (make-lexical-ref name gensym) into variable-names
and collect value into variable-values
finally (return (values functions variable-names variable-values)))))
(define (to-cps expr continuation)
(match expr
(_ (when (or (constant? expr)
(lexical-ref? expr)
(library-ref? expr)))
(continuation expr))
((% %lexical-set ref arg)
(to-cps
arg
(lambda (val)
(make-update ref val (continuation (make-constant #f))))))
((% %library-define ref arg)
(to-cps
arg
(lambda (val)
(make-update ref val (continuation (make-constant #f))))))
((% %define-syntax _ _)
; no-op
(continuation (make-constant #f)))
((% %if test consequent alternate)
(to-cps
test
(lambda (val)
(define continuation-ref (new-ref))
(define result-ref (new-ref))
(make-fix
(list (make-closure continuation-ref (list result-ref) #f
(continuation result-ref)))
(make-branch val
(to-cps
consequent
(lambda (result)
(make-apply continuation-ref (list result))))
(to-cps
alternate
(lambda (result)
(make-apply continuation-ref (list result)))))))))
((% %call proc args)
(define return-address (new-ref))
(define result (new-ref))
(make-fix
(list (make-closure return-address (list result) #f (continuation result)))
(to-cps
proc
(lambda (f)
; Technically the order of evaluation is unspecified.
; We evaluate expressions left to right.
;
; I would use the loop macro, but it mutates the loop
; variables which plays badly with building a lambda.
(let loop ((args* (reverse args))
(exprs (lambda (vals)
(make-apply f (cons return-address (reverse vals))))))
(if (null? args*)
(exprs '())
(loop (cdr args*)
(lambda (vals)
(to-cps
(car args*)
(lambda (val)
(exprs (cons val vals))))))))))))
((% %sequence head tail)
(to-cps
head
(lambda (x)
(to-cps
tail
continuation))))
((% %lambda args rest body)
(define f (new-ref))
(define k (new-ref))
(make-fix
(list
(make-closure f (cons k args) rest
(to-cps
body
(lambda (ret)
(make-apply k (list ret))))))
(continuation f)))
((% %letrec in-order? _ _ _ body)
(define-values (functions variable-names variable-values) (collect-functions-and-variables expr))
(make-fix functions
(to-cps
; We re-write a letrec into a corresponding lambda form.
(if in-order?
(loop for name in (reverse variable-names)
for value in (reverse variable-values)
for expr = (make-call
(make-lambda
(list name)
#f
body)
(list value))
then (make-call
(make-lambda
(list name)
#f
expr)
(list value))
finally (return expr))
(make-call
(make-lambda
variable-names
#f
body)
variable-values))
continuation)))
(_ (error "unexpected type in to-cps" expr))))
(define (make-ref-map)
(make-map
(lambda (ref)
(gensym->int (lexical-ref-gensym ref)))
(lambda (x y) (< (gensym->int (lexical-ref-gensym x))
(gensym->int (lexical-ref-gensym y))))))
(define (get-boxed expr)
(match expr
((% %update ref _ continuation)
(define m (get-boxed continuation))
(when (lexical-ref? ref)
(set! m (insert m ref #t)))
m)
((% %primitive _ _ _ continuation)
(get-boxed continuation))
((% %branch _ true false)
(merge
(get-boxed true)
(get-boxed false)))
((% %apply proc args)
(make-ref-map))
((% %fix funs body)
(loop with m = (get-boxed body)
for fun in funs
do (set! m (merge m (get-boxed (closure-body fun))))
finally (return m)))
(_ (error "Unexpected form in get-boxed"))))
(define (all-closure-args fun)
(define args (closure-arguments fun))
(define rest (closure-rest fun))
(when rest
(set! args (cons rest args)))
args)
; Rewrites the given expression to have no more <update> forms.
(define (box-conversion expr)
(define boxed-refs (get-boxed expr))
(define (boxed? ref)
(or (library-ref? ref) ; globals are always boxed
(and (lexical-ref? ref)
(guard (e ((key-not-found-error? e) #f))
(lookup boxed-refs ref)))))
(define (convert-arg-list args)
(define boxed-args (loop for arg in args
if (boxed? arg)
collect arg))
(define vars (loop for x in boxed-args
collect (new-ref)))
(define new-args (loop with v* = vars
for arg in args
collect (if (boxed? arg)
(car v*)
arg)
if (boxed? arg)
do (set! v* (cdr v*))))
(values new-args boxed-args vars))
(let convert ((expr expr))
(match expr
((% %update ref atom continuation)
(make-primitive 'poke (list atom ref (make-constant 0)) '() (convert continuation)))
((% %primitive op args res continuation)
; Note that no reference in res can be boxed.
(define-values (new-args boxed-args vars) (convert-arg-list args))
(define new-expr (make-primitive op new-args res (convert continuation)))
(loop for arg in boxed-args
for var in vars
do (set! new-expr (make-primitive 'peek (list arg (make-constant 0)) (list var)
new-expr))
finally (return new-expr)))
((% %branch atom true false)
(if (boxed? atom)
(let ((var (new-ref)))
(make-primitive 'peek (list atom (make-constant 0)) (list var)
(make-branch var (convert true) (convert false))))
(make-branch atom (convert true) (convert false))))
((% %apply proc args)
(define-values (new-params boxed-params vars) (convert-arg-list (cons proc args)))
(define new-expr (make-apply (car new-params) (cdr new-params)))
(loop for p in boxed-params
for var in vars
do (set! new-expr (make-primitive 'peek (list p (make-constant 0)) (list var)
new-expr))
finally (return new-expr)))
((% %fix funs body)
(define-values (new-names boxed-names temp-names) (convert-arg-list (loop for fun in funs
collect (closure-name fun))))
(define new-funs (loop for fun in funs
for new-name in new-names
for rest = (closure-rest fun)
collect (let-values (((new-args boxed-args temp-args) (convert-arg-list (all-closure-args fun))))
(make-closure
new-name
(if rest
(cdr new-args)
new-args)
(if rest
(car new-args)
#f)
(let ((new-expr (convert (closure-body fun))))
(loop for arg in boxed-args
for var in temp-args
do (set! new-expr (make-primitive 'alloc (list (make-constant 1)) (list arg)
(make-primitive 'poke (list var arg (make-constant 0)) '()
new-expr)))
finally (return new-expr)))))))
(define new-body (convert body))
(loop for name in boxed-names
for var in temp-names
do (set! new-body (make-primitive 'poke (list var name (make-constant 0)) '()
new-body)))
(define new-expr (make-fix new-funs new-body))
(loop for name in boxed-names
do (set! new-expr (make-primitive 'alloc (list (make-constant 1)) (list name)
new-expr))
finally (return expr))))))
(define (ir1->ir2 expr continuation)
(box-conversion (to-cps expr continuation)))))
|