Coalesce constant strings more aggressively maybe.
[sbcl.git] / src / pcl / low.lisp
blob4723e137b638fdd07394bc2965ca928ca8e8bcc8
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.
6 ;;;;
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
13 ;;;; 19981108)
15 ;;;; This software is part of the SBCL system. See the README file for more
16 ;;;; information.
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
22 ;;;; information.
24 ;;;; copyright information from original PCL sources:
25 ;;;;
26 ;;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
27 ;;;; All rights reserved.
28 ;;;;
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
32 ;;;; control laws.
33 ;;;;
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
36 ;;;; specification.
38 (in-package "SB!PCL")
40 (eval-when (:compile-toplevel :load-toplevel :execute)
41 (defvar *optimize-speed*
42 '(optimize (speed 3) (safety 0) (sb!ext:inhibit-warnings 3) (debug 0)))
43 ) ; EVAL-WHEN
45 (defmacro dotimes-fixnum ((var count &optional (result nil)) &body body)
46 `(dotimes (,var (the fixnum ,count) ,result)
47 (declare (fixnum ,var))
48 ,@body))
50 (declaim (inline random-fixnum))
51 (defun random-fixnum ()
52 (random (1+ most-positive-fixnum)))
54 ;;; Lambda which executes its body (or not) randomly. Used to drop
55 ;;; random cache entries.
56 ;;; This formerly punted with slightly greater than 50% probability,
57 ;;; and there was a periodicity to the nonrandomess.
58 ;;; If that was intentional, it should have been commented to that effect.
59 (defmacro randomly-punting-lambda (lambda-list &body body)
60 (with-unique-names (drops drop-pos)
61 `(let ((,drops (random-fixnum)) ; means a POSITIVE fixnum
62 (,drop-pos sb!vm:n-positive-fixnum-bits))
63 (declare (fixnum ,drops)
64 (type (mod #.sb!vm:n-fixnum-bits) ,drop-pos))
65 (lambda ,lambda-list
66 (when (logbitp (the unsigned-byte (decf ,drop-pos)) ,drops)
67 (locally ,@body))
68 (when (zerop ,drop-pos)
69 (setf ,drops (random-fixnum)
70 ,drop-pos sb!vm:n-positive-fixnum-bits))))))
72 (import 'sb!kernel:funcallable-instance-p) ; why?
74 (defun set-funcallable-instance-function (fin new-value)
75 (declare (type function new-value)
76 ;; KLUDGE: it might be nice to restrict
77 ;; SB-MOP:SET-FUNCALLABLE-INSTANCE-FUNCTION to operate only
78 ;; on generalized instances of
79 ;; SB-MOP:FUNCALLABLE-STANDARD-OBJECT; at present, even
80 ;; PCL's internal use of SET-FUNCALLABLE-INSTANCE-FUNCTION
81 ;; doesn't obey this restriction.
82 (type funcallable-instance fin))
83 (setf (funcallable-instance-fun fin) new-value))
85 ;;; FIXME: these macros should just go away. It's not clear whether
86 ;;; the inline functions defined by
87 ;;; !DEFSTRUCT-WITH-ALTERNATE-METACLASS are as efficient as they could
88 ;;; be; ordinary defstruct accessors are defined as source transforms.
89 (declaim (inline fsc-instance-p))
90 (defun fsc-instance-p (fin)
91 (funcallable-instance-p fin))
92 (defmacro fsc-instance-slots (fin)
93 `(%funcallable-instance-info ,fin sb!vm:instance-data-start))
95 (declaim (inline clos-slots-ref (setf clos-slots-ref)))
96 (declaim (ftype (function (simple-vector index) t) clos-slots-ref))
97 (defun clos-slots-ref (slots index)
98 (svref slots index))
99 (declaim (ftype (function (t simple-vector index) t) (setf clos-slots-ref)))
100 (defun (setf clos-slots-ref) (new-value slots index)
101 (setf (svref slots index) new-value))
103 ;;; Note on implementation under CMU CL >=17 and SBCL: STD-INSTANCE-P
104 ;;; is only used to discriminate between functions (including FINs)
105 ;;; and normal instances, so we can return true on structures also. A
106 ;;; few uses of (OR STD-INSTANCE-P FSC-INSTANCE-P) are changed to
107 ;;; PCL-INSTANCE-P.
108 (declaim (inline std-instance-p))
109 (defun std-instance-p (x)
110 (%instancep x))
112 ;;; When given a funcallable instance, SET-FUN-NAME *must* side-effect
113 ;;; that FIN to give it the name. When given any other kind of
114 ;;; function SET-FUN-NAME is allowed to return a new function which is
115 ;;; "the same" except that it has the name.
117 ;;; In all cases, SET-FUN-NAME must return the new (or same)
118 ;;; function. (Unlike other functions to set stuff, it does not return
119 ;;; the new value.)
120 ;; This is an absolutely terrible name for a function which both assigns
121 ;; the name slot of a function, and _sometimes_ binds a name to a function.
122 (defun set-fun-name (fun new-name)
123 "Set the name of a compiled function object. Return the function."
124 (when (valid-function-name-p fun)
125 (setq fun (fdefinition fun)))
126 (typecase fun
127 (%method-function fun)
128 ;; a closure potentially becomes a different closure
129 (closure (setq fun (sb!impl::set-closure-name fun new-name)))
130 (t (setf (%fun-name fun) new-name)))
131 ;; Fixup name-to-function mappings in cases where the function
132 ;; hasn't been defined by DEFUN. (FIXME: is this right? This logic
133 ;; comes from CMUCL). -- CSR, 2004-12-31
135 ;; Now, given this logic is somewhat suspect to begin with, and is the final
136 ;; remaining contributor to the immortalization of EQL-specialized methods,
137 ;; I'm going to say that we don't create an fdefn for anything
138 ;; whose specializers are not symbols.
139 ;; Otherwise, adding+removing N methods named
140 ;; (SLOW-METHOD BLAH ((EQL <HAIRY-LIST-OBJECT>)))
141 ;; makes them all permanent because FDEFNs are compared by name EQUALity,
142 ;; so each gets its own FDEFN. This is bad, and pretty much useless anyway.
143 (when (and (consp new-name)
144 (or (eq (car new-name) 'slot-accessor)
145 (and (member (car new-name) '(slow-method fast-method))
146 ;; name is: ({SLOW|FAST}-METHOD root <qual>* spec+)
147 (every #'symbolp (car (last new-name))))))
148 (setf (fdefinition new-name) fun))
149 fun)
151 ;;; FIXME: probably no longer needed after init
152 (defmacro precompile-random-code-segments (&optional system)
153 `(progn
154 (eval-when (:compile-toplevel)
155 (update-dispatch-dfuns))
156 (precompile-function-generators ,system)
157 (precompile-dfun-constructors ,system)
158 (precompile-ctors)))
160 ;;; Return true of any object which is either a funcallable-instance,
161 ;;; or an ordinary instance that is not a structure-object.
162 ;;; This used to be implemented as (LAYOUT-FOR-STD-CLASS-P (LAYOUT-OF x))
163 ;;; but LAYOUT-OF is more general than need be here. So this bails out
164 ;;; after the first two clauses of the equivalent COND in LAYOUT-OF
165 ;;; because nothing else could possibly return T.
166 (declaim (inline %pcl-instance-p))
167 (defun %pcl-instance-p (x)
168 (layout-for-std-class-p
169 (cond ((%instancep x) (%instance-layout x))
170 ((funcallable-instance-p x) (%funcallable-instance-layout x))
171 (t (return-from %pcl-instance-p nil)))))
173 ;;; This definition is for interpreted code.
174 (defun pcl-instance-p (x) (declare (explicit-check)) (%pcl-instance-p x))
176 ;;; Both of these operations "work" on structures, which allows the above
177 ;;; weakening of STD-INSTANCE-P.
178 ;;; FIXME: what does the preceding comment mean? You can't use instance-slots
179 ;;; on a structure. (Consider especially a structure of 0 slots.)
180 (defmacro std-instance-slots (x) `(%instance-ref ,x sb!vm:instance-data-start))
182 ;;; FIXME: These functions are called every place we do a
183 ;;; CALL-NEXT-METHOD, and probably other places too. It's likely worth
184 ;;; selectively optimizing them with DEFTRANSFORMs and stuff, rather
185 ;;; than just indiscriminately expanding them inline everywhere.
186 (declaim (inline get-slots get-slots-or-nil))
187 (declaim (ftype (function (t) simple-vector) get-slots))
188 (declaim (ftype (function (t) (or simple-vector null)) get-slots-or-nil))
189 (defun get-slots (instance)
190 (if (std-instance-p instance)
191 (std-instance-slots instance)
192 (fsc-instance-slots instance)))
193 (defun get-slots-or-nil (instance)
194 ;; Suppress a code-deletion note. FIXME: doing the FIXME above,
195 ;; integrating PCL more with the compiler, would remove the need for
196 ;; this icky stuff.
197 (declare (optimize (inhibit-warnings 3)))
198 (when (pcl-instance-p instance)
199 (get-slots instance)))
201 ;; This macro is used only by %CHANGE-CLASS. Can we just do this there?
202 ;; [The code in 'fsc.lisp' which looks like it needs it is commented out]
203 (defmacro get-wrapper (inst)
204 (once-only ((wrapper `(layout-of ,inst)))
205 `(progn
206 (aver (layout-for-std-class-p ,wrapper))
207 ,wrapper)))
209 ;;;; structure-instance stuff
210 ;;;;
211 ;;;; FIXME: Now that the code is SBCL-only, this extra layer of
212 ;;;; abstraction around our native structure representation doesn't
213 ;;;; seem to add anything useful, and could probably go away.
215 ;;; The definition of STRUCTURE-TYPE-P was moved to early-low.lisp.
217 (defun structure-type-slot-description-list (type)
218 (let* ((dd (find-defstruct-description type))
219 (include (dd-include dd))
220 (all-slots (dd-slots dd)))
221 (multiple-value-bind (super slot-overrides)
222 (if (consp include)
223 (values (car include) (mapcar #'car (cdr include)))
224 (values include nil))
225 (let ((included-slots
226 (when super
227 (dd-slots (find-defstruct-description super)))))
228 (loop for slot = (pop all-slots)
229 for included-slot = (pop included-slots)
230 while slot
231 when (or (not included-slot)
232 (member (dsd-name included-slot) slot-overrides :test #'eq))
233 collect slot)))))
235 (defun uninitialized-accessor-function (type slotd)
236 (lambda (&rest args)
237 (declare (ignore args))
238 (error "~:(~A~) function~@[ for ~S ~] not yet initialized."
239 type slotd)))
241 (defun structure-slotd-name (slotd)
242 (dsd-name slotd))
244 (defun structure-slotd-accessor-symbol (slotd)
245 (dsd-accessor-name slotd))
247 (defun structure-slotd-reader-function (slotd)
248 (let ((name (dsd-accessor-name slotd)))
249 (if (fboundp name)
250 (fdefinition name)
251 (uninitialized-accessor-function :reader slotd))))
253 ;;; Return a function to write the slot identified by SLOTD.
254 ;;; This is easy for read/write slots - we just return the accessor
255 ;;; that was already set up - but it requires work for read-only slots.
256 ;;; Basically we get the slotter-setter-lambda-form and compile it.
257 ;;; Using (COERCE lambda-form 'FUNCTION) as used to be done might produce
258 ;;; an interpreted function. I'm not sure whether that's right or wrong,
259 ;;; because if the DEFSTRUCT itself were evaluated, then the ordinary
260 ;;; accessors would indeed be interpreted. However if the DEFSTRUCT were
261 ;;; compiled, and the fasl loaded in a Lisp with *EVALUATOR-MODE* = :INTERPRET,
262 ;;; arguably this is against the expectation that all things got compiled.
263 ;;; But can people really expect that manipulating read-only slots
264 ;;; via (SETF SLOT-VALUE) should be fast?
266 ;;; Damned-if-you-do / damned-if-you don't - the best thing would be to
267 ;;; compile all accessors at "really" compile-time but not store the writer
268 ;;; for a reaadonly slot under the #<fdefn> for #'(SETF slot-name).
270 (defun structure-slotd-writer-function (type slotd)
271 ;; TYPE is not used, because the DD is taken from runtime data.
272 (declare (ignore type))
273 (if (dsd-read-only slotd)
274 ;; We'd like to compile the writer just-in-time and store it
275 ;; back into the STRUCTURE-DIRECT-SLOT-DEFINITION and also
276 ;; the LAYOUT for the class, but we don't have a handle on
277 ;; any of the containing objects. So this has to be a closure.
278 (let ((setter 0))
279 (lambda (newval instance)
280 (if (eql setter 0)
281 (let* ((dd (layout-info (%instance-layout instance)))
282 (f (compile nil (slot-setter-lambda-form dd slotd))))
283 (if (functionp f)
284 (funcall (setq setter f) newval instance)
285 (uninitialized-accessor-function :writer slotd)))
286 (funcall (truly-the function setter) newval instance))))
287 (let ((name `(setf ,(dsd-accessor-name slotd))))
288 (if (fboundp name)
289 (fdefinition name)
290 (uninitialized-accessor-function :writer slotd)))))
292 (defun structure-slotd-type (slotd)
293 (dsd-type slotd))
295 (defun structure-slotd-init-form (slotd)
296 (dsd-default slotd))