-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstab.c
301 lines (244 loc) · 6.35 KB
/
stab.c
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
#include <assert.h>
#include <string.h>
#include <sys/types.h>
#include <stdlib.h>
#include "c2ada.h"
#undef NULL
#define NULL 0L
#undef HASH_MAX
#define HASH_MAX 512
#ifndef MAX_SCOPE_LEVEL
#define MAX_SCOPE_LEVEL 64
#endif
extern file_pos_t yypos;
static symbol_t* hash_table[HASH_MAX];
typedef struct
{
int nparams;
symbol_t* scope_thread;
int scope_id;
} scope_stack_info_t;
typedef struct
{
scope_kind_t kind;
scope_id_t parent;
short int level;
symbol_t* sym;
} scope_info_t, *scope_info_pt;
static scope_stack_info_t scope_info[MAX_SCOPE_LEVEL];
static int cur_scope_level = 0; /* current scope depth */
/* scope_id assigned to most recent innermost scope */
static int scope_id = 0;
static int current_scope_id;
#define SCOPE_INFO_BLOCKSIZE 64
static scope_info_pt* scope_tab;
static int scope_tab_size = 0;
scope_id_t new_scope_id(scope_kind_t kind)
{
static scope_info_t* free = NULL;
static int free_index;
scope_info_t* new_scope;
scope_id_t result = ++scope_id;
if(!scope_tab_size)
{
scope_tab_size = 128;
scope_tab = (scope_info_pt*)allocate(sizeof(scope_info_pt) * scope_tab_size);
}
else if(result == scope_tab_size)
{
scope_tab_size = 1.5 * scope_tab_size;
scope_tab = (scope_info_pt*)realloc(scope_tab, sizeof(scope_info_pt) * scope_tab_size);
}
if(!free || free_index > SCOPE_INFO_BLOCKSIZE - 1)
{
free = (scope_info_t*)allocate(sizeof(scope_info_t) * SCOPE_INFO_BLOCKSIZE);
free_index = 0;
}
new_scope = &free[free_index++];
new_scope->kind = kind;
scope_tab[result] = new_scope;
current_scope_id = result;
return result;
}
scope_id_t new_block_scope(scope_id_t parent)
{
scope_id_t result = new_scope_id(Block_scope);
set_scope_parent(result, parent);
scope_tab[result]->level = scope_level(parent) + 1;
return result;
}
scope_id_t current_scope(void)
{
return current_scope_id;
}
void set_current_scope(scope_id_t scope)
{
current_scope_id = scope;
}
symbol_t* new_sym(void)
{
static symbol_t* free = NULL;
static int free_index;
symbol_t* decl;
if(free == NULL || free_index > 63)
{
free = (symbol_t*)allocate(sizeof(symbol_t) * 64);
free_index = 0;
}
decl = &free[free_index++];
decl->sym_def = yypos;
decl->sym_scope = cur_scope_level;
decl->sym_scope_id = scope_info[cur_scope_level].scope_id;
decl->traversal_unit = -1;
decl->is_declared_in_header = current_unit_is_header;
return decl;
}
symbol_t* find_sym(char* name)
{
hash_t hash;
int index;
symbol_t* decl;
assert(name != NULL);
hash = common_hash(name);
index = hash & (HASH_MAX - 1);
for(decl = hash_table[index]; decl; decl = decl->sym_hash_list)
{
assert(decl->sym_hash_list != decl);
if(decl->sym_hash == hash && !strcmp(name, decl->sym_ident->node.id.name))
{
return decl;
}
}
return NULL;
}
void store_sym(symbol_t* decl)
{
int index;
int level = decl->sym_scope;
assert(decl != NULL);
assert(decl->sym_ident != NULL);
assert(decl->sym_ident->node_kind == _Ident);
assert(decl->sym_ident->node.id.name != NULL);
if(decl->stored)
return;
decl->stored = 1;
decl->sym_hash = common_hash(decl->sym_ident->node.id.name);
index = decl->sym_hash & (HASH_MAX - 1);
decl->sym_hash_list = hash_table[index];
hash_table[index] = decl;
#if 0
/* this should only occur on func defs */
if (level != cur_scope_level) {
warning(__FILE__, __LINE__, "scope level mismatch for %s",
decl->sym_ident->node.id.name);
}
#endif
decl->sym_scope_list = scope_info[level].scope_thread;
scope_info[level].scope_thread = decl;
}
void scope_push(scope_kind_t kind)
{
scope_id_t scope;
if(++cur_scope_level >= MAX_SCOPE_LEVEL)
{
fatal(__FILE__, __LINE__, "Maximum scope depth exceeded");
}
scope = new_scope_id(kind);
set_scope_parent(scope, scope_info[cur_scope_level - 1].scope_id);
scope_tab[scope]->level = cur_scope_level;
scope_info[cur_scope_level].nparams = 0;
scope_info[cur_scope_level].scope_thread = NULL;
scope_info[cur_scope_level].scope_id = scope_id;
}
static void pop_scope_thread()
{
symbol_t *thread, *sym, *last, *next;
int index;
for(thread = scope_info[cur_scope_level].scope_thread; thread; thread = next)
{
next = thread->sym_scope_list;
thread->sym_scope_list = NULL;
/*
* Do we really want to pull this symbol out
* of the symbol table?
*/
switch(thread->sym_kind)
{
case type_symbol:
case enum_literal:
case func_symbol:
continue;
default:
break;
}
last = NULL;
index = thread->sym_hash & (HASH_MAX - 1);
for(sym = hash_table[index]; sym && sym != thread; sym = sym->sym_hash_list)
{
last = sym;
}
if(sym != NULL)
{
if(last != NULL)
{
last->sym_hash_list = sym->sym_hash_list;
}
else
{
hash_table[index] = sym->sym_hash_list;
}
}
}
}
void scope_pop()
{
pop_scope_thread();
if(--cur_scope_level < 0)
{
fatal(__FILE__, __LINE__, "Scope stack underflow");
}
current_scope_id = scope_info[cur_scope_level].scope_id;
}
int next_param()
{
return ++scope_info[cur_scope_level].nparams;
}
scope_kind_t scope_kind(scope_id_t scope)
{
return scope_tab[scope]->kind;
}
scope_id_t scope_parent(scope_id_t scope)
{
return scope_tab[scope]->parent;
}
symbol_t* scope_symbol(scope_id_t scope)
{
return scope_tab[scope]->sym;
}
int scope_level(scope_id_t scope)
{
return scope_tab[scope]->level;
}
symbol_t* scope_parent_func(scope_id_t scope)
{
symbol_t* sym;
for(; scope; scope = scope_parent(scope))
{
if((sym = scope_symbol(scope)))
return sym;
}
return 0;
}
void set_scope_kind(scope_id_t scope, scope_kind_t kind)
{
scope_tab[scope]->kind = kind;
}
void set_scope_parent(scope_id_t scope, scope_id_t parent)
{
scope_tab[scope]->parent = parent;
}
void set_scope_symbol(scope_id_t scope, symbol_t* sym)
{
assert(scope != 0);
scope_tab[scope]->sym = sym;
}