This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscope.cc
382 lines (329 loc) · 10.3 KB
/
scope.cc
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
/*****************************************************************************
* Copyright 1994-2005, Elliot Mednick and Mark Hummel
* This file is part of Veriwell.
*
* Veriwell is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Veriwell is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/* SCOPE.C - Routines for resolving hierarchical names, setting scopes, etc. */
#define SCOPE_C
#include <string.h>
#include "vtypes.h"
#include "tree.h"
#include "scope.h"
/* Pointer to the current scope's block */
tree current_scope;
/* This is used at compile time mostly to save outer-level declarations
for vars that are redefined at as inner level but then have to be restored
upon leaving the scope. */
struct scoping_level {
struct scoping_level *previous; /* points to outer-level scope */
tree block; /* points to current block node */
tree shadow; /* points to tree list of outer-level decls to be */
/* restored upon exit from this scope */
};
/* level0 and scope0 will hold names to top-level modules */
static struct scoping_level level0;
tree scope0 = NULL; /* Initialized in pass2.s */
static struct scoping_level *current_level = &level0;
static struct scoping_level *free_level = (struct scoping_level *) NULL;
/* Remember the previous declaration of an ident by making a TREE_LIST
with iden as TREE_PURPOSE and its old declaration as TREE_VALUE. */
void set_decl(tree ident, tree decl)
{
current_level->shadow =
link_tree_cons(ident, IDENT_CURRENT_DECL(ident),
current_level->shadow);
IDENT_CURRENT_DECL(ident) = decl;
}
void push_scope()
{
struct scoping_level *tmp;
/* create a new scoping level, or resuse one */
if (!free_level)
tmp =
(struct scoping_level *)
linkalloc(sizeof(struct scoping_level));
else {
tmp = free_level;
free_level = tmp->previous;
}
tmp->block = current_scope;
tmp->previous = current_level;
tmp->shadow = NULL_TREE;
current_level = tmp;
}
tree pop_scope()
{
tree decl_list, next;
struct scoping_level *level;
/* restore declarations of previous scope (set in set_decl). TREE_PURPOSE
has the IDENTIFIER_NODE; TREE_VALUE has the DECL_NODE. */
for (decl_list = current_level->shadow; decl_list; decl_list = next) {
next = TREE_CHAIN(decl_list);
IDENT_CURRENT_DECL(TREE_PURPOSE(decl_list)) =
TREE_VALUE(decl_list);
free_link_list(decl_list);
}
/* pop the scoping level and save the old frame for later use */
level = current_level;
current_level = current_level->previous;
level->previous = free_level;
free_level = level;
if (!current_level)
return NULL_TREE;
return current_level->block;
}
void set_scope(tree new_scope)
{
tree t;
current_scope = new_scope;
push_scope();
/* first, do ports */
for (t = BLOCK_PORTS(new_scope); t; t = TREE_CHAIN(t))
set_decl(DECL_NAME(t), t);
/* then do DECLs */
for (t = BLOCK_DECL(new_scope); t; t = TREE_CHAIN(t)) {
t = t;
set_decl(DECL_NAME(t), t);
}
/* if scope is a module then do specparams */
if (TREE_CODE(new_scope) == MODULE_BLOCK) {
for (t = MODULE_SPECDEFS(new_scope); t; t = TREE_CHAIN(t)) {
if (TREE_CODE(t) == SPECPARAM_DECL) {
set_decl(DECL_NAME(t), t);
}
}
}
// for (t = BLOCK_DOWN (new_scope); t; t = TREE_CHAIN (t))
// set_decl (BLOCK_NAME (t), t);
}
void reset_scope()
{
tree decl_list, next;
for (decl_list = current_level->shadow; decl_list; decl_list = next) {
next = TREE_CHAIN(decl_list);
IDENT_CURRENT_DECL(TREE_PURPOSE(decl_list)) =
TREE_VALUE(decl_list);
free_link_list(decl_list);
}
}
/* At runtime, puts the list of top-level modules in the symbol table
and sets the scope to the first listed top-level module. */
void initialize_scope(tree top_level)
{
tree t;
current_level = &level0;
current_scope = scope0;
/* Read top-level block names, only if called after pass2 */
if (top_level) {
for (t = BLOCK_DECL(scope0); t; t = TREE_CHAIN(t))
set_decl(DECL_NAME(t), t);
set_scope(top_level);
}
}
tree search_scope_across(tree starting_scope, char *name, int set)
{
tree scope;
for (scope = starting_scope; scope; scope = TREE_CHAIN(scope))
if (!strcmp(name, IDENT(BLOCK_NAME(scope)))) {
if (set)
set_scope(scope);
return scope;
}
return NULL_TREE;
}
tree search_scope_up(tree starting_scope, char *name, int set)
{
tree scope, new_scope;
for (scope = starting_scope; scope; scope = BLOCK_UP(scope)) {
if (set)
current_scope = pop_scope();
// if (!strcmp (name, IDENT (BLOCK_NAME (scope))))
if ((new_scope =
search_scope_across(scope, name, set)) != NULL_TREE)
return new_scope;
}
return search_scope_across(top_level, name, set);
}
/*
static
void
lookupward_scope (tree scope)
{
tree t;
int found = 0;
for (t = current_scope; t; t = BLOCK_UP (t))
{
current_scope = pop_scope ();
if (scope == t)
{
found = 1;
set_scope (t);
break;
}
}
if (!found)
{
for (t = top_level; t; t = TREE_CHAIN (t))
if (scope == t) {
set_scope (t);
found = 1;
}
}
if (!found)
error ("Scope not found", NULL_CHAR, NULL_CHAR);
}
*/
tree resolve_hierarchical_path(char *path)
{
extern char *token_buffer; /* Will already be as large as needed */
char *path_name = token_buffer, *decl_name;
strcpy(path_name, path);
/* seperate the last component of the path */
decl_name = strrchr(path_name, '.');
*decl_name = '\0';
decl_name++;
return search_up_decl(decl_name, search_scope(path_name, 0));
}
tree resolve_hierarchical_name(tree ident)
{
return (resolve_hierarchical_path(IDENT(ident)));
}
/* Traverse the tree following the hierichical name. If "set" is non-zero,
then also set the scope as the tree is traversed. Not that "set"
should be set _only_ if the hierachical name is known to be good,
usually (always?) by first calling this without setting "set".
If "set" is set, but the name is bad, the the scope will be screwed up
with no chance of recovery. */
tree search_scope(char *path_name, int set)
{
extern char *token_buffer; /* Will already be as large as needed */
tree scope, set_scope;
extern tree scope0; /* defined in decl.c */
char *p, *path = token_buffer;
strcpy(path, path_name);
p = strtok(path, ".");
set_scope = current_scope;
scope = search_scope_across(BLOCK_DOWN(set_scope), p, set);
if (!scope) {
scope = search_scope_up(set_scope, p, set);
if (!scope) {
if (set) {
for (set_scope = current_scope; set_scope != scope0;
set_scope = BLOCK_UP(set_scope))
current_scope = pop_scope();
}
scope = search_scope_across(top_level, p, set);
if (!scope) {
for (set_scope = current_scope;
TREE_CODE(set_scope) != MODULE_BLOCK;
set_scope = BLOCK_UP(set_scope))
if (set)
current_scope = pop_scope();
scope = search_scope_across(BLOCK_DOWN(set_scope), p, set);
if (!scope) {
error
("Component '%s' is not in up, down, or top-level path",
p, NULL_CHAR);
return error_mark_node;
}
}
}
}
for (p = strtok(NULL, "."); p; p = strtok(NULL, ".")) {
set_scope = scope;
scope = search_scope_across(BLOCK_DOWN(set_scope), p, set);
if (!scope) {
scope = search_scope_up(set_scope, p, set);
if (!scope) {
error
("Component '%s' is not in up, down, or top-level path",
p, NULL_CHAR);
return error_mark_node;
} else if (!set)
warning("Hierarchical component '%s' found on upward path",
IDENT(BLOCK_NAME(scope)), NULL_CHAR);
}
}
return scope;
}
/* Look for a decl node with the give name in the current scope. If
not there, search up the hierarchy. If found higher up, give
a warning. If not found, return error_mark_node and print error.
Don't look any higher than the first MODULE_BLOCK. */
tree search_up_decl(char *name, tree starting_scope)
{
tree scope, t, t1;
if (starting_scope == error_mark_node)
return error_mark_node;
for (scope = starting_scope; scope;
// scope = (TREE_CODE (scope) == MODULE_BLOCK ? NULL_TREE : BLOCK_UP (scope)))
scope = BLOCK_UP(scope)) {
for (t = BLOCK_DECL(scope); t; t = TREE_CHAIN(t)) {
// if (TREE_CODE (t) == ARRAY_DECL)
// t1 = ARRAY_DECL (t);
// else
t1 = t;
if (!strcmp(name, IDENT(DECL_NAME(t1)))) {
if (scope != starting_scope)
warning("Identifier '%s' found on upward path", name,
NULL_CHAR);
return t;
}
}
for (t = BLOCK_PORTS(scope); t; t = TREE_CHAIN(t)) {
if (TREE_CODE(t) == ARRAY_REF)
t1 = ARRAY_REF_DECL(t);
else
t1 = t;
if (!strcmp(name, IDENT(DECL_NAME(t1)))) {
if (scope != starting_scope)
warning("Identifier '%s' found on upward path", name,
NULL_CHAR);
return t;
}
}
}
error("Identifier '%s' not declared", name, NULL_CHAR);
return error_mark_node;
}
/* Given the current scope and a new scope, trace the path down or up
to the new scope and then call set_scope() and pop_scope() enough times
to set the new scope. This will be called only when the new scope is
in the symbol table, so it can only be one down, across, or top_level.
*/
void look_for_and_set_scope(tree scope)
{
tree s;
/* Case 1: top level - pop until top of hierarchy, then set */
if (!BLOCK_UP(scope)) {
for (s = current_scope; s; s = BLOCK_UP(s))
current_scope = pop_scope();
set_scope(scope);
}
/* Case 2: one down - simply set the scope */
else if (BLOCK_UP(scope) == current_scope)
set_scope(scope);
else
error("Scope case not handled yet", NULL_CHAR, NULL_CHAR);
}
void init_scope()
{
scope0 = NULL;
current_level = &level0;
free_level = NULL;
current_scope = NULL_TREE;
memset(&level0, 0, sizeof(level0));
}