summaryrefslogtreecommitdiff
path: root/cmamut.lisp
blob: 55784ccd30e2f3b9b01c578adc7f20a1128ca270 (plain)
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
(in-package #:cmamut)

; Ideas:
; Generate code that creates a package and puts all definitions there.
; To avoid collisions the created package shouldn't import any symbols.
; Only define functions from the target library, but get all other definitions.
; To reduce amount of names being generated, resolve all types to their base counterparts
; Separate generation, preprocessing and type resolution phases
; Preprocessing phase should handle enums, structs and unions
; Basic types are handled by type resolution phase
; Can put an error to default case in cook type to enforce type resolution

; TODO: Get constants from macro file generated by c2ffi -M
; TODO: Use c2ffi to extract the spec.json using the header file as parameter

(defvar enum-references (make-hash-table :test #'equal :size 256))

(defun get-spec ()
  (json:from-file "~/common-lisp/cmamut/spec.json"))

(defun cook-type (raw-type)
  (let ((tag (gethash "tag" raw-type)))
    (cond ((string= tag ":void")
           'sb-alien:void)
          ((string= tag ":function-pointer")
           'sb-alien:function)
          ((string= tag ":pointer")
           (let ((pointed-type (gethash "type" raw-type)))
             (cond ((string= (gethash "tag" pointed-type) ":char")
                    'sb-alien:c-string)
                   ((string= (gethash "tag" pointed-type) ":void")
                    (list 'sb-alien:* t))
                   (t (list 'sb-alien:* (cook-type pointed-type))))))
          ((string= tag ":float")
           'sb-alien:float)
          ((string= tag ":double")
           'sb-alien:double)
          ((string= tag ":unsigned-char")
           'sb-alien:unsigned-char)
          ((string= tag ":unsigned-short")
           'sb-alien:unsigned-short)
          ((string= tag ":unsigned-int")
           'sb-alien:unsigned-int)
          ((string= tag ":unsigned-long")
           'sb-alien:unsigned-long)
          ((or (string= tag ":char") (string= tag ":signed-char"))
           'sb-alien:char)
          ((or (string= tag ":short") (string= tag ":signed-short"))
           'sb-alien:short)
          ((or (string= tag ":int") (string= tag ":signed-int"))
           'sb-alien:int)
          ((or (string= tag ":long") (string= tag ":signed-long"))
           'sb-alien:long)
          ((string= tag ":array")
           (list 'sb-alien:array
                 (cook-type (gethash "type" raw-type))
                 (gethash "size" raw-type)))
          ((string= tag ":struct")
           (list 'sb-alien:struct (intern (gethash "name" raw-type))))
          ((string= tag ":union")
           (list 'sb-alien:union (intern (gethash "name" raw-type))))
          ((string= tag "struct")
           (cook-struct raw-type))
          ((string= tag "union")
           (cook-union raw-type))
          (t tag))))

(defun cook-function (raw-function &optional name-transformer)
  (let ((raw-params (gethash "parameters" raw-function))
        (cooked-params (queue:new)))
    (dotimes (j (length raw-params))
      (queue:add cooked-params
                 (list (intern (string-upcase (gethash "name" (aref raw-params j))))
                       (cook-type (gethash "type" (aref raw-params j))))))
    `(sb-alien:define-alien-routine
       ,(let ((function-name (gethash "name" raw-function)))
          (if name-transformer
            (list function-name (funcall name-transformer function-name))
            function-name))
       ,(cook-type (gethash "return-type" raw-function))
       ,@(queue:to-list cooked-params))))

(defun cook-typedef (raw-typedef)
  (let* ((base-type (gethash "type" raw-typedef))
         (new-name (gethash "name" raw-typedef)))
    (if (string= (gethash "tag" base-type) ":enum")
      (progn (setf (gethash (gethash "id" base-type) enum-references) new-name) nil)
      `(sb-alien:define-alien-type
         ,new-name
         ,(cook-type base-type)))))

(defun cook-struct (raw-struct)
  (let ((raw-fields (gethash "fields" raw-struct))
        (cooked-fields (queue:new)))
    (dotimes (j (length raw-fields))
      (queue:add cooked-fields
                 (list (intern (string-upcase (gethash "name" (aref raw-fields j))))
                       (cook-type (gethash "type" (aref raw-fields j))))))
    `(sb-alien:struct
       ,(when (> (length (gethash "name" raw-struct)) 0)
          (intern (gethash "name" raw-struct)))
       ,@(queue:to-list cooked-fields))))

(defun cook-union (raw-union)
  (let ((raw-fields (gethash "fields" raw-union))
        (cooked-fields (queue:new)))
    (dotimes (j (length raw-fields))
      (queue:add cooked-fields
                 (list (intern (string-upcase (gethash "name" (aref raw-fields j))))
                       (cook-type (gethash "type" (aref raw-fields j))))))
    `(sb-alien:union
       ,(when (> (length (gethash "name" raw-union)) 0)
          (intern (gethash "name" raw-union)))
       ,@(queue:to-list cooked-fields))))

(defun cook-enum (raw-enum)
  (let ((name (gethash (gethash "id" raw-enum) enum-references))
        (raw-fields (gethash "fields" raw-enum))
        (cooked-fields (queue:new)))
    (dotimes (j (length raw-fields))
      (queue:add cooked-fields
                 (list (intern (string-upcase (gethash "name" (aref raw-fields j))))
                       (gethash "value" (aref raw-fields j)))))
    `(sb-alien:define-alien-type
       ,name
       (sb-alien:enum
         nil
         ,@(queue:to-list cooked-fields)))))

(defun filter-base-types (raw-typedefs)
  (clrhash type-associations)
  (setf (gethash ":char" type-associations) t)
  (setf (gethash ":short" type-associations) t)
  (setf (gethash ":int" type-associations) t)
  (setf (gethash ":long" type-associations) t)
  (setf (gethash ":signed-char" type-associations) t)
  (setf (gethash ":signed-short" type-associations) t)
  (setf (gethash ":signed-int" type-associations) t)
  (setf (gethash ":signed-long" type-associations) t)
  (setf (gethash ":unsigned-char" type-associations) t)
  (setf (gethash ":unsigned-short" type-associations) t)
  (setf (gethash ":unsigned-int" type-associations) t)
  (setf (gethash ":unsigned-long" type-associations) t)
  (setf (gethash ":float" type-associations) t)
  (setf (gethash ":double" type-associations) t)
  (setf (gethash ":function-pointer" type-associations) t)
  (labels ((take-deps
             (deps)
             (let (remaining)
               (dolist (i deps)
                 (let ((tag (gethash "tag" (gethash "type" i))))
                   (if (gethash tag type-associations)
                     (setf (gethash (gethash "name" i) type-associations) tag)
                     (push i remaining))))
               (if (= (length deps) (length remaining))
                 remaining
                 (take-deps remaining)))))
    (let ((result (take-deps raw-typedefs)))
      (labels ((associate-base-type
                 (k v)
                 (let ((new-v (gethash v type-associations)))
                   (if (eq new-v t)
                     (setf (gethash k type-associations) v)
                     (associate-base-type k new-v)))))
        (maphash (lambda (k v)
                   (unless (eq v t)
                     (associate-base-type k v)))
                 type-associations))
      result)))

(defstruct spec
  functions
  typedefs
  structs
  unions
  enums)

(defun classify-definitions (spec)
  (let ((s (make-spec)))
    (dotimes (i (length spec))
      (let ((tag (gethash "tag" (aref spec i))))
        (cond ((string= tag "function")
               (push (aref spec i) (spec-functions s)))

              ((string= tag "typedef")
               (let ((internal-type (gethash "type" (aref spec i))))
                 (cond ((string= (gethash "tag" internal-type) "struct")
                        (when (string= (gethash "name" internal-type) "")
                          (setf (gethash "name" internal-type) (gethash "name" (aref spec i))))
                        (push (gethash "type" (aref spec i)) (spec-structs s))
                        (setf (gethash "type" (aref spec i))
                              (json:from-string
                                (format nil
                                        "{ \"tag\": \":struct\", \"name\": ~s }"
                                        (gethash "name" (aref spec i)))))))
                 (push (aref spec i) (spec-typedefs s))))

              ((string= tag "struct")
               (push (aref spec i) (spec-structs s)))
              ((string= tag "enum")
               (push (aref spec i) (spec-enums s)))
              ((string= tag "union")
               (push (aref spec i) (spec-unions s)))
              (t))))
    s))

(defun codegen (spec)
  (clrhash enum-references)
  ; typedefs must be generated first to build the reference table
  (setf (spec-typedefs spec)
        (remove nil (mapcar #'cook-typedef (filter-base-types (spec-typedefs spec)))))
  (setf (spec-functions spec) (mapcar #'cook-function (spec-functions spec)))
  (setf (spec-enums spec) (mapcar #'cook-enum (spec-enums spec)))
  (setf (spec-structs spec)
        (mapcar (lambda (x)
                  `(sb-alien:define-alien-type nil ,(cook-struct x)))
                (spec-structs spec)))
  (setf (spec-unions spec)
        (mapcar (lambda (x)
                  `(sb-alien:define-alien-type nil ,(cook-union x)))
                (spec-unions spec)))
  spec)

(defun to-file (spec filename) 
  (with-open-file (f filename
                     :direction :output
                     :if-exists :supersede
                     :if-does-not-exist :create)
    (princ ";;; GENERATED BY CMAMUT" f)
    (terpri f)
    (let ((*print-length* nil)
          (*print-level* nil))
      (format f "~{~s~&~}" (spec-enums spec))
      (format f "~{~s~&~}" (spec-typedefs spec)))))
;(format f "~{~s~&~}" (spec-structs spec))
;(format f "~{~s~&~}" (spec-unions spec))
;(format f "~{~s~&~}" (spec-functions spec)))))