forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1721686 - wasm: Generate intrinsics boilerplate code via yaml. r=…
…rhunt Differential Revision: https://phabricator.services.mozilla.com/D120724
- Loading branch information
1 parent
3aa3d28
commit ad2ffab
Showing
9 changed files
with
170 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import buildconfig | ||
import yaml | ||
import six | ||
from collections import OrderedDict | ||
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/GenerateInstrinsic.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() | ||
|
||
# Load into an OrderedDict to ensure order is preserved. Note: Python 3.7+ | ||
# also preserves ordering for normal dictionaries. | ||
# Code based on https://stackoverflow.com/a/21912744. | ||
class OrderedLoader(yaml.Loader): | ||
pass | ||
|
||
def construct_mapping(loader, node): | ||
loader.flatten_mapping(node) | ||
return OrderedDict(loader.construct_pairs(node)) | ||
|
||
tag = yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG | ||
OrderedLoader.add_constructor(tag, construct_mapping) | ||
return yaml.load(contents, OrderedLoader) | ||
|
||
|
||
def main(c_out, yaml_path): | ||
data = load_yaml(yaml_path) | ||
|
||
# Interate for all defined intrinsics | ||
contents = "#define FOR_EACH_INTRINSIC(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']}, {i})\\\n" | ||
) | ||
contents += "\n" | ||
|
||
for op in data: | ||
# Define DECLARE_INTRINSIC_SAS_PARAM_VALTYPES_<op> as: | ||
# `{ValType::I32, ValType::I32, ...}`. | ||
contents += ( | ||
f"#define DECLARE_INTRINSIC_SAS_PARAM_VALTYPES_{op['op']} " | ||
f"{{ValType::{', ValType::'.join(op['params'])}}}\n" | ||
) | ||
# Define DECLARE_INTRINSIC_PARAM_TYPES_<op> as: | ||
# `<num_types>, {_PTR, _I32, ..., _PTR, _END}`. | ||
sas_types = f"{{_PTR{''.join(', _' + p for p in op['params'])}, _PTR, _END}}" | ||
num_types = len(op["params"]) + 2 | ||
contents += f"#define DECLARE_INTRINSIC_PARAM_TYPES_{op['op']} {num_types}, {sas_types}\n" | ||
|
||
generate_header(c_out, "wasm_WasmIntrinsicGenerated_h", contents) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# The file contains information need to define wasm intrinsic operations. | ||
|
||
# i8vecmul(dest: i32, src1: i32, src2: i32, len: i32) | ||
# Performs pairwise multiplication of two i8 vectors of 'len' specified at | ||
# 'src1' and 'src2'. Output is written to 'dest'. This is used as a | ||
# basic self-test for intrinsics. | ||
- op: I8VecMul | ||
symbolic_address: | ||
name: IntrI8VecMul | ||
type: Args_Int32_GeneralInt32Int32Int32Int32General | ||
entry: Instance::intrI8VecMul | ||
export: i8vecmul | ||
params: | ||
- I32 | ||
- I32 | ||
- I32 | ||
- I32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters