-
Notifications
You must be signed in to change notification settings - Fork 2
/
MidCode.cpp
174 lines (171 loc) · 4.38 KB
/
MidCode.cpp
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
#include"MidCode.h"
#include<map>
SymbolTable* MidCode::table = nullptr;
int MidCode::labelCount = -2;
int MidCode::tmpVarNo = -2;
int MidCode::tmpVarAlloc() {
return tmpVarNo--;
}
int MidCode::labelAlloc() {
return labelCount--;
}
string MidCode::getOperandName(int n,bool isImmediate) {
if (isImmediate) {
return to_string(n);
}
if (n >= 0) {
SymbolEntry* entry = table->getSymbolById(n);
return entry->name;
}
else if (n == -1) {
return "RET";
}
else {
return string("#TMP") + to_string(-n);
}
}
string MidCode::getLabelName(int n) {
if (n >= 0) {
SymbolEntry* entry = table->getSymbolById(n);
return entry->name;
}
else {
string tmp = "_label";
return tmp + to_string(-n);
}
}
ostream& operator<<(ostream& out, MidCode c) {
vector<string> type = { "int","char","void" };
map<MidCodeOp, string>ops;
ops[MIDADD] = "+";
ops[MIDSUB] = "-";
ops[MIDMULT] = "*";
ops[MIDDIV] = "/";
ops[MIDLSS] = "<";
ops[MIDLEQ] = "<=";
ops[MIDGRE] = ">";
ops[MIDGEQ] = ">=";
ops[MIDEQL] = "==";
ops[MIDNEQ] = "!=";
ops[MIDREM] = "mod";
if (c.labelNo < -1) {
out << "_label" << -(c.labelNo) << ": ";
}
else if(c.labelNo>=0) {
out <<MidCode::getOperandName(c.labelNo,false) << ": " << endl;
}
switch (c.op) {
case MIDFUNC:
out << type[c.operand2] << " ";
out << MidCode::getOperandName(c.operand1,c.isImmediate1)<< "()";
break;
case MIDPARA:
out << "para " << type[c.operand2]<<" ";
out << MidCode::getOperandName(c.operand1, c.isImmediate1);
break;
case MIDPUSH:
out << "push ";
out << MidCode::getOperandName(c.operand1, c.isImmediate1);
break;
case MIDCALL:
out << "call ";
out << MidCode::getOperandName(c.operand1, c.isImmediate1);
break;
case MIDRET:
out << "ret ";
if (c.operand1 == -1) {
break;
}
else {
out << MidCode::getOperandName(c.operand1, c.isImmediate1);
}
break;
case MIDADD:
case MIDSUB:
case MIDMULT:
case MIDDIV:
case MIDLSS:
case MIDLEQ:
case MIDGRE:
case MIDGEQ:
case MIDEQL:
case MIDNEQ:
case MIDREM:
out << MidCode::getOperandName(c.target,false);
out << " " << "=" << " ";
out << MidCode::getOperandName(c.operand1, c.isImmediate1);
out << " "<<ops[c.op]<<" ";
out << MidCode::getOperandName(c.operand2, c.isImmediate2);
break;
case MIDNEGATE:
out << MidCode::getOperandName(c.target, false);
out << " " << "=" << " negate ";
out << MidCode::getOperandName(c.operand1, c.isImmediate1);
break;
case MIDARRAYGET:
out << MidCode::getOperandName(c.target,false);
out << " " << "=" << " ";
out << MidCode::getOperandName(c.operand1, c.isImmediate1);
out << " [ " << MidCode::getOperandName(c.operand2, c.isImmediate2) << " ] ";
break;
case MIDARRAYWRITE:
out << MidCode::getOperandName(c.target,false);
out << "[" << MidCode::getOperandName(c.operand1, c.isImmediate1) << "] =";
out << MidCode::getOperandName(c.operand2, c.isImmediate2);
break;
case MIDASSIGN:
out << MidCode::getOperandName(c.target,false);
out << "=" << MidCode::getOperandName(c.operand1, c.isImmediate1);
break;
case MIDPRINTINT:
out << "syscall-1 " << MidCode::getOperandName(c.operand1,c.isImmediate1);
break;
case MIDPRINTCHAR:
out << "syscall-11 " << MidCode::getOperandName(c.operand1, c.isImmediate1);
break;
case MIDPRINTSTRING:
out << "syscall-4 ";
out << MidCode::table->getString(c.operand1);
break;
case MIDREADINTEGER:
out << MidCode::getOperandName(c.target, false) <<" = syscall-5 ";
break;
case MIDREADCHAR:
out << MidCode::getOperandName(c.target, false) << " = syscall-12 " ;
break;
case MIDGOTO:
out << "GOTO "<<MidCode::getLabelName(c.operand1);
break;
case MIDBNZ:
case MIDBZ:
out << ((c.op == MIDBZ) ? "BZ " : "BNZ ");
out << MidCode::getOperandName(c.operand1,c.isImmediate1) << " " << MidCode::getLabelName(c.operand2);
break;
case MIDNOP:
out << "nop";
break;
default:
out << "unknown instruction";
}
/*out << " active[";
for (int i : c.activeVariable) {
out << i << " ";
}
out <<"]"<< endl;*/
out << endl;
return out;
}
MidCode MidCode::generateMidCode(MidCodeOp op, int target,
int operand1, bool isImmediate1,
int operand2, bool isImmediate2,
int label = -1) {
MidCode tmp;
tmp.op = op;
tmp.target = target;
tmp.operand1 = operand1;
tmp.operand2 = operand2;
tmp.isImmediate1 = isImmediate1;
tmp.isImmediate2 = isImmediate2;
tmp.labelNo = label;
return tmp;
}