1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!BYTECODE")
12 ;;; This is defined here so that the printer etc. can call
13 ;;; INTERPRETED-FUNCTION-P before the full interpreter is loaded.
15 ;;; an interpreted function
16 (defstruct (interpreted-function
17 (:alternate-metaclass sb!kernel:funcallable-instance
18 sb!kernel:funcallable-structure-class
19 sb!kernel:make-funcallable-structure-class)
20 (:type sb!kernel:funcallable-structure)
21 (:constructor %make-interpreted-function)
23 ;; FIXME: Binding PRINT-OBJECT isn't going to help unless
24 ;; we fix the print-a-funcallable-instance code so that
25 ;; it calls PRINT-OBJECT in this case.
28 (print-unreadable-object (x stream :identity t)
29 (interpreted-function-%name x)))))
30 ;; The name of this interpreted function, or NIL if none specified.
32 ;; This function's debug arglist.
34 ;; A lambda that can be converted to get the definition.
36 ;; If this function has been converted, then this is the XEP. If this is
37 ;; false, then the function is not in the cache (or is in the process of
39 (definition nil :type (or sb!c::clambda null))
40 ;; The number of consecutive GCs that this function has been unused.
41 ;; This is used to control cache replacement.
42 (gcs 0 :type sb!c::index)
43 ;; True if Lambda has been converted at least once, and thus warnings should
44 ;; be suppressed on additional conversions.
46 ;; For a closure, the closure date vector.
47 (closure nil :type (or null simple-vector)))
49 ;;; FIXME: Could we make this extra IN-PACKAGE go away, so that all
50 ;;; this bytecode interpreter implementation stuff was in the
51 ;;; SB!BYTECODE package?
52 (in-package "SB!IMPL")
56 ;;; This needs to be initialized in the cold load, since the top-level
57 ;;; catcher will always restore the initial value.
58 (defvar *eval-stack-top* 0)
60 ;;; general case of EVAL (except in that it can't handle toplevel
61 ;;; EVAL-WHEN magic properly): Delegate to the byte compiler.
62 (defun sb!bytecode:internal-eval (expr)
63 (funcall (compile (gensym "EVAL-TMPFUN-")
66 ;; SPEED=0,DEBUG=1 => byte-compile
67 (declare (optimize (speed 0) (debug 1)))
69 ;; Other than that, basically we care about
70 ;; compilation speed, compilation speed, and
71 ;; compilation speed. (There are cases where
72 ;; the user wants something else, but we don't
73 ;; know enough to guess that; and if he is
74 ;; unhappy about our guessed emphasis, he
75 ;; should explicitly compile his code, with
76 ;; explicit declarations to tell us what to
78 (declare (optimize (space 1) (safety 1)))
79 (declare (optimize (compilation-speed 3)))
83 ;;; Pick off a few easy cases, and call INTERNAL-EVAL for the rest. If
84 ;;; *ALREADY-EVALED-THIS* is true, then we bind it to NIL before doing
85 ;;; a call so that the effect is confined to the lexical scope of the
87 (defun eval (original-exp)
89 "Evaluate the argument in a null lexical environment, returning the
91 (declare (optimize (safety 1)))
92 (let ((exp (macroexpand original-exp)))
95 (ecase (info :variable :kind exp)
97 (values (info :variable :constant-value exp)))
101 (sb!bytecode:internal-eval original-exp))))
103 (let ((name (first exp))
104 (args (1- (length exp))))
108 (error "wrong number of args to FUNCTION:~% ~S" exp))
109 (let ((name (second exp)))
112 (eq (car name) 'setf)))
114 (sb!bytecode:internal-eval original-exp))))
117 (error "wrong number of args to QUOTE:~% ~S" exp))
121 (error "odd number of args to SETQ:~% ~S" exp))
123 (do ((name (cdr exp) (cddr name)))
125 (do ((args (cdr exp) (cddr args)))
127 ;; We duplicate the call to SET so that the
128 ;; correct value gets returned.
129 (set (first args) (eval (second args))))
130 (set (first args) (eval (second args)))))
131 (let ((symbol (first name)))
132 (case (info :variable :kind symbol)
133 ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
134 ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
135 ;; variable; the code should now act as though that
136 ;; variable is NIL. This should be tested..
138 (t (return (sb!bytecode:internal-eval original-exp))))))))
141 (dolist (x (butlast (rest exp)) (eval (car (last exp))))
145 (or (member 'eval (second exp))
146 (member :execute (second exp))))
148 (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
150 (sb!bytecode:internal-eval original-exp)))
152 (if (and (symbolp name)
153 (eq (info :function :kind name) :function))
155 (dolist (arg (rest exp))
157 (apply (symbol-function name) (args)))
158 (sb!bytecode:internal-eval original-exp))))))
162 ;;; Given a function, return three values:
163 ;;; 1] A lambda expression that could be used to define the function,
164 ;;; or NIL if the definition isn't available.
165 ;;; 2] NIL if the function was definitely defined in a null lexical
166 ;;; environment, and T otherwise.
167 ;;; 3] Some object that \"names\" the function. Although this is
168 ;;; allowed to be any object, CMU CL always returns a valid
169 ;;; function name or a string.
171 ;;; If interpreted, use the interpreter interface. Otherwise, see
172 ;;; whether it was compiled with COMPILE. If that fails, check for an
173 ;;; inline expansion.
174 (defun function-lambda-expression (fun)
175 (declare (type function fun))
176 (let* ((fun (%function-self fun))
177 (name (%function-name fun))
178 (code (sb!di::function-code-header fun))
179 (info (sb!kernel:%code-debug-info code)))
181 (let ((source (first (sb!c::compiled-debug-info-source info))))
182 (cond ((and (eq (sb!c::debug-source-from source) :lisp)
183 (eq (sb!c::debug-source-info source) fun))
184 (values (second (svref (sb!c::debug-source-name source) 0))
189 (let ((exp (info :function :inline-expansion name)))
191 (values exp nil name)
192 (values nil t name))))))
193 (values nil t name))))
195 ;;; This is like FIND-IF, except that we do it on a compiled closure's
197 (defun find-if-in-closure (test fun)
198 (dotimes (index (1- (get-closure-length fun)))
199 (let ((elt (%closure-index-ref fun index)))
200 (when (funcall test elt)
203 ;;; miscellaneous full function definitions of things which are
204 ;;; ordinarily handled magically by the compiler
206 (defun apply (function arg &rest arguments)
208 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
209 the manner of LIST*. That is, a list is made of the values of all but the
210 last argument, appended to the value of the last argument, which must be a
212 (cond ((atom arguments)
213 (apply function arg))
214 ((atom (cdr arguments))
215 (apply function (cons arg (car arguments))))
216 (t (do* ((a1 arguments a2)
217 (a2 (cdr arguments) (cdr a2)))
220 (apply function (cons arg arguments)))))))
222 (defun funcall (function &rest arguments)
224 "Call FUNCTION with the given ARGUMENTS."
225 (apply function arguments))
227 (defun values (&rest values)
229 "Return all arguments, in order, as values."
230 (values-list values))
232 (defun values-list (list)
234 "Return all of the elements of LIST, in order, as values."