diff --git a/libsolidity/codegen/CompilerContext.cpp b/libsolidity/codegen/CompilerContext.cpp index ef5534a3c..c57e4f8b0 100644 --- a/libsolidity/codegen/CompilerContext.cpp +++ b/libsolidity/codegen/CompilerContext.cpp @@ -387,7 +387,7 @@ void CompilerContext::appendInlineAssembly( yul::EVMDialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(m_evmVersion); auto parserResult = yul::Parser(errorReporter, dialect).parse(scanner, false); #ifdef SOL_OUTPUT_ASM - cout << yul::AsmPrinter()(*parserResult) << endl; + cout << yul::AsmPrinter(&dialect)(*parserResult) << endl; #endif auto reportError = [&](string const& _context) @@ -438,7 +438,7 @@ void CompilerContext::appendInlineAssembly( #ifdef SOL_OUTPUT_ASM cout << "After optimizer:" << endl; - cout << yul::AsmPrinter()(*parserResult) << endl; + cout << yul::AsmPrinter(&dialect)(*parserResult) << endl; #endif } diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index fe3e2a7ef..16c11e3e7 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -892,6 +892,7 @@ bool IRGeneratorForStatements::visit(InlineAssembly const& _inlineAsm) solAssert(holds_alternative(modified), ""); + // Do not provide dialect so that we get the full type information. m_code << yul::AsmPrinter()(std::get(std::move(modified))) << "\n"; return false; } diff --git a/libyul/AsmAnalysis.cpp b/libyul/AsmAnalysis.cpp index 148eeabfe..a117f8cd9 100644 --- a/libyul/AsmAnalysis.cpp +++ b/libyul/AsmAnalysis.cpp @@ -608,7 +608,7 @@ Scope& AsmAnalyzer::scope(Block const* _block) } void AsmAnalyzer::expectValidType(YulString _type, SourceLocation const& _location) { - if (!_type.empty() && !contains(m_dialect.types, _type)) + if (!_type.empty() && !m_dialect.types.count(_type)) m_errorReporter.typeError( _location, "\"" + _type.str() + "\" is not a valid type (user defined types are not yet supported)." diff --git a/libyul/AsmParser.cpp b/libyul/AsmParser.cpp index 3b955b96f..fcfb46261 100644 --- a/libyul/AsmParser.cpp +++ b/libyul/AsmParser.cpp @@ -366,7 +366,7 @@ Parser::ElementaryOperation Parser::parseElementaryOperation() location(), kind, YulString{currentLiteral()}, - {} + kind == LiteralKind::Boolean ? m_dialect.boolType : m_dialect.defaultType }; advance(); if (currentToken() == Token::Colon) @@ -497,6 +497,9 @@ TypedName Parser::parseTypedName() typedName.location.end = endPosition(); typedName.type = expectAsmIdentifier(); } + else + typedName.type = m_dialect.defaultType; + return typedName; } diff --git a/libyul/AsmPrinter.cpp b/libyul/AsmPrinter.cpp index a62db6123..671a0fa24 100644 --- a/libyul/AsmPrinter.cpp +++ b/libyul/AsmPrinter.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include @@ -238,7 +239,8 @@ string AsmPrinter::formatTypedName(TypedName _variable) const string AsmPrinter::appendTypeName(YulString _type) const { - if (!_type.empty()) + if (_type.empty() || (m_dialect && _type == m_dialect->defaultType)) + return {}; + else return ":" + _type.str(); - return ""; } diff --git a/libyul/AsmPrinter.h b/libyul/AsmPrinter.h index 30569922d..9edc3a7c3 100644 --- a/libyul/AsmPrinter.h +++ b/libyul/AsmPrinter.h @@ -30,10 +30,16 @@ namespace solidity::yul { struct Dialect; +/** + * Converts a parsed Yul AST into readable string representation. + * Ignores source locations. + * If a dialect is provided, the dialect's default type is omitted. + */ class AsmPrinter { public: - explicit AsmPrinter() {} + AsmPrinter() {} + explicit AsmPrinter(Dialect const& _dialect): m_dialect(&_dialect) {} std::string operator()(Literal const& _literal) const; std::string operator()(Identifier const& _identifier) const; @@ -53,6 +59,8 @@ public: private: std::string formatTypedName(TypedName _variable) const; std::string appendTypeName(YulString _type) const; + + Dialect const* m_dialect = nullptr; }; } diff --git a/libyul/Dialect.h b/libyul/Dialect.h index b96e1851c..f08a04e65 100644 --- a/libyul/Dialect.h +++ b/libyul/Dialect.h @@ -51,7 +51,7 @@ struct Dialect: boost::noncopyable YulString defaultType; /// Type used for the literals "true" and "false". YulString boolType; - std::vector types; + std::set types; /// @returns the builtin function of the given name or a nullptr if it is not a builtin function. virtual BuiltinFunction const* builtin(YulString /*_name*/) const { return nullptr; } diff --git a/libyul/backends/evm/EVMDialect.cpp b/libyul/backends/evm/EVMDialect.cpp index 399445efa..baab0a268 100644 --- a/libyul/backends/evm/EVMDialect.cpp +++ b/libyul/backends/evm/EVMDialect.cpp @@ -218,6 +218,9 @@ EVMDialectTyped::EVMDialectTyped(langutil::EVMVersion _evmVersion, bool _objectA EVMDialect(_evmVersion, _objectAccess) { defaultType = "u256"_yulstring; + boolType = "bool"_yulstring; + types = {defaultType, boolType}; + m_functions["lt"_yulstring].returns = {"bool"_yulstring}; m_functions["gt"_yulstring].returns = {"bool"_yulstring}; m_functions["slt"_yulstring].returns = {"bool"_yulstring}; diff --git a/test/libyul/YulOptimizerTest.cpp b/test/libyul/YulOptimizerTest.cpp index 609473acc..44be41fc4 100644 --- a/test/libyul/YulOptimizerTest.cpp +++ b/test/libyul/YulOptimizerTest.cpp @@ -365,7 +365,7 @@ TestCase::TestResult YulOptimizerTest::run(ostream& _stream, string const& _line return TestResult::FatalError; } - m_obtainedResult = AsmPrinter{}(*m_ast) + "\n"; + m_obtainedResult = AsmPrinter{*m_dialect}(*m_ast) + "\n"; if (m_optimizerStep != m_validatedSettings["step"]) { diff --git a/test/tools/yulopti.cpp b/test/tools/yulopti.cpp index 23e535734..998c25bae 100644 --- a/test/tools/yulopti.cpp +++ b/test/tools/yulopti.cpp @@ -248,7 +248,7 @@ public: default: cout << "Unknown option." << endl; } - source = AsmPrinter{}(*m_ast); + source = AsmPrinter{m_dialect}(*m_ast); } }