-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cc
199 lines (175 loc) · 3.52 KB
/
stack.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
190
191
192
193
194
195
196
197
198
#include "stack.h"
string hp;
Stack::Stack()
{
this->sp = -1;
this->output_sp = -1;
this->loop_sp = -1;
this->main_function = true;
}
int Stack::in(bool call_fun)
{
if (this-> sp >= this->max_size - 1 )
{
throw("stack is full");
}
// this->output_sp += 1;
if (this->sp == 0)
{
this->stack[this->sp+1].call_fun = true;
}
else if (this->sp > 0)
{
this->stack[this->sp].save_and_load(SAVE, (*this));
stringstream ss;
ss << this->stack[this->sp].cnt;
string cmd = "subu $sp, " + ss.str();
//cerr << this->sp <<" " << call_fun<< endl;
this->add_cmd(cmd);
}
this->sp += 1;
return 0;
}
string Stack::out(int load)
{
/*stack pop, return the commands in this levestack pop, return the commands in this levell*/
if (this->sp < 0)
{
throw("stack is empty");
}
string ret;
if (this->sp >= 0)
{
if (this->sp > 1)
{
stringstream ss;
ss << this->stack[(this->sp)-1].cnt;
string cmd = "addu $sp, " + ss.str();
this->add_cmd(cmd);
if (load >= 1)
{
this->stack[(this->sp) - 1].save_and_load(LOAD, (*this));
}
}
if (load == 2)this->stack[this->sp].clear();
}
if (load == 2) this->stack[this->sp].call_fun = false;
this->sp -= 1;
if (this->sp == 0 && this->main_function)
this->main_function = false;
return "";
}
int Stack::output_in()
{
if (this-> output_sp >= this->max_size - 1)
{
throw("stack is full");
}
this->output_sp += 1;
return 0;
}
string Stack::output_out()
{
if ( this->output_sp < 0)
{
throw("stack is empty");
}
string ret = this->output_stack[this->output_sp].get_cmd();
this->output_sp -= 1;
return ret;
}
int Stack::loop_in(int tag_num)
{
if (this-> loop_sp >= this->max_size - 1)
{
throw("stack is full");
}
this->loop_sp += 1;
this->loop_stack[this->loop_sp] = tag_num;
this->loop_pos [this->loop_sp] = this->sp;
return 0;
}
int Stack::loop_out()
{
if (this->loop_sp < 0)
{
throw("stack is empty");
}
this->loop_sp -= 1;
return 0;
}
int Stack::current_loop(int &pos)
{
pos = this->loop_pos[this->loop_sp];
return this->loop_stack[this->loop_sp];
}
int Stack::add_cmd(string s, char split)
{
if (s == "")
{
return -1;
}
int ret = this->output_stack[this->output_sp].add(s, split);
return ret;
}
string Stack::find_var(string name, string &pointer)
{
int sp = this->sp + 1;
map<string, int>::iterator it;
int base = 0;
int finding_global = 0;
//cerr <<name <<": " << endl;
do
{
sp--;
// cerr << "stack: " <<sp <<", " << this->stack[sp].call_fun << endl;
if (sp < 0) throw("variable doesn't exist");
if (sp < this->sp)
base += this->stack[sp].cnt;
if (finding_global == 1)
finding_global = 2;
if (finding_global == 2 && sp > 0) continue;
if (finding_global == 0 && this->stack[sp].call_fun)
{
finding_global = 1;
}
it = this->stack[sp].vars.find(name);
}
while (it == this->stack[sp].vars.end());
int pos;
//cerr<<finding_global << endl;
//cerr<<"===" << endl;
bool flag = false;
int v = (*it).second;
if (v < 0 )
{
v = -v-1;
flag = true;
}
if (sp > 0)
{
pos = base - v* Memory::step - Memory::step;
pointer = "sp";
}
else
{
pos = - v* Memory::step - Memory::step;
pointer = hp;
}
stringstream ss;
ss << pos;
if (flag)
{
string reg = this->stack[this->sp].find_slot();
this->add_cmd("lw $" + reg + ", "+ss.str()+"($"+pointer+")");
pointer = reg;
return "0";
}
/*cerr << name <<": " << pos << endl;
cerr << name << ": " << endl;
cerr << this->sp <<", " << sp << endl;
cerr << base <<", " << pos << endl;
cerr <<ss.str() << endl;
cerr << "===" << endl;*/
return ss.str();
}