7b9b242813723d9079af2c4f7589ad3b664306f8
[sbcl.git] / src / compiler / generic / objdef.lisp
1 ;;;; machine-independent aspects of the object representation
2
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.
11
12 (in-package "SB!VM")
13
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 ;;;;   * (mysterious crashes which occur after changing the length
18 ;;;;     of SIMPLE-FUN, just adding a new slot not even doing anything
19 ;;;;     with it, still dunno why)
20 ;;;;   * The GC scavenging code (and for all I know other GC code too)
21 ;;;;     is not automatically generated from these layouts, but instead
22 ;;;;     was hand-written to correspond to them. The offsets are
23 ;;;;     automatically propagated into the GC scavenging code, but the
24 ;;;;     existence of slots, and whether they should be scavenged, is
25 ;;;;     not automatically propagated. Thus e.g. if you add a
26 ;;;;     SIMPLE-FUN-DEBUG-INFO slot holding a tagged object which needs
27 ;;;;     to be GCed, you need to tweak scav_code_header() and
28 ;;;;     verify_space() in gencgc.c, and the corresponding code in gc.c.
29 ;;;;   * The src/runtime/print.c code (used by LDB) is implemented
30 ;;;;     using hand-written lists of slot names, which aren't automatically
31 ;;;;     generated from the code in this file.
32 ;;;;   * Various code (e.g. STATIC-FSET in genesis.lisp) is hard-wired
33 ;;;;     to know the name of the last slot of the object the code works
34 ;;;;     with, and implicitly to know that the last slot is special (being
35 ;;;;     the beginning of an arbitrary-length sequence of bytes following
36 ;;;;     the fixed-layout slots).
37 ;;;; -- WHN 2001-12-29
38 \f
39 ;;;; the primitive objects themselves
40
41 (define-primitive-object (cons :lowtag list-pointer-lowtag
42                                :alloc-trans cons)
43   (car :ref-trans car :set-trans sb!c::%rplaca :init :arg)
44   (cdr :ref-trans cdr :set-trans sb!c::%rplacd :init :arg))
45
46 (define-primitive-object (instance :lowtag instance-pointer-lowtag
47                                    :widetag instance-header-widetag
48                                    :alloc-trans %make-instance)
49   (slots :rest-p t))
50
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 "long" #!+alpha "u32"))
55
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))
68
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"))
73
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))
78
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))
84
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))
97
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 (unsafe))
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 (unsafe))
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 (unsafe))
117   (data :type array
118         :ref-trans %array-data-vector
119         :ref-known (flushable foldable)
120         :set-trans (setf %array-data-vector)
121         :set-known (unsafe))
122   (displacement :type (or index null)
123                 :ref-trans %array-displacement
124                 :ref-known (flushable foldable)
125                 :set-trans (setf %array-displacement)
126                 :set-known (unsafe))
127   (displaced-p :type (member t nil)
128                :ref-trans %array-displaced-p
129                :ref-known (flushable foldable)
130                :set-trans (setf %array-displaced-p)
131                :set-known (unsafe))
132   (dimensions :rest-p t))
133
134 (define-primitive-object (vector :type vector
135                                  :lowtag other-pointer-lowtag
136                                  :widetag t)
137   ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
138   ;; VECTOR -- see SHRINK-VECTOR.
139   (length :ref-trans sb!c::vector-length
140           :type index)
141   (data :rest-p t :c-type #!-alpha "unsigned long" #!+alpha "u32"))
142
143 (define-primitive-object (code :type code-component
144                                :lowtag other-pointer-lowtag
145                                :widetag t)
146   (code-size :type index
147              :ref-known (flushable movable)
148              :ref-trans %code-code-size)
149   (entry-points :type (or function null)
150                 :ref-known (flushable)
151                 :ref-trans %code-entry-points
152                 :set-known (unsafe)
153                 :set-trans (setf %code-entry-points))
154   (debug-info :type t
155               :ref-known (flushable)
156               :ref-trans %code-debug-info
157               :set-known (unsafe)
158               :set-trans (setf %code-debug-info))
159   (trace-table-offset)
160   (constants :rest-p t))
161
162 (define-primitive-object (fdefn :type fdefn
163                                 :lowtag other-pointer-lowtag
164                                 :widetag fdefn-widetag)
165   (name :ref-trans fdefn-name)
166   (fun :type (or function null) :ref-trans fdefn-fun)
167   (raw-addr :c-type #!-alpha "char *" #!+alpha "u32"))
168
169 ;;; a simple function (as opposed to hairier things like closures
170 ;;; which are also subtypes of Common Lisp's FUNCTION type)
171 (define-primitive-object (simple-fun :type function
172                                      :lowtag fun-pointer-lowtag
173                                      :widetag simple-fun-header-widetag)
174   #!-(or x86 x86-64) (self :ref-trans %simple-fun-self
175                :set-trans (setf %simple-fun-self))
176   #!+(or x86 x86-64) (self
177           ;; KLUDGE: There's no :SET-KNOWN, :SET-TRANS, :REF-KNOWN, or
178           ;; :REF-TRANS here in this case. Instead, there's separate
179           ;; DEFKNOWN/DEFINE-VOP/DEFTRANSFORM stuff in
180           ;; compiler/x86/system.lisp to define and declare them by
181           ;; hand. I don't know why this is, but that's (basically)
182           ;; the way it was done in CMU CL, and it works. (It's not
183           ;; exactly the same way it was done in CMU CL in that CMU
184           ;; CL's allows duplicate DEFKNOWNs, blithely overwriting any
185           ;; previous data associated with the previous DEFKNOWN, and
186           ;; that property was used to mask the definitions here. In
187           ;; SBCL as of 0.6.12.64 that's not allowed -- too confusing!
188           ;; -- so we have to explicitly suppress the DEFKNOWNish
189           ;; stuff here in order to allow this old hack to work in the
190           ;; new world. -- WHN 2001-08-82
191           )
192   (next :type (or function null)
193         :ref-known (flushable)
194         :ref-trans %simple-fun-next
195         :set-known (unsafe)
196         :set-trans (setf %simple-fun-next))
197   (name :ref-known (flushable)
198         :ref-trans %simple-fun-name
199         :set-known (unsafe)
200         :set-trans (setf %simple-fun-name))
201   (arglist :type list
202            :ref-known (flushable)
203            :ref-trans %simple-fun-arglist
204            :set-known (unsafe)
205            :set-trans (setf %simple-fun-arglist))
206   (type :ref-known (flushable)
207         :ref-trans %simple-fun-type
208         :set-known (unsafe)
209         :set-trans (setf %simple-fun-type))
210   ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
211   #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
212   (debug-fun :ref-known (flushable)
213              :ref-trans %simple-fun-debug-fun
214              :set-known (unsafe)
215              :set-trans (setf %simple-fun-debug-fun))
216   (code :rest-p t :c-type "unsigned char"))
217
218 (define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
219   (return-point :c-type "unsigned char" :rest-p t))
220
221 (define-primitive-object (closure :lowtag fun-pointer-lowtag
222                                   :widetag closure-header-widetag)
223   (fun :init :arg :ref-trans %closure-fun)
224   (info :rest-p t))
225
226 (define-primitive-object (funcallable-instance
227                           :lowtag fun-pointer-lowtag
228                           :widetag funcallable-instance-header-widetag
229                           :alloc-trans %make-funcallable-instance)
230   (trampoline :init :funcallable-instance-tramp)
231   (function :ref-known (flushable) :ref-trans %funcallable-instance-function
232             :set-known (unsafe) :set-trans (setf %funcallable-instance-function))
233   (info :rest-p t))
234
235 (define-primitive-object (value-cell :lowtag other-pointer-lowtag
236                                      :widetag value-cell-header-widetag
237                                      :alloc-trans make-value-cell)
238   (value :set-trans value-cell-set
239          :set-known (unsafe)
240          :ref-trans value-cell-ref
241          :ref-known (flushable)
242          :init :arg))
243
244 #!+alpha
245 (define-primitive-object (sap :lowtag other-pointer-lowtag
246                               :widetag sap-widetag)
247   (padding)
248   (pointer :c-type "char *" :length 2))
249
250 #!-alpha
251 (define-primitive-object (sap :lowtag other-pointer-lowtag
252                               :widetag sap-widetag)
253   (pointer :c-type "char *"))
254
255
256 (define-primitive-object (weak-pointer :type weak-pointer
257                                        :lowtag other-pointer-lowtag
258                                        :widetag weak-pointer-widetag
259                                        :alloc-trans make-weak-pointer)
260   (value :ref-trans sb!c::%weak-pointer-value :ref-known (flushable)
261          :init :arg)
262   (broken :type (member t nil)
263           :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
264           :init :null)
265   (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
266
267 ;;;; other non-heap data blocks
268
269 (define-primitive-object (binding)
270   value
271   symbol)
272
273 (define-primitive-object (unwind-block)
274   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
275   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
276   #!-(or x86 x86-64) current-code
277   entry-pc)
278
279 (define-primitive-object (catch-block)
280   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
281   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
282   #!-(or x86 x86-64) current-code
283   entry-pc
284   tag
285   (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32")
286   size)
287
288 ;;; (For an explanation of this, see the comments at the definition of
289 ;;; KLUDGE-NONDETERMINISTIC-CATCH-BLOCK-SIZE.)
290 (aver (= kludge-nondeterministic-catch-block-size catch-block-size))
291 \f
292 ;;;; symbols
293
294 (define-primitive-object (symbol :lowtag other-pointer-lowtag
295                                  :widetag symbol-header-widetag
296                                  :alloc-trans %make-symbol)
297
298   ;; Beware when changing this definition.  NIL-the-symbol is defined
299   ;; using this layout, and NIL-the-end-of-list-marker is the cons
300   ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
301   ;; (conses have no header).  Careful selection of lowtags ensures
302   ;; that the same pointer can be used for both purposes:
303   ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
304   ;; subtract 3 from (SB-KERNEL:GET-LISP-OBJ-ADDRESS 'NIL) you get the
305   ;; first data slot, and if you subtract 7 you get a symbol header.
306
307   ;; also the CAR of NIL-as-end-of-list
308   (value :init :unbound :ref-known (flushable) :ref-trans symbol-global-value)
309   ;; also the CDR of NIL-as-end-of-list.  Its reffer needs special
310   ;; care for this reason, as hash values must be fixnums.
311   (hash :set-trans %set-symbol-hash)
312
313   (plist :ref-trans symbol-plist
314          :set-trans %set-symbol-plist
315          :init :null)
316   (name :ref-trans symbol-name :init :arg)
317   (package :ref-trans symbol-package
318            :set-trans %set-symbol-package
319            :init :null)
320   #!+sb-thread (tls-index :ref-known (flushable) :ref-trans symbol-tls-index))
321
322 (define-primitive-object (complex-single-float
323                           :lowtag other-pointer-lowtag
324                           :widetag complex-single-float-widetag)
325   (real :c-type "float")
326   (imag :c-type "float"))
327
328 (define-primitive-object (complex-double-float
329                           :lowtag other-pointer-lowtag
330                           :widetag complex-double-float-widetag)
331   #!-x86-64 (filler)
332   (real :c-type "double" :length #!-x86-64 2 #!+x86-64 1)
333   (imag :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
334
335 #!+(and sb-lutex sb-thread)
336 (define-primitive-object (lutex
337                           :lowtag other-pointer-lowtag
338                           :widetag lutex-widetag
339                           :alloc-trans %make-lutex)
340   (gen :c-type "long" :length 1)
341   (live :c-type "long" :length 1)
342   (next :c-type "struct lutex *" :length 1)
343   (prev :c-type "struct lutex *" :length 1)
344   (mutex :c-type "pthread_mutex_t *"
345          :length 1)
346   (condition-variable :c-type "pthread_cond_t *"
347                       :length 1))
348
349 ;;; this isn't actually a lisp object at all, it's a c structure that lives
350 ;;; in c-land.  However, we need sight of so many parts of it from Lisp that
351 ;;; it makes sense to define it here anyway, so that the GENESIS machinery
352 ;;; can take care of maintaining Lisp and C versions.
353 ;;; Hence the even-fixnum lowtag just so we don't get odd(sic) numbers
354 ;;; added to the slot offsets
355 (define-primitive-object (thread :lowtag even-fixnum-lowtag)
356   ;; no_tls_value_marker is borrowed very briefly at thread startup to
357   ;; pass the address of initial-function into new_thread_trampoline.
358   ;; tls[0] = NO_TLS_VALUE_MARKER_WIDETAG because a the tls index slot
359   ;; of a symbol is initialized to zero
360   (no-tls-value-marker)
361   (os-thread :c-type "volatile os_thread_t")
362   (binding-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
363   (binding-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
364   (control-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
365   (control-stack-end :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
366   (alien-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
367   (alien-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
368   #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
369   (this :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
370   (prev :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
371   (next :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
372   ;; starting, running, suspended, dead
373   (state :c-type "volatile lispobj")
374   (tls-cookie)                          ;  on x86, the LDT index
375   #!+(or x86 x86-64) (pseudo-atomic-bits)
376   (interrupt-data :c-type "struct interrupt_data *"
377                   :length #!+alpha 2 #!-alpha 1)
378   (stepping)
379   (interrupt-contexts :c-type "os_context_t *" :rest-p t))