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
|
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include "semantics.h"
#define NEW(type) ((type *)malloc(sizeof(type)))
#ifdef CIBIC_DEBUG
CTable_t ctable_create(Hashfunc_t hfunc, Printfunc_t pfunc) {
CTable_t ct = NEW(CTable);
memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
ct->hfunc = hfunc;
ct->pfunc = pfunc;
return ct;
}
#else
CTable_t ctable_create(Hashfunc_t hfunc) {
CTable_t ct = NEW(CTable);
memset(ct->head, 0, sizeof(CTNode*) * MAX_TABLE_SIZE);
ct->hfunc = hfunc;
return ct;
}
#endif
void *ctable_lookup(CTable_t ct, const char *key) {
unsigned int hv = (ct->hfunc(key)) % MAX_TABLE_SIZE;
CTNode *p = ct->head[hv];
for (; p; p = p->next)
if (strcmp(p->key, key))
return p->val;
return NULL; /* not found */
}
void ctable_insert(CTable_t ct, const char *key, void *val, int lvl) {
unsigned int hv = (ct->hfunc(key)) % MAX_TABLE_SIZE;
CTNode *np = NEW(CTNode);
np->key = key;
np->val = val;
np->lvl = lvl;
np->next = ct->head[hv];
ct->head[hv] = np;
}
void ctable_clip(CTable_t ct, unsigned int hv, int max_lvl) {
CTNode *p = ct->head[hv], *np;
for (; p && p->lvl > max_lvl; p = np)
{
np = p->next;
free(p);
}
ct->head[hv] = p;
}
CScope_t cscope_create() {
CScope_t p = NEW(CScope);
p->lvl = -1;
p->top = NULL;
#ifdef CIBIC_DEBUG
p->tvar = ctable_create(bkdr_hash, cvar_print);
p->ttype = ctable_create(bkdr_hash, ctype_print);
#else
p->tvar = ctable_create(bkdr_hash);
p->ttype = ctable_create(bkdr_hash);
#endif
cscope_enter(p);
return p;
}
void cscope_push_var(CScope_t cs, CVar *var) {
#ifdef CIBIC_DEBUG
assert(cs->top);
#endif
var->next = cs->top->vhead;
cs->top->vhead = var;
ctable_insert(cs->tvar, var->name, var, cs->lvl);
}
void cscope_push_type(CScope_t cs, CType *type) {
#ifdef CIBIC_DEBUG
assert(cs->top);
#endif
type->next = cs->top->thead;
cs->top->thead = type;
ctable_insert(cs->ttype, type->name, type, cs->lvl);
}
void cscope_enter(CScope_t cs) {
CSNode *np = NEW(CSNode);
np->next = cs->top;
np->vhead = NULL;
np->thead = NULL;
cs->top = np;
cs->lvl++;
}
void cscope_exit(CScope_t cs) {
CSNode *top_o = cs->top;
CVar *vp;
CType *tp;
cs->lvl--;
cs->top = top_o->next;
for (vp = top_o->vhead; vp; vp = vp->next)
ctable_clip(cs->tvar, bkdr_hash(vp->name), cs->lvl);
for (tp = top_o->thead; tp; tp = tp->next)
ctable_clip(cs->ttype, bkdr_hash(tp->name), cs->lvl);
free(top_o);
}
void ctable_debug_print(CTable_t ct) {
int i;
fprintf(stderr, "\n*** CTable ***\n");
for (i = 0; i < MAX_TABLE_SIZE; i++)
if (ct->head[i])
{
CTNode *p;
fprintf(stderr, "[%04d]", i);
for (p = ct->head[i]; p; p = p->next)
fprintf(stderr, "->[%s:%d]", ct->pfunc(p), p->lvl);
fprintf(stderr, "\n");
}
fprintf(stderr, "*** CTable ***\n\n");
}
void cscope_debug_print(CScope_t cs) {
int lvl = cs->lvl;
CSNode *p;
fprintf(stderr, "****** CScope ******\n");
for (p = cs->top; p; p = p->next)
{
CVar *vp;
CType *tp;
fprintf(stderr, "Level %d:\n", lvl--);
fprintf(stderr, "Vars: ");
for (vp = p->vhead; vp; vp = vp->next)
fprintf(stderr, "%s ", vp->name);
fprintf(stderr, "\nTypes: ");
for (tp = p->thead; tp; tp = tp->next)
fprintf(stderr, "%s ", tp->name);
fprintf(stderr, "\n\n");
}
fprintf(stderr, "Var Table:\n");
ctable_debug_print(cs->tvar);
fprintf(stderr, "Type Table:\n");
ctable_debug_print(cs->ttype);
fprintf(stderr, "****** CScope ******\n");
}
CVar *cscope_lookup_var(CScope_t cs, const char *name) {
return ctable_lookup(cs->tvar, name);
}
CType *cscope_lookup_type(CScope_t cs, const char *name) {
return ctable_lookup(cs->ttype, name);
}
unsigned int bkdr_hash(const char *str) {
unsigned int seed = 131;
unsigned int hv = 0;
while (*str)
hv = hv * seed + (unsigned)(*str++);
return hv % 5;
}
const char *cvar_print(void *var) {
static char buff[MAX_DEBUG_PRINT_BUFF];
sprintf(buff, "%s", ((CVar *)var)->name);
return buff;
}
const char *ctype_print(void *type) {
static char buff[MAX_DEBUG_PRINT_BUFF];
sprintf(buff, "%s", ((CType *)type)->name);
return buff;
}
CVar_t cvar_create(const char *name, CType *type) {
CVar *cv = NEW(CVar);
cv->name = name;
cv->type = type;
return cv;
}
CType_t ctype_create(const char *name, int type) {
CType *ct = NEW(CType);
ct->name = name;
ct->type = type;
return ct;
}
|