-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathblocks.cpp
More file actions
146 lines (130 loc) · 3.58 KB
/
blocks.cpp
File metadata and controls
146 lines (130 loc) · 3.58 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
#include "gen/blocks.hpp"
#include <fstream>
#include <string>
#include "lang/parser.hpp"
#include "lang/program_util.hpp"
Blocks::Interface::Interface() {}
Blocks::Interface::Interface(const Program& p) {
for (auto& op : p.ops) {
extend(op);
}
}
void Blocks::Interface::extend(const Operation& op) {
auto& meta = Operation::Metadata::get(op.type);
if (meta.num_operands > 0 && op.target.type == Operand::Type::DIRECT) {
if (meta.is_reading_target) {
inputs.insert(op.target.value.asInt());
all.insert(op.target.value.asInt());
}
if (meta.is_writing_target) {
outputs.insert(op.target.value.asInt());
all.insert(op.target.value.asInt());
}
}
if (meta.num_operands > 1 && op.source.type == Operand::Type::DIRECT) {
inputs.insert(op.source.value.asInt());
all.insert(op.source.value.asInt());
}
}
void Blocks::Interface::clear() {
inputs.clear();
outputs.clear();
all.clear();
}
void Blocks::Collector::add(const Program& p) {
interface.clear();
Program block;
for (auto op : p.ops) {
if (op.type == Operation::Type::NOP) {
continue;
}
op.comment.clear();
// decide whether and where to cut
bool include_now = true;
bool next_block = false;
if (op.type == Operation::Type::LPB) {
include_now = false;
next_block = true;
}
if (op.type == Operation::Type::LPE) {
next_block = true;
}
interface.extend(op);
if (interface.all.size() > 3) // magic number
{
include_now = false;
next_block = true;
}
// append to block and cut if needed
if (include_now) {
block.ops.push_back(op);
}
if (next_block) {
if (!block.ops.empty()) {
if (block.ops.front().type == Operation::Type::LPB &&
block.ops.back().type != Operation::Type::LPE) {
block.ops.erase(block.ops.begin(), block.ops.begin() + 1);
}
if (block.ops.back().type == Operation::Type::LPE &&
block.ops.front().type != Operation::Type::LPB) {
block.ops.pop_back();
}
if (!block.ops.empty()) {
blocks[block]++;
block.ops.clear();
}
}
interface.clear();
}
if (!include_now) {
block.ops.push_back(op);
}
}
// final block
if (!block.ops.empty()) {
blocks[block]++;
}
}
Blocks Blocks::Collector::finalize() {
Blocks result;
for (auto it : blocks) {
Operation nop(Operation::Type::NOP);
nop.comment = std::to_string(it.second);
result.list.ops.push_back(nop);
result.list.ops.insert(result.list.ops.end(), it.first.ops.begin(),
it.first.ops.end());
}
blocks.clear();
result.initRatesAndOffsets();
return result;
}
bool Blocks::Collector::empty() const { return blocks.empty(); }
void Blocks::load(const std::string& path) {
Parser parser;
list = parser.parse(path);
initRatesAndOffsets();
}
void Blocks::save(const std::string& path) {
std::ofstream out(path);
ProgramUtil::print(list, out);
}
Program Blocks::getBlock(size_t index) const {
Program block;
size_t offset = offsets.at(index) + 1; // skip rate comment
while (offset < list.ops.size() &&
list.ops[offset].type != Operation::Type::NOP) {
block.ops.push_back(list.ops[offset++]);
}
return block;
}
void Blocks::initRatesAndOffsets() {
offsets.clear();
rates.clear();
for (size_t i = 0; i < list.ops.size(); i++) {
if (list.ops[i].type == Operation::Type::NOP &&
!list.ops[i].comment.empty()) {
offsets.push_back(i);
rates.push_back(std::stoll(list.ops[i].comment));
}
}
}