symtab.c++
3.88 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
183
184
185
186
187
188
189
190
/* Symbol table */
#include "libbank.h"
#define ktab 4
int objectID = 0;
/*
* Push a null on top of scope stack. (Pretend there's a scope
* outside the global scope with no symbols). This makes some code
* slightly simpler.
*/
ICSymTab::ICSymTab(void)
{
stacktop = &scopestack[0]; /* used to be **stacktop = ### ??? */
offsettop = &offsetstack[0];
*stacktop = NULL;
*offsettop = 0;
PushScope(); /* makes endcases easier if init with extra NULL scope */
globalscope = stacktop;
}
/*
* True if in global scope; false otherwise.
*/
int ICSymTab::InGlobalScope()
{
return (stacktop == globalscope);
}
/*
* push a copy of top of stack onto stack.
*/
void ICSymTab::PushScope()
{
ste *temp = *stacktop++;
*stacktop = temp;
/* Prepare the new offset. */
offsettop++;
*offsettop = 0;
}
/*
* Restore old scope. Return list of ste's from scope that was popped.
*/
ste *ICSymTab::PopScope()
{
ste *innerscope = *stacktop--;
ste *list = innerscope;
/* restore previous offset */
offsettop--;
/* Handle special case where scope is empty. */
if (list == *stacktop) {
return NULL;
}
while ((list->prev != *stacktop) && (list != NULL)) {
list = list->prev;
}
if (!list)
return NULL;
list->prev = NULL;
/* return the popped scope. */
return innerscope;
}
void ICSymTab::Declare(id *idptr, ICDecl *declptr)
{
ste *steptr;
/* Make sure it hasn't already been declared in the current scope. */
CheckRedecl(idptr);
/* Allocate a new ste. */
steptr = new ste;
/* bind id to declptr. */
steptr->name = idptr;
steptr->decl = declptr;
declptr->SetDeclID(idptr);
/* push onto front of symbol table */
steptr->prev = *stacktop;
*stacktop = steptr;
}
void ICSymTab::DeclareGlobal(id *idptr, ICDecl *declptr)
{
ste *steptr;
/* Make sure it hasn't already been declared in the current scope. */
CheckRedecl(idptr);
/* Allocate a new ste. */
steptr = new ste;
/* bind id to declptr. */
steptr->name = idptr;
steptr->decl = declptr;
declptr->SetDeclID(idptr);
/* push onto front of symbol table */
steptr->prev = *globalscope;
*stacktop = steptr;
}
/* Look up declaration on the list indicated by ste argument */
ICDecl *ICSymTab::FindDecl(id *idptr, ste *steptr)
{
ste *tsteptr = steptr;
while ((tsteptr != NULL) && (idptr != tsteptr->name)) {
tsteptr = tsteptr->prev;
}
if (tsteptr)
return tsteptr->decl;
else
return NULL;
}
/* Look up declaration in current scope. */
ICDecl *ICSymTab::FindCurrentDecl(struct id *idptr)
{
ICDecl *declptr = FindDecl(idptr, *stacktop);
if (!declptr) {
return 0;
} else {
return declptr;
}
}
/* Returns id of a given decl */
id *ICSymTab::FindName(ICDecl *thedecl)
{
ste *helper = *stacktop;
while ((helper != NULL) && (helper->decl != thedecl)) {
helper = helper->prev;
}
if (helper == NULL) return NULL;
else return helper->name;
}
void ICSymTab::CheckRedecl(struct id *idptr)
{
ste *steptr = *stacktop;
ste *scopeend = *(stacktop-1);
while ((steptr != scopeend) && (idptr != steptr->name)) {
steptr = steptr->prev;
}
if (steptr != scopeend) {
FailIfMsg(0, IC_INTERNAL_ERR, "Already declared in this scope.");
}
}
/* if id is globally declared, returns reference, else zero */
ICDecl *ICSymTab::TestGlobalDecl(struct id *idptr)
{
return FindDecl(idptr, *globalscope);
}
/****************************************************************************
Pretty printing stuff for symbol table
****************************************************************************/
void make_tab_str(char *tabstr, int tab)
{
int i;
for (i=0; i < tab; i++)
tabstr[i] = ' ';
tabstr[i] = '\0';
}