-
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.
(#120) CodeGen: add for scope and
break
statement
- Loading branch information
1 parent
fc6060c
commit e8b4499
Showing
9 changed files
with
95 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace Cesium.CodeGen.Tests; | ||
|
||
public class CodeGenBreakStatementTests : CodeGenTestBase | ||
{ | ||
private static Task DoTest(string source) | ||
{ | ||
var assembly = GenerateAssembly(default, source); | ||
|
||
var moduleType = assembly.Modules.Single().GetType("<Module>"); | ||
return VerifyMethods(moduleType); | ||
} | ||
|
||
[Fact] | ||
public Task BreakInFor() => DoTest(@"int main() | ||
{ | ||
for(;;) break; | ||
}"); | ||
|
||
[Fact] | ||
public Task BreakNotInFor() => Assert.ThrowsAsync<InvalidOperationException>( | ||
() => DoTest(@"int main() | ||
{ | ||
break; | ||
}")); | ||
} |
6 changes: 6 additions & 0 deletions
6
Cesium.CodeGen.Tests/verified/CodeGenBreakStatementTests.BreakInFor.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,6 @@ | ||
System.Int32 <Module>::main() | ||
IL_0000: br IL_000a | ||
IL_0005: br IL_0014 | ||
IL_000a: ldc.i4 1 | ||
IL_000f: brtrue IL_0005 | ||
IL_0014: nop |
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,19 @@ | ||
using Cesium.CodeGen.Contexts.Meta; | ||
using Mono.Cecil; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace Cesium.CodeGen.Contexts; | ||
|
||
internal record ForScope(IDeclarationScope Parent) : IDeclarationScope | ||
{ | ||
public AssemblyContext AssemblyContext => Parent.AssemblyContext; | ||
public ModuleDefinition Module => Parent.Module; | ||
public TypeSystem TypeSystem => Parent.TypeSystem; | ||
public IReadOnlyDictionary<string, FunctionInfo> Functions => Parent.Functions; | ||
public TranslationUnitContext Context => Parent.Context; | ||
public MethodDefinition Method => Parent.Method; | ||
public Dictionary<string, VariableDefinition> Variables => Parent.Variables; // no declarations for `for` now, so pass parent variables | ||
public ParameterDefinition? GetParameter(string name) => Parent.GetParameter(name); | ||
|
||
public Instruction? EndInstruction { get; set; } | ||
} |
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,20 @@ | ||
using Cesium.CodeGen.Contexts; | ||
using Mono.Cecil.Cil; | ||
|
||
namespace Cesium.CodeGen.Ir.BlockItems; | ||
|
||
internal class BreakStatement : IBlockItem | ||
{ | ||
public IBlockItem Lower() => this; | ||
|
||
public void EmitTo(IDeclarationScope scope) | ||
{ | ||
if (scope is not ForScope forScope) | ||
throw new InvalidOperationException("Can't break not from for statement"); | ||
|
||
var endInstruction = scope.Method.Body.GetILProcessor().Create(OpCodes.Nop); | ||
scope.Method.Body.Instructions.Add(Instruction.Create(OpCodes.Br, endInstruction)); | ||
|
||
forScope.EndInstruction = endInstruction; | ||
} | ||
} |
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,12 @@ | ||
int main(int argc, char *argv[]) | ||
{ | ||
int i; | ||
int j = 0; | ||
for (i = 0; i < 10000; ++i) | ||
{ | ||
if (j == 42) | ||
break; | ||
++j; | ||
} | ||
return j; | ||
} |