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!IMPL")
15 ;;; FIXME: These probably belong in some package other than SB!IMPL.
16 ;;; Perhaps SB!KERNEL?
18 (defconstant call-arguments-limit most-positive-fixnum
20 "The exclusive upper bound on the number of arguments which may be passed
21 to a function, including rest args.")
23 (defconstant lambda-parameters-limit most-positive-fixnum
25 "The exclusive upper bound on the number of parameters which may be specifed
26 in a given lambda list. This is actually the limit on required and optional
27 parameters. With &key and &aux you can get more.")
29 (defconstant multiple-values-limit most-positive-fixnum
31 "The exclusive upper bound on the number of multiple-values that you can
34 ;;; FIXME: more than one IN-PACKAGE in one file, ick
35 (in-package "SB!EVAL")
37 ;;; This is defined here so that the printer etc. can call
38 ;;; INTERPRETED-FUNCTION-P before the full interpreter is loaded.
40 ;;; an interpreted function
41 (defstruct (interpreted-function
42 (:alternate-metaclass sb!kernel:funcallable-instance
43 sb!kernel:funcallable-structure-class
44 sb!kernel:make-funcallable-structure-class)
45 (:type sb!kernel:funcallable-structure)
46 (:constructor %make-interpreted-function)
48 ;; FIXME: Binding PRINT-OBJECT isn't going to help unless
49 ;; we fix the print-a-funcallable-instance code so that
50 ;; it calls PRINT-OBJECT in this case.
53 (print-unreadable-object (x stream :identity t)
54 (sb!impl::output-interpreted-function x stream)))))
55 ;; The name of this interpreted function, or NIL if none specified.
57 ;; This function's debug arglist.
59 ;; A lambda that can be converted to get the definition.
61 ;; If this function has been converted, then this is the XEP. If this is
62 ;; false, then the function is not in the cache (or is in the process of
64 (definition nil :type (or sb!c::clambda null))
65 ;; The number of consequtive GCs that this function has been unused. This is
66 ;; used to control cache replacement.
67 (gcs 0 :type sb!c::index)
68 ;; True if Lambda has been converted at least once, and thus warnings should
69 ;; be suppressed on additional conversions.
71 ;; For a closure, the closure date vector.
72 (closure nil :type (or null simple-vector)))
74 (in-package "SB!IMPL")
76 ;;;; One of the steps in building a nice debuggable macro is changing
77 ;;;; its MACRO-FUNCTION to print as e.g.
78 ;;;; #<Interpreted Function "DEFMACRO BAR" {9166351}>
80 ;;;; weird internal representation showing the environment argument and stuff.
81 ;;;; This function is called in order to try to make that happen.
83 ;;;; When we're running in the target SBCL, we own the INTERPRETED-FUNCTION
84 ;;;; definition, and we can do this; that's what the definition below does.
85 ;;;; When we're a Python cross-compiler running in some arbitrary ANSI Common
86 ;;;; Lisp, we can't do this (and we don't care that much about making nice
87 ;;;; debuggable macros anyway). In that environment, a stub no-op version of
88 ;;;; this function is used.
89 (defun try-to-rename-interpreted-function-as-macro (f name lambda-list)
90 (assert (sb!eval:interpreted-function-p f))
91 (setf (sb!eval:interpreted-function-name f)
92 (format nil "DEFMACRO ~S" name)
93 (sb!eval:interpreted-function-arglist f)
99 ;;; This needs to be initialized in the cold load, since the top-level catcher
100 ;;; will always restore the initial value.
101 (defvar *eval-stack-top* 0)
103 ;;; Pick off a few easy cases, and call INTERNAL-EVAL for the rest. If
104 ;;; *ALREADY-EVALED-THIS* is true, then we bind it to NIL before doing a call
105 ;;; so that the effect is confined to the lexical scope of the EVAL-WHEN.
106 (defun eval (original-exp)
108 "Evaluates its single arg in a null lexical environment, returns the
110 (declare (optimize (safety 1)))
111 (let ((exp (macroexpand original-exp)))
114 (ecase (info :variable :kind exp)
116 (values (info :variable :constant-value exp)))
120 (sb!eval:internal-eval original-exp))))
122 (let ((name (first exp))
123 (args (1- (length exp))))
127 (error "wrong number of args to FUNCTION:~% ~S" exp))
128 (let ((name (second exp)))
131 (eq (car name) 'setf)))
133 (sb!eval:make-interpreted-function name))))
136 (error "wrong number of args to QUOTE:~% ~S" exp))
140 (error "odd number of args to SETQ:~% ~S" exp))
142 (do ((name (cdr exp) (cddr name)))
144 (do ((args (cdr exp) (cddr args)))
146 ;; We duplicate the call to SET so that the correct
147 ;; value gets returned.
148 (set (first args) (eval (second args))))
149 (set (first args) (eval (second args)))))
150 (let ((symbol (first name)))
151 (case (info :variable :kind symbol)
152 ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
153 ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
154 ;; variable; the code should now act as though that
155 ;; variable is NIL. This should be tested..
157 (t (return (sb!eval:internal-eval original-exp))))))))
160 (dolist (x (butlast (rest exp)) (eval (car (last exp))))
164 (or (member 'eval (second exp))
165 (member :execute (second exp))))
167 (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
169 (sb!eval:internal-eval original-exp)))
171 (if (and (symbolp name)
172 (eq (info :function :kind name) :function))
174 (dolist (arg (rest exp))
176 (if sb!eval::*already-evaled-this*
177 (let ((sb!eval::*already-evaled-this* nil))
178 (apply (symbol-function name) (args)))
179 (apply (symbol-function name) (args))))
180 (sb!eval:internal-eval original-exp))))))
184 ;;; not needed in new from-scratch cross-compilation procedure -- WHN 19990714
186 ;;; Dummy stubs for SB!EVAL:INTERNAL-EVAL and SB!EVAL:MAKE-INTERPRETED-FUNCTION
187 ;;; in case the compiler isn't loaded yet.
188 (defun sb!eval:internal-eval (x)
189 (error "attempt to evaluation a complex expression:~% ~S~@
190 This expression must be compiled, but the compiler is not loaded."
192 (defun sb!eval:make-interpreted-function (x)
193 (error "EVAL called on #'(lambda (x) ...) when the compiler isn't loaded:~
198 ;;; If interpreted, use the interpreter interface. Otherwise, see
199 ;;; whether it was compiled with COMPILE. If that fails, check for an
200 ;;; inline expansion.
201 (defun function-lambda-expression (fun)
203 "Given a function, return three values:
204 1] A lambda expression that could be used to define the function, or NIL if
205 the definition isn't available.
206 2] NIL if the function was definitely defined in a null lexical environment,
208 3] Some object that \"names\" the function. Although this is allowed to be
209 any object, CMU CL always returns a valid function name or a string."
210 (declare (type function fun))
211 (if (sb!eval:interpreted-function-p fun)
212 (sb!eval:interpreted-function-lambda-expression fun)
213 (let* ((fun (%function-self fun))
214 (name (%function-name fun))
215 (code (sb!di::function-code-header fun))
216 (info (sb!kernel:%code-debug-info code)))
218 (let ((source (first (sb!c::compiled-debug-info-source info))))
219 (cond ((and (eq (sb!c::debug-source-from source) :lisp)
220 (eq (sb!c::debug-source-info source) fun))
221 (values (second (svref (sb!c::debug-source-name source) 0))
226 (let ((exp (info :function :inline-expansion name)))
228 (values exp nil name)
229 (values nil t name))))))
230 (values nil t name)))))
232 ;;; Like FIND-IF, only we do it on a compiled closure's environment.
233 (defun find-if-in-closure (test fun)
234 (dotimes (index (1- (get-closure-length fun)))
235 (let ((elt (%closure-index-ref fun index)))
236 (when (funcall test elt)
239 ;;; function invocation
241 (defun apply (function arg &rest args)
243 "Applies FUNCTION to a list of arguments produced by evaluating ARGS in
244 the manner of LIST*. That is, a list is made of the values of all but the
245 last argument, appended to the value of the last argument, which must be a
248 (apply function arg))
250 (apply function (cons arg (car args))))
251 (t (do* ((a1 args a2)
252 (a2 (cdr args) (cdr a2)))
255 (apply function (cons arg args)))))))
257 (defun funcall (function &rest arguments)
259 "Calls Function with the given Arguments."
260 (apply function arguments))
262 ;;; multiple-value forms
264 (defun values (&rest values)
266 "Returns all arguments, in order, as values."
267 (values-list values))
269 (defun values-list (list)
271 "Returns all of the elements of LIST, in order, as values."