2020-01-17 06:34:18 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-01-17 06:34:18 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <libyul/optimiser/NameDispenser.h>
|
2020-10-29 14:00:27 +00:00
|
|
|
#include <libyul/AST.h>
|
2020-01-17 06:34:18 +00:00
|
|
|
|
2020-02-28 02:33:46 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2021-05-17 09:48:48 +00:00
|
|
|
#include <cstddef>
|
2020-01-17 06:34:18 +00:00
|
|
|
#include <optional>
|
2020-01-23 09:27:50 +00:00
|
|
|
#include <ostream>
|
2020-01-17 06:34:18 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2020-02-28 02:33:46 +00:00
|
|
|
#include <variant>
|
2020-01-17 06:34:18 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace solidity::langutil
|
|
|
|
{
|
|
|
|
|
|
|
|
class CharStream;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace solidity::yul
|
|
|
|
{
|
|
|
|
|
|
|
|
struct AsmAnalysisInfo;
|
|
|
|
struct Dialect;
|
2020-02-29 20:55:32 +00:00
|
|
|
struct CodeWeights;
|
2020-01-17 06:34:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-28 02:25:00 +00:00
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& _outputStream, solidity::langutil::ErrorList const& _errors);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-17 06:34:18 +00:00
|
|
|
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.
|
|
|
|
*/
|
2020-01-31 12:23:57 +00:00
|
|
|
class Program
|
2020-01-17 06:34:18 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-01-31 12:23:57 +00:00
|
|
|
Program(Program const& program);
|
2020-01-22 18:38:19 +00:00
|
|
|
Program(Program&& program):
|
|
|
|
m_ast(std::move(program.m_ast)),
|
2020-01-24 22:22:01 +00:00
|
|
|
m_dialect{program.m_dialect},
|
|
|
|
m_nameDispenser(std::move(program.m_nameDispenser))
|
2020-01-22 18:38:19 +00:00
|
|
|
{}
|
2020-01-31 12:23:57 +00:00
|
|
|
Program operator=(Program const& program) = delete;
|
2020-01-22 18:38:19 +00:00
|
|
|
Program operator=(Program&& program) = delete;
|
|
|
|
|
2020-02-28 02:33:46 +00:00
|
|
|
static std::variant<Program, langutil::ErrorList> load(langutil::CharStream& _sourceCode);
|
2020-01-17 06:34:18 +00:00
|
|
|
void optimise(std::vector<std::string> const& _optimisationSteps);
|
|
|
|
|
2020-02-29 20:55:32 +00:00
|
|
|
size_t codeSize(yul::CodeWeights const& _weights) const { return computeCodeSize(*m_ast, _weights); }
|
2020-01-17 06:34:18 +00:00
|
|
|
yul::Block const& ast() const { return *m_ast; }
|
|
|
|
|
2020-01-23 09:27:50 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& _stream, Program const& _program);
|
|
|
|
std::string toJson() const;
|
|
|
|
|
2020-01-17 06:34:18 +00:00
|
|
|
private:
|
|
|
|
Program(
|
|
|
|
yul::Dialect const& _dialect,
|
2020-01-22 16:33:36 +00:00
|
|
|
std::unique_ptr<yul::Block> _ast
|
2020-01-17 06:34:18 +00:00
|
|
|
):
|
2020-01-22 16:33:36 +00:00
|
|
|
m_ast(std::move(_ast)),
|
2020-01-24 22:22:01 +00:00
|
|
|
m_dialect{_dialect},
|
|
|
|
m_nameDispenser(_dialect, *m_ast, {})
|
2020-01-17 06:34:18 +00:00
|
|
|
{}
|
|
|
|
|
2020-03-11 20:15:12 +00:00
|
|
|
static std::variant<std::unique_ptr<yul::Block>, langutil::ErrorList> parseObject(
|
|
|
|
yul::Dialect const& _dialect,
|
|
|
|
langutil::CharStream _source
|
|
|
|
);
|
2020-02-28 02:33:46 +00:00
|
|
|
static std::variant<std::unique_ptr<yul::AsmAnalysisInfo>, langutil::ErrorList> analyzeAST(
|
2020-01-17 06:34:18 +00:00
|
|
|
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
|
|
|
|
);
|
2020-01-31 10:32:44 +00:00
|
|
|
static std::unique_ptr<yul::Block> applyOptimisationSteps(
|
2020-01-24 22:22:01 +00:00
|
|
|
yul::Dialect const& _dialect,
|
|
|
|
yul::NameDispenser& _nameDispenser,
|
2020-01-31 10:32:44 +00:00
|
|
|
std::unique_ptr<yul::Block> _ast,
|
2020-01-17 06:34:18 +00:00
|
|
|
std::vector<std::string> const& _optimisationSteps
|
|
|
|
);
|
2020-02-29 20:55:32 +00:00
|
|
|
static size_t computeCodeSize(yul::Block const& _ast, yul::CodeWeights const& _weights);
|
2020-01-17 06:34:18 +00:00
|
|
|
|
2020-01-22 16:33:36 +00:00
|
|
|
std::unique_ptr<yul::Block> m_ast;
|
2020-01-24 22:22:01 +00:00
|
|
|
yul::Dialect const& m_dialect;
|
2020-01-17 06:34:18 +00:00
|
|
|
yul::NameDispenser m_nameDispenser;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|