-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-define-base-macros#.scm
356 lines (331 loc) · 16.1 KB
/
c-define-base-macros#.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
;;!!! Simple FFI generation macros
;; .author Álvaro Castro Castilla, 2013-2019. All Rights Reserved.
;;------------------------------------------------------------------------------
;;!! Prelude
(define-macro (eval-in-macro-environment . exprs)
(eval (if (null? (cdr exprs)) (car exprs) (cons 'begin exprs))
(interaction-environment)))
(define-macro (define^ . args)
`(eval-in-macro-environment
(define ,(car args) ,@(cdr args))))
;;------------------------------------------------------------------------------
;;!! Macro utils
(define^ (%%get-key-arg args key default)
(if (null? args)
default
(let ((found (memq key args)))
(if (and found
(not (null? (cdr found))))
(cadr found)
default))))
;; Build a string from list of elements (anything)
(define^ (%%generic-string-append . ol)
(define (->string o)
(cond ((string? o) o)
((symbol? o) (symbol->string o))
((keyword? o) (keyword->string o))
(else (error (string-append "c-define-array :: ->string: undesirable type -- "
(object->string o))))))
(apply string-append (map ->string ol)))
;; Append anything into a symbol
(define^ (%%generic-symbol-append . ol)
(string->symbol (apply %%generic-string-append ol)))
;; Turn a scheme-name into a c_name, simply changing the - to _
(define^ (%%scheme-name->c-name name)
(let* ((name-str (cond ((string? name) name)
((symbol? name) (symbol->string name))
(else (object->string name))))
(new-name (string-copy name-str))
(name-length (string-length new-name)))
(let recur ((i 0))
(if (< i name-length)
(begin (if (char=? (string-ref new-name i) #\-)
(string-set! new-name i #\_))
(recur (+ i 1)))
new-name))))
;;! Schemify a name: turn-it-into-a-scheme-name
;; .author Fred LeMaster
(define^ (%%c-name->scheme-name symbol-or-string)
((lambda (str)
(letrec
((str-length (string-length str))
(case-changed?
(lambda (i)
(let ((h (- i 1)))
(cond ((< h 0) #f)
((and (char-lower-case? (string-ref str h))
(char-upper-case? (string-ref str i)))
#t)
(else #f)))))
(char-loop
(lambda (i out)
(cond ((>= i str-length) out)
((char=? (string-ref str i) #\_)
(char-loop (+ i 1) (cons #\- out)))
((and (char=? (string-ref str i) #\:)
(< (+ i 1) str-length)
(char=? (string-ref str (+ i 1)) #\:))
(char-loop (+ i 2) (cons #\- out)))
((case-changed? i)
(char-loop (+ i 1)
(cons (char-downcase
(string-ref str i))
(cons #\-
out))))
(else (char-loop (+ i 1)
(cons
(char-downcase
(string-ref str i))
out)))))))
(list->string (reverse (char-loop 0 '())))))
(cond ((symbol? symbol-or-string) (symbol->string symbol-or-string))
((string? symbol-or-string) symbol-or-string)
(else (error "%%c-name->scheme-name: expected symbol or string")))))
;; Generate a list of top-level forms
(define^ (%%begin-top-level-forms #!rest define-blocks)
(cons 'begin
(let recur ((ds define-blocks))
(cond ((null? ds) '())
((null? (car ds)) (recur (cdr ds)))
(else (cons (car ds)
(recur (cdr ds))))))))
;;------------------------------------------------------------------------------
;;!! FFI generation
;;! C constants generation macro
;; Creating the bindings in a simple C function makes for more compact
;; https://mercure.iro.umontreal.ca/pipermail/gambit-list/2012-February/005688.html
(define-macro (c-define-constants . names)
(let ((nb-names (length names))
(wrapper (string->symbol
(string-append (symbol->string (gensym)) "c-constants"))))
(letrec ((interval (lambda (lo hi) (if (< lo hi) (cons lo (interval (+ lo 1) hi)) '()))))
`(begin
(##define ,wrapper
(c-lambda (int)
int
,(string-append
"static int _tmp_[] = {\n"
(apply string-append
(map (lambda (i name)
(let ((name-str (symbol->string name)))
(string-append
(if (> i 0) "," "")
name-str)))
(interval 0 nb-names)
names))
"};\n"
"___return(_tmp_[___arg1]);\n")))
,@(map (lambda (i name)
`(##define ,name (,wrapper ,i)))
(interval 0 nb-names)
names)))))
;;! Build a size-of value equivalent to the C operator
;; c-build-sizeof float -> sizeof-float
(define-macro (c-define-sizeof scheme-type . rest)
(let ((c-type (%%get-key-arg rest c-type: (symbol->string scheme-type))))
`(define ,(string->symbol (string-append (symbol->string scheme-type) "-size"))
((c-lambda () size-t
,(string-append "___return(sizeof(" c-type "));"))))))
;;! Build FFI procedures for C type arrays. Only for use with basic types, not structs.
;; (c-define-array float f32) ->
;; alloc-float*
;; float*-ref
;; float*-set!
;; *->float*
;; f32vector->float*
(define-macro (c-define-array scheme-type . rest)
(let ((c-type (%%get-key-arg rest c-type: scheme-type))
(scheme-vector (%%get-key-arg rest scheme-vector: #f)))
(if (not scheme-vector) (error "c-define-array macro :: scheme-vector: argument is mandatory"))
(let ((release-type-str (string-append "___release_" (%%scheme-name->c-name scheme-type)))
(type scheme-type)
(type*
(%%generic-symbol-append scheme-type "*"))
(type*/nonnull
(%%generic-symbol-append scheme-type "*/nonnull"))
(type*/release-rc
(%%generic-symbol-append scheme-type "*/release-rc")))
(let ((expansion
(%%begin-top-level-forms
`(c-declare
,(string-append
"static ___SCMOBJ " release-type-str "( void* ptr )\n"
"{\n"
" printf(\"GC called " release-type-str "()\\n\");\n"
" ___EXT(___release_rc)( ptr );\n"
;;" free( ptr );\n"
" return ___FIX(___NO_ERR);\n"
"}\n"))
;; Alloc managed by Gambit's GC
`(define ,(%%generic-symbol-append 'alloc- scheme-type '*/unmanaged)
(c-lambda (size-t)
,type*/nonnull
,(%%generic-string-append "___return(malloc(___arg1*sizeof(" c-type ")));")))
;; Alloc unmanaged by Gambit's GC
`(define ,(%%generic-symbol-append 'alloc- scheme-type '*)
(c-lambda (size-t)
,type*/release-rc
;; ,(%%generic-string-append "___result_voidstar = ___EXT(___alloc_rc)(___arg1*sizeof(" c-type "));")
,(%%generic-string-append "___return(___EXT(___alloc_rc)(___arg1*sizeof(" c-type ")));")))
`(define ,(%%generic-symbol-append scheme-type '*-ref)
(c-lambda (,type*/nonnull size-t)
,scheme-type
"___return(___arg1[___arg2]);"))
`(define ,(%%generic-symbol-append scheme-type '*-set!)
(c-lambda (,type*/nonnull size-t ,scheme-type)
void
"___arg1[___arg2] = ___arg3; ___return;"))
`(define ,(%%generic-symbol-append '*-> scheme-type)
(c-lambda (,type*/nonnull)
,scheme-type
"___return(*___arg1);"))
(if scheme-vector
`(define (,(%%generic-symbol-append scheme-vector 'vector-> scheme-type '*) vec)
(let* ((length (,(%%generic-symbol-append scheme-vector 'vector-length) vec))
(buf (,(%%generic-symbol-append 'alloc- scheme-type '*) length)))
(let loop ((i 0))
(if (fx< i length)
(begin
(,(%%generic-symbol-append scheme-type '*-set!) buf i (,(%%generic-symbol-append scheme-vector 'vector-ref) vec i))
(loop (fx+ i 1)))
buf))))
'()))))
(if #f ;; #t for debugging
(pp `(definition:
(c-define-array scheme-type: ,scheme-type c-type: ,c-type scheme-vector: ,scheme-vector)
expansion:
,expansion)))
expansion))))
;;------------------------------------------------------------------------------
;;!! FFI generation
;;! define types for structs, unions and arrays
;; .author Álvaro Castro-Castilla, based on code by Estevo Castro
;; (c-define-type* (struct MyStruct))
;; (c-define-type* (union MyUnion))
;; (c-define-type* myType)
(define-macro (c-define-type* type/struct/union)
(let* ((type (if (pair? type/struct/union)
(cadr type/struct/union)
type/struct/union))
(struct-or-union (if (pair? type/struct/union)
(car type/struct/union)
#f))
(type-str (symbol->string type))
(release-type-str (string-append "___release_" (%%scheme-name->c-name type-str)))
(type* (%%generic-symbol-append type-str "*"))
(type*/nonnull (%%generic-symbol-append type-str "*/nonnull"))
(type*/release-rc (%%generic-symbol-append type-str "*/release-rc")))
(let ((expansion
(%%begin-top-level-forms
(if struct-or-union
`(c-define-type ,type (,struct-or-union ,type-str))
'())
`(c-define-type ,type* (pointer ,type (,type*)))
`(c-define-type ,type*/nonnull (nonnull-pointer ,type (,type*)))
`(c-define-type ,type*/release-rc (nonnull-pointer ,type (,type*) ,release-type-str)))))
(if #f ;; #t for debugging
(pp `(definition:
(c-define-extended-type ,type)
expansion:
,expansion)))
expansion)))
;; Helper for define-c-struct and define-c-union
(define^ (%%c-define-struct-or-union struct-or-union type fields)
(let* ((type-str (symbol->string type))
(struct-type-str (string-append
(case struct-or-union
((struct) "struct ")
((union) "union ")
(else
(error "%%c-define-struct-or-union: first parameter must be 'struct or 'union")))
type-str))
(struct-type*-str (string-append struct-type-str "*"))
(release-type-str (string-append "___release_" type-str))
(type* (%%generic-symbol-append type-str "*"))
(type*/nonnull (%%generic-symbol-append type-str "*/nonnull"))
(type*/release-rc (%%generic-symbol-append type-str "*/release-rc")))
(define (field-getter-setter field-spec)
(let* ((field (car field-spec))
(field-str (symbol->string field))
(field-description (cadr field-spec)))
(if (pair? field-description)
;; Field is either a 'struct', an 'array' or an 'array of structs'
(let* ((field-tag (car field-description))
(field-type (cadr field-description))
(field-type-str (symbol->string field-type)))
(case field-tag
;; Struct
((struct)
`((define ,(%%generic-symbol-append type-str "-" field-str)
(c-lambda (,type*/nonnull)
,(%%generic-symbol-append field-type-str "*/nonnull")
,(string-append "___return(&___arg1->" field-str ");")))
(define ,(%%generic-symbol-append type-str "-" field-str "-set!")
(c-lambda (,type*/nonnull ,field-type)
void
,(string-append "___arg1->" field-str " = ___arg2; ___return;")))))
;; Array of fundamental type
((array)
;; generate a getter and a setter
`((define ,(%%generic-symbol-append type-str "-" field-str "-ref")
(c-lambda (,type*/nonnull int)
,field-type
,(string-append "___return(___arg1->" field-str "[___arg2]);")))
(define ,(%%generic-symbol-append type-str "-" field-str "-set!")
(c-lambda (,type*/nonnull int ,field-type)
void
,(string-append "___arg1->" field-str "[___arg2] = ___arg3; ___return;")))))
;; Array of structs
((struct-array)
;; only generate a getter returning struct address
`((define ,(%%generic-symbol-append type-str "-" field-str "-ref")
(c-lambda (,type*/nonnull int)
,(%%generic-symbol-append field-type-str "*/nonnull")
,(string-append "___return(&___arg1->" field-str "[___arg2]);")))))))
;; Field is fundamental type
`((define ,(%%generic-symbol-append type-str "-" field-str)
(c-lambda (,type*/nonnull)
,field-description
,(string-append "___return(___arg1->" field-str ");")))
(define ,(%%generic-symbol-append type-str "-" field-str "-set!")
(c-lambda (,type*/nonnull ,field-description)
void
,(string-append "___arg1->" field-str " = ___arg2; ___return;")))))))
(let ((expansion
`(begin
;; Define the release function which is called when the
;; object is no longer accessible from the Scheme world.
(c-declare
,(string-append
"static ___SCMOBJ " release-type-str "( void* ptr )\n"
"{\n"
" printf(\"GC called " release-type-str "()\\n\");\n"
" ___EXT(___release_rc)( ptr );\n"
" return ___FIX(___NO_ERR);\n"
"}\n"))
;; Define type allocator procedure.
(define ,(%%generic-symbol-append "alloc-" type-str)
(c-lambda ()
,type*/release-rc
,(string-append "___return(___EXT(___alloc_rc)(sizeof(" struct-type-str " )));")))
;; Dereference
(define ,(%%generic-symbol-append "*->" type-str)
(c-lambda (,type*/nonnull)
,type
"___return(*___arg1);"))
;; Define field getters and setters.
,@(apply append (map field-getter-setter fields)))))
(if #f ;; #t for debugging
(pp `(definition:
(c-define-struct ,type ,@fields)
expansion:
,expansion)))
expansion)))
;;! Defines the c-define-struct macro, which extends the Gambit FFI to
;; interface to C structures.
(define-macro (c-define-struct type . fields)
(%%c-define-struct-or-union 'struct type fields))
;;! Defines the c-define-union macro, which extends the Gambit FFI to
;; interface to C structures.
(define-macro (c-define-union type . fields)
(%%c-define-struct-or-union 'union type fields))