summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/language-bindings/python/wasm-c-api/utils/bindgen.py
blob: a505404d589d0028ea0a5b32044430c93414ad9c (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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
#
# Copyright (C) 2019 Intel Corporation.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring
# pylint: disable=missing-module-docstring

"""
- Need to run *download_wamr.py* firstly.
- Parse *./wasm-micro-runtime/core/iwasm/include/wasm_c_api.h* and generate
  *wamr/binding.py*
"""
import os
import pathlib
import shutil
import sys

from pycparser import c_ast, parse_file

WASM_C_API_HEADER = "core/iwasm/include/wasm_c_api.h"
BINDING_PATH = "language-bindings/python/wamr/wasmcapi/binding.py"
# 4 spaces as default indent
INDENT = "    "

IGNORE_SYMOLS = (
    "wasm_engine_new_with_args",
    "wasm_valkind_is_num",
    "wasm_valkind_is_ref",
    "wasm_valtype_is_num",
    "wasm_valtype_is_ref",
    "wasm_valtype_new_i32",
    "wasm_valtype_new_i64",
    "wasm_valtype_new_f32",
    "wasm_valtype_new_f64",
    "wasm_valtype_new_anyref",
    "wasm_valtype_new_funcref",
    "wasm_functype_new_0_0",
    "wasm_functype_new_0_0",
    "wasm_functype_new_1_0",
    "wasm_functype_new_2_0",
    "wasm_functype_new_3_0",
    "wasm_functype_new_0_1",
    "wasm_functype_new_1_1",
    "wasm_functype_new_2_1",
    "wasm_functype_new_3_1",
    "wasm_functype_new_0_2",
    "wasm_functype_new_1_2",
    "wasm_functype_new_2_2",
    "wasm_functype_new_3_2",
    "wasm_val_init_ptr",
    "wasm_val_ptr",
    "wasm_val_t",
    "wasm_ref_t",
    "wasm_name_new_from_string",
    "wasm_name_new_from_string_nt",
)


class Visitor(c_ast.NodeVisitor):
    def __init__(self):
        self.type_map = {
            "_Bool": "c_bool",
            "byte_t": "c_ubyte",
            "char": "c_char",
            "errno_t": "c_int",
            "int": "c_int",
            "long": "c_long",
            "size_t": "c_size_t",
            "uint32_t": "c_uint32",
            "uint8_t": "c_uint8",
            "void": "None",
        }
        self.ret = (
            "# -*- coding: utf-8 -*-\n"
            "#!/usr/bin/env python3\n"
            "#\n"
            "# Copyright (C) 2019 Intel Corporation.  All rights reserved.\n"
            "# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
            "#\n"
            "#It is a generated file. DO NOT EDIT.\n"
            "#\n"
            "from ctypes import *\n"
            "\n"
            "from .ffi import dereference, libiwasm, wasm_ref_t, wasm_val_t\n"
            "\n"
            "\n"
        )

    def get_type_name(self, c_type):
        if isinstance(c_type, c_ast.TypeDecl):
            return self.get_type_name(c_type.type)
        elif isinstance(c_type, c_ast.PtrDecl):
            pointed_type = self.get_type_name(c_type.type)

            if isinstance(c_type.type, c_ast.FuncDecl):
                # CFUCNTYPE is a pointer of function
                return pointed_type

            if "None" == pointed_type:
                return "c_void_p"

            return f"POINTER({pointed_type})"

        elif isinstance(c_type, c_ast.ArrayDecl):
            return f"POINTER({self.get_type_name(c_type.type)})"
        elif isinstance(c_type, c_ast.IdentifierType):
            if len(c_type.names) > 1:
                raise RuntimeError(f"unexpected type with a long names: {c_type}")

            type_name = c_type.names[0]

            if type_name.startswith("wasm_"):
                return type_name

            if not type_name in self.type_map:
                raise RuntimeError(f"a new type should be in type_map: {type_name}")

            return self.type_map.get(type_name)
        elif isinstance(c_type, c_ast.Union):
            if not c_type.name:
                raise RuntimeError(f"found an anonymous union {c_type}")

            return c_type.name
        elif isinstance(c_type, c_ast.Struct):
            if not c_type.name:
                raise RuntimeError(f"found an anonymous union {c_type}")

            return c_type.name
        elif isinstance(c_type, c_ast.FuncDecl):
            content = "CFUNCTYPE("
            if isinstance(c_type.type, c_ast.PtrDecl):
                # there is a bug in CFUNCTYPE if the result type is a pointer
                content += "c_void_p"
            else:
                content += f"{self.get_type_name(c_type.type)}"
            content += f",{self.get_type_name(c_type.args)}" if c_type.args else ""
            content += ")"
            return content
        elif isinstance(c_type, c_ast.Decl):
            return self.get_type_name(c_type.type)
        elif isinstance(c_type, c_ast.ParamList):
            content = ",".join(
                [self.get_type_name(param.type) for param in c_type.params]
            )
            return content
        else:
            raise RuntimeError(f"unexpected type: {c_type.show()}")

    def visit_Struct(self, node):
        # pylint: disable=invalid-name
        def gen_fields(info, indent):
            content = ""
            for k, v in info.items():
                content += f'{indent}("{k}", {v}),\n'
            return content[:-1]

        def gen_equal(info, indent):
            content = f"{indent}return"
            for k, v in info.items():
                # not compare pointer value in __eq__
                if v.startswith("POINTER") or v.startswith("c_void_p"):
                    continue

                content += f" self.{k} == other.{k} and"
            return content[:-4]

        def gen_repr(info, indent):
            content = f'{indent}return f"{{{{'
            for k, _ in info.items():
                content += f"{k}={{self.{k}}}, "
            content = content[:-2] + '}}"'
            return content

        def gen_vector_repr(info, indent):
            content = f'{indent}ret = ""\n'
            content += f"{indent}for i in range(self.num_elems):\n"

            if 1 == info["data"].count("POINTER"):
                # pointer
                content += f"{2*indent}ret += str(self.data[i])\n"
            else:
                # pointer of pointer
                content += f"{2*indent}ret += str(dereference(self.data[i]))\n"

            content += f'{2*indent}ret += " "\n'
            content += f"{indent}return ret\n"
            return content

        if not node.name or not node.name.lower().startswith("wasm"):
            return

        if node.name in IGNORE_SYMOLS:
            return

        name = node.name

        info = {}
        if node.decls:
            for decl in node.decls:
                info[decl.name] = self.get_type_name(decl.type)

        if info:
            self.ret += (
                f"class {name}(Structure):\n"
                f"{INDENT}_fields_ = [\n"
                f"{gen_fields(info, INDENT*2)}\n"
                f"{INDENT}]\n"
                f"\n"
                f"{INDENT}def __eq__(self, other):\n"
                f"{INDENT*2}if not isinstance(other, {name}):\n"
                f"{INDENT*3}return False\n"
                f"{gen_equal(info, INDENT*2)}\n"
                f"\n"
                f"{INDENT}def __repr__(self):\n"
            )
            self.ret += (
                f"{gen_vector_repr(info, INDENT*2)}\n"
                if name.endswith("_vec_t")
                else f"{gen_repr(info, INDENT*2)}\n"
            )
            self.ret += "\n"

        else:
            self.ret += f"class {name}(Structure):\n{INDENT}pass\n"

        self.ret += "\n"

    def visit_Union(self, node):
        # pylint: disable=invalid-name
        print(f"Union: {node.show()}")

    def visit_Typedef(self, node):
        # pylint: disable=invalid-name
        # system defined
        if not node.name:
            return

        if not node.name.startswith("wasm_"):
            return

        if node.name in IGNORE_SYMOLS:
            return

        self.visit(node.type)

        if node.name == self.get_type_name(node.type):
            return
        else:
            self.ret += f"{node.name} = {self.get_type_name(node.type)}\n"
            self.ret += "\n"

    def visit_FuncDecl(self, node):
        # pylint: disable=invalid-name
        restype = self.get_type_name(node.type)

        if isinstance(node.type, c_ast.TypeDecl):
            func_name = node.type.declname
        elif isinstance(node.type, c_ast.PtrDecl):
            func_name = node.type.type.declname
        else:
            raise RuntimeError(f"unexpected type in FuncDecl: {type}")

        if not func_name.startswith("wasm_") or func_name.endswith("_t"):
            return

        if func_name in IGNORE_SYMOLS:
            return

        params_len = 0
        for arg in node.args.params:
            # ignore void but not void*
            if isinstance(arg.type, c_ast.TypeDecl):
                type_name = self.get_type_name(arg.type)
                if "None" == type_name:
                    continue

            params_len += 1

        args = (
            "" if not params_len else ",".join([f"arg{i}" for i in range(params_len)])
        )
        argtypes = f"[{self.get_type_name(node.args)}]" if params_len else "None"

        self.ret += (
            f"def {func_name}({args}):\n"
            f"{INDENT}_{func_name} = libiwasm.{func_name}\n"
            f"{INDENT}_{func_name}.restype = {restype}\n"
            f"{INDENT}_{func_name}.argtypes = {argtypes}\n"
            f"{INDENT}return _{func_name}({args})\n"
        )
        self.ret += "\n"

    def visit_Enum(self, node):
        # pylint: disable=invalid-name
        elem_value = 0
        # generate enum elementes directly as consts with values
        for i, elem in enumerate(node.values.enumerators):
            self.ret += f"{elem.name}"

            if elem.value:
                elem_value = int(elem.value.value)
            else:
                if 0 == i:
                    elem_value = 0
                else:
                    elem_value += 1

            self.ret += f" = {elem_value}\n"

        self.ret += "\n"


def preflight_check(workspace):
    wamr_repo = workspace
    file_check_list = [
        wamr_repo.exists(),
        wamr_repo.joinpath(WASM_C_API_HEADER).exists(),
    ]

    if not all(file_check_list):
        print(
            "please run utils/download_wamr.py to download the repo, or re-download the repo"
        )
        return False

    if not shutil.which("gcc"):
        print("please install gcc")
        return False

    return True


def do_parse(workspace):
    filename = workspace.joinpath(WASM_C_API_HEADER)
    filename = str(filename)

    ast = parse_file(
        filename,
        use_cpp=True,
        cpp_path="gcc",
        cpp_args=[
            "-E",
            "-D__attribute__(x)=",
            "-D__asm__(x)=",
            "-D__asm(x)=",
            "-D__builtin_va_list=int",
            "-D__extension__=",
            "-D__inline__=",
            "-D__restrict=",
            "-D__restrict__=",
            "-D_Static_assert(x, y)=",
            "-D__signed=",
            "-D__volatile__(x)=",
            "-Dstatic_assert(x, y)=",
        ],
    )

    ast_visitor = Visitor()
    ast_visitor.visit(ast)
    return ast_visitor.ret


def main():
    current_file = pathlib.Path(__file__)
    if current_file.is_symlink():
        current_file = pathlib.Path(os.readlink(current_file))

    current_dir = current_file.parent.resolve()
    root_dir = current_dir.joinpath("../../../..").resolve()

    if not preflight_check(root_dir):
        return False

    wamr_repo = root_dir
    binding_file_path = root_dir.joinpath(BINDING_PATH)
    with open(binding_file_path, "wt", encoding="utf-8") as binding_file:
        binding_file.write(do_parse(wamr_repo))

    return True


if __name__ == "__main__":
    sys.exit(0 if main() else 1)