-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgenerator_v1.cpp
More file actions
164 lines (145 loc) · 5.25 KB
/
generator_v1.cpp
File metadata and controls
164 lines (145 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "gen/generator_v1.hpp"
#include <ctype.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <stdexcept>
#include "eval/interpreter.hpp"
#include "eval/optimizer.hpp"
#include "lang/parser.hpp"
#include "lang/program_util.hpp"
#include "math/semantics.hpp"
#include "mine/distribution.hpp"
#include "mine/stats.hpp"
#include "sys/log.hpp"
#define POSITION_RANGE 100
GeneratorV1::GeneratorV1(const Config& config, const Stats& stats)
: Generator(config, stats), current_template(0), mutator(stats) {
// the post processing adds operations, so we reduce the target length here
num_operations = std::max<int64_t>(config.length / 2, 1);
std::string operand_types = config.indirect_access ? "cdi" : "cd";
// add operation types
for (auto t : Operation::Types) {
auto& metadata = Operation::Metadata::get(t);
if (!metadata.is_public || t == Operation::Type::LPE) {
continue;
}
if (t == Operation::Type::LPB && !config.loops) {
continue;
}
if (t == Operation::Type::SEQ && !config.calls) {
continue;
}
this->operation_types.push_back(t);
}
std::vector<double> source_type_rates;
std::vector<double> target_type_rates;
if (operand_types.find('c') != std::string::npos) {
source_operand_types.push_back(Operand::Type::CONSTANT);
source_type_rates.push_back(4);
}
if (operand_types.find('d') != std::string::npos) {
source_operand_types.push_back(Operand::Type::DIRECT);
source_type_rates.push_back(4);
target_operand_types.push_back(Operand::Type::DIRECT);
target_type_rates.push_back(4);
}
if (operand_types.find('i') != std::string::npos) {
source_operand_types.push_back(Operand::Type::INDIRECT);
source_type_rates.push_back(1);
target_operand_types.push_back(Operand::Type::INDIRECT);
target_type_rates.push_back(1);
}
if (source_operand_types.empty()) {
Log::get().error("No source operation types", true);
}
if (target_operand_types.empty()) {
Log::get().error("No target operation types", true);
}
// load program templates
Parser parser;
Program p;
for (const auto& t : config.templates) {
try {
p = parser.parse(t);
ProgramUtil::removeOps(p, Operation::Type::NOP);
for (auto& op : p.ops) {
op.comment.clear();
}
templates.push_back(p);
} catch (const std::exception&) {
Log::get().warn("Cannot load template (ignoring): " + t);
}
}
// initialize distributions
constants.resize(stats.num_constants.size());
size_t i = 0;
for (const auto& c : stats.num_constants) {
constants[i++] = c.first;
}
constants_dist = constantsDist(constants, stats);
operation_dist = operationDist(stats, this->operation_types);
target_type_dist = std::discrete_distribution<>(target_type_rates.begin(),
target_type_rates.end());
target_value_dist = uniformDist(config.max_constant + 1);
source_type_dist = std::discrete_distribution<>(source_type_rates.begin(),
source_type_rates.end());
source_value_dist = uniformDist(config.max_constant + 1);
position_dist = uniformDist(POSITION_RANGE);
}
std::pair<Operation, double> GeneratorV1::generateOperation() {
Operation op;
op.type = operation_types.at(operation_dist(Random::get().gen));
op.target.type = target_operand_types.at(target_type_dist(Random::get().gen));
op.target.value = target_value_dist(Random::get().gen);
op.source.type = source_operand_types.at(source_type_dist(Random::get().gen));
op.source.value = source_value_dist(Random::get().gen);
// check number of operands
if (Operation::Metadata::get(op.type).num_operands < 2) {
op.source.type = Operand::Type::CONSTANT;
op.source.value = 0;
}
if (Operation::Metadata::get(op.type).num_operands < 1) {
op.target.type = Operand::Type::CONSTANT;
op.target.value = 0;
}
// bias for constant loop fragment length
if (op.type == Operation::Type::LPB &&
op.source.type != Operand::Type::CONSTANT &&
position_dist(Random::get().gen) % 10 > 0) {
op.source.type = Operand::Type::CONSTANT;
}
// use constants distribution from stats
if (op.source.type == Operand::Type::CONSTANT) {
op.source.value = constants.at(constants_dist(Random::get().gen));
if (op.type == Operation::Type::LPB ||
ProgramUtil::isWritingRegion(op.type)) {
op.source.value = Semantics::mod(
Semantics::max(op.source.value, Number::ONE), 10); // magic number
}
}
// avoid meaningless zeros or singularities
ProgramUtil::avoidNopOrOverflow(op);
std::pair<Operation, double> next_op;
next_op.first = op;
next_op.second =
static_cast<double>(position_dist(Random::get().gen)) / POSITION_RANGE;
return next_op;
}
Program GeneratorV1::generateProgram() {
// use template for base program
Program p;
if (!templates.empty()) {
p = templates[current_template];
current_template = (current_template + 1) % templates.size();
}
if (p.ops.empty() || (Random::get().gen() % 2)) {
generateStateless(p, num_operations);
applyPostprocessing(p);
} else {
mutator.mutateRandom(p);
}
return p;
}
bool GeneratorV1::supportsRestart() const { return true; }
bool GeneratorV1::isFinished() const { return false; };