-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A lot of noise comes from fact that I distiguish between constant literals and constant expressions. We incorrectly name ConstantExpresion the expression which works with constants, but that's more broad concept. In the spec names are `constant` and `constant-expression` and because we have suffix Expression to all expression we have a clash. Closes #74 and superseeds #139
- Loading branch information
Showing
88 changed files
with
1,069 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using JetBrains.Annotations; | ||
|
||
namespace Cesium.CodeGen.Tests; | ||
|
||
public class CodeGenSwitchTests : CodeGenTestBase | ||
{ | ||
[MustUseReturnValue] | ||
private static Task DoTest(string source) | ||
{ | ||
var assembly = GenerateAssembly(default, source); | ||
|
||
var moduleType = assembly.Modules.Single().GetType("<Module>"); | ||
return VerifyMethods(moduleType); | ||
} | ||
|
||
[Fact] | ||
public Task Empty() => DoTest(@"int main() | ||
{ | ||
int x = 0; | ||
switch(x) { }; | ||
return 1; | ||
}"); | ||
|
||
[Fact] | ||
public Task OneCase() => DoTest(@"int main() | ||
{ | ||
int x = 0; | ||
switch(x) { case 0: break; }; | ||
return 1; | ||
}"); | ||
|
||
[Fact] | ||
public Task MultiCases() => DoTest(@"int main() | ||
{ | ||
int x = 0; | ||
switch(x) { | ||
case 0: break; | ||
case 1: break; | ||
}; | ||
}"); | ||
|
||
[Fact] | ||
public Task MultiCasesWithDefault() => DoTest(@"int main() | ||
{ | ||
int x = 0; | ||
switch(x) { | ||
case 0: break; | ||
case 1: break; | ||
default: break; | ||
} | ||
}"); | ||
|
||
[Fact] | ||
public Task FallthroughCase() => DoTest(@"int main() | ||
{ | ||
int x = 0; | ||
switch(x) { | ||
case 0: break; | ||
case 1: | ||
default: break; | ||
} | ||
}"); | ||
} |
22 changes: 22 additions & 0 deletions
22
Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.Empty.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
System.Int32 <Module>::main() | ||
Locals: | ||
System.Int32 V_0 | ||
System.Int32 V_1 | ||
IL_0000: ldc.i4.0 | ||
IL_0001: stloc.0 | ||
IL_0002: ldloc.0 | ||
IL_0003: stloc V_1 | ||
IL_0007: br IL_0000 | ||
IL_000c: nop | ||
IL_000d: ldc.i4.1 | ||
IL_000e: 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 |
35 changes: 35 additions & 0 deletions
35
Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.FallthroughCase.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
System.Int32 <Module>::main() | ||
Locals: | ||
System.Int32 V_0 | ||
System.Int32 V_1 | ||
IL_0000: ldc.i4.0 | ||
IL_0001: stloc.0 | ||
IL_0002: ldloc.0 | ||
IL_0003: stloc V_1 | ||
IL_0007: ldloc V_1 | ||
IL_000b: ldc.i4.0 | ||
IL_000c: ceq | ||
IL_000e: brtrue IL_0024 | ||
IL_0013: ldloc V_1 | ||
IL_0017: ldc.i4.1 | ||
IL_0018: ceq | ||
IL_001a: brtrue IL_002a | ||
IL_001f: br IL_002b | ||
IL_0024: nop | ||
IL_0025: br IL_0031 | ||
IL_002a: nop | ||
IL_002b: nop | ||
IL_002c: br IL_0031 | ||
IL_0031: nop | ||
IL_0032: ldc.i4.0 | ||
IL_0033: 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 |
34 changes: 34 additions & 0 deletions
34
Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.MultiCases.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
System.Int32 <Module>::main() | ||
Locals: | ||
System.Int32 V_0 | ||
System.Int32 V_1 | ||
IL_0000: ldc.i4.0 | ||
IL_0001: stloc.0 | ||
IL_0002: ldloc.0 | ||
IL_0003: stloc V_1 | ||
IL_0007: ldloc V_1 | ||
IL_000b: ldc.i4.0 | ||
IL_000c: ceq | ||
IL_000e: brtrue IL_0024 | ||
IL_0013: ldloc V_1 | ||
IL_0017: ldc.i4.1 | ||
IL_0018: ceq | ||
IL_001a: brtrue IL_002a | ||
IL_001f: br IL_0000 | ||
IL_0024: nop | ||
IL_0025: br IL_0030 | ||
IL_002a: nop | ||
IL_002b: br IL_0030 | ||
IL_0030: nop | ||
IL_0031: ldc.i4.0 | ||
IL_0032: 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 |
36 changes: 36 additions & 0 deletions
36
Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.MultiCasesWithDefault.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
System.Int32 <Module>::main() | ||
Locals: | ||
System.Int32 V_0 | ||
System.Int32 V_1 | ||
IL_0000: ldc.i4.0 | ||
IL_0001: stloc.0 | ||
IL_0002: ldloc.0 | ||
IL_0003: stloc V_1 | ||
IL_0007: ldloc V_1 | ||
IL_000b: ldc.i4.0 | ||
IL_000c: ceq | ||
IL_000e: brtrue IL_0024 | ||
IL_0013: ldloc V_1 | ||
IL_0017: ldc.i4.1 | ||
IL_0018: ceq | ||
IL_001a: brtrue IL_002a | ||
IL_001f: br IL_0030 | ||
IL_0024: nop | ||
IL_0025: br IL_0036 | ||
IL_002a: nop | ||
IL_002b: br IL_0036 | ||
IL_0030: nop | ||
IL_0031: br IL_0036 | ||
IL_0036: nop | ||
IL_0037: ldc.i4.0 | ||
IL_0038: 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 |
28 changes: 28 additions & 0 deletions
28
Cesium.CodeGen.Tests/verified/CodeGenSwitchTests.OneCase.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
System.Int32 <Module>::main() | ||
Locals: | ||
System.Int32 V_0 | ||
System.Int32 V_1 | ||
IL_0000: ldc.i4.0 | ||
IL_0001: stloc.0 | ||
IL_0002: ldloc.0 | ||
IL_0003: stloc V_1 | ||
IL_0007: ldloc V_1 | ||
IL_000b: ldc.i4.0 | ||
IL_000c: ceq | ||
IL_000e: brtrue IL_0018 | ||
IL_0013: br IL_0000 | ||
IL_0018: nop | ||
IL_0019: br IL_001e | ||
IL_001e: nop | ||
IL_001f: ldc.i4.1 | ||
IL_0020: 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Cesium.CodeGen.Ir.Expressions; | ||
using Cesium.CodeGen.Ir.Expressions.Constants; | ||
using Cesium.Core; | ||
|
||
namespace Cesium.CodeGen; | ||
|
||
internal class ConstantEvaluator | ||
{ | ||
private readonly IExpression _expression; | ||
|
||
public ConstantEvaluator(IExpression expression) | ||
{ | ||
_expression = expression; | ||
} | ||
|
||
public IConstant GetConstantValue() | ||
{ | ||
var expression = _expression; | ||
if (expression is ConstantExpression constantExpression) | ||
{ | ||
expression = constantExpression.Expression; | ||
} | ||
|
||
if (expression is not ConstantLiteralExpression literalExpression) | ||
{ | ||
throw new AssertException($"Expression {expression} cannot be evaluated as constant expression."); | ||
} | ||
|
||
return literalExpression.Constant; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Cesium.CodeGen.Contexts.Meta; | ||
using Cesium.CodeGen.Ir; | ||
using Cesium.CodeGen.Ir.Types; | ||
using Cesium.Core; | ||
using Mono.Cecil; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace Cesium.CodeGen.Contexts; | ||
|
||
internal record SwitchScope(IEmitScope Parent) : IEmitScope, IDeclarationScope | ||
{ | ||
public AssemblyContext AssemblyContext => Parent.AssemblyContext; | ||
public ModuleDefinition Module => Parent.Module; | ||
public CTypeSystem CTypeSystem => Parent.CTypeSystem; | ||
public FunctionInfo? GetFunctionInfo(string identifier) | ||
=> ((IDeclarationScope)Parent).GetFunctionInfo(identifier); | ||
public TranslationUnitContext Context => Parent.Context; | ||
public MethodDefinition Method => Parent.Method; | ||
|
||
public IType? GetVariable(string identifier) | ||
{ | ||
return ((IDeclarationScope)Parent).GetVariable(identifier); | ||
} | ||
public IReadOnlyDictionary<string, IType> GlobalFields => ((IDeclarationScope)Parent).GlobalFields; | ||
public void AddVariable(string identifier, IType variable) => | ||
throw new WipException(205, "Variable addition into a switch scope is not implemented, yet."); | ||
public VariableDefinition ResolveVariable(string identifier) => Parent.ResolveVariable(identifier); // no declarations for `for` now, so pass parent variables | ||
|
||
public ParameterDefinition ResolveParameter(string name) => Parent.ResolveParameter(name); | ||
public ParameterInfo? GetParameterInfo(string name) => ((IDeclarationScope)Parent).GetParameterInfo(name); | ||
|
||
/// <inheritdoc /> | ||
public IType ResolveType(IType type) => Context.ResolveType(type); | ||
public void AddTypeDefinition(string identifier, IType type) => throw new AssertException("Not supported"); | ||
|
||
/// <inheritdoc /> | ||
public void AddLabel(string identifier) | ||
{ | ||
((IDeclarationScope)Parent).AddLabel(identifier); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public Instruction ResolveLabel(string label) | ||
{ | ||
return Parent.ResolveLabel(label); | ||
} | ||
|
||
private string _breakLabel = $"switch_{Guid.NewGuid()}"; | ||
|
||
/// <inheritdoc /> | ||
public string GetBreakLabel() | ||
{ | ||
return _breakLabel; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.