Codegen for object access.

This commit is contained in:
chriseth 2018-12-07 00:56:16 +01:00
parent 8d49e53995
commit fb3a0ac1c7
33 changed files with 560 additions and 173 deletions

View File

@ -9,6 +9,7 @@ Compiler Features:
* Code Generator: Use binary search for dispatch function if more efficient. The size/speed tradeoff can be tuned using ``--optimize-runs``. * Code Generator: Use binary search for dispatch function if more efficient. The size/speed tradeoff can be tuned using ``--optimize-runs``.
* SMTChecker: Support mathematical and cryptographic functions in an uninterpreted way. * SMTChecker: Support mathematical and cryptographic functions in an uninterpreted way.
* Type Checker: Add an additional reason to be displayed when type conversion fails. * Type Checker: Add an additional reason to be displayed when type conversion fails.
* Yul: Support object access via ``datasize``, ``dataoffset`` and ``datacopy`` in standalone assembly mode.
Bugfixes: Bugfixes:

View File

@ -27,6 +27,7 @@
#include <libyul/AsmAnalysis.h> #include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <liblangutil/Exceptions.h> #include <liblangutil/Exceptions.h>
@ -321,7 +322,7 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
errorsIgnored, errorsIgnored,
EVMVersion(), EVMVersion(),
errorTypeForLoose, errorTypeForLoose,
yul::Dialect::looseAssemblyForEVM(), yul::EVMDialect::looseAssemblyForEVM(),
resolver resolver
).analyze(_inlineAssembly.operations()); ).analyze(_inlineAssembly.operations());
return false; return false;

View File

@ -27,6 +27,8 @@
#include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmAnalysisInfo.h>
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <libdevcore/Algorithms.h> #include <libdevcore/Algorithms.h>
@ -658,7 +660,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
m_errorReporter, m_errorReporter,
m_evmVersion, m_evmVersion,
Error::Type::SyntaxError, Error::Type::SyntaxError,
yul::Dialect::looseAssemblyForEVM(), yul::EVMDialect::looseAssemblyForEVM(),
identifierAccess identifierAccess
); );
if (!analyzer.analyze(_inlineAssembly.operations())) if (!analyzer.analyze(_inlineAssembly.operations()))

View File

