-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsymtab.cpp
More file actions
166 lines (147 loc) · 3.3 KB
/
symtab.cpp
File metadata and controls
166 lines (147 loc) · 3.3 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
#include <assert.h>
#include "error.h"
#include "namestore.h"
#include "symtab.h"
symtab_t::symtab_t()
{
level = -1;
NewBlock();
}
symtab_t::~symtab_t()
{
}
void symtab_t::NewBlock(symtab_entry_t *se)
{
level++;
assert(level < MAX_LEVELS); // should be error, not assert
scope_tbl[level] = se;
tempsize[level] = maxtempsize[level] = 0;
}
void symtab_t::ExitBlock()
{
symtab_entry_t *e;
assert(level>0);
level--;
}
void symtab_t::Push(int cnt)
{
tempsize[level] += cnt;
if (tempsize[level] > maxtempsize[level])
maxtempsize[level] = tempsize[level];
}
void symtab_t::Pop(int cnt)
{
tempsize[level] -= cnt;
}
symtab_entry_t* symtab_t::IsDefined(int index_in, int lvl)
{
for (symtab_entry_t *e = scope_tbl[lvl] ; e ; e = e->next)
{
if (e->index == index_in)
return e;
}
return NULL;
}
symtab_entry_t *symtab_t::Define(symtab_entry_t *e)
{
assert(e);
e->next = scope_tbl[level];
scope_tbl[level] = e;
return e;
}
symtab_entry_t *symtab_t::Define(int index_in, entry_class_t _class_in)
{
if (!IsDefined(index_in, level))
{
symtab_entry_t *e = new symtab_entry_t(_class_in, index_in);
e->next = scope_tbl[level];
scope_tbl[level] = e;
return e;
}
ErrorHandler->Error(ERR_REDEFINE, NameStore->Name(index_in));
return NULL;
}
symtab_entry_t *symtab_t::Find(int index_in)
{
symtab_entry_t *rtn;
int lvl = level;
while (lvl>=0)
{
if ((rtn = IsDefined(index_in, lvl)) != NULL)
return rtn;
lvl--;
}
ErrorHandler->Error(ERR_UNDEFINED, NameStore->Name(index_in));
return Define(index_in, UNDEFINED); // so we don't report it undefined again
}
fieldtype_t *symtab_t::FindField(recordtype_t *rt, int id)
{
for (symtab_entry_t *f = rt->LastField() ; f ; f = f->Next() )
{
assert(f->Class() == FIELD);
if (f->Index() == id)
return (fieldtype_t *)f;
}
return NULL;
}
int symtab_entry_t::Size()
{
if (Class() == STANDARDTYPE)
return 1;
else if (Class() == ARRAYTYPE)
return ((arraytype_t *)this)->Size();
else
{
assert(Class() == RECORDTYPE);
return ((recordtype_t *)this)->Size();
}
}
void symtab_entry_t::AllocateAddress(entry_class_t cls, int offset)
{
switch(Class())
{
case VARIABLE:
if (cls == VARIABLE)
((variable_t *)this)->AllocateAddress(cls, offset);
case VALUEPARAM:
if (cls == VALUEPARAM)
((valueparam_t *)this)->AllocateAddress(cls, offset);
case REFPARAM:
if (cls == VALUEPARAM)
((refparam_t *)this)->AllocateAddress(cls, offset);
case FIELD:
if (cls == FIELD)
((fieldtype_t *)this)->AllocateAddress(cls, offset);
}
}
void variable_t::AllocateAddress(entry_class_t cls, int offset)
{
level = SymTab->Level();
displ = offset - Type()->Size();
Next()->AllocateAddress(cls, displ);
}
void valueparam_t::AllocateAddress(entry_class_t cls, int offset)
{
level = SymTab->Level();
displ = offset - Type()->Size();
Next()->AllocateAddress(cls, displ);
}
void refparam_t::AllocateAddress(entry_class_t cls, int offset)
{
level = SymTab->Level();
displ = offset - 1;
Next()->AllocateAddress(cls, displ);
}
void fieldtype_t::AllocateAddress(entry_class_t cls, int offset)
{
displ = offset - Type()->Size();
Next()->AllocateAddress(cls, displ);
}
procedure_t::procedure_t(int index_in, int label_in,
symtab_entry_t *lastparam_in)
: symtab_entry_t(PROCEDURE, index_in)
{
lastparam = lastparam_in;
level = SymTab->Level();
label = label_in;
}