[libevmasm] Add support to import evm assembly json.

This commit is contained in:
Alexander Arlt
2023-05-16 19:57:31 -05:00
parent aa9e25021d
commit fd137e2929
59 changed files with 1110 additions and 61 deletions
+51 -6
View File
@@ -69,6 +69,7 @@
#include <liblangutil/Scanner.h>
#include <liblangutil/SemVerHandler.h>
#include <libevmasm/EVMAssemblyStack.h>
#include <libevmasm/Exceptions.h>
#include <libsolutil/SwarmHash.h>
@@ -425,6 +426,27 @@ void CompilerStack::importASTs(map<string, Json::Value> const& _sources)
storeContractDefinitions();
}
void CompilerStack::importFromEVMAssemblyStack(std::string const& _sourceName, std::string const& _source)
{
solRequire(m_stackState == Empty, CompilerError, "");
m_evmAssemblyStack = std::make_unique<evmasm::EVMAssemblyStack>(m_evmVersion);
Json::Value evmAsmJson;
if (m_evmAssemblyStack->parseAndAnalyze(_sourceName, _source))
{
m_evmAssemblyStack->assemble();
string const name{m_evmAssemblyStack->name()};
Json::Value const& json = m_evmAssemblyStack->json();
m_sourceJsons[name] = json;
Contract& contract = m_contracts[name];
contract.evmAssembly = m_evmAssemblyStack->evmAssembly();
contract.evmRuntimeAssembly= m_evmAssemblyStack->evmRuntimeAssembly();
contract.object = m_evmAssemblyStack->object();
contract.runtimeObject = m_evmAssemblyStack->runtimeObject();
m_stackState = CompilationSuccessful;
}
}
bool CompilerStack::analyze()
{
if (m_stackState != ParsedAndImported || m_stackState >= AnalysisPerformed)
@@ -661,6 +683,7 @@ bool CompilerStack::isRequestedContract(ContractDefinition const& _contract) con
bool CompilerStack::compile(State _stopAfter)
{
solAssert(m_compilationSourceType != CompilationSourceType::EvmAssemblyJSON);
m_stopAfter = _stopAfter;
if (m_stackState < AnalysisPerformed)
if (!parseAndAnalyze(_stopAfter))
@@ -703,7 +726,7 @@ bool CompilerStack::compile(State _stopAfter)
{
if (
SourceLocation const* sourceLocation =
boost::get_error_info<langutil::errinfo_sourceLocation>(_unimplementedError)
boost::get_error_info<langutil::errinfo_sourceLocation>(_unimplementedError)
)
{
string const* comment = _unimplementedError.comment();
@@ -928,7 +951,13 @@ Json::Value CompilerStack::assemblyJSON(string const& _contractName) const
Contract const& currentContract = contract(_contractName);
if (currentContract.evmAssembly)
return currentContract.evmAssembly->assemblyJSON(sourceIndices());
{
std::vector<std::string> sources = sourceNames();
if (std::find(sources.begin(), sources.end(), CompilerContext::yulUtilityFileName()) == sources.end())
sources.emplace_back(CompilerContext::yulUtilityFileName());
currentContract.evmAssembly->setSourceList(sources);
return currentContract.evmAssembly->assemblyJSON();
}
else
return Json::Value();
}
@@ -942,10 +971,21 @@ map<string, unsigned> CompilerStack::sourceIndices() const
{
map<string, unsigned> indices;
unsigned index = 0;
for (auto const& s: m_sources)
indices[s.first] = index++;
solAssert(!indices.count(CompilerContext::yulUtilityFileName()), "");
indices[CompilerContext::yulUtilityFileName()] = index++;
if (m_evmAssemblyStack)
{
for (auto const& s: m_evmAssemblyStack->evmAssembly()->sourceList())
if (s != CompilerContext::yulUtilityFileName())
indices[s] = index++;
}
else
{
for (auto const& s: m_sources)
if (s.first != CompilerContext::yulUtilityFileName())
indices[s.first] = index++;
}
if (indices.find(CompilerContext::yulUtilityFileName()) == indices.end())
indices[CompilerContext::yulUtilityFileName()] = index++;
return indices;
}
@@ -1538,6 +1578,11 @@ string CompilerStack::createMetadata(Contract const& _contract, bool _forIR) con
case CompilationSourceType::SolidityAST:
sourceType = "SolidityAST";
break;
case CompilationSourceType::EvmAssemblyJSON:
sourceType = "EvmAssemblyJson";
break;
default:
solAssert(false);
}
meta["language"] = sourceType;
meta["compiler"]["version"] = VersionStringStrict;
+7 -1
View File
@@ -42,6 +42,7 @@
#include <liblangutil/SourceLocation.h>
#include <libevmasm/LinkerObject.h>
#include <libevmasm/EVMAssemblyStack.h>
#include <libsolutil/Common.h>
#include <libsolutil/FixedHash.h>
@@ -121,7 +122,9 @@ public:
/// Regular compilation from Solidity source files.
Solidity,
/// Compilation from an imported Solidity AST.
SolidityAST
SolidityAST,
/// Compilation from an imported EVM Assembly JSON.
EvmAssemblyJSON
};
/// Creates a new compiler stack.
@@ -230,6 +233,8 @@ public:
/// Will throw errors if the import fails
void importASTs(std::map<std::string, Json::Value> const& _sources);
void importFromEVMAssemblyStack(std::string const& _sourceName, std::string const& _source);
/// Performs the analysis steps (imports, scopesetting, syntaxCheck, referenceResolving,
/// typechecking, staticAnalysis) on previously parsed sources.
/// @returns false on error.
@@ -516,6 +521,7 @@ private:
/// If this is true, the stack will refuse to generate code.
bool m_hasError = false;
MetadataFormat m_metadataFormat = defaultMetadataFormat();
std::unique_ptr<evmasm::EVMAssemblyStack> m_evmAssemblyStack;
};
}