Speed up vector extension in VECTOR-PUSH-EXTEND.
[sbcl.git] / src / code / x86-vm.lisp
blob322dc3966e1cb90c279882bdfce021c3750b52e5
1 ;;;; X86-specific runtime stuff
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.
12 (in-package "SB!VM")
14 ;;;; OS-CONTEXT-T
16 ;;; a POSIX signal context, i.e. the type passed as the third
17 ;;; argument to an SA_SIGACTION-style signal handler
18 ;;;
19 ;;; The real type does have slots, but at Lisp level, we never
20 ;;; access them, or care about the size of the object. Instead, we
21 ;;; always refer to these objects by pointers handed to us by the C
22 ;;; runtime library, and ask the runtime library any time we need
23 ;;; information about the contents of one of these objects. Thus, it
24 ;;; works to represent this as an object with no slots.
25 ;;;
26 ;;; KLUDGE: It would be nice to have a type definition analogous to
27 ;;; C's "struct os_context_t;", for an incompletely specified object
28 ;;; which can only be referred to by reference, but I don't know how
29 ;;; to do that in the FFI, so instead we just this bogus no-slots
30 ;;; representation. -- WHN 20000730
31 ;;;
32 ;;; FIXME: Since SBCL, unlike CMU CL, uses this as an opaque type,
33 ;;; it's no longer architecture-dependent, and probably belongs in
34 ;;; some other package, perhaps SB-KERNEL.
35 (define-alien-type os-context-t (struct os-context-t-struct))
37 ;;;; MACHINE-TYPE
39 (defun machine-type ()
40 #!+sb-doc
41 "Return a string describing the type of the local machine."
42 "X86")
44 ;;;; :CODE-OBJECT fixups
46 ;;; a counter to measure the storage overhead of these fixups
47 (defvar *num-fixups* 0)
48 ;;; FIXME: When the system runs, it'd be interesting to see what this is.
50 (declaim (inline adjust-fixup-array))
51 (defun adjust-fixup-array (array size)
52 (let ((new (make-array size :element-type '(unsigned-byte 32))))
53 (replace new array)
54 new))
56 ;;; This gets called by LOAD to resolve newly positioned objects
57 ;;; with things (like code instructions) that have to refer to them.
58 ;;;
59 ;;; Add a fixup offset to the vector of fixup offsets for the given
60 ;;; code object.
61 (defun fixup-code-object (code offset fixup kind)
62 (declare (type index offset))
63 (flet ((add-fixup (code offset)
64 ;; (We check for and ignore fixups for code objects in the
65 ;; read-only and static spaces. (In the old CMU CL code
66 ;; this check was conditional on *ENABLE-DYNAMIC-SPACE-CODE*,
67 ;; but in SBCL relocatable dynamic space code is always in
68 ;; use, so we always do the check.)
69 (incf *num-fixups*)
70 (let ((fixups (code-header-ref code code-constants-offset)))
71 (cond ((typep fixups '(simple-array (unsigned-byte 32) (*)))
72 (let ((new-fixups
73 (adjust-fixup-array fixups (1+ (length fixups)))))
74 (setf (aref new-fixups (length fixups)) offset)
75 (setf (code-header-ref code code-constants-offset)
76 new-fixups)))
78 (unless (or (eq (widetag-of fixups)
79 unbound-marker-widetag)
80 (zerop fixups))
81 (format t "** Init. code FU = ~S~%" fixups)) ; FIXME
82 (setf (code-header-ref code code-constants-offset)
83 (make-array
85 :element-type '(unsigned-byte 32)
86 :initial-element offset)))))))
87 (without-gcing
88 (let* ((sap (truly-the system-area-pointer
89 (code-instructions code)))
90 (obj-start-addr (logand (get-lisp-obj-address code)
91 #xfffffff8))
92 ;; FIXME: what is this 5?
93 #+nil (const-start-addr (+ obj-start-addr (* 5 n-word-bytes)))
94 (code-start-addr (sap-int (code-instructions code)))
95 (ncode-words (code-header-ref code 1))
96 (code-end-addr (+ code-start-addr (* ncode-words n-word-bytes))))
97 (unless (member kind '(:absolute :relative))
98 (error "Unknown code-object-fixup kind ~S." kind))
99 (ecase kind
100 (:absolute
101 ;; Word at sap + offset contains a value to be replaced by
102 ;; adding that value to fixup.
103 (setf (sap-ref-32 sap offset) (+ fixup (sap-ref-32 sap offset)))
104 ;; Record absolute fixups that point within the code object.
105 (when (> code-end-addr (sap-ref-32 sap offset) obj-start-addr)
106 (add-fixup code offset)))
107 (:relative
108 ;; Fixup is the actual address wanted.
110 ;; Record relative fixups that point outside the code
111 ;; object.
112 (when (or (< fixup obj-start-addr) (> fixup code-end-addr))
113 (add-fixup code offset))
114 ;; Replace word with value to add to that loc to get there.
115 (let* ((loc-sap (+ (sap-int sap) offset))
116 ;; Use modular arithmetic so that if the offset
117 ;; doesn't fit into signed-byte-32 it'll wrap around
118 ;; when added to EIP
119 (rel-val (ldb (byte 32 0)
120 (- fixup loc-sap n-word-bytes))))
121 (declare (type (unsigned-byte 32) loc-sap rel-val))
122 (setf (sap-ref-32 sap offset) rel-val))))))
123 nil))
125 ;;;; low-level signal context access functions
126 ;;;;
127 ;;;; Note: In CMU CL, similar functions were hardwired to access
128 ;;;; BSD-style sigcontext structures defined as alien objects. Our
129 ;;;; approach is different in two ways:
130 ;;;; 1. We use POSIX SA_SIGACTION-style signals, so our context is
131 ;;;; whatever the void pointer in the sigaction handler dereferences
132 ;;;; to, not necessarily a sigcontext.
133 ;;;; 2. We don't try to maintain alien definitions of the context
134 ;;;; structure at Lisp level, but instead call alien C functions
135 ;;;; which take care of access for us. (Since the C functions can
136 ;;;; be defined in terms of system standard header files, they
137 ;;;; should be easier to maintain; and since Lisp code uses signal
138 ;;;; contexts only in interactive or exception code (like the debugger
139 ;;;; and internal error handling) the extra runtime cost should be
140 ;;;; negligible.
142 (declaim (inline context-pc-addr))
143 (define-alien-routine ("os_context_pc_addr" context-pc-addr) (* unsigned-int)
144 ;; (Note: Just as in CONTEXT-REGISTER-ADDR, we intentionally use an
145 ;; 'unsigned *' interpretation for the 32-bit word passed to us by
146 ;; the C code, even though the C code may think it's an 'int *'.)
147 (context (* os-context-t)))
149 (declaim (inline context-pc))
150 (defun context-pc (context)
151 (declare (type (alien (* os-context-t)) context))
152 (let ((addr (context-pc-addr context)))
153 (declare (type (alien (* unsigned-int)) addr))
154 (int-sap (deref addr))))
156 (declaim (inline context-register-addr))
157 (define-alien-routine ("os_context_register_addr" context-register-addr)
158 (* unsigned-int)
159 ;; (Note the mismatch here between the 'int *' value that the C code
160 ;; may think it's giving us and the 'unsigned *' value that we
161 ;; receive. It's intentional: the C header files may think of
162 ;; register values as signed, but the CMU CL code tends to think of
163 ;; register values as unsigned, and might get bewildered if we ask
164 ;; it to work with signed values.)
165 (context (* os-context-t))
166 (index int))
168 #!+(or linux win32)
169 (define-alien-routine ("os_context_float_register_addr" context-float-register-addr)
170 (* unsigned) (context (* os-context-t)) (index int))
172 (declaim (inline context-register))
173 (defun context-register (context index)
174 (declare (type (alien (* os-context-t)) context))
175 (let ((addr (context-register-addr context index)))
176 (declare (type (alien (* unsigned-int)) addr))
177 (deref addr)))
179 (defun %set-context-register (context index new)
180 (declare (type (alien (* os-context-t)) context))
181 (let ((addr (context-register-addr context index)))
182 (declare (type (alien (* unsigned-int)) addr))
183 (setf (deref addr) new)))
185 (defun context-float-register (context index format)
186 (declare (ignorable context index))
187 #!-(or linux win32)
188 (progn
189 (warn "stub CONTEXT-FLOAT-REGISTER")
190 (coerce 0 format))
191 #!+(or linux win32)
192 (let ((sap (alien-sap (context-float-register-addr context index))))
193 (ecase format
194 (single-float
195 (coerce (sap-ref-long sap 0) 'single-float))
196 (double-float
197 (sap-ref-long sap 0))
198 (complex-single-float
199 (complex (coerce (sap-ref-long sap 0) 'single-float)
200 (coerce (sap-ref-long sap 10) 'single-float)))
201 (complex-double-float
202 (complex (sap-ref-long sap 0)
203 (sap-ref-long sap 10))))))
205 (defun %set-context-float-register (context index format new-value)
206 (declare (ignore context index))
207 (warn "stub %SET-CONTEXT-FLOAT-REGISTER")
208 (coerce new-value format))
210 ;;; Given a signal context, return the floating point modes word in
211 ;;; the same format as returned by FLOATING-POINT-MODES.
212 #!-(or linux sunos)
213 (defun context-floating-point-modes (context)
214 ;; FIXME: As of sbcl-0.6.7 and the big rewrite of signal handling for
215 ;; POSIXness and (at the Lisp level) opaque signal contexts,
216 ;; this is stubified. It needs to be rewritten as an
217 ;; alien function.
218 (declare (ignore context)) ; stub!
219 (warn "stub CONTEXT-FLOATING-POINT-MODES")
222 #!+(or linux sunos)
223 (define-alien-routine ("os_context_fp_control" context-floating-point-modes)
224 (sb!alien:unsigned 32)
225 (context (* os-context-t)))
227 ;;;; INTERNAL-ERROR-ARGS
229 ;;; Given a (POSIX) signal context, extract the internal error
230 ;;; arguments from the instruction stream.
231 (defun internal-error-args (context)
232 (declare (type (alien (* os-context-t)) context))
233 (/show0 "entering INTERNAL-ERROR-ARGS, CONTEXT=..")
234 (/hexstr context)
235 (let ((pc (context-pc context)))
236 (declare (type system-area-pointer pc))
237 (/show0 "got PC")
238 ;; using INT3 the pc is .. INT3 <here> code length bytes...
239 (let* ((length (sap-ref-8 pc 1))
240 (vector (make-array length :element-type '(unsigned-byte 8))))
241 (declare (type (unsigned-byte 8) length)
242 (type (simple-array (unsigned-byte 8) (*)) vector))
243 (/show0 "LENGTH,VECTOR,ERROR-NUMBER=..")
244 (/hexstr length)
245 (/hexstr vector)
246 (copy-ub8-from-system-area pc 2 vector 0 length)
247 (let* ((index 0)
248 (error-number (sb!c:read-var-integer vector index)))
249 (/hexstr error-number)
250 (collect ((sc-offsets))
251 (loop
252 (/show0 "INDEX=..")
253 (/hexstr index)
254 (when (>= index length)
255 (return))
256 (let ((sc-offset (sb!c:read-var-integer vector index)))
257 (/show0 "SC-OFFSET=..")
258 (/hexstr sc-offset)
259 (sc-offsets sc-offset)))
260 (values error-number (sc-offsets)))))))
262 ;;; This is used in error.lisp to insure that floating-point exceptions
263 ;;; are properly trapped. The compiler translates this to a VOP.
264 (defun float-wait ()
265 (float-wait))
267 ;;; float constants
269 ;;; These are used by the FP MOVE-FROM-{SINGLE|DOUBLE} VOPs rather
270 ;;; than the i387 load constant instructions to avoid consing in some
271 ;;; cases. Note these are initialized by GENESIS as they are needed
272 ;;; early.
273 (defvar *fp-constant-0f0*)
274 (defvar *fp-constant-1f0*)
275 (defvar *fp-constant-0d0*)
276 (defvar *fp-constant-1d0*)
277 ;;; the long-float constants
278 (defvar *fp-constant-0l0*)
279 (defvar *fp-constant-1l0*)
280 (defvar *fp-constant-pi*)
281 (defvar *fp-constant-l2t*)
282 (defvar *fp-constant-l2e*)
283 (defvar *fp-constant-lg2*)
284 (defvar *fp-constant-ln2*)
286 ;;; the current alien stack pointer; saved/restored for non-local exits
287 (defvar *alien-stack-pointer*)
289 ;;; Support for the MT19937 random number generator. The update
290 ;;; function is implemented as an assembly routine. This definition is
291 ;;; transformed to a call to the assembly routine allowing its use in
292 ;;; interpreted code.
293 (defun random-mt19937 (state)
294 (declare (type (simple-array (unsigned-byte 32) (627)) state))
295 (random-mt19937 state))