Backed out changeset 88fbb17e3c20 (bug 1865637) for causing animation related mochite...
[gecko.git] / js / src / wasm / GenerateBuiltinModules.py
blob17270bc46e7082e2e5587f0cbfcb15bbcb99f8e0
1 import buildconfig
2 import six
3 import yaml
4 from mozbuild.preprocessor import Preprocessor
6 HEADER_TEMPLATE = """\
7 /* This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #ifndef %(includeguard)s
12 #define %(includeguard)s
14 /* This file is generated by wasm/GenerateBuiltinModules.py. Do not edit! */
16 %(contents)s
18 #endif // %(includeguard)s
19 """
22 def generate_header(c_out, includeguard, contents):
23 c_out.write(
24 HEADER_TEMPLATE
25 % {
26 "includeguard": includeguard,
27 "contents": contents,
32 def load_yaml(yaml_path):
33 # First invoke preprocessor.py so that we can use #ifdef JS_SIMULATOR in
34 # the YAML file.
35 pp = Preprocessor()
36 pp.context.update(buildconfig.defines["ALLDEFINES"])
37 pp.out = six.StringIO()
38 pp.do_filter("substitution")
39 pp.do_include(yaml_path)
40 contents = pp.out.getvalue()
41 return yaml.safe_load(contents)
44 def cppBool(v):
45 if v:
46 return "true"
47 return "false"
50 def main(c_out, yaml_path):
51 data = load_yaml(yaml_path)
53 # Iterate for all defined builtin methods
54 contents = "#define FOR_EACH_BUILTIN_MODULE_FUNC(M) \\\n"
55 for i in range(len(data)):
56 op = data[i]
57 sa = op["symbolic_address"]
58 contents += (
59 f" M({op['op']}, \"{op['export']}\", "
60 f"{sa['name']}, {sa['type']}, {op['entry']}, {cppBool(op['uses_memory'])}, {i})\\\n"
62 contents += "\n"
64 for op in data:
65 # Define DECLARE_BUILTIN_MODULE_FUNC_PARAM_VALTYPES_<op> as:
66 # `{ValType::I32, ValType::I32, ...}`.
67 contents += (
68 f"#define DECLARE_BUILTIN_MODULE_FUNC_PARAM_VALTYPES_{op['op']} "
69 f"{{{', '.join(op['params'])}}}\n"
72 # Define DECLARE_BUILTIN_MODULE_FUNC_PARAM_SASTYPES_<op> as:
73 # `<num_types>, {_PTR, _I32, ..., _PTR, _END}`.
74 num_types = len(op["params"]) + 1
75 sas_types = (
76 f"{{_PTR{''.join(', ' + (p + '.toMIRType()') for p in op['params'])}"
78 if op["uses_memory"]:
79 sas_types += ", _PTR"
80 num_types += 1
81 sas_types += ", _END}"
83 contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_PARAM_SASTYPES_{op['op']} {num_types}, {sas_types}\n"
85 result_valtype = ""
86 result_sastype = ""
87 if "result" in op:
88 result_valtype = f"Some({op['result']})\n"
89 result_sastype = f"{op['result']}.toMIRType()\n"
90 else:
91 result_valtype = "Nothing()"
92 result_sastype = "_VOID"
93 contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_RESULT_VALTYPE_{op['op']} {result_valtype}\n"
94 contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_RESULT_SASTYPE_{op['op']} {result_sastype}\n"
95 contents += f"#define DECLARE_BUILTIN_MODULE_FUNC_FAILMODE_{op['op']} _{op['fail_mode']}\n"
97 generate_header(c_out, "wasm_WasmBuiltinModuleGenerated_h", contents)