From de49f38b3e98bbe1fc9935aa00d595e699bf23a1 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 8 Feb 2024 13:05:14 -0800 Subject: [PATCH 1/3] start --- src/passes/StringLowering.cpp | 3 ++ src/tools/wasm-opt.cpp | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp index e1bae85200a..bc60782e288 100644 --- a/src/passes/StringLowering.cpp +++ b/src/passes/StringLowering.cpp @@ -25,6 +25,9 @@ // imported globals of type externref, for use with the string imports proposal. // String operations will likewise need to be lowered. TODO // +// Specs: +// https://github.com/WebAssembly/stringref/blob/main/proposals/stringref/Overview.md +// https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md #include diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index 0b0ea2cd4b5..b534db30fd7 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -70,6 +70,62 @@ willRemoveDebugInfo(const std::vector& passes) { return false; } +static std::string capitalize(std::string str) { + assert(!str.empty()); + str[0] = std::toupper(str[0]); + return str; +} + +static std::string unpluralize(std::string str) { + assert(!str.empty()); + if (str.back() == 's') { + str.pop_back(); + } + return str; +} + +static void generateCAPIHeader() { + size_t id = Expression::InvalidId; + // No need to emit anything for the invalid ID. + id++; + // Iterate overall expressions and emit header info. + for (; id < Expression::NumExpressionIds; id++) { + +#define DELEGATE_ID id + +#define DELEGATE_START(id) \ + std::cout << "// " #id << '\n'; + +#define DELEGATE_END(id) \ + std::cout << '\n'; + +#define DELEGATE_FIELD_CHILD(id, field) \ + std::cout << "BINARYEN_API BinaryenExpressionRef Binaryen" << #id << "Get" << capitalize(#field) << "(BinaryenExpressionRef expr);\n"; + +#define DELEGATE_FIELD_CHILD_VECTOR(id, field) \ + std::cout << "BINARYEN_API BinaryenExpressionRef Binaryen" << #id << "Get" << unpluralize(capitalize(#field)) << "At(BinaryenExpressionRef expr, BinaryenIndex index);\n"; + +// TODO +#define DELEGATE_FIELD_TYPE(id, field) +#define DELEGATE_FIELD_HEAPTYPE(id, field) +#define DELEGATE_FIELD_OPTIONAL_CHILD(id, field) +#define DELEGATE_FIELD_INT(id, field) +#define DELEGATE_FIELD_LITERAL(id, field) +#define DELEGATE_FIELD_NAME(id, field) +#define DELEGATE_FIELD_SCOPE_NAME_DEF(id, field) +#define DELEGATE_FIELD_SCOPE_NAME_USE(id, field) +#define DELEGATE_FIELD_ADDRESS(id, field) +#define DELEGATE_FIELD_INT_ARRAY(id, field) +#define DELEGATE_FIELD_INT_VECTOR(id, field) +#define DELEGATE_FIELD_NAME_VECTOR(id, field) +#define DELEGATE_FIELD_SCOPE_NAME_USE_VECTOR(id, field) +#define DELEGATE_FIELD_TYPE_VECTOR(id, field) + +#include "wasm-delegations-fields.def" + + } +} + // // main // @@ -237,6 +293,15 @@ int main(int argc, const char* argv[]) { WasmOptOption, Options::Arguments::Zero, [](Options*, const std::string&) { useNewWATParser = true; }) + .add("--gen-c-api-h", + "", + "Generate C API header and exit", + WasmOptOption, + Options::Arguments::Zero, + [](Options* o, const std::string&) { + generateCAPIHeader(); + exit(EXIT_SUCCESS); + }) .add_positional("INFILE", Options::Arguments::One, [](Options* o, const std::string& argument) { From c1b69d44f52210f502087076e25a287adeddbae8 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 8 Feb 2024 13:06:19 -0800 Subject: [PATCH 2/3] format --- src/tools/wasm-opt.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp index b534db30fd7..a2581035f80 100644 --- a/src/tools/wasm-opt.cpp +++ b/src/tools/wasm-opt.cpp @@ -93,17 +93,18 @@ static void generateCAPIHeader() { #define DELEGATE_ID id -#define DELEGATE_START(id) \ - std::cout << "// " #id << '\n'; +#define DELEGATE_START(id) std::cout << "// " #id << '\n'; -#define DELEGATE_END(id) \ - std::cout << '\n'; +#define DELEGATE_END(id) std::cout << '\n'; -#define DELEGATE_FIELD_CHILD(id, field) \ - std::cout << "BINARYEN_API BinaryenExpressionRef Binaryen" << #id << "Get" << capitalize(#field) << "(BinaryenExpressionRef expr);\n"; +#define DELEGATE_FIELD_CHILD(id, field) \ + std::cout << "BINARYEN_API BinaryenExpressionRef Binaryen" << #id << "Get" \ + << capitalize(#field) << "(BinaryenExpressionRef expr);\n"; -#define DELEGATE_FIELD_CHILD_VECTOR(id, field) \ - std::cout << "BINARYEN_API BinaryenExpressionRef Binaryen" << #id << "Get" << unpluralize(capitalize(#field)) << "At(BinaryenExpressionRef expr, BinaryenIndex index);\n"; +#define DELEGATE_FIELD_CHILD_VECTOR(id, field) \ + std::cout << "BINARYEN_API BinaryenExpressionRef Binaryen" << #id << "Get" \ + << unpluralize(capitalize(#field)) \ + << "At(BinaryenExpressionRef expr, BinaryenIndex index);\n"; // TODO #define DELEGATE_FIELD_TYPE(id, field) @@ -122,7 +123,6 @@ static void generateCAPIHeader() { #define DELEGATE_FIELD_TYPE_VECTOR(id, field) #include "wasm-delegations-fields.def" - } } From 62d5cb4e509efa543fdcc381d8f5fed02b162272 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 8 Feb 2024 13:20:23 -0800 Subject: [PATCH 3/3] clean --- src/passes/StringLowering.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp index bc60782e288..e1bae85200a 100644 --- a/src/passes/StringLowering.cpp +++ b/src/passes/StringLowering.cpp @@ -25,9 +25,6 @@ // imported globals of type externref, for use with the string imports proposal. // String operations will likewise need to be lowered. TODO // -// Specs: -// https://github.com/WebAssembly/stringref/blob/main/proposals/stringref/Overview.md -// https://github.com/WebAssembly/js-string-builtins/blob/main/proposals/js-string-builtins/Overview.md #include