-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.h
110 lines (107 loc) · 2.92 KB
/
register.h
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
// Register Lib Done
#ifndef REGISTER
#define REGISTER
#include <string>
#include <fstream>
using namespace std;
class Register {
protected:
struct RegisterNode {
string name;
long long int value;
int* address;
RegisterNode* next;
};
int* ptr;
string linkFile;
Register::RegisterNode* root;
public:
Register(string _linkFile);
void init();
void setRegisterAddress(string registerName, int* ptr);
void setRegisterValue(string registerName, long long int value);
long long int getRegisterValue(string registerName);
int* getAddressValue(string registerName);
~Register() {
delete[] ptr;
}
};
Register::Register(string _linkFile) {
this->linkFile = _linkFile;
this->root = nullptr;
}
void Register::init() {
ifstream registerList;
registerList.open(this->linkFile, istream::binary);
int countRegister = 1;
RegisterNode* preRegisterNode;
while(!registerList.eof()) {
RegisterNode* newNode = new RegisterNode;
newNode->value = 0;
newNode->address = nullptr;
registerList >> newNode->name;
if(!newNode->name.compare("$sp")) {
newNode->address = new int[100];
ptr = newNode->address;
newNode->value = (int)newNode->address;
}
if(countRegister == 1) {
root = newNode;
newNode->next = nullptr;
countRegister++;
}
else {
newNode->next = root;
root = newNode;
}
}
}
void Register::setRegisterAddress(string registerName, int* ptr) {
registerName = getWord(registerName, 1);
RegisterNode* seeker;
seeker = root;
while(seeker->next != nullptr) {
if(!seeker->name.compare(registerName)) {
seeker->address = ptr;
seeker->value = *(int*)(&seeker->address);
}
seeker = seeker->next;
}
}
void Register::setRegisterValue(string registerName, long long int _value) {
registerName = getWord(registerName, 1);
RegisterNode* seeker;
seeker = root;
while(seeker != nullptr) {
if(!seeker->name.compare(registerName)) {
seeker->value = _value;
seeker->address = (int*)seeker->value;
}
seeker = seeker->next;
}
}
int* Register::getAddressValue(string registerName) {
registerName = getWord(registerName, 1);
RegisterNode* seeker;
seeker = root;
while(seeker != nullptr) {
if(!seeker->name.compare(registerName)) {
return seeker->address;
}
seeker = seeker->next;
}
return nullptr;
}
long long int Register::getRegisterValue(string registerName) {
registerName = getWord(registerName, 1);
RegisterNode* seeker;
seeker = root;
while(seeker != nullptr) {
if(!seeker->name.compare(registerName)) {
return seeker->value;
}
seeker = seeker->next;
}
return 0;
}
#endif