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