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 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
50 (defun clos-slots-ref (slots index)
52 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
53 (defun (setf clos-slots-ref) (new-value slots index)
54 (setf (svref slots index) new-value))
56 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
57 ;;; is only used to discriminate between functions (including FINs)
58 ;;; and normal instances, so we can return true on structures also. A
59 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
61 (defmacro std-instance-p (x)
62 `(sb-kernel:%instancep ,x))
64 (defmacro get-slots (inst)
65 `(cond ((std-instance-p ,inst) (std-instance-slots ,inst))
66 ((fsc-instance-p ,inst) (fsc-instance-slots ,inst))
67 (t (error "What kind of instance is this?"))))
69 ;; a temporary definition used for debugging the bootstrap
71 (defun print-std-instance (instance stream depth)
72 (declare (ignore depth))
73 (print-unreadable-object (instance stream :type t :identity t)
74 (let ((class (class-of instance)))
75 (when (or (eq class (find-class 'standard-class nil))
76 (eq class (find-class 'funcallable-standard-class nil))
77 (eq class (find-class 'built-in-class nil)))
78 (princ (early-class-name instance) stream)))))
80 ;;; This is the value that we stick into a slot to tell us that it is unbound.
81 ;;; It may seem gross, but for performance reasons, we make this an interned
82 ;;; symbol. That means that the fast check to see whether a slot is unbound is
83 ;;; to say (EQ <val> '..SLOT-UNBOUND..). That is considerably faster than
84 ;;; looking at the value of a special variable. Be careful, there are places in
85 ;;; the code which actually use ..slot-unbound.. rather than this variable. So
86 ;;; much for modularity..
88 ;;; FIXME: Now that we're tightly integrated into SBCL, we could use the
89 ;;; SBCL built-in unbound value token instead.
90 (defconstant +slot-unbound+ '..slot-unbound..)
92 (defmacro %allocate-static-slot-storage--class (no-of-slots)
93 `(make-array ,no-of-slots :initial-element +slot-unbound+))
95 (defmacro std-instance-class (instance)
96 `(wrapper-class* (std-instance-wrapper ,instance)))
101 ;;; When given a function should give this function the name <new-name>.
102 ;;; Note that <new-name> is sometimes a list. Some lisps get the upset
103 ;;; in the tummy when they start thinking about functions which have
104 ;;; lists as names. To deal with that there is set-function-name-intern
105 ;;; which takes a list spec for a function name and turns it into a symbol
108 ;;; When given a funcallable instance, set-function-name MUST side-effect
109 ;;; that FIN to give it the name. When given any other kind of function
110 ;;; set-function-name is allowed to return new function which is the 'same'
111 ;;; except that it has the name.
113 ;;; In all cases, set-function-name must return the new (or same) function.
114 ;;; (Unlike other functions to set stuff, it does not return the new value.)
115 (defun set-function-name (fcn new-name)
117 "Set the name of a compiled function object. Return the function."
118 (declare (special *boot-state* *the-class-standard-generic-function*))
120 (set-function-name (symbol-function fcn) new-name))
121 ((funcallable-instance-p fcn)
122 (if (if (eq *boot-state* 'complete)
123 (typep fcn 'generic-function)
124 (eq (class-of fcn) *the-class-standard-generic-function*))
125 (setf (sb-kernel:%funcallable-instance-info fcn 1) new-name)
127 (sb-kernel:byte-closure
128 (set-function-name (sb-kernel:byte-closure-function fcn)
130 (sb-kernel:byte-function
131 (setf (sb-kernel:byte-function-name fcn) new-name))
132 (sb-eval:interpreted-function
133 (setf (sb-eval:interpreted-function-name fcn) new-name))))
136 ;; pw-- This seems wrong and causes trouble. Tests show
137 ;; that loading CL-HTTP resulted in ~5400 closures being
138 ;; passed through this code of which ~4000 of them pointed
139 ;; to but 16 closure-functions, including 1015 each of
140 ;; DEFUN MAKE-OPTIMIZED-STD-WRITER-METHOD-FUNCTION
141 ;; DEFUN MAKE-OPTIMIZED-STD-READER-METHOD-FUNCTION
142 ;; DEFUN MAKE-OPTIMIZED-STD-BOUNDP-METHOD-FUNCTION.
143 ;; Since the actual functions have been moved by PURIFY
144 ;; to memory not seen by GC, changing a pointer there
145 ;; not only clobbers the last change but leaves a dangling
146 ;; pointer invalid after the next GC. Comments in low.lisp
147 ;; indicate this code need do nothing. Setting the
148 ;; function-name to NIL loses some info, and not changing
149 ;; it loses some info of potential hacking value. So,
150 ;; lets not do this...
152 (let ((header (sb-kernel:%closure-function fcn)))
153 (setf (sb-c::%function-name header) new-name))
155 ;; Maybe add better scheme here someday.
158 (defun intern-function-name (name)
159 (cond ((symbolp name) name)
161 (intern (let ((*package* *pcl-package*)
162 (*print-case* :upcase)
165 (format nil "~S" name))
168 ;;; FIXME: probably no longer needed after init
169 (defmacro precompile-random-code-segments (&optional system)
171 (eval-when (:compile-toplevel)
172 (update-dispatch-dfuns)
173 (compile-iis-functions nil))
174 (precompile-function-generators ,system)
175 (precompile-dfun-constructors ,system)
176 (precompile-iis-functions ,system)
177 (eval-when (:load-toplevel)
178 (compile-iis-functions t))))
180 (defun record-definition (type spec &rest args)
181 (declare (ignore type spec args))
184 (defun doctor-dfun-for-the-debugger (gf dfun) (declare (ignore gf)) dfun)
186 ;;; This definition is for interpreted code.
187 (defun pcl-instance-p (x)
188 (typep (sb-kernel:layout-of x) 'wrapper))
190 ;;; We define this as STANDARD-INSTANCE, since we're going to clobber
191 ;;; the layout with some standard-instance layout as soon as we make
192 ;;; it, and we want the accessor to still be type-correct.
193 (defstruct (standard-instance
195 (:constructor %%allocate-instance--class ())
197 (:alternate-metaclass sb-kernel:instance cl:standard-class
198 sb-kernel:make-standard-class))
201 ;;; Both of these operations "work" on structures, which allows the above
202 ;;; weakening of STD-INSTANCE-P.
203 (defmacro std-instance-slots (x) `(sb-kernel:%instance-ref ,x 1))
204 (defmacro std-instance-wrapper (x) `(sb-kernel:%instance-layout ,x))
206 (defmacro built-in-or-structure-wrapper (x) `(sb-kernel:layout-of ,x))
208 (defmacro get-wrapper (inst)
209 (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
211 (assert (typep ,wrapper 'wrapper) () "What kind of instance is this?")
214 ;;; FIXME: could be an inline function (like many other things around
216 (defmacro get-instance-wrapper-or-nil (inst)
217 (sb-int:once-only ((wrapper `(wrapper-of ,inst)))
218 `(if (typep ,wrapper 'wrapper)
222 (defmacro get-slots-or-nil (inst)
223 (sb-int:once-only ((n-inst inst))
224 `(when (pcl-instance-p ,n-inst)
225 (if (std-instance-p ,n-inst)
226 (std-instance-slots ,n-inst)
227 (fsc-instance-slots ,n-inst)))))
229 ;;;; structure-instance stuff
230 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
232 (defun get-structure-dd (type)
233 (sb-kernel:layout-info (sb-kernel:class-layout (cl:find-class type))))
235 (defun structure-type-included-type-name (type)
236 (let ((include (sb-kernel::dd-include (get-structure-dd type))))
241 (defun structure-type-slot-description-list (type)
242 (nthcdr (length (let ((include (structure-type-included-type-name type)))
244 (sb-kernel:dd-slots (get-structure-dd include)))))
245 (sb-kernel:dd-slots (get-structure-dd type))))
247 (defun structure-slotd-name (slotd)
248 (sb-kernel:dsd-name slotd))
250 (defun structure-slotd-accessor-symbol (slotd)
251 (sb-kernel:dsd-accessor slotd))
253 (defun structure-slotd-reader-function (slotd)
254 (fdefinition (sb-kernel:dsd-accessor slotd)))
256 (defun structure-slotd-writer-function (slotd)
257 (unless (sb-kernel:dsd-read-only slotd)
258 (fdefinition `(setf ,(sb-kernel:dsd-accessor slotd)))))
260 (defun structure-slotd-type (slotd)
261 (sb-kernel:dsd-type slotd))
263 (defun structure-slotd-init-form (slotd)
264 (sb-kernel::dsd-default slotd))