Debian Codegraph @

src/cmp/cmppass1-fun.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 c1labels (args) (c1labels/flet 'LABELS args))
13
14 (defun c1flet (args) (c1labels/flet 'FLET args))
15
16 (defun c1labels/flet (origin args)
17 (check-args-number origin args 1)
18 (let ((new-env (cmp-env-copy))
19 (defs '())
20 (local-funs '())
21 (fnames '())
22 body-c1form)
23 ;; On a first round, we extract the definitions of the functions,
24 ;; and build empty function objects that record the references to
25 ;; this functions in the processed body. In the end
26 ;; DEFS = ( { ( fun-object function-body ) }* ).
27 (dolist (def (car args))
28 (cmpck (or (endp def)
29 (not (si::valid-function-name-p (car def)))
30 (endp (cdr def)))
31 "The local function definition ~s is illegal." def)
32 (cmpck (member (car def) fnames)
33 "~s: The function ~s was already defined." origin (car def))
34 (push (car def) fnames)
35 (let* ((name (car def))
36 (var (make-var :name (gensym) :kind :object))
37 (fun (make-fun :name name :var var)))
38 (cmp-env-register-function fun new-env)
39 (push (cons fun (cdr def)) defs)))
40
41 ;; Now we compile the functions, either in the current environment
42 ;; or in an empty environment in which there are no new functions
43 (let ((*cmp-env* (cmp-env-copy (if (eq origin 'FLET) *cmp-env* new-env))))
44 (dolist (def (nreverse defs))
45 (let ((fun (first def)))
46 ;; The closure type will be fixed later on by COMPUTE-...
47 (push (c1compile-function (rest def) :fun fun) local-funs))))
48
49 ;; When we are in a LABELs form, we have to propagate the external
50 ;; variables from one function to the other functions that use it.
51 (when (eq origin 'LABELS)
52 (loop for change = nil
53 do (loop for f1 in local-funs
54 for vars = (fun-referenced-vars f1)
55 for funs = (fun-referenced-funs f1)
56 do (loop for f2 in (fun-referencing-funs f1)
57 for c1 = (add-to-fun-referenced-vars f2 vars)
58 for c2 = (add-to-fun-referenced-funs f2 funs)
59 for c3 = (update-fun-closure-type f2)
60 when (or c1 c2 c3)
61 do (setf change t)))
62 do (unless change (return))))
63
64 ;; Now we can compile the body itself.
65 (let ((*cmp-env* new-env))
66 (multiple-value-bind (body ss ts is other-decl)
67 (c1body (rest args) t)
68 (mapc #'declare-special ss)
69 (check-vdecl nil ts is)
70 (setq body-c1form (c1decl-body other-decl body))))
71
72 ;; Keep only functions that have been referenced at least once.
73 ;; It is not possible to look at FUN-REF before because functions
74 ;; in a LABELS can reference each other.
75 (setf local-funs (remove-if-not #'plusp local-funs :key #'fun-ref))
76
77 ;; Keep on inspecting the functions until the closure type does not
78 ;; change.
79 (loop while
80 (let ((x nil))
81 (loop for f in local-funs
82 when (update-fun-closure-type f)
83 do (setf x t))
84 x))
85
86 (if local-funs
87 (make-c1form* 'LOCALS :type (c1form-type body-c1form)
88 :args local-funs body-c1form (eq origin 'LABELS))
89 body-c1form)))
90
91 ;;; During Pass1, a lambda-list
92 ;;;
93 ;;; ( { var }*
94 ;;; [ &optional { var | ( var [ initform [ svar ] ] ) }* ]
95 ;;; [ &rest var ]
96 ;;; [ &key { var | ( { var | ( kwd var ) } [initform [ svar ]])}*
97 ;;; [&allow-other-keys]]
98 ;;; [ &aux {var | (var [initform])}*]
99 ;;; )
100 ;;;
101 ;;; is transformed into
102 ;;;
103 ;;; ( ( { var }* ) ; required
104 ;;; ( { var initform svar }* ) ; optional
105 ;;; { var | nil } ; rest
106 ;;; allow-other-keys-flag
107 ;;; ( { kwd-vv-index var initform svar }* ) ; key
108 ;;; )
109 ;;;
110 ;;; where
111 ;;; svar: NIL ; means svar is not supplied
112 ;;; | var
113 ;;;
114 ;;; &aux parameters will be embedded into LET*.
115 ;;;
116 ;;; c1lambda-expr receives
117 ;;; ( lambda-list { doc | decl }* . body )
118 ;;; and returns
119 ;;; ( lambda info-object lambda-list' doc body' )
120 ;;;
121 ;;; Doc is NIL if no doc string is supplied.
122 ;;; Body' is body possibly surrounded by a LET* (if &aux parameters are
123 ;;; supplied) and an implicit block.
124
125 (defun c1compile-function (lambda-list-and-body
126 &key (fun (make-fun)) (name (fun-name fun)))
127 (let ((lambda (if name
128 `(ext:lambda-block ,name ,@lambda-list-and-body)
129 `(lambda ,@lambda-list-and-body))))
130 (setf (fun-name fun) name
131 (fun-lambda-expression fun) lambda
132 (fun-parent fun) *current-function*))
133 (when *current-function*
134 (push fun (fun-child-funs *current-function*)))
135 (let* ((*current-function* fun)
136 (*cmp-env* (setf (fun-cmp-env fun) (cmp-env-mark 'SI:FUNCTION-BOUNDARY)))
137 (setjmps *setjmps*)
138 (decl (si:process-declarations (rest lambda-list-and-body)))
139 (global (and *use-c-global*
140 (assoc 'SI::C-GLOBAL decl)
141 (setf (fun-global fun) T)))
142 (no-entry (assoc 'SI::C-LOCAL decl))
143 cfun exported minarg maxarg proclamation-found-p)
144 (multiple-value-bind (lambda-expr optional-type-checks keyword-type-checks)
145 (c1lambda-expr lambda-list-and-body name
146 (si:function-block-name name))
147 (when (and no-entry (policy-debug-ihs-frame))
148 (setf no-entry nil)
149 (cmpnote "Ignoring SI::C-LOCAL declaration for~%~4I~A~%because the debug level is large" name))
150 (unless (eql setjmps *setjmps*)
151 (setf (c1form-volatile lambda-expr) t))
152 (setf (fun-lambda fun) lambda-expr)
153 (if global
154 (multiple-value-setq (cfun exported) (exported-fname name))
155 (setf cfun (next-cfun "LC~D~A" name) exported nil))
156 (if exported
157 ;; Check whether the function was proclaimed to have a certain
158 ;; number of arguments, and otherwise produce a function with
159 ;; a flexible signature.
160 (progn
161 (multiple-value-setq (minarg maxarg proclamation-found-p)
162 (get-proclaimed-narg name))
163 (cmpdebug "~&;;; Function ~A proclaimed (~A,~A)" name minarg maxarg)
164 (multiple-value-bind (found-minarg found-maxarg)
165 (lambda-form-allowed-nargs lambda-expr)
166 (if proclamation-found-p
167 ;; sanity check that the proclamation matches the actual
168 ;; number of arguments found
169 (when (or (/= minarg found-minarg) (/= maxarg found-maxarg))
170 (cmperr "Function ~A takes between ~A and ~A arguments, but is proclaimed to take between ~A and ~A arguments"
171 name found-minarg found-maxarg minarg maxarg))
172 ;; no proclamation found, produce function with
173 ;; flexible signature
174 (setf minarg found-minarg))))
175 (multiple-value-setq (minarg maxarg)
176 (lambda-form-allowed-nargs lambda-expr)))
177 #+ecl-min
178 (when exported
179 (multiple-value-bind (foundp ignored minarg-core maxarg-core)
180 (si::mangle-name name)
181 (declare (ignore ignored))
182 (when foundp
183 ;; Core functions declared in symbols_list.h are
184 ;; initialized during startup in all_symbols.d
185 (setf no-entry t)
186 ;; Sanity check that the number of arguments is consistent
187 ;; with the declaration in symbols_list.h. Note that the
188 ;; information about the maximum number of arguments for
189 ;; variadic functions is missing from this declaration.
190 (when (or (/= minarg minarg-core)
191 (and (= maxarg minarg)
192 (/= maxarg maxarg-core)))
193 (cmperr "Function ~A takes between ~A and ~A arguments, but is declared to take between ~A and ~A arguments in symbols_list.h"
194 name minarg maxarg minarg-core maxarg-core)))))
195 (setf (fun-cfun fun) cfun
196 (fun-exported fun) exported
197 (fun-closure fun) nil
198 (fun-minarg fun) minarg
199 (fun-maxarg fun) maxarg
200 (fun-description fun) (or (function-block-name-declaration decl) name)
201 (fun-no-entry fun) no-entry
202 (fun-optional-type-check-forms fun) optional-type-checks
203 (fun-keyword-type-check-forms fun) keyword-type-checks)
204 (loop for child in (fun-child-funs fun)
205 do (add-to-fun-referenced-vars fun (fun-referenced-vars child))
206 do (add-to-fun-referenced-funs fun (fun-referenced-funs child)))
207 (loop for f in (fun-referenced-funs fun)
208 do (add-to-fun-referenced-vars fun (fun-referenced-vars f)))
209 (update-fun-closure-type fun)
210 (when global
211 (if (fun-closure fun)
212 (cmpnote "Function ~A is global but is closed over some variables.~%~{~A ~}"
213 (fun-name fun) (mapcar #'var-name (fun-referenced-vars fun)))
214 (push fun *global-funs*))))
215 fun))
216
217 (defun c1lambda-expr (lambda-expr function-name block-name
218 &aux doc body ss is ts
219 other-decls
220 (type-checks '())
221 (*permanent-data* t)
222 (old-env *cmp-env*)
223 (*cmp-env* (cmp-env-copy)))
224 (declare (si::c-local))
225
226 (cmpck (endp lambda-expr)
227 "The lambda expression ~s is illegal." (cons 'LAMBDA lambda-expr))
228
229 (multiple-value-setq (body ss ts is other-decls doc)
230 (c1body (cdr lambda-expr) t))
231
232 (when block-name (setq body (list (cons 'BLOCK (cons block-name body)))))
233
234 (multiple-value-bind (requireds optionals rest key-flag keywords
235 allow-other-keys aux-vars)
236 (cmp-process-lambda-list (car lambda-expr))
237
238 (do ((specs (setq requireds (cdr requireds)) (cdr specs)))
239 ((endp specs))
240 (let* ((name (first specs))
241 (var (c1make-var name ss is ts)))
242 (push var type-checks)
243 (setf (first specs) var)
244 (cmp-env-register-var var)))
245
246 (do ((specs (setq optionals (cdr optionals)) (cdddr specs)))
247 ((endp specs))
248 (let* ((name (first specs))
249 (var (c1make-var name ss is ts))
250 (init (second specs))
251 (flag (third specs)))
252 (setq init (if init
253 (and-form-type (var-type var) (c1expr init) init
254 :safe "In (LAMBDA ~a...)" function-name)
255 (default-init var)))
256 (push var type-checks)
257 (cmp-env-register-var var)
258 (when flag
259 (setq flag (c1make-var flag ss is ts))
260 (cmp-env-register-var flag))
261 (setf (first specs) var
262 (second specs) init
263 (third specs) flag)))
264
265 (when rest
266 (setq rest (c1make-var rest ss is ts))
267 (cmp-env-register-var rest))
268
269 (do ((specs (setq keywords (cdr keywords)) (cddddr specs)))
270 ((endp specs))
271 (let* ((key (first specs))
272 (name (second specs))
273 (var (c1make-var name ss is ts))
274 (init (third specs))
275 (flag (fourth specs)))
276 (declare (ignore key))
277 (setq init (if init
278 (and-form-type (var-type var) (c1expr init) init
279 :safe "In (LAMBDA ~a...)" function-name)
280 (default-init var)))
281 (push var type-checks)
282 (cmp-env-register-var var)
283 (when flag
284 (setq flag (c1make-var flag ss is ts))
285 (cmp-env-register-var flag))
286 (setf (second specs) var
287 (third specs) init
288 (fourth specs) flag)))
289
290 ;; Make other declarations take effect right now
291 (setf *cmp-env* (reduce #'add-one-declaration other-decls
292 :initial-value *cmp-env*))
293
294 ;; After creating all variables and processing the initalization
295 ;; forms, we will process the body. However, all free declarations,
296 ;; that is declarations which do not refer to the function
297 ;; arguments, have to be applied to the body. At the same time, we
298 ;; replace &aux variables with a LET* form that defines them.
299 (multiple-value-bind (required-type-check-forms optional-type-check-forms keyword-type-check-forms new-auxs)
300 (extract-lambda-type-checks function-name requireds optionals
301 keywords ts other-decls)
302 (let* ((declarations other-decls)
303 (let-vars (loop for spec on (nconc new-auxs (rest aux-vars))
304 by #'cddr
305 for name = (first spec)
306 for init = (second spec)
307 collect (list name init)))
308 (new-variables (cmp-env-new-variables *cmp-env* old-env))
309 (already-declared-names (set-difference (mapcar #'var-name new-variables)
310 (mapcar #'car let-vars))))
311 ;; Gather declarations for &aux variables, either special...
312 (let ((specials (set-difference ss already-declared-names)))
313 (when specials
314 (push `(special ,@specials) declarations)))
315 ;; ...ignorable...
316 (let ((ignorables (loop for (var . expected-uses) in is
317 unless (member var already-declared-names)
318 collect var)))
319 (when ignorables
320 (push `(ignorable ,@ignorables) declarations)))
321 ;; ...or type declarations
322 (loop for (var . type) in ts
323 unless (member var already-declared-names)
324 do (push `(type ,type ,var) declarations))
325 ;; ...create the enclosing LET* form for the &aux variables
326 (when (or let-vars declarations)
327 (setq body `((let* ,let-vars
328 (declare ,@declarations)
329 ,@body))))
330 ;; ...wrap around the type checks for required variables, removing
331 ;; unnecessary nil's
332 (setq body (nconc (delete nil required-type-check-forms) body))
333 ;; ...now finally compile the body with the type checks
334 (let ((*cmp-env* (cmp-env-copy *cmp-env*)))
335 (setf body (c1progn body)))
336 ;;
337 ;; ...and verify whether all variables are used.
338 (dolist (var new-variables)
339 (check-vref var))
340 (values (make-c1form* 'LAMBDA
341 :local-vars new-variables
342 :args (list requireds optionals rest key-flag keywords
343 allow-other-keys)
344 doc body)
345 (mapcar #'(lambda (x) (if x (c1expr x) nil))
346 optional-type-check-forms)
347 (mapcar #'(lambda (x) (if x (c1expr x) nil))
348 keyword-type-check-forms))))))
349
350 (defun cmp-process-lambda-list (list)
351 (handler-case (si:process-lambda-list list 'function)
352 (error (c) (cmperr "Illegal lambda list ~S:~%~A" list c))))
353
354 (defun lambda-form-allowed-nargs (lambda)
355 (let ((minarg 0)
356 (maxarg call-arguments-limit))
357 (destructuring-bind (requireds optionals rest key-flag keywords a-o-k)
358 (c1form-arg 0 lambda)
359 (declare (ignore keywords))
360 (setf minarg (length requireds))
361 (when (and (null rest) (not key-flag) (not a-o-k))
362 (setf maxarg (+ minarg (/ (length optionals) 3)))))
363 (values minarg maxarg)))