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