Optimize special variable binding on sb-thread.
[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 ;;;;   * The GC scavenging code (and for all I know other GC code too)
18 ;;;;     is not automatically generated from these layouts, but instead
19 ;;;;     was hand-written to correspond to them. The offsets are
20 ;;;;     automatically propagated into the GC scavenging code, but the
21 ;;;;     existence of slots, and whether they should be scavenged, is
22 ;;;;     not automatically propagated. Thus e.g. if you add a
23 ;;;;     SIMPLE-FUN-DEBUG-INFO slot holding a tagged object which needs
24 ;;;;     to be GCed, you need to tweak scav_code_header() and
25 ;;;;     verify_space() in gencgc.c, and the corresponding code in gc.c.
26 ;;;;   * The src/runtime/print.c code (used by LDB) is implemented
27 ;;;;     using hand-written lists of slot names, which aren't automatically
28 ;;;;     generated from the code in this file.
29 ;;;;   * Various code (e.g. STATIC-FSET in genesis.lisp) is hard-wired
30 ;;;;     to know the name of the last slot of the object the code works
31 ;;;;     with, and implicitly to know that the last slot is special (being
32 ;;;;     the beginning of an arbitrary-length sequence of bytes following
33 ;;;;     the fixed-layout slots).
34 ;;;; -- WHN 2001-12-29
35 \f
36 ;;;; the primitive objects themselves
37
38 (define-primitive-object (cons :type cons
39                                :lowtag list-pointer-lowtag
40                                :alloc-trans cons)
41   (car :ref-trans car :set-trans sb!c::%rplaca :init :arg
42        :cas-trans %compare-and-swap-car)
43   (cdr :ref-trans cdr :set-trans sb!c::%rplacd :init :arg
44        :cas-trans %compare-and-swap-cdr))
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 "sword_t" #!+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 ())
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 ())
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 ())
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 ())
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 ())
127   (displaced-p :type t
128                :ref-trans %array-displaced-p
129                :ref-known (flushable foldable)
130                :set-trans (setf %array-displaced-p)
131                :set-known ())
132   (displaced-from :type list
133                   :ref-trans %array-displaced-from
134                   :ref-known (flushable)
135                   :set-trans (setf %array-displaced-from)
136                   :set-known ())
137   (dimensions :rest-p t))
138
139 (define-primitive-object (vector :type vector
140                                  :lowtag other-pointer-lowtag
141                                  :widetag t)
142   ;; FILL-POINTER of an ARRAY is in the same place as LENGTH of a
143   ;; VECTOR -- see SHRINK-VECTOR.
144   (length :ref-trans sb!c::vector-length
145           :type index)
146   (data :rest-p t :c-type #!-alpha "uword_t" #!+alpha "u32"))
147
148 (define-primitive-object (code :type code-component
149                                :lowtag other-pointer-lowtag
150                                :widetag t)
151   (code-size :type index
152              :ref-known (flushable movable)
153              :ref-trans %code-code-size)
154   (entry-points :type (or function null)
155                 :ref-known (flushable)
156                 :ref-trans %code-entry-points
157                 :set-known ()
158                 :set-trans (setf %code-entry-points))
159   (debug-info :type t
160               :ref-known (flushable)
161               :ref-trans %code-debug-info
162               :set-known ()
163               :set-trans (setf %code-debug-info))
164   (trace-table-offset)
165   (constants :rest-p t))
166
167 (define-primitive-object (fdefn :type fdefn
168                                 :lowtag other-pointer-lowtag
169                                 :widetag fdefn-widetag)
170   (name :ref-trans fdefn-name)
171   (fun :type (or function null) :ref-trans fdefn-fun)
172   (raw-addr :c-type #!-alpha "char *" #!+alpha "u32"))
173
174 ;;; a simple function (as opposed to hairier things like closures
175 ;;; which are also subtypes of Common Lisp's FUNCTION type)
176 (define-primitive-object (simple-fun :type function
177                                      :lowtag fun-pointer-lowtag
178                                      :widetag simple-fun-header-widetag)
179   #!-(or x86 x86-64) (self :ref-trans %simple-fun-self
180                :set-trans (setf %simple-fun-self))
181   #!+(or x86 x86-64) (self
182           ;; KLUDGE: There's no :SET-KNOWN, :SET-TRANS, :REF-KNOWN, or
183           ;; :REF-TRANS here in this case. Instead, there's separate
184           ;; DEFKNOWN/DEFINE-VOP/DEFTRANSFORM stuff in
185           ;; compiler/x86/system.lisp to define and declare them by
186           ;; hand. I don't know why this is, but that's (basically)
187           ;; the way it was done in CMU CL, and it works. (It's not
188           ;; exactly the same way it was done in CMU CL in that CMU
189           ;; CL's allows duplicate DEFKNOWNs, blithely overwriting any
190           ;; previous data associated with the previous DEFKNOWN, and
191           ;; that property was used to mask the definitions here. In
192           ;; SBCL as of 0.6.12.64 that's not allowed -- too confusing!
193           ;; -- so we have to explicitly suppress the DEFKNOWNish
194           ;; stuff here in order to allow this old hack to work in the
195           ;; new world. -- WHN 2001-08-82
196           )
197   (next :type (or function null)
198         :ref-known (flushable)
199         :ref-trans %simple-fun-next
200         :set-known ()
201         :set-trans (setf %simple-fun-next))
202   (name :ref-known (flushable)
203         :ref-trans %simple-fun-name
204         :set-known ()
205         :set-trans (setf %simple-fun-name))
206   (arglist :type list
207            :ref-known (flushable)
208            :ref-trans %simple-fun-arglist
209            :set-known ()
210            :set-trans (setf %simple-fun-arglist))
211   (type :ref-known (flushable)
212         :ref-trans %simple-fun-type
213         :set-known ()
214         :set-trans (setf %simple-fun-type))
215   ;; NIL for empty, STRING for a docstring, SIMPLE-VECTOR for XREFS, and (CONS
216   ;; STRING SIMPLE-VECTOR) for both.
217   (info :init :null
218         :ref-trans %simple-fun-info
219         :ref-known (flushable)
220         :set-trans (setf %simple-fun-info)
221         :set-known ())
222   ;; the SB!C::DEBUG-FUN object corresponding to this object, or NIL for none
223   #+nil ; FIXME: doesn't work (gotcha, lowly maintenoid!) See notes on bug 137.
224   (debug-fun :ref-known (flushable)
225              :ref-trans %simple-fun-debug-fun
226              :set-known ()
227              :set-trans (setf %simple-fun-debug-fun))
228   (code :rest-p t :c-type "unsigned char"))
229
230 (define-primitive-object (return-pc :lowtag other-pointer-lowtag :widetag t)
231   (return-point :c-type "unsigned char" :rest-p t))
232
233 (define-primitive-object (closure :lowtag fun-pointer-lowtag
234                                   :widetag closure-header-widetag)
235   (fun :init :arg :ref-trans %closure-fun)
236   (info :rest-p t))
237
238 (define-primitive-object (funcallable-instance
239                           :lowtag fun-pointer-lowtag
240                           :widetag funcallable-instance-header-widetag
241                           :alloc-trans %make-funcallable-instance)
242   (trampoline :init :funcallable-instance-tramp)
243   (function :ref-known (flushable) :ref-trans %funcallable-instance-function
244             :set-known () :set-trans (setf %funcallable-instance-function))
245   (info :rest-p t))
246
247 (define-primitive-object (value-cell :lowtag other-pointer-lowtag
248                                      :widetag value-cell-header-widetag
249                                      ;; FIXME: We also have an explicit VOP
250                                      ;; for this. Is this needed as well?
251                                      :alloc-trans make-value-cell)
252   (value :set-trans value-cell-set
253          :set-known ()
254          :ref-trans value-cell-ref
255          :ref-known (flushable)
256          :init :arg))
257
258 #!+alpha
259 (define-primitive-object (sap :lowtag other-pointer-lowtag
260                               :widetag sap-widetag)
261   (padding)
262   (pointer :c-type "char *" :length 2))
263
264 #!-alpha
265 (define-primitive-object (sap :lowtag other-pointer-lowtag
266                               :widetag sap-widetag)
267   (pointer :c-type "char *"))
268
269
270 (define-primitive-object (weak-pointer :type weak-pointer
271                                        :lowtag other-pointer-lowtag
272                                        :widetag weak-pointer-widetag
273                                        :alloc-trans make-weak-pointer)
274   (value :ref-trans sb!c::%weak-pointer-value :ref-known (flushable)
275          :init :arg)
276   (broken :type (member t nil)
277           :ref-trans sb!c::%weak-pointer-broken :ref-known (flushable)
278           :init :null)
279   (next :c-type #!-alpha "struct weak_pointer *" #!+alpha "u32"))
280
281 ;;;; other non-heap data blocks
282
283 (define-primitive-object (binding)
284   value
285   symbol) ;; on sb-thread, this is actually a tls-index
286
287 (define-primitive-object (unwind-block)
288   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
289   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
290   #!-(or x86 x86-64) current-code
291   entry-pc
292   #!+win32 next-seh-frame
293   #!+win32 seh-frame-handler)
294
295 (define-primitive-object (catch-block)
296   (current-uwp :c-type #!-alpha "struct unwind_block *" #!+alpha "u32")
297   (current-cont :c-type #!-alpha "lispobj *" #!+alpha "u32")
298   #!-(or x86 x86-64) current-code
299   entry-pc
300   #!+(and win32 x86) next-seh-frame
301   #!+(and win32 x86) seh-frame-handler
302   tag
303   (previous-catch :c-type #!-alpha "struct catch_block *" #!+alpha "u32"))
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 (define-primitive-object (symbol :lowtag other-pointer-lowtag
312                                  :widetag symbol-header-widetag
313                                  :alloc-trans %make-symbol
314                                  :type symbol)
315
316   ;; Beware when changing this definition.  NIL-the-symbol is defined
317   ;; using this layout, and NIL-the-end-of-list-marker is the cons
318   ;; ( NIL . NIL ), living in the first two slots of NIL-the-symbol
319   ;; (conses have no header).  Careful selection of lowtags ensures
320   ;; that the same pointer can be used for both purposes:
321   ;; OTHER-POINTER-LOWTAG is 7, LIST-POINTER-LOWTAG is 3, so if you
322   ;; subtract 3 from (SB-KERNEL:GET-LISP-OBJ-ADDRESS 'NIL) you get the
323   ;; first data slot, and if you subtract 7 you get a symbol header.
324
325   ;; also the CAR of NIL-as-end-of-list
326   (value :init :unbound
327          :set-trans %set-symbol-global-value
328          :set-known ())
329   ;; also the CDR of NIL-as-end-of-list.  Its reffer needs special
330   ;; care for this reason, as hash values must be fixnums.
331   (hash :set-trans %set-symbol-hash)
332
333   (plist :ref-trans symbol-plist
334          :set-trans %set-symbol-plist
335          :cas-trans %compare-and-swap-symbol-plist
336          :type list
337          :init :null)
338   (name :ref-trans symbol-name :init :arg)
339   (package :ref-trans symbol-package
340            :set-trans %set-symbol-package
341            :init :null)
342   ;; 0 tls-index means no tls-index is allocated
343   #!+sb-thread
344   (tls-index :ref-known (flushable) :ref-trans symbol-tls-index))
345
346 (define-primitive-object (complex-single-float
347                           :lowtag other-pointer-lowtag
348                           :widetag complex-single-float-widetag)
349   #!+x86-64
350   (data :c-type "struct { float data[2]; } ")
351   #!-x86-64
352   (real :c-type "float")
353   #!-x86-64
354   (imag :c-type "float"))
355
356 (define-primitive-object (complex-double-float
357                           :lowtag other-pointer-lowtag
358                           :widetag complex-double-float-widetag)
359   (filler)
360   (real :c-type "double" :length #!-x86-64 2 #!+x86-64 1)
361   (imag :c-type "double" :length #!-x86-64 2 #!+x86-64 1))
362
363 #!+sb-simd-pack
364 (define-primitive-object (simd-pack
365                           :lowtag other-pointer-lowtag
366                           :widetag simd-pack-widetag)
367   (tag :ref-trans %simd-pack-tag
368        :attributes (movable flushable)
369        :type fixnum)
370   (lo-value :c-type "long" :type (unsigned-byte 64))
371   (hi-value :c-type "long" :type (unsigned-byte 64)))
372
373 ;;; this isn't actually a lisp object at all, it's a c structure that lives
374 ;;; in c-land.  However, we need sight of so many parts of it from Lisp that
375 ;;; it makes sense to define it here anyway, so that the GENESIS machinery
376 ;;; can take care of maintaining Lisp and C versions.
377 (define-primitive-object (thread)
378   ;; no_tls_value_marker is borrowed very briefly at thread startup to
379   ;; pass the address of initial-function into new_thread_trampoline.
380   ;; tls[0] = NO_TLS_VALUE_MARKER_WIDETAG because a the tls index slot
381   ;; of a symbol is initialized to zero
382   (no-tls-value-marker)
383   (os-thread :c-type "os_thread_t")
384   ;; This is the original address at which the memory was allocated,
385   ;; which may have different alignment then what we prefer to use.
386   ;; Kept here so that when the thread dies we can release the whole
387   ;; memory we reserved.
388   (os-address :c-type "void *" :length #!+alpha 2 #!-alpha 1)
389   ;; Keep these next four slots close to the beginning of the structure.
390   ;; Doing so reduces code size for x86-64 allocation sequences and
391   ;; special variable manipulations.
392   #!+gencgc (alloc-region :c-type "struct alloc_region" :length 5)
393   #!+(or x86 x86-64 sb-thread) (pseudo-atomic-bits)
394   (binding-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
395   (binding-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
396   #!+sb-thread
397   (os-attr :c-type "pthread_attr_t *" :length #!+alpha 2 #!-alpha 1)
398   #!+sb-thread
399   (state-sem :c-type "os_sem_t *" :length #!+alpha 2 #!-alpha 1)
400   #!+sb-thread
401   (state-not-running-sem :c-type "os_sem_t *" :length #!+alpha 2 #!-alpha 1)
402   #!+sb-thread
403   (state-not-running-waitcount :c-type "int" :length 1)
404   #!+sb-thread
405   (state-not-stopped-sem :c-type "os_sem_t *" :length #!+alpha 2 #!-alpha 1)
406   #!+sb-thread
407   (state-not-stopped-waitcount :c-type "int" :length 1)
408   (control-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
409   (control-stack-end :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
410   (control-stack-guard-page-protected)
411   (alien-stack-start :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
412   (alien-stack-pointer :c-type "lispobj *" :length #!+alpha 2 #!-alpha 1)
413   #!+win32 (private-events :c-type "struct private_events" :length 2)
414   (this :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
415   (prev :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
416   (next :c-type "struct thread *" :length #!+alpha 2 #!-alpha 1)
417   ;; starting, running, suspended, dead
418   (state :c-type "lispobj")
419   (tls-cookie)                          ;  on x86, the LDT index
420   (interrupt-data :c-type "struct interrupt_data *"
421                   :length #!+alpha 2 #!-alpha 1)
422   (stepping)
423   ;; For various reasons related to pseudo-atomic and interrupt
424   ;; handling, we need to know if the machine context is in Lisp code
425   ;; or not.  On non-threaded targets, this is a global variable in
426   ;; the runtime, but it's clearly a per-thread value.
427   #!+sb-thread
428   (foreign-function-call-active :c-type "boolean")
429   ;; Same as above for the location of the current control stack frame.
430   #!+(and sb-thread (not (or x86 x86-64)))
431   (control-frame-pointer :c-type "lispobj *")
432   ;; Same as above for the location of the current control stack
433   ;; pointer.  This is also used on threaded x86oids to allow LDB to
434   ;; print an approximation of the CSP as needed.
435   #!+sb-thread
436   (control-stack-pointer :c-type "lispobj *")
437   #!+mach-exception-handler
438   (mach-port-name :c-type "mach_port_name_t")
439   (nonpointer-data :c-type "struct nonpointer_thread_data *" :length #!+alpha 2 #!-alpha 1)
440   #!+(and sb-safepoint x86) (selfptr :c-type "struct thread *")
441   ;; Context base pointer for running on top of system libraries built using
442   ;; -fomit-frame-pointer.  Currently truly required and implemented only
443   ;; for (and win32 x86-64), but could be generalized to other platforms if
444   ;; needed:
445   #!+win32 (carried-base-pointer :c-type "os_context_register_t")
446   #!+sb-safepoint (csp-around-foreign-call :c-type "lispobj *")
447   #!+sb-safepoint (pc-around-foreign-call :c-type "lispobj *")
448   #!+win32 (synchronous-io-handle-and-flag :c-type "HANDLE" :length 1)
449   #!+(and sb-safepoint-strictly (not win32))
450   (sprof-alloc-region :c-type "struct alloc_region" :length 5)
451   ;; KLUDGE: On alpha, until STEPPING we have been lucky and the 32
452   ;; bit slots came in pairs. However the C compiler will align
453   ;; interrupt_contexts on a double word boundary. This logic should
454   ;; be handled by DEFINE-PRIMITIVE-OBJECT.
455   #!+alpha
456   (padding)
457   (interrupt-contexts :c-type "os_context_t *" :rest-p t))