0.9.13.22:
[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   ;; This SELF slot needs explanation.
225   ;;
226   ;; Ordinary closures did not need this slot before version 0.9.3.xx,
227   ;; as the closure object was already in some dedicated register --
228   ;; EAX/RAX on x86(-64), reg_LEXENV on register-rich platforms -- and
229   ;; consequently setting up the environment (from the INFO slot,
230   ;; below) was easy.
231   ;;
232   ;; However, it is not easy to support calling FUNCALLABLE-INSTANCEs
233   ;; in the same way; in a FUNCALLABLE-INSTANCE, there are
234   ;; conceptually two variable-length data areas: the closure
235   ;; environment, if any, and the slots of the instance.
236   ;;
237   ;; Until sbcl-0.9.3.xx, it was required that closures to be set as a
238   ;; FUNCALLABLE-INSTANCE-FUNCTION be defined using the magical
239   ;; keyword SB-KERNEL:INSTANCE-LAMBDA, rather than ordinary LAMBDA;
240   ;; this caused an extra indirection to be compiled into the closure
241   ;; code to load the closure from the FUNCALLABLE-INSTANCE-LEXENV
242   ;; slot before setting up the environment for the function body.
243   ;; Failure to obey this protocol yielded confusing error messages as
244   ;; either INSTANCE-LAMBDAs tried to dereference environments that
245   ;; weren't there, or ordinary LAMBDAs got hold of the LAYOUT and
246   ;; LEXENV slots of a FUNCALLABLE-INSTANCE.
247   ;;
248   ;; By adding this SELF slot, which is at the same offset in a
249   ;; regular CLOSURE as the LEXENV slot is in a FUNCALLABLE-INSTANCE,
250   ;; we enable the extra indirection (VOP FUNCALLABLE-INSTANCE-LEXENV,
251   ;; in src/compiler/ir2tran.lisp) to be compiled unconditionally
252   ;; (provided that we set this slot to the closure object itself).
253   ;; Relative to the code before, this adds a word to the space
254   ;; requirements of a closure, and one instruction (a memory fetch)
255   ;; to the body of a closure function.
256   ;;
257   ;; There are potentially other implementation strategies which would
258   ;; remove the need for this extra indirection in regular closures,
259   ;; such as setting up a trampoline for funcallable instances (though
260   ;; it was not clear to me that there are enough registers free in
261   ;; the x86 backend to permit this).  This indirection should not be
262   ;; too disastrous, given that for regular closures the fetch is from
263   ;; memory which is known to be active.
264   ;;
265   ;; CSR, 2005-08-05
266   (self) ; KLUDGE (see above comment)
267   (info :rest-p t))
268
269 (define-primitive-object (funcallable-instance
270                           :lowtag fun-pointer-lowtag
271                           :widetag funcallable-instance-header-widetag
272                           :alloc-trans %make-funcallable-instance)
273   #!-(or x86 x86-64)
274   (fun
275    :ref-known (flushable) :ref-trans %funcallable-instance-fun
276    :set-known (unsafe) :set-trans (setf %funcallable-instance-fun))
277   #!+(or x86 x86-64)
278   (fun
279    :ref-known (flushable) :ref-trans %funcallable-instance-fun
280    ;; KLUDGE: There's no :SET-KNOWN or :SET-TRANS in this case.
281    ;; Instead, later in compiler/x86/system.lisp there's a separate
282    ;; DEFKNOWN for (SETF %FUNCALLABLE-INSTANCE-FUN), and a weird
283    ;; unexplained DEFTRANSFORM from (SETF %SIMPLE-FUN-INSTANCE-FUN)
284    ;; into (SETF %SIMPLE-FUN-SELF). The #!+X86 wrapped around this case
285    ;; is a literal translation of the old CMU CL implementation into
286    ;; the new world of sbcl-0.6.12.63, where multiple DEFKNOWNs for
287    ;; the same operator cause an error (instead of silently deleting
288    ;; all information associated with the old DEFKNOWN, as before).
289    ;; It's definitely not very clean, with too many #!+ conditionals and
290    ;; too little documentation, but I have more urgent things to
291    ;; clean up right now, so I've just left it as a literal
292    ;; translation without trying to fix it. -- WHN 2001-08-02
293    )
294   (lexenv :ref-known (flushable) :ref-trans %funcallable-instance-lexenv
295           :set-known (unsafe) :set-trans (setf %funcallable-instance-lexenv))
296   (info :rest-p t))
297
298 (define-primitive-object (value-cell :lowtag other-pointer-lowtag
299                                      :widetag value-cell-header-widetag
300                                      :alloc-trans make-value-cell)
301   (value :set-trans value-cell-set
302          :set-known (unsafe)
303          :ref-trans value-cell-ref
304          :ref-known (flushable)
305          :init :arg))
306
307 #!+alpha
308 (define-primitive-object (sap :lowtag other-pointer-lowtag
309                               :widetag sap-widetag)
310   (padding)
311   (pointer :c-type "char *" :length 2))
312
313 #!-alpha
314 (define-primitive-object (sap :lowtag other-pointer-lowtag
315                               :widetag sap-widetag)
316   (pointer :c-type "char *"))
317
318
319 (define-primitive-object (weak-pointer :type weak-pointer
320                                        :lowtag other-pointer-lowtag
321                                        :widetag weak-pointer-widetag
322                                        :alloc-trans make-weak-pointer)
323   (value :ref-trans sb!c::%weak-pointer-value :ref-known (flushable)
324          :init :arg)
325   (broken :type (member t nil)
326           :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
327           :init :null)
328   (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
329
330 ;;;; other non-heap data blocks
331
332 (define-primitive-object (binding)
333   value
334   symbol)
335
336 (define-primitive-object (unwind-block)
337   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
338   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
339   #!-(or x86 x86-64) current-code
340   entry-pc)
341
342 (define-primitive-object (catch-block)
343   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
344   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
345   #!-(or x86 x86-64) current-code
346   entry-pc
347   tag
348   (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32")
349   size)
350
351 ;;; (For an explanation of this, see the comments at the definition of
352 ;;; KLUDGE-NONDETERMINISTIC-CATCH-BLOCK-SIZE.)
353 (aver (= kludge-nondeterministic-catch-block-size catch-block-size))
354 \f
355 ;;;; symbols
356
357 (define-primitive-object (symbol :lowtag other-pointer-lowtag
358                                  :widetag symbol-header-widetag
359                                  :alloc-trans %make-symbol)
360
361   ;; Beware when changing this definition.  NIL-the-symbol is defined
362   ;; using this layout, and NIL-the-end-of-list-marker is the cons
363   ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
364   ;; (conses have no header).  Careful selection of lowtags ensures
365   ;; that the same pointer can be used for both purposes:
366   ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
367   ;; subtract 3 from (SB-KERNEL:GET-LISP-OBJ-ADDRESS 'NIL) you get the
368   ;; first data slot, and if you subtract 7 you get a symbol header.
369
370   ;; also the CAR of NIL-as-end-of-list
371   (value :init :unbound :ref-known (flushable) :ref-trans symbol-global-value)
372   ;; also the CDR of NIL-as-end-of-list.  Its reffer needs special
373   ;; care for this reason, as hash values must be fixnums.
374   (hash :set-trans %set-symbol-hash)
375
376   (plist :ref-trans symbol-plist
377          :set-trans %set-symbol-plist
378          :init :null)
379   (name :ref-trans symbol-name :init :arg)
380   (package :ref-trans symbol-package
381            :set-trans %set-symbol-package
382            :init :null)
383   #!+sb-thread (tls-index :ref-known (flushable) :ref-trans symbol-tls-index))
384
385 (define-primitive-object (complex-single-float
386                           :lowtag other-pointer-lowtag
387                           :widetag complex-single-float-widetag)
388   (real :c-type "float")
389   (imag :c-type "float"))
390
391 (define-primitive-object (complex-double-float
392                           :lowtag other-pointer-lowtag
393                           :widetag complex-double-float-widetag)
394   #!-x86-64 (filler)
395   (real :c-type "double" :length #!-x86-64 2 #!+x86-64 1)
396   (imag :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
397
398 #!+(and sb-thread sb-lutex)
399 (define-primitive-object (lutex
400                           :lowtag other-pointer-lowtag
401                           :widetag lutex-widetag
402                           :alloc-trans %make-lutex)
403   (gen :c-type "long" :length 1)
404   (live :c-type "long" :length 1)
405   (next :c-type "struct lutex *" :length 1)
406   (prev :c-type "struct lutex *" :length 1)
407   (mutex :c-type "pthread_mutex_t *"
408          :length 1)
409   (condition-variable :c-type "pthread_cond_t *"
410                       :length 1))
411
412 ;;; this isn't actually a lisp object at all, it's a c structure that lives
413 ;;; in c-land.  However, we need sight of so many parts of it from Lisp that
414 ;;; it makes sense to define it here anyway, so that the GENESIS machinery
415 ;;; can take care of maintaining Lisp and C versions.
416 ;;; Hence the even-fixnum lowtag just so we don't get odd(sic) numbers
417 ;;; added to the slot offsets
418 (define-primitive-object (thread :lowtag even-fixnum-lowtag)
419   ;; no_tls_value_marker is borrowed very briefly at thread startup to
420   ;; pass the address of initial-function into new_thread_trampoline.
421   ;; tls[0] = NO_TLS_VALUE_MARKER_WIDETAG because a the tls index slot
422   ;; of a symbol is initialized to zero
423   (no-tls-value-marker)
424   (os-thread :c-type "volatile os_thread_t")
425   (binding-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
426   (binding-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
427   (control-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
428   (control-stack-end :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
429   (alien-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
430   (alien-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
431   #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
432   (this :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
433   (prev :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
434   (next :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
435   ;; starting, running, suspended, dead
436   (state :c-type "volatile lispobj")
437   (tls-cookie)                          ;  on x86, the LDT index
438   #!+(or x86 x86-64) (pseudo-atomic-atomic)
439   #!+(or x86 x86-64) (pseudo-atomic-interrupted)
440   (interrupt-data :c-type "struct interrupt_data *"
441                   :length #!+alpha 2 #!-alpha 1)
442   (interrupt-contexts :c-type "os_context_t *" :rest-p t))