This commit is contained in:
Daniel Kirchner
2023-06-20 04:08:23 +02:00
parent 4357b0316b
commit bd0e0fcdbe
15 changed files with 586 additions and 156 deletions
@@ -0,0 +1,43 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <libsolidity/ast/ASTForward.h>
#include <list>
#include <set>
namespace solidity::frontend::experimental
{
class Analysis;
struct IRGenerationContext
{
Analysis const& analysis;
std::list<FunctionDefinition const*> functionQueue;
std::set<FunctionDefinition const*> generatedFunctions;
void enqueueFunctionDefinition(FunctionDefinition const* _functionDefinition)
{
if (!generatedFunctions.count(_functionDefinition))
functionQueue.emplace_back(_functionDefinition);
}
};
}
@@ -18,6 +18,7 @@
#include <libsolidity/codegen/experimental/IRGenerator.h>
#include <libsolidity/codegen/experimental/IRGenerationContext.h>
#include <libsolidity/codegen/experimental/IRGeneratorForStatements.h>
#include <libsolidity/codegen/ir/Common.h>
@@ -31,6 +32,8 @@
#include <libsolutil/Whiskers.h>
#include <range/v3/view/drop_last.hpp>
#include <variant>
using namespace std;
@@ -43,7 +46,7 @@ string IRGenerator::run(
ContractDefinition const& _contract,
bytes const& /*_cborMetadata*/,
map<ContractDefinition const*, string_view const> const& /*_otherYulSources*/
) const
)
{
Whiskers t(R"(
@@ -66,30 +69,52 @@ string IRGenerator::run(
return t.render();
}
string IRGenerator::generate(ContractDefinition const& _contract) const
string IRGenerator::generate(ContractDefinition const& _contract)
{
std::stringstream code;
code << "{\n";
if (_contract.fallbackFunction())
{
code << IRNames::function(*_contract.fallbackFunction()) << "()\n";
m_context.functionQueue.emplace_front(_contract.fallbackFunction());
}
code << "revert(0,0)\n";
code << "}\n";
for (FunctionDefinition const* f: _contract.definedFunctions())
code << generate(*f);
while (!m_context.functionQueue.empty())
{
FunctionDefinition const* function = m_context.functionQueue.front();
m_context.functionQueue.pop_front();
m_context.generatedFunctions.insert(function);
code << generate(*function);
}
return code.str();
}
string IRGenerator::generate(FunctionDefinition const& _function) const
string IRGenerator::generate(FunctionDefinition const& _function)
{
std::stringstream code;
code << "function " << IRNames::function(_function) << "() {\n";
code << "function " << IRNames::function(_function) << "(";
if (_function.parameters().size() > 1)
for (auto const& arg: _function.parameters() | ranges::view::drop_last(1))
code << IRNames::localVariable(*arg) << ", ";
if (!_function.parameters().empty())
code << IRNames::localVariable(*_function.parameters().back());
code << ")";
if (_function.returnParameterList() && !_function.returnParameters().empty())
{
code << " -> ";
if (_function.returnParameters().size() > 1)
for (auto const& arg: _function.returnParameters() | ranges::view::drop_last(1))
code << IRNames::localVariable(*arg) << ", ";
if (!_function.returnParameters().empty())
code << IRNames::localVariable(*_function.returnParameters().back());
}
code << "{\n";
for (auto _statement: _function.body().statements())
{
IRGeneratorForStatements statementGenerator{m_analysis};
IRGeneratorForStatements statementGenerator{m_context};
code << statementGenerator.generate(*_statement);
}
code << "}\n";
@@ -18,6 +18,7 @@
#pragma once
#include <libsolidity/codegen/experimental/IRGenerationContext.h>
#include <libsolidity/interface/DebugSettings.h>
#include <libsolidity/interface/OptimiserSettings.h>
#include <libsolidity/ast/ASTForward.h>
@@ -53,24 +54,24 @@ public:
m_eofVersion(_eofVersion),
m_debugInfoSelection(_debugInfoSelection),
m_soliditySourceProvider(_soliditySourceProvider),
m_analysis(_analysis)
m_context{_analysis, {}, {}}
{}
std::string run(
ContractDefinition const& _contract,
bytes const& _cborMetadata,
std::map<ContractDefinition const*, std::string_view const> const& _otherYulSources
) const;
);
std::string generate(ContractDefinition const& _contract) const;
std::string generate(FunctionDefinition const& _function) const;
std::string generate(ContractDefinition const& _contract);
std::string generate(FunctionDefinition const& _function);
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;
IRGenerationContext m_context;
};
}
@@ -25,6 +25,8 @@
#include <libsolidity/codegen/ir/Common.h>
#include <range/v3/view/drop_last.hpp>
using namespace std;
using namespace solidity;
using namespace solidity::util;
@@ -104,7 +106,7 @@ 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));
m_code << yul::AsmPrinter()(std::get<yul::Block>(modified)) << "\n";
return false;
}
@@ -119,6 +121,55 @@ bool IRGeneratorForStatements::visit(VariableDeclarationStatement const& _variab
return false;
}
bool IRGeneratorForStatements::visit(ExpressionStatement const&)
{
return true;
}
bool IRGeneratorForStatements::visit(Identifier const& _identifier)
{
auto const* rhsVar = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration);
solAssert(rhsVar, "Can only reference identifiers referring to variables.");
m_code << "let " << IRNames::localVariable(_identifier) << " := " << IRNames::localVariable(*rhsVar) << "\n";
return false;
}
bool IRGeneratorForStatements::visit(FunctionCall const& _functionCall)
{
for(auto arg: _functionCall.arguments())
arg->accept(*this);
auto const* identifier = dynamic_cast<Identifier const*>(&_functionCall.expression());
solAssert(identifier, "Complex function call expressions not supported.");
auto const* functionDefinition = dynamic_cast<FunctionDefinition const*>(identifier->annotation().referencedDeclaration);
solAssert(functionDefinition, "Function call expression must refer to a function definition.");
m_context.enqueueFunctionDefinition(functionDefinition);
m_code << "let " << IRNames::localVariable(_functionCall) << " := " << IRNames::function(*functionDefinition) << "(";
auto const& arguments = _functionCall.arguments();
if (arguments.size() > 1)
for (auto arg: arguments | ranges::view::drop_last(1))
m_code << IRNames::localVariable(*arg) << ", ";
if (!arguments.empty())
m_code << IRNames::localVariable(*arguments.back());
m_code << ")\n";
return false;
}
bool IRGeneratorForStatements::visit(Assignment const& _assignment)
{
_assignment.rightHandSide().accept(*this);
auto const* lhs = dynamic_cast<Identifier const*>(&_assignment.leftHandSide());
solAssert(lhs, "Can only assign to identifiers.");
auto const* lhsVar = dynamic_cast<VariableDeclaration const*>(lhs->annotation().referencedDeclaration);
solAssert(lhsVar, "Can only assign to identifiers referring to variables.");
m_code << IRNames::localVariable(*lhsVar) << " := " << IRNames::localVariable(_assignment.rightHandSide()) << "\n";
m_code << "let " << IRNames::localVariable(_assignment) << " := " << IRNames::localVariable(*lhsVar) << "\n";
return false;
}
bool IRGeneratorForStatements::visitNode(ASTNode const&)
{
solAssert(false, "Unsupported AST node during statement code generation.");
@@ -18,6 +18,7 @@
#pragma once
#include <libsolidity/codegen/experimental/IRGenerationContext.h>
#include <libsolidity/ast/ASTVisitor.h>
#include <functional>
@@ -30,15 +31,19 @@ class Analysis;
class IRGeneratorForStatements: public ASTConstVisitor
{
public:
IRGeneratorForStatements(Analysis const& _analysis): m_analysis(_analysis) {}
IRGeneratorForStatements(IRGenerationContext& _context): m_context(_context) {}
std::string generate(ASTNode const& _node);
private:
bool visit(ExpressionStatement const& _expressionStatement) override;
bool visit(Assignment const& _assignment) override;
bool visit(Identifier const& _identifier) override;
bool visit(FunctionCall const&) override;
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;
IRGenerationContext& m_context;
std::stringstream m_code;
};