mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
tmp
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
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
|
||||
|
||||
#include <libsolidity/codegen/experimental/Common.h>
|
||||
#include <libsolidity/ast/experimental/TypeSystem.h>
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <libyul/AsmPrinter.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::frontend;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::yul;
|
||||
|
||||
namespace solidity::frontend::experimental
|
||||
{
|
||||
|
||||
string IRNames::function(FunctionDefinition const& _function, Type _type)
|
||||
{
|
||||
if (_function.isConstructor())
|
||||
return constructor(*_function.annotation().contract);
|
||||
|
||||
return "fun_" + _function.name() + "_" + to_string(_function.id()) + "$" + canonicalTypeName(_type) + "$";
|
||||
}
|
||||
|
||||
string IRNames::function(VariableDeclaration const& _varDecl)
|
||||
{
|
||||
return "getter_fun_" + _varDecl.name() + "_" + to_string(_varDecl.id());
|
||||
}
|
||||
|
||||
string IRNames::creationObject(ContractDefinition const& _contract)
|
||||
{
|
||||
return _contract.name() + "_" + toString(_contract.id());
|
||||
}
|
||||
|
||||
string IRNames::deployedObject(ContractDefinition const& _contract)
|
||||
{
|
||||
return _contract.name() + "_" + toString(_contract.id()) + "_deployed";
|
||||
}
|
||||
|
||||
string IRNames::constructor(ContractDefinition const& _contract)
|
||||
{
|
||||
return "constructor_" + _contract.name() + "_" + to_string(_contract.id());
|
||||
}
|
||||
|
||||
string IRNames::localVariable(VariableDeclaration const& _declaration)
|
||||
{
|
||||
return "var_" + _declaration.name() + '_' + std::to_string(_declaration.id());
|
||||
}
|
||||
|
||||
string IRNames::localVariable(Expression const& _expression)
|
||||
{
|
||||
return "expr_" + to_string(_expression.id());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
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/AST.h>
|
||||
#include <libsolidity/ast/experimental/TypeSystem.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
namespace solidity::frontend::experimental
|
||||
{
|
||||
|
||||
struct IRNames
|
||||
{
|
||||
static std::string function(FunctionDefinition const& _function, Type _type);
|
||||
static std::string function(VariableDeclaration const& _varDecl);
|
||||
static std::string creationObject(ContractDefinition const& _contract);
|
||||
static std::string deployedObject(ContractDefinition const& _contract);
|
||||
static std::string constructor(ContractDefinition const& _contract);
|
||||
static std::string localVariable(VariableDeclaration const& _declaration);
|
||||
static std::string localVariable(Expression const& _expression);
|
||||
};
|
||||
|
||||
}
|
||||
@@ -20,6 +20,9 @@
|
||||
|
||||
#include <libsolidity/ast/ASTForward.h>
|
||||
|
||||
#include <libsolidity/analysis/experimental/Analysis.h>
|
||||
#include <libsolidity/ast/experimental/TypeSystem.h>
|
||||
|
||||
#include <list>
|
||||
#include <set>
|
||||
|
||||
@@ -31,13 +34,23 @@ class Analysis;
|
||||
struct IRGenerationContext
|
||||
{
|
||||
Analysis const& analysis;
|
||||
std::list<FunctionDefinition const*> functionQueue;
|
||||
std::set<FunctionDefinition const*> generatedFunctions;
|
||||
void enqueueFunctionDefinition(FunctionDefinition const* _functionDefinition)
|
||||
TypeEnvironment const* env = nullptr;
|
||||
void enqueueFunctionDefinition(FunctionDefinition const* _functionDefinition, Type _type)
|
||||
{
|
||||
if (!generatedFunctions.count(_functionDefinition))
|
||||
functionQueue.emplace_back(_functionDefinition);
|
||||
QueuedFunction queue{_functionDefinition, env->resolve(_type)};
|
||||
if (!generatedFunctions.count(queue))
|
||||
functionQueue.emplace_back(queue);
|
||||
}
|
||||
struct QueuedFunction
|
||||
{
|
||||
FunctionDefinition const* function;
|
||||
Type type;
|
||||
bool operator<(QueuedFunction const& _rhs) const {
|
||||
return std::make_tuple(function, type) < std::make_tuple(_rhs.function, _rhs.type);
|
||||
}
|
||||
};
|
||||
std::list<QueuedFunction> functionQueue;
|
||||
std::set<QueuedFunction> generatedFunctions;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -17,11 +17,14 @@
|
||||
// SPDX-License-Identifier: GPL-3.0
|
||||
|
||||
#include <libsolidity/codegen/experimental/IRGenerator.h>
|
||||
|
||||
#include <libsolidity/codegen/experimental/IRGenerationContext.h>
|
||||
#include <libsolidity/codegen/experimental/IRGeneratorForStatements.h>
|
||||
|
||||
#include <libsolidity/codegen/ir/Common.h>
|
||||
#include <libsolidity/codegen/experimental/Common.h>
|
||||
|
||||
#include <libsolidity/analysis/experimental/Analysis.h>
|
||||
#include <libsolidity/analysis/experimental/TypeInference.h>
|
||||
|
||||
|
||||
#include <libyul/YulStack.h>
|
||||
#include <libyul/AsmPrinter.h>
|
||||
@@ -42,6 +45,24 @@ using namespace solidity::frontend::experimental;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::util;
|
||||
|
||||
IRGenerator::IRGenerator(
|
||||
EVMVersion _evmVersion,
|
||||
std::optional<uint8_t> _eofVersion,
|
||||
frontend::RevertStrings, std::map<std::string, unsigned int>,
|
||||
DebugInfoSelection const&,
|
||||
CharStreamProvider const*,
|
||||
Analysis const& _analysis
|
||||
)
|
||||
:
|
||||
m_evmVersion(_evmVersion),
|
||||
m_eofVersion(_eofVersion),
|
||||
// m_debugInfoSelection(_debugInfoSelection),
|
||||
// m_soliditySourceProvider(_soliditySourceProvider),
|
||||
m_env(_analysis.typeSystem().env().clone()),
|
||||
m_context{_analysis, &m_env, {}, {}}
|
||||
{
|
||||
}
|
||||
|
||||
string IRGenerator::run(
|
||||
ContractDefinition const& _contract,
|
||||
bytes const& /*_cborMetadata*/,
|
||||
@@ -75,30 +96,33 @@ string IRGenerator::generate(ContractDefinition const& _contract)
|
||||
code << "{\n";
|
||||
if (_contract.fallbackFunction())
|
||||
{
|
||||
code << IRNames::function(*_contract.fallbackFunction()) << "()\n";
|
||||
m_context.functionQueue.emplace_front(_contract.fallbackFunction());
|
||||
auto type = m_context.analysis.annotation<TypeInference>(*_contract.fallbackFunction()).type;
|
||||
solAssert(type);
|
||||
type = m_context.analysis.typeSystem().env().resolve(*type);
|
||||
code << IRNames::function(*_contract.fallbackFunction(), *type) << "()\n";
|
||||
m_context.enqueueFunctionDefinition(_contract.fallbackFunction(), *type);
|
||||
}
|
||||
code << "revert(0,0)\n";
|
||||
code << "}\n";
|
||||
|
||||
while (!m_context.functionQueue.empty())
|
||||
{
|
||||
FunctionDefinition const* function = m_context.functionQueue.front();
|
||||
auto function = m_context.functionQueue.front();
|
||||
m_context.functionQueue.pop_front();
|
||||
if (!m_context.generatedFunctions.count(function))
|
||||
{
|
||||
m_context.generatedFunctions.insert(function);
|
||||
code << generate(*function);
|
||||
code << generate(*function.function, function.type);
|
||||
}
|
||||
}
|
||||
|
||||
return code.str();
|
||||
}
|
||||
|
||||
string IRGenerator::generate(FunctionDefinition const& _function)
|
||||
string IRGenerator::generate(FunctionDefinition const& _function, Type _type)
|
||||
{
|
||||
std::stringstream code;
|
||||
code << "function " << IRNames::function(_function) << "(";
|
||||
code << "function " << IRNames::function(_function, _type) << "(";
|
||||
if (_function.parameters().size() > 1)
|
||||
for (auto const& arg: _function.parameters() | ranges::views::drop_last(1))
|
||||
code << IRNames::localVariable(*arg) << ", ";
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <libsolidity/interface/OptimiserSettings.h>
|
||||
#include <libsolidity/ast/ASTForward.h>
|
||||
#include <libsolidity/ast/CallGraph.h>
|
||||
#include <libsolidity/ast/experimental/TypeSystem.h>
|
||||
|
||||
#include <liblangutil/CharStreamProvider.h>
|
||||
#include <liblangutil/DebugInfoSelection.h>
|
||||
@@ -35,7 +36,6 @@
|
||||
namespace solidity::frontend::experimental
|
||||
{
|
||||
|
||||
class SourceUnit;
|
||||
class Analysis;
|
||||
|
||||
class IRGenerator
|
||||
@@ -49,13 +49,7 @@ public:
|
||||
langutil::DebugInfoSelection const& /*_debugInfoSelection*/,
|
||||
langutil::CharStreamProvider const* /*_soliditySourceProvider*/,
|
||||
Analysis const& _analysis
|
||||
):
|
||||
m_evmVersion(_evmVersion),
|
||||
m_eofVersion(_eofVersion),
|
||||
// m_debugInfoSelection(_debugInfoSelection),
|
||||
// m_soliditySourceProvider(_soliditySourceProvider),
|
||||
m_context{_analysis, {}, {}}
|
||||
{}
|
||||
);
|
||||
|
||||
std::string run(
|
||||
ContractDefinition const& _contract,
|
||||
@@ -64,13 +58,14 @@ public:
|
||||
);
|
||||
|
||||
std::string generate(ContractDefinition const& _contract);
|
||||
std::string generate(FunctionDefinition const& _function);
|
||||
std::string generate(FunctionDefinition const& _function, Type _type);
|
||||
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;
|
||||
TypeEnvironment m_env;
|
||||
IRGenerationContext m_context;
|
||||
};
|
||||
|
||||
|
||||
@@ -18,12 +18,16 @@
|
||||
|
||||
#include <libsolidity/codegen/experimental/IRGeneratorForStatements.h>
|
||||
|
||||
#include <libsolidity/analysis/experimental/Analysis.h>
|
||||
#include <libsolidity/analysis/experimental/TypeInference.h>
|
||||
#include <libsolidity/analysis/experimental/TypeRegistration.h>
|
||||
|
||||
#include <libyul/YulStack.h>
|
||||
#include <libyul/AsmPrinter.h>
|
||||
#include <libyul/AST.h>
|
||||
#include <libyul/optimiser/ASTCopier.h>
|
||||
|
||||
#include <libsolidity/codegen/ir/Common.h>
|
||||
#include <libsolidity/codegen/experimental/Common.h>
|
||||
|
||||
#include <range/v3/view/drop_last.hpp>
|
||||
|
||||
@@ -46,9 +50,10 @@ namespace {
|
||||
struct CopyTranslate: public yul::ASTCopier
|
||||
{
|
||||
CopyTranslate(
|
||||
IRGenerationContext const& _context,
|
||||
yul::Dialect const& _dialect,
|
||||
map<yul::Identifier const*, InlineAssemblyAnnotation::ExternalIdentifierInfo> _references
|
||||
): m_dialect(_dialect), m_references(std::move(_references)) {}
|
||||
): m_context(_context), m_dialect(_dialect), m_references(std::move(_references)) {}
|
||||
|
||||
using ASTCopier::operator();
|
||||
|
||||
@@ -90,11 +95,15 @@ private:
|
||||
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.
|
||||
auto type = m_context.analysis.annotation<TypeInference>(*varDecl).type;
|
||||
solAssert(type);
|
||||
type = m_context.env->resolve(*type);
|
||||
solAssert(*type == m_context.analysis.typeSystem().builtinType(BuiltinType::Word, {}));
|
||||
string value = IRNames::localVariable(*varDecl);
|
||||
return yul::Identifier{_identifier.debugData, yul::YulString{value}};
|
||||
}
|
||||
|
||||
IRGenerationContext const& m_context;
|
||||
yul::Dialect const& m_dialect;
|
||||
map<yul::Identifier const*, InlineAssemblyAnnotation::ExternalIdentifierInfo> m_references;
|
||||
};
|
||||
@@ -103,7 +112,7 @@ private:
|
||||
|
||||
bool IRGeneratorForStatements::visit(InlineAssembly const& _assembly)
|
||||
{
|
||||
CopyTranslate bodyCopier{_assembly.dialect(), _assembly.annotation().externalReferences};
|
||||
CopyTranslate bodyCopier{m_context, _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)) << "\n";
|
||||
@@ -153,13 +162,45 @@ 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);
|
||||
FunctionDefinition const* functionDefinition = nullptr;
|
||||
if (auto const* identifier = dynamic_cast<Identifier const*>(&_functionCall.expression()))
|
||||
{
|
||||
functionDefinition = dynamic_cast<FunctionDefinition const*>(identifier->annotation().referencedDeclaration);
|
||||
}
|
||||
else if (auto const* memberAccess = dynamic_cast<MemberAccess const*>(&_functionCall.expression()))
|
||||
{
|
||||
auto const& expressionAnnotation = m_context.analysis.annotation<TypeInference>(memberAccess->expression());
|
||||
solAssert(expressionAnnotation.type);
|
||||
|
||||
m_code << "let " << IRNames::localVariable(_functionCall) << " := " << IRNames::function(*functionDefinition) << "(";
|
||||
auto typeConstructor = std::get<0>(TypeSystemHelpers{m_context.analysis.typeSystem()}.destTypeExpression(
|
||||
m_context.analysis.typeSystem().env().resolve(*expressionAnnotation.type)
|
||||
));
|
||||
auto const* typeClass = dynamic_cast<Identifier const*>(&memberAccess->expression());
|
||||
solAssert(typeClass, "Function call to member access only supported for type classes.");
|
||||
auto const* typeClassDefinition = dynamic_cast<TypeClassDefinition const*>(typeClass->annotation().referencedDeclaration);
|
||||
solAssert(typeClassDefinition, "Function call to member access only supported for type classes.");
|
||||
auto const& classAnnotation = m_context.analysis.annotation<TypeRegistration>(*typeClassDefinition);
|
||||
TypeClassInstantiation const* instantiation = classAnnotation.instantiations.at(typeConstructor);
|
||||
for (auto const& node: instantiation->subNodes())
|
||||
{
|
||||
auto const* def = dynamic_cast<FunctionDefinition const*>(node.get());
|
||||
solAssert(def);
|
||||
if (def->name() == memberAccess->memberName())
|
||||
{
|
||||
functionDefinition = def;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
solAssert(false, "Complex function call expressions not supported.");
|
||||
|
||||
solAssert(functionDefinition);
|
||||
auto functionType = m_context.analysis.annotation<TypeInference>(_functionCall).type;
|
||||
solAssert(functionType);
|
||||
functionType = m_context.analysis.typeSystem().env().resolve(*functionType);
|
||||
m_context.enqueueFunctionDefinition(functionDefinition, *functionType);
|
||||
m_code << "let " << IRNames::localVariable(_functionCall) << " := " << IRNames::function(*functionDefinition, *functionType) << "(";
|
||||
auto const& arguments = _functionCall.arguments();
|
||||
if (arguments.size() > 1)
|
||||
for (auto arg: arguments | ranges::views::drop_last(1))
|
||||
|
||||
Reference in New Issue
Block a user