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!BYTECODE")
14 ;;; This needs to be initialized in the cold load, since the top-level
15 ;;; catcher will always restore the initial value.
16 (defvar *eval-stack-top* 0)
18 ;;; general case of EVAL (except in that it can't handle toplevel
19 ;;; EVAL-WHEN magic properly): Delegate to the byte compiler.
21 (funcall (compile (gensym "EVAL-TMPFUN-")
24 ;; SPEED=0,DEBUG=1 => byte-compile
25 (declare (optimize (speed 0) (debug 1)))
27 ;; Other than that, basically we care about
28 ;; compilation speed, compilation speed, and
29 ;; compilation speed. (There are cases where
30 ;; the user wants something else, but we don't
31 ;; know enough to guess that; and if he is
32 ;; unhappy about our guessed emphasis, he
33 ;; should explicitly compile his code, with
34 ;; explicit declarations to tell us what to
36 (declare (optimize (space 1) (safety 1)))
37 (declare (optimize (compilation-speed 3)))
41 ;;; Pick off a few easy cases, and the various top-level EVAL-WHEN
42 ;;; magical cases, and call %EVAL for the rest.
43 (defun eval (original-exp)
45 "Evaluate the argument in a null lexical environment, returning the
47 (declare (optimize (safety 1)))
48 (let ((exp (macroexpand original-exp)))
51 (ecase (info :variable :kind exp)
53 (values (info :variable :constant-value exp)))
56 ;; FIXME: This special case here is a symptom of non-ANSI
57 ;; weirdness in SBCL's ALIEN implementation, which could
58 ;; cause problems for e.g. code walkers. It'd probably be
59 ;; good to ANSIfy it by making alien variable accessors into
60 ;; ordinary forms, e.g. (SB-UNIX:ENV) and (SETF SB-UNIX:ENV),
61 ;; instead of magical symbols, e.g. plain SB-UNIX:ENV. Then
62 ;; if the old magical-symbol syntax is to be retained for
63 ;; compatibility, it can be implemented with
64 ;; DEFINE-SYMBOL-MACRO, keeping the code walkers happy.
66 (%eval original-exp))))
68 (let ((name (first exp))
69 (args (1- (length exp))))
73 (error "wrong number of args to FUNCTION:~% ~S" exp))
74 (let ((name (second exp)))
77 (eq (car name) 'setf)))
79 (%eval original-exp))))
82 (error "wrong number of args to QUOTE:~% ~S" exp))
86 (error "odd number of args to SETQ:~% ~S" exp))
88 (do ((name (cdr exp) (cddr name)))
90 (do ((args (cdr exp) (cddr args)))
92 ;; We duplicate the call to SET so that the
93 ;; correct value gets returned.
94 (set (first args) (eval (second args))))
95 (set (first args) (eval (second args)))))
96 (let ((symbol (first name)))
97 (case (info :variable :kind symbol)
98 ;; FIXME: I took out the *TOP-LEVEL-AUTO-DECLARE*
99 ;; test here, and removed the *TOP-LEVEL-AUTO-DECLARE*
100 ;; variable; the code should now act as though that
101 ;; variable is NIL. This should be tested..
103 (t (return (%eval original-exp))))))))
106 (dolist (x (butlast (rest exp)) (eval (car (last exp))))
110 (or (member 'eval (second exp))
111 (member :execute (second exp))))
113 (dolist (x (butlast (cddr exp)) (eval (car (last exp))))
115 (%eval original-exp)))
117 (if (and (symbolp name)
118 (eq (info :function :kind name) :function))
120 (dolist (arg (rest exp))
122 (apply (symbol-function name) (args)))
123 (%eval original-exp))))))
127 ;;; Given a function, return three values:
128 ;;; 1] A lambda expression that could be used to define the function,
129 ;;; or NIL if the definition isn't available.
130 ;;; 2] NIL if the function was definitely defined in a null lexical
131 ;;; environment, and T otherwise.
132 ;;; 3] Some object that \"names\" the function. Although this is
133 ;;; allowed to be any object, CMU CL always returns a valid
134 ;;; function name or a string.
136 ;;; If interpreted, use the interpreter interface. Otherwise, see
137 ;;; whether it was compiled with COMPILE. If that fails, check for an
138 ;;; inline expansion.
139 (defun function-lambda-expression (fun)
140 (declare (type function fun))
141 (let* ((fun (%function-self fun))
142 (name (%function-name fun))
143 (code (sb!di::function-code-header fun))
144 (info (sb!kernel:%code-debug-info code)))
146 (let ((source (first (sb!c::compiled-debug-info-source info))))
147 (cond ((and (eq (sb!c::debug-source-from source) :lisp)
148 (eq (sb!c::debug-source-info source) fun))
149 (values (second (svref (sb!c::debug-source-name source) 0))
154 (let ((exp (info :function :inline-expansion name)))
156 (values exp nil name)
157 (values nil t name))))))
158 (values nil t name))))
160 ;;; miscellaneous full function definitions of things which are
161 ;;; ordinarily handled magically by the compiler
163 (defun apply (function arg &rest arguments)
165 "Apply FUNCTION to a list of arguments produced by evaluating ARGUMENTS in
166 the manner of LIST*. That is, a list is made of the values of all but the
167 last argument, appended to the value of the last argument, which must be a
169 (cond ((atom arguments)
170 (apply function arg))
171 ((atom (cdr arguments))
172 (apply function (cons arg (car arguments))))
173 (t (do* ((a1 arguments a2)
174 (a2 (cdr arguments) (cdr a2)))
177 (apply function (cons arg arguments)))))))
179 (defun funcall (function &rest arguments)
181 "Call FUNCTION with the given ARGUMENTS."
182 (apply function arguments))
184 (defun values (&rest values)
186 "Return all arguments, in order, as values."
187 (values-list values))
189 (defun values-list (list)
191 "Return all of the elements of LIST, in order, as values."