15d86cc2417c2ab6f7662f630faf97cc3a05ca3d
[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   (xrefs :init :null
211          :ref-trans %simple-fun-xrefs
212          :ref-known (flushable)
213          :set-trans (setf %simple-fun-xrefs)
214          :set-known ())
215   ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
216   #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
217   (debug-fun :ref-known (flushable)
218              :ref-trans %simple-fun-debug-fun
219              :set-known (unsafe)
220              :set-trans (setf %simple-fun-debug-fun))
221   (code :rest-p t :c-type "unsigned char"))
222
223 (define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
224   (return-point :c-type "unsigned char" :rest-p t))
225
226 (define-primitive-object (closure :lowtag fun-pointer-lowtag
227                                   :widetag closure-header-widetag)
228   (fun :init :arg :ref-trans %closure-fun)
229   (info :rest-p t))
230
231 (define-primitive-object (funcallable-instance
232                           :lowtag fun-pointer-lowtag
233                           :widetag funcallable-instance-header-widetag
234                           :alloc-trans %make-funcallable-instance)
235   (trampoline :init :funcallable-instance-tramp)
236   (function :ref-known (flushable) :ref-trans %funcallable-instance-function
237             :set-known (unsafe) :set-trans (setf %funcallable-instance-function))
238   (info :rest-p t))
239
240 (define-primitive-object (value-cell :lowtag other-pointer-lowtag
241                                      :widetag value-cell-header-widetag
242                                      :alloc-trans make-value-cell)
243   (value :set-trans value-cell-set
244          :set-known (unsafe)
245          :ref-trans value-cell-ref
246          :ref-known (flushable)
247          :init :arg))
248
249 #!+alpha
250 (define-primitive-object (sap :lowtag other-pointer-lowtag
251                               :widetag sap-widetag)
252   (padding)
253   (pointer :c-type "char *" :length 2))
254
255 #!-alpha
256 (define-primitive-object (sap :lowtag other-pointer-lowtag
257                               :widetag sap-widetag)
258   (pointer :c-type "char *"))
259
260
261 (define-primitive-object (weak-pointer :type weak-pointer
262                                        :lowtag other-pointer-lowtag
263                                        :widetag weak-pointer-widetag
264                                        :alloc-trans make-weak-pointer)
265   (value :ref-trans sb!c::%weak-pointer-value :ref-known (flushable)
266          :init :arg)
267   (broken :type (member t nil)
268           :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
269           :init :null)
270   (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
271
272 ;;;; other non-heap data blocks
273
274 (define-primitive-object (binding)
275   value
276   symbol)
277
278 (define-primitive-object (unwind-block)
279   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
280   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
281   #!-(or x86 x86-64) current-code
282   entry-pc
283   #!+win32 next-seh-frame
284   #!+win32 seh-frame-handler)
285
286 (define-primitive-object (catch-block)
287   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
288   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
289   #!-(or x86 x86-64) current-code
290   entry-pc
291   #!+win32 next-seh-frame
292   #!+win32 seh-frame-handler
293   tag
294   (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32")
295   size)
296
297 ;;; (For an explanation of this, see the comments at the definition of
298 ;;; KLUDGE-NONDETERMINISTIC-CATCH-BLOCK-SIZE.)
299 (aver (= kludge-nondeterministic-catch-block-size catch-block-size))
300 \f
301 ;;;; symbols
302
303 (define-primitive-object (symbol :lowtag other-pointer-lowtag
304                                  :widetag symbol-header-widetag
305                                  :alloc-trans %make-symbol)
306
307   ;; Beware when changing this definition.  NIL-the-symbol is defined
308   ;; using this layout, and NIL-the-end-of-list-marker is the cons
309   ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
310   ;; (conses have no header).  Careful selection of lowtags ensures
311   ;; that the same pointer can be used for both purposes:
312   ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
313   ;; subtract 3 from (SB-KERNEL:GET-LISP-OBJ-ADDRESS 'NIL) you get the
314   ;; first data slot, and if you subtract 7 you get a symbol header.
315
316   ;; also the CAR of NIL-as-end-of-list
317   (value :init :unbound :ref-known (flushable) :ref-trans symbol-global-value)
318   ;; also the CDR of NIL-as-end-of-list.  Its reffer needs special
319   ;; care for this reason, as hash values must be fixnums.
320   (hash :set-trans %set-symbol-hash)
321
322   (plist :ref-trans symbol-plist
323          :set-trans %set-symbol-plist
324          :init :null)
325   (name :ref-trans symbol-name :init :arg)
326   (package :ref-trans symbol-package
327            :set-trans %set-symbol-package
328            :init :null)
329   #!+sb-thread (tls-index :ref-known (flushable) :ref-trans symbol-tls-index))
330
331 (define-primitive-object (complex-single-float
332                           :lowtag other-pointer-lowtag
333                           :widetag complex-single-float-widetag)
334   (real :c-type "float")
335   (imag :c-type "float"))
336
337 (define-primitive-object (complex-double-float
338                           :lowtag other-pointer-lowtag
339                           :widetag complex-double-float-widetag)
340   #!-x86-64 (filler)
341   (real :c-type "double" :length #!-x86-64 2 #!+x86-64 1)
342   (imag :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
343
344 #!+(and sb-lutex sb-thread)
345 (define-primitive-object (lutex
346                           :lowtag other-pointer-lowtag
347                           :widetag lutex-widetag
348                           :alloc-trans %make-lutex)
349   (gen :c-type "long" :length 1)
350   (live :c-type "long" :length 1)
351   (next :c-type "struct lutex *" :length 1)
352   (prev :c-type "struct lutex *" :length 1)
353   (mutex :c-type "pthread_mutex_t *"
354          :length 1)
355   (mutexattr :c-type "pthread_mutexattr_t *"
356              :length 1)
357   (condition-variable :c-type "pthread_cond_t *"
358                       :length 1))
359
360 ;;; this isn't actually a lisp object at all, it's a c structure that lives
361 ;;; in c-land.  However, we need sight of so many parts of it from Lisp that
362 ;;; it makes sense to define it here anyway, so that the GENESIS machinery
363 ;;; can take care of maintaining Lisp and C versions.
364 ;;; Hence the even-fixnum lowtag just so we don't get odd(sic) numbers
365 ;;; added to the slot offsets
366 (define-primitive-object (thread :lowtag even-fixnum-lowtag)
367   ;; no_tls_value_marker is borrowed very briefly at thread startup to
368   ;; pass the address of initial-function into new_thread_trampoline.
369   ;; tls[0] = NO_TLS_VALUE_MARKER_WIDETAG because a the tls index slot
370   ;; of a symbol is initialized to zero
371   (no-tls-value-marker)
372   (os-thread :c-type "volatile os_thread_t")
373   (binding-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
374   (binding-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
375   (control-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
376   (control-stack-end :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
377   (alien-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
378   (alien-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
379   #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
380   (this :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
381   (prev :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
382   (next :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
383   ;; starting, running, suspended, dead
384   (state :c-type "volatile lispobj")
385   (tls-cookie)                          ;  on x86, the LDT index
386   #!+(or x86 x86-64) (pseudo-atomic-bits)
387   (interrupt-data :c-type "struct interrupt_data *"
388                   :length #!+alpha 2 #!-alpha 1)
389   (stepping)
390   (interrupt-contexts :c-type "os_context_t *" :rest-p t))