mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
tmp
This commit is contained in:
@@ -18,6 +18,8 @@
|
||||
|
||||
#include <libsolidity/codegen/experimental/IRGenerator.h>
|
||||
|
||||
#include <libsolidity/codegen/experimental/IRGeneratorForStatements.h>
|
||||
|
||||
#include <libsolidity/codegen/ir/Common.h>
|
||||
|
||||
#include <libyul/YulStack.h>
|
||||
@@ -87,74 +89,9 @@ string IRGenerator::generate(FunctionDefinition const& _function) const
|
||||
code << "function " << IRNames::function(_function) << "() {\n";
|
||||
for (auto _statement: _function.body().statements())
|
||||
{
|
||||
if (auto assembly = dynamic_cast<InlineAssembly const*>(_statement.get()))
|
||||
code << generate(*assembly) << "\n";
|
||||
else
|
||||
solUnimplemented("Unsupported statement type.");
|
||||
IRGeneratorForStatements statementGenerator{m_analysis};
|
||||
code << statementGenerator.generate(*_statement);
|
||||
}
|
||||
code << "}\n";
|
||||
return code.str();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
struct CopyTranslate: public yul::ASTCopier
|
||||
{
|
||||
CopyTranslate(
|
||||
yul::Dialect const& _dialect,
|
||||
map<yul::Identifier const*, void*> _references
|
||||
): m_dialect(_dialect), m_references(std::move(_references)) {}
|
||||
|
||||
using ASTCopier::operator();
|
||||
|
||||
yul::Expression operator()(yul::Identifier const& _identifier) override
|
||||
{
|
||||
// The operator() function is only called in lvalue context. In rvalue context,
|
||||
// only translate(yul::Identifier) is called.
|
||||
if (m_references.count(&_identifier))
|
||||
return translateReference(_identifier);
|
||||
else
|
||||
return ASTCopier::operator()(_identifier);
|
||||
}
|
||||
|
||||
yul::YulString translateIdentifier(yul::YulString _name) override
|
||||
{
|
||||
if (m_dialect.builtin(_name))
|
||||
return _name;
|
||||
else
|
||||
return yul::YulString{"usr$" + _name.str()};
|
||||
}
|
||||
|
||||
yul::Identifier translate(yul::Identifier const& _identifier) override
|
||||
{
|
||||
if (!m_references.count(&_identifier))
|
||||
return ASTCopier::translate(_identifier);
|
||||
|
||||
yul::Expression translated = translateReference(_identifier);
|
||||
solAssert(holds_alternative<yul::Identifier>(translated));
|
||||
return get<yul::Identifier>(std::move(translated));
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// Translates a reference to a local variable, potentially including
|
||||
/// a suffix. Might return a literal, which causes this to be invalid in
|
||||
/// lvalue-context.
|
||||
yul::Expression translateReference(yul::Identifier const&)
|
||||
{
|
||||
solUnimplemented("External references in inline assembly not implemented.");
|
||||
}
|
||||
|
||||
yul::Dialect const& m_dialect;
|
||||
map<yul::Identifier const*, void*> m_references;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
string IRGenerator::generate(InlineAssembly const& _assembly) const
|
||||
{
|
||||
CopyTranslate bodyCopier{_assembly.dialect(), {}};
|
||||
yul::Statement modified = bodyCopier(_assembly.operations());
|
||||
solAssert(holds_alternative<yul::Block>(modified));
|
||||
return yul::AsmPrinter()(std::get<yul::Block>(modified));
|
||||
}
|
||||
@@ -35,6 +35,7 @@ namespace solidity::frontend::experimental
|
||||
{
|
||||
|
||||
class SourceUnit;
|
||||
class Analysis;
|
||||
|
||||
class IRGenerator
|
||||
{
|
||||
@@ -45,12 +46,14 @@ public:
|
||||
RevertStrings /*_revertStrings*/,
|
||||
std::map<std::string, unsigned> /*_sourceIndices*/,
|
||||
langutil::DebugInfoSelection const& _debugInfoSelection,
|
||||
langutil::CharStreamProvider const* _soliditySourceProvider
|
||||
langutil::CharStreamProvider const* _soliditySourceProvider,
|
||||
Analysis const& _analysis
|
||||
):
|
||||
m_evmVersion(_evmVersion),
|
||||
m_eofVersion(_eofVersion),
|
||||
m_debugInfoSelection(_debugInfoSelection),
|
||||
m_soliditySourceProvider(_soliditySourceProvider)
|
||||
m_soliditySourceProvider(_soliditySourceProvider),
|
||||
m_analysis(_analysis)
|
||||
{}
|
||||
|
||||
std::string run(
|
||||
@@ -61,13 +64,13 @@ public:
|
||||
|
||||
std::string generate(ContractDefinition const& _contract) const;
|
||||
std::string generate(FunctionDefinition const& _function) const;
|
||||
std::string generate(InlineAssembly const& _assembly) const;
|
||||
private:
|
||||
langutil::EVMVersion const m_evmVersion;
|
||||
std::optional<uint8_t> const m_eofVersion;
|
||||
OptimiserSettings const m_optimiserSettings;
|
||||
langutil::DebugInfoSelection m_debugInfoSelection = {};
|
||||
langutil::CharStreamProvider const* m_soliditySourceProvider = nullptr;
|
||||
Analysis const& m_analysis;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -18,9 +18,108 @@
|
||||
|
||||
#include <libsolidity/codegen/experimental/IRGeneratorForStatements.h>
|
||||
|
||||
#include <libyul/YulStack.h>
|
||||
#include <libyul/AsmPrinter.h>
|
||||
#include <libyul/AST.h>
|
||||
#include <libyul/optimiser/ASTCopier.h>
|
||||
|
||||
#include <libsolidity/codegen/ir/Common.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::frontend;
|
||||
using namespace solidity::frontend::experimental;
|
||||
using namespace std::string_literals;
|
||||
|
||||
std::string IRGeneratorForStatements::generate(ASTNode const& _node)
|
||||
{
|
||||
_node.accept(*this);
|
||||
return m_code.str();
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
struct CopyTranslate: public yul::ASTCopier
|
||||
{
|
||||
CopyTranslate(
|
||||
yul::Dialect const& _dialect,
|
||||
map<yul::Identifier const*, InlineAssemblyAnnotation::ExternalIdentifierInfo> _references
|
||||
): m_dialect(_dialect), m_references(std::move(_references)) {}
|
||||
|
||||
using ASTCopier::operator();
|
||||
|
||||
yul::Expression operator()(yul::Identifier const& _identifier) override
|
||||
{
|
||||
// The operator() function is only called in lvalue context. In rvalue context,
|
||||
// only translate(yul::Identifier) is called.
|
||||
if (m_references.count(&_identifier))
|
||||
return translateReference(_identifier);
|
||||
else
|
||||
return ASTCopier::operator()(_identifier);
|
||||
}
|
||||
|
||||
yul::YulString translateIdentifier(yul::YulString _name) override
|
||||
{
|
||||
if (m_dialect.builtin(_name))
|
||||
return _name;
|
||||
else
|
||||
return yul::YulString{"usr$" + _name.str()};
|
||||
}
|
||||
|
||||
yul::Identifier translate(yul::Identifier const& _identifier) override
|
||||
{
|
||||
if (!m_references.count(&_identifier))
|
||||
return ASTCopier::translate(_identifier);
|
||||
|
||||
yul::Expression translated = translateReference(_identifier);
|
||||
solAssert(holds_alternative<yul::Identifier>(translated));
|
||||
return get<yul::Identifier>(std::move(translated));
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
/// Translates a reference to a local variable, potentially including
|
||||
/// a suffix. Might return a literal, which causes this to be invalid in
|
||||
/// lvalue-context.
|
||||
yul::Expression translateReference(yul::Identifier const& _identifier)
|
||||
{
|
||||
auto const& reference = m_references.at(&_identifier);
|
||||
auto const varDecl = dynamic_cast<VariableDeclaration const*>(reference.declaration);
|
||||
solAssert(varDecl, "External reference in inline assembly to something that is not a variable declaration.");
|
||||
// TODO: validate that variable is known and has word type.
|
||||
string value = IRNames::localVariable(*varDecl);
|
||||
return yul::Identifier{_identifier.debugData, yul::YulString{value}};
|
||||
}
|
||||
|
||||
yul::Dialect const& m_dialect;
|
||||
map<yul::Identifier const*, InlineAssemblyAnnotation::ExternalIdentifierInfo> m_references;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
bool IRGeneratorForStatements::visit(InlineAssembly const& _assembly)
|
||||
{
|
||||
CopyTranslate bodyCopier{_assembly.dialect(), _assembly.annotation().externalReferences};
|
||||
yul::Statement modified = bodyCopier(_assembly.operations());
|
||||
solAssert(holds_alternative<yul::Block>(modified));
|
||||
m_code << yul::AsmPrinter()(std::get<yul::Block>(modified));
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IRGeneratorForStatements::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
|
||||
{
|
||||
solAssert(_variableDeclarationStatement.declarations().size() == 1, "multi variable declarations not supported");
|
||||
solAssert(!_variableDeclarationStatement.initialValue(), "initial values not yet supported");
|
||||
VariableDeclaration const* variableDeclaration = _variableDeclarationStatement.declarations().front().get();
|
||||
solAssert(variableDeclaration);
|
||||
// TODO: check the type of the variable; register local variable; initialize
|
||||
m_code << "let " << IRNames::localVariable(*variableDeclaration) << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IRGeneratorForStatements::visitNode(ASTNode const&)
|
||||
{
|
||||
solAssert(false, "Unsupported AST node during statement code generation.");
|
||||
}
|
||||
@@ -21,18 +21,25 @@
|
||||
#include <libsolidity/ast/ASTVisitor.h>
|
||||
|
||||
#include <functional>
|
||||
#include <sstream>
|
||||
|
||||
namespace solidity::frontend::experimental
|
||||
{
|
||||
class Analysis;
|
||||
|
||||
class IRGeneratorForStatements: public ASTConstVisitor
|
||||
{
|
||||
public:
|
||||
IRGeneratorForStatements() {}
|
||||
IRGeneratorForStatements(Analysis const& _analysis): m_analysis(_analysis) {}
|
||||
|
||||
std::string generate(ASTNode const& _node);
|
||||
private:
|
||||
bool visit(InlineAssembly const& _inlineAssembly) override;
|
||||
bool visit(VariableDeclarationStatement const& _variableDeclarationStatement) override;
|
||||
/// Default visit will reject all AST nodes that are not explicitly supported.
|
||||
bool visitNode(ASTNode const& _node) override;
|
||||
Analysis const& m_analysis;
|
||||
std::stringstream m_code;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user