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)
135 (%make-funcallable-instance len))
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"). When
149 ;;; called (as with any other function), we grab the code pointer, and
150 ;;; call it, leaving the original function object in LEXENV (in case
151 ;;; it was a closure). If it is actually a FIN, then we need to do an
152 ;;; extra indirection with funcallable-instance-lexenv to get at any
153 ;;; closure environment. This extra indirection is set up when
154 ;;; accessing the closure environment of an INSTANCE-LAMBDA. Note that
155 ;;; the original FIN pointer is lost, so if the called function wants
156 ;;; to get at the original object to do some slot accesses, it must
157 ;;; close over the FIN object.
159 ;;; If we set the FIN function to be a FIN, we directly copy across
160 ;;; both the code pointer and the lexenv, since that code pointer (for
161 ;;; an instance-lambda) is expecting that lexenv to be accessed. This
162 ;;; effectively pre-flattens what would otherwise be a chain of
163 ;;; indirections. (That used to happen when PCL dispatch functions
164 ;;; were byte-compiled; now that the byte compiler is gone, I can't
165 ;;; think of another example offhand. -- WHN 2001-10-06)
167 ;;; The only loss is that if someone accesses the
168 ;;; FUNCALLABLE-INSTANCE-FUN, then won't get a FIN back. This
169 ;;; probably doesn't matter, since PCL only sets the FIN function.
170 (defun (setf funcallable-instance-fun) (new-value fin)
171 (setf (%funcallable-instance-fun fin)
172 (%closure-fun new-value))
173 (setf (%funcallable-instance-lexenv fin)
174 (if (funcallable-instance-p new-value)
175 (%funcallable-instance-lexenv new-value)
178 ;;; service function for structure constructors
179 (defun %make-instance-with-layout (layout)
180 ;; Make sure the object ends at a two-word boundary. Note that this does
181 ;; not affect the amount of memory used, since the allocator would add the
182 ;; same padding anyway. However, raw slots are indexed from the length of
183 ;; the object as indicated in the header, so the pad word needs to be
184 ;; included in that length to guarantee proper alignment of raw double float
185 ;; slots, necessary for (at least) the SPARC backend.
186 (let* ((length (layout-length layout))
187 (result (%make-instance (+ length (mod (1+ length) 2)))))
188 (setf (%instance-layout result) layout)
191 ;;;; target-only parts of the DEFSTRUCT top level code
193 ;;; A list of hooks designating functions of one argument, the
194 ;;; classoid, to be called when a defstruct is evaluated.
195 (defvar *defstruct-hooks* nil)
197 ;;; Catch attempts to mess up definitions of symbols in the CL package.
198 (defun protect-cl (symbol)
199 (/show0 "entering PROTECT-CL, SYMBOL=..")
201 (when (and *cold-init-complete-p*
202 (eq (symbol-package symbol) *cl-package*))
203 (cerror "Go ahead and patch the system."
204 "attempting to modify a symbol in the COMMON-LISP package: ~S"
206 (/show0 "leaving PROTECT-CL")
209 ;;; the part of %DEFSTRUCT which makes sense only on the target SBCL
211 ;;; (The "static" in the name is because it needs to be done not only
212 ;;; in ordinary toplevel %DEFSTRUCT, but also in cold init as early as
213 ;;; possible, to simulate static linking of structure functions as
214 ;;; nearly as possible.)
215 (defun %target-defstruct (dd layout)
216 (declare (type defstruct-description dd))
217 (declare (type layout layout))
219 (/show0 "entering %TARGET-DEFSTRUCT")
221 (remhash (dd-name dd) *typecheckfuns*)
223 ;; (Constructors aren't set up here, because constructors are
224 ;; varied enough (possibly parsing any specified argument list)
225 ;; that we can't reasonably implement them as closures, so we
226 ;; implement them with DEFUN instead.)
228 ;; Set FDEFINITIONs for slot accessors.
229 (dolist (dsd (dd-slots dd))
230 (/show0 "doing FDEFINITION for slot accessor")
231 (let ((accessor-name (dsd-accessor-name dsd)))
232 ;; We mustn't step on any inherited accessors
233 (unless (accessor-inherited-data accessor-name dd)
234 (/show0 "ACCESSOR-NAME=..")
235 (/hexstr accessor-name)
236 (protect-cl accessor-name)
237 (/hexstr "getting READER-FUN and WRITER-FUN")
238 (multiple-value-bind (reader-fun writer-fun)
239 (slot-accessor-funs dd dsd)
240 (declare (type function reader-fun writer-fun))
241 (/show0 "got READER-FUN and WRITER-FUN=..")
243 (setf (symbol-function accessor-name) reader-fun)
244 (unless (dsd-read-only dsd)
245 (/show0 "setting FDEFINITION for WRITER-FUN=..")
247 (setf (fdefinition `(setf ,accessor-name)) writer-fun))))))
249 ;; Set FDEFINITION for copier.
250 (when (dd-copier-name dd)
251 (/show0 "doing FDEFINITION for copier")
252 (protect-cl (dd-copier-name dd))
253 ;; We can't use COPY-STRUCTURE for other kinds of objects, notably
254 ;; funcallable structures, since it returns a STRUCTURE-OBJECT.
255 ;; (And funcallable instances don't need copiers anyway.)
256 (aver (eql (dd-type dd) 'structure))
257 (setf (symbol-function (dd-copier-name dd))
258 ;; FIXME: should use a closure which checks arg type before copying
261 ;; Set FDEFINITION for predicate.
262 (when (dd-predicate-name dd)
263 (/show0 "doing FDEFINITION for predicate")
264 (protect-cl (dd-predicate-name dd))
265 (setf (symbol-function (dd-predicate-name dd))
267 ;; structures with LAYOUTs
268 ((structure funcallable-structure)
269 (/show0 "with-LAYOUT case")
271 (locally ; <- to keep SAFETY 0 from affecting arg count checking
272 (declare (optimize (speed 3) (safety 0)))
273 (/noshow0 "in with-LAYOUT structure predicate closure, OBJECT,LAYOUT=..")
276 (typep-to-layout object layout))))
277 ;; structures with no LAYOUT (i.e. :TYPE VECTOR or :TYPE LIST)
279 ;; FIXME: should handle the :NAMED T case in these cases
281 (/show0 ":TYPE VECTOR case")
284 (/show0 ":TYPE LIST case")
288 (setf (fdocumentation (dd-name dd) 'structure)
291 ;; the BOUNDP test here is to get past cold-init.
292 (when (boundp '*defstruct-hooks*)
293 (dolist (fun *defstruct-hooks*)
294 (funcall fun (find-classoid (dd-name dd)))))
296 (/show0 "leaving %TARGET-DEFSTRUCT")
299 ;;;; generating out-of-line slot accessor functions
301 ;;; FIXME: Ideally, the presence of the type checks in the functions
302 ;;; here would be conditional on the optimization policy at the point
303 ;;; of expansion of DEFSTRUCT. (For now we're just doing the simpler
304 ;;; thing, putting in the type checks unconditionally.)
306 ;;; KLUDGE: Why use this closure approach at all? The macrology in
307 ;;; SLOT-ACCESSOR-FUNS seems to be half stub, half OAOOM to me. --DFL
309 ;;; Return (VALUES SLOT-READER-FUN SLOT-WRITER-FUN).
310 (defun slot-accessor-funs (dd dsd)
312 #+sb-xc (/show0 "entering SLOT-ACCESSOR-FUNS")
314 ;; various code generators
316 ;; Note: They're only minimally parameterized, and cavalierly grab
317 ;; things like INSTANCE and DSD-INDEX from the namespace they're
319 (macrolet (;; code shared between funcallable instance case and the
320 ;; ordinary STRUCTURE-OBJECT case: Handle native
321 ;; structures with LAYOUTs and (possibly) raw slots.
322 (%native-slot-accessor-funs (dd-ref-fun-name)
323 (let ((instance-type-check-form
324 '(%check-structure-type-from-layout instance layout)))
325 (/show "macroexpanding %NATIVE-SLOT-ACCESSOR-FUNS" dd-ref-fun-name instance-type-check-form)
326 `(let ((layout (dd-layout-or-lose dd))
327 (dsd-raw-type (dsd-raw-type dsd)))
328 #+sb-xc (/show0 "in %NATIVE-SLOT-ACCESSOR-FUNS macroexpanded code")
329 ;; Map over all the possible RAW-TYPEs, compiling
330 ;; a different closure function for each one, so
331 ;; that once the COND over RAW-TYPEs happens (at
332 ;; the time closure is allocated) there are no
333 ;; more decisions to be made and things execute
334 ;; reasonably efficiently.
337 ((eql dsd-raw-type t)
338 #+sb-xc (/show0 "in nonraw slot case")
339 (%slotplace-accessor-funs
340 (,dd-ref-fun-name instance dsd-index)
341 ,instance-type-check-form))
343 ,@(mapcar (lambda (rtd)
344 (let ((raw-type (raw-slot-data-raw-type rtd))
346 (raw-slot-data-accessor-name rtd)))
347 `((equal dsd-raw-type ',raw-type)
348 #+sb-xc (/show0 "in raw slot case")
349 (%slotplace-accessor-funs
350 (,accessor-name instance dsd-index)
351 ,instance-type-check-form))))
352 *raw-slot-data-list*)
355 (bug "unexpected DSD-RAW-TYPE ~S" dsd-raw-type))))))
356 ;; code shared between DEFSTRUCT :TYPE LIST and
357 ;; DEFSTRUCT :TYPE VECTOR cases: Handle the "typed
358 ;; structure" case, with no LAYOUTs and no raw slots.
359 (%colontyped-slot-accessor-funs () (error "stub"))
360 ;; the common structure of the raw-slot and not-raw-slot
361 ;; cases, defined in terms of the writable SLOTPLACE. All
362 ;; possible flavors of slot access should be able to pass
364 (%slotplace-accessor-funs (slotplace instance-type-check-form)
365 (/show "macroexpanding %SLOTPLACE-ACCESSOR-FUNS" slotplace instance-type-check-form)
366 `(let ((typecheckfun (typespec-typecheckfun dsd-type)))
367 (values (if (dsd-safe-p dsd)
369 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
370 ,instance-type-check-form
371 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
374 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined reader")
375 ,instance-type-check-form
376 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
377 (let ((value ,slotplace))
378 (funcall typecheckfun value)
380 (lambda (new-value instance)
381 (/noshow0 "in %SLOTPLACE-ACCESSOR-FUNS-defined writer")
382 ,instance-type-check-form
383 (/noshow0 "back from INSTANCE-TYPE-CHECK-FORM")
384 (funcall typecheckfun new-value)
385 (/noshow0 "back from TYPECHECKFUN")
386 (setf ,slotplace new-value))))))
388 (let ((dsd-index (dsd-index dsd))
389 (dsd-type (dsd-type dsd)))
391 #+sb-xc (/show0 "got DSD-TYPE=..")
392 #+sb-xc (/hexstr dsd-type)
397 #+sb-xc (/show0 "case of DSD-TYPE = STRUCTURE")
398 (%native-slot-accessor-funs %instance-ref))
400 ;; structures with the :TYPE option
402 ;; FIXME: Worry about these later..
404 ;; In :TYPE LIST and :TYPE VECTOR structures, ANSI specifies the
405 ;; layout completely, so that raw slots are impossible.
407 (dd-type-slot-accessor-funs nth-but-with-sane-arg-order
408 `(%check-structure-type-from-dd
411 (dd-type-slot-accessor-funs aref
416 ;;; Copy any old kind of structure.
417 (defun copy-structure (structure)
419 "Return a copy of STRUCTURE with the same (EQL) slot values."
420 (declare (type structure-object structure))
421 (let* ((len (%instance-length structure))
422 (res (%make-instance len))
423 (layout (%instance-layout structure))
424 (nuntagged (layout-n-untagged-slots layout)))
426 (declare (type index len))
427 (when (layout-invalid layout)
428 (error "attempt to copy an obsolete structure:~% ~S" structure))
430 ;; Copy ordinary slots.
431 (dotimes (i (- len nuntagged))
432 (declare (type index i))
433 (setf (%instance-ref res i)
434 (%instance-ref structure i)))
437 (dotimes (i nuntagged)
438 (declare (type index i))
439 (setf (%raw-instance-ref/word res i)
440 (%raw-instance-ref/word structure i)))
444 ;;; default PRINT-OBJECT method
446 (defun %default-structure-pretty-print (structure stream)
447 (let* ((layout (%instance-layout structure))
448 (name (classoid-name (layout-classoid layout)))
449 (dd (layout-info layout)))
450 ;; KLUDGE: during the build process with SB-SHOW, we can sometimes
451 ;; attempt to print out a PCL object (with null LAYOUT-INFO).
454 (pprint-logical-block (stream nil :prefix "#<" :suffix ">")
456 (write-char #\space stream)
457 (write-string "(no LAYOUT-INFO)"))
458 (return-from %default-structure-pretty-print nil))
459 ;; the structure type doesn't count as a component for
460 ;; *PRINT-LEVEL* processing. We can likewise elide the logical
461 ;; block processing, since all we have to print is the type name.
462 ;; -- CSR, 2004-10-05
463 (when (and dd (null (dd-slots dd)))
464 (write-string "#S(" stream)
466 (write-char #\) stream)
467 (return-from %default-structure-pretty-print nil))
468 (pprint-logical-block (stream nil :prefix "#S(" :suffix ")")
470 (let ((remaining-slots (dd-slots dd)))
471 (when remaining-slots
472 (write-char #\space stream)
473 ;; CMU CL had (PPRINT-INDENT :BLOCK 2 STREAM) here,
474 ;; but I can't see why. -- WHN 20000205
475 (pprint-newline :linear stream)
478 (let ((slot (pop remaining-slots)))
479 (write-char #\: stream)
480 (output-symbol-name (symbol-name (dsd-name slot)) stream)
481 (write-char #\space stream)
482 (pprint-newline :miser stream)
483 (output-object (funcall (fdefinition (dsd-accessor-name slot))
486 (when (null remaining-slots)
488 (write-char #\space stream)
489 (pprint-newline :linear stream))))))))
490 (defun %default-structure-ugly-print (structure stream)
491 (let* ((layout (%instance-layout structure))
492 (name (classoid-name (layout-classoid layout)))
493 (dd (layout-info layout)))
494 (when (and dd (null (dd-slots dd)))
495 (write-string "#S(" stream)
497 (write-char #\) stream)
498 (return-from %default-structure-ugly-print nil))
499 (descend-into (stream)
500 (write-string "#S(" stream)
502 (do ((index 0 (1+ index))
503 (remaining-slots (dd-slots dd) (cdr remaining-slots)))
504 ((or (null remaining-slots)
505 (and (not *print-readably*)
507 (>= index *print-length*)))
508 (if (null remaining-slots)
509 (write-string ")" stream)
510 (write-string " ...)" stream)))
511 (declare (type index index))
512 (write-char #\space stream)
513 (write-char #\: stream)
514 (let ((slot (first remaining-slots)))
515 (output-symbol-name (symbol-name (dsd-name slot)) stream)
516 (write-char #\space stream)
518 (funcall (fdefinition (dsd-accessor-name slot))
521 (defun default-structure-print (structure stream depth)
522 (declare (ignore depth))
523 (cond ((funcallable-instance-p structure)
524 (print-unreadable-object (structure stream :identity t :type t)))
526 (%default-structure-pretty-print structure stream))
528 (%default-structure-ugly-print structure stream))))
529 (def!method print-object ((x structure-object) stream)
530 (default-structure-print x stream *current-level-in-print*))
532 ;;;; testing structure types
534 ;;; Return true if OBJ is an object of the structure type
535 ;;; corresponding to LAYOUT. This is called by the accessor closures,
536 ;;; which have a handle on the type's LAYOUT.
538 ;;; FIXME: This is fairly big, so it should probably become
539 ;;; MAYBE-INLINE instead of INLINE, or its inlineness should become
540 ;;; conditional (probably through DEFTRANSFORM) on (> SPEED SPACE). Or
541 ;;; else we could fix things up so that the things which call it are
542 ;;; all closures, so that it's expanded only in a small number of
544 #!-sb-fluid (declaim (inline typep-to-layout))
545 (defun typep-to-layout (obj layout)
546 (declare (type layout layout) (optimize (speed 3) (safety 0)))
547 (/noshow0 "entering TYPEP-TO-LAYOUT, OBJ,LAYOUT=..")
550 (when (layout-invalid layout)
551 (error "An obsolete structure accessor function was called."))
552 (/noshow0 "back from testing LAYOUT-INVALID LAYOUT")
553 (and (%instancep obj)
554 (let ((obj-layout (%instance-layout obj)))
555 (cond ((eq obj-layout layout)
556 ;; (In this case OBJ-LAYOUT can't be invalid, because
557 ;; we determined LAYOUT is valid in the test above.)
560 ((layout-invalid obj-layout)
561 (/noshow0 "LAYOUT-INVALID case")
562 (error 'layout-invalid
563 :expected-type (layout-classoid obj-layout)
566 (let ((depthoid (layout-depthoid layout)))
567 (/noshow0 "DEPTHOID case, DEPTHOID,LAYOUT-INHERITS=..")
569 (/nohexstr layout-inherits)
570 (and (> (layout-depthoid obj-layout) depthoid)
571 (eq (svref (layout-inherits obj-layout) depthoid)
574 ;;;; checking structure types
576 ;;; Check that X is an instance of the named structure type.
577 (defmacro %check-structure-type-from-name (x name)
578 `(%check-structure-type-from-layout ,x ,(compiler-layout-or-lose name)))
580 ;;; Check that X is a structure of the type described by DD.
581 (defmacro %check-structure-type-from-dd (x dd)
582 (declare (type defstruct-description dd))
583 (let ((class-name (dd-name dd)))
585 ((structure funcallable-instance)
586 `(%check-structure-type-from-layout
588 ,(compiler-layout-or-lose class-name)))
590 (with-unique-names (xx)
592 (declare (type vector ,xx))
593 ,@(when (dd-named dd)
594 `((unless (eql (aref ,xx 0) ',class-name)
598 :expected-type `(member ,class-name)
600 "~@<missing name in instance of ~
601 VECTOR-typed structure ~S: ~2I~_S~:>"
602 :format-arguments (list ',class-name ,xx)))))
605 (with-unique-names (xx)
607 (declare (type list ,xx))
608 ,@(when (dd-named dd)
609 `((unless (eql (first ,xx) ',class-name)
613 :expected-type `(member ,class-name)
615 "~@<missing name in instance of LIST-typed structure ~S: ~
617 :format-arguments (list ',class-name ,xx)))))
620 ;;; Check that X is an instance of the structure class with layout LAYOUT.
621 (defun %check-structure-type-from-layout (x layout)
622 (unless (typep-to-layout x layout)
625 :expected-type (classoid-name (layout-classoid layout))))
628 (/show0 "target-defstruct.lisp end of file")