-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_writer.cpp
132 lines (111 loc) · 3.88 KB
/
code_writer.cpp
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
//
// Created by erthax on 15.01.2021.
//
#include "code_writer.h"
void CodeWriter::writeOutput(const std::string& tFileName) {
std::ofstream output;
output.open(tFileName);
for (auto &it : instructionList) {
output << it->to_string() + "\n";
}
output.close();
}
void CodeWriter::loadNumberToRegister(std::string tRegName, uint value) {
std::vector<std::string> value_instructions;
this->reset(tRegName);
if (value > 6) {
value--;
value_instructions.emplace_back("INC");
while (value > 0) {
if (value % 2 == 0) {
value /= 2;
value_instructions.emplace_back("SHL");
} else {
value--;
value_instructions.emplace_back("INC");
}
}
for (int i = value_instructions.size() - 1; i >= 0; i--) {
if (value_instructions[i] == "INC") {
this->inc(tRegName);
} else {
this->shl(tRegName);
}
}
} else {
for (int i = 0; i < value; i++) {
this->inc(tRegName);
}
}
}
// Istruction generation
void CodeWriter::putInstructionAtIndex(Instruction *instruction, uint index) {
this->instructionList.insert(instructionList.begin()+index, instruction);
this->instructionCount++;
}
void CodeWriter::get(std::string tRegName) {
this->instructionList.push_back(new Instruction("GET", tRegName));
this->instructionCount++;
}
void CodeWriter::put(std::string tRegName) {
this->instructionList.push_back(new Instruction("PUT", tRegName));
this->instructionCount++;
}
void CodeWriter::load(std::string tRegName1, std::string tRegName2) {
this->instructionList.push_back(new Instruction("LOAD", tRegName1, tRegName2));
this->instructionCount++;
}
void CodeWriter::store(std::string tRegName1, std::string tRegName2) {
this->instructionList.push_back(new Instruction("STORE", tRegName1, tRegName2));
this->instructionCount++;
}
void CodeWriter::add(std::string tRegName1, std::string tRegName2) {
this->instructionList.push_back(new Instruction("ADD", tRegName1, tRegName2));
this->instructionCount++;
}
void CodeWriter::sub(std::string tRegName1, std::string tRegName2) {
this->instructionList.push_back(new Instruction("SUB", tRegName1, tRegName2));
this->instructionCount++;
}
void CodeWriter::reset(std::string tRegName) {
this->instructionList.push_back(new Instruction("RESET", tRegName));
this->instructionCount++;
}
void CodeWriter::inc(std::string tRegName) {
this->instructionList.push_back(new Instruction("INC", tRegName));
this->instructionCount++;
}
void CodeWriter::dec(std::string tRegName) {
this->instructionList.push_back(new Instruction("DEC", tRegName));
this->instructionCount++;
}
void CodeWriter::shr(std::string tRegName) {
this->instructionList.push_back(new Instruction("SHR", tRegName));
this->instructionCount++;
}
void CodeWriter::shl(std::string tRegName) {
this->instructionList.push_back(new Instruction("SHL", tRegName));
this->instructionCount++;
}
void CodeWriter::jump(int j) {
this->instructionList.push_back(new Instruction("JUMP", std::to_string(j)));
this->instructionCount++;
}
void CodeWriter::jzero(std::string tRegName, int j) {
this->instructionList.push_back(new Instruction("JZERO", tRegName, std::to_string(j)));
this->instructionCount++;
}
void CodeWriter::jodd(std::string tRegName, int j) {
this->instructionList.push_back(new Instruction("JODD", tRegName, std::to_string(j)));
this->instructionCount++;
}
void CodeWriter::halt() {
this->instructionList.push_back(new Instruction("HALT"));
this->instructionCount++;
}
uint CodeWriter::getInstructionCount() const {
return instructionCount;
}
void CodeWriter::setInstructionCount(uint instructionCount) {
CodeWriter::instructionCount = instructionCount;
}