Eliminate COLD-FSET. It's just fop-funcall of %DEFUN
[sbcl.git] / src / compiler / x86-64 / sap.lisp
blob0da01040d261751f91d32efa8d8793bdecc75942
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 (: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)))
128 (inst mov temp value)
129 (inst lea res (make-ea :qword :base ptr
130 :index temp
131 :scale 1))))))))
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)))
142 (inst mov temp value)
143 (inst add res temp))))))))))
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)))
157 ;;;; mumble-SYSTEM-REF and mumble-SYSTEM-SET
159 (macrolet ((def-system-ref-and-set (ref-name
160 set-name
161 ref-insn
163 type
164 size)
165 (let ((ref-name-c (symbolicate ref-name "-C"))
166 (set-name-c (symbolicate set-name "-C")))
167 `(progn
168 (define-vop (,ref-name)
169 (:translate ,ref-name)
170 (:policy :fast-safe)
171 (:args (sap :scs (sap-reg))
172 (offset :scs (signed-reg)))
173 (:arg-types system-area-pointer signed-num)
174 (:results (result :scs (,sc)))
175 (:result-types ,type)
176 (:generator 5
177 (inst ,ref-insn result
178 (make-ea ,size :base sap :index offset))))
179 (define-vop (,ref-name-c)
180 (:translate ,ref-name)
181 (:policy :fast-safe)
182 (:args (sap :scs (sap-reg)))
183 (:arg-types system-area-pointer
184 (:constant (signed-byte 32)))
185 (:info offset)
186 (:results (result :scs (,sc)))
187 (:result-types ,type)
188 (:generator 4
189 (inst ,ref-insn result
190 (make-ea ,size :base sap :disp offset))))
191 (define-vop (,set-name)
192 (:translate ,set-name)
193 (:policy :fast-safe)
194 (:args (sap :scs (sap-reg) :to (:eval 0))
195 (offset :scs (signed-reg) :to (:eval 0))
196 (value :scs (,sc) :target result))
197 (:arg-types system-area-pointer signed-num ,type)
198 (:results (result :scs (,sc)))
199 (:result-types ,type)
200 (:generator 5
201 (inst mov (make-ea ,size :base sap :index offset)
202 (reg-in-size value ,size))
203 (move result value)))
204 (define-vop (,set-name-c)
205 (:translate ,set-name)
206 (:policy :fast-safe)
207 (:args (sap :scs (sap-reg) :to (:eval 0))
208 (value :scs (,sc) :target result))
209 (:arg-types system-area-pointer
210 (:constant (signed-byte 32)) ,type)
211 (:info offset)
212 (:results (result :scs (,sc)))
213 (:result-types ,type)
214 (:generator 4
215 (inst mov (make-ea ,size :base sap :disp offset)
216 (reg-in-size value ,size))
217 (move result value)))))))
219 (def-system-ref-and-set sap-ref-8 %set-sap-ref-8 movzx
220 unsigned-reg positive-fixnum :byte)
221 (def-system-ref-and-set signed-sap-ref-8 %set-signed-sap-ref-8 movsx
222 signed-reg tagged-num :byte)
223 (def-system-ref-and-set sap-ref-16 %set-sap-ref-16 movzx
224 unsigned-reg positive-fixnum :word)
225 (def-system-ref-and-set signed-sap-ref-16 %set-signed-sap-ref-16 movsx
226 signed-reg tagged-num :word)
227 (def-system-ref-and-set sap-ref-32 %set-sap-ref-32 movzxd
228 unsigned-reg unsigned-num :dword)
229 (def-system-ref-and-set signed-sap-ref-32 %set-signed-sap-ref-32 movsxd
230 signed-reg signed-num :dword)
231 (def-system-ref-and-set sap-ref-64 %set-sap-ref-64 mov
232 unsigned-reg unsigned-num :qword)
233 (def-system-ref-and-set signed-sap-ref-64 %set-signed-sap-ref-64 mov
234 signed-reg signed-num :qword)
235 (def-system-ref-and-set sap-ref-sap %set-sap-ref-sap mov
236 sap-reg system-area-pointer :qword)
237 (def-system-ref-and-set sap-ref-lispobj %set-sap-ref-lispobj mov
238 descriptor-reg * :qword))
240 ;;;; SAP-REF-DOUBLE
242 (define-vop (sap-ref-double)
243 (:translate sap-ref-double)
244 (:policy :fast-safe)
245 (:args (sap :scs (sap-reg))
246 (offset :scs (signed-reg)))
247 (:arg-types system-area-pointer signed-num)
248 (:results (result :scs (double-reg)))
249 (:result-types double-float)
250 (:generator 5
251 (inst movsd result (make-ea :qword :base sap :index offset))))
253 (define-vop (sap-ref-double-c)
254 (:translate sap-ref-double)
255 (:policy :fast-safe)
256 (:args (sap :scs (sap-reg)))
257 (:arg-types system-area-pointer (:constant (signed-byte 32)))
258 (:info offset)
259 (:results (result :scs (double-reg)))
260 (:result-types double-float)
261 (:generator 4
262 (inst movsd result (make-ea :qword :base sap :disp offset))))
264 (define-vop (%set-sap-ref-double)
265 (:translate %set-sap-ref-double)
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 (:arg-types system-area-pointer signed-num double-float)
271 (:results (result :scs (double-reg)))
272 (:result-types double-float)
273 (:generator 5
274 (inst movsd (make-ea :qword :base sap :index offset) value)
275 (move result value)))
277 (define-vop (%set-sap-ref-double-c)
278 (:translate %set-sap-ref-double)
279 (:policy :fast-safe)
280 (:args (sap :scs (sap-reg) :to (:eval 0))
281 (value :scs (double-reg)))
282 (:arg-types system-area-pointer (:constant (signed-byte 32)) double-float)
283 (:info offset)
284 (:results (result :scs (double-reg)))
285 (:result-types double-float)
286 (:generator 4
287 (inst movsd (make-ea :qword :base sap :disp offset) value)
288 (move result value)))
290 ;;;; SAP-REF-SINGLE
292 (define-vop (sap-ref-single)
293 (:translate sap-ref-single)
294 (:policy :fast-safe)
295 (:args (sap :scs (sap-reg))
296 (offset :scs (signed-reg)))
297 (:arg-types system-area-pointer signed-num)
298 (:results (result :scs (single-reg)))
299 (:result-types single-float)
300 (:generator 5
301 (inst movss result (make-ea :dword :base sap :index offset))))
303 (define-vop (sap-ref-single-c)
304 (:translate sap-ref-single)
305 (:policy :fast-safe)
306 (:args (sap :scs (sap-reg)))
307 (:arg-types system-area-pointer (:constant (signed-byte 32)))
308 (:info offset)
309 (:results (result :scs (single-reg)))
310 (:result-types single-float)
311 (:generator 4
312 (inst movss result (make-ea :dword :base sap :disp offset))))
314 (define-vop (%set-sap-ref-single)
315 (:translate %set-sap-ref-single)
316 (:policy :fast-safe)
317 (:args (sap :scs (sap-reg) :to (:eval 0))
318 (offset :scs (signed-reg) :to (:eval 0))
319 (value :scs (single-reg)))
320 (:arg-types system-area-pointer signed-num single-float)
321 (:results (result :scs (single-reg)))
322 (:result-types single-float)
323 (:generator 5
324 (inst movss (make-ea :dword :base sap :index offset) value)
325 (move result value)))
327 (define-vop (%set-sap-ref-single-c)
328 (:translate %set-sap-ref-single)
329 (:policy :fast-safe)
330 (:args (sap :scs (sap-reg) :to (:eval 0))
331 (value :scs (single-reg)))
332 (:arg-types system-area-pointer (:constant (signed-byte 32)) single-float)
333 (:info offset)
334 (:results (result :scs (single-reg)))
335 (:result-types single-float)
336 (:generator 4
337 (inst movss (make-ea :dword :base sap :disp offset) value)
338 (move result value)))
341 ;;; noise to convert normal lisp data objects into SAPs
343 (define-vop (vector-sap)
344 (:translate vector-sap)
345 (:policy :fast-safe)
346 (:args (vector :scs (descriptor-reg) :target sap))
347 (:results (sap :scs (sap-reg)))
348 (:result-types system-area-pointer)
349 (:generator 2
350 (let ((disp (- (* vector-data-offset n-word-bytes) other-pointer-lowtag)))
351 (if (location= sap vector)
352 (inst add sap disp)
353 (inst lea sap (make-ea :qword :base vector :disp disp))))))