Eliminate *!LATE-PRIMITIVE-OBJECT-FORMS* from the target image.
[sbcl.git] / src / compiler / generic / objdef.lisp
blob28f8a96270129877ccfb39914cf28a73e4a3315e
1 ;;;; machine-independent aspects of the object representation
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
12 (in-package "SB!VM")
14 ;;;; KLUDGE: The primitive objects here may look like self-contained
15 ;;;; definitions, but in general they're not. In particular, if you
16 ;;;; try to add a slot to them, beware of the following:
17 ;;;; * The GC scavenging code (and for all I know other GC code too)
18 ;;;; is not automatically generated from these layouts, but instead
19 ;;;; was hand-written to correspond to them. The offsets are
20 ;;;; automatically propagated into the GC scavenging code, but the
21 ;;;; existence of slots, and whether they should be scavenged, is
22 ;;;; not automatically propagated. Thus e.g. if you add a
23 ;;;; SIMPLE-FUN-DEBUG-INFO slot holding a tagged object which needs
24 ;;;; to be GCed, you need to tweak scav_code_header() and
25 ;;;; verify_space() in gencgc.c, and the corresponding code in gc.c.
26 ;;;; * The src/runtime/print.c code (used by LDB) is implemented
27 ;;;; using hand-written lists of slot names, which aren't automatically
28 ;;;; generated from the code in this file.
29 ;;;; * Various code (e.g. STATIC-FSET in genesis.lisp) is hard-wired
30 ;;;; to know the name of the last slot of the object the code works
31 ;;;; with, and implicitly to know that the last slot is special (being
32 ;;;; the beginning of an arbitrary-length sequence of bytes following
33 ;;;; the fixed-layout slots).
34 ;;;; -- WHN 2001-12-29
36 ;;;; the primitive objects themselves
38 (!define-primitive-object (cons :type cons
39 :lowtag list-pointer-lowtag
40 :alloc-trans cons)
41 (car :ref-trans car :set-trans sb!c::%rplaca :init :arg
42 :cas-trans %compare-and-swap-car)
43 (cdr :ref-trans cdr :set-trans sb!c::%rplacd :init :arg
44 :cas-trans %compare-and-swap-cdr))
46 (!define-primitive-object (instance :lowtag instance-pointer-lowtag
47 :widetag instance-header-widetag
48 :alloc-trans %make-instance)
49 (slots :rest-p t))
51 (!define-primitive-object (bignum :lowtag other-pointer-lowtag
52 :widetag bignum-widetag
53 :alloc-trans sb!bignum::%allocate-bignum)
54 (digits :rest-p t :c-type #!-alpha "sword_t" #!+alpha "u32"))
56 (!define-primitive-object (ratio :type ratio
57 :lowtag other-pointer-lowtag
58 :widetag ratio-widetag
59 :alloc-trans %make-ratio)
60 (numerator :type integer
61 :ref-known (flushable movable)
62 :ref-trans %numerator
63 :init :arg)
64 (denominator :type integer
65 :ref-known (flushable movable)
66 :ref-trans %denominator
67 :init :arg))
69 #!+#.(cl:if (cl:= sb!vm:n-word-bits 32) '(and) '(or))
70 (!define-primitive-object (single-float :lowtag other-pointer-lowtag
71 :widetag single-float-widetag)
72 (value :c-type "float"))
74 (!define-primitive-object (double-float :lowtag other-pointer-lowtag
75 :widetag double-float-widetag)
76 #!-x86-64 (filler)
77 (value :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
79 #!+long-float
80 (!define-primitive-object (long-float :lowtag other-pointer-lowtag
81 :widetag long-float-widetag)
82 #!+sparc (filler)
83 (value :c-type "long double" :length #!+x86 3 #!+sparc 4))
85 (!define-primitive-object (complex :type complex
86 :lowtag other-pointer-lowtag
87 :widetag complex-widetag
88 :alloc-trans %make-complex)
89 (real :type real
90 :ref-known (flushable movable)
91 :ref-trans %realpart
92 :init :arg)
93 (imag :type real
94 :ref-known (flushable movable)
95 :ref-trans %imagpart
96 :init :arg))
98 (!define-primitive-object (array :lowtag other-pointer-lowtag
99 :widetag t)
100 ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
101 ;; VECTOR -- see SHRINK-VECTOR.
102 (fill-pointer :type index
103 :ref-trans %array-fill-pointer
104 :ref-known (flushable foldable)
105 :set-trans (setf %array-fill-pointer)
106 :set-known ())
107 (fill-pointer-p :type (member t nil)
108 :ref-trans %array-fill-pointer-p
109 :ref-known (flushable foldable)
110 :set-trans (setf %array-fill-pointer-p)
111 :set-known ())
112 (elements :type index
113 :ref-trans %array-available-elements
114 :ref-known (flushable foldable)
115 :set-trans (setf %array-available-elements)
116 :set-known ())
117 (data :type array
118 ;; FIXME: terrible name for the accessor.
119 ;; It is in general just an ARRAY,
120 ;; and should be named %ARRAY-DATA.
121 :ref-trans %array-data-vector
122 :ref-known (flushable foldable)
123 :set-trans (setf %array-data-vector)
124 :set-known ())
125 (displacement :type (or index null)
126 :ref-trans %array-displacement
127 :ref-known (flushable foldable)
128 :set-trans (setf %array-displacement)
129 :set-known ())
130 (displaced-p :type t
131 :ref-trans %array-displaced-p
132 :ref-known (flushable foldable)
133 :set-trans (setf %array-displaced-p)
134 :set-known ())
135 (displaced-from :type list
136 :ref-trans %array-displaced-from
137 :ref-known (flushable)
138 :set-trans (setf %array-displaced-from)
139 :set-known ())
140 (dimensions :rest-p t))
142 (!define-primitive-object (vector :type vector
143 :lowtag other-pointer-lowtag
144 :widetag t)
145 ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
146 ;; VECTOR -- see SHRINK-VECTOR.
147 (length :ref-trans sb!c::vector-length
148 :type index)
149 (data :rest-p t :c-type #!-alpha "uword_t" #!+alpha "u32"))
151 ;;; The header contains the size of slots and constants in words.
152 (!define-primitive-object (code :type code-component
153 :lowtag other-pointer-lowtag
154 :widetag t)
155 ;; This is the size of instructions in bytes, not aligned.
156 ;; Adding the size from the header and aligned code-size will yield
157 ;; the total size of the code-object.
158 (code-size :type index
159 :ref-known (flushable movable)
160 :ref-trans %code-code-size)
161 (entry-points :type (or function null)
162 :ref-known (flushable)
163 :ref-trans %code-entry-points
164 :set-known ()
165 :set-trans (setf %code-entry-points))
166 (debug-info :type t
167 :ref-known (flushable)
168 :ref-trans %code-debug-info
169 :set-known ()
170 :set-trans (setf %code-debug-info))
171 (constants :rest-p t))
173 (!define-primitive-object (fdefn :type fdefn
174 :lowtag other-pointer-lowtag
175 :widetag fdefn-widetag)
176 (name :ref-trans fdefn-name)
177 (fun :type (or function null) :ref-trans fdefn-fun)
178 (raw-addr :c-type #!-alpha "char *" #!+alpha "u32"))
180 ;;; a simple function (as opposed to hairier things like closures
181 ;;; which are also subtypes of Common Lisp's FUNCTION type)
182 (!define-primitive-object (simple-fun :type function
183 :lowtag fun-pointer-lowtag
184 :widetag simple-fun-header-widetag)
185 #!-(or x86 x86-64) (self :ref-trans %simple-fun-self
186 :set-trans (setf %simple-fun-self))
187 ;; FIXME: we don't currently detect/prevent at compile-time the bad
188 ;; scenario this comment claims to disallow, as determined by re-enabling
189 ;; these SET- and REF- specifiers, which led to a cold-init crash.
190 #!+(or x86 x86-64) (self
191 ;; KLUDGE: There's no :SET-KNOWN, :SET-TRANS, :REF-KNOWN, or
192 ;; :REF-TRANS here in this case. Instead, there's separate
193 ;; DEFKNOWN/DEFINE-VOP/DEFTRANSFORM stuff in
194 ;; compiler/x86/system.lisp to define and declare them by
195 ;; hand. I don't know why this is, but that's (basically)
196 ;; the way it was done in CMU CL, and it works. (It's not
197 ;; exactly the same way it was done in CMU CL in that CMU
198 ;; CL's allows duplicate DEFKNOWNs, blithely overwriting any
199 ;; previous data associated with the previous DEFKNOWN, and
200 ;; that property was used to mask the definitions here. In
201 ;; SBCL as of 0.6.12.64 that's not allowed -- too confusing!
202 ;; -- so we have to explicitly suppress the DEFKNOWNish
203 ;; stuff here in order to allow this old hack to work in the
204 ;; new world. -- WHN 2001-08-82
206 (next :type (or function null)
207 :ref-known (flushable)
208 :ref-trans %simple-fun-next
209 :set-known ()
210 :set-trans (setf %simple-fun-next))
211 (name :ref-known (flushable)
212 :ref-trans %simple-fun-name
213 :set-known ()
214 :set-trans (setf %simple-fun-name))
215 (arglist :type list
216 :ref-known (flushable)
217 :ref-trans %simple-fun-arglist
218 :set-known ()
219 :set-trans (setf %simple-fun-arglist))
220 (type :ref-known (flushable)
221 :ref-trans %simple-fun-type
222 :set-known ()
223 :set-trans (setf %simple-fun-type))
224 ;; NIL for empty, STRING for a docstring, SIMPLE-VECTOR for XREFS, and (CONS
225 ;; STRING SIMPLE-VECTOR) for both.
226 (info :init :null
227 :ref-trans %simple-fun-info
228 :ref-known (flushable)
229 :set-trans (setf %simple-fun-info)
230 :set-known ())
231 ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
232 #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
233 (debug-fun :ref-known (flushable)
234 :ref-trans %simple-fun-debug-fun
235 :set-known ()
236 :set-trans (setf %simple-fun-debug-fun))
237 (code :rest-p t :c-type "unsigned char"))
239 (!define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
240 (return-point :c-type "unsigned char" :rest-p t))
242 (!define-primitive-object (closure :lowtag fun-pointer-lowtag
243 :widetag closure-header-widetag)
244 ;; %CLOSURE-FUN should never be invoked on x86[-64].
245 ;; The above remark at %SIMPLE-FUN-SELF is relevant in its sentiment,
246 ;; but actually no longer true - the confusing situation is not caught
247 ;; until too late. But at least this one was nonfatal.
248 #!-(or x86 x86-64) (fun :init :arg :ref-trans %closure-fun)
249 #!+(or x86 x86-64) (fun :init :arg)
250 (info :rest-p t))
252 (!define-primitive-object (funcallable-instance
253 :lowtag fun-pointer-lowtag
254 :widetag funcallable-instance-header-widetag
255 :alloc-trans %make-funcallable-instance)
256 (trampoline :init :funcallable-instance-tramp)
257 (function :ref-known (flushable) :ref-trans %funcallable-instance-function
258 :set-known () :set-trans (setf %funcallable-instance-function))
259 (info :rest-p t))
261 (!define-primitive-object (value-cell :lowtag other-pointer-lowtag
262 :widetag value-cell-header-widetag
263 ;; FIXME: We also have an explicit VOP
264 ;; for this. Is this needed as well?
265 :alloc-trans make-value-cell)
266 (value :set-trans value-cell-set
267 :set-known ()
268 :ref-trans value-cell-ref
269 :ref-known (flushable)
270 :init :arg))
272 #!+alpha
273 (!define-primitive-object (sap :lowtag other-pointer-lowtag
274 :widetag sap-widetag)
275 (padding)
276 (pointer :c-type "char *" :length 2))
278 #!-alpha
279 (!define-primitive-object (sap :lowtag other-pointer-lowtag
280 :widetag sap-widetag)
281 (pointer :c-type "char *"))
284 (!define-primitive-object (weak-pointer :type weak-pointer
285 :lowtag other-pointer-lowtag
286 :widetag weak-pointer-widetag
287 :alloc-trans make-weak-pointer)
288 (value :ref-trans sb!c::%weak-pointer-value :ref-known (flushable)
289 :init :arg)
290 (broken :type (member t nil)
291 :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
292 :init :null)
293 (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
295 ;;;; other non-heap data blocks
297 (!define-primitive-object (binding)
298 value
299 symbol) ;; on sb-thread, this is actually a tls-index
301 (!define-primitive-object (unwind-block)
302 (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
303 (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
304 #!-(or x86 x86-64) current-code
305 entry-pc
306 #!+win32 next-seh-frame
307 #!+win32 seh-frame-handler)
309 (!define-primitive-object (catch-block)
310 (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
311 (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
312 #!-(or x86 x86-64) current-code
313 entry-pc
314 #!+(and win32 x86) next-seh-frame
315 #!+(and win32 x86) seh-frame-handler
317 (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32"))
319 ;;;; symbols
321 (!define-primitive-object (symbol :lowtag other-pointer-lowtag
322 :widetag symbol-header-widetag
323 :alloc-trans %make-symbol
324 :type symbol)
326 ;; Beware when changing this definition. NIL-the-symbol is defined
327 ;; using this layout, and NIL-the-end-of-list-marker is the cons
328 ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
329 ;; (conses have no header). Careful selection of lowtags ensures
330 ;; that the same pointer can be used for both purposes:
331 ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
332 ;; subtract 3 from (SB-KERNEL:GET-LISP-OBJ-ADDRESS 'NIL) you get the
333 ;; first data slot, and if you subtract 7 you get a symbol header.
335 ;; also the CAR of NIL-as-end-of-list
336 (value :init :unbound
337 :set-trans %set-symbol-global-value
338 :set-known ())
339 ;; also the CDR of NIL-as-end-of-list. Its reffer needs special
340 ;; care for this reason, as hash values must be fixnums.
341 (hash :set-trans %set-symbol-hash)
343 (info :ref-trans symbol-info :ref-known (flushable)
344 :set-trans (setf symbol-info)
345 :set-known ()
346 :cas-trans %compare-and-swap-symbol-info
347 :type (or simple-vector list)
348 :init :null)
349 (name :ref-trans symbol-name :init :arg)
350 (package :ref-trans symbol-package
351 :set-trans %set-symbol-package
352 :init :null)
353 ;; 0 tls-index means no tls-index is allocated
354 ;; x86-64 puts the tls-index in the header word.
355 #!+(and sb-thread (not x86-64))
356 (tls-index :ref-known (flushable) :ref-trans symbol-tls-index))
358 (!define-primitive-object (complex-single-float
359 :lowtag other-pointer-lowtag
360 :widetag complex-single-float-widetag)
361 #!+x86-64
362 (data :c-type "struct { float data[2]; } ")
363 #!-x86-64
364 (real :c-type "float")
365 #!-x86-64
366 (imag :c-type "float"))
368 (!define-primitive-object (complex-double-float
369 :lowtag other-pointer-lowtag
370 :widetag complex-double-float-widetag)
371 (filler)
372 (real :c-type "double" :length #!-x86-64 2 #!+x86-64 1)
373 (imag :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
375 #!+sb-simd-pack
376 (!define-primitive-object (simd-pack
377 :lowtag other-pointer-lowtag
378 :widetag simd-pack-widetag)
379 (tag :ref-trans %simd-pack-tag
380 :attributes (movable flushable)
381 :type fixnum)
382 (lo-value :c-type "long" :type (unsigned-byte 64))
383 (hi-value :c-type "long" :type (unsigned-byte 64)))
385 ;;; this isn't actually a lisp object at all, it's a c structure that lives
386 ;;; in c-land. However, we need sight of so many parts of it from Lisp that
387 ;;; it makes sense to define it here anyway, so that the GENESIS machinery
388 ;;; can take care of maintaining Lisp and C versions.
389 (!define-primitive-object (thread)
390 ;; no_tls_value_marker is borrowed very briefly at thread startup to
391 ;; pass the address of initial-function into new_thread_trampoline.
392 ;; tls[0] = NO_TLS_VALUE_MARKER_WIDETAG because a the tls index slot
393 ;; of a symbol is initialized to zero
394 (no-tls-value-marker)
395 (os-thread :c-type "os_thread_t")
396 ;; This is the original address at which the memory was allocated,
397 ;; which may have different alignment then what we prefer to use.
398 ;; Kept here so that when the thread dies we can release the whole
399 ;; memory we reserved.
400 (os-address :c-type "void *" :length #!+alpha 2 #!-alpha 1)
402 ;; Keep these next six slots (alloc-region being figured in as 1 slot)
403 ;; near the beginning of the structure so that x86[-64] assembly code
404 ;; can use single-byte displacements from thread-base-tn.
405 ;; Doing so reduces code size for allocation sequences and special variable
406 ;; manipulations by fixing their TLS offsets to be < 2^7, the largest
407 ;; aligned displacement fitting in a signed byte.
408 #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
409 #!+(or x86 x86-64 sb-thread) (pseudo-atomic-bits :special *pseudo-atomic-bits*)
410 ;; next two not used in C, but this wires the TLS offsets to small values
411 #!+(and x86-64 sb-thread)
412 (current-catch-block :special *current-catch-block*)
413 #!+(and x86-64 sb-thread)
414 (current-unwind-protect-block :special *current-unwind-protect-block*)
415 (alien-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1
416 :special *alien-stack-pointer*)
417 (binding-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1
418 :special *binding-stack-pointer*)
419 ;; END of slots to keep near the beginning.
421 ;; These aren't accessed (much) from Lisp, so don't really care
422 ;; if it takes a 4-byte displacement.
423 (alien-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
424 (binding-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1
425 :special *binding-stack-start*)
426 #!+sb-thread
427 (os-attr :c-type "pthread_attr_t *" :length #!+alpha 2 #!-alpha 1)
428 #!+sb-thread
429 (state-sem :c-type "os_sem_t *" :length #!+alpha 2 #!-alpha 1)
430 #!+sb-thread
431 (state-not-running-sem :c-type "os_sem_t *" :length #!+alpha 2 #!-alpha 1)
432 #!+sb-thread
433 (state-not-running-waitcount :c-type "int" :length 1)
434 #!+sb-thread
435 (state-not-stopped-sem :c-type "os_sem_t *" :length #!+alpha 2 #!-alpha 1)
436 #!+sb-thread
437 (state-not-stopped-waitcount :c-type "int" :length 1)
438 (control-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1
439 :special *control-stack-start*)
440 (control-stack-end :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1
441 :special *control-stack-end*)
442 (control-stack-guard-page-protected)
443 #!+win32 (private-events :c-type "struct private_events" :length 2)
444 (this :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
445 (prev :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
446 (next :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
447 ;; starting, running, suspended, dead
448 (state :c-type "lispobj")
449 (tls-cookie) ; on x86, the LDT index
450 (interrupt-data :c-type "struct interrupt_data *"
451 :length #!+alpha 2 #!-alpha 1)
452 (stepping)
453 ;; For various reasons related to pseudo-atomic and interrupt
454 ;; handling, we need to know if the machine context is in Lisp code
455 ;; or not. On non-threaded targets, this is a global variable in
456 ;; the runtime, but it's clearly a per-thread value.
457 #!+sb-thread
458 (foreign-function-call-active :c-type "boolean")
459 ;; Same as above for the location of the current control stack frame.
460 #!+(and sb-thread (not (or x86 x86-64)))
461 (control-frame-pointer :c-type "lispobj *")
462 ;; Same as above for the location of the current control stack
463 ;; pointer. This is also used on threaded x86oids to allow LDB to
464 ;; print an approximation of the CSP as needed.
465 #!+sb-thread
466 (control-stack-pointer :c-type "lispobj *")
467 #!+mach-exception-handler
468 (mach-port-name :c-type "mach_port_name_t")
469 (nonpointer-data :c-type "struct nonpointer_thread_data *" :length #!+alpha 2 #!-alpha 1)
470 #!+(and sb-safepoint x86) (selfptr :c-type "struct thread *")
471 ;; Context base pointer for running on top of system libraries built using
472 ;; -fomit-frame-pointer. Currently truly required and implemented only
473 ;; for (and win32 x86-64), but could be generalized to other platforms if
474 ;; needed:
475 #!+win32 (carried-base-pointer :c-type "os_context_register_t")
476 #!+sb-safepoint (csp-around-foreign-call :c-type "lispobj *")
477 #!+sb-safepoint (pc-around-foreign-call :c-type "lispobj *")
478 #!+win32 (synchronous-io-handle-and-flag :c-type "HANDLE" :length 1)
479 #!+(and sb-safepoint-strictly (not win32))
480 (sprof-alloc-region :c-type "struct alloc_region" :length 5)
481 ;; KLUDGE: On alpha, until STEPPING we have been lucky and the 32
482 ;; bit slots came in pairs. However the C compiler will align
483 ;; interrupt_contexts on a double word boundary. This logic should
484 ;; be handled by !DEFINE-PRIMITIVE-OBJECT.
485 #!+alpha
486 (padding)
487 (interrupt-contexts :c-type "os_context_t *" :rest-p t))