Add STDCALL alien convention support for Windows
[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 (integer :naturalize-gen) (type alien)
82   (if (<= (alien-type-bits type) 16)
83       (if (alien-integer-type-signed type)
84           `(sign-extend ,alien ,(alien-type-bits type))
85           `(logand ,alien ,(1- (ash 1 (alien-type-bits type)))))
86       alien))
87
88 (define-alien-type-method (system-area-pointer :result-tn) (type state)
89   (declare (ignore type))
90   (let ((num-results (result-state-num-results state)))
91     (setf (result-state-num-results state) (1+ num-results))
92     (my-make-wired-tn 'system-area-pointer 'sap-reg
93                       (result-reg-offset num-results))))
94
95 #!+long-float
96 (define-alien-type-method (long-float :result-tn) (type state)
97   (declare (ignore type))
98   (let ((num-results (result-state-num-results state)))
99     (setf (result-state-num-results state) (1+ num-results))
100     (my-make-wired-tn 'long-float 'long-reg (* num-results 2))))
101
102 (define-alien-type-method (double-float :result-tn) (type state)
103   (declare (ignore type))
104   (let ((num-results (result-state-num-results state)))
105     (setf (result-state-num-results state) (1+ num-results))
106     (my-make-wired-tn 'double-float 'double-reg (* num-results 2))))
107
108 (define-alien-type-method (single-float :result-tn) (type state)
109   (declare (ignore type))
110   (let ((num-results (result-state-num-results state)))
111     (setf (result-state-num-results state) (1+ num-results))
112     (my-make-wired-tn 'single-float 'single-reg (* num-results 2))))
113
114 (define-alien-type-method (values :result-tn) (type state)
115   (let ((values (alien-values-type-values type)))
116     (when (> (length values) 2)
117       (error "Too many result values from c-call."))
118     (mapcar (lambda (type)
119               (invoke-alien-type-method :result-tn type state))
120             values)))
121
122 (!def-vm-support-routine make-call-out-tns (type)
123   (let ((arg-state (make-arg-state)))
124     (collect ((arg-tns))
125       (dolist (arg-type (alien-fun-type-arg-types type))
126         (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
127       (values (my-make-wired-tn 'positive-fixnum 'any-reg esp-offset)
128               (* (arg-state-stack-frame-size arg-state) n-word-bytes)
129               (arg-tns)
130               (invoke-alien-type-method :result-tn
131                                         (alien-fun-type-result-type type)
132                                         (make-result-state))))))
133
134
135 (deftransform %alien-funcall ((function type &rest args) * * :node node)
136   (aver (sb!c::constant-lvar-p type))
137   (let* ((type (sb!c::lvar-value type))
138          (env (sb!c::node-lexenv node))
139          (arg-types (alien-fun-type-arg-types type))
140          (result-type (alien-fun-type-result-type type)))
141     (aver (= (length arg-types) (length args)))
142     (if (or (some #'(lambda (type)
143                       (and (alien-integer-type-p type)
144                            (> (sb!alien::alien-integer-type-bits type) 32)))
145                   arg-types)
146             (and (alien-integer-type-p result-type)
147                  (> (sb!alien::alien-integer-type-bits result-type) 32)))
148         (collect ((new-args) (lambda-vars) (new-arg-types))
149           (dolist (type arg-types)
150             (let ((arg (gensym)))
151               (lambda-vars arg)
152               (cond ((and (alien-integer-type-p type)
153                           (> (sb!alien::alien-integer-type-bits type) 32))
154                      (new-args `(logand ,arg #xffffffff))
155                      (new-args `(ash ,arg -32))
156                      (new-arg-types (parse-alien-type '(unsigned 32) env))
157                      (if (alien-integer-type-signed type)
158                          (new-arg-types (parse-alien-type '(signed 32) env))
159                          (new-arg-types (parse-alien-type '(unsigned 32) env))))
160                     (t
161                      (new-args arg)
162                      (new-arg-types type)))))
163           (cond ((and (alien-integer-type-p result-type)
164                       (> (sb!alien::alien-integer-type-bits result-type) 32))
165                  (let ((new-result-type
166                         (let ((sb!alien::*values-type-okay* t))
167                           (parse-alien-type
168                            (if (alien-integer-type-signed result-type)
169                                '(values (unsigned 32) (signed 32))
170                                '(values (unsigned 32) (unsigned 32)))
171                            env))))
172                    `(lambda (function type ,@(lambda-vars))
173                       (declare (ignore type))
174                       (multiple-value-bind (low high)
175                           (%alien-funcall function
176                                           ',(make-alien-fun-type
177                                              :arg-types (new-arg-types)
178                                              :result-type new-result-type)
179                                           ,@(new-args))
180                         (logior low (ash high 32))))))
181                 (t
182                  `(lambda (function type ,@(lambda-vars))
183                     (declare (ignore type))
184                     (%alien-funcall function
185                                     ',(make-alien-fun-type
186                                        :arg-types (new-arg-types)
187                                        :result-type result-type)
188                                     ,@(new-args))))))
189         (sb!c::give-up-ir1-transform))))
190
191 ;;; The ABI is vague about how signed sub-word integer return values
192 ;;; are handled, but since gcc versions >=4.3 no longer do sign
193 ;;; extension in the callee, we need to do it in the caller.  FIXME:
194 ;;; If the value to be extended is known to already be of the target
195 ;;; type at compile time, we can (and should) elide the extension.
196 (defknown sign-extend ((signed-byte 32) t) fixnum
197     (foldable flushable movable))
198
199 (define-vop (sign-extend)
200   (:translate sign-extend)
201   (:policy :fast-safe)
202   ;; Need to wire this to EAX since in x86 some dword registers don't
203   ;; have a matching word or byte register.
204   (:args (val :scs (signed-reg) :target eax))
205   (:temporary (:sc signed-reg :offset eax-offset :from :eval :to :result) eax)
206   (:arg-types signed-num (:constant fixnum))
207   (:info size)
208   (:results (res :scs (signed-reg)))
209   (:result-types fixnum)
210   (:ignore eax)
211   (:generator 1
212    (inst movsx res
213          (make-random-tn :kind :normal
214                          :sc (sc-or-lose (ecase size
215                                            (8 'byte-reg)
216                                            (16 'word-reg)))
217                          :offset (tn-offset val)))))
218
219 #-sb-xc-host
220 (defun sign-extend (x size)
221   (declare (type (signed-byte 32) x))
222   (ecase size
223     (8 (sign-extend x size))
224     (16 (sign-extend x size))))
225
226 #+sb-xc-host
227 (defun sign-extend (x size)
228   (if (logbitp (1- size) x)
229       (dpb x (byte size 0) -1)
230       x))
231
232 (define-vop (foreign-symbol-sap)
233   (:translate foreign-symbol-sap)
234   (:policy :fast-safe)
235   (:args)
236   (:arg-types (:constant simple-string))
237   (:info foreign-symbol)
238   (:results (res :scs (sap-reg)))
239   (:result-types system-area-pointer)
240   (:generator 2
241    (inst lea res (make-fixup foreign-symbol :foreign))))
242
243 #!+linkage-table
244 (define-vop (foreign-symbol-dataref-sap)
245   (:translate foreign-symbol-dataref-sap)
246   (:policy :fast-safe)
247   (:args)
248   (:arg-types (:constant simple-string))
249   (:info foreign-symbol)
250   (:results (res :scs (sap-reg)))
251   (:result-types system-area-pointer)
252   (:generator 2
253    (inst mov res (make-fixup foreign-symbol :foreign-dataref))))
254
255 (define-vop (call-out)
256   (:args (function :scs (sap-reg))
257          (args :more t))
258   (:results (results :more t))
259   (:temporary (:sc unsigned-reg :offset eax-offset
260                    :from :eval :to :result) eax)
261   (:temporary (:sc unsigned-reg :offset ecx-offset
262                    :from :eval :to :result) ecx)
263   (:temporary (:sc unsigned-reg :offset edx-offset
264                    :from :eval :to :result) edx)
265   #!+sb-safepoint (:temporary (:sc unsigned-reg :offset esi-offset) esi)
266   #!+sb-safepoint (:temporary (:sc unsigned-reg :offset edi-offset) edi)
267   #!-sb-safepoint (:node-var node)
268   (:vop-var vop)
269   (:save-p t)
270   (:ignore args ecx edx
271            #!+sb-safepoint esi
272            #!+sb-safepoint edi)
273   (:generator 0
274     ;; FIXME & OAOOM: This is brittle and error-prone to maintain two
275     ;; instances of the same logic, on in arch-assem.S, and one in
276     ;; c-call.lisp. If you modify this, modify that one too...
277     (cond ((and
278             ;; On safepoints builds, we currently use the out-of-line
279             ;; calling routine irrespectively of SPACE and SPEED policy.
280             ;; An inline version of said changes is left to the
281             ;; sufficiently motivated maintainer.
282             #!-sb-safepoint (policy node (> space speed)))
283            (move eax function)
284            (inst call (make-fixup "call_into_c" :foreign)))
285           (t
286            ;; Setup the NPX for C; all the FP registers need to be
287            ;; empty; pop them all.
288            (dotimes (i 8)
289              (inst fstp fr0-tn))
290
291            ;; Clear out DF: Darwin, Windows, and Solaris at least require
292            ;; this, and it should not hurt others either.
293            (inst cld)
294
295            (inst call function)
296            ;; To give the debugger a clue. FIXME: not really internal-error?
297            (note-this-location vop :internal-error)
298
299            ;; Restore the NPX for lisp; ensure no regs are empty
300            (dotimes (i 7)
301              (inst fldz))
302
303            (if (and results
304                     (location= (tn-ref-tn results) fr0-tn))
305                ;; The return result is in fr0.
306                (inst fxch fr7-tn)       ; move the result back to fr0
307                (inst fldz))             ; insure no regs are empty
308            ))))
309
310 ;;; While SBCL uses the FPU in 53-bit mode, most C libraries assume that
311 ;;; the FPU is in 64-bit mode. So we change the FPU mode to 64-bit with
312 ;;; the SET-FPU-WORD-FOR-C VOP before calling out to C and set it back
313 ;;; to 53-bit mode after coming back using the SET-FPU-WORD-FOR-LISP VOP.
314 (define-vop (set-fpu-word-for-c)
315   (:node-var node)
316   (:generator 0
317     (when (policy node (= sb!c::float-accuracy 3))
318       (inst sub esp-tn 4)
319       (inst fnstcw (make-ea :word :base esp-tn))
320       (inst wait)
321       (inst or (make-ea :word :base esp-tn) #x300)
322       (inst fldcw (make-ea :word :base esp-tn))
323       (inst wait))))
324
325 (define-vop (set-fpu-word-for-lisp)
326   (:node-var node)
327   (:generator 0
328     (when (policy node (= sb!c::float-accuracy 3))
329       (inst fnstcw (make-ea :word :base esp-tn))
330       (inst wait)
331       (inst and (make-ea :word :base esp-tn) #xfeff)
332       (inst fldcw (make-ea :word :base esp-tn))
333       (inst wait)
334       (inst add esp-tn 4))))
335
336 (define-vop (alloc-number-stack-space)
337   (:info amount)
338   (:results (result :scs (sap-reg any-reg)))
339   (:result-types system-area-pointer)
340   (:generator 0
341     (aver (location= result esp-tn))
342     (unless (zerop amount)
343       (let ((delta (logandc2 (+ amount 3) 3)))
344         (inst sub esp-tn delta)))
345     (align-stack-pointer esp-tn)
346     (move result esp-tn)))
347
348 (define-vop (alloc-alien-stack-space)
349   (:info amount)
350   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
351   (:results (result :scs (sap-reg any-reg)))
352   (:result-types system-area-pointer)
353   #!+sb-thread
354   (:generator 0
355     (aver (not (location= result esp-tn)))
356     (unless (zerop amount)
357       (let ((delta (logandc2 (+ amount 3) 3)))
358         (with-tls-ea (EA :base temp
359                          :disp-type :index
360                          :disp (make-ea-for-symbol-tls-index *alien-stack*))
361           (inst sub EA delta :maybe-fs))))
362     (load-tl-symbol-value result *alien-stack*))
363   #!-sb-thread
364   (:generator 0
365     (aver (not (location= result esp-tn)))
366     (unless (zerop amount)
367       (let ((delta (logandc2 (+ amount 3) 3)))
368         (inst sub (make-ea-for-symbol-value *alien-stack*)
369               delta)))
370     (load-symbol-value result *alien-stack*)))
371
372 (define-vop (dealloc-alien-stack-space)
373   (:info amount)
374   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
375   #!+sb-thread
376   (:generator 0
377     (unless (zerop amount)
378       (let ((delta (logandc2 (+ amount 3) 3)))
379         (with-tls-ea (EA :base temp
380                          :disp-type :index
381                          :disp (make-ea-for-symbol-tls-index *alien-stack*))
382           (inst add EA delta :maybe-fs)))))
383   #!-sb-thread
384   (:generator 0
385     (unless (zerop amount)
386       (let ((delta (logandc2 (+ amount 3) 3)))
387         (inst add (make-ea-for-symbol-value *alien-stack*)
388               delta)))))
389
390 ;;; not strictly part of the c-call convention, but needed for the
391 ;;; WITH-PINNED-OBJECTS macro used for "locking down" lisp objects so
392 ;;; that GC won't move them while foreign functions go to work.
393 (define-vop (touch-object)
394   (:translate touch-object)
395   (:args (object))
396   (:ignore object)
397   (:policy :fast-safe)
398   (:arg-types t)
399   (:generator 0))
400
401 #-sb-xc-host
402 (defun alien-callback-accessor-form (type sp offset)
403   `(deref (sap-alien (sap+ ,sp ,offset) (* ,type))))
404
405 #-sb-xc-host
406 (defun alien-callback-assembler-wrapper
407     (index return-type arg-types &optional (stack-offset 0))
408   "Cons up a piece of code which calls call-callback with INDEX and a
409 pointer to the arguments."
410   (declare (ignore arg-types))
411   (let* ((segment (make-segment))
412          (eax eax-tn)
413          (edx edx-tn)
414          (ebp ebp-tn)
415          (esp esp-tn)
416          ([ebp-8] (make-ea :dword :base ebp :disp -8))
417          ([ebp-4] (make-ea :dword :base ebp :disp -4)))
418     (assemble (segment)
419               (inst push ebp)                       ; save old frame pointer
420               (inst mov  ebp esp)                   ; establish new frame
421               (inst mov  eax esp)                   ;
422               (inst sub  eax 8)                     ; place for result
423               (inst push eax)                       ; arg2
424               (inst add  eax 16)                    ; arguments
425               (inst push eax)                       ; arg1
426               (inst push (ash index 2))             ; arg0
427
428               #!+sb-safepoint
429               (progn
430                 (inst mov eax (foreign-symbol-address "callback_wrapper_trampoline"))
431                 (inst call eax))
432
433               #!-sb-safepoint
434               (progn
435                 ;; Indirect the access to ENTER-ALIEN-CALLBACK through
436                 ;; the symbol-value slot of SB-ALIEN::*ENTER-ALIEN-CALLBACK*
437                 ;; to ensure it'll work even if the GC moves ENTER-ALIEN-CALLBACK.
438                 ;; Skip any SB-THREAD TLS magic, since we don't expecte anyone
439                 ;; to rebind the variable. -- JES, 2006-01-01
440                 (load-symbol-value eax sb!alien::*enter-alien-callback*)
441                 (inst push eax)         ; function
442                 (inst mov  eax (foreign-symbol-address "funcall3"))
443                 (inst call eax))
444
445               ;; now put the result into the right register
446               (cond
447                 ((and (alien-integer-type-p return-type)
448                       (eql (alien-type-bits return-type) 64))
449                  (inst mov eax [ebp-8])
450                  (inst mov edx [ebp-4]))
451                 ((or (alien-integer-type-p return-type)
452                      (alien-pointer-type-p return-type)
453                      (alien-type-= #.(parse-alien-type 'system-area-pointer nil)
454                                    return-type))
455                  (inst mov eax [ebp-8]))
456                 ((alien-single-float-type-p return-type)
457                  (inst fld  [ebp-8]))
458                 ((alien-double-float-type-p return-type)
459                  (inst fldd [ebp-8]))
460                 ((alien-void-type-p return-type))
461                 (t
462                  (error "unrecognized alien type: ~A" return-type)))
463               (inst mov esp ebp)                   ; discard frame
464               (inst pop ebp)                       ; restore frame pointer
465               (inst ret stack-offset))
466     (finalize-segment segment)
467     ;; Now that the segment is done, convert it to a static
468     ;; vector we can point foreign code to.
469     (let ((buffer (sb!assem::segment-buffer segment)))
470       (make-static-vector (length buffer)
471                           :element-type '(unsigned-byte 8)
472                           :initial-contents buffer))))