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)
170 (error 'simple-type-error
172 :expected-type 'generic-function
173 :format-control "internal error: bad function type"))
176 ;; pw-- This seems wrong and causes trouble. Tests show
177 ;; that loading CL-HTTP resulted in ~5400 closures being
178 ;; passed through this code of which ~4000 of them pointed
179 ;; to but 16 closure-functions, including 1015 each of
180 ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
181 ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
182 ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
183 ;; Since the actual functions have been moved by PURIFY
184 ;; to memory not seen by GC, changing a pointer there
185 ;; not only clobbers the last change but leaves a dangling
186 ;; pointer invalid after the next GC. Comments in low.lisp
187 ;; indicate this code need do nothing. Setting the
188 ;; function-name to NIL loses some info, and not changing
189 ;; it loses some info of potential hacking value. So,
190 ;; lets not do this...
192 (let ((header (sb-kernel:%closure-function fcn)))
193 (setf (sb-kernel:%function-name header) new-name))
195 ;; XXX Maybe add better scheme here someday.
198 (defun intern-function-name (name)
199 (cond ((symbolp name) name)
201 (intern (let ((*package* *pcl-package*)
202 (*print-case* :upcase)
205 (format nil "~S" name))
208 ;;; FIXME: probably no longer needed after init
209 (defmacro precompile-random-code-segments (&optional system)
211 (eval-when (:compile-toplevel)
212 (update-dispatch-dfuns)
213 (compile-iis-functions nil))
214 (precompile-function-generators ,system)
215 (precompile-dfun-constructors ,system)
216 (precompile-iis-functions ,system)
217 (eval-when (:load-toplevel)
218 (compile-iis-functions t))))
220 ;;; This definition is for interpreted code.
221 (defun pcl-instance-p (x)
222 (typep (sb-kernel:layout-of x) 'wrapper))
224 ;;; We define this as STANDARD-INSTANCE, since we're going to clobber
225 ;;; the layout with some standard-instance layout as soon as we make
226 ;;; it, and we want the accessor to still be type-correct.
227 (defstruct (standard-instance
229 (:constructor %%allocate-instance--class ())
231 (:alternate-metaclass sb-kernel:instance cl:standard-class
232 sb-kernel:make-standard-class))
235 ;;; Both of these operations "work" on structures, which allows the above
236 ;;; weakening of STD-INSTANCE-P.
237 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
238 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
240 ;;; FIXME: These functions are called every place we do a
241 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
242 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
243 ;;; than just indiscriminately expanding them inline everywhere.
244 (declaim (inline get-slots get-slots-or-nil))
245 (declaim (ftype (function (t) simple-vector) get-slots))
246 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
247 (defun get-slots (instance)
248 (if (std-instance-p instance)
249 (std-instance-slots instance)
250 (fsc-instance-slots instance)))
251 (defun get-slots-or-nil (instance)
252 (when (pcl-instance-p instance)
253 (get-slots instance)))
255 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
257 (defmacro get-wrapper (inst)
258 (once-only ((wrapper `(wrapper-of ,inst)))
260 (aver (typep ,wrapper 'wrapper))
263 ;;; FIXME: could be an inline function or ordinary function (like many
264 ;;; other things around here)
265 (defmacro get-instance-wrapper-or-nil (inst)
266 (once-only ((wrapper `(wrapper-of ,inst)))
267 `(if (typep ,wrapper 'wrapper)
271 ;;;; structure-instance stuff
273 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
274 ;;;; abstraction around our native structure representation doesn't
275 ;;;; seem to add anything useful, and could probably go away.
277 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
279 (defun get-structure-dd (type)
280 (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
282 (defun structure-type-included-type-name (type)
283 (let ((include (sb-kernel::dd-include (get-structure-dd type))))
288 (defun structure-type-slot-description-list (type)
289 (nthcdr (length (let ((include (structure-type-included-type-name type)))
291 (sb-kernel:dd-slots (get-structure-dd include)))))
292 (sb-kernel:dd-slots (get-structure-dd type))))
294 (defun structure-slotd-name (slotd)
295 (sb-kernel:dsd-name slotd))
297 (defun structure-slotd-accessor-symbol (slotd)
298 (sb-kernel:dsd-accessor-name slotd))
300 (defun structure-slotd-reader-function (slotd)
301 (fdefinition (sb-kernel:dsd-accessor-name slotd)))
303 (defun structure-slotd-writer-function (slotd)
304 (unless (sb-kernel:dsd-read-only slotd)
305 (fdefinition `(setf ,(sb-kernel:dsd-accessor-name slotd)))))
307 (defun structure-slotd-type (slotd)
308 (sb-kernel:dsd-type slotd))
310 (defun structure-slotd-init-form (slotd)
311 (sb-kernel::dsd-default slotd))