1.0.7.19: SB-EXT:COMPARE-AND-SWAP
[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        :cas-trans %compare-and-swap-car)
45   (cdr :ref-trans cdr :set-trans sb!c::%rplacd :init :arg
46        :cas-trans %compare-and-swap-cdr))
47
48 (define-primitive-object (instance :lowtag instance-pointer-lowtag
49                                    :widetag instance-header-widetag
50                                    :alloc-trans %make-instance)
51   (slots :rest-p t))
52
53 (define-primitive-object (bignum :lowtag other-pointer-lowtag
54                                  :widetag bignum-widetag
55                                  :alloc-trans sb!bignum::%allocate-bignum)
56   (digits :rest-p t :c-type #!-alpha "long" #!+alpha "u32"))
57
58 (define-primitive-object (ratio :type ratio
59                                 :lowtag other-pointer-lowtag
60                                 :widetag ratio-widetag
61                                 :alloc-trans %make-ratio)
62   (numerator :type integer
63              :ref-known (flushable movable)
64              :ref-trans %numerator
65              :init :arg)
66   (denominator :type integer
67                :ref-known (flushable movable)
68                :ref-trans %denominator
69                :init :arg))
70
71 #!+#.(cl:if (cl:= sb!vm:n-word-bits 32) '(and) '(or))
72 (define-primitive-object (single-float :lowtag other-pointer-lowtag
73                                        :widetag single-float-widetag)
74   (value :c-type "float"))
75
76 (define-primitive-object (double-float :lowtag other-pointer-lowtag
77                                        :widetag double-float-widetag)
78   #!-x86-64 (filler)
79   (value :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
80
81 #!+long-float
82 (define-primitive-object (long-float :lowtag other-pointer-lowtag
83                                      :widetag long-float-widetag)
84   #!+sparc (filler)
85   (value :c-type "long double" :length #!+x86 3 #!+sparc 4))
86
87 (define-primitive-object (complex :type complex
88                                   :lowtag other-pointer-lowtag
89                                   :widetag complex-widetag
90                                   :alloc-trans %make-complex)
91   (real :type real
92         :ref-known (flushable movable)
93         :ref-trans %realpart
94         :init :arg)
95   (imag :type real
96         :ref-known (flushable movable)
97         :ref-trans %imagpart
98         :init :arg))
99
100 (define-primitive-object (array :lowtag other-pointer-lowtag
101                                 :widetag t)
102   ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
103   ;; VECTOR -- see SHRINK-VECTOR.
104   (fill-pointer :type index
105                 :ref-trans %array-fill-pointer
106                 :ref-known (flushable foldable)
107                 :set-trans (setf %array-fill-pointer)
108                 :set-known (unsafe))
109   (fill-pointer-p :type (member t nil)
110                   :ref-trans %array-fill-pointer-p
111                   :ref-known (flushable foldable)
112                   :set-trans (setf %array-fill-pointer-p)
113                   :set-known (unsafe))
114   (elements :type index
115             :ref-trans %array-available-elements
116             :ref-known (flushable foldable)
117             :set-trans (setf %array-available-elements)
118             :set-known (unsafe))
119   (data :type array
120         :ref-trans %array-data-vector
121         :ref-known (flushable foldable)
122         :set-trans (setf %array-data-vector)
123         :set-known (unsafe))
124   (displacement :type (or index null)
125                 :ref-trans %array-displacement
126                 :ref-known (flushable foldable)
127                 :set-trans (setf %array-displacement)
128                 :set-known (unsafe))
129   (displaced-p :type (member t nil)
130                :ref-trans %array-displaced-p
131                :ref-known (flushable foldable)
132                :set-trans (setf %array-displaced-p)
133                :set-known (unsafe))
134   (dimensions :rest-p t))
135
136 (define-primitive-object (vector :type vector
137                                  :lowtag other-pointer-lowtag
138                                  :widetag t)
139   ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
140   ;; VECTOR -- see SHRINK-VECTOR.
141   (length :ref-trans sb!c::vector-length
142           :type index)
143   (data :rest-p t :c-type #!-alpha "unsigned long" #!+alpha "u32"))
144
145 (define-primitive-object (code :type code-component
146                                :lowtag other-pointer-lowtag
147                                :widetag t)
148   (code-size :type index
149              :ref-known (flushable movable)
150              :ref-trans %code-code-size)
151   (entry-points :type (or function null)
152                 :ref-known (flushable)
153                 :ref-trans %code-entry-points
154                 :set-known (unsafe)
155                 :set-trans (setf %code-entry-points))
156   (debug-info :type t
157               :ref-known (flushable)
158               :ref-trans %code-debug-info
159               :set-known (unsafe)
160               :set-trans (setf %code-debug-info))
161   (trace-table-offset)
162   (constants :rest-p t))
163
164 (define-primitive-object (fdefn :type fdefn
165                                 :lowtag other-pointer-lowtag
166                                 :widetag fdefn-widetag)
167   (name :ref-trans fdefn-name)
168   (fun :type (or function null) :ref-trans fdefn-fun)
169   (raw-addr :c-type #!-alpha "char *" #!+alpha "u32"))
170
171 ;;; a simple function (as opposed to hairier things like closures
172 ;;; which are also subtypes of Common Lisp's FUNCTION type)
173 (define-primitive-object (simple-fun :type function
174                                      :lowtag fun-pointer-lowtag
175                                      :widetag simple-fun-header-widetag)
176   #!-(or x86 x86-64) (self :ref-trans %simple-fun-self
177                :set-trans (setf %simple-fun-self))
178   #!+(or x86 x86-64) (self
179           ;; KLUDGE: There's no :SET-KNOWN, :SET-TRANS, :REF-KNOWN, or
180           ;; :REF-TRANS here in this case. Instead, there's separate
181           ;; DEFKNOWN/DEFINE-VOP/DEFTRANSFORM stuff in
182           ;; compiler/x86/system.lisp to define and declare them by
183           ;; hand. I don't know why this is, but that's (basically)
184           ;; the way it was done in CMU CL, and it works. (It's not
185           ;; exactly the same way it was done in CMU CL in that CMU
186           ;; CL's allows duplicate DEFKNOWNs, blithely overwriting any
187           ;; previous data associated with the previous DEFKNOWN, and
188           ;; that property was used to mask the definitions here. In
189           ;; SBCL as of 0.6.12.64 that's not allowed -- too confusing!
190           ;; -- so we have to explicitly suppress the DEFKNOWNish
191           ;; stuff here in order to allow this old hack to work in the
192           ;; new world. -- WHN 2001-08-82
193           )
194   (next :type (or function null)
195         :ref-known (flushable)
196         :ref-trans %simple-fun-next
197         :set-known (unsafe)
198         :set-trans (setf %simple-fun-next))
199   (name :ref-known (flushable)
200         :ref-trans %simple-fun-name
201         :set-known (unsafe)
202         :set-trans (setf %simple-fun-name))
203   (arglist :type list
204            :ref-known (flushable)
205            :ref-trans %simple-fun-arglist
206            :set-known (unsafe)
207            :set-trans (setf %simple-fun-arglist))
208   (type :ref-known (flushable)
209         :ref-trans %simple-fun-type
210         :set-known (unsafe)
211         :set-trans (setf %simple-fun-type))
212   (xrefs :init :null
213          :ref-trans %simple-fun-xrefs
214          :ref-known (flushable)
215          :set-trans (setf %simple-fun-xrefs)
216          :set-known ())
217   ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
218   #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
219   (debug-fun :ref-known (flushable)
220              :ref-trans %simple-fun-debug-fun
221              :set-known (unsafe)
222              :set-trans (setf %simple-fun-debug-fun))
223   (code :rest-p t :c-type "unsigned char"))
224
225 (define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
226   (return-point :c-type "unsigned char" :rest-p t))
227
228 (define-primitive-object (closure :lowtag fun-pointer-lowtag
229                                   :widetag closure-header-widetag)
230   (fun :init :arg :ref-trans %closure-fun)
231   (info :rest-p t))
232
233 (define-primitive-object (funcallable-instance
234                           :lowtag fun-pointer-lowtag
235                           :widetag funcallable-instance-header-widetag
236                           :alloc-trans %make-funcallable-instance)
237   (trampoline :init :funcallable-instance-tramp)
238   (function :ref-known (flushable) :ref-trans %funcallable-instance-function
239             :set-known (unsafe) :set-trans (setf %funcallable-instance-function))
240   (info :rest-p t))
241
242 (define-primitive-object (value-cell :lowtag other-pointer-lowtag
243                                      :widetag value-cell-header-widetag
244                                      :alloc-trans make-value-cell)
245   (value :set-trans value-cell-set
246          :set-known (unsafe)
247          :ref-trans value-cell-ref
248          :ref-known (flushable)
249          :init :arg))
250
251 #!+alpha
252 (define-primitive-object (sap :lowtag other-pointer-lowtag
253                               :widetag sap-widetag)
254   (padding)
255   (pointer :c-type "char *" :length 2))
256
257 #!-alpha
258 (define-primitive-object (sap :lowtag other-pointer-lowtag
259                               :widetag sap-widetag)
260   (pointer :c-type "char *"))
261
262
263 (define-primitive-object (weak-pointer :type weak-pointer
264                                        :lowtag other-pointer-lowtag
265                                        :widetag weak-pointer-widetag
266                                        :alloc-trans make-weak-pointer)
267   (value :ref-trans sb!c::%weak-pointer-value :ref-known (flushable)
268          :init :arg)
269   (broken :type (member t nil)
270           :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
271           :init :null)
272   (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
273
274 ;;;; other non-heap data blocks
275
276 (define-primitive-object (binding)
277   value
278   symbol)
279
280 (define-primitive-object (unwind-block)
281   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
282   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
283   #!-(or x86 x86-64) current-code
284   entry-pc
285   #!+win32 next-seh-frame
286   #!+win32 seh-frame-handler)
287
288 (define-primitive-object (catch-block)
289   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
290   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
291   #!-(or x86 x86-64) current-code
292   entry-pc
293   #!+win32 next-seh-frame
294   #!+win32 seh-frame-handler
295   tag
296   (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32")
297   size)
298
299 ;;; (For an explanation of this, see the comments at the definition of
300 ;;; KLUDGE-NONDETERMINISTIC-CATCH-BLOCK-SIZE.)
301 (aver (= kludge-nondeterministic-catch-block-size catch-block-size))
302 \f
303 ;;;; symbols
304
305 (define-primitive-object (symbol :lowtag other-pointer-lowtag
306                                  :widetag symbol-header-widetag
307                                  :alloc-trans %make-symbol)
308
309   ;; Beware when changing this definition.  NIL-the-symbol is defined
310   ;; using this layout, and NIL-the-end-of-list-marker is the cons
311   ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
312   ;; (conses have no header).  Careful selection of lowtags ensures
313   ;; that the same pointer can be used for both purposes:
314   ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
315   ;; subtract 3 from (SB-KERNEL:GET-LISP-OBJ-ADDRESS 'NIL) you get the
316   ;; first data slot, and if you subtract 7 you get a symbol header.
317
318   ;; also the CAR of NIL-as-end-of-list
319   (value :init :unbound :ref-known (flushable) :ref-trans symbol-global-value)
320   ;; also the CDR of NIL-as-end-of-list.  Its reffer needs special
321   ;; care for this reason, as hash values must be fixnums.
322   (hash :set-trans %set-symbol-hash)
323
324   (plist :ref-trans symbol-plist
325          :set-trans %set-symbol-plist
326          :cas-trans %compare-and-swap-symbol-plist
327          :init :null)
328   (name :ref-trans symbol-name :init :arg)
329   (package :ref-trans symbol-package
330            :set-trans %set-symbol-package
331            :init :null)
332   #!+sb-thread (tls-index :ref-known (flushable) :ref-trans symbol-tls-index))
333
334 (define-primitive-object (complex-single-float
335                           :lowtag other-pointer-lowtag
336                           :widetag complex-single-float-widetag)
337   (real :c-type "float")
338   (imag :c-type "float"))
339
340 (define-primitive-object (complex-double-float
341                           :lowtag other-pointer-lowtag
342                           :widetag complex-double-float-widetag)
343   #!-x86-64 (filler)
344   (real :c-type "double" :length #!-x86-64 2 #!+x86-64 1)
345   (imag :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
346
347 #!+(and sb-lutex sb-thread)
348 (define-primitive-object (lutex
349                           :lowtag other-pointer-lowtag
350                           :widetag lutex-widetag
351                           :alloc-trans %make-lutex)
352   (gen :c-type "long" :length 1)
353   (live :c-type "long" :length 1)
354   (next :c-type "struct lutex *" :length 1)
355   (prev :c-type "struct lutex *" :length 1)
356   (mutex :c-type "pthread_mutex_t *"
357          :length 1)
358   (mutexattr :c-type "pthread_mutexattr_t *"
359              :length 1)
360   (condition-variable :c-type "pthread_cond_t *"
361                       :length 1))
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   ;; no_tls_value_marker is borrowed very briefly at thread startup to
371   ;; pass the address of initial-function into new_thread_trampoline.
372   ;; tls[0] = NO_TLS_VALUE_MARKER_WIDETAG because a the tls index slot
373   ;; of a symbol is initialized to zero
374   (no-tls-value-marker)
375   (os-thread :c-type "volatile os_thread_t")
376   (binding-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
377   (binding-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
378   (control-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
379   (control-stack-end :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
380   (alien-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
381   (alien-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
382   #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
383   (this :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
384   (prev :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
385   (next :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
386   ;; starting, running, suspended, dead
387   (state :c-type "volatile lispobj")
388   (tls-cookie)                          ;  on x86, the LDT index
389   #!+(or x86 x86-64) (pseudo-atomic-bits)
390   (interrupt-data :c-type "struct interrupt_data *"
391                   :length #!+alpha 2 #!-alpha 1)
392   (stepping)
393   (interrupt-contexts :c-type "os_context_t *" :rest-p t))