Produce error rather than assert when encountering a nested function.

Remove extra '\n' characters from error messages.

Dynamically allocate error message buffer.
This commit is contained in:
Jack Palevich 2009-09-18 15:03:03 -07:00
parent 05647c89ef
commit 1c60e46e0e
2 changed files with 47 additions and 32 deletions

View file

@ -846,10 +846,10 @@ class Compiler : public ErrorSink {
// Operand by a small constant // Operand by a small constant
// push;mov #imm;pop;op ==> op #imm // push;mov #imm;pop;op ==> op #imm
if (mQ[-4] == 0xe92d0001 && // stmfd r13!, {r0} if (mQ[-4] == 0xe92d0001 && // stmfd r13!, {r0}
(mQ[-3] & ~immediateMask) == 0xe3a00000 && // mov r0, #X (mQ[-3] & ~immediateMask) == 0xe3a00000 && // mov r0, #X
mQ[-2] == 0xe8bd0002 && // ldmea r13!, {r1} mQ[-2] == 0xe8bd0002 && // ldmea r13!, {r1}
(mQ[-1] & ~opMask) == (0xe0810000 & ~opMask)) { // OP r0, r1, r0 (mQ[-1] & ~opMask) == (0xe0810000 & ~opMask)) { // OP r0, r1, r0
unsigned int movConst = mQ[-3]; unsigned int movConst = mQ[-3];
unsigned int op = mQ[-1]; unsigned int op = mQ[-1];
unsigned int combined = 0xe2000000 | (op & opMask) | (movConst & immediateMask); unsigned int combined = 0xe2000000 | (op & opMask) | (movConst & immediateMask);
@ -876,10 +876,10 @@ class Compiler : public ErrorSink {
mQ.popBack(2); mQ.popBack(2);
mQ.pushBack(combined); mQ.pushBack(combined);
didPeep = true; 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); unsigned int decodedImmediate = decode12BitImmediate(encodedImmediate);
if (decodedImmediate <= 1020 && ((decodedImmediate & 3) == 0)) { 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.popBack(2);
mQ.pushBack(combined); mQ.pushBack(combined);
didPeep = true; didPeep = true;
@ -916,8 +916,8 @@ class Compiler : public ErrorSink {
// Pointer arithmetic with a stride that is a power of two // Pointer arithmetic with a stride that is a power of two
if (mQ.count() >= 3 && if (mQ.count() >= 3 &&
(mQ[-3] & ~ immediateMask) == 0xe3a02000 && // mov r2, #stride (mQ[-3] & ~ immediateMask) == 0xe3a02000 && // mov r2, #stride
mQ[-2] == 0xe0000092 && // mul r0, r2, r0 mQ[-2] == 0xe0000092 && // mul r0, r2, r0
mQ[-1] == 0xe0810000) { // add r0, r1, r0 mQ[-1] == 0xe0810000) { // add r0, r1, r0
int stride = decode12BitImmediate(mQ[-3]); int stride = decode12BitImmediate(mQ[-3]);
if (isPowerOfTwo(stride)) { if (isPowerOfTwo(stride)) {
@ -4696,7 +4696,7 @@ class Compiler : public ErrorSink {
linkGlobal(t, tok == '('); linkGlobal(t, tok == '(');
n = (intptr_t) pVI->pAddress; n = (intptr_t) pVI->pAddress;
if (!n && tok != '(') { if (!n && tok != '(') {
error("Undeclared variable %s\n", nameof(t)); error("Undeclared variable %s", nameof(t));
} }
} }
if (tok != '(') { if (tok != '(') {
@ -4705,7 +4705,7 @@ class Compiler : public ErrorSink {
linkGlobal(t, false); linkGlobal(t, false);
n = (intptr_t) pVI->pAddress; n = (intptr_t) pVI->pAddress;
if (!n) { 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)) { if (checkUndeclaredStruct(pDecl)) {
break; break;
} }
int variableAddress = 0;
addLocalSymbol(pDecl); addLocalSymbol(pDecl);
size_t alignment = pGen->alignmentOf(pDecl); if (pDecl->tag == TY_FUNC) {
assert(alignment > 0); if (tok == '{') {
size_t alignmentMask = ~ (alignment - 1); error("Nested functions are not allowed. Did you forget a '}' ?");
size_t sizeOf = pGen->sizeOf(pDecl); break;
assert(sizeOf > 0); }
loc = (loc + alignment - 1) & alignmentMask; // Else it's a forward declaration of a function.
size_t alignedSize = (sizeOf + alignment - 1) & alignmentMask; } else {
loc = loc + alignedSize; int variableAddress = 0;
variableAddress = -loc; size_t alignment = pGen->alignmentOf(pDecl);
VI(pDecl->id)->pAddress = (void*) variableAddress; assert(alignment > 0);
if (accept('=')) { size_t alignmentMask = ~ (alignment - 1);
/* assignment */ size_t sizeOf = pGen->sizeOf(pDecl);
pGen->leaR0(variableAddress, createPtrType(pDecl), ET_LVALUE); assert(sizeOf > 0);
pGen->pushR0(); loc = (loc + alignment - 1) & alignmentMask;
expr(); size_t alignedSize = (sizeOf + alignment - 1) & alignmentMask;
pGen->forceR0RVal(); loc = loc + alignedSize;
pGen->storeR0ToTOS(); 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 == ',') if (tok == ',')
next(); next();

View file

@ -80,7 +80,7 @@ static int disassemble(ACCscript* script, FILE* out) {
unsigned long* pEnd = (unsigned long*) (((unsigned char*) base) + length); unsigned long* pEnd = (unsigned long*) (((unsigned char*) base) + length);
for(unsigned long* pInstruction = pBase; pInstruction < pEnd; pInstruction++) { 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); ::disasm(&di, (uint) pInstruction, 0);
} }
return 0; return 0;
@ -151,9 +151,16 @@ int main(int argc, char** argv) {
int result = accGetError(script); int result = accGetError(script);
MainPtr mainPointer = 0; MainPtr mainPointer = 0;
if (result != 0) { if (result != 0) {
char buf[1024]; ACCsizei bufferLength;
accGetScriptInfoLog(script, sizeof(buf), NULL, buf); accGetScriptInfoLog(script, 0, &bufferLength, NULL);
fprintf(stderr, "%s", buf); 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; goto exit;
} }