forked from kekyo/IL2C
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 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,48 @@ | ||
using IL2C.ILConverters; | ||
using IL2C.Metadata; | ||
using IL2C.Translators; | ||
using Mono.Cecil.Cil; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace IL2C.ILConveters | ||
{ | ||
internal sealed class SwitchConverter : ILConverter<Instruction[]> | ||
{ | ||
public override OpCode OpCode => OpCodes.Switch; | ||
|
||
public override ExpressionEmitter Prepare(Instruction[] operands, DecodeContext decodeContext) | ||
{ | ||
var si = decodeContext.PopStack(); | ||
if (!si.TargetType.IsInt32Type) | ||
throw new InvalidProgramSequenceException( | ||
"Invalid type at stack for switch: Location={0}, StackType={1}", | ||
decodeContext.CurrentCode.RawLocation, | ||
si.TargetType.FriendlyName); | ||
|
||
var labels = new string[operands.Length]; | ||
int i; | ||
|
||
for (i = 0; i < operands.Length; i++) | ||
{ | ||
labels[i] = decodeContext.EnqueueNewPath(operands[i].Offset); | ||
} | ||
|
||
return (extractContext, _) => | ||
{ | ||
var result = new string[labels.Length + 1]; | ||
result[0] = $"switch ({extractContext.GetSymbolName(si)}) {{\n "; | ||
for (int q = 0; q < labels.Length; q++) | ||
{ | ||
var caseStr = $"case {q}: goto {labels[q]}"; | ||
if (q == 0) result[q] += caseStr; else result[q] = caseStr; | ||
} | ||
result[result.Length - 1] = "default: }"; | ||
return result; | ||
}; | ||
} | ||
} | ||
} |