Merge pull request #7884 from ethereum/yul-exceptions

Replace all solAsserts with yulAsserts in libyul (and support YulException in StandardCompiler)
This commit is contained in:
chriseth 2019-12-03 20:33:42 +01:00 committed by GitHub
commit 306c7eb5e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 174 additions and 162 deletions

View File

@ -24,6 +24,7 @@
#include <libsolidity/ast/ASTJsonConverter.h> #include <libsolidity/ast/ASTJsonConverter.h>
#include <libyul/AssemblyStack.h> #include <libyul/AssemblyStack.h>
#include <libyul/Exceptions.h>
#include <liblangutil/SourceReferenceFormatter.h> #include <liblangutil/SourceReferenceFormatter.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <libdevcore/JSON.h> #include <libdevcore/JSON.h>
@ -817,6 +818,16 @@ Json::Value StandardCompiler::compileSolidity(StandardCompiler::InputsAndSetting
"Unimplemented feature (" + _exception.lineInfo() + ")" "Unimplemented feature (" + _exception.lineInfo() + ")"
)); ));
} }
catch (yul::YulException const& _exception)
{
errors.append(formatErrorWithException(
_exception,
false,
"YulException",
"general",
"Yul exception"
));
}
catch (Exception const& _exception) catch (Exception const& _exception)
{ {
errors.append(formatError( errors.append(formatError(

View File

@ -60,12 +60,12 @@ bool AsmAnalyzer::analyze(Block const& _block)
success = (*this)(_block); success = (*this)(_block);
if (!success) if (!success)
solAssert(m_errorReporter.hasErrors(), "No success but no error."); yulAssert(m_errorReporter.hasErrors(), "No success but no error.");
} }
catch (FatalError const&) catch (FatalError const&)
{ {
// This FatalError con occur if the errorReporter has too many errors. // This FatalError con occur if the errorReporter has too many errors.
solAssert(!m_errorReporter.errors().empty(), "Fatal error detected, but no error is reported."); yulAssert(!m_errorReporter.errors().empty(), "Fatal error detected, but no error is reported.");
} }
return success && !m_errorReporter.hasErrors(); return success && !m_errorReporter.hasErrors();
} }
@ -83,13 +83,13 @@ AsmAnalysisInfo AsmAnalyzer::analyzeStrictAssertCorrect(Dialect const& _dialect,
{}, {},
_object.dataNames() _object.dataNames()
).analyze(*_object.code); ).analyze(*_object.code);
solAssert(success && errorList.empty(), "Invalid assembly/yul code."); yulAssert(success && errorList.empty(), "Invalid assembly/yul code.");
return analysisInfo; return analysisInfo;
} }
bool AsmAnalyzer::operator()(Label const& _label) bool AsmAnalyzer::operator()(Label const& _label)
{ {
solAssert(!_label.name.empty(), ""); yulAssert(!_label.name.empty(), "");
checkLooseFeature( checkLooseFeature(
_label.location, _label.location,
"The use of labels is disallowed. Please use \"if\", \"switch\", \"for\" or function calls instead." "The use of labels is disallowed. Please use \"if\", \"switch\", \"for\" or function calls instead."
@ -134,8 +134,8 @@ bool AsmAnalyzer::operator()(Literal const& _literal)
} }
else if (_literal.kind == LiteralKind::Boolean) else if (_literal.kind == LiteralKind::Boolean)
{ {
solAssert(m_dialect.flavour == AsmFlavour::Yul, ""); yulAssert(m_dialect.flavour == AsmFlavour::Yul, "");
solAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, ""); yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "");
} }
m_info.stackHeightInfo[&_literal] = m_stackHeight; m_info.stackHeightInfo[&_literal] = m_stackHeight;
return true; return true;
@ -143,7 +143,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal)
bool AsmAnalyzer::operator()(Identifier const& _identifier) bool AsmAnalyzer::operator()(Identifier const& _identifier)
{ {
solAssert(!_identifier.name.empty(), ""); yulAssert(!_identifier.name.empty(), "");
size_t numErrorsBefore = m_errorReporter.errors().size(); size_t numErrorsBefore = m_errorReporter.errors().size();
bool success = true; bool success = true;
if (m_currentScope->lookup(_identifier.name, GenericVisitor{ if (m_currentScope->lookup(_identifier.name, GenericVisitor{
@ -197,14 +197,14 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier)
bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr) bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
{ {
solAssert(m_dialect.flavour != AsmFlavour::Yul, ""); yulAssert(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))
success = false; success = false;
// Parser already checks that the number of arguments is correct. // Parser already checks that the number of arguments is correct.
auto const& info = instructionInfo(_instr.instruction); auto const& info = instructionInfo(_instr.instruction);
solAssert(info.args == int(_instr.arguments.size()), ""); yulAssert(info.args == int(_instr.arguments.size()), "");
m_stackHeight += info.ret - info.args; m_stackHeight += info.ret - info.args;
m_info.stackHeightInfo[&_instr] = m_stackHeight; m_info.stackHeightInfo[&_instr] = m_stackHeight;
warnOnInstructions(_instr.instruction, _instr.location); warnOnInstructions(_instr.instruction, _instr.location);
@ -245,9 +245,9 @@ bool AsmAnalyzer::operator()(StackAssignment const& _assignment)
bool AsmAnalyzer::operator()(Assignment const& _assignment) bool AsmAnalyzer::operator()(Assignment const& _assignment)
{ {
solAssert(_assignment.value, ""); yulAssert(_assignment.value, "");
int const expectedItems = _assignment.variableNames.size(); int const expectedItems = _assignment.variableNames.size();
solAssert(expectedItems >= 1, ""); yulAssert(expectedItems >= 1, "");
int const stackHeight = m_stackHeight; int const stackHeight = m_stackHeight;
bool success = std::visit(*this, *_assignment.value); bool success = std::visit(*this, *_assignment.value);
if ((m_stackHeight - stackHeight) != expectedItems) if ((m_stackHeight - stackHeight) != expectedItems)
@ -306,9 +306,9 @@ bool AsmAnalyzer::operator()(VariableDeclaration const& _varDecl)
bool AsmAnalyzer::operator()(FunctionDefinition const& _funDef) bool AsmAnalyzer::operator()(FunctionDefinition const& _funDef)
{ {
solAssert(!_funDef.name.empty(), ""); yulAssert(!_funDef.name.empty(), "");
Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get(); Block const* virtualBlock = m_info.virtualBlocks.at(&_funDef).get();
solAssert(virtualBlock, ""); yulAssert(virtualBlock, "");
Scope& varScope = scope(virtualBlock); Scope& varScope = scope(virtualBlock);
for (auto const& var: _funDef.parameters + _funDef.returnVariables) for (auto const& var: _funDef.parameters + _funDef.returnVariables)
{ {
@ -328,7 +328,7 @@ bool AsmAnalyzer::operator()(FunctionDefinition const& _funDef)
bool AsmAnalyzer::operator()(FunctionCall const& _funCall) bool AsmAnalyzer::operator()(FunctionCall const& _funCall)
{ {
solAssert(!_funCall.functionName.name.empty(), ""); yulAssert(!_funCall.functionName.name.empty(), "");
bool success = true; bool success = true;
size_t parameters = 0; size_t parameters = 0;
size_t returns = 0; size_t returns = 0;
@ -426,7 +426,7 @@ bool AsmAnalyzer::operator()(If const& _if)
bool AsmAnalyzer::operator()(Switch const& _switch) bool AsmAnalyzer::operator()(Switch const& _switch)
{ {
solAssert(_switch.expression, ""); yulAssert(_switch.expression, "");
bool success = true; bool success = true;
@ -498,7 +498,7 @@ bool AsmAnalyzer::operator()(Switch const& _switch)
bool AsmAnalyzer::operator()(ForLoop const& _for) bool AsmAnalyzer::operator()(ForLoop const& _for)
{ {
solAssert(_for.condition, ""); yulAssert(_for.condition, "");
Scope* outerScope = m_currentScope; Scope* outerScope = m_currentScope;
@ -609,7 +609,7 @@ bool AsmAnalyzer::expectDeposit(int _deposit, int _oldHeight, SourceLocation con
bool AsmAnalyzer::checkAssignment(Identifier const& _variable, size_t _valueSize) bool AsmAnalyzer::checkAssignment(Identifier const& _variable, size_t _valueSize)
{ {
solAssert(!_variable.name.empty(), ""); yulAssert(!_variable.name.empty(), "");
bool success = true; bool success = true;
size_t numErrorsBefore = m_errorReporter.errors().size(); size_t numErrorsBefore = m_errorReporter.errors().size();
size_t variableSize(-1); size_t variableSize(-1);
@ -665,9 +665,9 @@ bool AsmAnalyzer::checkAssignment(Identifier const& _variable, size_t _valueSize
Scope& AsmAnalyzer::scope(Block const* _block) Scope& AsmAnalyzer::scope(Block const* _block)
{ {
solAssert(m_info.scopes.count(_block) == 1, "Scope requested but not present."); yulAssert(m_info.scopes.count(_block) == 1, "Scope requested but not present.");
auto scopePtr = m_info.scopes.at(_block); auto scopePtr = m_info.scopes.at(_block);
solAssert(scopePtr, "Scope requested but not present."); yulAssert(scopePtr, "Scope requested but not present.");
return *scopePtr; return *scopePtr;
} }
void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location) void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location)
@ -686,10 +686,10 @@ void AsmAnalyzer::warnOnInstructions(dev::eth::Instruction _instr, SourceLocatio
{ {
// We assume that returndatacopy, returndatasize and staticcall are either all available // We assume that returndatacopy, returndatasize and staticcall are either all available
// or all not available. // or all not available.
solAssert(m_evmVersion.supportsReturndata() == m_evmVersion.hasStaticCall(), ""); yulAssert(m_evmVersion.supportsReturndata() == m_evmVersion.hasStaticCall(), "");
// Similarly we assume bitwise shifting and create2 go together. // Similarly we assume bitwise shifting and create2 go together.
solAssert(m_evmVersion.hasBitwiseShifting() == m_evmVersion.hasCreate2(), ""); yulAssert(m_evmVersion.hasBitwiseShifting() == m_evmVersion.hasCreate2(), "");
solAssert(m_dialect.flavour != AsmFlavour::Yul, ""); yulAssert(m_dialect.flavour != AsmFlavour::Yul, "");
auto errorForVM = [=](string const& vmKindMessage) { auto errorForVM = [=](string const& vmKindMessage) {
m_errorReporter.typeError( m_errorReporter.typeError(
@ -768,7 +768,7 @@ void AsmAnalyzer::warnOnInstructions(dev::eth::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); yulAssert(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

@ -21,6 +21,7 @@
*/ */
#include <libyul/AsmParser.h> #include <libyul/AsmParser.h>
#include <libyul/Exceptions.h>
#include <liblangutil/Scanner.h> #include <liblangutil/Scanner.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
@ -52,7 +53,7 @@ shared_ptr<Block> Parser::parse(std::shared_ptr<Scanner> const& _scanner, bool _
} }
catch (FatalError const&) catch (FatalError const&)
{ {
solAssert(!m_errorReporter.errors().empty(), "Fatal error detected, but no error is reported."); yulAssert(!m_errorReporter.errors().empty(), "Fatal error detected, but no error is reported.");
} }
return nullptr; return nullptr;
@ -257,7 +258,7 @@ Statement Parser::parseStatement()
} }
else else
{ {
solAssert(holds_alternative<Instruction>(elementary), "Invalid elementary operation."); yulAssert(holds_alternative<Instruction>(elementary), "Invalid elementary operation.");
return std::get<Instruction>(elementary); return std::get<Instruction>(elementary);
} }
} }
@ -277,7 +278,7 @@ Case Parser::parseCase()
_case.value = make_unique<Literal>(std::get<Literal>(std::move(literal))); _case.value = make_unique<Literal>(std::get<Literal>(std::move(literal)));
} }
else else
solAssert(false, "Case or default case expected."); yulAssert(false, "Case or default case expected.");
_case.body = parseBlock(); _case.body = parseBlock();
_case.location.end = _case.body.location.end; _case.location.end = _case.body.location.end;
return _case; return _case;
@ -315,7 +316,7 @@ Expression Parser::parseExpression()
return parseCall(std::move(operation)); return parseCall(std::move(operation));
else if (holds_alternative<Instruction>(operation)) else if (holds_alternative<Instruction>(operation))
{ {
solAssert(m_dialect.flavour == AsmFlavour::Loose, ""); yulAssert(m_dialect.flavour == AsmFlavour::Loose, "");
Instruction const& instr = std::get<Instruction>(operation); Instruction const& instr = std::get<Instruction>(operation);
// Disallow instructions returning multiple values (and DUP/SWAP) as expression. // Disallow instructions returning multiple values (and DUP/SWAP) as expression.
if ( if (
@ -348,7 +349,7 @@ Expression Parser::parseExpression()
else if (holds_alternative<Instruction>(operation)) else if (holds_alternative<Instruction>(operation))
{ {
// Instructions not taking arguments are allowed as expressions. // Instructions not taking arguments are allowed as expressions.
solAssert(m_dialect.flavour == AsmFlavour::Loose, ""); yulAssert(m_dialect.flavour == AsmFlavour::Loose, "");
Instruction& instr = std::get<Instruction>(operation); Instruction& instr = std::get<Instruction>(operation);
return FunctionalInstruction{std::move(instr.location), instr.instruction, {}}; return FunctionalInstruction{std::move(instr.location), instr.instruction, {}};
} }
@ -356,7 +357,7 @@ Expression Parser::parseExpression()
return std::get<Identifier>(operation); return std::get<Identifier>(operation);
else else
{ {
solAssert(holds_alternative<Literal>(operation), ""); yulAssert(holds_alternative<Literal>(operation), "");
return std::get<Literal>(operation); return std::get<Literal>(operation);
} }
} }
@ -539,7 +540,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp)
RecursionGuard recursionGuard(*this); RecursionGuard recursionGuard(*this);
if (holds_alternative<Instruction>(_initialOp)) if (holds_alternative<Instruction>(_initialOp))
{ {
solAssert(m_dialect.flavour != AsmFlavour::Yul, "Instructions are invalid in Yul"); yulAssert(m_dialect.flavour != AsmFlavour::Yul, "Instructions are invalid in Yul");
Instruction& instruction = std::get<Instruction>(_initialOp); Instruction& instruction = std::get<Instruction>(_initialOp);
FunctionalInstruction ret; FunctionalInstruction ret;
ret.instruction = instruction.instruction; ret.instruction = instruction.instruction;

View File

@ -22,7 +22,7 @@
#include <libyul/AsmPrinter.h> #include <libyul/AsmPrinter.h>
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <liblangutil/Exceptions.h> #include <libyul/Exceptions.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
@ -41,8 +41,8 @@ using namespace yul;
string AsmPrinter::operator()(yul::Instruction const& _instruction) const string AsmPrinter::operator()(yul::Instruction const& _instruction) const
{ {
solAssert(!m_yul, ""); yulAssert(!m_yul, "");
solAssert(isValidInstruction(_instruction.instruction), "Invalid instruction"); yulAssert(isValidInstruction(_instruction.instruction), "Invalid instruction");
return boost::to_lower_copy(instructionInfo(_instruction.instruction).name); return boost::to_lower_copy(instructionInfo(_instruction.instruction).name);
} }
@ -51,10 +51,10 @@ string AsmPrinter::operator()(Literal const& _literal) const
switch (_literal.kind) switch (_literal.kind)
{ {
case LiteralKind::Number: case LiteralKind::Number:
solAssert(isValidDecimal(_literal.value.str()) || isValidHex(_literal.value.str()), "Invalid number literal"); yulAssert(isValidDecimal(_literal.value.str()) || isValidHex(_literal.value.str()), "Invalid number literal");
return _literal.value.str() + appendTypeName(_literal.type); return _literal.value.str() + appendTypeName(_literal.type);
case LiteralKind::Boolean: case LiteralKind::Boolean:
solAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal."); yulAssert(_literal.value == "true"_yulstring || _literal.value == "false"_yulstring, "Invalid bool literal.");
return ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type); return ((_literal.value == "true"_yulstring) ? "true" : "false") + appendTypeName(_literal.type);
case LiteralKind::String: case LiteralKind::String:
break; break;
@ -91,14 +91,14 @@ string AsmPrinter::operator()(Literal const& _literal) const
string AsmPrinter::operator()(Identifier const& _identifier) const string AsmPrinter::operator()(Identifier const& _identifier) const
{ {
solAssert(!_identifier.name.empty(), "Invalid identifier."); yulAssert(!_identifier.name.empty(), "Invalid identifier.");
return _identifier.name.str(); return _identifier.name.str();
} }
string AsmPrinter::operator()(FunctionalInstruction const& _functionalInstruction) const string AsmPrinter::operator()(FunctionalInstruction const& _functionalInstruction) const
{ {
solAssert(!m_yul, ""); yulAssert(!m_yul, "");
solAssert(isValidInstruction(_functionalInstruction.instruction), "Invalid instruction"); yulAssert(isValidInstruction(_functionalInstruction.instruction), "Invalid instruction");
return return
boost::to_lower_copy(instructionInfo(_functionalInstruction.instruction).name) + boost::to_lower_copy(instructionInfo(_functionalInstruction.instruction).name) +
"(" + "(" +
@ -116,21 +116,21 @@ string AsmPrinter::operator()(ExpressionStatement const& _statement) const
string AsmPrinter::operator()(Label const& _label) const string AsmPrinter::operator()(Label const& _label) const
{ {
solAssert(!m_yul, ""); yulAssert(!m_yul, "");
solAssert(!_label.name.empty(), "Invalid label."); yulAssert(!_label.name.empty(), "Invalid label.");
return _label.name.str() + ":"; return _label.name.str() + ":";
} }
string AsmPrinter::operator()(StackAssignment const& _assignment) const string AsmPrinter::operator()(StackAssignment const& _assignment) const
{ {
solAssert(!m_yul, ""); yulAssert(!m_yul, "");
solAssert(!_assignment.variableName.name.empty(), "Invalid variable name."); yulAssert(!_assignment.variableName.name.empty(), "Invalid variable name.");
return "=: " + (*this)(_assignment.variableName); return "=: " + (*this)(_assignment.variableName);
} }
string AsmPrinter::operator()(Assignment const& _assignment) const string AsmPrinter::operator()(Assignment const& _assignment) const
{ {
solAssert(_assignment.variableNames.size() >= 1, ""); yulAssert(_assignment.variableNames.size() >= 1, "");
string variables = (*this)(_assignment.variableNames.front()); string variables = (*this)(_assignment.variableNames.front());
for (size_t i = 1; i < _assignment.variableNames.size(); ++i) for (size_t i = 1; i < _assignment.variableNames.size(); ++i)
variables += ", " + (*this)(_assignment.variableNames[i]); variables += ", " + (*this)(_assignment.variableNames[i]);
@ -156,7 +156,7 @@ string AsmPrinter::operator()(VariableDeclaration const& _variableDeclaration) c
string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition) const string AsmPrinter::operator()(FunctionDefinition const& _functionDefinition) const
{ {
solAssert(!_functionDefinition.name.empty(), "Invalid function name."); yulAssert(!_functionDefinition.name.empty(), "Invalid function name.");
string out = "function " + _functionDefinition.name.str() + "("; string out = "function " + _functionDefinition.name.str() + "(";
out += boost::algorithm::join( out += boost::algorithm::join(
_functionDefinition.parameters | boost::adaptors::transformed( _functionDefinition.parameters | boost::adaptors::transformed(
@ -191,7 +191,7 @@ string AsmPrinter::operator()(FunctionCall const& _functionCall) const
string AsmPrinter::operator()(If const& _if) const string AsmPrinter::operator()(If const& _if) const
{ {
solAssert(_if.condition, "Invalid if condition."); yulAssert(_if.condition, "Invalid if condition.");
string body = (*this)(_if.body); string body = (*this)(_if.body);
char delim = '\n'; char delim = '\n';
if (body.find('\n') == string::npos) if (body.find('\n') == string::npos)
@ -201,7 +201,7 @@ string AsmPrinter::operator()(If const& _if) const
string AsmPrinter::operator()(Switch const& _switch) const string AsmPrinter::operator()(Switch const& _switch) const
{ {
solAssert(_switch.expression, "Invalid expression pointer."); yulAssert(_switch.expression, "Invalid expression pointer.");
string out = "switch " + std::visit(*this, *_switch.expression); string out = "switch " + std::visit(*this, *_switch.expression);
for (auto const& _case: _switch.cases) for (auto const& _case: _switch.cases)
{ {
@ -216,7 +216,7 @@ string AsmPrinter::operator()(Switch const& _switch) const
string AsmPrinter::operator()(ForLoop const& _forLoop) const string AsmPrinter::operator()(ForLoop const& _forLoop) const
{ {
solAssert(_forLoop.condition, "Invalid for loop condition."); yulAssert(_forLoop.condition, "Invalid for loop condition.");
string pre = (*this)(_forLoop.pre); string pre = (*this)(_forLoop.pre);
string condition = std::visit(*this, *_forLoop.condition); string condition = std::visit(*this, *_forLoop.condition);
string post = (*this)(_forLoop.post); string post = (*this)(_forLoop.post);
@ -261,7 +261,7 @@ string AsmPrinter::operator()(Block const& _block) const
string AsmPrinter::formatTypedName(TypedName _variable) const string AsmPrinter::formatTypedName(TypedName _variable) const
{ {
solAssert(!_variable.name.empty(), "Invalid variable name."); yulAssert(!_variable.name.empty(), "Invalid variable name.");
return _variable.name.str() + appendTypeName(_variable.type); return _variable.name.str() + appendTypeName(_variable.type);
} }

View File

@ -23,9 +23,9 @@
#include <libyul/AsmData.h> #include <libyul/AsmData.h>
#include <libyul/AsmScope.h> #include <libyul/AsmScope.h>
#include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmAnalysisInfo.h>
#include <libyul/Exceptions.h>
#include <liblangutil/ErrorReporter.h> #include <liblangutil/ErrorReporter.h>
#include <liblangutil/Exceptions.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
@ -88,7 +88,7 @@ bool ScopeFiller::operator()(FunctionDefinition const& _funDef)
if (!(*this)(_funDef.body)) if (!(*this)(_funDef.body))
success = false; success = false;
solAssert(m_currentScope == &varScope, ""); yulAssert(m_currentScope == &varScope, "");
m_currentScope = m_currentScope->superScope; m_currentScope = m_currentScope->superScope;
return success; return success;

View File

@ -63,7 +63,7 @@ Dialect const& languageToDialect(AssemblyStack::Language _language, EVMVersion _
case AssemblyStack::Language::EWasm: case AssemblyStack::Language::EWasm:
return WasmDialect::instance(); return WasmDialect::instance();
} }
solAssert(false, ""); yulAssert(false, "");
return Dialect::yul(); return Dialect::yul();
} }
@ -72,7 +72,7 @@ Dialect const& languageToDialect(AssemblyStack::Language _language, EVMVersion _
Scanner const& AssemblyStack::scanner() const Scanner const& AssemblyStack::scanner() const
{ {
solAssert(m_scanner, ""); yulAssert(m_scanner, "");
return *m_scanner; return *m_scanner;
} }
@ -84,8 +84,8 @@ bool AssemblyStack::parseAndAnalyze(std::string const& _sourceName, std::string
m_parserResult = ObjectParser(m_errorReporter, languageToDialect(m_language, m_evmVersion)).parse(m_scanner, false); m_parserResult = ObjectParser(m_errorReporter, languageToDialect(m_language, m_evmVersion)).parse(m_scanner, false);
if (!m_errorReporter.errors().empty()) if (!m_errorReporter.errors().empty())
return false; return false;
solAssert(m_parserResult, ""); yulAssert(m_parserResult, "");
solAssert(m_parserResult->code, ""); yulAssert(m_parserResult->code, "");
return analyzeParsed(); return analyzeParsed();
} }
@ -95,12 +95,12 @@ void AssemblyStack::optimize()
if (!m_optimiserSettings.runYulOptimiser) if (!m_optimiserSettings.runYulOptimiser)
return; return;
solAssert(m_analysisSuccessful, "Analysis was not successful."); yulAssert(m_analysisSuccessful, "Analysis was not successful.");
m_analysisSuccessful = false; m_analysisSuccessful = false;
solAssert(m_parserResult, ""); yulAssert(m_parserResult, "");
optimize(*m_parserResult, true); optimize(*m_parserResult, true);
solAssert(analyzeParsed(), "Invalid source code after optimization."); yulAssert(analyzeParsed(), "Invalid source code after optimization.");
} }
void AssemblyStack::translate(AssemblyStack::Language _targetLanguage) void AssemblyStack::translate(AssemblyStack::Language _targetLanguage)
@ -122,14 +122,14 @@ void AssemblyStack::translate(AssemblyStack::Language _targetLanguage)
bool AssemblyStack::analyzeParsed() bool AssemblyStack::analyzeParsed()
{ {
solAssert(m_parserResult, ""); yulAssert(m_parserResult, "");
m_analysisSuccessful = analyzeParsed(*m_parserResult); m_analysisSuccessful = analyzeParsed(*m_parserResult);
return m_analysisSuccessful; return m_analysisSuccessful;
} }
bool AssemblyStack::analyzeParsed(Object& _object) bool AssemblyStack::analyzeParsed(Object& _object)
{ {
solAssert(_object.code, ""); yulAssert(_object.code, "");
_object.analysisInfo = make_shared<AsmAnalysisInfo>(); _object.analysisInfo = make_shared<AsmAnalysisInfo>();
AsmAnalyzer analyzer( AsmAnalyzer analyzer(
@ -159,15 +159,15 @@ void AssemblyStack::compileEVM(AbstractAssembly& _assembly, bool _evm15, bool _o
else if (m_language == AssemblyStack::Language::Yul) else if (m_language == AssemblyStack::Language::Yul)
dialect = &EVMDialect::yulForEVM(m_evmVersion); dialect = &EVMDialect::yulForEVM(m_evmVersion);
else else
solAssert(false, "Invalid language."); yulAssert(false, "Invalid language.");
EVMObjectCompiler::compile(*m_parserResult, _assembly, *dialect, _evm15, _optimize); EVMObjectCompiler::compile(*m_parserResult, _assembly, *dialect, _evm15, _optimize);
} }
void AssemblyStack::optimize(Object& _object, bool _isCreation) void AssemblyStack::optimize(Object& _object, bool _isCreation)
{ {
solAssert(_object.code, ""); yulAssert(_object.code, "");
solAssert(_object.analysisInfo, ""); yulAssert(_object.analysisInfo, "");
for (auto& subNode: _object.subObjects) for (auto& subNode: _object.subObjects)
if (auto subObject = dynamic_cast<Object*>(subNode.get())) if (auto subObject = dynamic_cast<Object*>(subNode.get()))
optimize(*subObject, false); optimize(*subObject, false);
@ -186,10 +186,10 @@ void AssemblyStack::optimize(Object& _object, bool _isCreation)
MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const
{ {
solAssert(m_analysisSuccessful, ""); yulAssert(m_analysisSuccessful, "");
solAssert(m_parserResult, ""); yulAssert(m_parserResult, "");
solAssert(m_parserResult->code, ""); yulAssert(m_parserResult->code, "");
solAssert(m_parserResult->analysisInfo, ""); yulAssert(m_parserResult->analysisInfo, "");
switch (_machine) switch (_machine)
{ {
@ -214,7 +214,7 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const
} }
case Machine::eWasm: case Machine::eWasm:
{ {
solAssert(m_language == Language::EWasm, ""); yulAssert(m_language == Language::EWasm, "");
Dialect const& dialect = languageToDialect(m_language, EVMVersion{}); Dialect const& dialect = languageToDialect(m_language, EVMVersion{});
MachineAssemblyObject object; MachineAssemblyObject object;
@ -231,15 +231,15 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const
string AssemblyStack::print() const string AssemblyStack::print() const
{ {
solAssert(m_parserResult, ""); yulAssert(m_parserResult, "");
solAssert(m_parserResult->code, ""); yulAssert(m_parserResult->code, "");
return m_parserResult->toString(m_language == Language::Yul) + "\n"; return m_parserResult->toString(m_language == Language::Yul) + "\n";
} }
shared_ptr<Object> AssemblyStack::parserResult() const shared_ptr<Object> AssemblyStack::parserResult() const
{ {
solAssert(m_analysisSuccessful, "Analysis was not successful."); yulAssert(m_analysisSuccessful, "Analysis was not successful.");
solAssert(m_parserResult, ""); yulAssert(m_parserResult, "");
solAssert(m_parserResult->code, ""); yulAssert(m_parserResult->code, "");
return m_parserResult; return m_parserResult;
} }

View File

@ -41,7 +41,7 @@ map<YulString, int> CompilabilityChecker::run(
if (_dialect.flavour == AsmFlavour::Yul) if (_dialect.flavour == AsmFlavour::Yul)
return {}; return {};
solAssert(_dialect.flavour == AsmFlavour::Strict, ""); yulAssert(_dialect.flavour == AsmFlavour::Strict, "");
if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect)) if (EVMDialect const* evmDialect = dynamic_cast<EVMDialect const*>(&_dialect))
{ {
@ -69,7 +69,7 @@ map<YulString, int> CompilabilityChecker::run(
} }
catch (StackTooDeepError const&) catch (StackTooDeepError const&)
{ {
solAssert(!transform.stackErrors().empty(), "Got stack too deep exception that was not stored."); yulAssert(!transform.stackErrors().empty(), "Got stack too deep exception that was not stored.");
} }
std::map<YulString, int> functions; std::map<YulString, int> functions;

View File

@ -112,7 +112,7 @@ shared_ptr<Block> ObjectParser::parseBlock()
void ObjectParser::parseData(Object& _containingObject) void ObjectParser::parseData(Object& _containingObject)
{ {
solAssert( yulAssert(
currentToken() == Token::Identifier && currentLiteral() == "data", currentToken() == Token::Identifier && currentLiteral() == "data",
"parseData called on wrong input." "parseData called on wrong input."
); );

View File

@ -118,19 +118,19 @@ void EthAssemblyAdapter::appendJumpToIf(LabelID _labelId)
void EthAssemblyAdapter::appendBeginsub(LabelID, int) void EthAssemblyAdapter::appendBeginsub(LabelID, int)
{ {
// TODO we could emulate that, though // TODO we could emulate that, though
solAssert(false, "BEGINSUB not implemented for EVM 1.0"); yulAssert(false, "BEGINSUB not implemented for EVM 1.0");
} }
void EthAssemblyAdapter::appendJumpsub(LabelID, int, int) void EthAssemblyAdapter::appendJumpsub(LabelID, int, int)
{ {
// TODO we could emulate that, though // TODO we could emulate that, though
solAssert(false, "JUMPSUB not implemented for EVM 1.0"); yulAssert(false, "JUMPSUB not implemented for EVM 1.0");
} }
void EthAssemblyAdapter::appendReturnsub(int, int) void EthAssemblyAdapter::appendReturnsub(int, int)
{ {
// TODO we could emulate that, though // TODO we could emulate that, though
solAssert(false, "RETURNSUB not implemented for EVM 1.0"); yulAssert(false, "RETURNSUB not implemented for EVM 1.0");
} }
void EthAssemblyAdapter::appendAssemblySize() void EthAssemblyAdapter::appendAssemblySize()
@ -174,7 +174,7 @@ AbstractAssembly::SubID EthAssemblyAdapter::appendData(bytes const& _data)
EthAssemblyAdapter::LabelID EthAssemblyAdapter::assemblyTagToIdentifier(eth::AssemblyItem const& _tag) EthAssemblyAdapter::LabelID EthAssemblyAdapter::assemblyTagToIdentifier(eth::AssemblyItem const& _tag)
{ {
u256 id = _tag.data(); u256 id = _tag.data();
solAssert(id <= std::numeric_limits<LabelID>::max(), "Tag id too large."); yulAssert(id <= std::numeric_limits<LabelID>::max(), "Tag id too large.");
return LabelID(id); return LabelID(id);
} }
@ -207,11 +207,11 @@ void CodeGenerator::assemble(
} }
catch (StackTooDeepError const& _e) catch (StackTooDeepError const& _e)
{ {
solAssert( yulAssert(
false, false,
"Stack too deep when compiling inline assembly" + "Stack too deep when compiling inline assembly" +
(_e.comment() ? ": " + *_e.comment() : ".") (_e.comment() ? ": " + *_e.comment() : ".")
); );
} }
solAssert(transform.stackErrors().empty(), "Stack errors present but not thrown."); yulAssert(transform.stackErrors().empty(), "Stack errors present but not thrown.");
} }

View File

@ -19,11 +19,10 @@
*/ */
#include <libyul/backends/evm/EVMAssembly.h> #include <libyul/backends/evm/EVMAssembly.h>
#include <libyul/Exceptions.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <liblangutil/Exceptions.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::eth; using namespace dev::eth;
@ -65,7 +64,7 @@ void EVMAssembly::appendLabel(LabelID _labelId)
void EVMAssembly::appendLabelReference(LabelID _labelId) void EVMAssembly::appendLabelReference(LabelID _labelId)
{ {
solAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode."); yulAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode.");
// @TODO we now always use labelReferenceSize for all labels, it could be shortened // @TODO we now always use labelReferenceSize for all labels, it could be shortened
// for some of them. // for some of them.
appendInstruction(dev::eth::pushInstruction(labelReferenceSize)); appendInstruction(dev::eth::pushInstruction(labelReferenceSize));
@ -81,7 +80,7 @@ EVMAssembly::LabelID EVMAssembly::newLabelId()
AbstractAssembly::LabelID EVMAssembly::namedLabel(string const& _name) AbstractAssembly::LabelID EVMAssembly::namedLabel(string const& _name)
{ {
solAssert(!_name.empty(), ""); yulAssert(!_name.empty(), "");
if (!m_namedLabels.count(_name)) if (!m_namedLabels.count(_name))
m_namedLabels[_name] = newLabelId(); m_namedLabels[_name] = newLabelId();
return m_namedLabels[_name]; return m_namedLabels[_name];
@ -89,12 +88,12 @@ AbstractAssembly::LabelID EVMAssembly::namedLabel(string const& _name)
void EVMAssembly::appendLinkerSymbol(string const&) void EVMAssembly::appendLinkerSymbol(string const&)
{ {
solAssert(false, "Linker symbols not yet implemented."); yulAssert(false, "Linker symbols not yet implemented.");
} }
void EVMAssembly::appendJump(int _stackDiffAfter) void EVMAssembly::appendJump(int _stackDiffAfter)
{ {
solAssert(!m_evm15, "Plain JUMP used for EVM 1.5"); yulAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
appendInstruction(dev::eth::Instruction::JUMP); appendInstruction(dev::eth::Instruction::JUMP);
m_stackHeight += _stackDiffAfter; m_stackHeight += _stackDiffAfter;
} }
@ -131,8 +130,8 @@ void EVMAssembly::appendJumpToIf(LabelID _labelId)
void EVMAssembly::appendBeginsub(LabelID _labelId, int _arguments) void EVMAssembly::appendBeginsub(LabelID _labelId, int _arguments)
{ {
solAssert(m_evm15, "BEGINSUB used for EVM 1.0"); yulAssert(m_evm15, "BEGINSUB used for EVM 1.0");
solAssert(_arguments >= 0, ""); yulAssert(_arguments >= 0, "");
setLabelToCurrentPosition(_labelId); setLabelToCurrentPosition(_labelId);
m_bytecode.push_back(uint8_t(dev::eth::Instruction::BEGINSUB)); m_bytecode.push_back(uint8_t(dev::eth::Instruction::BEGINSUB));
m_stackHeight += _arguments; m_stackHeight += _arguments;
@ -140,8 +139,8 @@ void EVMAssembly::appendBeginsub(LabelID _labelId, int _arguments)
void EVMAssembly::appendJumpsub(LabelID _labelId, int _arguments, int _returns) void EVMAssembly::appendJumpsub(LabelID _labelId, int _arguments, int _returns)
{ {
solAssert(m_evm15, "JUMPSUB used for EVM 1.0"); yulAssert(m_evm15, "JUMPSUB used for EVM 1.0");
solAssert(_arguments >= 0 && _returns >= 0, ""); yulAssert(_arguments >= 0 && _returns >= 0, "");
m_bytecode.push_back(uint8_t(dev::eth::Instruction::JUMPSUB)); m_bytecode.push_back(uint8_t(dev::eth::Instruction::JUMPSUB));
appendLabelReferenceInternal(_labelId); appendLabelReferenceInternal(_labelId);
m_stackHeight += _returns - _arguments; m_stackHeight += _returns - _arguments;
@ -149,8 +148,8 @@ void EVMAssembly::appendJumpsub(LabelID _labelId, int _arguments, int _returns)
void EVMAssembly::appendReturnsub(int _returns, int _stackDiffAfter) void EVMAssembly::appendReturnsub(int _returns, int _stackDiffAfter)
{ {
solAssert(m_evm15, "RETURNSUB used for EVM 1.0"); yulAssert(m_evm15, "RETURNSUB used for EVM 1.0");
solAssert(_returns >= 0, ""); yulAssert(_returns >= 0, "");
m_bytecode.push_back(uint8_t(dev::eth::Instruction::RETURNSUB)); m_bytecode.push_back(uint8_t(dev::eth::Instruction::RETURNSUB));
m_stackHeight += _stackDiffAfter - _returns; m_stackHeight += _stackDiffAfter - _returns;
} }
@ -164,9 +163,9 @@ eth::LinkerObject EVMAssembly::finalize()
for (auto const& ref: m_labelReferences) for (auto const& ref: m_labelReferences)
{ {
size_t referencePos = ref.first; size_t referencePos = ref.first;
solAssert(m_labelPositions.count(ref.second), ""); yulAssert(m_labelPositions.count(ref.second), "");
size_t labelPos = m_labelPositions.at(ref.second); size_t labelPos = m_labelPositions.at(ref.second);
solAssert(labelPos != size_t(-1), "Undefined but allocated label used."); yulAssert(labelPos != size_t(-1), "Undefined but allocated label used.");
updateReference(referencePos, labelReferenceSize, u256(labelPos)); updateReference(referencePos, labelReferenceSize, u256(labelPos));
} }
@ -177,8 +176,8 @@ eth::LinkerObject EVMAssembly::finalize()
void EVMAssembly::setLabelToCurrentPosition(LabelID _labelId) void EVMAssembly::setLabelToCurrentPosition(LabelID _labelId)
{ {
solAssert(m_labelPositions.count(_labelId), "Label not found."); yulAssert(m_labelPositions.count(_labelId), "Label not found.");
solAssert(m_labelPositions[_labelId] == size_t(-1), "Label already set."); yulAssert(m_labelPositions[_labelId] == size_t(-1), "Label already set.");
m_labelPositions[_labelId] = m_bytecode.size(); m_labelPositions[_labelId] = m_bytecode.size();
} }
@ -197,29 +196,29 @@ void EVMAssembly::appendAssemblySize()
pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EVMAssembly::createSubAssembly() pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> EVMAssembly::createSubAssembly()
{ {
solAssert(false, "Sub assemblies not implemented."); yulAssert(false, "Sub assemblies not implemented.");
return {}; return {};
} }
void EVMAssembly::appendDataOffset(AbstractAssembly::SubID) void EVMAssembly::appendDataOffset(AbstractAssembly::SubID)
{ {
solAssert(false, "Data not implemented."); yulAssert(false, "Data not implemented.");
} }
void EVMAssembly::appendDataSize(AbstractAssembly::SubID) void EVMAssembly::appendDataSize(AbstractAssembly::SubID)
{ {
solAssert(false, "Data not implemented."); yulAssert(false, "Data not implemented.");
} }
AbstractAssembly::SubID EVMAssembly::appendData(bytes const&) AbstractAssembly::SubID EVMAssembly::appendData(bytes const&)
{ {
solAssert(false, "Data not implemented."); yulAssert(false, "Data not implemented.");
} }
void EVMAssembly::updateReference(size_t pos, size_t size, u256 value) void EVMAssembly::updateReference(size_t pos, size_t size, u256 value)
{ {
solAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, ""); yulAssert(m_bytecode.size() >= size && pos <= m_bytecode.size() - size, "");
solAssert(value < (u256(1) << (8 * size)), ""); yulAssert(value < (u256(1) << (8 * size)), "");
for (size_t i = 0; i < size; i++) for (size_t i = 0; i < size; i++)
m_bytecode[pos + i] = uint8_t((value >> (8 * (size - i - 1))) & 0xff); m_bytecode[pos + i] = uint8_t((value >> (8 * (size - i - 1))) & 0xff);
} }

View File

@ -44,9 +44,9 @@ void VariableReferenceCounter::operator()(FunctionDefinition const& _function)
{ {
Scope* originalScope = m_scope; Scope* originalScope = m_scope;
solAssert(m_info.virtualBlocks.at(&_function), ""); yulAssert(m_info.virtualBlocks.at(&_function), "");
m_scope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get(); m_scope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
solAssert(m_scope, "Variable scope does not exist."); yulAssert(m_scope, "Variable scope does not exist.");
for (auto const& v: _function.returnVariables) for (auto const& v: _function.returnVariables)
increaseRefIfFound(v.name); increaseRefIfFound(v.name);
@ -131,7 +131,7 @@ void CodeTransform::decreaseReference(YulString, Scope::Variable const& _var)
return; return;
unsigned& ref = m_context->variableReferences.at(&_var); unsigned& ref = m_context->variableReferences.at(&_var);
solAssert(ref >= 1, ""); yulAssert(ref >= 1, "");
--ref; --ref;
if (ref == 0) if (ref == 0)
m_variablesScheduledForDeletion.insert(&_var); m_variablesScheduledForDeletion.insert(&_var);
@ -157,7 +157,7 @@ void CodeTransform::freeUnusedVariables()
while (m_unusedStackSlots.count(m_assembly.stackHeight() - 1)) while (m_unusedStackSlots.count(m_assembly.stackHeight() - 1))
{ {
solAssert(m_unusedStackSlots.erase(m_assembly.stackHeight() - 1), ""); yulAssert(m_unusedStackSlots.erase(m_assembly.stackHeight() - 1), "");
m_assembly.appendInstruction(dev::eth::Instruction::POP); m_assembly.appendInstruction(dev::eth::Instruction::POP);
--m_stackAdjustment; --m_stackAdjustment;
} }
@ -165,8 +165,8 @@ void CodeTransform::freeUnusedVariables()
void CodeTransform::deleteVariable(Scope::Variable const& _var) void CodeTransform::deleteVariable(Scope::Variable const& _var)
{ {
solAssert(m_allowStackOpt, ""); yulAssert(m_allowStackOpt, "");
solAssert(m_context->variableStackHeights.count(&_var) > 0, ""); yulAssert(m_context->variableStackHeights.count(&_var) > 0, "");
m_unusedStackSlots.insert(m_context->variableStackHeights[&_var]); m_unusedStackSlots.insert(m_context->variableStackHeights[&_var]);
m_context->variableStackHeights.erase(&_var); m_context->variableStackHeights.erase(&_var);
m_context->variableReferences.erase(&_var); m_context->variableReferences.erase(&_var);
@ -175,7 +175,7 @@ void CodeTransform::deleteVariable(Scope::Variable const& _var)
void CodeTransform::operator()(VariableDeclaration const& _varDecl) void CodeTransform::operator()(VariableDeclaration const& _varDecl)
{ {
solAssert(m_scope, ""); yulAssert(m_scope, "");
int const numVariables = _varDecl.variables.size(); int const numVariables = _varDecl.variables.size();
int height = m_assembly.stackHeight(); int height = m_assembly.stackHeight();
@ -254,7 +254,7 @@ void CodeTransform::operator()(Assignment const& _assignment)
void CodeTransform::operator()(StackAssignment const& _assignment) void CodeTransform::operator()(StackAssignment const& _assignment)
{ {
solAssert(!m_allowStackOpt, ""); yulAssert(!m_allowStackOpt, "");
m_assembly.setSourceLocation(_assignment.location); m_assembly.setSourceLocation(_assignment.location);
generateAssignment(_assignment.variableName); generateAssignment(_assignment.variableName);
checkStackHeight(&_assignment); checkStackHeight(&_assignment);
@ -269,10 +269,10 @@ void CodeTransform::operator()(ExpressionStatement const& _statement)
void CodeTransform::operator()(Label const& _label) void CodeTransform::operator()(Label const& _label)
{ {
solAssert(!m_allowStackOpt, ""); yulAssert(!m_allowStackOpt, "");
m_assembly.setSourceLocation(_label.location); m_assembly.setSourceLocation(_label.location);
solAssert(m_scope, ""); yulAssert(m_scope, "");
solAssert(m_scope->identifiers.count(_label.name), ""); yulAssert(m_scope->identifiers.count(_label.name), "");
Scope::Label& label = std::get<Scope::Label>(m_scope->identifiers.at(_label.name)); Scope::Label& label = std::get<Scope::Label>(m_scope->identifiers.at(_label.name));
m_assembly.appendLabel(labelID(label)); m_assembly.appendLabel(labelID(label));
checkStackHeight(&_label); checkStackHeight(&_label);
@ -280,7 +280,7 @@ void CodeTransform::operator()(Label const& _label)
void CodeTransform::operator()(FunctionCall const& _call) void CodeTransform::operator()(FunctionCall const& _call)
{ {
solAssert(m_scope, ""); yulAssert(m_scope, "");
if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name)) if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name))
{ {
@ -302,13 +302,13 @@ void CodeTransform::operator()(FunctionCall const& _call)
} }
Scope::Function* function = nullptr; Scope::Function* function = nullptr;
solAssert(m_scope->lookup(_call.functionName.name, GenericVisitor{ yulAssert(m_scope->lookup(_call.functionName.name, GenericVisitor{
[=](Scope::Variable&) { solAssert(false, "Expected function name."); }, [=](Scope::Variable&) { yulAssert(false, "Expected function name."); },
[=](Scope::Label&) { solAssert(false, "Expected function name."); }, [=](Scope::Label&) { yulAssert(false, "Expected function name."); },
[&](Scope::Function& _function) { function = &_function; } [&](Scope::Function& _function) { function = &_function; }
}), "Function name not found."); }), "Function name not found.");
solAssert(function, ""); yulAssert(function, "");
solAssert(function->arguments.size() == _call.arguments.size(), ""); yulAssert(function->arguments.size() == _call.arguments.size(), "");
for (auto const& arg: _call.arguments | boost::adaptors::reversed) for (auto const& arg: _call.arguments | boost::adaptors::reversed)
visitExpression(arg); visitExpression(arg);
m_assembly.setSourceLocation(_call.location); m_assembly.setSourceLocation(_call.location);
@ -334,12 +334,12 @@ void CodeTransform::operator()(FunctionalInstruction const& _instruction)
bool const isJumpI = _instruction.instruction == dev::eth::Instruction::JUMPI; bool const isJumpI = _instruction.instruction == dev::eth::Instruction::JUMPI;
if (isJumpI) if (isJumpI)
{ {
solAssert(_instruction.arguments.size() == 2, ""); yulAssert(_instruction.arguments.size() == 2, "");
visitExpression(_instruction.arguments.at(1)); visitExpression(_instruction.arguments.at(1));
} }
else else
{ {
solAssert(_instruction.arguments.size() == 1, ""); yulAssert(_instruction.arguments.size() == 1, "");
} }
m_assembly.setSourceLocation(_instruction.location); m_assembly.setSourceLocation(_instruction.location);
auto label = labelFromIdentifier(std::get<Identifier>(_instruction.arguments.at(0))); auto label = labelFromIdentifier(std::get<Identifier>(_instruction.arguments.at(0)));
@ -362,7 +362,7 @@ void CodeTransform::operator()(Identifier const& _identifier)
{ {
m_assembly.setSourceLocation(_identifier.location); m_assembly.setSourceLocation(_identifier.location);
// First search internals, then externals. // First search internals, then externals.
solAssert(m_scope, ""); yulAssert(m_scope, "");
if (m_scope->lookup(_identifier.name, GenericVisitor{ if (m_scope->lookup(_identifier.name, GenericVisitor{
[=](Scope::Variable& _var) [=](Scope::Variable& _var)
{ {
@ -381,13 +381,13 @@ void CodeTransform::operator()(Identifier const& _identifier)
}, },
[=](Scope::Function&) [=](Scope::Function&)
{ {
solAssert(false, "Function not removed during desugaring."); yulAssert(false, "Function not removed during desugaring.");
} }
})) }))
{ {
return; return;
} }
solAssert( yulAssert(
m_identifierAccess.generateCode, m_identifierAccess.generateCode,
"Identifier not found and no external access available." "Identifier not found and no external access available."
); );
@ -405,9 +405,9 @@ void CodeTransform::operator()(Literal const& _literal)
void CodeTransform::operator()(yul::Instruction const& _instruction) void CodeTransform::operator()(yul::Instruction const& _instruction)
{ {
solAssert(!m_allowStackOpt, ""); yulAssert(!m_allowStackOpt, "");
solAssert(!m_evm15 || _instruction.instruction != dev::eth::Instruction::JUMP, "Bare JUMP instruction used for EVM1.5"); yulAssert(!m_evm15 || _instruction.instruction != dev::eth::Instruction::JUMP, "Bare JUMP instruction used for EVM1.5");
solAssert(!m_evm15 || _instruction.instruction != dev::eth::Instruction::JUMPI, "Bare JUMPI instruction used for EVM1.5"); yulAssert(!m_evm15 || _instruction.instruction != dev::eth::Instruction::JUMPI, "Bare JUMPI instruction used for EVM1.5");
m_assembly.setSourceLocation(_instruction.location); m_assembly.setSourceLocation(_instruction.location);
m_assembly.appendInstruction(_instruction.instruction); m_assembly.appendInstruction(_instruction.instruction);
checkStackHeight(&_instruction); checkStackHeight(&_instruction);
@ -442,7 +442,7 @@ void CodeTransform::operator()(Switch const& _switch)
m_assembly.setSourceLocation(c.location); m_assembly.setSourceLocation(c.location);
AbstractAssembly::LabelID bodyLabel = m_assembly.newLabelId(); AbstractAssembly::LabelID bodyLabel = m_assembly.newLabelId();
caseBodies[&c] = bodyLabel; caseBodies[&c] = bodyLabel;
solAssert(m_assembly.stackHeight() == expressionHeight + 1, ""); yulAssert(m_assembly.stackHeight() == expressionHeight + 1, "");
m_assembly.appendInstruction(dev::eth::dupInstruction(2)); m_assembly.appendInstruction(dev::eth::dupInstruction(2));
m_assembly.appendInstruction(dev::eth::Instruction::EQ); m_assembly.appendInstruction(dev::eth::Instruction::EQ);
m_assembly.appendJumpToIf(bodyLabel); m_assembly.appendJumpToIf(bodyLabel);
@ -476,15 +476,15 @@ void CodeTransform::operator()(Switch const& _switch)
void CodeTransform::operator()(FunctionDefinition const& _function) void CodeTransform::operator()(FunctionDefinition const& _function)
{ {
solAssert(m_scope, ""); yulAssert(m_scope, "");
solAssert(m_scope->identifiers.count(_function.name), ""); yulAssert(m_scope->identifiers.count(_function.name), "");
Scope::Function& function = std::get<Scope::Function>(m_scope->identifiers.at(_function.name)); Scope::Function& function = std::get<Scope::Function>(m_scope->identifiers.at(_function.name));
int const localStackAdjustment = m_evm15 ? 0 : 1; int const localStackAdjustment = m_evm15 ? 0 : 1;
int height = localStackAdjustment; int height = localStackAdjustment;
solAssert(m_info.scopes.at(&_function.body), ""); yulAssert(m_info.scopes.at(&_function.body), "");
Scope* varScope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get(); Scope* varScope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get();
solAssert(varScope, ""); yulAssert(varScope, "");
for (auto const& v: _function.parameters | boost::adaptors::reversed) for (auto const& v: _function.parameters | boost::adaptors::reversed)
{ {
auto& var = std::get<Scope::Variable>(varScope->identifiers.at(v.name)); auto& var = std::get<Scope::Variable>(varScope->identifiers.at(v.name));
@ -582,7 +582,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function)
swap(stackLayout[stackLayout.back()], stackLayout.back()); swap(stackLayout[stackLayout.back()], stackLayout.back());
} }
for (int i = 0; size_t(i) < stackLayout.size(); ++i) for (int i = 0; size_t(i) < stackLayout.size(); ++i)
solAssert(i == stackLayout[i], "Error reshuffling stack."); yulAssert(i == stackLayout[i], "Error reshuffling stack.");
} }
} }
if (m_evm15) if (m_evm15)
@ -683,15 +683,15 @@ AbstractAssembly::LabelID CodeTransform::labelFromIdentifier(Identifier const& _
{ {
AbstractAssembly::LabelID label = AbstractAssembly::LabelID(-1); AbstractAssembly::LabelID label = AbstractAssembly::LabelID(-1);
if (!m_scope->lookup(_identifier.name, GenericVisitor{ if (!m_scope->lookup(_identifier.name, GenericVisitor{
[=](Scope::Variable&) { solAssert(false, "Expected label"); }, [=](Scope::Variable&) { yulAssert(false, "Expected label"); },
[&](Scope::Label& _label) [&](Scope::Label& _label)
{ {
label = labelID(_label); label = labelID(_label);
}, },
[=](Scope::Function&) { solAssert(false, "Expected label"); } [=](Scope::Function&) { yulAssert(false, "Expected label"); }
})) }))
{ {
solAssert(false, "Identifier not found."); yulAssert(false, "Identifier not found.");
} }
return label; return label;
} }
@ -759,15 +759,15 @@ void CodeTransform::finalizeBlock(Block const& _block, int blockStartStackHeight
freeUnusedVariables(); freeUnusedVariables();
// pop variables // pop variables
solAssert(m_info.scopes.at(&_block).get() == m_scope, ""); yulAssert(m_info.scopes.at(&_block).get() == m_scope, "");
for (auto const& id: m_scope->identifiers) for (auto const& id: m_scope->identifiers)
if (holds_alternative<Scope::Variable>(id.second)) if (holds_alternative<Scope::Variable>(id.second))
{ {
Scope::Variable const& var = std::get<Scope::Variable>(id.second); Scope::Variable const& var = std::get<Scope::Variable>(id.second);
if (m_allowStackOpt) if (m_allowStackOpt)
{ {
solAssert(!m_context->variableStackHeights.count(&var), ""); yulAssert(!m_context->variableStackHeights.count(&var), "");
solAssert(!m_context->variableReferences.count(&var), ""); yulAssert(!m_context->variableReferences.count(&var), "");
m_stackAdjustment++; m_stackAdjustment++;
} }
else else
@ -775,20 +775,20 @@ void CodeTransform::finalizeBlock(Block const& _block, int blockStartStackHeight
} }
int deposit = m_assembly.stackHeight() - blockStartStackHeight; int deposit = m_assembly.stackHeight() - blockStartStackHeight;
solAssert(deposit == 0, "Invalid stack height at end of block: " + to_string(deposit)); yulAssert(deposit == 0, "Invalid stack height at end of block: " + to_string(deposit));
checkStackHeight(&_block); checkStackHeight(&_block);
} }
void CodeTransform::generateMultiAssignment(vector<Identifier> const& _variableNames) void CodeTransform::generateMultiAssignment(vector<Identifier> const& _variableNames)
{ {
solAssert(m_scope, ""); yulAssert(m_scope, "");
for (auto const& variableName: _variableNames | boost::adaptors::reversed) for (auto const& variableName: _variableNames | boost::adaptors::reversed)
generateAssignment(variableName); generateAssignment(variableName);
} }
void CodeTransform::generateAssignment(Identifier const& _variableName) void CodeTransform::generateAssignment(Identifier const& _variableName)
{ {
solAssert(m_scope, ""); yulAssert(m_scope, "");
if (auto var = m_scope->lookup(_variableName.name)) if (auto var = m_scope->lookup(_variableName.name))
{ {
Scope::Variable const& _var = std::get<Scope::Variable>(*var); Scope::Variable const& _var = std::get<Scope::Variable>(*var);
@ -799,7 +799,7 @@ void CodeTransform::generateAssignment(Identifier const& _variableName)
} }
else else
{ {
solAssert( yulAssert(
m_identifierAccess.generateCode, m_identifierAccess.generateCode,
"Identifier not found and no external access available." "Identifier not found and no external access available."
); );
@ -809,9 +809,9 @@ void CodeTransform::generateAssignment(Identifier const& _variableName)
int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap) int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap)
{ {
solAssert(m_context->variableStackHeights.count(&_var), ""); yulAssert(m_context->variableStackHeights.count(&_var), "");
int heightDiff = m_assembly.stackHeight() - m_context->variableStackHeights[&_var]; int heightDiff = m_assembly.stackHeight() - m_context->variableStackHeights[&_var];
solAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable."); yulAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable.");
int limit = _forSwap ? 17 : 16; int limit = _forSwap ? 17 : 16;
if (heightDiff > limit) if (heightDiff > limit)
{ {
@ -830,15 +830,15 @@ int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _va
void CodeTransform::expectDeposit(int _deposit, int _oldHeight) const void CodeTransform::expectDeposit(int _deposit, int _oldHeight) const
{ {
solAssert(m_assembly.stackHeight() == _oldHeight + _deposit, "Invalid stack deposit."); yulAssert(m_assembly.stackHeight() == _oldHeight + _deposit, "Invalid stack deposit.");
} }
void CodeTransform::checkStackHeight(void const* _astElement) const void CodeTransform::checkStackHeight(void const* _astElement) const
{ {
solAssert(m_info.stackHeightInfo.count(_astElement), "Stack height for AST element not found."); yulAssert(m_info.stackHeightInfo.count(_astElement), "Stack height for AST element not found.");
int stackHeightInAnalysis = m_info.stackHeightInfo.at(_astElement); int stackHeightInAnalysis = m_info.stackHeightInfo.at(_astElement);
int stackHeightInCodegen = m_assembly.stackHeight() - m_stackAdjustment; int stackHeightInCodegen = m_assembly.stackHeight() - m_stackAdjustment;
solAssert( yulAssert(
stackHeightInAnalysis == stackHeightInCodegen, stackHeightInAnalysis == stackHeightInCodegen,
"Stack height mismatch between analysis and code generation phase: Analysis: " + "Stack height mismatch between analysis and code generation phase: Analysis: " +
to_string(stackHeightInAnalysis) + to_string(stackHeightInAnalysis) +

View File

@ -19,11 +19,10 @@
*/ */
#include <libyul/backends/evm/NoOutputAssembly.h> #include <libyul/backends/evm/NoOutputAssembly.h>
#include <libyul/Exceptions.h>
#include <libevmasm/Instruction.h> #include <libevmasm/Instruction.h>
#include <liblangutil/Exceptions.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace langutil; using namespace langutil;
@ -47,7 +46,7 @@ void NoOutputAssembly::appendLabel(LabelID)
void NoOutputAssembly::appendLabelReference(LabelID) void NoOutputAssembly::appendLabelReference(LabelID)
{ {
solAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode."); yulAssert(!m_evm15, "Cannot use plain label references in EMV1.5 mode.");
appendInstruction(dev::eth::pushInstruction(1)); appendInstruction(dev::eth::pushInstruction(1));
} }
@ -63,12 +62,12 @@ AbstractAssembly::LabelID NoOutputAssembly::namedLabel(string const&)
void NoOutputAssembly::appendLinkerSymbol(string const&) void NoOutputAssembly::appendLinkerSymbol(string const&)
{ {
solAssert(false, "Linker symbols not yet implemented."); yulAssert(false, "Linker symbols not yet implemented.");
} }
void NoOutputAssembly::appendJump(int _stackDiffAfter) void NoOutputAssembly::appendJump(int _stackDiffAfter)
{ {
solAssert(!m_evm15, "Plain JUMP used for EVM 1.5"); yulAssert(!m_evm15, "Plain JUMP used for EVM 1.5");
appendInstruction(dev::eth::Instruction::JUMP); appendInstruction(dev::eth::Instruction::JUMP);
m_stackHeight += _stackDiffAfter; m_stackHeight += _stackDiffAfter;
} }
@ -97,22 +96,22 @@ void NoOutputAssembly::appendJumpToIf(LabelID _labelId)
void NoOutputAssembly::appendBeginsub(LabelID, int _arguments) void NoOutputAssembly::appendBeginsub(LabelID, int _arguments)
{ {
solAssert(m_evm15, "BEGINSUB used for EVM 1.0"); yulAssert(m_evm15, "BEGINSUB used for EVM 1.0");
solAssert(_arguments >= 0, ""); yulAssert(_arguments >= 0, "");
m_stackHeight += _arguments; m_stackHeight += _arguments;
} }
void NoOutputAssembly::appendJumpsub(LabelID, int _arguments, int _returns) void NoOutputAssembly::appendJumpsub(LabelID, int _arguments, int _returns)
{ {
solAssert(m_evm15, "JUMPSUB used for EVM 1.0"); yulAssert(m_evm15, "JUMPSUB used for EVM 1.0");
solAssert(_arguments >= 0 && _returns >= 0, ""); yulAssert(_arguments >= 0 && _returns >= 0, "");
m_stackHeight += _returns - _arguments; m_stackHeight += _returns - _arguments;
} }
void NoOutputAssembly::appendReturnsub(int _returns, int _stackDiffAfter) void NoOutputAssembly::appendReturnsub(int _returns, int _stackDiffAfter)
{ {
solAssert(m_evm15, "RETURNSUB used for EVM 1.0"); yulAssert(m_evm15, "RETURNSUB used for EVM 1.0");
solAssert(_returns >= 0, ""); yulAssert(_returns >= 0, "");
m_stackHeight += _stackDiffAfter - _returns; m_stackHeight += _stackDiffAfter - _returns;
} }
@ -123,7 +122,7 @@ void NoOutputAssembly::appendAssemblySize()
pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> NoOutputAssembly::createSubAssembly() pair<shared_ptr<AbstractAssembly>, AbstractAssembly::SubID> NoOutputAssembly::createSubAssembly()
{ {
solAssert(false, "Sub assemblies not implemented."); yulAssert(false, "Sub assemblies not implemented.");
return {}; return {};
} }

View File

@ -1199,7 +1199,9 @@ BOOST_AUTO_TEST_CASE(use_stack_optimization)
result = compiler.compile(parsedInput); result = compiler.compile(parsedInput);
BOOST_REQUIRE(result["errors"].isArray()); BOOST_REQUIRE(result["errors"].isArray());
BOOST_CHECK(result["errors"][0]["severity"] == "error"); BOOST_CHECK(result["errors"][0]["severity"] == "error");
BOOST_CHECK(result["errors"][0]["type"] == "InternalCompilerError"); BOOST_REQUIRE(result["errors"][0]["message"].isString());
BOOST_CHECK(result["errors"][0]["message"].asString().find("Stack too deep when compiling inline assembly") != std::string::npos);
BOOST_CHECK(result["errors"][0]["type"] == "YulException");
} }
BOOST_AUTO_TEST_CASE(standard_output_selection_wildcard) BOOST_AUTO_TEST_CASE(standard_output_selection_wildcard)