ea4f7baea7013aec775ccaa22f35a87181553884
[sbcl.git] / src / compiler / x86 / 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-argument)
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-argument :move-argument
70   (descriptor-reg sap-reg) (sap-reg))
71
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))
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   (: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))))))
125           (t
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))))))))
132
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)))
144 \f
145 ;;;; mumble-SYSTEM-REF and mumble-SYSTEM-SET
146
147 (macrolet ((def-system-ref-and-set (ref-name
148                                     set-name
149                                     sc
150                                     type
151                                     size
152                                     &optional signed)
153              (let ((ref-name-c (symbolicate ref-name "-C"))
154                    (set-name-c (symbolicate set-name "-C"))
155                    (temp-sc (symbolicate size "-REG")))
156                `(progn
157                   (define-vop (,ref-name)
158                     (:translate ,ref-name)
159                     (:policy :fast-safe)
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
165                                        :from (:eval 0)
166                                        :to (:eval 1))
167                                       temp)))
168                     (:results (result :scs (,sc)))
169                     (:result-types ,type)
170                     (:generator 5
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)
175                                             result temp)))))
176                   (define-vop (,ref-name-c)
177                     (:translate ,ref-name)
178                     (:policy :fast-safe)
179                     (:args (sap :scs (sap-reg)))
180                     (:arg-types system-area-pointer
181                                 (:constant (signed-byte 32)))
182                     (:info offset)
183                     ,@(unless (eq size :dword)
184                         `((:temporary (:sc ,temp-sc
185                                        :from (:eval 0)
186                                        :to (:eval 1))
187                                       temp)))
188                     (:results (result :scs (,sc)))
189                     (:result-types ,type)
190                     (:generator 4
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)
195                                             result temp)))))
196                   (define-vop (,set-name)
197                     (:translate ,set-name)
198                     (:policy :fast-safe)
199                     (:args (sap :scs (sap-reg) :to (:eval 0))
200                            (offset :scs (signed-reg) :to (:eval 0))
201                            (value :scs (,sc)
202                                   :target ,(if (eq size :dword)
203                                                'result
204                                                'temp)))
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)
209                                            :target result)
210                                       temp)))
211                     (:results (result :scs (,sc)))
212                     (:result-types ,type)
213                     (:generator 5
214                                 ,@(unless (eq size :dword)
215                                     `((move eax-tn value)))
216                                 (inst mov (make-ea ,size
217                                                    :base sap
218                                                    :index offset)
219                                       ,(if (eq size :dword) 'value 'temp))
220                                 (move result
221                                       ,(if (eq size :dword) 'value 'eax-tn))))
222                   (define-vop (,set-name-c)
223                     (:translate ,set-name)
224                     (:policy :fast-safe)
225                     (:args (sap :scs (sap-reg) :to (:eval 0))
226                            (value :scs (,sc)
227                                   :target ,(if (eq size :dword)
228                                                'result
229                                                'temp)))
230                     (:arg-types system-area-pointer
231                                 (:constant (signed-byte 32)) ,type)
232                     (:info offset)
233                     ,@(unless (eq size :dword)
234                         `((:temporary (:sc ,temp-sc :offset eax-offset
235                                            :from (:argument 2) :to (:result 0)
236                                            :target result)
237                                       temp)))
238                     (:results (result :scs (,sc)))
239                     (:result-types ,type)
240                     (:generator 4
241                                 ,@(unless (eq size :dword)
242                                     `((move eax-tn value)))
243                                 (inst mov
244                                       (make-ea ,size :base sap :disp offset)
245                                       ,(if (eq size :dword) 'value 'temp))
246                                 (move result ,(if (eq size :dword)
247                                                   'value
248                                                   'eax-tn))))))))
249
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))
264 \f
265 ;;;; SAP-REF-DOUBLE
266
267 (define-vop (sap-ref-double)
268   (:translate sap-ref-double)
269   (:policy :fast-safe)
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)
275   (:generator 5
276      (with-empty-tn@fp-top(result)
277         (inst fldd (make-ea :dword :base sap :index offset)))))
278
279 (define-vop (sap-ref-double-c)
280   (:translate sap-ref-double)
281   (:policy :fast-safe)
282   (:args (sap :scs (sap-reg)))
283   (:arg-types system-area-pointer (:constant (signed-byte 32)))
284   (:info offset)
285   (:results (result :scs (double-reg)))
286   (:result-types double-float)
287   (:generator 4
288      (with-empty-tn@fp-top(result)
289         (inst fldd (make-ea :dword :base sap :disp offset)))))
290
291 (define-vop (%set-sap-ref-double)
292   (:translate %set-sap-ref-double)
293   (:policy :fast-safe)
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)
300   (:generator 5
301     (cond ((zerop (tn-offset value))
302            ;; Value is in ST0.
303            (inst fstd (make-ea :dword :base sap :index offset))
304            (unless (zerop (tn-offset result))
305                    ;; Value is in ST0 but not result.
306                    (inst fstd result)))
307           (t
308            ;; Value is not in ST0.
309            (inst fxch value)
310            (inst fstd (make-ea :dword :base sap :index offset))
311            (cond ((zerop (tn-offset result))
312                   ;; The result is in ST0.
313                   (inst fstd value))
314                  (t
315                   ;; Neither value or result are in ST0.
316                   (unless (location= value result)
317                           (inst fstd result))
318                   (inst fxch value)))))))
319
320 (define-vop (%set-sap-ref-double-c)
321   (:translate %set-sap-ref-double)
322   (:policy :fast-safe)
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)
326   (:info offset)
327   (:results (result :scs (double-reg)))
328   (:result-types double-float)
329   (:generator 4
330     (cond ((zerop (tn-offset value))
331            ;; Value is in ST0.
332            (inst fstd (make-ea :dword :base sap :disp offset))
333            (unless (zerop (tn-offset result))
334                    ;; Value is in ST0 but not result.
335                    (inst fstd result)))
336           (t
337            ;; Value is not in ST0.
338            (inst fxch value)
339            (inst fstd (make-ea :dword :base sap :disp offset))
340            (cond ((zerop (tn-offset result))
341                   ;; The result is in ST0.
342                   (inst fstd value))
343                  (t
344                   ;; Neither value or result are in ST0.
345                   (unless (location= value result)
346                           (inst fstd result))
347                   (inst fxch value)))))))
348 \f
349 ;;;; SAP-REF-SINGLE
350
351 (define-vop (sap-ref-single)
352   (:translate sap-ref-single)
353   (:policy :fast-safe)
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)
359   (:generator 5
360      (with-empty-tn@fp-top(result)
361         (inst fld (make-ea :dword :base sap :index offset)))))
362
363 (define-vop (sap-ref-single-c)
364   (:translate sap-ref-single)
365   (:policy :fast-safe)
366   (:args (sap :scs (sap-reg)))
367   (:arg-types system-area-pointer (:constant (signed-byte 32)))
368   (:info offset)
369   (:results (result :scs (single-reg)))
370   (:result-types single-float)
371   (:generator 4
372      (with-empty-tn@fp-top(result)
373         (inst fld (make-ea :dword :base sap :disp offset)))))
374
375 (define-vop (%set-sap-ref-single)
376   (:translate %set-sap-ref-single)
377   (:policy :fast-safe)
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)
384   (:generator 5
385     (cond ((zerop (tn-offset value))
386            ;; Value is in ST0
387            (inst fst (make-ea :dword :base sap :index offset))
388            (unless (zerop (tn-offset result))
389                    ;; Value is in ST0 but not result.
390                    (inst fst result)))
391           (t
392            ;; Value is not in ST0.
393            (inst fxch value)
394            (inst fst (make-ea :dword :base sap :index offset))
395            (cond ((zerop (tn-offset result))
396                   ;; The result is in ST0.
397                   (inst fst value))
398                  (t
399                   ;; Neither value or result are in ST0
400                   (unless (location= value result)
401                           (inst fst result))
402                   (inst fxch value)))))))
403
404 (define-vop (%set-sap-ref-single-c)
405   (:translate %set-sap-ref-single)
406   (:policy :fast-safe)
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)
410   (:info offset)
411   (:results (result :scs (single-reg)))
412   (:result-types single-float)
413   (:generator 4
414     (cond ((zerop (tn-offset value))
415            ;; Value is in ST0
416            (inst fst (make-ea :dword :base sap :disp offset))
417            (unless (zerop (tn-offset result))
418                    ;; Value is in ST0 but not result.
419                    (inst fst result)))
420           (t
421            ;; Value is not in ST0.
422            (inst fxch value)
423            (inst fst (make-ea :dword :base sap :disp offset))
424            (cond ((zerop (tn-offset result))
425                   ;; The result is in ST0.
426                   (inst fst value))
427                  (t
428                   ;; Neither value or result are in ST0
429                   (unless (location= value result)
430                           (inst fst result))
431                   (inst fxch value)))))))
432 \f
433 ;;;; SAP-REF-LONG
434
435 (define-vop (sap-ref-long)
436   (:translate sap-ref-long)
437   (:policy :fast-safe)
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)
443   (:generator 5
444      (with-empty-tn@fp-top(result)
445         (inst fldl (make-ea :dword :base sap :index offset)))))
446
447 (define-vop (sap-ref-long-c)
448   (:translate sap-ref-long)
449   (:policy :fast-safe)
450   (:args (sap :scs (sap-reg)))
451   (:arg-types system-area-pointer (:constant (signed-byte 32)))
452   (:info offset)
453   (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
454   (:result-types #!+long-float long-float #!-long-float double-float)
455   (:generator 4
456      (with-empty-tn@fp-top(result)
457         (inst fldl (make-ea :dword :base sap :disp offset)))))
458
459 #!+long-float
460 (define-vop (%set-sap-ref-long)
461   (:translate %set-sap-ref-long)
462   (:policy :fast-safe)
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)
469   (:generator 5
470     (cond ((zerop (tn-offset value))
471            ;; Value is in ST0
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.
475              (inst fstd result)))
476           (t
477            ;; Value is not in ST0.
478            (inst fxch value)
479            (store-long-float (make-ea :dword :base sap :index offset))
480            (cond ((zerop (tn-offset result))
481                   ;; The result is in ST0.
482                   (inst fstd value))
483                  (t
484                   ;; Neither value or result are in ST0
485                   (unless (location= value result)
486                     (inst fstd result))
487                   (inst fxch value)))))))
488 \f
489 ;;; noise to convert normal lisp data objects into SAPs
490
491 (define-vop (vector-sap)
492   (:translate vector-sap)
493   (:policy :fast-safe)
494   (:args (vector :scs (descriptor-reg) :target sap))
495   (:results (sap :scs (sap-reg)))
496   (:result-types system-area-pointer)
497   (:generator 2
498     (move sap vector)
499     (inst add
500           sap
501           (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))