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-type)))
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-type sap-size node)
34 (storew sap res sap-pointer-slot other-pointer-type))))
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-argument)
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 (- (1+ (tn-offset y)))))))))
69 (define-move-vop move-sap-argument :move-argument
70 (descriptor-reg sap-reg) (sap-reg))
72 ;;; Use standard MOVE-ARGUMENT + coercion to move an untagged sap to a
73 ;;; descriptor passing location.
74 (define-move-vop move-argument :move-argument
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 ((ref-name-c (symbolicate ref-name "-C"))
154 (set-name-c (symbolicate set-name "-C"))
155 (temp-sc (symbolicate size "-REG")))
157 (define-vop (,ref-name)
158 (:translate ,ref-name)
160 (:args (sap :scs (sap-reg))
161 (offset :scs (signed-reg)))
162 (:arg-types system-area-pointer signed-num)
163 ,@(unless (eq size :dword)
164 `((:temporary (:sc ,temp-sc
168 (:results (result :scs (,sc)))
169 (:result-types ,type)
171 (inst mov ,(if (eq size :dword) 'result 'temp)
172 (make-ea ,size :base sap :index offset))
173 ,@(unless (eq size :dword)
174 `((inst ,(if signed 'movsx 'movzx)
176 (define-vop (,ref-name-c)
177 (:translate ,ref-name)
179 (:args (sap :scs (sap-reg)))
180 (:arg-types system-area-pointer
181 (:constant (signed-byte 32)))
183 ,@(unless (eq size :dword)
184 `((:temporary (:sc ,temp-sc
188 (:results (result :scs (,sc)))
189 (:result-types ,type)
191 (inst mov ,(if (eq size :dword) 'result 'temp)
192 (make-ea ,size :base sap :disp offset))
193 ,@(unless (eq size :dword)
194 `((inst ,(if signed 'movsx 'movzx)
196 (define-vop (,set-name)
197 (:translate ,set-name)
199 (:args (sap :scs (sap-reg) :to (:eval 0))
200 (offset :scs (signed-reg) :to (:eval 0))
202 :target ,(if (eq size :dword)
205 (:arg-types system-area-pointer signed-num ,type)
206 ,@(unless (eq size :dword)
207 `((:temporary (:sc ,temp-sc :offset eax-offset
208 :from (:argument 2) :to (:result 0)
211 (:results (result :scs (,sc)))
212 (:result-types ,type)
214 ,@(unless (eq size :dword)
215 `((move eax-tn value)))
216 (inst mov (make-ea ,size
219 ,(if (eq size :dword) 'value 'temp))
221 ,(if (eq size :dword) 'value 'eax-tn))))
222 (define-vop (,set-name-c)
223 (:translate ,set-name)
225 (:args (sap :scs (sap-reg) :to (:eval 0))
227 :target ,(if (eq size :dword)
230 (:arg-types system-area-pointer
231 (:constant (signed-byte 32)) ,type)
233 ,@(unless (eq size :dword)
234 `((:temporary (:sc ,temp-sc :offset eax-offset
235 :from (:argument 2) :to (:result 0)
238 (:results (result :scs (,sc)))
239 (:result-types ,type)
241 ,@(unless (eq size :dword)
242 `((move eax-tn value)))
244 (make-ea ,size :base sap :disp offset)
245 ,(if (eq size :dword) 'value 'temp))
246 (move result ,(if (eq size :dword)
250 (def-system-ref-and-set sap-ref-8 %set-sap-ref-8
251 unsigned-reg positive-fixnum :byte nil)
252 (def-system-ref-and-set signed-sap-ref-8 %set-signed-sap-ref-8
253 signed-reg tagged-num :byte t)
254 (def-system-ref-and-set sap-ref-16 %set-sap-ref-16
255 unsigned-reg positive-fixnum :word nil)
256 (def-system-ref-and-set signed-sap-ref-16 %set-signed-sap-ref-16
257 signed-reg tagged-num :word t)
258 (def-system-ref-and-set sap-ref-32 %set-sap-ref-32
259 unsigned-reg unsigned-num :dword nil)
260 (def-system-ref-and-set signed-sap-ref-32 %set-signed-sap-ref-32
261 signed-reg signed-num :dword t)
262 (def-system-ref-and-set sap-ref-sap %set-sap-ref-sap
263 sap-reg system-area-pointer :dword))
267 (define-vop (sap-ref-double)
268 (:translate sap-ref-double)
270 (:args (sap :scs (sap-reg))
271 (offset :scs (signed-reg)))
272 (:arg-types system-area-pointer signed-num)
273 (:results (result :scs (double-reg)))
274 (:result-types double-float)
276 (with-empty-tn@fp-top(result)
277 (inst fldd (make-ea :dword :base sap :index offset)))))
279 (define-vop (sap-ref-double-c)
280 (:translate sap-ref-double)
282 (:args (sap :scs (sap-reg)))
283 (:arg-types system-area-pointer (:constant (signed-byte 32)))
285 (:results (result :scs (double-reg)))
286 (:result-types double-float)
288 (with-empty-tn@fp-top(result)
289 (inst fldd (make-ea :dword :base sap :disp offset)))))
291 (define-vop (%set-sap-ref-double)
292 (:translate %set-sap-ref-double)
294 (:args (sap :scs (sap-reg) :to (:eval 0))
295 (offset :scs (signed-reg) :to (:eval 0))
296 (value :scs (double-reg)))
297 (:arg-types system-area-pointer signed-num double-float)
298 (:results (result :scs (double-reg)))
299 (:result-types double-float)
301 (cond ((zerop (tn-offset value))
303 (inst fstd (make-ea :dword :base sap :index offset))
304 (unless (zerop (tn-offset result))
305 ;; Value is in ST0 but not result.
308 ;; Value is not in ST0.
310 (inst fstd (make-ea :dword :base sap :index offset))
311 (cond ((zerop (tn-offset result))
312 ;; The result is in ST0.
315 ;; Neither value or result are in ST0.
316 (unless (location= value result)
318 (inst fxch value)))))))
320 (define-vop (%set-sap-ref-double-c)
321 (:translate %set-sap-ref-double)
323 (:args (sap :scs (sap-reg) :to (:eval 0))
324 (value :scs (double-reg)))
325 (:arg-types system-area-pointer (:constant (signed-byte 32)) double-float)
327 (:results (result :scs (double-reg)))
328 (:result-types double-float)
330 (cond ((zerop (tn-offset value))
332 (inst fstd (make-ea :dword :base sap :disp offset))
333 (unless (zerop (tn-offset result))
334 ;; Value is in ST0 but not result.
337 ;; Value is not in ST0.
339 (inst fstd (make-ea :dword :base sap :disp offset))
340 (cond ((zerop (tn-offset result))
341 ;; The result is in ST0.
344 ;; Neither value or result are in ST0.
345 (unless (location= value result)
347 (inst fxch value)))))))
351 (define-vop (sap-ref-single)
352 (:translate sap-ref-single)
354 (:args (sap :scs (sap-reg))
355 (offset :scs (signed-reg)))
356 (:arg-types system-area-pointer signed-num)
357 (:results (result :scs (single-reg)))
358 (:result-types single-float)
360 (with-empty-tn@fp-top(result)
361 (inst fld (make-ea :dword :base sap :index offset)))))
363 (define-vop (sap-ref-single-c)
364 (:translate sap-ref-single)
366 (:args (sap :scs (sap-reg)))
367 (:arg-types system-area-pointer (:constant (signed-byte 32)))
369 (:results (result :scs (single-reg)))
370 (:result-types single-float)
372 (with-empty-tn@fp-top(result)
373 (inst fld (make-ea :dword :base sap :disp offset)))))
375 (define-vop (%set-sap-ref-single)
376 (:translate %set-sap-ref-single)
378 (:args (sap :scs (sap-reg) :to (:eval 0))
379 (offset :scs (signed-reg) :to (:eval 0))
380 (value :scs (single-reg)))
381 (:arg-types system-area-pointer signed-num single-float)
382 (:results (result :scs (single-reg)))
383 (:result-types single-float)
385 (cond ((zerop (tn-offset value))
387 (inst fst (make-ea :dword :base sap :index offset))
388 (unless (zerop (tn-offset result))
389 ;; Value is in ST0 but not result.
392 ;; Value is not in ST0.
394 (inst fst (make-ea :dword :base sap :index offset))
395 (cond ((zerop (tn-offset result))
396 ;; The result is in ST0.
399 ;; Neither value or result are in ST0
400 (unless (location= value result)
402 (inst fxch value)))))))
404 (define-vop (%set-sap-ref-single-c)
405 (:translate %set-sap-ref-single)
407 (:args (sap :scs (sap-reg) :to (:eval 0))
408 (value :scs (single-reg)))
409 (:arg-types system-area-pointer (:constant (signed-byte 32)) single-float)
411 (:results (result :scs (single-reg)))
412 (:result-types single-float)
414 (cond ((zerop (tn-offset value))
416 (inst fst (make-ea :dword :base sap :disp offset))
417 (unless (zerop (tn-offset result))
418 ;; Value is in ST0 but not result.
421 ;; Value is not in ST0.
423 (inst fst (make-ea :dword :base sap :disp offset))
424 (cond ((zerop (tn-offset result))
425 ;; The result is in ST0.
428 ;; Neither value or result are in ST0
429 (unless (location= value result)
431 (inst fxch value)))))))
435 (define-vop (sap-ref-long)
436 (:translate sap-ref-long)
438 (:args (sap :scs (sap-reg))
439 (offset :scs (signed-reg)))
440 (:arg-types system-area-pointer signed-num)
441 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
442 (:result-types #!+long-float long-float #!-long-float double-float)
444 (with-empty-tn@fp-top(result)
445 (inst fldl (make-ea :dword :base sap :index offset)))))
447 (define-vop (sap-ref-long-c)
448 (:translate sap-ref-long)
450 (:args (sap :scs (sap-reg)))
451 (:arg-types system-area-pointer (:constant (signed-byte 32)))
453 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
454 (:result-types #!+long-float long-float #!-long-float double-float)
456 (with-empty-tn@fp-top(result)
457 (inst fldl (make-ea :dword :base sap :disp offset)))))
460 (define-vop (%set-sap-ref-long)
461 (:translate %set-sap-ref-long)
463 (:args (sap :scs (sap-reg) :to (:eval 0))
464 (offset :scs (signed-reg) :to (:eval 0))
465 (value :scs (long-reg)))
466 (:arg-types system-area-pointer signed-num long-float)
467 (:results (result :scs (long-reg)))
468 (:result-types long-float)
470 (cond ((zerop (tn-offset value))
472 (store-long-float (make-ea :dword :base sap :index offset))
473 (unless (zerop (tn-offset result))
474 ;; Value is in ST0 but not result.
477 ;; Value is not in ST0.
479 (store-long-float (make-ea :dword :base sap :index offset))
480 (cond ((zerop (tn-offset result))
481 ;; The result is in ST0.
484 ;; Neither value or result are in ST0
485 (unless (location= value result)
487 (inst fxch value)))))))
489 ;;; noise to convert normal lisp data objects into SAPs
491 (define-vop (vector-sap)
492 (:translate vector-sap)
494 (:args (vector :scs (descriptor-reg) :target sap))
495 (:results (sap :scs (sap-reg)))
496 (:result-types system-area-pointer)
499 (inst add sap (- (* vector-data-offset word-bytes) other-pointer-type))))