0.8.0.78.vector-nil-string.1:
[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 #+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)))
112
113 ;;; pfw - from alpha
114 (define-alien-type-method (values :result-tn) (type state)
115   (let ((values (alien-values-type-values type)))
116     (when (cdr values)
117       (error "Too many result values from c-call."))
118     (when values
119       (invoke-alien-type-method :result-tn (car values) state))))
120
121 (!def-vm-support-routine make-call-out-tns (type)
122   (let ((arg-state (make-arg-state)))
123     (collect ((arg-tns))
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)
130               (arg-tns)
131               (invoke-alien-type-method :result-tn
132                                         (alien-fun-type-result-type type)
133                                         (make-result-state))))))
134
135
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)))
146                   arg-types)
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)))
152               (lambda-vars arg)
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))))
161                     (t
162                      (new-args arg)
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))
168                           (parse-alien-type
169                            (if (alien-integer-type-signed result-type)
170                                '(values (unsigned 32) (signed 32))
171                                '(values (unsigned 32) (unsigned 32)))
172                            env))))
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)
180                                           ,@(new-args))
181                         (logior low (ash high 32))))))
182                 (t
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)
189                                     ,@(new-args))))))
190         (sb!c::give-up-ir1-transform))))
191
192
193
194
195 (define-vop (foreign-symbol-address)
196   (:translate foreign-symbol-address)
197   (:policy :fast-safe)
198   (:args)
199   (:arg-types (:constant simple-base-string))
200   (:info foreign-symbol)
201   (:results (res :scs (sap-reg)))
202   (:result-types system-area-pointer)
203   (:generator 2
204    (inst lea res (make-fixup (extern-alien-name foreign-symbol) :foreign))))
205
206 (define-vop (call-out)
207   (:args (function :scs (sap-reg))
208          (args :more t))
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)
213   (:node-var node)
214   (:vop-var vop)
215   (:save-p t)
216   (:ignore args ecx edx)
217   (:generator 0
218     (cond ((policy node (> space speed))
219            (move eax-tn function)
220            (inst call (make-fixup (extern-alien-name "call_into_c") :foreign)))
221           (t
222            ;; Setup the NPX for C; all the FP registers need to be
223            ;; empty; pop them all.
224            (inst fstp fr0-tn)
225            (inst fstp fr0-tn)
226            (inst fstp fr0-tn)
227            (inst fstp fr0-tn)
228            (inst fstp fr0-tn)
229            (inst fstp fr0-tn)
230            (inst fstp fr0-tn)
231            (inst fstp fr0-tn)
232
233            (inst call function)
234            ;; To give the debugger a clue. XX not really internal-error?
235            (note-this-location vop :internal-error)
236
237            ;; Restore the NPX for lisp.
238            (inst fldz) ; insure no regs are empty
239            (inst fldz)
240            (inst fldz)
241            (inst fldz)
242            (inst fldz)
243            (inst fldz)
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 (define-vop (alloc-number-stack-space)
254   (:info amount)
255   (:results (result :scs (sap-reg any-reg)))
256   (:generator 0
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)))
262
263 (define-vop (dealloc-number-stack-space)
264   (:info amount)
265   (:generator 0
266     (unless (zerop amount)
267       (let ((delta (logandc2 (+ amount 3) 3)))
268         (inst add esp-tn delta)))))
269
270 (define-vop (alloc-alien-stack-space)
271   (:info amount)
272   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
273   (:results (result :scs (sap-reg any-reg)))
274   #!+sb-thread
275   (:generator 0
276     (aver (not (location= result esp-tn)))
277     (unless (zerop amount)
278       (let ((delta (logandc2 (+ amount 3) 3)))
279         (inst mov temp
280               (make-ea :dword
281                        :disp (+ nil-value
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*))
288   #!-sb-thread
289   (:generator 0
290     (aver (not (location= result esp-tn)))
291     (unless (zerop amount)
292       (let ((delta (logandc2 (+ amount 3) 3)))
293         (inst sub (make-ea :dword
294                            :disp (+ nil-value
295                                     (static-symbol-offset '*alien-stack*)
296                                     (ash symbol-value-slot word-shift)
297                                     (- other-pointer-lowtag)))
298               delta)))
299     (load-symbol-value result *alien-stack*)))
300
301 (define-vop (dealloc-alien-stack-space)
302   (:info amount)
303   #!+sb-thread (:temporary (:sc unsigned-reg) temp)
304   #!+sb-thread
305   (:generator 0
306     (unless (zerop amount)
307       (let ((delta (logandc2 (+ amount 3) 3)))
308         (inst mov temp
309               (make-ea :dword
310                            :disp (+ nil-value
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))))
316   #!-sb-thread
317   (:generator 0
318     (unless (zerop amount)
319       (let ((delta (logandc2 (+ amount 3) 3)))
320         (inst add (make-ea :dword
321                            :disp (+ nil-value
322                                     (static-symbol-offset '*alien-stack*)
323                                     (ash symbol-value-slot word-shift)
324                                     (- other-pointer-lowtag)))
325               delta)))))