-
Notifications
You must be signed in to change notification settings - Fork 0
/
mips_command.h
259 lines (224 loc) · 7.06 KB
/
mips_command.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// Created by 23207 on 2020/11/6.
//
#ifndef UNTITLED_MIPS_COMMAND_H
#define UNTITLED_MIPS_COMMAND_H
#include "string"
#include "global.h"
string blank = ", ";
static int reserved = 0;
string split_reg_src_from_lw_and_sw(string origin) {
string ans = "";
if (origin.substr(0, 3) != "sw " && origin.substr(0, 3) != "lw ") return ans;
return origin.substr(3, 3);
}
static string last_output;
void lw(const string& reg_name, int offset, const string& base_reg) {
string ans;
if ((last_output == "sw " + reg_name + blank + to_string(offset) +
"(" + base_reg + ")" && o1)) return;
if ((last_output == "lw " + reg_name + blank + to_string(offset) +
"(" + base_reg + ")" && o1)) return;
else if (last_output == "sw " + split_reg_src_from_lw_and_sw(last_output)
+ blank + to_string(offset) + "(" + base_reg + ")" && o1)
ans = "move " + reg_name + blank + split_reg_src_from_lw_and_sw(last_output);
else ans = "lw " + reg_name + blank + to_string(offset) + "(" + base_reg + ")";
mout << ans << endl;
last_output = ans;
}
void lw(const string& reg_name, const string& off_reg, const string& base_reg) {
string ans1, ans2;
ans1 = "add " + off_reg + blank + base_reg + blank + off_reg;
ans2 = "lw " + reg_name + ", (" + off_reg + ")";
mout << ans1 << endl;
mout << ans2 << endl;
last_output = ans2;
}
void sw(const string& reg_name, int offset, const string& base_reg) {
string ans;
ans = "sw " + reg_name + blank + to_string(offset) + "(" + base_reg + ")";
mout << ans << endl;
last_output = ans;
}
void sw(const string& reg_name, const string& off_reg, const string& base_reg) {
string ans1, ans2;
ans1 = "add " + off_reg + blank + base_reg + blank + off_reg;
ans2 = "sw " + reg_name + ", (" + off_reg + ")";
mout << ans1 << endl;
mout << ans2 << endl;
last_output = ans2;
}
void sb(const string& reg_name, int offset, const string& base_reg) {
string ans;
ans = "sb " + reg_name + blank + to_string(offset) + "(" + base_reg + ")";
mout << ans << endl;
last_output = ans;
}
void add(const string& reg_name_dst, const string& reg_name1, const string& reg_name2) {
string ans;
ans = "addu " + reg_name_dst + blank + reg_name1 + blank + reg_name2;
mout << ans << endl;
last_output = ans;
}
void addi(const string& reg_name_dst, const string& reg_name1, const string& constant) {
string ans;
ans = "addiu " + reg_name_dst + blank + reg_name1 + blank + constant;
mout << ans << endl;
last_output = ans;
}
void sub(const string& reg_name_dst, const string& reg_name1, const string& reg_name2) {
string ans;
ans = "subu " + reg_name_dst + blank + reg_name1 + blank + reg_name2;
mout << ans << endl;
last_output = ans;
}
void subi(const string& reg_name_dst, const string& reg_name1, const string& constant) {
string ans;
int con = string2int(constant);
con = -con;
ans = "addiu " + reg_name_dst + blank + reg_name1 + blank + to_string(con);
mout << ans << endl;
last_output = ans;
}
void mult(const string& reg_name1, const string& reg_name2) {
string ans;
ans = "mult " + reg_name1 + blank + reg_name2;
mout << ans << endl;
last_output = ans;
}
void neg(const string& reg_name1, const string& reg_name2) {
string ans;
ans = "neg " + reg_name1 + blank + reg_name2;
mout << ans << endl;
last_output = ans;
}
void div(const string& reg_name1, const string& reg_name2) {
string ans;
ans = "div " + reg_name1 + blank + reg_name2;
mout << ans << endl;
last_output = ans;
}
void mflo(const string& reg_name) {
string ans;
ans = "mflo " + reg_name;
mout << ans << endl;
last_output = ans;
}
void li(const string& reg_name, const string& num) {
string ans;
ans = "li " + reg_name + blank + num;
mout << ans << endl;
last_output = ans;
}
void syscall() {
string ans;
ans = "syscall";
mout << ans << endl;
last_output = ans;
}
void set_flag(const string& flag_name) {
string ans;
ans = flag_name + ":";
mout << ans << endl;
last_output = ans;
}
void set_data_seg() {
string ans;
ans = ".data";
mout << ans << endl;
last_output = ans;
}
void set_text_seg() {
string ans;
ans = ".text";
mout << ans << endl;
last_output = ans;
}
void j(const string& label_name) {
string ans;
ans = "j " + label_name;
mout << ans << endl;
last_output = ans;
}
void allocate(const string& name, const string& size) {
string ans;
ans = name + ": .space " + size;
mout << ans << endl;
last_output = ans;
}
void allocate_init(const string& type, const string& name, vector<int> array) {
string ans;
ans = name + ": .word ";
int n = array.size();
ans += to_string(array[0]);
for (int i = 1;i < n;i++) {
ans += blank + to_string(array[i]);
}
mout << ans << endl;
last_output = ans;
}
void la(const string& reg_name, const string& label_name) {
string ans;
ans = "la " + reg_name + blank + label_name;
mout << ans << endl;
last_output = ans;
}
void branch(const string& type, const string& op1, const string& op2, const string& label) {
string ans;
ans = type + " " + op1 + blank + op2 + blank + label;
mout << ans << endl;
last_output = ans;
}
void jal(const string& label_name) {
string ans;
ans = "jal " + label_name;
mout << ans << endl;
last_output = ans;
}
void jr(const string& reg_name) {
string ans;
ans = "jr " + reg_name;
mout << ans << endl;
last_output = ans;
}
void allocate_string(const string& string_name, const string& content) {
string ans;
ans = string_name + ": .asciiz \"" + content + "\"";
mout << ans << endl;
last_output = ans;
}
void mv(const string& reg_dst, const string& reg_src) {
string ans;
if (reg_dst == reg_src) return;
ans = "move " + reg_dst + blank + reg_src;
mout << ans << endl;
last_output = ans;
}
void sll(const string& reg_name_dst, const string& reg_name1, const string& constant) {
string ans;
ans = "sll " + reg_name_dst + blank + reg_name1 + blank + constant;
mout << ans << endl;
last_output = ans;
}
void sra(const string& reg_name_dst, const string& reg_name1, const string& constant) {
string ans;
string reserved_name = "RESERVED_" + to_string(reserved);
reserved++;
ans = "bgez " + reg_name1 + blank + reserved_name;
mout << ans << endl;
ans = "neg " + reg_name1 + blank + reg_name1;
mout << ans << endl;
ans = "sra " + reg_name_dst + blank + reg_name1 + blank + constant;
mout << ans << endl;
ans = "neg " + reg_name_dst + blank + reg_name_dst;
mout << ans << endl;
mout << "j RESERVED_" + to_string(reserved) << endl;
mout << reserved_name + ":" << endl;
reserved_name = "RESERVED_" + to_string(reserved);
reserved++;
ans = "sra " + reg_name_dst + blank + reg_name1 + blank + constant;
mout << ans << endl;
mout << reserved_name + ":" << endl;
last_output = ans;
}
#endif //UNTITLED_MIPS_COMMAND_H