Merge pull request #10219 from ethereum/wasm-objects

[ewasm] Support compiling data subobjects
This commit is contained in:
Alex Beregszaszi 2020-11-09 21:05:37 +00:00 committed by GitHub
commit 41f5036507
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 6 deletions

View File

@ -305,13 +305,23 @@ bytes BinaryTransform::run(Module const& _module)
ret += exportSection(functionIDs);
map<string, pair<size_t, size_t>> 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};
}
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(

View File

@ -112,6 +112,8 @@ private:
std::map<std::string, size_t> const m_globalIDs;
std::map<std::string, size_t> const m_functionIDs;
std::map<std::string, size_t> 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<std::string, std::pair<size_t, size_t>> const m_subModulePosAndSize;
std::map<std::string, size_t> m_locals;

View File

@ -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;

View File

@ -21,6 +21,8 @@
#pragma once
#include <libsolutil/Common.h>
#include <variant>
#include <string>
#include <vector>
@ -108,6 +110,7 @@ struct Module
std::vector<FunctionImport> imports;
std::vector<FunctionDefinition> functions;
std::map<std::string, Module> subModules;
std::map<std::string, bytes> customSections;
};
}

View File

@ -51,8 +51,10 @@ wasm::Module WasmObjectCompiler::run(Object& _object)
for (auto& subNode: _object.subObjects)
if (Object* subObject = dynamic_cast<Object*>(subNode.get()))
module.subModules[subObject->name.str()] = run(*subObject);
else if (Data* subObject = dynamic_cast<Data*>(subNode.get()))
module.customSections[subObject->name.str()] = subObject->data;
else
yulAssert(false, "Data is not yet supported for Wasm.");
yulAssert(false, "");
return module;
}