| 1 |
import io |
| 2 |
|
| 3 |
import buildconfig |
| 4 |
import yaml |
| 5 |
from mozbuild.preprocessor import Preprocessor |
| 6 |
|
| 7 |
HEADER_TEMPLATE = """\ |
| 8 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 9 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 10 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 11 |
|
| 12 |
#ifndef %(includeguard)s |
| 13 |
#define %(includeguard)s |
| 14 |
|
| 15 |
/* This file is generated by wasm/GenerateBuiltinModules.py. Do not edit! */ |
| 16 |
|
| 17 |
%(contents)s |
| 18 |
|
| 19 |
#endif // %(includeguard)s |
| 20 |
""" |
| 21 |
|
| 22 |
|
| 23 |
def generate_header(c_out, includeguard, contents): |
| 24 |
c_out.write( |
| 25 |
HEADER_TEMPLATE |
| 26 |
% { |
| 27 |
"includeguard": includeguard, |
| 28 |
"contents": contents, |
| 29 |
} |
| 30 |
) |
| 31 |
|
| 32 |
|
| 33 |
def load_yaml(yaml_path): |
| 34 |
# First invoke preprocessor.py so that we can use #ifdef JS_SIMULATOR in |
| 35 |
# the YAML file. |
| 36 |
pp = Preprocessor() |
| 37 |
pp.context.update(buildconfig.defines["ALLDEFINES"]) |
| 38 |
pp.out = io.StringIO() |
| 39 |
pp.do_filter("substitution") |
| 40 |
pp.do_include(yaml_path) |
| 41 |
contents = pp.out.getvalue() |
| 42 |
return yaml.safe_load(contents) |
| 43 |
|
| 44 |
|
| 45 |
def cppBool(v): |
| 46 |
if v: |
| 47 |
return "true" |
| 48 |
return "false" |
| 49 |
|
| 50 |
|
| 51 |
def specTypeToMIRType(specType): |
| 52 |
if specType == "i32" or specType == "i64" or specType == "f32" or specType == "f64": |
| 53 |
return f"ValType::{specType}().toMIRType()" |
| 54 |
if ( |
| 55 |
specType == "externref" |
| 56 |
or specType == "anyref" |
| 57 |
or specType == "funcref" |
| 58 |
or specType == "exnref" |
| 59 |
or isinstance(specType, dict) |
| 60 |
): |
| 61 |
return "MIRType::WasmAnyRef" |
| 62 |
raise ValueError() |
| 63 |
|
| 64 |
|
| 65 |
def specHeapTypeToTypeCode(specHeapType): |
| 66 |
if specHeapType == "func": |
| 67 |
return "Func" |
| 68 |
if specHeapType == "any": |
| 69 |
return "Any" |
| 70 |
if specHeapType == "extern": |
| 71 |
return "Extern" |
| 72 |
if specHeapType == "exn": |
| 73 |
return "Exn" |
| 74 |
if specHeapType == "array": |
| 75 |
return "Array" |
| 76 |
if specHeapType == "struct": |
| 77 |
return "Struct" |
| 78 |
raise ValueError() |
| 79 |
|
| 80 |
|
| 81 |
def specTypeToValType(specType): |
| 82 |
if specType == "i32" or specType == "i64" or specType == "f32" or specType == "f64": |
| 83 |
return f"ValType::{specType}()" |
| 84 |
|
| 85 |
if specType == "externref": |
| 86 |
return "ValType(RefType::extern_())" |
| 87 |
|
| 88 |
if specType == "exnref": |
| 89 |
return "ValType(RefType::exn())" |
| 90 |
|
| 91 |
if specType == "anyref": |
| 92 |
return "ValType(RefType::any())" |
| 93 |
|
| 94 |
if specType == "funcref": |
| 95 |
return "ValType(RefType::func())" |
| 96 |
|
| 97 |
if isinstance(specType, dict): |
| 98 |
nullable = cppBool(specType["nullable"]) |
| 99 |
if "type" in specType: |
| 100 |
ref = specType["type"] |
| 101 |
return f"ValType(RefType::fromTypeDef({ref}, {nullable}))" |
| 102 |
else: |
| 103 |
code = specType["code"] |
| 104 |
return f"ValType(RefType::fromTypeCode(TypeCode(RefType::{specHeapTypeToTypeCode(code)}), {nullable}))" |
| 105 |
|
| 106 |
raise ValueError() |
| 107 |
|
| 108 |
|
| 109 |
def main(c_out, yaml_path): |
| 110 |
data = load_yaml(yaml_path) |
| 111 |
|
| 112 |
# Iterate for all defined builtin methods |
| 113 |
contents = "#define FOR_EACH_BUILTIN_MODULE_FUNC(M) \\\n" |
| 114 |
for i in range(len(data)): |
| 115 |
op = data[i] |
| 116 |
sa = op["symbolic_address"] |
| 117 |
inlineOp = "BuiltinInlineOp::None" |
| 118 |
if "inline_op" in op: |
| 119 |
inlineOp = f"BuiltinInlineOp::{op['inline_op']}" |
| 120 |
contents += ( |
| 121 |
f" M({op['op']}, \"{op['export']}\", " |
| 122 |
f"{sa['name']}, {sa['type']}, {op['entry']}, {cppBool(op['uses_memory'])}, {inlineOp}, {i})\\\n" |
| 123 |
) |
| 124 |
contents += "\n" |
| 125 |
|
| 126 |
for op in data: |
| 127 |
# Define DECLARE_BUILTIN_MODULE_FUNC_PARAM_VALTYPES_<op> as: |
| 128 |
# `{ValType::I32, ValType::I32, ...}`. |
| 129 |
valTypes = ", ".join(specTypeToValType(p) for p in op["params"]) |
| 130 |
contents += ( |
| 131 |
f"#define DECLARE_BUILTIN_MODULE_FUNC_PARAM_VALTYPES_{op['op']} " |
| 132 |
f"{{{valTypes}}}\n" |
| 133 |
) |
| 134 |
|
| 135 |
# Define DECLARE_BUILTIN_MODULE_FUNC_PARAM_MIRTYPES_<op> as: |
| 136 |
# `<num_types>, {MIRType::Pointer, _I32, ..., MIRType::Pointer, _END}`. |
| 137 |
num_types = len(op["params"]) + 1 |
| 138 |
mir_types = "{MIRType::Pointer" |
| 139 |
mir_types += "".join(", " + specTypeToMIRType(p) for p in op["params"]) |
| 140 |
if op["uses_memory"]: |
| 141 |
mir_types += ", MIRType::Pointer" |
| 142 |
num_types += 1 |
| 143 |
# Add the end marker |
| 144 |
mir_types += ", MIRType::None}" |
| 145 |
|
| 146 |
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_PARAM_MIRTYPES_{op['op']} {num_types}, {mir_types}\n" |
| 147 |
|
| 148 |
# Define DECLARE_BUILTIN_MODULE_FUNC_RESULT_VALTYPE_<op> as: |
| 149 |
# `Some(X)` if present, or else `Nothing()`. |
| 150 |
result_valtype = "" |
| 151 |
if "result" in op: |
| 152 |
result_valtype = f"mozilla::Some({specTypeToValType(op['result'])})\n" |
| 153 |
else: |
| 154 |
result_valtype = "mozilla::Nothing()" |
| 155 |
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_RESULT_VALTYPE_{op['op']} {result_valtype}\n" |
| 156 |
|
| 157 |
# Define DECLARE_BUILTIN_MODULE_FUNC_RESULT_MIRTYPE_<op> as: |
| 158 |
# `X` if present, or else `MIRType::None`. |
| 159 |
result_mirtype = "" |
| 160 |
if "result" in op: |
| 161 |
result_mirtype = specTypeToMIRType(op["result"]) + "\n" |
| 162 |
else: |
| 163 |
result_mirtype = "MIRType::None" |
| 164 |
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_RESULT_MIRTYPE_{op['op']} {result_mirtype}\n" |
| 165 |
|
| 166 |
# Define DECLARE_BUILTIN_MODULE_FUNC_FAILMODE_<op> as: |
| 167 |
# `FailureMode::X`. |
| 168 |
contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_FAILMODE_{op['op']} FailureMode::{op['fail_mode']}\n" |
| 169 |
|
| 170 |
generate_header(c_out, "wasm_WasmBuiltinModuleGenerated_h", contents) |