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))
36 (defun %raw-instance-ref/word (instance index)
37 (declare (type index index))
38 (%raw-instance-ref/word instance index))
39 (defun %raw-instance-set/word (instance index new-value)
40 (declare (type index index)
41 (type sb!vm:word new-value))
42 (%raw-instance-set/word instance index new-value))
44 (defun %raw-instance-ref/single (instance index)
45 (declare (type index index))
46 (%raw-instance-ref/single instance index))
47 (defun %raw-instance-set/single (instance index new-value)
48 (declare (type index index)
49 (type single-float new-value))
50 (%raw-instance-set/single instance index new-value))
52 (defun %raw-instance-ref/double (instance index)
53 (declare (type index index))
54 (%raw-instance-ref/double instance index))
55 (defun %raw-instance-set/double (instance index new-value)
56 (declare (type index index)
57 (type double-float new-value))
58 (%raw-instance-set/double instance index new-value))
60 (defun %raw-instance-ref/complex-single (instance index)
61 (declare (type index index))
62 (%raw-instance-ref/complex-single instance index))
63 (defun %raw-instance-set/complex-single (instance index new-value)
64 (declare (type index index)
65 (type (complex single-float) new-value))
66 (%raw-instance-set/complex-single instance index new-value))
68 (defun %raw-instance-ref/complex-double (instance index)
69 (declare (type index index))
70 (%raw-instance-ref/complex-double instance index))
71 (defun %raw-instance-set/complex-double (instance index new-value)
72 (declare (type index index)
73 (type (complex double-float) new-value))
74 (%raw-instance-set/complex-double instance index new-value)))
76 (defun %raw-ref-single (vec index)
77 (declare (type index index))
78 (%raw-ref-single vec index))
80 (defun %raw-ref-double (vec index)
81 (declare (type index index))
82 (%raw-ref-double vec index))
85 (defun %raw-ref-long (vec index)
86 (declare (type index index))
87 (%raw-ref-long vec index))
89 (defun %raw-set-single (vec index val)
90 (declare (type index index))
91 (%raw-set-single vec index val))
93 (defun %raw-set-double (vec index val)
94 (declare (type index index))
95 (%raw-set-double vec index val))
98 (defun %raw-set-long (vec index val)
99 (declare (type index index))
100 (%raw-set-long vec index val))
102 (defun %raw-ref-complex-single (vec index)
103 (declare (type index index))
104 (%raw-ref-complex-single vec index))
106 (defun %raw-ref-complex-double (vec index)
107 (declare (type index index))
108 (%raw-ref-complex-double vec index))
111 (defun %raw-ref-complex-long (vec index)
112 (declare (type index index))
113 (%raw-ref-complex-long vec index))
115 (defun %raw-set-complex-single (vec index val)
116 (declare (type index index))
117 (%raw-set-complex-single vec index val))
119 (defun %raw-set-complex-double (vec index val)
120 (declare (type index index))
121 (%raw-set-complex-double vec index val))
124 (defun %raw-set-complex-long (vec index val)
125 (declare (type index index))
126 (%raw-set-complex-long vec index val))
128 (defun %instance-layout (instance)
129 (%instance-layout instance))
131 (defun %set-instance-layout (instance new-value)
132 (%set-instance-layout instance new-value))
134 (defun %make-funcallable-instance (len layout)
135 (%make-funcallable-instance len layout))
137 (defun funcallable-instance-p (x) (funcallable-instance-p x))
139 (defun %funcallable-instance-info (fin i)
140 (%funcallable-instance-info fin i))
142 (defun %set-funcallable-instance-info (fin i new-value)
143 (%set-funcallable-instance-info fin i new-value))
145 (defun funcallable-instance-fun (fin)
146 (%funcallable-instance-lexenv fin))
148 ;;; The heart of the magic of funcallable instances ("FINs"). The
149 ;;; function for a FIN must be a magical INSTANCE-LAMBDA form. When
150 ;;; called (as with any other function), we grab the code pointer, and
151 ;;; call it, leaving the original function object in LEXENV (in case
152 ;;; it was a closure). If it is actually a FIN, then we need to do an
153 ;;; extra indirection with funcallable-instance-lexenv to get at any
154 ;;; closure environment. This extra indirection is set up when
155 ;;; accessing the closure environment of an INSTANCE-LAMBDA. Note that
156 ;;; the original FIN pointer is lost, so if the called function wants
157 ;;; to get at the original object to do some slot accesses, it must
158 ;;; close over the FIN object.
160 ;;; If we set the FIN function to be a FIN, we directly copy across
161 ;;; both the code pointer and the lexenv, since that code pointer (for
162 ;;; an instance-lambda) is expecting that lexenv to be accessed. This
163 ;;; effectively pre-flattens what would otherwise be a chain of
164 ;;; indirections. (That used to happen when PCL dispatch functions
165 ;;; were byte-compiled; now that the byte compiler is gone, I can't
166 ;;; think of another example offhand. -- WHN 2001-10-06)
168 ;;; The only loss is that if someone accesses the
169 ;;; FUNCALLABLE-INSTANCE-FUN, then won't get a FIN back. This probably
170 ;;; doesn't matter, since PCL only sets the FIN function.
171 (defun (setf funcallable-instance-fun) (new-value fin)
172 (setf (%funcallable-instance-fun fin)
173 (%closure-fun new-value))
174 (setf (%funcallable-instance-lexenv fin)
175 (if (funcallable-instance-p new-value)
176 (%funcallable-instance-lexenv new-value)
179 ;;; service function for structure constructors
180 (defun %make-instance-with-layout (layout)
181 ;; Make sure the object ends at a two-word boundary. Note that this does
182 ;; not affect the amount of memory used, since the allocator would add the
183 ;; same padding anyway. However, raw slots are indexed from the length of
184 ;; the object as indicated in the header, so the pad word needs to be
185 ;; included in that length to guarantee proper alignment of raw double float
186 ;; slots, necessary for (at least) the SPARC backend.
187 (let* ((length (layout-length layout))
188 (result (%make-instance (+ length (mod (1+ length) 2)))))
189 (setf (%instance-layout result) layout)
192 ;;;; target-only parts of the DEFSTRUCT top level code
194 ;;; A list of hooks designating functions of one argument, the
195 ;;; classoid, to be called when a defstruct is evaluated.
196 (defvar *defstruct-hooks* nil)
198 ;;; Catch attempts to mess up definitions of symbols in the CL package.
199 (defun protect-cl (symbol)
200 (/show0 "entering PROTECT-CL, SYMBOL=..")
202 (when (and *cold-init-complete-p*
203 (eq (symbol-package symbol) *cl-package*))
204 (cerror "Go ahead and patch the system."
205 "attempting to modify a symbol in the COMMON-LISP package: ~S"
207 (/show0 "leaving PROTECT-CL")
210 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
212 ;;; (The "static" in the name is because it needs to be done not only
213 ;;; in ordinary toplevel %DEFSTRUCT, but also in cold init as early as
214 ;;; possible, to simulate static linking of structure functions as
215 ;;; nearly as possible.)
216 (defun %target-defstruct (dd layout)
217 (declare (type defstruct-description dd))
218 (declare (type layout layout))
220 (/show0 "entering %TARGET-DEFSTRUCT")
222 (remhash (dd-name dd) *typecheckfuns*)
224 ;; (Constructors aren't set up here, because constructors are
225 ;; varied enough (possibly parsing any specified argument list)
226 ;; that we can't reasonably implement them as closures, so we
227 ;; implement them with DEFUN instead.)
229 ;; Set FDEFINITIONs for slot accessors.
230 (dolist (dsd (dd-slots dd))
231 (/show0 "doing FDEFINITION for slot accessor")
232 (let ((accessor-name (dsd-accessor-name dsd)))
233 ;; We mustn't step on any inherited accessors
234 (unless (accessor-inherited-data accessor-name dd)
235 (/show0 "ACCESSOR-NAME=..")
236 (/hexstr accessor-name)
237 (protect-cl accessor-name)
238 (/hexstr "getting READER-FUN and WRITER-FUN")
239 (multiple-value-bind (reader-fun writer-fun)
240 (slot-accessor-funs dd dsd)
241 (declare (type function reader-fun writer-fun))
242 (/show0 "got READER-FUN and WRITER-FUN=..")
244 (setf (symbol-function accessor-name) reader-fun)
245 (unless (dsd-read-only dsd)
246 (/show0 "setting FDEFINITION for WRITER-FUN=..")
248 (setf (fdefinition `(setf ,accessor-name)) writer-fun))))))
250 ;; Set FDEFINITION for copier.
251 (when (dd-copier-name dd)
252 (/show0 "doing FDEFINITION for copier")
253 (protect-cl (dd-copier-name dd))
254 ;; We can't use COPY-STRUCTURE for other kinds of objects, notably
255 ;; funcallable structures, since it returns a STRUCTURE-OBJECT.
256 ;; (And funcallable instances don't need copiers anyway.)
257 (aver (eql (dd-type dd) 'structure))
258 (setf (symbol-function (dd-copier-name dd))
259 ;; FIXME: should use a closure which checks arg type before copying
262 ;; Set FDEFINITION for predicate.
263 (when (dd-predicate-name dd)
264 (/show0 "doing FDEFINITION for predicate")
265 (protect-cl (dd-predicate-name dd))
266 (setf (symbol-function (dd-predicate-name dd))
268 ;; structures with LAYOUTs
269 ((structure funcallable-structure)
270 (/show0 "with-LAYOUT case")
272 (locally ; <- to keep SAFETY 0 from affecting arg count checking
273 (declare (optimize (speed 3) (safety 0)))
274 (/noshow0 "in with-LAYOUT structure predicate closure, OBJECT,LAYOUT=..")
277 (typep-to-layout object layout))))
278 ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
280 ;; FIXME: should handle the :NAMED T case in these cases
282 (/show0 ":TYPE VECTOR case")
285 (/show0 ":TYPE LIST case")
289 (setf (fdocumentation (dd-name dd) 'structure)
292 ;; the BOUNDP test here is to get past cold-init.
293 (when (boundp '*defstruct-hooks*)
294 (dolist (fun *defstruct-hooks*)
295 (funcall fun (find-classoid (dd-name dd)))))
297 (/show0 "leaving %TARGET-DEFSTRUCT")
300 ;;;; generating out-of-line slot accessor functions
302 ;;; FIXME: Ideally, the presence of the type checks in the functions
303 ;;; here would be conditional on the optimization policy at the point
304 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
305 ;;; thing, putting in the type checks unconditionally.)
307 ;;; KLUDGE: Why use this closure approach at all? The macrology in
308 ;;; SLOT-ACCESSOR-FUNS seems to be half stub, half OAOOM to me. --DFL
310 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
311 (defun slot-accessor-funs (dd dsd)
313 #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
315 ;; various code generators
317 ;; Note: They're only minimally parameterized, and cavalierly grab
318 ;; things like INSTANCE and DSD-INDEX from the namespace they're
320 (macrolet (;; code shared between funcallable instance case and the
321 ;; ordinary STRUCTURE-OBJECT case: Handle native
322 ;; structures with LAYOUTs and (possibly) raw slots.
323 (%native-slot-accessor-funs (dd-ref-fun-name)
324 (let ((instance-type-check-form
325 '(%check-structure-type-from-layout instance layout)))
326 (/show "macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form)
327 `(let ((layout (dd-layout-or-lose dd))
328 (dsd-raw-type (dsd-raw-type dsd)))
329 #+sb-xc (/show0 "in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
330 ;; Map over all the possible RAW-TYPEs, compiling
331 ;; a different closure function for each one, so
332 ;; that once the COND over RAW-TYPEs happens (at
333 ;; the time closure is allocated) there are no
334 ;; more decisions to be made and things execute
335 ;; reasonably efficiently.
338 ((eql dsd-raw-type t)
339 #+sb-xc (/show0 "in nonraw slot case")
340 (%slotplace-accessor-funs
341 (,dd-ref-fun-name instance dsd-index)
342 ,instance-type-check-form))
344 ,@(mapcar (lambda (rtd)
345 (let ((raw-type (raw-slot-data-raw-type rtd))
347 (raw-slot-data-accessor-name rtd)))
348 `((equal dsd-raw-type ',raw-type)
349 #+sb-xc (/show0 "in raw slot case")
350 (%slotplace-accessor-funs
351 (,accessor-name instance dsd-index)
352 ,instance-type-check-form))))
353 *raw-slot-data-list*)
356 (bug "unexpected DSD-RAW-TYPE ~S" dsd-raw-type))))))
357 ;; code shared between DEFSTRUCT :TYPE LIST and
358 ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
359 ;; structure" case, with no LAYOUTs and no raw slots.
360 (%colontyped-slot-accessor-funs () (error "stub"))
361 ;; the common structure of the raw-slot and not-raw-slot
362 ;; cases, defined in terms of the writable SLOTPLACE. All
363 ;; possible flavors of slot access should be able to pass
365 (%slotplace-accessor-funs (slotplace instance-type-check-form)
366 (/show "macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form)
367 `(let ((typecheckfun (typespec-typecheckfun dsd-type)))
368 (values (if (dsd-safe-p dsd)
370 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
371 ,instance-type-check-form
372 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
375 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
376 ,instance-type-check-form
377 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
378 (let ((value ,slotplace))
379 (funcall typecheckfun value)
381 (lambda (new-value instance)
382 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
383 ,instance-type-check-form
384 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
385 (funcall typecheckfun new-value)
386 (/noshow0 "back from TYPECHECKFUN")
387 (setf ,slotplace new-value))))))
389 (let ((dsd-index (dsd-index dsd))
390 (dsd-type (dsd-type dsd)))
392 #+sb-xc (/show0 "got DSD-TYPE=..")
393 #+sb-xc (/hexstr dsd-type)
398 #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
399 (%native-slot-accessor-funs %instance-ref))
401 ;; structures with the :TYPE option
403 ;; FIXME: Worry about these later..
405 ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
406 ;; layout completely, so that raw slots are impossible.
408 (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
409 `(%check-structure-type-from-dd
412 (dd-type-slot-accessor-funs aref
417 ;;; Copy any old kind of structure.
418 (defun copy-structure (structure)
420 "Return a copy of STRUCTURE with the same (EQL) slot values."
421 (declare (type structure-object structure))
422 (let* ((len (%instance-length structure))
423 (res (%make-instance len))
424 (layout (%instance-layout structure))
425 (nuntagged (layout-n-untagged-slots layout)))
427 (declare (type index len))
428 (when (layout-invalid layout)
429 (error "attempt to copy an obsolete structure:~% ~S" structure))
431 ;; Copy ordinary slots.
432 (dotimes (i (- len nuntagged))
433 (declare (type index i))
434 (setf (%instance-ref res i)
435 (%instance-ref structure i)))
438 (dotimes (i nuntagged)
439 (declare (type index i))
440 (setf (%raw-instance-ref/word res i)
441 (%raw-instance-ref/word structure i)))
445 ;;; default PRINT-OBJECT method
447 (defun %default-structure-pretty-print (structure stream)
448 (let* ((layout (%instance-layout structure))
449 (name (classoid-name (layout-classoid layout)))
450 (dd (layout-info layout)))
451 ;; KLUDGE: during the build process with SB-SHOW, we can sometimes
452 ;; attempt to print out a PCL object (with null LAYOUT-INFO).
455 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
457 (write-char #\space stream)
458 (write-string "(no LAYOUT-INFO)"))
459 (return-from %default-structure-pretty-print nil))
460 ;; the structure type doesn't count as a component for
461 ;; *PRINT-LEVEL* processing. We can likewise elide the logical
462 ;; block processing, since all we have to print is the type name.
463 ;; -- CSR, 2004-10-05
464 (when (and dd (null (dd-slots dd)))
465 (write-string "#S(" stream)
467 (write-char #\) stream)
468 (return-from %default-structure-pretty-print nil))
469 (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
471 (let ((remaining-slots (dd-slots dd)))
472 (when remaining-slots
473 (write-char #\space stream)
474 ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
475 ;; but I can't see why. -- WHN 20000205
476 (pprint-newline :linear stream)
479 (let ((slot (pop remaining-slots)))
480 (write-char #\: stream)
481 (output-symbol-name (symbol-name (dsd-name slot)) stream)
482 (write-char #\space stream)
483 (pprint-newline :miser stream)
484 (output-object (funcall (fdefinition (dsd-accessor-name slot))
487 (when (null remaining-slots)
489 (write-char #\space stream)
490 (pprint-newline :linear stream))))))))
491 (defun %default-structure-ugly-print (structure stream)
492 (let* ((layout (%instance-layout structure))
493 (name (classoid-name (layout-classoid layout)))
494 (dd (layout-info layout)))
495 (when (and dd (null (dd-slots dd)))
496 (write-string "#S(" stream)
498 (write-char #\) stream)
499 (return-from %default-structure-ugly-print nil))
500 (descend-into (stream)
501 (write-string "#S(" stream)
503 (do ((index 0 (1+ index))
504 (remaining-slots (dd-slots dd) (cdr remaining-slots)))
505 ((or (null remaining-slots)
506 (and (not *print-readably*)
508 (>= index *print-length*)))
509 (if (null remaining-slots)
510 (write-string ")" stream)
511 (write-string " ...)" stream)))
512 (declare (type index index))
513 (write-char #\space stream)
514 (write-char #\: stream)
515 (let ((slot (first remaining-slots)))
516 (output-symbol-name (symbol-name (dsd-name slot)) stream)
517 (write-char #\space stream)
519 (funcall (fdefinition (dsd-accessor-name slot))
522 (defun default-structure-print (structure stream depth)
523 (declare (ignore depth))
524 (cond ((funcallable-instance-p structure)
525 (print-unreadable-object (structure stream :identity t :type t)))
527 (%default-structure-pretty-print structure stream))
529 (%default-structure-ugly-print structure stream))))
530 (def!method print-object ((x structure-object) stream)
531 (default-structure-print x stream *current-level-in-print*))
533 ;;;; testing structure types
535 ;;; Return true if OBJ is an object of the structure type
536 ;;; corresponding to LAYOUT. This is called by the accessor closures,
537 ;;; which have a handle on the type's LAYOUT.
539 ;;; FIXME: This is fairly big, so it should probably become
540 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
541 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
542 ;;; else we could fix things up so that the things which call it are
543 ;;; all closures, so that it's expanded only in a small number of
545 #!-sb-fluid (declaim (inline typep-to-layout))
546 (defun typep-to-layout (obj layout)
547 (declare (type layout layout) (optimize (speed 3) (safety 0)))
548 (/noshow0 "entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
551 (when (layout-invalid layout)
552 (error "An obsolete structure accessor function was called."))
553 (/noshow0 "back from testing LAYOUT-INVALID LAYOUT")
554 ;; FIXME: CMU CL used (%INSTANCEP OBJ) here. Check that
555 ;; (TYPEP OBJ 'INSTANCE) is optimized to equally efficient code.
556 (and (typep obj 'instance)
557 (let ((obj-layout (%instance-layout obj)))
558 (cond ((eq obj-layout layout)
559 ;; (In this case OBJ-LAYOUT can't be invalid, because
560 ;; we determined LAYOUT is valid in the test above.)
563 ((layout-invalid obj-layout)
564 (/noshow0 "LAYOUT-INVALID case")
565 (error 'layout-invalid
566 :expected-type (layout-classoid obj-layout)
569 (let ((depthoid (layout-depthoid layout)))
570 (/noshow0 "DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
572 (/nohexstr layout-inherits)
573 (and (> (layout-depthoid obj-layout) depthoid)
574 (eq (svref (layout-inherits obj-layout) depthoid)
577 ;;;; checking structure types
579 ;;; Check that X is an instance of the named structure type.
580 (defmacro %check-structure-type-from-name (x name)
581 `(%check-structure-type-from-layout ,x ,(compiler-layout-or-lose name)))
583 ;;; Check that X is a structure of the type described by DD.
584 (defmacro %check-structure-type-from-dd (x dd)
585 (declare (type defstruct-description dd))
586 (let ((class-name (dd-name dd)))
588 ((structure funcallable-instance)
589 `(%check-structure-type-from-layout
591 ,(compiler-layout-or-lose class-name)))
593 (with-unique-names (xx)
595 (declare (type vector ,xx))
596 ,@(when (dd-named dd)
597 `((unless (eql (aref ,xx 0) ',class-name)
601 :expected-type `(member ,class-name)
603 "~@<missing name in instance of ~
604 VECTOR-typed structure ~S: ~2I~_S~:>"
605 :format-arguments (list ',class-name ,xx)))))
608 (with-unique-names (xx)
610 (declare (type list ,xx))
611 ,@(when (dd-named dd)
612 `((unless (eql (first ,xx) ',class-name)
616 :expected-type `(member ,class-name)
618 "~@<missing name in instance of LIST-typed structure ~S: ~
620 :format-arguments (list ',class-name ,xx)))))
623 ;;; Check that X is an instance of the structure class with layout LAYOUT.
624 (defun %check-structure-type-from-layout (x layout)
625 (unless (typep-to-layout x layout)
628 :expected-type (classoid-name (layout-classoid layout))))
631 (/show0 "target-defstruct.lisp end of file")