Skip to content

Commit

Permalink
Make sure that lowering of test expression happens in switch
Browse files Browse the repository at this point in the history
  • Loading branch information
kant2002 committed Feb 8, 2024
1 parent 0422125 commit eb601d3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Cesium.CodeGen.Tests/CodeGenSwitchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,17 @@ public Task DeepCase() => DoTest(@"int main()
int x = 0;
switch(x) while (0) { default: break; }
}");

[Fact]
public Task LowerExpression() => DoTest(@"
int my_condition() { return 0; }
int main()
{
switch(my_condition()) {
case 0: break;
case 1:
default: break;
};
return 1;
}");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
System.Int32 <Module>::my_condition()
IL_0000: ldc.i4.0
IL_0001: ret

System.Int32 <Module>::main()
Locals:
System.Int32 V_0
IL_0000: call System.Int32 <Module>::my_condition()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ldc.i4.0
IL_0008: ceq
IL_000a: brfalse IL_0014
IL_000f: br IL_0029
IL_0014: nop
IL_0015: ldloc.0
IL_0016: ldc.i4.1
IL_0017: ceq
IL_0019: brfalse IL_0023
IL_001e: br IL_002f
IL_0023: nop
IL_0024: br IL_002f
IL_0029: nop
IL_002a: br IL_0035
IL_002f: nop
IL_0030: br IL_0035
IL_0035: nop
IL_0036: ldc.i4.1
IL_0037: ret

System.Int32 <Module>::<SyntheticEntrypoint>()
Locals:
System.Int32 V_0
IL_0000: call System.Int32 <Module>::main()
IL_0005: stloc.s V_0
IL_0007: ldloc.s V_0
IL_0009: call System.Void Cesium.Runtime.RuntimeHelpers::Exit(System.Int32)
IL_000e: ldloc.s V_0
IL_0010: ret
5 changes: 3 additions & 2 deletions Cesium.CodeGen/Ir/Lowering/BlockItemLowering.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,14 @@ void ProcessCaseStatement(CaseStatement caseStatement, BlockScope switchBlock, s
return Lower(switchScope, new ExpressionStatement(s.Expression));
}

var testExpression = s.Expression.Lower(scope);
var dbi = new DeclarationBlockItem(
new ScopedIdentifierDeclaration(
StorageClass.Auto,
new List<InitializableDeclarationInfo>
{
new(new LocalDeclarationInfo(s.Expression.GetExpressionType(scope), "$switch_tmp", null),
s.Expression)
new(new LocalDeclarationInfo(testExpression.GetExpressionType(scope), "$switch_tmp", null),
testExpression)
}));

targetStmts.Add(Lower(switchScope, dbi));
Expand Down

0 comments on commit eb601d3

Please sign in to comment.