-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstruction.hpp
More file actions
67 lines (55 loc) · 1.66 KB
/
Instruction.hpp
File metadata and controls
67 lines (55 loc) · 1.66 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
#pragma once
#include "../Generators/Temporary.h"
#include <fstream>
#include <string>
#include <vector>
namespace irt {
class Instruction {
public:
Instruction() = default;
Instruction(std::string str, std::vector<Temporary> targets, std::vector<Temporary> sources)
: str_(std::move(str)), targets_(std::move(targets)), sources_(std::move(sources)) {}
std::string GetStr() const {
return str_;
}
void Print(std::ofstream& out) const {
std::string result = str_;
for (size_t i = 0; i < targets_.size(); ++i) {
std::string match = "t" + std::to_string(i);
size_t pos = result.find(match);
while (pos != std::string::npos) {
result.replace(pos, match.length(), targets_[i].ToString());
pos = result.find(match);
}
}
for (size_t i = 0; i < sources_.size(); ++i) {
std::string match = "s" + std::to_string(i);
size_t pos = result.find(match);
while (pos != std::string::npos) {
result.replace(pos, match.length(), sources_[i].ToString());
pos = result.find(match);
}
}
out << result << std::endl;
}
std::vector<Temporary> GetTargets() const {
return targets_;
}
std::vector<Temporary> GetSources() const {
return sources_;
}
private:
std::string str_ = {};
std::vector<Temporary> targets_ = {};
std::vector<Temporary> sources_ = {};
};
static void PrintInstructions(std::string filename, const std::vector<Instruction>& instructions) {
std::ofstream stream(filename);
for (const auto& instruction: instructions) {
if (instruction.GetStr().back() != ':') {
stream << " ";
}
instruction.Print(stream);
}
}
}