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 %print-structure-sans-layout-info
(name stream
)
433 ;; KLUDGE: during PCL build debugging, we can sometimes
434 ;; attempt to print out a PCL object (with null LAYOUT-INFO).
435 (pprint-logical-block (stream nil
:prefix
"#<" :suffix
">")
437 (write-char #\space stream
)
438 (write-string "(no LAYOUT-INFO)" stream
)))
440 (defun %print-structure-sans-slots
(name stream
)
441 ;; the structure type doesn't count as a component for *PRINT-LEVEL*
442 ;; processing. We can likewise elide the logical block processing,
443 ;; since all we have to print is the type name. -- CSR, 2004-10-05
444 (write-string "#S(" stream
)
446 (write-char #\
) stream
))
448 (defun %default-structure-pretty-print
(structure stream
)
449 (let* ((layout (%instance-layout structure
))
450 (name (classoid-name (layout-classoid layout
)))
451 (dd (layout-info layout
)))
453 (%print-structure-sans-layout-info name stream
))
455 (%print-structure-sans-slots name stream
))
457 (pprint-logical-block (stream nil
:prefix
"#S(" :suffix
")")
459 (let ((remaining-slots (dd-slots dd
)))
460 (when remaining-slots
461 (write-char #\space stream
)
462 ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
463 ;; but I can't see why. -- WHN 20000205
464 (pprint-newline :linear stream
)
467 (let ((slot (pop remaining-slots
)))
468 (write-char #\
: stream
)
469 (output-symbol-name (symbol-name (dsd-name slot
)) stream
)
470 (write-char #\space stream
)
471 (pprint-newline :miser stream
)
472 (output-object (funcall (fdefinition (dsd-accessor-name slot
))
475 (when (null remaining-slots
)
477 (write-char #\space stream
)
478 (pprint-newline :linear stream
))))))))))
480 (defun %default-structure-ugly-print
(structure stream
)
481 (let* ((layout (%instance-layout structure
))
482 (name (classoid-name (layout-classoid layout
)))
483 (dd (layout-info layout
)))
485 (%print-structure-sans-layout-info name stream
))
487 (%print-structure-sans-slots name stream
))
489 (descend-into (stream)
490 (write-string "#S(" stream
)
492 (do ((index 0 (1+ index
))
493 (remaining-slots (dd-slots dd
) (cdr remaining-slots
)))
494 ((or (null remaining-slots
)
495 (and (not *print-readably
*)
497 (>= index
*print-length
*)))
498 (if (null remaining-slots
)
499 (write-string ")" stream
)
500 (write-string " ...)" stream
)))
501 (declare (type index index
))
502 (write-string " :" stream
)
503 (let ((slot (first remaining-slots
)))
504 (output-symbol-name (symbol-name (dsd-name slot
)) stream
)
505 (write-char #\space stream
)
507 (funcall (fdefinition (dsd-accessor-name slot
))
511 (defun default-structure-print (structure stream depth
)
512 (declare (ignore depth
))
513 (cond ((funcallable-instance-p structure
)
514 (print-unreadable-object (structure stream
:identity t
:type t
)))
516 (%default-structure-pretty-print structure stream
))
518 (%default-structure-ugly-print structure stream
))))
520 (def!method print-object
((x structure-object
) stream
)
521 (default-structure-print x stream
*current-level-in-print
*))
523 ;;;; testing structure types
525 ;;; Return true if OBJ is an object of the structure type
526 ;;; corresponding to LAYOUT. This is called by the accessor closures,
527 ;;; which have a handle on the type's LAYOUT.
529 ;;; FIXME: This is fairly big, so it should probably become
530 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
531 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
532 ;;; else we could fix things up so that the things which call it are
533 ;;; all closures, so that it's expanded only in a small number of
535 #!-sb-fluid
(declaim (inline typep-to-layout
))
536 (defun typep-to-layout (obj layout
)
537 (declare (type layout layout
) (optimize (speed 3) (safety 0)))
538 (/noshow0
"entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
541 (when (layout-invalid layout
)
542 (error "An obsolete structure accessor function was called."))
543 (/noshow0
"back from testing LAYOUT-INVALID LAYOUT")
544 (and (%instancep obj
)
545 (let ((obj-layout (%instance-layout obj
)))
546 (cond ((eq obj-layout layout
)
547 ;; (In this case OBJ-LAYOUT can't be invalid, because
548 ;; we determined LAYOUT is valid in the test above.)
551 ((layout-invalid obj-layout
)
552 (/noshow0
"LAYOUT-INVALID case")
553 (error 'layout-invalid
554 :expected-type
(layout-classoid obj-layout
)
557 (let ((depthoid (layout-depthoid layout
)))
558 (/noshow0
"DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
560 (/nohexstr layout-inherits
)
561 (and (> (layout-depthoid obj-layout
) depthoid
)
562 (eq (svref (layout-inherits obj-layout
) depthoid
)
565 ;;;; checking structure types
567 ;;; Check that X is an instance of the named structure type.
568 (defmacro %check-structure-type-from-name
(x name
)
569 `(%check-structure-type-from-layout
,x
,(compiler-layout-or-lose name
)))
571 ;;; Check that X is a structure of the type described by DD.
572 (defmacro %check-structure-type-from-dd
(x dd
)
573 (declare (type defstruct-description dd
))
574 (let ((class-name (dd-name dd
)))
576 ((structure funcallable-instance
)
577 `(%check-structure-type-from-layout
579 ,(compiler-layout-or-lose class-name
)))
581 (with-unique-names (xx)
583 (declare (type vector
,xx
))
584 ,@(when (dd-named dd
)
585 `((unless (eql (aref ,xx
0) ',class-name
)
589 :expected-type
`(member ,class-name
)
591 "~@<missing name in instance of ~
592 VECTOR-typed structure ~S: ~2I~_S~:>"
593 :format-arguments
(list ',class-name
,xx
)))))
596 (with-unique-names (xx)
598 (declare (type list
,xx
))
599 ,@(when (dd-named dd
)
600 `((unless (eql (first ,xx
) ',class-name
)
604 :expected-type
`(member ,class-name
)
606 "~@<missing name in instance of LIST-typed structure ~S: ~
608 :format-arguments
(list ',class-name
,xx
)))))
611 ;;; Check that X is an instance of the structure class with layout LAYOUT.
612 (defun %check-structure-type-from-layout
(x layout
)
613 (unless (typep-to-layout x layout
)
616 :expected-type
(classoid-name (layout-classoid layout
))))
620 (/show0
"target-defstruct.lisp end of file")