1 ;;;; VOPs and other machine-specific support routines for call-out to C
3 ;;;; This software is part of the SBCL system. See the README file for
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.
14 (defun my-make-wired-tn (prim-type-name sc-name offset)
15 (make-wired-tn (primitive-type-or-lose prim-type-name)
16 (sc-number-or-lose sc-name)
24 (define-alien-type-method (integer :arg-tn) (type state)
25 (let ((stack-frame-size (arg-state-stack-frame-size state)))
26 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
27 (setf (arg-state-did-int-arg state) t)
29 (ptype reg-sc stack-sc)
30 (if (alien-integer-type-signed type)
31 (values 'signed-byte-32 'signed-reg 'signed-stack)
32 (values 'unsigned-byte-32 'unsigned-reg 'unsigned-stack))
33 (if (< stack-frame-size 4)
34 (my-make-wired-tn ptype reg-sc (+ stack-frame-size 4))
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 (setf (arg-state-did-int-arg state) t)
42 (if (< stack-frame-size 4)
43 (my-make-wired-tn 'system-area-pointer
45 (+ stack-frame-size 4))
46 (my-make-wired-tn 'system-area-pointer
50 (define-alien-type-method (double-float :arg-tn) (type state)
51 (declare (ignore type))
52 (let ((stack-frame-size (logandc2 (1+ (arg-state-stack-frame-size state)) 1))
53 (float-args (arg-state-float-args state)))
54 (setf (arg-state-stack-frame-size state) (+ stack-frame-size 2))
55 (setf (arg-state-float-args state) (1+ float-args))
56 (cond ((>= stack-frame-size 4)
57 (my-make-wired-tn 'double-float
60 ((and (not (arg-state-did-int-arg state))
62 (my-make-wired-tn 'double-float
64 (+ (* float-args 2) 12)))
66 (my-make-wired-tn 'double-float
68 (+ stack-frame-size 4))))))
70 (define-alien-type-method (single-float :arg-tn) (type state)
71 (declare (ignore type))
72 (let ((stack-frame-size (arg-state-stack-frame-size state))
73 (float-args (arg-state-float-args state)))
74 (setf (arg-state-stack-frame-size state) (1+ stack-frame-size))
75 (setf (arg-state-float-args state) (1+ float-args))
76 (cond ((>= stack-frame-size 4)
77 (my-make-wired-tn 'single-float
80 ((and (not (arg-state-did-int-arg state))
82 (my-make-wired-tn 'single-float
84 (+ (* float-args 2) 12)))
86 (my-make-wired-tn 'single-float
88 (+ stack-frame-size 4))))))
90 (defstruct result-state
93 (defun offset-for-result (n)
100 (define-alien-type-method (integer :result-tn) (type state)
101 (let ((num-results (result-state-num-results state)))
102 (setf (result-state-num-results state) (1+ num-results))
105 (if (alien-integer-type-signed type)
106 (values 'signed-byte-32 'signed-reg)
107 (values 'unsigned-byte-32 'unsigned-reg))
108 (my-make-wired-tn ptype reg-sc (offset-for-result num-results)))))
110 (define-alien-type-method (system-area-pointer :result-tn) (type state)
111 (declare (ignore type))
112 (let ((num-results (result-state-num-results state)))
113 (setf (result-state-num-results state) (1+ num-results))
114 (my-make-wired-tn 'system-area-pointer 'sap-reg (offset-for-result num-results))))
116 ;;; FIXME: do these still work? -- CSR, 2002-08-28
117 (define-alien-type-method (double-float :result-tn) (type state)
118 (declare (ignore type))
119 (let ((num-results (result-state-num-results state)))
120 (setf (result-state-num-results state) (1+ num-results))
121 (my-make-wired-tn 'double-float 'double-reg (* num-results 2))))
123 (define-alien-type-method (single-float :result-tn) (type state)
124 (declare (ignore type))
125 (let ((num-results (result-state-num-results state)))
126 (setf (result-state-num-results state) (1+ num-results))
127 (my-make-wired-tn 'single-float 'single-reg (* num-results 2))))
129 (define-alien-type-method (values :result-tn) (type state)
130 (mapcar #'(lambda (type)
131 (invoke-alien-type-method :result-tn type state))
132 (alien-values-type-values type)))
134 (!def-vm-support-routine make-call-out-tns (type)
135 (let ((arg-state (make-arg-state)))
137 (dolist (arg-type (alien-fun-type-arg-types type))
138 (arg-tns (invoke-alien-type-method :arg-tn arg-type arg-state)))
139 (values (my-make-wired-tn 'positive-fixnum 'any-reg nsp-offset)
140 (* (max (arg-state-stack-frame-size arg-state) 4) n-word-bytes)
142 (invoke-alien-type-method :result-tn
143 (alien-fun-type-result-type type)
144 (make-result-state))))))
146 (define-vop (foreign-symbol-address)
147 (:translate foreign-symbol-address)
150 (:arg-types (:constant simple-string))
151 (:info foreign-symbol)
152 (:results (res :scs (sap-reg)))
153 (:result-types system-area-pointer)
155 (inst li res (make-fixup foreign-symbol :foreign))))
157 (define-vop (call-out)
158 (:args (function :scs (sap-reg) :target cfunc)
160 (:results (results :more t))
161 (:ignore args results)
163 (:temporary (:sc any-reg :offset cfunc-offset
164 :from (:argument 0) :to (:result 0)) cfunc)
165 (:temporary (:sc control-stack :offset nfp-save-offset) nfp-save)
168 (let ((cur-nfp (current-nfp-tn vop)))
170 (store-stack-tn nfp-save cur-nfp))
171 (move cfunc function)
172 (inst jal (make-fixup "call_into_c" :foreign))
175 (load-stack-tn cur-nfp nfp-save)))))
177 (define-vop (alloc-number-stack-space)
179 (:results (result :scs (sap-reg any-reg)))
180 (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
182 (unless (zerop amount)
183 (let ((delta (logandc2 (+ amount 7) 7)))
184 (cond ((< delta (ash 1 15))
185 (inst subu nsp-tn delta))
188 (inst subu nsp-tn temp)))))
189 (move result nsp-tn)))
191 (define-vop (dealloc-number-stack-space)
194 (:temporary (:scs (unsigned-reg) :to (:result 0)) temp)
196 (unless (zerop amount)
197 (let ((delta (logandc2 (+ amount 7) 7)))
198 (cond ((< delta (ash 1 15))
199 (inst addu nsp-tn delta))
202 (inst addu nsp-tn temp)))))))