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