Initial revision
[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 (file-comment
13   "$Header$")
14
15 ;;; FIXME: These probably belong in some package other than SB!IMPL.
16 ;;; Perhaps SB!KERNEL?
17
18 (defconstant call-arguments-limit most-positive-fixnum
19   #!+sb-doc
20   "The exclusive upper bound on the number of arguments which may be passed
21   to a function, including rest args.")
22
23 (defconstant lambda-parameters-limit most-positive-fixnum
24   #!+sb-doc
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.")
28
29 (defconstant multiple-values-limit most-positive-fixnum
30   #!+sb-doc
31   "The exclusive upper bound on the number of multiple-values that you can
32   have.")
33 \f
34 ;;; FIXME: more than one IN-PACKAGE in one file, ick
35 (in-package "SB!EVAL")
36
37 ;;; This is defined here so that the printer etc. can call
38 ;;; INTERPRETED-FUNCTION-P before the full interpreter is loaded.
39
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)
47             (:copier nil)
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.
51             (:print-object
52              (lambda (x stream)
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.
56   (%name nil)
57   ;; This function's debug arglist.
58   (arglist nil)
59   ;; A lambda that can be converted to get the definition.
60   (lambda nil)
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
63   ;; being removed.)
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.
70   (converted-once nil)
71   ;; For a closure, the closure date vector.
72   (closure nil :type (or null simple-vector)))
73 \f
74 (in-package "SB!IMPL")
75
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}>
79 ;;;; instead of some
80 ;;;; weird internal representation showing the environment argument and stuff.
81 ;;;; This function is called in order to try to make that happen.
82 ;;;;
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)
94         lambda-list)
95   (values))
96 \f
97 ;;;; EVAL and friends
98
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)
102
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)
107   #!+sb-doc
108   "Evaluates its single arg in a null lexical environment, returns the
109   result or results."
110   (declare (optimize (safety 1)))
111   (let ((exp (macroexpand original-exp)))
112     (typecase exp
113       (symbol
114        (ecase (info :variable :kind exp)
115          (:constant
116           (values (info :variable :constant-value exp)))
117          ((:special :global)
118           (symbol-value exp))
119          (:alien
120           (sb!eval:internal-eval original-exp))))
121       (list
122        (let ((name (first exp))
123              (args (1- (length exp))))
124          (case name
125            (function
126             (unless (= args 1)
127               (error "wrong number of args to FUNCTION:~% ~S" exp))
128             (let ((name (second exp)))
129               (if (or (atom name)
130                       (and (consp name)
131                            (eq (car name) 'setf)))
132                   (fdefinition name)
133                   (sb!eval:make-interpreted-function name))))
134            (quote
135             (unless (= args 1)
136               (error "wrong number of args to QUOTE:~% ~S" exp))
137             (second exp))
138            (setq
139             (unless (evenp args)
140               (error "odd number of args to SETQ:~% ~S" exp))
141             (unless (zerop args)
142               (do ((name (cdr exp) (cddr name)))
143                   ((null name)
144                    (do ((args (cdr exp) (cddr args)))
145                        ((null (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..
156                     (:special)
157                     (t (return (sb!eval:internal-eval original-exp))))))))
158            ((progn)
159             (when (> args 0)
160               (dolist (x (butlast (rest exp)) (eval (car (last exp))))
161                 (eval x))))
162            ((eval-when)
163             (if (and (> args 0)
164                      (or (member 'eval (second exp))
165                          (member :execute (second exp))))
166                 (when (> args 1)
167                   (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
168                     (eval x)))
169                 (sb!eval:internal-eval original-exp)))
170            (t
171             (if (and (symbolp name)
172                      (eq (info :function :kind name) :function))
173                 (collect ((args))
174                   (dolist (arg (rest exp))
175                     (args (eval arg)))
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))))))
181       (t
182        exp))))
183
184 ;;; not needed in new from-scratch cross-compilation procedure -- WHN 19990714
185 #|
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."
191          x))
192 (defun sb!eval:make-interpreted-function (x)
193   (error "EVAL called on #'(lambda (x) ...) when the compiler isn't loaded:~
194           ~%     ~S~%"
195          x))
196 |#
197
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)
202   #!+sb-doc
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,
207       and T otherwise.
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)))
217         (if info
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))
222                              nil name))
223                     ((stringp name)
224                      (values nil t name))
225                     (t
226                      (let ((exp (info :function :inline-expansion name)))
227                        (if exp
228                            (values exp nil name)
229                            (values nil t name))))))
230             (values nil t name)))))
231
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)
237         (return elt)))))
238 \f
239 ;;; function invocation
240
241 (defun apply (function arg &rest args)
242   #!+sb-doc
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
246   list."
247   (cond ((atom args)
248          (apply function arg))
249         ((atom (cdr args))
250          (apply function (cons arg (car args))))
251         (t (do* ((a1 args a2)
252                  (a2 (cdr args) (cdr a2)))
253                 ((atom (cdr a2))
254                  (rplacd a1 (car a2))
255                  (apply function (cons arg args)))))))
256
257 (defun funcall (function &rest arguments)
258   #!+sb-doc
259   "Calls Function with the given Arguments."
260   (apply function arguments))
261 \f
262 ;;; multiple-value forms
263
264 (defun values (&rest values)
265   #!+sb-doc
266   "Returns all arguments, in order, as values."
267   (values-list values))
268
269 (defun values-list (list)
270   #!+sb-doc
271   "Returns all of the elements of LIST, in order, as values."
272   (values-list list))