590d146c297a1b8d710b15889f2ffcb3db27f9b0
[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 (define-primitive-object (single-float :lowtag other-pointer-lowtag
70                                        :widetag single-float-widetag)
71   (value :c-type "float"))
72
73 (define-primitive-object (double-float :lowtag other-pointer-lowtag
74                                        :widetag double-float-widetag)
75   (filler)
76   (value :c-type "double" :length 2))
77
78 #!+long-float
79 (define-primitive-object (long-float :lowtag other-pointer-lowtag
80                                      :widetag long-float-widetag)
81   #!+sparc (filler)
82   (value :c-type "long double" :length #!+x86 3 #!+sparc 4))
83
84 (define-primitive-object (complex :type complex
85                                   :lowtag other-pointer-lowtag
86                                   :widetag complex-widetag
87                                   :alloc-trans %make-complex)
88   (real :type real
89         :ref-known (flushable movable)
90         :ref-trans %realpart
91         :init :arg)
92   (imag :type real
93         :ref-known (flushable movable)
94         :ref-trans %imagpart
95         :init :arg))
96
97 (define-primitive-object (array :lowtag other-pointer-lowtag
98                                 :widetag t)
99   ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
100   ;; VECTOR -- see SHRINK-VECTOR.
101   (fill-pointer :type index
102                 :ref-trans %array-fill-pointer
103                 :ref-known (flushable foldable)
104                 :set-trans (setf %array-fill-pointer)
105                 :set-known (unsafe))
106   (fill-pointer-p :type (member t nil)
107                   :ref-trans %array-fill-pointer-p
108                   :ref-known (flushable foldable)
109                   :set-trans (setf %array-fill-pointer-p)
110                   :set-known (unsafe))
111   (elements :type index
112             :ref-trans %array-available-elements
113             :ref-known (flushable foldable)
114             :set-trans (setf %array-available-elements)
115             :set-known (unsafe))
116   (data :type array
117         :ref-trans %array-data-vector
118         :ref-known (flushable foldable)
119         :set-trans (setf %array-data-vector)
120         :set-known (unsafe))
121   (displacement :type (or index null)
122                 :ref-trans %array-displacement
123                 :ref-known (flushable foldable)
124                 :set-trans (setf %array-displacement)
125                 :set-known (unsafe))
126   (displaced-p :type (member t nil)
127                :ref-trans %array-displaced-p
128                :ref-known (flushable foldable)
129                :set-trans (setf %array-displaced-p)
130                :set-known (unsafe))
131   (dimensions :rest-p t))
132
133 (define-primitive-object (vector :type vector
134                                  :lowtag other-pointer-lowtag
135                                  :widetag t)
136   ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
137   ;; VECTOR -- see SHRINK-VECTOR.
138   (length :ref-trans sb!c::vector-length
139           :type index)
140   (data :rest-p t :c-type #!-alpha "unsigned long" #!+alpha "u32"))
141
142 (define-primitive-object (code :type code-component
143                                :lowtag other-pointer-lowtag
144                                :widetag t)
145   (code-size :type index
146              :ref-known (flushable movable)
147              :ref-trans %code-code-size)
148   (entry-points :type (or function null)
149                 :ref-known (flushable)
150                 :ref-trans %code-entry-points
151                 :set-known (unsafe)
152                 :set-trans (setf %code-entry-points))
153   (debug-info :type t
154               :ref-known (flushable)
155               :ref-trans %code-debug-info
156               :set-known (unsafe)
157               :set-trans (setf %code-debug-info))
158   (trace-table-offset)
159   (constants :rest-p t))
160
161 (define-primitive-object (fdefn :type fdefn
162                                 :lowtag other-pointer-lowtag
163                                 :widetag fdefn-widetag)
164   (name :ref-trans fdefn-name)
165   (fun :type (or function null) :ref-trans fdefn-fun)
166   (raw-addr :c-type #!-alpha "char *" #!+alpha "u32"))
167
168 ;;; a simple function (as opposed to hairier things like closures
169 ;;; which are also subtypes of Common Lisp's FUNCTION type)
170 (define-primitive-object (simple-fun :type function
171                                      :lowtag fun-pointer-lowtag
172                                      :widetag simple-fun-header-widetag)
173   #!-x86 (self :ref-trans %simple-fun-self
174                :set-trans (setf %simple-fun-self))
175   #!+x86 (self
176           ;; KLUDGE: There's no :SET-KNOWN, :SET-TRANS, :REF-KNOWN, or
177           ;; :REF-TRANS here in this case. Instead, there's separate
178           ;; DEFKNOWN/DEFINE-VOP/DEFTRANSFORM stuff in
179           ;; compiler/x86/system.lisp to define and declare them by
180           ;; hand. I don't know why this is, but that's (basically)
181           ;; the way it was done in CMU CL, and it works. (It's not
182           ;; exactly the same way it was done in CMU CL in that CMU
183           ;; CL's allows duplicate DEFKNOWNs, blithely overwriting any
184           ;; previous data associated with the previous DEFKNOWN, and
185           ;; that property was used to mask the definitions here. In
186           ;; SBCL as of 0.6.12.64 that's not allowed -- too confusing!
187           ;; -- so we have to explicitly suppress the DEFKNOWNish
188           ;; stuff here in order to allow this old hack to work in the
189           ;; new world. -- WHN 2001-08-82
190           )
191   (next :type (or function null)
192         :ref-known (flushable)
193         :ref-trans %simple-fun-next
194         :set-known (unsafe)
195         :set-trans (setf %simple-fun-next))
196   (name :ref-known (flushable)
197         :ref-trans %simple-fun-name
198         :set-known (unsafe)
199         :set-trans (setf %simple-fun-name))
200   (arglist :type list
201            :ref-known (flushable)
202            :ref-trans %simple-fun-arglist
203            :set-known (unsafe)
204            :set-trans (setf %simple-fun-arglist))
205   (type :ref-known (flushable)
206         :ref-trans %simple-fun-type
207         :set-known (unsafe)
208         :set-trans (setf %simple-fun-type))
209   ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
210   #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
211   (debug-fun :ref-known (flushable)
212              :ref-trans %simple-fun-debug-fun
213              :set-known (unsafe)
214              :set-trans (setf %simple-fun-debug-fun))
215   (code :rest-p t :c-type "unsigned char"))
216
217 (define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
218   (return-point :c-type "unsigned char" :rest-p t))
219
220 (define-primitive-object (closure :lowtag fun-pointer-lowtag
221                                   :widetag closure-header-widetag)
222   (fun :init :arg :ref-trans %closure-fun)
223   (info :rest-p t))
224
225 (define-primitive-object (funcallable-instance
226                           :lowtag fun-pointer-lowtag
227                           :widetag funcallable-instance-header-widetag
228                           :alloc-trans %make-funcallable-instance)
229   #!-x86
230   (fun
231    :ref-known (flushable) :ref-trans %funcallable-instance-fun
232    :set-known (unsafe) :set-trans (setf %funcallable-instance-fun))
233   #!+x86
234   (fun
235    :ref-known (flushable) :ref-trans %funcallable-instance-fun
236    ;; KLUDGE: There's no :SET-KNOWN or :SET-TRANS in this case.
237    ;; Instead, later in compiler/x86/system.lisp there's a separate
238    ;; DEFKNOWN for (SETF %FUNCALLABLE-INSTANCE-FUN), and a weird
239    ;; unexplained DEFTRANSFORM from (SETF %SIMPLE-FUN-INSTANCE-FUN)
240    ;; into (SETF %SIMPLE-FUN-SELF). The #!+X86 wrapped around this case
241    ;; is a literal translation of the old CMU CL implementation into
242    ;; the new world of sbcl-0.6.12.63, where multiple DEFKNOWNs for
243    ;; the same operator cause an error (instead of silently deleting
244    ;; all information associated with the old DEFKNOWN, as before).
245    ;; It's definitely not very clean, with too many #!+ conditionals and
246    ;; too little documentation, but I have more urgent things to
247    ;; clean up right now, so I've just left it as a literal
248    ;; translation without trying to fix it. -- WHN 2001-08-02
249    )
250   (lexenv :ref-known (flushable) :ref-trans %funcallable-instance-lexenv
251           :set-known (unsafe) :set-trans (setf %funcallable-instance-lexenv))
252   (layout :init :arg
253           :ref-known (flushable) :ref-trans %funcallable-instance-layout
254           :set-known (unsafe) :set-trans (setf %funcallable-instance-layout))
255   (info :rest-p t))
256
257 (define-primitive-object (value-cell :lowtag other-pointer-lowtag
258                                      :widetag value-cell-header-widetag
259                                      :alloc-trans make-value-cell)
260   (value :set-trans value-cell-set
261          :set-known (unsafe)
262          :ref-trans value-cell-ref
263          :ref-known (flushable)
264          :init :arg))
265
266 #!+alpha
267 (define-primitive-object (sap :lowtag other-pointer-lowtag
268                               :widetag sap-widetag)
269   (padding)
270   (pointer :c-type "char *" :length 2))
271
272 #!-alpha
273 (define-primitive-object (sap :lowtag other-pointer-lowtag
274                               :widetag sap-widetag)
275   (pointer :c-type "char *"))
276
277
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 sb!c::%weak-pointer-value :ref-known (flushable)
283          :init :arg)
284   (broken :type (member t nil)
285           :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
286           :init :null)
287   (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
288
289 ;;;; other non-heap data blocks
290
291 (define-primitive-object (binding)
292   value
293   symbol)
294
295 (define-primitive-object (unwind-block)
296   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
297   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
298   #!-x86 current-code
299   entry-pc)
300
301 (define-primitive-object (catch-block)
302   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
303   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
304   #!-x86 current-code
305   entry-pc
306   tag
307   (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32")
308   size)
309
310 ;;; (For an explanation of this, see the comments at the definition of
311 ;;; KLUDGE-NONDETERMINISTIC-CATCH-BLOCK-SIZE.)
312 (aver (= kludge-nondeterministic-catch-block-size catch-block-size))
313 \f
314 ;;;; symbols
315
316 #!+x86
317 (defknown symbol-hash (symbol) (integer 0 #.sb!xc:most-positive-fixnum)
318   (flushable movable))
319
320 (define-primitive-object (symbol :lowtag other-pointer-lowtag
321                                  :widetag symbol-header-widetag
322                                  #!-x86 :alloc-trans #!-x86 make-symbol)
323
324   ;; Beware when changing this definition.  NIL-the-symbol is defined
325   ;; using this layout, and NIL-the-end-of-list-marker is the cons 
326   ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
327   ;; (conses have no header).  Careful selection of lowtags ensures
328   ;; that the same pointer can be used for both purposes:
329   ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
330   ;; subtract 3 from (sb-kernel:get-lisp-obj-address 'NIL) you get the
331   ;; first data slot, and if you subtract 7 you get a symbol header.
332
333   (value :init :unbound)                ;also the CAR of NIL-as-end-of-list
334   (hash)                                ;the CDR of NIL-as-end-of-list
335
336   (plist :ref-trans symbol-plist
337          :set-trans %set-symbol-plist
338          :init :null)
339   (name :ref-trans symbol-name :init :arg)
340   (package :ref-trans symbol-package
341            :set-trans %set-symbol-package
342            :init :null)
343   #!+sb-thread (tls-index))
344
345 (define-primitive-object (complex-single-float
346                           :lowtag other-pointer-lowtag
347                           :widetag complex-single-float-widetag)
348   (real :c-type "float")
349   (imag :c-type "float"))
350
351 (define-primitive-object (complex-double-float
352                           :lowtag other-pointer-lowtag
353                           :widetag complex-double-float-widetag)
354   (filler)
355   (real :c-type "double" :length 2)
356   (imag :c-type "double" :length 2))
357
358 #!+long-float
359 (define-primitive-object (complex-long-float
360                           :lowtag other-pointer-lowtag
361                           :widetag complex-long-float-widetag)
362   #!+sparc (filler)
363   (real :c-type "long double" :length #!+x86 3 #!+sparc 4)
364   (imag :c-type "long double" :length #!+x86 3 #!+sparc 4))
365
366 ;;; this isn't actually a lisp object at all, it's a c structure that lives
367 ;;; in c-land.  However, we need sight of so many parts of it from Lisp that
368 ;;; it makes sense to define it here anyway, so that the GENESIS machinery
369 ;;; can take care of maintaining Lisp and C versions.
370 ;;; Hence the even-fixnum lowtag just so we don't get odd(sic) numbers 
371 ;;; added to the slot offsets
372 (define-primitive-object (thread :lowtag even-fixnum-lowtag)
373   ;; unbound_marker is borrowed very briefly at thread startup to 
374   ;; pass the address of initial-function into new_thread_trampoline 
375   (unbound-marker :init :unbound) ; tls[0] = UNBOUND_MARKER_WIDETAG 
376   (pid :c-type "pid_t")
377   (binding-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
378   (binding-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
379   (control-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
380   (control-stack-end :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
381   (alien-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
382   (alien-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
383   #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
384   (tls-cookie)                          ;  on x86, the LDT index 
385   (this :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
386   (next :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
387   (state)                               ; running, stopping, stopped
388   #!+x86 (pseudo-atomic-atomic)
389   #!+x86 (pseudo-atomic-interrupted)
390   (interrupt-data :c-type "struct interrupt_data *" 
391                   :length #!+alpha 2 #!-alpha 1)
392   (interrupt-contexts :c-type "os_context_t *" :rest-p t))