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)
115 (:temporary (:sc signed-reg) temp)
118 (cond ((and (sc-is ptr sap-reg) (sc-is res sap-reg)
119 (not (location= ptr res)))
122 (inst lea res (make-ea :qword :base ptr :index offset :scale 1)))
124 (let ((value (tn-value offset)))
125 (cond ((typep value '(or (signed-byte 32) (unsigned-byte 31)))
126 (inst lea res (make-ea :qword :base ptr :disp value)))
128 (inst mov temp value)
129 (inst lea res (make-ea :qword :base ptr
136 (inst add res offset))
138 (let ((value (tn-value offset)))
139 (cond ((typep value '(or (signed-byte 32) (unsigned-byte 31)))
140 (inst add res (tn-value offset)))
142 (inst mov temp value)
143 (inst add res temp))))))))))
145 (define-vop (pointer-)
147 (:args (ptr1 :scs (sap-reg) :target res)
148 (ptr2 :scs (sap-reg)))
149 (:arg-types system-area-pointer system-area-pointer)
151 (:results (res :scs (signed-reg) :from (:argument 0)))
152 (:result-types signed-num)
155 (inst sub res ptr2)))
157 ;;;; mumble-SYSTEM-REF and mumble-SYSTEM-SET
159 (macrolet ((def-system-ref-and-set (ref-name
165 (let ((ref-name-c (symbolicate ref-name "-C"))
166 (set-name-c (symbolicate set-name "-C"))
167 (temp-sc (symbolicate size "-REG")))
169 (define-vop (,ref-name)
170 (:translate ,ref-name)
172 (:args (sap :scs (sap-reg))
173 (offset :scs (signed-reg)))
174 (:arg-types system-area-pointer signed-num)
175 ,@(unless (eq size :qword)
176 `((:temporary (:sc ,temp-sc
180 (:results (result :scs (,sc)))
181 (:result-types ,type)
183 (inst mov ,(if (eq size :qword) 'result 'temp)
184 (make-ea ,size :base sap :index offset))
185 ,@(unless (eq size :qword)
186 `((inst ,(if signed 'movsx 'movzx)
188 (define-vop (,ref-name-c)
189 (:translate ,ref-name)
191 (:args (sap :scs (sap-reg)))
192 (:arg-types system-area-pointer
193 (:constant (signed-byte 64)))
195 ,@(unless (eq size :qword)
196 `((:temporary (:sc ,temp-sc
200 (:results (result :scs (,sc)))
201 (:result-types ,type)
203 (inst mov ,(if (eq size :qword) 'result 'temp)
204 (make-ea ,size :base sap :disp offset))
205 ,@(unless (eq size :qword)
206 `((inst ,(if signed 'movsx 'movzx)
208 (define-vop (,set-name)
209 (:translate ,set-name)
211 (:args (sap :scs (sap-reg) :to (:eval 0))
212 (offset :scs (signed-reg) :to (:eval 0))
214 :target ,(if (eq size :qword)
217 (:arg-types system-area-pointer signed-num ,type)
218 ,@(unless (eq size :qword)
219 `((:temporary (:sc ,temp-sc :offset rax-offset
220 :from (:argument 2) :to (:result 0)
223 (:results (result :scs (,sc)))
224 (:result-types ,type)
226 ,@(unless (eq size :qword)
227 `((move rax-tn value)))
228 (inst mov (make-ea ,size
231 ,(if (eq size :qword) 'value 'temp))
233 ,(if (eq size :qword) 'value 'rax-tn))))
234 (define-vop (,set-name-c)
235 (:translate ,set-name)
237 (:args (sap :scs (sap-reg) :to (:eval 0))
239 :target ,(if (eq size :qword)
242 (:arg-types system-area-pointer
243 (:constant (signed-byte 64)) ,type)
245 ,@(unless (eq size :qword)
246 `((:temporary (:sc ,temp-sc :offset rax-offset
247 :from (:argument 2) :to (:result 0)
250 (:results (result :scs (,sc)))
251 (:result-types ,type)
253 ,@(unless (eq size :qword)
254 `((move rax-tn value)))
256 (make-ea ,size :base sap :disp offset)
257 ,(if (eq size :qword) 'value 'temp))
258 (move result ,(if (eq size :qword)
262 (def-system-ref-and-set sap-ref-8 %set-sap-ref-8
263 unsigned-reg positive-fixnum :byte nil)
264 (def-system-ref-and-set signed-sap-ref-8 %set-signed-sap-ref-8
265 signed-reg tagged-num :byte t)
266 (def-system-ref-and-set sap-ref-16 %set-sap-ref-16
267 unsigned-reg positive-fixnum :word nil)
268 (def-system-ref-and-set signed-sap-ref-16 %set-signed-sap-ref-16
269 signed-reg tagged-num :word t)
270 (def-system-ref-and-set sap-ref-32 %set-sap-ref-32
271 unsigned-reg unsigned-num :dword nil)
272 (def-system-ref-and-set signed-sap-ref-32 %set-signed-sap-ref-32
273 signed-reg signed-num :dword t)
274 (def-system-ref-and-set sap-ref-64 %set-sap-ref-64
275 unsigned-reg unsigned-num :qword nil)
276 (def-system-ref-and-set signed-sap-ref-64 %set-signed-sap-ref-64
277 signed-reg signed-num :qword t)
278 (def-system-ref-and-set sap-ref-sap %set-sap-ref-sap
279 sap-reg system-area-pointer :qword))
283 (define-vop (sap-ref-double)
284 (:translate sap-ref-double)
286 (:args (sap :scs (sap-reg))
287 (offset :scs (signed-reg)))
288 (:arg-types system-area-pointer signed-num)
289 (:results (result :scs (double-reg)))
290 (:result-types double-float)
292 (inst movsd result (make-ea :qword :base sap :index offset))))
294 (define-vop (sap-ref-double-c)
295 (:translate sap-ref-double)
297 (:args (sap :scs (sap-reg)))
298 (:arg-types system-area-pointer (:constant (signed-byte 64)))
300 (:results (result :scs (double-reg)))
301 (:result-types double-float)
303 (inst movsd result (make-ea :qword :base sap :disp offset))))
305 (define-vop (%set-sap-ref-double)
306 (:translate %set-sap-ref-double)
308 (:args (sap :scs (sap-reg) :to (:eval 0))
309 (offset :scs (signed-reg) :to (:eval 0))
310 (value :scs (double-reg)))
311 (:arg-types system-area-pointer signed-num double-float)
312 (:results (result :scs (double-reg)))
313 (:result-types double-float)
315 (inst movsd (make-ea :qword :base sap :index offset) value)
316 (move result value)))
318 (define-vop (%set-sap-ref-double-c)
319 (:translate %set-sap-ref-double)
321 (:args (sap :scs (sap-reg) :to (:eval 0))
322 (value :scs (double-reg)))
323 (:arg-types system-area-pointer (:constant (signed-byte 64)) double-float)
325 (:results (result :scs (double-reg)))
326 (:result-types double-float)
328 (inst movsd (make-ea :qword :base sap :disp offset) value)
329 (move result value)))
333 (define-vop (sap-ref-single)
334 (:translate sap-ref-single)
336 (:args (sap :scs (sap-reg))
337 (offset :scs (signed-reg)))
338 (:arg-types system-area-pointer signed-num)
339 (:results (result :scs (single-reg)))
340 (:result-types single-float)
342 (inst movss result (make-ea :dword :base sap :index offset))))
344 (define-vop (sap-ref-single-c)
345 (:translate sap-ref-single)
347 (:args (sap :scs (sap-reg)))
348 (:arg-types system-area-pointer (:constant (signed-byte 32)))
350 (:results (result :scs (single-reg)))
351 (:result-types single-float)
353 (inst movss result (make-ea :dword :base sap :disp offset))))
355 (define-vop (%set-sap-ref-single)
356 (:translate %set-sap-ref-single)
358 (:args (sap :scs (sap-reg) :to (:eval 0))
359 (offset :scs (signed-reg) :to (:eval 0))
360 (value :scs (single-reg)))
361 (:arg-types system-area-pointer signed-num single-float)
362 (:results (result :scs (single-reg)))
363 (:result-types single-float)
365 (inst movss (make-ea :dword :base sap :index offset) value)
366 (move result value)))
368 (define-vop (%set-sap-ref-single-c)
369 (:translate %set-sap-ref-single)
371 (:args (sap :scs (sap-reg) :to (:eval 0))
372 (value :scs (single-reg)))
373 (:arg-types system-area-pointer (:constant (signed-byte 32)) single-float)
375 (:results (result :scs (single-reg)))
376 (:result-types single-float)
378 (inst movss (make-ea :dword :base sap :disp offset) value)
379 (move result value)))
383 (define-vop (sap-ref-long)
384 (:translate sap-ref-long)
386 (:args (sap :scs (sap-reg))
387 (offset :scs (signed-reg)))
388 (:arg-types system-area-pointer signed-num)
389 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
390 (:result-types #!+long-float long-float #!-long-float double-float)
392 (with-empty-tn@fp-top(result)
393 (inst fldl (make-ea :qword :base sap :index offset)))))
395 (define-vop (sap-ref-long-c)
396 (:translate sap-ref-long)
398 (:args (sap :scs (sap-reg)))
399 (:arg-types system-area-pointer (:constant (signed-byte 64)))
401 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
402 (:result-types #!+long-float long-float #!-long-float double-float)
404 (with-empty-tn@fp-top(result)
405 (inst fldl (make-ea :qword :base sap :disp offset)))))
408 ;;; noise to convert normal lisp data objects into SAPs
410 (define-vop (vector-sap)
411 (:translate vector-sap)
413 (:args (vector :scs (descriptor-reg) :target sap))
414 (:results (sap :scs (sap-reg)))
415 (:result-types system-area-pointer)
420 (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))