0baf841741fca1974013affc6595bd77d4aed4dc
[sbcl.git] / src / code / target-eval.lisp
1 ;;;; This software is part of the SBCL system. See the README file for
2 ;;;; more information.
3 ;;;;
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.
9
10 (in-package "SB!IMPL")
11
12 ;;; FIXME: These probably belong in some package other than SB!IMPL.
13 ;;; Perhaps SB!KERNEL?
14
15 (defconstant call-arguments-limit most-positive-fixnum
16   #!+sb-doc
17   "The exclusive upper bound on the number of arguments which may be passed
18   to a function, including rest args.")
19
20 (defconstant lambda-parameters-limit most-positive-fixnum
21   #!+sb-doc
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.")
25
26 (defconstant multiple-values-limit most-positive-fixnum
27   #!+sb-doc
28   "The exclusive upper bound on the number of multiple-values that you can
29   have.")
30 \f
31 ;;; FIXME: more than one IN-PACKAGE in one file, ick
32 (in-package "SB!EVAL")
33
34 ;;; This is defined here so that the printer etc. can call
35 ;;; INTERPRETED-FUNCTION-P before the full interpreter is loaded.
36
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)
44             (:copier nil)
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.
48             (:print-object
49              (lambda (x stream)
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.
53   (%name nil)
54   ;; This function's debug arglist.
55   (arglist nil)
56   ;; A lambda that can be converted to get the definition.
57   (lambda nil)
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
60   ;; being removed.)
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.
67   (converted-once nil)
68   ;; For a closure, the closure date vector.
69   (closure nil :type (or null simple-vector)))
70 \f
71 (in-package "SB!IMPL")
72
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}>
76 ;;;; instead of some
77 ;;;; weird internal representation showing the environment argument and stuff.
78 ;;;; This function is called in order to try to make that happen.
79 ;;;;
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   (assert (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)
91         lambda-list)
92   (values))
93 \f
94 ;;;; EVAL and friends
95
96 ;;; This needs to be initialized in the cold load, since the top-level catcher
97 ;;; will always restore the initial value.
98 (defvar *eval-stack-top* 0)
99
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 a call
102 ;;; so that the effect is confined to the lexical scope of the EVAL-WHEN.
103 (defun eval (original-exp)
104   #!+sb-doc
105   "Evaluates its single arg in a null lexical environment, returns the
106   result or results."
107   (declare (optimize (safety 1)))
108   (let ((exp (macroexpand original-exp)))
109     (typecase exp
110       (symbol
111        (ecase (info :variable :kind exp)
112          (:constant
113           (values (info :variable :constant-value exp)))
114          ((:special :global)
115           (symbol-value exp))
116          (:alien
117           (sb!eval:internal-eval original-exp))))
118       (list
119        (let ((name (first exp))
120              (args (1- (length exp))))
121          (case name
122            (function
123             (unless (= args 1)
124               (error "wrong number of args to FUNCTION:~% ~S" exp))
125             (let ((name (second exp)))
126               (if (or (atom name)
127                       (and (consp name)
128                            (eq (car name) 'setf)))
129                   (fdefinition name)
130                   (sb!eval:make-interpreted-function name))))
131            (quote
132             (unless (= args 1)
133               (error "wrong number of args to QUOTE:~% ~S" exp))
134             (second exp))
135            (setq
136             (unless (evenp args)
137               (error "odd number of args to SETQ:~% ~S" exp))
138             (unless (zerop args)
139               (do ((name (cdr exp) (cddr name)))
140                   ((null name)
141                    (do ((args (cdr exp) (cddr args)))
142                        ((null (cddr args))
143                         ;; We duplicate the call to SET so that the correct
144                         ;; value gets returned.
145                         (set (first args) (eval (second args))))
146                      (set (first args) (eval (second args)))))
147                 (let ((symbol (first name)))
148                   (case (info :variable :kind symbol)
149                     ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
150                     ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
151                     ;; variable; the code should now act as though that
152                     ;; variable is NIL. This should be tested..
153                     (:special)
154                     (t (return (sb!eval:internal-eval original-exp))))))))
155            ((progn)
156             (when (> args 0)
157               (dolist (x (butlast (rest exp)) (eval (car (last exp))))
158                 (eval x))))
159            ((eval-when)
160             (if (and (> args 0)
161                      (or (member 'eval (second exp))
162                          (member :execute (second exp))))
163                 (when (> args 1)
164                   (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
165                     (eval x)))
166                 (sb!eval:internal-eval original-exp)))
167            (t
168             (if (and (symbolp name)
169                      (eq (info :function :kind name) :function))
170                 (collect ((args))
171                   (dolist (arg (rest exp))
172                     (args (eval arg)))
173                   (if sb!eval::*already-evaled-this*
174                       (let ((sb!eval::*already-evaled-this* nil))
175                         (apply (symbol-function name) (args)))
176                       (apply (symbol-function name) (args))))
177                 (sb!eval:internal-eval original-exp))))))
178       (t
179        exp))))
180
181 ;;; not needed in new from-scratch cross-compilation procedure -- WHN 19990714
182 #|
183 ;;; Dummy stubs for SB!EVAL:INTERNAL-EVAL and SB!EVAL:MAKE-INTERPRETED-FUNCTION
184 ;;; in case the compiler isn't loaded yet.
185 (defun sb!eval:internal-eval (x)
186   (error "attempt to evaluation a complex expression:~%     ~S~@
187           This expression must be compiled, but the compiler is not loaded."
188          x))
189 (defun sb!eval:make-interpreted-function (x)
190   (error "EVAL called on #'(lambda (x) ...) when the compiler isn't loaded:~
191           ~%     ~S~%"
192          x))
193 |#
194
195 ;;; If interpreted, use the interpreter interface. Otherwise, see
196 ;;; whether it was compiled with COMPILE. If that fails, check for an
197 ;;; inline expansion.
198 (defun function-lambda-expression (fun)
199   #!+sb-doc
200   "Given a function, return three values:
201    1] A lambda expression that could be used to define the function, or NIL if
202       the definition isn't available.
203    2] NIL if the function was definitely defined in a null lexical environment,
204       and T otherwise.
205    3] Some object that \"names\" the function. Although this is allowed to be
206       any object, CMU CL always returns a valid function name or a string."
207   (declare (type function fun))
208   (if (sb!eval:interpreted-function-p fun)
209       (sb!eval:interpreted-function-lambda-expression fun)
210       (let* ((fun (%function-self fun))
211              (name (%function-name fun))
212              (code (sb!di::function-code-header fun))
213              (info (sb!kernel:%code-debug-info code)))
214         (if info
215             (let ((source (first (sb!c::compiled-debug-info-source info))))
216               (cond ((and (eq (sb!c::debug-source-from source) :lisp)
217                           (eq (sb!c::debug-source-info source) fun))
218                      (values (second (svref (sb!c::debug-source-name source) 0))
219                              nil name))
220                     ((stringp name)
221                      (values nil t name))
222                     (t
223                      (let ((exp (info :function :inline-expansion name)))
224                        (if exp
225                            (values exp nil name)
226                            (values nil t name))))))
227             (values nil t name)))))
228
229 ;;; Like FIND-IF, only we do it on a compiled closure's environment.
230 (defun find-if-in-closure (test fun)
231   (dotimes (index (1- (get-closure-length fun)))
232     (let ((elt (%closure-index-ref fun index)))
233       (when (funcall test elt)
234         (return elt)))))
235 \f
236 ;;; function invocation
237
238 (defun apply (function arg &rest args)
239   #!+sb-doc
240   "Applies FUNCTION to a list of arguments produced by evaluating ARGS in
241   the manner of LIST*. That is, a list is made of the values of all but the
242   last argument, appended to the value of the last argument, which must be a
243   list."
244   (cond ((atom args)
245          (apply function arg))
246         ((atom (cdr args))
247          (apply function (cons arg (car args))))
248         (t (do* ((a1 args a2)
249                  (a2 (cdr args) (cdr a2)))
250                 ((atom (cdr a2))
251                  (rplacd a1 (car a2))
252                  (apply function (cons arg args)))))))
253
254 (defun funcall (function &rest arguments)
255   #!+sb-doc
256   "Calls Function with the given Arguments."
257   (apply function arguments))
258 \f
259 ;;; multiple-value forms
260
261 (defun values (&rest values)
262   #!+sb-doc
263   "Returns all arguments, in order, as values."
264   (values-list values))
265
266 (defun values-list (list)
267   #!+sb-doc
268   "Returns all of the elements of LIST, in order, as values."
269   (values-list list))