|
|
|
@ -20,6 +20,7 @@ using System;
@@ -20,6 +20,7 @@ using System;
|
|
|
|
|
using System.Collections.Generic; |
|
|
|
|
using System.Linq; |
|
|
|
|
using System.Text; |
|
|
|
|
using ICSharpCode.Decompiler.TypeSystem; |
|
|
|
|
|
|
|
|
|
namespace ICSharpCode.Decompiler.IL.Transforms |
|
|
|
|
{ |
|
|
|
@ -31,9 +32,16 @@ namespace ICSharpCode.Decompiler.IL.Transforms
@@ -31,9 +32,16 @@ namespace ICSharpCode.Decompiler.IL.Transforms
|
|
|
|
|
|
|
|
|
|
foreach (var block in function.Descendants.OfType<Block>()) { |
|
|
|
|
for (int i = block.Instructions.Count - 1; i >= 0; i--) { |
|
|
|
|
if (!MatchRoslynSwitchOnString(block.Instructions, i, out var newSwitch)) |
|
|
|
|
SwitchInstruction newSwitch; |
|
|
|
|
Block blockAfterSwitch = null; |
|
|
|
|
if (!MatchLegacySwitchOnString(block.Instructions, i, out newSwitch, out blockAfterSwitch) && |
|
|
|
|
!MatchRoslynSwitchOnString(block.Instructions, i, out newSwitch)) |
|
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
if (i + 1 < block.Instructions.Count && block.Instructions[i + 1] is Branch b && blockAfterSwitch != null) { |
|
|
|
|
block.Instructions[i + 1].ReplaceWith(new Branch(blockAfterSwitch)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
block.Instructions[i].ReplaceWith(newSwitch); |
|
|
|
|
block.Instructions.RemoveAt(i - 1); |
|
|
|
|
|
|
|
|
@ -53,6 +61,115 @@ namespace ICSharpCode.Decompiler.IL.Transforms
@@ -53,6 +61,115 @@ namespace ICSharpCode.Decompiler.IL.Transforms
|
|
|
|
|
container.SortBlocks(deleteUnreachableBlocks: true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool MatchLegacySwitchOnString(InstructionCollection<ILInstruction> instructions, int i, out SwitchInstruction inst, out Block blockAfterSwitch) |
|
|
|
|
{ |
|
|
|
|
inst = null; |
|
|
|
|
blockAfterSwitch = null; |
|
|
|
|
if (i < 1) return false; |
|
|
|
|
// match first block: checking switch-value for null
|
|
|
|
|
if (!(instructions[i].MatchIfInstruction(out var condition, out var exitBlockJump) && |
|
|
|
|
instructions[i - 1].MatchStLoc(out var switchValueVar, out var switchValue) && switchValueVar.Type.IsKnownType(KnownTypeCode.String))) |
|
|
|
|
return false; |
|
|
|
|
if (!exitBlockJump.MatchBranch(out var exitBlock)) |
|
|
|
|
return false; |
|
|
|
|
if (!(condition.MatchCompEquals(out var left, out var right) && right.MatchLdNull() && left.Match(switchValue).Success)) |
|
|
|
|
return false; |
|
|
|
|
var nextBlockJump = instructions.ElementAtOrDefault(i + 1) as Branch; |
|
|
|
|
if (nextBlockJump == null || nextBlockJump.TargetBlock.IncomingEdgeCount != 1) |
|
|
|
|
return false; |
|
|
|
|
// match second block: checking compiler-generated Dictionary<string, int> for null
|
|
|
|
|
var nextBlock = nextBlockJump.TargetBlock; |
|
|
|
|
if (nextBlock.Instructions.Count != 2 || !nextBlock.Instructions[0].MatchIfInstruction(out condition, out var tryGetValueBlockJump)) |
|
|
|
|
return false; |
|
|
|
|
if (!tryGetValueBlockJump.MatchBranch(out var tryGetValueBlock)) |
|
|
|
|
return false; |
|
|
|
|
if (!nextBlock.Instructions[1].MatchBranch(out var dictInitBlock) || dictInitBlock.IncomingEdgeCount != 1) |
|
|
|
|
return false; |
|
|
|
|
if (!(condition.MatchCompNotEquals(out left, out right) && right.MatchLdNull() && |
|
|
|
|
MatchDictionaryFieldLoad(left, out var dictField, out var dictionaryType))) |
|
|
|
|
return false; |
|
|
|
|
// match third block: initialization of compiler-generated Dictionary<string, int>
|
|
|
|
|
if (dictInitBlock.IncomingEdgeCount != 1 || dictInitBlock.Instructions.Count < 3) |
|
|
|
|
return false; |
|
|
|
|
if (!ExtractStringValuesFromDictionaryInitBlock(dictInitBlock, out var stringValues, tryGetValueBlock, dictionaryType, dictField)) |
|
|
|
|
return false; |
|
|
|
|
// match fourth block: TryGetValue on compiler-generated Dictionary<string, int>
|
|
|
|
|
if (tryGetValueBlock.IncomingEdgeCount != 2 || tryGetValueBlock.Instructions.Count != 2) |
|
|
|
|
return false; |
|
|
|
|
if (!tryGetValueBlock.Instructions[0].MatchIfInstruction(out condition, out var defaultBlockJump)) |
|
|
|
|
return false; |
|
|
|
|
if (!defaultBlockJump.MatchBranch(out var defaultBlock)) |
|
|
|
|
return false; |
|
|
|
|
if (!(condition.MatchLogicNot(out var arg) && arg is Call c && c.Method.Name == "TryGetValue" && |
|
|
|
|
MatchDictionaryFieldLoad(c.Arguments[0], out var dictField2, out _) && dictField2.Equals(dictField))) |
|
|
|
|
return false; |
|
|
|
|
if (!c.Arguments[1].MatchLdLoc(switchValueVar) || !c.Arguments[2].MatchLdLoca(out var switchIndexVar)) |
|
|
|
|
return false; |
|
|
|
|
if (!tryGetValueBlock.Instructions[1].MatchBranch(out var switchBlock)) |
|
|
|
|
return false; |
|
|
|
|
// match fifth block: switch-instruction block
|
|
|
|
|
if (switchBlock.IncomingEdgeCount != 1 || switchBlock.Instructions.Count != 2) |
|
|
|
|
return false; |
|
|
|
|
if (!(switchBlock.Instructions[0] is SwitchInstruction switchInst && switchInst.Value.MatchLdLoc(switchIndexVar))) |
|
|
|
|
return false; |
|
|
|
|
if (!switchBlock.Instructions[1].MatchBranch(defaultBlock)) |
|
|
|
|
return false; |
|
|
|
|
// switch contains case null:
|
|
|
|
|
var sections = new List<SwitchSection>(switchInst.Sections); |
|
|
|
|
if (exitBlock != defaultBlock) { |
|
|
|
|
stringValues.Add(null); |
|
|
|
|
sections.Add(new SwitchSection() { Labels = new Util.LongSet(stringValues.Count - 1), Body = new Branch(exitBlock) }); |
|
|
|
|
} |
|
|
|
|
var stringToInt = new StringToInt(switchValue.Clone(), stringValues.ToArray()); |
|
|
|
|
inst = new SwitchInstruction(stringToInt); |
|
|
|
|
inst.DefaultBody = switchInst.DefaultBody; |
|
|
|
|
inst.Sections.AddRange(sections); |
|
|
|
|
blockAfterSwitch = defaultBlock; |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool MatchDictionaryFieldLoad(ILInstruction inst, out IField dictField, out IType dictionaryType) |
|
|
|
|
{ |
|
|
|
|
dictField = null; |
|
|
|
|
dictionaryType = null; |
|
|
|
|
return inst.MatchLdObj(out var dictionaryFieldLoad, out dictionaryType) && |
|
|
|
|
IsStringToIntDictionary(dictionaryType) && |
|
|
|
|
dictionaryFieldLoad.MatchLdsFlda(out dictField) && |
|
|
|
|
dictField.IsCompilerGeneratedOrIsInCompilerGeneratedClass(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool ExtractStringValuesFromDictionaryInitBlock(Block block, out List<string> values, Block targetBlock, IType dictionaryType, IField dictionaryField) |
|
|
|
|
{ |
|
|
|
|
values = null; |
|
|
|
|
if (!(block.Instructions[0].MatchStLoc(out var dictVar, out var newObjDict) && |
|
|
|
|
newObjDict is NewObj newObj && newObj.Arguments.Count == 1 && newObj.Arguments[0].MatchLdcI4(out var valuesLength))) |
|
|
|
|
return false; |
|
|
|
|
if (block.Instructions.Count != valuesLength + 3) |
|
|
|
|
return false; |
|
|
|
|
values = new List<string>(valuesLength); |
|
|
|
|
for (int i = 0; i < valuesLength; i++) { |
|
|
|
|
if (!(block.Instructions[i + 1] is Call c && c.Method.Name == "Add" && c.Arguments.Count == 3 && |
|
|
|
|
c.Arguments[0].MatchLdLoc(dictVar) && c.Arguments[1].MatchLdStr(out var value) && c.Arguments[2].MatchLdcI4(i))) |
|
|
|
|
return false; |
|
|
|
|
values.Add(value); |
|
|
|
|
} |
|
|
|
|
if (!(block.Instructions[valuesLength + 1].MatchStObj(out var loadField, out var dictVarLoad, out var dictType) && |
|
|
|
|
dictType.Equals(dictionaryType) && loadField.MatchLdsFlda(out var dictField) && dictField.Equals(dictionaryField)) && |
|
|
|
|
dictVarLoad.MatchLdLoc(dictVar)) |
|
|
|
|
return false; |
|
|
|
|
return block.Instructions[valuesLength + 2].MatchBranch(targetBlock); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool IsStringToIntDictionary(IType dictionaryType) |
|
|
|
|
{ |
|
|
|
|
if (dictionaryType.FullName != "System.Collections.Generic.Dictionary") |
|
|
|
|
return false; |
|
|
|
|
if (dictionaryType.TypeArguments.Count != 2) |
|
|
|
|
return false; |
|
|
|
|
return dictionaryType.TypeArguments[0].IsKnownType(KnownTypeCode.String) && |
|
|
|
|
dictionaryType.TypeArguments[1].IsKnownType(KnownTypeCode.Int32); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool MatchRoslynSwitchOnString(InstructionCollection<ILInstruction> instructions, int i, out SwitchInstruction inst) |
|
|
|
|
{ |
|
|
|
|
inst = null; |
|
|
|
|