/* 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 . */ // SPDX-License-Identifier: GPL-3.0 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace solidity; using namespace solidity::langutil; using namespace solidity::yul; using namespace solidity::util; using namespace solidity::phaser; namespace solidity::phaser { ostream& operator<<(ostream& _stream, Program const& _program); } ostream& std::operator<<(ostream& _outputStream, ErrorList const& _errors) { SourceReferenceFormatter formatter(_outputStream, true, false); for (auto const& error: _errors) formatter.printErrorInformation(*error); return _outputStream; } Program::Program(Program const& program): m_ast(make_unique(get(ASTCopier{}(*program.m_ast)))), m_dialect{program.m_dialect}, m_nameDispenser(program.m_nameDispenser) { } variant Program::load(CharStream& _sourceCode) { // ASSUMPTION: parseSource() rewinds the stream on its own Dialect const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{}); variant, ErrorList> astOrErrors = parseObject(dialect, _sourceCode); if (holds_alternative(astOrErrors)) return get(astOrErrors); variant, ErrorList> analysisInfoOrErrors = analyzeAST( dialect, *get>(astOrErrors) ); if (holds_alternative(analysisInfoOrErrors)) return get(analysisInfoOrErrors); Program program( dialect, disambiguateAST( dialect, *get>(astOrErrors), *get>(analysisInfoOrErrors) ) ); program.optimise({ FunctionHoister::name, FunctionGrouper::name, ForLoopInitRewriter::name, }); return program; } void Program::optimise(vector const& _optimisationSteps) { m_ast = applyOptimisationSteps(m_dialect, m_nameDispenser, move(m_ast), _optimisationSteps); } ostream& phaser::operator<<(ostream& _stream, Program const& _program) { return _stream << AsmPrinter()(*_program.m_ast); } string Program::toJson() const { Json::Value serializedAst = AsmJsonConverter(0)(*m_ast); return jsonPrettyPrint(removeNullMembers(std::move(serializedAst))); } variant, ErrorList> Program::parseObject(Dialect const& _dialect, CharStream _source) { ErrorList errors; ErrorReporter errorReporter(errors); auto scanner = make_shared(move(_source)); ObjectParser parser(errorReporter, _dialect); shared_ptr object = parser.parse(scanner, false); if (object == nullptr || !errorReporter.errors().empty()) // NOTE: It's possible to get errors even if the returned object is non-null. // For example when there are errors in a nested object. return errors; Object* deployedObject = nullptr; if (object->subObjects.size() > 0) for (auto& subObject: object->subObjects) // solc --ir produces an object with a subobject of the same name as the outer object // but suffixed with "_deployed". // The other object references the nested one which makes analysis fail. Below we try to // extract just the nested one for that reason. This is just a heuristic. If there's no // subobject with such a suffix we fall back to accepting the whole object as is. if (subObject != nullptr && subObject->name.str() == object->name.str() + "_deployed") { deployedObject = dynamic_cast(subObject.get()); if (deployedObject != nullptr) break; } Object* selectedObject = (deployedObject != nullptr ? deployedObject : object.get()); // NOTE: I'm making a copy of the whole AST to get unique_ptr rather than shared_ptr. // This is a slight performance hit but it's much less than the parsing itself. // unique_ptr lets me be sure that two Program instances can never share the AST by mistake. // The public API of the class does not provide access to the smart pointer so it won't be hard // to switch to shared_ptr if the copying turns out to be an issue (though it would be better // to refactor ObjectParser and Object to use unique_ptr instead). auto astCopy = make_unique(get(ASTCopier{}(*selectedObject->code))); return variant, ErrorList>(move(astCopy)); } variant, ErrorList> Program::analyzeAST(Dialect const& _dialect, Block const& _ast) { ErrorList errors; ErrorReporter errorReporter(errors); auto analysisInfo = make_unique(); AsmAnalyzer analyzer(*analysisInfo, errorReporter, _dialect); bool analysisSuccessful = analyzer.analyze(_ast); if (!analysisSuccessful) return errors; assert(errorReporter.errors().empty()); return variant, ErrorList>(move(analysisInfo)); } unique_ptr Program::disambiguateAST( Dialect const& _dialect, Block const& _ast, AsmAnalysisInfo const& _analysisInfo ) { set const externallyUsedIdentifiers = {}; Disambiguator disambiguator(_dialect, _analysisInfo, externallyUsedIdentifiers); return make_unique(get(disambiguator(_ast))); } unique_ptr Program::applyOptimisationSteps( Dialect const& _dialect, NameDispenser& _nameDispenser, unique_ptr _ast, vector const& _optimisationSteps ) { // An empty set of reserved identifiers. It could be a constructor parameter but I don't // think it would be useful in this tool. Other tools (like yulopti) have it empty too. set const externallyUsedIdentifiers = {}; OptimiserStepContext context{ _dialect, _nameDispenser, externallyUsedIdentifiers, frontend::OptimiserSettings::standard().expectedExecutionsPerDeployment }; for (string const& step: _optimisationSteps) OptimiserSuite::allSteps().at(step)->run(context, *_ast); return _ast; } size_t Program::computeCodeSize(Block const& _ast, CodeWeights const& _weights) { return CodeSize::codeSizeIncludingFunctions(_ast, _weights); }