Introduce the concept of builtin functions.

This commit is contained in:
chriseth 2018-12-03 15:49:23 +01:00
parent e49f37be7f
commit 99db4e3ff4
16 changed files with 148 additions and 62 deletions

View File

@ -316,7 +316,14 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly)
// We use the latest EVM version because we will re-run it anyway.
yul::AsmAnalysisInfo analysisInfo;
boost::optional<Error::Type> errorTypeForLoose = Error::Type::SyntaxError;
yul::AsmAnalyzer(analysisInfo, errorsIgnored, EVMVersion(), errorTypeForLoose, yul::AsmFlavour::Loose, resolver).analyze(_inlineAssembly.operations());
yul::AsmAnalyzer(
analysisInfo,
errorsIgnored,
EVMVersion(),
errorTypeForLoose,
yul::Dialect::looseAssemblyForEVM(),
resolver
).analyze(_inlineAssembly.operations());
return false;
}

View File

@ -658,7 +658,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly)
m_errorReporter,
m_evmVersion,
Error::Type::SyntaxError,
yul::AsmFlavour::Loose,
yul::Dialect::looseAssemblyForEVM(),
identifierAccess
);
if (!analyzer.analyze(_inlineAssembly.operations()))

View File

@ -361,7 +361,7 @@ void CompilerContext::appendInlineAssembly(
ErrorList errors;
ErrorReporter errorReporter(errors);
auto scanner = make_shared<langutil::Scanner>(langutil::CharStream(_assembly, "--CODEGEN--"));
auto parserResult = yul::Parser(errorReporter, yul::AsmFlavour::Strict).parse(scanner, false);
auto parserResult = yul::Parser(errorReporter, yul::Dialect::strictAssemblyForEVM()).parse(scanner, false);
#ifdef SOL_OUTPUT_ASM
cout << yul::AsmPrinter()(*parserResult) << endl;
#endif
@ -373,7 +373,7 @@ void CompilerContext::appendInlineAssembly(
errorReporter,
m_evmVersion,
boost::none,
yul::AsmFlavour::Strict,
yul::Dialect::strictAssemblyForEVM(),
identifierAccess.resolve
).analyze(*parserResult);
if (!parserResult || !errorReporter.errors().empty() || !analyzerResult)

View File

@ -43,19 +43,19 @@ using namespace dev::solidity;
namespace
{
yul::AsmFlavour languageToAsmFlavour(AssemblyStack::Language _language)
yul::Dialect languageToDialect(AssemblyStack::Language _language)
{
switch (_language)
{
case AssemblyStack::Language::Assembly:
return yul::AsmFlavour::Loose;
return yul::Dialect::looseAssemblyForEVM();
case AssemblyStack::Language::StrictAssembly:
return yul::AsmFlavour::Strict;
return yul::Dialect::strictAssemblyForEVM();
case AssemblyStack::Language::Yul:
return yul::AsmFlavour::Yul;
return yul::Dialect::yul();
}
solAssert(false, "");
return yul::AsmFlavour::Yul;
return yul::Dialect::yul();
}
}
@ -72,7 +72,7 @@ bool AssemblyStack::parseAndAnalyze(std::string const& _sourceName, std::string
m_errors.clear();
m_analysisSuccessful = false;
m_scanner = make_shared<Scanner>(CharStream(_source, _sourceName));
m_parserResult = yul::ObjectParser(m_errorReporter, languageToAsmFlavour(m_language)).parse(m_scanner, false);
m_parserResult = yul::ObjectParser(m_errorReporter, languageToDialect(m_language)).parse(m_scanner, false);
if (!m_errorReporter.errors().empty())
return false;
solAssert(m_parserResult, "");
@ -93,7 +93,7 @@ bool AssemblyStack::analyzeParsed()
solAssert(m_parserResult, "");
solAssert(m_parserResult->code, "");
m_parserResult->analysisInfo = make_shared<yul::AsmAnalysisInfo>();
yul::AsmAnalyzer analyzer(*m_parserResult->analysisInfo, m_errorReporter, m_evmVersion, boost::none, languageToAsmFlavour(m_language));
yul::AsmAnalyzer analyzer(*m_parserResult->analysisInfo, m_errorReporter, m_evmVersion, boost::none, languageToDialect(m_language));
m_analysisSuccessful = analyzer.analyze(*m_parserResult->code);
return m_analysisSuccessful;
}

View File

@ -101,7 +101,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal)
}
else if (_literal.kind == LiteralKind::Boolean)
{
solAssert(m_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_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_flavour != AsmFlavour::Loose || m_errorTypeForLoose))
if (m_stackHeight != initialStackHeight && (m_dialect.flavour != AsmFlavour::Loose || m_errorTypeForLoose))
{
Error::Type errorType = m_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) +
@ -563,7 +563,7 @@ Scope& AsmAnalyzer::scope(Block const* _block)
}
void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
{
if (m_flavour != AsmFlavour::Yul)
if (m_dialect.flavour != AsmFlavour::Yul)
return;
if (!builtinTypes.count(type))
@ -623,7 +623,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio
if (_instr == solidity::Instruction::JUMP || _instr == solidity::Instruction::JUMPI || _instr == solidity::Instruction::JUMPDEST)
{
solAssert(m_flavour == AsmFlavour::Loose, "");
solAssert(m_dialect.flavour == AsmFlavour::Loose, "");
m_errorReporter.error(
m_errorTypeForLoose ? *m_errorTypeForLoose : Error::Type::Warning,
_location,
@ -636,7 +636,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio
void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description)
{
if (m_flavour != AsmFlavour::Loose)
if (m_dialect.flavour != AsmFlavour::Loose)
solAssert(false, _description);
else if (m_errorTypeForLoose)
m_errorReporter.error(*m_errorTypeForLoose, _location, _description);

View File

@ -23,12 +23,12 @@
#include <liblangutil/Exceptions.h>
#include <liblangutil/EVMVersion.h>
#include <libyul/Dialect.h>
#include <libyul/AsmScope.h>
#include <libyul/AsmDataForward.h>
#include <libyul/backends/evm/AbstractAssembly.h>
#include <libyul/AsmDataForward.h>
#include <boost/variant.hpp>
#include <boost/optional.hpp>
@ -59,14 +59,14 @@ public:
langutil::ErrorReporter& _errorReporter,
dev::solidity::EVMVersion _evmVersion,
boost::optional<langutil::Error::Type> _errorTypeForLoose,
AsmFlavour _flavour = AsmFlavour::Loose,
Dialect _dialect = Dialect::looseAssemblyForEVM(),
ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver()
):
m_resolver(_resolver),
m_info(_analysisInfo),
m_errorReporter(_errorReporter),
m_evmVersion(_evmVersion),
m_flavour(_flavour),
m_dialect(std::move(_dialect)),
m_errorTypeForLoose(_errorTypeForLoose)
{}
@ -115,7 +115,7 @@ private:
AsmAnalysisInfo& m_info;
langutil::ErrorReporter& m_errorReporter;
dev::solidity::EVMVersion m_evmVersion;
AsmFlavour m_flavour = AsmFlavour::Loose;
Dialect m_dialect = Dialect::looseAssemblyForEVM();
boost::optional<langutil::Error::Type> m_errorTypeForLoose;
};

View File

@ -49,11 +49,4 @@ struct TypedName;
using Expression = boost::variant<FunctionalInstruction, FunctionCall, Identifier, Literal>;
using Statement = boost::variant<ExpressionStatement, Instruction, Label, StackAssignment, Assignment, VariableDeclaration, FunctionDefinition, If, Switch, ForLoop, Block>;
enum class AsmFlavour
{
Loose, // no types, EVM instructions as function, jumps and direct stack manipulations
Strict, // no types, EVM instructions as functions, but no jumps and no direct stack manipulations
Yul // same as Strict mode with types
};
}

