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