-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSymbolTable.cpp
More file actions
170 lines (153 loc) · 3.99 KB
/
Copy pathSymbolTable.cpp
File metadata and controls
170 lines (153 loc) · 3.99 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
#include "SymbolTable.h"
#include <cassert>
#include <iostream>
SymbolTable::SymbolTable(uint32 stackSize)
:m_StackSize(stackSize)
{
m_CurrentFunc = SymbolTable::Func();
}
bool SymbolTable::AddFunction(const std::string &name, std::string &arguments)
{
if(HasSymbol(name))
return false;
//Add Symbol
auto sbl = SymbolTable::Symbol();
sbl.name = name;
sbl.value = m_StackSize + m_NumInstructions;
sbl.type = SymbolType::FUNCTION;
m_Table.push_back(sbl);
//The following variables are not static anymore
SetParsingStatic(false, name);
m_NumInstructions += 8;//First two instructions are int32 numArgs and int32 numLoc
//Also add function arguments (parameters)
while(!arguments.empty())
{
std::string arg;
std::size_t nDelim = arguments.find(' ', 1);
if(nDelim == std::string::npos)
{
arg = arguments;
arguments = std::string();
}
else
{
arg = arguments.substr(0, nDelim);
arguments = arguments.substr(nDelim+1);
}
if(!(AddVariable(arg, true))) return false;
}
return true;
}
bool SymbolTable::AddLabel(const std::string &name)
{
if(HasSymbol(name))
return false;
auto sbl = SymbolTable::Symbol();
sbl.name = name;
sbl.value = m_StackSize + m_NumInstructions;
sbl.type = SymbolType::LABEL;
m_Table.push_back(sbl);
return true;
}
bool SymbolTable::AddVariable(const std::string &name, bool isArg)
{
if(HasSymbol(name))
return false;
auto sbl = SymbolTable::Symbol();
sbl.name = name;
if(isArg)
{
if(m_ParsingStatic)
return false;//Static segments don't have arguments
sbl.type = SymbolType::ARG;
}
else if(m_ParsingStatic) sbl.type = SymbolType::STATIC;
else sbl.type = SymbolType::LOCAL;
switch(sbl.type)
{
case SymbolType::STATIC:
sbl.value = m_StaticCounter;
m_StaticCounter += 4; //Multitype support should use variable size here
break;
case SymbolType::LOCAL:
sbl.value = m_CurrentFunc.numLoc;
m_CurrentFunc.numLoc += 4;
break;
case SymbolType::ARG:
sbl.value = m_CurrentFunc.numArg;
m_CurrentFunc.numArg += 4;
break;
default:
break;
}
m_Table.push_back(sbl);
return true;
}
void SymbolTable::SetParsingStatic(bool staticSection /*= true*/, std::string functionName)
{
m_ParsingStatic = staticSection;
//Prepare for next function
if (!m_CurrentFunc.name.empty())
{
m_FuncTable.push_back(m_CurrentFunc);
std::cout << "[SYMBOL] function: " << m_CurrentFunc.name << "; args: " << m_CurrentFunc.numArg << "; vars: " << m_CurrentFunc.numLoc << std::endl;
}
m_CurrentFunc = SymbolTable::Func();
m_CurrentFunc.name = functionName;
}
void SymbolTable::AllocateStatic()
{
std::cout << "[SYMBOL] Instruction count: " << m_NumInstructions << "; Symbols: " << std::endl;
uint32 staticBase = m_StackSize + m_NumInstructions;
for(auto & sbl : m_Table)
{
if(sbl.type == SymbolType::STATIC)
{
sbl.value += staticBase;
}
std::cout << "[SYMBOL] name: " << sbl.name << "; value: " << sbl.value << std::endl;
if(sbl.type == SymbolType::FUNCTION)
{
std::cout << "[SYMBOL] instruction pointer: " << sbl.value - m_StackSize << std::endl;
}
}
}
bool SymbolTable::HasSymbol(const std::string &name) const
{
for(auto sbl : m_Table)
{
if(sbl.name == name) return true;
}
return false;
}
uint32 SymbolTable::GetValue(const std::string &name) const
{
for(auto sbl : m_Table)
{
if(sbl.name == name) return sbl.value;
}
std::cerr << "[SYMBOL] Could not find Symbol " << name << std::endl;
return 0;
}
uint32 SymbolTable::GetFunctionArgCount(const std::string &name) const
{
for(auto func : m_FuncTable)
{
if(func.name == name) return func.numArg;
}
std::cerr << "[SYMBOL] Could not find Function " << name << std::endl;
return 0;
}
uint32 SymbolTable::GetFunctionVarCount(const std::string &name) const
{
for(auto func : m_FuncTable)
{
if(func.name == name) return func.numLoc;
}
std::cerr << "[SYMBOL] Could not find Function " << name << std::endl;
return 0;
}
uint32 SymbolTable::GetStaticVarCount() const
{
return m_StaticCounter;
}