-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstruction.cpp
More file actions
477 lines (412 loc) · 11.8 KB
/
Instruction.cpp
File metadata and controls
477 lines (412 loc) · 11.8 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "Instruction.h"
#include "Type.h"
#include <fstream>
/*
Define how each instruction is emitted into .ll file.
*/
int Instruction::counter = 0;
int Instruction::label = 0;
Instruction::Instruction(Type* type) : type(type), address(""), isName(false), isNum(false), isBranch(false) {}
void Instruction::SetCounterToZero() {
counter = 0;
}
BinaryOperation::BinaryOperation(const char* bop, Instruction* lop, Instruction* rop, Type* type)
: binaryOp(bop), leftOperand(lop), rightOperand(rop), Instruction(type) {
live = false;
}
/*
Resolve labels for basic blocks
*/
void BinaryOperation::PassForBranch() {
char* name = new char[10];
sprintf(name, "%d", ++counter);
address = name;
address = "%" + address;
}
void BinaryOperation::CheckReferences() {
live = true;
leftOperand->CheckReferences();
rightOperand->CheckReferences();
}
/*
For binary operation, first emit the operator name, then two operands.
*/
void BinaryOperation::Emit(ofstream& os) {
os << this->address << " = ";
string op = binaryOp;
if(op == "+") {
os << "add ";
}
else if(op == "-") {
os << "sub ";
}
else if(op == "*") {
os << "mul ";
}
else if(op == "/") {
os << "sdiv";
}
else if(op == "%") {
os << "srem";
}
else if(op == "<") {
os << "icmp slt";
}
else if(op == ">") {
os << "icmp sgt";
}
else if(op == "<=") {
os << "icmp sle";
}
else if(op == ">=") {
os << "icmp sge";
}
else if(op == "==") {
os << "icmp eq";
}
else if(op == "!=") {
os << "icmp ne";
}
else if(op == "&" || op == "&&") {
os << "and";
}
else if(op == "^") {
os << "xor";
}
else if(op == "|" || op == "||") {
os << "or";
}
else if(op == ">>") {
os << "ashr";
}
else if (op == "<<") {
os << "ashl";
}
os << " ";
leftOperand->type->Emit(os);
os << " " << leftOperand->address << ",";
os << " ";
os << rightOperand->address << endl;
}
UnaryOperation::UnaryOperation(const char* uop, Instruction* operand, Type* type)
: unaryOp(uop), operand(operand), Instruction(type) {
live = false;
}
void UnaryOperation::CheckReferences() {
live = true;
operand->CheckReferences();
}
void UnaryOperation::Emit(ofstream& os) {}
void UnaryOperation::PassForBranch() {}
Compare::Compare(Instruction* leftOperand, Instruction* rightOperand) : leftOperand(leftOperand), rightOperand(rightOperand), Instruction(new Int()) {}
void Compare::Emit(ofstream& os) {}
void Compare::CheckReferences() {}
void Compare::PassForBranch() {}
//A branch takes two basic blocks as destination, and one result as flag.
Branch::Branch(Instruction* compareResult, BasicBlock* leftBranch, BasicBlock* rightBranch) : compareResult(compareResult), leftBranch(leftBranch), rightBranch(rightBranch), Instruction(new EmptyType()) {
leftBranch->referenceCounter++;
rightBranch->referenceCounter++;
isBranch = true;
live = true;
}
void Branch::CheckReferences() {
live = true;
compareResult->CheckReferences();
}
/*
Resolve labels for basic blocks
*/
void Branch::PassForBranch() {
char* name = new char[10];
sprintf(name, "%d", counter);
address = name;
address = "%" + address;
}
void Branch::Emit(ofstream& os) {
os << "br i1 " << compareResult->address << ", label %"
<< leftBranch->name << ", label %" << rightBranch->name << endl;
}
UncondBranch::UncondBranch(BasicBlock* destination) : destination(destination), Instruction(new EmptyType()) {
destination->referenceCounter++;
isBranch = true;
live = true;
}
/*
Resolve labels for basic blocks
*/
void UncondBranch::PassForBranch() {
char* name = new char[10];
sprintf(name, "%d", counter);
address = name;
address = "%" + address;
}
void UncondBranch::CheckReferences() {
live = true;
}
void UncondBranch::Emit(ofstream& os) {
os << "br label %" << destination->name << endl;
}
Store::Store(Instruction* address, Instruction* value) : address(address), value(value), Instruction(new EmptyType()) {
live = true;
}
//For store, first emit the value to be sotred, then the address
void Store::Emit(ofstream& os) {
os << "store ";
value->type->Emit(os);
os << " ";
os << value->address;
os << ", ";
address->type->Emit(os);
os << "* ";
os << address->address << endl;
}
void Store::CheckReferences() {
live = true;
address->CheckReferences();
value->CheckReferences();
}
void Store::PassForBranch() {}
Load::Load(Instruction* address) : address(address), Instruction(address->type) {}
void Load::CheckReferences() {
live = true;
address->CheckReferences();
}
/*
Resolve labels for basic blocks
*/
void Load::PassForBranch() {
char* name = new char[10];
sprintf(name, "%d", ++counter);
Instruction::address = name;
Instruction::address = "%" + Instruction::address;
}
//For load, emit the address to load from
void Load::Emit(ofstream& os) {
os << Instruction::address << " = load ";
address->type->Emit(os);
os << "* ";
os << address->address << endl;
}
Alloca::Alloca(Symbol* symbol) : symbol(symbol), Instruction(symbol->type) {
live = true;
}
Alloca::Alloca(Type* type) : symbol(NULL), Instruction(type) {}
void Alloca::CheckReferences() {
live = true;
}
/*
Resolve labels for basic blocks
*/
void Alloca::PassForBranch() {
if(symbol == NULL) {
char* name = new char[10];
sprintf(name, "%d", ++counter);
Instruction::address = name;
Instruction::address = "%" + Instruction::address;
} else {
address = "%" + symbol->name;
}
}
/*
For memory allocation, just emit the type of variable
*/
void Alloca::Emit(ofstream& os) {
os << address << " = alloca ";
type->Emit(os);
os << endl;
}
/*
We need to tell whether a element is extract from array or from struct
*/
GetElementPtr::GetElementPtr(Instruction* address, Instruction* index, Array* arrayType)
: address(address), index(index), arrayType(arrayType), Instruction(new EmptyType()) {
if(!index->isNum && arrayType == NULL) {
int indexInStruct = 0;
Structure* structType = (Structure*)address->type;
NameInstruction* nameInstruction = (NameInstruction*)index;
for(int i = 0; i < structType->elementTypes.size(); ++i) {
if(structType->elementTypes[i]->name == nameInstruction->symbol->name) {
type = structType->elementTypes[i]->type;
indexInStruct = i;
}
}
} else {
NumInstruction* numInstruction = (NumInstruction*)index;
if(address->type->isStruct) {
type = ((Structure*)address->type)->elementTypes[numInstruction->num]->type;
}
else {
type = (arrayType->elementType);
}
}
live = false;
}
void GetElementPtr::CheckReferences() {
live = true;
address->CheckReferences();
index->CheckReferences();
}
/*
Resolve labels for basic blocks
*/
void GetElementPtr::PassForBranch() {
char* name = new char[10];
sprintf(name, "%d", ++counter);
Instruction::address = name;
Instruction::address = "%" + Instruction::address;
}
void GetElementPtr::Emit(ofstream& os) {
os << Instruction::address << " = getelementptr inbounds ";
if(!index->isNum && arrayType == NULL) {
int indexInStruct = 0;
Structure* structType = (Structure*)address->type;
NameInstruction* nameInstruction = (NameInstruction*)index;
for(int i = 0; i < structType->elementTypes.size(); ++i) {
if(structType->elementTypes[i]->name == nameInstruction->symbol->name) {
indexInStruct = i;
}
}
structType->Emit(os);
os << "* " << address->address;
os << ", i32 0, ";
os << "i32 " << indexInStruct << endl;
} else {
address->type->Emit(os);
os << "* " << address->address;
os << ", i32 0, ";
index->type->Emit(os);
os << " ";
os << index->address << endl;
}
}
Return::Return(Instruction* address, Type* type) : address(address), Instruction(type) {
isBranch = true;
live = true;
}
void Return::CheckReferences() {
live = true;
address->CheckReferences();
}
/*
Resolve labels for basic blocks
*/
void Return::PassForBranch() {}
void Return::Emit(ofstream& os) {
os << "ret ";
address->type->Emit(os);
os << " ";
os << address->address << endl;
}
FunctionCall::FunctionCall(Instruction* function, vector<Instruction*>* arguments, Type* type)
: function(function), arguments(arguments), Instruction(type) {
live = true;
}
void FunctionCall::CheckReferences() {
live = true;
function->CheckReferences();
for(int i = 0; i < arguments->size(); ++i) {
(*arguments)[i]->CheckReferences();
}
}
/*
Resolve labels for basic blocks
*/
void FunctionCall::PassForBranch() {
char* name = new char[10];
sprintf(name, "%d", ++counter);
address = name;
address = "%" + address;
}
/*
For function call, first emit the return type and function name, then the arguments.
*/
void FunctionCall::Emit(ofstream& os) {
if(function->address == "%read") {
callRead(os);
return;
}
else if(function->address == "%write") {
callWrite(os);
return;
}
os << address << " = call ";
type->Emit(os);
os << " ";
os << function->address << "(";
if(arguments->size() > 0) {
for(int i = 0; i < arguments->size() - 1; ++i) {
(*arguments)[i]->type->Emit(os);
os << " ";
os << (*arguments)[arguments->size() - 1 - i]->address;
os << ", ";
}
arguments->back()->type->Emit(os);
os << " " << arguments->front()->address;
}
os << ")";
os << endl;
}
//Treat I/O as special cases, use printf and scanf
void FunctionCall::callRead(ofstream& os) {
os << address << " = call ";
os << "i32 (i8*, ...)* @scanf(i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0), i32* ";
os << (*arguments)[0]->address << ")" << endl;
}
void FunctionCall::callWrite(ofstream& os) {
os << address << " = call ";
os << "i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.str1, i32 0, i32 0), i32 ";
os << (*arguments)[0]->address << ")" << endl;
}
//pseudo instruction, just indicate a number value
NumInstruction::NumInstruction(int num) : num(num), Instruction(new Int()) {
char* name = new char[10];
sprintf(name, "%d", num);
Instruction::address = name;
isNum = true;
live = false;
}
void NumInstruction::CheckReferences() {
live = true;
}
void NumInstruction::PassForBranch() {}
void NumInstruction::Emit(ofstream& os) {
//os << num;
}
//pseudo instruction, just indicate a variable
NameInstruction::NameInstruction(Symbol* symbol) : symbol(symbol), Instruction(symbol->type) {
address = symbol->name;
if(symbol->isGlobal) {
address = "@" + address;
}
else {
address = "%" + address;
}
isName = true;
live = false;
}
void NameInstruction::CheckReferences() {
live = true;
}
void NameInstruction::PassForBranch() {}
void NameInstruction::Emit(ofstream& os) {}
ZeroExtension::ZeroExtension(Instruction* address) : address(address), Instruction(new Int()) {
live = false;
}
void ZeroExtension::CheckReferences() {
live = true;
address->CheckReferences();
}
/*
Resolve labels for basic blocks
*/
void ZeroExtension::PassForBranch() {
char* name = new char[10];
sprintf(name, "%d", ++counter);
Instruction::address = name;
Instruction::address = "%" + Instruction::address;
}
void ZeroExtension::Emit(ofstream& os) {
os << Instruction::address << " = zext i1 ";
os << address->address;
os << " to i32" << endl;
}