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 (- (1+ (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 :qword :base ptr :index offset :scale 1)))
123 (inst lea res (make-ea :qword :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 :qword)
164 `((:temporary (:sc ,temp-sc
168 (:results (result :scs (,sc)))
169 (:result-types ,type)
171 (inst mov ,(if (eq size :qword) 'result 'temp)
172 (make-ea ,size :base sap :index offset))
173 ,@(unless (eq size :qword)
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 64)))
183 ,@(unless (eq size :qword)
184 `((:temporary (:sc ,temp-sc
188 (:results (result :scs (,sc)))
189 (:result-types ,type)
191 (inst mov ,(if (eq size :qword) 'result 'temp)
192 (make-ea ,size :base sap :disp offset))
193 ,@(unless (eq size :qword)
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 :qword)
205 (:arg-types system-area-pointer signed-num ,type)
206 ,@(unless (eq size :qword)
207 `((:temporary (:sc ,temp-sc :offset rax-offset
208 :from (:argument 2) :to (:result 0)
211 (:results (result :scs (,sc)))
212 (:result-types ,type)
214 ,@(unless (eq size :qword)
215 `((move rax-tn value)))
216 (inst mov (make-ea ,size
219 ,(if (eq size :qword) 'value 'temp))
221 ,(if (eq size :qword) 'value 'rax-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 :qword)
230 (:arg-types system-area-pointer
231 (:constant (signed-byte 64)) ,type)
233 ,@(unless (eq size :qword)
234 `((:temporary (:sc ,temp-sc :offset rax-offset
235 :from (:argument 2) :to (:result 0)
238 (:results (result :scs (,sc)))
239 (:result-types ,type)
241 ,@(unless (eq size :qword)
242 `((move rax-tn value)))
244 (make-ea ,size :base sap :disp offset)
245 ,(if (eq size :qword) 'value 'temp))
246 (move result ,(if (eq size :qword)
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-64 %set-sap-ref-64
263 unsigned-reg unsigned-num :qword nil)
264 (def-system-ref-and-set signed-sap-ref-64 %set-signed-sap-ref-64
265 signed-reg signed-num :qword t)
266 (def-system-ref-and-set sap-ref-sap %set-sap-ref-sap
267 sap-reg system-area-pointer :qword))
271 (define-vop (sap-ref-double)
272 (:translate sap-ref-double)
274 (:args (sap :scs (sap-reg))
275 (offset :scs (signed-reg)))
276 (:arg-types system-area-pointer signed-num)
277 (:results (result :scs (double-reg)))
278 (:result-types double-float)
280 (with-empty-tn@fp-top(result)
281 (inst fldd (make-ea :dword :base sap :index offset)))))
283 (define-vop (sap-ref-double-c)
284 (:translate sap-ref-double)
286 (:args (sap :scs (sap-reg)))
287 (:arg-types system-area-pointer (:constant (signed-byte 64)))
289 (:results (result :scs (double-reg)))
290 (:result-types double-float)
292 (with-empty-tn@fp-top(result)
293 (inst fldd (make-ea :dword :base sap :disp offset)))))
295 (define-vop (%set-sap-ref-double)
296 (:translate %set-sap-ref-double)
298 (:args (sap :scs (sap-reg) :to (:eval 0))
299 (offset :scs (signed-reg) :to (:eval 0))
300 (value :scs (double-reg)))
301 (:arg-types system-area-pointer signed-num double-float)
302 (:results (result :scs (double-reg)))
303 (:result-types double-float)
305 (cond ((zerop (tn-offset value))
307 (inst fstd (make-ea :dword :base sap :index offset))
308 (unless (zerop (tn-offset result))
309 ;; Value is in ST0 but not result.
312 ;; Value is not in ST0.
314 (inst fstd (make-ea :dword :base sap :index offset))
315 (cond ((zerop (tn-offset result))
316 ;; The result is in ST0.
319 ;; Neither value or result are in ST0.
320 (unless (location= value result)
322 (inst fxch value)))))))
324 (define-vop (%set-sap-ref-double-c)
325 (:translate %set-sap-ref-double)
327 (:args (sap :scs (sap-reg) :to (:eval 0))
328 (value :scs (double-reg)))
329 (:arg-types system-area-pointer (:constant (signed-byte 64)) double-float)
331 (:results (result :scs (double-reg)))
332 (:result-types double-float)
334 (cond ((zerop (tn-offset value))
336 (inst fstd (make-ea :qword :base sap :disp offset))
337 (unless (zerop (tn-offset result))
338 ;; Value is in ST0 but not result.
341 ;; Value is not in ST0.
343 (inst fstd (make-ea :qword :base sap :disp offset))
344 (cond ((zerop (tn-offset result))
345 ;; The result is in ST0.
348 ;; Neither value or result are in ST0.
349 (unless (location= value result)
351 (inst fxch value)))))))
355 (define-vop (sap-ref-single)
356 (:translate sap-ref-single)
358 (:args (sap :scs (sap-reg))
359 (offset :scs (signed-reg)))
360 (:arg-types system-area-pointer signed-num)
361 (:results (result :scs (single-reg)))
362 (:result-types single-float)
364 (with-empty-tn@fp-top(result)
365 (inst fld (make-ea :dword :base sap :index offset)))))
367 (define-vop (sap-ref-single-c)
368 (:translate sap-ref-single)
370 (:args (sap :scs (sap-reg)))
371 (:arg-types system-area-pointer (:constant (signed-byte 32)))
373 (:results (result :scs (single-reg)))
374 (:result-types single-float)
376 (with-empty-tn@fp-top(result)
377 (inst fld (make-ea :dword :base sap :disp offset)))))
379 (define-vop (%set-sap-ref-single)
380 (:translate %set-sap-ref-single)
382 (:args (sap :scs (sap-reg) :to (:eval 0))
383 (offset :scs (signed-reg) :to (:eval 0))
384 (value :scs (single-reg)))
385 (:arg-types system-area-pointer signed-num single-float)
386 (:results (result :scs (single-reg)))
387 (:result-types single-float)
389 (cond ((zerop (tn-offset value))
391 (inst fst (make-ea :dword :base sap :index offset))
392 (unless (zerop (tn-offset result))
393 ;; Value is in ST0 but not result.
396 ;; Value is not in ST0.
398 (inst fst (make-ea :dword :base sap :index offset))
399 (cond ((zerop (tn-offset result))
400 ;; The result is in ST0.
403 ;; Neither value or result are in ST0
404 (unless (location= value result)
406 (inst fxch value)))))))
408 (define-vop (%set-sap-ref-single-c)
409 (:translate %set-sap-ref-single)
411 (:args (sap :scs (sap-reg) :to (:eval 0))
412 (value :scs (single-reg)))
413 (:arg-types system-area-pointer (:constant (signed-byte 32)) single-float)
415 (:results (result :scs (single-reg)))
416 (:result-types single-float)
418 (cond ((zerop (tn-offset value))
420 (inst fst (make-ea :dword :base sap :disp offset))
421 (unless (zerop (tn-offset result))
422 ;; Value is in ST0 but not result.
425 ;; Value is not in ST0.
427 (inst fst (make-ea :dword :base sap :disp offset))
428 (cond ((zerop (tn-offset result))
429 ;; The result is in ST0.
432 ;; Neither value or result are in ST0
433 (unless (location= value result)
435 (inst fxch value)))))))
439 (define-vop (sap-ref-long)
440 (:translate sap-ref-long)
442 (:args (sap :scs (sap-reg))
443 (offset :scs (signed-reg)))
444 (:arg-types system-area-pointer signed-num)
445 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
446 (:result-types #!+long-float long-float #!-long-float double-float)
448 (with-empty-tn@fp-top(result)
449 (inst fldl (make-ea :qword :base sap :index offset)))))
451 (define-vop (sap-ref-long-c)
452 (:translate sap-ref-long)
454 (:args (sap :scs (sap-reg)))
455 (:arg-types system-area-pointer (:constant (signed-byte 64)))
457 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
458 (:result-types #!+long-float long-float #!-long-float double-float)
460 (with-empty-tn@fp-top(result)
461 (inst fldl (make-ea :qword :base sap :disp offset)))))
464 ;;; noise to convert normal lisp data objects into SAPs
466 (define-vop (vector-sap)
467 (:translate vector-sap)
469 (:args (vector :scs (descriptor-reg) :target sap))
470 (:results (sap :scs (sap-reg)))
471 (:result-types system-area-pointer)
476 (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))