0.9.2.45:
[sbcl/lichteblau.git] / src / compiler / ir1-translators.lisp
blob88f8ff3f5f80f6feea06f3f27406e6e42c8a08ec
1 ;;;; the usual place for DEF-IR1-TRANSLATOR forms (and their
2 ;;;; close personal friends)
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
13 (in-package "SB!C")
15 ;;;; special forms for control
17 (def-ir1-translator progn ((&rest forms) start next result)
18 #!+sb-doc
19 "Progn Form*
20 Evaluates each Form in order, returning the values of the last form. With no
21 forms, returns NIL."
22 (ir1-convert-progn-body start next result forms))
24 (def-ir1-translator if ((test then &optional else) start next result)
25 #!+sb-doc
26 "If Predicate Then [Else]
27 If Predicate evaluates to non-null, evaluate Then and returns its values,
28 otherwise evaluate Else and return its values. Else defaults to NIL."
29 (let* ((pred-ctran (make-ctran))
30 (pred-lvar (make-lvar))
31 (then-ctran (make-ctran))
32 (then-block (ctran-starts-block then-ctran))
33 (else-ctran (make-ctran))
34 (else-block (ctran-starts-block else-ctran))
35 (node (make-if :test pred-lvar
36 :consequent then-block
37 :alternative else-block)))
38 ;; IR1-CONVERT-MAYBE-PREDICATE requires DEST to be CIF, so the
39 ;; order of the following two forms is important
40 (setf (lvar-dest pred-lvar) node)
41 (ir1-convert start pred-ctran pred-lvar test)
42 (link-node-to-previous-ctran node pred-ctran)
44 (let ((start-block (ctran-block pred-ctran)))
45 (setf (block-last start-block) node)
46 (ctran-starts-block next)
48 (link-blocks start-block then-block)
49 (link-blocks start-block else-block))
51 (ir1-convert then-ctran next result then)
52 (ir1-convert else-ctran next result else)))
54 ;;;; BLOCK and TAGBODY
56 ;;;; We make an ENTRY node to mark the start and a :ENTRY cleanup to
57 ;;;; mark its extent. When doing GO or RETURN-FROM, we emit an EXIT
58 ;;;; node.
60 ;;; Make a :ENTRY cleanup and emit an ENTRY node, then convert the
61 ;;; body in the modified environment. We make NEXT start a block now,
62 ;;; since if it was done later, the block would be in the wrong
63 ;;; environment.
64 (def-ir1-translator block ((name &rest forms) start next result)
65 #!+sb-doc
66 "Block Name Form*
67 Evaluate the Forms as a PROGN. Within the lexical scope of the body,
68 (RETURN-FROM Name Value-Form) can be used to exit the form, returning the
69 result of Value-Form."
70 (unless (symbolp name)
71 (compiler-error "The block name ~S is not a symbol." name))
72 (start-block start)
73 (ctran-starts-block next)
74 (let* ((dummy (make-ctran))
75 (entry (make-entry))
76 (cleanup (make-cleanup :kind :block
77 :mess-up entry)))
78 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
79 (setf (entry-cleanup entry) cleanup)
80 (link-node-to-previous-ctran entry start)
81 (use-ctran entry dummy)
83 (let* ((env-entry (list entry next result))
84 (*lexenv* (make-lexenv :blocks (list (cons name env-entry))
85 :cleanup cleanup)))
86 (ir1-convert-progn-body dummy next result forms))))
88 (def-ir1-translator return-from ((name &optional value) start next result)
89 #!+sb-doc
90 "Return-From Block-Name Value-Form
91 Evaluate the Value-Form, returning its values from the lexically enclosing
92 BLOCK Block-Name. This is constrained to be used only within the dynamic
93 extent of the BLOCK."
94 ;; old comment:
95 ;; We make NEXT start a block just so that it will have a block
96 ;; assigned. People assume that when they pass a ctran into
97 ;; IR1-CONVERT as NEXT, it will have a block when it is done.
98 ;; KLUDGE: Note that this block is basically fictitious. In the code
99 ;; (BLOCK B (RETURN-FROM B) (SETQ X 3))
100 ;; it's the block which answers the question "which block is
101 ;; the (SETQ X 3) in?" when the right answer is that (SETQ X 3) is
102 ;; dead code and so doesn't really have a block at all. The existence
103 ;; of this block, and that way that it doesn't explicitly say
104 ;; "I'm actually nowhere at all" makes some logic (e.g.
105 ;; BLOCK-HOME-LAMBDA-OR-NULL) more obscure, and it might be better
106 ;; to get rid of it, perhaps using a special placeholder value
107 ;; to indicate the orphanedness of the code.
108 (declare (ignore result))
109 (ctran-starts-block next)
110 (let* ((found (or (lexenv-find name blocks)
111 (compiler-error "return for unknown block: ~S" name)))
112 (exit-ctran (second found))
113 (value-ctran (make-ctran))
114 (value-lvar (make-lvar))
115 (entry (first found))
116 (exit (make-exit :entry entry
117 :value value-lvar)))
118 (when (ctran-deleted-p exit-ctran)
119 (throw 'locall-already-let-converted exit-ctran))
120 (push exit (entry-exits entry))
121 (setf (lvar-dest value-lvar) exit)
122 (ir1-convert start value-ctran value-lvar value)
123 (link-node-to-previous-ctran exit value-ctran)
124 (let ((home-lambda (ctran-home-lambda-or-null start)))
125 (when home-lambda
126 (push entry (lambda-calls-or-closes home-lambda))))
127 (use-continuation exit exit-ctran (third found))))
129 ;;; Return a list of the segments of a TAGBODY. Each segment looks
130 ;;; like (<tag> <form>* (go <next tag>)). That is, we break up the
131 ;;; tagbody into segments of non-tag statements, and explicitly
132 ;;; represent the drop-through with a GO. The first segment has a
133 ;;; dummy NIL tag, since it represents code before the first tag. The
134 ;;; last segment (which may also be the first segment) ends in NIL
135 ;;; rather than a GO.
136 (defun parse-tagbody (body)
137 (declare (list body))
138 (collect ((segments))
139 (let ((current (cons nil body)))
140 (loop
141 (let ((tag-pos (position-if (complement #'listp) current :start 1)))
142 (unless tag-pos
143 (segments `(,@current nil))
144 (return))
145 (let ((tag (elt current tag-pos)))
146 (when (assoc tag (segments))
147 (compiler-error
148 "The tag ~S appears more than once in the tagbody."
149 tag))
150 (unless (or (symbolp tag) (integerp tag))
151 (compiler-error "~S is not a legal tagbody statement." tag))
152 (segments `(,@(subseq current 0 tag-pos) (go ,tag))))
153 (setq current (nthcdr tag-pos current)))))
154 (segments)))
156 ;;; Set up the cleanup, emitting the entry node. Then make a block for
157 ;;; each tag, building up the tag list for LEXENV-TAGS as we go.
158 ;;; Finally, convert each segment with the precomputed Start and Cont
159 ;;; values.
160 (def-ir1-translator tagbody ((&rest statements) start next result)
161 #!+sb-doc
162 "Tagbody {Tag | Statement}*
163 Define tags for used with GO. The Statements are evaluated in order
164 (skipping Tags) and NIL is returned. If a statement contains a GO to a
165 defined Tag within the lexical scope of the form, then control is transferred
166 to the next statement following that tag. A Tag must an integer or a
167 symbol. A statement must be a list. Other objects are illegal within the
168 body."
169 (start-block start)
170 (ctran-starts-block next)
171 (let* ((dummy (make-ctran))
172 (entry (make-entry))
173 (segments (parse-tagbody statements))
174 (cleanup (make-cleanup :kind :tagbody
175 :mess-up entry)))
176 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
177 (setf (entry-cleanup entry) cleanup)
178 (link-node-to-previous-ctran entry start)
179 (use-ctran entry dummy)
181 (collect ((tags)
182 (starts)
183 (ctrans))
184 (starts dummy)
185 (dolist (segment (rest segments))
186 (let* ((tag-ctran (make-ctran))
187 (tag (list (car segment) entry tag-ctran)))
188 (ctrans tag-ctran)
189 (starts tag-ctran)
190 (ctran-starts-block tag-ctran)
191 (tags tag)))
192 (ctrans next)
194 (let ((*lexenv* (make-lexenv :cleanup cleanup :tags (tags))))
195 (mapc (lambda (segment start end)
196 (ir1-convert-progn-body start end
197 (when (eq end next) result)
198 (rest segment)))
199 segments (starts) (ctrans))))))
201 ;;; Emit an EXIT node without any value.
202 (def-ir1-translator go ((tag) start next result)
203 #!+sb-doc
204 "Go Tag
205 Transfer control to the named Tag in the lexically enclosing TAGBODY. This
206 is constrained to be used only within the dynamic extent of the TAGBODY."
207 (ctran-starts-block next)
208 (let* ((found (or (lexenv-find tag tags :test #'eql)
209 (compiler-error "attempt to GO to nonexistent tag: ~S"
210 tag)))
211 (entry (first found))
212 (exit (make-exit :entry entry)))
213 (push exit (entry-exits entry))
214 (link-node-to-previous-ctran exit start)
215 (let ((home-lambda (ctran-home-lambda-or-null start)))
216 (when home-lambda
217 (push entry (lambda-calls-or-closes home-lambda))))
218 (use-ctran exit (second found))))
220 ;;;; translators for compiler-magic special forms
222 ;;; This handles EVAL-WHEN in non-top-level forms. (EVAL-WHENs in top
223 ;;; level forms are picked off and handled by PROCESS-TOPLEVEL-FORM,
224 ;;; so that they're never seen at this level.)
226 ;;; ANSI "3.2.3.1 Processing of Top Level Forms" says that processing
227 ;;; of non-top-level EVAL-WHENs is very simple:
228 ;;; EVAL-WHEN forms cause compile-time evaluation only at top level.
229 ;;; Both :COMPILE-TOPLEVEL and :LOAD-TOPLEVEL situation specifications
230 ;;; are ignored for non-top-level forms. For non-top-level forms, an
231 ;;; eval-when specifying the :EXECUTE situation is treated as an
232 ;;; implicit PROGN including the forms in the body of the EVAL-WHEN
233 ;;; form; otherwise, the forms in the body are ignored.
234 (def-ir1-translator eval-when ((situations &rest forms) start next result)
235 #!+sb-doc
236 "EVAL-WHEN (Situation*) Form*
237 Evaluate the Forms in the specified Situations (any of :COMPILE-TOPLEVEL,
238 :LOAD-TOPLEVEL, or :EXECUTE, or (deprecated) COMPILE, LOAD, or EVAL)."
239 (multiple-value-bind (ct lt e) (parse-eval-when-situations situations)
240 (declare (ignore ct lt))
241 (ir1-convert-progn-body start next result (and e forms)))
242 (values))
244 ;;; common logic for MACROLET and SYMBOL-MACROLET
246 ;;; Call DEFINITIONIZE-FUN on each element of DEFINITIONS to find its
247 ;;; in-lexenv representation, stuff the results into *LEXENV*, and
248 ;;; call FUN (with no arguments).
249 (defun %funcall-in-foomacrolet-lexenv (definitionize-fun
250 definitionize-keyword
251 definitions
252 fun)
253 (declare (type function definitionize-fun fun))
254 (declare (type (member :vars :funs) definitionize-keyword))
255 (declare (type list definitions))
256 (unless (= (length definitions)
257 (length (remove-duplicates definitions :key #'first)))
258 (compiler-style-warn "duplicate definitions in ~S" definitions))
259 (let* ((processed-definitions (mapcar definitionize-fun definitions))
260 (*lexenv* (make-lexenv definitionize-keyword processed-definitions)))
261 ;; I wonder how much of an compiler performance penalty this
262 ;; non-constant keyword is.
263 (funcall fun definitionize-keyword processed-definitions)))
265 ;;; Tweak LEXENV to include the DEFINITIONS from a MACROLET, then
266 ;;; call FUN (with no arguments).
268 ;;; This is split off from the IR1 convert method so that it can be
269 ;;; shared by the special-case top level MACROLET processing code, and
270 ;;; further split so that the special-case MACROLET processing code in
271 ;;; EVAL can likewise make use of it.
272 (defun macrolet-definitionize-fun (context lexenv)
273 (flet ((fail (control &rest args)
274 (ecase context
275 (:compile (apply #'compiler-error control args))
276 (:eval (error 'simple-program-error
277 :format-control control
278 :format-arguments args)))))
279 (lambda (definition)
280 (unless (list-of-length-at-least-p definition 2)
281 (fail "The list ~S is too short to be a legal local macro definition."
282 definition))
283 (destructuring-bind (name arglist &body body) definition
284 (unless (symbolp name)
285 (fail "The local macro name ~S is not a symbol." name))
286 (when (fboundp name)
287 (compiler-assert-symbol-home-package-unlocked
288 name "binding ~A as a local macro"))
289 (unless (listp arglist)
290 (fail "The local macro argument list ~S is not a list."
291 arglist))
292 (with-unique-names (whole environment)
293 (multiple-value-bind (body local-decls)
294 (parse-defmacro arglist whole body name 'macrolet
295 :environment environment)
296 `(,name macro .
297 ,(compile-in-lexenv
299 `(lambda (,whole ,environment)
300 ,@local-decls
301 ,body)
302 lexenv))))))))
304 (defun funcall-in-macrolet-lexenv (definitions fun context)
305 (%funcall-in-foomacrolet-lexenv
306 (macrolet-definitionize-fun context (make-restricted-lexenv *lexenv*))
307 :funs
308 definitions
309 fun))
311 (def-ir1-translator macrolet ((definitions &rest body) start next result)
312 #!+sb-doc
313 "MACROLET ({(Name Lambda-List Form*)}*) Body-Form*
314 Evaluate the Body-Forms in an environment with the specified local macros
315 defined. Name is the local macro name, Lambda-List is the DEFMACRO style
316 destructuring lambda list, and the Forms evaluate to the expansion.."
317 (funcall-in-macrolet-lexenv
318 definitions
319 (lambda (&key funs)
320 (declare (ignore funs))
321 (ir1-translate-locally body start next result))
322 :compile))
324 (defun symbol-macrolet-definitionize-fun (context)
325 (flet ((fail (control &rest args)
326 (ecase context
327 (:compile (apply #'compiler-error control args))
328 (:eval (error 'simple-program-error
329 :format-control control
330 :format-arguments args)))))
331 (lambda (definition)
332 (unless (proper-list-of-length-p definition 2)
333 (fail "malformed symbol/expansion pair: ~S" definition))
334 (destructuring-bind (name expansion) definition
335 (unless (symbolp name)
336 (fail "The local symbol macro name ~S is not a symbol." name))
337 (when (or (boundp name) (eq (info :variable :kind name) :macro))
338 (compiler-assert-symbol-home-package-unlocked
339 name "binding ~A as a local symbol-macro"))
340 (let ((kind (info :variable :kind name)))
341 (when (member kind '(:special :constant))
342 (fail "Attempt to bind a ~(~A~) variable with SYMBOL-MACROLET: ~S"
343 kind name)))
344 ;; A magical cons that MACROEXPAND-1 understands.
345 `(,name . (macro . ,expansion))))))
347 (defun funcall-in-symbol-macrolet-lexenv (definitions fun context)
348 (%funcall-in-foomacrolet-lexenv
349 (symbol-macrolet-definitionize-fun context)
350 :vars
351 definitions
352 fun))
354 (def-ir1-translator symbol-macrolet
355 ((macrobindings &body body) start next result)
356 #!+sb-doc
357 "SYMBOL-MACROLET ({(Name Expansion)}*) Decl* Form*
358 Define the Names as symbol macros with the given Expansions. Within the
359 body, references to a Name will effectively be replaced with the Expansion."
360 (funcall-in-symbol-macrolet-lexenv
361 macrobindings
362 (lambda (&key vars)
363 (ir1-translate-locally body start next result :vars vars))
364 :compile))
366 ;;;; %PRIMITIVE
367 ;;;;
368 ;;;; Uses of %PRIMITIVE are either expanded into Lisp code or turned
369 ;;;; into a funny function.
371 ;;; Carefully evaluate a list of forms, returning a list of the results.
372 (defun eval-info-args (args)
373 (declare (list args))
374 (handler-case (mapcar #'eval args)
375 (error (condition)
376 (compiler-error "Lisp error during evaluation of info args:~%~A"
377 condition))))
379 ;;; Convert to the %%PRIMITIVE funny function. The first argument is
380 ;;; the template, the second is a list of the results of any
381 ;;; codegen-info args, and the remaining arguments are the runtime
382 ;;; arguments.
384 ;;; We do various error checking now so that we don't bomb out with
385 ;;; a fatal error during IR2 conversion.
387 ;;; KLUDGE: It's confusing having multiple names floating around for
388 ;;; nearly the same concept: PRIMITIVE, TEMPLATE, VOP. Now that CMU
389 ;;; CL's *PRIMITIVE-TRANSLATORS* stuff is gone, we could call
390 ;;; primitives VOPs, rename TEMPLATE to VOP-TEMPLATE, rename
391 ;;; BACKEND-TEMPLATE-NAMES to BACKEND-VOPS, and rename %PRIMITIVE to
392 ;;; VOP or %VOP.. -- WHN 2001-06-11
393 ;;; FIXME: Look at doing this ^, it doesn't look too hard actually.
394 (def-ir1-translator %primitive ((name &rest args) start next result)
395 (declare (type symbol name))
396 (let* ((template (or (gethash name *backend-template-names*)
397 (bug "undefined primitive ~A" name)))
398 (required (length (template-arg-types template)))
399 (info (template-info-arg-count template))
400 (min (+ required info))
401 (nargs (length args)))
402 (if (template-more-args-type template)
403 (when (< nargs min)
404 (bug "Primitive ~A was called with ~R argument~:P, ~
405 but wants at least ~R."
406 name
407 nargs
408 min))
409 (unless (= nargs min)
410 (bug "Primitive ~A was called with ~R argument~:P, ~
411 but wants exactly ~R."
412 name
413 nargs
414 min)))
416 (when (eq (template-result-types template) :conditional)
417 (bug "%PRIMITIVE was used with a conditional template."))
419 (when (template-more-results-type template)
420 (bug "%PRIMITIVE was used with an unknown values template."))
422 (ir1-convert start next result
423 `(%%primitive ',template
424 ',(eval-info-args
425 (subseq args required min))
426 ,@(subseq args 0 required)
427 ,@(subseq args min)))))
429 ;;;; QUOTE
431 (def-ir1-translator quote ((thing) start next result)
432 #!+sb-doc
433 "QUOTE Value
434 Return Value without evaluating it."
435 (reference-constant start next result thing))
437 ;;;; FUNCTION and NAMED-LAMBDA
438 (defun name-lambdalike (thing)
439 (ecase (car thing)
440 ((named-lambda)
441 (second thing))
442 ((lambda instance-lambda)
443 `(lambda ,(second thing)))
444 ((lambda-with-lexenv)'
445 `(lambda ,(fifth thing)))))
447 (defun fun-name-leaf (thing)
448 (if (consp thing)
449 (cond
450 ((member (car thing)
451 '(lambda named-lambda instance-lambda lambda-with-lexenv))
452 (values (ir1-convert-lambdalike
453 thing
454 :debug-name (name-lambdalike thing))
456 ((legal-fun-name-p thing)
457 (values (find-lexically-apparent-fun
458 thing "as the argument to FUNCTION")
459 nil))
461 (compiler-error "~S is not a legal function name." thing)))
462 (values (find-lexically-apparent-fun
463 thing "as the argument to FUNCTION")
464 nil)))
466 (def-ir1-translator %%allocate-closures ((&rest leaves) start next result)
467 (aver (eq result 'nil))
468 (let ((lambdas leaves))
469 (ir1-convert start next result `(%allocate-closures ',lambdas))
470 (let ((allocator (node-dest (ctran-next start))))
471 (dolist (lambda lambdas)
472 (setf (functional-allocator lambda) allocator)))))
474 (defmacro with-fun-name-leaf ((leaf thing start) &body body)
475 `(multiple-value-bind (,leaf allocate-p) (fun-name-leaf ,thing)
476 (if allocate-p
477 (let ((.new-start. (make-ctran)))
478 (ir1-convert ,start .new-start. nil `(%%allocate-closures ,leaf))
479 (let ((,start .new-start.))
480 ,@body))
481 (locally
482 ,@body))))
484 (def-ir1-translator function ((thing) start next result)
485 #!+sb-doc
486 "FUNCTION Name
487 Return the lexically apparent definition of the function Name. Name may also
488 be a lambda expression."
489 (with-fun-name-leaf (leaf thing start)
490 (reference-leaf start next result leaf)))
492 ;;;; FUNCALL
494 ;;; FUNCALL is implemented on %FUNCALL, which can only call functions
495 ;;; (not symbols). %FUNCALL is used directly in some places where the
496 ;;; call should always be open-coded even if FUNCALL is :NOTINLINE.
497 (deftransform funcall ((function &rest args) * *)
498 (let ((arg-names (make-gensym-list (length args))))
499 `(lambda (function ,@arg-names)
500 (%funcall ,(if (csubtypep (lvar-type function)
501 (specifier-type 'function))
502 'function
503 '(%coerce-callable-to-fun function))
504 ,@arg-names))))
506 (def-ir1-translator %funcall ((function &rest args) start next result)
507 (if (and (consp function) (eq (car function) 'function))
508 (with-fun-name-leaf (leaf (second function) start)
509 (ir1-convert start next result `(,leaf ,@args)))
510 (let ((ctran (make-ctran))
511 (fun-lvar (make-lvar)))
512 (ir1-convert start ctran fun-lvar `(the function ,function))
513 (ir1-convert-combination-args fun-lvar ctran next result args))))
515 ;;; This source transform exists to reduce the amount of work for the
516 ;;; compiler. If the called function is a FUNCTION form, then convert
517 ;;; directly to %FUNCALL, instead of waiting around for type
518 ;;; inference.
519 (define-source-transform funcall (function &rest args)
520 (if (and (consp function) (eq (car function) 'function))
521 `(%funcall ,function ,@args)
522 (values nil t)))
524 (deftransform %coerce-callable-to-fun ((thing) (function) *)
525 "optimize away possible call to FDEFINITION at runtime"
526 'thing)
528 ;;;; LET and LET*
529 ;;;;
530 ;;;; (LET and LET* can't be implemented as macros due to the fact that
531 ;;;; any pervasive declarations also affect the evaluation of the
532 ;;;; arguments.)
534 ;;; Given a list of binding specifiers in the style of LET, return:
535 ;;; 1. The list of var structures for the variables bound.
536 ;;; 2. The initial value form for each variable.
538 ;;; The variable names are checked for legality and globally special
539 ;;; variables are marked as such. Context is the name of the form, for
540 ;;; error reporting purposes.
541 (declaim (ftype (function (list symbol) (values list list))
542 extract-let-vars))
543 (defun extract-let-vars (bindings context)
544 (collect ((vars)
545 (vals)
546 (names))
547 (flet ((get-var (name)
548 (varify-lambda-arg name
549 (if (eq context 'let*)
551 (names)))))
552 (dolist (spec bindings)
553 (cond ((atom spec)
554 (let ((var (get-var spec)))
555 (vars var)
556 (names spec)
557 (vals nil)))
559 (unless (proper-list-of-length-p spec 1 2)
560 (compiler-error "The ~S binding spec ~S is malformed."
561 context
562 spec))
563 (let* ((name (first spec))
564 (var (get-var name)))
565 (vars var)
566 (names name)
567 (vals (second spec)))))))
568 (dolist (name (names))
569 (when (eq (info :variable :kind name) :macro)
570 (compiler-assert-symbol-home-package-unlocked
571 name "lexically binding symbol-macro ~A")))
572 (values (vars) (vals))))
574 (def-ir1-translator let ((bindings &body body) start next result)
575 #!+sb-doc
576 "LET ({(Var [Value]) | Var}*) Declaration* Form*
577 During evaluation of the Forms, bind the Vars to the result of evaluating the
578 Value forms. The variables are bound in parallel after all of the Values are
579 evaluated."
580 (cond ((null bindings)
581 (ir1-translate-locally body start next result))
582 ((listp bindings)
583 (multiple-value-bind (forms decls)
584 (parse-body body :doc-string-allowed nil)
585 (multiple-value-bind (vars values) (extract-let-vars bindings 'let)
586 (binding* ((ctran (make-ctran))
587 (fun-lvar (make-lvar))
588 ((next result)
589 (processing-decls (decls vars nil next result
590 post-binding-lexenv)
591 (let ((fun (ir1-convert-lambda-body
592 forms
593 vars
594 :post-binding-lexenv post-binding-lexenv
595 :debug-name (debug-name 'let bindings))))
596 (reference-leaf start ctran fun-lvar fun))
597 (values next result))))
598 (ir1-convert-combination-args fun-lvar ctran next result values)))))
600 (compiler-error "Malformed LET bindings: ~S." bindings))))
602 (def-ir1-translator let* ((bindings &body body)
603 start next result)
604 #!+sb-doc
605 "LET* ({(Var [Value]) | Var}*) Declaration* Form*
606 Similar to LET, but the variables are bound sequentially, allowing each Value
607 form to reference any of the previous Vars."
608 (if (listp bindings)
609 (multiple-value-bind (forms decls)
610 (parse-body body :doc-string-allowed nil)
611 (multiple-value-bind (vars values) (extract-let-vars bindings 'let*)
612 (processing-decls (decls vars nil start next post-binding-lexenv)
613 (ir1-convert-aux-bindings start
614 next
615 result
616 forms
617 vars
618 values
619 post-binding-lexenv))))
620 (compiler-error "Malformed LET* bindings: ~S." bindings)))
622 ;;; logic shared between IR1 translators for LOCALLY, MACROLET,
623 ;;; and SYMBOL-MACROLET
625 ;;; Note that all these things need to preserve toplevel-formness,
626 ;;; but we don't need to worry about that within an IR1 translator,
627 ;;; since toplevel-formness is picked off by PROCESS-TOPLEVEL-FOO
628 ;;; forms before we hit the IR1 transform level.
629 (defun ir1-translate-locally (body start next result &key vars funs)
630 (declare (type ctran start next) (type (or lvar null) result)
631 (type list body))
632 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
633 (processing-decls (decls vars funs next result)
634 (ir1-convert-progn-body start next result forms))))
636 (def-ir1-translator locally ((&body body) start next result)
637 #!+sb-doc
638 "LOCALLY Declaration* Form*
639 Sequentially evaluate the Forms in a lexical environment where the
640 the Declarations have effect. If LOCALLY is a top level form, then
641 the Forms are also processed as top level forms."
642 (ir1-translate-locally body start next result))
644 ;;;; FLET and LABELS
646 ;;; Given a list of local function specifications in the style of
647 ;;; FLET, return lists of the function names and of the lambdas which
648 ;;; are their definitions.
650 ;;; The function names are checked for legality. CONTEXT is the name
651 ;;; of the form, for error reporting.
652 (declaim (ftype (function (list symbol) (values list list)) extract-flet-vars))
653 (defun extract-flet-vars (definitions context)
654 (collect ((names)
655 (defs))
656 (dolist (def definitions)
657 (when (or (atom def) (< (length def) 2))
658 (compiler-error "The ~S definition spec ~S is malformed." context def))
660 (let ((name (first def)))
661 (check-fun-name name)
662 (when (fboundp name)
663 (compiler-assert-symbol-home-package-unlocked
664 name "binding ~A as a local function"))
665 (names name)
666 (multiple-value-bind (forms decls) (parse-body (cddr def))
667 (defs `(lambda ,(second def)
668 ,@decls
669 (block ,(fun-name-block-name name)
670 . ,forms))))))
671 (values (names) (defs))))
673 (defun ir1-convert-fbindings (start next result funs body)
674 (let ((ctran (make-ctran))
675 (dx-p (find-if #'leaf-dynamic-extent funs)))
676 (when dx-p
677 (ctran-starts-block ctran)
678 (ctran-starts-block next))
679 (ir1-convert start ctran nil `(%%allocate-closures ,@funs))
680 (cond (dx-p
681 (let* ((dummy (make-ctran))
682 (entry (make-entry))
683 (cleanup (make-cleanup :kind :dynamic-extent
684 :mess-up entry
685 :info (list (node-dest
686 (ctran-next start))))))
687 (push entry (lambda-entries (lexenv-lambda *lexenv*)))
688 (setf (entry-cleanup entry) cleanup)
689 (link-node-to-previous-ctran entry ctran)
690 (use-ctran entry dummy)
692 (let ((*lexenv* (make-lexenv :cleanup cleanup)))
693 (ir1-convert-progn-body dummy next result body))))
694 (t (ir1-convert-progn-body ctran next result body)))))
696 (def-ir1-translator flet ((definitions &body body)
697 start next result)
698 #!+sb-doc
699 "FLET ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
700 Evaluate the Body-Forms with some local function definitions. The bindings
701 do not enclose the definitions; any use of Name in the Forms will refer to
702 the lexically apparent function definition in the enclosing environment."
703 (multiple-value-bind (forms decls)
704 (parse-body body :doc-string-allowed nil)
705 (multiple-value-bind (names defs)
706 (extract-flet-vars definitions 'flet)
707 (let ((fvars (mapcar (lambda (n d)
708 (ir1-convert-lambda d
709 :source-name n
710 :debug-name (debug-name 'flet n)))
711 names defs)))
712 (processing-decls (decls nil fvars next result)
713 (let ((*lexenv* (make-lexenv :funs (pairlis names fvars))))
714 (ir1-convert-fbindings start next result fvars forms)))))))
716 (def-ir1-translator labels ((definitions &body body) start next result)
717 #!+sb-doc
718 "LABELS ({(Name Lambda-List Declaration* Form*)}*) Declaration* Body-Form*
719 Evaluate the Body-Forms with some local function definitions. The bindings
720 enclose the new definitions, so the defined functions can call themselves or
721 each other."
722 (multiple-value-bind (forms decls) (parse-body body :doc-string-allowed nil)
723 (multiple-value-bind (names defs)
724 (extract-flet-vars definitions 'labels)
725 (let* (;; dummy LABELS functions, to be used as placeholders
726 ;; during construction of real LABELS functions
727 (placeholder-funs (mapcar (lambda (name)
728 (make-functional
729 :%source-name name
730 :%debug-name (debug-name
731 'labels-placeholder
732 name)))
733 names))
734 ;; (like PAIRLIS but guaranteed to preserve ordering:)
735 (placeholder-fenv (mapcar #'cons names placeholder-funs))
736 ;; the real LABELS functions, compiled in a LEXENV which
737 ;; includes the dummy LABELS functions
738 (real-funs
739 (let ((*lexenv* (make-lexenv :funs placeholder-fenv)))
740 (mapcar (lambda (name def)
741 (ir1-convert-lambda def
742 :source-name name
743 :debug-name (debug-name 'labels name)))
744 names defs))))
746 ;; Modify all the references to the dummy function leaves so
747 ;; that they point to the real function leaves.
748 (loop for real-fun in real-funs and
749 placeholder-cons in placeholder-fenv do
750 (substitute-leaf real-fun (cdr placeholder-cons))
751 (setf (cdr placeholder-cons) real-fun))
753 ;; Voila.
754 (processing-decls (decls nil real-funs next result)
755 (let ((*lexenv* (make-lexenv
756 ;; Use a proper FENV here (not the
757 ;; placeholder used earlier) so that if the
758 ;; lexical environment is used for inline
759 ;; expansion we'll get the right functions.
760 :funs (pairlis names real-funs))))
761 (ir1-convert-fbindings start next result real-funs forms)))))))
764 ;;;; the THE special operator, and friends
766 ;;; A logic shared among THE and TRULY-THE.
767 (defun the-in-policy (type value policy start next result)
768 (let ((type (if (ctype-p type) type
769 (compiler-values-specifier-type type))))
770 (cond ((or (eq type *wild-type*)
771 (eq type *universal-type*)
772 (and (leaf-p value)
773 (values-subtypep (make-single-value-type (leaf-type value))
774 type))
775 (and (sb!xc:constantp value)
776 (ctypep (constant-form-value value)
777 (single-value-type type))))
778 (ir1-convert start next result value))
779 (t (let ((value-ctran (make-ctran))
780 (value-lvar (make-lvar)))
781 (ir1-convert start value-ctran value-lvar value)
782 (let ((cast (make-cast value-lvar type policy)))
783 (link-node-to-previous-ctran cast value-ctran)
784 (setf (lvar-dest value-lvar) cast)
785 (use-continuation cast next result)))))))
787 ;;; Assert that FORM evaluates to the specified type (which may be a
788 ;;; VALUES type). TYPE may be a type specifier or (as a hack) a CTYPE.
789 (def-ir1-translator the ((type value) start next result)
790 (the-in-policy type value (lexenv-policy *lexenv*) start next result))
792 ;;; This is like the THE special form, except that it believes
793 ;;; whatever you tell it. It will never generate a type check, but
794 ;;; will cause a warning if the compiler can prove the assertion is
795 ;;; wrong.
796 (def-ir1-translator truly-the ((type value) start next result)
797 #!+sb-doc
799 #-nil
800 (let ((type (coerce-to-values (compiler-values-specifier-type type)))
801 (old (when result (find-uses result))))
802 (ir1-convert start next result value)
803 (when result
804 (do-uses (use result)
805 (unless (memq use old)
806 (derive-node-type use type)))))
807 #+nil
808 (the-in-policy type value '((type-check . 0)) start cont))
810 ;;;; SETQ
812 ;;; If there is a definition in LEXENV-VARS, just set that, otherwise
813 ;;; look at the global information. If the name is for a constant,
814 ;;; then error out.
815 (def-ir1-translator setq ((&whole source &rest things) start next result)
816 (let ((len (length things)))
817 (when (oddp len)
818 (compiler-error "odd number of args to SETQ: ~S" source))
819 (if (= len 2)
820 (let* ((name (first things))
821 (leaf (or (lexenv-find name vars)
822 (find-free-var name))))
823 (etypecase leaf
824 (leaf
825 (when (constant-p leaf)
826 (compiler-error "~S is a constant and thus can't be set." name))
827 (when (lambda-var-p leaf)
828 (let ((home-lambda (ctran-home-lambda-or-null start)))
829 (when home-lambda
830 (pushnew leaf (lambda-calls-or-closes home-lambda))))
831 (when (lambda-var-ignorep leaf)
832 ;; ANSI's definition of "Declaration IGNORE, IGNORABLE"
833 ;; requires that this be a STYLE-WARNING, not a full warning.
834 (compiler-style-warn
835 "~S is being set even though it was declared to be ignored."
836 name)))
837 (setq-var start next result leaf (second things)))
838 (cons
839 (aver (eq (car leaf) 'macro))
840 ;; FIXME: [Free] type declaration. -- APD, 2002-01-26
841 (ir1-convert start next result
842 `(setf ,(cdr leaf) ,(second things))))
843 (heap-alien-info
844 (ir1-convert start next result
845 `(%set-heap-alien ',leaf ,(second things))))))
846 (collect ((sets))
847 (do ((thing things (cddr thing)))
848 ((endp thing)
849 (ir1-convert-progn-body start next result (sets)))
850 (sets `(setq ,(first thing) ,(second thing))))))))
852 ;;; This is kind of like REFERENCE-LEAF, but we generate a SET node.
853 ;;; This should only need to be called in SETQ.
854 (defun setq-var (start next result var value)
855 (declare (type ctran start next) (type (or lvar null) result)
856 (type basic-var var))
857 (let ((dest-ctran (make-ctran))
858 (dest-lvar (make-lvar))
859 (type (or (lexenv-find var type-restrictions)
860 (leaf-type var))))
861 (ir1-convert start dest-ctran dest-lvar `(the ,type ,value))
862 (let ((res (make-set :var var :value dest-lvar)))
863 (setf (lvar-dest dest-lvar) res)
864 (setf (leaf-ever-used var) t)
865 (push res (basic-var-sets var))
866 (link-node-to-previous-ctran res dest-ctran)
867 (use-continuation res next result))))
869 ;;;; CATCH, THROW and UNWIND-PROTECT
871 ;;; We turn THROW into a MULTIPLE-VALUE-CALL of a magical function,
872 ;;; since as as far as IR1 is concerned, it has no interesting
873 ;;; properties other than receiving multiple-values.
874 (def-ir1-translator throw ((tag result) start next result-lvar)
875 #!+sb-doc
876 "Throw Tag Form
877 Do a non-local exit, return the values of Form from the CATCH whose tag
878 evaluates to the same thing as Tag."
879 (ir1-convert start next result-lvar
880 `(multiple-value-call #'%throw ,tag ,result)))
882 ;;; This is a special special form used to instantiate a cleanup as
883 ;;; the current cleanup within the body. KIND is the kind of cleanup
884 ;;; to make, and MESS-UP is a form that does the mess-up action. We
885 ;;; make the MESS-UP be the USE of the MESS-UP form's continuation,
886 ;;; and introduce the cleanup into the lexical environment. We
887 ;;; back-patch the ENTRY-CLEANUP for the current cleanup to be the new
888 ;;; cleanup, since this inner cleanup is the interesting one.
889 (def-ir1-translator %within-cleanup
890 ((kind mess-up &body body) start next result)
891 (let ((dummy (make-ctran))
892 (dummy2 (make-ctran)))
893 (ir1-convert start dummy nil mess-up)
894 (let* ((mess-node (ctran-use dummy))
895 (cleanup (make-cleanup :kind kind
896 :mess-up mess-node))
897 (old-cup (lexenv-cleanup *lexenv*))
898 (*lexenv* (make-lexenv :cleanup cleanup)))
899 (setf (entry-cleanup (cleanup-mess-up old-cup)) cleanup)
900 (ir1-convert dummy dummy2 nil '(%cleanup-point))
901 (ir1-convert-progn-body dummy2 next result body))))
903 ;;; This is a special special form that makes an "escape function"
904 ;;; which returns unknown values from named block. We convert the
905 ;;; function, set its kind to :ESCAPE, and then reference it. The
906 ;;; :ESCAPE kind indicates that this function's purpose is to
907 ;;; represent a non-local control transfer, and that it might not
908 ;;; actually have to be compiled.
910 ;;; Note that environment analysis replaces references to escape
911 ;;; functions with references to the corresponding NLX-INFO structure.
912 (def-ir1-translator %escape-fun ((tag) start next result)
913 (let ((fun (let ((*allow-instrumenting* nil))
914 (ir1-convert-lambda
915 `(lambda ()
916 (return-from ,tag (%unknown-values)))
917 :debug-name (debug-name 'escape-fun tag))))
918 (ctran (make-ctran)))
919 (setf (functional-kind fun) :escape)
920 (ir1-convert start ctran nil `(%%allocate-closures ,fun))
921 (reference-leaf ctran next result fun)))
923 ;;; Yet another special special form. This one looks up a local
924 ;;; function and smashes it to a :CLEANUP function, as well as
925 ;;; referencing it.
926 (def-ir1-translator %cleanup-fun ((name) start next result)
927 (let ((fun (lexenv-find name funs)))
928 (aver (lambda-p fun))
929 (setf (functional-kind fun) :cleanup)
930 (reference-leaf start next result fun)))
932 (def-ir1-translator catch ((tag &body body) start next result)
933 #!+sb-doc
934 "Catch Tag Form*
935 Evaluate TAG and instantiate it as a catcher while the body forms are
936 evaluated in an implicit PROGN. If a THROW is done to TAG within the dynamic
937 scope of the body, then control will be transferred to the end of the body
938 and the thrown values will be returned."
939 ;; We represent the possibility of the control transfer by making an
940 ;; "escape function" that does a lexical exit, and instantiate the
941 ;; cleanup using %WITHIN-CLEANUP.
942 (ir1-convert
943 start next result
944 (with-unique-names (exit-block)
945 `(block ,exit-block
946 (%within-cleanup
947 :catch (%catch (%escape-fun ,exit-block) ,tag)
948 ,@body)))))
950 (def-ir1-translator unwind-protect
951 ((protected &body cleanup) start next result)
952 #!+sb-doc
953 "Unwind-Protect Protected Cleanup*
954 Evaluate the form PROTECTED, returning its values. The CLEANUP forms are
955 evaluated whenever the dynamic scope of the PROTECTED form is exited (either
956 due to normal completion or a non-local exit such as THROW)."
957 ;; UNWIND-PROTECT is similar to CATCH, but hairier. We make the
958 ;; cleanup forms into a local function so that they can be referenced
959 ;; both in the case where we are unwound and in any local exits. We
960 ;; use %CLEANUP-FUN on this to indicate that reference by
961 ;; %UNWIND-PROTECT isn't "real", and thus doesn't cause creation of
962 ;; an XEP.
963 (ir1-convert
964 start next result
965 (with-unique-names (cleanup-fun drop-thru-tag exit-tag next start count)
966 `(flet ((,cleanup-fun () ,@cleanup nil))
967 ;; FIXME: If we ever get DYNAMIC-EXTENT working, then
968 ;; ,CLEANUP-FUN should probably be declared DYNAMIC-EXTENT,
969 ;; and something can be done to make %ESCAPE-FUN have
970 ;; dynamic extent too.
971 (block ,drop-thru-tag
972 (multiple-value-bind (,next ,start ,count)
973 (block ,exit-tag
974 (%within-cleanup
975 :unwind-protect
976 (%unwind-protect (%escape-fun ,exit-tag)
977 (%cleanup-fun ,cleanup-fun))
978 (return-from ,drop-thru-tag ,protected)))
979 (,cleanup-fun)
980 (%continue-unwind ,next ,start ,count)))))))
982 ;;;; multiple-value stuff
984 (def-ir1-translator multiple-value-call ((fun &rest args) start next result)
985 #!+sb-doc
986 "MULTIPLE-VALUE-CALL Function Values-Form*
987 Call FUNCTION, passing all the values of each VALUES-FORM as arguments,
988 values from the first VALUES-FORM making up the first argument, etc."
989 (let* ((ctran (make-ctran))
990 (fun-lvar (make-lvar))
991 (node (if args
992 ;; If there are arguments, MULTIPLE-VALUE-CALL
993 ;; turns into an MV-COMBINATION.
994 (make-mv-combination fun-lvar)
995 ;; If there are no arguments, then we convert to a
996 ;; normal combination, ensuring that a MV-COMBINATION
997 ;; always has at least one argument. This can be
998 ;; regarded as an optimization, but it is more
999 ;; important for simplifying compilation of
1000 ;; MV-COMBINATIONS.
1001 (make-combination fun-lvar))))
1002 (ir1-convert start ctran fun-lvar
1003 (if (and (consp fun) (eq (car fun) 'function))
1005 `(%coerce-callable-to-fun ,fun)))
1006 (setf (lvar-dest fun-lvar) node)
1007 (collect ((arg-lvars))
1008 (let ((this-start ctran))
1009 (dolist (arg args)
1010 (let ((this-ctran (make-ctran))
1011 (this-lvar (make-lvar node)))
1012 (ir1-convert this-start this-ctran this-lvar arg)
1013 (setq this-start this-ctran)
1014 (arg-lvars this-lvar)))
1015 (link-node-to-previous-ctran node this-start)
1016 (use-continuation node next result)
1017 (setf (basic-combination-args node) (arg-lvars))))))
1019 (def-ir1-translator multiple-value-prog1
1020 ((values-form &rest forms) start next result)
1021 #!+sb-doc
1022 "MULTIPLE-VALUE-PROG1 Values-Form Form*
1023 Evaluate Values-Form and then the Forms, but return all the values of
1024 Values-Form."
1025 (let ((dummy (make-ctran)))
1026 (ctran-starts-block dummy)
1027 (ir1-convert start dummy result values-form)
1028 (ir1-convert-progn-body dummy next nil forms)))
1030 ;;;; interface to defining macros
1032 ;;; Old CMUCL comment:
1034 ;;; Return a new source path with any stuff intervening between the
1035 ;;; current path and the first form beginning with NAME stripped
1036 ;;; off. This is used to hide the guts of DEFmumble macros to
1037 ;;; prevent annoying error messages.
1039 ;;; Now that we have implementations of DEFmumble macros in terms of
1040 ;;; EVAL-WHEN, this function is no longer used. However, it might be
1041 ;;; worth figuring out why it was used, and maybe doing analogous
1042 ;;; munging to the functions created in the expanders for the macros.
1043 (defun revert-source-path (name)
1044 (do ((path *current-path* (cdr path)))
1045 ((null path) *current-path*)
1046 (let ((first (first path)))
1047 (when (or (eq first name)
1048 (eq first 'original-source-start))
1049 (return path)))))