blob: 40109fe0a96f54feb9eef347c56cbf185da18096 (
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
|
(define-library (csc ir2)
(export
apply-arguments
apply-procedure
apply?
atom-continuation
atom-expression
atom?
branch-atom
branch-false
branch-true
branch?
call-closure-args
call-closure-closure
call-closure?
closure-arguments
closure-body
closure-name
closure-rest
closure?
fix-body
fix-functions
fix?
ir2=?
kargs-expression
kargs-refs
kargs?
klabel-expression
klabel?
ktail?
make-apply
make-atom
make-branch
make-call-closure
make-closure
make-fix
make-kargs
make-klabel
make-ktail
make-update
update-atom
update-continuation
update-ref
; Re-exports from IR1.
constant-expression
constant?
lexical-ref-gensym
lexical-ref-name
lexical-ref?
library-ref-library
library-ref-name
library-ref?
make-constant
make-lexical-ref
lexical-set-expression
lexical-set-ref
lexical-set?
make-lexical-set
make-library-ref)
(import (scheme base)
(only (csc ir1)
constant?
ir1=?
lexical-ref-gensym
lexical-ref-name
lexical-ref?
lexical-set-expression
lexical-set-ref
lexical-set?
library-ref-library
library-ref-name
library-ref?
make-constant
make-lexical-ref
make-lexical-set
make-library-ref)
(only (csc list) all)
(only (csc loop)
loop
return)
(only (csc match)
define-match-record-type))
(begin
; This library defines the intermediate representation IR2.
; It's CPS time bitch.
; CPS atom:
; An atom is a value that can be computed immediately without
; any subexpressions.
; Atoms consist of
; - constant,
; - lexical-ref,
; - or library-ref
; After closure conversion, lexical refs are no longer allowed.
; Lexical refs are converted to one of the below data types.
; A variable representing the address of a function in the same compilation
; unit. This will be a constant after linking.
(define-match-record-type <label>
(make-label gensym)
label?
%label
(gensym label-gensym))
; A local variable. This can be an argument to a function or the result of
; a primitive.
(define-match-record-type <var>
(make-var gensym)
var?
%var
(gensym var-gensym))
; CPS expressions:
; CPS expressions are similar to IR1 expressions,
; but constrained not to have any subexpressions except atoms.
; And they take a continuation.
; Modifies a library or lexically bound variable to the given atom.
(define-match-record-type <update>
(make-update ref atom continuation)
update?
%update
(ref update-ref)
(atom update-atom)
(continuation update-continuation))
; Branches depending on the given atom.
; If it is true, continue with continuation true.
; If false, continue with continuation false.
(define-match-record-type <branch>
(make-branch atom true false)
branch?
%branch
(atom branch-atom)
(true branch-true)
(false branch-false))
; Applies a procedure to a list of arguments. Apply does not take a
; continuation. Instead the continuation will be passed as the first
; argument to the function.
(define-match-record-type <apply>
(make-apply procedure arguments)
apply?
%apply
(procedure apply-procedure)
(arguments apply-arguments))
; A procedure. All closures are allocated in a fix expression. A closure
; does not take a continuation. Instead, the procedure will accept the
; continuation as an argument.
(define-match-record-type <closure>
(make-closure name arguments rest body)
closure?
%closure
(name closure-name)
(arguments closure-arguments)
(rest closure-rest)
(body closure-body))
; Defines a list of mutually recursive procedures.
; Functions is a list of closures, and body is an expression.
(define-match-record-type <fix>
(make-fix functions body)
fix?
%fix
(functions fix-functions)
(body fix-body))
(define (closure=? x y)
(let ((x-args (closure-arguments x))
(x-rest (closure-rest x))
(y-args (closure-arguments y))
(y-rest (closure-rest y)))
(and (ir1=? (closure-name x) (closure-name y))
(= (length x-args) (length y-args))
(all ir1=? x-args y-args)
(or (and (not x-rest) (not y-rest))
(and x-rest y-rest (ir1=? x-rest y-rest)))
(ir2=? (closure-body x) (closure-body y)))))
(define (ir2=?-sametype x y)
(cond
((and (update? x) (update? y))
(and (ir1=? (update-ref x) (update-ref y))
(ir1=? (update-atom x) (update-atom y))
(ir2=? (update-continuation x) (update-continuation y))))
((and (branch? x) (branch? y))
(and (ir1=? (branch-atom x) (branch-atom y))
(ir2=? (branch-true x) (branch-true y))
(ir2=? (branch-false x) (branch-false y))))
((and (apply? x) (apply? y))
(let ((x-args (apply-arguments x))
(y-args (apply-arguments y)))
(and (ir1=? (apply-procedure x) (apply-procedure y))
(= (length x-args) (length y-args))
(all ir1=? x-args y-args))))
((and (fix? x) (fix? y))
(let ((x-funs (fix-functions x))
(y-funs (fix-functions y)))
(and (= (length x-funs) (length y-funs))
(all closure=? x-funs y-funs)
(ir2=? (fix-body x) (fix-body y)))))
(else #f)))
(define (ir2=? x y)
(cond
((ir2=?-sametype x y) #t)
((and (ir2=?-sametype x x) (ir2=?-sametype y y))
#f)
(else (error "One or more arguments has a type unknown to ir2=?" x y))))))
|