-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.cc
189 lines (176 loc) · 3.2 KB
/
memory.cc
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "stack.h"
Memory::Memory()
{
this->clear();
}
void Memory::remove_slot(string name)
{
this->set_it = this->regi.find(name);
if (this->set_it == this->regi.end())
{
string s = "register doesn't exist ";
s += name;
throw s.c_str();
throw("register doesn't exist" + name);
}
else
this->regi.erase(this->set_it);
}
string Memory::find_slot()
{
const string reg_name[17] =
{"t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "t9",
"s0", "s1", "s2", "s3", "s4", "s5", "s6"};
const int reg_num = 17;
for (int i = 0; i < reg_num; i++)
{
string name = reg_name[i];
try
{
this->add_slot(name);
}
catch (const char *s)
{
continue;
}
return name;
}
throw("register is full");
}
int Memory::add_slot(string name)
{
this->set_it = this->regi.find(name);
if (set_it == regi.end())
{
regi.insert(name);
}
else
{
throw ("register exist");
}
}
string Memory::new_value(int value, Stack &s )
{
string name = this->find_slot();
stringstream ss;
ss << value;
string cmd = "li $" + name + ", " + ss.str();
s.add_cmd(cmd);
return name;
}
int Memory::add_var(string name, int len )
{
this->map_it = this->vars.find(name);
if (this->map_it != this->vars.end())
{
throw("variable already exist");
}
else
{
if (len == -1)
{
vars[name] = -((this->num)+1);
}
else
{
vars[name] = this->num;
this->num += len;
}
}
return 0;
}
int Memory::set_var(string name, string reg, Stack &s, string move )
{
string sp;
string pos = s.find_var(name, sp);
if (move != "")
{
s.add_cmd("sll $" + move + ", $" + move + ", 2");
s.add_cmd("sub $"+sp+", $"+sp+", $" + move);
}
string cmd = "sw $" + reg + ", " + pos + "($"+sp+")";
s.add_cmd(cmd);
if (move != "")
{
s.add_cmd("add $"+sp+", $"+sp+", $" + move);
this->remove_slot(move);
}
return 0;
}
string Memory::get_var(string name, Stack &s, string move )
{
string sp;
string pos = s.find_var(name, sp);
string reg = this->find_slot();
if (move != "")
{
s.add_cmd("sll $" + move + ", $" + move + ", 2");
s.add_cmd("sub $"+sp+", $"+sp+", $" + move);
}
string cmd = "lw $" + reg + ", " + pos + "($"+sp+")";
s.add_cmd(cmd);
if (move != "")
{
s.add_cmd("add $"+sp+", $"+sp+", $" + move);
this->remove_slot(move);
}
return reg;
}
int Memory::save_and_load(int flag, Stack &s)
{
string op;
if (flag == SAVE)
op = "sw";
else
op = "lw";
this->set_it = this->regi.begin();
int step = this->step;
int cnt = this->num * step;
while (this->set_it != this->regi.end())
{
cnt += step;
stringstream ss;
ss<< -cnt;
string cmd = op + " $" + (*set_it) + ", " + ss.str() + "($sp)";
s.add_cmd(cmd);
set_it++;
}
this->cnt = cnt;
return cnt;
}
int Memory::clear()
{
this->regi.clear();
this->vars.clear();
this->cnt = 0;
this->num = 0;
this->call_fun = false;
this->release_list.clear();
}
/*
* Unit Test
*/
/*
int main()
{
try
{
Stack s;
s.stack[s.sp].new_value(3);
s.stack[s.sp].new_value(3);
s.stack[s.sp].add_var("a");
s.stack[s.sp].set_var("a","t0");
s.stack[s.sp].get_var("a");
s.in();
s.out();
s.stack[s.sp].remove_slot("t0");
s.stack[s.sp].new_value(4);
s.stack[s.sp].add_var("a");
}
catch (const char *s)
{
string err(s);
cerr << err << endl;
}
}
*/