-
Notifications
You must be signed in to change notification settings - Fork 1
/
codegen_x86.hpp
290 lines (288 loc) · 10.4 KB
/
codegen_x86.hpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#pragma once
#include "ast.hpp"
#include "assembler.hpp"
class CodegenX86: public Visitor<std::uint32_t> {
using A = Assembler;
using Jump = typename A::Jump;
static std::uint32_t get_type_size(const Type* type) {
switch (type->get_id()) {
case TypeId::INT:
return 4;
case TypeId::TUPLE:
{
std::uint32_t size = 0;
for (const Type* element_type: static_cast<const TupleType*>(type)->get_element_types()) {
size += get_type_size(element_type);
}
return size;
}
default:
return 0;
}
}
static std::uint32_t get_input_size(const Function* function) {
std::uint32_t input_size = 0;
for (const Type* type: function->get_argument_types()) {
input_size += get_type_size(type);
}
return input_size;
}
static std::uint32_t get_output_size(const Function* function) {
return get_type_size(function->get_return_type());
}
struct DeferredCall {
Jump jump;
const Function* function;
DeferredCall(const Jump& jump, const Function* function): jump(jump), function(function) {}
};
std::vector<DeferredCall>& deferred_calls;
const Function* function;
A& assembler;
using ExpressionTable = std::map<const Expression*,std::uint32_t>;
ExpressionTable& expression_table;
std::uint32_t variable = 0;
std::uint32_t result;
CodegenX86(std::vector<DeferredCall>& deferred_calls, const Function* function, A& assembler, ExpressionTable& expression_table, std::uint32_t result): deferred_calls(deferred_calls), function(function), assembler(assembler), expression_table(expression_table), result(result) {}
static void evaluate(std::vector<DeferredCall>& deferred_calls, const Function* function, A& assembler, ExpressionTable& expression_table, std::uint32_t result, const Block& block) {
CodegenX86 codegen(deferred_calls, function, assembler, expression_table, result);
for (const Expression* expression: block) {
expression_table[expression] = visit(codegen, expression);
}
}
static void evaluate(std::vector<DeferredCall>& deferred_calls, const Function* function, A& assembler, std::uint32_t result, const Block& block) {
ExpressionTable expression_table;
evaluate(deferred_calls, function, assembler, expression_table, result, block);
}
void evaluate(std::uint32_t result, const Block& block) {
evaluate(deferred_calls, function, assembler, expression_table, result, block);
}
std::uint32_t allocate(std::uint32_t size) {
variable -= size;
return variable;
}
void memcopy(std::uint32_t destination, std::uint32_t source, std::uint32_t size) {
for (std::uint32_t i = 0; i < size; i += 4) {
assembler.MOV(EAX, PTR(EBP, source + i));
assembler.MOV(PTR(EBP, destination + i), EAX);
}
}
public:
std::uint32_t visit_int_literal(const IntLiteral& int_literal) override {
const std::uint32_t result = allocate(4);
assembler.MOV(PTR(EBP, result), int_literal.get_value());
return result;
}
std::uint32_t visit_binary_expression(const BinaryExpression& binary_expression) override {
const std::uint32_t left = expression_table[binary_expression.get_left()];
const std::uint32_t right = expression_table[binary_expression.get_right()];
assembler.MOV(EAX, PTR(EBP, left));
assembler.MOV(EBX, PTR(EBP, right));
const std::uint32_t result = allocate(4);
switch (binary_expression.get_operation()) {
case BinaryOperation::ADD:
assembler.ADD(EAX, EBX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::SUB:
assembler.SUB(EAX, EBX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::MUL:
assembler.IMUL(EBX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::DIV:
assembler.CDQ();
assembler.IDIV(EBX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::REM:
assembler.CDQ();
assembler.IDIV(EBX);
assembler.MOV(PTR(EBP, result), EDX);
break;
case BinaryOperation::EQ:
assembler.CMP(EAX, EBX);
assembler.SETE(EAX);
assembler.MOVZX(EAX, EAX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::NE:
assembler.CMP(EAX, EBX);
assembler.SETNE(EAX);
assembler.MOVZX(EAX, EAX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::LT:
assembler.CMP(EAX, EBX);
assembler.SETL(EAX);
assembler.MOVZX(EAX, EAX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::LE:
assembler.CMP(EAX, EBX);
assembler.SETLE(EAX);
assembler.MOVZX(EAX, EAX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::GT:
assembler.CMP(EAX, EBX);
assembler.SETG(EAX);
assembler.MOVZX(EAX, EAX);
assembler.MOV(PTR(EBP, result), EAX);
break;
case BinaryOperation::GE:
assembler.CMP(EAX, EBX);
assembler.SETGE(EAX);
assembler.MOVZX(EAX, EAX);
assembler.MOV(PTR(EBP, result), EAX);
break;
}
return result;
}
std::uint32_t visit_if(const If& if_) override {
const std::uint32_t condition = expression_table[if_.get_condition()];
assembler.MOV(EAX, PTR(EBP, condition));
assembler.CMP(EAX, 0);
const std::uint32_t size = get_type_size(if_.get_type());
const std::uint32_t result = allocate(size);
const Jump jump_else = assembler.JE();
assembler.comment("if");
evaluate(result, if_.get_then_block());
const Jump jump_end = assembler.JMP();
assembler.comment("else");
jump_else.set_target(assembler, assembler.get_position());
evaluate(result, if_.get_else_block());
assembler.comment("end");
jump_end.set_target(assembler, assembler.get_position());
return result;
}
std::uint32_t visit_tuple_literal(const TupleLiteral& tuple_literal) override {
const std::vector<const Expression*>& elements = tuple_literal.get_elements();
if (elements.size() == 1) {
return expression_table[elements[0]];
}
else {
for (const Expression* element: elements) {
const std::uint32_t element_size = get_type_size(element->get_type());
const std::uint32_t element_destination = allocate(element_size);
memcopy(element_destination, expression_table[element], element_size);
}
return variable;
}
}
std::uint32_t visit_tuple_access(const TupleAccess& tuple_access) override {
const std::uint32_t tuple = expression_table[tuple_access.get_tuple()];
const std::vector<const Type*>& element_types = static_cast<const TupleType*>(tuple_access.get_tuple()->get_type())->get_element_types();
std::uint32_t location = tuple + get_type_size(tuple_access.get_tuple()->get_type());
for (std::size_t i = 0; i <= tuple_access.get_index(); ++i) {
location -= get_type_size(element_types[i]);
}
return location;
}
std::uint32_t visit_argument(const Argument& argument) override {
const std::vector<const Type*>& argument_types = function->get_argument_types();
std::uint32_t location = 8 + std::max(get_input_size(function), get_output_size(function));
for (std::size_t i = 0; i <= argument.get_index(); ++i) {
location -= get_type_size(argument_types[i]);
}
return location;
}
std::uint32_t visit_function_call(const FunctionCall& call) override {
const std::uint32_t input_size = get_input_size(call.get_function());
const std::uint32_t output_size = get_output_size(call.get_function());
for (const Expression* argument: call.get_arguments()) {
const std::uint32_t argument_size = get_type_size(argument->get_type());
const std::uint32_t argument_destination = allocate(argument_size);
memcopy(argument_destination, expression_table[argument], argument_size);
}
if (output_size > input_size) {
variable -= output_size - input_size;
}
assembler.LEA(ESP, PTR(EBP, variable));
Jump jump = assembler.CALL();
deferred_calls.emplace_back(jump, call.get_function());
if (output_size < input_size) {
variable += input_size - output_size;
}
return variable;
}
std::uint32_t visit_intrinsic(const Intrinsic& intrinsic) override {
if (intrinsic.name_equals("putChar")) {
const std::uint32_t argument = expression_table[intrinsic.get_arguments()[0]];
assembler.comment("putChar");
assembler.MOV(EAX, 0x04);
assembler.MOV(EBX, 1); // stdout
assembler.LEA(ECX, PTR(EBP, argument));
assembler.MOV(EDX, 1);
assembler.INT(0x80);
assembler.POP(EAX);
return allocate(0);
}
else if (intrinsic.name_equals("getChar")) {
const std::uint32_t result = allocate(4);
assembler.comment("getChar");
assembler.MOV(EAX, 0x03);
assembler.MOV(EBX, 0); // stdin
assembler.LEA(ECX, PTR(EBP, result));
assembler.MOV(PTR(ECX), EBX);
assembler.MOV(EDX, 1);
assembler.INT(0x80);
return result;
}
else if (intrinsic.name_equals("arrayNew")) {
print_error(Printer(std::cerr), "the x86 codegen does not support arrays");
std::exit(EXIT_FAILURE);
}
else {
return allocate(0);
}
}
std::uint32_t visit_bind(const Bind& bind) override {
return allocate(0);
}
std::uint32_t visit_return(const Return& return_) override {
assembler.comment("return");
const std::uint32_t size = get_type_size(return_.get_expression()->get_type());
memcopy(result, expression_table[return_.get_expression()], size);
return allocate(0);
}
static void codegen(const Program& program, const char* source_path, const TailCallData& tail_call_data) {
std::vector<DeferredCall> deferred_calls;
std::map<const Function*, std::size_t> function_locations;
A assembler;
assembler.write_headers();
{
Jump jump = assembler.CALL();
deferred_calls.emplace_back(jump, program.get_main_function());
assembler.comment("exit");
assembler.MOV(EAX, 0x01);
assembler.MOV(EBX, 0);
assembler.INT(0x80);
}
for (const Function* function: program) {
assembler.comment("function");
function_locations[function] = assembler.get_position();
assembler.PUSH(EBP);
assembler.MOV(EBP, ESP);
assembler.comment("--");
const std::uint32_t output_size = get_output_size(function);
const std::uint32_t size = std::max(get_input_size(function), output_size);
CodegenX86::evaluate(deferred_calls, function, assembler, 8 + size - output_size, function->get_block());
assembler.comment("--");
assembler.MOV(ESP, EBP);
assembler.POP(EBP);
assembler.RET();
}
for (const DeferredCall& deferred_call: deferred_calls) {
const std::size_t target = function_locations[deferred_call.function];
deferred_call.jump.set_target(assembler, target);
}
std::string path = std::string(source_path) + ".exe";
assembler.write_file(path.c_str());
Printer status_printer(std::cerr);
status_printer.print(bold(path));
status_printer.print(bold(green(" successfully generated")));
status_printer.print('\n');
}
};