@ -188,8 +188,8 @@ void CodeGenerator::assemble(
assemblyAdapter, assemblyAdapter,
_analysisInfo, _analysisInfo,
_parsedData, _parsedData,
*EVMDialect::strictAssemblyForEVM(),
_optimize, _optimize,
Dialect::strictAssemblyForEVM(),
false, false,
_identifierAccess, _identifierAccess,
_useNamedLabelsForFunctions _useNamedLabelsForFunctions

View File

@ -31,6 +31,7 @@
#include <libyul/AsmAnalysis.h> #include <libyul/AsmAnalysis.h>
#include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmAnalysisInfo.h>
#include <libyul/YulString.h> #include <libyul/YulString.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h> #include <liblangutil/Scanner.h>
@ -361,7 +362,7 @@ void CompilerContext::appendInlineAssembly(
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
auto scanner = make_shared<langutil::Scanner>(langutil::CharStream(_assembly, "--CODEGEN--")); auto scanner = make_shared<langutil::Scanner>(langutil::CharStream(_assembly, "--CODEGEN--"));
auto parserResult = yul::Parser(errorReporter, yul::Dialect::strictAssemblyForEVM()).parse(scanner, false); auto parserResult = yul::Parser(errorReporter, yul::EVMDialect::strictAssemblyForEVM()).parse(scanner, false);
#ifdef SOL_OUTPUT_ASM #ifdef SOL_OUTPUT_ASM
cout << yul::AsmPrinter()(*parserResult) << endl; cout << yul::AsmPrinter()(*parserResult) << endl;
#endif #endif
@ -373,7 +374,7 @@ void CompilerContext::appendInlineAssembly(
errorReporter, errorReporter,
m_evmVersion, m_evmVersion,
boost::none, boost::none,
yul::Dialect::strictAssemblyForEVM(), yul::EVMDialect::strictAssemblyForEVM(),
identifierAccess.resolve identifierAccess.resolve
).analyze(*parserResult); ).analyze(*parserResult);
if (!parserResult || !errorReporter.errors().empty() || !analyzerResult) if (!parserResult || !errorReporter.errors().empty() || !analyzerResult)

View File

@ -32,6 +32,7 @@
#include <libyul/backends/evm/EVMObjectCompiler.h> #include <libyul/backends/evm/EVMObjectCompiler.h>
#include <libyul/backends/evm/EVMCodeTransform.h> #include <libyul/backends/evm/EVMCodeTransform.h>
#include <libyul/backends/evm/EVMAssembly.h> #include <libyul/backends/evm/EVMAssembly.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/ObjectParser.h> #include <libyul/ObjectParser.h>
#include <libevmasm/Assembly.h> #include <libevmasm/Assembly.h>
@ -45,14 +46,14 @@ using namespace dev::solidity;
namespace namespace
{ {
yul::Dialect languageToDialect(AssemblyStack::Language _language) shared_ptr<yul::Dialect> languageToDialect(AssemblyStack::Language _language)
{ {
switch (_language) switch (_language)
{ {
case AssemblyStack::Language::Assembly: case AssemblyStack::Language::Assembly:
return yul::Dialect::looseAssemblyForEVM(); return yul::EVMDialect::looseAssemblyForEVM();
case AssemblyStack::Language::StrictAssembly: case AssemblyStack::Language::StrictAssembly:
return yul::Dialect::strictAssemblyForEVMObjects(); return yul::EVMDialect::strictAssemblyForEVMObjects();
case AssemblyStack::Language::Yul: case AssemblyStack::Language::Yul:
return yul::Dialect::yul(); return yul::Dialect::yul();
} }
@ -112,6 +113,22 @@ bool AssemblyStack::analyzeParsed(yul::Object& _object)
return success; return success;
} }
void AssemblyStack::compileEVM(yul::AbstractAssembly& _assembly, bool _evm15, bool _optimize) const
{
shared_ptr<yul::EVMDialect> dialect;
if (m_language == Language::Assembly)
dialect = yul::EVMDialect::looseAssemblyForEVM();
else if (m_language == AssemblyStack::Language::StrictAssembly)
dialect = yul::EVMDialect::strictAssemblyForEVMObjects();
else if (m_language == AssemblyStack::Language::Yul)
dialect = yul::EVMDialect::yulForEVM();
else
solAssert(false, "Invalid language.");
yul::EVMObjectCompiler::compile(*m_parserResult, _assembly, *dialect, _evm15, _optimize);
}
void AssemblyStack::optimize(yul::Object& _object) void AssemblyStack::optimize(yul::Object& _object)
{ {
solAssert(_object.code, ""); solAssert(_object.code, "");
@ -136,7 +153,7 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine, bool _optimize)
MachineAssemblyObject object; MachineAssemblyObject object;
eth::Assembly assembly; eth::Assembly assembly;
EthAssemblyAdapter adapter(assembly); EthAssemblyAdapter adapter(assembly);
yul::EVMObjectCompiler::compile(*m_parserResult, adapter, languageToDialect(m_language), false, _optimize); compileEVM(adapter, false, _optimize);
object.bytecode = make_shared<eth::LinkerObject>(assembly.assemble()); object.bytecode = make_shared<eth::LinkerObject>(assembly.assemble());
object.assembly = assembly.assemblyString(); object.assembly = assembly.assemblyString();
return object; return object;
@ -145,7 +162,7 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine, bool _optimize)
{ {
MachineAssemblyObject object; MachineAssemblyObject object;
yul::EVMAssembly assembly(true); yul::EVMAssembly assembly(true);
yul::EVMObjectCompiler::compile(*m_parserResult, assembly, languageToDialect(m_language), true, _optimize); compileEVM(assembly, true, _optimize);
object.bytecode = make_shared<eth::LinkerObject>(assembly.finalize()); object.bytecode = make_shared<eth::LinkerObject>(assembly.finalize());
/// TODO: fill out text representation /// TODO: fill out text representation
return object; return object;

View File

@ -36,6 +36,10 @@ namespace langutil
{ {
class Scanner; class Scanner;
} }
namespace yul
{
class AbstractAssembly;
}
namespace dev namespace dev
{ {
@ -86,6 +90,8 @@ private:
bool analyzeParsed(); bool analyzeParsed();
bool analyzeParsed(yul::Object& _object); bool analyzeParsed(yul::Object& _object);
void compileEVM(yul::AbstractAssembly& _assembly, bool _evm15, bool _optimize) const;
void optimize(yul::Object& _object); void optimize(yul::Object& _object);
Language m_language = Language::Assembly; Language m_language = Language::Assembly;

View File

@ -24,6 +24,7 @@
#include <vector> #include <vector>
#include <libsolidity/parsing/Parser.h> #include <libsolidity/parsing/Parser.h>
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/SourceLocation.h> #include <liblangutil/SourceLocation.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <liblangutil/Scanner.h> #include <liblangutil/Scanner.h>
@ -1012,7 +1013,7 @@ ASTPointer<InlineAssembly> Parser::parseInlineAssembly(ASTPointer<ASTString> con
m_scanner->next(); m_scanner->next();
} }
yul::Parser asmParser(m_errorReporter); yul::Parser asmParser(m_errorReporter, yul::EVMDialect::looseAssemblyForEVM());
shared_ptr<yul::Block> block = asmParser.parse(m_scanner, true); shared_ptr<yul::Block> block = asmParser.parse(m_scanner, true);
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();
return nodeFactory.createNode<InlineAssembly>(_docString, block); return nodeFactory.createNode<InlineAssembly>(_docString, block);

View File

@ -101,7 +101,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal)
} }
else if (_literal.kind == LiteralKind::Boolean) 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")}, ""); solAssert(_literal.value == YulString{string("true")} || _literal.value == YulString{string("false")}, "");
} }
m_info.stackHeightInfo[&_literal] = m_stackHeight; m_info.stackHeightInfo[&_literal] = m_stackHeight;
@ -164,7 +164,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr) bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
{ {
solAssert(m_dialect.flavour != AsmFlavour::Yul, ""); solAssert(m_dialect->flavour != AsmFlavour::Yul, "");
bool success = true; bool success = true;
for (auto const& arg: _instr.arguments | boost::adaptors::reversed) for (auto const& arg: _instr.arguments | boost::adaptors::reversed)
if (!expectExpression(arg)) if (!expectExpression(arg))
@ -182,9 +182,9 @@ bool AsmAnalyzer::operator()(ExpressionStatement const& _statement)
{ {
int initialStackHeight = m_stackHeight; int initialStackHeight = m_stackHeight;
bool success = boost::apply_visitor(*this, _statement.expression); 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 = string msg =
"Top-level expressions are not supposed to return values (this expression returns " + "Top-level expressions are not supposed to return values (this expression returns " +
to_string(m_stackHeight - initialStackHeight) + to_string(m_stackHeight - initialStackHeight) +
@ -299,7 +299,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
bool success = true; bool success = true;
size_t parameters = 0; size_t parameters = 0;
size_t returns = 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 // TODO: compare types, too
parameters = f->parameters.size(); parameters = f->parameters.size();
@ -569,7 +569,7 @@ Scope& AsmAnalyzer::scope(Block const* _block)
} }
void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location) void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
{ {
if (m_dialect.flavour != AsmFlavour::Yul) if (m_dialect->flavour != AsmFlavour::Yul)
return; return;
if (!builtinTypes.count(type)) 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 (_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, ""); solAssert(m_errorTypeForLoose && *m_errorTypeForLoose != Error::Type::Warning, "");
m_errorReporter.error( m_errorReporter.error(
@ -644,7 +644,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio
void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description) void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description)
{ {
if (m_dialect.flavour != AsmFlavour::Loose) if (m_dialect->flavour != AsmFlavour::Loose)
solAssert(false, _description); solAssert(false, _description);
else if (m_errorTypeForLoose) else if (m_errorTypeForLoose)
m_errorReporter.error(*m_errorTypeForLoose, _location, _description); m_errorReporter.error(*m_errorTypeForLoose, _location, _description);

View File

@ -59,7 +59,7 @@ public:
langutil::ErrorReporter& _errorReporter, langutil::ErrorReporter& _errorReporter,
dev::solidity::EVMVersion _evmVersion, dev::solidity::EVMVersion _evmVersion,
boost::optional<langutil::Error::Type> _errorTypeForLoose, boost::optional<langutil::Error::Type> _errorTypeForLoose,
Dialect _dialect = Dialect::looseAssemblyForEVM(), std::shared_ptr<Dialect> _dialect,
ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver() ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver()
): ):
m_resolver(_resolver), m_resolver(_resolver),
@ -115,7 +115,7 @@ private:
AsmAnalysisInfo& m_info; AsmAnalysisInfo& m_info;
langutil::ErrorReporter& m_errorReporter; langutil::ErrorReporter& m_errorReporter;
dev::solidity::EVMVersion m_evmVersion; dev::solidity::EVMVersion m_evmVersion;
Dialect m_dialect = Dialect::looseAssemblyForEVM(); std::shared_ptr<Dialect> m_dialect;
boost::optional<langutil::Error::Type> m_errorTypeForLoose; boost::optional<langutil::Error::Type> m_errorTypeForLoose;
}; };

View File

@ -107,14 +107,14 @@ Statement Parser::parseStatement()
return parseForLoop(); return parseForLoop();
case Token::Assign: case Token::Assign:
{ {
if (m_dialect.flavour != AsmFlavour::Loose) if (m_dialect->flavour != AsmFlavour::Loose)
break; break;
StackAssignment assignment = createWithLocation<StackAssignment>(); StackAssignment assignment = createWithLocation<StackAssignment>();
advance(); advance();
expectToken(Token::Colon); expectToken(Token::Colon);
assignment.variableName.location = location(); assignment.variableName.location = location();
assignment.variableName.name = YulString(currentLiteral()); 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."); fatalParserError("Identifier expected, got builtin symbol.");
else if (instructions().count(assignment.variableName.name.str())) else if (instructions().count(assignment.variableName.name.str()))
fatalParserError("Identifier expected, got instruction name."); fatalParserError("Identifier expected, got instruction name.");
@ -176,9 +176,9 @@ Statement Parser::parseStatement()
if (currentToken() == Token::Assign && peekNextToken() != Token::Colon) if (currentToken() == Token::Assign && peekNextToken() != Token::Colon)
{ {
Assignment assignment = createWithLocation<Assignment>(identifier.location); 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() + "\"."); 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."); fatalParserError("Cannot use instruction names for identifier names.");
advance(); advance();
assignment.variableNames.emplace_back(identifier); assignment.variableNames.emplace_back(identifier);
@ -189,7 +189,7 @@ Statement Parser::parseStatement()
else else
{ {
// label // label
if (m_dialect.flavour != AsmFlavour::Loose) if (m_dialect->flavour != AsmFlavour::Loose)
fatalParserError("Labels are not supported."); fatalParserError("Labels are not supported.");
Label label = createWithLocation<Label>(identifier.location); Label label = createWithLocation<Label>(identifier.location);
label.name = identifier.name; label.name = identifier.name;
@ -197,7 +197,7 @@ Statement Parser::parseStatement()
} }
} }
default: default:
if (m_dialect.flavour != AsmFlavour::Loose) if (m_dialect->flavour != AsmFlavour::Loose)
fatalParserError("Call or assignment expected."); fatalParserError("Call or assignment expected.");
break; break;
} }
@ -273,7 +273,7 @@ Expression Parser::parseExpression()
instructionNames().at(instr.instruction) + instructionNames().at(instr.instruction) +
"\" not allowed in this context." "\" not allowed in this context."
); );
if (m_dialect.flavour != AsmFlavour::Loose && currentToken() != Token::LParen) if (m_dialect->flavour != AsmFlavour::Loose && currentToken() != Token::LParen)
fatalParserError( fatalParserError(
"Non-functional instructions are not allowed in this context." "Non-functional instructions are not allowed in this context."
); );
@ -293,7 +293,7 @@ Expression Parser::parseExpression()
else if (operation.type() == typeid(Instruction)) else if (operation.type() == typeid(Instruction))
{ {
// Instructions not taking arguments are allowed as expressions. // 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); Instruction& instr = boost::get<Instruction>(operation);
return FunctionalInstruction{std::move(instr.location), instr.instruction, {}}; return FunctionalInstruction{std::move(instr.location), instr.instruction, {}};
} }
@ -362,9 +362,9 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
else else
literal = YulString{currentLiteral()}; literal = YulString{currentLiteral()};
// first search the set of builtins, then the instructions. // first search the set of builtins, then the instructions.
if (m_dialect.builtins->query(literal)) if (m_dialect->builtin(literal))
ret = Identifier{location(), 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()); dev::solidity::Instruction const& instr = instructions().at(literal.str());
ret = Instruction{location(), instr}; ret = Instruction{location(), instr};
@ -405,7 +405,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
{} {}
}; };
advance(); advance();
if (m_dialect.flavour == AsmFlavour::Yul) if (m_dialect->flavour == AsmFlavour::Yul)
{ {
expectToken(Token::Colon); expectToken(Token::Colon);
literal.location.end = endPosition(); literal.location.end = endPosition();
@ -418,7 +418,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
} }
default: default:
fatalParserError( fatalParserError(
m_dialect.flavour == AsmFlavour::Yul ? m_dialect->flavour == AsmFlavour::Yul ?
"Literal or identifier expected." : "Literal or identifier expected." :
"Literal, identifier or instruction expected." "Literal, identifier or instruction expected."
); );
@ -488,7 +488,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
RecursionGuard recursionGuard(*this); RecursionGuard recursionGuard(*this);
if (_initialOp.type() == typeid(Instruction)) 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); Instruction& instruction = boost::get<Instruction>(_initialOp);
FunctionalInstruction ret; FunctionalInstruction ret;
ret.instruction = instruction.instruction; ret.instruction = instruction.instruction;
@ -559,7 +559,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
} }
else else
fatalParserError( fatalParserError(
m_dialect.flavour == AsmFlavour::Yul ? m_dialect->flavour == AsmFlavour::Yul ?
"Function name expected." : "Function name expected." :
"Assembly instruction or function name required in front of \"(\")" "Assembly instruction or function name required in front of \"(\")"
); );
@ -572,7 +572,7 @@ TypedName Parser::parseTypedName()
RecursionGuard recursionGuard(*this); RecursionGuard recursionGuard(*this);
TypedName typedName = createWithLocation<TypedName>(); TypedName typedName = createWithLocation<TypedName>();
typedName.name = expectAsmIdentifier(); typedName.name = expectAsmIdentifier();
if (m_dialect.flavour == AsmFlavour::Yul) if (m_dialect->flavour == AsmFlavour::Yul)
{ {
expectToken(Token::Colon); expectToken(Token::Colon);
typedName.location.end = endPosition(); typedName.location.end = endPosition();
@ -584,7 +584,7 @@ TypedName Parser::parseTypedName()
YulString Parser::expectAsmIdentifier() YulString Parser::expectAsmIdentifier()
{ {
YulString name = YulString{currentLiteral()}; YulString name = YulString{currentLiteral()};
if (m_dialect.flavour == AsmFlavour::Yul) if (m_dialect->flavour == AsmFlavour::Yul)
{ {
switch (currentToken()) switch (currentToken())
{ {
@ -598,7 +598,7 @@ YulString Parser::expectAsmIdentifier()
break; 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."); fatalParserError("Cannot use builtin function name \"" + name.str() + "\" as identifier name.");
else if (instructions().count(name.str())) else if (instructions().count(name.str()))
fatalParserError("Cannot use instruction names for identifier names."); fatalParserError("Cannot use instruction names for identifier names.");

View File

@ -38,7 +38,7 @@ namespace yul
class Parser: public langutil::ParserBase class Parser: public langutil::ParserBase
{ {
public: 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)) {} ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
/// Parses an inline assembly block starting with `{` and ending with `}`. /// Parses an inline assembly block starting with `{` and ending with `}`.
@ -86,7 +86,7 @@ protected:
static bool isValidNumberLiteral(std::string const& _literal); static bool isValidNumberLiteral(std::string const& _literal);
private: private:
Dialect m_dialect = Dialect::looseAssemblyForEVM(); std::shared_ptr<Dialect> m_dialect;
}; };
} }

View File

@ -10,6 +10,7 @@ add_library(yul
ObjectParser.cpp ObjectParser.cpp
backends/evm/EVMAssembly.cpp backends/evm/EVMAssembly.cpp
backends/evm/EVMCodeTransform.cpp backends/evm/EVMCodeTransform.cpp
backends/evm/EVMDialect.cpp
backends/evm/EVMObjectCompiler.cpp backends/evm/EVMObjectCompiler.cpp
optimiser/ASTCopier.cpp optimiser/ASTCopier.cpp
optimiser/ASTWalker.cpp optimiser/ASTWalker.cpp

View File

@ -20,57 +20,10 @@
#include <libyul/Dialect.h> #include <libyul/Dialect.h>
#include <libyul/Object.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <map> #include <map>
using namespace yul; using namespace yul;
using namespace std; 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)};
}

View File

@ -22,7 +22,9 @@
#include <libyul/YulString.h> #include <libyul/YulString.h>
#include <memory> #include <boost/noncopyable.hpp>
#include <vector>
namespace yul namespace yul
{ {
@ -45,38 +47,19 @@ struct BuiltinFunction
bool movable; bool movable;
}; };
/** struct Dialect: boost::noncopyable
* Class to query for builtin functions and their semantics.
*/
struct Builtins
{ {
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. /// @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 Dialect(AsmFlavour _flavour): flavour(_flavour) {}
{ virtual ~Dialect() {}
AsmFlavour flavour = AsmFlavour::Loose;
std::shared_ptr<Builtins> builtins;
Dialect(AsmFlavour _flavour, std::shared_ptr<Builtins> _builtins): static std::shared_ptr<Dialect> yul()
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()
{ {
// Will have to add builtins later. // Will have to add builtins later.
return Dialect{AsmFlavour::Yul, std::make_shared<Builtins>()}; return std::make_shared<Dialect>(AsmFlavour::Yul);
} }
}; };

View File

@ -47,7 +47,7 @@ class ObjectParser: public langutil::ParserBase
public: public:
explicit ObjectParser( explicit ObjectParser(
langutil::ErrorReporter& _errorReporter, langutil::ErrorReporter& _errorReporter,
Dialect _dialect = Dialect::looseAssemblyForEVM() std::shared_ptr<Dialect> _dialect
): ):
ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {} ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
@ -67,7 +67,7 @@ private:
YulString parseUniqueName(Object const* _containingObject); YulString parseUniqueName(Object const* _containingObject);
void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject); void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject);
Dialect m_dialect; std::shared_ptr<Dialect> m_dialect;
}; };
} }

