f518110a5417827198fe604a4ff221abbcb95235
[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   (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)
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
97 ;;; catcher 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
102 ;;; a call so that the effect is confined to the lexical scope of the
103 ;;; EVAL-WHEN.
104 (defun eval (original-exp)
105   #!+sb-doc
106   "Evaluates its single argument in a null lexical environment, returns the
107   result or results."
108   (declare (optimize (safety 1)))
109   (let ((exp (macroexpand original-exp)))
110     (typecase exp
111       (symbol
112        (ecase (info :variable :kind exp)
113          (:constant
114           (values (info :variable :constant-value exp)))
115          ((:special :global)
116           (symbol-value exp))
117          (:alien
118           (sb!eval:internal-eval original-exp))))
119       (list
120        (let ((name (first exp))
121              (args (1- (length exp))))
122          (case name
123            (function
124             (unless (= args 1)
125               (error "wrong number of args to FUNCTION:~% ~S" exp))
126             (let ((name (second exp)))
127               (if (or (atom name)
128                       (and (consp name)
129                            (eq (car name) 'setf)))
130                   (fdefinition name)
131                   (sb!eval:make-interpreted-function name))))
132            (quote
133             (unless (= args 1)
134               (error "wrong number of args to QUOTE:~% ~S" exp))
135             (second exp))
136            (setq
137             (unless (evenp args)
138               (error "odd number of args to SETQ:~% ~S" exp))
139             (unless (zerop args)
140               (do ((name (cdr exp) (cddr name)))
141                   ((null name)
142                    (do ((args (cdr exp) (cddr args)))
143                        ((null (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..
154                     (:special)
155                     (t (return (sb!eval:internal-eval original-exp))))))))
156            ((progn)
157             (when (> args 0)
158               (dolist (x (butlast (rest exp)) (eval (car (last exp))))
159                 (eval x))))
160            ((eval-when)
161             (if (and (> args 0)
162                      (or (member 'eval (second exp))
163                          (member :execute (second exp))))
164                 (when (> args 1)
165                   (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
166                     (eval x)))
167                 (sb!eval:internal-eval original-exp)))
168            (t
169             (if (and (symbolp name)
170                      (eq (info :function :kind name) :function))
171                 (collect ((args))
172                   (dolist (arg (rest exp))
173                     (args (eval arg)))
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))))))
179       (t
180        exp))))
181
182 ;;; not needed in new from-scratch cross-compilation procedure -- WHN 19990714
183 #|
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."
189          x))
190 (defun sb!eval:make-interpreted-function (x)
191   (error "EVAL called on #'(lambda (x) ...) when the compiler isn't loaded:~
192           ~%     ~S~%"
193          x))
194 |#
195
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)
200   #!+sb-doc
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,
205       and T otherwise.
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)))
215         (if info
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))
220                              nil name))
221                     ((stringp name)
222                      (values nil t name))
223                     (t
224                      (let ((exp (info :function :inline-expansion name)))
225                        (if exp
226                            (values exp nil name)
227                            (values nil t name))))))
228             (values nil t name)))))
229
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)
235         (return elt)))))
236 \f
237 ;;; function invocation
238
239 (defun apply (function arg &rest args)
240   #!+sb-doc
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
244   list."
245   (cond ((atom args)
246          (apply function arg))
247         ((atom (cdr args))
248          (apply function (cons arg (car args))))
249         (t (do* ((a1 args a2)
250                  (a2 (cdr args) (cdr a2)))
251                 ((atom (cdr a2))
252                  (rplacd a1 (car a2))
253                  (apply function (cons arg args)))))))
254
255 (defun funcall (function &rest arguments)
256   #!+sb-doc
257   "Calls Function with the given Arguments."
258   (apply function arguments))
259 \f
260 ;;; multiple-value forms
261
262 (defun values (&rest values)
263   #!+sb-doc
264   "Returns all arguments, in order, as values."
265   (values-list values))
266
267 (defun values-list (list)
268   #!+sb-doc
269   "Returns all of the elements of LIST, in order, as values."
270   (values-list list))