3 ;;;; This software is part of the SBCL system. See the README file for
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 ()
18 *evaluator-mode* :compile)
20 (setf sb!eval::*eval-level* -1
21 sb!eval::*eval-verbose* nil))
23 (defvar *eval-source-context* nil)
25 (defvar *eval-tlf-index* nil)
26 (defvar *eval-source-info* nil)
28 ;;;; Turns EXPR into a lambda-form we can pass to COMPILE. Returns
29 ;;;; a secondary value of T if we must call the resulting function
30 ;;;; to evaluate EXPR -- if EXPR is already a lambda form, there's
32 (defun make-eval-lambda (expr)
33 (if (typep expr `(cons (member lambda named-lambda lambda-with-lexenv)))
36 ;; why PROGN? So that attempts to eval free declarations
37 ;; signal errors rather than return NIL. -- CSR, 2007-05-01
41 ;;; general case of EVAL (except in that it can't handle toplevel
42 ;;; EVAL-WHEN magic properly): Delegate to #'COMPILE.
43 (defun %simple-eval (expr lexenv)
44 (multiple-value-bind (lambda call) (make-eval-lambda expr)
46 ;; This tells the compiler where the lambda comes from, in case it
47 ;; wants to report any problems.
48 (let ((sb!c::*source-form-context-alist*
49 (acons lambda *eval-source-context*
50 sb!c::*source-form-context-alist*)))
51 (handler-bind (;; Compiler notes just clutter up the REPL:
52 ;; anyone caring about performance should not
54 (compiler-note #'muffle-warning))
55 (sb!c:compile-in-lexenv
56 nil lambda lexenv *eval-source-info* *eval-tlf-index* (not call))))))
57 (declare (function fun))
62 ;;; Handle PROGN and implicit PROGN.
63 (defun simple-eval-progn-body (progn-body lexenv)
64 (unless (list-with-length-p progn-body)
65 (let ((*print-circle* t))
66 (error 'simple-program-error
68 "~@<not a proper list in PROGN or implicit PROGN: ~2I~_~S~:>"
69 :format-arguments (list progn-body))))
71 ;; * We can't just use (MAP NIL #'EVAL PROGN-BODY) here, because we
72 ;; need to take care to return all the values of the final EVAL.
73 ;; * It's left as an exercise to the reader to verify that this
74 ;; gives the right result when PROGN-BODY is NIL, because
75 ;; (FIRST NIL) = (REST NIL) = NIL.
76 (do* ((i progn-body rest-i)
77 (rest-i (rest i) (rest i)))
79 (if rest-i ; if not last element of list
80 (simple-eval-in-lexenv (first i) lexenv)
81 (return (simple-eval-in-lexenv (first i) lexenv)))))
83 (defun simple-eval-locally (exp lexenv &key vars)
84 (multiple-value-bind (body decls)
85 (parse-body (rest exp) :doc-string-allowed nil)
87 ;; KLUDGE: Uh, yeah. I'm not anticipating
88 ;; winning any prizes for this code, which was
89 ;; written on a "let's get it to work" basis.
90 ;; These seem to be the variables that need
91 ;; bindings for PROCESS-DECLS to work
92 ;; (*FREE-FUNS* and *FREE-VARS* so that
93 ;; references to free functions and variables
94 ;; in the declarations can be noted;
95 ;; *UNDEFINED-WARNINGS* so that warnings about
96 ;; undefined things can be accumulated [and
97 ;; then thrown away, as it happens]). -- CSR,
99 (let* ((sb!c:*lexenv* lexenv)
100 (sb!c::*free-funs* (make-hash-table :test 'equal))
101 (sb!c::*free-vars* (make-hash-table :test 'eq))
102 (sb!c::*undefined-warnings* nil))
103 ;; FIXME: VALUES declaration
104 (sb!c::process-decls decls
109 (simple-eval-progn-body body lexenv))))
113 ;;;; Analogous to COMPILER-ERROR, but simpler.
115 (define-condition eval-error (encapsulated-condition)
117 (:report (lambda (condition stream)
118 (print-object (encapsulated-condition condition) stream))))
120 (defun eval-error (condition)
121 (signal 'eval-error :condition condition)
122 (bug "Unhandled EVAL-ERROR"))
124 ;;; Pick off a few easy cases, and the various top level EVAL-WHEN
125 ;;; magical cases, and call %SIMPLE-EVAL for the rest.
126 (defun simple-eval-in-lexenv (original-exp lexenv)
127 (declare (optimize (safety 1)))
128 ;; (aver (lexenv-simple-p lexenv))
130 (sb!c:with-compiler-error-resignalling
131 (let ((exp (macroexpand original-exp lexenv)))
132 (handler-bind ((eval-error
134 (error 'interpreted-program-error
135 :condition (encapsulated-condition condition)
139 (ecase (info :variable :kind exp)
140 ((:special :global :constant :unknown)
142 ;; FIXME: This special case here is a symptom of non-ANSI
143 ;; weirdness in SBCL's ALIEN implementation, which could
144 ;; cause problems for e.g. code walkers. It'd probably be
145 ;; good to ANSIfy it by making alien variable accessors
146 ;; into ordinary forms, e.g. (SB-UNIX:ENV) and (SETF
147 ;; SB-UNIX:ENV), instead of magical symbols, e.g. plain
148 ;; SB-UNIX:ENV. Then if the old magical-symbol syntax is to
149 ;; be retained for compatibility, it can be implemented
150 ;; with DEFINE-SYMBOL-MACRO, keeping the code walkers
153 (sb!alien-internals:alien-value exp))))
155 (let ((name (first exp))
156 (n-args (1- (length exp))))
160 (error "wrong number of args to FUNCTION:~% ~S" exp))
161 (let ((name (second exp)))
162 (if (and (legal-fun-name-p name)
163 (not (consp (let ((sb!c:*lexenv* lexenv))
164 (sb!c:lexenv-find name funs)))))
165 (%coerce-name-to-fun name)
166 ;; FIXME: This is a bit wasteful: it would be nice to call
167 ;; COMPILE-IN-LEXENV with the lambda-form directly, but
168 ;; getting consistent source context and muffling compiler notes
169 ;; is easier this way.
170 (%simple-eval original-exp lexenv))))
173 (error "wrong number of args to QUOTE:~% ~S" exp))
176 (unless (evenp n-args)
177 (error "odd number of args to SETQ:~% ~S" exp))
178 (unless (zerop n-args)
179 (do ((name (cdr exp) (cddr name)))
181 (do ((args (cdr exp) (cddr args)))
183 ;; We duplicate the call to SET so that the
184 ;; correct value gets returned.
186 (simple-eval-in-lexenv (second args) lexenv)))
188 (simple-eval-in-lexenv (second args) lexenv))))
189 (let ((symbol (first name)))
190 (case (info :variable :kind symbol)
192 (t (return (%simple-eval original-exp lexenv))))
193 (unless (type= (info :variable :type symbol)
195 ;; let the compiler deal with type checking
196 (return (%simple-eval original-exp lexenv)))))))
198 (simple-eval-progn-body (rest exp) lexenv))
200 ;; FIXME: DESTRUCTURING-BIND returns ARG-COUNT-ERROR
201 ;; instead of PROGRAM-ERROR when there's something wrong
202 ;; with the syntax here (e.g. missing SITUATIONS). This
203 ;; could be fixed by hand-crafting clauses to catch and
204 ;; report each possibility, but it would probably be
205 ;; cleaner to write a new macro
206 ;; DESTRUCTURING-BIND-PROGRAM-SYNTAX which does
207 ;; DESTRUCTURING-BIND and promotes any mismatch to
208 ;; PROGRAM-ERROR, then to use it here and in (probably
209 ;; dozens of) other places where the same problem
211 (destructuring-bind (eval-when situations &rest body) exp
212 (declare (ignore eval-when))
213 (multiple-value-bind (ct lt e)
214 (sb!c:parse-eval-when-situations situations)
215 ;; CLHS 3.8 - Special Operator EVAL-WHEN: The use of
216 ;; the situation :EXECUTE (or EVAL) controls whether
217 ;; evaluation occurs for other EVAL-WHEN forms; that
218 ;; is, those that are not top level forms, or those
219 ;; in code processed by EVAL or COMPILE. If the
220 ;; :EXECUTE situation is specified in such a form,
221 ;; then the body forms are processed as an implicit
222 ;; PROGN; otherwise, the EVAL-WHEN form returns NIL.
223 (declare (ignore ct lt))
225 (simple-eval-progn-body body lexenv)))))
227 (simple-eval-locally exp lexenv))
229 (destructuring-bind (definitions &rest body)
232 (let ((sb!c:*lexenv* lexenv))
233 (sb!c::funcall-in-macrolet-lexenv
236 (declare (ignore funs))
239 (simple-eval-locally `(locally ,@body) lexenv))))
241 (destructuring-bind (definitions &rest body) (rest exp)
242 (multiple-value-bind (lexenv vars)
243 (let ((sb!c:*lexenv* lexenv))
244 (sb!c::funcall-in-symbol-macrolet-lexenv
247 (values sb!c:*lexenv* vars))
249 (simple-eval-locally `(locally ,@body) lexenv :vars vars))))
251 (destructuring-bind (test then &optional else) (rest exp)
252 (eval-in-lexenv (if (eval-in-lexenv test lexenv)
257 (%simple-eval exp lexenv))
259 (if (and (symbolp name)
260 (eq (info :function :kind name) :function))
262 (dolist (arg (rest exp))
263 (args (eval-in-lexenv arg lexenv)))
264 (apply (symbol-function name) (args)))
265 (%simple-eval exp lexenv))))))
269 (defun eval-in-lexenv (exp lexenv)
271 (if (eq *evaluator-mode* :compile)
272 (simple-eval-in-lexenv exp lexenv)
273 (sb!eval:eval-in-native-environment exp lexenv))
275 (simple-eval-in-lexenv exp lexenv))
277 (defun eval (original-exp)
279 "Evaluate the argument in a null lexical environment, returning the
281 (let ((*eval-source-context* original-exp)
282 (*eval-tlf-index* nil)
283 (*eval-source-info* nil))
284 (eval-in-lexenv original-exp (make-null-lexenv))))
286 (defun eval-tlf (original-exp tlf-index &optional (lexenv (make-null-lexenv)))
287 (let ((*eval-source-context* original-exp)
288 (*eval-tlf-index* tlf-index)
289 (*eval-source-info* sb!c::*source-info*))
290 (eval-in-lexenv original-exp lexenv)))
292 ;;; miscellaneous full function definitions of things which are
293 ;;; ordinarily handled magically by the compiler
295 (defun apply (function arg &rest arguments)
297 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
298 the manner of LIST*. That is, a list is made of the values of all but the
299 last argument, appended to the value of the last argument, which must be a
301 (cond ((atom arguments)
302 (apply function arg))
303 ((atom (cdr arguments))
304 (apply function (cons arg (car arguments))))
305 (t (do* ((a1 arguments a2)
306 (a2 (cdr arguments) (cdr a2)))
309 (apply function (cons arg arguments)))))))
311 (defun funcall (function &rest arguments)
313 "Call FUNCTION with the given ARGUMENTS."
314 (apply function arguments))
316 (defun values (&rest values)
318 "Return all arguments, in order, as values."
319 (declare (truly-dynamic-extent values))
320 (values-list values))
322 (defun values-list (list)
324 "Return all of the elements of LIST, in order, as values."