1 ;;;; This file contains portable versions of low-level functions and macros
2 ;;;; which are ripe for implementation specific customization. None of the code
3 ;;;; in this file *has* to be customized for a particular Common Lisp
4 ;;;; implementation. Moreover, in some implementations it may not make any
5 ;;;; sense to customize some of this code.
7 ;;;; The original version was intended to support portable customization to
8 ;;;; lotso different Lisp implementations. This functionality is gone in the
9 ;;;; current version, and it now runs only under SBCL. (Now that ANSI Common
10 ;;;; Lisp has mixed CLOS into the insides of the system (e.g. error handling
11 ;;;; and printing) so deeply that it's not very meaningful to bootstrap Common
12 ;;;; Lisp without CLOS, the old functionality is of dubious use. -- WHN
15 ;;;; This software is part of the SBCL system. See the README file for more
18 ;;;; This software is derived from software originally released by Xerox
19 ;;;; Corporation. Copyright and release statements follow. Later modifications
20 ;;;; to the software are in the public domain and are provided with
21 ;;;; absolutely no warranty. See the COPYING and CREDITS files for more
24 ;;;; copyright information from original PCL sources:
26 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
27 ;;;; All rights reserved.
29 ;;;; Use and copying of this software and preparation of derivative works based
30 ;;;; upon this software are permitted. Any distribution of this software or
31 ;;;; derivative works must comply with all applicable United States export
34 ;;;; This software is made available AS IS, and Xerox Corporation makes no
35 ;;;; warranty about the software, its performance or its conformity to any
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed* '(optimize (speed 3) (safety 0)))
44 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
45 `(dotimes (,var (the fixnum ,count) ,result)
46 (declare (fixnum ,var))
49 ;;;; PCL's view of funcallable instances
51 (defstruct (pcl-funcallable-instance
52 (:alternate-metaclass sb-kernel:funcallable-instance
53 sb-kernel:random-pcl-class
54 sb-kernel:make-random-pcl-class)
55 (:type sb-kernel:funcallable-structure)
56 (:constructor allocate-funcallable-instance-1 ())
59 ;; Note: The PCL wrapper is in the layout slot.
62 (pcl-funcallable-instance-slots nil)
63 ;; The debug-name for this function.
64 (funcallable-instance-name nil))
66 (import 'sb-kernel:funcallable-instance-p)
68 ;;; This "works" on non-PCL FINs, which allows us to weaken
69 ;;; FUNCALLABLE-INSTANCE-P to return true for all FINs. This is also
70 ;;; necessary for bootstrapping to work, since the layouts for early
71 ;;; GFs are not initially initialized.
72 (defmacro funcallable-instance-data-1 (fin slot)
74 (wrapper `(sb-kernel:%funcallable-instance-layout ,fin))
75 (slots `(sb-kernel:%funcallable-instance-info ,fin 0))))
77 ;;; FIXME: Now that we no longer try to make our CLOS implementation
78 ;;; portable to other implementations of Common Lisp, all the
79 ;;; funcallable instance wrapper logic here can go away in favor
80 ;;; of direct calls to native SBCL funcallable instance operations.
81 (defun set-funcallable-instance-function (fin new-value)
82 (declare (type function new-value))
83 (aver (funcallable-instance-p fin))
84 (setf (sb-kernel:funcallable-instance-function fin) new-value))
85 (defmacro fsc-instance-p (fin)
86 `(funcallable-instance-p ,fin))
87 (defmacro fsc-instance-class (fin)
88 `(wrapper-class (funcallable-instance-data-1 ,fin 'wrapper)))
89 (defmacro fsc-instance-wrapper (fin)
90 `(funcallable-instance-data-1 ,fin 'wrapper))
91 (defmacro fsc-instance-slots (fin)
92 `(funcallable-instance-data-1 ,fin 'slots))
94 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
95 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
96 (defun clos-slots-ref (slots index)
98 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
99 (defun (setf clos-slots-ref) (new-value slots index)
100 (setf (svref slots index) new-value))
102 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
103 ;;; is only used to discriminate between functions (including FINs)
104 ;;; and normal instances, so we can return true on structures also. A
105 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
107 (defmacro std-instance-p (x)
108 `(sb-kernel:%instancep ,x))
110 ;; a temporary definition used for debugging the bootstrap
112 (defun print-std-instance (instance stream depth)
113 (declare (ignore depth))
114 (print-unreadable-object (instance stream :type t :identity t)
115 (let ((class (class-of instance)))
116 (when (or (eq class (find-class 'standard-class nil))
117 (eq class (find-class 'funcallable-standard-class nil))
118 (eq class (find-class 'built-in-class nil)))
119 (princ (early-class-name instance) stream)))))
121 ;;; This is the value that we stick into a slot to tell us that it is
122 ;;; unbound. It may seem gross, but for performance reasons, we make
123 ;;; this an interned symbol. That means that the fast check to see
124 ;;; whether a slot is unbound is to say (EQ <val> '..SLOT-UNBOUND..).
125 ;;; That is considerably faster than looking at the value of a special
126 ;;; variable. Be careful, there are places in the code which actually
127 ;;; use ..SLOT-UNBOUND.. rather than this variable. So much for
130 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use
131 ;;; the SBCL built-in unbound value token instead. Perhaps if we did
132 ;;; so it would be a good idea to define collections of CLOS slots as
133 ;;; a new type of heap object, instead of using bare SIMPLE-VECTOR, in
134 ;;; order to avoid problems (in the debugger if nowhere else) with
135 ;;; SIMPLE-VECTORs some of whose elements are unbound tokens.
136 (defconstant +slot-unbound+ '..slot-unbound..)
138 (defmacro %allocate-static-slot-storage--class (no-of-slots)
139 `(make-array ,no-of-slots :initial-element +slot-unbound+))
141 (defmacro std-instance-class (instance)
142 `(wrapper-class* (std-instance-wrapper ,instance)))
144 ;;; When given a function should give this function the name
145 ;;; NEW-NAME. Note that NEW-NAME is sometimes a list. Some lisps
146 ;;; get the upset in the tummy when they start thinking about
147 ;;; functions which have lists as names. To deal with that there is
148 ;;; SET-FUNCTION-NAME-INTERN which takes a list spec for a function
149 ;;; name and turns it into a symbol if need be.
151 ;;; When given a funcallable instance, SET-FUNCTION-NAME *must*
152 ;;; side-effect that FIN to give it the name. When given any other
153 ;;; kind of function SET-FUNCTION-NAME is allowed to return a new
154 ;;; function which is "the same" except that it has the name.
156 ;;; In all cases, SET-FUNCTION-NAME must return the new (or same)
157 ;;; function. (Unlike other functions to set stuff, it does not return
159 (defun set-function-name (fcn new-name)
161 "Set the name of a compiled function object. Return the function."
162 (declare (special *boot-state* *the-class-standard-generic-function*))
164 (set-function-name (symbol-function fcn) new-name))
165 ((funcallable-instance-p fcn)
166 (if (if (eq *boot-state* 'complete)
167 (typep fcn 'generic-function)
168 (eq (class-of fcn) *the-class-standard-generic-function*))
169 (setf (sb-kernel:%funcallable-instance-info fcn 1) new-name)
171 (sb-kernel:byte-closure
172 (set-function-name (sb-kernel:byte-closure-function fcn)
174 (sb-kernel:byte-function
175 (setf (sb-kernel:byte-function-name fcn) new-name))
176 (sb-eval:interpreted-function
177 (setf (sb-eval:interpreted-function-name fcn) new-name))))
180 ;; pw-- This seems wrong and causes trouble. Tests show
181 ;; that loading CL-HTTP resulted in ~5400 closures being
182 ;; passed through this code of which ~4000 of them pointed
183 ;; to but 16 closure-functions, including 1015 each of
184 ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
185 ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
186 ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
187 ;; Since the actual functions have been moved by PURIFY
188 ;; to memory not seen by GC, changing a pointer there
189 ;; not only clobbers the last change but leaves a dangling
190 ;; pointer invalid after the next GC. Comments in low.lisp
191 ;; indicate this code need do nothing. Setting the
192 ;; function-name to NIL loses some info, and not changing
193 ;; it loses some info of potential hacking value. So,
194 ;; lets not do this...
196 (let ((header (sb-kernel:%closure-function fcn)))
197 (setf (sb-c::%function-name header) new-name))
199 ;; XXX Maybe add better scheme here someday.
202 (defun intern-function-name (name)
203 (cond ((symbolp name) name)
205 (intern (let ((*package* *pcl-package*)
206 (*print-case* :upcase)
209 (format nil "~S" name))
212 ;;; FIXME: probably no longer needed after init
213 (defmacro precompile-random-code-segments (&optional system)
215 (eval-when (:compile-toplevel)
216 (update-dispatch-dfuns)
217 (compile-iis-functions nil))
218 (precompile-function-generators ,system)
219 (precompile-dfun-constructors ,system)
220 (precompile-iis-functions ,system)
221 (eval-when (:load-toplevel)
222 (compile-iis-functions t))))
224 ;;; This definition is for interpreted code.
225 (defun pcl-instance-p (x)
226 (typep (sb-kernel:layout-of x) 'wrapper))
228 ;;; We define this as STANDARD-INSTANCE, since we're going to clobber
229 ;;; the layout with some standard-instance layout as soon as we make
230 ;;; it, and we want the accessor to still be type-correct.
231 (defstruct (standard-instance
233 (:constructor %%allocate-instance--class ())
235 (:alternate-metaclass sb-kernel:instance cl:standard-class
236 sb-kernel:make-standard-class))
239 ;;; Both of these operations "work" on structures, which allows the above
240 ;;; weakening of STD-INSTANCE-P.
241 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
242 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
244 ;;; FIXME: These functions are called every place we do a
245 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
246 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
247 ;;; than just indiscriminately expanding them inline everywhere.
248 (declaim (inline get-slots get-slots-or-nil))
249 (declaim (ftype (function (t) simple-vector) get-slots))
250 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
251 (defun get-slots (instance)
252 (if (std-instance-p instance)
253 (std-instance-slots instance)
254 (fsc-instance-slots instance)))
255 (defun get-slots-or-nil (instance)
256 (when (pcl-instance-p instance)
257 (get-slots instance)))
259 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
261 (defmacro get-wrapper (inst)
262 (once-only ((wrapper `(wrapper-of ,inst)))
264 (aver (typep ,wrapper 'wrapper))
267 ;;; FIXME: could be an inline function or ordinary function (like many
268 ;;; other things around here)
269 (defmacro get-instance-wrapper-or-nil (inst)
270 (once-only ((wrapper `(wrapper-of ,inst)))
271 `(if (typep ,wrapper 'wrapper)
275 ;;;; structure-instance stuff
277 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
278 ;;;; abstraction around our native structure representation doesn't
279 ;;;; seem to add anything useful, and could probably go away.
281 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
283 (defun get-structure-dd (type)
284 (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
286 (defun structure-type-included-type-name (type)
287 (let ((include (sb-kernel::dd-include (get-structure-dd type))))
292 (defun structure-type-slot-description-list (type)
293 (nthcdr (length (let ((include (structure-type-included-type-name type)))
295 (sb-kernel:dd-slots (get-structure-dd include)))))
296 (sb-kernel:dd-slots (get-structure-dd type))))
298 (defun structure-slotd-name (slotd)
299 (sb-kernel:dsd-name slotd))
301 (defun structure-slotd-accessor-symbol (slotd)
302 (sb-kernel:dsd-accessor slotd))
304 (defun structure-slotd-reader-function (slotd)
305 (fdefinition (sb-kernel:dsd-accessor slotd)))
307 (defun structure-slotd-writer-function (slotd)
308 (unless (sb-kernel:dsd-read-only slotd)
309 (fdefinition `(setf ,(sb-kernel:dsd-accessor slotd)))))
311 (defun structure-slotd-type (slotd)
312 (sb-kernel:dsd-type slotd))
314 (defun structure-slotd-init-form (slotd)
315 (sb-kernel::dsd-default slotd))