From cd08dab4e644c7c237972d510ce3448d54e9b030 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 5 Nov 2020 17:26:04 +0000 Subject: [PATCH] [ewasm] Allow compiling Yul without "main" --- libyul/backends/wasm/BinaryTransform.cpp | 6 ++++-- libyul/backends/wasm/TextTransform.cpp | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/libyul/backends/wasm/BinaryTransform.cpp b/libyul/backends/wasm/BinaryTransform.cpp index 6c19b19f5..58a34ac04 100644 --- a/libyul/backends/wasm/BinaryTransform.cpp +++ b/libyul/backends/wasm/BinaryTransform.cpp @@ -663,9 +663,11 @@ bytes BinaryTransform::globalSection(vector con bytes BinaryTransform::exportSection(map 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)); } diff --git a/libyul/backends/wasm/TextTransform.cpp b/libyul/backends/wasm/TextTransform.cpp index 059f89600..89123b16c 100644 --- a/libyul/backends/wasm/TextTransform.cpp +++ b/libyul/backends/wasm/TextTransform.cpp @@ -56,8 +56,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";