-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
211 lines (160 loc) · 4.38 KB
/
Copy pathcompiler.cpp
File metadata and controls
211 lines (160 loc) · 4.38 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "compiler.h"
#include "types.h"
#include <string>
Compiler::Compiler() {
outFile = std::ofstream("a.asm");
// Generate 'headers'
}
Compiler::~Compiler() {
}
void Compiler::Compile(Parser *parser) {
generateDataSection();
generateBssSection();
generateTextSection(parser);
}
void Compiler::generateDataSection() {
writeLine("SECTION .data ; Section containing initialized data");
writeLine("");
}
void Compiler::generateBssSection() {
writeLine("SECTION .bss ; Section containing uninitialized data");
writeInstr("savedEAX resb 4");
writeInstr("savedEDX resb 4");
writeLine("");
}
void Compiler::generateTextSection(Parser *parser) {
writeLine("SECTION .text ; Section containing code\n");
writeLine("global main\n");
writeLine("main:");
writeInstr("nop");
std::vector<StatementStruct *> statements = parser->parse();
for (int i = 0; i < statements.size(); i++) {
translate(statements.at(i));
}
}
void Compiler::translate(StatementStruct *statement) {
writeInstr("; Line " + std::to_string(statement->line));
switch (statement->type) {
case LOAD:
translateLoad(statement->ptr);
break;
case STORE:
translateStore(statement->ptr);
break;
case JUMP:
translateJump(statement->ptr);
break;
case CONVERT:
translateConvert(statement->ptr);
break;
case KEYWORD:
translateKeyword(statement->ptr);
break;
default:
break;
}
writeLine("");
}
void Compiler::translateStore(void *ptr) {
Parser::StoreStruct *structure = (Parser::StoreStruct *) ptr;
if (structure->dest.operation != "") {
translateExpression(structure->dest);
std::string line = "mov ";
line += operationSpecifier(structure->store) + " [EAX], ";
line += instantToString(structure->val, structure->store);
writeInstr(line);
writeInstr("mov EAX, [savedEAX]");
} else {
std::string line = "mov " + operationSpecifier(structure->store);
line += " [" + instantToString(structure->dest.operand1) + "], ";
line += instantToString(structure->val, structure->store);
writeInstr(line);
}
}
void Compiler::translateLoad(void *ptr) {
Parser::LoadStruct *structure = (Parser::LoadStruct *) ptr;
if (structure->expr.operation != "") {
translateExpression(structure->expr);
if (structure->reference)
writeInstr("mov " + toRealRegister(structure->reg, structure->load) +", [EAX]");
else
writeInstr("mov " + toRealRegister(structure->reg, structure->load) +", EAX");
writeInstr("mov EAX, [savedEAX]");
} else {
writeInstr("mov " + toRealRegister(structure->reg, structure->load) + ", "
+ instantToString(structure->expr.operand1, structure->load));
}
}
void Compiler::translateJump(void *ptr) {
}
void Compiler::translateConvert(void *ptr) {
}
void Compiler::translateKeyword(void *ptr) {
}
std::string Compiler::operationSpecifier(int size) {
switch (size) {
case 1:
return "byte";
case 2:
return "word";
case 4:
return "dword";
default:
return "dword.";
}
}
std::string Compiler::instantToString(Token tok) {
if (tok.type == REG)
return toRealRegister(tok.value);
return tok.value;
}
std::string Compiler::instantToString(Token tok, int size) {
if (tok.type == REG)
return toRealRegister(tok.value, size);
return tok.value;
}
void Compiler::writeOut(std::string str) {
outFile.write(str.c_str(), str.length());
}
void Compiler::writeLine(std::string line) {
writeOut(line + "\n");
}
void Compiler::writeInstr(std::string instr) {
writeLine("\t" + instr);
}
void Compiler::translateExpression(ExprStruct expr) {
writeInstr("mov dword [savedEAX], EAX");
writeInstr("mov EAX, " + instantToString(expr.operand1));
if (expr.operation == "") return;
switch (expr.operation[0]) {
case '+':
writeInstr("add EAX, " + instantToString(expr.operand2));
break;
case '-':
writeInstr("sub EAX, " + instantToString(expr.operand2));
break;
case '/':
translateExpressionDiv(expr);
break;
case '*':
writeInstr("imul EAX, " + instantToString(expr.operand2));
break;
case '^':
writeInstr("xor EAX, " + instantToString(expr.operand2));
break;
case '&':
writeInstr("and EAX, " + instantToString(expr.operand2));
break;
case '|':
writeInstr("or EAX, " + instantToString(expr.operand2));
break;
default:
break;
}
}
void Compiler::translateExpressionDiv(ExprStruct expr) {
writeInstr("mov dword [savedEDX], EDX");
writeInstr("mov EDX, 0");
writeInstr("idiv " + instantToString(expr.operand2));
writeInstr("mov EDX, [savedEDX]");
}