Merge remote-tracking branch 'origin/develop' into breaking

This commit is contained in:
chriseth
2020-11-10 13:48:32 +01:00
72 changed files with 608 additions and 112 deletions
+19 -7
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(
@@ -663,9 +673,11 @@ bytes BinaryTransform::globalSection(vector<wasm::GlobalVariableDeclaration> con
bytes BinaryTransform::exportSection(map<string, size_t> const& _functionIDs)
{
bytes result = lebEncode(2);
bool hasMain = _functionIDs.count("main");
bytes result = lebEncode(hasMain ? 2 : 1);
result += encodeName("memory") + toBytes(Export::Memory) + lebEncode(0);
result += encodeName("main") + toBytes(Export::Function) + lebEncode(_functionIDs.at("main"));
if (hasMain)
result += encodeName("main") + toBytes(Export::Function) + lebEncode(_functionIDs.at("main"));
return makeSection(Section::EXPORT, move(result));
}
+2
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;
+12 -2
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;
@@ -56,8 +61,13 @@ string TextTransform::run(wasm::Module const& _module)
// allocate one 64k page of memory and make it available to the Ethereum client
ret += " (memory $memory (export \"memory\") 1)\n";
// export the main function
ret += " (export \"main\" (func $main))\n";
for (auto const& f: _module.functions)
if (f.name == "main")
{
// export the main function
ret += " (export \"main\" (func $main))\n";
break;
}
for (auto const& g: _module.globals)
ret += " (global $" + g.variableName + " (mut " + encodeType(g.type) + ") (" + encodeType(g.type) + ".const 0))\n";
+3
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;
};
}
+3 -1
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;
}