Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify symbol table: now a stack with locals on top hiding globals … #28

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 23 additions & 26 deletions c4.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ char *p, *lp, // current position in source code

int *e, *le, // current position in emitted code
*id, // currently parsed identifier
*sym, // symbol table (simple list of identifiers)
*sym, // symbol table (stack of identifiers, locals on top)
*symtop, // next empty entry in symbol table
tk, // current token
ival, // current token value
ty, // current expression type
Expand All @@ -42,7 +43,7 @@ enum { LEA ,IMM ,JMP ,JSR ,BZ ,BNZ ,ENT ,ADJ ,LEV ,LI ,LC ,SI ,SC ,PSH ,
enum { CHAR, INT, PTR };

// identifier offsets (since we can't create an ident struct)
enum { Tk, Hash, Name, Class, Type, Val, HClass, HType, HVal, Idsz };
enum { Tk, Hash, Name, Class, Type, Val, Idsz };

void next()
{
Expand Down Expand Up @@ -70,15 +71,16 @@ void next()
pp = p - 1;
while ((*p >= 'a' && *p <= 'z') || (*p >= 'A' && *p <= 'Z') || (*p >= '0' && *p <= '9') || *p == '_')
tk = tk * 147 + *p++;
tk = (tk << 6) + (p - pp);
id = sym;
while (id[Tk]) {
if (tk == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; }
id = id + Idsz;
symtop[Hash] = ival = (tk << 6) + (p - pp);
symtop[Tk] = tk = Id;
symtop[Name] = (int)pp;
symtop[Class] = 0;
id = symtop - Idsz;
while (id >= sym) {
if (ival == id[Hash] && !memcmp((char *)id[Name], pp, p - pp)) { tk = id[Tk]; return; }
id = id - Idsz;
}
id[Name] = (int)pp;
id[Hash] = tk;
tk = id[Tk] = Id;
id = symtop; symtop = symtop + Idsz;
return;
}
else if (tk >= '0' && tk <= '9') {
Expand Down Expand Up @@ -343,7 +345,7 @@ int main(int argc, char **argv)
if ((fd = open(*argv, 0)) < 0) { printf("could not open(%s)\n", *argv); return -1; }

poolsz = 256*1024; // arbitrary size
if (!(sym = malloc(poolsz))) { printf("could not malloc(%d) symbol area\n", poolsz); return -1; }
if (!(symtop = sym = malloc(poolsz))) { printf("could not malloc(%d) symbol area\n", poolsz); return -1; }
if (!(le = e = malloc(poolsz))) { printf("could not malloc(%d) text area\n", poolsz); return -1; }
if (!(data = malloc(poolsz))) { printf("could not malloc(%d) data area\n", poolsz); return -1; }
if (!(sp = malloc(poolsz))) { printf("could not malloc(%d) stack area\n", poolsz); return -1; }
Expand Down Expand Up @@ -400,6 +402,7 @@ int main(int argc, char **argv)
next();
id[Type] = ty;
if (tk == '(') { // function
t = symtop;
id[Class] = Fun;
id[Val] = (int)(e + 1);
next(); i = 0;
Expand All @@ -410,9 +413,10 @@ int main(int argc, char **argv)
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk != Id) { printf("%d: bad parameter declaration\n", line); return -1; }
if (id[Class] == Loc) { printf("%d: duplicate parameter definition\n", line); return -1; }
id[HClass] = id[Class]; id[Class] = Loc;
id[HType] = id[Type]; id[Type] = ty;
id[HVal] = id[Val]; id[Val] = i++;
if (id[Class] == Glo) { id = symtop; symtop = symtop + Idsz; }
id[Class] = Loc;
id[Type] = ty;
id[Val] = i++;
next();
if (tk == ',') next();
}
Expand All @@ -428,9 +432,10 @@ int main(int argc, char **argv)
while (tk == Mul) { next(); ty = ty + PTR; }
if (tk != Id) { printf("%d: bad local declaration\n", line); return -1; }
if (id[Class] == Loc) { printf("%d: duplicate local definition\n", line); return -1; }
id[HClass] = id[Class]; id[Class] = Loc;
id[HType] = id[Type]; id[Type] = ty;
id[HVal] = id[Val]; id[Val] = ++i;
if (id[Class] == Glo) { id = symtop; symtop = symtop + Idsz; }
id[Class] = Loc;
id[Type] = ty;
id[Val] = ++i;
next();
if (tk == ',') next();
}
Expand All @@ -439,15 +444,7 @@ int main(int argc, char **argv)
*++e = ENT; *++e = i - loc;
while (tk != '}') stmt();
*++e = LEV;
id = sym; // unwind symbol table locals
while (id[Tk]) {
if (id[Class] == Loc) {
id[Class] = id[HClass];
id[Type] = id[HType];
id[Val] = id[HVal];
}
id = id + Idsz;
}
symtop = t; // unwind symbol table locals
}
else {
id[Class] = Glo;
Expand Down