private void TryOptimizeVariable(MsilInstruction instruction)
{
var variable = instruction.Operand as VariableSignature;
var localVarSig = Signature != null ? Signature.Signature as LocalVariableSignature : null;
if (localVarSig == null || variable == null)
return;
int index = localVarSig.Variables.IndexOf(variable);
if (index < 0 || index > byte.MaxValue)
return;
switch (instruction.OpCode.Code)
{
case MsilCode.Ldloc:
if (index <= 3)
{
instruction.OpCode = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Ldloc_0.Op2 + index];
instruction.Operand = null;
}
else
{
instruction.OpCode = MsilOpCodes.Ldloc_S;
}
break;
case MsilCode.Ldloca:
instruction.OpCode = MsilOpCodes.Ldloca_S;
break;
case MsilCode.Stloc:
if (index <= 3)
{
instruction.OpCode = MsilOpCodes.SingleByteOpCodes[MsilOpCodes.Stloc_0.Op2 + index];
instruction.Operand = null;
}
else
{
instruction.OpCode = MsilOpCodes.Stloc_S;
}
break;
}
}