-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmeta.rkt
More file actions
219 lines (193 loc) · 6.04 KB
/
meta.rkt
File metadata and controls
219 lines (193 loc) · 6.04 KB
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
;; This file provides an API for obtaining
;; AST declaration metadata for C data types and
;; functions
#lang racket/base
(require (for-syntax racket/base)
racket/list
racket/runtime-path
"common.rkt")
(provide declaration
(all-from-out 'ctype-defs)
(rename-out
[dynamic-ffi-wrapper
dynamic-ffi-parse]))
(define-runtime-path dynamic-ffi-core.rkt "dynamic-ffi-core.rkt")
;; Check for existance of the native extension
;; if it is not built, warn the user about
;; the missing dependencies.
(define dynamic-ffi-parse
(if (file-exists? dynamic-ffi-core_rkt.so)
(dynamic-require dynamic-ffi-core.rkt 'dynamic-ffi-parse)
(λ (x . args)
(warn-dependencies)
(exit 1))))
(module ctype-defs racket/base
(struct declaration [name type type-string literal-value] #:transparent)
(struct function-decl declaration [])
(struct var-decl declaration [])
(struct record-decl declaration [])
(struct enum-decl declaration [])
(struct typedef-decl declaration [])
(struct ctype [const? volatile? literal? width] #:transparent)
(struct ctype-void ctype [] #:transparent)
(struct ctype-int ctype [signed?] #:transparent)
(struct ctype-float ctype [] #:transparent)
(struct ctype-pointer ctype [restrict? >pointee] #:transparent)
(struct ctype-array ctype [element] #:transparent)
(struct ctype-record ctype [members member-names] #:transparent)
(struct ctype-struct ctype-record [] #:transparent)
(struct ctype-union ctype-record [] #:transparent)
(struct ctype-function ctype [return params] #:transparent)
(provide (all-defined-out)))
(require 'ctype-defs)
;; This module will provide extra debug
;; output when __ debug is #t
(define _debug #f)
;; Take a list of path-like things (c headers),
;; convert them to byte strings, and call
;; out to the native extension routine
(define (dynamic-ffi-wrapper . files-list)
(define (path->byte-string path)
(cond [(bytes? path) path]
[(string? path) (string->bytes/locale path)]
[(path? path)
(string->bytes/locale
(path->string path))]
[else (error "dynamic-ffi: unsupported path format: " path)]))
(define byte-string-paths
(for/list ([path files-list])
(path->byte-string path)))
(define c-decls-list
(apply dynamic-ffi-parse
(cons #"dynamic-ffi-parse" byte-string-paths)))
(when _debug (printf "parse complete\n"))
(map make-declaration
(filter (λ (decl) (> (string-length (decl-name decl)) 0))
c-decls-list)))
;; Where d is a list obtained from dynamic-ffi-parse
(define (make-declaration d)
(define decl-hash
(make-hash
(list
(cons 'var-decl var-decl)
(cons 'function-decl function-decl)
(cons 'record-decl record-decl)
(cons 'enum-decl enum-decl)
(cons 'typedef-decl typedef-decl)
(cons 'unknown (λ (x)x)))))
(define ct (decl-ctype d))
(define dispatch (hash-ref decl-hash (decl-type-sym d)))
(dispatch
(decl-name d)
(make-ctype (decl-ctype d))
(decl-type-str d)
(decl-literal-value d)))
;; Where t is ctype sublist of d
(define (make-ctype t)
(define ctype-hash
(make-hash
(list
(cons 'integer make-ctype-int)
(cons 'floating make-ctype-float)
(cons 'pointer make-ctype-pointer)
(cons 'struct make-ctype-struct)
(cons 'union make-ctype-union)
(cons 'array make-ctype-array)
(cons 'function make-ctype-function)
(cons 'void make-ctype-void)
(cons 'unknown (λ (x)x)))))
(define dispatch (hash-ref ctype-hash (raw-ctype-sym t)))
(dispatch t))
;;List to Struct Functions
(define (decl-type-sym c-decl)
(first c-decl))
(define (decl-type-str c-decl)
(second c-decl))
(define (decl-name c-decl)
(third c-decl))
(define (decl-ctype c-decl)
(fourth c-decl))
(define (decl-literal-value c-decl)
(fifth c-decl))
(define (raw-ctype-sym ctype)
(first ctype))
(define (raw-ctype-width ctype)
(second ctype))
(define (raw-ctype-signed? ctype)
(third ctype))
(define (raw-ctype-const? ctype)
(fourth ctype))
(define (raw-ctype-volatile? ctype)
(fifth ctype))
(define (raw-ctype-restrict? ctype)
(sixth ctype))
(define (raw-ctype-literal? ctype)
(seventh ctype))
(define (raw-ctype-fields ctype)
(eighth ctype))
(define (raw-ctype-field-names ctype)
(ninth ctype))
(define (raw-ctype-pointee ctype)
(car (raw-ctype-fields ctype)))
(define (raw-ctype-function-ret ctype)
(car (raw-ctype-fields ctype)))
(define (raw-ctype-function-args ctype)
(cdr (raw-ctype-fields ctype)))
(define (make-ctype-int t)
(ctype-int
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)
(raw-ctype-signed? t)))
(define (make-ctype-float t)
(ctype-float
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)))
(define (make-ctype-void t)
(ctype-void
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)))
(define (make-ctype-array t)
(ctype-array
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)
(make-ctype (raw-ctype-pointee t))))
(define (make-ctype-pointer t)
(ctype-pointer
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)
(raw-ctype-restrict? t)
(make-ctype (raw-ctype-pointee t))))
(define (make-ctype-struct t)
(ctype-struct
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)
(map make-ctype (raw-ctype-fields t))
(raw-ctype-field-names t)))
(define (make-ctype-union t)
(ctype-union
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)
(map make-ctype (raw-ctype-fields t))
(raw-ctype-field-names t)))
(define (make-ctype-function t)
(ctype-function
(raw-ctype-const? t)
(raw-ctype-volatile? t)
(raw-ctype-literal? t)
(raw-ctype-width t)
(make-ctype (raw-ctype-function-ret t))
(map make-ctype (raw-ctype-function-args t))))