From bb42dd026cda93d2a0df9c996c0e4add3450c283 Mon Sep 17 00:00:00 2001 From: Westin Miller Date: Fri, 22 Sep 2017 14:45:07 -0700 Subject: [PATCH] Some more misc Msil stuff --- .../MSIL/MsilInstructionExtensions.cs | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs b/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs index 4629f7f..74b8912 100644 --- a/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs +++ b/Torch/Managers/PatchManager/MSIL/MsilInstructionExtensions.cs @@ -198,5 +198,73 @@ namespace Torch.Managers.PatchManager.MSIL return new MsilInstruction(argument.Position < 0xFF ? OpCodes.Ldarga_S : OpCodes.Ldarga).InlineValue(argument); } #endregion + + #region Constant Utils + /// + /// Determines if this instruction is a constant int load instruction. + /// + /// Instruction + /// True if this instruction pushes a constant int onto the stack + public static bool IsConstIntLoad(this MsilInstruction m) + { + if (m.OpCode == OpCodes.Ldc_I4_0) + return true; + if (m.OpCode == OpCodes.Ldc_I4_1) + return true; + if (m.OpCode == OpCodes.Ldc_I4_2) + return true; + if (m.OpCode == OpCodes.Ldc_I4_3) + return true; + if (m.OpCode == OpCodes.Ldc_I4_4) + return true; + if (m.OpCode == OpCodes.Ldc_I4_5) + return true; + if (m.OpCode == OpCodes.Ldc_I4_6) + return true; + if (m.OpCode == OpCodes.Ldc_I4_7) + return true; + if (m.OpCode == OpCodes.Ldc_I4_8) + return true; + if (m.OpCode == OpCodes.Ldc_I4_M1) + return true; + if (m.OpCode == OpCodes.Ldc_I4) + return true; + return m.OpCode == OpCodes.Ldc_I4_S; + } + + /// + /// Gets the constant int this instruction pushes onto the stack. + /// + /// Instruction + /// The constant int + public static int GetConstInt(this MsilInstruction m) + { + if (m.OpCode == OpCodes.Ldc_I4_0) + return 0; + if (m.OpCode == OpCodes.Ldc_I4_1) + return 1; + if (m.OpCode == OpCodes.Ldc_I4_2) + return 2; + if (m.OpCode == OpCodes.Ldc_I4_3) + return 3; + if (m.OpCode == OpCodes.Ldc_I4_4) + return 4; + if (m.OpCode == OpCodes.Ldc_I4_5) + return 5; + if (m.OpCode == OpCodes.Ldc_I4_6) + return 6; + if (m.OpCode == OpCodes.Ldc_I4_7) + return 7; + if (m.OpCode == OpCodes.Ldc_I4_8) + return 8; + if (m.OpCode == OpCodes.Ldc_I4_M1) + return -1; + if (m.OpCode == OpCodes.Ldc_I4) + return ((MsilOperandInline) m.Operand).Value; + if (m.OpCode == OpCodes.Ldc_I4_S) + return ((MsilOperandInline)m.Operand).Value; + throw new ArgumentException($"Can't get constant int from instruction {m}"); + } + #endregion } }