diff --git a/Cesium.CodeGen.Tests/verified/CodeGenIncrementDecrementStatementTests.PostfixIncrementVariable.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenIncrementDecrementStatementTests.PostfixIncrementVariable.verified.txt index 525315cd..effa225e 100644 --- a/Cesium.CodeGen.Tests/verified/CodeGenIncrementDecrementStatementTests.PostfixIncrementVariable.verified.txt +++ b/Cesium.CodeGen.Tests/verified/CodeGenIncrementDecrementStatementTests.PostfixIncrementVariable.verified.txt @@ -6,7 +6,13 @@ IL_0002: add IL_0003: stloc.0 IL_0004: ldloc.0 - IL_0005: ret + IL_0005: pop + IL_0006: ldloc.0 + IL_0007: ldc.i4.1 + IL_0008: sub + IL_0009: pop + IL_000a: ldloc.0 + IL_000b: ret System.Int32 ::() Locals: diff --git a/Cesium.CodeGen.Tests/verified/CodeGenOperatorTests.IncrementAsValue.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenOperatorTests.IncrementAsValue.verified.txt index e50aad88..db6e3b68 100644 --- a/Cesium.CodeGen.Tests/verified/CodeGenOperatorTests.IncrementAsValue.verified.txt +++ b/Cesium.CodeGen.Tests/verified/CodeGenOperatorTests.IncrementAsValue.verified.txt @@ -8,9 +8,13 @@ IL_0004: add IL_0005: stloc.0 IL_0006: ldloc.0 - IL_0007: stloc.0 - IL_0008: ldc.i4.0 - IL_0009: ret + IL_0007: pop + IL_0008: ldloc.0 + IL_0009: ldc.i4.1 + IL_000a: sub + IL_000b: stloc.0 + IL_000c: ldc.i4.0 + IL_000d: ret System.Int32 ::() Locals: diff --git a/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementPrimitivePointer.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementPrimitivePointer.verified.txt index e7b12fde..e36c104b 100644 --- a/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementPrimitivePointer.verified.txt +++ b/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementPrimitivePointer.verified.txt @@ -5,4 +5,10 @@ IL_0003: mul IL_0004: add IL_0005: starg.s x - IL_0007: ret + IL_0007: ldarg.0 + IL_0008: pop + IL_0009: ldarg.0 + IL_000a: ldc.i4.1 + IL_000b: sub + IL_000c: pop + IL_000d: ret diff --git a/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementStructPointer.verified.txt b/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementStructPointer.verified.txt index a7bae49a..32e92b94 100644 --- a/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementStructPointer.verified.txt +++ b/Cesium.CodeGen.Tests/verified/CodeGenPointersTests.IncrementStructPointer.verified.txt @@ -7,4 +7,10 @@ IL_0008: mul IL_0009: add IL_000a: stloc.0 - IL_000b: ret + IL_000b: ldloc.0 + IL_000c: pop + IL_000d: ldloc.0 + IL_000e: ldc.i4.1 + IL_000f: sub + IL_0010: pop + IL_0011: ret diff --git a/Cesium.CodeGen/Ir/Expressions/PostfixIncrementDecrementExpression.cs b/Cesium.CodeGen/Ir/Expressions/PostfixIncrementDecrementExpression.cs index b748832a..73da5e47 100644 --- a/Cesium.CodeGen/Ir/Expressions/PostfixIncrementDecrementExpression.cs +++ b/Cesium.CodeGen/Ir/Expressions/PostfixIncrementDecrementExpression.cs @@ -36,11 +36,15 @@ public IExpression Lower(IDeclarationScope scope) throw new CompilationException($"'{_prefixOperator.Text}' needs l-value"); } - return new AssignmentExpression( + return new CommaExpression(new AssignmentExpression( valueTarget, AssignmentOperator.Assign, newValueExpression - ).Lower(scope); + ).Lower(scope), new BinaryOperatorExpression( + target, + GetReverseOperator(), + new ConstantLiteralExpression(new IntegerConstant("1")) + )); } public void EmitTo(IEmitScope scope) => throw new AssertException("Should be lowered"); @@ -53,4 +57,11 @@ public IExpression Lower(IDeclarationScope scope) CTokenType.Decrement => BinaryOperator.Subtract, _ => throw new AssertException($"Token type {token.Kind} is invalid"), }; + + private BinaryOperator GetReverseOperator() => _operator switch + { + BinaryOperator.Add => BinaryOperator.Subtract, + BinaryOperator.Subtract => BinaryOperator.Add, + _ => throw new AssertException($"Operator {_operator} is invalid"), + }; } diff --git a/Cesium.IntegrationTests/value_assignments.c b/Cesium.IntegrationTests/value_assignments.c index c56dbd22..52fb5202 100644 --- a/Cesium.IntegrationTests/value_assignments.c +++ b/Cesium.IntegrationTests/value_assignments.c @@ -4,5 +4,9 @@ int main() int j = i++; + int a = 10; + if (a++ != 10) return -1; + if (++a != 12) return -2; + return i += 41; }