1.0.2.1: DATA-VECTOR-{REF,SET}-WITH-OFFSET for the x86
[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-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   (: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 ((temp-sc (symbolicate size "-REG"))
154                    (element-size (ecase size
155                                    (:byte 1)
156                                    (:word 2)
157                                    (:dword 4))))
158                `(progn
159                   (define-vop (,ref-name)
160                     (:translate ,ref-name)
161                     (:policy :fast-safe)
162                     (:args (sap :scs (sap-reg))
163                            (offset :scs (signed-reg immediate)))
164                     (:info disp)
165                     (:arg-types system-area-pointer signed-num
166                                 (:constant (constant-displacement 0 ; lowtag
167                                                                   ,element-size
168                                                                   0)))
169                     (:results (result :scs (,sc)))
170                     (:result-types ,type)
171                     (:generator 5
172                       ,(let ((mov-inst (cond
173                                          ((eq size :dword) 'mov)
174                                          (signed 'movsx)
175                                          (t 'movzx))))
176                          `(sc-case offset
177                             (immediate
178                              (inst ,mov-inst result
179                                    (make-ea ,size :base sap
180                                             :disp (+ (tn-value offset)
181                                                      (* ,element-size disp)))))
182                             (t (inst ,mov-inst result
183                                      (make-ea ,size :base sap
184                                               :index offset
185                                               :disp (* ,element-size disp))))))))
186                   (define-vop (,set-name)
187                     (:translate ,set-name)
188                     (:policy :fast-safe)
189                     (:args (sap :scs (sap-reg) :to (:eval 0))
190                            (offset :scs (signed-reg immediate) :to (:eval 0))
191                            (value :scs (,sc)
192                                   :target ,(if (eq size :dword)
193                                                'result
194                                                'temp)))
195                     (:info disp)
196                     (:arg-types system-area-pointer signed-num
197                                 (:constant (constant-displacement 0 ; lowtag
198                                                                   ,element-size
199                                                                   0))
200                                 ,type)
201                     ,@(unless (eq size :dword)
202                         `((:temporary (:sc ,temp-sc :offset eax-offset
203                                            :from (:argument 2) :to (:result 0)
204                                            :target result)
205                                       temp)))
206                     (:results (result :scs (,sc)))
207                     (:result-types ,type)
208                     (:generator 5
209                       ,@(unless (eq size :dword)
210                           `((move eax-tn value)))
211                       (inst mov (sc-case offset
212                                          (immediate
213                                           (make-ea ,size :base sap
214                                                    :disp (+ (tn-value offset)
215                                                             (* ,element-size disp))))
216                                          (t (make-ea ,size
217                                                      :base sap
218                                                      :index offset
219                                                      :disp (* ,element-size disp))))
220                             ,(if (eq size :dword) 'value 'temp))
221                       (move result
222                             ,(if (eq size :dword) 'value 'eax-tn))))))))
223
224   (def-system-ref-and-set sb!c::sap-ref-8-with-offset sb!c::%set-sap-ref-8-with-offset
225     unsigned-reg positive-fixnum :byte nil)
226   (def-system-ref-and-set sb!c::signed-sap-ref-8-with-offset sb!c::%set-signed-sap-ref-8-with-offset
227     signed-reg tagged-num :byte t)
228   (def-system-ref-and-set sb!c::sap-ref-16-with-offset sb!c::%set-sap-ref-16-with-offset
229     unsigned-reg positive-fixnum :word nil)
230   (def-system-ref-and-set sb!c::signed-sap-ref-16-with-offset sb!c::%set-signed-sap-ref-16-with-offset
231     signed-reg tagged-num :word t)
232   (def-system-ref-and-set sb!c::sap-ref-32-with-offset sb!c::%set-sap-ref-32-with-offset
233     unsigned-reg unsigned-num :dword nil)
234   (def-system-ref-and-set sb!c::signed-sap-ref-32-with-offset sb!c::%set-signed-sap-ref-32-with-offset
235     signed-reg signed-num :dword t)
236   (def-system-ref-and-set sb!c::sap-ref-sap-with-offset sb!c::%set-sap-ref-sap-with-offset
237     sap-reg system-area-pointer :dword))
238 \f
239 ;;;; SAP-REF-DOUBLE
240
241 (define-vop (sap-ref-double-with-offset)
242   (:translate sb!c::sap-ref-double-with-offset)
243   (:policy :fast-safe)
244   (:args (sap :scs (sap-reg))
245          (offset :scs (signed-reg immediate)))
246   (:info disp)
247   (:arg-types system-area-pointer signed-num
248               (:constant (constant-displacement 0 ; lowtag
249                                                 8 ; double-float size
250                                                 0)))
251   (:results (result :scs (double-reg)))
252   (:result-types double-float)
253   (:generator 5
254      (sc-case offset
255        (immediate
256         (aver (zerop disp))
257         (with-empty-tn@fp-top(result)
258           (inst fldd (make-ea :dword :base sap :disp (tn-value offset)))))
259        (t
260         (with-empty-tn@fp-top(result)
261           (inst fldd (make-ea :dword :base sap :index offset
262                               :disp (* 4 disp))))))))
263
264 (define-vop (%set-sap-ref-double-with-offset)
265   (:translate sb!c::%set-sap-ref-double-with-offset)
266   (:policy :fast-safe)
267   (:args (sap :scs (sap-reg) :to (:eval 0))
268          (offset :scs (signed-reg) :to (:eval 0))
269          (value :scs (double-reg)))
270   (:info disp)
271   (:arg-types system-area-pointer signed-num
272               (:constant (constant-displacement 0 ; lowtag
273                                                 8 ; double-float size
274                                                 0))
275               double-float)
276   (:results (result :scs (double-reg)))
277   (:result-types double-float)
278   (:generator 5
279     (cond ((zerop (tn-offset value))
280            ;; Value is in ST0.
281            (inst fstd (make-ea :dword :base sap :index offset
282                                :disp (* 8 disp)))
283            (unless (zerop (tn-offset result))
284              ;; Value is in ST0 but not result.
285              (inst fstd result)))
286           (t
287            ;; Value is not in ST0.
288            (inst fxch value)
289            (inst fstd (make-ea :dword :base sap :index offset
290                                :disp (* 8 disp)))
291            (cond ((zerop (tn-offset result))
292                   ;; The result is in ST0.
293                   (inst fstd value))
294                  (t
295                   ;; Neither value or result are in ST0.
296                   (unless (location= value result)
297                     (inst fstd result))
298                   (inst fxch value)))))))
299
300 (define-vop (%set-sap-ref-double-with-offset-c)
301   (:translate sb!c::%set-sap-ref-double-with-offset)
302   (:policy :fast-safe)
303   (:args (sap :scs (sap-reg) :to (:eval 0))
304          (value :scs (double-reg)))
305   (:arg-types system-area-pointer (:constant (signed-byte 32))
306               (:constant (constant-displacement 0 ; lowtag
307                                                 8 ; double-float size
308                                                 0))
309               double-float)
310   (:info offset disp)
311   (:results (result :scs (double-reg)))
312   (:result-types double-float)
313   (:generator 4
314     (aver (zerop disp))
315     (cond ((zerop (tn-offset value))
316            ;; Value is in ST0.
317            (inst fstd (make-ea :dword :base sap :disp offset))
318            (unless (zerop (tn-offset result))
319              ;; Value is in ST0 but not result.
320              (inst fstd result)))
321           (t
322            ;; Value is not in ST0.
323            (inst fxch value)
324            (inst fstd (make-ea :dword :base sap :disp offset))
325            (cond ((zerop (tn-offset result))
326                   ;; The result is in ST0.
327                   (inst fstd value))
328                  (t
329                   ;; Neither value or result are in ST0.
330                   (unless (location= value result)
331                     (inst fstd result))
332                   (inst fxch value)))))))
333 \f
334 ;;;; SAP-REF-SINGLE
335
336 (define-vop (sap-ref-single-with-offset)
337   (:translate sb!c::sap-ref-single-with-offset)
338   (:policy :fast-safe)
339   (:args (sap :scs (sap-reg))
340          (offset :scs (signed-reg immediate)))
341   (:info disp)
342   (:arg-types system-area-pointer signed-num
343               (:constant (constant-displacement 0 ; lowtag
344                                                 4 ; single-float size
345                                                 0)))
346   (:results (result :scs (single-reg)))
347   (:result-types single-float)
348   (:generator 5
349      (sc-case offset
350        (immediate
351         (aver (zerop disp))
352         (with-empty-tn@fp-top(result)
353           (inst fld (make-ea :dword :base sap :disp (tn-value offset)))))
354        (t
355         (with-empty-tn@fp-top(result)
356           (inst fld (make-ea :dword :base sap :index offset
357                              :disp (* 4 disp))))))))
358
359 (define-vop (%set-sap-ref-single-with-offset)
360   (:translate sb!c::%set-sap-ref-single-with-offset)
361   (:policy :fast-safe)
362   (:args (sap :scs (sap-reg) :to (:eval 0))
363          (offset :scs (signed-reg) :to (:eval 0))
364          (value :scs (single-reg)))
365   (:info disp)
366   (:arg-types system-area-pointer signed-num
367               (:constant (constant-displacement 0 ; lowtag
368                                                 4 ; single-float size
369                                                 0))
370               single-float)
371   (:results (result :scs (single-reg)))
372   (:result-types single-float)
373   (:generator 5
374     (cond ((zerop (tn-offset value))
375            ;; Value is in ST0
376            (inst fst (make-ea :dword :base sap :index offset
377                               :disp (* 4 disp)))
378            (unless (zerop (tn-offset result))
379              ;; Value is in ST0 but not result.
380              (inst fst result)))
381           (t
382            ;; Value is not in ST0.
383            (inst fxch value)
384            (inst fst (make-ea :dword :base sap :index offset
385                               :disp (* 4 disp)))
386            (cond ((zerop (tn-offset result))
387                   ;; The result is in ST0.
388                   (inst fst value))
389                  (t
390                   ;; Neither value or result are in ST0
391                   (unless (location= value result)
392                     (inst fst result))
393                   (inst fxch value)))))))
394
395 (define-vop (%set-sap-ref-single-with-offset-c)
396   (:translate sb!c::%set-sap-ref-single-with-offset)
397   (:policy :fast-safe)
398   (:args (sap :scs (sap-reg) :to (:eval 0))
399          (value :scs (single-reg)))
400   (:arg-types system-area-pointer (:constant (signed-byte 32))
401               (:constant (constant-displacement 0 ; lowtag
402                                                 4 ; single-float size
403                                                 0))
404               single-float)
405   (:info offset disp)
406   (:results (result :scs (single-reg)))
407   (:result-types single-float)
408   (:generator 4
409     (aver (zerop disp))
410     (cond ((zerop (tn-offset value))
411            ;; Value is in ST0
412            (inst fst (make-ea :dword :base sap :disp offset))
413            (unless (zerop (tn-offset result))
414              ;; Value is in ST0 but not result.
415              (inst fst result)))
416           (t
417            ;; Value is not in ST0.
418            (inst fxch value)
419            (inst fst (make-ea :dword :base sap :disp offset))
420            (cond ((zerop (tn-offset result))
421                   ;; The result is in ST0.
422                   (inst fst value))
423                  (t
424                   ;; Neither value or result are in ST0
425                   (unless (location= value result)
426                     (inst fst result))
427                   (inst fxch value)))))))
428 \f
429 ;;;; SAP-REF-LONG
430
431 (define-vop (sap-ref-long)
432   (:translate sap-ref-long)
433   (:policy :fast-safe)
434   (:args (sap :scs (sap-reg))
435          (offset :scs (signed-reg)))
436   (:arg-types system-area-pointer signed-num)
437   (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
438   (:result-types #!+long-float long-float #!-long-float double-float)
439   (:generator 5
440      (with-empty-tn@fp-top(result)
441         (inst fldl (make-ea :dword :base sap :index offset)))))
442
443 (define-vop (sap-ref-long-c)
444   (:translate sap-ref-long)
445   (:policy :fast-safe)
446   (:args (sap :scs (sap-reg)))
447   (:arg-types system-area-pointer (:constant (signed-byte 32)))
448   (:info offset)
449   (:results (result :scs (#!+long-float long-reg #!-long-float double-reg)))
450   (:result-types #!+long-float long-float #!-long-float double-float)
451   (:generator 4
452      (with-empty-tn@fp-top(result)
453         (inst fldl (make-ea :dword :base sap :disp offset)))))
454
455 #!+long-float
456 (define-vop (%set-sap-ref-long)
457   (:translate %set-sap-ref-long)
458   (:policy :fast-safe)
459   (:args (sap :scs (sap-reg) :to (:eval 0))
460          (offset :scs (signed-reg) :to (:eval 0))
461          (value :scs (long-reg)))
462   (:arg-types system-area-pointer signed-num long-float)
463   (:results (result :scs (long-reg)))
464   (:result-types long-float)
465   (:generator 5
466     (cond ((zerop (tn-offset value))
467            ;; Value is in ST0
468            (store-long-float (make-ea :dword :base sap :index offset))
469            (unless (zerop (tn-offset result))
470              ;; Value is in ST0 but not result.
471              (inst fstd result)))
472           (t
473            ;; Value is not in ST0.
474            (inst fxch value)
475            (store-long-float (make-ea :dword :base sap :index offset))
476            (cond ((zerop (tn-offset result))
477                   ;; The result is in ST0.
478                   (inst fstd value))
479                  (t
480                   ;; Neither value or result are in ST0
481                   (unless (location= value result)
482                     (inst fstd result))
483                   (inst fxch value)))))))
484 \f
485 ;;; noise to convert normal lisp data objects into SAPs
486
487 (define-vop (vector-sap)
488   (:translate vector-sap)
489   (:policy :fast-safe)
490   (:args (vector :scs (descriptor-reg) :target sap))
491   (:results (sap :scs (sap-reg)))
492   (:result-types system-area-pointer)
493   (:generator 2
494     (move sap vector)
495     (inst add
496           sap
497           (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))
498
499 ;;; Transforms for 64-bit SAP accessors.
500
501 (deftransform sap-ref-64 ((sap offset) (* *))
502   '(logior (sap-ref-32 sap offset)
503            (ash (sap-ref-32 sap (+ offset 4)) 32)))
504
505 (deftransform signed-sap-ref-64 ((sap offset) (* *))
506   '(logior (sap-ref-32 sap offset)
507            (ash (signed-sap-ref-32 sap (+ offset 4)) 32)))
508
509 (deftransform %set-sap-ref-64 ((sap offset value) (* * *))
510   '(progn
511      (%set-sap-ref-32 sap offset (logand value #xffffffff))
512      (%set-sap-ref-32 sap (+ offset 4) (ash value -32))))
513
514 (deftransform %set-signed-sap-ref-64 ((sap offset value) (* * *))
515   '(progn
516      (%set-sap-ref-32 sap offset (logand value #xffffffff))
517      (%set-signed-sap-ref-32 sap (+ offset 4) (ash value -32))))