-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.h
206 lines (143 loc) · 5.17 KB
/
SymbolTable.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
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
#ifndef SYMBOLTABLE_H_
#define SYMBOLTABLE_H_
#include "Semantikin.h"
#include <iostream>
#include <vector>
#include <map>
#include <memory>
#include <string>
using std::cout;
using std::endl;
using std::map;
using std::pair;
using std::string;
using std::vector;
using std::unique_ptr;
using std::shared_ptr;
namespace Parser {
class SymbolTableEntry {
protected:
/* Name the user gave to the variable/function. */
string name;
/* the variable's type. */
NativeType _type;
public:
SymbolTableEntry() : name(""), _type(Parser::NOT_A_TYPE) { }
SymbolTableEntry(string nm, NativeType tp) : name(nm), _type(tp) {}
virtual string getName() { return this->name; }
NativeType type() const { return this->_type; }
void type(NativeType tp) { this->_type = tp; }
void rename(string newName) { this->name = newName; }
virtual void dump(std::stringstream& buffer) const = 0;
virtual ~SymbolTableEntry() {};
};
/* Represent any kind of variable: parameter, temporary, local. */
class STVariableDeclaration : public SymbolTableEntry {
protected:
/* How many bytes this variable occupy. */
int _width;
/* Offset within the function frame where this variable will be stored. */
int _offset;
/* Is this variable scalar (dims.size() == 0) or a vector with N dimensions
* (dims.size() == N)? If it is a vector, each index store the size of each
* respective dimension. */
vector<int> _dims;
public:
STVariableDeclaration() : SymbolTableEntry("", Parser::NOT_A_TYPE), _width(0), _offset(0)
{ }
STVariableDeclaration(string nm, NativeType tp, int wd, int off) : SymbolTableEntry(nm, tp), _width(wd), _offset(off)
{ }
const vector<int>& dims() const { return _dims; }
int getNumDims() const { return _dims.size(); }
void addDim(int size) { _dims.push_back(size); }
int offset() const { return _offset; }
void offset(int ofts) { this->_offset = ofts; }
int width() const { return _width; }
};
/* This represent a local variable declaration. */
class STLocalVarDecl : public STVariableDeclaration {
public:
STLocalVarDecl(string nm, NativeType tp, int wd, int off) : STVariableDeclaration(nm, tp, wd, off)
{ }
void dump(std::stringstream& buffer) const;
};
/* This represent a formal parameter declaration in a function. */
class STParamDecl : public STVariableDeclaration {
public:
STParamDecl(string nm, NativeType tp, int wd, int off) : STVariableDeclaration(nm, tp, wd, off)
{ }
void dump(std::stringstream& buffer) const;
};
/* This is the base class for all kinds of registers. */
class STRegister : public SymbolTableEntry {
private:
unsigned int _width = 0;
public:
STRegister(string nm, NativeType tp, int wd) : SymbolTableEntry(nm, tp), _width(wd)
{ }
unsigned int width() const { return this->_width; }
void dump(std::stringstream& buffer) const;
};
/* This entry will represent a address label used in the IR. */
class STLabelDef : public SymbolTableEntry {
private:
int _address;
int _usageCounter;
public:
STLabelDef(string nm) : SymbolTableEntry(nm, Parser::NOT_A_TYPE), _address(0), _usageCounter(0)
{ }
void address(int addr) { this->_address = addr; }
int address() const { return this->_address; }
int usageCounter() const { return this->_usageCounter; }
void incrementUses() { this->_usageCounter++; }
void dump(std::stringstream& buffer) const;
};
/* This is used to store the constants when representing the IR. */
class STConstantDef : public SymbolTableEntry {
private:
/* TODO: make these fields part of a union */
int _integer;
float _floating;
string _str;
public:
/* TODO: Change this */
STConstantDef(string nm, int i) : SymbolTableEntry(nm, Parser::INT), _str(""), _integer(i), _floating(0) {}
string getName() {
switch (this->_type) {
case Parser::INT: return std::to_string(this->_integer);
case Parser::VOID: return "void";
case Parser::NOT_A_TYPE:
throw System::EXCEPTION_UNEXPECTED_TYPE;
}
}
void dump(std::stringstream& buffer) const;
};
/* Represents the definition of a function. */
class STFunctionDeclaration : public SymbolTableEntry {
private:
/* Compiler assigned function's label. */
string label;
vector<shared_ptr<SymbolTableEntry>> _params;
public:
STFunctionDeclaration(string nm, string lbl, NativeType rtp) : SymbolTableEntry(nm, rtp), label(lbl)
{ }
string getLabel() const { return label; }
const vector<shared_ptr<SymbolTableEntry>>& params() const { return _params; }
void addParam(shared_ptr<SymbolTableEntry> newParam) { _params.push_back(newParam); }
void dump(std::stringstream& buffer) const;
};
class SymbolTable {
private:
map<string, shared_ptr<SymbolTableEntry>> _entries;
shared_ptr<SymbolTable> parent;
public:
SymbolTable(shared_ptr<SymbolTable> _parent) : parent(_parent) {}
shared_ptr<SymbolTable> getParent() const { return this->parent; }
shared_ptr<SymbolTableEntry> lookup(string name);
bool add(shared_ptr<SymbolTableEntry> _entry);
map<string, shared_ptr<SymbolTableEntry>> entries() { return this->_entries; }
void dump(std::stringstream& buffer) const ;
int stackFrameSize() const ;
};
}
#endif /* SYMBOLTABLE_H_ */