-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGen.cpp
117 lines (99 loc) · 2.76 KB
/
CodeGen.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
#include "CodeGen.hpp"
std::ostream& operator << (std::ostream& out, const Cmd& t){
return (out << commands[t]);
}
std::ostream& operator << (std::ostream& out, const Operands& t){
return (out << operands[t]);
}
ListCmd* addListCmd(ListCmd* list, AsmCmd* node){
if (list == NULL)
list = new ListCmd(node, NULL);
else{
auto temp_list = list;
while (temp_list->getNext() != NULL) temp_list = temp_list->getNext();
temp_list->setNext(new ListCmd(node, NULL));
temp_list->getNext()->setPrev(temp_list);
}
return list;
}
void AsmCode::addCmd0(Cmd cm){
list = addListCmd(list, new AsmCmd0(cm));
}
void AsmCode::addCmd1(Cmd cm, Operands oper){
list = addListCmd(list, new AsmCmd1(cm, new AsmReg(oper)));
}
void AsmCode::addCmdConst(Cmd cm, Scanner::Lexem Lex){
list = addListCmd(list, new AsmCmd1(cm, new AsmImmediate(Lex)));
}
void AsmCode::addCmdVar(Cmd cm, string a){
list = addListCmd(list, new AsmCmd1(cm , new AsmMem(a)));
}
void AsmCode::addCmdInit(Cmd cm, string a, int init){
list = addListCmd(list, new AsmInit(cm, a, init));
}
void AsmCode::addCmd2(Cmd cm, Operands first_oper, Operands sec_oper){
list = addListCmd(list, new AsmCmd2(cm , new AsmReg(first_oper), new AsmReg(sec_oper)));
}
void AsmCode :: print(){
printf(".686p\n.model flat\n");
if(!fmt.empty()){
printf("includelib MSVCRT.lib\n");
printf(".data\n");
printf("extrn _printf: proc\n");
map<string, string>::iterator it;
for(it = fmt.begin(); it != fmt.end(); it++)
printf("%s db '%s', 10, 0\n", (it->second).c_str(), (it->first).c_str());
}
else
printf(".data\n");
auto temp_list = list;
while(temp_list != NULL){
temp_list->print();
temp_list = temp_list->getNext();
}
printf("ret\nmain endp\nend\n");
}
Operands AsmCmd1 :: getOper(){
return oper->getOper();
}
void AsmCode :: optimize(){
auto temp_first = list;
auto temp_second = list->getNext();
while(temp_first != NULL && temp_second != NULL){
if(temp_first->getVal()->operator==(c_push) && temp_second->getVal()->operator==(c_pop)
&& temp_first->getVal()->getOper() == temp_second->getVal()->getOper()){
auto first = temp_first->getPrev();
auto second = temp_second->getNext();
//delete temp_first->getNext();
//delete second->getPrev();
first->setNext(second);
second->setPrev(first);
}
temp_first = temp_first->getNext();
temp_second = temp_second->getNext();
}
}
void ListCmd :: print(){
val->print();
}
void AsmCmd0 :: print(){
cout << command << endl;
}
void AsmInit :: print(){
cout << var << " " << command << " " << val << endl;
}
void AsmCmd1 :: print(){
cout << command << " ";
oper->print();
cout << endl;
}
void AsmCmd2 :: print(){
cout << command << " ";
first_oper->print();
cout << ", ";
sec_oper->print();
cout << endl;
}
void AsmReg :: print(){
cout << operand;
}