This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRTypes.h
596 lines (476 loc) · 16.8 KB
/
IRTypes.h
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//
// Created by 廖治平 on 5/18/21.
//
#ifndef SYSYBACKEND_IRTYPES_H
#define SYSYBACKEND_IRTYPES_H
#include <map>
#include <string>
#include <utility>
#include <vector>
#include <memory>
#include <utility>
#include <algorithm>
#include "IRUtilities.h"
namespace IntermediateRepresentation {
enum IRDataType {
i32, // types of integers
str, t_void, label = str
};
typedef IRDataType IRType;
enum IRStmtType {
BR, // branching
/**
* br label label
*
* // conditional branching
* br <else> %condition, <if true>, <if false>
* */
ADD, // addition
/**
* add i32 %dest, i32 %opr1, i32 %opr2
* %dest = %opr1 + %opr2;
* */
MUL, // multiplication
/**
* mul i32 %dest, i32 %opr1, i32 %opr2
* %dest = %opr1 * %opr2;
* */
DIV, // division
/**
* div i32 %dest, i32 %opr1, i32 %opr2
* %dest = %opr1 / %opr2;
* */
MOD, // residue
/**
* mov i32 %dest, i32 %opr1, i32 %opr2
* %dest = %opr1 % %opr2;
* */
SUB, // subtraction
/**
* sub i32 %dest, i32 %opr1, i32 %opr2
* %dest = %opr1 - %opr2;
* */
CALL, // function call
/**
* call (return_type) <return_var>, <func>, par1, par2, par3, ...
*
* If 'func' returns void, you must:
* 1. set call type to t_void
* 2. set return_var to void, t_void
* */
RETURN, // return from a function
MOV, // move or assign. %source is not allowed in SSA form
/**
* mov i32 %dest, i32 %source
* mov i32 %dest, i32 <imm>
* */
LABEL, // label
LOAD, // load to variable
/**
* load i32 %dest, *i32 %base, i32 %off
* %off: offset bytes
* */
STORE, // save to allocated space
/**
* store i32 %source, *i32 %base, i32 %off
* %off: offset bytes
* */
ALLOCA, // allocating on stack
/**
* alloca *i32 %dest, i32 %size
* %size: size bytes
* */
CMP_EQ, // comparison (equal)
CMP_NE, // comparison (notequal)
CMP_SGE, // comparison (signed, greatereuqal)
CMP_SLE, // comparison (signed, lessequal)
CMP_SGT, // comparison (signed, greater)
CMP_SLT, // comparison (signed, less)
/*
* cmp_xx %dest, %opr1, %opr2
* */
GLB_CONST, // global constant
GLB_VAR, // global variable
/**
* (glb_var i32), %dest, i32 <val>
* */
GLB_ARR,
/**/
LSH, RSH, // bits shifting
OR, AND, XOR, NOT, // boolean operators
PHI, // phi function
/**
* 5 arguments:
* phi <dest_var> [ <varA>, <blockA> ], [ <varB>, <blockB> ]
*
* where block{ A | B } are **labels** (blocks);
* '[', ']' are presented here as semantic separators, and they do **NOT** appear in IR.
* Arguments inside a bracket pair denote a variable and the branch jumped from.
* */
PARAM,
/**
* load function parameters
*
* param %1, #pos
* */
STK_LOAD,
STK_STR
/**
* These are low-level IRs. Frontend should not use them.
* STK_LOAD i32 %dest, %off
* STK_STR i32 %src, %off
* */
};
enum IROpType {
ImmVal, Var, Null
};
class NamingUtil {
public:
static size_t counter;
static std::string nextVarName() {
return std::string("VAR_") + std::to_string(counter++);
}
static std::string nextLblName() {
return std::string("LBL_") + std::to_string(counter++);
}
static std::string nextHeapLblName() {
return std::string("LBL_HP_") + std::to_string(counter++);
}
};
struct IRArray {
std::string arrayName;
size_t arrSize; // elements
int defaultValue;
std::map<uint64_t , int> data;
std::string toString() const {
std::string ans = "arrayName: " + arrayName + ", arrSize:" + std::to_string(arrSize) + ", defaultValue: " + std::to_string(defaultValue) + ", data: [ ";
for (auto& pair : data)
ans += "(pos:" + std::to_string(pair.first) + ", value: " + std::to_string(pair.second) + "), ";
return ans;
}
IRArray() = default;
IRArray(std::string arrayName, size_t arrSize);
IRArray(std::string arrayName, size_t arrSize, int defaultValue);
void addData(size_t position, int value);
const std::map<uint64_t, int> &getData() const;
size_t getArrSize() const;
const std::string &getArrayName() const;
};
class IROperand {
private:
IROpType irOpType;
IRDataType irValType;
int32_t Value;
std::string strValue;
std::string varName;
bool isPointer = false;
public:
size_t operator() (const IROperand& operand) const {
return std::hash<std::string> { }(operand.toString());
}
std::string toString() const {
std::string opStr = Utility::IROpTypeToStr[irOpType], valTypeStr = Utility::IRDataTypeToStr[irValType];
switch (irOpType) {
case ImmVal:
return opStr + " " + valTypeStr + " " + (irValType == i32 ? std::to_string(Value) : strValue);
case Var:
return opStr + " " + valTypeStr + " " + varName;
case Null:
return opStr + " " + valTypeStr;
default:
throw std::runtime_error("Invalid IR: unexpected IROperand type: " + std::to_string(irOpType));
}
}
bool operator<(const IROperand &rhs) const {
return toString() < rhs.toString();
}
bool operator>(const IROperand &rhs) const {
return rhs < *this;
}
bool operator<=(const IROperand &rhs) const {
return !(rhs < *this);
}
bool operator>=(const IROperand &rhs) const {
return !(*this < rhs);
}
bool operator==(const IROperand& b) const {
return toString() == b.toString();
}
bool operator!=(const IROperand& b) const {
return toString() != b.toString();
}
const std::string &getStrValue() const {
return strValue;
}
void setStrValue(const std::string &strValue) {
IROperand::strValue = strValue;
}
IROpType getIrOpType() const {
return irOpType;
}
[[deprecated]]
IRDataType getIrType() const {
return irValType;
}
IRDataType getIrDataType() const {
return irValType;
}
void setIrType(IRDataType irType) {
IROperand::irValType = irType;
}
int32_t getValue() const {
return Value;
}
void setValue(int32_t value) {
Value = value;
}
const std::string &getVarName() const {
return varName;
}
void setVarName(const std::string &varName) {
IROperand::varName = varName;
}
bool getIsPointer() const {
return isPointer;
}
// set pointer
void setIsPointer(bool isPointer) {
IROperand::isPointer = isPointer;
}
// Immediate Number
IROperand(IRDataType irValType, int32_t value) : irValType(irValType), Value(value), irOpType(ImmVal), varName("") { }
// String
IROperand(std::string strValue) : strValue(std::move(strValue)), irValType(str), irOpType(ImmVal), varName("") { }
// Variable (specify whether it's a pointer)
IROperand(IRDataType irValType, std::string varName,
bool isPointer) : irValType(irValType), Value(0), varName(std::move(varName)), isPointer(isPointer), irOpType(Var) {
// if it's anonymous, a generated name will be assigned.
if (this->varName.empty())
this->varName = std::move(NamingUtil::nextVarName());
}
// Variable (not a pointer)
IROperand(IRDataType irValType, std::string varName)
: irValType(irValType), Value(0), varName(std::move(varName)), irOpType(Var) {
// if it's anonymous, a generated name will be assigned.
if (this->varName.empty())
this->varName = std::move(NamingUtil::nextVarName());
}
// Empty constructor
IROperand() : irOpType(Null), Value(0), irValType(t_void) { }
};
class Statement {
IRStmtType stmtType;
IRDataType dataType;
// Ops[0]: Destination
// Ops[1], [2], ..., [n]: operand 1, 2, 3, ...
// size >= 1
std::vector<IROperand> Ops;
std::string label_name;
public:
std::string toString() const {
std::string ans = Utility::IRStmtTypeToStr[stmtType] + " " + Utility::IRDataTypeToStr[dataType];
for (auto& op : Ops)
ans += " [" + std::string(op.getIsPointer() ? "*" : "") + op.toString() + "]";
return ans;
}
auto& atOperands(const size_t pos) {
return Ops[pos];
}
auto& operator[] (const size_t pos) {
return Ops[pos];
}
Statement() = default;
// Label
explicit Statement(std::string labelName) : stmtType(LABEL), dataType(t_void), label_name(std::move(labelName)) { }
// Normal statement:
// 1. Statement type;
// 2. operands
Statement(IRStmtType stmtType, IRDataType irDataType, std::vector<IROperand> ops) : stmtType(stmtType), dataType(irDataType), Ops(std::move(ops)) { }
// Variadic-style statement constructor:
// 1. Statement Type
// 2. Statement data type
// 3. Destination variable
// 4, 5, 6, ..., N: arguments
template<typename ...Args>
Statement(IRStmtType stmtType1, IRDataType irDataType, Args&&... args) : stmtType(stmtType1), dataType(irDataType) {
(Ops.push_back(std::forward<Args>(args)), ...);
}
IRDataType getDataType() const {
return dataType;
}
void setDataType(IRDataType dataType) {
Statement::dataType = dataType;
}
IRStmtType getStmtType() const {
return stmtType;
}
void setStmtType(IRStmtType stmtType) {
Statement::stmtType = stmtType;
}
const std::vector<IROperand> &getOps() const {
return Ops;
}
std::vector<IROperand>& getRefOps() {
return Ops;
}
void setOps(const std::vector<IROperand> &ops) {
Ops = ops;
}
const std::string &getLabelName() const {
return label_name;
}
void setLabelName(const std::string &labelName) {
label_name = labelName;
}
};
typedef std::vector<Statement> IRSequence;
class Function {
std::string funName;
IRDataType returnType;
std::vector<Statement> statements;
std::vector<IROperand> parameters;
public:
std::string toString() const {
std::string ans;
ans += "FuncName: " + funName + "\n";
ans += "ReturnType: " + Utility::IRDataTypeToStr[returnType] + "\n";
ans += "Parameters: {\n" + [&] () {
std::string ret = "\t";
for (auto& param : parameters)
ret += "[" + std::string(param.getIsPointer() ? "*" : "") + param.toString() + "], ";
return ret;
} () + "\n}\n";
ans += "Statements: {\n" + [&] () {
std::string ret;
for (auto& stmt : statements)
ret += "\t[" + stmt.toString() + "], \n";
return ret;
} () + "}";
return ans;
}
auto& operator[] (const size_t pos) {
return statements[pos];
}
auto& atStatements(const size_t pos) {
return statements[pos];
}
auto& atParameters(const size_t pos) {
return parameters[pos];
}
Function(std::string funName) : funName(std::move(funName)), returnType(t_void) { }
Function(std::string funName, IRDataType returnType) : funName(std::move(funName)), returnType(returnType) { }
Function() = default;
IRDataType getReturnType() const {
return returnType;
}
void setReturnType(IRDataType returnType) {
Function::returnType = returnType;
}
void insertStatement(const Statement& statement) {
statements.push_back(statement);
}
template<class ...Args>
void insert(Args&& ... args) {
(statements.push_back(std::forward<Args>(args)), ...);
}
friend Function& operator<< (Function& func, const Statement& statement) {
func.insertStatement(statement);
return func;
}
template<class ...Args>
void insertParam(Args&& ...args) {
(parameters.template emplace_back(std::forward<Args>(args)), ...);
}
const std::string &getFunName() const {
return funName;
}
void setFunName(const std::string &funName) {
Function::funName = funName;
}
const std::vector<Statement> &getStatements() const {
return statements;
}
auto &getRefStatements() {
return statements;
}
void setStatements(const std::vector<Statement> &statements) {
Function::statements = statements;
}
const std::vector<IROperand> &getParameters() const {
return parameters;
}
void setParameters(const std::vector<IROperand> ¶meters) {
Function::parameters = parameters;
}
};
class IRProgram {
std::vector<Statement> global;
std::vector<Function> functions;
std::vector<IRArray> globalArrays;
public:
// 1. functions, 2. global variables
IRProgram(std::vector<Statement> global, std::vector<Function> functions) : global(std::move(global)), functions(std::move(functions)) { }
// default constructor
IRProgram() = default;
std::string toString() const {
std::string ans = "IRProgram: \n";
ans += "Global: {\n" + [&] () {
std::string ret;
for (auto& stmt : global)
ret += "\t[" + stmt.toString() + "],\n";
return ret;
} () + "}\n";
ans += "IRArray: {\n" + [&] () {
std::string ret;
for (auto& array : globalArrays)
ret += "\t[" + array.toString() + "],\n";
return ret;
} () + "}\n";
ans += "Functions: \n" + [&] () {
std::string ret;
for (auto& func : functions)
ret += func.toString() + "\n";
return ret;
} () + "\n";
return ans;
}
const std::vector<IRArray> &getGlobalArrays() const {
return globalArrays;
}
void setGlobalArrays(const std::vector<IRArray> &globalArrays) {
IRProgram::globalArrays = globalArrays;
}
auto& atGlobal(const size_t pos) {
return global[pos];
}
auto& atFunctions(const size_t pos) {
return functions[pos];
}
// Insert functions
// eg: insert(func1, func2, func3, ..., funcN)
template<class ...Args>
void insert(Args&&... args) {
(functions.emplace_back(args), ...);
}
// Insert globals
// eg: insert(global1, global2, global3, ..., paramN)
template<class ...Args>
void insert(const Statement& glb, Args... args) {
global.push_back(glb);
(global.push_back(std::forward<Args>(args)), ...);
}
const std::vector<Statement> &getGlobal() const {
return global;
}
void setGlobal(const std::vector<Statement> &global) {
IRProgram::global = global;
}
const std::vector<Function> &getFunctions() const {
return functions;
}
void setFunctions(const std::vector<Function> &functions) {
IRProgram::functions = functions;
}
};
}
#endif //SYSYBACKEND_IRTYPES_H