mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #6716 from ethereum/wasmTranslation
[Yul] EVM to Wasm translation
This commit is contained in:
commit
8d18003808
@ -7,6 +7,7 @@ Language Features:
|
||||
|
||||
|
||||
Compiler Features:
|
||||
* eWasm: Highly experimental eWasm output using ``--ewasm`` in the commandline interface or output selection of ``ewasm.wast`` in standard-json.
|
||||
|
||||
|
||||
|
||||
|
@ -49,6 +49,12 @@
|
||||
#include <libsolidity/codegen/ir/IRGenerator.h>
|
||||
|
||||
#include <libyul/YulString.h>
|
||||
#include <libyul/AsmPrinter.h>
|
||||
#include <libyul/backends/wasm/EVMToEWasmTranslator.h>
|
||||
#include <libyul/backends/wasm/EWasmObjectCompiler.h>
|
||||
#include <libyul/backends/wasm/WasmDialect.h>
|
||||
#include <libyul/backends/evm/EVMDialect.h>
|
||||
#include <libyul/AssemblyStack.h>
|
||||
|
||||
#include <liblangutil/Scanner.h>
|
||||
#include <liblangutil/SemVerHandler.h>
|
||||
@ -73,6 +79,7 @@ static int g_compilerStackCounts = 0;
|
||||
CompilerStack::CompilerStack(ReadCallback::Callback const& _readFile):
|
||||
m_readFile{_readFile},
|
||||
m_generateIR{false},
|
||||
m_generateEWasm{false},
|
||||
m_errorList{},
|
||||
m_errorReporter{m_errorList}
|
||||
{
|
||||
@ -171,6 +178,7 @@ void CompilerStack::reset(bool _keepSettings)
|
||||
m_libraries.clear();
|
||||
m_evmVersion = langutil::EVMVersion();
|
||||
m_generateIR = false;
|
||||
m_generateEWasm = false;
|
||||
m_optimiserSettings = OptimiserSettings::minimal();
|
||||
m_metadataLiteralSources = false;
|
||||
}
|
||||
@ -413,8 +421,10 @@ bool CompilerStack::compile()
|
||||
if (isRequestedContract(*contract))
|
||||
{
|
||||
compileContract(*contract, otherCompilers);
|
||||
if (m_generateIR)
|
||||
if (m_generateIR || m_generateEWasm)
|
||||
generateIR(*contract);
|
||||
if (m_generateEWasm)
|
||||
generateEWasm(*contract);
|
||||
}
|
||||
m_stackState = CompilationSuccessful;
|
||||
this->link();
|
||||
@ -540,6 +550,14 @@ string const& CompilerStack::yulIROptimized(string const& _contractName) const
|
||||
return contract(_contractName).yulIROptimized;
|
||||
}
|
||||
|
||||
string const& CompilerStack::eWasm(string const& _contractName) const
|
||||
{
|
||||
if (m_stackState != CompilationSuccessful)
|
||||
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_comment("Compilation was not successful."));
|
||||
|
||||
return contract(_contractName).eWasm;
|
||||
}
|
||||
|
||||
eth::LinkerObject const& CompilerStack::object(string const& _contractName) const
|
||||
{
|
||||
if (m_stackState != CompilationSuccessful)
|
||||
@ -971,6 +989,36 @@ void CompilerStack::generateIR(ContractDefinition const& _contract)
|
||||
tie(compiledContract.yulIR, compiledContract.yulIROptimized) = generator.run(_contract);
|
||||
}
|
||||
|
||||
void CompilerStack::generateEWasm(ContractDefinition const& _contract)
|
||||
{
|
||||
solAssert(m_stackState >= AnalysisSuccessful, "");
|
||||
Contract& compiledContract = m_contracts.at(_contract.fullyQualifiedName());
|
||||
solAssert(!compiledContract.yulIROptimized.empty(), "");
|
||||
if (!compiledContract.eWasm.empty())
|
||||
return;
|
||||
|
||||
// Re-parse the Yul IR in EVM dialect
|
||||
yul::AssemblyStack evmStack(m_evmVersion, yul::AssemblyStack::Language::StrictAssembly, m_optimiserSettings);
|
||||
evmStack.parseAndAnalyze("", compiledContract.yulIROptimized);
|
||||
|
||||
// Turn into eWasm dialect
|
||||
yul::Object ewasmObject = yul::EVMToEWasmTranslator(
|
||||
yul::EVMDialect::strictAssemblyForEVMObjects(m_evmVersion)
|
||||
).run(*evmStack.parserResult());
|
||||
|
||||
// Re-inject into an assembly stack for the eWasm dialect
|
||||
yul::AssemblyStack ewasmStack(m_evmVersion, yul::AssemblyStack::Language::EWasm, m_optimiserSettings);
|
||||
// TODO this is a hack for now - provide as structured AST!
|
||||
ewasmStack.parseAndAnalyze("", "{}");
|
||||
*ewasmStack.parserResult() = move(ewasmObject);
|
||||
ewasmStack.optimize();
|
||||
|
||||
//cout << yul::AsmPrinter{}(*ewasmStack.parserResult()->code) << endl;
|
||||
|
||||
// Turn into eWasm text representation.
|
||||
compiledContract.eWasm = ewasmStack.assemble(yul::AssemblyStack::Machine::eWasm).assembly;
|
||||
}
|
||||
|
||||
CompilerStack::Contract const& CompilerStack::contract(string const& _contractName) const
|
||||
{
|
||||
solAssert(m_stackState >= AnalysisSuccessful, "");
|
||||
|
@ -154,6 +154,9 @@ public:
|
||||
/// Enable experimental generation of Yul IR code.
|
||||
void enableIRGeneration(bool _enable = true) { m_generateIR = _enable; }
|
||||
|
||||
/// Enable experimental generation of eWasm code. If enabled, IR is also generated.
|
||||
void enableEWasmGeneration(bool _enable = true) { m_generateEWasm = _enable; }
|
||||
|
||||
/// @arg _metadataLiteralSources When true, store sources as literals in the contract metadata.
|
||||
/// Must be set before parsing.
|
||||
void useMetadataLiteralSources(bool _metadataLiteralSources);
|
||||
@ -219,6 +222,9 @@ public:
|
||||
/// @returns the optimized IR representation of a contract.
|
||||
std::string const& yulIROptimized(std::string const& _contractName) const;
|
||||
|
||||
/// @returns the eWasm (text) representation of a contract.
|
||||
std::string const& eWasm(std::string const& _contractName) const;
|
||||
|
||||
/// @returns the assembled object for a contract.
|
||||
eth::LinkerObject const& object(std::string const& _contractName) const;
|
||||
|
||||
@ -296,6 +302,7 @@ private:
|
||||
eth::LinkerObject runtimeObject; ///< Runtime object.
|
||||
std::string yulIR; ///< Experimental Yul IR code.
|
||||
std::string yulIROptimized; ///< Optimized experimental Yul IR code.
|
||||
std::string eWasm; ///< Experimental eWasm code (text representation).
|
||||
mutable std::unique_ptr<std::string const> metadata; ///< The metadata json that will be hashed into the chain.
|
||||
mutable std::unique_ptr<Json::Value const> abi;
|
||||
mutable std::unique_ptr<Json::Value const> userDocumentation;
|
||||
@ -326,6 +333,9 @@ private:
|
||||
/// The IR is stored but otherwise unused.
|
||||
void generateIR(ContractDefinition const& _contract);
|
||||
|
||||
/// Generate eWasm text representation for a single contract.
|
||||
void generateEWasm(ContractDefinition const& _contract);
|
||||
|
||||
/// Links all the known library addresses in the available objects. Any unknown
|
||||
/// library will still be kept as an unlinked placeholder in the objects.
|
||||
void link();
|
||||
@ -379,6 +389,7 @@ private:
|
||||
langutil::EVMVersion m_evmVersion;
|
||||
std::set<std::string> m_requestedContractNames;
|
||||
bool m_generateIR;
|
||||
bool m_generateEWasm;
|
||||
std::map<std::string, h160> m_libraries;
|
||||
/// list of path prefix remappings, e.g. mylibrary: github.com/ethereum = /usr/local/ethereum
|
||||
/// "context:prefix=target"
|
||||
|
@ -129,16 +129,17 @@ bool hashMatchesContent(string const& _hash, string const& _content)
|
||||
}
|
||||
}
|
||||
|
||||
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _artifact, bool _wildcardMatchesIR)
|
||||
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _artifact, bool _wildcardMatchesExperimental)
|
||||
{
|
||||
static set<string> experimental{"ir", "irOptimized", "wast", "ewasm", "ewasm.wast"};
|
||||
for (auto const& artifact: _outputSelection)
|
||||
/// @TODO support sub-matching, e.g "evm" matches "evm.assembly"
|
||||
if (artifact == _artifact)
|
||||
return true;
|
||||
else if (artifact == "*")
|
||||
{
|
||||
// "ir" and "irOptimized" can only be matched by "*" if activated.
|
||||
if ((_artifact != "ir" && _artifact != "irOptimized") || _wildcardMatchesIR)
|
||||
// "ir", "irOptimized", "wast" and "ewasm.wast" can only be matched by "*" if activated.
|
||||
if (experimental.count(_artifact) == 0 || _wildcardMatchesExperimental)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -157,7 +158,7 @@ bool isArtifactRequested(Json::Value const& _outputSelection, string const& _art
|
||||
///
|
||||
/// @TODO optimise this. Perhaps flatten the structure upfront.
|
||||
///
|
||||
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _file, string const& _contract, string const& _artifact, bool _wildcardMatchesIR)
|
||||
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _file, string const& _contract, string const& _artifact, bool _wildcardMatchesExperimental)
|
||||
{
|
||||
if (!_outputSelection.isObject())
|
||||
return false;
|
||||
@ -174,7 +175,7 @@ bool isArtifactRequested(Json::Value const& _outputSelection, string const& _fil
|
||||
if (
|
||||
_outputSelection[file].isMember(contract) &&
|
||||
_outputSelection[file][contract].isArray() &&
|
||||
isArtifactRequested(_outputSelection[file][contract], _artifact, _wildcardMatchesIR)
|
||||
isArtifactRequested(_outputSelection[file][contract], _artifact, _wildcardMatchesExperimental)
|
||||
)
|
||||
return true;
|
||||
}
|
||||
@ -182,10 +183,10 @@ bool isArtifactRequested(Json::Value const& _outputSelection, string const& _fil
|
||||
return false;
|
||||
}
|
||||
|
||||
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _file, string const& _contract, vector<string> const& _artifacts, bool _wildcardMatchesIR)
|
||||
bool isArtifactRequested(Json::Value const& _outputSelection, string const& _file, string const& _contract, vector<string> const& _artifacts, bool _wildcardMatchesExperimental)
|
||||
{
|
||||
for (auto const& artifact: _artifacts)
|
||||
if (isArtifactRequested(_outputSelection, _file, _contract, artifact, _wildcardMatchesIR))
|
||||
if (isArtifactRequested(_outputSelection, _file, _contract, artifact, _wildcardMatchesExperimental))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@ -200,6 +201,7 @@ bool isBinaryRequested(Json::Value const& _outputSelection)
|
||||
static vector<string> const outputsThatRequireBinaries{
|
||||
"*",
|
||||
"ir", "irOptimized",
|
||||
"wast", "wasm", "ewasm.wast", "ewasm.wasm",
|
||||
"evm.deployedBytecode", "evm.deployedBytecode.object", "evm.deployedBytecode.opcodes",
|
||||
"evm.deployedBytecode.sourceMap", "evm.deployedBytecode.linkReferences",
|
||||
"evm.bytecode", "evm.bytecode.object", "evm.bytecode.opcodes", "evm.bytecode.sourceMap",
|
||||
@ -215,10 +217,29 @@ bool isBinaryRequested(Json::Value const& _outputSelection)
|
||||
return false;
|
||||
}
|
||||
|
||||
/// @returns true if any eWasm code was requested. Note that as an exception, '*' does not
|
||||
/// yet match "ewasm.wast" or "ewasm"
|
||||
bool isEWasmRequested(Json::Value const& _outputSelection)
|
||||
{
|
||||
if (!_outputSelection.isObject())
|
||||
return false;
|
||||
|
||||
for (auto const& fileRequests: _outputSelection)
|
||||
for (auto const& requests: fileRequests)
|
||||
for (auto const& request: requests)
|
||||
if (request == "ewasm" || request == "ewasm.wast")
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// @returns true if any Yul IR was requested. Note that as an exception, '*' does not
|
||||
/// yet match "ir" or "irOptimized"
|
||||
bool isIRRequested(Json::Value const& _outputSelection)
|
||||
{
|
||||
if (isEWasmRequested(_outputSelection))
|
||||
return true;
|
||||
|
||||
if (!_outputSelection.isObject())
|
||||
return false;
|
||||
|
||||
@ -231,7 +252,6 @@ bool isIRRequested(Json::Value const& _outputSelection)
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Json::Value formatLinkReferences(std::map<size_t, std::string> const& linkReferences)
|
||||
{
|
||||
Json::Value ret(Json::objectValue);
|
||||
@ -689,9 +709,9 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
|
||||
compilerStack.useMetadataLiteralSources(_inputsAndSettings.metadataLiteralSources);
|
||||
compilerStack.setRequestedContractNames(requestedContractNames(_inputsAndSettings.outputSelection));
|
||||
|
||||
bool const irRequested = isIRRequested(_inputsAndSettings.outputSelection);
|
||||
compilerStack.enableIRGeneration(isIRRequested(_inputsAndSettings.outputSelection));
|
||||
|
||||
compilerStack.enableIRGeneration(irRequested);
|
||||
compilerStack.enableEWasmGeneration(isEWasmRequested(_inputsAndSettings.outputSelection));
|
||||
|
||||
Json::Value errors = std::move(_inputsAndSettings.errors);
|
||||
|
||||
@ -812,7 +832,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
|
||||
for (string const& query: compilerStack.unhandledSMTLib2Queries())
|
||||
output["auxiliaryInputRequested"]["smtlib2queries"]["0x" + keccak256(query).hex()] = query;
|
||||
|
||||
bool const wildcardMatchesIR = false;
|
||||
bool const wildcardMatchesExperimental = false;
|
||||
|
||||
output["sources"] = Json::objectValue;
|
||||
unsigned sourceIndex = 0;
|
||||
@ -820,9 +840,9 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
|
||||
{
|
||||
Json::Value sourceResult = Json::objectValue;
|
||||
sourceResult["id"] = sourceIndex++;
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, "", "ast", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, "", "ast", wildcardMatchesExperimental))
|
||||
sourceResult["ast"] = ASTJsonConverter(false, compilerStack.sourceIndices()).toJson(compilerStack.ast(sourceName));
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, "", "legacyAST", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, "", "legacyAST", wildcardMatchesExperimental))
|
||||
sourceResult["legacyAST"] = ASTJsonConverter(true, compilerStack.sourceIndices()).toJson(compilerStack.ast(sourceName));
|
||||
output["sources"][sourceName] = sourceResult;
|
||||
}
|
||||
@ -837,30 +857,34 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
|
||||
|
||||
// ABI, documentation and metadata
|
||||
Json::Value contractData(Json::objectValue);
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "abi", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "abi", wildcardMatchesExperimental))
|
||||
contractData["abi"] = compilerStack.contractABI(contractName);
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "metadata", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "metadata", wildcardMatchesExperimental))
|
||||
contractData["metadata"] = compilerStack.metadata(contractName);
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "userdoc", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "userdoc", wildcardMatchesExperimental))
|
||||
contractData["userdoc"] = compilerStack.natspecUser(contractName);
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "devdoc", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "devdoc", wildcardMatchesExperimental))
|
||||
contractData["devdoc"] = compilerStack.natspecDev(contractName);
|
||||
|
||||
// IR
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "ir", wildcardMatchesIR))
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "ir", wildcardMatchesExperimental))
|
||||
contractData["ir"] = compilerStack.yulIR(contractName);
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "irOptimized", wildcardMatchesIR))
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "irOptimized", wildcardMatchesExperimental))
|
||||
contractData["irOptimized"] = compilerStack.yulIROptimized(contractName);
|
||||
|
||||
// eWasm
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "ewasm.wast", wildcardMatchesExperimental))
|
||||
contractData["ewasm"]["wast"] = compilerStack.eWasm(contractName);
|
||||
|
||||
// EVM
|
||||
Json::Value evmData(Json::objectValue);
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.assembly", wildcardMatchesIR))
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.assembly", wildcardMatchesExperimental))
|
||||
evmData["assembly"] = compilerStack.assemblyString(contractName, sourceList);
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.legacyAssembly", wildcardMatchesIR))
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.legacyAssembly", wildcardMatchesExperimental))
|
||||
evmData["legacyAssembly"] = compilerStack.assemblyJSON(contractName, sourceList);
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.methodIdentifiers", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.methodIdentifiers", wildcardMatchesExperimental))
|
||||
evmData["methodIdentifiers"] = compilerStack.methodIdentifiers(contractName);
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.gasEstimates", wildcardMatchesIR))
|
||||
if (compilationSuccess && isArtifactRequested(_inputsAndSettings.outputSelection, file, name, "evm.gasEstimates", wildcardMatchesExperimental))
|
||||
evmData["gasEstimates"] = compilerStack.gasEstimates(contractName);
|
||||
|
||||
if (compilationSuccess && isArtifactRequested(
|
||||
@ -868,7 +892,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
|
||||
file,
|
||||
name,
|
||||
{ "evm.bytecode", "evm.bytecode.object", "evm.bytecode.opcodes", "evm.bytecode.sourceMap", "evm.bytecode.linkReferences" },
|
||||
wildcardMatchesIR
|
||||
wildcardMatchesExperimental
|
||||
))
|
||||
evmData["bytecode"] = collectEVMObject(
|
||||
compilerStack.object(contractName),
|
||||
@ -880,7 +904,7 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
|
||||
file,
|
||||
name,
|
||||
{ "evm.deployedBytecode", "evm.deployedBytecode.object", "evm.deployedBytecode.opcodes", "evm.deployedBytecode.sourceMap", "evm.deployedBytecode.linkReferences" },
|
||||
wildcardMatchesIR
|
||||
wildcardMatchesExperimental
|
||||
))
|
||||
evmData["deployedBytecode"] = collectEVMObject(
|
||||
compilerStack.runtimeObject(contractName),
|
||||
@ -954,8 +978,8 @@ Json::Value StandardCompiler::compileYul(InputsAndSettings _inputsAndSettings)
|
||||
|
||||
string contractName = stack.parserResult()->name.str();
|
||||
|
||||
bool const wildcardMatchesIR = true;
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, contractName, "ir", wildcardMatchesIR))
|
||||
bool const wildcardMatchesExperimental = true;
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, contractName, "ir", wildcardMatchesExperimental))
|
||||
output["contracts"][sourceName][contractName]["ir"] = stack.print();
|
||||
|
||||
stack.optimize();
|
||||
@ -967,13 +991,13 @@ Json::Value StandardCompiler::compileYul(InputsAndSettings _inputsAndSettings)
|
||||
sourceName,
|
||||
contractName,
|
||||
{ "evm.bytecode", "evm.bytecode.object", "evm.bytecode.opcodes", "evm.bytecode.sourceMap", "evm.bytecode.linkReferences" },
|
||||
wildcardMatchesIR
|
||||
wildcardMatchesExperimental
|
||||
))
|
||||
output["contracts"][sourceName][contractName]["evm"]["bytecode"] = collectEVMObject(*object.bytecode, nullptr);
|
||||
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, contractName, "irOptimized", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, contractName, "irOptimized", wildcardMatchesExperimental))
|
||||
output["contracts"][sourceName][contractName]["irOptimized"] = stack.print();
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, contractName, "evm.assembly", wildcardMatchesIR))
|
||||
if (isArtifactRequested(_inputsAndSettings.outputSelection, sourceName, contractName, "evm.assembly", wildcardMatchesExperimental))
|
||||
output["contracts"][sourceName][contractName]["evm"]["assembly"] = object.assembly;
|
||||
|
||||
return output;
|
||||
|
@ -42,6 +42,8 @@ add_library(yul
|
||||
backends/evm/EVMMetrics.h
|
||||
backends/evm/NoOutputAssembly.h
|
||||
backends/evm/NoOutputAssembly.cpp
|
||||
backends/wasm/EVMToEWasmTranslator.cpp
|
||||
backends/wasm/EVMToEWasmTranslator.h
|
||||
backends/wasm/EWasmCodeTransform.cpp
|
||||
backends/wasm/EWasmCodeTransform.h
|
||||
backends/wasm/EWasmObjectCompiler.cpp
|
||||
|
376
libyul/backends/wasm/EVMToEWasmTranslator.cpp
Normal file
376
libyul/backends/wasm/EVMToEWasmTranslator.cpp
Normal file
@ -0,0 +1,376 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Translates Yul code from EVM dialect to eWasm dialect.
|
||||
*/
|
||||
|
||||
#include <libyul/backends/wasm/EVMToEWasmTranslator.h>
|
||||
|
||||
#include <libyul/backends/wasm/WordSizeTransform.h>
|
||||
#include <libyul/backends/wasm/WasmDialect.h>
|
||||
#include <libyul/optimiser/ExpressionSplitter.h>
|
||||
#include <libyul/optimiser/FunctionGrouper.h>
|
||||
#include <libyul/optimiser/MainFunction.h>
|
||||
#include <libyul/optimiser/FunctionHoister.h>
|
||||
#include <libyul/optimiser/Disambiguator.h>
|
||||
#include <libyul/optimiser/NameDisplacer.h>
|
||||
|
||||
#include <libyul/AsmParser.h>
|
||||
#include <libyul/AsmAnalysis.h>
|
||||
#include <libyul/AsmAnalysisInfo.h>
|
||||
#include <libyul/Object.h>
|
||||
|
||||
#include <liblangutil/ErrorReporter.h>
|
||||
#include <liblangutil/Scanner.h>
|
||||
#include <liblangutil/SourceReferenceFormatter.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace dev;
|
||||
using namespace yul;
|
||||
using namespace langutil;
|
||||
|
||||
namespace
|
||||
{
|
||||
static string const polyfill{R"({
|
||||
function or_bool(a, b, c, d) -> r {
|
||||
r := i64.ne(0, i64.or(i64.or(a, b), i64.or(c, d)))
|
||||
}
|
||||
// returns a + y + c plus carry value on 64 bit values.
|
||||
// c should be at most 2
|
||||
function add_carry(x, y, c) -> r, r_c {
|
||||
let t := i64.add(x, y)
|
||||
r := i64.add(t, c)
|
||||
r_c := i64.or(
|
||||
i64.lt_u(t, x),
|
||||
i64.lt_u(r, t)
|
||||
)
|
||||
}
|
||||
function add(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
let carry
|
||||
r4, carry := add_carry(x4, y4, 0)
|
||||
r3, carry := add_carry(x3, y3, carry)
|
||||
r2, carry := add_carry(x2, y2, carry)
|
||||
r1, carry := add_carry(x1, y1, carry)
|
||||
}
|
||||
function bit_negate(x) -> y {
|
||||
y := i64.xor(x, 0xffffffffffffffff)
|
||||
}
|
||||
function sub(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
// x - y = x + (~y + 1)
|
||||
let carry
|
||||
r4, carry := add_carry(x4, bit_negate(y4), 1)
|
||||
r3, carry := add_carry(x3, bit_negate(y3), i64.add(carry, 1))
|
||||
r2, carry := add_carry(x2, bit_negate(y2), i64.add(carry, 1))
|
||||
r1, carry := add_carry(x1, bit_negate(y1), i64.add(carry, 1))
|
||||
}
|
||||
function split(x) -> hi, lo {
|
||||
hi := i64.shr_u(x, 32)
|
||||
lo := i64.and(x, 0xffffffff)
|
||||
}
|
||||
// Multiplies two 64 bit values resulting in a 128 bit
|
||||
// value split into two 64 bit values.
|
||||
function mul_64x64_128(x, y) -> hi, lo {
|
||||
let xh, xl := split(x)
|
||||
let yh, yl := split(y)
|
||||
|
||||
let t0 := i64.mul(xl, yl)
|
||||
let t1 := i64.mul(xh, yl)
|
||||
let t2 := i64.mul(xl, yh)
|
||||
let t3 := i64.mul(xh, yh)
|
||||
|
||||
let t0h, t0l := split(t0)
|
||||
let u1 := i64.add(t1, t0h)
|
||||
let u1h, u1l := split(u1)
|
||||
let u2 := i64.add(t2, u1l)
|
||||
|
||||
lo := i64.or(i64.shl(u2, 32), t0l)
|
||||
hi := i64.add(t3, i64.add(i64.shr_u(u2, 32), u1h))
|
||||
}
|
||||
// Add three 64-bit values plus carry (at most 2).
|
||||
// Return the sum and the new carry value.
|
||||
function add3_carry(a, b, c, carr) -> x, carry {
|
||||
let c1, c2
|
||||
x, c1 := add_carry(a, b, carr)
|
||||
x, c2 := add_carry(x, c, 0)
|
||||
carry := i64.add(c1, c2)
|
||||
}
|
||||
// Multiplies two 128 bit values resulting in a 256 bit
|
||||
// value split into four 64 bit values.
|
||||
function mul_128x128_256(x1, x2, y1, y2) -> r1, r2, r3, r4 {
|
||||
let ah, al := mul_64x64_128(x1, y1)
|
||||
let bh, bl := mul_64x64_128(x1, y2)
|
||||
let ch, cl := mul_64x64_128(x2, y1)
|
||||
let dh, dl := mul_64x64_128(x2, y2)
|
||||
let carry
|
||||
r4 := dl
|
||||
r3, carry := add3_carry(bl, cl, dh, 0)
|
||||
r2, carry := add3_carry(al, bh, ch, carry)
|
||||
r1 := i64.add(ah, carry)
|
||||
}
|
||||
function mul(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
}
|
||||
function div(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {}
|
||||
function mod(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {}
|
||||
function smod(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {}
|
||||
function exp(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {}
|
||||
|
||||
function byte(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
if i64.eqz(i64.or(i64.or(x1, x2), x3)) {
|
||||
let component
|
||||
switch i64.div_u(x4, 8)
|
||||
case 0 { component := y1 }
|
||||
case 1 { component := y2 }
|
||||
case 2 { component := y3 }
|
||||
case 3 { component := y4 }
|
||||
x4 := i64.mul(i64.rem_u(x4, 8), 8)
|
||||
r4 := i64.shr_u(component, i64.sub(56, x4))
|
||||
r4 := i64.and(0xff, r4)
|
||||
}
|
||||
}
|
||||
function xor(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
r1 := i64.xor(x1, y1)
|
||||
r2 := i64.xor(x2, y2)
|
||||
r3 := i64.xor(x3, y3)
|
||||
r4 := i64.xor(x4, y4)
|
||||
}
|
||||
function or(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
r1 := i64.or(x1, y1)
|
||||
r2 := i64.or(x2, y2)
|
||||
r3 := i64.or(x3, y3)
|
||||
r4 := i64.or(x4, y4)
|
||||
}
|
||||
function and(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
r1 := i64.and(x1, y1)
|
||||
r2 := i64.and(x2, y2)
|
||||
r3 := i64.and(x3, y3)
|
||||
r4 := i64.and(x4, y4)
|
||||
}
|
||||
function not(x1, x2, x3, x4) -> r1, r2, r3, r4 {
|
||||
let mask := 0xffffffffffffffff
|
||||
r1, r2, r3, r4 := xor(x1, x2, x3, x4, mask, mask, mask, mask)
|
||||
}
|
||||
function iszero(x1, x2, x3, x4) -> r1, r2, r3, r4 {
|
||||
r4 := i64.eqz(i64.or(i64.or(x1, x2), i64.or(x3, x4)))
|
||||
}
|
||||
function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
|
||||
if i64.eq(x1, y1) {
|
||||
if i64.eq(x2, y2) {
|
||||
if i64.eq(x3, y3) {
|
||||
if i64.eq(x4, y4) {
|
||||
r4 := 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// TODO
|
||||
function lt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function gt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function slt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function sgt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
|
||||
function shl(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function shr(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function sar(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function addmod(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function mulmod(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function signextend(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
function keccak256(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 {}
|
||||
|
||||
function address() -> z1, z2, z3, z4 {}
|
||||
function balance(x1, x2, x3, x4) -> z1, z2, z3, z4 {}
|
||||
function origin() -> z1, z2, z3, z4 {}
|
||||
function caller() -> z1, z2, z3, z4 {}
|
||||
function callvalue() -> z1, z2, z3, z4 {}
|
||||
function calldataload(x1, x2, x3, x4) -> z1, z2, z3, z4 {}
|
||||
function calldatasize() -> z1, z2, z3, z4 {}
|
||||
function calldatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {}
|
||||
|
||||
// Needed?
|
||||
function codesize() -> z1, z2, z3, z4 {}
|
||||
function codecopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {}
|
||||
function datacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {}
|
||||
|
||||
function gasprice() -> z1, z2, z3, z4 {}
|
||||
function extcodesize(x1, x2, x3, x4) -> z1, z2, z3, z4 {}
|
||||
function extcodehash(x1, x2, x3, x4) -> z1, z2, z3, z4 {}
|
||||
function extcodecopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {}
|
||||
|
||||
function returndatasize() -> z1, z2, z3, z4 {}
|
||||
function returndatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {}
|
||||
|
||||
function blockhash(x1, x2, x3, x4) -> z1, z2, z3, z4 {}
|
||||
function coinbase() -> z1, z2, z3, z4 {}
|
||||
function timestamp() -> z1, z2, z3, z4 {}
|
||||
function number() -> z1, z2, z3, z4 {}
|
||||
function difficulty() -> z1, z2, z3, z4 {}
|
||||
function gaslimit() -> z1, z2, z3, z4 {}
|
||||
|
||||
function pop(x1, x2, x3, x4) {}
|
||||
|
||||
function mload(x1, x2, x3, x4) -> z1, z2, z3, z4 {}
|
||||
function mstore(x1, x2, x3, x4, y1, y2, y3, y4) {}
|
||||
function mstore8(x1, x2, x3, x4, y1, y2, y3, y4) {}
|
||||
// Needed?
|
||||
function msize() -> z1, z2, z3, z4 {}
|
||||
function sload(x1, x2, x3, x4) -> z1, z2, z3, z4 {}
|
||||
function sstore(x1, x2, x3, x4, y1, y2, y3, y4) {}
|
||||
|
||||
// Needed?
|
||||
function pc() -> z1, z2, z3, z4 {}
|
||||
function gas() -> z1, z2, z3, z4 {}
|
||||
|
||||
function log0(p1, p2, p3, p4, s1, s2, s3, s4) {}
|
||||
function log1(
|
||||
p1, p2, p3, p4, s1, s2, s3, s4,
|
||||
t11, t12, t13, t14
|
||||
) {}
|
||||
function log2(
|
||||
p1, p2, p3, p4, s1, s2, s3, s4,
|
||||
t11, t12, t13, t14,
|
||||
t21, t22, t23, t24
|
||||
) {}
|
||||
function log3(
|
||||
p1, p2, p3, p4, s1, s2, s3, s4,
|
||||
t11, t12, t13, t14,
|
||||
t21, t22, t23, t24,
|
||||
t31, t32, t33, t34
|
||||
) {}
|
||||
function log4(
|
||||
p1, p2, p3, p4, s1, s2, s3, s4,
|
||||
t11, t12, t13, t14,
|
||||
t21, t22, t23, t24,
|
||||
t31, t32, t33, t34,
|
||||
t41, t42, t43, t44,
|
||||
) {}
|
||||
|
||||
function create(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) -> a1, a2, a3, a4 {}
|
||||
function call(
|
||||
a1, a2, a3, a4,
|
||||
b1, b2, b3, b4,
|
||||
c1, c2, c3, c4,
|
||||
d1, d2, d3, d4,
|
||||
e1, e2, e3, e4,
|
||||
f1, f2, f3, f4,
|
||||
g1, g2, g3, g4
|
||||
) -> x1, x2, x3, x4 {}
|
||||
function callcode(
|
||||
a1, a2, a3, a4,
|
||||
b1, b2, b3, b4,
|
||||
c1, c2, c3, c4,
|
||||
d1, d2, d3, d4,
|
||||
e1, e2, e3, e4,
|
||||
f1, f2, f3, f4,
|
||||
g1, g2, g3, g4
|
||||
) -> x1, x2, x3, x4 {}
|
||||
function delegatecall(
|
||||
a1, a2, a3, a4,
|
||||
b1, b2, b3, b4,
|
||||
c1, c2, c3, c4,
|
||||
d1, d2, d3, d4,
|
||||
e1, e2, e3, e4,
|
||||
f1, f2, f3, f4
|
||||
) -> x1, x2, x3, x4 {}
|
||||
function staticcall(
|
||||
a1, a2, a3, a4,
|
||||
b1, b2, b3, b4,
|
||||
c1, c2, c3, c4,
|
||||
d1, d2, d3, d4,
|
||||
e1, e2, e3, e4,
|
||||
f1, f2, f3, f4
|
||||
) -> x1, x2, x3, x4 {}
|
||||
function create2(
|
||||
a1, a2, a3, a4,
|
||||
b1, b2, b3, b4,
|
||||
c1, c2, c3, c4,
|
||||
d1, d2, d3, d4
|
||||
) -> x1, x2, x3, x4 {}
|
||||
function selfdestruct(a1, a2, a3, a4) {}
|
||||
|
||||
function return(x1, x2, x3, x4, y1, y2, y3, y4) {}
|
||||
function revert(x1, x2, x3, x4, y1, y2, y3, y4) {}
|
||||
function invalid() {
|
||||
unreachable()
|
||||
}
|
||||
})"};
|
||||
|
||||
}
|
||||
|
||||
Object EVMToEWasmTranslator::run(Object const& _object)
|
||||
{
|
||||
if (!m_polyfill)
|
||||
parsePolyfill();
|
||||
|
||||
Block ast = boost::get<Block>(Disambiguator(m_dialect, *_object.analysisInfo)(*_object.code));
|
||||
NameDispenser nameDispenser{m_dialect, ast};
|
||||
FunctionHoister{}(ast);
|
||||
FunctionGrouper{}(ast);
|
||||
MainFunction{}(ast);
|
||||
ExpressionSplitter{m_dialect, nameDispenser}(ast);
|
||||
WordSizeTransform::run(m_dialect, ast, nameDispenser);
|
||||
|
||||
NameDisplacer{nameDispenser, m_polyfillFunctions}(ast);
|
||||
for (auto const& st: m_polyfill->statements)
|
||||
ast.statements.emplace_back(ASTCopier{}.translate(st));
|
||||
|
||||
Object ret;
|
||||
ret.code = make_shared<Block>(move(ast));
|
||||
ret.analysisInfo = make_shared<AsmAnalysisInfo>();
|
||||
|
||||
ErrorList errors;
|
||||
ErrorReporter errorReporter(errors);
|
||||
AsmAnalyzer analyzer(*ret.analysisInfo, errorReporter, boost::none, WasmDialect::instance());
|
||||
if (!analyzer.analyze(*ret.code))
|
||||
{
|
||||
// TODO the errors here are "wrong" because they have invalid source references!
|
||||
string message;
|
||||
for (auto const& err: errors)
|
||||
message += langutil::SourceReferenceFormatter::formatErrorInformation(*err);
|
||||
yulAssert(false, message);
|
||||
}
|
||||
|
||||
for (auto const& subObjectNode: _object.subObjects)
|
||||
if (Object const* subObject = dynamic_cast<Object const*>(subObjectNode.get()))
|
||||
ret.subObjects.push_back(make_shared<Object>(run(*subObject)));
|
||||
else
|
||||
ret.subObjects.push_back(make_shared<Data>(dynamic_cast<Data const&>(*subObjectNode)));
|
||||
ret.subIndexByName = _object.subIndexByName;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void EVMToEWasmTranslator::parsePolyfill()
|
||||
{
|
||||
ErrorList errors;
|
||||
ErrorReporter errorReporter(errors);
|
||||
shared_ptr<Scanner> scanner{make_shared<Scanner>(CharStream(polyfill, ""))};
|
||||
m_polyfill = Parser(errorReporter, WasmDialect::instance()).parse(scanner, false);
|
||||
if (!errors.empty())
|
||||
{
|
||||
string message;
|
||||
for (auto const& err: errors)
|
||||
message += langutil::SourceReferenceFormatter::formatErrorInformation(*err);
|
||||
yulAssert(false, message);
|
||||
}
|
||||
|
||||
m_polyfillFunctions.clear();
|
||||
for (auto const& statement: m_polyfill->statements)
|
||||
m_polyfillFunctions.insert(boost::get<FunctionDefinition>(statement).name);
|
||||
}
|
||||
|
46
libyul/backends/wasm/EVMToEWasmTranslator.h
Normal file
46
libyul/backends/wasm/EVMToEWasmTranslator.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
This file is part of solidity.
|
||||
|
||||
solidity is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
solidity is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/**
|
||||
* Translates Yul code from EVM dialect to eWasm dialect.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <libyul/AsmDataForward.h>
|
||||
#include <libyul/optimiser/ASTWalker.h>
|
||||
#include <libyul/Dialect.h>
|
||||
|
||||
namespace yul
|
||||
{
|
||||
struct Object;
|
||||
|
||||
class EVMToEWasmTranslator: public ASTModifier
|
||||
{
|
||||
public:
|
||||
EVMToEWasmTranslator(Dialect const& _evmDialect): m_dialect(_evmDialect) {}
|
||||
Object run(Object const& _object);
|
||||
|
||||
private:
|
||||
void parsePolyfill();
|
||||
|
||||
Dialect const& m_dialect;
|
||||
|
||||
std::shared_ptr<Block> m_polyfill;
|
||||
std::set<YulString> m_polyfillFunctions;
|
||||
};
|
||||
|
||||
}
|
@ -30,6 +30,7 @@ namespace wasm
|
||||
{
|
||||
|
||||
struct Literal;
|
||||
struct StringLiteral;
|
||||
struct LocalVariable;
|
||||
struct GlobalVariable;
|
||||
struct Label;
|
||||
@ -43,12 +44,13 @@ struct Loop;
|
||||
struct Break;
|
||||
struct Continue;
|
||||
using Expression = boost::variant<
|
||||
Literal, LocalVariable, GlobalVariable, Label,
|
||||
Literal, StringLiteral, LocalVariable, GlobalVariable, Label,
|
||||
FunctionCall, BuiltinCall, LocalAssignment, GlobalAssignment,
|
||||
Block, If, Loop, Break, Continue
|
||||
>;
|
||||
|
||||
struct Literal { uint64_t value; };
|
||||
struct StringLiteral { std::string value; };
|
||||
struct LocalVariable { std::string name; };
|
||||
struct GlobalVariable { std::string name; };
|
||||
struct Label { std::string name; };
|
||||
|
@ -48,7 +48,8 @@ string EWasmCodeTransform::run(Dialect const& _dialect, yul::Block const& _ast)
|
||||
statement.type() == typeid(yul::FunctionDefinition),
|
||||
"Expected only function definitions at the highest level."
|
||||
);
|
||||
functions.emplace_back(transform.translateFunction(boost::get<yul::FunctionDefinition>(statement)));
|
||||
if (statement.type() == typeid(yul::FunctionDefinition))
|
||||
functions.emplace_back(transform.translateFunction(boost::get<yul::FunctionDefinition>(statement)));
|
||||
}
|
||||
|
||||
return EWasmToText{}.run(transform.m_globalVariables, functions);
|
||||
@ -65,6 +66,8 @@ wasm::Expression EWasmCodeTransform::generateMultiAssignment(
|
||||
if (_variableNames.size() == 1)
|
||||
return { std::move(assignment) };
|
||||
|
||||
allocateGlobals(_variableNames.size() - 1);
|
||||
|
||||
wasm::Block block;
|
||||
block.statements.emplace_back(move(assignment));
|
||||
for (size_t i = 1; i < _variableNames.size(); ++i)
|
||||
@ -123,8 +126,18 @@ wasm::Expression EWasmCodeTransform::operator()(FunctionalInstruction const& _f)
|
||||
|
||||
wasm::Expression EWasmCodeTransform::operator()(FunctionCall const& _call)
|
||||
{
|
||||
if (m_dialect.builtin(_call.functionName.name))
|
||||
return wasm::BuiltinCall{_call.functionName.name.str(), visit(_call.arguments)};
|
||||
if (BuiltinFunction const* builtin = m_dialect.builtin(_call.functionName.name))
|
||||
{
|
||||
if (builtin->literalArguments)
|
||||
{
|
||||
vector<wasm::Expression> literals;
|
||||
for (auto const& arg: _call.arguments)
|
||||
literals.emplace_back(wasm::StringLiteral{boost::get<Literal>(arg).value.str()});
|
||||
return wasm::BuiltinCall{_call.functionName.name.str(), std::move(literals)};
|
||||
}
|
||||
else
|
||||
return wasm::BuiltinCall{_call.functionName.name.str(), visit(_call.arguments)};
|
||||
}
|
||||
else
|
||||
// If this function returns multiple values, then the first one will
|
||||
// be returned in the expression itself and the others in global variables.
|
||||
@ -141,7 +154,7 @@ wasm::Expression EWasmCodeTransform::operator()(Identifier const& _identifier)
|
||||
wasm::Expression EWasmCodeTransform::operator()(Literal const& _literal)
|
||||
{
|
||||
u256 value = valueOfLiteral(_literal);
|
||||
yulAssert(value <= numeric_limits<uint64_t>::max(), "");
|
||||
yulAssert(value <= numeric_limits<uint64_t>::max(), "Literal too large: " + value.str());
|
||||
return wasm::Literal{uint64_t(value)};
|
||||
}
|
||||
|
||||
|
@ -56,6 +56,13 @@ string EWasmToText::operator()(wasm::Literal const& _literal)
|
||||
return "(i64.const " + to_string(_literal.value) + ")";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::StringLiteral const& _literal)
|
||||
{
|
||||
string quoted = boost::replace_all_copy(_literal.value, "\\", "\\\\");
|
||||
boost::replace_all(quoted, "\"", "\\\"");
|
||||
return "\"" + quoted + "\"";
|
||||
}
|
||||
|
||||
string EWasmToText::operator()(wasm::LocalVariable const& _identifier)
|
||||
{
|
||||
return "(get_local $" + _identifier.name + ")";
|
||||
|
@ -38,6 +38,7 @@ public:
|
||||
|
||||
public:
|
||||
std::string operator()(wasm::Literal const& _literal);
|
||||
std::string operator()(wasm::StringLiteral const& _literal);
|
||||
std::string operator()(wasm::LocalVariable const& _identifier);
|
||||
std::string operator()(wasm::GlobalVariable const& _identifier);
|
||||
std::string operator()(wasm::Label const& _label);
|
||||
|
@ -52,6 +52,9 @@ WasmDialect::WasmDialect():
|
||||
|
||||
addFunction("drop", 1, 0);
|
||||
addFunction("unreachable", 0, 0);
|
||||
|
||||
addFunction("datasize", 1, 4, true);
|
||||
addFunction("dataoffset", 1, 4, true);
|
||||
}
|
||||
|
||||
BuiltinFunction const* WasmDialect::builtin(YulString _name) const
|
||||
@ -72,7 +75,7 @@ WasmDialect const& WasmDialect::instance()
|
||||
return *dialect;
|
||||
}
|
||||
|
||||
void WasmDialect::addFunction(string _name, size_t _params, size_t _returns)
|
||||
void WasmDialect::addFunction(string _name, size_t _params, size_t _returns, bool _literalArguments)
|
||||
{
|
||||
YulString name{move(_name)};
|
||||
BuiltinFunction& f = m_functions[name];
|
||||
@ -85,5 +88,5 @@ void WasmDialect::addFunction(string _name, size_t _params, size_t _returns)
|
||||
f.isMSize = false;
|
||||
f.invalidatesStorage = true;
|
||||
f.invalidatesMemory = true;
|
||||
f.literalArguments = false;
|
||||
f.literalArguments = _literalArguments;
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ struct WasmDialect: public Dialect
|
||||
static WasmDialect const& instance();
|
||||
|
||||
private:
|
||||
void addFunction(std::string _name, size_t _params, size_t _returns);
|
||||
void addFunction(std::string _name, size_t _params, size_t _returns, bool _literalArguments = false);
|
||||
|
||||
std::map<YulString, BuiltinFunction> m_functions;
|
||||
};
|
||||
|
@ -81,6 +81,5 @@ void NameDisplacer::checkAndReplace(YulString& _name) const
|
||||
{
|
||||
if (m_translations.count(_name))
|
||||
_name = m_translations.at(_name);
|
||||
yulAssert(!m_namesToFree.count(_name), "");
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,9 @@ struct Dialect;
|
||||
/**
|
||||
* Optimiser component that renames identifiers to free up certain names.
|
||||
*
|
||||
* Only replaces names that have been defined inside the code. If the code uses
|
||||
* names to be freed but does not define them, they remain unchanged.
|
||||
*
|
||||
* Prerequisites: Disambiguator
|
||||
*/
|
||||
class NameDisplacer: public ASTModifier
|
||||
|
@ -50,6 +50,7 @@
|
||||
#include <libyul/AsmData.h>
|
||||
#include <libyul/AsmPrinter.h>
|
||||
|
||||
#include <libyul/backends/wasm/WasmDialect.h>
|
||||
#include <libyul/backends/evm/NoOutputAssembly.h>
|
||||
|
||||
#include <libdevcore/CommonData.h>
|
||||
@ -215,6 +216,13 @@ void OptimiserSuite::run(
|
||||
yulAssert(_meter, "");
|
||||
ConstantOptimiser{*dialect, *_meter}(ast);
|
||||
}
|
||||
else if (dynamic_cast<WasmDialect const*>(&_dialect))
|
||||
{
|
||||
// If the first statement is an empty block, remove it.
|
||||
// We should only have function definitions after that.
|
||||
if (ast.statements.size() > 1 && boost::get<Block>(ast.statements.front()).statements.empty())
|
||||
ast.statements.erase(ast.statements.begin());
|
||||
}
|
||||
VarNameCleaner{ast, _dialect, reservedIdentifiers}(ast);
|
||||
yul::AsmAnalyzer::analyzeStrictAssertCorrect(_dialect, ast);
|
||||
|
||||
|
@ -123,6 +123,7 @@ static string const g_strInputFile = "input-file";
|
||||
static string const g_strInterface = "interface";
|
||||
static string const g_strYul = "yul";
|
||||
static string const g_strIR = "ir";
|
||||
static string const g_strEWasm = "ewasm";
|
||||
static string const g_strLicense = "license";
|
||||
static string const g_strLibraries = "libraries";
|
||||
static string const g_strLink = "link";
|
||||
@ -170,6 +171,7 @@ static string const g_argHelp = g_strHelp;
|
||||
static string const g_argInputFile = g_strInputFile;
|
||||
static string const g_argYul = g_strYul;
|
||||
static string const g_argIR = g_strIR;
|
||||
static string const g_argEWasm = g_strEWasm;
|
||||
static string const g_argLibraries = g_strLibraries;
|
||||
static string const g_argLink = g_strLink;
|
||||
static string const g_argMachine = g_strMachine;
|
||||
@ -311,6 +313,20 @@ void CommandLineInterface::handleIR(string const& _contractName)
|
||||
}
|
||||
}
|
||||
|
||||
void CommandLineInterface::handleEWasm(string const& _contractName)
|
||||
{
|
||||
if (m_args.count(g_argEWasm))
|
||||
{
|
||||
if (m_args.count(g_argOutputDir))
|
||||
createFile(m_compiler->filesystemFriendlyName(_contractName) + ".wast", m_compiler->eWasm(_contractName));
|
||||
else
|
||||
{
|
||||
sout() << "eWasm: " << endl;
|
||||
sout() << m_compiler->eWasm(_contractName) << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CommandLineInterface::handleBytecode(string const& _contract)
|
||||
{
|
||||
if (m_args.count(g_argOpcodes))
|
||||
@ -705,6 +721,7 @@ Allowed options)",
|
||||
(g_argBinaryRuntime.c_str(), "Binary of the runtime part of the contracts in hex.")
|
||||
(g_argAbi.c_str(), "ABI specification of the contracts.")
|
||||
(g_argIR.c_str(), "Intermediate Representation (IR) of all contracts (EXPERIMENTAL).")
|
||||
(g_argEWasm.c_str(), "EWasm text representation of all contracts (EXPERIMENTAL).")
|
||||
(g_argSignatureHashes.c_str(), "Function signature hashes of the contracts.")
|
||||
(g_argNatspecUser.c_str(), "Natspec user documentation of all contracts.")
|
||||
(g_argNatspecDev.c_str(), "Natspec developer documentation of all contracts.")
|
||||
@ -932,6 +949,7 @@ bool CommandLineInterface::processInput()
|
||||
// TODO: Perhaps we should not compile unless requested
|
||||
|
||||
m_compiler->enableIRGeneration(m_args.count(g_argIR));
|
||||
m_compiler->enableEWasmGeneration(m_args.count(g_argEWasm));
|
||||
|
||||
OptimiserSettings settings = m_args.count(g_argOptimize) ? OptimiserSettings::standard() : OptimiserSettings::minimal();
|
||||
settings.expectedExecutionsPerDeployment = m_args[g_argOptimizeRuns].as<unsigned>();
|
||||
@ -1409,6 +1427,7 @@ void CommandLineInterface::outputCompilationResults()
|
||||
|
||||
handleBytecode(contract);
|
||||
handleIR(contract);
|
||||
handleEWasm(contract);
|
||||
handleSignatureHashes(contract);
|
||||
handleMetadata(contract);
|
||||
handleABI(contract);
|
||||
|
@ -66,6 +66,7 @@ private:
|
||||
void handleBinary(std::string const& _contract);
|
||||
void handleOpcode(std::string const& _contract);
|
||||
void handleIR(std::string const& _contract);
|
||||
void handleEWasm(std::string const& _contract);
|
||||
void handleBytecode(std::string const& _contract);
|
||||
void handleSignatureHashes(std::string const& _contract);
|
||||
void handleMetadata(std::string const& _contract);
|
||||
|
22
test/cmdlineTests/standard_eWasm_requested/input.json
Normal file
22
test/cmdlineTests/standard_eWasm_requested/input.json
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"language": "Solidity",
|
||||
"sources":
|
||||
{
|
||||
"A":
|
||||
{
|
||||
"content": "pragma solidity >=0.0; contract C { }"
|
||||
}
|
||||
},
|
||||
"settings":
|
||||
{
|
||||
"optimizer":
|
||||
{
|
||||
"enabled": true,
|
||||
"details": {"yul": true}
|
||||
},
|
||||
"outputSelection":
|
||||
{
|
||||
"*": { "*": ["ewasm.wast"] }
|
||||
}
|
||||
}
|
||||
}
|
1
test/cmdlineTests/standard_eWasm_requested/output.json
Normal file
1
test/cmdlineTests/standard_eWasm_requested/output.json
Normal file
@ -0,0 +1 @@
|
||||
{"contracts":{"A":{"C":{"ewasm":{"wast":"(module\n (memory $memory (export \"memory\") 1)\n (export \"main\" (func $main))\n\n(func $main\n)\n\n)\n(module\n (memory $memory (export \"memory\") 1)\n (export \"main\" (func $main))\n (global $global_ (mut i64) (i64.const 0))\n (global $global__1 (mut i64) (i64.const 0))\n (global $global__2 (mut i64) (i64.const 0))\n\n(func $main\n (local $_1 i64)\n (local $_2 i64)\n (local $_3 i64)\n (local $_4 i64)\n (local $_5 i64)\n (local $_6 i64)\n (local $_7 i64)\n (local $_8 i64)\n (block\n (set_local $_1 (datasize \"C_2_deployed\"))\n (set_local $_2 (get_global $global_))\n (set_local $_3 (get_global $global__1))\n (set_local $_4 (get_global $global__2))\n \n )\n (block\n (set_local $_5 (dataoffset \"C_2_deployed\"))\n (set_local $_6 (get_global $global_))\n (set_local $_7 (get_global $global__1))\n (set_local $_8 (get_global $global__2))\n \n )\n)\n\n)\n"}}}},"errors":[{"component":"general","formattedMessage":"Warning: The Yul optimiser is still experimental. Do not use it in production unless correctness of generated code is verified with extensive tests.\n","message":"The Yul optimiser is still experimental. Do not use it in production unless correctness of generated code is verified with extensive tests.","severity":"warning","type":"Warning"}],"sources":{"A":{"id":0}}}
|
Loading…
Reference in New Issue
Block a user