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 %instance-compare-and-swap (instance index old new)
36 (%instance-compare-and-swap instance index old new)
38 (let ((n-old (%instance-ref instance index)))
40 (%instance-set instance index new))
45 (defun %raw-instance-ref/word (instance index)
46 (declare (type index index))
47 (%raw-instance-ref/word instance index))
48 (defun %raw-instance-set/word (instance index new-value)
49 (declare (type index index)
50 (type sb!vm:word new-value))
51 (%raw-instance-set/word instance index new-value))
53 (defun %raw-instance-ref/single (instance index)
54 (declare (type index index))
55 (%raw-instance-ref/single instance index))
56 (defun %raw-instance-set/single (instance index new-value)
57 (declare (type index index)
58 (type single-float new-value))
59 (%raw-instance-set/single instance index new-value))
61 (defun %raw-instance-ref/double (instance index)
62 (declare (type index index))
63 (%raw-instance-ref/double instance index))
64 (defun %raw-instance-set/double (instance index new-value)
65 (declare (type index index)
66 (type double-float new-value))
67 (%raw-instance-set/double instance index new-value))
69 (defun %raw-instance-ref/complex-single (instance index)
70 (declare (type index index))
71 (%raw-instance-ref/complex-single instance index))
72 (defun %raw-instance-set/complex-single (instance index new-value)
73 (declare (type index index)
74 (type (complex single-float) new-value))
75 (%raw-instance-set/complex-single instance index new-value))
77 (defun %raw-instance-ref/complex-double (instance index)
78 (declare (type index index))
79 (%raw-instance-ref/complex-double instance index))
80 (defun %raw-instance-set/complex-double (instance index new-value)
81 (declare (type index index)
82 (type (complex double-float) new-value))
83 (%raw-instance-set/complex-double instance index new-value))
88 (defun %raw-ref-single (vec index)
89 (declare (type index index))
90 (%raw-ref-single vec index))
92 (defun %raw-ref-double (vec index)
93 (declare (type index index))
94 (%raw-ref-double vec index))
97 (defun %raw-ref-long (vec index)
98 (declare (type index index))
99 (%raw-ref-long vec index))
101 (defun %raw-set-single (vec index val)
102 (declare (type index index))
103 (%raw-set-single vec index val))
105 (defun %raw-set-double (vec index val)
106 (declare (type index index))
107 (%raw-set-double vec index val))
110 (defun %raw-set-long (vec index val)
111 (declare (type index index))
112 (%raw-set-long vec index val))
114 (defun %raw-ref-complex-single (vec index)
115 (declare (type index index))
116 (%raw-ref-complex-single vec index))
118 (defun %raw-ref-complex-double (vec index)
119 (declare (type index index))
120 (%raw-ref-complex-double vec index))
123 (defun %raw-ref-complex-long (vec index)
124 (declare (type index index))
125 (%raw-ref-complex-long vec index))
127 (defun %raw-set-complex-single (vec index val)
128 (declare (type index index))
129 (%raw-set-complex-single vec index val))
131 (defun %raw-set-complex-double (vec index val)
132 (declare (type index index))
133 (%raw-set-complex-double vec index val))
136 (defun %raw-set-complex-long (vec index val)
137 (declare (type index index))
138 (%raw-set-complex-long vec index val))
141 (defun %instance-layout (instance)
142 (%instance-layout instance))
144 (defun %set-instance-layout (instance new-value)
145 (%set-instance-layout instance new-value))
147 (defun %make-funcallable-instance (len)
148 (%make-funcallable-instance len))
150 (defun funcallable-instance-p (x) (funcallable-instance-p x))
152 (defun %funcallable-instance-info (fin i)
153 (%funcallable-instance-info fin i))
155 (defun %set-funcallable-instance-info (fin i new-value)
156 (%set-funcallable-instance-info fin i new-value))
158 (defun funcallable-instance-fun (fin)
159 (%funcallable-instance-function fin))
161 (defun (setf funcallable-instance-fun) (new-value fin)
162 (setf (%funcallable-instance-function fin) new-value))
164 ;;; service function for structure constructors
165 (defun %make-instance-with-layout (layout)
166 ;; Make sure the object ends at a two-word boundary. Note that this does
167 ;; not affect the amount of memory used, since the allocator would add the
168 ;; same padding anyway. However, raw slots are indexed from the length of
169 ;; the object as indicated in the header, so the pad word needs to be
170 ;; included in that length to guarantee proper alignment of raw double float
171 ;; slots, necessary for (at least) the SPARC backend.
172 (let* ((length (layout-length layout))
173 (result (%make-instance (+ length (mod (1+ length) 2)))))
174 (setf (%instance-layout result) layout)
177 ;;;; target-only parts of the DEFSTRUCT top level code
179 ;;; A list of hooks designating functions of one argument, the
180 ;;; classoid, to be called when a defstruct is evaluated.
181 (defvar *defstruct-hooks* nil)
183 ;;; Catch attempts to mess up definitions of symbols in the CL package.
184 (defun protect-cl (symbol)
185 (/show0 "entering PROTECT-CL, SYMBOL=..")
187 (when (and *cold-init-complete-p*
188 (eq (symbol-package symbol) *cl-package*))
189 (cerror "Go ahead and patch the system."
190 "attempting to modify a symbol in the COMMON-LISP package: ~S"
192 (/show0 "leaving PROTECT-CL")
195 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
197 ;;; (The "static" in the name is because it needs to be done not only
198 ;;; in ordinary toplevel %DEFSTRUCT, but also in cold init as early as
199 ;;; possible, to simulate static linking of structure functions as
200 ;;; nearly as possible.)
201 (defun %target-defstruct (dd layout)
202 (declare (type defstruct-description dd))
203 (declare (type layout layout))
205 (/show0 "entering %TARGET-DEFSTRUCT")
207 (remhash (dd-name dd) *typecheckfuns*)
209 ;; (Constructors aren't set up here, because constructors are
210 ;; varied enough (possibly parsing any specified argument list)
211 ;; that we can't reasonably implement them as closures, so we
212 ;; implement them with DEFUN instead.)
214 ;; Set FDEFINITIONs for slot accessors.
215 (dolist (dsd (dd-slots dd))
216 (/show0 "doing FDEFINITION for slot accessor")
217 (let ((accessor-name (dsd-accessor-name dsd)))
218 ;; We mustn't step on any inherited accessors
219 (unless (accessor-inherited-data accessor-name dd)
220 (/show0 "ACCESSOR-NAME=..")
221 (/hexstr accessor-name)
222 (protect-cl accessor-name)
223 (/hexstr "getting READER-FUN and WRITER-FUN")
224 (multiple-value-bind (reader-fun writer-fun)
225 (slot-accessor-funs dd dsd)
226 (declare (type function reader-fun writer-fun))
227 (/show0 "got READER-FUN and WRITER-FUN=..")
229 (setf (symbol-function accessor-name) reader-fun)
230 (unless (dsd-read-only dsd)
231 (/show0 "setting FDEFINITION for WRITER-FUN=..")
233 (setf (fdefinition `(setf ,accessor-name)) writer-fun))))))
235 ;; Set FDEFINITION for copier.
236 (when (dd-copier-name dd)
237 (/show0 "doing FDEFINITION for copier")
238 (protect-cl (dd-copier-name dd))
239 ;; We can't use COPY-STRUCTURE for other kinds of objects, notably
240 ;; funcallable structures, since it returns a STRUCTURE-OBJECT.
241 ;; (And funcallable instances don't need copiers anyway.)
242 (aver (eql (dd-type dd) 'structure))
243 (setf (symbol-function (dd-copier-name dd))
244 ;; FIXME: should use a closure which checks arg type before copying
247 ;; Set FDEFINITION for predicate.
248 (when (dd-predicate-name dd)
249 (/show0 "doing FDEFINITION for predicate")
250 (protect-cl (dd-predicate-name dd))
251 (setf (symbol-function (dd-predicate-name dd))
253 ;; structures with LAYOUTs
254 ((structure funcallable-structure)
255 (/show0 "with-LAYOUT case")
257 (locally ; <- to keep SAFETY 0 from affecting arg count checking
258 (declare (optimize (speed 3) (safety 0)))
259 (/noshow0 "in with-LAYOUT structure predicate closure, OBJECT,LAYOUT=..")
262 (typep-to-layout object layout))))
263 ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
265 ;; FIXME: should handle the :NAMED T case in these cases
267 (/show0 ":TYPE VECTOR case")
270 (/show0 ":TYPE LIST case")
274 (setf (fdocumentation (dd-name dd) 'structure)
277 ;; the BOUNDP test here is to get past cold-init.
278 (when (boundp '*defstruct-hooks*)
279 (dolist (fun *defstruct-hooks*)
280 (funcall fun (find-classoid (dd-name dd)))))
282 (/show0 "leaving %TARGET-DEFSTRUCT")
285 ;;;; generating out-of-line slot accessor functions
287 ;;; FIXME: Ideally, the presence of the type checks in the functions
288 ;;; here would be conditional on the optimization policy at the point
289 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
290 ;;; thing, putting in the type checks unconditionally.)
292 ;;; KLUDGE: Why use this closure approach at all? The macrology in
293 ;;; SLOT-ACCESSOR-FUNS seems to be half stub, half OAOOM to me. --DFL
295 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
296 (defun slot-accessor-funs (dd dsd)
298 #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
300 ;; various code generators
302 ;; Note: They're only minimally parameterized, and cavalierly grab
303 ;; things like INSTANCE and DSD-INDEX from the namespace they're
305 (macrolet (;; code shared between funcallable instance case and the
306 ;; ordinary STRUCTURE-OBJECT case: Handle native
307 ;; structures with LAYOUTs and (possibly) raw slots.
308 (%native-slot-accessor-funs (dd-ref-fun-name)
309 (let ((instance-type-check-form
310 '(%check-structure-type-from-layout instance layout)))
311 (/show "macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form)
312 `(let ((layout (dd-layout-or-lose dd))
313 (dsd-raw-type (dsd-raw-type dsd)))
314 #+sb-xc (/show0 "in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
315 ;; Map over all the possible RAW-TYPEs, compiling
316 ;; a different closure function for each one, so
317 ;; that once the COND over RAW-TYPEs happens (at
318 ;; the time closure is allocated) there are no
319 ;; more decisions to be made and things execute
320 ;; reasonably efficiently.
323 ((eql dsd-raw-type t)
324 #+sb-xc (/show0 "in nonraw slot case")
325 (%slotplace-accessor-funs
326 (,dd-ref-fun-name instance dsd-index)
327 ,instance-type-check-form))
329 ,@(mapcar (lambda (rtd)
330 (let ((raw-type (raw-slot-data-raw-type rtd))
332 (raw-slot-data-accessor-name rtd)))
333 `((equal dsd-raw-type ',raw-type)
334 #+sb-xc (/show0 "in raw slot case")
335 (%slotplace-accessor-funs
336 (,accessor-name instance dsd-index)
337 ,instance-type-check-form))))
338 *raw-slot-data-list*)
341 (bug "unexpected DSD-RAW-TYPE ~S" dsd-raw-type))))))
342 ;; code shared between DEFSTRUCT :TYPE LIST and
343 ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
344 ;; structure" case, with no LAYOUTs and no raw slots.
345 (%colontyped-slot-accessor-funs () (error "stub"))
346 ;; the common structure of the raw-slot and not-raw-slot
347 ;; cases, defined in terms of the writable SLOTPLACE. All
348 ;; possible flavors of slot access should be able to pass
350 (%slotplace-accessor-funs (slotplace instance-type-check-form)
351 (/show "macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form)
352 `(let ((typecheckfun (typespec-typecheckfun dsd-type)))
353 (values (if (dsd-safe-p dsd)
355 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
356 ,instance-type-check-form
357 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
360 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
361 ,instance-type-check-form
362 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
363 (let ((value ,slotplace))
364 (funcall typecheckfun value)
366 (lambda (new-value instance)
367 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
368 ,instance-type-check-form
369 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
370 (funcall typecheckfun new-value)
371 (/noshow0 "back from TYPECHECKFUN")
372 (setf ,slotplace new-value))))))
374 (let ((dsd-index (dsd-index dsd))
375 (dsd-type (dsd-type dsd)))
377 #+sb-xc (/show0 "got DSD-TYPE=..")
378 #+sb-xc (/hexstr dsd-type)
383 #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
384 (%native-slot-accessor-funs %instance-ref))
386 ;; structures with the :TYPE option
388 ;; FIXME: Worry about these later..
390 ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
391 ;; layout completely, so that raw slots are impossible.
393 (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
394 `(%check-structure-type-from-dd
397 (dd-type-slot-accessor-funs aref
402 ;;; Copy any old kind of structure.
403 (defun copy-structure (structure)
405 "Return a copy of STRUCTURE with the same (EQL) slot values."
406 (declare (type structure-object structure))
407 (let* ((len (%instance-length structure))
408 (res (%make-instance len))
409 (layout (%instance-layout structure))
410 (nuntagged (layout-n-untagged-slots layout)))
412 (declare (type index len))
413 (when (layout-invalid layout)
414 (error "attempt to copy an obsolete structure:~% ~S" structure))
416 ;; Copy ordinary slots.
417 (dotimes (i (- len nuntagged))
418 (declare (type index i))
419 (setf (%instance-ref res i)
420 (%instance-ref structure i)))
423 (dotimes (i nuntagged)
424 (declare (type index i))
425 (setf (%raw-instance-ref/word res i)
426 (%raw-instance-ref/word structure i)))
430 ;;; default PRINT-OBJECT method
432 (defun %default-structure-pretty-print (structure stream)
433 (let* ((layout (%instance-layout structure))
434 (name (classoid-name (layout-classoid layout)))
435 (dd (layout-info layout)))
436 ;; KLUDGE: during the build process with SB-SHOW, we can sometimes
437 ;; attempt to print out a PCL object (with null LAYOUT-INFO).
440 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
442 (write-char #\space stream)
443 (write-string "(no LAYOUT-INFO)"))
444 (return-from %default-structure-pretty-print nil))
445 ;; the structure type doesn't count as a component for
446 ;; *PRINT-LEVEL* processing. We can likewise elide the logical
447 ;; block processing, since all we have to print is the type name.
448 ;; -- CSR, 2004-10-05
449 (when (and dd (null (dd-slots dd)))
450 (write-string "#S(" stream)
452 (write-char #\) stream)
453 (return-from %default-structure-pretty-print nil))
454 (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
456 (let ((remaining-slots (dd-slots dd)))
457 (when remaining-slots
458 (write-char #\space stream)
459 ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
460 ;; but I can't see why. -- WHN 20000205
461 (pprint-newline :linear stream)
464 (let ((slot (pop remaining-slots)))
465 (write-char #\: stream)
466 (output-symbol-name (symbol-name (dsd-name slot)) stream)
467 (write-char #\space stream)
468 (pprint-newline :miser stream)
469 (output-object (funcall (fdefinition (dsd-accessor-name slot))
472 (when (null remaining-slots)
474 (write-char #\space stream)
475 (pprint-newline :linear stream))))))))
476 (defun %default-structure-ugly-print (structure stream)
477 (let* ((layout (%instance-layout structure))
478 (name (classoid-name (layout-classoid layout)))
479 (dd (layout-info layout)))
480 (when (and dd (null (dd-slots dd)))
481 (write-string "#S(" stream)
483 (write-char #\) stream)
484 (return-from %default-structure-ugly-print nil))
485 (descend-into (stream)
486 (write-string "#S(" stream)
488 (do ((index 0 (1+ index))
489 (remaining-slots (dd-slots dd) (cdr remaining-slots)))
490 ((or (null remaining-slots)
491 (and (not *print-readably*)
493 (>= index *print-length*)))
494 (if (null remaining-slots)
495 (write-string ")" stream)
496 (write-string " ...)" stream)))
497 (declare (type index index))
498 (write-char #\space stream)
499 (write-char #\: stream)
500 (let ((slot (first remaining-slots)))
501 (output-symbol-name (symbol-name (dsd-name slot)) stream)
502 (write-char #\space stream)
504 (funcall (fdefinition (dsd-accessor-name slot))
507 (defun default-structure-print (structure stream depth)
508 (declare (ignore depth))
509 (cond ((funcallable-instance-p structure)
510 (print-unreadable-object (structure stream :identity t :type t)))
512 (%default-structure-pretty-print structure stream))
514 (%default-structure-ugly-print structure stream))))
515 (def!method print-object ((x structure-object) stream)
516 (default-structure-print x stream *current-level-in-print*))
518 ;;;; testing structure types
520 ;;; Return true if OBJ is an object of the structure type
521 ;;; corresponding to LAYOUT. This is called by the accessor closures,
522 ;;; which have a handle on the type's LAYOUT.
524 ;;; FIXME: This is fairly big, so it should probably become
525 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
526 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
527 ;;; else we could fix things up so that the things which call it are
528 ;;; all closures, so that it's expanded only in a small number of
530 #!-sb-fluid (declaim (inline typep-to-layout))
531 (defun typep-to-layout (obj layout)
532 (declare (type layout layout) (optimize (speed 3) (safety 0)))
533 (/noshow0 "entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
536 (when (layout-invalid layout)
537 (error "An obsolete structure accessor function was called."))
538 (/noshow0 "back from testing LAYOUT-INVALID LAYOUT")
539 (and (%instancep obj)
540 (let ((obj-layout (%instance-layout obj)))
541 (cond ((eq obj-layout layout)
542 ;; (In this case OBJ-LAYOUT can't be invalid, because
543 ;; we determined LAYOUT is valid in the test above.)
546 ((layout-invalid obj-layout)
547 (/noshow0 "LAYOUT-INVALID case")
548 (error 'layout-invalid
549 :expected-type (layout-classoid obj-layout)
552 (let ((depthoid (layout-depthoid layout)))
553 (/noshow0 "DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
555 (/nohexstr layout-inherits)
556 (and (> (layout-depthoid obj-layout) depthoid)
557 (eq (svref (layout-inherits obj-layout) depthoid)
560 ;;;; checking structure types
562 ;;; Check that X is an instance of the named structure type.
563 (defmacro %check-structure-type-from-name (x name)
564 `(%check-structure-type-from-layout ,x ,(compiler-layout-or-lose name)))
566 ;;; Check that X is a structure of the type described by DD.
567 (defmacro %check-structure-type-from-dd (x dd)
568 (declare (type defstruct-description dd))
569 (let ((class-name (dd-name dd)))
571 ((structure funcallable-instance)
572 `(%check-structure-type-from-layout
574 ,(compiler-layout-or-lose class-name)))
576 (with-unique-names (xx)
578 (declare (type vector ,xx))
579 ,@(when (dd-named dd)
580 `((unless (eql (aref ,xx 0) ',class-name)
584 :expected-type `(member ,class-name)
586 "~@<missing name in instance of ~
587 VECTOR-typed structure ~S: ~2I~_S~:>"
588 :format-arguments (list ',class-name ,xx)))))
591 (with-unique-names (xx)
593 (declare (type list ,xx))
594 ,@(when (dd-named dd)
595 `((unless (eql (first ,xx) ',class-name)
599 :expected-type `(member ,class-name)
601 "~@<missing name in instance of LIST-typed structure ~S: ~
603 :format-arguments (list ',class-name ,xx)))))
606 ;;; Check that X is an instance of the structure class with layout LAYOUT.
607 (defun %check-structure-type-from-layout (x layout)
608 (unless (typep-to-layout x layout)
611 :expected-type (classoid-name (layout-classoid layout))))
615 (/show0 "target-defstruct.lisp end of file")