1.0.27.46: Fix build on systems with "src" in the path.
[sbcl/tcr.git] / src / compiler / mips / sap.lisp
blob144e6186ffb4caabd812c0cd4cb48a5483ef9af2
1 ;;;; the MIPS VM definition of SAP operations
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 (any-reg 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)))
24 (define-move-vop move-to-sap :move
25 (descriptor-reg) (sap-reg))
27 ;;; Move an untagged SAP to a tagged representation.
28 (define-vop (move-from-sap)
29 (:args (sap :scs (sap-reg) :to :save))
30 (:temporary (:scs (non-descriptor-reg)) ndescr)
31 (:temporary (:sc non-descriptor-reg :offset nl4-offset) pa-flag)
32 (:results (res :scs (descriptor-reg)))
33 (:note "SAP to pointer coercion")
34 (:generator 20
35 (with-fixed-allocation (res pa-flag ndescr sap-widetag sap-size nil)
36 (storew sap res sap-pointer-slot other-pointer-lowtag))))
38 (define-move-vop move-from-sap :move
39 (sap-reg) (descriptor-reg))
41 ;;; Move untagged SAP values.
42 (define-vop (sap-move)
43 (:args (x :target y
44 :scs (sap-reg)
45 :load-if (not (location= x y))))
46 (:results (y :scs (sap-reg)
47 :load-if (not (location= x y))))
48 (:note "SAP move")
49 (:effects)
50 (:affected)
51 (:generator 0
52 (move y x)))
54 (define-move-vop sap-move :move
55 (sap-reg) (sap-reg))
57 ;;; Move untagged SAP arguments/return-values.
58 (define-vop (move-sap-arg)
59 (:args (x :target y
60 :scs (sap-reg))
61 (fp :scs (any-reg)
62 :load-if (not (sc-is y sap-reg))))
63 (:results (y))
64 (:note "SAP argument move")
65 (:generator 0
66 (sc-case y
67 (sap-reg
68 (move y x))
69 (sap-stack
70 (storew x fp (tn-offset y))))))
72 (define-move-vop move-sap-arg :move-arg
73 (descriptor-reg sap-reg) (sap-reg))
75 ;;; Use standard MOVE-ARG + coercion to move an untagged SAP to a
76 ;;; descriptor passing location.
77 (define-move-vop move-arg :move-arg
78 (sap-reg) (descriptor-reg))
80 ;;;; SAP-INT and INT-SAP
82 ;;; The function SAP-INT is used to generate an integer corresponding
83 ;;; to the system area pointer, suitable for passing to the kernel
84 ;;; interfaces (which want all addresses specified as integers). The
85 ;;; function INT-SAP is used to do the opposite conversion. The
86 ;;; integer representation of a SAP is the byte offset of the SAP from
87 ;;; the start of the address space.
88 (define-vop (sap-int)
89 (:args (sap :scs (sap-reg) :target int))
90 (:arg-types system-area-pointer)
91 (:results (int :scs (unsigned-reg)))
92 (:result-types unsigned-num)
93 (:translate sap-int)
94 (:policy :fast-safe)
95 (:generator 1
96 (move int sap)))
98 (define-vop (int-sap)
99 (:args (int :scs (unsigned-reg) :target sap))
100 (:arg-types unsigned-num)
101 (:results (sap :scs (sap-reg)))
102 (:result-types system-area-pointer)
103 (:translate int-sap)
104 (:policy :fast-safe)
105 (:generator 1
106 (move sap int)))
108 ;;;; POINTER+ and POINTER-
109 (define-vop (pointer+)
110 (:translate sap+)
111 (:args (ptr :scs (sap-reg))
112 (offset :scs (signed-reg immediate)))
113 (:arg-types system-area-pointer signed-num)
114 (:results (res :scs (sap-reg)))
115 (:result-types system-area-pointer)
116 (:policy :fast-safe)
117 (:generator 1
118 (sc-case offset
119 (signed-reg
120 (inst addu res ptr offset))
121 (immediate
122 (inst addu res ptr (tn-value offset))))))
124 (define-vop (pointer-)
125 (:translate sap-)
126 (:args (ptr1 :scs (sap-reg))
127 (ptr2 :scs (sap-reg)))
128 (:arg-types system-area-pointer system-area-pointer)
129 (:policy :fast-safe)
130 (:results (res :scs (signed-reg)))
131 (:result-types signed-num)
132 (:generator 1
133 (inst subu res ptr1 ptr2)))
135 ;;;; mumble-SYSTEM-REF and mumble-SYSTEM-SET
136 (macrolet ((def-system-ref-and-set
137 (ref-name set-name sc type size &optional signed)
138 (let ((ref-name-c (symbolicate ref-name "-C"))
139 (set-name-c (symbolicate set-name "-C")))
140 `(progn
141 (define-vop (,ref-name)
142 (:translate ,ref-name)
143 (:policy :fast-safe)
144 (:args (object :scs (sap-reg) :target sap)
145 (offset :scs (signed-reg)))
146 (:arg-types system-area-pointer signed-num)
147 (:results (result :scs (,sc)))
148 (:result-types ,type)
149 (:temporary (:scs (sap-reg) :from (:argument 0)) sap)
150 (:generator 5
151 (inst addu sap object offset)
152 ,@(ecase size
153 (:byte
154 (if signed
155 '((inst lb result sap 0))
156 '((inst lbu result sap 0))))
157 (:short
158 (if signed
159 '((inst lh result sap 0))
160 '((inst lhu result sap 0))))
161 (:long
162 '((inst lw result sap 0)))
163 (:single
164 '((inst lwc1 result sap 0)))
165 (:double
166 (ecase *backend-byte-order*
167 (:big-endian
168 '((inst lwc1 result sap n-word-bytes)
169 (inst lwc1-odd result sap 0)))
170 (:little-endian
171 '((inst lwc1 result sap 0)
172 (inst lwc1-odd result sap n-word-bytes))))))
173 (inst nop)))
174 (define-vop (,ref-name-c)
175 (:translate ,ref-name)
176 (:policy :fast-safe)
177 (:args (object :scs (sap-reg)))
178 (:arg-types system-area-pointer
179 (:constant ,(if (eq size :double)
180 ;; We need to be able to add 4.
181 `(integer ,(- (ash 1 16))
182 ,(- (ash 1 16) 5))
183 '(signed-byte 16))))
184 (:info offset)
185 (:results (result :scs (,sc)))
186 (:result-types ,type)
187 (:generator 4
188 ,@(ecase size
189 (:byte
190 (if signed
191 '((inst lb result object offset))
192 '((inst lbu result object offset))))
193 (:short
194 (if signed
195 '((inst lh result object offset))
196 '((inst lhu result object offset))))
197 (:long
198 '((inst lw result object offset)))
199 (:single
200 '((inst lwc1 result object offset)))
201 (:double
202 (ecase *backend-byte-order*
203 (:big-endian
204 '((inst lwc1 result object (+ offset n-word-bytes))
205 (inst lwc1-odd result object offset)))
206 (:little-endian
207 '((inst lwc1 result object offset)
208 (inst lwc1-odd result object (+ offset n-word-bytes)))))))
209 (inst nop)))
210 (define-vop (,set-name)
211 (:translate ,set-name)
212 (:policy :fast-safe)
213 (:args (object :scs (sap-reg) :target sap)
214 (offset :scs (signed-reg))
215 (value :scs (,sc) :target result))
216 (:arg-types system-area-pointer signed-num ,type)
217 (:results (result :scs (,sc)))
218 (:result-types ,type)
219 (:temporary (:scs (sap-reg) :from (:argument 0)) sap)
220 (:generator 5
221 (inst addu sap object offset)
222 ,@(ecase size
223 (:byte
224 '((inst sb value sap 0)
225 (move result value)))
226 (:short
227 '((inst sh value sap 0)
228 (move result value)))
229 (:long
230 '((inst sw value sap 0)
231 (move result value)))
232 (:single
233 '((inst swc1 value sap 0)
234 (unless (location= result value)
235 (inst fmove :single result value))))
236 (:double
237 (ecase *backend-byte-order*
238 (:big-endian
239 '((inst swc1 value sap n-word-bytes)
240 (inst swc1-odd value sap 0)
241 (unless (location= result value)
242 (inst fmove :double result value))))
243 (:little-endian
244 '((inst swc1 value sap 0)
245 (inst swc1-odd value sap n-word-bytes)
246 (unless (location= result value)
247 (inst fmove :double result value)))))))))
248 (define-vop (,set-name-c)
249 (:translate ,set-name)
250 (:policy :fast-safe)
251 (:args (object :scs (sap-reg))
252 (value :scs (,sc) :target result))
253 (:arg-types system-area-pointer
254 (:constant ,(if (eq size :double)
255 ;; We need to be able to add 4.
256 `(integer ,(- (ash 1 16))
257 ,(- (ash 1 16) 5))
258 '(signed-byte 16)))
259 ,type)
260 (:info offset)
261 (:results (result :scs (,sc)))
262 (:result-types ,type)
263 (:generator 4
264 ,@(ecase size
265 (:byte
266 '((inst sb value object offset)
267 (move result value)))
268 (:short
269 '((inst sh value object offset)
270 (move result value)))
271 (:long
272 '((inst sw value object offset)
273 (move result value)))
274 (:single
275 '((inst swc1 value object offset)
276 (unless (location= result value)
277 (inst fmove :single result value))))
278 (:double
279 (ecase *backend-byte-order*
280 (:big-endian
281 '((inst swc1 value object (+ offset n-word-bytes))
282 (inst swc1-odd value object (+ offset n-word-bytes))
283 (unless (location= result value)
284 (inst fmove :double result value))))
285 (:little-endian
286 '((inst swc1 value object offset)
287 (inst swc1-odd value object (+ offset n-word-bytes))
288 (unless (location= result value)
289 (inst fmove :double result value)))))))))))))
290 (def-system-ref-and-set sap-ref-8 %set-sap-ref-8
291 unsigned-reg positive-fixnum :byte nil)
292 (def-system-ref-and-set signed-sap-ref-8 %set-signed-sap-ref-8
293 signed-reg tagged-num :byte t)
294 (def-system-ref-and-set sap-ref-16 %set-sap-ref-16
295 unsigned-reg positive-fixnum :short nil)
296 (def-system-ref-and-set signed-sap-ref-16 %set-signed-sap-ref-16
297 signed-reg tagged-num :short t)
298 (def-system-ref-and-set sap-ref-32 %set-sap-ref-32
299 unsigned-reg unsigned-num :long nil)
300 (def-system-ref-and-set signed-sap-ref-32 %set-signed-sap-ref-32
301 signed-reg signed-num :long t)
302 (def-system-ref-and-set sap-ref-sap %set-sap-ref-sap
303 sap-reg system-area-pointer :long)
304 (def-system-ref-and-set sap-ref-single %set-sap-ref-single
305 single-reg single-float :single)
306 (def-system-ref-and-set sap-ref-double %set-sap-ref-double
307 double-reg double-float :double))
309 ;;; Noise to convert normal lisp data objects into SAPs.
310 (define-vop (vector-sap)
311 (:translate vector-sap)
312 (:policy :fast-safe)
313 (:args (vector :scs (descriptor-reg)))
314 (:results (sap :scs (sap-reg)))
315 (:result-types system-area-pointer)
316 (:generator 2
317 (inst addu sap vector
318 (- (* vector-data-offset n-word-bytes) other-pointer-lowtag))))
320 ;;; Transforms for 64-bit SAP accessors.
321 #!+little-endian
322 (progn
323 (deftransform sap-ref-64 ((sap offset) (* *))
324 '(logior (sap-ref-32 sap offset)
325 (ash (sap-ref-32 sap (+ offset 4)) 32)))
327 (deftransform signed-sap-ref-64 ((sap offset) (* *))
328 '(logior (sap-ref-32 sap offset)
329 (ash (signed-sap-ref-32 sap (+ offset 4)) 32)))
331 (deftransform %set-sap-ref-64 ((sap offset value) (* * *))
332 '(progn
333 (%set-sap-ref-32 sap offset (logand value #xffffffff))
334 (%set-sap-ref-32 sap (+ offset 4) (ash value -32))))
336 (deftransform %set-signed-sap-ref-64 ((sap offset value) (* * *))
337 '(progn
338 (%set-sap-ref-32 sap offset (logand value #xffffffff))
339 (%set-signed-sap-ref-32 sap (+ offset 4) (ash value -32)))))
341 #!-little-endian
342 (progn
343 (deftransform sap-ref-64 ((sap offset) (* *))
344 '(logior (ash (sap-ref-32 sap offset) 32)
345 (sap-ref-32 sap (+ offset 4))))
347 (deftransform signed-sap-ref-64 ((sap offset) (* *))
348 '(logior (ash (signed-sap-ref-32 sap offset) 32)
349 (sap-ref-32 sap (+ 4 offset))))
351 (deftransform %set-sap-ref-64 ((sap offset value) (* * *))
352 '(progn
353 (%set-sap-ref-32 sap offset (ash value -32))
354 (%set-sap-ref-32 sap (+ offset 4) (logand value #xffffffff))))
356 (deftransform %set-signed-sap-ref-64 ((sap offset value) (* * *))
357 '(progn
358 (%set-signed-sap-ref-32 sap offset (ash value -32))
359 (%set-sap-ref-32 sap (+ 4 offset) (logand value #xffffffff)))))