Skip to content

Commit 4473b3c

Browse files
committed
[libsolidity] Basic output generation of assembly json import.
1 parent f11044d commit 4473b3c

File tree

2 files changed

+61
-46
lines changed

2 files changed

+61
-46
lines changed

libsolidity/interface/CompilerStack.cpp

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ void CompilerStack::importEvmAssemblyJson(map<string, Json::Value> const& _sourc
394394
if (m_stackState != Empty)
395395
solThrow(CompilerError, "Must call importEvmAssemblyJson only before the SourcesSet state.");
396396

397-
m_evmAssemblyJson = std::make_unique<Json::Value>(_sources.begin()->second);
397+
m_evmAssemblyJson[_sources.begin()->first] = _sources.begin()->second;
398398
m_importedSources = true;
399399
m_stackState = SourcesSet;
400400
}
@@ -654,58 +654,73 @@ bool CompilerStack::compile(State _stopAfter)
654654
if (m_hasError)
655655
solThrow(CompilerError, "Called compile with errors.");
656656

657-
// Only compile contracts individually which have been requested.
658-
map<ContractDefinition const*, shared_ptr<Compiler const>> otherCompilers;
657+
if (!m_evmAssemblyJson.empty())
658+
{
659+
solAssert(m_importedSources, "");
660+
solAssert(m_evmAssemblyJson.size() == 1, "");
659661

660-
for (Source const* source: m_sourceOrder)
661-
for (ASTPointer<ASTNode> const& node: source->ast->nodes())
662-
if (auto contract = dynamic_cast<ContractDefinition const*>(node.get()))
663-
if (isRequestedContract(*contract))
664-
{
665-
try
662+
string const evmAssemblyJsonSource = m_evmAssemblyJson.begin()->first;
663+
664+
m_contracts[evmAssemblyJsonSource].evmAssembly = make_shared<evmasm::Assembly>(evmAssemblyJsonSource);
665+
m_contracts[evmAssemblyJsonSource].evmAssembly->loadFromAssemblyJSON(m_evmAssemblyJson[evmAssemblyJsonSource]);
666+
m_contracts[evmAssemblyJsonSource].object = m_contracts[evmAssemblyJsonSource].evmAssembly->assemble();
667+
668+
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly = make_shared<evmasm::Assembly>(evmAssemblyJsonSource);
669+
m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->loadFromAssemblyJSON(m_evmAssemblyJson[evmAssemblyJsonSource][".data"]["0"]);
670+
m_contracts[evmAssemblyJsonSource].runtimeObject = m_contracts[evmAssemblyJsonSource].evmRuntimeAssembly->assemble();
671+
}
672+
else
673+
{
674+
// Only compile contracts individually which have been requested.
675+
map<ContractDefinition const*, shared_ptr<Compiler const>> otherCompilers;
676+
677+
for (Source const* source: m_sourceOrder)
678+
for (ASTPointer<ASTNode> const& node: source->ast->nodes())
679+
if (auto contract = dynamic_cast<ContractDefinition const*>(node.get()))
680+
if (isRequestedContract(*contract))
666681
{
667-
if (m_viaIR || m_generateIR || m_generateEwasm)
668-
generateIR(*contract);
669-
if (m_generateEvmBytecode)
682+
try
670683
{
671-
if (m_viaIR)
672-
generateEVMFromIR(*contract);
673-
else
674-
compileContract(*contract, otherCompilers);
684+
if (m_viaIR || m_generateIR || m_generateEwasm)
685+
generateIR(*contract);
686+
if (m_generateEvmBytecode)
687+
{
688+
if (m_viaIR)
689+
generateEVMFromIR(*contract);
690+
else
691+
compileContract(*contract, otherCompilers);
692+
}
693+
if (m_generateEwasm)
694+
generateEwasm(*contract);
675695
}
676-
if (m_generateEwasm)
677-
generateEwasm(*contract);
678-
}
679-
catch (Error const& _error)
680-
{
681-
if (_error.type() != Error::Type::CodeGenerationError)
682-
throw;
683-
m_errorReporter.error(_error.errorId(), _error.type(), SourceLocation(), _error.what());
684-
return false;
685-
}
686-
catch (UnimplementedFeatureError const& _unimplementedError)
687-
{
688-
if (
689-
SourceLocation const* sourceLocation =
690-
boost::get_error_info<langutil::errinfo_sourceLocation>(_unimplementedError)
691-
)
696+
catch (Error const& _error)
692697
{
693-
string const* comment = _unimplementedError.comment();
694-
m_errorReporter.error(
695-
1834_error,
696-
Error::Type::CodeGenerationError,
697-
*sourceLocation,
698-
"Unimplemented feature error" +
699-
((comment && !comment->empty()) ? ": " + *comment : string{}) +
700-
" in " +
701-
_unimplementedError.lineInfo()
702-
);
698+
if (_error.type() != Error::Type::CodeGenerationError)
699+
throw;
700+
m_errorReporter.error(_error.errorId(), _error.type(), SourceLocation(), _error.what());
703701
return false;
704702
}
705-
else
706-
throw;
703+
catch (UnimplementedFeatureError const& _unimplementedError)
704+
{
705+
if (SourceLocation const* sourceLocation
706+
= boost::get_error_info<langutil::errinfo_sourceLocation>(_unimplementedError))
707+
{
708+
string const* comment = _unimplementedError.comment();
709+
m_errorReporter.error(
710+
1834_error,
711+
Error::Type::CodeGenerationError,
712+
*sourceLocation,
713+
"Unimplemented feature error"
714+
+ ((comment && !comment->empty()) ? ": " + *comment : string{}) + " in "
715+
+ _unimplementedError.lineInfo());
716+
return false;
717+
}
718+
else
719+
throw;
720+
}
707721
}
708-
}
722+
}
723+
709724
m_stackState = CompilationSuccessful;
710725
this->link();
711726
return true;

libsolidity/interface/CompilerStack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ class CompilerStack: public langutil::CharStreamProvider
501501
std::map<std::string const, Source> m_sources;
502502
// if imported, store AST-JSONS for each filename
503503
std::map<std::string, Json::Value> m_sourceJsons;
504-
std::unique_ptr<Json::Value> m_evmAssemblyJson;
504+
std::map<std::string, Json::Value> m_evmAssemblyJson;
505505
std::vector<std::string> m_unhandledSMTLib2Queries;
506506
std::map<util::h256, std::string> m_smtlib2Responses;
507507
std::shared_ptr<GlobalContext> m_globalContext;

0 commit comments

Comments
 (0)