-
Notifications
You must be signed in to change notification settings - Fork 5
/
symtab.c
143 lines (117 loc) · 2.93 KB
/
symtab.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
#include <stdlib.h>
#include <assert.h>
#include "cc.h"
#define NBUCKETS ARRAY_SIZE(((struct table *)0)->buckets)
struct table *identifiers;
struct table *constants;
struct table *tags;
struct table *globals;
struct table *externals;
int cscope = GLOBAL;
struct table *new_table(struct table *up, int scope)
{
struct table *t = zmalloc(sizeof(struct table));
t->up = up;
t->scope = scope;
return t;
}
void free_table(struct table *t)
{
free(t);
}
void symbol_init(void)
{
globals = identifiers = new_table(NULL, GLOBAL);
constants = new_table(NULL, CONSTANT);
tags = new_table(NULL, GLOBAL);
externals = new_table(NULL, GLOBAL);
}
void enter_scope(void)
{
cscope++;
}
void exit_scope(void)
{
if (tags->scope == cscope) {
struct table *up = tags->up;
free_table(tags);
tags = up;
}
if (identifiers->scope == cscope) {
struct table *up = identifiers->up;
free_table(identifiers);
identifiers = up;
}
assert(cscope >= GLOBAL);
cscope--;
}
void foreach(struct table *tp, int level,
void (*apply) (struct symbol *, void *),
void *context)
{
struct symbol *p;
assert(tp);
while (tp && tp->scope > level)
tp = tp->up;
if (tp && tp->scope == level)
for (p = tp->all; p && p->scope == level; p = p->link)
apply(p, context);
}
bool is_current_scope(struct symbol *sym)
{
return sym->scope == cscope || (sym->scope == PARAM && cscope == LOCAL);
}
struct symbol *anonymous(struct table **tpp, int scope, int area)
{
static unsigned int i;
struct symbol *sym;
sym = install(format("@%u", ++i), tpp, scope, area);
sym->anonymous = true;
return sym;
}
struct symbol *temporary(int scope, int area)
{
static unsigned int i;
struct symbol *sym;
sym = NEWS0(struct symbol, area);
sym->name = format(".T%u", ++i);
sym->scope = scope;
sym->temporary = true;
return sym;
}
struct symbol *lookup(const char *name, struct table * table)
{
unsigned int hash;
assert(name);
hash = strhash(name) & (NBUCKETS - 1);
for (struct table *t = table; t; t = t->up)
for (struct entry *p = t->buckets[hash]; p; p = p->link)
if (name == p->sym.name)
return &p->sym;
return NULL;
}
struct symbol *install(const char *name,
struct table ** tpp, int scope, int area)
{
struct table *tp = *tpp;
unsigned int hash;
struct entry *p;
if (scope > tp->scope) {
tp = *tpp = new_table(tp, scope);
} else {
while (scope != tp->scope)
tp = tp->up;
}
assert(tp);
// entry
hash = strhash(name) & (NBUCKETS - 1);
p = NEWS0(struct entry, area);
p->sym.name = name;
p->sym.scope = scope;
p->link = tp->buckets[hash];
tp->buckets[hash] = p;
// all/link
p->sym.link = tp->all;
tp->all = &p->sym;
return &p->sym;
}