1import buildconfig 2import yaml 3import six 4from collections import OrderedDict 5from mozbuild.preprocessor import Preprocessor 6 7HEADER_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/GenerateInstrinsic.py. Do not edit! */ 16 17%(contents)s 18 19#endif // %(includeguard)s 20""" 21 22 23def generate_header(c_out, includeguard, contents): 24 c_out.write( 25 HEADER_TEMPLATE 26 % { 27 "includeguard": includeguard, 28 "contents": contents, 29 } 30 ) 31 32 33def 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 = six.StringIO() 39 pp.do_filter("substitution") 40 pp.do_include(yaml_path) 41 contents = pp.out.getvalue() 42 43 # Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ 44 # also preserves ordering for normal dictionaries. 45 # Code based on https://stackoverflow.com/a/21912744. 46 class OrderedLoader(yaml.Loader): 47 pass 48 49 def construct_mapping(loader, node): 50 loader.flatten_mapping(node) 51 return OrderedDict(loader.construct_pairs(node)) 52 53 tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG 54 OrderedLoader.add_constructor(tag, construct_mapping) 55 return yaml.load(contents, OrderedLoader) 56 57 58def main(c_out, yaml_path): 59 data = load_yaml(yaml_path) 60 61 # Interate for all defined intrinsics 62 contents = "#define FOR_EACH_INTRINSIC(M) \\\n" 63 for i in range(len(data)): 64 op = data[i] 65 sa = op["symbolic_address"] 66 contents += ( 67 f" M({op['op']}, \"{op['export']}\", " 68 f"{sa['name']}, {sa['type']}, {op['entry']}, {i})\\\n" 69 ) 70 contents += "\n" 71 72 for op in data: 73 # Define DECLARE_INTRINSIC_SAS_PARAM_VALTYPES_<op> as: 74 # `{ValType::I32, ValType::I32, ...}`. 75 contents += ( 76 f"#define DECLARE_INTRINSIC_SAS_PARAM_VALTYPES_{op['op']} " 77 f"{{ValType::{', ValType::'.join(op['params'])}}}\n" 78 ) 79 # Define DECLARE_INTRINSIC_PARAM_TYPES_<op> as: 80 # `<num_types>, {_PTR, _I32, ..., _PTR, _END}`. 81 sas_types = f"{{_PTR{''.join(', _' + p for p in op['params'])}, _PTR, _END}}" 82 num_types = len(op["params"]) + 2 83 contents += f"#define DECLARE_INTRINSIC_PARAM_TYPES_{op['op']} {num_types}, {sas_types}\n" 84 85 generate_header(c_out, "wasm_WasmIntrinsicGenerated_h", contents) 86