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
|
import buildconfig
import six
import yaml
from mozbuild.preprocessor import Preprocessor
HEADER_TEMPLATE = """\
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef %(includeguard)s
#define %(includeguard)s
/* This file is generated by wasm/GenerateBuiltinModules.py. Do not edit! */
%(contents)s
#endif // %(includeguard)s
"""
def generate_header(c_out, includeguard, contents):
c_out.write(
HEADER_TEMPLATE
% {
"includeguard": includeguard,
"contents": contents,
}
)
def load_yaml(yaml_path):
# First invoke preprocessor.py so that we can use #ifdef JS_SIMULATOR in
# the YAML file.
pp = Preprocessor()
pp.context.update(buildconfig.defines["ALLDEFINES"])
pp.out = six.StringIO()
pp.do_filter("substitution")
pp.do_include(yaml_path)
contents = pp.out.getvalue()
return yaml.safe_load(contents)
def cppBool(v):
if v:
return "true"
return "false"
def specTypeToMIRType(specType):
if specType == "i32" or specType == "i64" or specType == "f32" or specType == "f64":
return f"ValType::{specType}().toMIRType()"
if (
specType == "externref"
or specType == "anyref"
or specType == "funcref"
or isinstance(specType, dict)
):
return "MIRType::WasmAnyRef"
raise ValueError()
def specHeapTypeToTypeCode(specHeapType):
if specHeapType == "func":
return "Func"
if specHeapType == "any":
return "Any"
if specHeapType == "extern":
return "Extern"
if specHeapType == "array":
return "Array"
if specHeapType == "struct":
return "Struct"
raise ValueError()
def specTypeToValType(specType):
if specType == "i32" or specType == "i64" or specType == "f32" or specType == "f64":
return f"ValType::{specType}()"
if specType == "externref":
return "ValType(RefType::extern_())"
if specType == "anyref":
return "ValType(RefType::any())"
if specType == "funcref":
return "ValType(RefType::func())"
if isinstance(specType, dict):
nullable = cppBool(specType["nullable"])
if "type" in specType:
ref = specType["type"]
return f"ValType(RefType::fromTypeDef({ref}, {nullable}))"
else:
code = specType["code"]
return f"ValType(RefType::fromTypeCode(TypeCode(RefType::{specHeapTypeToTypeCode(code)}), {nullable}))"
raise ValueError()
def main(c_out, yaml_path):
data = load_yaml(yaml_path)
# Iterate for all defined builtin methods
contents = "#define FOR_EACH_BUILTIN_MODULE_FUNC(M) \\\n"
for i in range(len(data)):
op = data[i]
sa = op["symbolic_address"]
contents += (
f" M({op['op']}, \"{op['export']}\", "
f"{sa['name']}, {sa['type']}, {op['entry']}, {cppBool(op['uses_memory'])}, {i})\\\n"
)
contents += "\n"
for op in data:
# Define DECLARE_BUILTIN_MODULE_FUNC_PARAM_VALTYPES_<op> as:
# `{ValType::I32, ValType::I32, ...}`.
valTypes = ", ".join(specTypeToValType(p) for p in op["params"])
contents += (
f"#define DECLARE_BUILTIN_MODULE_FUNC_PARAM_VALTYPES_{op['op']} "
f"{{{valTypes}}}\n"
)
# Define DECLARE_BUILTIN_MODULE_FUNC_PARAM_MIRTYPES_<op> as:
# `<num_types>, {MIRType::Pointer, _I32, ..., MIRType::Pointer, _END}`.
num_types = len(op["params"]) + 1
mir_types = "{MIRType::Pointer"
mir_types += "".join(", " + specTypeToMIRType(p) for p in op["params"])
if op["uses_memory"]:
mir_types += ", MIRType::Pointer"
num_types += 1
# Add the end marker
mir_types += ", MIRType::None}"
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_PARAM_MIRTYPES_{op['op']} {num_types}, {mir_types}\n"
# Define DECLARE_BUILTIN_MODULE_FUNC_RESULT_VALTYPE_<op> as:
# `Some(X)` if present, or else `Nothing()`.
result_valtype = ""
if "result" in op:
result_valtype = f"Some({specTypeToValType(op['result'])})\n"
else:
result_valtype = "Nothing()"
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_RESULT_VALTYPE_{op['op']} {result_valtype}\n"
# Define DECLARE_BUILTIN_MODULE_FUNC_RESULT_MIRTYPE_<op> as:
# `X` if present, or else `MIRType::None`.
result_mirtype = ""
if "result" in op:
result_mirtype = specTypeToMIRType(op["result"]) + "\n"
else:
result_mirtype = "MIRType::None"
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_RESULT_MIRTYPE_{op['op']} {result_mirtype}\n"
# Define DECLARE_BUILTIN_MODULE_FUNC_FAILMODE_<op> as:
# `FailureMode::X`.
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_FAILMODE_{op['op']} FailureMode::{op['fail_mode']}\n"
generate_header(c_out, "wasm_WasmBuiltinModuleGenerated_h", contents)
|