diff options
78 files changed, 1785 insertions, 578 deletions
diff --git a/Changelog.md b/Changelog.md index 6a366ca0..5bbf5af6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,8 +5,11 @@ Language Features: Compiler Features: * Inline Assembly: Improve error messages around invalid function argument count. + * Code Generator: Use codecopy for string constants more aggressively. * 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. * 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: diff --git a/docs/contributing.rst b/docs/contributing.rst index 43b2fd38..12dea7d1 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -61,7 +61,7 @@ New features and bugfixes should be added to the ``Changelog.md`` file: please follow the style of previous entries, when applicable. Finally, please make sure you respect the `coding style -<https://raw.githubusercontent.com/ethereum/solidity/develop/CODING_STYLE.md>`_ +<https://github.com/ethereum/solidity/blob/develop/CODING_STYLE.md>`_ for this project. Also, even though we do CI testing, please test your code and ensure that it builds locally before submitting a pull request. diff --git a/docs/miscellaneous.rst b/docs/miscellaneous.rst index 017d5b81..5a6f3875 100644 --- a/docs/miscellaneous.rst +++ b/docs/miscellaneous.rst @@ -224,9 +224,9 @@ for displaying the current position in the source code inside a debugger or for breakpoint handling. Both kinds of source mappings use integer identifiers to refer to source files. -These are regular array indices into a list of source files usually called -``"sourceList"``, which is part of the combined-json and the output of -the json / npm compiler. +The identifier of a source file is stored in +``output['sources'][sourceName]['id']`` where ``output`` is the output of the +standard-json compiler interface parsed as JSON. .. note :: In the case of instructions that are not associated with any particular source file, diff --git a/docs/using-the-compiler.rst b/docs/using-the-compiler.rst index 9ba6caa5..4749ef1f 100644 --- a/docs/using-the-compiler.rst +++ b/docs/using-the-compiler.rst @@ -200,15 +200,27 @@ Input Description "MyLib": "0x123123..." } } - // The following can be used to select desired outputs. - // If this field is omitted, then the compiler loads and does type checking, but will not generate any outputs apart from errors. - // The first level key is the file name and the second is the contract name, where empty contract name refers to the file itself, - // while the star refers to all of the contracts. + // The following can be used to select desired outputs based + // on file and contract names. + // If this field is omitted, then the compiler loads and does type checking, + // but will not generate any outputs apart from errors. + // The first level key is the file name and the second level key is the contract name. + // An empty contract name is used for outputs that are not tied to a contract + // but to the whole source file like the AST. + // A star as contract name refers to all contracts in the file. + // Similarly, a star as a file name matches all files. + // To select all outputs the compiler can possibly generate, use + // "outputSelection: { "*": { "*": [ "*" ], "": [ "*" ] } }" + // but note that this might slow down the compilation process needlessly. // // The available output types are as follows: - // abi - ABI + // + // File level (needs empty string as contract name): // ast - AST of all source files // legacyAST - legacy AST of all source files + // + // Contract level (needs the contract name or "*"): + // abi - ABI // devdoc - Developer documentation (natspec) // userdoc - User documentation (natspec) // metadata - Metadata @@ -281,7 +293,7 @@ Output Description // This contains the file-level outputs. In can be limited/filtered by the outputSelection settings. sources: { "sourceFile.sol": { - // Identifier (used in source maps) + // Identifier of the source (used in source maps) id: 1, // The AST object ast: {}, diff --git a/libdevcore/IndentedWriter.cpp b/libdevcore/IndentedWriter.cpp index 96aaf0fa..1a85957b 100644 --- a/libdevcore/IndentedWriter.cpp +++ b/libdevcore/IndentedWriter.cpp @@ -36,7 +36,7 @@ string IndentedWriter::format() const void IndentedWriter::newLine() { if (!m_lines.back().contents.empty()) - m_lines.push_back({ string(), m_lines.back().indentation }); + m_lines.emplace_back(Line{string(), m_lines.back().indentation}); } void IndentedWriter::indent() diff --git a/libdevcore/StringUtils.cpp b/libdevcore/StringUtils.cpp index 50bf7cce..196ac8f7 100644 --- a/libdevcore/StringUtils.cpp +++ b/libdevcore/StringUtils.cpp @@ -91,7 +91,7 @@ string dev::quotedAlternativesList(vector<string> const& suggestions) vector<string> quotedSuggestions; for (auto& suggestion: suggestions) - quotedSuggestions.push_back("\"" + suggestion + "\""); + quotedSuggestions.emplace_back("\"" + suggestion + "\""); return joinHumanReadable(quotedSuggestions, ", ", " or "); } diff --git a/libevmasm/Assembly.cpp b/libevmasm/Assembly.cpp index 29d9846d..231eed93 100644 --- a/libevmasm/Assembly.cpp +++ b/libevmasm/Assembly.cpp @@ -83,7 +83,7 @@ AssemblyItem const& Assembly::append(AssemblyItem const& _i) { assertThrow(m_deposit >= 0, AssemblyException, "Stack underflow."); m_deposit += _i.deposit(); - m_items.push_back(_i); + m_items.emplace_back(_i); if (m_items.back().location().isEmpty() && !m_currentSourceLocation.isEmpty()) m_items.back().setLocation(m_currentSourceLocation); return back(); @@ -353,14 +353,14 @@ AssemblyItem Assembly::namedTag(string const& _name) assertThrow(!_name.empty(), AssemblyException, "Empty named tag."); if (!m_namedTags.count(_name)) m_namedTags[_name] = size_t(newTag().data()); - return AssemblyItem(Tag, m_namedTags.at(_name)); + return AssemblyItem{Tag, m_namedTags.at(_name)}; } AssemblyItem Assembly::newPushLibraryAddress(string const& _identifier) { h256 h(dev::keccak256(_identifier)); m_libraries[h] = _identifier; - return AssemblyItem(PushLibraryAddress, h); + return AssemblyItem{PushLibraryAddress, h}; } Assembly& Assembly::optimise(bool _enable, EVMVersion _evmVersion, bool _isCreation, size_t _runs) @@ -415,14 +415,14 @@ map<u256, u256> Assembly::optimiseInternal( if (_settings.runJumpdestRemover) { - JumpdestRemover jumpdestOpt(m_items); + JumpdestRemover jumpdestOpt{m_items}; if (jumpdestOpt.optimise(_tagsReferencedFromOutside)) count++; } if (_settings.runPeephole) { - PeepholeOptimiser peepOpt(m_items); + PeepholeOptimiser peepOpt{m_items}; while (peepOpt.optimise()) { count++; @@ -433,7 +433,7 @@ map<u256, u256> Assembly::optimiseInternal( // This only modifies PushTags, we have to run again to actually remove code. if (_settings.runDeduplicate) { - BlockDeduplicator dedup(m_items); + BlockDeduplicator dedup{m_items}; if (dedup.deduplicate()) { tagReplacements.insert(dedup.replacedTags().begin(), dedup.replacedTags().end()); @@ -448,13 +448,13 @@ map<u256, u256> Assembly::optimiseInternal( // function types that can be stored in storage. AssemblyItems optimisedItems; - bool usesMSize = (find(m_items.begin(), m_items.end(), AssemblyItem(Instruction::MSIZE)) != m_items.end()); + bool usesMSize = (find(m_items.begin(), m_items.end(), AssemblyItem{Instruction::MSIZE}) != m_items.end()); auto iter = m_items.begin(); while (iter != m_items.end()) { KnownState emptyState; - CommonSubexpressionEliminator eliminator(emptyState); + CommonSubexpressionEliminator eliminator{emptyState}; auto orig = iter; iter = eliminator.feedItems(iter, m_items.end(), usesMSize); bool shouldReplace = false; diff --git a/libevmasm/BlockDeduplicator.cpp b/libevmasm/BlockDeduplicator.cpp index b7c69531..ca439925 100644 --- a/libevmasm/BlockDeduplicator.cpp +++ b/libevmasm/BlockDeduplicator.cpp @@ -41,7 +41,7 @@ bool BlockDeduplicator::deduplicate() // Virtual tag that signifies "the current block" and which is used to optimise loops. // We abort if this virtual tag actually exists. - AssemblyItem pushSelf(PushTag, u256(-4)); + AssemblyItem pushSelf{PushTag, u256(-4)}; if ( std::count(m_items.cbegin(), m_items.cend(), pushSelf.tag()) || std::count(m_items.cbegin(), m_items.cend(), pushSelf.pushTag()) @@ -55,17 +55,17 @@ bool BlockDeduplicator::deduplicate() // To compare recursive loops, we have to already unify PushTag opcodes of the // block's own tag. - AssemblyItem pushFirstTag(pushSelf); - AssemblyItem pushSecondTag(pushSelf); + AssemblyItem pushFirstTag{pushSelf}; + AssemblyItem pushSecondTag{pushSelf}; if (_i < m_items.size() && m_items.at(_i).type() == Tag) pushFirstTag = m_items.at(_i).pushTag(); if (_j < m_items.size() && m_items.at(_j).type() == Tag) pushSecondTag = m_items.at(_j).pushTag(); - BlockIterator first(m_items.begin() + _i, m_items.end(), &pushFirstTag, &pushSelf); - BlockIterator second(m_items.begin() + _j, m_items.end(), &pushSecondTag, &pushSelf); - BlockIterator end(m_items.end(), m_items.end()); + BlockIterator first{m_items.begin() + _i, m_items.end(), &pushFirstTag, &pushSelf}; + BlockIterator second{m_items.begin() + _j, m_items.end(), &pushSecondTag, &pushSelf}; + BlockIterator end{m_items.end(), m_items.end()}; if (first != end && (*first).type() == Tag) ++first; @@ -126,7 +126,7 @@ BlockDeduplicator::BlockIterator& BlockDeduplicator::BlockIterator::operator++() { if (it == end) return *this; - if (SemanticInformation::altersControlFlow(*it) && *it != AssemblyItem(Instruction::JUMPI)) + if (SemanticInformation::altersControlFlow(*it) && *it != AssemblyItem{Instruction::JUMPI}) it = end; else { diff --git a/libevmasm/ControlFlowGraph.cpp b/libevmasm/ControlFlowGraph.cpp index d62f5436..e82c2903 100644 --- a/libevmasm/ControlFlowGraph.cpp +++ b/libevmasm/ControlFlowGraph.cpp @@ -87,7 +87,7 @@ void ControlFlowGraph::splitBlocks() m_blocks[id].begin = index; } if (item.type() == PushTag) - m_blocks[id].pushedTags.push_back(BlockId(item.data())); + m_blocks[id].pushedTags.emplace_back(item.data()); if (SemanticInformation::altersControlFlow(item)) { m_blocks[id].end = index + 1; diff --git a/libevmasm/SimplificationRules.cpp b/libevmasm/SimplificationRules.cpp index 1dce5f1e..b812cecc 100644 --- a/libevmasm/SimplificationRules.cpp +++ b/libevmasm/SimplificationRules.cpp @@ -209,7 +209,7 @@ ExpressionTemplate::ExpressionTemplate(Pattern const& _pattern, SourceLocation c item = _pattern.toAssemblyItem(_location); } for (auto const& arg: _pattern.arguments()) - arguments.push_back(ExpressionTemplate(arg, _location)); + arguments.emplace_back(arg, _location); } string ExpressionTemplate::toString() const diff --git a/liblangutil/Exceptions.h b/liblangutil/Exceptions.h index 5ad31ab2..22deb058 100644 --- a/liblangutil/Exceptions.h +++ b/liblangutil/Exceptions.h @@ -105,7 +105,7 @@ class SecondarySourceLocation public: SecondarySourceLocation& append(std::string const& _errMsg, SourceLocation const& _sourceLocation) { - infos.push_back(std::make_pair(_errMsg, _sourceLocation)); + infos.emplace_back(_errMsg, _sourceLocation); return *this; } diff --git a/liblll/CodeFragment.cpp b/liblll/CodeFragment.cpp index b32f14e9..63d8da3d 100644 --- a/liblll/CodeFragment.cpp +++ b/liblll/CodeFragment.cpp @@ -353,7 +353,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) if (j.tag() || j.which() != sp::utree_type::symbol_type) error<InvalidMacroArgs>(); auto sr = j.get<sp::basic_string<boost::iterator_range<char const*>, sp::utree_type::symbol_type>>(); - args.push_back(string(sr.begin(), sr.end())); + args.emplace_back(sr.begin(), sr.end()); } else if (ii == 3) { @@ -464,9 +464,9 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s) if (c++) { if (us == "LLL" && c == 1) - code.push_back(CodeFragment(i, ns, m_readFile)); + code.emplace_back(i, ns, m_readFile); else - code.push_back(CodeFragment(i, _s, m_readFile)); + code.emplace_back(i, _s, m_readFile); } auto requireSize = [&](unsigned s) { if (code.size() != s) error<IncorrectParameterCount>(us); }; auto requireMinSize = [&](unsigned s) { if (code.size() < s) error<IncorrectParameterCount>(us); }; diff --git a/liblll/Compiler.cpp b/liblll/Compiler.cpp index f944adbd..6296cbcf 100644 --- a/liblll/Compiler.cpp +++ b/liblll/Compiler.cpp @@ -46,22 +46,22 @@ bytes dev::lll::compileLLL(string const& _src, dev::solidity::EVMVersion _evmVer { if (_errors) { - _errors->push_back("Parse error."); - _errors->push_back(boost::diagnostic_information(_e)); + _errors->emplace_back("Parse error."); + _errors->emplace_back(boost::diagnostic_information(_e)); } } catch (std::exception const& _e) { if (_errors) { - _errors->push_back("Parse exception."); - _errors->push_back(boost::diagnostic_information(_e)); + _errors->emplace_back("Parse exception."); + _errors->emplace_back(boost::diagnostic_information(_e)); } } catch (...) { if (_errors) - _errors->push_back("Internal compiler exception."); + _errors->emplace_back("Internal compiler exception."); } return bytes(); } @@ -84,22 +84,22 @@ std::string dev::lll::compileLLLToAsm(std::string const& _src, EVMVersion _evmVe { if (_errors) { - _errors->push_back("Parse error."); - _errors->push_back(boost::diagnostic_information(_e)); + _errors->emplace_back("Parse error."); + _errors->emplace_back(boost::diagnostic_information(_e)); } } catch (std::exception const& _e) { if (_errors) { - _errors->push_back("Parse exception."); - _errors->push_back(boost::diagnostic_information(_e)); + _errors->emplace_back("Parse exception."); + _errors->emplace_back(boost::diagnostic_information(_e)); } } catch (...) { if (_errors) - _errors->push_back("Internal compiler exception."); + _errors->emplace_back("Internal compiler exception."); } return string(); } diff --git a/libsolidity/analysis/ContractLevelChecker.cpp b/libsolidity/analysis/ContractLevelChecker.cpp index 6dc564de..526caff9 100644 --- a/libsolidity/analysis/ContractLevelChecker.cpp +++ b/libsolidity/analysis/ContractLevelChecker.cpp @@ -227,7 +227,7 @@ void ContractLevelChecker::checkAbstractFunctions(ContractDefinition const& _con return _type->hasEqualParameterTypes(*_funAndFlag.first); }); if (it == overloads.end()) - overloads.push_back(make_pair(_type, _implemented)); + overloads.emplace_back(_type, _implemented); else if (it->second) { if (!_implemented) @@ -409,8 +409,8 @@ void ContractLevelChecker::checkExternalTypeClashes(ContractDefinition const& _c auto functionType = make_shared<FunctionType>(*f); // under non error circumstances this should be true if (functionType->interfaceFunctionType()) - externalDeclarations[functionType->externalSignature()].push_back( - make_pair(f, functionType->asCallableFunction(false)) + externalDeclarations[functionType->externalSignature()].emplace_back( + f, functionType->asCallableFunction(false) ); } for (VariableDeclaration const* v: contract->stateVariables()) @@ -419,8 +419,8 @@ void ContractLevelChecker::checkExternalTypeClashes(ContractDefinition const& _c auto functionType = make_shared<FunctionType>(*v); // under non error circumstances this should be true if (functionType->interfaceFunctionType()) - externalDeclarations[functionType->externalSignature()].push_back( - make_pair(v, functionType->asCallableFunction(false)) + externalDeclarations[functionType->externalSignature()].emplace_back( + v, functionType->asCallableFunction(false) ); } } diff --git a/libsolidity/analysis/ReferencesResolver.cpp b/libsolidity/analysis/ReferencesResolver.cpp index ac88a052..76641c04 100644 --- a/libsolidity/analysis/ReferencesResolver.cpp +++ b/libsolidity/analysis/ReferencesResolver.cpp @@ -27,6 +27,7 @@ #include <libyul/AsmAnalysis.h> #include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmData.h> +#include <libyul/backends/evm/EVMDialect.h> #include <liblangutil/ErrorReporter.h> #include <liblangutil/Exceptions.h> @@ -321,7 +322,7 @@ bool ReferencesResolver::visit(InlineAssembly const& _inlineAssembly) errorsIgnored, EVMVersion(), errorTypeForLoose, - yul::Dialect::looseAssemblyForEVM(), + yul::EVMDialect::looseAssemblyForEVM(), resolver ).analyze(_inlineAssembly.operations()); return false; diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 1c9f1956..5bd96f8d 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -27,6 +27,8 @@ #include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmData.h> +#include <libyul/backends/evm/EVMDialect.h> + #include <liblangutil/ErrorReporter.h> #include <libdevcore/Algorithms.h> @@ -658,7 +660,7 @@ bool TypeChecker::visit(InlineAssembly const& _inlineAssembly) m_errorReporter, m_evmVersion, Error::Type::SyntaxError, - yul::Dialect::looseAssemblyForEVM(), + yul::EVMDialect::looseAssemblyForEVM(), identifierAccess ); if (!analyzer.analyze(_inlineAssembly.operations())) @@ -935,30 +937,32 @@ bool TypeChecker::visit(VariableDeclarationStatement const& _statement) var.accept(*this); if (!valueComponentType->isImplicitlyConvertibleTo(*var.annotation().type)) { + auto errorMsg = "Type " + + valueComponentType->toString() + + " is not implicitly convertible to expected type " + + var.annotation().type->toString(); if ( valueComponentType->category() == Type::Category::RationalNumber && dynamic_cast<RationalNumberType const&>(*valueComponentType).isFractional() && valueComponentType->mobileType() ) - m_errorReporter.typeError( - _statement.location(), - "Type " + - valueComponentType->toString() + - " is not implicitly convertible to expected type " + - var.annotation().type->toString() + - ". Try converting to type " + - valueComponentType->mobileType()->toString() + - " or use an explicit conversion." - ); + { + if (var.annotation().type->operator==(*valueComponentType->mobileType())) + m_errorReporter.typeError( + _statement.location(), + errorMsg + ", but it can be explicitly converted." + ); + else + m_errorReporter.typeError( + _statement.location(), + errorMsg + + ". Try converting to type " + + valueComponentType->mobileType()->toString() + + " or use an explicit conversion." + ); + } else - m_errorReporter.typeError( - _statement.location(), - "Type " + - valueComponentType->toString() + - " is not implicitly convertible to expected type " + - var.annotation().type->toString() + - "." - ); + m_errorReporter.typeError(_statement.location(), errorMsg + "."); } } } @@ -2331,30 +2335,32 @@ bool TypeChecker::expectType(Expression const& _expression, Type const& _expecte _expression.accept(*this); if (!type(_expression)->isImplicitlyConvertibleTo(_expectedType)) { + auto errorMsg = "Type " + + type(_expression)->toString() + + " is not implicitly convertible to expected type " + + _expectedType.toString(); if ( type(_expression)->category() == Type::Category::RationalNumber && dynamic_pointer_cast<RationalNumberType const>(type(_expression))->isFractional() && type(_expression)->mobileType() ) - m_errorReporter.typeError( - _expression.location(), - "Type " + - type(_expression)->toString() + - " is not implicitly convertible to expected type " + - _expectedType.toString() + - ". Try converting to type " + - type(_expression)->mobileType()->toString() + - " or use an explicit conversion." - ); + { + if (_expectedType.operator==(*type(_expression)->mobileType())) + m_errorReporter.typeError( + _expression.location(), + errorMsg + ", but it can be explicitly converted." + ); + else + m_errorReporter.typeError( + _expression.location(), + errorMsg + + ". Try converting to type " + + type(_expression)->mobileType()->toString() + + " or use an explicit conversion." + ); + } else - m_errorReporter.typeError( - _expression.location(), - "Type " + - type(_expression)->toString() + - " is not implicitly convertible to expected type " + - _expectedType.toString() + - "." - ); + m_errorReporter.typeError(_expression.location(), errorMsg + "."); return false; } return true; diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 3ae6bd6d..5779e4ad 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -198,7 +198,7 @@ vector<pair<FixedHash<4>, FunctionTypePointer>> const& ContractDefinition::inter { signaturesSeen.insert(functionSignature); FixedHash<4> hash(dev::keccak256(functionSignature)); - m_interfaceFunctionList->push_back(make_pair(hash, fun)); + m_interfaceFunctionList->emplace_back(hash, fun); } } } diff --git a/libsolidity/ast/ASTJsonConverter.cpp b/libsolidity/ast/ASTJsonConverter.cpp index e92134a8..1a2b3345 100644 --- a/libsolidity/ast/ASTJsonConverter.cpp +++ b/libsolidity/ast/ASTJsonConverter.cpp @@ -234,7 +234,7 @@ bool ASTJsonConverter::visit(ImportDirective const& _node) make_pair(m_legacy ? "SourceUnit" : "sourceUnit", nodeId(*_node.annotation().sourceUnit)), make_pair("scope", idOrNull(_node.scope())) }; - attributes.push_back(make_pair("unitAlias", _node.name())); + attributes.emplace_back("unitAlias", _node.name()); Json::Value symbolAliases(Json::arrayValue); for (auto const& symbolAlias: _node.symbolAliases()) { @@ -244,7 +244,7 @@ bool ASTJsonConverter::visit(ImportDirective const& _node) tuple["local"] = symbolAlias.second ? Json::Value(*symbolAlias.second) : Json::nullValue; symbolAliases.append(tuple); } - attributes.push_back(make_pair("symbolAliases", std::move(symbolAliases))); + attributes.emplace_back("symbolAliases", std::move(symbolAliases)); setJsonNode(_node, "ImportDirective", std::move(attributes)); return false; } @@ -357,7 +357,7 @@ bool ASTJsonConverter::visit(VariableDeclaration const& _node) make_pair("typeDescriptions", typePointerToJson(_node.annotation().type, true)) }; if (m_inEvent) - attributes.push_back(make_pair("indexed", _node.isIndexed())); + attributes.emplace_back("indexed", _node.isIndexed()); setJsonNode(_node, "VariableDeclaration", std::move(attributes)); return false; } @@ -647,11 +647,11 @@ bool ASTJsonConverter::visit(FunctionCall const& _node) }; if (m_legacy) { - attributes.push_back(make_pair("isStructConstructorCall", _node.annotation().kind == FunctionCallKind::StructConstructorCall)); - attributes.push_back(make_pair("type_conversion", _node.annotation().kind == FunctionCallKind::TypeConversion)); + attributes.emplace_back("isStructConstructorCall", _node.annotation().kind == FunctionCallKind::StructConstructorCall); + attributes.emplace_back("type_conversion", _node.annotation().kind == FunctionCallKind::TypeConversion); } else - attributes.push_back(make_pair("kind", functionCallKind(_node.annotation().kind))); + attributes.emplace_back("kind", functionCallKind(_node.annotation().kind)); appendExpressionAttributes(attributes, _node.annotation()); setJsonNode(_node, "FunctionCall", std::move(attributes)); return false; diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index f5a38747..c35dde3c 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -125,6 +125,22 @@ bool fitsPrecisionBase2(bigint const& _mantissa, uint32_t _expBase2) return fitsPrecisionBaseX(_mantissa, 1.0, _expBase2); } +/// Checks whether _value fits into IntegerType _type. +bool fitsIntegerType(bigint const& _value, IntegerType const& _type) +{ + return (_type.minValue() <= _value) && (_value <= _type.maxValue()); +} + +/// Checks whether _value fits into _bits bits when having 1 bit as the sign bit +/// if _signed is true. +bool fitsIntoBits(bigint const& _value, unsigned _bits, bool _signed) +{ + return fitsIntegerType(_value, IntegerType( + _bits, + _signed ? IntegerType::Modifier::Signed : IntegerType::Modifier::Unsigned + )); +} + } void StorageOffsets::computeOffsets(TypePointers const& _types) @@ -446,7 +462,7 @@ MemberList::MemberMap Type::boundFunctions(Type const& _type, ContractDefinition continue; FunctionTypePointer fun = FunctionType(*function, false).asCallableFunction(true, true); if (_type.isImplicitlyConvertibleTo(*fun->selfType())) - members.push_back(MemberList::Member(function->name(), fun, function)); + members.emplace_back(function->name(), fun, function); } } return members; @@ -963,27 +979,21 @@ BoolResult RationalNumberType::isImplicitlyConvertibleTo(Type const& _convertTo) if (isFractional()) return false; IntegerType const& targetType = dynamic_cast<IntegerType const&>(_convertTo); - if (m_value == rational(0)) - return true; - unsigned forSignBit = (targetType.isSigned() ? 1 : 0); - if (m_value > rational(0)) - { - if (m_value.numerator() <= (u256(-1) >> (256 - targetType.numBits() + forSignBit))) - return true; - return false; - } - if (targetType.isSigned()) - { - if (-m_value.numerator() <= (u256(1) << (targetType.numBits() - forSignBit))) - return true; - } - return false; + return fitsIntegerType(m_value.numerator(), targetType); } case Category::FixedPoint: { - if (auto fixed = fixedPointType()) - return fixed->isImplicitlyConvertibleTo(_convertTo); - return false; + FixedPointType const& targetType = dynamic_cast<FixedPointType const&>(_convertTo); + // Store a negative number into an unsigned. + if (isNegative() && !targetType.isSigned()) + return false; + if (!isFractional()) + return (targetType.minIntegerValue() <= m_value) && (m_value <= targetType.maxIntegerValue()); + rational value = m_value * pow(bigint(10), targetType.fractionalDigits()); + // Need explicit conversion since truncation will occur. + if (value.denominator() != 1) + return false; + return fitsIntoBits(value.numerator(), targetType.numBits(), targetType.isSigned()); } case Category::FixedBytes: return (m_value == rational(0)) || (m_compatibleBytesType && *m_compatibleBytesType == _convertTo); @@ -1811,23 +1821,23 @@ MemberList::MemberMap ArrayType::nativeMembers(ContractDefinition const*) const MemberList::MemberMap members; if (!isString()) { - members.push_back({"length", make_shared<IntegerType>(256)}); + members.emplace_back("length", make_shared<IntegerType>(256)); if (isDynamicallySized() && location() == DataLocation::Storage) { - members.push_back({"push", make_shared<FunctionType>( + members.emplace_back("push", make_shared<FunctionType>( TypePointers{baseType()}, TypePointers{make_shared<IntegerType>(256)}, strings{string()}, strings{string()}, isByteArray() ? FunctionType::Kind::ByteArrayPush : FunctionType::Kind::ArrayPush - )}); - members.push_back({"pop", make_shared<FunctionType>( + )); + members.emplace_back("pop", make_shared<FunctionType>( TypePointers{}, TypePointers{}, strings{string()}, strings{string()}, FunctionType::Kind::ArrayPop - )}); + )); } } return members; @@ -1956,21 +1966,17 @@ MemberList::MemberMap ContractType::nativeMembers(ContractDefinition const* _con break; } if (!functionWithEqualArgumentsFound) - members.push_back(MemberList::Member( - function->name(), - functionType, - function - )); + members.emplace_back(function->name(), functionType, function); } } else if (!m_contract.isLibrary()) { for (auto const& it: m_contract.interfaceFunctions()) - members.push_back(MemberList::Member( + members.emplace_back( it.second->declaration().name(), it.second->asCallableFunction(m_contract.isLibrary()), &it.second->declaration() - )); + ); } return members; } @@ -1998,7 +2004,7 @@ vector<tuple<VariableDeclaration const*, u256, unsigned>> ContractType::stateVar vector<tuple<VariableDeclaration const*, u256, unsigned>> variablesAndOffsets; for (size_t index = 0; index < variables.size(); ++index) if (auto const* offset = offsets.offset(index)) - variablesAndOffsets.push_back(make_tuple(variables[index], offset->first, offset->second)); + variablesAndOffsets.emplace_back(variables[index], offset->first, offset->second); return variablesAndOffsets; } @@ -2088,10 +2094,10 @@ MemberList::MemberMap StructType::nativeMembers(ContractDefinition const*) const // Skip all mapping members if we are not in storage. if (location() != DataLocation::Storage && !type->canLiveOutsideStorage()) continue; - members.push_back(MemberList::Member( + members.emplace_back( variable->name(), copyForLocationIfReference(type), - variable.get()) + variable.get() ); } return members; @@ -2428,7 +2434,7 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl): if (auto mappingType = dynamic_cast<MappingType const*>(returnType.get())) { m_parameterTypes.push_back(mappingType->keyType()); - m_parameterNames.push_back(""); + m_parameterNames.emplace_back(""); returnType = mappingType->valueType(); } else if (auto arrayType = dynamic_cast<ArrayType const*>(returnType.get())) @@ -2437,7 +2443,7 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl): // Return byte arrays as whole. break; returnType = arrayType->baseType(); - m_parameterNames.push_back(""); + m_parameterNames.emplace_back(""); m_parameterTypes.push_back(make_shared<IntegerType>(256)); } else @@ -2468,7 +2474,7 @@ FunctionType::FunctionType(VariableDeclaration const& _varDecl): DataLocation::Memory, returnType )); - m_returnParameterNames.push_back(""); + m_returnParameterNames.emplace_back(""); } } @@ -2837,14 +2843,11 @@ MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) con { MemberList::MemberMap members; if (m_kind == Kind::External) - members.push_back(MemberList::Member( - "selector", - make_shared<FixedBytesType>(4) - )); + members.emplace_back("selector", make_shared<FixedBytesType>(4)); if (m_kind != Kind::BareDelegateCall) { if (isPayable()) - members.push_back(MemberList::Member( + members.emplace_back( "value", make_shared<FunctionType>( parseElementaryTypeVector({"uint"}), @@ -2858,10 +2861,10 @@ MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) con m_gasSet, m_valueSet ) - )); + ); } if (m_kind != Kind::Creation) - members.push_back(MemberList::Member( + members.emplace_back( "gas", make_shared<FunctionType>( parseElementaryTypeVector({"uint"}), @@ -2875,7 +2878,7 @@ MemberList::MemberMap FunctionType::nativeMembers(ContractDefinition const*) con m_gasSet, m_valueSet ) - )); + ); return members; } default: @@ -3203,24 +3206,24 @@ MemberList::MemberMap TypeType::nativeMembers(ContractDefinition const* _current if (contract.isLibrary()) for (FunctionDefinition const* function: contract.definedFunctions()) if (function->isVisibleAsLibraryMember()) - members.push_back(MemberList::Member( + members.emplace_back( function->name(), FunctionType(*function).asCallableFunction(true), function - )); + ); if (isBase) { // We are accessing the type of a base contract, so add all public and protected // members. Note that this does not add inherited functions on purpose. for (Declaration const* decl: contract.inheritableMembers()) - members.push_back(MemberList::Member(decl->name(), decl->type(), decl)); + members.emplace_back(decl->name(), decl->type(), decl); } else { for (auto const& stru: contract.definedStructs()) - members.push_back(MemberList::Member(stru->name(), stru->type(), stru)); + members.emplace_back(stru->name(), stru->type(), stru); for (auto const& enu: contract.definedEnums()) - members.push_back(MemberList::Member(enu->name(), enu->type(), enu)); + members.emplace_back(enu->name(), enu->type(), enu); } } else if (m_actualType->category() == Category::Enum) @@ -3228,7 +3231,7 @@ MemberList::MemberMap TypeType::nativeMembers(ContractDefinition const* _current EnumDefinition const& enumDef = dynamic_cast<EnumType const&>(*m_actualType).enumDefinition(); auto enumType = make_shared<EnumType>(enumDef); for (ASTPointer<EnumValue> const& enumValue: enumDef.members()) - members.push_back(MemberList::Member(enumValue->name(), enumType)); + members.emplace_back(enumValue->name(), enumType); } return members; } @@ -3293,7 +3296,7 @@ MemberList::MemberMap ModuleType::nativeMembers(ContractDefinition const*) const MemberList::MemberMap symbols; for (auto const& symbolName: m_sourceUnit.annotation().exportedSymbols) for (Declaration const* symbol: symbolName.second) - symbols.push_back(MemberList::Member(symbolName.first, symbol->type(), symbol)); + symbols.emplace_back(symbolName.first, symbol->type(), symbol); return symbols; } diff --git a/libsolidity/codegen/ABIFunctions.cpp b/libsolidity/codegen/ABIFunctions.cpp index b02623de..5000131c 100644 --- a/libsolidity/codegen/ABIFunctions.cpp +++ b/libsolidity/codegen/ABIFunctions.cpp @@ -141,8 +141,8 @@ string ABIFunctions::tupleDecoder(TypePointers const& _types, bool _fromMemory) vector<string> valueNamesLocal; for (size_t j = 0; j < sizeOnStack; j++) { - valueNamesLocal.push_back("value" + to_string(stackPos)); - valueReturnParams.push_back("value" + to_string(stackPos)); + valueNamesLocal.emplace_back("value" + to_string(stackPos)); + valueReturnParams.emplace_back("value" + to_string(stackPos)); stackPos++; } bool dynamic = decodingTypes[i]->isDynamicallyEncoded(); diff --git a/libsolidity/codegen/AsmCodeGen.cpp b/libsolidity/codegen/AsmCodeGen.cpp index dfcc900b..c04c1c34 100644 --- a/libsolidity/codegen/AsmCodeGen.cpp +++ b/libsolidity/codegen/AsmCodeGen.cpp @@ -179,14 +179,17 @@ void CodeGenerator::assemble( AsmAnalysisInfo& _analysisInfo, eth::Assembly& _assembly, ExternalIdentifierAccess const& _identifierAccess, - bool _useNamedLabelsForFunctions + bool _useNamedLabelsForFunctions, + bool _optimize ) { EthAssemblyAdapter assemblyAdapter(_assembly); CodeTransform( assemblyAdapter, _analysisInfo, - false, + _parsedData, + *EVMDialect::strictAssemblyForEVM(), + _optimize, false, _identifierAccess, _useNamedLabelsForFunctions diff --git a/libsolidity/codegen/AsmCodeGen.h b/libsolidity/codegen/AsmCodeGen.h index 4c6d97f4..303d32ee 100644 --- a/libsolidity/codegen/AsmCodeGen.h +++ b/libsolidity/codegen/AsmCodeGen.h @@ -85,7 +85,8 @@ public: yul::AsmAnalysisInfo& _analysisInfo, dev::eth::Assembly& _assembly, yul::ExternalIdentifierAccess const& _identifierAccess = yul::ExternalIdentifierAccess(), - bool _useNamedLabelsForFunctions = false + bool _useNamedLabelsForFunctions = false, + bool _optimize = false ); }; diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index dac09c2e..0b018e1e 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -31,6 +31,7 @@ #include <libyul/AsmAnalysis.h> #include <libyul/AsmAnalysisInfo.h> #include <libyul/YulString.h> +#include <libyul/backends/evm/EVMDialect.h> #include <liblangutil/ErrorReporter.h> #include <liblangutil/Scanner.h> @@ -361,7 +362,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::Dialect::strictAssemblyForEVM()).parse(scanner, false); + auto parserResult = yul::Parser(errorReporter, yul::EVMDialect::strictAssemblyForEVM()).parse(scanner, false); #ifdef SOL_OUTPUT_ASM cout << yul::AsmPrinter()(*parserResult) << endl; #endif @@ -373,7 +374,7 @@ void CompilerContext::appendInlineAssembly( errorReporter, m_evmVersion, boost::none, - yul::Dialect::strictAssemblyForEVM(), + yul::EVMDialect::strictAssemblyForEVM(), identifierAccess.resolve ).analyze(*parserResult); if (!parserResult || !errorReporter.errors().empty() || !analyzerResult) diff --git a/libsolidity/codegen/CompilerUtils.cpp b/libsolidity/codegen/CompilerUtils.cpp index 7d2ad9d2..259f1620 100644 --- a/libsolidity/codegen/CompilerUtils.cpp +++ b/libsolidity/codegen/CompilerUtils.cpp @@ -1205,7 +1205,7 @@ void CompilerUtils::storeStringData(bytesConstRef _data) { //@todo provide both alternatives to the optimiser // stack: mempos - if (_data.size() <= 128) + if (_data.size() <= 32) { for (unsigned i = 0; i < _data.size(); i += 32) { diff --git a/libsolidity/codegen/ContractCompiler.cpp b/libsolidity/codegen/ContractCompiler.cpp index 79c53a1c..67876721 100644 --- a/libsolidity/codegen/ContractCompiler.cpp +++ b/libsolidity/codegen/ContractCompiler.cpp @@ -721,14 +721,14 @@ bool ContractCompiler::visit(WhileStatement const& _whileStatement) eth::AssemblyItem loopStart = m_context.newTag(); eth::AssemblyItem loopEnd = m_context.newTag(); - m_breakTags.push_back({loopEnd, m_context.stackHeight()}); + m_breakTags.emplace_back(loopEnd, m_context.stackHeight()); m_context << loopStart; if (_whileStatement.isDoWhile()) { eth::AssemblyItem condition = m_context.newTag(); - m_continueTags.push_back({condition, m_context.stackHeight()}); + m_continueTags.emplace_back(condition, m_context.stackHeight()); _whileStatement.body().accept(*this); @@ -739,7 +739,7 @@ bool ContractCompiler::visit(WhileStatement const& _whileStatement) } else { - m_continueTags.push_back({loopStart, m_context.stackHeight()}); + m_continueTags.emplace_back(loopStart, m_context.stackHeight()); compileExpression(_whileStatement.condition()); m_context << Instruction::ISZERO; m_context.appendConditionalJumpTo(loopEnd); @@ -770,8 +770,8 @@ bool ContractCompiler::visit(ForStatement const& _forStatement) if (_forStatement.initializationExpression()) _forStatement.initializationExpression()->accept(*this); - m_breakTags.push_back({loopEnd, m_context.stackHeight()}); - m_continueTags.push_back({loopNext, m_context.stackHeight()}); + m_breakTags.emplace_back(loopEnd, m_context.stackHeight()); + m_continueTags.emplace_back(loopNext, m_context.stackHeight()); m_context << loopStart; // if there is no terminating condition in for, default is to always be true @@ -997,7 +997,7 @@ void ContractCompiler::appendModifierOrFunctionCode() if (codeBlock) { - m_returnTags.push_back({m_context.newTag(), m_context.stackHeight()}); + m_returnTags.emplace_back(m_context.newTag(), m_context.stackHeight()); codeBlock->accept(*this); solAssert(!m_returnTags.empty(), ""); diff --git a/libsolidity/formal/SMTChecker.cpp b/libsolidity/formal/SMTChecker.cpp index 17dc11ac..dc14f4c2 100644 --- a/libsolidity/formal/SMTChecker.cpp +++ b/libsolidity/formal/SMTChecker.cpp @@ -90,8 +90,7 @@ bool SMTChecker::visit(FunctionDefinition const& _function) m_interface->reset(); m_pathConditions.clear(); m_expressions.clear(); - m_specialVariables.clear(); - m_uninterpretedFunctions.clear(); + m_globalContext.clear(); m_uninterpretedTerms.clear(); resetStateVariables(); initializeLocalVariables(_function); @@ -268,16 +267,9 @@ void SMTChecker::endVisit(Assignment const& _assignment) else if (Identifier const* identifier = dynamic_cast<Identifier const*>(&_assignment.leftHandSide())) { VariableDeclaration const& decl = dynamic_cast<VariableDeclaration const&>(*identifier->annotation().referencedDeclaration); - if (knownVariable(decl)) - { - assignment(decl, _assignment.rightHandSide(), _assignment.location()); - defineExpr(_assignment, expr(_assignment.rightHandSide())); - } - else - m_errorReporter.warning( - _assignment.location(), - "Assertion checker does not yet implement such assignments." - ); + solAssert(knownVariable(decl), ""); + assignment(decl, _assignment.rightHandSide(), _assignment.location()); + defineExpr(_assignment, expr(_assignment.rightHandSide())); } else m_errorReporter.warning( @@ -288,7 +280,11 @@ void SMTChecker::endVisit(Assignment const& _assignment) void SMTChecker::endVisit(TupleExpression const& _tuple) { - if (_tuple.isInlineArray() || _tuple.components().size() != 1) + if ( + _tuple.isInlineArray() || + _tuple.components().size() != 1 || + !isSupportedType(_tuple.components()[0]->annotation().type->category()) + ) m_errorReporter.warning( _tuple.location(), "Assertion checker does not yet implement tuples and inline arrays." @@ -399,18 +395,30 @@ void SMTChecker::endVisit(FunctionCall const& _funCall) FunctionType const& funType = dynamic_cast<FunctionType const&>(*_funCall.expression().annotation().type); std::vector<ASTPointer<Expression const>> const args = _funCall.arguments(); - if (funType.kind() == FunctionType::Kind::Assert) + switch (funType.kind()) + { + case FunctionType::Kind::Assert: visitAssert(_funCall); - else if (funType.kind() == FunctionType::Kind::Require) + break; + case FunctionType::Kind::Require: visitRequire(_funCall); - else if (funType.kind() == FunctionType::Kind::GasLeft) + break; + case FunctionType::Kind::GasLeft: visitGasLeft(_funCall); - else if (funType.kind() == FunctionType::Kind::BlockHash) - visitBlockHash(_funCall); - else if (funType.kind() == FunctionType::Kind::Internal) + break; + case FunctionType::Kind::Internal: inlineFunctionCall(_funCall); - else - { + break; + case FunctionType::Kind::KECCAK256: + case FunctionType::Kind::ECRecover: + case FunctionType::Kind::SHA256: + case FunctionType::Kind::RIPEMD160: + case FunctionType::Kind::BlockHash: + case FunctionType::Kind::AddMod: + case FunctionType::Kind::MulMod: + abstractFunctionCall(_funCall); + break; + default: m_errorReporter.warning( _funCall.location(), "Assertion checker does not yet implement this type of function call." @@ -442,8 +450,8 @@ void SMTChecker::visitGasLeft(FunctionCall const& _funCall) string gasLeft = "gasleft()"; // We increase the variable index since gasleft changes // inside a tx. - defineSpecialVariable(gasLeft, _funCall, true); - auto const& symbolicVar = m_specialVariables.at(gasLeft); + defineGlobalVariable(gasLeft, _funCall, true); + auto const& symbolicVar = m_globalContext.at(gasLeft); unsigned index = symbolicVar->index(); // We set the current value to unknown anyway to add type constraints. setUnknownValue(*symbolicVar); @@ -451,21 +459,6 @@ void SMTChecker::visitGasLeft(FunctionCall const& _funCall) m_interface->addAssertion(symbolicVar->currentValue() <= symbolicVar->valueAtIndex(index - 1)); } -void SMTChecker::visitBlockHash(FunctionCall const& _funCall) -{ - string blockHash = "blockhash"; - auto const& arguments = _funCall.arguments(); - solAssert(arguments.size() == 1, ""); - smt::SortPointer paramSort = smtSort(*arguments.at(0)->annotation().type); - smt::SortPointer returnSort = smtSort(*_funCall.annotation().type); - defineUninterpretedFunction( - blockHash, - make_shared<smt::FunctionSort>(vector<smt::SortPointer>{paramSort}, returnSort) - ); - defineExpr(_funCall, m_uninterpretedFunctions.at(blockHash)({expr(*arguments.at(0))})); - m_uninterpretedTerms.push_back(&_funCall); -} - void SMTChecker::inlineFunctionCall(FunctionCall const& _funCall) { FunctionDefinition const* _funDef = nullptr; @@ -533,29 +526,32 @@ void SMTChecker::inlineFunctionCall(FunctionCall const& _funCall) } } +void SMTChecker::abstractFunctionCall(FunctionCall const& _funCall) +{ + vector<smt::Expression> smtArguments; + for (auto const& arg: _funCall.arguments()) + smtArguments.push_back(expr(*arg)); + defineExpr(_funCall, (*m_expressions.at(&_funCall.expression()))(smtArguments)); + m_uninterpretedTerms.push_back(&_funCall); + setSymbolicUnknownValue(expr(_funCall), _funCall.annotation().type, *m_interface); +} + void SMTChecker::endVisit(Identifier const& _identifier) { if (_identifier.annotation().lValueRequested) { // Will be translated as part of the node that requested the lvalue. } - else if (FunctionType const* fun = dynamic_cast<FunctionType const*>(_identifier.annotation().type.get())) + else if (dynamic_cast<FunctionType const*>(_identifier.annotation().type.get())) { - if ( - fun->kind() == FunctionType::Kind::Assert || - fun->kind() == FunctionType::Kind::Require || - fun->kind() == FunctionType::Kind::GasLeft || - fun->kind() == FunctionType::Kind::BlockHash - ) - return; - createExpr(_identifier); + visitFunctionIdentifier(_identifier); } else if (isSupportedType(_identifier.annotation().type->category())) { if (VariableDeclaration const* decl = dynamic_cast<VariableDeclaration const*>(_identifier.annotation().referencedDeclaration)) defineExpr(_identifier, currentValue(*decl)); else if (_identifier.name() == "now") - defineSpecialVariable(_identifier.name(), _identifier); + defineGlobalVariable(_identifier.name(), _identifier); else // TODO: handle MagicVariableDeclaration here m_errorReporter.warning( @@ -565,6 +561,20 @@ void SMTChecker::endVisit(Identifier const& _identifier) } } +void SMTChecker::visitFunctionIdentifier(Identifier const& _identifier) +{ + auto const& fType = dynamic_cast<FunctionType const&>(*_identifier.annotation().type); + if (fType.returnParameterTypes().size() > 1) + { + m_errorReporter.warning( + _identifier.location(), + "Assertion checker does not yet support functions with more than one return parameter." + ); + } + defineGlobalFunction(fType.richIdentifier(), _identifier); + m_expressions.emplace(&_identifier, m_globalContext.at(fType.richIdentifier())); +} + void SMTChecker::endVisit(Literal const& _literal) { Type const& type = *_literal.annotation().type; @@ -616,7 +626,7 @@ bool SMTChecker::visit(MemberAccess const& _memberAccess) _memberAccess.location(), "Assertion checker does not yet support this expression." ); - defineSpecialVariable(accessedName + "." + _memberAccess.memberName(), _memberAccess); + defineGlobalVariable(accessedName + "." + _memberAccess.memberName(), _memberAccess); return false; } else @@ -628,30 +638,39 @@ bool SMTChecker::visit(MemberAccess const& _memberAccess) return true; } -void SMTChecker::defineSpecialVariable(string const& _name, Expression const& _expr, bool _increaseIndex) +void SMTChecker::defineGlobalVariable(string const& _name, Expression const& _expr, bool _increaseIndex) { - if (!knownSpecialVariable(_name)) + if (!knownGlobalSymbol(_name)) { auto result = newSymbolicVariable(*_expr.annotation().type, _name, *m_interface); - m_specialVariables.emplace(_name, result.second); + m_globalContext.emplace(_name, result.second); setUnknownValue(*result.second); if (result.first) m_errorReporter.warning( _expr.location(), - "Assertion checker does not yet support this special variable." + "Assertion checker does not yet support this global variable." ); } else if (_increaseIndex) - m_specialVariables.at(_name)->increaseIndex(); + m_globalContext.at(_name)->increaseIndex(); // The default behavior is not to increase the index since - // most of the special values stay the same throughout a tx. - defineExpr(_expr, m_specialVariables.at(_name)->currentValue()); + // most of the global values stay the same throughout a tx. + if (isSupportedType(_expr.annotation().type->category())) + defineExpr(_expr, m_globalContext.at(_name)->currentValue()); } -void SMTChecker::defineUninterpretedFunction(string const& _name, smt::SortPointer _sort) +void SMTChecker::defineGlobalFunction(string const& _name, Expression const& _expr) { - if (!m_uninterpretedFunctions.count(_name)) - m_uninterpretedFunctions.emplace(_name, m_interface->newVariable(_name, _sort)); + if (!knownGlobalSymbol(_name)) + { + auto result = newSymbolicVariable(*_expr.annotation().type, _name, *m_interface); + m_globalContext.emplace(_name, result.second); + if (result.first) + m_errorReporter.warning( + _expr.location(), + "Assertion checker does not yet support the type of this function." + ); + } } void SMTChecker::arithmeticOperation(BinaryOperation const& _op) @@ -831,10 +850,13 @@ void SMTChecker::checkCondition( expressionsToEvaluate.emplace_back(currentValue(*var.first)); expressionNames.push_back(var.first->name()); } - for (auto const& var: m_specialVariables) + for (auto const& var: m_globalContext) { - expressionsToEvaluate.emplace_back(var.second->currentValue()); - expressionNames.push_back(var.first); + if (smtKind(var.second->type()->category()) != smt::Kind::Function) + { + expressionsToEvaluate.emplace_back(var.second->currentValue()); + expressionNames.push_back(var.first); + } } for (auto const& uf: m_uninterpretedTerms) { @@ -1147,9 +1169,9 @@ bool SMTChecker::knownExpr(Expression const& _e) const return m_expressions.count(&_e); } -bool SMTChecker::knownSpecialVariable(string const& _var) const +bool SMTChecker::knownGlobalSymbol(string const& _var) const { - return m_specialVariables.count(_var); + return m_globalContext.count(_var); } void SMTChecker::createExpr(Expression const& _e) @@ -1172,6 +1194,7 @@ void SMTChecker::createExpr(Expression const& _e) void SMTChecker::defineExpr(Expression const& _e, smt::Expression _value) { createExpr(_e); + solAssert(isSupportedType(*_e.annotation().type), "Equality operator applied to type that is not fully supported"); m_interface->addAssertion(expr(_e) == _value); } diff --git a/libsolidity/formal/SMTChecker.h b/libsolidity/formal/SMTChecker.h index 34724848..9f8c04ab 100644 --- a/libsolidity/formal/SMTChecker.h +++ b/libsolidity/formal/SMTChecker.h @@ -84,16 +84,18 @@ private: void compareOperation(BinaryOperation const& _op); void booleanOperation(BinaryOperation const& _op); - void visitAssert(FunctionCall const&); - void visitRequire(FunctionCall const&); - void visitGasLeft(FunctionCall const&); - void visitBlockHash(FunctionCall const&); + void visitAssert(FunctionCall const& _funCall); + void visitRequire(FunctionCall const& _funCall); + void visitGasLeft(FunctionCall const& _funCall); /// Visits the FunctionDefinition of the called function /// if available and inlines the return value. - void inlineFunctionCall(FunctionCall const&); + void inlineFunctionCall(FunctionCall const& _funCall); + /// Creates an uninterpreted function call. + void abstractFunctionCall(FunctionCall const& _funCall); + void visitFunctionIdentifier(Identifier const& _identifier); - void defineSpecialVariable(std::string const& _name, Expression const& _expr, bool _increaseIndex = false); - void defineUninterpretedFunction(std::string const& _name, smt::SortPointer _sort); + void defineGlobalVariable(std::string const& _name, Expression const& _expr, bool _increaseIndex = false); + void defineGlobalFunction(std::string const& _name, Expression const& _expr); /// Division expression in the given type. Requires special treatment because /// of rounding for signed division. @@ -176,8 +178,8 @@ private: /// Creates the expression and sets its value. void defineExpr(Expression const& _e, smt::Expression _value); - /// Checks if special variable was seen. - bool knownSpecialVariable(std::string const& _var) const; + /// Checks if special variable or function was seen. + bool knownGlobalSymbol(std::string const& _var) const; /// Adds a new path condition void pushPathCondition(smt::Expression const& _e); @@ -205,9 +207,7 @@ private: /// repeated calls to the same function. std::unordered_map<Expression const*, std::shared_ptr<SymbolicVariable>> m_expressions; std::unordered_map<VariableDeclaration const*, std::shared_ptr<SymbolicVariable>> m_variables; - std::unordered_map<std::string, std::shared_ptr<SymbolicVariable>> m_specialVariables; - /// Stores the declaration of an Uninterpreted Function. - std::unordered_map<std::string, smt::Expression> m_uninterpretedFunctions; + std::unordered_map<std::string, std::shared_ptr<SymbolicVariable>> m_globalContext; /// Stores the instances of an Uninterpreted Function applied to arguments. /// Used to retrieve models. std::vector<Expression const*> m_uninterpretedTerms; diff --git a/libsolidity/formal/SymbolicTypes.cpp b/libsolidity/formal/SymbolicTypes.cpp index c297c807..3cfaa271 100644 --- a/libsolidity/formal/SymbolicTypes.cpp +++ b/libsolidity/formal/SymbolicTypes.cpp @@ -38,15 +38,25 @@ smt::SortPointer dev::solidity::smtSort(Type const& _type) solAssert(fType, ""); vector<smt::SortPointer> parameterSorts = smtSort(fType->parameterTypes()); auto returnTypes = fType->returnParameterTypes(); - // TODO remove this when we support tuples. - solAssert(returnTypes.size() == 1, ""); - smt::SortPointer returnSort = smtSort(*returnTypes.at(0)); + smt::SortPointer returnSort; + // TODO change this when we support tuples. + if (returnTypes.size() == 0) + // We cannot declare functions without a return sort, so we use the smallest. + returnSort = make_shared<smt::Sort>(smt::Kind::Bool); + else if (returnTypes.size() > 1) + // Abstract sort. + returnSort = make_shared<smt::Sort>(smt::Kind::Int); + else + returnSort = smtSort(*returnTypes.at(0)); return make_shared<smt::FunctionSort>(parameterSorts, returnSort); } case smt::Kind::Array: { solUnimplementedAssert(false, "Invalid type"); } + default: + // Abstract case. + return make_shared<smt::Sort>(smt::Kind::Int); } solAssert(false, "Invalid type"); } @@ -65,13 +75,21 @@ smt::Kind dev::solidity::smtKind(Type::Category _category) return smt::Kind::Int; else if (isBool(_category)) return smt::Kind::Bool; - solAssert(false, "Invalid type"); + else if (isFunction(_category)) + return smt::Kind::Function; + // Abstract case. + return smt::Kind::Int; } bool dev::solidity::isSupportedType(Type::Category _category) { return isNumber(_category) || - isBool(_category) || + isBool(_category); +} + +bool dev::solidity::isSupportedTypeDeclaration(Type::Category _category) +{ + return isSupportedType(_category) || isFunction(_category); } @@ -84,7 +102,7 @@ pair<bool, shared_ptr<SymbolicVariable>> dev::solidity::newSymbolicVariable( bool abstract = false; shared_ptr<SymbolicVariable> var; TypePointer type = _type.shared_from_this(); - if (!isSupportedType(_type)) + if (!isSupportedTypeDeclaration(_type)) { abstract = true; var = make_shared<SymbolicIntVariable>(make_shared<IntegerType>(256), _uniqueName, _solver); @@ -92,7 +110,7 @@ pair<bool, shared_ptr<SymbolicVariable>> dev::solidity::newSymbolicVariable( else if (isBool(_type.category())) var = make_shared<SymbolicBoolVariable>(type, _uniqueName, _solver); else if (isFunction(_type.category())) - var = make_shared<SymbolicIntVariable>(make_shared<IntegerType>(256), _uniqueName, _solver); + var = make_shared<SymbolicFunctionVariable>(type, _uniqueName, _solver); else if (isInteger(_type.category())) var = make_shared<SymbolicIntVariable>(type, _uniqueName, _solver); else if (isFixedBytes(_type.category())) @@ -122,6 +140,11 @@ bool dev::solidity::isSupportedType(Type const& _type) return isSupportedType(_type.category()); } +bool dev::solidity::isSupportedTypeDeclaration(Type const& _type) +{ + return isSupportedTypeDeclaration(_type.category()); +} + bool dev::solidity::isInteger(Type::Category _category) { return _category == Type::Category::Integer; diff --git a/libsolidity/formal/SymbolicTypes.h b/libsolidity/formal/SymbolicTypes.h index 984653b3..2c568f5b 100644 --- a/libsolidity/formal/SymbolicTypes.h +++ b/libsolidity/formal/SymbolicTypes.h @@ -34,10 +34,12 @@ std::vector<smt::SortPointer> smtSort(std::vector<TypePointer> const& _types); /// Returns the SMT kind that models the Solidity type type category _category. smt::Kind smtKind(Type::Category _category); -/// So far int, bool and address are supported. -/// Returns true if type is supported. +/// Returns true if type is fully supported (declaration and operations). bool isSupportedType(Type::Category _category); bool isSupportedType(Type const& _type); +/// Returns true if type is partially supported (declaration). +bool isSupportedTypeDeclaration(Type::Category _category); +bool isSupportedTypeDeclaration(Type const& _type); bool isInteger(Type::Category _category); bool isRational(Type::Category _category); diff --git a/libsolidity/interface/AssemblyStack.cpp b/libsolidity/interface/AssemblyStack.cpp index fbfb3472..b97e00ae 100644 --- a/libsolidity/interface/AssemblyStack.cpp +++ b/libsolidity/interface/AssemblyStack.cpp @@ -32,6 +32,7 @@ #include <libyul/backends/evm/EVMObjectCompiler.h> #include <libyul/backends/evm/EVMCodeTransform.h> #include <libyul/backends/evm/EVMAssembly.h> +#include <libyul/backends/evm/EVMDialect.h> #include <libyul/ObjectParser.h> #include <libevmasm/Assembly.h> @@ -45,14 +46,14 @@ using namespace dev::solidity; namespace { -yul::Dialect languageToDialect(AssemblyStack::Language _language) +shared_ptr<yul::Dialect> languageToDialect(AssemblyStack::Language _language) { switch (_language) { case AssemblyStack::Language::Assembly: - return yul::Dialect::looseAssemblyForEVM(); + return yul::EVMDialect::looseAssemblyForEVM(); case AssemblyStack::Language::StrictAssembly: - return yul::Dialect::strictAssemblyForEVMObjects(); + return yul::EVMDialect::strictAssemblyForEVMObjects(); case AssemblyStack::Language::Yul: return yul::Dialect::yul(); } @@ -112,6 +113,22 @@ bool AssemblyStack::analyzeParsed(yul::Object& _object) 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) { solAssert(_object.code, ""); @@ -122,7 +139,7 @@ void AssemblyStack::optimize(yul::Object& _object) yul::OptimiserSuite::run(*_object.code, *_object.analysisInfo); } -MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const +MachineAssemblyObject AssemblyStack::assemble(Machine _machine, bool _optimize) const { solAssert(m_analysisSuccessful, ""); solAssert(m_parserResult, ""); @@ -136,7 +153,7 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const MachineAssemblyObject object; eth::Assembly assembly; EthAssemblyAdapter adapter(assembly); - yul::EVMObjectCompiler::compile(*m_parserResult, adapter, m_language == Language::Yul, false); + compileEVM(adapter, false, _optimize); object.bytecode = make_shared<eth::LinkerObject>(assembly.assemble()); object.assembly = assembly.assemblyString(); return object; @@ -145,7 +162,7 @@ MachineAssemblyObject AssemblyStack::assemble(Machine _machine) const { MachineAssemblyObject object; yul::EVMAssembly assembly(true); - yul::EVMObjectCompiler::compile(*m_parserResult, assembly, m_language == Language::Yul, true); + compileEVM(assembly, true, _optimize); object.bytecode = make_shared<eth::LinkerObject>(assembly.finalize()); /// TODO: fill out text representation return object; diff --git a/libsolidity/interface/AssemblyStack.h b/libsolidity/interface/AssemblyStack.h index 485ec1e7..c8e3d35a 100644 --- a/libsolidity/interface/AssemblyStack.h +++ b/libsolidity/interface/AssemblyStack.h @@ -36,6 +36,10 @@ namespace langutil { class Scanner; } +namespace yul +{ +class AbstractAssembly; +} namespace dev { @@ -73,7 +77,8 @@ public: void optimize(); /// Run the assembly step (should only be called after parseAndAnalyze). - MachineAssemblyObject assemble(Machine _machine) const; + /// @param _optimize does not run the optimizer but performs optimized code generation. + MachineAssemblyObject assemble(Machine _machine, bool _optimize = false) const; /// @returns the errors generated during parsing, analysis (and potentially assembly). langutil::ErrorList const& errors() const { return m_errors; } @@ -85,6 +90,8 @@ private: bool analyzeParsed(); bool analyzeParsed(yul::Object& _object); + void compileEVM(yul::AbstractAssembly& _assembly, bool _evm15, bool _optimize) const; + void optimize(yul::Object& _object); Language m_language = Language::Assembly; diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 6cab7be3..26f13f93 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -24,6 +24,7 @@ #include <vector> #include <libsolidity/parsing/Parser.h> #include <libyul/AsmParser.h> +#include <libyul/backends/evm/EVMDialect.h> #include <liblangutil/SourceLocation.h> #include <liblangutil/ErrorReporter.h> #include <liblangutil/Scanner.h> @@ -170,7 +171,7 @@ ASTPointer<ImportDirective> Parser::parseImportDirective() expectToken(Token::As); alias = expectIdentifierToken(); } - symbolAliases.push_back(make_pair(move(id), move(alias))); + symbolAliases.emplace_back(move(id), move(alias)); if (m_scanner->currentToken() != Token::Comma) break; m_scanner->next(); @@ -1012,7 +1013,7 @@ ASTPointer<InlineAssembly> Parser::parseInlineAssembly(ASTPointer<ASTString> con 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); nodeFactory.markEndPosition(); return nodeFactory.createNode<InlineAssembly>(_docString, block); @@ -1690,7 +1691,7 @@ Parser::IndexAccessedPath Parser::parseIndexAccessedPath() index = parseExpression(); SourceLocation indexLocation = iap.path.front()->location(); indexLocation.end = endPosition(); - iap.indices.push_back(make_pair(index, indexLocation)); + iap.indices.emplace_back(index, indexLocation); expectToken(Token::RBrack); } diff --git a/libyul/AsmAnalysis.cpp b/libyul/AsmAnalysis.cpp index 1be1cf1a..821da005 100644 --- a/libyul/AsmAnalysis.cpp +++ b/libyul/AsmAnalysis.cpp @@ -101,7 +101,7 @@ bool AsmAnalyzer::operator()(Literal const& _literal) } else if (_literal.kind == LiteralKind::Boolean) { - solAssert(m_dialect.flavour == AsmFlavour::Yul, ""); + solAssert(m_dialect->flavour == AsmFlavour::Yul, ""); solAssert(_literal.value == YulString{string("true")} || _literal.value == YulString{string("false")}, ""); } m_info.stackHeightInfo[&_literal] = m_stackHeight; @@ -164,7 +164,7 @@ bool AsmAnalyzer::operator()(Identifier const& _identifier) bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr) { - solAssert(m_dialect.flavour != AsmFlavour::Yul, ""); + solAssert(m_dialect->flavour != AsmFlavour::Yul, ""); bool success = true; for (auto const& arg: _instr.arguments | boost::adaptors::reversed) if (!expectExpression(arg)) @@ -182,9 +182,9 @@ bool AsmAnalyzer::operator()(ExpressionStatement const& _statement) { int initialStackHeight = m_stackHeight; bool success = boost::apply_visitor(*this, _statement.expression); - if (m_stackHeight != initialStackHeight && (m_dialect.flavour != AsmFlavour::Loose || m_errorTypeForLoose)) + if (m_stackHeight != initialStackHeight && (m_dialect->flavour != AsmFlavour::Loose || m_errorTypeForLoose)) { - Error::Type errorType = m_dialect.flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError; + Error::Type errorType = m_dialect->flavour == AsmFlavour::Loose ? *m_errorTypeForLoose : Error::Type::TypeError; string msg = "Top-level expressions are not supposed to return values (this expression returns " + to_string(m_stackHeight - initialStackHeight) + @@ -299,7 +299,7 @@ bool AsmAnalyzer::operator()(FunctionCall const& _funCall) bool success = true; size_t parameters = 0; size_t returns = 0; - if (BuiltinFunction const* f = m_dialect.builtins->query(_funCall.functionName.name)) + if (BuiltinFunction const* f = m_dialect->builtin(_funCall.functionName.name)) { // TODO: compare types, too parameters = f->parameters.size(); @@ -569,7 +569,7 @@ Scope& AsmAnalyzer::scope(Block const* _block) } void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _location) { - if (m_dialect.flavour != AsmFlavour::Yul) + if (m_dialect->flavour != AsmFlavour::Yul) return; if (!builtinTypes.count(type)) @@ -629,7 +629,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio if (_instr == solidity::Instruction::JUMP || _instr == solidity::Instruction::JUMPI || _instr == solidity::Instruction::JUMPDEST) { - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) solAssert(m_errorTypeForLoose && *m_errorTypeForLoose != Error::Type::Warning, ""); m_errorReporter.error( @@ -644,7 +644,7 @@ void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocatio void AsmAnalyzer::checkLooseFeature(SourceLocation const& _location, string const& _description) { - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) solAssert(false, _description); else if (m_errorTypeForLoose) m_errorReporter.error(*m_errorTypeForLoose, _location, _description); diff --git a/libyul/AsmAnalysis.h b/libyul/AsmAnalysis.h index ec2b8868..21cc1142 100644 --- a/libyul/AsmAnalysis.h +++ b/libyul/AsmAnalysis.h @@ -59,7 +59,7 @@ public: langutil::ErrorReporter& _errorReporter, dev::solidity::EVMVersion _evmVersion, boost::optional<langutil::Error::Type> _errorTypeForLoose, - Dialect _dialect = Dialect::looseAssemblyForEVM(), + std::shared_ptr<Dialect> _dialect, ExternalIdentifierAccess::Resolver const& _resolver = ExternalIdentifierAccess::Resolver() ): m_resolver(_resolver), @@ -115,7 +115,7 @@ private: AsmAnalysisInfo& m_info; langutil::ErrorReporter& m_errorReporter; dev::solidity::EVMVersion m_evmVersion; - Dialect m_dialect = Dialect::looseAssemblyForEVM(); + std::shared_ptr<Dialect> m_dialect; boost::optional<langutil::Error::Type> m_errorTypeForLoose; }; diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index 33bb42f9..c7302063 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -107,14 +107,14 @@ Statement Parser::parseStatement() return parseForLoop(); case Token::Assign: { - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) break; StackAssignment assignment = createWithLocation<StackAssignment>(); advance(); expectToken(Token::Colon); assignment.variableName.location = location(); assignment.variableName.name = YulString(currentLiteral()); - if (m_dialect.builtins->query(assignment.variableName.name)) + if (m_dialect->builtin(assignment.variableName.name)) fatalParserError("Identifier expected, got builtin symbol."); else if (instructions().count(assignment.variableName.name.str())) fatalParserError("Identifier expected, got instruction name."); @@ -176,9 +176,9 @@ Statement Parser::parseStatement() if (currentToken() == Token::Assign && peekNextToken() != Token::Colon) { Assignment assignment = createWithLocation<Assignment>(identifier.location); - if (m_dialect.builtins->query(identifier.name)) + if (m_dialect->builtin(identifier.name)) fatalParserError("Cannot assign to builtin function \"" + identifier.name.str() + "\"."); - else if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(identifier.name.str())) + else if (m_dialect->flavour != AsmFlavour::Yul && instructions().count(identifier.name.str())) fatalParserError("Cannot use instruction names for identifier names."); advance(); assignment.variableNames.emplace_back(identifier); @@ -189,7 +189,7 @@ Statement Parser::parseStatement() else { // label - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) fatalParserError("Labels are not supported."); Label label = createWithLocation<Label>(identifier.location); label.name = identifier.name; @@ -197,7 +197,7 @@ Statement Parser::parseStatement() } } default: - if (m_dialect.flavour != AsmFlavour::Loose) + if (m_dialect->flavour != AsmFlavour::Loose) fatalParserError("Call or assignment expected."); break; } @@ -273,7 +273,7 @@ Expression Parser::parseExpression() instructionNames().at(instr.instruction) + "\" not allowed in this context." ); - if (m_dialect.flavour != AsmFlavour::Loose && currentToken() != Token::LParen) + if (m_dialect->flavour != AsmFlavour::Loose && currentToken() != Token::LParen) fatalParserError( "Non-functional instructions are not allowed in this context." ); @@ -293,7 +293,7 @@ Expression Parser::parseExpression() else if (operation.type() == typeid(Instruction)) { // Instructions not taking arguments are allowed as expressions. - solAssert(m_dialect.flavour == AsmFlavour::Loose, ""); + solAssert(m_dialect->flavour == AsmFlavour::Loose, ""); Instruction& instr = boost::get<Instruction>(operation); return FunctionalInstruction{std::move(instr.location), instr.instruction, {}}; } @@ -362,9 +362,9 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() else literal = YulString{currentLiteral()}; // first search the set of builtins, then the instructions. - if (m_dialect.builtins->query(literal)) + if (m_dialect->builtin(literal)) ret = Identifier{location(), literal}; - else if (m_dialect.flavour != AsmFlavour::Yul && instructions().count(literal.str())) + else if (m_dialect->flavour != AsmFlavour::Yul && instructions().count(literal.str())) { dev::solidity::Instruction const& instr = instructions().at(literal.str()); ret = Instruction{location(), instr}; @@ -405,7 +405,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() {} }; advance(); - if (m_dialect.flavour == AsmFlavour::Yul) + if (m_dialect->flavour == AsmFlavour::Yul) { expectToken(Token::Colon); literal.location.end = endPosition(); @@ -418,7 +418,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() } default: fatalParserError( - m_dialect.flavour == AsmFlavour::Yul ? + m_dialect->flavour == AsmFlavour::Yul ? "Literal or identifier expected." : "Literal, identifier or instruction expected." ); @@ -488,7 +488,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp) RecursionGuard recursionGuard(*this); if (_initialOp.type() == typeid(Instruction)) { - solAssert(m_dialect.flavour != AsmFlavour::Yul, "Instructions are invalid in Yul"); + solAssert(m_dialect->flavour != AsmFlavour::Yul, "Instructions are invalid in Yul"); Instruction& instruction = boost::get<Instruction>(_initialOp); FunctionalInstruction ret; ret.instruction = instruction.instruction; @@ -559,7 +559,7 @@ Expression Parser::parseCall(Parser::ElementaryOperation&& _initialOp) } else fatalParserError( - m_dialect.flavour == AsmFlavour::Yul ? + m_dialect->flavour == AsmFlavour::Yul ? "Function name expected." : "Assembly instruction or function name required in front of \"(\")" ); @@ -572,7 +572,7 @@ TypedName Parser::parseTypedName() RecursionGuard recursionGuard(*this); TypedName typedName = createWithLocation<TypedName>(); typedName.name = expectAsmIdentifier(); - if (m_dialect.flavour == AsmFlavour::Yul) + if (m_dialect->flavour == AsmFlavour::Yul) { expectToken(Token::Colon); typedName.location.end = endPosition(); @@ -584,7 +584,7 @@ TypedName Parser::parseTypedName() YulString Parser::expectAsmIdentifier() { YulString name = YulString{currentLiteral()}; - if (m_dialect.flavour == AsmFlavour::Yul) + if (m_dialect->flavour == AsmFlavour::Yul) { switch (currentToken()) { @@ -598,7 +598,7 @@ YulString Parser::expectAsmIdentifier() break; } } - else if (m_dialect.builtins->query(name)) + else if (m_dialect->builtin(name)) fatalParserError("Cannot use builtin function name \"" + name.str() + "\" as identifier name."); else if (instructions().count(name.str())) fatalParserError("Cannot use instruction names for identifier names."); diff --git a/libyul/AsmParser.h b/libyul/AsmParser.h index b40a717c..a02f0b4d 100644 --- a/libyul/AsmParser.h +++ b/libyul/AsmParser.h @@ -38,7 +38,7 @@ namespace yul class Parser: public langutil::ParserBase { public: - explicit Parser(langutil::ErrorReporter& _errorReporter, Dialect _dialect = Dialect::looseAssemblyForEVM()): + explicit Parser(langutil::ErrorReporter& _errorReporter, std::shared_ptr<Dialect> _dialect): ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {} /// Parses an inline assembly block starting with `{` and ending with `}`. @@ -86,7 +86,7 @@ protected: static bool isValidNumberLiteral(std::string const& _literal); private: - Dialect m_dialect = Dialect::looseAssemblyForEVM(); + std::shared_ptr<Dialect> m_dialect; }; } diff --git a/libyul/CMakeLists.txt b/libyul/CMakeLists.txt index 7d1ec6ba..74a5703c 100644 --- a/libyul/CMakeLists.txt +++ b/libyul/CMakeLists.txt @@ -10,6 +10,7 @@ add_library(yul ObjectParser.cpp backends/evm/EVMAssembly.cpp backends/evm/EVMCodeTransform.cpp + backends/evm/EVMDialect.cpp backends/evm/EVMObjectCompiler.cpp optimiser/ASTCopier.cpp optimiser/ASTWalker.cpp diff --git a/libyul/Dialect.cpp b/libyul/Dialect.cpp index 99718787..f6985c17 100644 --- a/libyul/Dialect.cpp +++ b/libyul/Dialect.cpp @@ -20,57 +20,10 @@ #include <libyul/Dialect.h> +#include <libyul/Object.h> +#include <libyul/backends/evm/AbstractAssembly.h> + #include <map> using namespace yul; using namespace std; - -namespace -{ - -void addFunction( - map<YulString, BuiltinFunction>& _repository, - string const& _name, - size_t _params, - size_t _returns, - bool _movable -) -{ - _repository[YulString{_name}] = BuiltinFunction{ - YulString{_name}, - vector<Type>(_params), - vector<Type>(_returns), - _movable - }; -} - -class GenericBuiltins: public Builtins -{ -public: - GenericBuiltins(map<YulString, BuiltinFunction> const& _functions): m_functions(_functions) {} - BuiltinFunction const* query(YulString _name) const - { - auto it = m_functions.find(_name); - if (it != end(m_functions)) - return &it->second; - else - return nullptr; - } -private: - map<YulString, BuiltinFunction> const& m_functions; -}; - -} - -Dialect Dialect::strictAssemblyForEVMObjects() -{ - static map<YulString, BuiltinFunction> functions; - if (functions.empty()) - { - addFunction(functions, "datasize", 1, 1, true); - addFunction(functions, "dataoffset", 1, 1, true); - addFunction(functions, "datacopy", 3, 0, false); - } - // The EVM instructions will be moved to builtins at some point. - return Dialect{AsmFlavour::Strict, std::make_shared<GenericBuiltins>(functions)}; -} diff --git a/libyul/Dialect.h b/libyul/Dialect.h index b78f1aaf..2def566c 100644 --- a/libyul/Dialect.h +++ b/libyul/Dialect.h @@ -22,7 +22,9 @@ #include <libyul/YulString.h> -#include <memory> +#include <boost/noncopyable.hpp> + +#include <vector> namespace yul { @@ -45,38 +47,19 @@ struct BuiltinFunction bool movable; }; -/** - * Class to query for builtin functions and their semantics. - */ -struct Builtins +struct Dialect: boost::noncopyable { - virtual ~Builtins() = default; + AsmFlavour const flavour = AsmFlavour::Loose; /// @returns the builtin function of the given name or a nullptr if it is not a builtin function. - virtual BuiltinFunction const* query(YulString /*_name*/) const { return nullptr; } -}; + virtual BuiltinFunction const* builtin(YulString /*_name*/) const { return nullptr; } -struct Dialect -{ - AsmFlavour flavour = AsmFlavour::Loose; - std::shared_ptr<Builtins> builtins; + Dialect(AsmFlavour _flavour): flavour(_flavour) {} + virtual ~Dialect() {} - Dialect(AsmFlavour _flavour, std::shared_ptr<Builtins> _builtins): - flavour(_flavour), builtins(std::move(_builtins)) - {} - static Dialect looseAssemblyForEVM() - { - return Dialect{AsmFlavour::Loose, std::make_shared<Builtins>()}; - } - static Dialect strictAssemblyForEVM() - { - // The EVM instructions will be moved to builtins at some point. - return Dialect{AsmFlavour::Strict, std::make_shared<Builtins>()}; - } - static Dialect strictAssemblyForEVMObjects(); - static Dialect yul() + static std::shared_ptr<Dialect> yul() { // Will have to add builtins later. - return Dialect{AsmFlavour::Yul, std::make_shared<Builtins>()}; + return std::make_shared<Dialect>(AsmFlavour::Yul); } }; diff --git a/libyul/Exceptions.h b/libyul/Exceptions.h index e10e53ef..d1f1fe96 100644 --- a/libyul/Exceptions.h +++ b/libyul/Exceptions.h @@ -28,6 +28,7 @@ namespace yul struct YulException: virtual dev::Exception {}; struct OptimizerException: virtual YulException {}; +struct CodegenException: virtual YulException {}; struct YulAssertion: virtual YulException {}; /// Assertion that throws an YulAssertion containing the given description if it is not met. diff --git a/libyul/ObjectParser.h b/libyul/ObjectParser.h index 59efb8ab..e39f24cd 100644 --- a/libyul/ObjectParser.h +++ b/libyul/ObjectParser.h @@ -47,7 +47,7 @@ class ObjectParser: public langutil::ParserBase public: explicit ObjectParser( langutil::ErrorReporter& _errorReporter, - Dialect _dialect = Dialect::looseAssemblyForEVM() + std::shared_ptr<Dialect> _dialect ): ParserBase(_errorReporter), m_dialect(std::move(_dialect)) {} @@ -67,7 +67,7 @@ private: YulString parseUniqueName(Object const* _containingObject); void addNamedSubObject(Object& _container, YulString _name, std::shared_ptr<ObjectNode> _subObject); - Dialect m_dialect; + std::shared_ptr<Dialect> m_dialect; }; } diff --git a/libyul/backends/evm/EVMCodeTransform.cpp b/libyul/backends/evm/EVMCodeTransform.cpp index 12abd754..bd18985c 100644 --- a/libyul/backends/evm/EVMCodeTransform.cpp +++ b/libyul/backends/evm/EVMCodeTransform.cpp @@ -20,6 +20,7 @@ #include <libyul/backends/evm/EVMCodeTransform.h> +#include <libyul/optimiser/NameCollector.h> #include <libyul/AsmAnalysisInfo.h> #include <libyul/AsmData.h> @@ -32,6 +33,144 @@ using namespace dev; using namespace yul; using namespace dev::solidity; +void VariableReferenceCounter::operator()(Identifier const& _identifier) +{ + increaseRefIfFound(_identifier.name); +} + +void VariableReferenceCounter::operator()(FunctionDefinition const& _function) +{ + Scope* originalScope = m_scope; + + solAssert(m_info.virtualBlocks.at(&_function), ""); + m_scope = m_info.scopes.at(m_info.virtualBlocks.at(&_function).get()).get(); + solAssert(m_scope, "Variable scope does not exist."); + + for (auto const& v: _function.returnVariables) + increaseRefIfFound(v.name); + + VariableReferenceCounter{m_context, m_info}(_function.body); + + m_scope = originalScope; +} + +void VariableReferenceCounter::operator()(ForLoop const& _forLoop) +{ + Scope* originalScope = m_scope; + // Special scoping rules. + m_scope = m_info.scopes.at(&_forLoop.pre).get(); + + walkVector(_forLoop.pre.statements); + visit(*_forLoop.condition); + (*this)(_forLoop.body); + (*this)(_forLoop.post); + + m_scope = originalScope; +} + + +void VariableReferenceCounter::operator()(Block const& _block) +{ + Scope* originalScope = m_scope; + m_scope = m_info.scopes.at(&_block).get(); + + ASTWalker::operator()(_block); + + m_scope = originalScope; +} + +void VariableReferenceCounter::increaseRefIfFound(YulString _variableName) +{ + m_scope->lookup(_variableName, Scope::Visitor( + [=](Scope::Variable const& _var) + { + ++m_context.variableReferences[&_var]; + }, + [=](Scope::Label const&) { }, + [=](Scope::Function const&) { } + )); +} + + +CodeTransform::CodeTransform( + AbstractAssembly& _assembly, + AsmAnalysisInfo& _analysisInfo, + Block const& _block, + bool _allowStackOpt, + EVMDialect const& _dialect, + bool _evm15, + ExternalIdentifierAccess const& _identifierAccess, + bool _useNamedLabelsForFunctions, + int _stackAdjustment, + shared_ptr<Context> _context +): + m_assembly(_assembly), + m_info(_analysisInfo), + m_dialect(_dialect), + m_allowStackOpt(_allowStackOpt), + m_evm15(_evm15), + m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions), + m_identifierAccess(_identifierAccess), + m_stackAdjustment(_stackAdjustment), + m_context(_context) +{ + if (!m_context) + { + // initialize + m_context = make_shared<Context>(); + if (m_allowStackOpt) + VariableReferenceCounter{*m_context, m_info}(_block); + } +} + +void CodeTransform::decreaseReference(YulString, Scope::Variable const& _var) +{ + if (!m_allowStackOpt) + return; + + unsigned& ref = m_context->variableReferences.at(&_var); + solAssert(ref >= 1, ""); + --ref; + if (ref == 0) + m_variablesScheduledForDeletion.insert(&_var); +} + +bool CodeTransform::unreferenced(Scope::Variable const& _var) const +{ + return !m_context->variableReferences.count(&_var) || m_context->variableReferences[&_var] == 0; +} + +void CodeTransform::freeUnusedVariables() +{ + if (!m_allowStackOpt) + return; + + for (auto const& identifier: m_scope->identifiers) + if (identifier.second.type() == typeid(Scope::Variable)) + { + Scope::Variable const& var = boost::get<Scope::Variable>(identifier.second); + if (m_variablesScheduledForDeletion.count(&var)) + deleteVariable(var); + } + + while (m_unusedStackSlots.count(m_assembly.stackHeight() - 1)) + { + solAssert(m_unusedStackSlots.erase(m_assembly.stackHeight() - 1), ""); + m_assembly.appendInstruction(solidity::Instruction::POP); + --m_stackAdjustment; + } +} + +void CodeTransform::deleteVariable(Scope::Variable const& _var) +{ + solAssert(m_allowStackOpt, ""); + solAssert(m_context->variableStackHeights.count(&_var) > 0, ""); + m_unusedStackSlots.insert(m_context->variableStackHeights[&_var]); + m_context->variableStackHeights.erase(&_var); + m_context->variableReferences.erase(&_var); + m_variablesScheduledForDeletion.erase(&_var); +} + void CodeTransform::operator()(VariableDeclaration const& _varDecl) { solAssert(m_scope, ""); @@ -49,10 +188,40 @@ void CodeTransform::operator()(VariableDeclaration const& _varDecl) while (variablesLeft--) m_assembly.appendConstant(u256(0)); } - for (auto const& variable: _varDecl.variables) + + bool atTopOfStack = true; + for (int varIndex = numVariables - 1; varIndex >= 0; --varIndex) { - auto& var = boost::get<Scope::Variable>(m_scope->identifiers.at(variable.name)); - m_context->variableStackHeights[&var] = height++; + auto& var = boost::get<Scope::Variable>(m_scope->identifiers.at(_varDecl.variables[varIndex].name)); + m_context->variableStackHeights[&var] = height + varIndex; + if (!m_allowStackOpt) + continue; + + if (unreferenced(var)) + { + if (atTopOfStack) + { + m_context->variableStackHeights.erase(&var); + m_assembly.setSourceLocation(_varDecl.location); + m_assembly.appendInstruction(solidity::Instruction::POP); + --m_stackAdjustment; + } + else + m_variablesScheduledForDeletion.insert(&var); + } + else if (m_unusedStackSlots.empty()) + atTopOfStack = false; + else + { + int slot = *m_unusedStackSlots.begin(); + m_unusedStackSlots.erase(m_unusedStackSlots.begin()); + m_context->variableStackHeights[&var] = slot; + m_assembly.setSourceLocation(_varDecl.location); + if (int heightDiff = variableHeightDiff(var, true)) + m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1)); + m_assembly.appendInstruction(solidity::Instruction::POP); + --m_stackAdjustment; + } } checkStackHeight(&_varDecl); } @@ -70,6 +239,7 @@ void CodeTransform::operator()(Assignment const& _assignment) void CodeTransform::operator()(StackAssignment const& _assignment) { + solAssert(!m_allowStackOpt, ""); m_assembly.setSourceLocation(_assignment.location); generateAssignment(_assignment.variableName); checkStackHeight(&_assignment); @@ -84,6 +254,7 @@ void CodeTransform::operator()(ExpressionStatement const& _statement) void CodeTransform::operator()(Label const& _label) { + solAssert(!m_allowStackOpt, ""); m_assembly.setSourceLocation(_label.location); solAssert(m_scope, ""); solAssert(m_scope->identifiers.count(_label.name), ""); @@ -96,35 +267,46 @@ void CodeTransform::operator()(FunctionCall const& _call) { solAssert(m_scope, ""); - m_assembly.setSourceLocation(_call.location); - EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0 - if (!m_evm15) + if (BuiltinFunctionForEVM const* builtin = m_dialect.builtin(_call.functionName.name)) { - returnLabel = m_assembly.newLabelId(); - m_assembly.appendLabelReference(returnLabel); - m_stackAdjustment++; + builtin->generateCode(_call, m_assembly, [&]() { + for (auto const& arg: _call.arguments | boost::adaptors::reversed) + visitExpression(arg); + m_assembly.setSourceLocation(_call.location); + }); } - - Scope::Function* function = nullptr; - solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor( - [=](Scope::Variable&) { solAssert(false, "Expected function name."); }, - [=](Scope::Label&) { solAssert(false, "Expected function name."); }, - [&](Scope::Function& _function) { function = &_function; } - )), "Function name not found."); - solAssert(function, ""); - solAssert(function->arguments.size() == _call.arguments.size(), ""); - for (auto const& arg: _call.arguments | boost::adaptors::reversed) - visitExpression(arg); - m_assembly.setSourceLocation(_call.location); - if (m_evm15) - m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size()); else { - m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1); - m_assembly.appendLabel(returnLabel); - m_stackAdjustment--; + m_assembly.setSourceLocation(_call.location); + EVMAssembly::LabelID returnLabel(-1); // only used for evm 1.0 + if (!m_evm15) + { + returnLabel = m_assembly.newLabelId(); + m_assembly.appendLabelReference(returnLabel); + m_stackAdjustment++; + } + + Scope::Function* function = nullptr; + solAssert(m_scope->lookup(_call.functionName.name, Scope::NonconstVisitor( + [=](Scope::Variable&) { solAssert(false, "Expected function name."); }, + [=](Scope::Label&) { solAssert(false, "Expected function name."); }, + [&](Scope::Function& _function) { function = &_function; } + )), "Function name not found."); + solAssert(function, ""); + solAssert(function->arguments.size() == _call.arguments.size(), ""); + for (auto const& arg: _call.arguments | boost::adaptors::reversed) + visitExpression(arg); + m_assembly.setSourceLocation(_call.location); + if (m_evm15) + m_assembly.appendJumpsub(functionEntryID(_call.functionName.name, *function), function->arguments.size(), function->returns.size()); + else + { + m_assembly.appendJumpTo(functionEntryID(_call.functionName.name, *function), function->returns.size() - function->arguments.size() - 1); + m_assembly.appendLabel(returnLabel); + m_stackAdjustment--; + } + checkStackHeight(&_call); } - checkStackHeight(&_call); } void CodeTransform::operator()(FunctionalInstruction const& _instruction) @@ -169,11 +351,14 @@ void CodeTransform::operator()(Identifier const& _identifier) if (m_scope->lookup(_identifier.name, Scope::NonconstVisitor( [=](Scope::Variable& _var) { + // TODO: opportunity for optimization: Do not DUP if this is the last reference + // to the top most element of the stack if (int heightDiff = variableHeightDiff(_var, false)) m_assembly.appendInstruction(solidity::dupInstruction(heightDiff)); else // Store something to balance the stack m_assembly.appendConstant(u256(0)); + decreaseReference(_identifier.name, _var); }, [=](Scope::Label& _label) { @@ -217,6 +402,7 @@ void CodeTransform::operator()(Literal const& _literal) void CodeTransform::operator()(yul::Instruction const& _instruction) { + solAssert(!m_allowStackOpt, ""); solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMP, "Bare JUMP instruction used for EVM1.5"); solAssert(!m_evm15 || _instruction.instruction != solidity::Instruction::JUMPI, "Bare JUMPI instruction used for EVM1.5"); m_assembly.setSourceLocation(_instruction.location); @@ -329,7 +515,9 @@ void CodeTransform::operator()(FunctionDefinition const& _function) CodeTransform( m_assembly, m_info, - m_yul, + _function.body, + m_allowStackOpt, + m_dialect, m_evm15, m_identifierAccess, m_useNamedLabelsForFunctions, @@ -350,6 +538,7 @@ void CodeTransform::operator()(FunctionDefinition const& _function) if (!m_evm15) stackLayout.push_back(_function.returnVariables.size()); // Move return label to the top stackLayout += vector<int>(_function.parameters.size(), -1); // discard all arguments + for (size_t i = 0; i < _function.returnVariables.size(); ++i) stackLayout.push_back(i); // Move return values down, but keep order. @@ -475,20 +664,37 @@ void CodeTransform::visitExpression(Expression const& _expression) void CodeTransform::visitStatements(vector<Statement> const& _statements) { for (auto const& statement: _statements) + { + freeUnusedVariables(); boost::apply_visitor(*this, statement); + } + freeUnusedVariables(); } void CodeTransform::finalizeBlock(Block const& _block, int blockStartStackHeight) { m_assembly.setSourceLocation(_block.location); + freeUnusedVariables(); + // pop variables solAssert(m_info.scopes.at(&_block).get() == m_scope, ""); - for (size_t i = 0; i < m_scope->numberOfVariables(); ++i) - m_assembly.appendInstruction(solidity::Instruction::POP); + for (auto const& id: m_scope->identifiers) + if (id.second.type() == typeid(Scope::Variable)) + { + Scope::Variable const& var = boost::get<Scope::Variable>(id.second); + if (m_allowStackOpt) + { + solAssert(!m_context->variableStackHeights.count(&var), ""); + solAssert(!m_context->variableReferences.count(&var), ""); + m_stackAdjustment++; + } + else + m_assembly.appendInstruction(solidity::Instruction::POP); + } int deposit = m_assembly.stackHeight() - blockStartStackHeight; - solAssert(deposit == 0, "Invalid stack height at end of block."); + solAssert(deposit == 0, "Invalid stack height at end of block: " + to_string(deposit)); checkStackHeight(&_block); } @@ -502,13 +708,13 @@ void CodeTransform::generateMultiAssignment(vector<Identifier> const& _variableN void CodeTransform::generateAssignment(Identifier const& _variableName) { solAssert(m_scope, ""); - auto var = m_scope->lookup(_variableName.name); - if (var) + if (auto var = m_scope->lookup(_variableName.name)) { Scope::Variable const& _var = boost::get<Scope::Variable>(*var); if (int heightDiff = variableHeightDiff(_var, true)) m_assembly.appendInstruction(solidity::swapInstruction(heightDiff - 1)); m_assembly.appendInstruction(solidity::Instruction::POP); + decreaseReference(_variableName.name, _var); } else { diff --git a/libyul/backends/evm/EVMCodeTransform.h b/libyul/backends/evm/EVMCodeTransform.h index d559f85a..28ef4e45 100644 --- a/libyul/backends/evm/EVMCodeTransform.h +++ b/libyul/backends/evm/EVMCodeTransform.h @@ -20,8 +20,9 @@ #include <libyul/backends/evm/EVMAssembly.h> +#include <libyul/backends/evm/EVMDialect.h> +#include <libyul/optimiser/ASTWalker.h> #include <libyul/AsmDataForward.h> - #include <libyul/AsmScope.h> #include <boost/variant.hpp> @@ -37,6 +38,46 @@ namespace yul struct AsmAnalysisInfo; class EVMAssembly; +struct CodeTransformContext +{ + std::map<Scope::Label const*, AbstractAssembly::LabelID> labelIDs; + std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs; + std::map<Scope::Variable const*, int> variableStackHeights; + std::map<Scope::Variable const*, unsigned> variableReferences; +}; + +/** + * Counts the number of references to a variable. This includes actual (read) references + * but also assignments to the variable. It does not include the declaration itself or + * function parameters, but it does include function return parameters. + * + * This component can handle multiple variables of the same name. + * + * Can only be applied to strict assembly. + */ +class VariableReferenceCounter: public yul::ASTWalker +{ +public: + explicit VariableReferenceCounter( + CodeTransformContext& _context, + AsmAnalysisInfo const& _assemblyInfo + ): m_context(_context), m_info(_assemblyInfo) + {} + +public: + void operator()(Identifier const& _identifier); + void operator()(FunctionDefinition const&); + void operator()(ForLoop const&); + void operator()(Block const& _block); + +private: + void increaseRefIfFound(YulString _variableName); + + CodeTransformContext& m_context; + AsmAnalysisInfo const& m_info; + Scope* m_scope = nullptr; +}; + class CodeTransform: public boost::static_visitor<> { public: @@ -45,50 +86,51 @@ public: CodeTransform( AbstractAssembly& _assembly, AsmAnalysisInfo& _analysisInfo, - bool _yul = false, + Block const& _block, + EVMDialect const& _dialect, + bool _allowStackOpt = false, bool _evm15 = false, ExternalIdentifierAccess const& _identifierAccess = ExternalIdentifierAccess(), bool _useNamedLabelsForFunctions = false ): CodeTransform( _assembly, _analysisInfo, - _yul, + _block, + _allowStackOpt, + _dialect, _evm15, _identifierAccess, _useNamedLabelsForFunctions, _assembly.stackHeight(), - std::make_shared<Context>() + nullptr ) { } protected: - struct Context - { - std::map<Scope::Label const*, AbstractAssembly::LabelID> labelIDs; - std::map<Scope::Function const*, AbstractAssembly::LabelID> functionEntryIDs; - std::map<Scope::Variable const*, int> variableStackHeights; - }; + using Context = CodeTransformContext; CodeTransform( AbstractAssembly& _assembly, AsmAnalysisInfo& _analysisInfo, - bool _yul, + Block const& _block, + bool _allowStackOpt, + EVMDialect const& _dialect, bool _evm15, ExternalIdentifierAccess const& _identifierAccess, bool _useNamedLabelsForFunctions, int _stackAdjustment, std::shared_ptr<Context> _context - ): - m_assembly(_assembly), - m_info(_analysisInfo), - m_yul(_yul), - m_evm15(_evm15), - m_useNamedLabelsForFunctions(_useNamedLabelsForFunctions), - m_identifierAccess(_identifierAccess), - m_stackAdjustment(_stackAdjustment), - m_context(_context) - {} + ); + + void decreaseReference(YulString _name, Scope::Variable const& _var); + bool unreferenced(Scope::Variable const& _var) const; + /// Marks slots of variables that are not used anymore + /// and were defined in the current scope for reuse. + /// Also POPs unused topmost stack slots. + void freeUnusedVariables(); + /// Marks the stack slot of @a _var to be reused. + void deleteVariable(Scope::Variable const& _var); public: void operator()(Instruction const& _instruction); @@ -137,9 +179,10 @@ private: AbstractAssembly& m_assembly; AsmAnalysisInfo& m_info; Scope* m_scope = nullptr; - bool m_yul = false; - bool m_evm15 = false; - bool m_useNamedLabelsForFunctions = false; + EVMDialect const& m_dialect; + bool const m_allowStackOpt = true; + bool const m_evm15 = false; + bool const m_useNamedLabelsForFunctions = false; ExternalIdentifierAccess m_identifierAccess; /// Adjustment between the stack height as determined during the analysis phase /// and the stack height in the assembly. This is caused by an initial stack being present @@ -147,6 +190,12 @@ private: /// (EVM 1.0 or 1.5). int m_stackAdjustment = 0; std::shared_ptr<Context> m_context; + + /// Set of variables whose reference counter has reached zero, + /// and whose stack slot will be marked as unused once we reach + /// statement level in the scope where the variable was defined. + std::set<Scope::Variable const*> m_variablesScheduledForDeletion; + std::set<int> m_unusedStackSlots; }; } diff --git a/libyul/backends/evm/EVMDialect.cpp b/libyul/backends/evm/EVMDialect.cpp new file mode 100644 index 00000000..33ee19d4 --- /dev/null +++ b/libyul/backends/evm/EVMDialect.cpp @@ -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); +} diff --git a/libyul/backends/evm/EVMDialect.h b/libyul/backends/evm/EVMDialect.h new file mode 100644 index 00000000..feb00b03 --- /dev/null +++ b/libyul/backends/evm/EVMDialect.h @@ -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; +}; + +} diff --git a/libyul/backends/evm/EVMObjectCompiler.cpp b/libyul/backends/evm/EVMObjectCompiler.cpp index e7e8ad99..3f7634b2 100644 --- a/libyul/backends/evm/EVMObjectCompiler.cpp +++ b/libyul/backends/evm/EVMObjectCompiler.cpp @@ -21,19 +21,21 @@ #include <libyul/backends/evm/EVMObjectCompiler.h> #include <libyul/backends/evm/EVMCodeTransform.h> +#include <libyul/backends/evm/EVMDialect.h> + #include <libyul/Object.h> #include <libyul/Exceptions.h> using namespace yul; using namespace std; -void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, bool _yul, bool _evm15) +void EVMObjectCompiler::compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize) { - EVMObjectCompiler compiler(_assembly, _yul, _evm15); - compiler.run(_object); + EVMObjectCompiler compiler(_assembly, _dialect, _evm15); + compiler.run(_object, _optimize); } -void EVMObjectCompiler::run(Object& _object) +void EVMObjectCompiler::run(Object& _object, bool _optimize) { map<YulString, AbstractAssembly::SubID> subIDs; @@ -42,7 +44,7 @@ void EVMObjectCompiler::run(Object& _object) { auto subAssemblyAndID = m_assembly.createSubAssembly(); subIDs[subObject->name] = subAssemblyAndID.second; - compile(*subObject, *subAssemblyAndID.first, m_yul, m_evm15); + compile(*subObject, *subAssemblyAndID.first, m_dialect, m_evm15, _optimize); } else { @@ -50,6 +52,13 @@ void EVMObjectCompiler::run(Object& _object) 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."); - CodeTransform{m_assembly, *_object.analysisInfo, m_yul, m_evm15}(*_object.code); + yulAssert(_object.code, "No code."); + CodeTransform{m_assembly, *_object.analysisInfo, *_object.code, m_dialect, _optimize, m_evm15}(*_object.code); } diff --git a/libyul/backends/evm/EVMObjectCompiler.h b/libyul/backends/evm/EVMObjectCompiler.h index c7172e47..057521a8 100644 --- a/libyul/backends/evm/EVMObjectCompiler.h +++ b/libyul/backends/evm/EVMObjectCompiler.h @@ -23,20 +23,21 @@ namespace yul { struct Object; class AbstractAssembly; +struct EVMDialect; class EVMObjectCompiler { public: - static void compile(Object& _object, AbstractAssembly& _assembly, bool _yul, bool _evm15); + static void compile(Object& _object, AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15, bool _optimize); private: - EVMObjectCompiler(AbstractAssembly& _assembly, bool _yul, bool _evm15): - m_assembly(_assembly), m_yul(_yul), m_evm15(_evm15) + EVMObjectCompiler(AbstractAssembly& _assembly, EVMDialect& _dialect, bool _evm15): + m_assembly(_assembly), m_dialect(_dialect), m_evm15(_evm15) {} - void run(Object& _object); + void run(Object& _object, bool _optimize); AbstractAssembly& m_assembly; - bool m_yul = false; + EVMDialect& m_dialect; bool m_evm15 = false; }; diff --git a/libyul/optimiser/ASTWalker.cpp b/libyul/optimiser/ASTWalker.cpp index 0d568007..6adcb2e1 100644 --- a/libyul/optimiser/ASTWalker.cpp +++ b/libyul/optimiser/ASTWalker.cpp @@ -93,6 +93,16 @@ void ASTWalker::operator()(Block const& _block) walkVector(_block.statements); } +void ASTWalker::visit(Statement const& _st) +{ + boost::apply_visitor(*this, _st); +} + +void ASTWalker::visit(Expression const& _e) +{ + boost::apply_visitor(*this, _e); +} + void ASTModifier::operator()(FunctionalInstruction& _instr) { walkVector(_instr.arguments | boost::adaptors::reversed); @@ -155,3 +165,13 @@ void ASTModifier::operator()(Block& _block) { walkVector(_block.statements); } + +void ASTModifier::visit(Statement& _st) +{ + boost::apply_visitor(*this, _st); +} + +void ASTModifier::visit(Expression& _e) +{ + boost::apply_visitor(*this, _e); +} diff --git a/libyul/optimiser/ASTWalker.h b/libyul/optimiser/ASTWalker.h index b59b405e..272725f9 100644 --- a/libyul/optimiser/ASTWalker.h +++ b/libyul/optimiser/ASTWalker.h @@ -58,14 +58,8 @@ public: virtual void operator()(ForLoop const&); virtual void operator()(Block const& _block); - virtual void visit(Statement const& _st) - { - boost::apply_visitor(*this, _st); - } - virtual void visit(Expression const& _e) - { - boost::apply_visitor(*this, _e); - } + virtual void visit(Statement const& _st); + virtual void visit(Expression const& _e); protected: template <class T> @@ -99,14 +93,8 @@ public: virtual void operator()(ForLoop&); virtual void operator()(Block& _block); - virtual void visit(Statement& _st) - { - boost::apply_visitor(*this, _st); - } - virtual void visit(Expression& _e) - { - boost::apply_visitor(*this, _e); - } + virtual void visit(Statement& _st); + virtual void visit(Expression& _e); protected: template <class T> diff --git a/test/InteractiveTests.h b/test/InteractiveTests.h new file mode 100644 index 00000000..be076059 --- /dev/null +++ b/test/InteractiveTests.h @@ -0,0 +1,63 @@ +/* + 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/>. +*/ + +#pragma once + +#include <test/TestCase.h> +#include <test/libsolidity/ASTJSONTest.h> +#include <test/libsolidity/SyntaxTest.h> +#include <test/libsolidity/SMTCheckerJSONTest.h> +#include <test/libyul/YulOptimizerTest.h> +#include <test/libyul/ObjectCompilerTest.h> + +#include <boost/filesystem.hpp> + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +/** Container for all information regarding a testsuite */ +struct Testsuite +{ + char const* title; + boost::filesystem::path const path; + boost::filesystem::path const subpath; + bool smt; + bool ipc; + TestCase::TestCaseCreator testCaseCreator; +}; + + +/// Array of testsuits that can be run interactively as well as automatically +Testsuite const g_interactiveTestsuites[] = { +/* + Title Path Subpath SMT IPC Creator function */ + {"Yul Optimizer", "libyul", "yulOptimizerTests", false, false, &yul::test::YulOptimizerTest::create}, + {"Yul Object Compiler", "libyul", "objectCompiler", false, false, &yul::test::ObjectCompilerTest::create}, + {"Syntax", "libsolidity", "syntaxTests", false, false, &SyntaxTest::create}, + {"JSON AST", "libsolidity", "ASTJSON", false, false, &ASTJSONTest::create}, + {"SMT Checker", "libsolidity", "smtCheckerTests", true, false, &SyntaxTest::create}, + {"SMT Checker JSON", "libsolidity", "smtCheckerTestsJSON", true, false, &SMTCheckerTest::create} +}; + +} +} +} + diff --git a/test/boostTest.cpp b/test/boostTest.cpp index ff443d11..d6e75cb9 100644 --- a/test/boostTest.cpp +++ b/test/boostTest.cpp @@ -35,16 +35,13 @@ #pragma GCC diagnostic pop +#include <test/InteractiveTests.h> #include <test/Options.h> -#include <test/libsolidity/ASTJSONTest.h> -#include <test/libsolidity/SyntaxTest.h> -#include <test/libsolidity/SMTCheckerJSONTest.h> -#include <test/libyul/YulOptimizerTest.h> -#include <test/libyul/ObjectCompilerTest.h> #include <boost/algorithm/string.hpp> #include <boost/algorithm/string/predicate.hpp> #include <boost/filesystem.hpp> +#include <string> using namespace boost::unit_test; using namespace dev::solidity::test; @@ -129,46 +126,26 @@ test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] ) master_test_suite_t& master = framework::master_test_suite(); master.p_name.value = "SolidityTests"; dev::test::Options::get().validate(); - solAssert(registerTests( - master, - dev::test::Options::get().testPath / "libsolidity", - "syntaxTests", - SyntaxTest::create - ) > 0, "no syntax tests found"); - solAssert(registerTests( - master, - dev::test::Options::get().testPath / "libsolidity", - "ASTJSON", - ASTJSONTest::create - ) > 0, "no JSON AST tests found"); - solAssert(registerTests( - master, - dev::test::Options::get().testPath / "libyul", - "yulOptimizerTests", - yul::test::YulOptimizerTest::create - ) > 0, "no Yul Optimizer tests found"); - solAssert(registerTests( - master, - dev::test::Options::get().testPath / "libyul", - "objectCompiler", - yul::test::ObjectCompilerTest::create - ) > 0, "no Yul Object compiler tests found"); - if (!dev::test::Options::get().disableSMT) + + // Include the interactive tests in the automatic tests as well + for (auto const& ts: g_interactiveTestsuites) { - solAssert(registerTests( - master, - dev::test::Options::get().testPath / "libsolidity", - "smtCheckerTests", - SyntaxTest::create - ) > 0, "no SMT checker tests found"); + auto const& options = dev::test::Options::get(); + + if (ts.smt && options.disableSMT) + continue; + + if (ts.ipc && options.disableIPC) + continue; solAssert(registerTests( master, - dev::test::Options::get().testPath / "libsolidity", - "smtCheckerTestsJSON", - SMTCheckerTest::create - ) > 0, "no SMT checker JSON tests found"); + options.testPath / ts.path, + ts.subpath, + ts.testCaseCreator + ) > 0, std::string("no ") + ts.title + " tests found"); } + if (dev::test::Options::get().disableIPC) { for (auto suite: { @@ -183,11 +160,13 @@ test_suite* init_unit_test_suite( int /*argc*/, char* /*argv*/[] ) "LLLEndToEndTest", #endif "GasMeterTests", + "GasCostTests", "SolidityEndToEndTest", "SolidityOptimizer" }) removeTestSuite(suite); } + if (dev::test::Options::get().disableSMT) removeTestSuite("SMTChecker"); diff --git a/test/cmdlineTests/data_storage.sol b/test/cmdlineTests/data_storage.sol new file mode 100644 index 00000000..cc602cc9 --- /dev/null +++ b/test/cmdlineTests/data_storage.sol @@ -0,0 +1,15 @@ +pragma solidity >=0.0; + +contract C { + function f() pure public { + require(false, "1234567890123456789012345678901"); + require(false, "12345678901234567890123456789012"); + require(false, "123456789012345678901234567890123"); + require(false, "1234567890123456789012345678901234"); + require(false, "12345678901234567890123456789012345"); + require(false, "123456789012345678901234567890123456"); + require(false, "123456789012345678901234567890121234567890123456789012345678901"); + require(false, "1234567890123456789012345678901212345678901234567890123456789012"); + require(false, "12345678901234567890123456789012123456789012345678901234567890123"); + } +} diff --git a/test/cmdlineTests/data_storage.sol.args b/test/cmdlineTests/data_storage.sol.args new file mode 100644 index 00000000..3684987e --- /dev/null +++ b/test/cmdlineTests/data_storage.sol.args @@ -0,0 +1 @@ +--gas diff --git a/test/cmdlineTests/data_storage.sol.stdout b/test/cmdlineTests/data_storage.sol.stdout new file mode 100644 index 00000000..4a5250f7 --- /dev/null +++ b/test/cmdlineTests/data_storage.sol.stdout @@ -0,0 +1,7 @@ + +======= data_storage.sol:C ======= +Gas estimation: +construction: + 306 + 264400 = 264706 +external: + f(): 263 diff --git a/test/libsolidity/GasCosts.cpp b/test/libsolidity/GasCosts.cpp new file mode 100644 index 00000000..15658a91 --- /dev/null +++ b/test/libsolidity/GasCosts.cpp @@ -0,0 +1,89 @@ +/* + 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/>. +*/ +/** + * Tests that check that the cost of certain operations stay within range. + */ + +#include <test/libsolidity/SolidityExecutionFramework.h> + +#include <cmath> + +using namespace std; +using namespace langutil; +using namespace dev::eth; +using namespace dev::solidity; +using namespace dev::test; + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +#define CHECK_GAS(_gasNoOpt, _gasOpt, _tolerance) \ + do \ + { \ + u256 gasOpt{_gasOpt}; \ + u256 gasNoOpt{_gasNoOpt}; \ + u256 tolerance{_tolerance}; \ + u256 gas = m_optimize ? gasOpt : gasNoOpt; \ + u256 diff = gas < m_gasUsed ? m_gasUsed - gas : gas - m_gasUsed; \ + BOOST_CHECK_MESSAGE( \ + diff <= tolerance, \ + "Gas used: " + \ + m_gasUsed.str() + \ + " - expected: " + \ + gas.str() + \ + " (tolerance: " + \ + tolerance.str() + \ + ")" \ + ); \ + } while(0) + +BOOST_FIXTURE_TEST_SUITE(GasCostTests, SolidityExecutionFramework) + +BOOST_AUTO_TEST_CASE(string_storage) +{ + char const* sourceCode = R"( + contract C { + function f() pure public { + require(false, "Not Authorized. This function can only be called by the custodian or owner of this contract"); + } + } + )"; + compileAndRun(sourceCode); + + if (Options::get().evmVersion() <= EVMVersion::byzantium()) + CHECK_GAS(134435, 130591, 100); + else + CHECK_GAS(127225, 124873, 100); + if (Options::get().evmVersion() >= EVMVersion::byzantium()) + { + callContractFunction("f()"); + if (Options::get().evmVersion() == EVMVersion::byzantium()) + CHECK_GAS(21551, 21526, 20); + else + CHECK_GAS(21546, 21526, 20); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} +} diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index c6135a72..dfa60fc5 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -12245,7 +12245,7 @@ BOOST_AUTO_TEST_CASE(include_creation_bytecode_only_once) compileAndRun(sourceCode); BOOST_CHECK_LE( double(m_compiler.object("Double").bytecode.size()), - 1.1 * double(m_compiler.object("Single").bytecode.size()) + 1.2 * double(m_compiler.object("Single").bytecode.size()) ); } diff --git a/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple.sol b/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple.sol index 3793f411..2ecff63e 100644 --- a/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple.sol +++ b/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple.sol @@ -12,3 +12,4 @@ contract C } // ---- +// Warning: (153-156): Assertion checker does not yet implement tuples and inline arrays. diff --git a/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple_fail.sol b/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple_fail.sol index e3c80594..a9bde9e4 100644 --- a/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple_fail.sol +++ b/test/libsolidity/smtCheckerTests/functions/functions_identity_as_tuple_fail.sol @@ -12,4 +12,5 @@ contract C } // ---- +// Warning: (153-156): Assertion checker does not yet implement tuples and inline arrays. // Warning: (163-176): Assertion violation happens here diff --git a/test/libsolidity/smtCheckerTests/special/msg_data.sol b/test/libsolidity/smtCheckerTests/special/msg_data.sol index 7e748f09..b667f971 100644 --- a/test/libsolidity/smtCheckerTests/special/msg_data.sol +++ b/test/libsolidity/smtCheckerTests/special/msg_data.sol @@ -8,7 +8,6 @@ contract C } // ---- // Warning: (86-101): Assertion checker does not yet support this expression. -// Warning: (86-94): Assertion checker does not yet support this special variable. -// Warning: (86-94): Assertion checker does not yet implement this type. +// Warning: (86-94): Assertion checker does not yet support this global variable. // Warning: (86-101): Internal error: Expression undefined for SMT solver. // Warning: (79-106): Assertion violation happens here diff --git a/test/libsolidity/smtCheckerTests/types/bytes_1.sol b/test/libsolidity/smtCheckerTests/types/bytes_1.sol new file mode 100644 index 00000000..fb957421 --- /dev/null +++ b/test/libsolidity/smtCheckerTests/types/bytes_1.sol @@ -0,0 +1,16 @@ +pragma experimental SMTChecker; + +contract C +{ + function f(bytes memory b) public pure returns (bytes memory) { + bytes memory c = b; + return b; + } +} +// ---- +// Warning: (113-127): Unused local variable. +// Warning: (113-127): Assertion checker does not yet support the type of this variable. +// Warning: (58-72): Assertion checker does not yet support the type of this variable. +// Warning: (95-107): Assertion checker does not yet support the type of this variable. +// Warning: (130-131): Internal error: Expression undefined for SMT solver. +// Warning: (130-131): Assertion checker does not yet implement this type. diff --git a/test/libsolidity/smtCheckerTestsJSON/multi.json b/test/libsolidity/smtCheckerTestsJSON/multi.json index 2ed5150d..df45fbf4 100644 --- a/test/libsolidity/smtCheckerTestsJSON/multi.json +++ b/test/libsolidity/smtCheckerTestsJSON/multi.json @@ -1,11 +1,11 @@ { - "auxiliaryInput": - { - "smtlib2responses": - { - "0x0426cd198d1e7123a28ffac2b759a666b86508ad046babf5166500dd6d8ed308": "unsat\n(error \"line 31 column 26: model is not available\")", - "0xa51ca41ae407f5a727f27101cbc079834743cc8955f9f585582034ca634953f6": "sat\n((|EVALEXPR_0| 1))", - "0xe9477f683ff20aa57fcb08682150f86c5917e1d4c0686b278ab9b73446d0682c": "sat\n((|EVALEXPR_0| 0))" - } - } + "auxiliaryInput": + { + "smtlib2responses": + { + "0x47826ec8b83cfec30171b34f944aed6c33ecd12f02b9ad522ca45317c9bd5f45": "sat\n((|EVALEXPR_0| 1))", + "0x734c058efe9d6ec224de3cf2bdfa6c936a1c9c159c040c128d631145b2fed195": "unsat\n", + "0xa0c072acdbe5181dd56cbad8960cb5bb0b9e97fd598cfd895467bd73bbcca028": "sat\n((|EVALEXPR_0| 0))" + } + } } diff --git a/test/libsolidity/smtCheckerTestsJSON/simple.json b/test/libsolidity/smtCheckerTestsJSON/simple.json index fd976b63..b82d93b8 100644 --- a/test/libsolidity/smtCheckerTestsJSON/simple.json +++ b/test/libsolidity/smtCheckerTestsJSON/simple.json @@ -1,9 +1,9 @@ { - "auxiliaryInput": - { - "smtlib2responses": - { - "0xe9477f683ff20aa57fcb08682150f86c5917e1d4c0686b278ab9b73446d0682c": "sat\n((|EVALEXPR_0| 0))" - } - } + "auxiliaryInput": + { + "smtlib2responses": + { + "0xa0c072acdbe5181dd56cbad8960cb5bb0b9e97fd598cfd895467bd73bbcca028": "sat\n((|EVALEXPR_0| 0))\n" + } + } } diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/313_fixed_type_size_capabilities.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/313_fixed_type_size_capabilities.sol index 441cf81e..295cf4ea 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/313_fixed_type_size_capabilities.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/313_fixed_type_size_capabilities.sol @@ -10,4 +10,5 @@ contract test { } } // ---- -// Warning: (20-707): Function state mutability can be restricted to pure +// TypeError: (153-250): Type rational_const 9208...(70 digits omitted)...7637 / 1000...(71 digits omitted)...0000 is not implicitly convertible to expected type ufixed256x77, but it can be explicitly converted. +// TypeError: (470-566): Type rational_const -933...(70 digits omitted)...0123 / 1000...(70 digits omitted)...0000 is not implicitly convertible to expected type fixed256x76, but it can be explicitly converted. diff --git a/test/libsolidity/syntaxTests/types/rational_number_literal_to_fixed_implicit.sol b/test/libsolidity/syntaxTests/types/rational_number_literal_to_fixed_implicit.sol new file mode 100644 index 00000000..ca50e03b --- /dev/null +++ b/test/libsolidity/syntaxTests/types/rational_number_literal_to_fixed_implicit.sol @@ -0,0 +1,15 @@ +contract C { + function literalToUFixed() public pure { + ufixed8x2 a = 0.10; + ufixed8x2 b = 0.00; + ufixed8x2 c = 2.55; + a; b; c; + } + function literalToFixed() public pure { + fixed8x1 a = 0.1; + fixed8x1 b = 12.7; + fixed8x1 c = -12.8; + a; b; c; + } +} +// ---- diff --git a/test/libsolidity/syntaxTests/types/too_small_negative_numbers.sol b/test/libsolidity/syntaxTests/types/too_small_negative_numbers.sol index 66bd9a8e..f86ffcdd 100644 --- a/test/libsolidity/syntaxTests/types/too_small_negative_numbers.sol +++ b/test/libsolidity/syntaxTests/types/too_small_negative_numbers.sol @@ -2,3 +2,4 @@ contract C { fixed8x80 a = -1e-100; } // ---- +// TypeError: (29-36): Type rational_const -1 / 1000...(93 digits omitted)...0000 is not implicitly convertible to expected type fixed8x80, but it can be explicitly converted. diff --git a/test/libyul/Common.cpp b/test/libyul/Common.cpp index 0f2529de..a337fa8d 100644 --- a/test/libyul/Common.cpp +++ b/test/libyul/Common.cpp @@ -29,6 +29,7 @@ #include <libyul/AsmParser.h> #include <libyul/AsmAnalysis.h> #include <libyul/AsmPrinter.h> +#include <libyul/backends/evm/EVMDialect.h> #include <liblangutil/Scanner.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) { - Dialect dialect = _yul ? yul::Dialect::yul() : yul::Dialect::strictAssemblyForEVM(); + shared_ptr<Dialect> dialect = _yul ? yul::Dialect::yul() : yul::EVMDialect::strictAssemblyForEVM(); ErrorList errors; ErrorReporter errorReporter(errors); auto scanner = make_shared<Scanner>(CharStream(_source, "")); diff --git a/test/libyul/Parser.cpp b/test/libyul/Parser.cpp index eb2e735a..5e9fba86 100644 --- a/test/libyul/Parser.cpp +++ b/test/libyul/Parser.cpp @@ -49,7 +49,7 @@ namespace test namespace { -bool parse(string const& _source, Dialect const& _dialect, ErrorReporter& errorReporter) +bool parse(string const& _source, std::shared_ptr<Dialect> _dialect, ErrorReporter& errorReporter) { try { @@ -74,7 +74,7 @@ bool parse(string const& _source, Dialect const& _dialect, ErrorReporter& errorR 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; ErrorReporter errorReporter(errors); @@ -99,12 +99,12 @@ boost::optional<Error> parseAndReturnFirstError(string const& _source, Dialect c 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); } -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); @@ -306,16 +306,17 @@ BOOST_AUTO_TEST_CASE(if_statement_invalid) 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; } 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("{ 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); @@ -323,16 +324,17 @@ BOOST_AUTO_TEST_CASE(builtins_parser) 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)); 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); diff --git a/test/libyul/StackReuseCodegen.cpp b/test/libyul/StackReuseCodegen.cpp new file mode 100644 index 00000000..97be11d3 --- /dev/null +++ b/test/libyul/StackReuseCodegen.cpp @@ -0,0 +1,353 @@ +/* + 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/>. +*/ +/** + * Unit tests for stack-reusing code generator. + */ + +#include <test/Options.h> + +#include <libsolidity/interface/AssemblyStack.h> +#include <libevmasm/Instruction.h> + +using namespace std; + +namespace dev +{ +namespace yul +{ +namespace test +{ + +namespace +{ +string assemble(string const& _input) +{ + solidity::AssemblyStack asmStack; + BOOST_REQUIRE_MESSAGE(asmStack.parseAndAnalyze("", _input), "Source did not parse: " + _input); + return solidity::disassemble(asmStack.assemble(solidity::AssemblyStack::Machine::EVM, true).bytecode->bytecode); +} +} + +BOOST_AUTO_TEST_SUITE(StackReuseCodegen) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + string out = assemble("{}"); + BOOST_CHECK_EQUAL(out, ""); +} + +BOOST_AUTO_TEST_CASE(single_var) +{ + string out = assemble("{ let x }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x0 POP "); +} + +BOOST_AUTO_TEST_CASE(single_var_assigned) +{ + string out = assemble("{ let x := 1 }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x1 POP "); +} + +BOOST_AUTO_TEST_CASE(single_var_assigned_plus_code) +{ + string out = assemble("{ let x := 1 mstore(3, 4) }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x1 POP PUSH1 0x4 PUSH1 0x3 MSTORE "); +} + +BOOST_AUTO_TEST_CASE(single_var_assigned_plus_code_and_reused) +{ + string out = assemble("{ let x := 1 mstore(3, 4) pop(mload(x)) }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x1 PUSH1 0x4 PUSH1 0x3 MSTORE DUP1 MLOAD POP POP "); +} + +BOOST_AUTO_TEST_CASE(multi_reuse_single_slot) +{ + string out = assemble("{ let x := 1 x := 6 let y := 2 y := 4 }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x1 PUSH1 0x6 SWAP1 POP POP PUSH1 0x2 PUSH1 0x4 SWAP1 POP POP "); +} + +BOOST_AUTO_TEST_CASE(multi_reuse_single_slot_nested) +{ + string out = assemble("{ let x := 1 x := 6 { let y := 2 y := 4 } }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x1 PUSH1 0x6 SWAP1 POP POP PUSH1 0x2 PUSH1 0x4 SWAP1 POP POP "); +} + +BOOST_AUTO_TEST_CASE(multi_reuse_same_variable_name) +{ + string out = assemble("{ let z := mload(0) { let x := 1 x := 6 z := x } { let x := 2 z := x x := 4 } }"); + BOOST_CHECK_EQUAL(out, + "PUSH1 0x0 MLOAD " + "PUSH1 0x1 PUSH1 0x6 SWAP1 POP DUP1 SWAP2 POP POP " + "PUSH1 0x2 DUP1 SWAP2 POP PUSH1 0x4 SWAP1 POP POP " + "POP " + ); +} + +BOOST_AUTO_TEST_CASE(last_use_in_nested_block) +{ + string out = assemble("{ let z := 0 { pop(z) } let x := 1 }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x0 DUP1 POP POP PUSH1 0x1 POP "); +} + +BOOST_AUTO_TEST_CASE(if_) +{ + // z is only removed after the if (after the jumpdest) + string out = assemble("{ let z := mload(0) if z { let x := z } let t := 3 }"); + BOOST_CHECK_EQUAL(out, "PUSH1 0x0 MLOAD DUP1 ISZERO PUSH1 0xA JUMPI DUP1 POP JUMPDEST POP PUSH1 0x3 POP "); +} + +BOOST_AUTO_TEST_CASE(switch_) +{ + string out = assemble("{ let z := 0 switch z case 0 { let x := 2 let y := 3 } default { z := 3 } let t := 9 }"); + BOOST_CHECK_EQUAL(out, + "PUSH1 0x0 DUP1 " + "PUSH1 0x0 DUP2 EQ PUSH1 0x11 JUMPI " + "PUSH1 0x3 SWAP2 POP PUSH1 0x18 JUMP " + "JUMPDEST PUSH1 0x2 POP PUSH1 0x3 POP " + "JUMPDEST POP POP " // This is where z and its copy (switch condition) can be removed. + "PUSH1 0x9 POP " + ); +} + +BOOST_AUTO_TEST_CASE(reuse_slots) +{ + // x and y should reuse the slots of b and d + string out = assemble("{ let a, b, c, d let x := 2 let y := 3 mstore(x, a) mstore(y, c) }"); + BOOST_CHECK_EQUAL(out, + "PUSH1 0x0 PUSH1 0x0 PUSH1 0x0 PUSH1 0x0 " + "POP " // d is removed right away + "PUSH1 0x2 SWAP2 POP " // x is stored at b's slot + "PUSH1 0x3 DUP4 DUP4 MSTORE " + "DUP2 DUP2 MSTORE " + "POP POP POP POP " + ); +} + +BOOST_AUTO_TEST_CASE(for_1) +{ + // Special scoping rules, but can remove z early + string out = assemble("{ for { let z := 0 } 1 { } { let x := 3 } let t := 2 }"); + BOOST_CHECK_EQUAL(out, + "PUSH1 0x0 POP " + "JUMPDEST PUSH1 0x1 ISZERO PUSH1 0x11 JUMPI " + "PUSH1 0x3 POP JUMPDEST PUSH1 0x3 JUMP " + "JUMPDEST PUSH1 0x2 POP " + ); +} + +BOOST_AUTO_TEST_CASE(for_2) +{ + // Special scoping rules, cannot remove z until after the loop! + string out = assemble("{ for { let z := 0 } 1 { } { z := 8 let x := 3 } let t := 2 }"); + BOOST_CHECK_EQUAL(out, + "PUSH1 0x0 " + "JUMPDEST PUSH1 0x1 ISZERO PUSH1 0x14 JUMPI " + "PUSH1 0x8 SWAP1 POP " + "PUSH1 0x3 POP " + "JUMPDEST PUSH1 0x2 JUMP " + "JUMPDEST POP " // z is removed + "PUSH1 0x2 POP " + ); +} + +BOOST_AUTO_TEST_CASE(function_trivial) +{ + string in = R"({ + function f() { } + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0x5 JUMP JUMPDEST JUMP JUMPDEST " + ); +} + +BOOST_AUTO_TEST_CASE(function_retparam) +{ + string in = R"({ + function f() -> x, y { } + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0xB JUMP " + "JUMPDEST PUSH1 0x0 PUSH1 0x0 SWAP1 SWAP2 JUMP " + "JUMPDEST " + ); +} + +BOOST_AUTO_TEST_CASE(function_params) +{ + string in = R"({ + function f(a, b) { } + })"; + BOOST_CHECK_EQUAL(assemble(in), "PUSH1 0x7 JUMP JUMPDEST POP POP JUMP JUMPDEST "); +} + +BOOST_AUTO_TEST_CASE(function_params_and_retparams) +{ + string in = R"({ + function f(a, b, c, d) -> x, y { } + })"; + // This does not re-use the parameters for the return parameters + // We do not expect parameters to be fully unused, so the stack + // layout for a function is still fixed, even though parameters + // can be re-used. + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0x10 JUMP JUMPDEST PUSH1 0x0 PUSH1 0x0 SWAP5 POP SWAP5 SWAP3 POP POP POP JUMP JUMPDEST " + ); +} + +BOOST_AUTO_TEST_CASE(function_params_and_retparams_partly_unused) +{ + string in = R"({ + function f(a, b, c, d) -> x, y { b := 3 let s := 9 y := 2 mstore(s, y) } + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0x1E JUMP " + "JUMPDEST PUSH1 0x0 PUSH1 0x0 " + "PUSH1 0x3 SWAP4 POP " + "PUSH1 0x9 PUSH1 0x2 SWAP2 POP " + "DUP2 DUP2 MSTORE " + "POP SWAP5 POP SWAP5 SWAP3 POP POP POP JUMP " + "JUMPDEST " + ); +} + +BOOST_AUTO_TEST_CASE(function_with_body_embedded) +{ + string in = R"({ + let b := 3 + function f(a, r) -> t { + // r could be removed right away, but a cannot - this is not implemented, though + let x := a a := 3 t := a + } + b := 7 + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0x3 PUSH1 " + "0x16 JUMP " + "JUMPDEST PUSH1 0x0 " // start of f, initialize t + "DUP2 POP " // let x := a + "PUSH1 0x3 SWAP2 POP " + "DUP2 SWAP1 POP " + "SWAP3 SWAP2 POP POP JUMP " + "JUMPDEST PUSH1 0x7 SWAP1 " + "POP POP " + ); +} + +BOOST_AUTO_TEST_CASE(function_call) +{ + string in = R"({ + let b := f(1, 2) + function f(a, r) -> t { } + b := f(3, 4) + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0x9 PUSH1 0x2 PUSH1 0x1 PUSH1 0xD JUMP " + "JUMPDEST PUSH1 0x15 JUMP " // jump over f + "JUMPDEST PUSH1 0x0 SWAP3 SWAP2 POP POP JUMP " // f + "JUMPDEST PUSH1 0x1F PUSH1 0x4 PUSH1 0x3 PUSH1 0xD JUMP " + "JUMPDEST SWAP1 POP POP " + ); +} + + +BOOST_AUTO_TEST_CASE(functions_multi_return) +{ + string in = R"({ + function f(a, b) -> t { } + function g() -> r, s { } + let x := f(1, 2) + x := f(3, 4) + let y, z := g() + y, z := g() + let unused := 7 + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0xB JUMP " + "JUMPDEST PUSH1 0x0 SWAP3 SWAP2 POP POP JUMP " // f + "JUMPDEST PUSH1 0x17 JUMP " + "JUMPDEST PUSH1 0x0 PUSH1 0x0 SWAP1 SWAP2 JUMP " // g + "JUMPDEST PUSH1 0x21 PUSH1 0x2 PUSH1 0x1 PUSH1 0x3 JUMP " // f(1, 2) + "JUMPDEST PUSH1 0x2B PUSH1 0x4 PUSH1 0x3 PUSH1 0x3 JUMP " // f(3, 4) + "JUMPDEST SWAP1 POP " // assignment to x + "POP " // remove x + "PUSH1 0x34 PUSH1 0xF JUMP " // g() + "JUMPDEST PUSH1 0x3A PUSH1 0xF JUMP " // g() + "JUMPDEST SWAP2 POP SWAP2 POP " // assignments + "POP POP " // removal of y and z + "PUSH1 0x7 POP " + ); +} + +BOOST_AUTO_TEST_CASE(reuse_slots_function) +{ + string in = R"({ + function f() -> x, y, z, t {} + let a, b, c, d := f() let x1 := 2 let y1 := 3 mstore(x1, a) mstore(y1, c) + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0x11 JUMP " + "JUMPDEST PUSH1 0x0 PUSH1 0x0 PUSH1 0x0 PUSH1 0x0 SWAP1 SWAP2 SWAP3 SWAP4 JUMP " + "JUMPDEST PUSH1 0x17 PUSH1 0x3 JUMP " + // Stack: a b c d + "JUMPDEST POP " // d is unused + // Stack: a b c + "PUSH1 0x2 SWAP2 POP " // x1 reuses b's slot + "PUSH1 0x3 " + // Stack: a x1 c y1 + "DUP4 DUP4 MSTORE " + "DUP2 DUP2 MSTORE " + "POP POP POP POP " + ); +} + +BOOST_AUTO_TEST_CASE(reuse_slots_function_with_gaps) +{ + string in = R"({ + // Only x3 is actually used, the slots of + // x1 and x2 will be reused right away. + let x1 := 5 let x2 := 6 let x3 := 7 + mstore(x1, x2) + function f() -> x, y, z, t {} + let a, b, c, d := f() mstore(x3, a) mstore(c, d) + })"; + BOOST_CHECK_EQUAL(assemble(in), + "PUSH1 0x5 PUSH1 0x6 PUSH1 0x7 " + "DUP2 DUP4 MSTORE " + "PUSH1 0x1A JUMP " // jump across function + "JUMPDEST PUSH1 0x0 PUSH1 0x0 PUSH1 0x0 PUSH1 0x0 SWAP1 SWAP2 SWAP3 SWAP4 JUMP " + "JUMPDEST PUSH1 0x20 PUSH1 0xC JUMP " + // stack: x1 x2 x3 a b c d + "JUMPDEST SWAP6 POP " // move d into x1 + // stack: d x2 x3 a b c + "SWAP4 POP " + // stack: d c x3 a b + "POP " + // stack: d c x3 a + "DUP1 DUP3 MSTORE " + "POP POP " + // stack: d c + "DUP2 DUP2 MSTORE " + "POP POP " + ); +} + + +BOOST_AUTO_TEST_SUITE_END() + +} +} +} diff --git a/test/libyul/YulOptimizerTest.cpp b/test/libyul/YulOptimizerTest.cpp index ba7eced3..68226e33 100644 --- a/test/libyul/YulOptimizerTest.cpp +++ b/test/libyul/YulOptimizerTest.cpp @@ -41,6 +41,7 @@ #include <libyul/optimiser/RedundantAssignEliminator.h> #include <libyul/optimiser/StructuralSimplifier.h> #include <libyul/optimiser/Suite.h> +#include <libyul/backends/evm/EVMDialect.h> #include <libyul/AsmPrinter.h> #include <libyul/AsmParser.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) { - 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; ErrorReporter errorReporter(errors); shared_ptr<Scanner> scanner = make_shared<Scanner>(CharStream(m_source, "")); diff --git a/test/libyul/objectCompiler/datacopy.yul b/test/libyul/objectCompiler/datacopy.yul new file mode 100644 index 00000000..2259e5dc --- /dev/null +++ b/test/libyul/objectCompiler/datacopy.yul @@ -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 diff --git a/test/libyul/objectCompiler/dataoffset_code.yul b/test/libyul/objectCompiler/dataoffset_code.yul new file mode 100644 index 00000000..725267f2 --- /dev/null +++ b/test/libyul/objectCompiler/dataoffset_code.yul @@ -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 diff --git a/test/libyul/objectCompiler/dataoffset_data.yul b/test/libyul/objectCompiler/dataoffset_data.yul new file mode 100644 index 00000000..9a0a461d --- /dev/null +++ b/test/libyul/objectCompiler/dataoffset_data.yul @@ -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 diff --git a/test/libyul/objectCompiler/dataoffset_self.yul b/test/libyul/objectCompiler/dataoffset_self.yul new file mode 100644 index 00000000..b7740735 --- /dev/null +++ b/test/libyul/objectCompiler/dataoffset_self.yul @@ -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 diff --git a/test/libyul/objectCompiler/datasize_code.yul b/test/libyul/objectCompiler/datasize_code.yul new file mode 100644 index 00000000..cff68515 --- /dev/null +++ b/test/libyul/objectCompiler/datasize_code.yul @@ -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 diff --git a/test/libyul/objectCompiler/datasize_data.yul b/test/libyul/objectCompiler/datasize_data.yul new file mode 100644 index 00000000..f8341469 --- /dev/null +++ b/test/libyul/objectCompiler/datasize_data.yul @@ -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 diff --git a/test/libyul/objectCompiler/datasize_self.yul b/test/libyul/objectCompiler/datasize_self.yul new file mode 100644 index 00000000..4579fe67 --- /dev/null +++ b/test/libyul/objectCompiler/datasize_self.yul @@ -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 diff --git a/test/tools/isoltest.cpp b/test/tools/isoltest.cpp index 13585887..e5578045 100644 --- a/test/tools/isoltest.cpp +++ b/test/tools/isoltest.cpp @@ -19,11 +19,7 @@ #include <test/Common.h> #include <test/libsolidity/AnalysisFramework.h> -#include <test/libsolidity/SyntaxTest.h> -#include <test/libsolidity/ASTJSONTest.h> -#include <test/libsolidity/SMTCheckerJSONTest.h> -#include <test/libyul/YulOptimizerTest.h> -#include <test/libyul/ObjectCompilerTest.h> +#include <test/InteractiveTests.h> #include <boost/algorithm/string.hpp> #include <boost/algorithm/string/replace.hpp> @@ -380,59 +376,13 @@ Allowed options)", TestStats global_stats{0, 0}; // Actually run the tests. - // If you add new tests here, you also have to add them in boostTest.cpp - if (auto stats = runTestSuite("Syntax", testPath / "libsolidity", "syntaxTests", SyntaxTest::create, formatted)) - global_stats += *stats; - else - return 1; - - if (auto stats = runTestSuite("JSON AST", testPath / "libsolidity", "ASTJSON", ASTJSONTest::create, formatted)) - global_stats += *stats; - else - return 1; - - if (auto stats = runTestSuite( - "Yul Optimizer", - testPath / "libyul", - "yulOptimizerTests", - yul::test::YulOptimizerTest::create, - formatted - )) - global_stats += *stats; - else - return 1; - - if (auto stats = runTestSuite( - "Yul Object Compiler", - testPath / "libyul", - "objectCompiler", - yul::test::ObjectCompilerTest::create, - formatted - )) - global_stats += *stats; - else - return 1; - - if (!disableSMT) + // Interactive tests are added in InteractiveTests.h + for (auto const& ts: g_interactiveTestsuites) { - if (auto stats = runTestSuite( - "SMT Checker", - testPath / "libsolidity", - "smtCheckerTests", - SyntaxTest::create, - formatted - )) - global_stats += *stats; - else - return 1; + if (ts.smt && disableSMT) + continue; - if (auto stats = runTestSuite( - "SMT Checker JSON", - testPath / "libsolidity", - "smtCheckerTestsJSON", - SMTCheckerTest::create, - formatted - )) + if (auto stats = runTestSuite(ts.title, testPath / ts.path, ts.subpath, ts.testCaseCreator, formatted)) global_stats += *stats; else return 1; diff --git a/test/tools/yulopti.cpp b/test/tools/yulopti.cpp index 477de42f..3dc35f73 100644 --- a/test/tools/yulopti.cpp +++ b/test/tools/yulopti.cpp @@ -49,6 +49,8 @@ #include <libyul/optimiser/StructuralSimplifier.h> #include <libyul/optimiser/VarDeclPropagator.h> +#include <libyul/backends/evm/EVMDialect.h> + #include <libdevcore/JSON.h> #include <boost/program_options.hpp> @@ -83,7 +85,7 @@ public: { ErrorReporter errorReporter(m_errors); 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()) { cout << "Error parsing source." << endl; @@ -96,7 +98,7 @@ public: errorReporter, EVMVersion::byzantium(), langutil::Error::Type::SyntaxError, - Dialect::strictAssemblyForEVM() + EVMDialect::strictAssemblyForEVM() ); if (!analyzer.analyze(*m_ast) || !errorReporter.errors().empty()) { |