-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirGenerator.h
66 lines (57 loc) · 1.43 KB
/
irGenerator.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
#pragma once
#include "astNode.h"
#include <string>
#include <list>
#include <map>
enum IR_OPERATION {
IR_ADD,
IR_SUBTRACT,
IR_ASSIGN,
IR_NEWLABEL,
IR_GOTO,
IR_IFTRUEGOTO,
IR_IFFALSEGOTO,
IR_BEGINFUNC,
IR_ENDFUNC,
IR_CALL,
IR_PUSHPARAM,
IR_POPPARAMS,
IR_DECLARATION,
IR_RETURN,
// NES-Specific IR
IR_NES_WAITFORFRAME,
IR_NES_SETSPRITEX,
IR_NES_SETSPRITEY,
};
static std::map<std::string, IR_OPERATION> opStringToEnum = {
{"+", IR_ADD},
{"-", IR_SUBTRACT},
};
struct TAC {
IR_OPERATION op;
std::string first;
std::string second;
std::string third;
};
class IRGenerator {
public:
IRGenerator(ASTNode * annotatedRoot);
std::list<TAC> generate();
private:
ASTNode * root;
int registerCount;
std::list<TAC> intermediateCode;
//std::map<std::string, int> labelCounts;
int labelCount;
// Generate Three-Address-Code representation for the node and its children
void genTAC(ASTNode * node);
// Generate TAC for specific types of nodes
std::string genTACExpression(ASTNode * expr);
void genTACStatement(ASTNode * statement);
void genTACFunction(ASTNode * func);
void genTACDeclaration(ASTNode * declaration);
// Generate new temporary address
std::string genAddress();
// Generate new label
std::string genLabel();
};