symtab.c
4.45 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Symbol table */
#include "ic.h"
#include "y.tab.h"
#define ktab 4
struct ste *scopestack[100]; /* stack of saved scopes */
struct ste **stacktop = &scopestack[0];
struct ste **globalscope;
/*
* Used to compute offsets of variables within local frames, records,
* etc.
*/
int offsetstack[100];
int *offsettop = &offsetstack[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.
*/
void initsymtab(void)
{
*stacktop = (struct ste *) NULL;
*offsettop = 0;
pushscope(); /* makes endcases easier if init with extra NULL scope */
globalscope = stacktop;
}
/*
* True if in global scope; false otherwise.
*/
int inglobalscope()
{
return (stacktop == globalscope);
}
/*
* push a copy of top of stack onto stack.
*/
void pushscope()
{
struct 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.
*/
struct ste *popscope()
{
struct ste *innerscope = *stacktop--;
struct ste *list = innerscope;
/* restore previous offset */
offsettop--;
#ifdef VERBOSE
if (verbose) {
printf("\nPopping scope. STE's popped: \n");
}
#endif
/* Handle special case where scope is empty. */
if (list == *stacktop) {
return (struct ste *) NULL;
}
while ((list->prev != *stacktop) && (list != NULL)) {
#ifdef VERBOSE
if (verbose) {
printf("%s ",(list->name)->name);
}
#endif
list = list->prev;
}
#ifdef VERBOSE
if (verbose) {
if (list) printf("%s ",(list->name)->name);
printf("\n");
}
#endif
if (list == (struct ste *) NULL) {
yyerror("No previous scope?!");
}
else list->prev = (struct ste *) NULL;
/* return the popped scope. */
return innerscope;
}
void declare(struct id *idptr, struct decl *declptr)
{
struct ste *steptr;
/* Make sure it hasn't already been declared in the current scope. */
checkredecl(idptr);
/* Allocate a new ste. */
steptr = (struct ste *) malloc(sizeof(struct ste));
/* bind id to declptr. */
steptr->name = idptr;
steptr->decl = declptr;
/* push onto front of symbol table */
steptr->prev = *stacktop;
*stacktop = steptr;
}
void declareglobal(struct id *idptr, struct decl *declptr)
{
struct ste *steptr;
/* Make sure it hasn't already been declared in the current scope. */
checkredecl(idptr);
/* Allocate a new ste. */
steptr = (struct ste *) malloc(sizeof(struct ste));
/* bind id to declptr. */
steptr->name = idptr;
steptr->decl = declptr;
/* push onto front of symbol table */
steptr->prev = *globalscope;
*stacktop = steptr;
}
/* Look up declaration on the list indicated by ste argument */
struct decl *finddecl(struct id *idptr, struct ste *steptr)
{
struct 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. */
struct decl *findcurrentdecl(struct id *idptr)
{
struct decl *declptr = finddecl(idptr, *stacktop);
if (!declptr) {
return errorvar;
} else {
return declptr;
}
}
/* Returns id of a given decl */
struct id *findname(struct decl *thedecl)
{
struct ste *helper = *stacktop;
while ((helper != NULL) && (helper->decl != thedecl)) {
helper = helper->prev;
}
if (helper == NULL) return NULL;
else return helper->name;
}
void checkredecl(struct id *idptr)
{
struct ste *steptr = *stacktop;
struct ste *scopeend = *(stacktop-1);
while ((steptr != scopeend) && (idptr != steptr->name)) {
steptr = steptr->prev;
}
if (steptr != scopeend) {
yyerror("Already declared in this scope.");
}
}
/* if id is globally declared, returns reference, else zero */
struct decl *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';
}