diff options
| author | Android (Google) Code Review <android-gerrit@google.com> | 2009-07-31 15:30:54 -0700 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2009-07-31 15:30:54 -0700 |
| commit | 84c7966eed355e9e577b59b974154939c8c6d1bc (patch) | |
| tree | 6ec9ed1070090f79768df62e623b29e8a3f1da3c | |
| parent | 6342dbd4cddd3f6a8bbdd83a131b803a99603bab (diff) | |
| parent | 47cbea9c696c8fbeb67c66387b85f59b73d32e6d (diff) | |
| download | system_core-84c7966eed355e9e577b59b974154939c8c6d1bc.tar.gz system_core-84c7966eed355e9e577b59b974154939c8c6d1bc.tar.bz2 system_core-84c7966eed355e9e577b59b974154939c8c6d1bc.zip | |
Merge change 9402
* changes:
Support brackets for accessing array values.
| -rw-r--r-- | libacc/acc.cpp | 36 | ||||
| -rw-r--r-- | libacc/tests/data/brackets.c | 61 | ||||
| -rw-r--r-- | libacc/tests/test.py | 7 |
3 files changed, 91 insertions, 13 deletions
diff --git a/libacc/acc.cpp b/libacc/acc.cpp index 436f0812..f70c7a13 100644 --- a/libacc/acc.cpp +++ b/libacc/acc.cpp @@ -3630,7 +3630,7 @@ class Compiler : public ErrorSink { /* check for op=, valid for * / % + - << >> & ^ | */ if (ch == '=' && ((tokl >= 1 && tokl <= 3) - || tokl >=6 && tokl <= 8) ) { + || (tokl >=6 && tokl <= 8)) ) { inp(); tok = TOK_OP_ASSIGNMENT; } @@ -3879,18 +3879,7 @@ class Compiler : public ErrorSink { /* This is a pointer dereference. */ unary(); - pGen->forceR0RVal(); - Type* pR0Type = pGen->getR0Type(); - if (pR0Type->tag != TY_POINTER) { - error("Expected a pointer type."); - } else { - if (pR0Type->pHead->tag == TY_FUNC) { - t = 0; - } - if (t) { - pGen->setR0ExpressionType(ET_LVALUE); - } - } + doPointer(); } else if (t == '&') { VariableInfo* pVI = VI(tok); pGen->leaR0((int) pVI->pAddress, createPtrType(pVI->pType), @@ -3949,6 +3938,15 @@ class Compiler : public ErrorSink { // post inc / post dec doIncDec(tokc == OP_INCREMENT, true); next(); + } else if (accept('[')) { + // Array reference + pGen->forceR0RVal(); + pGen->pushR0(); + commaExpr(); + pGen->forceR0RVal(); + pGen->genOp(OP_PLUS); + doPointer(); + skip(']'); } else if (accept('(')) { /* function call */ Type* pDecl = NULL; @@ -4035,6 +4033,18 @@ class Compiler : public ErrorSink { } } + void doPointer() { + pGen->forceR0RVal(); + Type* pR0Type = pGen->getR0Type(); + if (pR0Type->tag != TY_POINTER) { + error("Expected a pointer type."); + } else { + if (pR0Type->pHead->tag != TY_FUNC) { + pGen->setR0ExpressionType(ET_LVALUE); + } + } + } + /* Recursive descent parser for binary operations. */ void binaryOp(int level) { diff --git a/libacc/tests/data/brackets.c b/libacc/tests/data/brackets.c new file mode 100644 index 00000000..bab88a2f --- /dev/null +++ b/libacc/tests/data/brackets.c @@ -0,0 +1,61 @@ +void testBrackets(int* ar, int len) { + int i; + int errors = 0; + for (i = 0; i < len; i++) { + ar[i] = i; + } + for (i = 0; i < len; i++) { + if (ar[i] != i) { + printf("error: [%d] %d != %d\n", i, ar[i], i); + errors++; + } + } + printf("Errors: %d\n", errors); +} + +void testBrackets2D(int** ar2D, int lenX, int lenY) { + int x, y; + int errors = 0; + for (x = 0; x < lenX; x++) { + for (y = 0; y < lenY; y++) { + ar2D[x][y] = x * lenY + y; + } + } + for (x = 0; x < lenX; x++) { + for (y = 0; y < lenY; y++) { + int expected = x * lenY + y; + int val = ar2D[x][y]; + if (val != expected) { + printf("error: [%d][%d] %d != %d\n", x, y, val, expected); + errors++; + } + } + } + printf("2D Errors: %d\n", errors); +} + +void testHeap() { + int* ar = (int*) malloc(100); + testBrackets(ar, 25); + free(ar); +} + +void testHeap2D() { + int lenX = 10; + int lenY = 5; + int* ar = (int*) malloc(lenX * lenY * 4); + int** ar2D = (int**) malloc(lenX * 4); + int i; + for(i = 0; i < lenX; i++) { + ar2D[i] = ar + lenY * i; + } + testBrackets2D(ar2D, lenX, lenY); + free(ar); + free(ar2D); +} + +int main() { + testHeap(); + testHeap2D(); + return 0; +} diff --git a/libacc/tests/test.py b/libacc/tests/test.py index 0249695f..4be1f4b7 100644 --- a/libacc/tests/test.py +++ b/libacc/tests/test.py @@ -368,6 +368,13 @@ return: 30 arg: 12 """) + def testBrackets(self): + self.compileCheck(["-R", "data/brackets.c"], """Executing compiled code: +Errors: 0 +2D Errors: 0 +result: 0 +""","""""") + if __name__ == '__main__': if not outputCanRun(): print "Many tests are expected to fail, because acc is not a 32-bit x86 Linux executable." |
