-
Notifications
You must be signed in to change notification settings - Fork 0
/
three_expr.cpp
121 lines (99 loc) · 3.35 KB
/
three_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
#include "tree.h"
#include <stdio.h>
#include <iostream>
using namespace std;
class EXPR:public NODE{
};
class BINOPNODE : public EXPR
{ public:
BINOPNODE(optypes O, AST L, AST R) { op = O; left = L; right = R; }
virtual void load(int R);
optypes op;
EXPR* left, right;
void operation(optypes O, int R);
void loadreg(int R){dlog("LOAD REG IWTH LOAD "+to_string(R),0); load(R); }
};
void BINOPNODE::operation(optypes op, int R)
{ switch (op)
{
case Store: printf("bin STO "); break;
case Load: printf("bin LDA"); break;
case Plus: printf("bin ADD"); break;
case Minus: printf("bin SUB"); break;
case Times: printf("bin MUL"); break;
case Slash: printf("bin DVD"); break;
}
//if(op != Store) cout<<" R"<<R<<",R"<<R+1<<endl;
//if(op == Store) cout<<" "<<*name<<",R"<<R<<endl;
cout<<" " <<*_to_reg_(R)<<","<<*_to_reg_(R+1)<<endl;
//printf(" R%d,R%d\n", R, R + 1);
}
void BINOPNODE::load(int R)
{ if (!left || !right) return;
left->load(R); right->loadreg(R+1); right->operation(op, R);
delete left; delete right;
}
AST BinOpNode(optypes op, AST left, AST right)
{ if (left && right && left->defined && right->defined)
{ // constant folding
switch (op)
{ case Plus: left->value += right->value; break;
case Minus: left->value -= right->value; break;
case Times: left->value *= right->value; break;
case Slash: left->value /= right->value; break;
}
delete right; return left;
}
return new BINOPNODE(op, left, right);
}
class VARNODE : public NODE
{ public:
VARNODE(string * C) { name = C; type = 1;}
virtual void load(int R) { operation(Load, R); }
//protected:
string * name;
virtual void operation(optypes O, int R);
};
void VARNODE::operation(optypes op, int R)
{
dlog("VARNODE OPERATION "+to_string(R),0)
// cout<<"VARNODE OPERATION "<<R<<endl;
switch (op)
{
case Store: printf("MOV"); break;
case Load: printf("MOV"); break;
case Plus: printf("var ADD"); break;
case Minus: printf("SUB"); break;
case Times: printf("MUL"); break;
case Slash: printf("DVD"); break;
}
if(op != Store) cout<<" "<<*_to_reg_(R)<<",[rbp-"<<*name<<"]"<<endl;
if(op == Store) cout<<" [rbp-"<<*name<<"],"<<*_to_reg_(R)<<endl;
//printf(" R%d,%c\n", R, name);
}
AST VarNode(string * name)
{ return new VARNODE(name); }
class CONSTNODE : public NODE
{ public:
CONSTNODE(int V) { value = V; defined = 1; type = 1;}
virtual void load(int R) { operation(Load, R); }
//protected:
virtual void operation(optypes O, int R);
};
void CONSTNODE::operation(optypes op, int R)
{ switch (op)
{
case Store: printf("MOV"); break;
case Load: printf("MOV"); break;
case Plus: printf("const ADI"); break;
case Minus: printf("SBI"); break;
case Times: printf("MLI"); break;
case Slash: printf("DVI"); break;
}
if(op != Store) cout<<" "<<*_to_reg_(R)<<","<<value<<endl;
if(op == Store) cout<<" "<<value<<","<<*_to_reg_(R)<<endl;
//cout <<*_to_reg_(R)<<","*_to_reg_(R+1)<<endl;
//printf(" R%d,%d\n", R, value);
}
AST ConstNode(int value)
{ return new CONSTNODE(value); }