-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathentry.lisp
276 lines (234 loc) · 12 KB
/
entry.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
(defpackage #:coalton-impl/entry
(:use
#:cl)
(:shadow
#:compile)
(:local-nicknames
(#:se #:source-error)
(#:settings #:coalton-impl/settings)
(#:stream #:coalton-impl/stream)
(#:util #:coalton-impl/util)
(#:parser #:coalton-impl/parser)
(#:tc #:coalton-impl/typechecker)
(#:analysis #:coalton-impl/analysis)
(#:codegen #:coalton-impl/codegen))
(:export
#:*global-environment*
#:entry-point ; FUNCTION
#:expression-entry-point ; FUNCTION
#:codegen ; FUNCTION
#:compile ; FUNCTION
#:compile-coalton-toplevel ; FUNCTION
#:compile-to-lisp ; FUNCTION
))
(in-package #:coalton-impl/entry)
(defvar *global-environment* (tc:make-default-environment))
(defun entry-point (program)
(declare (type parser:program program))
(let* ((*package* (parser:program-lisp-package program))
(program (parser:rename-variables program))
(file (parser:program-file program))
(env *global-environment*))
(multiple-value-bind (type-definitions instances env)
(tc:toplevel-define-type (parser:program-types program)
(parser:program-structs program)
file
env)
(multiple-value-bind (class-definitions env)
(tc:toplevel-define-class (parser:program-classes program)
file
env)
(multiple-value-bind (ty-instances env)
(tc:toplevel-define-instance (append instances (parser:program-instances program)) env file)
(multiple-value-bind (toplevel-definitions env)
(tc:toplevel-define (parser:program-defines program)
(parser:program-declares program)
file
env)
(multiple-value-bind (toplevel-instances)
(tc:toplevel-typecheck-instance ty-instances
(append instances (parser:program-instances program))
env
file)
(setf env (tc:toplevel-specialize (parser:program-specializations program) env file))
(let ((monomorphize-table (make-hash-table :test #'eq))
(translation-unit
(tc:make-translation-unit
:types type-definitions
:definitions toplevel-definitions
:classes class-definitions
:instances toplevel-instances
:package *package*)))
(loop :for define :in (parser:program-defines program)
:when (parser:toplevel-define-monomorphize define)
:do (setf (gethash (parser:node-variable-name (parser:toplevel-define-name define))
monomorphize-table)
t))
(loop :for declare :in (parser:program-declares program)
:when (parser:toplevel-declare-monomorphize declare)
:do (setf (gethash (parser:identifier-src-name (parser:toplevel-declare-name declare))
monomorphize-table)
t))
(analysis:analyze-translation-unit translation-unit env file)
(codegen:compile-translation-unit translation-unit monomorphize-table env)))))))))
(defun expression-entry-point (node file)
(declare (type parser:node node)
(type se:file file))
(let ((env *global-environment*))
(multiple-value-bind (ty preds accessors node subs)
(tc:infer-expression-type (parser:rename-variables node)
(tc:make-variable)
nil
(tc:make-tc-env :env env)
file)
(multiple-value-bind (preds subs)
(tc:solve-fundeps env preds subs)
(setf accessors (tc:apply-substitution subs accessors))
(multiple-value-bind (accessors subs_)
(tc:solve-accessors accessors file env)
(setf subs (tc:compose-substitution-lists subs subs_))
(when accessors
(error 'tc:tc-error
:err (se:source-error
:span (tc:accessor-source (first accessors))
:file file
:message "Ambiguous accessor"
:primary-note "accessor is ambiguous")))
(let* ((preds (tc:reduce-context env preds subs))
(subs (tc:compose-substitution-lists
(tc:default-subs env nil preds)
subs))
(preds (tc:reduce-context env preds subs))
(node (tc:apply-substitution subs node))
(ty (tc:apply-substitution subs ty))
(qual-ty (tc:qualify preds ty))
(scheme (tc:quantify (tc:type-variables qual-ty) qual-ty)))
(when (null preds)
(return-from expression-entry-point
(let ((node (codegen:optimize-node
(codegen:translate-expression node nil env)
env)))
(codegen:codegen-expression
(codegen:direct-application
node
(codegen:make-function-table env))
nil
env))))
(let* ((tvars
(loop :for i :to (1- (length (remove-duplicates (tc:type-variables qual-ty)
:test #'equalp)))
:collect (tc:make-variable)))
(qual-type (tc:instantiate
tvars
(tc:ty-scheme-type scheme))))
(error 'tc:tc-error
:err (se:source-error
:span (tc:node-source node)
:file file
:message "Unable to codegen"
:primary-note (format nil
"expression has type ~A~{ ~S~}.~{ (~S)~} => ~S with unresolved constraint~A ~S"
(if settings:*coalton-print-unicode*
"∀"
"FORALL")
tvars
(tc:qualified-ty-predicates qual-type)
(tc:qualified-ty-type qual-type)
(if (= (length (tc:qualified-ty-predicates qual-type)) 1)
""
"s")
(tc:qualified-ty-predicates qual-type))
:notes
(list
(se:make-source-error-note
:type :secondary
:span (tc:node-source node)
:message "Add a type assertion with THE to resolve ambiguity")))))))))))
(defmacro with-environment-updates (updates &body body)
"Collect environment updates into a vector bound to UPDATES."
`(let* ((,updates (make-array 0 :adjustable t :fill-pointer 0))
(tc:*update-hook* (lambda (name arg-list)
(vector-push-extend (cons name arg-list) ,updates))))
,@body))
(defun make-environment-updater (update-log)
"Produce source form of the contents of an environment UPDATE-LOG (i.e., calls to functions in typechecker/environment)."
(let ((updates (remove-duplicates (coerce update-log 'list) :test #'code-update-eql)))
`(let ((env *global-environment*))
,@(loop :for (fn . args) :in updates
:collect `(setf env (,fn env ,@(mapcar #'util:runtime-quote args))))
(setf *global-environment* env))))
(defun compile-coalton-toplevel (program)
"Compile PROGRAM and return a single form suitable for direct inclusion by Lisp compiler. For implementing coalton-toplevel macro."
(with-environment-updates updates
(multiple-value-bind (program env)
(entry-point program)
(setf *global-environment* env)
`(progn
,(make-prologue)
,(make-environment-updater updates)
,program))))
(defun code-update-eql (a b)
"Compare environment updates, returning t for set-code updates of the same symbol."
(and (eql (first a) 'coalton-impl/typechecker/environment:set-code)
(eql (first b) 'coalton-impl/typechecker/environment:set-code)
(eql (second a)
(second b))))
(defun make-prologue ()
"Return source form of an assertion that prevents mixing development and release modes."
`(eval-when (:load-toplevel)
(unless (eq (settings:coalton-release-p) ,(settings:coalton-release-p))
,(if (settings:coalton-release-p)
`(error "~A was compiled in release mode but loaded in development."
,(or *compile-file-pathname* *load-truename*))
`(error "~A was compiled in development mode but loaded in release."
,(or *compile-file-pathname* *load-truename*))))))
(defun print-form (stream form &optional (package "CL"))
"Print a FORM to a STREAM, separated by 2 lines."
(with-standard-io-syntax
(let ((*package* (find-package package))
(*print-case* ':downcase)
(*print-pretty* t)
(*print-right-margin* 80))
(prin1 form stream)
(terpri stream)
(terpri stream))))
(defun compile-to-lisp (stream name output)
"Read Coalton source from STREAM and write Lisp source to OUTPUT. NAME may be the filename related to the input stream."
(declare (optimize (debug 3)))
(parser:with-reader-context stream
(with-environment-updates updates
(let* ((file (se:make-file :stream stream
:name name))
(program (parser:read-program stream file ':file))
(program-text (entry-point program))
(program-package (parser:program-package program))
(package-name (parser:toplevel-package-name program-package)))
(print-form output (make-prologue))
(print-form output (make-environment-updater updates))
(print-form output (parser:make-defpackage program-package))
(print-form output `(in-package ,package-name))
;; coalton-impl/codegen/program:compile-translation-unit wraps
;; definitions in progn to provide a single expression as the
;; macroexpansion of coalton-toplevel: unwrap for better
;; readability
(dolist (form (cdr program-text))
(print-form output form package-name))))))
(defun codegen (stream name)
"Compile Coalton source from STREAM and return Lisp program text. NAME may be the filename related to the input stream."
(with-output-to-string (output)
(compile-to-lisp stream name output)))
(defun compile (stream name &key (load t) (output-file nil))
"Compile Coalton code in STREAM, returning the pathname of the generated .fasl file. If OUTPUT-FILE is nil, the built-in compiler default output location will be used."
(uiop:with-temporary-file (:stream lisp-stream
:pathname lisp-file
:type "lisp"
:direction ':output)
(compile-to-lisp stream name lisp-stream)
:close-stream
(cond ((null output-file)
(setf output-file (compile-file lisp-file)))
(t
(compile-file lisp-file :output-file output-file)))
(when load
(load output-file))
output-file))