/* 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 . */ #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); } Program Program::load(string const& _sourcePath) { Dialect const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{}); unique_ptr ast = parseSource(dialect, loadSource(_sourcePath)); unique_ptr analysisInfo = analyzeAST(dialect, *ast); Program program( dialect, disambiguateAST(dialect, *ast, *analysisInfo) ); program.optimise({ FunctionHoister::name, FunctionGrouper::name, ForLoopInitRewriter::name, }); return program; } void Program::optimise(vector const& _optimisationSteps) { applyOptimisationSteps(m_dialect, m_nameDispenser, *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(serializedAst); } CharStream Program::loadSource(string const& _sourcePath) { assertThrow(boost::filesystem::exists(_sourcePath), InvalidProgram, "Source file does not exist"); string sourceCode = readFileAsString(_sourcePath); return CharStream(sourceCode, _sourcePath); } unique_ptr Program::parseSource(Dialect const& _dialect, CharStream _source) { ErrorList errors; ErrorReporter errorReporter(errors); auto scanner = make_shared(move(_source)); Parser parser(errorReporter, _dialect); unique_ptr ast = parser.parse(scanner, false); assertThrow(ast != nullptr, InvalidProgram, "Error parsing source"); assert(errorReporter.errors().empty()); return ast; } unique_ptr 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); assertThrow(analysisSuccessful, InvalidProgram, "Error analyzing source"); assert(errorReporter.errors().empty()); return 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))); } void Program::applyOptimisationSteps( Dialect const& _dialect, NameDispenser& _nameDispenser, Block& _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}; for (string const& step: _optimisationSteps) OptimiserSuite::allSteps().at(step)->run(context, _ast); } size_t Program::computeCodeSize(Block const& _ast) { return CodeSize::codeSizeIncludingFunctions(_ast); }