-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprogram_cache.cpp
More file actions
107 lines (96 loc) · 2.73 KB
/
program_cache.cpp
File metadata and controls
107 lines (96 loc) · 2.73 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
#include "lang/program_cache.hpp"
#include <exception>
#include <stdexcept>
#include <unordered_set>
#include <vector>
#include "lang/parser.hpp"
#include "lang/program_util.hpp"
const Program& ProgramCache::getProgram(UID id) {
if (missing.find(id) != missing.end()) {
throw std::runtime_error("Program not found: " + id.string());
}
if (programs.find(id) == programs.end()) {
try {
Parser parser;
auto path = ProgramUtil::getProgramPath(id);
programs[id] = parser.parse(path);
} catch (...) {
missing.insert(id);
std::rethrow_exception(std::current_exception());
}
}
return programs[id];
}
std::unordered_map<UID, Program> ProgramCache::collect(UID id) {
std::unordered_map<UID, Program> result;
std::unordered_set<UID> visiting;
std::vector<UID> stack;
stack.push_back(id);
while (!stack.empty()) {
auto cur_id = stack.back();
stack.pop_back();
if (result.count(cur_id)) {
continue; // already collected
}
if (visiting.count(cur_id)) {
throw std::runtime_error("Recursion detected in program dependencies: " +
cur_id.string());
}
visiting.insert(cur_id);
const Program& prog = getProgram(cur_id);
result[cur_id] = prog;
for (const auto& op : prog.ops) {
if ((op.type == Operation::Type::SEQ ||
op.type == Operation::Type::PRG) &&
op.source.type == Operand::Type::CONSTANT) {
auto dep_id = UID::castFromInt(op.source.value.asInt());
if (!result.count(dep_id)) {
stack.push_back(dep_id);
}
}
}
visiting.erase(cur_id);
}
return result;
}
int64_t ProgramCache::getOffset(UID id) {
if (offsets.find(id) == offsets.end()) {
try {
const Program& prog = getProgram(id);
offsets[id] = ProgramUtil::getOffset(prog);
} catch (...) {
missing.insert(id);
std::rethrow_exception(std::current_exception());
}
}
return offsets[id];
}
bool ProgramCache::shouldCheckOffset(UID id) const {
return skip_check_offsets.find(id) == skip_check_offsets.end();
}
void ProgramCache::setCheckOffset(UID id, bool check) {
if (check) {
skip_check_offsets.erase(id); // check enabled per default
} else {
skip_check_offsets.insert(id);
}
}
int64_t ProgramCache::getOverhead(UID id) const {
const auto it = overheads.find(id);
return it != overheads.end() ? it->second : 0;
}
void ProgramCache::setOverhead(UID id, int64_t overhead) {
overheads[id] = overhead;
}
void ProgramCache::insert(UID id, const Program& p) {
programs[id] = p;
missing.erase(id);
offsets.erase(id);
}
void ProgramCache::clear() {
programs.clear();
offsets.clear();
overheads.clear();
missing.clear();
skip_check_offsets.clear();
}