Merge change 9400

* changes:
  Implement pre-increment / pre-decrement
This commit is contained in:
Android (Google) Code Review 2009-07-31 15:30:07 -07:00
commit 61c00b8203
3 changed files with 43 additions and 27 deletions

View File

@ -3859,7 +3859,12 @@ class Compiler : public ErrorSink {
} else {
pGen->genUnaryOp(a);
}
} else if (t == '(') {
} else if (c == 11) {
// pre increment / pre decrement
unary();
doIncDec(a == OP_INCREMENT, 0);
}
else if (t == '(') {
// It's either a cast or an expression
Type* pCast = acceptCastTypeDeclaration();
if (pCast) {
@ -3926,35 +3931,11 @@ class Compiler : public ErrorSink {
}
}
// load a variable
pGen->leaR0(n, createPtrType(pVI->pType), ET_LVALUE);
if (tokl == 11) {
// post inc / post dec
pGen->leaR0(n, createPtrType(pVI->pType), ET_LVALUE);
pGen->pushR0();
pGen->loadR0FromR0();
pGen->over();
int lit = 1;
if (tokc == OP_DECREMENT) {
lit = -1;
}
switch (pVI->pType->tag) {
case TY_INT:
case TY_CHAR:
case TY_POINTER:
pGen->pushR0();
pGen->li(lit);
pGen->genOp(OP_PLUS);
break;
default:
error("++/-- illegal for this type.");
break;
}
pGen->storeR0ToTOS();
pGen->popR0();
doIncDec(tokc == OP_INCREMENT, true);
next();
} else {
pGen->leaR0(n, createPtrType(pVI->pType), ET_LVALUE);
}
}
}
@ -4029,6 +4010,33 @@ class Compiler : public ErrorSink {
}
}
void doIncDec(int isInc, int isPost) {
// R0 already has the lval
checkLVal();
int lit = isInc ? 1 : -1;
pGen->pushR0();
pGen->loadR0FromR0();
int tag = pGen->getR0Type()->tag;
if (!(tag == TY_INT || tag == TY_CHAR || tag == TY_POINTER)) {
error("++/-- illegal for this type. %d", tag);
}
if (isPost) {
pGen->over();
pGen->pushR0();
pGen->li(lit);
pGen->genOp(OP_PLUS);
pGen->storeR0ToTOS();
pGen->popR0();
} else {
pGen->pushR0();
pGen->li(lit);
pGen->genOp(OP_PLUS);
pGen->over();
pGen->storeR0ToTOS();
pGen->popR0();
}
}
/* Recursive descent parser for binary operations.
*/
void binaryOp(int level) {

View File

@ -6,5 +6,9 @@ int main() {
printf("%d\n", a++);
printf("%d\n", a--);
printf("%d\n", a--);
printf("%d\n", ++a);
printf("%d\n", ++a);
printf("%d\n", --a);
printf("%d\n", --a);
return a;
}

View File

@ -293,6 +293,10 @@ result: 0""", """0.002 0.1 10""")
1
2
1
1
2
1
0
result: 0
""","""""")