023c99b8c80c2db71a0f94224e2ba6b6369485b5
[sbcl.git] / src / compiler / x86 / c-call.lisp
1 ;;;; the VOPs and other necessary machine specific support
2 ;;;; routines for call-out to C
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
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.
12
13 (in-package "SB!VM")
14
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.
19
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)
23                  offset))
24
25 (defstruct (arg-state (:copier nil))
26   (stack-frame-size 0))
27
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))))
36
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
42                       'sap-stack
43                       stack-frame-size)))
44
45 #!+long-float
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)))
51
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)))
57
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)))
63
64 (defstruct (result-state (:copier nil))
65   (num-results 0))
66
67 (defun result-reg-offset (slot)
68   (ecase slot
69     (0 eax-offset)
70     (1 edx-offset)))
71
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)))))
80
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))))
87
88 #!+long-float
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))))
94
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))))
100
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))))
106
107 (define-alien-type-method (values :result-tn) (type state)
108   (let ((values (alien-values-type-values type)))
109     (when (> (length values) 2)
110       (error "Too many result values from c-call."))
111     (mapcar (lambda (type)
112               (invoke-alien-type-method :result-tn type state))
113             values)))
114
115 (!def-vm-support-routine make-call-out-tns (type)
116   (let ((arg-state (make-arg-state)))
117     (collect ((arg-tns))
118       (dolist (arg-type (alien-fun-type-arg-types type))
119         (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
120       (values (my-make-wired-tn 'positive-fixnum 'any-reg esp-offset)
121               (* (arg-state-stack-frame-size arg-state) n-word-bytes)
122               (arg-tns)
123               (invoke-alien-type-method :result-tn
124                                         (alien-fun-type-result-type type)
125                                         (make-result-state))))))
126
127
128 (deftransform %alien-funcall ((function type &rest args) * * :node node)
129   (aver (sb!c::constant-lvar-p type))
130   (let* ((type (sb!c::lvar-value type))
131          (env (sb!c::node-lexenv node))
132          (arg-types (alien-fun-type-arg-types type))
133          (result-type (alien-fun-type-result-type type)))
134     (aver (= (length arg-types) (length args)))
135     (if (or (some #'(lambda (type)
136                       (and (alien-integer-type-p type)
137                            (> (sb!alien::alien-integer-type-bits type) 32)))
138                   arg-types)
139             (and (alien-integer-type-p result-type)
140                  (> (sb!alien::alien-integer-type-bits result-type) 32)))
141         (collect ((new-args) (lambda-vars) (new-arg-types))
142           (dolist (type arg-types)
143             (let ((arg (gensym)))
144               (lambda-vars arg)
145               (cond ((and (alien-integer-type-p type)
146                           (> (sb!alien::alien-integer-type-bits type) 32))
147                      (new-args `(logand ,arg #xffffffff))
148                      (new-args `(ash ,arg -32))
149                      (new-arg-types (parse-alien-type '(unsigned 32) env))
150                      (if (alien-integer-type-signed type)
151                          (new-arg-types (parse-alien-type '(signed 32) env))
152                          (new-arg-types (parse-alien-type '(unsigned 32) env))))
153                     (t
154                      (new-args arg)
155                      (new-arg-types type)))))
156           (cond ((and (alien-integer-type-p result-type)
157                       (> (sb!alien::alien-integer-type-bits result-type) 32))
158                  (let ((new-result-type
159                         (let ((sb!alien::*values-type-okay* t))
160                           (parse-alien-type
161                            (if (alien-integer-type-signed result-type)
162                                '(values (unsigned 32) (signed 32))
163                                '(values (unsigned 32) (unsigned 32)))
164                            env))))
165                    `(lambda (function type ,@(lambda-vars))
166                       (declare (ignore type))
167                       (multiple-value-bind (low high)
168                           (%alien-funcall function
169                                           ',(make-alien-fun-type
170                                              :arg-types (new-arg-types)
171                                              :result-type new-result-type)
172                                           ,@(new-args))
173                         (logior low (ash high 32))))))
174                 (t
175                  `(lambda (function type ,@(lambda-vars))
176                     (declare (ignore type))
177                     (%alien-funcall function
178                                     ',(make-alien-fun-type
179                                        :arg-types (new-arg-types)
180                                        :result-type result-type)
181                                     ,@(new-args))))))
182         (sb!c::give-up-ir1-transform))))
183
184 (define-vop (foreign-symbol-sap)
185   (:translate foreign-symbol-sap)
186   (:policy :fast-safe)
187   (:args)
188   (:arg-types (:constant simple-string))
189   (:info foreign-symbol)
190   (:results (res :scs (sap-reg)))
191   (:result-types system-area-pointer)
192   (:generator 2
193    (inst lea res (make-fixup foreign-symbol :foreign))))
194
195 #!+linkage-table
196 (define-vop (foreign-symbol-dataref-sap)
197   (:translate foreign-symbol-dataref-sap)
198   (:policy :fast-safe)
199   (:args)
200   (:arg-types (:constant simple-string))
201   (:info foreign-symbol)
202   (:results (res :scs (sap-reg)))
203   (:result-types system-area-pointer)
204   (:generator 2
205    (inst mov res (make-fixup foreign-symbol :foreign-dataref))))
206
207 (define-vop (call-out)
208   (:args (function :scs (sap-reg))
209          (args :more t))
210   (:results (results :more t))
211   (:temporary (:sc unsigned-reg :offset eax-offset
212                    :from :eval :to :result) eax)
213   (:temporary (:sc unsigned-reg :offset ecx-offset
214                    :from :eval :to :result) ecx)
215   (:temporary (:sc unsigned-reg :offset edx-offset
216                    :from :eval :to :result) edx)
217   (:node-var node)
218   (:vop-var vop)
219   (:save-p t)
220   (:ignore args ecx edx)
221   (:generator 0
222     ;; FIXME & OAOOM: This is brittle and error-prone to maintain two
223     ;; instances of the same logic, on in arch-assem.S, and one in
224     ;; c-call.lisp. If you modify this, modify that one too...
225     (cond ((policy node (> space speed))
226            (move eax function)
227            (inst call (make-fixup "call_into_c" :foreign)))
228           (t
229            ;; Setup the NPX for C; all the FP registers need to be
230            ;; empty; pop them all.
231            (dotimes (i 8)
232              (inst fstp fr0-tn))
233
234            ;; Clear out DF: Darwin, Windows, and Solaris at least require
235            ;; this, and it should not hurt others either.
236            (inst cld)
237
238            (inst call function)
239            ;; To give the debugger a clue. FIXME: not really internal-error?
240            (note-this-location vop :internal-error)
241
242            ;; Restore the NPX for lisp; ensure no regs are empty
243            (dotimes (i 7)
244              (inst fldz))
245
246            (if (and results
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
251            ))))
252
253 ;;; While SBCL uses the FPU in 53-bit mode, most C libraries assume that
254 ;;; the FPU is in 64-bit mode. So we change the FPU mode to 64-bit with
255 ;;; the SET-FPU-WORD-FOR-C VOP before calling out to C and set it back
256 ;;; to 53-bit mode after coming back using the SET-FPU-WORD-FOR-LISP VOP.
257 (define-vop (set-fpu-word-for-c)
258   (:node-var node)
259   (:generator 0
260     (when (policy node (= sb!c::float-accuracy 3))
261       (inst sub esp-tn 4)
262       (inst fnstcw (make-ea :word :base esp-tn))
263       (inst wait)
264       (inst or (make-ea :word :base esp-tn) #x300)
265       (inst fldcw (make-ea :word :base esp-tn))
266       (inst wait))))
267
268 (define-vop (set-fpu-word-for-lisp)
269   (:node-var node)
270   (:generator 0
271     (when (policy node (= sb!c::float-accuracy 3))
272       (inst fnstcw (make-ea :word :base esp-tn))
273       (inst wait)
274       (inst and (make-ea :word :base esp-tn) #xfeff)
275       (inst fldcw (make-ea :word :base esp-tn))
276       (inst wait)
277       (inst add esp-tn 4))))
278
279 (define-vop (alloc-number-stack-space)
280   (:info amount)
281   (:results (result :scs (sap-reg any-reg)))
282   (:generator 0
283     (aver (location= result esp-tn))
284     (unless (zerop amount)
285       (let ((delta (logandc2 (+ amount 3) 3)))
286         (inst sub esp-tn delta)))
287     (align-stack-pointer esp-tn)
288     (move result esp-tn)))
289
290 (define-vop (alloc-alien-stack-space)
291   (:info amount)
292   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
293   (:results (result :scs (sap-reg any-reg)))
294   #!+sb-thread
295   (:generator 0
296     (aver (not (location= result esp-tn)))
297     (unless (zerop amount)
298       (let ((delta (logandc2 (+ amount 3) 3)))
299         (inst mov temp
300               (make-ea-for-symbol-tls-index *alien-stack*))
301         (inst fs-segment-prefix)
302         (inst sub (make-ea :dword :base temp) delta)))
303     (load-tl-symbol-value result *alien-stack*))
304   #!-sb-thread
305   (:generator 0
306     (aver (not (location= result esp-tn)))
307     (unless (zerop amount)
308       (let ((delta (logandc2 (+ amount 3) 3)))
309         (inst sub (make-ea-for-symbol-value *alien-stack*)
310               delta)))
311     (load-symbol-value result *alien-stack*)))
312
313 (define-vop (dealloc-alien-stack-space)
314   (:info amount)
315   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
316   #!+sb-thread
317   (:generator 0
318     (unless (zerop amount)
319       (let ((delta (logandc2 (+ amount 3) 3)))
320         (inst mov temp
321               (make-ea-for-symbol-tls-index *alien-stack*))
322         (inst fs-segment-prefix)
323         (inst add (make-ea :dword :base temp) delta))))
324   #!-sb-thread
325   (:generator 0
326     (unless (zerop amount)
327       (let ((delta (logandc2 (+ amount 3) 3)))
328         (inst add (make-ea-for-symbol-value *alien-stack*)
329               delta)))))
330
331 ;;; not strictly part of the c-call convention, but needed for the
332 ;;; WITH-PINNED-OBJECTS macro used for "locking down" lisp objects so
333 ;;; that GC won't move them while foreign functions go to work.
334 (define-vop (touch-object)
335   (:translate touch-object)
336   (:args (object))
337   (:ignore object)
338   (:policy :fast-safe)
339   (:arg-types t)
340   (:generator 0))
341
342 #-sb-xc-host
343 (defun alien-callback-accessor-form (type sp offset)
344   `(deref (sap-alien (sap+ ,sp ,offset) (* ,type))))
345
346 #-sb-xc-host
347 (defun alien-callback-assembler-wrapper (index return-type arg-types)
348   "Cons up a piece of code which calls call-callback with INDEX and a
349 pointer to the arguments."
350   (declare (ignore arg-types))
351   (let* ((segment (make-segment))
352          (eax eax-tn)
353          (edx edx-tn)
354          (ebp ebp-tn)
355          (esp esp-tn)
356          ([ebp-8] (make-ea :dword :base ebp :disp -8))
357          ([ebp-4] (make-ea :dword :base ebp :disp -4)))
358     (assemble (segment)
359               (inst push ebp)                       ; save old frame pointer
360               (inst mov  ebp esp)                   ; establish new frame
361               (inst mov  eax esp)                   ;
362               (inst sub  eax 8)                     ; place for result
363               (inst push eax)                       ; arg2
364               (inst add  eax 16)                    ; arguments
365               (inst push eax)                       ; arg1
366               (inst push (ash index 2))             ; arg0
367
368               ;; Indirect the access to ENTER-ALIEN-CALLBACK through
369               ;; the symbol-value slot of SB-ALIEN::*ENTER-ALIEN-CALLBACK*
370               ;; to ensure it'll work even if the GC moves ENTER-ALIEN-CALLBACK.
371               ;; Skip any SB-THREAD TLS magic, since we don't expecte anyone
372               ;; to rebind the variable. -- JES, 2006-01-01
373               (load-symbol-value eax sb!alien::*enter-alien-callback*)
374               (inst push eax) ; function
375               (inst mov  eax (foreign-symbol-address "funcall3"))
376               (inst call eax)
377               ;; now put the result into the right register
378               (cond
379                 ((and (alien-integer-type-p return-type)
380                       (eql (alien-type-bits return-type) 64))
381                  (inst mov eax [ebp-8])
382                  (inst mov edx [ebp-4]))
383                 ((or (alien-integer-type-p return-type)
384                      (alien-pointer-type-p return-type)
385                      (alien-type-= #.(parse-alien-type 'system-area-pointer nil)
386                                    return-type))
387                  (inst mov eax [ebp-8]))
388                 ((alien-single-float-type-p return-type)
389                  (inst fld  [ebp-8]))
390                 ((alien-double-float-type-p return-type)
391                  (inst fldd [ebp-8]))
392                 ((alien-void-type-p return-type))
393                 (t
394                  (error "unrecognized alien type: ~A" return-type)))
395               (inst mov esp ebp)                   ; discard frame
396               (inst pop ebp)                       ; restore frame pointer
397               (inst ret))
398     (finalize-segment segment)
399     ;; Now that the segment is done, convert it to a static
400     ;; vector we can point foreign code to.
401     (let ((buffer (sb!assem::segment-buffer segment)))
402       (make-static-vector (length buffer)
403                           :element-type '(unsigned-byte 8)
404                           :initial-contents buffer))))