forked from Uyouii/cCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeOptimize.cpp
63 lines (53 loc) · 1.34 KB
/
codeOptimize.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
#include "codeOptimize.h"
#include <iostream>
#include <set>
using namespace std;
Optimize::Optimize(vector<string> codelist) {
//this->codelist = codelist;
establishMap(codelist);
dropTrumpTemp(codelist);
}
void Optimize::dropTrumpTemp(vector<string>& codelist) {
set<int> lines;
for (auto p = tempMessage.begin(); p != tempMessage.end(); p++) {
//cout << p->first << " " << p->second.num << endl;
if (p->second.num == 1) {
int line = p->second.line;
lines.insert(line);
}
}
for (int i = 0; i < codelist.size(); i++) {
if (lines.find(i) == lines.end()) {
this->codelist.push_back(codelist[i]);
}
}
}
void Optimize::establishMap(vector<string>& codelist) {
for (int i = 0; i < codelist.size(); i++) {
string str = codelist[i];
int begin = 0;
int w = str.find("temp", begin);
while (w != string::npos) {
int end = str.find(" ", w);
string name = str.substr(w, end - w);
if (tempMessage.find(name) != tempMessage.end()) {
if (str.find("CALL") != string::npos)
tempMessage[name].num += 1000;
tempMessage[name].num++;
}
else {
Message m;
m.line = i;
m.num = 1;
if (str.find("CALL") != string::npos)
m.num += 1000;
tempMessage.insert({ name,m });
}
begin = end;
w = str.find("temp", begin);
}
}
}
vector<string> Optimize::getCodeList() {
return this->codelist;
}