-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.h
More file actions
182 lines (155 loc) · 4.56 KB
/
Copy pathSymbolTable.h
File metadata and controls
182 lines (155 loc) · 4.56 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
#ifndef __SYMBOLTABLE_H__
#define __SYMBOLTABLE_H__
#include <assert.h>
#include <string>
#include <map>
class Type;
class Operand;
class SymbolEntry
{
private:
int kind;
SymbolEntry* next;
protected:
enum { CONSTANT, VARIABLE, TEMPORARY };
Type* type;
public:
SymbolEntry(Type* type, int kind);
virtual ~SymbolEntry(){};
bool isConstant() const { return kind == CONSTANT; };
bool isTemporary() const { return kind == TEMPORARY; };
bool isVariable() const { return kind == VARIABLE; };
Type* getType() { return type; };
void setType(Type* type) { this->type = type; };
virtual std::string toStr() = 0;
bool setNext(SymbolEntry* se);
SymbolEntry* getNext() const { return next; };
// You can add any function you need here.
};
// symbol table managing identifier symbol entries
class SymbolTable
{
private:
std::map<std::string, SymbolEntry*> symbolTable;
SymbolTable* prev;
int level;
static int counter;
public:
SymbolTable();
SymbolTable(SymbolTable* prev);
bool install(std::string name, SymbolEntry* entry);
SymbolEntry* lookup(std::string name);
SymbolTable* getPrev() { return prev; };
int getLevel() { return level; };
static int getLabel() { return counter++; };
static void resetLabel() { counter = 0; };
};
/*
Symbol entry for literal constant. Example:
int a = 1;
Compiler should create constant symbol entry for literal constant '1'.
*/
class ConstantSymbolEntry : public SymbolEntry
{
private:
int value;
std::string strValue;
public:
ConstantSymbolEntry(Type* type, int value);
ConstantSymbolEntry(Type* type, std::string strValue);
ConstantSymbolEntry(Type* type);
virtual ~ConstantSymbolEntry(){};
int getValue() const;
std::string getStrValue() const;
std::string toStr();
// You can add any function you need here.
};
/*
Symbol entry for identifier. Example:
int a;
int b;
void f(int c)
{
int d;
{
int e;
}
}
Compiler should create identifier symbol entries for variables a, b, c, d and e:
| variable | scope |
| a | GLOBAL |
| b | GLOBAL |
| c | PARAM |
| d | LOCAL |
| e | LOCAL +1 |
*/
class IdentifierSymbolEntry : public SymbolEntry
{
private:
enum { GLOBAL = 0, PARAM, LOCAL };
std::string name;
int scope;
int value;
int label;
bool initial;
bool sysy;
int *arrayValue;
bool allZero;
int paramNo;
bool constant;
Operand* addr; // The address of the identifier.
// You can add any field you need here.
public:
IdentifierSymbolEntry(Type* type, std::string name, int scope, int paramNo = -1, bool sysy = false);
virtual ~IdentifierSymbolEntry(){};
std::string toStr();
bool isGlobal() const { return scope == GLOBAL; };
bool isParam() const { return scope == PARAM; };
bool isLocal() const { return scope >= LOCAL; };
bool isSysy() const { return sysy; };
int getScope() const { return scope; };
void setAddr(Operand* addr) { this->addr = addr; };
Operand* getAddr() { return addr; };
void setValue(int value);
int getValue() const { return value; };
void setArrayValue(int* arrayValue);
int* getArrayValue() const { return arrayValue; };
int getLabel() const { return label; };
void setLabel() { label = SymbolTable::getLabel(); };
void setAllZero() { allZero = true; };
bool isAllZero() const { return allZero; };
int getParamNo() const { return paramNo; };
void setConst() { constant = true;};
bool isConst() const { return constant; };
// You can add any function you need here.
};
/*
Symbol entry for temporary variable created by compiler. Example:
int a;
a = 1 + 2 + 3;
The compiler would generate intermediate code like:
t1 = 1 + 2
t2 = t1 + 3
a = t2
So compiler should create temporary symbol entries for t1 and t2:
| temporary variable | label |
| t1 | 1 |
| t2 | 2 |
*/
class TemporarySymbolEntry : public SymbolEntry
{
private:
int stack_offset;
int label;
public:
TemporarySymbolEntry(Type *type, int label);
virtual ~TemporarySymbolEntry() {};
std::string toStr();
int getLabel() const {return label;};
void setOffset(int offset) { this->stack_offset = offset; };
int getOffset() { return this->stack_offset; };
// You can add any function you need here.
};
extern SymbolTable *identifiers;
extern SymbolTable *globals;
#endif