-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtab.cpp
404 lines (336 loc) · 8.55 KB
/
symtab.cpp
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <algorithm>
#include "symtab.hpp"
#include <assert.h>
#include <stdio.h>
/****** SymName Implemenation **************************************/
SymName::SymName(const char* x)
{
m_spelling = x;
m_parent_attribute = NULL;
}
SymName::SymName(const SymName & other)
{
m_spelling = strdup(other.m_spelling);
m_parent_attribute = other.m_parent_attribute;
}
SymName& SymName::operator=(const SymName & other)
{
delete m_spelling;
SymName tmp(other);
swap(tmp);
return *this;
}
void SymName::swap(SymName & other)
{
std::swap(m_spelling, other.m_spelling);
}
SymName::~SymName()
{
delete( m_spelling );
}
void SymName::accept(Visitor *v)
{
v->visitSymName(this);
}
SymName* SymName::clone() const
{
return new SymName(*this);
}
const char* SymName::spelling()
{
return m_spelling;
}
Symbol* SymName::symbol()
{
return m_symbol;
}
void SymName::set_symbol(Symbol* s)
{
//you should not try to reset a symbol pointer
//once it is set, that pointer is used all
//sorts of places - changes to it once it is
//set can results in weird behavior
assert( m_symbol == NULL );
m_symbol = s;
}
/****** SymScope Def (used by SymTab) **************************************/
//comparison structure for strings
struct eqstr {
bool operator()(const char* s1, const char* s2) const
{ return strcmp(s1, s2) == 0; }
};
class SymScope
{
private:
SymScope* m_parent;
list<SymScope*> m_child;
typedef hash_map<const char*, Symbol*, hash<const char*>, eqstr> ScopeTableType;
ScopeTableType m_scopetable;
bool as_param;
int m_localsize;
int m_paramsize;
SymScope* parent();
void add_child(SymScope* c);
SymScope(SymScope * parent);
bool is_dup_string(const char* name); //used for error check
void dump( FILE* f, int nest_level );
void set_param_mode() { as_param = true; }
void set_local_mode() { as_param = false; }
SymScope* open_scope();
SymScope* close_scope();
bool exist( const char* name );
Symbol* insert( const char* name, Symbol * s );
Symbol* lookup( const char * name );
public:
SymScope();
~SymScope();
friend class SymTab; //symtab is a wrapper class
};
/****** SymTab Implementation **************************************/
SymTab::SymTab()
{
m_head = new SymScope;
m_cur_scope = m_head;
}
SymTab::~SymTab()
{
delete m_head;
}
bool SymTab::is_dup_string(const char* name)
{
return m_head->is_dup_string(name);
}
void SymTab::open_scope()
{
m_cur_scope = m_cur_scope->open_scope();
assert( m_cur_scope != NULL );
}
void SymTab::close_scope()
{
//check to make sure we don't pop more than we push
assert( m_cur_scope != m_head );
assert( m_cur_scope != NULL );
m_cur_scope = m_cur_scope->close_scope();
}
void SymTab::set_param_mode() { m_cur_scope->set_param_mode(); }
void SymTab::set_local_mode() { m_cur_scope->set_local_mode(); }
SymScope* SymTab::get_scope()
{
//check that we actually have a scope before we return it
assert( m_cur_scope != NULL );
return m_cur_scope;
}
// Returns the lexical level of the symbol
// Procedure symbol levels are increased by one to give the
// expected results, even though the procedure name appears one
// level below.
int SymTab::get_lexical_level(Symbol *s)
{
int level = lexical_distance(m_head, s->get_scope());
if (s->m_basetype == bt_procedure)
return level + 1;
return level;
}
bool SymTab::exist( const char* name )
{
assert( name != NULL );
return m_cur_scope->exist( name );
}
bool SymTab::insert( const char* name, Symbol * s )
{
assert( name != NULL );
assert( s != NULL );
// the assert below fails, it is because you tried to insert
// a pointer to a string that is already in the SymTab. You
// can have duplicate names, but each needs to reside it it's
// own chunk of memory (see example)
assert( is_dup_string(name) );
Symbol* r = m_cur_scope->insert( name, s );
if ( r == NULL ) return true;
else return false;
}
bool SymTab::insert_in_parent_scope( const char* name, Symbol * s )
{
assert( name != NULL );
assert( s != NULL );
// the assert below fails, it is because you tried to insert
// a pointer to a string that is already in the SymTab. You
// can have duplicate names, but each needs to reside it it's
// own chunk of memory (see example)
assert( is_dup_string(name) );
// make sure there is an actual parent scope
assert( m_cur_scope->m_parent != NULL );
Symbol* r = m_cur_scope->m_parent->insert( name, s );
if ( r == NULL ) return true;
else return false;
}
Symbol* SymTab::lookup( const char * name )
{
assert( name != NULL );
return m_cur_scope->lookup( name );
}
Symbol* SymTab::lookup( SymScope* targetscope, const char * name )
{
assert( name != NULL );
assert( targetscope != NULL );
return targetscope->lookup( name );
}
int SymTab::scopesize( SymScope* targetscope )
{
return targetscope->m_localsize;
}
int SymTab::lexical_distance( SymScope* higher_scope, SymScope* deeper_scope )
{
assert(deeper_scope != NULL);
assert(higher_scope != NULL);
if( higher_scope == deeper_scope ) return 0;
return lexical_distance( higher_scope, deeper_scope->m_parent ) + 1;
}
void SymTab::dump( FILE* f )
{
m_head->dump(f, 0);
}
/****** SymScope Implementation **************************************/
SymScope::SymScope()
{
m_parent = NULL;
m_localsize = 0;
m_paramsize = 0;
as_param = false;
}
SymScope::SymScope(SymScope * parent)
{
m_parent = parent;
m_localsize = 0;
m_paramsize = 0;
if (parent!=NULL) {
parent->add_child(this);
}
}
SymScope::~SymScope()
{
//delete the keys, but not the symbols (symbols are linked elsewhere)
ScopeTableType::iterator si, this_si;
si = m_scopetable.begin();
while ( si!=m_scopetable.end() )
{
char* oldkey = (char*)si->first;
this_si = si;
++si;
m_scopetable.erase( this_si );
free( oldkey );
}
//now delete all the children
list<SymScope*>::iterator li;
for( li=m_child.begin(); li!=m_child.end(); ++li )
{
delete *li;
}
}
void SymScope::dump(FILE* f, int nest_level)
{
//recursively prints out the symbol table
//from the head down through all the childrens
ScopeTableType::iterator si;
//indent appropriately
fprintf(f,"# ");
for( int i=0; i<nest_level; i++ ) { fprintf(f,"\t"); }
fprintf(f," +-- Symbol Scope (%d bytes at %x)---\n", m_localsize,
this);
for( si = m_scopetable.begin(); si != m_scopetable.end(); ++si )
{
//indent appropriately
fprintf(f,"# ");
for( int i=0; i<nest_level; i++ ) { fprintf(f,"\t"); }
fprintf( f, "| %s (offset=%d,scope=%x)\n", si->first, si->second->m_offset, si->second->m_symscope );
}
fprintf(f,"# ");
for( int i=0; i<nest_level; i++ ) { fprintf(f,"\t"); }
fprintf(f,"+-------------\n#\n");
//now print all the children
list<SymScope*>::iterator li;
for( li=m_child.begin(); li!=m_child.end(); ++li )
{
(*li)->dump(f, nest_level+1);
}
}
bool SymScope::is_dup_string(const char* name)
{
ScopeTableType::iterator si;
si = m_scopetable.find( name );
if ( si != m_scopetable.end() ) {
//check if the pointers match
if ( si->first == name ) return false;
}
list<SymScope*>::iterator li;
for( li=m_child.begin(); li!=m_child.end(); ++li )
{
bool r = (*li)->is_dup_string(name);
if ( r==false ) return false;
}
//if it gets this far, there is no duplicate
return true;
}
void SymScope::add_child(SymScope* c)
{
m_child.push_back(c);
}
SymScope* SymScope::open_scope()
{
return new SymScope(this);
}
SymScope* SymScope::close_scope()
{
return m_parent;
}
bool SymScope::exist( const char* name )
{
Symbol* s;
s = lookup(name);
// return true if name exists
if ( s!=NULL ) return true;
else return false;
}
Symbol* SymScope::insert( const char* name, Symbol * s )
{
pair<ScopeTableType::iterator,bool> iret;
typedef pair<const char*,Symbol*> hpair;
iret = m_scopetable.insert( hpair(name,s) );
if( iret.second == true ) {
//insert was successfull
//update the offset and size
if (as_param) {
// Inserts at positive values
s->m_offset = m_paramsize;
m_paramsize += s->get_size();
}
else {
// Inserts at negative values
m_localsize += s->get_size();
s->m_offset = -m_localsize;
}
//set the scope
s->m_symscope = this;
return NULL;
} else {
//cannot insert, there was a duplicate entry
//return a pointer to the conflicting symbol
return iret.first->second;
}
}
Symbol* SymScope::lookup( const char * name )
{
//first check the current table;
ScopeTableType::const_iterator i;
i = m_scopetable.find( name );
if ( i != m_scopetable.end() ) {
return i->second;
}
//failing that, check all the parents;
if ( m_parent != NULL ) {
return m_parent->lookup( name );
} else {
//if this has no parents, then it cannot be found
return NULL;
}
}