0.pre7.109:
[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 :ref-known (flushable)
197            :ref-trans %simple-fun-arglist
198            :set-known (unsafe)
199            :set-trans (setf %simple-fun-arglist))
200   (type :ref-known (flushable)
201         :ref-trans %simple-fun-type
202         :set-known (unsafe)
203         :set-trans (setf %simple-fun-type))
204   ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
205   #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
206   (debug-fun :ref-known (flushable)
207              :ref-trans %simple-fun-debug-fun
208              :set-known (unsafe)
209              :set-trans (setf %simple-fun-debug-fun))
210   (code :rest-p t :c-type "unsigned char"))
211
212 (define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
213   (return-point :c-type "unsigned char" :rest-p t))
214
215 (define-primitive-object (closure :lowtag fun-pointer-lowtag
216                                   :widetag closure-header-widetag)
217   (fun :init :arg :ref-trans %closure-fun)
218   (info :rest-p t))
219
220 (define-primitive-object (funcallable-instance
221                           :lowtag fun-pointer-lowtag
222                           :widetag funcallable-instance-header-widetag
223                           :alloc-trans %make-funcallable-instance)
224   #!-x86
225   (fun
226    :ref-known (flushable) :ref-trans %funcallable-instance-fun
227    :set-known (unsafe) :set-trans (setf %funcallable-instance-fun))
228   #!+x86
229   (fun
230    :ref-known (flushable) :ref-trans %funcallable-instance-fun
231    ;; KLUDGE: There's no :SET-KNOWN or :SET-TRANS in this case.
232    ;; Instead, later in compiler/x86/system.lisp there's a separate
233    ;; DEFKNOWN for (SETF %FUNCALLABLE-INSTANCE-FUN), and a weird
234    ;; unexplained DEFTRANSFORM from (SETF %SIMPLE-FUN-INSTANCE-FUN)
235    ;; into (SETF %SIMPLE-FUN-SELF). The #!+X86 wrapped around this case
236    ;; is a literal translation of the old CMU CL implementation into
237    ;; the new world of sbcl-0.6.12.63, where multiple DEFKNOWNs for
238    ;; the same operator cause an error (instead of silently deleting
239    ;; all information associated with the old DEFKNOWN, as before).
240    ;; It's definitely not very clean, with too many #!+ conditionals and
241    ;; too little documentation, but I have more urgent things to
242    ;; clean up right now, so I've just left it as a literal
243    ;; translation without trying to fix it. -- WHN 2001-08-02
244    )
245   (lexenv :ref-known (flushable) :ref-trans %funcallable-instance-lexenv
246           :set-known (unsafe) :set-trans (setf %funcallable-instance-lexenv))
247   (layout :init :arg
248           :ref-known (flushable) :ref-trans %funcallable-instance-layout
249           :set-known (unsafe) :set-trans (setf %funcallable-instance-layout))
250   (info :rest-p t))
251
252 (define-primitive-object (value-cell :lowtag other-pointer-lowtag
253                                      :widetag value-cell-header-widetag
254                                      :alloc-trans make-value-cell)
255   (value :set-trans value-cell-set
256          :set-known (unsafe)
257          :ref-trans value-cell-ref
258          :ref-known (flushable)
259          :init :arg))
260
261 #!+alpha
262 (define-primitive-object (sap :lowtag other-pointer-lowtag
263                               :widetag sap-widetag)
264   (padding)
265   (pointer :c-type "char *" :length 2))
266
267 #!-alpha
268 (define-primitive-object (sap :lowtag other-pointer-lowtag
269                               :widetag sap-widetag)
270   (pointer :c-type "char *"))
271
272
273 (define-primitive-object (weak-pointer :type weak-pointer
274                                        :lowtag other-pointer-lowtag
275                                        :widetag weak-pointer-widetag
276                                        :alloc-trans make-weak-pointer)
277   (value :ref-trans sb!c::%weak-pointer-value :ref-known (flushable)
278          :init :arg)
279   (broken :type (member t nil)
280           :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
281           :init :null)
282   (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
283
284 ;;;; other non-heap data blocks
285
286 (define-primitive-object (binding)
287   value
288   symbol)
289
290 (define-primitive-object (unwind-block)
291   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
292   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
293   #!-x86 current-code
294   entry-pc)
295
296 (define-primitive-object (catch-block)
297   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
298   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
299   #!-x86 current-code
300   entry-pc
301   tag
302   (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32")
303   size)
304
305 ;;; (For an explanation of this, see the comments at the definition of
306 ;;; KLUDGE-NONDETERMINISTIC-CATCH-BLOCK-SIZE.)
307 (aver (= kludge-nondeterministic-catch-block-size catch-block-size))
308 \f
309 ;;;; symbols
310
311 #!+x86
312 (defknown symbol-hash (symbol) (integer 0 #.*target-most-positive-fixnum*)
313   (flushable movable))
314
315 (define-primitive-object (symbol :lowtag other-pointer-lowtag
316                                  :widetag symbol-header-widetag
317                                  #!-x86 :alloc-trans #!-x86 make-symbol)
318   (value :set-trans %set-symbol-value
319          :init :unbound)
320   #!+x86 (hash)
321   (plist :ref-trans symbol-plist
322          :set-trans %set-symbol-plist
323          :init :null)
324   (name :ref-trans symbol-name :init :arg)
325   (package :ref-trans symbol-package
326            :set-trans %set-symbol-package
327            :init :null))
328
329 (define-primitive-object (complex-single-float
330                           :lowtag other-pointer-lowtag
331                           :widetag complex-single-float-widetag)
332   (real :c-type "float")
333   (imag :c-type "float"))
334
335 (define-primitive-object (complex-double-float
336                           :lowtag other-pointer-lowtag
337                           :widetag complex-double-float-widetag)
338   (filler)
339   (real :c-type "double" :length 2)
340   (imag :c-type "double" :length 2))
341
342 #!+long-float
343 (define-primitive-object (complex-long-float
344                           :lowtag other-pointer-lowtag
345                           :widetag complex-long-float-widetag)
346   #!+sparc (filler)
347   (real :c-type "long double" :length #!+x86 3 #!+sparc 4)
348   (imag :c-type "long double" :length #!+x86 3 #!+sparc 4))
349