-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.cpp
More file actions
188 lines (136 loc) · 4.22 KB
/
CodeGenerator.cpp
File metadata and controls
188 lines (136 loc) · 4.22 KB
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
188
#include "CodeGenerator.h"
void CodeGenerator::CodeGenerate(string path)
{
OutFile.open(path,ios::out);
if(!OutFile.is_open())
{
cout<<"Create the assembly file error!"<<endl;
return;
}
cout<<endl<<"--- CodeGenerate Begin ---"<<endl;
CodeGenerate_Head();
CodeGenerate_Translation_Unit(ast.Translation);
CodeGenerate_Tail();
cout<<"--- CodeGenerate Done ---"<<endl<<endl;
OutFile.close();
}
void CodeGenerator::CodeGenerate_Head()
{
OutFile<<"section .text"<<endl;
OutFile<<"\tglobal main"<<endl;
OutFile<<"\textern printf"<<endl<<endl;
OutFile<<"main:"<<endl;
}
void CodeGenerator::CodeGenerate_Translation_Unit(ASTNode* root)
{
WhoAmI("CodeGenerate_Translation_Unit");
for(int i=0;i<root->Children.size();i++)
{
CodeGenerate_Statement(root->Children[i]);
}
}
void CodeGenerator::CodeGenerate_Statement(ASTNode* root)
{
WhoAmI("CodeGenerate_Statement");
int print_ri=CodeGenerate_Expression(root->Children[1]);
Print(print_ri);
}
int CodeGenerator::CodeGenerate_Expression(ASTNode* root)
{
WhoAmI("CodeGenerate_Expression");
return CodeGenerate_PlusMinus_Expression(FirstChild(root));
}
int CodeGenerator::CodeGenerate_PlusMinus_Expression(ASTNode* root)
{
WhoAmI("CodeGenerate_PlusMinus_Expression");
int result_ri=CodeGenerate_MulDiv_Expression(FirstChild(root));
for(int i=1;i<root->Children.size();i+=2)
{
int temp_ri=CodeGenerate_MulDiv_Expression(root->Children[i+1]);
if(root->Children[i]->type==AST_PLUS)result_ri=Add(result_ri,temp_ri);
else if(root->Children[i]->type==AST_MINUS)result_ri=Sub(result_ri,temp_ri);
}
return result_ri;
}
int CodeGenerator::CodeGenerate_MulDiv_Expression(ASTNode* root)
{
WhoAmI("CodeGenerate_MulDiv_Expression");
int result_ri=CodeGenerate_Unary_Expression(FirstChild(root));
for(int i=1;i<root->Children.size();i+=2)
{
int temp_ri=CodeGenerate_Unary_Expression(root->Children[i+1]);
if(root->Children[i]->type==AST_STAR)result_ri=Mul(result_ri,temp_ri);
else if(root->Children[i]->type==AST_SLASH)result_ri=Div(result_ri,temp_ri);
}
return result_ri;
}
int CodeGenerator::CodeGenerate_Unary_Expression(ASTNode* root)
{
WhoAmI("CodeGenerate_Unary_Expression");
return CodeGenerate_Primary_Expression(FirstChild(root));
}
int CodeGenerator::CodeGenerate_Primary_Expression(ASTNode* root)
{
WhoAmI("CodeGenerate_Primary_Expression");
return Load(FirstChild(root)->value);
}
void CodeGenerator::CodeGenerate_Tail()
{
OutFile<<endl<<"exit:"<<endl;
OutFile<<"\tmov\trax, 0"<<endl;
OutFile<<"\tret"<<endl<<endl;
OutFile<<"section .data"<<endl;
OutFile<<"\tformat: db \"%d\",0XA,0"<<endl;
}
ASTNode* CodeGenerator::FirstChild(ASTNode* root)
{
if(root->Children.size()>=1)return root->Children[0];
return nullptr;
}
int CodeGenerator::Load(int value)
{
int register_i=R.Alloc();
OutFile<<"\tmov\t"<<R.Register_Table[register_i].name<<", "<<value<<endl;
return register_i;
}
int CodeGenerator::Add(int r1_i,int r2_i)
{
OutFile<<"\tadd\t"<<R.Register_Table[r1_i].name<<", "<<R.Register_Table[r2_i].name<<endl;
R.Free(r2_i);
return r1_i;
}
int CodeGenerator::Sub(int r1_i,int r2_i)
{
OutFile<<"\tsub\t"<<R.Register_Table[r1_i].name<<", "<<R.Register_Table[r2_i].name<<endl;
R.Free(r2_i);
return r1_i;
}
int CodeGenerator::Mul(int r1_i,int r2_i)
{
OutFile<<"\timul\t"<<R.Register_Table[r1_i].name<<", "<<R.Register_Table[r2_i].name<<endl;
R.Free(r2_i);
return r1_i;
}
int CodeGenerator::Div(int r1_i,int r2_i)
{
OutFile<<"\tmov\trax, "<<R.Register_Table[r1_i].name<<endl;
OutFile<<"\tcqo"<<endl;
OutFile<<"\tidiv\t"<<R.Register_Table[r2_i].name<<endl;
OutFile<<"\tmov\t"<<R.Register_Table[r1_i].name<<", rax"<<endl;
R.Free(r2_i);
return r1_i;
}
void CodeGenerator::Print(int r_i)
{
OutFile<<"\tpush\trbp"<<endl;
OutFile<<"\tmov\trdi, format"<<endl;
OutFile<<"\tmov\trsi, "<<R.Name(r_i)<<endl;
OutFile<<"\tmov\trax, 0"<<endl;
OutFile<<"\tcall\tprintf"<<endl;
OutFile<<"\tpop\trbp"<<endl;
R.Free(r_i);
}
void CodeGenerator::WhoAmI(string name)
{
if(DEBUG)cout<<name<<endl;
}