View File

@ -97,7 +97,7 @@ CodeTransform::CodeTransform(
AsmAnalysisInfo& _analysisInfo, AsmAnalysisInfo& _analysisInfo,
Block const& _block, Block const& _block,
bool _allowStackOpt, bool _allowStackOpt,
bool _yul, EVMDialect const& _dialect,
bool _evm15, bool _evm15,
ExternalIdentifierAccess const& _identifierAccess, ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions, bool _useNamedLabelsForFunctions,
@ -106,8 +106,8 @@ CodeTransform::CodeTransform(
): ):
m_assembly(_assembly), m_assembly(_assembly),
m_info(_analysisInfo), m_info(_analysisInfo),
m_dialect(_dialect),
m_allowStackOpt(_allowStackOpt), m_allowStackOpt(_allowStackOpt),
m_yul(_yul),
m_evm15(_evm15), m_evm15(_evm15),
m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions), m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions),
m_identifierAccess(_identifierAccess), m_identifierAccess(_identifierAccess),
@ -267,35 +267,46 @@ void CodeTransform::operator()(FunctionCall const& _call)
{ {
solAssert(m_scope, ""); solAssert(m_scope, "");
m_assembly.setSourceLocation(_call.location); if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name))
EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
if (!m_evm15)
{ {
returnLabel = m_assembly.newLabelId(); builtin->generateCode(_call, m_assembly, [&]() {
m_assembly.appendLabelReference(returnLabel); for (auto const& arg: _call.arguments | boost::adaptors::reversed)
m_stackAdjustment++; 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 else
{ {
m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1); m_assembly.setSourceLocation(_call.location);
m_assembly.appendLabel(returnLabel); EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0
m_stackAdjustment--; 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) void CodeTransform::operator()(FunctionalInstruction const& _instruction)

View File

@ -20,10 +20,10 @@
#include <libyul/backends/evm/EVMAssembly.h> #include <libyul/backends/evm/EVMAssembly.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/optimiser/ASTWalker.h> #include <libyul/optimiser/ASTWalker.h>
#include <libyul/AsmDataForward.h> #include <libyul/AsmDataForward.h>
#include <libyul/AsmScope.h> #include <libyul/AsmScope.h>
#include <libyul/Dialect.h>
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
@ -87,8 +87,8 @@ public:
AbstractAssembly& _assembly, AbstractAssembly& _assembly,
AsmAnalysisInfo& _analysisInfo, AsmAnalysisInfo& _analysisInfo,
Block const& _block, Block const& _block,
EVMDialect const& _dialect,
bool _allowStackOpt = false, bool _allowStackOpt = false,
Dialect const& _dialect,
bool _evm15 = false, bool _evm15 = false,
ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(), ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(),
bool _useNamedLabelsForFunctions = false bool _useNamedLabelsForFunctions = false
@ -115,7 +115,7 @@ protected:
AsmAnalysisInfo& _analysisInfo, AsmAnalysisInfo& _analysisInfo,
Block const& _block, Block const& _block,
bool _allowStackOpt, bool _allowStackOpt,
Dialect const& _dialect, EVMDialect const& _dialect,
bool _evm15, bool _evm15,
ExternalIdentifierAccess const& _identifierAccess, ExternalIdentifierAccess const& _identifierAccess,
bool _useNamedLabelsForFunctions, bool _useNamedLabelsForFunctions,
@ -179,10 +179,10 @@ private:
AbstractAssembly& m_assembly; AbstractAssembly& m_assembly;
AsmAnalysisInfo& m_info; AsmAnalysisInfo& m_info;
Scope* m_scope = nullptr; Scope* m_scope = nullptr;
EVMDialect const& m_dialect;
bool const m_allowStackOpt = true; bool const m_allowStackOpt = true;
Dialect const& m_dialect; bool const m_evm15 = false;
bool m_evm15 = false; bool const m_useNamedLabelsForFunctions = false;
bool m_useNamedLabelsForFunctions = false;
ExternalIdentifierAccess m_identifierAccess; ExternalIdentifierAccess m_identifierAccess;
/// Adjustment between the stack height as determined during the analysis phase /// 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 /// and the stack height in the assembly. This is caused by an initial stack being present

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);
}

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;
};
}

