-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunction.cpp
187 lines (176 loc) · 4.76 KB
/
function.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
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <string>
#include <map>
#include <sstream>
#include <iostream>
#include "symbolItem.h"
#include "function.h"
#include "midCode.h"
using namespace std;
extern map<string, symbolItem> globalSymbolTable;
extern map<string, symbolItem> localSymbolTable;
extern map<string, map<string, symbolItem>> allLocalSymbolTable; //保存所有的局部符号表 用于保留变量的地址
extern vector<string> stringList; //保存所有的字符串
extern map<string, vector<midCode> > funcMidCodeTable;
int labelId = 0; //标号的id
int tmpVarId = 0; //中间变量的id
int nameId = 0; //函数内联新产生的变量名常量名数组名
void showGlobal() {
cout << "----------------\n";
for (map<string, symbolItem>::iterator it = globalSymbolTable.begin(); it != globalSymbolTable.end(); it++) {
(*it).second.output();
}
cout << "----------------\n";
}
void showLocal() {
cout << "----------------\n";
for (map<string, symbolItem>::iterator it = localSymbolTable.begin(); it != localSymbolTable.end(); it++) {
(*it).second.output();
}
cout << "----------------\n";
}
void showAll() {
cout << "----------------\n";
for (map<string, map<string, symbolItem>>::iterator it = allLocalSymbolTable.begin(); it != allLocalSymbolTable.end(); it++) {
cout << "Func: " << (*it).first << "\n";
map<string, symbolItem> ss = (*it).second;
for (map<string, symbolItem>::iterator i = ss.begin(); i != ss.end(); i++) {
(*i).second.output();
}
}
cout << "----------------\n";
}
void showString() {
cout << "Show strings:\n";
for (int i = 0; i < stringList.size(); i++) {
cout << stringList[i] << "\n";
}
}
string int2string(int t) {
stringstream ss;
ss << t;
return ss.str();
}
int string2int(string s) {
stringstream ss;
ss << s;
int t;
ss >> t;
return t;
}
string genLabel(string app) {
labelId++;
return "Label" + int2string(labelId) + app;
}
string genTmp() {
tmpVarId++;
return "#T" + int2string(tmpVarId); //#开头 跟正常的变量区分开
}
string genName() {
nameId++;
return "%INLINE_" + int2string(nameId); //%开头 跟正常的变量区分开
}
void showFuncMidCode() {
cout << "*********************\n";
for (map<string, vector<midCode> >::iterator it = funcMidCodeTable.begin(); it != funcMidCodeTable.end(); it++) {
vector<midCode> ve = (*it).second;
for (int i = 0; i < ve.size(); i++) {
midCode mc = ve[i];
switch (mc.op) {
case PLUSOP:
cout << mc.z << " = " << mc.x << " + " << mc.y << "\n";
break;
case MINUOP:
cout << mc.z << " = " << mc.x << " - " << mc.y << "\n";
break;
case MULTOP:
cout << mc.z << " = " << mc.x << " * " << mc.y << "\n";
break;
case DIVOP:
cout << mc.z << " = " << mc.x << " / " << mc.y << "\n";
break;
case LSSOP:
cout << mc.z << " = (" << mc.x << " < " << mc.y << ")\n";
break;
case LEQOP:
cout << mc.z << " = (" << mc.x << " <= " << mc.y << ")\n";
break;
case GREOP:
cout << mc.z << " = (" << mc.x << " > " << mc.y << ")\n";
break;
case GEQOP:
cout << mc.z << " = (" << mc.x << " >= " << mc.y << ")\n";
break;
case EQLOP:
cout << mc.z << " = (" << mc.x << " == " << mc.y << ")\n";
break;
case NEQOP:
cout << mc.z << " = (" << mc.x << " != " << mc.y << ")\n";
break;
case ASSIGNOP:
cout << mc.z << " = " << mc.x << "\n";
break;
case GOTO:
cout << "GOTO " << mc.z << "\n";
break;
case BZ:
cout << "BZ " << mc.z << "(" << mc.x << "=0)" << "\n";
break;
case BNZ:
cout << "BNZ " << mc.z << "(" << mc.x << "=1)" << "\n";
break;
case PUSH:
cout << "PUSH " << mc.z << "\n";
break;
case CALL:
cout << "CALL " << mc.z << "\n";
break;
case RET:
cout << "RET " << mc.z << "\n";
break;
case INLINERET:
cout << "INLINERET " << mc.z << "\n";
break;
case RETVALUE:
cout << "RETVALUE " << mc.z << " = " << mc.x << "\n";
break;
case SCAN:
cout << "SCAN " << mc.z << "\n";
break;
case PRINT:
cout << "PRINT " << mc.z << " " << mc.x << "\n";
break;
case LABEL:
cout << mc.z << ": \n";
break;
case CONST:
cout << "CONST " << mc.z << " " << mc.x << " = " << mc.y << endl;
break;
case ARRAY:
cout << "ARRAY " << mc.z << " " << mc.x << "[" << mc.y << "]" << endl;
break;
case VAR:
cout << "VAR " << mc.z << " " << mc.x << endl;
break;
case FUNC:
cout << "FUNC " << mc.z << " " << mc.x << "()" << endl;
break;
case PARAM:
cout << "PARA " << mc.z << " " << mc.x << endl;
break;
case GETARRAY:
cout << mc.z << " = " << mc.x << "[" << mc.y << "]\n";
break;
case PUTARRAY:
cout << mc.z << "[" << mc.x << "]" << " = " << mc.y << "\n";
break;
case EXIT:
cout << "EXIT\n";
break;
default:
break;
}
}
cout << "\n";
}
cout << "*********************\n";
}