View File

@ -107,7 +107,7 @@ Statement Parser::parseStatement()
return parseForLoop();
case Token::Assign:
{
if (m_flavour != AsmFlavour::Loose)
if (m_dialect.flavour != AsmFlavour::Loose)
break;
StackAssignment assignment = createWithLocation<StackAssignment>();
advance();
@ -174,7 +174,7 @@ Statement Parser::parseStatement()
if (currentToken() == Token::Assign && peekNextToken() != Token::Colon)
{
Assignment assignment = createWithLocation<Assignment>(identifier.location);
if (m_flavour != AsmFlavour::Yul && instructions().count(identifier.name.str()))
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);
@ -185,7 +185,7 @@ Statement Parser::parseStatement()
else
{
// label
if (m_flavour != AsmFlavour::Loose)
if (m_dialect.flavour != AsmFlavour::Loose)
fatalParserError("Labels are not supported.");
Label label = createWithLocation<Label>(identifier.location);
label.name = identifier.name;
@ -193,7 +193,7 @@ Statement Parser::parseStatement()
}
}
default:
if (m_flavour != AsmFlavour::Loose)
if (m_dialect.flavour != AsmFlavour::Loose)
fatalParserError("Call or assignment expected.");
break;
}
@ -269,7 +269,7 @@ Expression Parser::parseExpression()
instructionNames().at(instr.instruction) +
"\" not allowed in this context."
);
if (m_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."
);
@ -289,7 +289,7 @@ Expression Parser::parseExpression()
else if (operation.type() == typeid(Instruction))
{
// Instructions not taking arguments are allowed as expressions.
solAssert(m_flavour == AsmFlavour::Loose, "");
solAssert(m_dialect.flavour == AsmFlavour::Loose, "");
Instruction& instr = boost::get<Instruction>(operation);
return FunctionalInstruction{std::move(instr.location), instr.instruction, {}};
}
@ -358,7 +358,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
else
literal = YulString{currentLiteral()};
// first search the set of instructions.
if (m_flavour != AsmFlavour::Yul && instructions().count(literal.str()))
if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(literal.str()))
{
dev::solidity::Instruction const& instr = instructions().at(literal.str());
ret = Instruction{location(), instr};
@ -399,7 +399,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
{}
};
advance();
if (m_flavour == AsmFlavour::Yul)
if (m_dialect.flavour == AsmFlavour::Yul)
{
expectToken(Token::Colon);
literal.location.end = endPosition();
@ -412,7 +412,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation()
}
default:
fatalParserError(
m_flavour == AsmFlavour::Yul ?
m_dialect.flavour == AsmFlavour::Yul ?
"Literal or identifier expected." :
"Literal, identifier or instruction expected."
);
@ -482,7 +482,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
RecursionGuard recursionGuard(*this);
if (_initialOp.type() == typeid(Instruction))
{
solAssert(m_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;
@ -553,7 +553,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
}
else
fatalParserError(
m_flavour == AsmFlavour::Yul ?
m_dialect.flavour == AsmFlavour::Yul ?
"Function name expected." :
"Assembly instruction or function name required in front of \"(\")"
);
@ -566,7 +566,7 @@ TypedName Parser::parseTypedName()
RecursionGuard recursionGuard(*this);
TypedName typedName = createWithLocation<TypedName>();
typedName.name = expectAsmIdentifier();
if (m_flavour == AsmFlavour::Yul)
if (m_dialect.flavour == AsmFlavour::Yul)
{
expectToken(Token::Colon);
typedName.location.end = endPosition();
@ -578,7 +578,7 @@ TypedName Parser::parseTypedName()
YulString Parser::expectAsmIdentifier()
{
YulString name = YulString{currentLiteral()};
if (m_flavour == AsmFlavour::Yul)
if (m_dialect.flavour == AsmFlavour::Yul)
{
switch (currentToken())
{

View File

@ -22,21 +22,24 @@
#pragma once
#include <memory>
#include <vector>
#include <libyul/AsmData.h>
#include <libyul/Dialect.h>
#include <liblangutil/SourceLocation.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/ParserBase.h>
#include <memory>
#include <vector>
namespace yul
{
class Parser: public langutil::ParserBase
{
public:
explicit Parser(langutil::ErrorReporter& _errorReporter, AsmFlavour _flavour = AsmFlavour::Loose):
ParserBase(_errorReporter), m_flavour(_flavour) {}
explicit Parser(langutil::ErrorReporter& _errorReporter, Dialect _dialect = Dialect::looseAssemblyForEVM()):
ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
/// Parses an inline assembly block starting with `{` and ending with `}`.
/// @param _reuseScanner if true, do check for end of input after the `}`.
@ -83,7 +86,7 @@ protected:
static bool isValidNumberLiteral(std::string const& _literal);
private:
AsmFlavour m_flavour = AsmFlavour::Loose;
Dialect m_dialect = Dialect::looseAssemblyForEVM();
};
}

82
libyul/Dialect.h Normal file
View File

@ -0,0 +1,82 @@
/*
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 dialect.
*/
#pragma once
#include <libyul/YulString.h>
#include <memory>
namespace yul
{
class YulString;
using Type = YulString;
enum class AsmFlavour
{
Loose, // no types, EVM instructions as function, jumps and direct stack manipulations
Strict, // no types, EVM instructions as functions, but no jumps and no direct stack manipulations
Yul // same as Strict mode with types
};
struct BuiltinFunction
{
YulString name;
std::vector<Type> parameters;
std::vector<Type> returns;
bool movable;
};
/**
* Class to query for builtin functions and their semantics.
*/
struct Builtins
{
virtual ~Builtins() = default;
/// @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; }
};
struct Dialect
{
AsmFlavour flavour = AsmFlavour::Loose;
std::shared_ptr<Builtins> builtins;
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 yul()
{
// Will have to add builtins later.
return Dialect{AsmFlavour::Yul, std::make_shared<Builtins>()};
}
};
}

View File

@ -104,7 +104,7 @@ shared_ptr<Block> ObjectParser::parseCode()
shared_ptr<Block> ObjectParser::parseBlock()
{
Parser parser(m_errorReporter, m_flavour);
Parser parser(m_errorReporter, m_dialect);
shared_ptr<Block> block = parser.parse(m_scanner, true);
yulAssert(block || m_errorReporter.hasErrors(), "Invalid block but no error!");
return block;

View File

@ -22,6 +22,7 @@
#include <libyul/YulString.h>
#include <libyul/Object.h>
#include <libyul/Dialect.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/ParserBase.h>
@ -46,9 +47,9 @@ class ObjectParser: public langutil::ParserBase
public:
explicit ObjectParser(
langutil::ErrorReporter& _errorReporter,
yul::AsmFlavour _flavour = yul::AsmFlavour::Loose
Dialect _dialect = Dialect::looseAssemblyForEVM()
):
ParserBase(_errorReporter), m_flavour(_flavour) {}
ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {}
/// Parses a Yul object.
/// Falls back to code-only parsing if the source starts with `{`.
@ -66,7 +67,7 @@ private:
YulString parseUniqueName(Object const* _containingObject);
void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject);
yul::AsmFlavour m_flavour;
Dialect m_dialect;
};
}

View File

@ -54,11 +54,11 @@ void yul::test::printErrors(ErrorList const& _errors, Scanner const& _scanner)
pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(string const& _source, bool _yul)
{
auto flavour = _yul ? yul::AsmFlavour::Yul : yul::AsmFlavour::Strict;
Dialect dialect = _yul ? yul::Dialect::yul() : yul::Dialect::strictAssemblyForEVM();
ErrorList errors;
ErrorReporter errorReporter(errors);
auto scanner = make_shared<Scanner>(CharStream(_source, ""));
auto parserResult = yul::Parser(errorReporter, flavour).parse(scanner, false);
auto parserResult = yul::Parser(errorReporter, dialect).parse(scanner, false);
if (parserResult)
{
BOOST_REQUIRE(errorReporter.errors().empty());
@ -68,7 +68,7 @@ pair<shared_ptr<Block>, shared_ptr<yul::AsmAnalysisInfo>> yul::test::parse(strin
errorReporter,
dev::test::Options::get().evmVersion(),
boost::none,
flavour
dialect
);
if (analyzer.analyze(*parserResult))
{

View File

@ -52,7 +52,7 @@ bool parse(string const& _source, ErrorReporter& errorReporter)
try
{
auto scanner = make_shared<Scanner>(CharStream(_source, ""));
auto parserResult = yul::Parser(errorReporter, yul::AsmFlavour::Yul).parse(scanner, false);
auto parserResult = yul::Parser(errorReporter, yul::Dialect::yul()).parse(scanner, false);
if (parserResult)
{
yul::AsmAnalysisInfo analysisInfo;
@ -61,7 +61,7 @@ bool parse(string const& _source, ErrorReporter& errorReporter)
errorReporter,
dev::test::Options::get().evmVersion(),
boost::none,
yul::AsmFlavour::Yul
yul::Dialect::yul()
)).analyze(*parserResult);
}
}

View File

@ -256,11 +256,11 @@ void YulOptimizerTest::printIndented(ostream& _stream, string const& _output, st
bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool const _formatted)
{
yul::AsmFlavour flavour = m_yul ? yul::AsmFlavour::Yul : yul::AsmFlavour::Strict;
yul::Dialect dialect = m_yul ? yul::Dialect::yul() : yul::Dialect::strictAssemblyForEVM();
ErrorList errors;
ErrorReporter errorReporter(errors);
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, ""));
m_ast = yul::Parser(errorReporter, flavour).parse(scanner, false);
m_ast = yul::Parser(errorReporter, dialect).parse(scanner, false);
if (!m_ast || !errorReporter.errors().empty())
{
FormattedScope(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
@ -273,7 +273,7 @@ bool YulOptimizerTest::parse(ostream& _stream, string const& _linePrefix, bool c
errorReporter,
dev::test::Options::get().evmVersion(),
boost::none,
flavour
dialect
);
if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty())
{

View File

@ -82,7 +82,7 @@ public:
{
ErrorReporter errorReporter(m_errors);
shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(_input, ""));
m_ast = yul::Parser(errorReporter, yul::AsmFlavour::Strict).parse(scanner, false);
m_ast = yul::Parser(errorReporter, yul::Dialect::strictAssemblyForEVM()).parse(scanner, false);
if (!m_ast || !errorReporter.errors().empty())
{
cout << "Error parsing source." << endl;
@ -95,7 +95,7 @@ public:
errorReporter,
EVMVersion::byzantium(),
boost::none,
AsmFlavour::Strict
Dialect::strictAssemblyForEVM()
);
if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty())
{