0.pre7.17:
[sbcl.git] / src / code / 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!BYTECODE")
11
12 ;;; This is defined here so that the printer etc. can call
13 ;;; INTERPRETED-FUNCTION-P before the full interpreter is loaded.
14
15 ;;; an interpreted function
16 (defstruct (interpreted-function
17             (:alternate-metaclass sb!kernel:funcallable-instance
18                                   sb!kernel:funcallable-structure-class
19                                   sb!kernel:make-funcallable-structure-class)
20             (:type sb!kernel:funcallable-structure)
21             (:constructor %make-interpreted-function)
22             (:copier nil)
23             ;; FIXME: Binding PRINT-OBJECT isn't going to help unless
24             ;; we fix the print-a-funcallable-instance code so that
25             ;; it calls PRINT-OBJECT in this case.
26             (:print-object
27              (lambda (x stream)
28                (print-unreadable-object (x stream :identity t)
29                  (interpreted-function-%name x)))))
30   ;; The name of this interpreted function, or NIL if none specified.
31   (%name nil)
32   ;; This function's debug arglist.
33   (arglist nil)
34   ;; A lambda that can be converted to get the definition.
35   (lambda nil)
36   ;; If this function has been converted, then this is the XEP. If this is
37   ;; false, then the function is not in the cache (or is in the process of
38   ;; being removed.)
39   (definition nil :type (or sb!c::clambda null))
40   ;; The number of consecutive GCs that this function has been unused.
41   ;; This is used to control cache replacement.
42   (gcs 0 :type sb!c::index)
43   ;; True if Lambda has been converted at least once, and thus warnings should
44   ;; be suppressed on additional conversions.
45   (converted-once nil)
46   ;; For a closure, the closure date vector.
47   (closure nil :type (or null simple-vector)))
48 \f
49 ;;; FIXME: Could we make this extra IN-PACKAGE go away, so that all
50 ;;; this bytecode interpreter implementation stuff was in the
51 ;;; SB!BYTECODE package?
52 (in-package "SB!IMPL")
53
54 ;;;; EVAL and friends
55
56 ;;; This needs to be initialized in the cold load, since the top-level
57 ;;; catcher will always restore the initial value.
58 (defvar *eval-stack-top* 0)
59
60 ;;; general case of EVAL (except in that it can't handle toplevel
61 ;;; EVAL-WHEN magic properly): Delegate to the byte compiler.
62 (defun sb!bytecode:internal-eval (expr)
63   (funcall (compile (gensym "EVAL-TMPFUN-")
64                     `(lambda ()
65
66                        ;; SPEED=0,DEBUG=1 => byte-compile
67                        (declare (optimize (speed 0) (debug 1))) 
68
69                        ;; Other than that, basically we care about
70                        ;; compilation speed, compilation speed, and
71                        ;; compilation speed. (There are cases where
72                        ;; the user wants something else, but we don't
73                        ;; know enough to guess that; and if he is
74                        ;; unhappy about our guessed emphasis, he
75                        ;; should explicitly compile his code, with
76                        ;; explicit declarations to tell us what to
77                        ;; emphasize.)
78                        (declare (optimize (space 1) (safety 1)))
79                        (declare (optimize (compilation-speed 3)))
80
81                        ,expr))))
82
83 ;;; Pick off a few easy cases, and call INTERNAL-EVAL for the rest. If
84 ;;; *ALREADY-EVALED-THIS* is true, then we bind it to NIL before doing
85 ;;; a call so that the effect is confined to the lexical scope of the
86 ;;; EVAL-WHEN.
87 (defun eval (original-exp)
88   #!+sb-doc
89   "Evaluate the argument in a null lexical environment, returning the
90   result or results."
91   (declare (optimize (safety 1)))
92   (let ((exp (macroexpand original-exp)))
93     (typecase exp
94       (symbol
95        (ecase (info :variable :kind exp)
96          (:constant
97           (values (info :variable :constant-value exp)))
98          ((:special :global)
99           (symbol-value exp))
100          (:alien
101           (sb!bytecode:internal-eval original-exp))))
102       (list
103        (let ((name (first exp))
104              (args (1- (length exp))))
105          (case name
106            (function
107             (unless (= args 1)
108               (error "wrong number of args to FUNCTION:~% ~S" exp))
109             (let ((name (second exp)))
110               (if (or (atom name)
111                       (and (consp name)
112                            (eq (car name) 'setf)))
113                   (fdefinition name)
114                   (sb!bytecode:internal-eval original-exp))))
115            (quote
116             (unless (= args 1)
117               (error "wrong number of args to QUOTE:~% ~S" exp))
118             (second exp))
119            (setq
120             (unless (evenp args)
121               (error "odd number of args to SETQ:~% ~S" exp))
122             (unless (zerop args)
123               (do ((name (cdr exp) (cddr name)))
124                   ((null name)
125                    (do ((args (cdr exp) (cddr args)))
126                        ((null (cddr args))
127                         ;; We duplicate the call to SET so that the
128                         ;; correct value gets returned.
129                         (set (first args) (eval (second args))))
130                      (set (first args) (eval (second args)))))
131                 (let ((symbol (first name)))
132                   (case (info :variable :kind symbol)
133                     ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
134                     ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
135                     ;; variable; the code should now act as though that
136                     ;; variable is NIL. This should be tested..
137                     (:special)
138                     (t (return (sb!bytecode:internal-eval original-exp))))))))
139            ((progn)
140             (when (> args 0)
141               (dolist (x (butlast (rest exp)) (eval (car (last exp))))
142                 (eval x))))
143            ((eval-when)
144             (if (and (> args 0)
145                      (or (member 'eval (second exp))
146                          (member :execute (second exp))))
147                 (when (> args 1)
148                   (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
149                     (eval x)))
150                 (sb!bytecode:internal-eval original-exp)))
151            (t
152             (if (and (symbolp name)
153                      (eq (info :function :kind name) :function))
154                 (collect ((args))
155                   (dolist (arg (rest exp))
156                     (args (eval arg)))
157                   (apply (symbol-function name) (args)))
158                 (sb!bytecode:internal-eval original-exp))))))
159       (t
160        exp))))
161
162 ;;; Given a function, return three values:
163 ;;; 1] A lambda expression that could be used to define the function,
164 ;;;    or NIL if the definition isn't available.
165 ;;; 2] NIL if the function was definitely defined in a null lexical
166 ;;;    environment, and T otherwise.
167 ;;; 3] Some object that \"names\" the function. Although this is
168 ;;;    allowed to be any object, CMU CL always returns a valid
169 ;;;    function name or a string.
170 ;;;
171 ;;; If interpreted, use the interpreter interface. Otherwise, see
172 ;;; whether it was compiled with COMPILE. If that fails, check for an
173 ;;; inline expansion.
174 (defun function-lambda-expression (fun)
175   (declare (type function fun))
176   (let* ((fun (%function-self fun))
177          (name (%function-name fun))
178          (code (sb!di::function-code-header fun))
179          (info (sb!kernel:%code-debug-info code)))
180     (if info
181         (let ((source (first (sb!c::compiled-debug-info-source info))))
182           (cond ((and (eq (sb!c::debug-source-from source) :lisp)
183                       (eq (sb!c::debug-source-info source) fun))
184                  (values (second (svref (sb!c::debug-source-name source) 0))
185                          nil name))
186                 ((stringp name)
187                  (values nil t name))
188                 (t
189                  (let ((exp (info :function :inline-expansion name)))
190                    (if exp
191                        (values exp nil name)
192                        (values nil t name))))))
193         (values nil t name))))
194
195 ;;; This is like FIND-IF, except that we do it on a compiled closure's
196 ;;; environment.
197 (defun find-if-in-closure (test fun)
198   (dotimes (index (1- (get-closure-length fun)))
199     (let ((elt (%closure-index-ref fun index)))
200       (when (funcall test elt)
201         (return elt)))))
202 \f
203 ;;; miscellaneous full function definitions of things which are
204 ;;; ordinarily handled magically by the compiler
205
206 (defun apply (function arg &rest arguments)
207   #!+sb-doc
208   "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
209   the manner of LIST*. That is, a list is made of the values of all but the
210   last argument, appended to the value of the last argument, which must be a
211   list."
212   (cond ((atom arguments)
213          (apply function arg))
214         ((atom (cdr arguments))
215          (apply function (cons arg (car arguments))))
216         (t (do* ((a1 arguments a2)
217                  (a2 (cdr arguments) (cdr a2)))
218                 ((atom (cdr a2))
219                  (rplacd a1 (car a2))
220                  (apply function (cons arg arguments)))))))
221
222 (defun funcall (function &rest arguments)
223   #!+sb-doc
224   "Call FUNCTION with the given ARGUMENTS."
225   (apply function arguments))
226
227 (defun values (&rest values)
228   #!+sb-doc
229   "Return all arguments, in order, as values."
230   (values-list values))
231
232 (defun values-list (list)
233   #!+sb-doc
234   "Return all of the elements of LIST, in order, as values."
235   (values-list list))