-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.cpp
More file actions
341 lines (275 loc) · 7.89 KB
/
node.cpp
File metadata and controls
341 lines (275 loc) · 7.89 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <cstdio>
#include <cstdarg>
#include <cassert>
#include "util.h"
/*
#include "symbol.h"
*/
#include "node.h"
#define DEBUG_PRINT(args...)
//#define DEBUG_PRINT(args...) printf(args)
////////////////////////////////////////////////////////////////////////
// C++ Node data type implementation
////////////////////////////////////////////////////////////////////////
Node::Node(int tag)
: m_tag(tag)
, m_source_info { .filename = "<unknown file>", .line = -1, .col = -1 }
, m_ival(0L)
, m_symtab(nullptr)
, m_index(0)
, m_operand(nullptr)
, m_inverted(false)
{
}
Node::~Node() {
}
int Node::get_tag() const {
return m_tag;
}
int Node::get_num_kids() const {
return int(m_kids.size());
}
void Node::add_kid(Node *kid) {
m_kids.push_back(kid);
// If the parent node doesn't yet have source info set,
// and the child has source info, copy the child's source info
if (m_source_info.line < 0 && kid->m_source_info.line > 0) {
m_source_info = kid->m_source_info;
}
}
void Node::prepend_kid(Node *kid) {
m_kids.insert(m_kids.begin(), kid);
// Copy child's source info, if it has valid source info
if (kid->m_source_info.line > 0) {
m_source_info = kid->m_source_info;
}
}
Node *Node::get_kid(int index) {
assert(index >= 0);
return m_kids.at(unsigned(index));
}
void Node::set_str(const std::string &s) {
m_strval = s;
}
const std::string &Node::get_str() const {
return m_strval;
}
SourceInfo Node::get_source_info() const {
return m_source_info;
}
void Node::set_source_info(const SourceInfo &source_info) {
m_source_info = source_info;
}
long Node::get_ival() const {
return m_ival;
}
void Node::set_ival(long ival) {
m_ival = ival;
}
void Node::set_symtab(SymbolTable *symtab) {
m_symtab = symtab;
}
SymbolTable *Node::get_symtab() {
return m_symtab;
}
void Node::set_index(unsigned index) {
m_index = index;
}
unsigned Node::get_index() const {
return m_index;
}
void Node::set_symbol(SymbolTable *symtab, unsigned index) {
m_symtab = symtab;
m_index = index;
}
void Node::set_type(Type *type) {
m_type = type;
}
Type *Node::get_type() {
// If the Node is directly annotated with a Type,
// return it
if (m_type) {
return m_type;
}
/*// If there is a symbol table entry, then return its type
Symbol *sym = node_get_symbol(this);
if (sym) {
return sym->get_type();
}*/
// no type is associated with this node
return nullptr;
}
void Node::set_operand(Operand* operand) {
m_operand = operand;
}
Operand* Node::get_operand() {
return m_operand;
}
void Node::set_inverted(bool inverted) {
m_inverted = inverted;
}
bool Node::is_inverted()
{
return m_inverted;
}
void Node::set_vregs_used(int vregs_used) {
m_vregs_used = vregs_used;
}
int Node::get_num_vregs_used() {
return m_vregs_used;
}
////////////////////////////////////////////////////////////////////////
// C API for working with Nodes
////////////////////////////////////////////////////////////////////////
struct Node *node_alloc(int tag) {
return new Node(tag);
}
struct Node *node_alloc_str_copy(int tag, const char *str_to_copy) {
Node *n = new Node(tag);
n->set_str(str_to_copy);
return n;
}
struct Node *node_alloc_str_adopt(int tag, char *str_to_adopt) {
Node *n = new Node(tag);
n->set_str(str_to_adopt);
free(str_to_adopt);
return n;
}
struct Node *node_alloc_ival(int tag, long ival) {
Node *n = new Node(tag);
n->set_ival(ival);
return n;
}
struct Node *node_build0(int tag) {
DEBUG_PRINT("Node0: %d\n", tag);
return node_buildn(tag, NULL);
}
struct Node *node_build1(int tag, struct Node *child1) {
DEBUG_PRINT("Node1: %d\n", tag);
return node_buildn(tag, child1, NULL);
}
struct Node *node_build2(int tag, struct Node *child1, struct Node *child2) {
DEBUG_PRINT("Node2: %d\n", tag);
return node_buildn(tag, child1, child2, NULL);
}
struct Node *node_build3(int tag, struct Node *child1, struct Node *child2, struct Node *child3) {
DEBUG_PRINT("Node3: %d\n", tag);
return node_buildn(tag, child1, child2, child3, NULL);
}
struct Node *node_build4(int tag,
struct Node *child1, struct Node *child2, struct Node *child3,
struct Node *child4) {
DEBUG_PRINT("Node4: %d\n", tag);
return node_buildn(tag, child1, child2, child3, child4, NULL);
}
struct Node *node_build5(int tag,
struct Node *child1, struct Node *child2, struct Node *child3,
struct Node *child4, struct Node *child5) {
DEBUG_PRINT("Node5: %d\n", tag);
return node_buildn(tag, child1, child2, child3, child4, child5, NULL);
}
struct Node *node_build6(int tag,
struct Node *child1, struct Node *child2, struct Node *child3,
struct Node *child4, struct Node *child5, struct Node *child6) {
DEBUG_PRINT("Node6: %d\n", tag);
return node_buildn(tag, child1, child2, child3, child4, child5, child6, NULL);
}
struct Node *node_build7(int tag,
struct Node *child1, struct Node *child2, struct Node *child3,
struct Node *child4, struct Node *child5, struct Node *child6,
struct Node *child7) {
DEBUG_PRINT("Node7: %d\n", tag);
return node_buildn(tag, child1, child2, child3, child4, child5, child6, child7, NULL);
}
struct Node *node_build8(int tag,
struct Node *child1, struct Node *child2, struct Node *child3,
struct Node *child4, struct Node *child5, struct Node *child6,
struct Node *child7, struct Node *child8) {
DEBUG_PRINT("Node8: %d\n", tag);
return node_buildn(tag, child1, child2, child3, child4, child5, child6, child7, child8, NULL);
}
struct Node *node_buildn(int tag, ...) {
va_list args;
va_start(args, tag);
struct Node *n = node_alloc(tag);
int done = 0;
while (!done) {
struct Node *child = (struct Node *) va_arg(args, struct Node *);
if (child) {
node_add_kid(n, child);
} else {
done = 1;
}
}
va_end(args);
return n;
}
void node_destroy(struct Node *n) {
delete n;
}
void node_destroy_recursive(struct Node *n) {
int num_kids = n->get_num_kids();
for (int i = 0; i < num_kids; i++) {
Node *kid = n->get_kid(i);
if (kid) {
node_destroy_recursive(kid);
}
}
node_destroy(n);
}
int node_get_tag(struct Node *n) {
return n->get_tag();
}
int node_get_num_kids(struct Node *n) {
return n->get_num_kids();
}
void node_add_kid(struct Node *n, struct Node *kid) {
n->add_kid(kid);
}
void node_prepend_kid(struct Node *n, struct Node *kid) {
n->prepend_kid(kid);
}
struct Node *node_get_kid(struct Node *n, int index) {
return n->get_kid(index);
}
int node_first_kid_has_tag(struct Node *n, int tag) {
return n->get_num_kids() > 0 && n->get_kid(0)->get_tag() == tag;
}
void node_set_source_info(struct Node *n, struct SourceInfo source_info) {
n->set_source_info(source_info);
}
struct SourceInfo node_get_source_info(struct Node *n) {
return n->get_source_info();
}
const char *node_get_str(struct Node *n) {
return n->get_str().c_str();
}
long node_get_ival(struct Node *n) {
return n->get_ival();
}
void node_set_ival(struct Node *n, long ival) {
n->set_ival(ival);
}
/*
void node_set_symbol(struct Node *n, struct SymbolTable *symtab, unsigned index) {
n->set_symtab(symtab);
n->set_index(index);
}
struct SymbolTable *node_get_symtab(struct Node *n) {
return n->get_symtab();
}
unsigned node_get_index(struct Node *n) {
return n->get_index();
}
// Assume that this function exists, and can retrieve a pointer to
// a Symbol from a SymbolTable, given its index.
extern "C" struct Symbol *symtab_get_entry(struct SymbolTable *symtab, unsigned index);
struct Symbol *node_get_symbol(struct Node *n) {
SymbolTable *symtab = n->get_symtab();
if (!symtab) {
// The Node doesn't have a symbol table entry
return nullptr;
}
return symtab_get_entry(symtab, n->get_index());
}
*/