-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbols.h
169 lines (136 loc) · 2.99 KB
/
symbols.h
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
#ifndef _SYMBOLS_
#define _SYMBOLS_
/*
The symbol table. At top level in root_scope is a FunctionScope,
which may contain other FunctionScopes (child functions)
and SymbolScopes (e.g. blocks).
*/
#include <assert.h>
#include <list>
#include <string>
#include <unordered_map>
#include <vector>
#include "platform.h"
class Value;
class FunctionScope;
class FunctionType;
class SymbolScope
{
public:
SymbolScope(SymbolScope *p, std::string n)
{
parent_scope = p;
if (p)
{
p->addChild(this);
}
symbol_name = n;
values_gotten = false;
}
virtual ~SymbolScope()
{
for (auto & it:children)
{
delete it;
}
}
void addChild(SymbolScope *s)
{
children.push_back(s);
}
void add(Value *);
Value *lookup(std::string, int &, bool = true);
Value *lookupLocal(std::string n)
{
int static_depth = 0;
Value *ret = lookup(n, static_depth, false);
return ret;
}
FunctionScope *lookup_function(std::string);
virtual FunctionScope *currentFunction();
virtual FunctionScope *parentFunction();
virtual bool isFunction()
{
return false;
}
SymbolScope *parent()
{
return parent_scope;
}
void addFunction(FunctionScope *);
std::list<SymbolScope *> &getChildren()
{
return children;
}
// Caller becomes responsible for deleting values
void getValues(std::vector<Value *> &values);
std::string name()
{
return symbol_name;
}
std::string fqName();
std::string longName();
protected:
SymbolScope *parent_scope;
std::unordered_map<std::string, Value *> contents;
std::vector<Value *> sorted_contents;
std::unordered_map<std::string, FunctionScope *> functions;
std::list<SymbolScope *> children;
std::string symbol_name;
bool values_gotten;
};
class FunctionScope : public SymbolScope
{
public:
FunctionScope(SymbolScope *p, std::string n, FunctionType *ft);
~FunctionScope();
bool isGeneric();
virtual FunctionScope *currentFunction();
virtual FunctionScope *parentFunction();
void addArg(Value *s)
{
add(s);
args_list.push_back(s);
}
std::vector<Value *> &args()
{
return args_list;
}
FunctionType *getType()
{
return type;
}
virtual bool isFunction()
{
return true;
}
uint64_t getAddr()
{
assert(addr);
return addr;
}
void setAddr(uint64_t a)
{
addr = a;
}
void setStackSize(uint64_t s)
{
stack_size = s;
}
uint64_t getStackSize()
{
assert(stack_size);
return stack_size;
}
protected:
std::vector<Value *> args_list;
FunctionType *type;
uint64_t addr;
uint64_t stack_size;
Value *oldframe;
Value *ip;
Value *staticlink;
Value *retvar;
};
extern FunctionScope *root_scope;
#endif