Debian Codegraph @

src/cmp/cmppass1-var.lsp

raw
1 ;;;;
2 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
3 ;;;; Copyright (c) 1990, Giuseppe Attardi
4 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
5 ;;;; Copyright (c) 2021, Daniel Kochmański
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8 ;;;;
9
10 (in-package #:compiler)
11
12 (defun c1let (args)
13 (check-args-number 'LET args 1)
14 (let ((bindings (pop args)))
15 (cond ((null bindings)
16 (c1locally args))
17 ((atom bindings)
18 (invalid-let-bindings 'LET bindings))
19 ((null (rest bindings))
20 (c1let/let* 'let* bindings args))
21 (t
22 (loop :with temp
23 :for b :in bindings
24 :if (atom b)
25 :collect b :into real-bindings :and
26 :collect b :into names
27 :else
28 :collect (setf temp (gensym "LET")) :into temp-names :and
29 :collect (cons temp (cdr b)) :into temp-bindings :and
30 :collect (list (car b) temp) :into real-bindings :and
31 :collect (car b) :into names
32 :do
33 (cmpck (member (car names) (cdr names) :test #'eq)
34 "LET: The variable ~s occurs more than once in the LET."
35 (car names))
36 :finally
37 (return (c1let/let* 'let*
38 (nconc temp-bindings real-bindings)
39 `((declare (ignorable ,@temp-names)
40 (:read-only ,@temp-names))
41 ,@args)))))
42 (t
43 (c1let/let* 'let bindings args)))))
44
45 (defun c1let* (args)
46 (check-args-number 'LET* args 1)
47 (let ((bindings (pop args)))
48 (cond ((null bindings)
49 (c1locally args))
50 ((atom bindings)
51 (invalid-let-bindings 'LET* bindings))
52 (t
53 (c1let/let* 'let* bindings args)))))
54
55 ;; Processing of a let form is split in two stages:
56 ;; - processing bindings
57 ;; - processing the body
58 ;; This allows reusing the below functions for inlined closures. These
59 ;; are transformed in a let statement for which the body needs to be
60 ;; compiled in a different lexical environment than the bindings.
61 (defun c1let/let* (let/let* bindings body)
62 (let* ((setjmps *setjmps*)
63 (*cmp-env* (cmp-env-copy)))
64 (multiple-value-bind (vars forms specials other-decls body)
65 (process-let-bindings let/let* bindings body)
66 (process-let-body let/let* vars forms specials other-decls body setjmps))))
67
68 (defun invalid-let-bindings (let/let* bindings)
69 (cmperr "Syntax error in ~A bindings:~%~4I~A"
70 let/let* bindings))
71
72 (defun process-let-bindings (let/let* bindings body)
73 (multiple-value-bind (body specials types ignoreds other-decls)
74 (c1body body nil)
75 (let ((vars '())
76 (forms '()))
77 (do ((b bindings)
78 name form)
79 ((atom b)
80 (unless (null b)
81 (invalid-let-bindings let/let* bindings)))
82 (if (symbolp (setf form (pop b)))
83 (setf name form form nil)
84 (progn
85 (check-args-number "LET/LET* binding" form 1 2)
86 (setf name (first form) form (rest form))))
87 (let* ((var (c1make-var name specials ignoreds types))
88 (type (var-type var))
89 (init (cond ((null form)
90 (default-init var))
91 ((trivial-type-p type)
92 (c1expr (first form)))
93 (t
94 (c1expr `(ext:checked-value ,type ,(first form)))))))
95 ;; :read-only variable handling. Beppe
96 (when (read-only-variable-p name other-decls)
97 (if (global-var-p var)
98 (cmpwarn "Found :READ-ONLY declaration for global var ~A"
99 name)
100 (setf (var-type var) (c1form-primary-type init)))
101 (multiple-value-bind (constantp value)
102 (c1form-constant-p init)
103 (when constantp
104 (cmp-env-register-symbol-macro name `',value)
105 (setf var nil))))
106 (when var
107 (push var vars)
108 (push init forms)
109 (when (eq let/let* 'LET*)
110 (cmp-env-register-var var)))))
111 (setf vars (nreverse vars)
112 forms (nreverse forms))
113 (when (eq let/let* 'LET)
114 (mapc #'cmp-env-register-var vars))
115 (check-vdecl (mapcar #'var-name vars) types ignoreds)
116 (values vars forms specials other-decls body))))
117
118 (defun process-let-body (let/let* vars forms specials other-decls body setjmps)
119 (mapc #'declare-special specials)
120 (setf body (c1decl-body other-decls body))
121 ;; Try eliminating unused variables, replace constant ones, etc.
122 (multiple-value-setq (vars forms)
123 (c1let-optimize-read-only-vars vars forms body))
124 ;; Verify that variables are referenced and assign final boxed / unboxed type
125 (mapc #'check-vref vars)
126 (make-c1form* let/let*
127 :type (c1form-type body)
128 :volatile (not (eql setjmps *setjmps*))
129 :local-vars vars
130 :args vars forms body))
131
132 (defun c1let-optimize-read-only-vars (all-vars all-forms body)
133 (loop with base = (list body)
134 for vars on all-vars
135 for forms on (nconc all-forms (list body))
136 for var = (first vars)
137 for form = (first forms)
138 for rest-vars = (cdr vars)
139 for rest-forms = (cdr forms)
140 for read-only-p = (and (null (var-set-nodes var))
141 (null (var-functions-reading var))
142 (null (var-functions-setting var))
143 (not (global-var-p var)))
144 when read-only-p
145 do (fix-read-only-variable-type var form)
146 unless (and read-only-p
147 (or (c1let-unused-variable-p var form)
148 (c1let-constant-value-p var form)
149 (c1let-constant-variable-p var form rest-vars rest-forms)
150 #+(or)
151 (c1let-can-move-variable-value-p var form rest-vars rest-forms)))
152 collect var into used-vars and
153 collect form into used-forms
154 finally (return (values used-vars used-forms))))
155
156 (defun fix-read-only-variable-type (var form)
157 (and-form-type (var-type var) form (var-name var) :unsafe "In LET body")
158 (let ((form-type (c1form-primary-type form)))
159 (setf (var-type var) form-type)
160 (update-variable-type var form-type)))
161
162 (defun c1let-unused-variable-p (var form)
163 ;; * (let ((v2 e2)) e3 e4) => (let () e3 e4)
164 ;; provided
165 ;; - v2 does not appear in body
166 ;; - e2 produces no side effects
167 (when (and (= 0 (var-ref var))
168 (not (member (var-kind var) '(special global)))
169 (not (c1form-side-effects form)))
170 (unless (var-ignorable var)
171 (cmpdebug "Removing unused variable ~A" (var-name var)))
172 (delete-c1forms form)
173 t))
174
175 (defun c1let-constant-value-p (var form)
176 ;; (let ((v1 e1) (v2 e2) (v3 e3)) (expr e4 v2 e5))
177 ;; - v2 is a read only variable
178 ;; - the value of e2 is not modified in e3 nor in following expressions
179 (when (and (eq (c1form-name form) 'LOCATION)
180 (loc-in-c1form-movable-p (c1form-arg 0 form)))
181 (cmpdebug "Replacing variable ~A by its value ~A" (var-name var) form)
182 (nsubst-var var form)
183 t))
184
185 (defun c1let-constant-variable-p (var form rest-vars rest-forms)
186 ;; (let ((v1 e1) (v2 e2) (v3 e3)) (expr e4 v2 e5))
187 ;; - v2 is a read only variable
188 ;; - the value of e2 is not modified in e3 nor in following expressions
189 (when (eq (c1form-name form) 'VARIABLE)
190 (let ((other-var (c1form-arg 0 form)))
191 (unless (or (global-var-p other-var)
192 (member other-var rest-vars)
193 (var-changed-in-form-list other-var rest-forms))
194 (cmpdebug "Replacing variable ~A by its value ~A" (var-name var) form)
195 (nsubst-var var form)
196 t))))
197
198 (defun c1let-can-move-variable-value-p (var form rest-vars rest-forms)
199 ;; (let ((v1 e1) (v2 e2) (v3 e3)) (expr e4 v2 e5))
200 ;; can become
201 ;; (let ((v1 e1) (v3 e3)) (expr e4 e2 e5))
202 ;; provided
203 ;; - v2 appears only once
204 ;; - v2 appears only in body
205 ;; - e2 does not affect v1 nor e3, e3 does not affect e2
206 ;; - e4 does not affect e2
207 (when (and (= 1 (var-ref var))
208 (not (c1form-side-effects form))
209 ;; it does not refer to special variables which
210 ;; are changed in the LET form
211 (notany #'(lambda (v) (var-referenced-in-form v form)) rest-vars)
212 (replaceable var rest-forms))
213 (cmpdebug "Replacing variable ~A by its value ~A" (var-name var) form)
214 (nsubst-var var form)
215 t))
216
217 (defun c1make-var (name specials ignores types)
218 (cmpck (not (symbolp name)) "The variable ~s is not a symbol." name)
219 (cmpck (constant-variable-p name) "The constant ~s is being bound." name)
220 (let ((ignorable (cdr (assoc name ignores)))
221 (kind 'LEXICAL) ; we rely on check-vref to fix it
222 (type (assoc name types)))
223 (cond ((null type)
224 (setq type 'T))
225 ((machine-c-type-p (setq type (cdr type)))
226 (setf kind type
227 type (host-type->lisp-type type))))
228 (cond ((or (member name specials) (special-variable-p name))
229 (unless (eq kind 'LEXICAL)
230 (cmperr "Special variable ~A cannot be declared to have C type ~A"
231 name type))
232 (when (eq type 'T)
233 (setf type (or (si:get-sysprop name 'CMP-TYPE) 'T)))
234 (make-global-var name :kind 'SPECIAL :type type))
235 (t
236 (make-var :name name :type type :loc 'OBJECT
237 :kind kind :ignorable ignorable
238 :ref 0)))))
239
240 ;;; When LOC is not NIL then we deal with a constant.
241 (defun c1variable (name loc)
242 (let* ((var (c1vref name))
243 (output (make-c1form* 'VARIABLE
244 :type (var-type var)
245 :args var loc)))
246 (add-to-read-nodes var output)
247 output))
248
249 ;;; A variable reference (vref for short) is a list: pair
250 ;;; ( var-object ) Beppe(ccb) ccb-reference )
251
252 (defun c1vref (name)
253 (multiple-value-bind (var cfb unw)
254 (cmp-env-search-var name)
255 (declare (ignore unw))
256 (cond ((null var)
257 (make-global-var name :warn t
258 :type (or (si:get-sysprop name 'CMP-TYPE) t)))
259 ((not (var-p var))
260 ;; symbol-macrolet
261 (baboon :format-control "c1vref: ~s is not a variable."
262 :format-arguments (list name)))
263 (t
264 (case (var-kind var)
265 ((SPECIAL GLOBAL))
266 ((CLOSURE))
267 ((LEXICAL)
268 (when cfb
269 (setf (var-ref-clb var) t)))
270 (t
271 (when cfb
272 (cmperr "Variable ~A declared of C type cannot be referenced across function boundaries."
273 (var-name var)))))
274 var))))
275
276 (defun c1setq (args)
277 (let ((l (length args)))
278 (cmpck (oddp l) "SETQ requires an even number of arguments.")
279 (cond ((zerop l) (c1nil))
280 ((= l 2) (c1setq1 (first args) (second args)))
281 (t
282 (c1progn
283 (loop while args
284 collect `(setq ,(pop args) ,(pop args))))))))
285
286 (defun c1setq1 (name form)
287 (cmpck (not (symbolp name)) "The variable ~s is not a symbol." name)
288 (cmpck (constantp name) "The constant ~s is being assigned a value." name)
289 (setq name (chk-symbol-macrolet name))
290 (if (symbolp name)
291 (let* ((name (c1vref name))
292 (type (var-type name))
293 (form (c1expr (if (trivial-type-p type)
294 form
295 `(ext:checked-value ,type ,form)))))
296 (add-to-set-nodes name (make-c1form* 'SETQ
297 :type (c1form-type form)
298 :args name form)))
299 `(setf ,name ,form)))
300
301 (defun c1progv (args)
302 (check-args-number 'PROGV args 2)
303 (let ((symbols (c1expr (first args)))
304 (values (c1expr (second args)))
305 (forms (c1progn (cddr args))))
306 (make-c1form* 'PROGV :type (c1form-type forms)
307 :args symbols values forms)))
308
309 (defun c1psetq (old-args &aux (args nil) (use-psetf nil))
310 ;; A first pass ensures that none of the assigned locations is
311 ;; a SETF form. Otherwise we have to resort to PSETF.
312 (do ((l old-args))
313 ((endp l))
314 (let ((var (pop l)))
315 (cmpck (not (symbolp var))
316 "The variable ~s is not a symbol." var)
317 (cmpck (endp l)
318 "No form was given for the value of ~s." var)
319 (setq var (chk-symbol-macrolet var))
320 (setq args (nconc args (list var (pop l))))
321 (if (symbolp var)
322 (cmpck (constantp var)
323 "The constant ~s is being assigned a value." var)
324 (setq use-psetf t))))
325 (when use-psetf
326 (return-from c1psetq `(psetf ,@args)))
327 ;; In the second pass we compile the variable references and the
328 ;; assignments. Here we may need to create checked values if the
329 ;; variables have been proclaimed.
330 (do ((vrefs '())
331 (forms '()))
332 ((endp args)
333 (add-to-set-nodes-of-var-list
334 vrefs (make-c1form* 'PSETQ :type '(MEMBER NIL)
335 :args (reverse vrefs) (nreverse forms))))
336 (let* ((vref (c1vref (pop args)))
337 (type (var-type vref))
338 (form (pop args)))
339 (push vref vrefs)
340 (push (c1expr (if (trivial-type-p type)
341 form
342 `(ext:checked-value ,type ,form)))
343 forms))))
344
345 (defun c1multiple-value-bind (args)
346 (check-args-number 'MULTIPLE-VALUE-BIND args 2)
347 (let* ((*cmp-env* (cmp-env-copy))
348 (variables (pop args))
349 (init-form (pop args)))
350 (when (= (length variables) 1)
351 (return-from c1multiple-value-bind
352 `(let* ((,(first variables) ,init-form))
353 ,@args)))
354 (multiple-value-bind (body ss ts is other-decls)
355 (c1body args nil)
356 (mapc #'declare-special ss)
357 (let* ((vars (loop for name in variables
358 collect (c1make-var name ss is ts))))
359 (setq init-form (c1expr init-form))
360 (mapc #'cmp-env-register-var vars)
361 (check-vdecl variables ts is)
362 (setq body (c1decl-body other-decls body))
363 (mapc #'check-vref vars)
364 (make-c1form* 'MULTIPLE-VALUE-BIND :type (c1form-type body)
365 :local-vars vars
366 :args vars init-form body)))))
367
368 (defun c1multiple-value-setq (args &aux (vars nil) (value-bindings nil)
369 (storing-forms nil) (return-value nil))
370 (check-args-number 'MULTIPLE-VALUE-SETQ args 2 2)
371 (dolist (var (reverse (first args)))
372 ;; check for symbol-macrolets and insert type checks if necessary
373 (cmpck (not (symbolp var)) "The variable ~s is not a symbol." var)
374 (let* ((var-or-form (chk-symbol-macrolet var))
375 (type t))
376 (if (symbolp var-or-form)
377 (progn
378 (cmpck (constantp var-or-form)
379 "The constant ~s is being assigned a value." var-or-form)
380 (setf return-value var-or-form)
381 (if (or (not (policy-type-assertions))
382 (trivial-type-p
383 (setf type (variable-type-in-env var-or-form))))
384 (push var-or-form vars)
385 (let ((new-var (gensym)))
386 (push new-var vars)
387 (push new-var value-bindings)
388 (push `(setf ,var-or-form (ext:checked-value ,type ,new-var)) storing-forms))))
389 (multiple-value-bind (setf-vars setf-vals stores storing-form get-form)
390 (get-setf-expansion var-or-form *cmp-env*)
391 (push (first stores) vars)
392 (setf value-bindings (nconc (mapcar #'list setf-vars setf-vals)
393 stores
394 value-bindings))
395 (push storing-form storing-forms)
396 (setf return-value get-form)))))
397 (let ((value (second args)))
398 (cond (value-bindings
399 `(let* (,@value-bindings)
400 (multiple-value-setq ,vars ,value)
401 ,@storing-forms
402 ,return-value))
403 ((endp vars)
404 `(values ,value))
405 ((= (length vars) 1)
406 `(setq ,(first vars) ,value))
407 (t
408 (setq value (c1expr value)
409 vars (mapcar #'c1vref vars))
410 (add-to-set-nodes-of-var-list
411 vars (make-c1form* 'MULTIPLE-VALUE-SETQ :args vars value))))))