-
Notifications
You must be signed in to change notification settings - Fork 61
/
codegen-expression.lisp
412 lines (361 loc) · 18.3 KB
/
codegen-expression.lisp
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
(defpackage #:coalton-impl/codegen/codegen-expression
(:use
#:cl
#:coalton-impl/codegen/pattern
#:coalton-impl/codegen/ast)
(:import-from
#:coalton-impl/codegen/codegen-pattern
#:codegen-pattern)
(:import-from
#:coalton-impl/codegen/codegen-type-definition
#:constructor-slot-name)
(:local-nicknames
(#:settings #:coalton-impl/settings)
(#:util #:coalton-impl/util)
(#:rt #:coalton-impl/runtime)
(#:tc #:coalton-impl/typechecker)
(#:const #:coalton-impl/constants))
(:export
#:codegen-expression ; FUNCTION
))
(in-package #:coalton-impl/codegen/codegen-expression)
(defun continue-label (lаbеl)
(declare (type symbol lаbеl))
(alexandria:format-symbol :keyword "~a-CONTINUE" lаbеl))
(defun break-label (lаbеl)
(declare (type symbol lаbеl))
(alexandria:format-symbol :keyword "~a-BREAK" lаbеl))
(defgeneric codegen-expression (node current-function env)
(:method ((node node-literal) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function)
(ignore current-function env))
(node-literal-value node))
(:method ((node node-variable) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function)
(ignore current-function env))
(node-variable-value node))
(:method ((node node-application) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
`(rt:call-coalton-function
,(codegen-expression (node-application-rator node) current-function env)
,@(mapcar
(lambda (node)
(codegen-expression node current-function env))
(node-application-rands node))))
(:method ((node node-direct-application) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
`(,(node-direct-application-rator node)
,@(mapcar
(lambda (node)
(codegen-expression node current-function env))
(node-direct-application-rands node))))
(:method ((expr node-abstraction) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
(let* ((var-names (node-abstraction-vars expr))
(arity (length var-names))
(type-decs
(when settings:*emit-type-annotations*
(append
(loop :for name :in (node-abstraction-vars expr)
:for i :from 0
:for arg-ty := (nth i (tc:function-type-arguments (node-type expr)))
:collect `(type ,(tc:lisp-type arg-ty env) ,name))
(list `(values ,(tc:lisp-type (node-type (node-abstraction-subexpr expr)) env) &optional))))))
(rt:construct-function-entry
`(lambda ,var-names
(declare ,@type-decs
(ignorable ,@var-names))
(block @@local
,(codegen-expression (node-abstraction-subexpr expr) '@@local env)))
arity)))
(:method ((expr node-let) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
(let ((sccs (node-binding-sccs (node-let-bindings expr))))
(codegen-let
expr
sccs
current-function
(node-variables expr :variable-namespace-only t)
env)))
(:method ((expr node-lisp) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
(let* ((inner `(values ,(car (last (node-lisp-form expr)))))
(inner
(if (node-lisp-vars expr)
`(let ,(mapcar
(lambda (var)
(list (car var) (cdr var)))
(node-lisp-vars expr))
,@(butlast (node-lisp-form expr))
(values ,(car (last (node-lisp-form expr)))))
inner)))
(if settings:*emit-type-annotations*
`(the (values ,(tc:lisp-type (node-type expr) env) &optional)
,inner)
inner)))
(:method ((expr node-while) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
(let ((pred-expr (codegen-expression (node-while-expr expr) current-function env))
(body-expr (codegen-expression (node-while-body expr) current-function env))
(label (node-while-label expr)))
`(loop
:named ,(break-label label)
:while ,pred-expr
:do
(block ,(continue-label label) ,body-expr))))
(:method ((expr node-while-let) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
(let ((match-expr (codegen-expression (node-while-let-expr expr) current-function env))
(body-expr (codegen-expression (node-while-let-body expr) current-function env))
(label (node-while-let-label expr))
(match-var (gensym "MATCH")))
(multiple-value-bind (pred bindings)
(codegen-pattern (node-while-let-pattern expr) match-var env)
`(loop
:named ,(break-label label)
:for ,match-var
:= ,(if settings:*emit-type-annotations*
`(the ,(tc:lisp-type (node-type (node-while-let-expr expr)) env) ,match-expr)
match-expr)
:while ,pred
:do (block ,(continue-label label)
,(cond ((null bindings) body-expr)
(t `(let ,bindings
(declare (ignorable ,@(mapcar #'car bindings)))
,body-expr))))))))
(:method ((expr node-loop) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
(let ((body-expr (codegen-expression (node-loop-body expr) current-function env))
(label (node-loop-label expr)))
`(loop :named ,(break-label label)
:do (block ,(continue-label label)
,body-expr))))
(:method ((expr node-break) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
`(return-from ,(break-label (node-break-label expr))))
(:method ((expr node-continue) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
`(return-from ,(continue-label (node-continue-label expr))))
(:method ((expr node-match) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
;; If possible codegen a cl:if instead of a trivia:match
(when (and (equalp (node-type (node-match-expr expr)) tc:*boolean-type*)
(= 2 (length (node-match-branches expr)))
(equalp (match-branch-pattern (first (node-match-branches expr)))
(make-pattern-constructor :type tc:*boolean-type* :name 'coalton:True :patterns nil))
(equalp (match-branch-pattern (second (node-match-branches expr)))
(make-pattern-constructor :type tc:*boolean-type* :name 'coalton:False :patterns nil)))
(return-from codegen-expression
`(if ,(codegen-expression (node-match-expr expr) current-function env)
,(codegen-expression (match-branch-body (first (node-match-branches expr))) current-function env)
,(codegen-expression (match-branch-body (second (node-match-branches expr))) current-function env))))
;; Otherwise do the thing
(let ((subexpr (codegen-expression (node-match-expr expr) current-function env))
(match-var (gensym "MATCH")))
`(let ((,match-var
,(if settings:*emit-type-annotations*
`(the ,(tc:lisp-type (node-type (node-match-expr expr)) env) ,subexpr)
subexpr)))
(declare (ignorable ,match-var))
(locally
#+sbcl (declare (sb-ext:muffle-conditions sb-ext:code-deletion-note))
(cond
,@(loop :for branch :in (node-match-branches expr)
:for pattern := (match-branch-pattern branch)
:for expr := (codegen-expression (match-branch-body branch) current-function env)
:collect
(multiple-value-bind (pred bindings)
(codegen-pattern pattern match-var env)
`(,pred
,(cond
((null bindings)
expr)
(t
`(let ,bindings
(declare (ignorable ,@(mapcar #'car bindings)))
,expr))))))
;; Only emit a fallback if there is not a catch-all clause.
,@(unless (member-if (lambda (pat)
(or (pattern-wildcard-p pat)
(pattern-var-p pat)))
(node-match-branches expr)
:key #'match-branch-pattern)
`((t
(error "Pattern match not exhaustive error")))))))))
(:method ((expr node-seq) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
`(progn
,@(mapcar
(lambda (node)
(codegen-expression node current-function env))
(node-seq-nodes expr))))
(:method ((expr node-return) current-function env)
(assert (not (null current-function)))
`(return-from ,current-function ,(codegen-expression (node-return-expr expr) current-function env)))
(:method ((expr node-field) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
`(,(node-field-name expr)
,(codegen-expression (node-field-dict expr) current-function env)))
(:method ((expr node-dynamic-extent) current-function env)
(declare (type tc:environment env)
(type (or null symbol) current-function))
`(let ((,(node-dynamic-extent-name expr)
,(codegen-expression (node-dynamic-extent-node expr) current-function env)))
(declare (dynamic-extent ,(node-dynamic-extent-name expr)))
,(codegen-expression (node-dynamic-extent-body expr) current-function env)))
(:method ((expr node-bind) current-function env)
(let ((name (node-bind-name expr)))
(cond
((and (node-abstraction-p (node-bind-expr expr))
(find name (node-variables (node-bind-body expr) :variable-namespace-only t)))
(let ((arity (length (node-abstraction-vars (node-bind-expr expr)))))
`(let ((,name))
(declare (ignorable ,name))
(flet ((,name
,(node-abstraction-vars (node-bind-expr expr))
(declare (ignorable ,@(node-abstraction-vars (node-bind-expr expr)))
,@(argument-types (node-bind-expr expr) env)
(values ,(tc:lisp-type (tc:function-return-type (node-type (node-bind-expr expr))) env) &optional))
,(codegen-expression (node-abstraction-subexpr (node-bind-expr expr)) name env)))
(setf ,name ,(rt:construct-function-entry `#',name arity))
,(codegen-expression (node-bind-body expr) current-function env)))))
((node-abstraction-p (node-bind-expr expr))
`(flet ((,name
,(node-abstraction-vars (node-bind-expr expr))
(declare (ignorable ,@(node-abstraction-vars (node-bind-expr expr)))
,@(argument-types (node-bind-expr expr) env)
(values ,(tc:lisp-type (tc:function-return-type (node-type (node-bind-expr expr))) env) &optional))
,(codegen-expression (node-abstraction-subexpr (node-bind-expr expr)) name env)))
,(codegen-expression (node-bind-body expr) current-function env)))
(t
`(let ((,name ,(codegen-expression (node-bind-expr expr) current-function env)))
(declare (ignorable ,name))
,(codegen-expression (node-bind-body expr) current-function env)))))))
(defun find-constructor (initform env)
(if (or (node-application-p initform) (node-direct-application-p initform))
(and (node-rator-name initform)
(tc:lookup-constructor env (node-rator-name initform) :no-error t))
nil))
(defun data-letrec-able-p (initform env)
(let ((ctor-ent (find-constructor initform env)))
(and ctor-ent
(let* ((type-name (tc:constructor-entry-constructs ctor-ent))
(type-ent (tc:lookup-type env type-name)))
(not (or (tc:type-entry-enum-repr type-ent)
(tc:type-entry-newtype type-ent)))))))
(declaim (ftype (function (tc:constructor-entry unsigned-byte symbol)
(values list &optional))
setf-accessor))
(defun setf-accessor (ctor-ent nth-slot instance)
(if (eq (tc:constructor-entry-name ctor-ent) 'coalton:Cons)
(ecase nth-slot
(0 `(car ,instance))
(1 `(cdr ,instance)))
`(slot-value ,instance ',(constructor-slot-name ctor-ent nth-slot))))
(defun codegen-let (node sccs current-function local-vars env)
(declare (type node-let node)
(type list sccs)
(type (or null symbol) current-function)
(type util:symbol-list local-vars)
(type tc:environment env))
(when (null sccs)
(return-from codegen-let (codegen-expression (node-let-subexpr node) current-function env)))
(let* ((scc (car sccs))
(scc-bindings
(remove-if-not
(lambda (binding)
(find (car binding) scc))
(node-let-bindings node))))
(cond
;; Function binding group
((every #'node-abstraction-p (mapcar #'cdr scc-bindings))
(let* (;; functions in this scc referenced in the variable namespace
(binding-names-vars (intersection (mapcar #'car scc-bindings) local-vars))
(inner (codegen-let node (cdr sccs) current-function local-vars env))
(inner
(if binding-names-vars
(append
(loop :for (name . node) :in scc-bindings
:for arity := (length (node-abstraction-vars node))
:if (find name binding-names-vars :test #'equalp)
:collect `(setf
,name
,(rt:construct-function-entry `#',name arity)))
(list inner))
(list inner)))
(inner
`(labels ,(loop :for (name . node) :in scc-bindings
:collect `(,name
,(node-abstraction-vars node)
(declare (ignorable ,@(node-abstraction-vars node))
,@(argument-types node env)
(values ,(tc:lisp-type
(tc:function-return-type (node-type node))
env)
&optional))
,(codegen-expression (node-abstraction-subexpr node) name env)))
,@inner))
(node
(if binding-names-vars
`(let
,(loop :for (name . node) :in scc-bindings
:if (find name binding-names-vars :test #'equalp)
:collect name)
(declare (ignorable ,@(mapcar #'car scc-bindings)))
,inner)
inner)))
node))
((every (lambda (pair)
(data-letrec-able-p (cdr pair)
env))
scc-bindings)
(let* ((inner (codegen-let node (cdr sccs) current-function local-vars env))
(assignments (loop :for (name . initform) :in scc-bindings
:for ctor-info := (find-constructor initform env)
:appending (loop :for arg :in (node-rands initform)
:for i :from 0
:collect `(setf ,(setf-accessor ctor-info i name)
,(codegen-expression arg current-function env)))))
(allocations (loop :for (name . initform) :in scc-bindings
:for ctor-info := (find-constructor initform env)
:for ctor-classname := (tc:constructor-entry-classname ctor-info)
:collect
(if ctor-classname
`(allocate-instance (find-class ',ctor-classname))
`(,(node-rator-name initform)
,@(mapcar (constantly nil) (node-direct-application-rands initform)))))))
`(let ,(mapcar (lambda (scc-binding allocation)
(list (car scc-binding) allocation))
scc-bindings allocations)
(declare (ignorable ,@(mapcar #'car scc-bindings)))
,@assignments
,inner)))
;; Single variable binding
((= 1 (length scc-bindings))
(let ((name (car (first scc-bindings)))
(node_ (cdr (first scc-bindings))))
`(let ((,name ,(codegen-expression node_ current-function env)))
(declare (ignorable ,name))
,(codegen-let node (cdr sccs) current-function local-vars env))))
(t (error "Invalid scc binding group. This should have been detected during typechecking.")))))
(defun argument-types (node env)
(declare (type node-abstraction node)
(type tc:environment env))
(loop :for var :in (node-abstraction-vars node)
:for ty :in (tc:function-type-arguments (node-type node))
:collect `(type ,(tc:lisp-type ty env) ,var)))