6eb57810226f75254cca193b915f3fa17b9d7574
[sbcl.git] / src / code / kernel.lisp
1 ;;;; miscellaneous kernel-level definitions
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!KERNEL")
13
14 ;;; Return the 24 bits of data in the header of object X, which must
15 ;;; be an other-pointer object.
16 (defun get-header-data (x)
17   (get-header-data x))
18
19 ;;; Set the 24 bits of data in the header of object X (which must be
20 ;;; an other-pointer object) to VAL.
21 (defun set-header-data (x val)
22   (set-header-data x val))
23
24 ;;; Return the 24 bits of data in the header of object X, which must
25 ;;; be a fun-pointer object.
26 ;;;
27 ;;; FIXME: Should this not be called GET-FUN-LENGTH instead? Or even better
28 ;;; yet, if GET-HEADER-DATA masked the lowtag instead of substracting it, we
29 ;;; could just use it instead -- or at least this could just be a function on
30 ;;; top of the same VOP.
31 (defun get-closure-length (x)
32   (get-closure-length x))
33
34 (defun lowtag-of (x)
35   (lowtag-of x))
36
37 (defun widetag-of (x)
38   (widetag-of x))
39
40 ;;; WIDETAG-OF needs extra code to handle LIST and FUNCTION lowtags. When
41 ;;; we're only dealing with other pointers (eg. when dispatching on array
42 ;;; element type), this is going to be faster.
43 (declaim (inline %other-pointer-widetag))
44 (defun %other-pointer-widetag (x)
45   (sb!sys:sap-ref-8 (int-sap (get-lisp-obj-address x))
46                     #.(ecase sb!c:*backend-byte-order*
47                         (:little-endian
48                          (- sb!vm:other-pointer-lowtag))
49                         (:big-endian
50                          (- (1- sb!vm:n-word-bytes) sb!vm:other-pointer-lowtag)))))
51
52 ;;; Return a System-Area-Pointer pointing to the data for the vector
53 ;;; X, which must be simple.
54 ;;;
55 ;;; FIXME: So it should be SIMPLE-VECTOR-SAP, right? (or UNHAIRY-VECTOR-SAP,
56 ;;; if the meaning is (SIMPLE-ARRAY * 1) instead of SIMPLE-VECTOR)
57 ;;; (or maybe SIMPLE-VECTOR-DATA-SAP or UNHAIRY-VECTOR-DATA-SAP?)
58 (defun vector-sap (x)
59   (declare (type (simple-unboxed-array (*)) x))
60   (vector-sap x))
61
62 ;;; Return a System-Area-Pointer pointing to the end of the binding stack.
63 (defun sb!c::binding-stack-pointer-sap ()
64   (sb!c::binding-stack-pointer-sap))
65
66 ;;; Return a System-Area-Pointer pointing to the next free word of the
67 ;;; current dynamic space.
68 (defun sb!c::dynamic-space-free-pointer ()
69   (sb!c::dynamic-space-free-pointer))
70
71 ;;; Return a System-Area-Pointer pointing to the end of the control stack.
72 (defun sb!c::control-stack-pointer-sap ()
73   (sb!c::control-stack-pointer-sap))
74
75 ;;; Return the header typecode for FUNCTION. Can be set with SETF.
76 (defun fun-subtype (function)
77   (fun-subtype function))
78 (defun (setf fun-subtype) (type function)
79   (setf (fun-subtype function) type))
80
81 ;;;; SIMPLE-FUN and accessors
82
83 (declaim (inline simple-fun-p))
84 (defun simple-fun-p (object)
85   (= sb!vm:simple-fun-header-widetag (widetag-of object)))
86
87 (deftype simple-fun ()
88   '(satisfies simple-fun-p))
89
90 ;;; Extract the arglist from the function header FUNC.
91 (defun %simple-fun-arglist (func)
92   (%simple-fun-arglist func))
93
94 (defun (setf %simple-fun-arglist) (new-value func)
95   (setf (%simple-fun-arglist func) new-value))
96
97 ;;; Extract the name from the function header FUNC.
98 (defun %simple-fun-name (func)
99   (%simple-fun-name func))
100
101 ;;; Extract the type from the function header FUNC.
102 (defun %simple-fun-type (func)
103   (%simple-fun-type func))
104
105 (defun %simple-fun-next (simple-fun)
106   (%simple-fun-next simple-fun))
107
108 (defun %simple-fun-self (simple-fun)
109   (%simple-fun-self simple-fun))
110
111 ;;;; CLOSURE type and accessors
112
113 (declaim (inline closurep))
114 (defun closurep (object)
115   (= sb!vm:closure-header-widetag (widetag-of object)))
116
117 (deftype closure ()
118   '(satisfies closurep))
119
120 (defmacro do-closure-values ((value closure) &body body)
121   (with-unique-names (i nclosure)
122     `(let ((,nclosure ,closure))
123        (declare (closure ,nclosure))
124        (dotimes (,i (- (1+ (get-closure-length ,nclosure)) sb!vm:closure-info-offset))
125          (let ((,value (%closure-index-ref ,nclosure ,i)))
126            ,@body)))))
127
128 (defun %closure-values (closure)
129   (declare (closure closure))
130   (let (values)
131     (do-closure-values (elt closure)
132       (push elt closure))
133     (nreverse values)))
134
135 ;;; Extract the function from CLOSURE.
136 (defun %closure-fun (closure)
137   (%closure-fun closure))
138
139 ;;; Extract the INDEXth slot from CLOSURE.
140 (defun %closure-index-ref (closure index)
141   (%closure-index-ref closure index))
142
143 ;;; Return the length of VECTOR. There is no reason to use this in
144 ;;; ordinary code, 'cause length (the vector foo)) is the same.
145 (defun sb!c::vector-length (vector)
146   (sb!c::vector-length vector))
147
148 ;;; Allocate a unboxed, simple vector with type code TYPE, length LENGTH, and
149 ;;; WORDS words long. Note: it is your responsibility to ensure that the
150 ;;; relation between LENGTH and WORDS is correct.
151 (defun allocate-vector (type length words)
152   (allocate-vector type length words))
153
154 ;;; Allocate an array header with type code TYPE and rank RANK.
155 (defun make-array-header (type rank)
156   (make-array-header type rank))
157
158 ;;; Return a SAP pointing to the instructions part of CODE-OBJ.
159 (defun code-instructions (code-obj)
160   (code-instructions code-obj))
161
162 ;;; Extract the INDEXth element from the header of CODE-OBJ. Can be
163 ;;; set with SETF.
164 (defun code-header-ref (code-obj index)
165   (code-header-ref code-obj index))
166
167 (defun code-header-set (code-obj index new)
168   (code-header-set code-obj index new))
169
170 (defun %vector-raw-bits (object offset)
171   (declare (type index offset))
172   (sb!kernel:%vector-raw-bits object offset))
173
174 (defun %set-vector-raw-bits (object offset value)
175   (declare (type index offset))
176   (declare (type sb!vm:word value))
177   (setf (sb!kernel:%vector-raw-bits object offset) value))
178
179 (defun make-single-float (x) (make-single-float x))
180 (defun make-double-float (hi lo) (make-double-float hi lo))
181
182 (defun single-float-bits (x) (single-float-bits x))
183 (defun double-float-high-bits (x) (double-float-high-bits x))
184 (defun double-float-low-bits (x) (double-float-low-bits x))
185