1 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; This software is derived from the CMU CL system, which was
5 ;;;; written at Carnegie Mellon University and released into the
6 ;;;; public domain. The software is in the public domain and is
7 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
8 ;;;; files for more information.
10 (in-package "SB!KERNEL")
12 (/show0 "target-defstruct.lisp 12")
14 ;;;; structure frobbing primitives
16 ;;; Allocate a new instance with LENGTH data slots.
17 (defun %make-instance (length)
18 (declare (type index length))
19 (%make-instance length))
21 ;;; Given an instance, return its length.
22 (defun %instance-length (instance)
23 (declare (type instance instance))
24 (%instance-length instance))
26 ;;; Return the value from the INDEXth slot of INSTANCE. This is SETFable.
27 (defun %instance-ref (instance index)
28 (%instance-ref instance index))
30 ;;; Set the INDEXth slot of INSTANCE to NEW-VALUE.
31 (defun %instance-set (instance index new-value)
32 (setf (%instance-ref instance index) new-value))
34 (defun %raw-ref-single (vec index)
35 (declare (type index index))
36 (%raw-ref-single vec index))
38 (defun %raw-ref-double (vec index)
39 (declare (type index index))
40 (%raw-ref-double vec index))
43 (defun %raw-ref-long (vec index)
44 (declare (type index index))
45 (%raw-ref-long vec index))
47 (defun %raw-set-single (vec index val)
48 (declare (type index index))
49 (%raw-set-single vec index val))
51 (defun %raw-set-double (vec index val)
52 (declare (type index index))
53 (%raw-set-double vec index val))
56 (defun %raw-set-long (vec index val)
57 (declare (type index index))
58 (%raw-set-long vec index val))
60 (defun %raw-ref-complex-single (vec index)
61 (declare (type index index))
62 (%raw-ref-complex-single vec index))
64 (defun %raw-ref-complex-double (vec index)
65 (declare (type index index))
66 (%raw-ref-complex-double vec index))
69 (defun %raw-ref-complex-long (vec index)
70 (declare (type index index))
71 (%raw-ref-complex-long vec index))
73 (defun %raw-set-complex-single (vec index val)
74 (declare (type index index))
75 (%raw-set-complex-single vec index val))
77 (defun %raw-set-complex-double (vec index val)
78 (declare (type index index))
79 (%raw-set-complex-double vec index val))
82 (defun %raw-set-complex-long (vec index val)
83 (declare (type index index))
84 (%raw-set-complex-long vec index val))
86 (defun %instance-layout (instance)
87 (%instance-layout instance))
89 (defun %set-instance-layout (instance new-value)
90 (%set-instance-layout instance new-value))
92 (defun %make-funcallable-instance (len layout)
93 (%make-funcallable-instance len layout))
95 (defun funcallable-instance-p (x) (funcallable-instance-p x))
97 (defun %funcallable-instance-info (fin i)
98 (%funcallable-instance-info fin i))
100 (defun %set-funcallable-instance-info (fin i new-value)
101 (%set-funcallable-instance-info fin i new-value))
103 (defun funcallable-instance-fun (fin)
104 (%funcallable-instance-lexenv fin))
106 ;;; The heart of the magic of funcallable instances ("FINs"). The
107 ;;; function for a FIN must be a magical INSTANCE-LAMBDA form. When
108 ;;; called (as with any other function), we grab the code pointer, and
109 ;;; call it, leaving the original function object in LEXENV (in case
110 ;;; it was a closure). If it is actually a FIN, then we need to do an
111 ;;; extra indirection with funcallable-instance-lexenv to get at any
112 ;;; closure environment. This extra indirection is set up when
113 ;;; accessing the closure environment of an INSTANCE-LAMBDA. Note that
114 ;;; the original FIN pointer is lost, so if the called function wants
115 ;;; to get at the original object to do some slot accesses, it must
116 ;;; close over the FIN object.
118 ;;; If we set the FIN function to be a FIN, we directly copy across
119 ;;; both the code pointer and the lexenv, since that code pointer (for
120 ;;; an instance-lambda) is expecting that lexenv to be accessed. This
121 ;;; effectively pre-flattens what would otherwise be a chain of
122 ;;; indirections. (That used to happen when PCL dispatch functions
123 ;;; were byte-compiled; now that the byte compiler is gone, I can't
124 ;;; think of another example offhand. -- WHN 2001-10-06)
126 ;;; The only loss is that if someone accesses the
127 ;;; FUNCALLABLE-INSTANCE-FUN, then won't get a FIN back. This probably
128 ;;; doesn't matter, since PCL only sets the FIN function.
129 (defun (setf funcallable-instance-fun) (new-value fin)
130 (setf (%funcallable-instance-fun fin)
131 (%closure-fun new-value))
132 (setf (%funcallable-instance-lexenv fin)
133 (if (funcallable-instance-p new-value)
134 (%funcallable-instance-lexenv new-value)
137 ;;; service function for structure constructors
138 (defun %make-instance-with-layout (layout)
139 (let ((result (%make-instance (layout-length layout))))
140 (setf (%instance-layout result) layout)
143 ;;;; target-only parts of the DEFSTRUCT top level code
145 ;;; Catch attempts to mess up definitions of symbols in the CL package.
146 (defun protect-cl (symbol)
147 (/show0 "entering PROTECT-CL, SYMBOL=..")
149 (when (and *cold-init-complete-p*
150 (eq (symbol-package symbol) *cl-package*))
151 (cerror "Go ahead and patch the system."
152 "attempting to modify a symbol in the COMMON-LISP package: ~S"
154 (/show0 "leaving PROTECT-CL")
157 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
159 ;;; (The "static" in the name is because it needs to be done not only
160 ;;; in ordinary toplevel %DEFSTRUCT, but also in cold init as early as
161 ;;; possible, to simulate static linking of structure functions as
162 ;;; nearly as possible.)
163 (defun %target-defstruct (dd layout)
164 (declare (type defstruct-description dd))
165 (declare (type layout layout))
167 (/show0 "entering %TARGET-DEFSTRUCT")
169 (remhash (dd-name dd) *typecheckfuns*)
171 ;; (Constructors aren't set up here, because constructors are
172 ;; varied enough (possibly parsing any specified argument list)
173 ;; that we can't reasonably implement them as closures, so we
174 ;; implement them with DEFUN instead.)
176 ;; Set FDEFINITIONs for slot accessors.
177 (dolist (dsd (dd-slots dd))
178 (/show0 "doing FDEFINITION for slot accessor")
179 (let ((accessor-name (dsd-accessor-name dsd)))
180 (/show0 "ACCESSOR-NAME=..")
181 (/hexstr accessor-name)
182 (protect-cl accessor-name)
183 (/hexstr "getting READER-FUN and WRITER-FUN")
184 (multiple-value-bind (reader-fun writer-fun) (slot-accessor-funs dd dsd)
185 (declare (type function reader-fun writer-fun))
186 (/show0 "got READER-FUN and WRITER-FUN=..")
188 (setf (symbol-function accessor-name) reader-fun)
189 (unless (dsd-read-only dsd)
190 (/show0 "setting FDEFINITION for WRITER-FUN=..")
192 (setf (fdefinition `(setf ,accessor-name)) writer-fun)))))
194 ;; Set FDEFINITION for copier.
195 (when (dd-copier-name dd)
196 (/show0 "doing FDEFINITION for copier")
197 (protect-cl (dd-copier-name dd))
198 ;; We can't use COPY-STRUCTURE for other kinds of objects, notably
199 ;; funcallable structures, since it returns a STRUCTURE-OBJECT.
200 ;; (And funcallable instances don't need copiers anyway.)
201 (aver (eql (dd-type dd) 'structure))
202 (setf (symbol-function (dd-copier-name dd))
203 ;; FIXME: should use a closure which checks arg type before copying
206 ;; Set FDEFINITION for predicate.
207 (when (dd-predicate-name dd)
208 (/show0 "doing FDEFINITION for predicate")
209 (protect-cl (dd-predicate-name dd))
210 (setf (symbol-function (dd-predicate-name dd))
212 ;; structures with LAYOUTs
213 ((structure funcallable-structure)
214 (/show0 "with-LAYOUT case")
216 (locally ; <- to keep SAFETY 0 from affecting arg count checking
217 (declare (optimize (speed 3) (safety 0)))
218 (/noshow0 "in with-LAYOUT structure predicate closure, OBJECT,LAYOUT=..")
221 (typep-to-layout object layout))))
222 ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
224 ;; FIXME: should handle the :NAMED T case in these cases
226 (/show0 ":TYPE VECTOR case")
229 (/show0 ":TYPE LIST case")
233 (setf (fdocumentation (dd-name dd) 'type)
236 (/show0 "leaving %TARGET-DEFSTRUCT")
239 ;;;; generating out-of-line slot accessor functions
241 ;;; FIXME: Ideally, the presence of the type checks in the functions
242 ;;; here would be conditional on the optimization policy at the point
243 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
244 ;;; thing, putting in the type checks unconditionally.)
246 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
247 (defun slot-accessor-funs (dd dsd)
249 #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
251 ;; various code generators
253 ;; Note: They're only minimally parameterized, and cavalierly grab
254 ;; things like INSTANCE and DSD-INDEX from the namespace they're
256 (macrolet (;; code shared between funcallable instance case and the
257 ;; ordinary STRUCTURE-OBJECT case: Handle native
258 ;; structures with LAYOUTs and (possibly) raw slots.
259 (%native-slot-accessor-funs (dd-ref-fun-name)
260 (let ((instance-type-check-form
261 '(%check-structure-type-from-layout instance layout)))
262 (/show "macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form)
263 `(let ((layout (dd-layout-or-lose dd))
264 (dsd-raw-type (dsd-raw-type dsd)))
265 #+sb-xc (/show0 "in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
266 ;; Map over all the possible RAW-TYPEs, compiling
267 ;; a different closure function for each one, so
268 ;; that once the COND over RAW-TYPEs happens (at
269 ;; the time closure is allocated) there are no
270 ;; more decisions to be made and things execute
271 ;; reasonably efficiently.
274 ((eql dsd-raw-type t)
275 #+sb-xc (/show0 "in nonraw slot case")
276 (%slotplace-accessor-funs
277 (,dd-ref-fun-name instance dsd-index)
278 ,instance-type-check-form))
280 ,@(mapcar (lambda (rtd)
281 (let ((raw-type (raw-slot-data-raw-type rtd))
283 (raw-slot-data-accessor-name rtd))
284 (n-words (raw-slot-data-n-words rtd)))
285 `((equal dsd-raw-type ',raw-type)
286 #+sb-xc (/show0 "in raw slot case")
287 (let ((raw-index (dd-raw-index dd)))
288 (multiple-value-bind (scaled-dsd-index
290 (floor dsd-index ,n-words)
291 (aver (zerop misalignment))
292 (%slotplace-accessor-funs
293 (,accessor-name (,dd-ref-fun-name
297 ,instance-type-check-form))))))
298 *raw-slot-data-list*)
301 (bug "unexpected DSD-RAW-TYPE ~S" dsd-raw-type))))))
302 ;; code shared between DEFSTRUCT :TYPE LIST and
303 ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
304 ;; structure" case, with no LAYOUTs and no raw slots.
305 (%colontyped-slot-accessor-funs () (error "stub"))
306 ;; the common structure of the raw-slot and not-raw-slot
307 ;; cases, defined in terms of the writable SLOTPLACE. All
308 ;; possible flavors of slot access should be able to pass
310 (%slotplace-accessor-funs (slotplace instance-type-check-form)
311 (/show "macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form)
312 `(values (lambda (instance)
313 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
314 ,instance-type-check-form
315 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
317 (let ((typecheckfun (typespec-typecheckfun dsd-type)))
318 (lambda (new-value instance)
319 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
320 ,instance-type-check-form
321 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
322 (funcall typecheckfun new-value)
323 (/noshow0 "back from TYPECHECKFUN")
324 (setf ,slotplace new-value))))))
326 (let ((dsd-index (dsd-index dsd))
327 (dsd-type (dsd-type dsd)))
329 #+sb-xc (/show0 "got DSD-TYPE=..")
330 #+sb-xc (/hexstr dsd-type)
335 #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
336 (%native-slot-accessor-funs %instance-ref))
338 ;; structures with the :TYPE option
340 ;; FIXME: Worry about these later..
342 ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
343 ;; layout completely, so that raw slots are impossible.
345 (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
346 `(%check-structure-type-from-dd
349 (dd-type-slot-accessor-funs aref
354 ;;; Copy any old kind of structure.
355 (defun copy-structure (structure)
357 "Return a copy of STRUCTURE with the same (EQL) slot values."
358 (declare (type structure-object structure))
359 (let* ((len (%instance-length structure))
360 (res (%make-instance len))
361 (layout (%instance-layout structure)))
363 (declare (type index len))
364 (when (layout-invalid layout)
365 (error "attempt to copy an obsolete structure:~% ~S" structure))
367 ;; Copy ordinary slots.
369 (declare (type index i))
370 (setf (%instance-ref res i)
371 (%instance-ref structure i)))
374 (let ((raw-index (dd-raw-index (layout-info layout))))
376 (let* ((data (%instance-ref structure raw-index))
377 (raw-len (length data))
378 (new (make-array raw-len :element-type '(unsigned-byte 32))))
379 (declare (type (simple-array (unsigned-byte 32) (*)) data))
380 (setf (%instance-ref res raw-index) new)
382 (setf (aref new i) (aref data i))))))
386 ;;; default PRINT-OBJECT and MAKE-LOAD-FORM methods
388 (defun %default-structure-pretty-print (structure stream)
389 (let* ((layout (%instance-layout structure))
390 (name (sb!xc:class-name (layout-class layout)))
391 (dd (layout-info layout)))
392 (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
394 (let ((remaining-slots (dd-slots dd)))
395 (when remaining-slots
396 (write-char #\space stream)
397 ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
398 ;; but I can't see why. -- WHN 20000205
399 (pprint-newline :linear stream)
402 (let ((slot (pop remaining-slots)))
403 (write-char #\: stream)
404 (output-symbol-name (dsd-%name slot) stream)
405 (write-char #\space stream)
406 (pprint-newline :miser stream)
407 (output-object (funcall (fdefinition (dsd-accessor-name slot))
410 (when (null remaining-slots)
412 (write-char #\space stream)
413 (pprint-newline :linear stream))))))))
414 (defun %default-structure-ugly-print (structure stream)
415 (let* ((layout (%instance-layout structure))
416 (name (sb!xc:class-name (layout-class layout)))
417 (dd (layout-info layout)))
418 (descend-into (stream)
419 (write-string "#S(" stream)
421 (do ((index 0 (1+ index))
422 (remaining-slots (dd-slots dd) (cdr remaining-slots)))
423 ((or (null remaining-slots)
424 (and (not *print-readably*)
426 (>= index *print-length*)))
427 (if (null remaining-slots)
428 (write-string ")" stream)
429 (write-string " ...)" stream)))
430 (declare (type index index))
431 (write-char #\space stream)
432 (write-char #\: stream)
433 (let ((slot (first remaining-slots)))
434 (output-symbol-name (dsd-%name slot) stream)
435 (write-char #\space stream)
437 (funcall (fdefinition (dsd-accessor-name slot))
440 (defun default-structure-print (structure stream depth)
441 (declare (ignore depth))
442 (cond ((funcallable-instance-p structure)
443 (print-unreadable-object (structure stream :identity t :type t)))
445 (%default-structure-pretty-print structure stream))
447 (%default-structure-ugly-print structure stream))))
448 (def!method print-object ((x structure-object) stream)
449 (default-structure-print x stream *current-level-in-print*))
451 (defun make-load-form-saving-slots (object &key slot-names environment)
452 (declare (ignore object environment))
454 (error "stub: MAKE-LOAD-FORM-SAVING-SLOTS :SLOT-NAMES not implemented") ; KLUDGE
455 :sb-just-dump-it-normally))
457 ;;;; testing structure types
459 ;;; Return true if OBJ is an object of the structure type
460 ;;; corresponding to LAYOUT. This is called by the accessor closures,
461 ;;; which have a handle on the type's LAYOUT.
463 ;;; FIXME: This is fairly big, so it should probably become
464 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
465 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
466 ;;; else we could fix things up so that the things which call it are
467 ;;; all closures, so that it's expanded only in a small number of
469 #!-sb-fluid (declaim (inline typep-to-layout))
470 (defun typep-to-layout (obj layout)
471 (declare (type layout layout) (optimize (speed 3) (safety 0)))
472 (/noshow0 "entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
475 (when (layout-invalid layout)
476 (error "An obsolete structure accessor function was called."))
477 (/noshow0 "back from testing LAYOUT-INVALID LAYOUT")
478 ;; FIXME: CMU CL used (%INSTANCEP OBJ) here. Check that
479 ;; (TYPEP OBJ 'INSTANCE) is optimized to equally efficient code.
480 (and (typep obj 'instance)
481 (let ((obj-layout (%instance-layout obj)))
482 (cond ((eq obj-layout layout)
483 ;; (In this case OBJ-LAYOUT can't be invalid, because
484 ;; we determined LAYOUT is valid in the test above.)
487 ((layout-invalid obj-layout)
488 (/noshow0 "LAYOUT-INVALID case")
489 (error 'layout-invalid
490 :expected-type (layout-class obj-layout)
493 (let ((depthoid (layout-depthoid layout)))
494 (/noshow0 "DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
496 (/nohexstr layout-inherits)
497 (and (> (layout-depthoid obj-layout) depthoid)
498 (eq (svref (layout-inherits obj-layout) depthoid)
501 ;;;; checking structure types
503 ;;; Check that X is an instance of the named structure type.
504 (defmacro %check-structure-type-from-name (x name)
505 `(%check-structure-type-from-layout ,x ,(compiler-layout-or-lose name)))
507 ;;; Check that X is a structure of the type described by DD.
508 (defmacro %check-structure-type-from-dd (x dd)
509 (declare (type defstruct-description dd))
510 (let ((class-name (dd-name dd)))
512 ((structure funcallable-instance)
513 `(%check-structure-type-from-layout
515 ,(compiler-layout-or-lose class-name)))
517 (let ((xx (gensym "X")))
519 (declare (type vector ,xx))
520 ,@(when (dd-named dd)
521 `((unless (eql (aref ,xx 0) ',class-name)
525 :expected-type `(member ,class-name)
527 "~@<missing name in instance of ~
528 VECTOR-typed structure ~S: ~2I~_S~:>"
529 :format-arguments (list ',class-name ,xx)))))
532 (let ((xx (gensym "X")))
534 (declare (type list ,xx))
535 ,@(when (dd-named dd)
536 `((unless (eql (first ,xx) ',class-name)
540 :expected-type `(member ,class-name)
542 "~@<missing name in instance of LIST-typed structure ~S: ~
544 :format-arguments (list ',class-name ,xx)))))
547 ;;; Check that X is an instance of the structure class with layout LAYOUT.
548 (defun %check-structure-type-from-layout (x layout)
549 (unless (typep-to-layout x layout)
552 :expected-type (class-name (layout-class layout))))
555 (/show0 "target-defstruct.lisp end of file")