Debian Codegraph @

lib/ypsilon-compat.ikarus.ss

raw
1 ;;; Ikarus Scheme -- A compiler for R6RS Scheme.
2 ;;; Copyright (C) 2008,2009 Abdulaziz Ghuloum
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License version 3 as
6 ;;; published by the Free Software Foundation.
7 ;;;
8 ;;; This program is distributed in the hope that it will be useful, but
9 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;;; General Public License for more details.
12 ;;;
13 ;;; You should have received a copy of the GNU General Public License
14 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16
17
18 (library (ypsilon-compat)
19 (export on-windows on-darwin on-linux on-freebsd on-posix
20 load-shared-object c-argument c-function
21 microsecond usleep library-pointer
22 (rename (ypsilon:format format)))
23 (import
24 (ikarus system $foreign)
25 (except (ikarus) library))
26
27 (define (microsecond)
28 (let ([t (current-time)])
29 (+ (* (time-second t) 1000000)
30 (div (time-nanosecond t) 1000))))
31
32 (define (usleep . args) (error '#f "invalid args" args))
33
34 (define (ypsilon:format what str . args)
35 (cond
36 [(eq? what #t)
37 (apply printf str args)]
38 [(eq? what #f)
39 (apply format str args)]
40 [else (error 'ypsion:format "invalid what" what)]))
41
42
43 (define (architecture-feature what)
44 (case what
45 [(operating-system) (host-info)]
46 [else (error 'architecture-feature "invalid args" what)]))
47
48 (define (string-contains text s)
49 (define (starts-at? i)
50 (let f ([i i] [j 0])
51 (cond
52 [(= j (string-length s)) #t]
53 [(= i (string-length text)) #f]
54 [else
55 (and (char=? (string-ref text i) (string-ref s j))
56 (f (+ i 1) (+ j 1)))])))
57 (let f ([i 0])
58 (cond
59 [(= i (string-length text)) #f]
60 [(starts-at? i) #t]
61 [else (f (+ i 1))])))
62
63 (define on-windows (and (string-contains (architecture-feature 'operating-system) "windows") #t))
64 (define on-darwin (and (string-contains (architecture-feature 'operating-system) "darwin") #t))
65 (define on-linux (and (string-contains (architecture-feature 'operating-system) "linux") #t))
66 (define on-freebsd (and (string-contains (architecture-feature 'operating-system) "freebsd") #t))
67 (define on-posix (not on-windows))
68
69
70 (define-record-type library (fields name pointer))
71
72 (define (load-shared-object libname)
73 (unless (string? libname)
74 (error 'load-shared-object "library name must be a string"
75 libname))
76 (make-library libname
77 (or (dlopen libname)
78 (error 'load-shared-object (dlerror) libname))))
79
80 (define (int? x) (or (fixnum? x) (bignum? x)))
81
82 (define (check-int who x)
83 (cond
84 [(int? x) x]
85 [else (die who "not an int" x)]))
86
87 (define (vector-andmap f v)
88 (andmap f (vector->list v)))
89
90 (define (check-int* who x)
91 (cond
92 [(and (vector? x) (vector-andmap int? x))
93 (let ([n (vector-length x)])
94 (let ([p (malloc (* n 4))])
95 (let f ([i 0])
96 (cond
97 [(= i n) p]
98 [else
99 (pointer-set-c-int! p (* i 4) (vector-ref x i))
100 (f (+ i 1))]))))]
101 [else (die who "not an int*" x)]))
102
103 (define (check-char* who x)
104 (cond
105 [(string? x)
106 (check-byte* who (string->utf8 x))]
107 [else (die who "not a char*" x)]))
108
109 (define pointer-size
110 (cond
111 [(<= (fixnum-width) 32) 4]
112 [else 8]))
113
114 (define (check-char** who x)
115 (cond
116 [(and (vector? x) (vector-andmap string? x))
117 (let ([n (vector-length x)])
118 (let ([p (malloc (* n pointer-size))])
119 (let f ([i 0])
120 (cond
121 [(= i n) p]
122 [else
123 (pointer-set-c-pointer! p (* i pointer-size)
124 (check-char* who (vector-ref x i)))
125 (f (+ i 1))]))))]
126 [else (die who "not a char**" x)]))
127
128 (define (check-byte* who x)
129 (cond
130 [(bytevector? x)
131 (let ([n (bytevector-length x)])
132 (let ([p (malloc (+ n 1))])
133 (pointer-set-c-char! p n 0)
134 (let f ([i 0])
135 (cond
136 [(= i n) p]
137 [else
138 (pointer-set-c-char! p i (bytevector-u8-ref x i))
139 (f (+ i 1))]))))]
140 [else (die who "not a byte*" x)]))
141
142 (define (check-float who x)
143 (cond
144 [(flonum? x) x]
145 [else (die who "not a flonum" x)]))
146
147 (define (check-double who x)
148 (cond
149 [(flonum? x) x]
150 [else (die who "not a double" x)]))
151
152 (define-syntax check-callback
153 (lambda (x)
154 (syntax-case x ()
155 [(_ foreign-name val return-type (arg-type* ...))
156 #'(let ([t val])
157 (if (procedure? t)
158 ((make-c-callback
159 (convert-type return-type)
160 (list (convert-type arg-type*) ...))
161 t)
162 (error 'foreign-name "not a procedure" t)))])))
163
164 (define-syntax todo
165 (syntax-rules ()
166 [(_ name* ...)
167 (begin
168 (define (name* . args) (error 'name* "not implemented"))
169 ...)]))
170
171 (define (check-void* who x)
172 (cond
173 [(pointer? x) x]
174 [else (die who "not a void*" x)]))
175
176 (define-syntax convert-arg
177 (lambda (x)
178 (syntax-case x (int char* byte* c-callback float double void*)
179 [(_ form foreign-name val char*)
180 #'(check-char* 'foreign-name val)]
181 [(_ form foreign-name val byte*)
182 #'(check-byte* 'foreign-name val)]
183 [(_ form foreign-name val void*)
184 #'(check-void* 'foreign-name val)]
185 [(_ form foreign-name val int)
186 #'(check-int 'foreign-name val)]
187 [(_ form foreign-name val float)
188 #'(check-float 'foreign-name val)]
189 [(_ form foreign-name val double)
190 #'(check-double 'foreign-name val)]
191 [(_ form foreign-name val [int])
192 #'(check-int* 'foreign-name val)]
193 [(_ form foreign-name val [char*])
194 #'(check-char** 'foreign-name val)]
195 [(_ form foreign-name val [c-callback return-type (arg-types ...)])
196 #'(check-callback foreign-name val return-type (arg-types ...))]
197 [(_ form foreign-name val arg-type)
198 (syntax-violation 'c-function "invalid argument type"
199 #'form #'arg-type)])))
200
201 (define (convert-out-byte* who s-val c-val)
202 (let ((n (bytevector-length s-val)))
203 (let loop ([i 0])
204 (unless (= i n)
205 (bytevector-u8-set! s-val i (pointer-ref-c-unsigned-char c-val i))
206 (loop (+ i 1))))))
207
208 (define-syntax convert-out-arg
209 (lambda (x)
210 (syntax-case x (int char* byte* c-callback float double void*)
211 ((_ form foreign-name s-val c-val byte*)
212 #'(convert-out-byte* 'foreign-name s-val c-val))
213 ((_ form foreign-name s-val c-val arg-ype)
214 #'(void)))))
215
216 (define (char*->string who x)
217 (define (strlen x)
218 (let f ([i 0])
219 (cond
220 [(= 0 (pointer-ref-c-unsigned-char x i)) i]
221 [else (f (+ i 1))])))
222 (let ([n (strlen x)])
223 (let ([s (make-string n)])
224 (let f ([i 0])
225 (if (= i n)
226 s
227 (begin
228 (string-set! s i
229 (integer->char (pointer-ref-c-unsigned-char x i)))
230 (f (+ i 1))))))))
231
232 (define-syntax convert-return
233 (lambda (x)
234 (syntax-case x (char*)
235 [(_ form foreign-name val char*)
236 #'(char*->string 'foreign-name val)]
237 [(_ form foreign-name val other)
238 #'val])))
239
240 (define-syntax convert-type
241 (lambda (x)
242 (define ls
243 '([void void]
244 [char* pointer]
245 [float float]
246 [double double]
247 [void* pointer]
248 [byte* pointer]
249 [int signed-int]))
250 (define (valid x)
251 (cond
252 [(and (list? x) (= (length x) 3) (eq? (car x) 'c-callback))
253 (and (valid (cadr x))
254 (andmap valid (caddr x))
255 'pointer)]
256 [(list? x)
257 (and (andmap valid x) 'pointer)]
258 [(assq x ls) => cadr]
259 [else #f]))
260 (syntax-case x (void)
261 [(ctxt t)
262 (cond
263 [(valid (syntax->datum #'t)) =>
264 (lambda (t)
265 (with-syntax ([t (datum->syntax #'ctxt t)])
266 #'(quote t)))]
267 [else (syntax-violation #f "invalid type" #'t)])])))
268
269 (define (lookup-shared-object lib name)
270 (define who 'lookup-shared-object)
271 (unless (symbol? name) (die who "not a symbol" name))
272 (unless (library? lib) (die who "not a library" lib))
273 (or (dlsym (library-pointer lib) (symbol->string name))
274 (error who
275 (format "cannot find object ~a in library ~a"
276 name (library-name lib)))))
277
278 (define-syntax c-function
279 (lambda (x)
280 (syntax-case x ()
281 [(_ lib lib-name return-type conv foreign-name (arg-type* ...))
282 (with-syntax ([x x]
283 [(t* ...) (generate-temporaries #'(arg-type* ...))]
284 [(u* ...) (generate-temporaries #'(arg-type* ...))])
285 #'(let ([callout
286 ((make-c-callout
287 (convert-type return-type)
288 (list (convert-type arg-type*) ...))
289 (lookup-shared-object lib 'foreign-name))])
290 (lambda (t* ...)
291 (let ([u* (convert-arg x foreign-name t* arg-type*)] ...)
292 (let ([v (callout u* ...)])
293 (convert-out-arg x foreign-name t* u* arg-type*)
294 ...
295 (convert-return x foreign-name v return-type))))))])))
296
297 (define-syntax c-argument
298 (lambda (x)
299 (syntax-case x ()
300 [(_ function-name argnum argtype argval)
301 (begin
302 ;(printf "syntax ~s\n" (syntax->datum x))
303 #'(void))])))
304
305 )