1 ;;;; SAP operations for the x86 VM
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 ;;;; moves and coercions
16 ;;; Move a tagged SAP to an untagged representation.
17 (define-vop (move-to-sap)
18 (:args (x :scs (descriptor-reg)))
19 (:results (y :scs (sap-reg)))
20 (:note "pointer to SAP coercion")
22 (loadw y x sap-pointer-slot other-pointer-lowtag)))
23 (define-move-vop move-to-sap :move
24 (descriptor-reg) (sap-reg))
26 ;;; Move an untagged SAP to a tagged representation.
27 (define-vop (move-from-sap)
28 (:args (sap :scs (sap-reg) :to :result))
29 (:results (res :scs (descriptor-reg) :from :argument))
30 (:note "SAP to pointer coercion")
33 (with-fixed-allocation (res sap-widetag sap-size node)
34 (storew sap res sap-pointer-slot other-pointer-lowtag))))
35 (define-move-vop move-from-sap :move
36 (sap-reg) (descriptor-reg))
38 ;;; Move untagged sap values.
39 (define-vop (sap-move)
42 :load-if (not (location= x y))))
43 (:results (y :scs (sap-reg)
44 :load-if (not (location= x y))))
50 (define-move-vop sap-move :move
53 ;;; Move untagged sap arguments/return-values.
54 (define-vop (move-sap-arg)
58 :load-if (not (sc-is y sap-reg))))
60 (:note "SAP argument move")
66 (if (= (tn-offset fp) esp-offset)
67 (storew x fp (tn-offset y)) ; c-call
68 (storew x fp (frame-word-offset (tn-offset y))))))))
69 (define-move-vop move-sap-arg :move-arg
70 (descriptor-reg sap-reg) (sap-reg))
72 ;;; Use standard MOVE-ARG + coercion to move an untagged sap to a
73 ;;; descriptor passing location.
74 (define-move-vop move-arg :move-arg
75 (sap-reg) (descriptor-reg))
77 ;;;; SAP-INT and INT-SAP
79 ;;; The function SAP-INT is used to generate an integer corresponding
80 ;;; to the system area pointer, suitable for passing to the kernel
81 ;;; interfaces (which want all addresses specified as integers). The
82 ;;; function INT-SAP is used to do the opposite conversion. The
83 ;;; integer representation of a SAP is the byte offset of the SAP from
84 ;;; the start of the address space.
86 (:args (sap :scs (sap-reg) :target int))
87 (:arg-types system-area-pointer)
88 (:results (int :scs (unsigned-reg)))
89 (:result-types unsigned-num)
95 (:args (int :scs (unsigned-reg) :target sap))
96 (:arg-types unsigned-num)
97 (:results (sap :scs (sap-reg)))
98 (:result-types system-area-pointer)
104 ;;;; POINTER+ and POINTER-
106 (define-vop (pointer+)
108 (:args (ptr :scs (sap-reg) :target res
109 :load-if (not (location= ptr res)))
110 (offset :scs (signed-reg immediate)))
111 (:arg-types system-area-pointer signed-num)
112 (:results (res :scs (sap-reg) :from (:argument 0)
113 :load-if (not (location= ptr res))))
114 (:result-types system-area-pointer)
117 (cond ((and (sc-is ptr sap-reg) (sc-is res sap-reg)
118 (not (location= ptr res)))
121 (inst lea res (make-ea :dword :base ptr :index offset :scale 1)))
123 (inst lea res (make-ea :dword :base ptr
124 :disp (tn-value offset))))))
129 (inst add res offset))
131 (inst add res (tn-value offset))))))))
133 (define-vop (pointer-)
135 (:args (ptr1 :scs (sap-reg) :target res)
136 (ptr2 :scs (sap-reg)))
137 (:arg-types system-area-pointer system-area-pointer)
139 (:results (res :scs (signed-reg) :from (:argument 0)))
140 (:result-types signed-num)
143 (inst sub res ptr2)))
145 ;;;; mumble-SYSTEM-REF and mumble-SYSTEM-SET
147 (macrolet ((def-system-ref-and-set (ref-name
153 (let ((temp-sc (symbolicate size "-REG")))
155 (define-vop (,ref-name)
156 (:translate ,ref-name)
158 (:args (sap :scs (sap-reg))
159 (offset :scs (signed-reg immediate)))
161 (:arg-types system-area-pointer signed-num
162 (:constant (constant-displacement 0 1 0)))
163 (:results (result :scs (,sc)))
164 (:result-types ,type)
166 ,(let ((mov-inst (cond
167 ((eq size :dword) 'mov)
172 (inst ,mov-inst result
173 (make-ea ,size :base sap
174 :disp (+ (tn-value offset) disp))))
175 (t (inst ,mov-inst result
176 (make-ea ,size :base sap
179 (define-vop (,set-name)
180 (:translate ,set-name)
182 (:args (sap :scs (sap-reg) :to (:eval 0))
183 (offset :scs (signed-reg immediate) :to (:eval 0))
185 :target ,(if (eq size :dword)
189 (:arg-types system-area-pointer signed-num
190 (:constant (constant-displacement 0 1 0))
192 ,@(unless (eq size :dword)
193 `((:temporary (:sc ,temp-sc :offset eax-offset
194 :from (:argument 2) :to (:result 0)
197 (:results (result :scs (,sc)))
198 (:result-types ,type)
200 ,@(unless (eq size :dword)
201 `((move eax-tn value)))
202 (inst mov (sc-case offset
204 (make-ea ,size :base sap
205 :disp (+ (tn-value offset)
211 ,(if (eq size :dword) 'value 'temp))
213 ,(if (eq size :dword) 'value 'eax-tn))))))))
215 (def-system-ref-and-set sb!c::sap-ref-8-with-offset sb!c::%set-sap-ref-8-with-offset
216 unsigned-reg positive-fixnum :byte nil)
217 (def-system-ref-and-set sb!c::signed-sap-ref-8-with-offset sb!c::%set-signed-sap-ref-8-with-offset
218 signed-reg tagged-num :byte t)
219 (def-system-ref-and-set sb!c::sap-ref-16-with-offset sb!c::%set-sap-ref-16-with-offset
220 unsigned-reg positive-fixnum :word nil)
221 (def-system-ref-and-set sb!c::signed-sap-ref-16-with-offset sb!c::%set-signed-sap-ref-16-with-offset
222 signed-reg tagged-num :word t)
223 (def-system-ref-and-set sb!c::sap-ref-32-with-offset sb!c::%set-sap-ref-32-with-offset
224 unsigned-reg unsigned-num :dword nil)
225 (def-system-ref-and-set sb!c::signed-sap-ref-32-with-offset sb!c::%set-signed-sap-ref-32-with-offset
226 signed-reg signed-num :dword t)
227 (def-system-ref-and-set sb!c::sap-ref-sap-with-offset sb!c::%set-sap-ref-sap-with-offset
228 sap-reg system-area-pointer :dword))
232 (define-vop (sap-ref-double-with-offset)
233 (:translate sb!c::sap-ref-double-with-offset)
235 (:args (sap :scs (sap-reg))
236 (offset :scs (signed-reg immediate)))
238 (:arg-types system-area-pointer signed-num
239 (:constant (constant-displacement 0 1 0)))
240 (:results (result :scs (double-reg)))
241 (:result-types double-float)
246 (with-empty-tn@fp-top(result)
247 (inst fldd (make-ea :dword :base sap :disp (tn-value offset)))))
249 (with-empty-tn@fp-top(result)
250 (inst fldd (make-ea :dword :base sap :index offset
253 (define-vop (%set-sap-ref-double-with-offset)
254 (:translate sb!c::%set-sap-ref-double-with-offset)
256 (:args (sap :scs (sap-reg) :to (:eval 0))
257 (offset :scs (signed-reg) :to (:eval 0))
258 (value :scs (double-reg)))
260 (:arg-types system-area-pointer signed-num
261 (:constant (constant-displacement 0 1 0))
263 (:results (result :scs (double-reg)))
264 (:result-types double-float)
266 (cond ((zerop (tn-offset value))
268 (inst fstd (make-ea :dword :base sap :index offset :disp disp))
269 (unless (zerop (tn-offset result))
270 ;; Value is in ST0 but not result.
273 ;; Value is not in ST0.
275 (inst fstd (make-ea :dword :base sap :index offset :disp disp))
276 (cond ((zerop (tn-offset result))
277 ;; The result is in ST0.
280 ;; Neither value or result are in ST0.
281 (unless (location= value result)
283 (inst fxch value)))))))
285 (define-vop (%set-sap-ref-double-with-offset-c)
286 (:translate sb!c::%set-sap-ref-double-with-offset)
288 (:args (sap :scs (sap-reg) :to (:eval 0))
289 (value :scs (double-reg)))
290 (:arg-types system-area-pointer (:constant (signed-byte 32))
291 (:constant (constant-displacement 0 1 0))
294 (:results (result :scs (double-reg)))
295 (:result-types double-float)
298 (cond ((zerop (tn-offset value))
300 (inst fstd (make-ea :dword :base sap :disp offset))
301 (unless (zerop (tn-offset result))
302 ;; Value is in ST0 but not result.
305 ;; Value is not in ST0.
307 (inst fstd (make-ea :dword :base sap :disp offset))
308 (cond ((zerop (tn-offset result))
309 ;; The result is in ST0.
312 ;; Neither value or result are in ST0.
313 (unless (location= value result)
315 (inst fxch value)))))))
319 (define-vop (sap-ref-single-with-offset)
320 (:translate sb!c::sap-ref-single-with-offset)
322 (:args (sap :scs (sap-reg))
323 (offset :scs (signed-reg immediate)))
325 (:arg-types system-area-pointer signed-num
326 (:constant (constant-displacement 0 1 0)))
327 (:results (result :scs (single-reg)))
328 (:result-types single-float)
333 (with-empty-tn@fp-top(result)
334 (inst fld (make-ea :dword :base sap :disp (tn-value offset)))))
336 (with-empty-tn@fp-top(result)
337 (inst fld (make-ea :dword :base sap :index offset :disp disp)))))))
339 (define-vop (%set-sap-ref-single-with-offset)
340 (:translate sb!c::%set-sap-ref-single-with-offset)
342 (:args (sap :scs (sap-reg) :to (:eval 0))
343 (offset :scs (signed-reg) :to (:eval 0))
344 (value :scs (single-reg)))
346 (:arg-types system-area-pointer signed-num
347 (:constant (constant-displacement 0 1 0))
349 (:results (result :scs (single-reg)))
350 (:result-types single-float)
352 (cond ((zerop (tn-offset value))
354 (inst fst (make-ea :dword :base sap :index offset :disp disp))
355 (unless (zerop (tn-offset result))
356 ;; Value is in ST0 but not result.
359 ;; Value is not in ST0.
361 (inst fst (make-ea :dword :base sap :index offset :disp disp))
362 (cond ((zerop (tn-offset result))
363 ;; The result is in ST0.
366 ;; Neither value or result are in ST0
367 (unless (location= value result)
369 (inst fxch value)))))))
371 (define-vop (%set-sap-ref-single-with-offset-c)
372 (:translate sb!c::%set-sap-ref-single-with-offset)
374 (:args (sap :scs (sap-reg) :to (:eval 0))
375 (value :scs (single-reg)))
376 (:arg-types system-area-pointer (:constant (signed-byte 32))
377 (:constant (constant-displacement 0 1 0))
380 (:results (result :scs (single-reg)))
381 (:result-types single-float)
384 (cond ((zerop (tn-offset value))
386 (inst fst (make-ea :dword :base sap :disp offset))
387 (unless (zerop (tn-offset result))
388 ;; Value is in ST0 but not result.
391 ;; Value is not in ST0.
393 (inst fst (make-ea :dword :base sap :disp offset))
394 (cond ((zerop (tn-offset result))
395 ;; The result is in ST0.
398 ;; Neither value or result are in ST0
399 (unless (location= value result)
401 (inst fxch value)))))))
405 (define-vop (sap-ref-long)
406 (:translate sap-ref-long)
408 (:args (sap :scs (sap-reg))
409 (offset :scs (signed-reg)))
410 (:arg-types system-area-pointer signed-num)
411 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
412 (:result-types #!+long-float long-float #!-long-float double-float)
414 (with-empty-tn@fp-top(result)
415 (inst fldl (make-ea :dword :base sap :index offset)))))
417 (define-vop (sap-ref-long-c)
418 (:translate sap-ref-long)
420 (:args (sap :scs (sap-reg)))
421 (:arg-types system-area-pointer (:constant (signed-byte 32)))
423 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
424 (:result-types #!+long-float long-float #!-long-float double-float)
426 (with-empty-tn@fp-top(result)
427 (inst fldl (make-ea :dword :base sap :disp offset)))))
430 (define-vop (%set-sap-ref-long)
431 (:translate %set-sap-ref-long)
433 (:args (sap :scs (sap-reg) :to (:eval 0))
434 (offset :scs (signed-reg) :to (:eval 0))
435 (value :scs (long-reg)))
436 (:arg-types system-area-pointer signed-num long-float)
437 (:results (result :scs (long-reg)))
438 (:result-types long-float)
440 (cond ((zerop (tn-offset value))
442 (store-long-float (make-ea :dword :base sap :index offset))
443 (unless (zerop (tn-offset result))
444 ;; Value is in ST0 but not result.
447 ;; Value is not in ST0.
449 (store-long-float (make-ea :dword :base sap :index offset))
450 (cond ((zerop (tn-offset result))
451 ;; The result is in ST0.
454 ;; Neither value or result are in ST0
455 (unless (location= value result)
457 (inst fxch value)))))))
459 ;;; noise to convert normal lisp data objects into SAPs
461 (define-vop (vector-sap)
462 (:translate vector-sap)
464 (:args (vector :scs (descriptor-reg) :target sap))
465 (:results (sap :scs (sap-reg)))
466 (:result-types system-area-pointer)
471 (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))