-
Notifications
You must be signed in to change notification settings - Fork 3
/
AST.cpp
179 lines (152 loc) · 4.47 KB
/
AST.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "AST.h"
using namespace std;
Node::Node(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = data2;
}
LITERAL::LITERAL(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
FCALL::FCALL(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
RET::RET(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
IMP::IMP(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
FDECL::FDECL(string type, string value, vector<AbstractNode> data,
vector<AbstractNode> data2) {
_type = type;
_value = value;
_data = data;
body = data2;
}
ASM::ASM(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
ASSIGN::ASSIGN(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
REFER::REFER(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
DEREF::DEREF(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
EXTERNAL::EXTERNAL(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
// body = {};
}
IE::IE(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
}
OPP::OPP(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
}
PNTR::PNTR(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
}
LCLASS::LCLASS(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
}
LCLDEF::LCLDEF(string type, string value, vector<AbstractNode> data) {
_type = type;
_value = value;
_data = data;
}
void x86::add(string addit) { out.append(addit); }
void x86::sout() { bss.append(out.append(data)); }
string x86::constant(string ctype, string cvalue) {
data += "RPP_CONSTANT_" + to_string(constants) + " db " + cvalue + ", 0\n";
constants++;
return "RPP_CONSTANT_" + to_string(constants - 1);
}
string x86::variable(string vtype, string vname, string vvalue, bool variate, bool islcl) {
// a->constant(_data[i]._LIT.ctype, _data[i]._LIT._value)
if ((!(find(vars.begin(), vars.end(), vname) != vars.end())) && islcl == false) {
vars.push_back(vname);
bss.append(vname + ": resb 4\n");
}
// return "mov eax, " + vvalue + "\nmov [" + vname + "], eax" + "\n";
if(vtype == "STRING") {
return "mov eax, " + constant(vtype, vvalue) + "\nmov [" + vname + "], eax" + "\n";
// }else if(vtype == "WORD") {
// return "mov eax, [" + vvalue + "]\nmov [" + vname + "], eax" + "\n";
}else {
return "mov eax, " + vvalue + "\nmov [" + vname + "], eax" + "\n";
}
}
string x86::point(string vtype, string vname, string vvalue) {
// a->constant(_data[i]._LIT.ctype, _data[i]._LIT._value)
if (!(find(vars.begin(), vars.end(), vname) != vars.end())) {
vars.push_back(vname);
bss.append(vname + ": resb 4\n");
}
// return "mov eax, " + vvalue + "\nmov [" + vname + "], eax" + "\n";
if(vtype == "STRING") {
return "lea eax, " + constant(vtype, vvalue) + "\nmov [" + vname + "], eax" + "\n";
}else {
return "lea eax, " + vvalue + "\nmov [" + vname + "], eax" + "\n";
}
}
string x86::deref(string vtype, string vname, string vvalue) {
// a->constant(_data[i]._LIT.ctype, _data[i]._LIT._value)
if (!(find(vars.begin(), vars.end(), vname) != vars.end())) {
vars.push_back(vname);
bss.append(vname + ": resb 4\n");
}
// return "mov eax, " + vvalue + "\nmov [" + vname + "], eax" + "\n";
// return "mov eax, [" + constant(vtype, vvalue) + "]\nmov [" + vname + "], eax" + "\n";
return "mov eax, [" + vvalue + "]\n mov edx, [eax] \n mov [" + vname + "], edx" + "\n";
}
/*
-- var& a = v;
lea eax, v -- pointer into eax
mov [a], eax -- pointer into a
-- var* b = a;
mov eax, [a] -- pointer into eax
mov edx, [eax] -- value into edx
mov [b], edx -- deref into b
*/