2020-01-17 06:19:05 +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-01-17 06:34:18 +00:00
|
|
|
#include <tools/yulPhaser/Exceptions.h>
|
2020-01-17 06:45:10 +00:00
|
|
|
#include <tools/yulPhaser/Population.h>
|
2020-02-05 14:58:35 +00:00
|
|
|
#include <tools/yulPhaser/FitnessMetrics.h>
|
2020-02-13 19:35:50 +00:00
|
|
|
#include <tools/yulPhaser/GeneticAlgorithms.h>
|
2020-01-31 12:26:54 +00:00
|
|
|
#include <tools/yulPhaser/Program.h>
|
2020-02-12 10:49:09 +00:00
|
|
|
#include <tools/yulPhaser/SimulationRNG.h>
|
2020-01-17 06:24:18 +00:00
|
|
|
|
2020-01-22 19:14:36 +00:00
|
|
|
#include <libsolutil/Assertions.h>
|
|
|
|
#include <libsolutil/CommonIO.h>
|
|
|
|
#include <liblangutil/CharStream.h>
|
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
2020-01-17 06:24:18 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace std;
|
2020-01-22 19:14:36 +00:00
|
|
|
using namespace solidity::langutil;
|
2020-01-17 06:34:18 +00:00
|
|
|
using namespace solidity::phaser;
|
2020-01-22 19:14:36 +00:00
|
|
|
using namespace solidity::util;
|
2020-01-17 06:34:18 +00:00
|
|
|
|
2020-01-17 06:24:18 +00:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct CommandLineParsingResult
|
|
|
|
{
|
|
|
|
int exitCode;
|
|
|
|
po::variables_map arguments;
|
|
|
|
};
|
|
|
|
|
2020-02-12 10:49:09 +00:00
|
|
|
|
|
|
|
void initializeRNG(po::variables_map const& arguments)
|
|
|
|
{
|
|
|
|
uint32_t seed;
|
|
|
|
if (arguments.count("seed") > 0)
|
|
|
|
seed = arguments["seed"].as<uint32_t>();
|
|
|
|
else
|
|
|
|
seed = SimulationRNG::generateSeed();
|
|
|
|
|
|
|
|
SimulationRNG::reset(seed);
|
|
|
|
cout << "Random seed: " << seed << endl;
|
|
|
|
}
|
|
|
|
|
2020-01-22 19:14:36 +00:00
|
|
|
CharStream loadSource(string const& _sourcePath)
|
|
|
|
{
|
|
|
|
assertThrow(boost::filesystem::exists(_sourcePath), InvalidProgram, "Source file does not exist");
|
|
|
|
|
|
|
|
string sourceCode = readFileAsString(_sourcePath);
|
|
|
|
return CharStream(sourceCode, _sourcePath);
|
|
|
|
}
|
|
|
|
|
2020-01-17 06:24:18 +00:00
|
|
|
void runAlgorithm(string const& _sourcePath)
|
|
|
|
{
|
2020-02-05 18:25:15 +00:00
|
|
|
constexpr size_t populationSize = 20;
|
2020-02-13 19:35:50 +00:00
|
|
|
constexpr size_t minChromosomeLength = 12;
|
|
|
|
constexpr size_t maxChromosomeLength = 30;
|
|
|
|
|
2020-01-31 12:26:54 +00:00
|
|
|
CharStream sourceCode = loadSource(_sourcePath);
|
2020-02-05 18:25:15 +00:00
|
|
|
shared_ptr<FitnessMetric> fitnessMetric = make_shared<ProgramSize>(Program::load(sourceCode), 5);
|
2020-02-06 05:19:55 +00:00
|
|
|
auto population = Population::makeRandom(
|
2020-02-05 14:58:35 +00:00
|
|
|
fitnessMetric,
|
2020-02-05 18:25:15 +00:00
|
|
|
populationSize,
|
2020-02-13 19:35:50 +00:00
|
|
|
minChromosomeLength,
|
|
|
|
maxChromosomeLength
|
2020-02-06 05:19:55 +00:00
|
|
|
);
|
2020-02-13 19:35:50 +00:00
|
|
|
RandomAlgorithm(
|
|
|
|
population,
|
|
|
|
cout,
|
|
|
|
{
|
2020-02-05 18:25:15 +00:00
|
|
|
/* elitePoolSize = */ 1.0 / populationSize,
|
2020-02-13 19:35:50 +00:00
|
|
|
/* minChromosomeLength = */ minChromosomeLength,
|
|
|
|
/* maxChromosomeLength = */ maxChromosomeLength,
|
|
|
|
}
|
|
|
|
).run();
|
2020-01-17 06:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandLineParsingResult parseCommandLine(int argc, char** argv)
|
|
|
|
{
|
|
|
|
po::options_description description(
|
|
|
|
"yul-phaser, a tool for finding the best sequence of Yul optimisation phases.\n"
|
|
|
|
"\n"
|
|
|
|
"Usage: yul-phaser [options] <file>\n"
|
|
|
|
"Reads <file> as Yul code and tries to find the best order in which to run optimisation"
|
|
|
|
" phases using a genetic algorithm.\n"
|
|
|
|
"Example:\n"
|
|
|
|
"yul-phaser program.yul\n"
|
|
|
|
"\n"
|
|
|
|
"Allowed options",
|
|
|
|
po::options_description::m_default_line_length,
|
|
|
|
po::options_description::m_default_line_length - 23
|
|
|
|
);
|
|
|
|
|
|
|
|
description.add_options()
|
|
|
|
("help", "Show help message and exit.")
|
|
|
|
("input-file", po::value<string>()->required(), "Input file")
|
2020-02-12 10:49:09 +00:00
|
|
|
("seed", po::value<uint32_t>(), "Seed for the random number generator")
|
2020-01-17 06:24:18 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
po::positional_options_description positionalDescription;
|
|
|
|
po::variables_map arguments;
|
|
|
|
positionalDescription.add("input-file", 1);
|
|
|
|
po::notify(arguments);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
po::command_line_parser parser(argc, argv);
|
|
|
|
parser.options(description).positional(positionalDescription);
|
|
|
|
po::store(parser.run(), arguments);
|
|
|
|
}
|
|
|
|
catch (po::error const & _exception)
|
|
|
|
{
|
|
|
|
cerr << _exception.what() << endl;
|
|
|
|
return {1, move(arguments)};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments.count("help") > 0)
|
|
|
|
{
|
|
|
|
cout << description << endl;
|
|
|
|
return {2, move(arguments)};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments.count("input-file") == 0)
|
|
|
|
{
|
|
|
|
cerr << "Missing argument: input-file." << endl;
|
|
|
|
return {1, move(arguments)};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {0, arguments};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
2020-01-17 06:19:05 +00:00
|
|
|
{
|
2020-01-17 06:24:18 +00:00
|
|
|
CommandLineParsingResult parsingResult = parseCommandLine(argc, argv);
|
|
|
|
if (parsingResult.exitCode != 0)
|
|
|
|
return parsingResult.exitCode;
|
|
|
|
|
2020-02-12 10:49:09 +00:00
|
|
|
initializeRNG(parsingResult.arguments);
|
|
|
|
|
2020-01-17 06:34:18 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
runAlgorithm(parsingResult.arguments["input-file"].as<string>());
|
|
|
|
}
|
|
|
|
catch (InvalidProgram const& _exception)
|
|
|
|
{
|
|
|
|
cerr << "ERROR: " << _exception.what() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
2020-01-17 06:24:18 +00:00
|
|
|
|
2020-01-17 06:19:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|