-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpr.cpp
161 lines (112 loc) · 3.3 KB
/
Expr.cpp
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
#include "Expr.hpp"
#include "asm.hpp"
#include "VarDecl.hpp"
NumExpr::NumExpr(i32_t val) : _value(val) {
}
void NumExpr::eval(std::ostream& out) const {
gas::mov(out, _value, E_AX);
}
NegExpr::NegExpr(const Expr* exp) : _expr(exp) {
}
void NegExpr::eval(std::ostream& out) const {
i32_t val;
if (_expr->cte(&val))
gas::mov(out, val * -1, E_AX);
else {
_expr->eval(out);
gas::neg(out, E_AX);
}
}
StringExpr::StringExpr(const std::string& label) : _label(label) {
}
void StringExpr::eval(std::ostream& out) const {
gas::mov(out, _label, E_AX);
}
VarExpr::VarExpr(const VarDecl* var) : _var(var) {
}
void VarExpr::eval(std::ostream& out) const {
gas::mov(out, Offset(_var->getBaseOffset(), P_BASE), E_AX);
}
PtrExpr::PtrExpr(const VarDecl* var) : VarExpr(var) {
}
void PtrExpr::eval(std::ostream& out) const {
if (_var)
gas::lea(out, Offset(_var->getBaseOffset(), P_STACK), E_AX);
else
gas::mov(out, 0, E_AX);
}
DerefExpr::DerefExpr(const VarDecl* var) : VarExpr(var) {
}
void DerefExpr::eval(std::ostream& out) const {
gas::mov(out, Offset(_var->getBaseOffset(), P_STACK), E_AX);
gas::mov(out, Offset(0, E_AX), E_AX);
}
BinaryExpr::BinaryExpr(const Expr* left, const Expr* right) : lhs(left), rhs(right) {
}
AddExpr::AddExpr(const Expr* left, const Expr* right) : BinaryExpr(left, right) {
}
void AddExpr::eval(std::ostream& out) const {
out << "# Begin AddExpr " << std::endl;
i32_t val;
if (this->lhs->cte(&val))
gas::push(out, val);
else {
this->lhs->eval(out);
gas::push(out, E_AX);
}
this->rhs->eval(out);
gas::add(out, Offset(0, P_STACK), E_AX);
gas::add(out, 4, P_STACK);
out << "# End AddExpr " << std::endl;
}
SubExpr::SubExpr(const Expr* left, const Expr* right) : BinaryExpr(left, right) {
}
void SubExpr::eval(std::ostream& out) const {
out << "# Begin SubExpr " << std::endl;
i32_t val;
if (this->lhs->cte(&val))
gas::push(out, val);
else {
this->lhs->eval(out);
gas::push(out, E_AX);
}
this->rhs->eval(out);
gas::sub(out, Offset(0, P_STACK), E_AX);
gas::add(out, 4, P_STACK);
out << "# End SubExpr " << std::endl;
}
MulExpr::MulExpr(const Expr* left, const Expr* right) : BinaryExpr(left, right) {
}
void MulExpr::eval(std::ostream& out) const {
out << "# Begin MulExpr " << std::endl;
i32_t val;
if (this->lhs->cte(&val))
gas::push(out, val);
else {
this->lhs->eval(out);
gas::push(out, E_AX);
}
this->rhs->eval(out);
gas::imul(out, Offset(0, P_STACK), E_AX);
gas::add(out, 4, P_STACK);
out << "# End MulExpr " << std::endl;
}
DivExpr::DivExpr(const Expr* left, const Expr* right) : BinaryExpr(left, right) {
}
void DivExpr::eval(std::ostream& out) const {
out << "# Begin DivExpr " << std::endl;
this->lhs->eval(out);
gas::mov(out, E_AX, E_BX);
this->rhs->eval(out);
gas::mov(out, 0, E_DX);
gas::idiv(out, E_BX);
out << "# End DivExpr " << std::endl;
}
ModExpr::ModExpr(const Expr* left, const Expr* right) : DivExpr(left, right) {
}
void ModExpr::eval(std::ostream& out) const {
out << "# Begin ModExpr " << std::endl;
DivExpr::eval(out);
gas::mov(out, E_DX, E_AX);
out << "# End ModExpr " << std::endl;
}