1 ;;;; This file represents the current state of on-going development on
2 ;;;; compiler hooks for an interpreter that takes the compiler's IR1 of
5 ;;;; This software is part of the SBCL system. See the README file for
8 ;;;; This software is derived from the CMU CL system, which was
9 ;;;; written at Carnegie Mellon University and released into the
10 ;;;; public domain. The software is in the public domain and is
11 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
12 ;;;; files for more information.
19 ;;; FIXME: Doesn't this belong somewhere else, like early-c.lisp?
20 (declaim (special *constants* *free-variables* *component-being-compiled*
21 *code-vector* *next-location* *result-fixups*
22 *free-functions* *source-paths* *failed-optimizations*
23 *seen-blocks* *seen-functions* *list-conflicts-table*
24 *continuation-number* *continuation-numbers*
25 *number-continuations* *tn-id* *tn-ids* *id-tns*
26 *label-ids* *label-id* *id-labels*
27 *compiler-error-count* *compiler-warning-count*
28 *compiler-style-warning-count* *compiler-note-count*
29 *compiler-error-bailout*
30 #!+sb-show *compiler-trace-output*
31 *last-source-context* *last-original-source*
32 *last-source-form* *last-format-string* *last-format-args*
33 *last-message-count* *check-consistency*
34 *all-components* *converting-for-interpreter*
35 *source-info* *block-compile* *current-path*
36 *current-component* *lexenv*))
38 ;;; Translate form into the compiler's IR1 and perform environment
39 ;;; analysis. This is sort of a combination of COMPILE-FILE,
40 ;;; SUB-COMPILE-FILE, COMPILE-TOP-LEVEL, and COMPILE-COMPONENT.
41 (defun compile-for-eval (form quietly)
43 (let* ((*block-compile* nil)
44 (*lexenv* (make-null-lexenv))
45 (*compiler-error-bailout*
46 #'(lambda () (error "fatal error, aborting evaluation")))
48 (*last-source-context* nil)
49 (*last-original-source* nil)
50 (*last-source-form* nil)
51 (*last-format-string* nil)
52 (*last-format-args* nil)
53 (*last-message-count* 0)
54 ;; These are now bound by WITH-COMPILATION-UNIT. -- WHN 20000308
55 #+nil (*compiler-error-count* 0)
56 #+nil (*compiler-warning-count* 0)
57 #+nil (*compiler-style-warning-count* 0)
58 #+nil (*compiler-note-count* 0)
59 (*source-info* (make-lisp-source-info form))
60 (*converting-for-interpreter* t)
66 (find-source-paths form 0)
67 ;; This LET comes from COMPILE-TOP-LEVEL.
68 ;; The noted DOLIST is a splice from a call that COMPILE-TOP-LEVEL makes.
69 (sb!xc:with-compilation-unit ()
70 (let ((lambdas (list (ir1-top-level form
71 '(original-source-start 0 0)
73 (declare (list lambdas))
74 (dolist (lambda lambdas)
76 (block-component (node-block (lambda-bind lambda))))
77 (*all-components* (list component)))
78 (local-call-analyze component)))
79 (multiple-value-bind (components top-components)
80 (find-initial-dfo lambdas)
81 (let ((*all-components* (append components top-components)))
82 (when *check-consistency*
83 (check-ir1-consistency *all-components*))
84 ;; This DOLIST body comes from the beginning of
86 (dolist (component *all-components*)
87 (ir1-finalize component)
88 (let ((*component-being-compiled* component))
89 (environment-analyze component))
90 (annotate-component-for-eval component))
91 (when *check-consistency*
92 (check-ir1-consistency *all-components*))))
95 ;;;; annotating IR1 for interpretation
97 (defstruct (lambda-eval-info (:constructor make-lambda-eval-info
98 (frame-size args-passed entries)))
99 frame-size ; number of stack locations needed to hold locals
100 args-passed ; number of referenced arguments passed to lambda
101 entries ; a-list mapping entry nodes to stack locations
102 (function nil)) ; a function object corresponding to this lambda
103 (def!method print-object ((obj lambda-eval-info) str)
104 (print-unreadable-object (obj str :type t)))
106 (defstruct (entry-node-info (:constructor make-entry-node-info
108 st-top ; stack top when we encounter the entry node
109 nlx-tag) ; tag to which to throw to get back entry node's context
110 (def!method print-object ((obj entry-node-info) str)
111 (print-unreadable-object (obj str :type t)))
113 ;;; Some compiler funny functions have definitions, so the interpreter can
114 ;;; call them. These require special action to coordinate the interpreter,
115 ;;; system call stack, and the environment. The annotation prepass marks the
116 ;;; references to these as :unused, so the interpreter doesn't try to fetch
117 ;;; functions through these undefined symbols.
118 (defconstant undefined-funny-funs
119 '(%special-bind %special-unbind %more-arg-context %unknown-values %catch
120 %unwind-protect %catch-breakup %unwind-protect-breakup
121 %lexical-exit-breakup %continue-unwind %nlx-entry))
123 ;;; Some kinds of functions are only passed as arguments to funny functions,
124 ;;; and are never actually evaluated at run time.
125 (defconstant non-closed-function-kinds '(:cleanup :escape))
127 ;;; This annotates continuations, lambda-vars, and lambdas. For each
128 ;;; continuation, we cache how its destination uses its value. This only buys
129 ;;; efficiency when the code executes more than once, but the overhead of this
130 ;;; part of the prepass for code executed only once should be negligible.
132 ;;; As a special case to aid interpreting local function calls, we sometimes
133 ;;; note the continuation as :unused. This occurs when there is a local call,
134 ;;; and there is no actual function object to call; we mark the continuation as
135 ;;; :unused since there is nothing to push on the interpreter's stack.
136 ;;; Normally we would see a reference to a function that we would push on the
137 ;;; stack to later pop and apply to the arguments on the stack. To determine
138 ;;; when we have a local call with no real function object, we look at the node
139 ;;; to see whether it is a reference with a destination that is a :local
140 ;;; combination whose function is the reference node's continuation.
142 ;;; After checking for virtual local calls, we check for funny functions the
143 ;;; compiler refers to for calling to note certain operations. These functions
144 ;;; are undefined, and if the interpreter tried to reference the function cells
145 ;;; of these symbols, it would get an error. We mark the continuations
146 ;;; delivering the values of these references as :unused, so the reference
147 ;;; never takes place.
149 ;;; For each lambda-var, including a lambda's vars and its let's vars, we note
150 ;;; the stack offset used to access and store that variable. Then we note the
151 ;;; lambda with the total number of variables, so we know how big its stack
152 ;;; frame is. Also in the lambda's info is the number of its arguments that it
153 ;;; actually references; the interpreter never pushes or pops an unreferenced
154 ;;; argument, so we can't just use LENGTH on LAMBDA-VARS to know how many args
155 ;;; the caller passed.
157 ;;; For each entry node in a lambda, we associate in the lambda-eval-info the
158 ;;; entry node with a stack offset. Evaluation code stores the frame pointer
159 ;;; in this slot upon processing the entry node to aid stack cleanup and
160 ;;; correct frame manipulation when processing exit nodes.
161 (defun annotate-component-for-eval (component)
162 (do-blocks (b component)
163 (do-nodes (node cont b)
164 (let* ((dest (continuation-dest cont))
165 (refp (typep node 'ref))
166 (leaf (if refp (ref-leaf node))))
167 (setf (continuation-info cont)
168 (cond ((and refp dest (typep dest 'basic-combination)
169 (eq (basic-combination-kind dest) :local)
170 (eq (basic-combination-fun dest) cont))
172 ((and leaf (typep leaf 'global-var)
173 (eq (global-var-kind leaf) :global-function)
174 (member (sb!c::global-var-name leaf)
178 ((and leaf (typep leaf 'clambda)
179 (member (functional-kind leaf)
180 non-closed-function-kinds))
181 (assert (not (eq (functional-kind leaf) :escape)))
185 ;; Change locations in eval.lisp that think :RETURN
187 ((or mv-combination creturn exit) :multiple)
190 (dolist (lambda (component-lambdas component))
191 (let ((locals-count 0)
192 (args-passed-count 0))
193 (dolist (var (lambda-vars lambda))
194 (setf (leaf-info var) locals-count)
196 (when (leaf-refs var) (incf args-passed-count)))
197 (dolist (let (lambda-lets lambda))
198 (dolist (var (lambda-vars let))
199 (setf (leaf-info var) locals-count)
200 (incf locals-count)))
202 (dolist (e (lambda-entries lambda))
203 (ecase (process-entry-node-p e)
206 (push (cons e (make-entry-node-info locals-count nil))
209 (:non-local-lexical-exit
211 (make-entry-node-info locals-count
212 (incf locals-count)))
214 (incf locals-count))))
215 (setf (lambda-info lambda)
216 (make-lambda-eval-info locals-count
220 (defun process-entry-node-p (entry)
221 (let ((entry-cleanup (entry-cleanup entry)))
222 (dolist (nlx (environment-nlx-info (node-environment entry))
224 (let ((cleanup (nlx-info-cleanup nlx)))
225 (when (eq entry-cleanup cleanup)
226 (ecase (cleanup-kind cleanup)
228 (return :non-local-lexical-exit))
229 ((:catch :unwind-protect)
230 (return :blow-it-off))))))))
232 ;;; Sometime consider annotations to exclude processing of exit nodes when
233 ;;; we want to do a tail-p thing.
235 ;;;; defining funny functions for interpreter
238 %listify-rest-args %more-arg %verify-argument-count %argument-count-error
239 %odd-keyword-arguments-error %unknown-keyword-argument-error
242 (defun %verify-argument-count (supplied-args defined-args)
243 (unless (= supplied-args defined-args)
244 (error "Wrong argument count, wanted ~D and got ~D."
245 defined-args supplied-args))
248 ;;; Use (SETF SYMBOL-FUNCTION) instead of DEFUN so that the compiler
249 ;;; doesn't try to compile the hidden %THROW MV-CALL in the throw below as
250 ;;; a local recursive call.
251 (setf (symbol-function '%throw)
252 #'(lambda (tag &rest args)
253 (throw tag (values-list args))))
255 (defun %more-arg (args index)
258 (defun %listify-rest-args (ptr count)
259 (declare (ignore count))
262 (defun %more-arg-values (args start count)
263 (values-list (subseq args start count)))
265 (defun %argument-count-error (args-passed-count)
266 (error 'simple-program-error
267 :format-control "wrong number of arguments passed: ~S"
268 :format-arguments (list args-passed-count)))
270 (defun %odd-keyword-arguments-error ()
271 (error 'simple-program-error
272 :format-control "function called with odd number of keyword arguments"
273 :format-arguments nil))
275 (defun %unknown-keyword-argument-error (keyword)
276 (error 'simple-program-error
277 :format-control "unknown keyword argument: ~S"
278 :format-arguments (list keyword)))
280 (defun %cleanup-point ())
282 (defun value-cell-ref (x) (value-cell-ref x))