00a02002a586e6e05daeeb2e8d02a570764335bc
[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!EVAL")
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 (in-package "SB!IMPL")
50
51 ;;;; One of the steps in building a nice debuggable macro is changing
52 ;;;; its MACRO-FUNCTION to print as e.g.
53 ;;;;   #<Interpreted Function "DEFMACRO BAR" {9166351}>
54 ;;;; instead of some weird internal representation showing the
55 ;;;; environment argument and stuff. This function is called in order
56 ;;;; to try to make that happen.
57 ;;;;
58 ;;;; When we're running in the target SBCL, we own the
59 ;;;; INTERPRETED-FUNCTION definition, and we can do this; that's what
60 ;;;; the definition below does. When we're a Python cross-compiler
61 ;;;; running in some arbitrary ANSI Common Lisp, we can't do this (and
62 ;;;; we don't care that much about making nice debuggable macros
63 ;;;; anyway). In that environment, a stub no-op version of this
64 ;;;; function is used.
65 (defun try-to-rename-interpreted-function-as-macro (f name lambda-list)
66   (aver (sb!eval:interpreted-function-p f))
67   (setf (sb!eval:interpreted-function-name f)
68         (format nil "DEFMACRO ~S" name)
69         (sb!eval:interpreted-function-arglist f)
70         lambda-list)
71   (values))
72 \f
73 ;;;; EVAL and friends
74
75 ;;; This needs to be initialized in the cold load, since the top-level
76 ;;; catcher will always restore the initial value.
77 (defvar *eval-stack-top* 0)
78
79 ;;; Pick off a few easy cases, and call INTERNAL-EVAL for the rest. If
80 ;;; *ALREADY-EVALED-THIS* is true, then we bind it to NIL before doing
81 ;;; a call so that the effect is confined to the lexical scope of the
82 ;;; EVAL-WHEN.
83 (defun eval (original-exp)
84   #!+sb-doc
85   "Evaluate the argument in a null lexical environment, returning the
86   result or results."
87   (declare (optimize (safety 1)))
88   (let ((exp (macroexpand original-exp)))
89     (typecase exp
90       (symbol
91        (ecase (info :variable :kind exp)
92          (:constant
93           (values (info :variable :constant-value exp)))
94          ((:special :global)
95           (symbol-value exp))
96          (:alien
97           (sb!eval:internal-eval original-exp))))
98       (list
99        (let ((name (first exp))
100              (args (1- (length exp))))
101          (case name
102            (function
103             (unless (= args 1)
104               (error "wrong number of args to FUNCTION:~% ~S" exp))
105             (let ((name (second exp)))
106               (if (or (atom name)
107                       (and (consp name)
108                            (eq (car name) 'setf)))
109                   (fdefinition name)
110                   (sb!eval:make-interpreted-function name))))
111            (quote
112             (unless (= args 1)
113               (error "wrong number of args to QUOTE:~% ~S" exp))
114             (second exp))
115            (setq
116             (unless (evenp args)
117               (error "odd number of args to SETQ:~% ~S" exp))
118             (unless (zerop args)
119               (do ((name (cdr exp) (cddr name)))
120                   ((null name)
121                    (do ((args (cdr exp) (cddr args)))
122                        ((null (cddr args))
123                         ;; We duplicate the call to SET so that the
124                         ;; correct value gets returned.
125                         (set (first args) (eval (second args))))
126                      (set (first args) (eval (second args)))))
127                 (let ((symbol (first name)))
128                   (case (info :variable :kind symbol)
129                     ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
130                     ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
131                     ;; variable; the code should now act as though that
132                     ;; variable is NIL. This should be tested..
133                     (:special)
134                     (t (return (sb!eval:internal-eval original-exp))))))))
135            ((progn)
136             (when (> args 0)
137               (dolist (x (butlast (rest exp)) (eval (car (last exp))))
138                 (eval x))))
139            ((eval-when)
140             (if (and (> args 0)
141                      (or (member 'eval (second exp))
142                          (member :execute (second exp))))
143                 (when (> args 1)
144                   (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
145                     (eval x)))
146                 (sb!eval:internal-eval original-exp)))
147            (t
148             (if (and (symbolp name)
149                      (eq (info :function :kind name) :function))
150                 (collect ((args))
151                   (dolist (arg (rest exp))
152                     (args (eval arg)))
153                   (if sb!eval::*already-evaled-this*
154                       (let ((sb!eval::*already-evaled-this* nil))
155                         (apply (symbol-function name) (args)))
156                       (apply (symbol-function name) (args))))
157                 (sb!eval:internal-eval original-exp))))))
158       (t
159        exp))))
160
161 ;;; general case of EVAL (except in that it can't handle toplevel
162 ;;; EVAL-WHEN magic properly): Delegate to the byte compiler.
163 #!-sb-interpreter
164 (defun internal-eval (expr)
165   (let ((name (gensym "EVAL-TMPFUN-")))
166     (multiple-value-bind (fun warnings-p failure-p)
167         (compile name
168                  `(lambda ()
169                     (declare (optimize (speed 0) (debug 1))) ; to byte-compile
170                     (declare (optimize (space 1) (safety 1)))
171                     (declare (optimize (compilation-speed 3)))
172                     ,expr))
173       (declare (ignore warnings-p))
174       (if failure-p
175           (error 'simple-program-error
176                  :format-control
177                  "~@<failure when precompiling ~2I~_~S ~I~_ for ~S"
178                  :format-arguments (list expr 'eval))
179           (funcall fun)))))
180
181 ;;; Given a function, return three values:
182 ;;; 1] A lambda expression that could be used to define the function,
183 ;;;    or NIL if the definition isn't available.
184 ;;; 2] NIL if the function was definitely defined in a null lexical
185 ;;;    environment, and T otherwise.
186 ;;; 3] Some object that \"names\" the function. Although this is
187 ;;;    allowed to be any object, CMU CL always returns a valid
188 ;;;    function name or a string.
189 ;;;
190 ;;; If interpreted, use the interpreter interface. Otherwise, see
191 ;;; whether it was compiled with COMPILE. If that fails, check for an
192 ;;; inline expansion.
193 (defun function-lambda-expression (fun)
194   (declare (type function fun))
195   (if (sb!eval:interpreted-function-p fun)
196       (sb!eval:interpreted-function-lambda-expression fun)
197       (let* ((fun (%function-self fun))
198              (name (%function-name fun))
199              (code (sb!di::function-code-header fun))
200              (info (sb!kernel:%code-debug-info code)))
201         (if info
202             (let ((source (first (sb!c::compiled-debug-info-source info))))
203               (cond ((and (eq (sb!c::debug-source-from source) :lisp)
204                           (eq (sb!c::debug-source-info source) fun))
205                      (values (second (svref (sb!c::debug-source-name source) 0))
206                              nil name))
207                     ((stringp name)
208                      (values nil t name))
209                     (t
210                      (let ((exp (info :function :inline-expansion name)))
211                        (if exp
212                            (values exp nil name)
213                            (values nil t name))))))
214             (values nil t name)))))
215
216 ;;; Like FIND-IF, only we do it on a compiled closure's environment.
217 (defun find-if-in-closure (test fun)
218   (dotimes (index (1- (get-closure-length fun)))
219     (let ((elt (%closure-index-ref fun index)))
220       (when (funcall test elt)
221         (return elt)))))
222 \f
223 ;;; function invocation
224
225 (defun apply (function arg &rest args)
226   #!+sb-doc
227   "Applies FUNCTION to a list of arguments produced by evaluating ARGS in
228   the manner of LIST*. That is, a list is made of the values of all but the
229   last argument, appended to the value of the last argument, which must be a
230   list."
231   (cond ((atom args)
232          (apply function arg))
233         ((atom (cdr args))
234          (apply function (cons arg (car args))))
235         (t (do* ((a1 args a2)
236                  (a2 (cdr args) (cdr a2)))
237                 ((atom (cdr a2))
238                  (rplacd a1 (car a2))
239                  (apply function (cons arg args)))))))
240
241 (defun funcall (function &rest arguments)
242   #!+sb-doc
243   "Calls Function with the given Arguments."
244   (apply function arguments))
245 \f
246 ;;; multiple-value forms
247
248 (defun values (&rest values)
249   #!+sb-doc
250   "Returns all arguments, in order, as values."
251   (values-list values))
252
253 (defun values-list (list)
254   #!+sb-doc
255   "Returns all of the elements of LIST, in order, as values."
256   (values-list list))