0.8.18.14:
[sbcl.git] / src / compiler / x86-64 / sap.lisp
1 ;;;; SAP operations for the x86 VM
2
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.
11
12 (in-package "SB!VM")
13 \f
14 ;;;; moves and coercions
15
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))
25
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))
37
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))
52
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 (- (1+ (tn-offset y)))))))))
69 (define-move-vop move-sap-arg :move-arg
70   (descriptor-reg sap-reg) (sap-reg))
71
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))
76 \f
77 ;;;; SAP-INT and INT-SAP
78
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)))
103 \f
104 ;;;; POINTER+ and POINTER-
105
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   (:temporary (:sc signed-reg) temp)
116   (:policy :fast-safe)
117   (:generator 1
118     (cond ((and (sc-is ptr sap-reg) (sc-is res sap-reg)
119                 (not (location= ptr res)))
120            (sc-case offset
121              (signed-reg
122               (inst lea res (make-ea :qword :base ptr :index offset :scale 1)))
123              (immediate
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)))
127                       (t
128                        (inst mov temp value)
129                        (inst lea res (make-ea :qword :base ptr
130                                               :index temp
131                                               :scale 1))))))))
132           (t
133            (move res ptr)
134            (sc-case offset
135              (signed-reg
136               (inst add res offset))
137              (immediate
138               (let ((value (tn-value offset)))
139                 (cond ((typep value '(or (signed-byte 32) (unsigned-byte 31)))
140                        (inst add res (tn-value offset)))
141                       (t
142                        (inst mov temp value)
143                        (inst add res temp))))))))))
144
145 (define-vop (pointer-)
146   (:translate sap-)
147   (:args (ptr1 :scs (sap-reg) :target res)
148          (ptr2 :scs (sap-reg)))
149   (:arg-types system-area-pointer system-area-pointer)
150   (:policy :fast-safe)
151   (:results (res :scs (signed-reg) :from (:argument 0)))
152   (:result-types signed-num)
153   (:generator 1
154     (move res ptr1)
155     (inst sub res ptr2)))
156 \f
157 ;;;; mumble-SYSTEM-REF and mumble-SYSTEM-SET
158
159 (macrolet ((def-system-ref-and-set (ref-name
160                                     set-name
161                                     sc
162                                     type
163                                     size
164                                     &optional signed)
165              (let ((ref-name-c (symbolicate ref-name "-C"))
166                    (set-name-c (symbolicate set-name "-C"))
167                    (temp-sc (symbolicate size "-REG")))
168                `(progn
169                   (define-vop (,ref-name)
170                     (:translate ,ref-name)
171                     (:policy :fast-safe)
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
177                                        :from (:eval 0)
178                                        :to (:eval 1))
179                                       temp)))
180                     (:results (result :scs (,sc)))
181                     (:result-types ,type)
182                     (:generator 5
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)
187                                             result temp)))))
188                   (define-vop (,ref-name-c)
189                     (:translate ,ref-name)
190                     (:policy :fast-safe)
191                     (:args (sap :scs (sap-reg)))
192                     (:arg-types system-area-pointer
193                                 (:constant (signed-byte 64)))
194                     (:info offset)
195                     ,@(unless (eq size :qword)
196                         `((:temporary (:sc ,temp-sc
197                                        :from (:eval 0)
198                                        :to (:eval 1))
199                                       temp)))
200                     (:results (result :scs (,sc)))
201                     (:result-types ,type)
202                     (:generator 4
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)
207                                             result temp)))))
208                   (define-vop (,set-name)
209                     (:translate ,set-name)
210                     (:policy :fast-safe)
211                     (:args (sap :scs (sap-reg) :to (:eval 0))
212                            (offset :scs (signed-reg) :to (:eval 0))
213                            (value :scs (,sc)
214                                   :target ,(if (eq size :qword)
215                                                'result
216                                                'temp)))
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)
221                                            :target result)
222                                       temp)))
223                     (:results (result :scs (,sc)))
224                     (:result-types ,type)
225                     (:generator 5
226                                 ,@(unless (eq size :qword)
227                                     `((move rax-tn value)))
228                                 (inst mov (make-ea ,size
229                                                    :base sap
230                                                    :index offset)
231                                       ,(if (eq size :qword) 'value 'temp))
232                                 (move result
233                                       ,(if (eq size :qword) 'value 'rax-tn))))
234                   (define-vop (,set-name-c)
235                     (:translate ,set-name)
236                     (:policy :fast-safe)
237                     (:args (sap :scs (sap-reg) :to (:eval 0))
238                            (value :scs (,sc)
239                                   :target ,(if (eq size :qword)
240                                                'result
241                                                'temp)))
242                     (:arg-types system-area-pointer
243                                 (:constant (signed-byte 64)) ,type)
244                     (:info offset)
245                     ,@(unless (eq size :qword)
246                         `((:temporary (:sc ,temp-sc :offset rax-offset
247                                            :from (:argument 2) :to (:result 0)
248                                            :target result)
249                                       temp)))
250                     (:results (result :scs (,sc)))
251                     (:result-types ,type)
252                     (:generator 4
253                                 ,@(unless (eq size :qword)
254                                     `((move rax-tn value)))
255                                 (inst mov
256                                       (make-ea ,size :base sap :disp offset)
257                                       ,(if (eq size :qword) 'value 'temp))
258                                 (move result ,(if (eq size :qword)
259                                                   'value
260                                                   'rax-tn))))))))
261
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))
280 \f
281 ;;;; SAP-REF-DOUBLE
282
283 (define-vop (sap-ref-double)
284   (:translate sap-ref-double)
285   (:policy :fast-safe)
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)
291   (:generator 5
292      (with-empty-tn@fp-top(result)
293         (inst fldd (make-ea :dword :base sap :index offset)))))
294
295 (define-vop (sap-ref-double-c)
296   (:translate sap-ref-double)
297   (:policy :fast-safe)
298   (:args (sap :scs (sap-reg)))
299   (:arg-types system-area-pointer (:constant (signed-byte 64)))
300   (:info offset)
301   (:results (result :scs (double-reg)))
302   (:result-types double-float)
303   (:generator 4
304      (with-empty-tn@fp-top(result)
305         (inst fldd (make-ea :dword :base sap :disp offset)))))
306
307 (define-vop (%set-sap-ref-double)
308   (:translate %set-sap-ref-double)
309   (:policy :fast-safe)
310   (:args (sap :scs (sap-reg) :to (:eval 0))
311          (offset :scs (signed-reg) :to (:eval 0))
312          (value :scs (double-reg)))
313   (:arg-types system-area-pointer signed-num double-float)
314   (:results (result :scs (double-reg)))
315   (:result-types double-float)
316   (:generator 5
317     (cond ((zerop (tn-offset value))
318            ;; Value is in ST0.
319            (inst fstd (make-ea :dword :base sap :index offset))
320            (unless (zerop (tn-offset result))
321                    ;; Value is in ST0 but not result.
322                    (inst fstd result)))
323           (t
324            ;; Value is not in ST0.
325            (inst fxch value)
326            (inst fstd (make-ea :dword :base sap :index offset))
327            (cond ((zerop (tn-offset result))
328                   ;; The result is in ST0.
329                   (inst fstd value))
330                  (t
331                   ;; Neither value or result are in ST0.
332                   (unless (location= value result)
333                           (inst fstd result))
334                   (inst fxch value)))))))
335
336 (define-vop (%set-sap-ref-double-c)
337   (:translate %set-sap-ref-double)
338   (:policy :fast-safe)
339   (:args (sap :scs (sap-reg) :to (:eval 0))
340          (value :scs (double-reg)))
341   (:arg-types system-area-pointer (:constant (signed-byte 64)) double-float)
342   (:info offset)
343   (:results (result :scs (double-reg)))
344   (:result-types double-float)
345   (:generator 4
346     (cond ((zerop (tn-offset value))
347            ;; Value is in ST0.
348            (inst fstd (make-ea :qword :base sap :disp offset))
349            (unless (zerop (tn-offset result))
350                    ;; Value is in ST0 but not result.
351                    (inst fstd result)))
352           (t
353            ;; Value is not in ST0.
354            (inst fxch value)
355            (inst fstd (make-ea :qword :base sap :disp offset))
356            (cond ((zerop (tn-offset result))
357                   ;; The result is in ST0.
358                   (inst fstd value))
359                  (t
360                   ;; Neither value or result are in ST0.
361                   (unless (location= value result)
362                           (inst fstd result))
363                   (inst fxch value)))))))
364 \f
365 ;;;; SAP-REF-SINGLE
366
367 (define-vop (sap-ref-single)
368   (:translate sap-ref-single)
369   (:policy :fast-safe)
370   (:args (sap :scs (sap-reg))
371          (offset :scs (signed-reg)))
372   (:arg-types system-area-pointer signed-num)
373   (:results (result :scs (single-reg)))
374   (:result-types single-float)
375   (:generator 5
376      (with-empty-tn@fp-top(result)
377         (inst fld (make-ea :dword :base sap :index offset)))))
378
379 (define-vop (sap-ref-single-c)
380   (:translate sap-ref-single)
381   (:policy :fast-safe)
382   (:args (sap :scs (sap-reg)))
383   (:arg-types system-area-pointer (:constant (signed-byte 32)))
384   (:info offset)
385   (:results (result :scs (single-reg)))
386   (:result-types single-float)
387   (:generator 4
388      (with-empty-tn@fp-top(result)
389         (inst fld (make-ea :dword :base sap :disp offset)))))
390
391 (define-vop (%set-sap-ref-single)
392   (:translate %set-sap-ref-single)
393   (:policy :fast-safe)
394   (:args (sap :scs (sap-reg) :to (:eval 0))
395          (offset :scs (signed-reg) :to (:eval 0))
396          (value :scs (single-reg)))
397   (:arg-types system-area-pointer signed-num single-float)
398   (:results (result :scs (single-reg)))
399   (:result-types single-float)
400   (:generator 5
401     (cond ((zerop (tn-offset value))
402            ;; Value is in ST0
403            (inst fst (make-ea :dword :base sap :index offset))
404            (unless (zerop (tn-offset result))
405                    ;; Value is in ST0 but not result.
406                    (inst fst result)))
407           (t
408            ;; Value is not in ST0.
409            (inst fxch value)
410            (inst fst (make-ea :dword :base sap :index offset))
411            (cond ((zerop (tn-offset result))
412                   ;; The result is in ST0.
413                   (inst fst value))
414                  (t
415                   ;; Neither value or result are in ST0
416                   (unless (location= value result)
417                           (inst fst result))
418                   (inst fxch value)))))))
419
420 (define-vop (%set-sap-ref-single-c)
421   (:translate %set-sap-ref-single)
422   (:policy :fast-safe)
423   (:args (sap :scs (sap-reg) :to (:eval 0))
424          (value :scs (single-reg)))
425   (:arg-types system-area-pointer (:constant (signed-byte 32)) single-float)
426   (:info offset)
427   (:results (result :scs (single-reg)))
428   (:result-types single-float)
429   (:generator 4
430     (cond ((zerop (tn-offset value))
431            ;; Value is in ST0
432            (inst fst (make-ea :dword :base sap :disp offset))
433            (unless (zerop (tn-offset result))
434                    ;; Value is in ST0 but not result.
435                    (inst fst result)))
436           (t
437            ;; Value is not in ST0.
438            (inst fxch value)
439            (inst fst (make-ea :dword :base sap :disp offset))
440            (cond ((zerop (tn-offset result))
441                   ;; The result is in ST0.
442                   (inst fst value))
443                  (t
444                   ;; Neither value or result are in ST0
445                   (unless (location= value result)
446                           (inst fst result))
447                   (inst fxch value)))))))
448 \f
449 ;;;; SAP-REF-LONG
450 #+nil
451 (define-vop (sap-ref-long)
452   (:translate sap-ref-long)
453   (:policy :fast-safe)
454   (:args (sap :scs (sap-reg))
455          (offset :scs (signed-reg)))
456   (:arg-types system-area-pointer signed-num)
457   (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
458   (:result-types #!+long-float long-float #!-long-float double-float)
459   (:generator 5
460      (with-empty-tn@fp-top(result)
461         (inst fldl (make-ea :qword :base sap :index offset)))))
462 #+nil
463 (define-vop (sap-ref-long-c)
464   (:translate sap-ref-long)
465   (:policy :fast-safe)
466   (:args (sap :scs (sap-reg)))
467   (:arg-types system-area-pointer (:constant (signed-byte 64)))
468   (:info offset)
469   (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
470   (:result-types #!+long-float long-float #!-long-float double-float)
471   (:generator 4
472      (with-empty-tn@fp-top(result)
473         (inst fldl (make-ea :qword :base sap :disp offset)))))
474
475 \f
476 ;;; noise to convert normal lisp data objects into SAPs
477
478 (define-vop (vector-sap)
479   (:translate vector-sap)
480   (:policy :fast-safe)
481   (:args (vector :scs (descriptor-reg) :target sap))
482   (:results (sap :scs (sap-reg)))
483   (:result-types system-area-pointer)
484   (:generator 2
485     (move sap vector)
486     (inst add
487           sap
488           (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))
489
490