Simplify fixup_space() in coreparse
[sbcl.git] / src / compiler / generic / objdef.lisp
blobd4e0a8ceac1e70d1bf40ff643f975d35a3536ce0
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 ;;;; * Various code (e.g. STATIC-FSET in genesis.lisp) is hard-wired
27 ;;;; to know the name of the last slot of the object the code works
28 ;;;; with, and implicitly to know that the last slot is special (being
29 ;;;; the beginning of an arbitrary-length sequence of bytes following
30 ;;;; the fixed-layout slots).
31 ;;;; -- WHN 2001-12-29
33 ;;;; the primitive objects themselves
35 (!define-primitive-object (cons :type cons
36 :lowtag list-pointer-lowtag
37 :alloc-trans cons)
38 (car :ref-trans car :set-trans sb!c::%rplaca :init :arg
39 :cas-trans %compare-and-swap-car)
40 (cdr :ref-trans cdr :set-trans sb!c::%rplacd :init :arg
41 :cas-trans %compare-and-swap-cdr))
43 (!define-primitive-object (instance :lowtag instance-pointer-lowtag
44 :widetag instance-widetag
45 :alloc-trans %make-instance)
46 (slots :rest-p t))
48 (!define-primitive-object (bignum :lowtag other-pointer-lowtag
49 :widetag bignum-widetag
50 :alloc-trans sb!bignum::%allocate-bignum)
51 (digits :rest-p t :c-type #!-alpha "sword_t" #!+alpha "u32"))
53 (!define-primitive-object (ratio :type ratio
54 :lowtag other-pointer-lowtag
55 :widetag ratio-widetag
56 :alloc-trans %make-ratio)
57 (numerator :type integer
58 :ref-known (flushable movable)
59 :ref-trans %numerator
60 :init :arg)
61 (denominator :type integer
62 :ref-known (flushable movable)
63 :ref-trans %denominator
64 :init :arg))
66 #!-64-bit
67 (!define-primitive-object (single-float :lowtag other-pointer-lowtag
68 :widetag single-float-widetag)
69 (value :c-type "float"))
71 (!define-primitive-object (double-float :lowtag other-pointer-lowtag
72 :widetag double-float-widetag)
73 #!-64-bit (filler)
74 (value :c-type "double" :length #.(/ 64 n-word-bits)))
76 #!+long-float
77 (!define-primitive-object (long-float :lowtag other-pointer-lowtag
78 :widetag long-float-widetag)
79 #!+sparc (filler)
80 (value :c-type "long double" :length #!+x86 3 #!+sparc 4))
82 (!define-primitive-object (complex :type complex
83 :lowtag other-pointer-lowtag
84 :widetag complex-widetag
85 :alloc-trans %make-complex)
86 (real :type real
87 :ref-known (flushable movable)
88 :ref-trans %realpart
89 :init :arg)
90 (imag :type real
91 :ref-known (flushable movable)
92 :ref-trans %imagpart
93 :init :arg))
95 (!define-primitive-object (array :lowtag other-pointer-lowtag
96 :widetag t)
97 ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
98 ;; VECTOR -- see SHRINK-VECTOR.
99 (fill-pointer :type index
100 :ref-trans %array-fill-pointer
101 :ref-known (flushable foldable)
102 :set-trans (setf %array-fill-pointer)
103 :set-known ())
104 (fill-pointer-p :type (member t nil)
105 :ref-trans %array-fill-pointer-p
106 :ref-known (flushable foldable)
107 :set-trans (setf %array-fill-pointer-p)
108 :set-known ())
109 (elements :type index
110 :ref-trans %array-available-elements
111 :ref-known (flushable foldable)
112 :set-trans (setf %array-available-elements)
113 :set-known ())
114 (data :type array
115 :ref-trans %array-data ; might be a vector, might not be
116 :ref-known (flushable foldable)
117 :set-trans (setf %array-data)
118 :set-known ())
119 (displacement :type index
120 :ref-trans %array-displacement
121 :ref-known (flushable foldable)
122 :set-trans (setf %array-displacement)
123 :set-known ())
124 (displaced-p :type t
125 :ref-trans %array-displaced-p
126 :ref-known (flushable foldable)
127 :set-trans (setf %array-displaced-p)
128 :set-known ())
129 (displaced-from :type list
130 :ref-trans %array-displaced-from
131 :ref-known (flushable)
132 :set-trans (setf %array-displaced-from)
133 :set-known ())
134 (dimensions :rest-p t))
136 (!define-primitive-object (vector :type vector
137 :lowtag other-pointer-lowtag
138 :widetag t)
139 ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
140 ;; VECTOR -- see SHRINK-VECTOR.
141 (length :ref-trans sb!c::vector-length
142 :type index)
143 (data :rest-p t :c-type #!-alpha "uword_t" #!+alpha "u32"))
145 ;;; The header contains the size of slots and constants in words.
146 (!define-primitive-object (code :type code-component
147 :lowtag other-pointer-lowtag
148 :widetag t)
149 ;; This is the size of instructions in bytes, not aligned.
150 ;; Adding the size from the header and aligned code-size will yield
151 ;; the total size of the code-object.
152 (code-size :type index
153 :ref-known (flushable movable)
154 :ref-trans %code-code-size)
155 (debug-info :type t
156 :ref-known (flushable)
157 :ref-trans %code-debug-info
158 :set-known ()
159 :set-trans (setf %code-debug-info))
160 #!-64-bit
161 (n-entries :type fixnum
162 :set-known ()
163 :set-trans (setf %code-n-entries)
164 :ref-trans %code-n-entries
165 :ref-known (flushable foldable))
166 #!+(or x86 immobile-space)
167 (fixups :type t
168 :ref-known (flushable)
169 :ref-trans %code-fixups
170 :set-known ()
171 :set-trans (setf %code-fixups))
172 (constants :rest-p t))
174 (!define-primitive-object (fdefn :type fdefn
175 :lowtag other-pointer-lowtag
176 :widetag fdefn-widetag)
177 (name :ref-trans fdefn-name
178 :set-trans %set-fdefn-name :set-known ())
179 (fun :type (or function null) :ref-trans fdefn-fun)
180 ;; raw-addr is used differently by the various backends:
181 ;; - Sparc and ARM store the same object as 'fun'
182 ;; unless the function is non-simple, in which case
183 ;; they store a descriptorized (fun-pointer lowtag)
184 ;; pointer to the closure tramp
185 ;; - x86-64 with immobile-code feature stores a JMP instruction
186 ;; to the function entry address. Special considerations
187 ;; pertain to undefined functions, FINs, and closures.
188 ;; - all others store a native pointer to the function entry address
189 ;; or closure tramp
190 (raw-addr :c-type #!-alpha "char *" #!+alpha "u32"))
192 ;;; a simple function (as opposed to hairier things like closures
193 ;;; which are also subtypes of Common Lisp's FUNCTION type)
194 (!define-primitive-object (simple-fun :type function
195 :lowtag fun-pointer-lowtag
196 :widetag simple-fun-widetag)
197 ;; All three function primitive-objects have the first word after the header
198 ;; as some kind of entry point, either the address to jump to, in the case
199 ;; of x86, or the Lisp function to jump to, for everybody else.
200 (self :set-known ()
201 :set-trans (setf %simple-fun-self))
202 (name :ref-known (flushable)
203 :ref-trans %simple-fun-name
204 :set-known ()
205 :set-trans (setf %simple-fun-name))
206 (arglist :type list
207 :ref-known (flushable)
208 :ref-trans %simple-fun-arglist
209 :set-known ()
210 :set-trans (setf %simple-fun-arglist))
211 (type :ref-known (flushable)
212 ;; %%SIMPLE-FUN-TYPE is used only by %SIMPLE-FUN-TYPE.
213 ;; Nobody should care that %SIMPLE-FUN-TYPE isn't open-coded.
214 :ref-trans %%simple-fun-type
215 :set-known ()
216 :set-trans (setf %simple-fun-type))
217 ;; NIL for empty, STRING for a docstring, SIMPLE-VECTOR for XREFS, and (CONS
218 ;; STRING SIMPLE-VECTOR) for both.
219 (info :init :null
220 :ref-trans %simple-fun-info
221 :ref-known (flushable)
222 :set-trans (setf %simple-fun-info)
223 :set-known ())
224 ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
225 #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
226 (debug-fun :ref-known (flushable)
227 :ref-trans %simple-fun-debug-fun
228 :set-known ()
229 :set-trans (setf %simple-fun-debug-fun))
230 (code :rest-p t :c-type "unsigned char"))
232 #!-(or x86 x86-64)
233 (!define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
234 (return-point :c-type "unsigned char" :rest-p t))
236 (!define-primitive-object (closure :lowtag fun-pointer-lowtag
237 :widetag closure-widetag
238 ;; This allocator is %COPY-foo because it's only
239 ;; used when renaming a closure. The compiler has
240 ;; its own way of making closures, which requires
241 ;; that the length be a compile-time constant.
242 :alloc-trans %copy-closure)
243 (fun :init :arg :ref-trans #!+(or x86 x86-64) %closure-callee
244 #!-(or x86 x86-64) %closure-fun)
245 (info :rest-p t))
247 (!define-primitive-object (funcallable-instance
248 :lowtag fun-pointer-lowtag
249 :widetag funcallable-instance-widetag
250 :alloc-trans %make-funcallable-instance)
251 (trampoline :init :funcallable-instance-tramp)
252 ;; TODO: if we can switch places of 'function' and 'fsc-instance-slots'
253 ;; (at least for the builds with compact-instance-header)
254 ;; then for both funcallable and non-funcallable instances,
255 ;; the CLOS slot vector will be in the word 5 bytes past the tagged pointer.
256 ;; This shouldn't be too hard to arrange, since nothing needs to know where
257 ;; the tagged function lives except the funcallable instance trampoline.
258 (function :ref-known (flushable) :ref-trans %funcallable-instance-function
259 :set-known () :set-trans (setf %funcallable-instance-function))
260 (info :rest-p t))
262 (!define-primitive-object (value-cell :lowtag other-pointer-lowtag
263 :widetag value-cell-widetag
264 ;; FIXME: We also have an explicit VOP
265 ;; for this. Is this needed as well?
266 :alloc-trans make-value-cell)
267 (value :set-trans value-cell-set
268 :set-known ()
269 :ref-trans value-cell-ref
270 :ref-known (flushable)
271 :init :arg))
273 (!define-primitive-object (sap :lowtag other-pointer-lowtag
274 :widetag sap-widetag)
275 (pointer :c-type "char *" :pointer t))
278 (!define-primitive-object (weak-pointer :type weak-pointer
279 :lowtag other-pointer-lowtag
280 :widetag weak-pointer-widetag
281 :alloc-trans make-weak-pointer)
282 (value :ref-trans %weak-pointer-value :ref-known (flushable)
283 :init :arg)
284 (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
286 ;;;; other non-heap data blocks
288 (!define-primitive-object (binding)
289 value
290 symbol) ;; on sb-thread, this is actually a tls-index
292 (!define-primitive-object (unwind-block)
293 (uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
294 (cfp :c-type #!-alpha "lispobj *" #!+alpha "u32")
295 #!-(or x86 x86-64) code
296 entry-pc
297 #!+win32 next-seh-frame
298 #!+win32 seh-frame-handler)
300 (!define-primitive-object (catch-block)
301 (uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
302 (cfp :c-type #!-alpha "lispobj *" #!+alpha "u32")
303 #!-(or x86 x86-64) code
304 entry-pc
305 #!+(and win32 x86) next-seh-frame
306 #!+(and win32 x86) seh-frame-handler
308 (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32"))
310 ;;;; symbols
312 (!define-primitive-object (symbol :lowtag other-pointer-lowtag
313 :widetag symbol-widetag
314 :alloc-trans %%make-symbol
315 :type symbol)
317 ;; Beware when changing this definition. NIL-the-symbol is defined
318 ;; using this layout, and NIL-the-end-of-list-marker is the cons
319 ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
320 ;; (conses have no header). Careful selection of lowtags ensures
321 ;; that the same pointer can be used for both purposes:
322 ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
323 ;; subtract 3 from (SB-KERNEL:GET-LISP-OBJ-ADDRESS 'NIL) you get the
324 ;; first data slot, and if you subtract 7 you get a symbol header.
326 ;; also the CAR of NIL-as-end-of-list
327 (value :init :unbound
328 :set-trans %set-symbol-global-value
329 :set-known ())
330 ;; also the CDR of NIL-as-end-of-list. Its reffer needs special
331 ;; care for this reason, as hash values must be fixnums.
332 (hash :set-trans %set-symbol-hash)
334 (info :ref-trans symbol-info :ref-known (flushable)
335 :set-trans (setf symbol-info)
336 :set-known ()
337 :cas-trans %compare-and-swap-symbol-info
338 :type (or simple-vector list)
339 :init :null)
340 (name :ref-trans symbol-name :init :arg)
341 (package :ref-trans symbol-package
342 :set-trans %set-symbol-package
343 :init :null)
344 ;; 0 tls-index means no tls-index is allocated
345 ;; 64-bit put the tls-index in the header word.
346 #!+(and sb-thread (not 64-bit))
347 (tls-index :ref-known (flushable) :ref-trans symbol-tls-index))
349 (!define-primitive-object (complex-single-float
350 :lowtag other-pointer-lowtag
351 :widetag complex-single-float-widetag)
352 #!+64-bit
353 (data :c-type "struct { float data[2]; } ")
354 #!-64-bit
355 (real :c-type "float")
356 #!-64-bit
357 (imag :c-type "float"))
359 (!define-primitive-object (complex-double-float
360 :lowtag other-pointer-lowtag
361 :widetag complex-double-float-widetag)
362 (filler)
363 (real :c-type "double" :length #.(/ 64 n-word-bits))
364 (imag :c-type "double" :length #.(/ 64 n-word-bits)))
366 #!+sb-simd-pack
367 (!define-primitive-object (simd-pack
368 :lowtag other-pointer-lowtag
369 :widetag simd-pack-widetag)
370 (tag :ref-trans %simd-pack-tag
371 :attributes (movable flushable)
372 :type fixnum)
373 (lo-value :c-type "long" :type (unsigned-byte 64))
374 (hi-value :c-type "long" :type (unsigned-byte 64)))
376 ;;; this isn't actually a lisp object at all, it's a c structure that lives
377 ;;; in c-land. However, we need sight of so many parts of it from Lisp that
378 ;;; it makes sense to define it here anyway, so that the GENESIS machinery
379 ;;; can take care of maintaining Lisp and C versions.
380 (!define-primitive-object (thread)
381 ;; no_tls_value_marker is borrowed very briefly at thread startup to
382 ;; pass the address of initial-function into new_thread_trampoline.
383 ;; tls[0] = NO_TLS_VALUE_MARKER_WIDETAG because a the tls index slot
384 ;; of a symbol is initialized to zero
385 (no-tls-value-marker)
386 (os-thread :c-type "os_thread_t")
387 ;; This is the original address at which the memory was allocated,
388 ;; which may have different alignment then what we prefer to use.
389 ;; Kept here so that when the thread dies we can release the whole
390 ;; memory we reserved.
391 (os-address :c-type "void *" :pointer t)
393 ;; Keep these next six slots (alloc-region being figured in as 1 slot)
394 ;; near the beginning of the structure so that x86[-64] assembly code
395 ;; can use single-byte displacements from thread-base-tn.
396 ;; Doing so reduces code size for allocation sequences and special variable
397 ;; manipulations by fixing their TLS offsets to be < 2^7, the largest
398 ;; aligned displacement fitting in a signed byte.
399 #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
400 #!+sb-thread (pseudo-atomic-bits #!+(or x86 x86-64) :special #!+(or x86 x86-64) *pseudo-atomic-bits*)
401 ;; next two not used in C, but this wires the TLS offsets to small values
402 #!+(and x86-64 sb-thread)
403 (current-catch-block :special *current-catch-block*)
404 #!+(and x86-64 sb-thread)
405 (current-unwind-protect-block :special *current-unwind-protect-block*)
406 (alien-stack-pointer :c-type "lispobj *" :pointer t
407 :special *alien-stack-pointer*)
408 (binding-stack-pointer :c-type "lispobj *" :pointer t
409 :special *binding-stack-pointer*)
410 (stepping)
411 ;; END of slots to keep near the beginning.
413 ;; These aren't accessed (much) from Lisp, so don't really care
414 ;; if it takes a 4-byte displacement.
415 (alien-stack-start :c-type "lispobj *" :pointer t)
416 (binding-stack-start :c-type "lispobj *" :pointer t
417 :special *binding-stack-start*)
419 #!+sb-thread
420 (os-attr :c-type "pthread_attr_t *" :pointer t)
421 #!+(and sb-thread (not sb-safepoint))
422 (state-sem :c-type "os_sem_t *" :pointer t)
423 #!+(and sb-thread (not sb-safepoint))
424 (state-not-running-sem :c-type "os_sem_t *" :pointer t)
425 #!+(and sb-thread (not sb-safepoint))
426 (state-not-running-waitcount :c-type "int" :length 1)
427 #!+(and sb-thread (not sb-safepoint))
428 (state-not-stopped-sem :c-type "os_sem_t *" :pointer t)
429 #!+(and sb-thread (not sb-safepoint))
430 (state-not-stopped-waitcount :c-type "int" :length 1)
431 (control-stack-start :c-type "lispobj *" :pointer t
432 :special *control-stack-start*)
433 (control-stack-end :c-type "lispobj *" :pointer t
434 :special *control-stack-end*)
435 (control-stack-guard-page-protected)
436 #!+win32 (private-events :c-type "struct private_events" :length 2)
437 (this :c-type "struct thread *" :pointer t)
438 (prev :c-type "struct thread *" :pointer t)
439 (next :c-type "struct thread *" :pointer t)
440 ;; starting, running, suspended, dead
441 (state :c-type "lispobj")
443 #!+x86 (tls-cookie) ; LDT index
444 (interrupt-data :c-type "struct interrupt_data *"
445 :pointer t)
446 ;; For various reasons related to pseudo-atomic and interrupt
447 ;; handling, we need to know if the machine context is in Lisp code
448 ;; or not. On non-threaded targets, this is a global variable in
449 ;; the runtime, but it's clearly a per-thread value.
450 #!+sb-thread
451 (foreign-function-call-active :c-type "boolean")
452 ;; Same as above for the location of the current control stack frame.
453 #!+(and sb-thread (not (or x86 x86-64)))
454 (control-frame-pointer :c-type "lispobj *")
455 ;; Same as above for the location of the current control stack
456 ;; pointer. This is also used on threaded x86oids to allow LDB to
457 ;; print an approximation of the CSP as needed.
458 #!+sb-thread
459 (control-stack-pointer :c-type "lispobj *")
460 #!+mach-exception-handler
461 (mach-port-name :c-type "mach_port_name_t")
462 ;; Context base pointer for running on top of system libraries built using
463 ;; -fomit-frame-pointer. Currently truly required and implemented only
464 ;; for (and win32 x86-64), but could be generalized to other platforms if
465 ;; needed:
466 #!+win32 (carried-base-pointer :c-type "os_context_register_t")
467 #!+sb-safepoint (csp-around-foreign-call :c-type "lispobj *")
468 #!+sb-safepoint (pc-around-foreign-call :c-type "lispobj *")
469 #!+win32 (synchronous-io-handle-and-flag :c-type "HANDLE" :length 1)
470 #!+(and sb-safepoint-strictly (not win32))
471 (sprof-alloc-region :c-type "struct alloc_region" :length 5)
472 (interrupt-contexts :c-type "os_context_t *" :rest-p t :pointer t))