View File

@ -21,13 +21,15 @@
#include <libyul/backends/evm/EVMObjectCompiler.h> #include <libyul/backends/evm/EVMObjectCompiler.h>
#include <libyul/backends/evm/EVMCodeTransform.h> #include <libyul/backends/evm/EVMCodeTransform.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/Object.h> #include <libyul/Object.h>
#include <libyul/Exceptions.h> #include <libyul/Exceptions.h>
using namespace yul; using namespace yul;
using namespace std; 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); EVMObjectCompiler compiler(_assembly, _dialect, _evm15);
compiler.run(_object, _optimize); compiler.run(_object, _optimize);
@ -50,7 +52,13 @@ void EVMObjectCompiler::run(Object& _object, bool _optimize)
subIDs[data.name] = m_assembly.appendData(data.data); 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.analysisInfo, "No analysis info.");
yulAssert(_object.code, "No code."); 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);
} }

View File

@ -23,21 +23,21 @@ namespace yul
{ {
struct Object; struct Object;
class AbstractAssembly; class AbstractAssembly;
struct Dialect; struct EVMDialect;
class EVMObjectCompiler class EVMObjectCompiler
{ {
public: 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: 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) m_assembly(_assembly), m_dialect(_dialect), m_evm15(_evm15)
{} {}
void run(Object& _object, bool _optimize); void run(Object& _object, bool _optimize);
AbstractAssembly& m_assembly; AbstractAssembly& m_assembly;
Dialect const& m_dialect; EVMDialect& m_dialect;
bool m_evm15 = false; bool m_evm15 = false;
}; };

View File

@ -29,6 +29,7 @@
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/AsmAnalysis.h> #include <libyul/AsmAnalysis.h>
#include <libyul/AsmPrinter.h> #include <libyul/AsmPrinter.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/Scanner.h> #include <liblangutil/Scanner.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
@ -54,7 +55,7 @@ void yul::test::printErrors(ErrorList const& _errors)
pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(string const& _source, bool _yul) pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(string const& _source, bool _yul)
{ {
Dialect dialect = _yul ? yul::Dialect::yul() : yul::Dialect::strictAssemblyForEVM(); shared_ptr<Dialect> dialect = _yul ? yul::Dialect::yul() : yul::EVMDialect::strictAssemblyForEVM();
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
auto scanner = make_shared<Scanner>(CharStream(_source, "")); auto scanner = make_shared<Scanner>(CharStream(_source, ""));

View File

@ -49,7 +49,7 @@ namespace test
namespace namespace
{ {
bool parse(string const& _source, Dialect const& _dialect, ErrorReporter& errorReporter) bool parse(string const& _source, std::shared_ptr<Dialect> _dialect, ErrorReporter& errorReporter)
{ {
try try
{ {
@ -74,7 +74,7 @@ bool parse(string const& _source, Dialect const& _dialect, ErrorReporter& errorR
return false; return false;
} }
boost::optional<Error> parseAndReturnFirstError(string const& _source, Dialect const& _dialect, bool _allowWarnings = true) boost::optional<Error> parseAndReturnFirstError(string const& _source, shared_ptr<Dialect> _dialect, bool _allowWarnings = true)
{ {
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
@ -99,12 +99,12 @@ boost::optional<Error> parseAndReturnFirstError(string const& _source, Dialect c
return {}; return {};
} }
bool successParse(std::string const& _source, Dialect const& _dialect = Dialect::yul(), bool _allowWarnings = true) bool successParse(std::string const& _source, shared_ptr<Dialect> _dialect = Dialect::yul(), bool _allowWarnings = true)
{ {
return !parseAndReturnFirstError(_source, _dialect, _allowWarnings); return !parseAndReturnFirstError(_source, _dialect, _allowWarnings);
} }
Error expectError(std::string const& _source, Dialect const& _dialect = Dialect::yul(), bool _allowWarnings = false) Error expectError(std::string const& _source, shared_ptr<Dialect> _dialect = Dialect::yul(), bool _allowWarnings = false)
{ {
auto error = parseAndReturnFirstError(_source, _dialect, _allowWarnings); auto error = parseAndReturnFirstError(_source, _dialect, _allowWarnings);
@ -306,16 +306,17 @@ BOOST_AUTO_TEST_CASE(if_statement_invalid)
BOOST_AUTO_TEST_CASE(builtins_parser) BOOST_AUTO_TEST_CASE(builtins_parser)
{ {
struct SimpleBuiltins: public Builtins struct SimpleDialect: public Dialect
{ {
BuiltinFunction const* query(YulString _name) const override SimpleDialect(): Dialect(AsmFlavour::Strict) {}
BuiltinFunction const* builtin(YulString _name) const override
{ {
return _name == YulString{"builtin"} ? &f : nullptr; return _name == YulString{"builtin"} ? &f : nullptr;
} }
BuiltinFunction f; BuiltinFunction f;
}; };
Dialect dialect(AsmFlavour::Strict, make_shared<SimpleBuiltins>()); shared_ptr<Dialect> dialect = make_shared<SimpleDialect>();
CHECK_ERROR_DIALECT("{ let builtin := 6 }", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect); CHECK_ERROR_DIALECT("{ let builtin := 6 }", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect);
CHECK_ERROR_DIALECT("{ function builtin() {} }", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect); CHECK_ERROR_DIALECT("{ function builtin() {} }", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect);
CHECK_ERROR_DIALECT("{ builtin := 6 }", ParserError, "Cannot assign to builtin function \"builtin\".", dialect); CHECK_ERROR_DIALECT("{ builtin := 6 }", ParserError, "Cannot assign to builtin function \"builtin\".", dialect);
@ -323,16 +324,17 @@ BOOST_AUTO_TEST_CASE(builtins_parser)
BOOST_AUTO_TEST_CASE(builtins_analysis) BOOST_AUTO_TEST_CASE(builtins_analysis)
{ {
struct SimpleBuiltinsAnalysis: public Builtins struct SimpleDialect: public Dialect
{ {
yul::BuiltinFunction const* query(YulString _name) const override SimpleDialect(): Dialect(AsmFlavour::Strict) {}
BuiltinFunction const* builtin(YulString _name) const override
{ {
return _name == YulString("builtin") ? &m_builtin : nullptr; return _name == YulString{"builtin"} ? &f : nullptr;
} }
BuiltinFunction m_builtin{YulString{"builtin"}, vector<Type>(2), vector<Type>(3), false}; BuiltinFunction f{YulString{"builtin"}, vector<Type>(2), vector<Type>(3), false};
}; };
Dialect dialect(AsmFlavour::Strict, make_shared<SimpleBuiltinsAnalysis>()); shared_ptr<Dialect> dialect = make_shared<SimpleDialect>();
BOOST_CHECK(successParse("{ let a, b, c := builtin(1, 2) }", dialect)); BOOST_CHECK(successParse("{ let a, b, c := builtin(1, 2) }", dialect));
CHECK_ERROR_DIALECT("{ let a, b, c := builtin(1) }", TypeError, "Function expects 2 arguments but got 1", dialect); CHECK_ERROR_DIALECT("{ let a, b, c := builtin(1) }", TypeError, "Function expects 2 arguments but got 1", dialect);
CHECK_ERROR_DIALECT("{ let a, b := builtin(1, 2) }", DeclarationError, "Variable count mismatch: 2 variables and 3 values.", dialect); CHECK_ERROR_DIALECT("{ let a, b := builtin(1, 2) }", DeclarationError, "Variable count mismatch: 2 variables and 3 values.", dialect);

View File

@ -41,6 +41,7 @@
#include <libyul/optimiser/RedundantAssignEliminator.h> #include <libyul/optimiser/RedundantAssignEliminator.h>
#include <libyul/optimiser/StructuralSimplifier.h> #include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/Suite.h> #include <libyul/optimiser/Suite.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/AsmPrinter.h> #include <libyul/AsmPrinter.h>
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/AsmAnalysis.h> #include <libyul/AsmAnalysis.h>
@ -262,7 +263,7 @@ void YulOptimizerTest::printIndented(ostream& _stream, string const& _output, st
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted) bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{ {
yul::Dialect dialect = m_yul ? yul::Dialect::yul() : yul::Dialect::strictAssemblyForEVM(); shared_ptr<yul::Dialect> dialect = m_yul ? yul::Dialect::yul() : yul::EVMDialect::strictAssemblyForEVM();
ErrorList errors; ErrorList errors;
ErrorReporter errorReporter(errors); ErrorReporter errorReporter(errors);
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, "")); shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, ""));

View File

@ -0,0 +1,48 @@
object "a" {
code {
datacopy(0, dataoffset("sub"), datasize("sub"))
return(0, datasize("sub"))
}
object "sub" {
code {
sstore(0, dataoffset("sub"))
mstore(0, datasize("data1"))
}
data "data1" "Hello, World!"
}
}
// ----
// Assembly:
// /* "source":26:73 */
// dataSize(sub_0)
// dataOffset(sub_0)
// /* "source":35:36 */
// 0x00
// /* "source":26:73 */
// codecopy
// /* "source":78:104 */
// dataSize(sub_0)
// /* "source":85:86 */
// 0x00
// /* "source":78:104 */
// return
// stop
//
// sub_0: assembly {
// /* "source":143:171 */
// 0x00
// /* "source":150:151 */
// 0x00
// /* "source":143:171 */
// sstore
// /* "source":178:206 */
// 0x0d
// /* "source":185:186 */
// 0x00
// /* "source":178:206 */
// mstore
// stop
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
// }
// Bytecode: 600b600d600039600b6000f3fe6000600055600d600052fe
// Opcodes: PUSH1 0xB PUSH1 0xD PUSH1 0x0 CODECOPY PUSH1 0xB PUSH1 0x0 RETURN INVALID PUSH1 0x0 PUSH1 0x0 SSTORE PUSH1 0xD PUSH1 0x0 MSTORE INVALID

View File

@ -0,0 +1,29 @@
object "a" {
code { sstore(0, dataoffset("sub")) }
object "sub" {
code { sstore(0, 8) }
data "data1" "Hello, World!"
}
}
// ----
// Assembly:
// /* "source":22:50 */
// dataOffset(sub_0)
// /* "source":29:30 */
// 0x00
// /* "source":22:50 */
// sstore
// stop
//
// sub_0: assembly {
// /* "source":91:92 */
// 0x08
// /* "source":88:89 */
// 0x00
// /* "source":81:93 */
// sstore
// stop
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
// }
// Bytecode: 6006600055fe6008600055fe
// Opcodes: PUSH1 0x6 PUSH1 0x0 SSTORE INVALID PUSH1 0x8 PUSH1 0x0 SSTORE INVALID

View File

@ -0,0 +1,16 @@
object "a" {
code { sstore(0, dataoffset("data1")) }
data "data1" "Hello, World!"
}
// ----
// Assembly:
// /* "source":22:52 */
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f
// /* "source":29:30 */
// 0x00
// /* "source":22:52 */
// sstore
// stop
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
// Bytecode: 6006600055fe48656c6c6f2c20576f726c6421
// Opcodes: PUSH1 0x6 PUSH1 0x0 SSTORE INVALID 0x48 PUSH6 0x6C6C6F2C2057 PUSH16 0x726C6421000000000000000000000000

View File

@ -0,0 +1,16 @@
object "a" {
code { sstore(0, dataoffset("a")) }
data "data1" "Hello, World!"
}
// ----
// Assembly:
// /* "source":22:48 */
// 0x00
// /* "source":29:30 */
// 0x00
// /* "source":22:48 */
// sstore
// stop
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
// Bytecode: 6000600055fe
// Opcodes: PUSH1 0x0 PUSH1 0x0 SSTORE INVALID

View File

@ -0,0 +1,29 @@
object "a" {
code { sstore(0, datasize("sub")) }
object "sub" {
code { sstore(0, 8) }
data "data1" "Hello, World!"
}
}
// ----
// Assembly:
// /* "source":22:48 */
// dataSize(sub_0)
// /* "source":29:30 */
// 0x00
// /* "source":22:48 */
// sstore
// stop
//
// sub_0: assembly {
// /* "source":89:90 */
// 0x08
// /* "source":86:87 */
// 0x00
// /* "source":79:91 */
// sstore
// stop
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
// }
// Bytecode: 6006600055fe
// Opcodes: PUSH1 0x6 PUSH1 0x0 SSTORE INVALID

View File

@ -0,0 +1,16 @@
object "a" {
code { sstore(0, datasize("data1")) }
data "data1" "Hello, World!"
}
// ----
// Assembly:
// /* "source":22:50 */
// 0x0d
// /* "source":29:30 */
// 0x00
// /* "source":22:50 */
// sstore
// stop
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
// Bytecode: 600d600055fe
// Opcodes: PUSH1 0xD PUSH1 0x0 SSTORE INVALID

View File

@ -0,0 +1,16 @@
object "a" {
code { sstore(0, datasize("a")) }
data "data1" "Hello, World!"
}
// ----
// Assembly:
// /* "source":22:46 */
// bytecodeSize
// /* "source":29:30 */
// 0x00
// /* "source":22:46 */
// sstore
// stop
// data_acaf3289d7b601cbd114fb36c4d29c85bbfd5e133f14cb355c3fd8d99367964f 48656c6c6f2c20576f726c6421
// Bytecode: 6006600055fe
// Opcodes: PUSH1 0x6 PUSH1 0x0 SSTORE INVALID

View File

@ -49,6 +49,8 @@
#include <libyul/optimiser/StructuralSimplifier.h> #include <libyul/optimiser/StructuralSimplifier.h>
#include <libyul/optimiser/VarDeclPropagator.h> #include <libyul/optimiser/VarDeclPropagator.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libdevcore/JSON.h> #include <libdevcore/JSON.h>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
@ -83,7 +85,7 @@ public:
{ {
ErrorReporter errorReporter(m_errors); ErrorReporter errorReporter(m_errors);
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(_input, "")); shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(_input, ""));
m_ast = yul::Parser(errorReporter, yul::Dialect::strictAssemblyForEVM()).parse(scanner, false); m_ast = yul::Parser(errorReporter, yul::EVMDialect::strictAssemblyForEVM()).parse(scanner, false);
if (!m_ast || !errorReporter.errors().empty()) if (!m_ast || !errorReporter.errors().empty())
{ {
cout << "Error parsing source." << endl; cout << "Error parsing source." << endl;
@ -96,7 +98,7 @@ public:
errorReporter, errorReporter,
EVMVersion::byzantium(), EVMVersion::byzantium(),
langutil::Error::Type::SyntaxError, langutil::Error::Type::SyntaxError,
Dialect::strictAssemblyForEVM() EVMDialect::strictAssemblyForEVM()
); );
if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty()) if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty())
{ {