solidity/tools/yulPhaser/Program.h
cameel 785f65d0f5 [yul-phaser] Make Program and Population classes accept source code rather than file path
- I need some sample .yul files for testing but I see that existing tests generally have source code hard-coded in them rather than in standalone .yul files. There are lots of .yul files but they seem to be automatically processed by a special test case rather loaded ad-hoc by manually created tests.
- Program and Population required a file name until now. I'm making them accept loaded source code to be able to give them data hard-coded in a test.
2020-02-05 18:13:30 +01:00

114 lines
2.9 KiB
C++

/*
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 <libyul/optimiser/NameDispenser.h>
#include <libyul/AsmData.h>
#include <boost/noncopyable.hpp>
#include <optional>
#include <ostream>
#include <set>
#include <string>
#include <vector>
namespace solidity::langutil
{
class CharStream;
}
namespace solidity::yul
{
struct AsmAnalysisInfo;
struct Dialect;
}
namespace solidity::phaser
{
/**
* Class representing parsed and analysed Yul program that we can apply optimisations to.
* The program is already disambiguated and has several prerequisite optimiser steps applied to it
* so that the requirements of any possible step that could be later applied by the user are
* already satisfied.
*
* The class allows the user to apply extra optimisations and obtain metrics and general
* information about the resulting syntax tree.
*/
class Program: private boost::noncopyable
{
public:
Program(Program&& program):
m_ast(std::move(program.m_ast)),
m_dialect{program.m_dialect},
m_nameDispenser(std::move(program.m_nameDispenser))
{}
Program operator=(Program&& program) = delete;
static Program load(langutil::CharStream& _sourceCode);
void optimise(std::vector<std::string> const& _optimisationSteps);
size_t codeSize() const { return computeCodeSize(*m_ast); }
yul::Block const& ast() const { return *m_ast; }
friend std::ostream& operator<<(std::ostream& _stream, Program const& _program);
std::string toJson() const;
private:
Program(
yul::Dialect const& _dialect,
std::unique_ptr<yul::Block> _ast
):
m_ast(std::move(_ast)),
m_dialect{_dialect},
m_nameDispenser(_dialect, *m_ast, {})
{}
static std::unique_ptr<yul::Block> parseSource(
yul::Dialect const& _dialect,
langutil::CharStream _source
);
static std::unique_ptr<yul::AsmAnalysisInfo> analyzeAST(
yul::Dialect const& _dialect,
yul::Block const& _ast
);
static std::unique_ptr<yul::Block> disambiguateAST(
yul::Dialect const& _dialect,
yul::Block const& _ast,
yul::AsmAnalysisInfo const& _analysisInfo
);
static void applyOptimisationSteps(
yul::Dialect const& _dialect,
yul::NameDispenser& _nameDispenser,
yul::Block& _ast,
std::vector<std::string> const& _optimisationSteps
);
static size_t computeCodeSize(yul::Block const& _ast);
std::unique_ptr<yul::Block> m_ast;
yul::Dialect const& m_dialect;
yul::NameDispenser m_nameDispenser;
};
}