Codegen for object access.

This commit is contained in:
chriseth
2018-12-11 19:24:44 +01:00
parent 8d49e53995
commit fb3a0ac1c7
33 changed files with 560 additions and 173 deletions
+8 -8
View File
@@ -101,7 +101,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal)
}
else if (_literal.kind == LiteralKind::Boolean)
{
solAssert(m_dialect.flavour == AsmFlavour::Yul, "");
solAssert(m_dialect->flavour == AsmFlavour::Yul, "");
solAssert(_literal.value == YulString{string("true")} || _literal.value == YulString{string("false")}, "");
}
m_info.stackHeightInfo[&_literal] = m_stackHeight;
@@ -164,7 +164,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
{
solAssert(m_dialect.flavour != AsmFlavour::Yul, "");
solAssert(m_dialect->flavour != AsmFlavour::Yul, "");
bool success = true;
for (auto const& arg: _instr.arguments | boost::adaptors::reversed)
if (!expectExpression(arg))
@@ -182,9 +182,9 @@ bool AsmAnalyzer::operator()(ExpressionStatement const& _statement)
{
int initialStackHeight = m_stackHeight;
bool success = boost::apply_visitor(*this, _statement.expression);
if (m_stackHeight != initialStackHeight && (m_dialect.flavour != AsmFlavour::Loose || m_errorTypeForLoose))
if (m_stackHeight != initialStackHeight && (m_dialect->flavour != AsmFlavour::Loose || m_errorTypeForLoose))
{
Error::Type errorType = m_dialect.flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError;
Error::Type errorType = m_dialect->flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError;
string msg =
"Top-level expressions are not supposed to return values (this expression returns " +
to_string(m_stackHeight - initialStackHeight) +
@@ -299,7 +299,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
bool success = true;
size_t parameters = 0;
size_t returns = 0;
if (BuiltinFunction const* f = m_dialect.builtins->query(_funCall.functionName.name))
if (BuiltinFunction const* f = m_dialect->builtin(_funCall.functionName.name))
{
// TODO: compare types, too
parameters = f->parameters.size();
@@ -569,7 +569,7 @@ Scope& AsmAnalyzer::scope(Block const* _block)
}
void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
{
if (m_dialect.flavour != AsmFlavour::Yul)
if (m_dialect->flavour != AsmFlavour::Yul)
return;
if (!builtinTypes.count(type))
@@ -629,7 +629,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio
if (_instr == solidity::Instruction::JUMP || _instr == solidity::Instruction::JUMPI || _instr == solidity::Instruction::JUMPDEST)
{
if (m_dialect.flavour != AsmFlavour::Loose)
if (m_dialect->flavour != AsmFlavour::Loose)
solAssert(m_errorTypeForLoose && *m_errorTypeForLoose != Error::Type::Warning, "");
m_errorReporter.error(
@@ -644,7 +644,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio
void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description)
{
if (m_dialect.flavour != AsmFlavour::Loose)
if (m_dialect->flavour != AsmFlavour::Loose)
solAssert(false, _description);
else if (m_errorTypeForLoose)
m_errorReporter.error(*m_errorTypeForLoose, _location, _description);
+2 -2
View File
@@ -59,7 +59,7 @@ public:
langutil::ErrorReporter& _errorReporter,
dev::solidity::EVMVersion _evmVersion,
boost::optional<langutil::Error::Type> _errorTypeForLoose,
Dialect _dialect = Dialect::looseAssemblyForEVM(),
std::shared_ptr<Dialect> _dialect,
ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver()
):
m_resolver(_resolver),
@@ -115,7 +115,7 @@ private:
AsmAnalysisInfo& m_info;
langutil::ErrorReporter& m_errorReporter;
dev::solidity::EVMVersion m_evmVersion;
Dialect m_dialect = Dialect::looseAssemblyForEVM();
std::shared_ptr<Dialect> m_dialect;
boost::optional<langutil::Error::Type> m_errorTypeForLoose;
};
+17 -17
View File
@@ -107,14 +107,14 @@ Statement Parser::parseStatement()
return parseForLoop();
case Token::Assign:
{
if (m_dialect.flavour != AsmFlavour::Loose)
if (m_dialect->flavour != AsmFlavour::Loose)
break;
StackAssignment assignment = createWithLocation<StackAssignment>();
advance();
expectToken(Token::Colon);
assignment.variableName.location = location();
assignment.variableName.name = YulString(currentLiteral());
if (m_dialect.builtins->query(assignment.variableName.name))
if (m_dialect->builtin(assignment.variableName.name))
fatalParserError("Identifier expected, got builtin symbol.");
else if (instructions().count(assignment.variableName.name.str()))
fatalParserError("Identifier expected, got instruction name.");
@@ -176,9 +176,9 @@ Statement Parser::parseStatement()
if (currentToken() == Token::Assign && peekNextToken() != Token::Colon)
{
Assignment assignment = createWithLocation<Assignment>(identifier.location);
if (m_dialect.builtins->query(identifier.name))
if (m_dialect->builtin(identifier.name))
fatalParserError("Cannot assign to builtin function \"" + identifier.name.str() + "\".");
else if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(identifier.name.str()))
else if (m_dialect->flavour != AsmFlavour::Yul && instructions().count(identifier.name.str()))
fatalParserError("Cannot use instruction names for identifier names.");
advance();
assignment.variableNames.emplace_back(identifier);
@@ -189,7 +189,7 @@ Statement Parser::parseStatement()
else
{
// label
if (m_dialect.flavour != AsmFlavour::Loose)
if (m_dialect->flavour != AsmFlavour::Loose)
fatalParserError("Labels are not supported.");
Label label = createWithLocation<Label>(identifier.location);
label.name = identifier.name;
@@ -197,7 +197,7 @@ Statement Parser::parseStatement()
}
}
default:
if (m_dialect.flavour != AsmFlavour::Loose)
if (m_dialect->flavour != AsmFlavour::Loose)
fatalParserError("Call or assignment expected.");
break;
}
@@ -273,7 +273,7 @@ Expression Parser::parseExpression()
instructionNames().at(instr.instruction) +
"\" not allowed in this context."
);
if (m_dialect.flavour != AsmFlavour::Loose && currentToken() != Token::LParen)
if (m_dialect->flavour != AsmFlavour::Loose && currentToken() != Token::LParen)
fatalParserError(
"Non-functional instructions are not allowed in this context."
);
@@ -293,7 +293,7 @@ Expression Parser::parseExpression()
else if (operation.type() == typeid(Instruction))
{
// Instructions not taking arguments are allowed as expressions.
solAssert(m_dialect.flavour == AsmFlavour::Loose, "");
solAssert(m_dialect->flavour == AsmFlavour::Loose, "");
Instruction& instr = boost::get<Instruction>(operation);
return FunctionalInstruction{std::move(instr.location), instr.instruction, {}};
}
@@ -362,9 +362,9 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
else
literal = YulString{currentLiteral()};
// first search the set of builtins, then the instructions.
if (m_dialect.builtins->query(literal))
if (m_dialect->builtin(literal))
ret = Identifier{location(), literal};
else if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(literal.str()))
else if (m_dialect->flavour != AsmFlavour::Yul && instructions().count(literal.str()))
{
dev::solidity::Instruction const& instr = instructions().at(literal.str());
ret = Instruction{location(), instr};
@@ -405,7 +405,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
{}
};
advance();
if (m_dialect.flavour == AsmFlavour::Yul)
if (m_dialect->flavour == AsmFlavour::Yul)
{
expectToken(Token::Colon);
literal.location.end = endPosition();
@@ -418,7 +418,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
}
default:
fatalParserError(
m_dialect.flavour == AsmFlavour::Yul ?
m_dialect->flavour == AsmFlavour::Yul ?
"Literal or identifier expected." :
"Literal, identifier or instruction expected."
);
@@ -488,7 +488,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
RecursionGuard recursionGuard(*this);
if (_initialOp.type() == typeid(Instruction))
{
solAssert(m_dialect.flavour != AsmFlavour::Yul, "Instructions are invalid in Yul");
solAssert(m_dialect->flavour != AsmFlavour::Yul, "Instructions are invalid in Yul");
Instruction& instruction = boost::get<Instruction>(_initialOp);
FunctionalInstruction ret;
ret.instruction = instruction.instruction;
@@ -559,7 +559,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
}
else
fatalParserError(
m_dialect.flavour == AsmFlavour::Yul ?
m_dialect->flavour == AsmFlavour::Yul ?
"Function name expected." :
"Assembly instruction or function name required in front of \"(\")"
);
@@ -572,7 +572,7 @@ TypedName Parser::parseTypedName()
RecursionGuard recursionGuard(*this);
TypedName typedName = createWithLocation<TypedName>();
typedName.name = expectAsmIdentifier();
if (m_dialect.flavour == AsmFlavour::Yul)
if (m_dialect->flavour == AsmFlavour::Yul)
{
expectToken(Token::Colon);
typedName.location.end = endPosition();
@@ -584,7 +584,7 @@ TypedName Parser::parseTypedName()
YulString Parser::expectAsmIdentifier()
{
YulString name = YulString{currentLiteral()};
if (m_dialect.flavour == AsmFlavour::Yul)
if (m_dialect->flavour == AsmFlavour::Yul)
{
switch (currentToken())
{
@@ -598,7 +598,7 @@ YulString Parser::expectAsmIdentifier()
break;
}
}
else if (m_dialect.builtins->query(name))
else if (m_dialect->builtin(name))
fatalParserError("Cannot use builtin function name \"" + name.str() + "\" as identifier name.");
else if (instructions().count(name.str()))
fatalParserError("Cannot use instruction names for identifier names.");
+2 -2
View File
@@ -38,7 +38,7 @@ namespace yul
class Parser: public langutil::ParserBase
{
public:
explicit Parser(langutil::ErrorReporter& _errorReporter, Dialect _dialect = Dialect::looseAssemblyForEVM()):
explicit Parser(langutil::ErrorReporter& _errorReporter, std::shared_ptr<Dialect> _dialect):
ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
/// Parses an inline assembly block starting with `{` and ending with `}`.
@@ -86,7 +86,7 @@ protected:
static bool isValidNumberLiteral(std::string const& _literal);
private:
Dialect m_dialect = Dialect::looseAssemblyForEVM();
std::shared_ptr<Dialect> m_dialect;
};
}
+1
View File
@@ -10,6 +10,7 @@ add_library(yul
ObjectParser.cpp
backends/evm/EVMAssembly.cpp
backends/evm/EVMCodeTransform.cpp
backends/evm/EVMDialect.cpp
backends/evm/EVMObjectCompiler.cpp
optimiser/ASTCopier.cpp
optimiser/ASTWalker.cpp
+3 -50
View File
@@ -20,57 +20,10 @@
#include <libyul/Dialect.h>
#include <libyul/Object.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <map>
using namespace yul;
using namespace std;
namespace
{
void addFunction(
map<YulString, BuiltinFunction>& _repository,
string const& _name,
size_t _params,
size_t _returns,
bool _movable
)
{
_repository[YulString{_name}] = BuiltinFunction{
YulString{_name},
vector<Type>(_params),
vector<Type>(_returns),
_movable
};
}
class GenericBuiltins: public Builtins
{
public:
GenericBuiltins(map<YulString, BuiltinFunction> const& _functions): m_functions(_functions) {}
BuiltinFunction const* query(YulString _name) const
{
auto it = m_functions.find(_name);
if (it != end(m_functions))
return &it->second;
else
return nullptr;
}
private:
map<YulString, BuiltinFunction> const& m_functions;
};
}
Dialect Dialect::strictAssemblyForEVMObjects()
{
static map<YulString, BuiltinFunction> functions;
if (functions.empty())
{
addFunction(functions, "datasize", 1, 1, true);
addFunction(functions, "dataoffset", 1, 1, true);
addFunction(functions, "datacopy", 3, 0, false);
}
// The EVM instructions will be moved to builtins at some point.
return Dialect{AsmFlavour::Strict, std::make_shared<GenericBuiltins>(functions)};
}
+10 -27
View File
@@ -22,7 +22,9 @@
#include <libyul/YulString.h>
#include <memory>
#include <boost/noncopyable.hpp>
#include <vector>
namespace yul
{
@@ -45,38 +47,19 @@ struct BuiltinFunction
bool movable;
};
/**
* Class to query for builtin functions and their semantics.
*/
struct Builtins
struct Dialect: boost::noncopyable
{
virtual ~Builtins() = default;
AsmFlavour const flavour = AsmFlavour::Loose;
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
virtual BuiltinFunction const* query(YulString /*_name*/) const { return nullptr; }
};
virtual BuiltinFunction const* builtin(YulString /*_name*/) const { return nullptr; }
struct Dialect
{
AsmFlavour flavour = AsmFlavour::Loose;
std::shared_ptr<Builtins> builtins;
Dialect(AsmFlavour _flavour): flavour(_flavour) {}
virtual ~Dialect() {}
Dialect(AsmFlavour _flavour, std::shared_ptr<Builtins> _builtins):
flavour(_flavour), builtins(std::move(_builtins))
{}
static Dialect looseAssemblyForEVM()
{
return Dialect{AsmFlavour::Loose, std::make_shared<Builtins>()};
}
static Dialect strictAssemblyForEVM()
{
// The EVM instructions will be moved to builtins at some point.
return Dialect{AsmFlavour::Strict, std::make_shared<Builtins>()};
}
static Dialect strictAssemblyForEVMObjects();
static Dialect yul()
static std::shared_ptr<Dialect> yul()
{
// Will have to add builtins later.
return Dialect{AsmFlavour::Yul, std::make_shared<Builtins>()};
return std::make_shared<Dialect>(AsmFlavour::Yul);
}
};
+2 -2
View File
@@ -47,7 +47,7 @@ class ObjectParser: public langutil::ParserBase
public:
explicit ObjectParser(
langutil::ErrorReporter& _errorReporter,
Dialect _dialect = Dialect::looseAssemblyForEVM()
std::shared_ptr<Dialect> _dialect
):
ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
@@ -67,7 +67,7 @@ private:
YulString parseUniqueName(Object const* _containingObject);
void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject);
Dialect m_dialect;
std::shared_ptr<Dialect> m_dialect;
};
}
+37 -26
View File
@@ -97,7 +97,7 @@ CodeTransform::CodeTransform(
AsmAnalysisInfo& _analysisInfo,
Block const& _block,
bool _allowStackOpt,
bool _yul,
EVMDialect const& _dialect,
bool _evm15,
ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions,
@@ -106,8 +106,8 @@ CodeTransform::CodeTransform(
):
m_assembly(_assembly),
m_info(_analysisInfo),
m_dialect(_dialect),
m_allowStackOpt(_allowStackOpt),
m_yul(_yul),
m_evm15(_evm15),
m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions),
m_identifierAccess(_identifierAccess),
@@ -267,35 +267,46 @@ void CodeTransform::operator()(FunctionCall const& _call)
{
solAssert(m_scope, "");
m_assembly.setSourceLocation(_call.location);
EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
if (!m_evm15)
if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name))
{
returnLabel = m_assembly.newLabelId();
m_assembly.appendLabelReference(returnLabel);
m_stackAdjustment++;
builtin->generateCode(_call, m_assembly, [&]() {
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
visitExpression(arg);
m_assembly.setSourceLocation(_call.location);
});
}
Scope::Function* function = nullptr;
solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor(
[=](Scope::Variable&) { solAssert(false, "Expected function name."); },
[=](Scope::Label&) { solAssert(false, "Expected function name."); },
[&](Scope::Function& _function) { function = &_function; }
)), "Function name not found.");
solAssert(function, "");
solAssert(function->arguments.size() == _call.arguments.size(), "");
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
visitExpression(arg);
m_assembly.setSourceLocation(_call.location);
if (m_evm15)
m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size());
else
{
m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1);
m_assembly.appendLabel(returnLabel);
m_stackAdjustment--;
m_assembly.setSourceLocation(_call.location);
EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
if (!m_evm15)
{
returnLabel = m_assembly.newLabelId();
m_assembly.appendLabelReference(returnLabel);
m_stackAdjustment++;
}
Scope::Function* function = nullptr;
solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor(
[=](Scope::Variable&) { solAssert(false, "Expected function name."); },
[=](Scope::Label&) { solAssert(false, "Expected function name."); },
[&](Scope::Function& _function) { function = &_function; }
)), "Function name not found.");
solAssert(function, "");
solAssert(function->arguments.size() == _call.arguments.size(), "");
for (auto const& arg: _call.arguments | boost::adaptors::reversed)
visitExpression(arg);
m_assembly.setSourceLocation(_call.location);
if (m_evm15)
m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size());
else
{
m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1);
m_assembly.appendLabel(returnLabel);
m_stackAdjustment--;
}
checkStackHeight(&_call);
}
checkStackHeight(&_call);
}
void CodeTransform::operator()(FunctionalInstruction const& _instruction)
+6 -6
View File
@@ -20,10 +20,10 @@
#include <libyul/backends/evm/EVMAssembly.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/AsmDataForward.h>
#include <libyul/AsmScope.h>
#include <libyul/Dialect.h>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
@@ -87,8 +87,8 @@ public:
AbstractAssembly& _assembly,
AsmAnalysisInfo& _analysisInfo,
Block const& _block,
EVMDialect const& _dialect,
bool _allowStackOpt = false,
Dialect const& _dialect,
bool _evm15 = false,
ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(),
bool _useNamedLabelsForFunctions = false
@@ -115,7 +115,7 @@ protected:
AsmAnalysisInfo& _analysisInfo,
Block const& _block,
bool _allowStackOpt,
Dialect const& _dialect,
EVMDialect const& _dialect,
bool _evm15,
ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions,
@@ -179,10 +179,10 @@ private:
AbstractAssembly& m_assembly;
AsmAnalysisInfo& m_info;
Scope* m_scope = nullptr;
EVMDialect const& m_dialect;
bool const m_allowStackOpt = true;
Dialect const& m_dialect;
bool m_evm15 = false;
bool m_useNamedLabelsForFunctions = false;
bool const m_evm15 = false;
bool const m_useNamedLabelsForFunctions = false;
ExternalIdentifierAccess m_identifierAccess;
/// Adjustment between the stack height as determined during the analysis phase
/// and the stack height in the assembly. This is caused by an initial stack being present
+141
View File
@@ -0,0 +1,141 @@
/*
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/>.
*/
/**
* Yul dialects for EVM.
*/
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h>
#include <libyul/Object.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <liblangutil/Exceptions.h>
#include <libyul/Exceptions.h>
#include <boost/range/adaptor/reversed.hpp>
using namespace std;
using namespace dev;
using namespace yul;
using namespace dev::solidity;
EVMDialect::EVMDialect(AsmFlavour _flavour, bool _objectAccess):
Dialect(_flavour), m_objectAccess(_objectAccess)
{
// The EVM instructions will be moved to builtins at some point.
if (!m_objectAccess)
return;
addFunction("datasize", 1, 1, true, [this](
FunctionCall const& _call,
AbstractAssembly& _assembly,
std::function<void()>
) {
yulAssert(m_currentObject, "No object available.");
yulAssert(_call.arguments.size() == 1, "");
Expression const& arg = _call.arguments.front();
YulString dataName = boost::get<Literal>(arg).value;
if (m_currentObject->name == dataName)
_assembly.appendAssemblySize();
else
_assembly.appendDataSize(m_subIDs.at(dataName));
});
addFunction("dataoffset", 1, 1, true, [this](
FunctionCall const& _call,
AbstractAssembly& _assembly,
std::function<void()>
) {
yulAssert(m_currentObject, "No object available.");
yulAssert(_call.arguments.size() == 1, "");
Expression const& arg = _call.arguments.front();
YulString dataName = boost::get<Literal>(arg).value;
if (m_currentObject->name == dataName)
_assembly.appendConstant(0);
else
_assembly.appendDataOffset(m_subIDs.at(dataName));
});
addFunction("datacopy", 3, 0, false, [](
FunctionCall const&,
AbstractAssembly& _assembly,
std::function<void()> _visitArguments
) {
_visitArguments();
_assembly.appendInstruction(solidity::Instruction::CODECOPY);
});
}
BuiltinFunctionForEVM const* EVMDialect::builtin(YulString _name) const
{
auto it = m_functions.find(_name);
if (it != m_functions.end())
return &it->second;
else
return nullptr;
}
shared_ptr<EVMDialect> EVMDialect::looseAssemblyForEVM()
{
return make_shared<EVMDialect>(AsmFlavour::Loose, false);
}
shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVM()
{
return make_shared<EVMDialect>(AsmFlavour::Strict, false);
}
shared_ptr<EVMDialect> EVMDialect::strictAssemblyForEVMObjects()
{
return make_shared<EVMDialect>(AsmFlavour::Strict, true);
}
shared_ptr<yul::EVMDialect> EVMDialect::yulForEVM()
{
return make_shared<EVMDialect>(AsmFlavour::Yul, false);
}
void EVMDialect::setSubIDs(map<YulString, AbstractAssembly::SubID> _subIDs)
{
yulAssert(m_objectAccess, "Sub IDs set with dialect that does not support object access.");
m_subIDs = std::move(_subIDs);
}
void EVMDialect::setCurrentObject(Object const* _object)
{
yulAssert(m_objectAccess, "Current object set with dialect that does not support object access.");
m_currentObject = _object;
}
void EVMDialect::addFunction(
string _name,
size_t _params,
size_t _returns,
bool _movable,
std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
)
{
YulString name{std::move(_name)};
BuiltinFunctionForEVM& f = m_functions[name];
f.name = name;
f.parameters.resize(_params);
f.returns.resize(_returns);
f.movable = _movable;
f.generateCode = std::move(_generateCode);
}
+85
View File
@@ -0,0 +1,85 @@
/*
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/>.
*/
/**
* Yul dialects for EVM.
*/
#pragma once
#include <libyul/Dialect.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <map>
namespace yul
{
class YulString;
using Type = YulString;
struct FunctionCall;
struct Object;
struct BuiltinFunctionForEVM: BuiltinFunction
{
/// Function to generate code for the given function call and append it to the abstract
/// assembly. The third parameter is called to visit (and generate code for) the arguments
/// from right to left.
std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> generateCode;
};
/**
* Yul dialect for EVM as a backend.
* The main difference is that the builtin functions take an AbstractAssembly for the
* code generation.
*/
struct EVMDialect: public Dialect
{
EVMDialect(AsmFlavour _flavour, bool _objectAccess);
/// @returns the builtin function of the given name or a nullptr if it is not a builtin function.
BuiltinFunctionForEVM const* builtin(YulString _name) const override;
static std::shared_ptr<EVMDialect> looseAssemblyForEVM();
static std::shared_ptr<EVMDialect> strictAssemblyForEVM();
static std::shared_ptr<EVMDialect> strictAssemblyForEVMObjects();
static std::shared_ptr<EVMDialect> yulForEVM();
bool providesObjectAccess() const { return m_objectAccess; }
/// Sets the mapping of current sub assembly IDs. Used during code generation.
void setSubIDs(std::map<YulString, AbstractAssembly::SubID> _subIDs);
/// Sets the current object. Used during code generation.
void setCurrentObject(Object const* _object);
private:
void addFunction(
std::string _name,
size_t _params,
size_t _returns,
bool _movable,
std::function<void(FunctionCall const&, AbstractAssembly&, std::function<void()>)> _generateCode
);
bool m_objectAccess;
Object const* m_currentObject = nullptr;
/// Mapping from named objects to abstract assembly sub IDs.
std::map<YulString, AbstractAssembly::SubID> m_subIDs;
std::map<YulString, BuiltinFunctionForEVM> m_functions;
};
}
+10 -2
View File
@@ -21,13 +21,15 @@
#include <libyul/backends/evm/EVMObjectCompiler.h>
#include <libyul/backends/evm/EVMCodeTransform.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/Object.h>
#include <libyul/Exceptions.h>
using namespace yul;
using namespace std;
void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, Dialect const& _dialect, bool _evm15, bool _optimize)
void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize)
{
EVMObjectCompiler compiler(_assembly, _dialect, _evm15);
compiler.run(_object, _optimize);
@@ -50,7 +52,13 @@ void EVMObjectCompiler::run(Object& _object, bool _optimize)
subIDs[data.name] = m_assembly.appendData(data.data);
}
if (m_dialect.providesObjectAccess())
{
m_dialect.setSubIDs(std::move(subIDs));
m_dialect.setCurrentObject(&_object);
}
yulAssert(_object.analysisInfo, "No analysis info.");
yulAssert(_object.code, "No code.");
CodeTransform{m_assembly, *_object.analysisInfo, *_object.code, _optimize, m_yul, m_evm15, _optimize}(*_object.code);
CodeTransform{m_assembly, *_object.analysisInfo, *_object.code, m_dialect, _optimize, m_evm15}(*_object.code);
}
+4 -4
View File
@@ -23,21 +23,21 @@ namespace yul
{
struct Object;
class AbstractAssembly;
struct Dialect;
struct EVMDialect;
class EVMObjectCompiler
{
public:
static void compile(Object& _object, AbstractAssembly& _assembly, Dialect const& _dialect, bool _evm15, bool _optimize);
static void compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize);
private:
EVMObjectCompiler(AbstractAssembly& _assembly, Dialect const& _dialect, bool _evm15):
EVMObjectCompiler(AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15):
m_assembly(_assembly), m_dialect(_dialect), m_evm15(_evm15)
{}
void run(Object& _object, bool _optimize);
AbstractAssembly& m_assembly;
Dialect const& m_dialect;
EVMDialect& m_dialect;
bool m_evm15 = false;
};