-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassembly.cpp
More file actions
100 lines (89 loc) · 2.92 KB
/
Copy pathassembly.cpp
File metadata and controls
100 lines (89 loc) · 2.92 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
#include "assembly.h"
#include "read/read.h"
#include <stdexcept>
#include "log.h"
AssemblyData::AssemblyData(const std::string &filename, BasicLogPtr logger) {
logger->info("Start read data from file: {} ", filename);
auto data = Read::readTable(filename, '\n');
int numberLine = 1;
while (data[numberLine][0] != ".code") {
const auto &line = data[numberLine];
if (line[1] == "?") {
unknownValue_ = line[0];
} else {
values_[line[0]] = std::stoi(line[1]);
}
++numberLine;
}
++numberLine;
while (numberLine < data.size() - 1) {
const auto &line = data[numberLine];
operations_.push(BinaryOperation(line[0], (line[1]), (line[2])));
++numberLine;
}
logger->info("Reading is over");
}
BinaryOperation::BinaryOperation(const TypeOperation &operation, const std::string &leftOperand,
const std::string &rightOperand) :
nameOperation_(operation),
leftOperand_(leftOperand),
rightOperand_(rightOperand) {}
BinaryOperation::BinaryOperation(const std::string &operation, const std::string &leftOperand,
const std::string &rightOperand) :
nameOperation_(whatOperation(operation)),
leftOperand_(leftOperand),
rightOperand_(rightOperand) {}
TypeOperation BinaryOperation::whatOperation(const std::string &nameOperation) {
if (nameOperation == "mov") {
return TypeOperation::mov;
}
if (nameOperation == "add") {
return TypeOperation::add;
}
if (nameOperation == "sub") {
return TypeOperation::sub;
}
if (nameOperation == "mul") {
return TypeOperation::mul;
}
if (nameOperation == "imul") {
return TypeOperation::imul;
}
if (nameOperation == "div") {
return TypeOperation::div;
}
if (nameOperation == "idiv") {
return TypeOperation::idiv;
}
throw std::invalid_argument(nameOperation + " unknown operation");
}
TypeOperation BinaryOperation::nameOperation() const {
return nameOperation_;
}
BinaryOperation AssemblyData::getNext(BasicLogPtr logger) {
try {
BinaryOperation result(operations_.front());
logger->info("Get operation: {} {} {}", static_cast<int>(result.nameOperation()), result.leftOperand(),
result.rightOperand());
operations_.pop();
return result;
} catch (...) {
logger->critical("Cannot get next operation in function {}", "getNext");
exit(1);
}
}
int &AssemblyData::getValue(const std::string &name) {
return values_[name];
}
bool AssemblyData::isEmpty() const {
return operations_.isEmpty();
}
int AssemblyData::getAns() {
return values_[unknownValue_];
}
const std::string &BinaryOperation::leftOperand() const {
return leftOperand_;
}
const std::string &BinaryOperation::rightOperand() const {
return rightOperand_;
}