| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- #if ENABLE_LUA_INJECTION
- //
- // Author:
- // Jb Evain (jbevain@gmail.com)
- //
- // Copyright (c) 2008 - 2015 Jb Evain
- // Copyright (c) 2008 - 2011 Novell, Inc.
- //
- // Licensed under the MIT/X11 license.
- //
- using System;
- using Mono.Cecil;
- using Mono.Cecil.Cil;
- namespace CustomCecilRocks {
- public
- static class MethodBodyRocks {
- public static void SimplifyMacros (this MethodBody self)
- {
- if (self == null)
- throw new ArgumentNullException ("self");
- foreach (var instruction in self.Instructions) {
- if (instruction.OpCode.OpCodeType != OpCodeType.Macro)
- continue;
- switch (instruction.OpCode.Code) {
- case Code.Ldarg_0:
- ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (0));
- break;
- case Code.Ldarg_1:
- ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (1));
- break;
- case Code.Ldarg_2:
- ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (2));
- break;
- case Code.Ldarg_3:
- ExpandMacro (instruction, OpCodes.Ldarg, self.GetParameter (3));
- break;
- case Code.Ldloc_0:
- ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [0]);
- break;
- case Code.Ldloc_1:
- ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [1]);
- break;
- case Code.Ldloc_2:
- ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [2]);
- break;
- case Code.Ldloc_3:
- ExpandMacro (instruction, OpCodes.Ldloc, self.Variables [3]);
- break;
- case Code.Stloc_0:
- ExpandMacro (instruction, OpCodes.Stloc, self.Variables [0]);
- break;
- case Code.Stloc_1:
- ExpandMacro (instruction, OpCodes.Stloc, self.Variables [1]);
- break;
- case Code.Stloc_2:
- ExpandMacro (instruction, OpCodes.Stloc, self.Variables [2]);
- break;
- case Code.Stloc_3:
- ExpandMacro (instruction, OpCodes.Stloc, self.Variables [3]);
- break;
- case Code.Ldarg_S:
- instruction.OpCode = OpCodes.Ldarg;
- break;
- case Code.Ldarga_S:
- instruction.OpCode = OpCodes.Ldarga;
- break;
- case Code.Starg_S:
- instruction.OpCode = OpCodes.Starg;
- break;
- case Code.Ldloc_S:
- instruction.OpCode = OpCodes.Ldloc;
- break;
- case Code.Ldloca_S:
- instruction.OpCode = OpCodes.Ldloca;
- break;
- case Code.Stloc_S:
- instruction.OpCode = OpCodes.Stloc;
- break;
- case Code.Ldc_I4_M1:
- ExpandMacro (instruction, OpCodes.Ldc_I4, -1);
- break;
- case Code.Ldc_I4_0:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 0);
- break;
- case Code.Ldc_I4_1:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 1);
- break;
- case Code.Ldc_I4_2:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 2);
- break;
- case Code.Ldc_I4_3:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 3);
- break;
- case Code.Ldc_I4_4:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 4);
- break;
- case Code.Ldc_I4_5:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 5);
- break;
- case Code.Ldc_I4_6:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 6);
- break;
- case Code.Ldc_I4_7:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 7);
- break;
- case Code.Ldc_I4_8:
- ExpandMacro (instruction, OpCodes.Ldc_I4, 8);
- break;
- case Code.Ldc_I4_S:
- ExpandMacro (instruction, OpCodes.Ldc_I4, (int) (sbyte) instruction.Operand);
- break;
- case Code.Br_S:
- instruction.OpCode = OpCodes.Br;
- break;
- case Code.Brfalse_S:
- instruction.OpCode = OpCodes.Brfalse;
- break;
- case Code.Brtrue_S:
- instruction.OpCode = OpCodes.Brtrue;
- break;
- case Code.Beq_S:
- instruction.OpCode = OpCodes.Beq;
- break;
- case Code.Bge_S:
- instruction.OpCode = OpCodes.Bge;
- break;
- case Code.Bgt_S:
- instruction.OpCode = OpCodes.Bgt;
- break;
- case Code.Ble_S:
- instruction.OpCode = OpCodes.Ble;
- break;
- case Code.Blt_S:
- instruction.OpCode = OpCodes.Blt;
- break;
- case Code.Bne_Un_S:
- instruction.OpCode = OpCodes.Bne_Un;
- break;
- case Code.Bge_Un_S:
- instruction.OpCode = OpCodes.Bge_Un;
- break;
- case Code.Bgt_Un_S:
- instruction.OpCode = OpCodes.Bgt_Un;
- break;
- case Code.Ble_Un_S:
- instruction.OpCode = OpCodes.Ble_Un;
- break;
- case Code.Blt_Un_S:
- instruction.OpCode = OpCodes.Blt_Un;
- break;
- case Code.Leave_S:
- instruction.OpCode = OpCodes.Leave;
- break;
- }
- }
- }
- static void ExpandMacro (Instruction instruction, OpCode opcode, object operand)
- {
- instruction.OpCode = opcode;
- instruction.Operand = operand;
- }
- static void MakeMacro (Instruction instruction, OpCode opcode)
- {
- instruction.OpCode = opcode;
- instruction.Operand = null;
- }
- public static void Optimize (this MethodBody self)
- {
- if (self == null)
- throw new ArgumentNullException ("self");
- OptimizeLongs (self);
- OptimizeMacros (self);
- }
- static void OptimizeLongs (this MethodBody self)
- {
- for (var i = 0; i < self.Instructions.Count; i++) {
- var instruction = self.Instructions [i];
- if (instruction.OpCode.Code != Code.Ldc_I8)
- continue;
- var l = (long) instruction.Operand;
- if (l >= int.MaxValue || l <= int.MinValue)
- continue;
- ExpandMacro (instruction, OpCodes.Ldc_I4, (int) l);
- self.Instructions.Insert (++i, Instruction.Create (OpCodes.Conv_I8));
- }
- }
- public static void OptimizeMacros (this MethodBody self)
- {
- if (self == null)
- throw new ArgumentNullException ("self");
- var method = self.Method;
- foreach (var instruction in self.Instructions) {
- int index;
- switch (instruction.OpCode.Code) {
- case Code.Ldarg:
- index = ((ParameterDefinition) instruction.Operand).Index;
- if (index == -1 && instruction.Operand == self.ThisParameter)
- index = 0;
- else if (method.HasThis)
- index++;
- switch (index) {
- case 0:
- MakeMacro (instruction, OpCodes.Ldarg_0);
- break;
- case 1:
- MakeMacro (instruction, OpCodes.Ldarg_1);
- break;
- case 2:
- MakeMacro (instruction, OpCodes.Ldarg_2);
- break;
- case 3:
- MakeMacro (instruction, OpCodes.Ldarg_3);
- break;
- default:
- if (index < 256)
- ExpandMacro (instruction, OpCodes.Ldarg_S, instruction.Operand);
- break;
- }
- break;
- case Code.Ldloc:
- index = ((VariableDefinition) instruction.Operand).Index;
- switch (index) {
- case 0:
- MakeMacro (instruction, OpCodes.Ldloc_0);
- break;
- case 1:
- MakeMacro (instruction, OpCodes.Ldloc_1);
- break;
- case 2:
- MakeMacro (instruction, OpCodes.Ldloc_2);
- break;
- case 3:
- MakeMacro (instruction, OpCodes.Ldloc_3);
- break;
- default:
- if (index < 256)
- ExpandMacro (instruction, OpCodes.Ldloc_S, instruction.Operand);
- break;
- }
- break;
- case Code.Stloc:
- index = ((VariableDefinition) instruction.Operand).Index;
- switch (index) {
- case 0:
- MakeMacro (instruction, OpCodes.Stloc_0);
- break;
- case 1:
- MakeMacro (instruction, OpCodes.Stloc_1);
- break;
- case 2:
- MakeMacro (instruction, OpCodes.Stloc_2);
- break;
- case 3:
- MakeMacro (instruction, OpCodes.Stloc_3);
- break;
- default:
- if (index < 256)
- ExpandMacro (instruction, OpCodes.Stloc_S, instruction.Operand);
- break;
- }
- break;
- case Code.Ldarga:
- index = ((ParameterDefinition) instruction.Operand).Index;
- if (index == -1 && instruction.Operand == self.ThisParameter)
- index = 0;
- else if (method.HasThis)
- index++;
- if (index < 256)
- ExpandMacro (instruction, OpCodes.Ldarga_S, instruction.Operand);
- break;
- case Code.Ldloca:
- if (((VariableDefinition) instruction.Operand).Index < 256)
- ExpandMacro (instruction, OpCodes.Ldloca_S, instruction.Operand);
- break;
- case Code.Ldc_I4:
- int i = (int) instruction.Operand;
- switch (i) {
- case -1:
- MakeMacro (instruction, OpCodes.Ldc_I4_M1);
- break;
- case 0:
- MakeMacro (instruction, OpCodes.Ldc_I4_0);
- break;
- case 1:
- MakeMacro (instruction, OpCodes.Ldc_I4_1);
- break;
- case 2:
- MakeMacro (instruction, OpCodes.Ldc_I4_2);
- break;
- case 3:
- MakeMacro (instruction, OpCodes.Ldc_I4_3);
- break;
- case 4:
- MakeMacro (instruction, OpCodes.Ldc_I4_4);
- break;
- case 5:
- MakeMacro (instruction, OpCodes.Ldc_I4_5);
- break;
- case 6:
- MakeMacro (instruction, OpCodes.Ldc_I4_6);
- break;
- case 7:
- MakeMacro (instruction, OpCodes.Ldc_I4_7);
- break;
- case 8:
- MakeMacro (instruction, OpCodes.Ldc_I4_8);
- break;
- default:
- if (i >= -128 && i < 128)
- ExpandMacro (instruction, OpCodes.Ldc_I4_S, (sbyte) i);
- break;
- }
- break;
- }
- }
- OptimizeBranches (self);
- }
- static void OptimizeBranches (MethodBody body)
- {
- ComputeOffsets (body);
- foreach (var instruction in body.Instructions) {
- if (instruction.OpCode.OperandType != OperandType.InlineBrTarget)
- continue;
- if (OptimizeBranch (instruction))
- ComputeOffsets (body);
- }
- }
- static bool OptimizeBranch (Instruction instruction)
- {
- var offset = ((Instruction) instruction.Operand).Offset - (instruction.Offset + instruction.OpCode.Size + 4);
- if (!(offset >= -128 && offset <= 127))
- return false;
- switch (instruction.OpCode.Code) {
- case Code.Br:
- instruction.OpCode = OpCodes.Br_S;
- break;
- case Code.Brfalse:
- instruction.OpCode = OpCodes.Brfalse_S;
- break;
- case Code.Brtrue:
- instruction.OpCode = OpCodes.Brtrue_S;
- break;
- case Code.Beq:
- instruction.OpCode = OpCodes.Beq_S;
- break;
- case Code.Bge:
- instruction.OpCode = OpCodes.Bge_S;
- break;
- case Code.Bgt:
- instruction.OpCode = OpCodes.Bgt_S;
- break;
- case Code.Ble:
- instruction.OpCode = OpCodes.Ble_S;
- break;
- case Code.Blt:
- instruction.OpCode = OpCodes.Blt_S;
- break;
- case Code.Bne_Un:
- instruction.OpCode = OpCodes.Bne_Un_S;
- break;
- case Code.Bge_Un:
- instruction.OpCode = OpCodes.Bge_Un_S;
- break;
- case Code.Bgt_Un:
- instruction.OpCode = OpCodes.Bgt_Un_S;
- break;
- case Code.Ble_Un:
- instruction.OpCode = OpCodes.Ble_Un_S;
- break;
- case Code.Blt_Un:
- instruction.OpCode = OpCodes.Blt_Un_S;
- break;
- case Code.Leave:
- instruction.OpCode = OpCodes.Leave_S;
- break;
- }
- return true;
- }
- static void ComputeOffsets (MethodBody body)
- {
- var offset = 0;
- foreach (var instruction in body.Instructions) {
- instruction.Offset = offset;
- offset += instruction.GetSize ();
- }
- }
- public static ParameterDefinition GetParameter (this MethodBody self, int index)
- {
- var method = self.Method;
- if (method.HasThis) {
- if (index == 0)
- return self.ThisParameter;
- index--;
- }
- var parameters = method.Parameters;
- if (index < 0 || index >= parameters.Count)
- return null;
- return parameters [index];
- }
- }
- }
- #endif
|