Keep track of the current arena.
This means we don't have to pass it around as an argument.
This change was made because I'm about to start creating pointer types
during expression evaluation, and I didn't want to add an arena
argument to all the expression functions.
diff --git a/libacc/acc.cpp b/libacc/acc.cpp
index 2b0964d..a21fd74 100644
--- a/libacc/acc.cpp
+++ b/libacc/acc.cpp
@@ -3216,6 +3216,8 @@
// Arena for data that's only needed when compiling a single function
Arena mLocalArena;
+ Arena* mpCurrentArena;
+
TokenTable mTokenTable;
SymbolStack mGlobals;
SymbolStack mLocals;
@@ -3895,7 +3897,7 @@
}
} else if (t == '(') {
// It's either a cast or an expression
- Type* pCast = acceptCastTypeDeclaration(mLocalArena);
+ Type* pCast = acceptCastTypeDeclaration();
if (pCast) {
skip(')');
unary(false);
@@ -3927,8 +3929,7 @@
// t == 0 to trigger an indirect function call. Hack!
} else if (t == '&') {
VariableInfo* pVI = VI(tok);
- pGen->leaR0((int) pVI->pAddress,
- createPtrType(pVI->pType, mLocalArena));
+ pGen->leaR0((int) pVI->pAddress, createPtrType(pVI->pType));
next();
} else if (t == EOF ) {
error("Unexpected EOF.");
@@ -4123,7 +4124,7 @@
intptr_t a, n, t;
Type* pBaseType;
- if ((pBaseType = acceptPrimitiveType(mLocalArena))) {
+ if ((pBaseType = acceptPrimitiveType())) {
/* declarations */
localDeclarations(pBaseType);
} else if (tok == TOK_IF) {
@@ -4223,9 +4224,9 @@
return true;
}
- Type* createType(TypeTag tag, Type* pHead, Type* pTail, Arena& arena) {
+ Type* createType(TypeTag tag, Type* pHead, Type* pTail) {
assert(tag >= TY_INT && tag <= TY_PARAM);
- Type* pType = (Type*) arena.alloc(sizeof(Type));
+ Type* pType = (Type*) mpCurrentArena->alloc(sizeof(Type));
memset(pType, 0, sizeof(*pType));
pType->tag = tag;
pType->pHead = pHead;
@@ -4233,8 +4234,8 @@
return pType;
}
- Type* createPtrType(Type* pType, Arena& arena) {
- return createType(TY_POINTER, pType, NULL, arena);
+ Type* createPtrType(Type* pType) {
+ return createType(TY_POINTER, pType, NULL);
}
/**
@@ -4350,7 +4351,7 @@
fprintf(stderr, "%s\n", buffer.getUnwrapped());
}
- Type* acceptPrimitiveType(Arena& arena) {
+ Type* acceptPrimitiveType() {
Type* pType;
if (tok == TOK_INT) {
pType = mkpInt;
@@ -4369,16 +4370,14 @@
return pType;
}
- Type* acceptDeclaration(Type* pType, bool nameAllowed, bool nameRequired,
- Arena& arena) {
+ Type* acceptDeclaration(Type* pType, bool nameAllowed, bool nameRequired) {
tokenid_t declName = 0;
bool reportFailure = false;
pType = acceptDecl2(pType, declName, nameAllowed,
- nameRequired, arena, reportFailure);
+ nameRequired, reportFailure);
if (declName) {
// Clone the parent type so we can set a unique ID
- pType = createType(pType->tag, pType->pHead,
- pType->pTail, arena);
+ pType = createType(pType->tag, pType->pHead, pType->pTail);
pType->id = declName;
}
@@ -4390,8 +4389,8 @@
return pType;
}
- Type* expectDeclaration(Type* pBaseType, Arena& arena) {
- Type* pType = acceptDeclaration(pBaseType, true, true, arena);
+ Type* expectDeclaration(Type* pBaseType) {
+ Type* pType = acceptDeclaration(pBaseType, true, true);
if (! pType) {
error("Expected a declaration");
}
@@ -4399,16 +4398,16 @@
}
/* Used for accepting types that appear in casts */
- Type* acceptCastTypeDeclaration(Arena& arena) {
- Type* pType = acceptPrimitiveType(arena);
+ Type* acceptCastTypeDeclaration() {
+ Type* pType = acceptPrimitiveType();
if (pType) {
- pType = acceptDeclaration(pType, false, false, arena);
+ pType = acceptDeclaration(pType, false, false);
}
return pType;
}
- Type* expectCastTypeDeclaration(Arena& arena) {
- Type* pType = acceptCastTypeDeclaration(arena);
+ Type* expectCastTypeDeclaration() {
+ Type* pType = acceptCastTypeDeclaration();
if (! pType) {
error("Expected a declaration");
}
@@ -4416,22 +4415,22 @@
}
Type* acceptDecl2(Type* pType, tokenid_t& declName,
- bool nameAllowed, bool nameRequired, Arena& arena,
+ bool nameAllowed, bool nameRequired,
bool& reportFailure) {
int ptrCounter = 0;
while (accept('*')) {
ptrCounter++;
}
- pType = acceptDecl3(pType, declName, nameAllowed, nameRequired, arena,
+ pType = acceptDecl3(pType, declName, nameAllowed, nameRequired,
reportFailure);
while (ptrCounter-- > 0) {
- pType = createType(TY_POINTER, pType, NULL, arena);
+ pType = createType(TY_POINTER, pType, NULL);
}
return pType;
}
Type* acceptDecl3(Type* pType, tokenid_t& declName,
- bool nameAllowed, bool nameRequired, Arena& arena,
+ bool nameAllowed, bool nameRequired,
bool& reportFailure) {
// direct-dcl :
// name
@@ -4441,7 +4440,7 @@
Type* pNewHead = NULL;
if (accept('(')) {
pNewHead = acceptDecl2(pNewHead, declName, nameAllowed,
- nameRequired, arena, reportFailure);
+ nameRequired, reportFailure);
skip(')');
} else if ((declName = acceptSymbol()) != 0) {
if (nameAllowed == false && declName) {
@@ -4456,8 +4455,8 @@
}
while (accept('(')) {
// Function declaration
- Type* pTail = acceptArgs(nameAllowed, arena);
- pType = createType(TY_FUNC, pType, pTail, arena);
+ Type* pTail = acceptArgs(nameAllowed);
+ pType = createType(TY_FUNC, pType, pTail);
skip(')');
}
@@ -4472,16 +4471,15 @@
return pType;
}
- Type* acceptArgs(bool nameAllowed, Arena& arena) {
+ Type* acceptArgs(bool nameAllowed) {
Type* pHead = NULL;
Type* pTail = NULL;
for(;;) {
- Type* pBaseArg = acceptPrimitiveType(arena);
+ Type* pBaseArg = acceptPrimitiveType();
if (pBaseArg) {
- Type* pArg = acceptDeclaration(pBaseArg, nameAllowed, false,
- arena);
+ Type* pArg = acceptDeclaration(pBaseArg, nameAllowed, false);
if (pArg) {
- Type* pParam = createType(TY_PARAM, pArg, NULL, arena);
+ Type* pParam = createType(TY_PARAM, pArg, NULL);
if (!pHead) {
pHead = pParam;
pTail = pParam;
@@ -4498,8 +4496,8 @@
return pHead;
}
- Type* expectPrimitiveType(Arena& arena) {
- Type* pType = acceptPrimitiveType(arena);
+ Type* expectPrimitiveType() {
+ Type* pType = acceptPrimitiveType();
if (!pType) {
String buf;
decodeToken(buf, tok, true);
@@ -4534,7 +4532,7 @@
while (pBaseType) {
while (tok != ';' && tok != EOF) {
- Type* pDecl = expectDeclaration(pBaseType, mLocalArena);
+ Type* pDecl = expectDeclaration(pBaseType);
if (!pDecl) {
break;
}
@@ -4554,7 +4552,7 @@
next();
}
skip(';');
- pBaseType = acceptPrimitiveType(mLocalArena);
+ pBaseType = acceptPrimitiveType();
}
}
@@ -4607,11 +4605,11 @@
void globalDeclarations() {
while (tok != EOF) {
- Type* pBaseType = expectPrimitiveType(mGlobalArena);
+ Type* pBaseType = expectPrimitiveType();
if (!pBaseType) {
break;
}
- Type* pDecl = expectDeclaration(pBaseType, mGlobalArena);
+ Type* pDecl = expectDeclaration(pBaseType);
if (!pDecl) {
break;
}
@@ -4643,7 +4641,7 @@
if (!accept(',')) {
break;
}
- pDecl = expectDeclaration(pBaseType, mGlobalArena);
+ pDecl = expectDeclaration(pBaseType);
if (!pDecl) {
break;
}
@@ -4660,6 +4658,7 @@
} else if (tok != '{') {
error("expected '{'");
} else {
+ mpCurrentArena = &mLocalArena;
if (name) {
/* patch forward references (XXX: does not work for function
pointers) */
@@ -4688,6 +4687,7 @@
pGen->gsym(rsym);
pGen->functionExit(pDecl, a, loc);
mLocals.popLevel();
+ mpCurrentArena = &mGlobalArena;
}
}
}
@@ -4805,6 +4805,7 @@
int compile(const char* text, size_t textLength) {
int result;
+ mpCurrentArena = &mGlobalArena;
createPrimitiveTypes();
cleanup();
clear();
@@ -4843,17 +4844,17 @@
}
void createPrimitiveTypes() {
- mkpInt = createType(TY_INT, NULL, NULL, mGlobalArena);
- mkpChar = createType(TY_CHAR, NULL, NULL, mGlobalArena);
- mkpVoid = createType(TY_VOID, NULL, NULL, mGlobalArena);
- mkpFloat = createType(TY_FLOAT, NULL, NULL, mGlobalArena);
- mkpDouble = createType(TY_DOUBLE, NULL, NULL, mGlobalArena);
- mkpIntFn = createType(TY_FUNC, mkpInt, NULL, mGlobalArena);
- mkpIntPtr = createPtrType(mkpInt, mGlobalArena);
- mkpCharPtr = createPtrType(mkpChar, mGlobalArena);
- mkpFloatPtr = createPtrType(mkpFloat, mGlobalArena);
- mkpDoublePtr = createPtrType(mkpDouble, mGlobalArena);
- mkpPtrIntFn = createPtrType(mkpIntFn, mGlobalArena);
+ mkpInt = createType(TY_INT, NULL, NULL);
+ mkpChar = createType(TY_CHAR, NULL, NULL);
+ mkpVoid = createType(TY_VOID, NULL, NULL);
+ mkpFloat = createType(TY_FLOAT, NULL, NULL);
+ mkpDouble = createType(TY_DOUBLE, NULL, NULL);
+ mkpIntFn = createType(TY_FUNC, mkpInt, NULL);
+ mkpIntPtr = createPtrType(mkpInt);
+ mkpCharPtr = createPtrType(mkpChar);
+ mkpFloatPtr = createPtrType(mkpFloat);
+ mkpDoublePtr = createPtrType(mkpDouble);
+ mkpPtrIntFn = createPtrType(mkpIntFn);
}
void checkForUndefinedForwardReferences() {