[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.
This commit is contained in:
cameel
2020-02-05 18:13:30 +01:00
parent 7b7c88ae95
commit 785f65d0f5
5 changed files with 37 additions and 30 deletions
+9 -8
View File
@@ -26,6 +26,7 @@
using namespace std;
using namespace solidity;
using namespace solidity::langutil;
using namespace solidity::phaser;
namespace solidity::phaser
@@ -48,25 +49,25 @@ ostream& phaser::operator<<(ostream& _stream, Individual const& _individual)
return _stream;
}
Population::Population(string const& _sourcePath, vector<Chromosome> const& _chromosomes):
m_sourcePath{_sourcePath}
Population::Population(CharStream _sourceCode, vector<Chromosome> const& _chromosomes):
m_sourceCode{move(_sourceCode)}
{
for (auto const& chromosome: _chromosomes)
m_individuals.push_back({chromosome});
}
Population Population::makeRandom(string const& _sourcePath, size_t _size)
Population Population::makeRandom(CharStream _sourceCode, size_t _size)
{
vector<Individual> individuals;
for (size_t i = 0; i < _size; ++i)
individuals.push_back({Chromosome::makeRandom(randomChromosomeLength())});
return Population(_sourcePath, individuals);
return Population(move(_sourceCode), individuals);
}
size_t Population::measureFitness(Chromosome const& _chromosome, string const& _sourcePath)
size_t Population::measureFitness(Chromosome const& _chromosome, CharStream& _sourceCode)
{
auto program = Program::load(_sourcePath);
auto program = Program::load(_sourceCode);
program.optimise(_chromosome.optimisationSteps());
return program.codeSize();
}
@@ -87,7 +88,7 @@ void Population::run(optional<size_t> _numRounds, ostream& _outputStream)
ostream& phaser::operator<<(ostream& _stream, Population const& _population)
{
_stream << "Source: " << _population.m_sourcePath << endl;
_stream << "Stream name: " << _population.m_sourceCode.name() << endl;
auto individual = _population.m_individuals.begin();
for (; individual != _population.m_individuals.end(); ++individual)
@@ -105,7 +106,7 @@ void Population::doEvaluation()
{
for (auto& individual: m_individuals)
if (!individual.fitness.has_value())
individual.fitness = measureFitness(individual.chromosome, m_sourcePath);
individual.fitness = measureFitness(individual.chromosome, m_sourceCode);
}
void Population::doSelection()