Skip to content

Commit 97608ac

Browse files
committed
started work on DAT namespaces
1 parent 561cc3d commit 97608ac

File tree

9 files changed

+83
-7
lines changed

9 files changed

+83
-7
lines changed

ast.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,8 @@ static const char *astnames[] = {
891891
"ditto_start",
892892
"ditto_end",
893893
"ditto_count",
894+
895+
"namespace",
894896
};
895897

896898
//

ast.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ enum astkind {
254254
AST_DITTO_END = 183,
255255
AST_DITTO_COUNT = 184,
256256

257+
AST_NAMESPACE = 185,
257258
};
258259

259260
/* forward reference */

backends/dat/outdat.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ outputDataList(Flexbuf *f, int size, AST *ast, Flexbuf *relocs, int checkSize)
732732
int32_t relocOff = 0;
733733
int origsize = size;
734734
int checkThis = 0;
735-
735+
736736
origval = 0;
737737
while (ast) {
738738
sub = ast->left;
@@ -2066,6 +2066,7 @@ PrintDataBlock(Flexbuf *f, AST *list, DataBlockOutFuncs *funcs, Flexbuf *relocs)
20662066

20672067
if (gl_errors != 0)
20682068
return;
2069+
current->name_space = NULL;
20692070
top = list;
20702071
while (top) {
20712072
ast = top->left;
@@ -2182,6 +2183,18 @@ PrintDataBlock(Flexbuf *f, AST *list, DataBlockOutFuncs *funcs, Flexbuf *relocs)
21822183
// emit a debug entry
21832184
SendOneComment(relocs, ast, flexbuf_curlen(f), 0);
21842185
break;
2186+
case AST_NAMESPACE: {
2187+
const char *name = NULL;
2188+
if (ast->left) {
2189+
name = GetIdentifierName(ast->left);
2190+
}
2191+
if (name) {
2192+
current->name_space = GetNamespace(&current->objsyms, name);
2193+
} else {
2194+
current->name_space = NULL;
2195+
}
2196+
break;
2197+
}
21852198
default:
21862199
ERROR(ast, "unknown element in data block");
21872200
break;

expr.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ LookupGlobalSymbol(const char *name)
147147
if (!IsSpinLang(lang)) {
148148
Module *top = GetTopLevelModule();
149149
if (top) {
150-
Symbol *s = LookupSymbolInTable(&top->objsyms, name);
150+
SymbolTable *tab = top->name_space;
151+
if (!tab) tab = &top->objsyms;
152+
Symbol *s = LookupSymbolInTable(tab, name);
151153
if (s) {
152154
return s;
153155
}
@@ -163,6 +165,8 @@ LookupSymbolInFunc(Function *func, const char *name)
163165

164166
if (func) {
165167
sym = LookupSymbolInTable(&func->localsyms, name);
168+
} else if (current->name_space) {
169+
sym = LookupSymbolInTable(current->name_space, name);
166170
} else {
167171
sym = LookupSymbolInTable(&current->objsyms, name);
168172
}

frontends/common.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,12 @@ struct modulestate {
576576
/* lexer state during input */
577577
LexStream *Lptr;
578578

579-
/* the symbol table */
579+
/* the main symbol table */
580580
SymbolTable objsyms;
581581

582+
/* the currently open namespace in a DAT section (or NULL if none) */
583+
SymbolTable *name_space;
584+
582585
/* size of variables & objects used */
583586
int varsize;
584587

frontends/spin/spin.y

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
%pure-parser
8-
%expect 43
8+
%expect 44
99

1010
%{
1111
#include <stdio.h>
@@ -1447,8 +1447,23 @@ basedatline:
14471447
ast = NewAST(AST_COMMENTEDNODE, ast, $1);
14481448
$$ = ast;
14491449
}
1450+
| SP_NAMESP optidentifier
1451+
{
1452+
$$ = NewAST(AST_NAMESPACE, $2, NULL);
1453+
}
14501454
;
14511455

1456+
optidentifier:
1457+
/* empty */
1458+
{
1459+
$$ = NULL;
1460+
}
1461+
| SP_IDENTIFIER
1462+
{
1463+
$$ = $1;
1464+
}
1465+
;
1466+
14521467
objblock:
14531468
objline
14541469
{ $$ = $1; }

pasm.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ ExpandDittos(AST *instrlist)
737737
}
738738

739739
void
740-
AssignAddresses(PASMAddresses *addr, SymbolTable *symtab, AST *instrlist, int startFlags)
740+
AssignAddresses(PASMAddresses *addr, SymbolTable *orig_symtab, AST *instrlist, int startFlags)
741741
{
742742
unsigned cogpc = 0;
743743
unsigned hubpc = 0;
@@ -762,10 +762,12 @@ AssignAddresses(PASMAddresses *addr, SymbolTable *symtab, AST *instrlist, int st
762762
expect_undefined_labels = 1;
763763
unsigned asm_nest;
764764
AsmState state[MAX_ASM_NEST] = { 0 };
765-
765+
SymbolTable *symtab;
766+
766767
ExpandDittos(instrlist);
767768

768769
again:
770+
symtab = orig_symtab;
769771
labels_changed = 0;
770772

771773
cogpc = coglimit = hublimit = 0;
@@ -1041,6 +1043,17 @@ AssignAddresses(PASMAddresses *addr, SymbolTable *symtab, AST *instrlist, int st
10411043
lasttype = ast->left;
10421044
}
10431045
break;
1046+
case AST_NAMESPACE: {
1047+
const char *name;
1048+
pendingLabels = emitPendingLabels(symtab, pendingLabels, hubpc, cogpc, lasttype, lastOrg, inHub, label_flags, pass);
1049+
if (!ast->left) {
1050+
symtab = orig_symtab;
1051+
} else {
1052+
name = GetIdentifierName(ast->left);
1053+
symtab = GetNamespace(orig_symtab, name);
1054+
}
1055+
break;
1056+
}
10441057
case AST_COMMENT:
10451058
case AST_SRCCOMMENT:
10461059
break;

symbol.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,21 @@ DumpSymbolTable(SymbolTable *table)
320320
{
321321
IterateOverSymbols(table, PrintSymbol, NULL);
322322
}
323+
324+
SymbolTable *
325+
GetNamespace(SymbolTable *orig, const char *name)
326+
{
327+
SymbolTable *p;
328+
Symbol *sym;
329+
330+
sym = LookupSymbolInTable(orig, name);
331+
if (sym && sym->kind == SYM_NAMESPACE) {
332+
p = (SymbolTable *)sym->v.ptr;
333+
} else {
334+
p = (SymbolTable *)calloc(1, sizeof(SymbolTable));
335+
p->flags = orig->flags;
336+
p->next = NULL;
337+
sym = AddSymbol(orig, name, SYM_NAMESPACE, p, name);
338+
}
339+
return p;
340+
}

symbol.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef enum symtype {
3030
SYM_REDEF, /* name redefinition, used for implementing static */
3131
SYM_ALIAS, /* makes a name an alias for an expression */
3232
SYM_TEMPLATE, /* points to a function or class template */
33+
SYM_NAMESPACE, /* points to a symbol namespace */
3334
} Symtype;
3435

3536
#define IsAlias(sym) ((sym)->kind == SYM_WEAK_ALIAS)
@@ -116,5 +117,11 @@ int SetTempVariableBase(int base, int max);
116117
*/
117118
typedef int (*SymbolFunc)(Symbol *sym, void *arg);
118119

119-
void IterateOverSymbols(SymbolTable *table, SymbolFunc func, void *arg);
120+
void IterateOverSymbols(SymbolTable *table, SymbolFunc func, void *arg);
121+
122+
/*
123+
* fetch or create a namespace as a child of "orig"
124+
*/
125+
SymbolTable *GetNamespace(SymbolTable *orig, const char *name);
126+
120127
#endif

0 commit comments

Comments
 (0)