ForNeVeR / Cesium

C compiler for the CLI platform
MIT License
381 stars 42 forks source link

Postfix operator: improve codegen #677

Closed evgTSV closed 2 weeks ago

evgTSV commented 3 weeks ago

Closes #648

Now, using dup instruction to create duplicate and apply operator to it, PostfixIncrementDecrementExpression returns old value (before operator)

I added new types of expression:

Codegen diff example:

int main()
{
    int x = 0;
    if (x++ == 0) { return 777; }
    return x;
}

Was:

System.Int32 <Module>::main()
  Locals:
    System.Int32 V_0
  IL_0000: ldc.i4.0
  IL_0001: stloc.0
  IL_0002: ldloc.0
  IL_0003: ldc.i4.1
  IL_0004: add
  IL_0005: stloc.0
  IL_0006: ldloc.0
  IL_0007: pop
  IL_0008: ldloc.0
  IL_0009: ldc.i4.1
  IL_000a: sub
  IL_000b: ldc.i4.0
  IL_000c: ceq
  IL_000e: brfalse IL_0019
  IL_0013: ldc.i4 777
  IL_0018: ret
  IL_0019: nop
  IL_001a: ldloc.0
  IL_001b: ret

Now:

System.Int32 <Module>::main()
  Locals:
    System.Int32 V_0
  IL_0000: ldc.i4.0
  IL_0001: stloc.0
  IL_0002: ldloc.0
  IL_0003: dup
  IL_0004: ldc.i4.1
  IL_0005: add
  IL_0006: stloc.0
  IL_0007: ldc.i4.0
  IL_0008: ceq
  IL_000a: brfalse IL_0015
  IL_000f: ldc.i4 777
  IL_0014: ret
  IL_0015: nop
  IL_0016: ldloc.0
  IL_0017: ret
kant2002 commented 3 weeks ago

Before we have this PR, I see issue like that

char s[1024];
int i, c;
for (i=0; (c = getchar()) != '\n'; i++)
    s[i] = c;
s[i] = '\0';

Currently last character from input eaten up. image

Can you please verify, or better somehow add integration test which capture that this change fix an issue (at least I hope so)