1.0.35.8: Fix FILE-POSITION on simple-streams after READ-VECTOR
[sbcl/smoofra.git] / src / compiler / x86 / sap.lisp
blob50daa83c9ecc31cc431ec0e399ba82743a39827a
1 ;;;; SAP operations for the x86 VM
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 ;;;; 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")
21 (:generator 1
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")
31 (:node-var node)
32 (:generator 20
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)
40 (:args (x :target y
41 :scs (sap-reg)
42 :load-if (not (location= x y))))
43 (:results (y :scs (sap-reg)
44 :load-if (not (location= x y))))
45 (:note "SAP move")
46 (:effects)
47 (:affected)
48 (:generator 0
49 (move y x)))
50 (define-move-vop sap-move :move
51 (sap-reg) (sap-reg))
53 ;;; Move untagged sap arguments/return-values.
54 (define-vop (move-sap-arg)
55 (:args (x :target y
56 :scs (sap-reg))
57 (fp :scs (any-reg)
58 :load-if (not (sc-is y sap-reg))))
59 (:results (y))
60 (:note "SAP argument move")
61 (:generator 0
62 (sc-case y
63 (sap-reg
64 (move y x))
65 (sap-stack
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.
85 (define-vop (sap-int)
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)
90 (:translate sap-int)
91 (:policy :fast-safe)
92 (:generator 1
93 (move int sap)))
94 (define-vop (int-sap)
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)
99 (:translate int-sap)
100 (:policy :fast-safe)
101 (:generator 1
102 (move sap int)))
104 ;;;; POINTER+ and POINTER-
106 (define-vop (pointer+)
107 (:translate sap+)
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 (:policy :fast-safe)
116 (:generator 1
117 (cond ((and (sc-is ptr sap-reg) (sc-is res sap-reg)
118 (not (location= ptr res)))
119 (sc-case offset
120 (signed-reg
121 (inst lea res (make-ea :dword :base ptr :index offset :scale 1)))
122 (immediate
123 (inst lea res (make-ea :dword :base ptr
124 :disp (tn-value offset))))))
126 (move res ptr)
127 (sc-case offset
128 (signed-reg
129 (inst add res offset))
130 (immediate
131 (inst add res (tn-value offset))))))))
133 (define-vop (pointer-)
134 (:translate sap-)
135 (:args (ptr1 :scs (sap-reg) :target res)
136 (ptr2 :scs (sap-reg)))
137 (:arg-types system-area-pointer system-area-pointer)
138 (:policy :fast-safe)
139 (:results (res :scs (signed-reg) :from (:argument 0)))
140 (:result-types signed-num)
141 (:generator 1
142 (move res ptr1)
143 (inst sub res ptr2)))
145 ;;;; mumble-SYSTEM-REF and mumble-SYSTEM-SET
147 (macrolet ((def-system-ref-and-set (ref-name
148 set-name
150 type
151 size
152 &optional signed)
153 (let ((temp-sc (symbolicate size "-REG")))
154 `(progn
155 (define-vop (,ref-name)
156 (:translate ,ref-name)
157 (:policy :fast-safe)
158 (:args (sap :scs (sap-reg))
159 (offset :scs (signed-reg immediate)))
160 (:info disp)
161 (:arg-types system-area-pointer signed-num
162 (:constant (constant-displacement 0 1 0)))
163 (:results (result :scs (,sc)))
164 (:result-types ,type)
165 (:generator 5
166 ,(let ((mov-inst (cond
167 ((eq size :dword) 'mov)
168 (signed 'movsx)
169 (t 'movzx))))
170 `(sc-case offset
171 (immediate
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
177 :index offset
178 :disp disp)))))))
179 (define-vop (,set-name)
180 (:translate ,set-name)
181 (:policy :fast-safe)
182 (:args (sap :scs (sap-reg) :to (:eval 0))
183 (offset :scs (signed-reg immediate) :to (:eval 0))
184 (value :scs (,sc)
185 :target ,(if (eq size :dword)
186 'result
187 'temp)))
188 (:info disp)
189 (:arg-types system-area-pointer signed-num
190 (:constant (constant-displacement 0 1 0))
191 ,type)
192 ,@(unless (eq size :dword)
193 `((:temporary (:sc ,temp-sc :offset eax-offset
194 :from (:argument 2) :to (:result 0)
195 :target result)
196 temp)))
197 (:results (result :scs (,sc)))
198 (:result-types ,type)
199 (:generator 5
200 ,@(unless (eq size :dword)
201 `((move eax-tn value)))
202 (inst mov (sc-case offset
203 (immediate
204 (make-ea ,size :base sap
205 :disp (+ (tn-value offset)
206 disp)))
207 (t (make-ea ,size
208 :base sap
209 :index offset
210 :disp disp)))
211 ,(if (eq size :dword) 'value 'temp))
212 (move result
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))
230 ;;;; SAP-REF-DOUBLE
232 (define-vop (sap-ref-double-with-offset)
233 (:translate sb!c::sap-ref-double-with-offset)
234 (:policy :fast-safe)
235 (:args (sap :scs (sap-reg))
236 (offset :scs (signed-reg immediate)))
237 (:info disp)
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)
242 (:generator 5
243 (sc-case offset
244 (immediate
245 (aver (zerop disp))
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
251 :disp disp)))))))
253 (define-vop (%set-sap-ref-double-with-offset)
254 (:translate sb!c::%set-sap-ref-double-with-offset)
255 (:policy :fast-safe)
256 (:args (sap :scs (sap-reg) :to (:eval 0))
257 (offset :scs (signed-reg) :to (:eval 0))
258 (value :scs (double-reg)))
259 (:info disp)
260 (:arg-types system-area-pointer signed-num
261 (:constant (constant-displacement 0 1 0))
262 double-float)
263 (:results (result :scs (double-reg)))
264 (:result-types double-float)
265 (:generator 5
266 (cond ((zerop (tn-offset value))
267 ;; Value is in ST0.
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.
271 (inst fstd result)))
273 ;; Value is not in ST0.
274 (inst fxch value)
275 (inst fstd (make-ea :dword :base sap :index offset :disp disp))
276 (cond ((zerop (tn-offset result))
277 ;; The result is in ST0.
278 (inst fstd value))
280 ;; Neither value or result are in ST0.
281 (unless (location= value result)
282 (inst fstd 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)
287 (:policy :fast-safe)
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))
292 double-float)
293 (:info offset disp)
294 (:results (result :scs (double-reg)))
295 (:result-types double-float)
296 (:generator 4
297 (aver (zerop disp))
298 (cond ((zerop (tn-offset value))
299 ;; Value is in ST0.
300 (inst fstd (make-ea :dword :base sap :disp offset))
301 (unless (zerop (tn-offset result))
302 ;; Value is in ST0 but not result.
303 (inst fstd result)))
305 ;; Value is not in ST0.
306 (inst fxch value)
307 (inst fstd (make-ea :dword :base sap :disp offset))
308 (cond ((zerop (tn-offset result))
309 ;; The result is in ST0.
310 (inst fstd value))
312 ;; Neither value or result are in ST0.
313 (unless (location= value result)
314 (inst fstd result))
315 (inst fxch value)))))))
317 ;;;; SAP-REF-SINGLE
319 (define-vop (sap-ref-single-with-offset)
320 (:translate sb!c::sap-ref-single-with-offset)
321 (:policy :fast-safe)
322 (:args (sap :scs (sap-reg))
323 (offset :scs (signed-reg immediate)))
324 (:info disp)
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)
329 (:generator 5
330 (sc-case offset
331 (immediate
332 (aver (zerop disp))
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)
341 (:policy :fast-safe)
342 (:args (sap :scs (sap-reg) :to (:eval 0))
343 (offset :scs (signed-reg) :to (:eval 0))
344 (value :scs (single-reg)))
345 (:info disp)
346 (:arg-types system-area-pointer signed-num
347 (:constant (constant-displacement 0 1 0))
348 single-float)
349 (:results (result :scs (single-reg)))
350 (:result-types single-float)
351 (:generator 5
352 (cond ((zerop (tn-offset value))
353 ;; Value is in ST0
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.
357 (inst fst result)))
359 ;; Value is not in ST0.
360 (inst fxch value)
361 (inst fst (make-ea :dword :base sap :index offset :disp disp))
362 (cond ((zerop (tn-offset result))
363 ;; The result is in ST0.
364 (inst fst value))
366 ;; Neither value or result are in ST0
367 (unless (location= value result)
368 (inst fst 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)
373 (:policy :fast-safe)
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))
378 single-float)
379 (:info offset disp)
380 (:results (result :scs (single-reg)))
381 (:result-types single-float)
382 (:generator 4
383 (aver (zerop disp))
384 (cond ((zerop (tn-offset value))
385 ;; Value is in ST0
386 (inst fst (make-ea :dword :base sap :disp offset))
387 (unless (zerop (tn-offset result))
388 ;; Value is in ST0 but not result.
389 (inst fst result)))
391 ;; Value is not in ST0.
392 (inst fxch value)
393 (inst fst (make-ea :dword :base sap :disp offset))
394 (cond ((zerop (tn-offset result))
395 ;; The result is in ST0.
396 (inst fst value))
398 ;; Neither value or result are in ST0
399 (unless (location= value result)
400 (inst fst result))
401 (inst fxch value)))))))
403 ;;;; SAP-REF-LONG
405 (define-vop (sap-ref-long)
406 (:translate sap-ref-long)
407 (:policy :fast-safe)
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)
413 (:generator 5
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)
419 (:policy :fast-safe)
420 (:args (sap :scs (sap-reg)))
421 (:arg-types system-area-pointer (:constant (signed-byte 32)))
422 (:info offset)
423 (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
424 (:result-types #!+long-float long-float #!-long-float double-float)
425 (:generator 4
426 (with-empty-tn@fp-top(result)
427 (inst fldl (make-ea :dword :base sap :disp offset)))))
429 #!+long-float
430 (define-vop (%set-sap-ref-long)
431 (:translate %set-sap-ref-long)
432 (:policy :fast-safe)
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)
439 (:generator 5
440 (cond ((zerop (tn-offset value))
441 ;; Value is in ST0
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.
445 (inst fstd result)))
447 ;; Value is not in ST0.
448 (inst fxch value)
449 (store-long-float (make-ea :dword :base sap :index offset))
450 (cond ((zerop (tn-offset result))
451 ;; The result is in ST0.
452 (inst fstd value))
454 ;; Neither value or result are in ST0
455 (unless (location= value result)
456 (inst fstd result))
457 (inst fxch value)))))))
459 ;;; noise to convert normal lisp data objects into SAPs
461 (define-vop (vector-sap)
462 (:translate vector-sap)
463 (:policy :fast-safe)
464 (:args (vector :scs (descriptor-reg) :target sap))
465 (:results (sap :scs (sap-reg)))
466 (:result-types system-area-pointer)
467 (:generator 2
468 (move sap vector)
469 (inst add
471 (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))