Make EVMAssemblyStack impolement AbstractAssemblyStack

This commit is contained in:
Kamil Śliwak 2023-04-21 21:50:16 +02:00
parent 68d4bfe11f
commit f1528e166a
2 changed files with 121 additions and 1 deletions

View File

@ -59,4 +59,97 @@ void EVMAssemblyStack::assemble()
}
}
LinkerObject const& EVMAssemblyStack::object(string const& _contractName) const
{
solAssert(_contractName == m_name);
return m_object;
}
LinkerObject const& EVMAssemblyStack::runtimeObject(string const& _contractName) const
{
solAssert(_contractName == m_name);
return m_runtimeObject;
}
// TODO: Review implementation here
map<string, unsigned> EVMAssemblyStack::sourceIndices() const
{
solAssert(m_evmAssembly);
map<string, unsigned> indices;
unsigned index = 0;
for (auto const& s: m_evmAssembly->sourceList())
if (s != CompilerContext::yulUtilityFileName())
indices[s] = index++;
if (indices.find(CompilerContext::yulUtilityFileName()) == indices.end())
indices[CompilerContext::yulUtilityFileName()] = index++;
return indices;
}
string const* EVMAssemblyStack::sourceMapping(string const& _contractName) const
{
solAssert(_contractName == m_name);
solAssert(m_evmAssembly);
if (!m_sourceMapping.has_value())
{
// TODO: Should this be already pre-computed in assemble() and only returned here?
AssemblyItems const& items = m_evmAssembly->items();
m_sourceMapping.emplace(AssemblyItem::computeSourceMapping(items, sourceIndices()));
}
return m_sourceMapping.has_value() ? &m_sourceMapping.value() : nullptr;
}
string const* EVMAssemblyStack::runtimeSourceMapping(string const& _contractName) const
{
solAssert(_contractName == m_name);
solAssert(m_evmRuntimeAssembly);
if (!m_sourceMapping.has_value())
{
// TODO: Should this be already pre-computed in assemble() and only returned here?
AssemblyItems const& items = m_evmRuntimeAssembly->items();
m_sourceMapping.emplace(AssemblyItem::computeSourceMapping(items, sourceIndices()));
}
return m_sourceMapping.has_value() ? &m_sourceMapping.value() : nullptr;
}
Json::Value EVMAssemblyStack::assemblyJSON(string const& _contractName) const
{
solAssert(_contractName == m_name);
solAssert(m_evmAssembly);
vector<string> sources = sourceNames();
if (find(sources.begin(), sources.end(), CompilerContext::yulUtilityFileName()) == sources.end())
sources.emplace_back(CompilerContext::yulUtilityFileName());
m_evmAssembly->setSourceList(sources);
return m_evmAssembly->assemblyJSON();
}
string EVMAssemblyStack::assemblyString(string const& _contractName, StringMap const& _sourceCodes) const
{
solAssert(_contractName == m_name);
solAssert(m_evmAssembly);
return m_evmAssembly->assemblyString(m_debugInfoSelection, _sourceCodes);
}
string const EVMAssemblyStack::filesystemFriendlyName(string const& _contractName) const
{
solAssert(_contractName == m_name);
// We have only one contract so there are no conflicts possible and no need to sanitize the name
return m_name;
}
vector<string> EVMAssemblyStack::sourceNames() const
{
solAssert(m_evmAssembly);
return m_evmAssembly->sourceList();
}
} // namespace solidity::evmasm

View File

@ -19,17 +19,22 @@
#pragma once
#include <libevmasm/Assembly.h>
#include <libevmasm/AbstractAssemblyStack.h>
#include <libevmasm/LinkerObject.h>
#include <liblangutil/DebugInfoSelection.h>
#include <libsolutil/JSON.h>
#include <map>
#include <optional>
#include <string>
#include <vector>
namespace solidity::evmasm
{
class EVMAssemblyStack
class EVMAssemblyStack: public AbstractAssemblyStack
{
public:
explicit EVMAssemblyStack(langutil::EVMVersion _evmVersion): m_evmVersion(_evmVersion) {}
@ -44,19 +49,41 @@ public:
evmasm::LinkerObject const& object() const { return m_object; }
evmasm::LinkerObject const& runtimeObject() const { return m_runtimeObject; }
virtual LinkerObject const& object(std::string const& _contractName) const override;
virtual LinkerObject const& runtimeObject(std::string const& _contractName) const override;
std::shared_ptr<evmasm::Assembly> const& evmAssembly() const { return m_evmAssembly; }
std::shared_ptr<evmasm::Assembly> const& evmRuntimeAssembly() const { return m_evmRuntimeAssembly; }
virtual std::string const* sourceMapping(std::string const& _contractName) const override;
virtual std::string const* runtimeSourceMapping(std::string const& _contractName) const override;
virtual Json::Value assemblyJSON(std::string const& _contractName) const override;
virtual std::string assemblyString(std::string const& _contractName, StringMap const& _sourceCodes) const override;
virtual std::string const filesystemFriendlyName(std::string const& _contractName) const override;
virtual std::vector<std::string> contractNames() const override { return {m_name}; }
virtual std::vector<std::string> sourceNames() const override;
std::map<std::string, unsigned> sourceIndices() const;
// TODO: This should depend on whether assemble() was successful
virtual bool compilationSuccessful() const override { return true; }
void selectDebugInfo(langutil::DebugInfoSelection _debugInfoSelection) { m_debugInfoSelection = _debugInfoSelection; }
private:
langutil::EVMVersion m_evmVersion;
langutil::DebugInfoSelection m_debugInfoSelection = langutil::DebugInfoSelection::Default();
std::string m_name;
Json::Value m_json;
std::shared_ptr<evmasm::Assembly> m_evmAssembly;
std::shared_ptr<evmasm::Assembly> m_evmRuntimeAssembly;
evmasm::LinkerObject m_object; ///< Deployment object (includes the runtime sub-object).
evmasm::LinkerObject m_runtimeObject; ///< Runtime object.
mutable std::optional<std::string const> m_sourceMapping;
mutable std::optional<std::string const> m_runtimeSourceMapping;
};
} // namespace solidity::evmasm