/
PyrSymbolTable.cpp
209 lines (177 loc) · 5.04 KB
/
PyrSymbolTable.cpp
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
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SCBase.h"
#include "PyrSymbolTable.h"
#include "clz.h"
#include <stdlib.h>
#include <string.h>
#include "InitAlloc.h"
#include "VMGlobals.h"
#include "Hash.h"
SC_DLLEXPORT_C PyrSymbol* getsym(const char *name)
{
PyrSymbol* symbol = gMainVMGlobals->symbolTable->Make(name);
if (!symbol) {
fprintf(stderr, "getsym failed '%s'\n", name);
exit(-1);
}
return symbol;
}
SC_DLLEXPORT_C PyrSymbol* getmetasym(const char *name)
{
char str[256];
strcpy(str, "Meta_");
strcat(str, name);
return getsym(str);
}
SC_DLLEXPORT_C PyrSymbol* findsym(const char *name)
{
PyrSymbol* symbol = gMainVMGlobals->symbolTable->Find(name);
return symbol;
}
SymbolSpace::SymbolSpace(AllocPool *inPool)
{
mPool = inPool;
mStringPool.Init(inPool, STRINGCHUNK, STRINGCHUNK, STRINGCHUNK/5);
mSymbolPool.Init(inPool, SYMBOLCHUNK, SYMBOLCHUNK, SYMBOLCHUNK/5);
}
PyrSymbol* SymbolSpace::NewSymbol(const char *inName, int inHash, int inLength)
{
PyrSymbol *sym;
sym = (PyrSymbol*)mSymbolPool.Alloc(sizeof(PyrSymbol));
MEMFAIL(sym);
sym->name = (char*)mStringPool.Alloc(inLength+1);
MEMFAIL(sym->name);
strcpy(sym->name, inName);
sym->hash = inHash;
sym->length = inLength;
sym->specialIndex = -1;
sym->flags = 0;
if (inName[0] >= 'A' && inName[0] <= 'Z') sym->flags |= sym_Class;
if (inLength > 1 && inName[0] == '_') sym->flags |= sym_Primitive;
if (inLength > 1 && inName[inLength-1] == '_') sym->flags |= sym_Setter;
sym->u.index = 0;
sym->classdep = NULL;
return sym;
}
SymbolTable::SymbolTable(AllocPool *inPool, int inSize)
: mPool(inPool), mSpace(inPool), mMaxItems(inSize)
{
assert(ISPOWEROFTWO(inSize));
AllocTable();
}
void SymbolTable::CopyFrom(SymbolTable& inTable)
{
MakeEmpty();
Rehash(inTable.mTable, inTable.mMaxItems);
}
int SymbolTable::StrHash(const char *inName, int *outLength)
{
return Hash(inName, outLength);
}
PyrSymbol* SymbolTable::Find(const char *inName)
{
int length;
int hash = StrHash(inName, &length);
return Find(inName, hash);
}
PyrSymbol* SymbolTable::Find(const char *inName, int inHash)
{
int index = inHash & mMask;
PyrSymbol* sym = mTable[index];
while (sym && (sym->hash != inHash || strcmp(inName, sym->name)!=0)) {
index = (index+1) & mMask;
sym = mTable[index];
}
return sym;
}
void SymbolTable::Add(PyrSymbol* inSymbol)
{
if (mNumItems + 1 > (mMaxItems>>1)) Grow();
int index = inSymbol->hash & mMask;
PyrSymbol *testSymbol = mTable[index];
while (testSymbol && testSymbol != inSymbol) {
index = (index + 1) & mMask;
testSymbol = mTable[index];
}
if (!testSymbol) { // if it is not already in the table.
mTable[index] = inSymbol;
mNumItems ++ ;
}
}
PyrSymbol* SymbolTable::MakeNew(const char *inName, int inHash, int inLength)
{
PyrSymbol* symbol = mSpace.NewSymbol(inName, inHash, inLength);
Add(symbol);
return symbol;
}
PyrSymbol* SymbolTable::Make(const char *inName)
{
int length;
int hash = StrHash(inName, &length);
PyrSymbol* symbol = Find(inName, hash);
if (!symbol) symbol = MakeNew(inName, hash, length);
return symbol;
}
void SymbolTable::MakeEmpty()
{
int size = mMaxItems * sizeof(PyrSymbol*);
memset(mTable, 0, size);
mNumItems = 0 ;
}
void SymbolTable::AllocTable()
{
int size = mMaxItems * sizeof(PyrSymbol*);
mTable = (PyrSymbol**)mPool->Alloc(size);
MEMFAIL(mTable);
MakeEmpty();
mMask = mMaxItems - 1;
}
void SymbolTable::Rehash(PyrSymbol** inTable, int inSize)
{
// rehash all entries from inTable into the new table
for (int i=0; i<inSize; ++i) {
if (inTable[i]) Add(inTable[i]);
}
}
void SymbolTable::Grow()
{
PyrSymbol **oldtable = mTable;
int oldsize = mMaxItems;
// create new table
mMaxItems += mMaxItems;
AllocTable();
Rehash(oldtable, oldsize);
mPool->Free(oldtable);
}
void SymbolTable::CheckSymbols()
{
for (int i=0; i<TableSize(); ++i) {
PyrSymbol *symbol = Get(i);
if (symbol && symbol->u.index == 0) {
int c;
c = symbol->name[0];
if (c == '_') {
post("WARNING: Primitive '%s' used but not bound\n", symbol->name);
} else if (c >= 'A' && c <= 'Z') {
post("WARNING: Symbol '%s' used but not defined as a Class\n", symbol->name);
} else if ((symbol->flags & sym_Called) && !(symbol->flags & sym_Selector)) {
post("WARNING: Method '%s' called but not defined\n", symbol->name);
}
}
}
}