restore non-consingness of WITH-SPINLOCK
[sbcl/pkhuong.git] / src / code / eval.lisp
blob39c295d7c83dea4e2a469f91f50344f2d58e0788
1 ;;;; EVAL and friends
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!IMPL")
14 (defparameter *eval-calls* 0)
16 (defun !eval-cold-init ()
17 (setf *eval-calls* 0
18 *evaluator-mode* :compile)
19 #!+sb-eval
20 (setf sb!eval::*eval-level* -1
21 sb!eval::*eval-verbose* nil))
23 ;;; general case of EVAL (except in that it can't handle toplevel
24 ;;; EVAL-WHEN magic properly): Delegate to #'COMPILE.
25 (defun %simple-eval (expr lexenv)
26 ;; FIXME: It might be nice to quieten the toplevel by muffling
27 ;; warnings generated by this compilation (since we're about to
28 ;; execute the results irrespective of the warnings). We might want
29 ;; to be careful about not muffling warnings arising from inner
30 ;; evaluations/compilations, though [e.g. the ignored variable in
31 ;; (DEFUN FOO (X) 1)]. -- CSR, 2003-05-13
32 (let* (;; why PROGN? So that attempts to eval free declarations
33 ;; signal errors rather than return NIL. -- CSR, 2007-05-01
34 (lambda `(lambda () (progn ,expr)))
35 (fun (sb!c:compile-in-lexenv nil lambda lexenv)))
36 (funcall fun)))
38 ;;; Handle PROGN and implicit PROGN.
39 (defun simple-eval-progn-body (progn-body lexenv)
40 (unless (list-with-length-p progn-body)
41 (let ((*print-circle* t))
42 (error 'simple-program-error
43 :format-control
44 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
45 :format-arguments (list progn-body))))
46 ;; Note:
47 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
48 ;; need to take care to return all the values of the final EVAL.
49 ;; * It's left as an exercise to the reader to verify that this
50 ;; gives the right result when PROGN-BODY is NIL, because
51 ;; (FIRST NIL) = (REST NIL) = NIL.
52 (do* ((i progn-body rest-i)
53 (rest-i (rest i) (rest i)))
54 (nil)
55 (if rest-i ; if not last element of list
56 (simple-eval-in-lexenv (first i) lexenv)
57 (return (simple-eval-in-lexenv (first i) lexenv)))))
59 (defun simple-eval-locally (exp lexenv &key vars)
60 (multiple-value-bind (body decls)
61 (parse-body (rest exp) :doc-string-allowed nil)
62 (let ((lexenv
63 ;; KLUDGE: Uh, yeah. I'm not anticipating
64 ;; winning any prizes for this code, which was
65 ;; written on a "let's get it to work" basis.
66 ;; These seem to be the variables that need
67 ;; bindings for PROCESS-DECLS to work
68 ;; (*FREE-FUNS* and *FREE-VARS* so that
69 ;; references to free functions and variables
70 ;; in the declarations can be noted;
71 ;; *UNDEFINED-WARNINGS* so that warnings about
72 ;; undefined things can be accumulated [and
73 ;; then thrown away, as it happens]). -- CSR,
74 ;; 2002-10-24
75 (let* ((sb!c:*lexenv* lexenv)
76 (sb!c::*free-funs* (make-hash-table :test 'equal))
77 (sb!c::*free-vars* (make-hash-table :test 'eq))
78 (sb!c::*undefined-warnings* nil))
79 ;; FIXME: VALUES declaration
80 (sb!c::process-decls decls
81 vars
82 nil
83 :lexenv lexenv
84 :context :eval))))
85 (simple-eval-progn-body body lexenv))))
87 ;;;; EVAL-ERROR
88 ;;;;
89 ;;;; Analogous to COMPILER-ERROR, but simpler.
91 (define-condition eval-error (encapsulated-condition)
93 (:report (lambda (condition stream)
94 (print-object (encapsulated-condition condition) stream))))
96 (defun eval-error (condition)
97 (signal 'eval-error :condition condition)
98 (bug "Unhandled EVAL-ERROR"))
100 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
101 ;;; magical cases, and call %SIMPLE-EVAL for the rest.
102 (defun simple-eval-in-lexenv (original-exp lexenv)
103 (declare (optimize (safety 1)))
104 ;; (aver (lexenv-simple-p lexenv))
105 (incf *eval-calls*)
106 (handler-bind
107 ((sb!c:compiler-error
108 (lambda (c)
109 (if (boundp 'sb!c::*compiler-error-bailout*)
110 ;; if we're in the compiler, delegate either to a higher
111 ;; authority or, if that's us, back down to the
112 ;; outermost compiler handler...
113 (progn
114 (signal c)
115 nil)
116 ;; ... if we're not in the compiler, better signal the
117 ;; error straight away.
118 (invoke-restart 'sb!c::signal-error)))))
119 (let ((exp (macroexpand original-exp lexenv)))
120 (handler-bind ((eval-error
121 (lambda (condition)
122 (error 'interpreted-program-error
123 :condition (encapsulated-condition condition)
124 :form exp))))
125 (typecase exp
126 (symbol
127 (ecase (info :variable :kind exp)
128 (:constant
129 (values (info :variable :constant-value exp)))
130 ((:special :global)
131 (symbol-value exp))
132 ;; FIXME: This special case here is a symptom of non-ANSI
133 ;; weirdness in SBCL's ALIEN implementation, which could
134 ;; cause problems for e.g. code walkers. It'd probably be
135 ;; good to ANSIfy it by making alien variable accessors
136 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
137 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
138 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
139 ;; be retained for compatibility, it can be implemented
140 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
141 ;; happy.
142 (:alien
143 (%simple-eval original-exp lexenv))))
144 (list
145 (let ((name (first exp))
146 (n-args (1- (length exp))))
147 (case name
148 ((function)
149 (unless (= n-args 1)
150 (error "wrong number of args to FUNCTION:~% ~S" exp))
151 (let ((name (second exp)))
152 (if (and (legal-fun-name-p name)
153 (not (consp (let ((sb!c:*lexenv* lexenv))
154 (sb!c:lexenv-find name funs)))))
155 (%coerce-name-to-fun name)
156 (%simple-eval original-exp lexenv))))
157 ((quote)
158 (unless (= n-args 1)
159 (error "wrong number of args to QUOTE:~% ~S" exp))
160 (second exp))
161 (setq
162 (unless (evenp n-args)
163 (error "odd number of args to SETQ:~% ~S" exp))
164 (unless (zerop n-args)
165 (do ((name (cdr exp) (cddr name)))
166 ((null name)
167 (do ((args (cdr exp) (cddr args)))
168 ((null (cddr args))
169 ;; We duplicate the call to SET so that the
170 ;; correct value gets returned.
171 (set (first args)
172 (simple-eval-in-lexenv (second args) lexenv)))
173 (set (first args)
174 (simple-eval-in-lexenv (second args) lexenv))))
175 (let ((symbol (first name)))
176 (case (info :variable :kind symbol)
177 (:special)
178 (t (return (%simple-eval original-exp lexenv))))
179 (unless (type= (info :variable :type symbol)
180 *universal-type*)
181 ;; let the compiler deal with type checking
182 (return (%simple-eval original-exp lexenv)))))))
183 ((progn)
184 (simple-eval-progn-body (rest exp) lexenv))
185 ((eval-when)
186 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
187 ;; instead of PROGRAM-ERROR when there's something wrong
188 ;; with the syntax here (e.g. missing SITUATIONS). This
189 ;; could be fixed by hand-crafting clauses to catch and
190 ;; report each possibility, but it would probably be
191 ;; cleaner to write a new macro
192 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
193 ;; DESTRUCTURING-BIND and promotes any mismatch to
194 ;; PROGRAM-ERROR, then to use it here and in (probably
195 ;; dozens of) other places where the same problem
196 ;; arises.
197 (destructuring-bind (eval-when situations &rest body) exp
198 (declare (ignore eval-when))
199 (multiple-value-bind (ct lt e)
200 (sb!c:parse-eval-when-situations situations)
201 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
202 ;; the situation :EXECUTE (or EVAL) controls whether
203 ;; evaluation occurs for other EVAL-WHEN forms; that
204 ;; is, those that are not top level forms, or those
205 ;; in code processed by EVAL or COMPILE. If the
206 ;; :EXECUTE situation is specified in such a form,
207 ;; then the body forms are processed as an implicit
208 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
209 (declare (ignore ct lt))
210 (when e
211 (simple-eval-progn-body body lexenv)))))
212 ((locally)
213 (simple-eval-locally exp lexenv))
214 ((macrolet)
215 (destructuring-bind (definitions &rest body)
216 (rest exp)
217 (let ((lexenv
218 (let ((sb!c:*lexenv* lexenv))
219 (sb!c::funcall-in-macrolet-lexenv
220 definitions
221 (lambda (&key funs)
222 (declare (ignore funs))
223 sb!c:*lexenv*)
224 :eval))))
225 (simple-eval-locally `(locally ,@body) lexenv))))
226 ((symbol-macrolet)
227 (destructuring-bind (definitions &rest body) (rest exp)
228 (multiple-value-bind (lexenv vars)
229 (let ((sb!c:*lexenv* lexenv))
230 (sb!c::funcall-in-symbol-macrolet-lexenv
231 definitions
232 (lambda (&key vars)
233 (values sb!c:*lexenv* vars))
234 :eval))
235 (simple-eval-locally `(locally ,@body) lexenv :vars vars))))
236 ((if)
237 (destructuring-bind (test then &optional else) (rest exp)
238 (eval-in-lexenv (if (eval-in-lexenv test lexenv)
239 then
240 else)
241 lexenv)))
242 ((let let*)
243 (destructuring-bind (definitions &rest body) (rest exp)
244 (if (null definitions)
245 (simple-eval-locally `(locally ,@body) lexenv)
246 (%simple-eval exp lexenv))))
248 (if (and (symbolp name)
249 (eq (info :function :kind name) :function))
250 (collect ((args))
251 (dolist (arg (rest exp))
252 (args (eval-in-lexenv arg lexenv)))
253 (apply (symbol-function name) (args)))
254 (%simple-eval exp lexenv))))))
256 exp))))))
258 (defun eval-in-lexenv (exp lexenv)
259 #!+sb-eval
260 (if (eq *evaluator-mode* :compile)
261 (simple-eval-in-lexenv exp lexenv)
262 (sb!eval:eval-in-native-environment exp lexenv))
263 #!-sb-eval
264 (simple-eval-in-lexenv exp lexenv))
266 (defun eval (original-exp)
267 #!+sb-doc
268 "Evaluate the argument in a null lexical environment, returning the
269 result or results."
270 (eval-in-lexenv original-exp (make-null-lexenv)))
273 ;;; miscellaneous full function definitions of things which are
274 ;;; ordinarily handled magically by the compiler
276 (defun apply (function arg &rest arguments)
277 #!+sb-doc
278 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
279 the manner of LIST*. That is, a list is made of the values of all but the
280 last argument, appended to the value of the last argument, which must be a
281 list."
282 (cond ((atom arguments)
283 (apply function arg))
284 ((atom (cdr arguments))
285 (apply function (cons arg (car arguments))))
286 (t (do* ((a1 arguments a2)
287 (a2 (cdr arguments) (cdr a2)))
288 ((atom (cdr a2))
289 (rplacd a1 (car a2))
290 (apply function (cons arg arguments)))))))
292 (defun funcall (function &rest arguments)
293 #!+sb-doc
294 "Call FUNCTION with the given ARGUMENTS."
295 (apply function arguments))
297 (defun values (&rest values)
298 #!+sb-doc
299 "Return all arguments, in order, as values."
300 (declare (dynamic-extent values))
301 (values-list values))
303 (defun values-list (list)
304 #!+sb-doc
305 "Return all of the elements of LIST, in order, as values."
306 (values-list list))