From 1c60e46e0e5a20426e235eaede8f1cf7395f4cc0 Mon Sep 17 00:00:00 2001 From: Jack Palevich Date: Fri, 18 Sep 2009 15:03:03 -0700 Subject: Produce error rather than assert when encountering a nested function. Remove extra '\n' characters from error messages. Dynamically allocate error message buffer. --- libacc/acc.cpp | 64 +++++++++++++++++++++++++++++---------------------- libacc/tests/main.cpp | 15 ++++++++---- 2 files changed, 47 insertions(+), 32 deletions(-) (limited to 'libacc') diff --git a/libacc/acc.cpp b/libacc/acc.cpp index 6dccc5b..436f806 100644 --- a/libacc/acc.cpp +++ b/libacc/acc.cpp @@ -846,10 +846,10 @@ class Compiler : public ErrorSink { // Operand by a small constant // push;mov #imm;pop;op ==> op #imm - if (mQ[-4] == 0xe92d0001 && // stmfd r13!, {r0} - (mQ[-3] & ~immediateMask) == 0xe3a00000 && // mov r0, #X - mQ[-2] == 0xe8bd0002 && // ldmea r13!, {r1} - (mQ[-1] & ~opMask) == (0xe0810000 & ~opMask)) { // OP r0, r1, r0 + if (mQ[-4] == 0xe92d0001 && // stmfd r13!, {r0} + (mQ[-3] & ~immediateMask) == 0xe3a00000 && // mov r0, #X + mQ[-2] == 0xe8bd0002 && // ldmea r13!, {r1} + (mQ[-1] & ~opMask) == (0xe0810000 & ~opMask)) { // OP r0, r1, r0 unsigned int movConst = mQ[-3]; unsigned int op = mQ[-1]; unsigned int combined = 0xe2000000 | (op & opMask) | (movConst & immediateMask); @@ -876,10 +876,10 @@ class Compiler : public ErrorSink { mQ.popBack(2); mQ.pushBack(combined); didPeep = true; - } else if (ld == 0xedd07a00) { // ldcl p10, c7, [r0, #0x000] + } else if (ld == 0xedd07a00) { // ldcl p10, c7, [r0, #0x000] unsigned int decodedImmediate = decode12BitImmediate(encodedImmediate); if (decodedImmediate <= 1020 && ((decodedImmediate & 3) == 0)) { - unsigned int combined = (decodedImmediate >> 2) | 0xed5b7a00; // ldcl p10, c7, [r11, #-0] + unsigned int combined = (decodedImmediate >> 2) | 0xed5b7a00; // ldcl p10, c7, [r11, #-0] mQ.popBack(2); mQ.pushBack(combined); didPeep = true; @@ -916,8 +916,8 @@ class Compiler : public ErrorSink { // Pointer arithmetic with a stride that is a power of two if (mQ.count() >= 3 && - (mQ[-3] & ~ immediateMask) == 0xe3a02000 && // mov r2, #stride - mQ[-2] == 0xe0000092 && // mul r0, r2, r0 + (mQ[-3] & ~ immediateMask) == 0xe3a02000 && // mov r2, #stride + mQ[-2] == 0xe0000092 && // mul r0, r2, r0 mQ[-1] == 0xe0810000) { // add r0, r1, r0 int stride = decode12BitImmediate(mQ[-3]); if (isPowerOfTwo(stride)) { @@ -4696,7 +4696,7 @@ class Compiler : public ErrorSink { linkGlobal(t, tok == '('); n = (intptr_t) pVI->pAddress; if (!n && tok != '(') { - error("Undeclared variable %s\n", nameof(t)); + error("Undeclared variable %s", nameof(t)); } } if (tok != '(') { @@ -4705,7 +4705,7 @@ class Compiler : public ErrorSink { linkGlobal(t, false); n = (intptr_t) pVI->pAddress; if (!n) { - error("Undeclared variable %s\n", nameof(t)); + error("Undeclared variable %s", nameof(t)); } } } @@ -5577,25 +5577,33 @@ class Compiler : public ErrorSink { if (checkUndeclaredStruct(pDecl)) { break; } - int variableAddress = 0; addLocalSymbol(pDecl); - size_t alignment = pGen->alignmentOf(pDecl); - assert(alignment > 0); - size_t alignmentMask = ~ (alignment - 1); - size_t sizeOf = pGen->sizeOf(pDecl); - assert(sizeOf > 0); - loc = (loc + alignment - 1) & alignmentMask; - size_t alignedSize = (sizeOf + alignment - 1) & alignmentMask; - loc = loc + alignedSize; - variableAddress = -loc; - VI(pDecl->id)->pAddress = (void*) variableAddress; - if (accept('=')) { - /* assignment */ - pGen->leaR0(variableAddress, createPtrType(pDecl), ET_LVALUE); - pGen->pushR0(); - expr(); - pGen->forceR0RVal(); - pGen->storeR0ToTOS(); + if (pDecl->tag == TY_FUNC) { + if (tok == '{') { + error("Nested functions are not allowed. Did you forget a '}' ?"); + break; + } + // Else it's a forward declaration of a function. + } else { + int variableAddress = 0; + size_t alignment = pGen->alignmentOf(pDecl); + assert(alignment > 0); + size_t alignmentMask = ~ (alignment - 1); + size_t sizeOf = pGen->sizeOf(pDecl); + assert(sizeOf > 0); + loc = (loc + alignment - 1) & alignmentMask; + size_t alignedSize = (sizeOf + alignment - 1) & alignmentMask; + loc = loc + alignedSize; + variableAddress = -loc; + VI(pDecl->id)->pAddress = (void*) variableAddress; + if (accept('=')) { + /* assignment */ + pGen->leaR0(variableAddress, createPtrType(pDecl), ET_LVALUE); + pGen->pushR0(); + expr(); + pGen->forceR0RVal(); + pGen->storeR0ToTOS(); + } } if (tok == ',') next(); diff --git a/libacc/tests/main.cpp b/libacc/tests/main.cpp index 311fec0..e4e386f 100644 --- a/libacc/tests/main.cpp +++ b/libacc/tests/main.cpp @@ -80,7 +80,7 @@ static int disassemble(ACCscript* script, FILE* out) { unsigned long* pEnd = (unsigned long*) (((unsigned char*) base) + length); for(unsigned long* pInstruction = pBase; pInstruction < pEnd; pInstruction++) { - fprintf(out, "%08x: %08x ", (int) pInstruction, *pInstruction); + fprintf(out, "%08x: %08x ", (int) pInstruction, (int) *pInstruction); ::disasm(&di, (uint) pInstruction, 0); } return 0; @@ -151,9 +151,16 @@ int main(int argc, char** argv) { int result = accGetError(script); MainPtr mainPointer = 0; if (result != 0) { - char buf[1024]; - accGetScriptInfoLog(script, sizeof(buf), NULL, buf); - fprintf(stderr, "%s", buf); + ACCsizei bufferLength; + accGetScriptInfoLog(script, 0, &bufferLength, NULL); + char* buf = (char*) malloc(bufferLength + 1); + if (buf != NULL) { + accGetScriptInfoLog(script, bufferLength + 1, NULL, buf); + fprintf(stderr, "%s", buf); + free(buf); + } else { + fprintf(stderr, "Out of memory.\n"); + } goto exit; } -- cgit v1.1