From e41700e5f79d789b738df613e00898f71708fe82 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 9 Nov 2020 17:43:41 +0000 Subject: [PATCH 1/2] [ewasm] Clarify custom section handling in BinaryTransform --- libyul/backends/wasm/BinaryTransform.cpp | 12 +++++++----- libyul/backends/wasm/BinaryTransform.h | 2 ++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libyul/backends/wasm/BinaryTransform.cpp b/libyul/backends/wasm/BinaryTransform.cpp index 58a34ac04..87f47e28c 100644 --- a/libyul/backends/wasm/BinaryTransform.cpp +++ b/libyul/backends/wasm/BinaryTransform.cpp @@ -305,13 +305,15 @@ bytes BinaryTransform::run(Module const& _module) ret += exportSection(functionIDs); map> subModulePosAndSize; - for (auto const& sub: _module.subModules) + for (auto const& [name, module]: _module.subModules) { // TODO should we prefix and / or shorten the name? - bytes data = BinaryTransform::run(sub.second); - size_t length = data.size(); - ret += customSection(sub.first, move(data)); - subModulePosAndSize[sub.first] = {ret.size() - length, length}; + bytes data = BinaryTransform::run(module); + size_t const length = data.size(); + ret += customSection(name, move(data)); + // Skip all the previous sections and the size field of this current custom section. + size_t const offset = ret.size() - length; + subModulePosAndSize[name] = {offset, length}; } BinaryTransform bt( diff --git a/libyul/backends/wasm/BinaryTransform.h b/libyul/backends/wasm/BinaryTransform.h index 348250546..d164c11bf 100644 --- a/libyul/backends/wasm/BinaryTransform.h +++ b/libyul/backends/wasm/BinaryTransform.h @@ -112,6 +112,8 @@ private: std::map const m_globalIDs; std::map const m_functionIDs; std::map const m_functionTypes; + /// The map of submodules, where the pair refers to the [offset, length]. The offset is + /// an absolute offset within the resulting assembled bytecode. std::map> const m_subModulePosAndSize; std::map m_locals; From 6e11754c8b3820355ca6f9fa46a0872435e360ef Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 30 Oct 2020 18:03:27 +0000 Subject: [PATCH 2/2] [ewasm] Support compiling data subobjects --- libyul/backends/wasm/BinaryTransform.cpp | 8 ++++++++ libyul/backends/wasm/TextTransform.cpp | 5 +++++ libyul/backends/wasm/WasmAST.h | 3 +++ libyul/backends/wasm/WasmObjectCompiler.cpp | 4 +++- 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/libyul/backends/wasm/BinaryTransform.cpp b/libyul/backends/wasm/BinaryTransform.cpp index 87f47e28c..17a9d0334 100644 --- a/libyul/backends/wasm/BinaryTransform.cpp +++ b/libyul/backends/wasm/BinaryTransform.cpp @@ -315,6 +315,14 @@ bytes BinaryTransform::run(Module const& _module) size_t const offset = ret.size() - length; subModulePosAndSize[name] = {offset, length}; } + for (auto const& [name, data]: _module.customSections) + { + size_t const length = data.size(); + ret += customSection(name, data); + // Skip all the previous sections and the size field of this current custom section. + size_t const offset = ret.size() - length; + subModulePosAndSize[name] = {offset, length}; + } BinaryTransform bt( move(globalIDs), diff --git a/libyul/backends/wasm/TextTransform.cpp b/libyul/backends/wasm/TextTransform.cpp index 89123b16c..05514cd7c 100644 --- a/libyul/backends/wasm/TextTransform.cpp +++ b/libyul/backends/wasm/TextTransform.cpp @@ -44,6 +44,11 @@ string TextTransform::run(wasm::Module const& _module) " ;; sub-module \"" + sub.first + "\" will be encoded as custom section in binary here, but is skipped in text mode.\n"; + for (auto const& data: _module.customSections) + ret += + " ;; custom-section \"" + + data.first + + "\" will be encoded as custom section in binary here, but is skipped in text mode.\n"; for (wasm::FunctionImport const& imp: _module.imports) { ret += " (import \"" + imp.module + "\" \"" + imp.externalName + "\" (func $" + imp.internalName; diff --git a/libyul/backends/wasm/WasmAST.h b/libyul/backends/wasm/WasmAST.h index ced3b7bc6..9abdd14d8 100644 --- a/libyul/backends/wasm/WasmAST.h +++ b/libyul/backends/wasm/WasmAST.h @@ -21,6 +21,8 @@ #pragma once +#include + #include #include #include @@ -108,6 +110,7 @@ struct Module std::vector imports; std::vector functions; std::map subModules; + std::map customSections; }; } diff --git a/libyul/backends/wasm/WasmObjectCompiler.cpp b/libyul/backends/wasm/WasmObjectCompiler.cpp index 2b4f0c355..bdd109d31 100644 --- a/libyul/backends/wasm/WasmObjectCompiler.cpp +++ b/libyul/backends/wasm/WasmObjectCompiler.cpp @@ -51,8 +51,10 @@ wasm::Module WasmObjectCompiler::run(Object& _object) for (auto& subNode: _object.subObjects) if (Object* subObject = dynamic_cast(subNode.get())) module.subModules[subObject->name.str()] = run(*subObject); + else if (Data* subObject = dynamic_cast(subNode.get())) + module.customSections[subObject->name.str()] = subObject->data; else - yulAssert(false, "Data is not yet supported for Wasm."); + yulAssert(false, ""); return module; }