1.0.21.19: Eagerly find a stream's external-format when a coding-error occurs.
[sbcl/tcr.git] / src / compiler / x86-64 / sap.lisp
blob9177094b1b4ba600bd42a0eca399ddea4079069a
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 (- (1+ (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
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 32)))
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 32)) ,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))))))))
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))
281 ;;;; SAP-REF-DOUBLE
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 (inst movsd result (make-ea :qword :base sap :index offset))))
294 (define-vop (sap-ref-double-c)
295 (:translate sap-ref-double)
296 (:policy :fast-safe)
297 (:args (sap :scs (sap-reg)))
298 (:arg-types system-area-pointer (:constant (signed-byte 32)))
299 (:info offset)
300 (:results (result :scs (double-reg)))
301 (:result-types double-float)
302 (:generator 4
303 (inst movsd result (make-ea :qword :base sap :disp offset))))
305 (define-vop (%set-sap-ref-double)
306 (:translate %set-sap-ref-double)
307 (:policy :fast-safe)
308 (:args (sap :scs (sap-reg) :to (:eval 0))
309 (offset :scs (signed-reg) :to (:eval 0))
310 (value :scs (double-reg)))
311 (:arg-types system-area-pointer signed-num double-float)
312 (:results (result :scs (double-reg)))
313 (:result-types double-float)
314 (:generator 5
315 (inst movsd (make-ea :qword :base sap :index offset) value)
316 (move result value)))
318 (define-vop (%set-sap-ref-double-c)
319 (:translate %set-sap-ref-double)
320 (:policy :fast-safe)
321 (:args (sap :scs (sap-reg) :to (:eval 0))
322 (value :scs (double-reg)))
323 (:arg-types system-area-pointer (:constant (signed-byte 32)) double-float)
324 (:info offset)
325 (:results (result :scs (double-reg)))
326 (:result-types double-float)
327 (:generator 4
328 (inst movsd (make-ea :qword :base sap :disp offset) value)
329 (move result value)))
331 ;;;; SAP-REF-SINGLE
333 (define-vop (sap-ref-single)
334 (:translate sap-ref-single)
335 (:policy :fast-safe)
336 (:args (sap :scs (sap-reg))
337 (offset :scs (signed-reg)))
338 (:arg-types system-area-pointer signed-num)
339 (:results (result :scs (single-reg)))
340 (:result-types single-float)
341 (:generator 5
342 (inst movss result (make-ea :dword :base sap :index offset))))
344 (define-vop (sap-ref-single-c)
345 (:translate sap-ref-single)
346 (:policy :fast-safe)
347 (:args (sap :scs (sap-reg)))
348 (:arg-types system-area-pointer (:constant (signed-byte 32)))
349 (:info offset)
350 (:results (result :scs (single-reg)))
351 (:result-types single-float)
352 (:generator 4
353 (inst movss result (make-ea :dword :base sap :disp offset))))
355 (define-vop (%set-sap-ref-single)
356 (:translate %set-sap-ref-single)
357 (:policy :fast-safe)
358 (:args (sap :scs (sap-reg) :to (:eval 0))
359 (offset :scs (signed-reg) :to (:eval 0))
360 (value :scs (single-reg)))
361 (:arg-types system-area-pointer signed-num single-float)
362 (:results (result :scs (single-reg)))
363 (:result-types single-float)
364 (:generator 5
365 (inst movss (make-ea :dword :base sap :index offset) value)
366 (move result value)))
368 (define-vop (%set-sap-ref-single-c)
369 (:translate %set-sap-ref-single)
370 (:policy :fast-safe)
371 (:args (sap :scs (sap-reg) :to (:eval 0))
372 (value :scs (single-reg)))
373 (:arg-types system-area-pointer (:constant (signed-byte 32)) single-float)
374 (:info offset)
375 (:results (result :scs (single-reg)))
376 (:result-types single-float)
377 (:generator 4
378 (inst movss (make-ea :dword :base sap :disp offset) value)
379 (move result value)))
382 ;;; noise to convert normal lisp data objects into SAPs
384 (define-vop (vector-sap)
385 (:translate vector-sap)
386 (:policy :fast-safe)
387 (:args (vector :scs (descriptor-reg) :target sap))
388 (:results (sap :scs (sap-reg)))
389 (:result-types system-area-pointer)
390 (:generator 2
391 (move sap vector)
392 (inst add
394 (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))