-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTac.hpp
139 lines (122 loc) · 4.1 KB
/
Tac.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
#ifndef TAC_HPP
#define TAC_HPP
#include "BytecodeMethodBlock.hpp"
#include <iostream>
#include <string>
#include <variant>
using Operand = std::variant<std::string, int>;
class Tac {
protected:
std::string result;
Operand lhsOp;
std::string op;
Operand rhsOp;
std::string lhs, rhs;
private:
static std::string to_string(const Operand &op) {
if (const auto *ptr = std::get_if<int>(&op)) {
return std::to_string(*ptr);
}
return std::get<std::string>(op);
}
public:
virtual void print(std::ostream &os) const;
virtual void
generateBytecode([[maybe_unused]] BytecodeMethodBlock &block){};
Tac(const std::string &result_) : result{result_} {}
Tac(const std::string &result_, const Operand &lhs_, const std::string &op_,
const Operand &rhs_)
: result(result_), lhsOp(lhs_), op(op_), rhsOp(rhs_) {
lhs = to_string(lhsOp);
rhs = to_string(rhsOp);
}
Tac(const std::string &result_, const Operand &rhs_)
: result{result_}, rhsOp{rhs_} {
rhs = to_string(rhsOp);
}
Tac(const Operand &rhs_) : rhsOp{rhs_} { rhs = to_string(rhsOp); }
virtual ~Tac() = default;
};
class ArrayCopyTac : public Tac {
public:
ArrayCopyTac(const std::string &result_, const Operand &index_,
const Operand &z_)
: Tac(result_, index_, ":=", z_){};
void print(std::ostream &os) const override;
};
class ArrayAccessTac : public Tac {
public:
ArrayAccessTac(const std::string &result_, const Operand &y_,
const Operand &z_)
: Tac(result_, y_, "", z_){};
void print(std::ostream &os) const override;
};
class ArrayLengthTac : public Tac {
public:
ArrayLengthTac(const std::string &result, const Operand &y_)
: Tac(result, y_){};
void print(std::ostream &os) const override;
};
class NewTac : public Tac {
public:
NewTac(const std::string &result, const Operand &y_) : Tac(result, y_){};
void print(std::ostream &os) const override;
};
class NewArrayTac : public Tac {
public:
NewArrayTac(const std::string &result, const Operand &length_)
: Tac(result, length_){};
void print(std::ostream &os) const override;
};
class NotTac : public Tac {
public:
NotTac(const std::string &result_, const Operand &z_) : Tac(result_, z_){};
void generateBytecode(BytecodeMethodBlock &block) override;
void print(std::ostream &os) const override;
};
class CopyTac : public Tac {
public:
CopyTac(const Operand &y_, const std::string &result_) : Tac(result_, y_){};
void print(std::ostream &os) const override;
void generateBytecode(BytecodeMethodBlock &block) override;
};
class CondJumpTac : public Tac {
public:
CondJumpTac(const Operand &label, const Operand &cond)
: Tac("", cond, "", label){};
void print(std::ostream &os) const override;
void generateBytecode(BytecodeMethodBlock &block) override;
};
class MethodCallTac : public Tac {
public:
MethodCallTac(const std::string &result, const std::string &objectName,
const Operand &methodName, const Operand &argCount)
: Tac(result, methodName, objectName, argCount){};
void print(std::ostream &os) const override;
void generateBytecode(BytecodeMethodBlock &block) override;
};
class JumpTac : public Tac {
public:
JumpTac(const std::string &_label) : Tac(_label){};
void print(std::ostream &os) const override;
void generateBytecode(BytecodeMethodBlock &block) override;
};
class ParamTac : public Tac {
public:
ParamTac(const Operand ¶m) : Tac(param){};
void print(std::ostream &os) const override;
void generateBytecode(BytecodeMethodBlock &block) override;
};
class ReturnTac : public Tac {
public:
ReturnTac(const Operand &name) : Tac(name){};
void print(std::ostream &os) const override;
void generateBytecode(BytecodeMethodBlock &block) override;
};
class PrintTac : public Tac {
public:
PrintTac(const Operand &value) : Tac(value){};
void print(std::ostream &os) const override;
void generateBytecode(BytecodeMethodBlock &block) override;
};
#endif