This repository has been archived by the owner on Mar 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringHandler.h
142 lines (118 loc) · 3.61 KB
/
StringHandler.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
#ifndef COMPILER_STRINGHANDLER_H
#define COMPILER_STRINGHANDLER_H
int tempCount = 0, labelCount = 0;
std::string stringAdder(int count, ...){
va_list varStringList;
int counter;
const char* t;
std::string s;
va_start(varStringList, count);
for(int counter=0;counter<count;counter++){
t = va_arg(varStringList, const char*);
std::string temp(t);
s += temp;
}
va_end(varStringList);
return s;
}
/* ******************* */
/* New Temporary */
/* Label */
/* Generator */
/* ******************* */
char* newLabel(){
char* newLabel = new char[4];
strcpy(newLabel, "L");
char newLabelCounter[3];
sprintf(newLabelCounter, "%d", labelCount);
labelCount++;
strcat(newLabel, newLabelCounter);
return newLabel;
}
/* ******************* */
/* New Temporary */
/* Variable */
/* Generator */
/* ******************* */
char* newTemp(){
char* tempVar = new char[4];
strcpy(tempVar, "t");
char tempVarCounter[3];
sprintf(tempVarCounter, "%d", tempCount);
tempCount++;
strcat(tempVar, tempVarCounter);
return tempVar;
}
std::vector<std::string> split_string(const std::string& str,const std::string& delimiter){
std::vector<std::string> splitted;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delimiter, prev)) != std::string::npos){
splitted.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
splitted.push_back(str.substr(prev));
return splitted;
}
bool is_valid_string(std::string a, std::string b){
int a_indx = a.find("MOV");
int b_indx = b.find("MOV");
if((a_indx!=std::string::npos)&&(b_indx!=std::string::npos)){
if((a.find(",")!=std::string::npos)&&(b.find(",")!=std::string::npos)){
return true;
}
}
return false;
}
/* Returns an optimized version of the assembly code */
std::string optimizer(std::string code)
{
std::string result;
int i;
bool is_it_last = false;
int temp;
std::vector<std::string>vect1;
std::vector<std::string>vect2;
std::vector<std::string>splitted_code =split_string(code,"\n");
// All the extra newlines from the splitted string are removed heree
for(int i=0;i<splitted_code.size();i++){
if(splitted_code[i]!=""){
vect1.push_back(splitted_code[i]);
}
}
splitted_code.clear();
for(int i=0;i<vect1.size();i++){
splitted_code.push_back(vect1[i]);
}
/* ********************************************* */
/* Here, checking pair of string is valid for */
/* comparison and then if necessary skip over */
/* concatenation which gives us optimized string */
/* ********************************************* */
vect1.clear();
for( i=0;i<splitted_code.size();i++){
temp = i;
if(i!=splitted_code.size()-1){
if(is_valid_string(splitted_code[i],splitted_code[i+1])){
std::string temp1 = splitted_code[i].substr(splitted_code[i].find(" ")+1,splitted_code[i].length()-1);
std::string temp2 = splitted_code[i+1].substr(splitted_code[i+1].find(" ")+1,splitted_code[i].length()-1);
vect1 = split_string(temp1,",");
vect2 = split_string(temp2,",");
if((vect1[0]==vect2[1])&&(vect1[1]==vect2[0])){
i++;
}
if((temp+1)==splitted_code.size()){
is_it_last = true;
}
}
}
if(!is_it_last){
result += splitted_code[temp]+"\n";
}
else{
is_it_last = false;
}
}
return result;
}
#endif