-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrb_comp.symtab.c
519 lines (391 loc) · 12.4 KB
/
rb_comp.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* -MODULE----------------------------------------------------------------------
RapidBATCH
Copyright (C) 2008, 2009 by Phorward Software Technologies, Jan Max Meyer
http://www.phorward-software.com ++ [email protected]
File: rb_comp.symtab.c
Author: Jan Max Meyer
Usage: Symbol table management functions
----------------------------------------------------------------------------- */
/*
* Includes
*/
#include "rb_global.h"
/*
* Global variables
*/
static symbol* symtab[ SYMTAB_SIZE ];
/*
* Functions
*/
/* -FUNCTION--------------------------------------------------------------------
Function: pfree_symbol()
Author: Jan Max Meyer
Usage: Frees a symbol.
Parameters: symbol* sym Symbol pointer to be freed.
Returns: symbol* Always (symbol*)NULL
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
static symbol* pfree_symbol( symbol* sym )
{
if( !sym )
return (symbol*)NULL;
if( sym->type == SYM_VAR && sym->constant )
pfree( sym->offset.constant );
pfree( sym->name );
pfree( sym );
return (symbol*)NULL;
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_engage()
Author: Jan Max Meyer
Usage: Engages/inserts a symbol pointer into the symbol database.
Parameters: symbol* sym The symbol to be inserted.
Returns: int The bucket index of the symbol
table that got the entry.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
static int rb_symtab_engage( symbol* sym )
{
symbol* link;
symbol* last;
int idx;
PROC( "rb_symtab_engage" );
PARMS( "sym", "%p", sym );
idx = hash_from_str( sym->name, SYMTAB_SIZE );
/* If no symbol table exists, yet... */
if( !( link = symtab[ idx ] ) )
{
symtab[ idx ] = sym;
RETURN( idx );
}
/* Insert symbol with same name before existing item, if available! */
for( last = (symbol*)NULL; link; link = link->next )
{
if( !strcmp( link->name, sym->name ) )
{
sym->next = link;
if( last )
last->next = sym;
else
symtab[ idx ] = sym;
RETURN( idx );
}
last = link;
}
/* Ok, then add this to the last one! */
last->next = sym;
RETURN( idx );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_drop()
Author: Jan Max Meyer
Usage: Drops a symbol pointer from the symbol database.
Parameters: symbol* sym The symbol to be dropped.
pboolean drop_sym If true, the sym pointer is
freed.
Returns: pboolean TRUE, if the symbol was dropped,
FALSE if ot could not be found.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
static BOOLEAN rb_symtab_drop( symbol* sym, pboolean drop_sym )
{
symbol* prev = (symbol*)NULL;
symbol* link;
int idx;
PROC( "rb_symtab_drop" );
PARMS( "sym", "%p", sym );
PARMS( "drop_sym", "%d", drop_sym );
if( !sym )
RETURN( FALSE );
/* Get hash table index */
idx = hash_from_str( sym->name, SYMTAB_SIZE );
VARS( "idx", "%d", idx );
for( link = symtab[ idx ]; link; prev = link, link = link->next )
{
if( link == sym )
{
if( !prev )
symtab[ idx ] = link->next;
else
prev->next = link->next;
if( drop_sym )
pfree_symbol( sym );
RETURN( TRUE );
}
}
if( drop_sym )
pfree_symbol( sym );
RETURN( FALSE );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_find()
Author: Jan Max Meyer
Usage: Returns a symbol with the same name and type from the
symbol table, or creates a new symbol table entry and
returns this entry.
Parameters: char* name The symbol's identifying name
symtype type The symbol type
Returns: symbol* Pointer to the symbol,
(symbol*)NULL if the symbol
does not exist.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
symbol* rb_symtab_find( char* name, symtype type )
{
symbol* link;
PROC( "rb_symtab_find" );
PARMS( "name", "%s", name );
PARMS( "type", "%d", type );
if( !( link = symtab[ hash_from_str( name, SYMTAB_SIZE ) ] ) )
{
MSG( "found nothing" );
RETURN( (symbol*)NULL );
}
for( ; link; link = link->next )
{
if( !strcmp( link->name, name ) && link->type == type )
RETURN( link );
}
MSG( "found nothing" );
RETURN( (symbol*)NULL );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_find_in_scope()
Author: Jan Max Meyer
Usage: Searches for a symbol with the given name and type within
a scope, that is defined by begin.
Parameters: symbol* begin Begin node of the scope.
char* name The symbol's identifying name
symtype type The symbol type
Returns: symbol* Pointer to the symbol,
(symbol*)NULL if the symbol
does not exist in the scope.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
symbol* rb_symtab_find_in_scope( symbol* begin, char* name, symtype type )
{
PROC( "rb_symtab_find_in_scope" );
PARMS( "begin", "%p", begin );
PARMS( "name", "%s", name );
PARMS( "type", "%d", type );
for( ; begin; begin = begin->next )
{
if( !strcmp( begin->name, name ) && begin->type == type )
{
MSG( "symbol found!" );
RETURN( begin );
}
}
MSG( "found nothing" );
RETURN( (symbol*)NULL );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_new()
Author: Jan Max Meyer
Usage: Creates a new symbol table entry and inserts it into the
symbol database.
Parameters: char* name The symbol's identifying name
symtype type The symbol type
Returns: symbol* Pointer to the desired symbol,
(symbol*)NULL if the symbol
could not be created.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
symbol* rb_symtab_new( char* name, symtype type )
{
symbol* sym;
PROC( "rb_new_symbol" );
PARMS( "name", "%s", name );
PARMS( "type", "%d", type );
MSG( "Creating new symbol" );
if( !( sym = (symbol*)pmalloc( sizeof( symbol ) ) ) )
RETURN( (symbol*)NULL );
memset( sym, 0, sizeof( symbol ) );
sym->name = pstrdup( name );
sym->type = type;
MSG( "Engaging symbol" );
rb_symtab_engage( sym );
RETURN( sym );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_engage_scope()
Author: Jan Max Meyer
Usage: Engages a symbol to a scope. The scope is defined by the
begin node. If begin is (symbol*)NULL, engage becomes the
leading node of the scope.
Parameters: symbol* begin The scope begin node
symbol* engange The new symbol node to be
engaged into the scope
Returns: symbol* Always the pointer to the
scope's beginning node.
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
symbol* rb_symtab_engage_scope( symbol* begin, symbol* engage )
{
symbol* sym;
PROC( "rb_symtab_engage_scope" );
PARMS( "begin", "%p", begin );
PARMS( "engage", "%p", engage );
if( !begin )
{
MSG( "Defining begin of scope" );
begin = engage;
}
else
{
MSG( "Moving to end of scope list" );
for( sym = begin; sym->scope_next; sym = sym->scope_next )
;
sym->scope_next = engage;
}
RETURN( begin );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_drop_scope()
Author: Jan Max Meyer
Usage: Drops an entire scope.
Parameters: symbol* begin The scope begin node
pboolean drop_sym Drop the symbol's memory, if
TRUE.
Returns: symbol* Always a pointer to
(symbol*)NULL
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
symbol* rb_symtab_drop_scope( symbol* begin, pboolean drop_sym )
{
symbol* del;
PROC( "rb_symtab_drop_scope" );
PARMS( "begin", "%p", begin );
PARMS( "drop_sym", "%p", drop_sym );
while( ( del = begin ) )
{
begin = begin->scope_next;
rb_symtab_drop( del, drop_sym );
}
RETURN( (symbol*)NULL );
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_scope_pos()
Author: Jan Max Meyer
Usage: Returns the position of a symbol in the scope.
This is required to locate variable entry points
and push their address onto the stack, e.g. at
var-calls.
Parameters: symbol* begin The scope begin node
symbol* find The symbol pointer to find
Returns: int The scope length
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
int rb_symtab_scope_pos( symbol* begin, symbol* find )
{
int pos = 0;
for( ; begin; begin = begin->scope_next, pos++ )
if( begin == find )
return pos;
return -1;
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_dump()
Author: Jan Max Meyer
Usage: Dumps the symbol table content.
Parameters: FILE* stream Output stream. If (FILE*)NULL,
stderr will be used.
Returns: void
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
void rb_symtab_dump( FILE* stream )
{
int i;
symbol* sym;
symbol* parm;
plistel* e;
if( !stream )
stream = stderr;
for( i = 0; i < SYMTAB_SIZE; i++ )
{
if( ! symtab[i] )
continue;
fprintf( stream, "bucket %02d ", i );
for( sym = symtab[i]; sym; sym = sym->next )
{
if( sym != symtab[i] )
fprintf( stream, " " );
if( !( sym->native ) )
{
if( sym->used )
{
if( !( sym->defined ) )
fprintf( stream, "[implicit] " );
}
else
fprintf( stream, "[unused] " );
}
else
fprintf( stream, "[native] " );
switch( sym->type )
{
case SYM_VAR:
fprintf( stream, "%sVariable %s (offset %ld, %s scope)",
sym->pointer ? "Pointer-" : "", sym->name,
sym->offset._long, sym->global ?
"global" : "local" );
break;
case SYM_PROC:
fprintf( stream, "%s %s",
sym->function ? "Function" : "Procedure",
sym->name );
fprintf( stream, "\n Signature >%s<",
sym->parmdef );
plist_for( sym->parmsym, e )
{
parm = (symbol*)plist_access( e );
fprintf( stream, "\n Parameter [%s%s]",
parm->pointer ? "*" : "", parm->name );
}
break;
case SYM_LABEL:
fprintf( stream, "Label %s", sym->name );
break;
default:
fprintf( stream, "[Unknown symbol]" );
break;
}
fprintf( stream, "\n" );
}
}
}
/* -FUNCTION--------------------------------------------------------------------
Function: rb_symtab_drop_all()
Author: Jan Max Meyer
Usage: Drop entire symbol table database.
Parameters: keep_native Keep natives
Returns: void
~~~ CHANGES & NOTES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Date: Author: Note:
----------------------------------------------------------------------------- */
void rb_symtab_drop_all( pboolean keep_native )
{
int i;
symbol* sym;
for( i = 0; i < SYMTAB_SIZE; i++ )
{
if( ! symtab[i] )
continue;
for( sym = symtab[i]; sym; sym = sym->next )
{
if( sym->native && keep_native )
continue;
rb_symtab_drop( sym, TRUE );
}
}
}