1 ;;;; the VOPs and other necessary machine specific support
2 ;;;; routines for call-out to C
4 ;;;; This software is part of the SBCL system. See the README file for
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
15 ;; The MOVE-ARG vop is going to store args on the stack for
16 ;; call-out. These tn's will be used for that. move-arg is normally
17 ;; used for things going down the stack but C wants to have args
18 ;; indexed in the positive direction.
20 (defun my-make-wired-tn (prim-type-name sc-name offset)
21 (make-wired-tn (primitive-type-or-lose prim-type-name)
22 (sc-number-or-lose sc-name)
25 (defstruct (arg-state (:copier nil))
28 (define-alien-type-method (integer :arg-tn) (type state)
29 (let ((stack-frame-size (arg-state-stack-frame-size state)))
30 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
31 (multiple-value-bind (ptype stack-sc)
32 (if (alien-integer-type-signed type)
33 (values 'signed-byte-32 'signed-stack)
34 (values 'unsigned-byte-32 'unsigned-stack))
35 (my-make-wired-tn ptype stack-sc stack-frame-size))))
37 (define-alien-type-method (system-area-pointer :arg-tn) (type state)
38 (declare (ignore type))
39 (let ((stack-frame-size (arg-state-stack-frame-size state)))
40 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
41 (my-make-wired-tn 'system-area-pointer
46 (define-alien-type-method (long-float :arg-tn) (type state)
47 (declare (ignore type))
48 (let ((stack-frame-size (arg-state-stack-frame-size state)))
49 (setf (arg-state-stack-frame-size state) (+ stack-frame-size 3))
50 (my-make-wired-tn 'long-float 'long-stack stack-frame-size)))
52 (define-alien-type-method (double-float :arg-tn) (type state)
53 (declare (ignore type))
54 (let ((stack-frame-size (arg-state-stack-frame-size state)))
55 (setf (arg-state-stack-frame-size state) (+ stack-frame-size 2))
56 (my-make-wired-tn 'double-float 'double-stack stack-frame-size)))
58 (define-alien-type-method (single-float :arg-tn) (type state)
59 (declare (ignore type))
60 (let ((stack-frame-size (arg-state-stack-frame-size state)))
61 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
62 (my-make-wired-tn 'single-float 'single-stack stack-frame-size)))
64 (defstruct (result-state (:copier nil))
67 (defun result-reg-offset (slot)
72 (define-alien-type-method (integer :result-tn) (type state)
73 (let ((num-results (result-state-num-results state)))
74 (setf (result-state-num-results state) (1+ num-results))
75 (multiple-value-bind (ptype reg-sc)
76 (if (alien-integer-type-signed type)
77 (values 'signed-byte-32 'signed-reg)
78 (values 'unsigned-byte-32 'unsigned-reg))
79 (my-make-wired-tn ptype reg-sc (result-reg-offset num-results)))))
81 (define-alien-type-method (system-area-pointer :result-tn) (type state)
82 (declare (ignore type))
83 (let ((num-results (result-state-num-results state)))
84 (setf (result-state-num-results state) (1+ num-results))
85 (my-make-wired-tn 'system-area-pointer 'sap-reg
86 (result-reg-offset num-results))))
89 (define-alien-type-method (long-float :result-tn) (type state)
90 (declare (ignore type))
91 (let ((num-results (result-state-num-results state)))
92 (setf (result-state-num-results state) (1+ num-results))
93 (my-make-wired-tn 'long-float 'long-reg (* num-results 2))))
95 (define-alien-type-method (double-float :result-tn) (type state)
96 (declare (ignore type))
97 (let ((num-results (result-state-num-results state)))
98 (setf (result-state-num-results state) (1+ num-results))
99 (my-make-wired-tn 'double-float 'double-reg (* num-results 2))))
101 (define-alien-type-method (single-float :result-tn) (type state)
102 (declare (ignore type))
103 (let ((num-results (result-state-num-results state)))
104 (setf (result-state-num-results state) (1+ num-results))
105 (my-make-wired-tn 'single-float 'single-reg (* num-results 2))))
107 #+nil ;;pfw obsolete now?
108 (define-alien-type-method (values :result-tn) (type state)
109 (mapcar (lambda (type)
110 (invoke-alien-type-method :result-tn type state))
111 (alien-values-type-values type)))
114 (define-alien-type-method (values :result-tn) (type state)
115 (let ((values (alien-values-type-values type)))
117 (error "Too many result values from c-call."))
119 (invoke-alien-type-method :result-tn (car values) state))))
121 (!def-vm-support-routine make-call-out-tns (type)
122 (let ((arg-state (make-arg-state)))
124 (dolist #+nil ;; this reversed list seems to cause the alien botches!!
125 (arg-type (reverse (alien-fun-type-arg-types type)))
126 (arg-type (alien-fun-type-arg-types type))
127 (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
128 (values (my-make-wired-tn 'positive-fixnum 'any-reg esp-offset)
129 (* (arg-state-stack-frame-size arg-state) n-word-bytes)
131 (invoke-alien-type-method :result-tn
132 (alien-fun-type-result-type type)
133 (make-result-state))))))
136 (deftransform %alien-funcall ((function type &rest args) * * :node node)
137 (aver (sb!c::constant-continuation-p type))
138 (let* ((type (sb!c::continuation-value type))
139 (env (sb!c::node-lexenv node))
140 (arg-types (alien-fun-type-arg-types type))
141 (result-type (alien-fun-type-result-type type)))
142 (aver (= (length arg-types) (length args)))
143 (if (or (some #'(lambda (type)
144 (and (alien-integer-type-p type)
145 (> (sb!alien::alien-integer-type-bits type) 32)))
147 (and (alien-integer-type-p result-type)
148 (> (sb!alien::alien-integer-type-bits result-type) 32)))
149 (collect ((new-args) (lambda-vars) (new-arg-types))
150 (dolist (type arg-types)
151 (let ((arg (gensym)))
153 (cond ((and (alien-integer-type-p type)
154 (> (sb!alien::alien-integer-type-bits type) 32))
155 (new-args `(logand ,arg #xffffffff))
156 (new-args `(ash ,arg -32))
157 (new-arg-types (parse-alien-type '(unsigned 32) env))
158 (if (alien-integer-type-signed type)
159 (new-arg-types (parse-alien-type '(signed 32) env))
160 (new-arg-types (parse-alien-type '(unsigned 32) env))))
163 (new-arg-types type)))))
164 (cond ((and (alien-integer-type-p result-type)
165 (> (sb!alien::alien-integer-type-bits result-type) 32))
166 (let ((new-result-type
167 (let ((sb!alien::*values-type-okay* t))
169 (if (alien-integer-type-signed result-type)
170 '(values (unsigned 32) (signed 32))
171 '(values (unsigned 32) (unsigned 32)))
173 `(lambda (function type ,@(lambda-vars))
174 (declare (ignore type))
175 (multiple-value-bind (low high)
176 (%alien-funcall function
177 ',(make-alien-fun-type
178 :arg-types (new-arg-types)
179 :result-type new-result-type)
181 (logior low (ash high 32))))))
183 `(lambda (function type ,@(lambda-vars))
184 (declare (ignore type))
185 (%alien-funcall function
186 ',(make-alien-fun-type
187 :arg-types (new-arg-types)
188 :result-type result-type)
190 (sb!c::give-up-ir1-transform))))
195 (define-vop (foreign-symbol-address)
196 (:translate foreign-symbol-address)
199 (:arg-types (:constant simple-base-string))
200 (:info foreign-symbol)
201 (:results (res :scs (sap-reg)))
202 (:result-types system-area-pointer)
204 (inst lea res (make-fixup (extern-alien-name foreign-symbol) :foreign))))
206 (define-vop (call-out)
207 (:args (function :scs (sap-reg))
209 (:results (results :more t))
210 ;; eax is already wired
211 (:temporary (:sc unsigned-reg :offset ecx-offset) ecx)
212 (:temporary (:sc unsigned-reg :offset edx-offset) edx)
216 (:ignore args ecx edx)
218 (cond ((policy node (> space speed))
219 (move eax-tn function)
220 (inst call (make-fixup (extern-alien-name "call_into_c") :foreign)))
222 ;; Setup the NPX for C; all the FP registers need to be
223 ;; empty; pop them all.
234 ;; To give the debugger a clue. XX not really internal-error?
235 (note-this-location vop :internal-error)
237 ;; Restore the NPX for lisp.
238 (inst fldz) ; insure no regs are empty
247 (location= (tn-ref-tn results) fr0-tn))
248 ;; The return result is in fr0.
249 (inst fxch fr7-tn) ; move the result back to fr0
250 (inst fldz)) ; insure no regs are empty
253 (define-vop (alloc-number-stack-space)
255 (:results (result :scs (sap-reg any-reg)))
257 (aver (location= result esp-tn))
258 (unless (zerop amount)
259 (let ((delta (logandc2 (+ amount 3) 3)))
260 (inst sub esp-tn delta)))
261 (move result esp-tn)))
263 (define-vop (dealloc-number-stack-space)
266 (unless (zerop amount)
267 (let ((delta (logandc2 (+ amount 3) 3)))
268 (inst add esp-tn delta)))))
270 (define-vop (alloc-alien-stack-space)
272 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
273 (:results (result :scs (sap-reg any-reg)))
276 (aver (not (location= result esp-tn)))
277 (unless (zerop amount)
278 (let ((delta (logandc2 (+ amount 3) 3)))
282 (static-symbol-offset '*alien-stack*)
283 (ash symbol-tls-index-slot word-shift)
284 (- other-pointer-lowtag))))
285 (inst fs-segment-prefix)
286 (inst sub (make-ea :dword :scale 1 :index temp) delta)))
287 (load-tl-symbol-value result *alien-stack*))
290 (aver (not (location= result esp-tn)))
291 (unless (zerop amount)
292 (let ((delta (logandc2 (+ amount 3) 3)))
293 (inst sub (make-ea :dword
295 (static-symbol-offset '*alien-stack*)
296 (ash symbol-value-slot word-shift)
297 (- other-pointer-lowtag)))
299 (load-symbol-value result *alien-stack*)))
301 (define-vop (dealloc-alien-stack-space)
303 #!+sb-thread (:temporary (:sc unsigned-reg) temp)
306 (unless (zerop amount)
307 (let ((delta (logandc2 (+ amount 3) 3)))
311 (static-symbol-offset '*alien-stack*)
312 (ash symbol-tls-index-slot word-shift)
313 (- other-pointer-lowtag))))
314 (inst fs-segment-prefix)
315 (inst add (make-ea :dword :scale 1 :index temp) delta))))
318 (unless (zerop amount)
319 (let ((delta (logandc2 (+ amount 3) 3)))
320 (inst add (make-ea :dword
322 (static-symbol-offset '*alien-stack*)
323 (ash symbol-value-slot word-shift)
324 (- other-pointer-lowtag)))