-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathentry.lisp
261 lines (219 loc) · 11.4 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
(defpackage #:coalton-impl/entry
(:use
#:cl)
(:shadow
#:compile)
(:local-nicknames
(#:settings #:coalton-impl/settings)
(#:util #:coalton-impl/util)
(#:parser #:coalton-impl/parser)
(#:source #:coalton-impl/source)
(#: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))
(env *global-environment*))
(multiple-value-bind (type-definitions instances env)
(tc:toplevel-define-type (parser:program-types program)
(parser:program-structs program)
(parser:program-type-aliases program)
env)
(let ((all-instances (append instances (parser:program-instances program))))
(multiple-value-bind (class-definitions env)
(tc:toplevel-define-class (parser:program-classes program)
env)
(multiple-value-bind (ty-instances env)
(tc:toplevel-define-instance all-instances env)
(multiple-value-bind (toplevel-definitions env)
(tc:toplevel-define (parser:program-defines program)
(parser:program-declares program)
env)
(multiple-value-bind (toplevel-instances)
(tc:toplevel-typecheck-instance ty-instances
all-instances
env)
(setf env (tc:toplevel-specialize (parser:program-specializations program) env))
(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
:lisp-forms (parser:program-lisp-forms program)
: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)
(codegen:compile-translation-unit translation-unit monomorphize-table env))))))))))
(defun expression-entry-point (node)
(declare (type parser:node node))
(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))
(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 env)
(setf subs (tc:compose-substitution-lists subs subs_))
(when accessors
(tc:tc-error "Ambiguous accessor"
(source:note (first accessors)
"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))
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))))
(tc:tc-error "Unable to codegen"
(tc:tc-note node
"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))
(tc:tc-note node
"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-circle* t allows gensym-generated, uninterned
;; *symbols to serve as variables in readable source.
(*print-circle* t)
(*print-pretty* t)
(*print-right-margin* 80))
(prin1 form stream)
(terpri stream)
(terpri stream))))
(defun compile-to-lisp (source output)
"Read Coalton source from SOURCE and write Lisp source to OUTPUT. NAME may be the filename related to the input stream."
(declare (optimize (debug 3)))
(with-open-stream (stream (source:source-stream source))
(parser:with-reader-context stream
(with-environment-updates updates
(let* ((program (parser:read-program stream source ':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 (source)
"Compile Coalton source from SOURCE and return Lisp program text. NAME may be the filename related to the input stream."
(with-output-to-string (output)
(compile-to-lisp source output)))
(defun compile (source &key (load t) (output-file nil))
"Compile Coalton code in SOURCE, 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 source 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))