-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlowlevelcodegen.cpp
More file actions
371 lines (344 loc) · 13.2 KB
/
lowlevelcodegen.cpp
File metadata and controls
371 lines (344 loc) · 13.2 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "lowlevelcodegen.h"
#include <iostream>
#include "highlevel.h"
#include "x86_64.h"
LowLevelCodeGen::LowLevelCodeGen(SymbolTable* symtab, int vregs): vregs_used(vregs), _iseq(new InstructionSequence),
symtab(symtab) {}
LowLevelCodeGen::~LowLevelCodeGen() {}
InstructionSequence* LowLevelCodeGen::get_iseq() const {
return _iseq;
}
Operand LowLevelCodeGen::vreg_ref(Operand op) {
if (!op.has_base_reg()) return op; // op is actually a literal
const int offset = vreg_refs.at(op.get_base_reg());
return Operand(OPERAND_MREG_MEMREF_OFFSET, MREG_RSP, offset);
}
void LowLevelCodeGen::generate(InstructionSequence* hl_iseq) {
// generate the boilerplate
std::cout << "/* " << vregs_used << " vregs with storage allocated */" << '\n';
std::cout << "\t.section .rodata" << '\n';
std::cout << R"(s_readint_fmt: .string "%ld")" << '\n';
std::cout << R"(s_writeint_fmt: .string "%ld\n")" << '\n';
std::cout << "\t.section .bss" << '\n';
std::cout << "\t.align 8" << '\n';
std::cout << "s_readbuf: .space 8" << '\n';
std::cout << "\t.section .text" << '\n';
std::cout << "\t.globl main" << '\n';
std::cout << "main:" << '\n';
// calculate vreg memory locations
int offset = symtab->get_offset(); // from start of vreg memory
for (int i = 0; i <= vregs_used; i++) {
vreg_refs[i] = offset;
offset += 8;
}
// align to 16 byte boundary
offset += offset % 16;
auto ins = new Instruction(MINS_SUBQ, Operand(OPERAND_INT_LITERAL, offset), Operand(OPERAND_MREG, MREG_RSP));
//_iseq->define_label("main");
_iseq->add_instruction(ins);
// generate program
// load constants into memory
ins = new Instruction(MINS_NOP);
ins->set_comment("load constants");
_iseq->add_instruction(ins);
for (const auto sym : symtab->get_syms()) {
if (sym->get_kind() == CONST) {
const auto memref = Operand(OPERAND_MREG_MEMREF_OFFSET, MREG_RSP, sym->get_offset());
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_INT_LITERAL, sym->get_ival()), memref);
_iseq->add_instruction(ins);
}
}
// generate instructions
for (unsigned int i = 0; i < hl_iseq->get_length(); i++) {
if (hl_iseq->has_label(i)) _iseq->define_label(hl_iseq->get_label(i));
const auto hlins = hl_iseq->get_instruction(i);
int opcode = hlins->get_opcode();
switch (opcode) {
case HINS_NOP:
generate_nop(hlins);
break;
case HINS_LOAD_ICONST:
generate_load_int_literal(hlins);
break;
case HINS_INT_ADD:
generate_add(hlins);
break;
case HINS_INT_SUB:
generate_sub(hlins);
break;
case HINS_INT_MUL:
generate_mul(hlins);
break;
case HINS_INT_DIV:
generate_div(hlins);
break;
case HINS_INT_MOD:
generate_mod(hlins);
break;
case HINS_INT_NEGATE:
generate_negate(hlins);
break;
case HINS_LOCALADDR:
generate_localaddr(hlins);
break;
case HINS_LOAD_INT:
generate_load_int(hlins);
break;
case HINS_STORE_INT:
generate_store_int(hlins);
break;
case HINS_READ_INT:
generate_read_int(hlins);
break;
case HINS_WRITE_INT:
generate_write_int(hlins);
break;
case HINS_JUMP:
generate_jump(hlins);
break;
case HINS_JE:
generate_je(hlins);
break;
case HINS_JNE:
generate_jne(hlins);
break;
case HINS_JLT:
generate_jlt(hlins);
break;
case HINS_JLTE:
generate_jlte(hlins);
break;
case HINS_JGT:
generate_jgt(hlins);
break;
case HINS_JGTE:
generate_jgte(hlins);
break;
case HINS_INT_COMPARE:
generate_compare(hlins);
break;
case HINS_MOV:
generate_mov(hlins);
break;
default:
assert(false); // unknown opcode
}
}
if (hl_iseq->has_label_at_end()) _iseq->define_label(hl_iseq->get_label_at_end());
ins = new Instruction(MINS_ADDQ, Operand(OPERAND_INT_LITERAL, offset), Operand(OPERAND_MREG, MREG_RSP));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_INT_LITERAL, 0), Operand(OPERAND_MREG, MREG_RAX));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_RET);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_nop(Instruction* hlins) {
const auto ins = new Instruction(MINS_NOP);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_load_int_literal(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto sourcereg = vreg_ref((*hlins)[1]);
const auto ins = new Instruction(MINS_MOVQ, sourcereg, destreg);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_add(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto leftreg = vreg_ref((*hlins)[1]);
const auto rightreg = vreg_ref((*hlins)[2]);
auto ins = new Instruction(MINS_MOVQ, leftreg, Operand(OPERAND_MREG, MREG_RAX));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_ADDQ, rightreg, Operand(OPERAND_MREG, MREG_RAX));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_RAX), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_sub(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto leftreg = vreg_ref((*hlins)[1]);
const auto rightreg = vreg_ref((*hlins)[2]);
auto ins = new Instruction(MINS_MOVQ, leftreg, Operand(OPERAND_MREG, MREG_RAX));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_SUBQ, rightreg, Operand(OPERAND_MREG, MREG_RAX));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_RAX), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_mul(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto leftreg = vreg_ref((*hlins)[1]);
const auto rightreg = vreg_ref((*hlins)[2]);
auto ins = new Instruction(MINS_MOVQ, leftreg, Operand(OPERAND_MREG, MREG_RAX));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_IMULQ, rightreg, Operand(OPERAND_MREG, MREG_RAX));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_RAX), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_div(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto leftreg = vreg_ref((*hlins)[1]);
const auto rightreg = vreg_ref((*hlins)[2]);
auto ins = new Instruction(MINS_MOVQ, leftreg, Operand(OPERAND_MREG, MREG_RAX));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_CQTO);
_iseq->add_instruction(ins);
ins = new Instruction(MINS_IDIVQ, rightreg);
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_RAX), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_mod(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto leftreg = vreg_ref((*hlins)[1]);
const auto rightreg = vreg_ref((*hlins)[2]);
auto ins = new Instruction(MINS_MOVQ, leftreg, Operand(OPERAND_MREG, MREG_RAX));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_CQTO);
_iseq->add_instruction(ins);
ins = new Instruction(MINS_IDIVQ, rightreg);
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_RDX), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_negate(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto sourcereg = vreg_ref((*hlins)[1]);
auto ins = new Instruction(MINS_MOVQ, sourcereg, Operand(OPERAND_MREG, MREG_RAX));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_IMULQ, Operand(OPERAND_INT_LITERAL, -1), Operand(OPERAND_MREG, MREG_RAX));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_RAX), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_localaddr(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto sourcereg = vreg_ref((*hlins)[1]);
const auto effective_address = Operand(OPERAND_MREG_MEMREF_OFFSET, MREG_RSP, sourcereg.get_int_value());
auto ins = new Instruction(MINS_LEAQ, effective_address, Operand(OPERAND_MREG, MREG_R10));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_R10), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_load_int(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto sourcereg = vreg_ref((*hlins)[1]);
auto ins = new Instruction(MINS_MOVQ, sourcereg, Operand(OPERAND_MREG, MREG_R10));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG_MEMREF, MREG_R10), Operand(OPERAND_MREG, MREG_R10));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_R10), destreg);
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_store_int(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto sourcereg = vreg_ref((*hlins)[1]);
auto ins = new Instruction(MINS_MOVQ, destreg, Operand(OPERAND_MREG, MREG_R11));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, sourcereg, Operand(OPERAND_MREG, MREG_R10));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_R10), Operand(OPERAND_MREG_MEMREF, MREG_R11));
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_read_int(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
auto ins = new Instruction(MINS_MOVQ, Operand("s_readint_fmt", true), Operand(OPERAND_MREG, MREG_RDI));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_LEAQ, destreg, Operand(OPERAND_MREG, MREG_RSI));
_iseq->add_instruction(ins);
// zero out %rax
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_INT_LITERAL, 0), Operand(OPERAND_MREG, MREG_RAX));
_iseq->add_instruction(ins);
// stack alignment
ins = new Instruction(MINS_SUBQ, Operand(OPERAND_INT_LITERAL, 8), Operand(OPERAND_MREG, MREG_RSP));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_CALL, Operand("scanf"));
_iseq->add_instruction(ins);
// stack alignment
ins = new Instruction(MINS_ADDQ, Operand(OPERAND_INT_LITERAL, 8), Operand(OPERAND_MREG, MREG_RSP));
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_write_int(Instruction* hlins) {
const auto sourcereg = vreg_ref((*hlins)[0]);
auto ins = new Instruction(MINS_MOVQ, Operand("s_writeint_fmt", true), Operand(OPERAND_MREG, MREG_RDI));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, sourcereg, Operand(OPERAND_MREG, MREG_RSI));
_iseq->add_instruction(ins);
// zero out %rax
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_INT_LITERAL, 0), Operand(OPERAND_MREG, MREG_RAX));
_iseq->add_instruction(ins);
ins = new Instruction(MINS_CALL, Operand("printf"));
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_jump(Instruction* hlins) {
const auto label = (*hlins)[0];
const auto ins = new Instruction(MINS_JMP, label);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_je(Instruction* hlins) {
const auto label = (*hlins)[0];
const auto ins = new Instruction(MINS_JE, label);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_jne(Instruction* hlins) {
const auto label = (*hlins)[0];
const auto ins = new Instruction(MINS_JNE, label);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_jlt(Instruction* hlins) {
const auto label = (*hlins)[0];
const auto ins = new Instruction(MINS_JL, label);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_jlte(Instruction* hlins) {
const auto label = (*hlins)[0];
const auto ins = new Instruction(MINS_JLE, label);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_jgt(Instruction* hlins) {
const auto label = (*hlins)[0];
const auto ins = new Instruction(MINS_JG, label);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_jgte(Instruction* hlins) {
const auto label = (*hlins)[0];
const auto ins = new Instruction(MINS_JGE, label);
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_compare(Instruction* hlins) {
const auto leftreg = vreg_ref((*hlins)[0]);
const auto rightreg = vreg_ref((*hlins)[1]);
auto ins = new Instruction(MINS_MOVQ, leftreg, Operand(OPERAND_MREG, MREG_R10));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_CMPQ, rightreg, Operand(OPERAND_MREG, MREG_R10));
_iseq->add_instruction(ins);
}
void LowLevelCodeGen::generate_mov(Instruction* hlins) {
const auto destreg = vreg_ref((*hlins)[0]);
const auto sourcereg = vreg_ref((*hlins)[1]);
auto ins = new Instruction(MINS_MOVQ, sourcereg, Operand(OPERAND_MREG, MREG_R10));
ins->set_comment(hlins->get_comment());
_iseq->add_instruction(ins);
ins = new Instruction(MINS_MOVQ, Operand(OPERAND_MREG, MREG_R10), destreg);
_iseq->add_instruction(ins);
}