From 8ab7cb02ee5741cdd4015d39936003bf04537683 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 15:18:49 +0500 Subject: [PATCH 01/10] Remove il modification related functionality entirely since it's been broken after adding .net core support in PR #9 (see related issue #12). --- Common.DotSettings | 1 + GrEmit.Tests/Test.cs | 23 +- GrEmit/Injection/MethodUtil.cs | 288 ------ GrEmit/MethodBodyParsing/ByteBuffer.cs | 348 ------- GrEmit/MethodBodyParsing/Code.cs | 235 ----- GrEmit/MethodBodyParsing/Collection.cs | 432 --------- .../MethodBodyParsing/DynamicILGenerator.cs | 42 - .../MethodBodyParsing/DynamicILInfoWrapper.cs | 69 -- .../MethodBodyParsing/DynamicMethodWrapper.cs | 97 -- GrEmit/MethodBodyParsing/DynamicResolver.cs | 186 ---- GrEmit/MethodBodyParsing/DynamicScope.cs | 91 -- GrEmit/MethodBodyParsing/ElementType.cs | 56 -- GrEmit/MethodBodyParsing/Empty.cs | 41 - GrEmit/MethodBodyParsing/ExceptionHandler.cs | 56 -- GrEmit/MethodBodyParsing/ExceptionsBaker.cs | 157 --- .../MethodBodyParsing/ExceptionsInfoReader.cs | 129 --- .../MethodBodyParsing/FullMethodBodyBaker.cs | 99 -- GrEmit/MethodBodyParsing/ILCodeBaker.cs | 154 --- GrEmit/MethodBodyParsing/ILCodeReader.cs | 159 ---- GrEmit/MethodBodyParsing/Instruction.cs | 269 ------ .../InstructionCollection.cs | 112 --- GrEmit/MethodBodyParsing/LocalInfo.cs | 23 - .../MethodBodyParsing/LocalInfoCollection.cs | 40 - .../MethodBodyParsing/LocalVarSigBuilder.cs | 68 -- .../MaxStackSizeCalculator.cs | 250 ----- .../MethodBodyParsing/MetadataExtensions.cs | 99 -- GrEmit/MethodBodyParsing/MetadataToken.cs | 92 -- GrEmit/MethodBodyParsing/MethodBody.cs | 249 ----- .../MethodBodyOnDynamicILGenerator.cs | 230 ----- .../MethodBodyOnDynamicILInfo.cs | 43 - .../MethodBodyOnMethodBase.cs | 66 -- .../MethodBodyOnUnmanagedBuffer.cs | 48 - GrEmit/MethodBodyParsing/MethodBodyRocks.cs | 386 -------- GrEmit/MethodBodyParsing/MethodHeader.cs | 12 - .../MethodBodyParsing/MethodHeaderReader.cs | 45 - GrEmit/MethodBodyParsing/OpCode.cs | 421 --------- GrEmit/MethodBodyParsing/OpCodes.cs | 894 ------------------ GrEmit/MethodBodyParsing/SignatureReader.cs | 272 ------ GrEmit/MethodBodyParsing/TokenType.cs | 39 - .../MethodBodyParsing/TypeSignatureReader.cs | 174 ---- .../MethodBodyParsing/UnmanagedByteBuffer.cs | 143 --- GrEmit/Utils/DynamicILInfo.cs | 239 ----- 42 files changed, 3 insertions(+), 6874 deletions(-) delete mode 100644 GrEmit/Injection/MethodUtil.cs delete mode 100644 GrEmit/MethodBodyParsing/ByteBuffer.cs delete mode 100644 GrEmit/MethodBodyParsing/Code.cs delete mode 100644 GrEmit/MethodBodyParsing/Collection.cs delete mode 100644 GrEmit/MethodBodyParsing/DynamicILGenerator.cs delete mode 100644 GrEmit/MethodBodyParsing/DynamicILInfoWrapper.cs delete mode 100644 GrEmit/MethodBodyParsing/DynamicMethodWrapper.cs delete mode 100644 GrEmit/MethodBodyParsing/DynamicResolver.cs delete mode 100644 GrEmit/MethodBodyParsing/DynamicScope.cs delete mode 100644 GrEmit/MethodBodyParsing/ElementType.cs delete mode 100644 GrEmit/MethodBodyParsing/Empty.cs delete mode 100644 GrEmit/MethodBodyParsing/ExceptionHandler.cs delete mode 100644 GrEmit/MethodBodyParsing/ExceptionsBaker.cs delete mode 100644 GrEmit/MethodBodyParsing/ExceptionsInfoReader.cs delete mode 100644 GrEmit/MethodBodyParsing/FullMethodBodyBaker.cs delete mode 100644 GrEmit/MethodBodyParsing/ILCodeBaker.cs delete mode 100644 GrEmit/MethodBodyParsing/ILCodeReader.cs delete mode 100644 GrEmit/MethodBodyParsing/Instruction.cs delete mode 100644 GrEmit/MethodBodyParsing/InstructionCollection.cs delete mode 100644 GrEmit/MethodBodyParsing/LocalInfo.cs delete mode 100644 GrEmit/MethodBodyParsing/LocalInfoCollection.cs delete mode 100644 GrEmit/MethodBodyParsing/LocalVarSigBuilder.cs delete mode 100644 GrEmit/MethodBodyParsing/MaxStackSizeCalculator.cs delete mode 100644 GrEmit/MethodBodyParsing/MetadataExtensions.cs delete mode 100644 GrEmit/MethodBodyParsing/MetadataToken.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodBody.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodBodyOnDynamicILGenerator.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodBodyOnDynamicILInfo.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodBodyOnMethodBase.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodBodyOnUnmanagedBuffer.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodBodyRocks.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodHeader.cs delete mode 100644 GrEmit/MethodBodyParsing/MethodHeaderReader.cs delete mode 100644 GrEmit/MethodBodyParsing/OpCode.cs delete mode 100644 GrEmit/MethodBodyParsing/OpCodes.cs delete mode 100644 GrEmit/MethodBodyParsing/SignatureReader.cs delete mode 100644 GrEmit/MethodBodyParsing/TokenType.cs delete mode 100644 GrEmit/MethodBodyParsing/TypeSignatureReader.cs delete mode 100644 GrEmit/MethodBodyParsing/UnmanagedByteBuffer.cs delete mode 100644 GrEmit/Utils/DynamicILInfo.cs diff --git a/Common.DotSettings b/Common.DotSettings index 3db4f8b..a3d6674 100644 --- a/Common.DotSettings +++ b/Common.DotSettings @@ -170,6 +170,7 @@ True True True + True True True True diff --git a/GrEmit.Tests/Test.cs b/GrEmit.Tests/Test.cs index 9497a58..0ca6e6d 100644 --- a/GrEmit.Tests/Test.cs +++ b/GrEmit.Tests/Test.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; @@ -7,7 +7,6 @@ using NUnit.Framework; -using MethodBody = GrEmit.MethodBodyParsing.MethodBody; #if NET45 using System.Diagnostics; using System.Linq; @@ -18,24 +17,6 @@ namespace GrEmit.Tests [TestFixture] public class Test { - public static void Qzz() - { - var list = new List(); - list.Add(1); - } - - [Test, Ignore("Is used for debugging")] - public void TestZzz() - { - var method = HackHelpers.GetMethodDefinition(x => Qzz()); - var body = MethodBody.Read(method, true); - var z = body.CreateDelegate(); - z(); - var body2 = MethodBody.Read(z.Method, true); - var z2 = body2.CreateDelegate(); - z2(); - } - [Test] public void TestDifferentStructs() { @@ -480,7 +461,7 @@ public void TestPerformance_Ifs_vs_Switch() var ifs = BuildIfs(module, keys); var switCh = BuildSwitch(module, keys); - const int iterations = 1000000000; + const int iterations = 100_000_000; Console.WriteLine("Worst case:"); diff --git a/GrEmit/Injection/MethodUtil.cs b/GrEmit/Injection/MethodUtil.cs deleted file mode 100644 index df8231f..0000000 --- a/GrEmit/Injection/MethodUtil.cs +++ /dev/null @@ -1,288 +0,0 @@ -using System; -using System.Reflection; -using System.Reflection.Emit; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Threading; - -namespace GrEmit.Injection -{ - public static class MethodUtil - { - [DllImport("kernel32.dll")] - public static extern unsafe bool VirtualProtect(IntPtr lpAddress, uint dwSize, MEMORY_PROTECTION_CONSTANTS flNewProtect, MEMORY_PROTECTION_CONSTANTS* lpflOldProtect); - - [Flags] - public enum MEMORY_PROTECTION_CONSTANTS - { - PAGE_EXECUTE = 0x10, - PAGE_EXECUTE_READ = 0x20, - PAGE_EXECUTE_READWRITE = 0x40, - PAGE_EXECUTE_WRITECOPY = 0x80, - PAGE_NOACCESS = 0x01, - PAGE_READONLY = 0x02, - PAGE_READWRITE = 0x04, - PAGE_WRITECOPY = 0x08, - PAGE_GUARD = 0x100, - PAGE_NOCACHE = 0x200, - PAGE_WRITECOMBINE = 0x400, - PAGE_TARGETS_INVALID = 0x40000000, - PAGE_TARGETS_NO_UPDATE = 0x40000000, - } - - public static unsafe bool HookMethod(MethodBase victim, MethodBase dest, out Action unhook) - { - var destAddr = GetMethodAddress(dest); - unhook = null; - - //var genericArguments = new Type[0]; - - //if (victim.DeclaringType.GetGenericArguments() != null) - //{ - // genericArguments = victim.DeclaringType.GetGenericArguments().ToArray(); - //} - - //if (victim.GetGenericArguments() != null) - //{ - // genericArguments = genericArguments.Concat(victim.GetGenericArguments()).ToArray(); - //} - - RuntimeHelpers.PrepareMethod(victim.MethodHandle); - //RuntimeHelpers.PrepareMethod(victim.MethodHandle, victim.DeclaringType.GetGenericArguments().Concat(victim.GetGenericArguments()).Select(type => type.TypeHandle).ToArray()); - var victimAddr = GetMethodAddress(victim); - - bool canMakeRelJmp; - int dist = 0; - if (IntPtr.Size == 4) - { - canMakeRelJmp = true; - dist = destAddr.ToInt32() - victimAddr.ToInt32() - 5; - } - else - { - var dist64 = destAddr.ToInt64() - victimAddr.ToInt64() - 5; - canMakeRelJmp = dist64 >= int.MinValue && dist64 <= int.MaxValue; - if (canMakeRelJmp) - dist = (int)dist64; - } - if (canMakeRelJmp) - { - // Make relative jump - var bytes = BitConverter.GetBytes(dist); - var hookCode = new byte[] - { - 0xE9, bytes[0], bytes[1], bytes[2], bytes[3], // jmp dest - 0x00, - 0x00, - 0x00, - }; - long x = BitConverter.ToInt64(hookCode, 0); - - long oldCode; - if (!PlantRelJmpHook(victimAddr, x, out oldCode)) - return false; - - unhook = () => - { - long tmp; - PlantRelJmpHook(victimAddr, oldCode, out tmp); - }; - - return true; - } - else - { - // todo: set unhook delegate - if (compareExchange2Words == null) - return false; - // Make absolute jump - UIntPtr lo, hi; - if (IntPtr.Size == 8) - { - // x64 - var bytes = BitConverter.GetBytes(destAddr.ToInt64()); - var hookCode = new byte[16] - { - 0x48, 0xB8, bytes[0], bytes[1], bytes[2], bytes[3], - bytes[4], bytes[5], bytes[6], bytes[7], // movabs rax, hookMethod - 0xFF, 0xE0, // jmp rax - 0x90, // nop - 0x90, // nop - 0x90, // nop - 0x90 // nop - }; - lo = new UIntPtr(BitConverter.ToUInt64(hookCode, 0)); - hi = new UIntPtr(BitConverter.ToUInt64(hookCode, 8)); - } - else - { - // x86 - var bytes = BitConverter.GetBytes(destAddr.ToInt32()); - var hookCode = new byte[8] - { - 0xB8, bytes[0], bytes[1], bytes[2], bytes[3], // mov eax, hookMethod - 0xFF, 0xE0, // jmp eax - 0x90 // nop - }; - lo = new UIntPtr(BitConverter.ToUInt32(hookCode, 0)); - hi = new UIntPtr(BitConverter.ToUInt32(hookCode, 4)); - } - - MEMORY_PROTECTION_CONSTANTS oldProtect; - if (!VirtualProtect(victimAddr, (uint)(IntPtr.Size * 2), MEMORY_PROTECTION_CONSTANTS.PAGE_EXECUTE_READWRITE, &oldProtect)) - return false; - - compareExchange2Words(victimAddr, lo, hi); - - VirtualProtect(victimAddr, (uint)(IntPtr.Size * 2), oldProtect, &oldProtect); - - return true; - } - } - - private static unsafe bool PlantRelJmpHook(IntPtr victimAddr, long newCode, out long oldCode) - { - MEMORY_PROTECTION_CONSTANTS oldProtect; - if (!VirtualProtect(victimAddr, 8, MEMORY_PROTECTION_CONSTANTS.PAGE_EXECUTE_READWRITE, &oldProtect)) - { - oldCode = 0; - return false; - } - - oldCode = relJmpHooker(victimAddr, newCode); - - VirtualProtect(victimAddr, 8, oldProtect, &oldProtect); - return true; - } - - private static Func EmitRelJmpHooker() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(long), new[] {typeof(IntPtr), typeof(long)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.VerificationKind = TypesAssignabilityVerificationKind.LowLevelOnly; - var cycleLabel = il.DefineLabel("cycle"); - il.MarkLabel(cycleLabel); - il.Ldarg(0); // stack: [ptr] - il.Dup(); // stack: [ptr, ptr] - var x = il.DeclareLocal(typeof(long)); - il.Ldind(typeof(long)); // stack: [ptr, *ptr] - il.Dup(); - il.Stloc(x); // x = *ptr; stack: [ptr, newCode] - il.Ldc_I8(unchecked((long)0xFFFFFF0000000000)); - il.And(); // stack: [ptr, x & 0xFFFFFF0000000000] - il.Ldarg(1); // stack: [ptr, x & 0xFFFFFF0000000000, code] - il.Or(); // stack: [ptr, (x & 0xFFFFFF0000000000) | code] - il.Ldloc(x); // stack: [ptr, (x & 0xFFFFFF0000000000) | code, newCode] - var methodInfo = typeof(Interlocked).GetMethod("CompareExchange", BindingFlags.Static | BindingFlags.Public, null, new[] {typeof(long).MakeByRefType(), typeof(long), typeof(long)}, null); - il.Call(methodInfo); // stack: [Interlocked.CompareExchange(ptr, (x & 0xFFFFFF0000000000) | code, newCode)] - il.Ldloc(x); // stack: [Interlocked.CompareExchange(ptr, (x & 0xFFFFFF0000000000) | code, newCode), newCode] - il.Bne_Un(cycleLabel); // if(Interlocked.CompareExchange(ptr, (x & 0xFFFFFF0000000000) | code, newCode) != newCode) goto cycle; stack: [] - il.Ldloc(x); - il.Ret(); - } - return (Func)method.CreateDelegate(typeof(Func)); - } - - [UnmanagedFunctionPointer(CallingConvention.StdCall)] - private delegate void CompareExchange2WordsDelegate(IntPtr location, UIntPtr lo, UIntPtr hi); - - private static unsafe CompareExchange2WordsDelegate EmitCompareExchange2Words() - { - byte[] code; - if (IntPtr.Size == 4) - { - code = new byte[] - { - 0x56, // push esi - 0x53, // push ebx - 0x8B, 0x74, 0x24, 0x0C, // mov esi, dword [esp + 12] - 0x8B, 0x5C, 0x24, 0x10, // mov ebx, dword [esp + 16] - 0x8B, 0x4C, 0x24, 0x14, // mov ecx, dword [esp + 20] - 0x8B, 0x06, // mov eax, dword [esi] - 0x8B, 0x56, 0x04, // mov edx, dword [esi + 4] - // _loop: - 0xF0, 0x0F, 0xC7, 0x0E, // lock cmpxchg8b qword [esi] - 0x75, 0xFA, // jne _loop - 0x5B, // pop ebx - 0x5E, // pop esi - 0xC2, 0x0C, 0x00 // ret 12 - }; - } - else - { - code = new byte[] - { - 0x56, // push rsi - 0x53, // push rbx - 0x48, 0x89, 0xCE, // mov rsi, rcx - 0x48, 0x89, 0xD3, // mov rbx, rdx - 0x4C, 0x89, 0xC1, // mov rcx, r8 - 0x48, 0x8B, 0x06, // mov rax, qword [rsi] - 0x48, 0x8B, 0x56, 0x08, // mov rdx, qword [rsi + 8] - // _loop: - 0xF0, 0x48, 0x0F, 0xC7, 0x0E, // lock cmpxchg16b dqword [rsi] - 0x75, 0xF9, // jne _loop - 0x5B, // pop rbx - 0x5E, // pop rsi - 0xC3 // ret - }; - } - var ptr = Marshal.AllocHGlobal(code.Length); - MEMORY_PROTECTION_CONSTANTS oldProtect; - if (!VirtualProtect(ptr, (uint)code.Length, MEMORY_PROTECTION_CONSTANTS.PAGE_EXECUTE_READWRITE, &oldProtect)) - return null; - Marshal.Copy(code, 0, ptr, code.Length); - return (CompareExchange2WordsDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(CompareExchange2WordsDelegate)); - } - - /// - /// Gets the address of the method stub - /// - /// The method handle. - /// - public static IntPtr GetMethodAddress(MethodBase method) - { - if (method is DynamicMethod) - return GetDynamicMethodAddress(method); - if (method.GetType() == rtDynamicMethodType) - return GetDynamicMethodAddress(m_ownerExtractor(method)); - - // Prepare the method so it gets jited - RuntimeHelpers.PrepareMethod(method.MethodHandle); - - return method.MethodHandle.GetFunctionPointer(); - } - - private static IntPtr GetDynamicMethodAddress(MethodBase method) - { - var handle = GetDynamicMethodRuntimeHandle(method); - RuntimeHelpers.PrepareMethod(handle); - return handle.GetFunctionPointer(); - } - - private static RuntimeMethodHandle GetDynamicMethodRuntimeHandle(MethodBase method) - { - RuntimeMethodHandle handle; - - if (Environment.Version.Major == 4) - { - var getMethodDescriptorInfo = typeof(DynamicMethod).GetMethod("GetMethodDescriptor", - BindingFlags.NonPublic | BindingFlags.Instance); - handle = (RuntimeMethodHandle)getMethodDescriptorInfo.Invoke(method, null); - } - else - { - var fieldInfo = typeof(DynamicMethod).GetField("m_method", BindingFlags.NonPublic | BindingFlags.Instance); - handle = ((RuntimeMethodHandle)fieldInfo.GetValue(method)); - } - - return handle; - } - - private static readonly CompareExchange2WordsDelegate compareExchange2Words = EmitCompareExchange2Words(); - private static readonly Func relJmpHooker = EmitRelJmpHooker(); - private static readonly Type rtDynamicMethodType = typeof(DynamicMethod).GetNestedType("RTDynamicMethod", BindingFlags.NonPublic); - private static readonly Func m_ownerExtractor = FieldsExtractor.GetExtractor(rtDynamicMethodType.GetField("m_owner", BindingFlags.NonPublic | BindingFlags.Instance)); - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/ByteBuffer.cs b/GrEmit/MethodBodyParsing/ByteBuffer.cs deleted file mode 100644 index 5e3de16..0000000 --- a/GrEmit/MethodBodyParsing/ByteBuffer.cs +++ /dev/null @@ -1,348 +0,0 @@ -// -// 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; - -namespace GrEmit.MethodBodyParsing -{ - public class ByteBuffer - { - public ByteBuffer() - { - this.buffer = Empty.Array; - } - - public ByteBuffer(int length) - { - this.buffer = new byte[length]; - } - - public ByteBuffer(byte[] buffer) - { - this.buffer = buffer ?? Empty.Array; - this.length = this.buffer.Length; - } - - public void Reset(byte[] buffer) - { - this.buffer = buffer ?? Empty.Array; - this.length = this.buffer.Length; - } - - public void Advance(int length) - { - position += length; - } - - public byte ReadByte() - { - return buffer[position++]; - } - - public sbyte ReadSByte() - { - return (sbyte)ReadByte(); - } - - public byte[] ReadBytes(int length) - { - var bytes = new byte[length]; - Buffer.BlockCopy(buffer, position, bytes, 0, length); - position += length; - return bytes; - } - - public ushort ReadUInt16() - { - ushort value = (ushort)(buffer[position] - | (buffer[position + 1] << 8)); - position += 2; - return value; - } - - public short ReadInt16() - { - return (short)ReadUInt16(); - } - - public uint ReadUInt32() - { - uint value = (uint)(buffer[position] - | (buffer[position + 1] << 8) - | (buffer[position + 2] << 16) - | (buffer[position + 3] << 24)); - position += 4; - return value; - } - - public int ReadInt32() - { - return (int)ReadUInt32(); - } - - public ulong ReadUInt64() - { - uint low = ReadUInt32(); - uint high = ReadUInt32(); - - return (((ulong)high) << 32) | low; - } - - public long ReadInt64() - { - return (long)ReadUInt64(); - } - - public uint ReadCompressedUInt32() - { - byte first = ReadByte(); - if ((first & 0x80) == 0) - return first; - - if ((first & 0x40) == 0) - { - return ((uint)(first & ~0x80) << 8) - | ReadByte(); - } - - return ((uint)(first & ~0xc0) << 24) - | (uint)ReadByte() << 16 - | (uint)ReadByte() << 8 - | ReadByte(); - } - - public int ReadCompressedInt32() - { - var value = (int)(ReadCompressedUInt32() >> 1); - if ((value & 1) == 0) - return value; - if (value < 0x40) - return value - 0x40; - if (value < 0x2000) - return value - 0x2000; - if (value < 0x10000000) - return value - 0x10000000; - return value - 0x20000000; - } - - public float ReadSingle() - { - if (!BitConverter.IsLittleEndian) - { - var bytes = ReadBytes(4); - Array.Reverse(bytes); - return BitConverter.ToSingle(bytes, 0); - } - - float value = BitConverter.ToSingle(buffer, position); - position += 4; - return value; - } - - public double ReadDouble() - { - if (!BitConverter.IsLittleEndian) - { - var bytes = ReadBytes(8); - Array.Reverse(bytes); - return BitConverter.ToDouble(bytes, 0); - } - - double value = BitConverter.ToDouble(buffer, position); - position += 8; - return value; - } - - internal byte[] buffer; - internal int length; - internal int position; - -#if !READ_ONLY - - public void WriteByte(byte value) - { - if (position == buffer.Length) - Grow(1); - - buffer[position++] = value; - - if (position > length) - length = position; - } - - public void WriteSByte(sbyte value) - { - WriteByte((byte)value); - } - - public void WriteUInt16(ushort value) - { - if (position + 2 > buffer.Length) - Grow(2); - - buffer[position++] = (byte)value; - buffer[position++] = (byte)(value >> 8); - - if (position > length) - length = position; - } - - public void WriteInt16(short value) - { - WriteUInt16((ushort)value); - } - - public void WriteUInt32(uint value) - { - if (position + 4 > buffer.Length) - Grow(4); - - buffer[position++] = (byte)value; - buffer[position++] = (byte)(value >> 8); - buffer[position++] = (byte)(value >> 16); - buffer[position++] = (byte)(value >> 24); - - if (position > length) - length = position; - } - - public void WriteInt32(int value) - { - WriteUInt32((uint)value); - } - - public void WriteUInt64(ulong value) - { - if (position + 8 > buffer.Length) - Grow(8); - - buffer[position++] = (byte)value; - buffer[position++] = (byte)(value >> 8); - buffer[position++] = (byte)(value >> 16); - buffer[position++] = (byte)(value >> 24); - buffer[position++] = (byte)(value >> 32); - buffer[position++] = (byte)(value >> 40); - buffer[position++] = (byte)(value >> 48); - buffer[position++] = (byte)(value >> 56); - - if (position > length) - length = position; - } - - public void WriteInt64(long value) - { - WriteUInt64((ulong)value); - } - - public void WriteCompressedUInt32(uint value) - { - if (value < 0x80) - WriteByte((byte)value); - else if (value < 0x4000) - { - WriteByte((byte)(0x80 | (value >> 8))); - WriteByte((byte)(value & 0xff)); - } - else - { - WriteByte((byte)((value >> 24) | 0xc0)); - WriteByte((byte)((value >> 16) & 0xff)); - WriteByte((byte)((value >> 8) & 0xff)); - WriteByte((byte)(value & 0xff)); - } - } - - public void WriteCompressedInt32(int value) - { - if (value >= 0) - { - WriteCompressedUInt32((uint)(value << 1)); - return; - } - - if (value > -0x40) - value = 0x40 + value; - else if (value >= -0x2000) - value = 0x2000 + value; - else if (value >= -0x20000000) - value = 0x20000000 + value; - - WriteCompressedUInt32((uint)((value << 1) | 1)); - } - - public void WriteBytes(byte[] bytes) - { - var length = bytes.Length; - if (position + length > buffer.Length) - Grow(length); - - Buffer.BlockCopy(bytes, 0, buffer, position, length); - position += length; - - if (position > this.length) - this.length = position; - } - - public void WriteBytes(int length) - { - if (position + length > buffer.Length) - Grow(length); - - position += length; - - if (position > this.length) - this.length = position; - } - - public void WriteBytes(ByteBuffer buffer) - { - if (position + buffer.length > this.buffer.Length) - Grow(buffer.length); - - Buffer.BlockCopy(buffer.buffer, 0, this.buffer, position, buffer.length); - position += buffer.length; - - if (position > this.length) - this.length = position; - } - - public void WriteSingle(float value) - { - var bytes = BitConverter.GetBytes(value); - - if (!BitConverter.IsLittleEndian) - Array.Reverse(bytes); - - WriteBytes(bytes); - } - - public void WriteDouble(double value) - { - var bytes = BitConverter.GetBytes(value); - - if (!BitConverter.IsLittleEndian) - Array.Reverse(bytes); - - WriteBytes(bytes); - } - - void Grow(int desired) - { - var current = this.buffer; - var current_length = current.Length; - - var buffer = new byte[Math.Max(current_length + desired, current_length * 2)]; - Buffer.BlockCopy(current, 0, buffer, 0, current_length); - this.buffer = buffer; - } - -#endif - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/Code.cs b/GrEmit/MethodBodyParsing/Code.cs deleted file mode 100644 index 1a98f8f..0000000 --- a/GrEmit/MethodBodyParsing/Code.cs +++ /dev/null @@ -1,235 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// -// Licensed under the MIT/X11 license. -// - -namespace GrEmit.MethodBodyParsing -{ - public enum Code - { - Nop, - Break, - Ldarg_0, - Ldarg_1, - Ldarg_2, - Ldarg_3, - Ldloc_0, - Ldloc_1, - Ldloc_2, - Ldloc_3, - Stloc_0, - Stloc_1, - Stloc_2, - Stloc_3, - Ldarg_S, - Ldarga_S, - Starg_S, - Ldloc_S, - Ldloca_S, - Stloc_S, - Ldnull, - Ldc_I4_M1, - Ldc_I4_0, - Ldc_I4_1, - Ldc_I4_2, - Ldc_I4_3, - Ldc_I4_4, - Ldc_I4_5, - Ldc_I4_6, - Ldc_I4_7, - Ldc_I4_8, - Ldc_I4_S, - Ldc_I4, - Ldc_I8, - Ldc_R4, - Ldc_R8, - Dup, - Pop, - Jmp, - Call, - Calli, - Ret, - Br_S, - Brfalse_S, - Brtrue_S, - Beq_S, - Bge_S, - Bgt_S, - Ble_S, - Blt_S, - Bne_Un_S, - Bge_Un_S, - Bgt_Un_S, - Ble_Un_S, - Blt_Un_S, - Br, - Brfalse, - Brtrue, - Beq, - Bge, - Bgt, - Ble, - Blt, - Bne_Un, - Bge_Un, - Bgt_Un, - Ble_Un, - Blt_Un, - Switch, - Ldind_I1, - Ldind_U1, - Ldind_I2, - Ldind_U2, - Ldind_I4, - Ldind_U4, - Ldind_I8, - Ldind_I, - Ldind_R4, - Ldind_R8, - Ldind_Ref, - Stind_Ref, - Stind_I1, - Stind_I2, - Stind_I4, - Stind_I8, - Stind_R4, - Stind_R8, - Add, - Sub, - Mul, - Div, - Div_Un, - Rem, - Rem_Un, - And, - Or, - Xor, - Shl, - Shr, - Shr_Un, - Neg, - Not, - Conv_I1, - Conv_I2, - Conv_I4, - Conv_I8, - Conv_R4, - Conv_R8, - Conv_U4, - Conv_U8, - Callvirt, - Cpobj, - Ldobj, - Ldstr, - Newobj, - Castclass, - Isinst, - Conv_R_Un, - Unbox, - Throw, - Ldfld, - Ldflda, - Stfld, - Ldsfld, - Ldsflda, - Stsfld, - Stobj, - Conv_Ovf_I1_Un, - Conv_Ovf_I2_Un, - Conv_Ovf_I4_Un, - Conv_Ovf_I8_Un, - Conv_Ovf_U1_Un, - Conv_Ovf_U2_Un, - Conv_Ovf_U4_Un, - Conv_Ovf_U8_Un, - Conv_Ovf_I_Un, - Conv_Ovf_U_Un, - Box, - Newarr, - Ldlen, - Ldelema, - Ldelem_I1, - Ldelem_U1, - Ldelem_I2, - Ldelem_U2, - Ldelem_I4, - Ldelem_U4, - Ldelem_I8, - Ldelem_I, - Ldelem_R4, - Ldelem_R8, - Ldelem_Ref, - Stelem_I, - Stelem_I1, - Stelem_I2, - Stelem_I4, - Stelem_I8, - Stelem_R4, - Stelem_R8, - Stelem_Ref, - Ldelem_Any, - Stelem_Any, - Unbox_Any, - Conv_Ovf_I1, - Conv_Ovf_U1, - Conv_Ovf_I2, - Conv_Ovf_U2, - Conv_Ovf_I4, - Conv_Ovf_U4, - Conv_Ovf_I8, - Conv_Ovf_U8, - Refanyval, - Ckfinite, - Mkrefany, - Ldtoken, - Conv_U2, - Conv_U1, - Conv_I, - Conv_Ovf_I, - Conv_Ovf_U, - Add_Ovf, - Add_Ovf_Un, - Mul_Ovf, - Mul_Ovf_Un, - Sub_Ovf, - Sub_Ovf_Un, - Endfinally, - Leave, - Leave_S, - Stind_I, - Conv_U, - Arglist, - Ceq, - Cgt, - Cgt_Un, - Clt, - Clt_Un, - Ldftn, - Ldvirtftn, - Ldarg, - Ldarga, - Starg, - Ldloc, - Ldloca, - Stloc, - Localloc, - Endfilter, - Unaligned, - Volatile, - Tail, - Initobj, - Constrained, - Cpblk, - Initblk, - No, - Rethrow, - Sizeof, - Refanytype, - Readonly, - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/Collection.cs b/GrEmit/MethodBodyParsing/Collection.cs deleted file mode 100644 index be9786f..0000000 --- a/GrEmit/MethodBodyParsing/Collection.cs +++ /dev/null @@ -1,432 +0,0 @@ -// -// 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 System.Collections; -using System.Collections.Generic; - -namespace GrEmit.MethodBodyParsing -{ - public class Collection : IList, IList - { - public Collection() - { - items = Empty.Array; - } - - public Collection(int capacity) - { - if (capacity < 0) - throw new ArgumentOutOfRangeException(); - - items = new T[capacity]; - } - - public Collection(ICollection items) - { - if (items == null) - throw new ArgumentNullException("items"); - - this.items = new T[items.Count]; - items.CopyTo(this.items, 0); - this.size = this.items.Length; - } - - public int Count { get { return size; } } - - public T this[int index] - { - get - { - if (index >= size) - throw new ArgumentOutOfRangeException(); - - return items[index]; - } - set - { - CheckIndex(index); - if (index == size) - throw new ArgumentOutOfRangeException(); - - OnSet(value, index); - - items[index] = value; - } - } - - public int Capacity - { - get { return items.Length; } - set - { - if (value < 0 || value < size) - throw new ArgumentOutOfRangeException(); - - Resize(value); - } - } - - bool ICollection.IsReadOnly { get { return false; } } - - bool IList.IsFixedSize { get { return false; } } - - bool IList.IsReadOnly { get { return false; } } - - object IList.this[int index] - { - get { return this[index]; } - set - { - CheckIndex(index); - - try - { - this[index] = (T)value; - return; - } - catch (InvalidCastException) - { - } - catch (NullReferenceException) - { - } - - throw new ArgumentException(); - } - } - - int ICollection.Count { get { return Count; } } - - bool ICollection.IsSynchronized { get { return false; } } - - object ICollection.SyncRoot { get { return this; } } - - public void Add(T item) - { - if (size == items.Length) - Grow(1); - - OnAdd(item, size); - - items[size++] = item; - version++; - } - - public bool Contains(T item) - { - return IndexOf(item) != -1; - } - - public int IndexOf(T item) - { - return Array.IndexOf(items, item, 0, size); - } - - public void Insert(int index, T item) - { - CheckIndex(index); - if (size == items.Length) - Grow(1); - - OnInsert(item, index); - - Shift(index, 1); - items[index] = item; - version++; - } - - public void RemoveAt(int index) - { - if (index < 0 || index >= size) - throw new ArgumentOutOfRangeException(); - - var item = items[index]; - - OnRemove(item, index); - - Shift(index, -1); - version++; - } - - public bool Remove(T item) - { - var index = IndexOf(item); - if (index == -1) - return false; - - OnRemove(item, index); - - Shift(index, -1); - version++; - - return true; - } - - public void Clear() - { - OnClear(); - - Array.Clear(items, 0, size); - size = 0; - version++; - } - - public void CopyTo(T[] array, int arrayIndex) - { - Array.Copy(items, 0, array, arrayIndex, size); - } - - public T[] ToArray() - { - var array = new T[size]; - Array.Copy(items, 0, array, 0, size); - return array; - } - - void CheckIndex(int index) - { - if (index < 0 || index > size) - throw new ArgumentOutOfRangeException(); - } - - void Shift(int start, int delta) - { - if (delta < 0) - start -= delta; - - if (start < size) - Array.Copy(items, start, items, start + delta, size - start); - - size += delta; - - if (delta < 0) - Array.Clear(items, size, -delta); - } - - protected virtual void OnAdd(T item, int index) - { - } - - protected virtual void OnInsert(T item, int index) - { - } - - protected virtual void OnSet(T item, int index) - { - } - - protected virtual void OnRemove(T item, int index) - { - } - - protected virtual void OnClear() - { - } - - internal virtual void Grow(int desired) - { - int new_size = size + desired; - if (new_size <= items.Length) - return; - - const int default_capacity = 4; - - new_size = Math.Max( - Math.Max(items.Length * 2, default_capacity), - new_size); - - Resize(new_size); - } - - protected void Resize(int new_size) - { - if (new_size == size) - return; - if (new_size < size) - throw new ArgumentOutOfRangeException(); - - items = items.Resize(new_size); - } - - int IList.Add(object value) - { - try - { - Add((T)value); - return size - 1; - } - catch (InvalidCastException) - { - } - catch (NullReferenceException) - { - } - - throw new ArgumentException(); - } - - void IList.Clear() - { - Clear(); - } - - bool IList.Contains(object value) - { - return ((IList)this).IndexOf(value) > -1; - } - - int IList.IndexOf(object value) - { - try - { - return IndexOf((T)value); - } - catch (InvalidCastException) - { - } - catch (NullReferenceException) - { - } - - return -1; - } - - void IList.Insert(int index, object value) - { - CheckIndex(index); - - try - { - Insert(index, (T)value); - return; - } - catch (InvalidCastException) - { - } - catch (NullReferenceException) - { - } - - throw new ArgumentException(); - } - - void IList.Remove(object value) - { - try - { - Remove((T)value); - } - catch (InvalidCastException) - { - } - catch (NullReferenceException) - { - } - } - - void IList.RemoveAt(int index) - { - RemoveAt(index); - } - - void ICollection.CopyTo(Array array, int index) - { - Array.Copy(items, 0, array, index, size); - } - - public Enumerator GetEnumerator() - { - return new Enumerator(this); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return new Enumerator(this); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return new Enumerator(this); - } - - public struct Enumerator : IEnumerator, IDisposable - { - internal Enumerator(Collection collection) - : this() - { - this.collection = collection; - this.version = collection.version; - } - - public T Current { get; private set; } - - object IEnumerator.Current - { - get - { - CheckState(); - - if (next <= 0) - throw new InvalidOperationException(); - - return Current; - } - } - - public bool MoveNext() - { - CheckState(); - - if (next < 0) - return false; - - if (next < collection.size) - { - Current = collection.items[next++]; - return true; - } - - next = -1; - return false; - } - - public void Reset() - { - CheckState(); - - next = 0; - } - - void CheckState() - { - if (collection == null) - throw new ObjectDisposedException(GetType().FullName); - - if (version != collection.version) - throw new InvalidOperationException(); - } - - public void Dispose() - { - collection = null; - } - - Collection collection; - - int next; - readonly int version; - } - - internal T[] items; - internal int size; - int version; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/DynamicILGenerator.cs b/GrEmit/MethodBodyParsing/DynamicILGenerator.cs deleted file mode 100644 index 20b7790..0000000 --- a/GrEmit/MethodBodyParsing/DynamicILGenerator.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Reflection; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - internal class DynamicILGenerator - { - static DynamicILGenerator() - { - var assembly = typeof(DynamicMethod).Assembly; - var dynamicILGeneratorType = assembly.GetType("System.Reflection.Emit.DynamicILGenerator"); - - var m_methodSigTokenField = dynamicILGeneratorType.GetField("m_methodSigToken", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_methodSigTokenField == null) - throw new InvalidOperationException("Field 'DynamicILGenerator.m_methodSigToken' is not found"); - m_methodSigTokenExtractor = FieldsExtractor.GetExtractor(m_methodSigTokenField); - - var m_scopeField = dynamicILGeneratorType.GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_scopeField == null) - throw new InvalidOperationException("Field 'DynamicILGenerator.m_scope' is not found"); - m_scopeExtractor = FieldsExtractor.GetExtractor(m_scopeField); - } - - public DynamicILGenerator(ILGenerator inst) - { - this.inst = inst; - } - - public int m_methodSigToken => m_methodSigTokenExtractor(inst); - public DynamicScope m_scope => new DynamicScope(m_scopeExtractor(inst)); - - public static void Init() - { - } - - public ILGenerator inst; - - private static readonly Func m_methodSigTokenExtractor; - private static readonly Func m_scopeExtractor; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/DynamicILInfoWrapper.cs b/GrEmit/MethodBodyParsing/DynamicILInfoWrapper.cs deleted file mode 100644 index 27a0df9..0000000 --- a/GrEmit/MethodBodyParsing/DynamicILInfoWrapper.cs +++ /dev/null @@ -1,69 +0,0 @@ -using System; -using System.Reflection; - -namespace GrEmit.MethodBodyParsing -{ - internal class DynamicILInfoWrapper - { - static DynamicILInfoWrapper() - { - var m_methodSignatureField = typeof(GrEmit.Utils.DynamicILInfo).GetField("m_methodSignature", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_methodSignatureField == null) - throw new InvalidOperationException("Field 'DynamicILInfo.m_methodSignature' is not found"); - m_methodSignatureExtractor = FieldsExtractor.GetExtractor(m_methodSignatureField); - - var m_scopeField = typeof(GrEmit.Utils.DynamicILInfo).GetField("m_scope", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_scopeField == null) - throw new InvalidOperationException("Field 'DynamicILInfo.m_scope' is not found"); - m_scopeExtractor = FieldsExtractor.GetExtractor(m_scopeField); - } - - public DynamicILInfoWrapper(GrEmit.Utils.DynamicILInfo inst) - { - this.inst = inst; - } - - public int m_methodSignature => m_methodSignatureExtractor(inst); - public DynamicScope m_scope => new DynamicScope(m_scopeExtractor(inst)); - - public static void Init() - { - } - - public MetadataToken GetTokenFor(OpCode opCode, object value) - { - if (value is MetadataToken) - return (MetadataToken)value; - if (value is MethodBase) - return GetTokenForMethod((MethodBase)value, opCode); - if (value is FieldInfo) - { - var field = (FieldInfo)value; - if (field.DeclaringType != null && field.DeclaringType.IsGenericType) - return new MetadataToken((uint)inst.GetTokenFor(field.FieldHandle, field.DeclaringType.TypeHandle)); - return new MetadataToken((uint)inst.GetTokenFor(field.FieldHandle)); - } - if (value is Type) - return new MetadataToken((uint)inst.GetTokenFor(((Type)value).TypeHandle)); - if (value is byte[]) - return new MetadataToken((uint)inst.GetTokenFor((byte[])value)); - if (value is string) - return new MetadataToken((uint)inst.GetTokenFor((string)value)); - throw new InvalidOperationException($"Unable to build token for {value.GetType()}"); - } - - private MetadataToken GetTokenForMethod(MethodBase methodBase, OpCode opcode) - { - if (opcode == OpCodes.Call || opcode == OpCodes.Callvirt) - return m_scope.GetTokenFor(methodBase, MetadataExtensions.BuildMemberRefSignature(methodBase)); - if (methodBase.DeclaringType != null && methodBase.DeclaringType.IsGenericType) - return new MetadataToken((uint)inst.GetTokenFor(methodBase.MethodHandle, methodBase.DeclaringType.TypeHandle)); - return new MetadataToken((uint)inst.GetTokenFor(methodBase.MethodHandle)); - } - - public GrEmit.Utils.DynamicILInfo inst; - - private static readonly Func m_methodSignatureExtractor; - private static readonly Func m_scopeExtractor; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/DynamicMethodWrapper.cs b/GrEmit/MethodBodyParsing/DynamicMethodWrapper.cs deleted file mode 100644 index e7ebba0..0000000 --- a/GrEmit/MethodBodyParsing/DynamicMethodWrapper.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.Reflection; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - internal class DynamicMethodWrapper - { - static DynamicMethodWrapper() - { - var assembly = typeof(DynamicMethod).Assembly; - dynamicScopeType = assembly.GetType("System.Reflection.Emit.DynamicScope"); - - var m_DynamicILInfoField = typeof(DynamicMethod).GetField("m_DynamicILInfo", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_DynamicILInfoField == null) - throw new InvalidOperationException("Field 'DynamicMethod.m_DynamicILInfo' is not found"); - m_DynamicILInfoExtractor = FieldsExtractor.GetExtractor(m_DynamicILInfoField); - - var m_ilGeneratorField = typeof(DynamicMethod).GetField("m_ilGenerator", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_ilGeneratorField == null) - throw new InvalidOperationException("Field 'DynamicMethod.m_ilGenerator' is not found"); - m_ilGeneratorExtractor = FieldsExtractor.GetExtractor(m_ilGeneratorField); - - getDynamicIlInfo = BuildGetDynamicILInfo(); - } - - public DynamicMethodWrapper(DynamicMethod inst) - { - this.inst = inst; - } - - private static Func BuildGetDynamicILInfo() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(GrEmit.Utils.DynamicILInfo), new[] {typeof(DynamicMethod), typeof(object)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); // stack: [dynamicMethod] - il.Ldarg(1); // stack: [dynamicMethod, scope] - il.Castclass(dynamicScopeType); - var getDynamicILInfoMethod = typeof(DynamicMethod).GetMethod("GetDynamicILInfo", BindingFlags.Instance | BindingFlags.NonPublic); - if (getDynamicILInfoMethod == null) - throw new MissingMethodException("DynamicMethod", "GetDynamicILInfo"); - il.Call(getDynamicILInfoMethod); // stack: [dynamicMethod.GetDynamicILInfo(scope)] - il.Ret(); - } - - return (Func)method.CreateDelegate(typeof(Func)); - } - - public GrEmit.Utils.DynamicILInfo m_DynamicILInfo => m_DynamicILInfoExtractor(inst); - public ILGenerator m_ilGenerator => m_ilGeneratorExtractor(inst); - - public static void Init() - { - } - - public GrEmit.Utils.DynamicILInfo GetDynamicILInfoWithOldScope() - { - return m_DynamicILInfo ?? (m_ilGenerator == null ? GetDynamicILInfo() : getDynamicIlInfo(inst, new DynamicILGenerator(m_ilGenerator).m_scope.inst)); - } - - private GrEmit.Utils.DynamicILInfo GetDynamicILInfo() - { - if (info == null) - { - info = CreateDynamicILInfo(new GrEmit.Utils.DynamicScope()); - } - - return info; - } - - private GrEmit.Utils.DynamicILInfo CreateDynamicILInfo(GrEmit.Utils.DynamicScope scope) - { - SignatureHelper helper = SignatureHelper.GetMethodSigHelper(inst.CallingConvention, inst.ReturnType); - foreach (var parameter in inst.GetParameters()) - { - helper.AddArgument(parameter.ParameterType); - } - byte[] methodSignature = helper.GetSignature(); - - // Have to do this, since needed method is internal - byte[] methodSignatureWithEnd = new byte[methodSignature.Length + 1]; - Array.Copy(methodSignature, methodSignatureWithEnd, methodSignature.Length); - methodSignatureWithEnd[methodSignatureWithEnd.Length - 1] = 0; - - return new GrEmit.Utils.DynamicILInfo(scope, inst, methodSignatureWithEnd); - } - - public DynamicMethod inst; - public GrEmit.Utils.DynamicILInfo info; - - private static readonly Func m_DynamicILInfoExtractor; - private static readonly Func m_ilGeneratorExtractor; - private static readonly Type dynamicScopeType; - private static readonly Func getDynamicIlInfo; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/DynamicResolver.cs b/GrEmit/MethodBodyParsing/DynamicResolver.cs deleted file mode 100644 index 4911079..0000000 --- a/GrEmit/MethodBodyParsing/DynamicResolver.cs +++ /dev/null @@ -1,186 +0,0 @@ -using System; -using System.Reflection; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - internal class DynamicResolver : IDisposable - { - static DynamicResolver() - { - var assembly = typeof(DynamicMethod).Assembly; - dynamicResolverType = assembly.GetType("System.Reflection.Emit.DynamicResolver"); - dynamicILInfoType = assembly.GetType("System.Reflection.Emit.DynamicILInfo"); - dynamicILGeneratorType = assembly.GetType("System.Reflection.Emit.DynamicILGenerator"); - BuildFactoryByDynamicILInfo(); - BuildFactoryByDynamicILGenerator(); - BuildGetCodeInfoDelegate(); - BuildGetRawEHInfoDelegate(); - BuildGetEHInfoDelegate(); - - var m_methodField = dynamicResolverType.GetField("m_method", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_methodField == null) - throw new InvalidOperationException("Field 'DynamicResolver.m_methodField' is not found"); - m_methodSetter = FieldsExtractor.GetSetter(m_methodField); - - var m_resolverField = typeof(DynamicMethod).GetField("m_resolver", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_resolverField == null) - throw new InvalidOperationException("Field 'DynamicResolver.m_resolver' is not found"); - m_resolverSetter = FieldsExtractor.GetSetter(m_resolverField); - - var m_localSignatureField = dynamicResolverType.GetField("m_localSignature", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_localSignatureField == null) - throw new InvalidOperationException("Field 'DynamicResolver.m_localSignature' is not found"); - m_localSignatureExtractor = FieldsExtractor.GetExtractor(m_localSignatureField); - } - - public DynamicResolver(DynamicMethod dynamicMethod, GrEmit.Utils.DynamicILInfo dynamicILInfo) - { - this.dynamicMethod = dynamicMethod; - inst = factoryByDynamicILInfo(dynamicILInfo); - } - - public DynamicResolver(DynamicMethod dynamicMethod, ILGenerator ilGenerator) - { - this.dynamicMethod = dynamicMethod; - inst = factoryByDynamicILGenerator(ilGenerator); - } - - private static void BuildGetCodeInfoDelegate() - { - var parameterTypes = new[] {typeof(object), typeof(int).MakeByRefType(), typeof(int).MakeByRefType(), typeof(int).MakeByRefType()}; - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(byte[]), parameterTypes, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); - il.Castclass(dynamicResolverType); - il.Ldarg(1); - il.Ldarg(2); - il.Ldarg(3); - var getCodeInfoMethod = dynamicResolverType.GetMethod("GetCodeInfo", BindingFlags.Instance | BindingFlags.NonPublic); - if (getCodeInfoMethod == null) - throw new MissingMethodException("DynamicResolver", "GetCodeInfo"); - il.Call(getCodeInfoMethod); - il.Ret(); - } - - getCodeInfoDelegate = (GetCodeInfoDelegate)method.CreateDelegate(typeof(GetCodeInfoDelegate)); - } - - private static void BuildGetEHInfoDelegate() - { - var parameterTypes = new[] {typeof(object), typeof(int), typeof(void*)}; - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(void), parameterTypes, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); - il.Castclass(dynamicResolverType); - il.Ldarg(1); - il.Ldarg(2); - var getEHInfoMethod = dynamicResolverType.GetMethod("GetEHInfo", BindingFlags.Instance | BindingFlags.NonPublic); - if (getEHInfoMethod == null) - throw new MissingMethodException("DynamicResolver", "GetEHInfo"); - il.Call(getEHInfoMethod); - il.Ret(); - } - - getEHInfoDelegate = (GetEHInfoDelegate)method.CreateDelegate(typeof(GetEHInfoDelegate)); - } - - private static void BuildGetRawEHInfoDelegate() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(byte[]), new[] {typeof(object)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); - il.Castclass(dynamicResolverType); - var getRawEHInfoMethod = dynamicResolverType.GetMethod("GetRawEHInfo", BindingFlags.Instance | BindingFlags.NonPublic); - if (getRawEHInfoMethod == null) - throw new MissingMethodException("DynamicResolver", "GetRawEHInfo"); - il.Call(getRawEHInfoMethod); - il.Ret(); - } - - getRawEHInfoDelegate = (Func)method.CreateDelegate(typeof(Func)); - } - - private static void BuildFactoryByDynamicILInfo() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(object), new[] {dynamicILInfoType}, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); - var constructor = dynamicResolverType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {dynamicILInfoType}, null); - if (constructor == null) - throw new MissingMethodException("DynamicResolver", ".ctor"); - il.Newobj(constructor); - il.Ret(); - } - - factoryByDynamicILInfo = (Func)method.CreateDelegate(typeof(Func)); - } - - private static void BuildFactoryByDynamicILGenerator() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(object), new[] {typeof(ILGenerator)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); - il.Castclass(dynamicILGeneratorType); - var constructor = dynamicResolverType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {dynamicILGeneratorType}, null); - if (constructor == null) - throw new MissingMethodException("DynamicResolver", ".ctor"); - il.Newobj(constructor); - il.Ret(); - } - - factoryByDynamicILGenerator = (Func)method.CreateDelegate(typeof(Func)); - } - - private delegate byte[] GetCodeInfoDelegate(object inst, out int stackSize, out int initLocals, out int EHCount); - - private unsafe delegate void GetEHInfoDelegate(object inst, int excNumber, void* exc); - - public void Dispose() - { - m_methodSetter(inst, null); - m_resolverSetter(dynamicMethod, null); - } - - public byte[] GetCodeInfo(out int stackSize, out int initLocals, out int EHCount) - { - return getCodeInfoDelegate(inst, out stackSize, out initLocals, out EHCount); - } - - public byte[] GetRawEHInfo() - { - return getRawEHInfoDelegate(inst); - } - - public unsafe void GetEHInfo(int excNumber, void* exc) - { - getEHInfoDelegate(inst, excNumber, exc); - } - - public byte[] m_localSignature => m_localSignatureExtractor(inst); - - public static void Init() - { - } - - private readonly DynamicMethod dynamicMethod; - private readonly object inst; - - private static Func factoryByDynamicILInfo; - private static Func factoryByDynamicILGenerator; - private static GetCodeInfoDelegate getCodeInfoDelegate; - private static GetEHInfoDelegate getEHInfoDelegate; - private static Func getRawEHInfoDelegate; - private static readonly Action m_methodSetter; - private static readonly Type dynamicResolverType; - private static readonly Type dynamicILInfoType; - private static readonly Action m_resolverSetter; - private static readonly Func m_localSignatureExtractor; - private static readonly Type dynamicILGeneratorType; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/DynamicScope.cs b/GrEmit/MethodBodyParsing/DynamicScope.cs deleted file mode 100644 index 710fb80..0000000 --- a/GrEmit/MethodBodyParsing/DynamicScope.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; -using System.Reflection; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - internal class DynamicScope - { - static DynamicScope() - { - var assembly = typeof(DynamicMethod).Assembly; - dynamicScopeType = assembly.GetType("System.Reflection.Emit.DynamicScope"); - varArgsMethodType = assembly.GetType("System.Reflection.Emit.VarArgMethod"); - runtimeMethodInfoType = assembly.GetType("System.Reflection.RuntimeMethodInfo"); - - itemGetter = BuildItemGetter(); - getTokenFor = BuildGetTokenFor(); - } - - public DynamicScope(object inst) - { - this.inst = inst; - } - - private static Func BuildItemGetter() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(object), new[] {typeof(object), typeof(int)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); // stack: [scope] - il.Castclass(dynamicScopeType); - il.Ldarg(1); // stack: [scope, token] - var property = dynamicScopeType.GetProperty("Item", BindingFlags.Instance | BindingFlags.NonPublic); - if (property == null) - throw new MissingMethodException("DynamicScope", "get_Item"); - var getter = property.GetGetMethod(true); - il.Call(getter); // stack: [scope[this]] - il.Ret(); - } - - return (Func)method.CreateDelegate(typeof(Func)); - } - - private static Func BuildGetTokenFor() - { - var parameterTypes = new[] {typeof(object), typeof(MethodBase), typeof(SignatureHelper)}; - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(uint), parameterTypes, typeof(string), true); - using (var il = new GroboIL(method)) - { - il.Ldarg(0); // stack: [scope] - il.Castclass(dynamicScopeType); - il.Ldarg(1); // stack: [scope, method] - il.Castclass(runtimeMethodInfoType); // stack: [scope, (RuntimeMethodInfo)method] - il.Ldarg(2); // stack: [scope, (RuntimeMethodInfo)method, signature] - - var constructor = varArgsMethodType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {runtimeMethodInfoType, typeof(SignatureHelper)}, null); - if (constructor == null) - throw new MissingMethodException("VarArgsMethod", ".ctor"); - var getTokenForMethod = dynamicScopeType.GetMethod("GetTokenFor", BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {varArgsMethodType}, null); - if (getTokenForMethod == null) - throw new MissingMethodException("DynamicScope", "GetTokenFor"); - - il.Newobj(constructor); // stack: [scope, new VarArgsMethod((RuntimeMethodInfo)method, signature)] - il.Call(getTokenForMethod); // stack: [scope.GetTokenFor(new VarArgsMethod((RuntimeMethodInfo)method, signature))] - - il.Ret(); - } - - return (Func)method.CreateDelegate(typeof(Func)); - } - - public object this[int token] => itemGetter(inst, token); - - public MetadataToken GetTokenFor(MethodBase method, SignatureHelper signature) - { - return new MetadataToken(getTokenFor(inst, method, signature)); - } - - public static void Init() - { - } - - public readonly object inst; - - private static readonly Func itemGetter; - private static readonly Func getTokenFor; - private static readonly Type dynamicScopeType; - private static readonly Type varArgsMethodType; - private static readonly Type runtimeMethodInfoType; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/ElementType.cs b/GrEmit/MethodBodyParsing/ElementType.cs deleted file mode 100644 index cf9d48c..0000000 --- a/GrEmit/MethodBodyParsing/ElementType.cs +++ /dev/null @@ -1,56 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// -// Licensed under the MIT/X11 license. -// - -namespace GrEmit.MethodBodyParsing -{ - internal enum ElementType : byte - { - None = 0x00, - Void = 0x01, - Boolean = 0x02, - Char = 0x03, - I1 = 0x04, - U1 = 0x05, - I2 = 0x06, - U2 = 0x07, - I4 = 0x08, - U4 = 0x09, - I8 = 0x0a, - U8 = 0x0b, - R4 = 0x0c, - R8 = 0x0d, - String = 0x0e, - Ptr = 0x0f, // Followed by token - ByRef = 0x10, // Followed by token - ValueType = 0x11, // Followed by token - Class = 0x12, // Followed by token - Var = 0x13, // Followed by generic parameter number - Array = 0x14, // - GenericInst = 0x15, // ... */ - TypedByRef = 0x16, - I = 0x18, // System.IntPtr - U = 0x19, // System.UIntPtr - FnPtr = 0x1b, // Followed by full method signature - Object = 0x1c, // System.Object - SzArray = 0x1d, // Single-dim array with 0 lower bound - MVar = 0x1e, // Followed by generic parameter number - CModReqD = 0x1f, // Required modifier : followed by a TypeDef or TypeRef token - CModOpt = 0x20, // Optional modifier : followed by a TypeDef or TypeRef token - Internal = 0x21, // Implemented within the CLI - Modifier = 0x40, // Or'd with following element types - Sentinel = 0x41, // Sentinel for varargs method signature - Pinned = 0x45, // Denotes a local variable that points at a pinned object - - // special undocumented constants - Type = 0x50, - Boxed = 0x51, - Enum = 0x55 - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/Empty.cs b/GrEmit/MethodBodyParsing/Empty.cs deleted file mode 100644 index f08b0ce..0000000 --- a/GrEmit/MethodBodyParsing/Empty.cs +++ /dev/null @@ -1,41 +0,0 @@ -// -// 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; - -namespace GrEmit.MethodBodyParsing -{ - internal static class Empty - { - public static readonly T[] Array = new T[0]; - } -} - -namespace GrEmit.MethodBodyParsing -{ - internal static partial class Mixin - { - public static bool IsNullOrEmpty(this T[] self) - { - return self == null || self.Length == 0; - } - - public static bool IsNullOrEmpty(this Collection self) - { - return self == null || self.size == 0; - } - - public static T[] Resize(this T[] self, int length) - { - Array.Resize(ref self, length); - return self; - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/ExceptionHandler.cs b/GrEmit/MethodBodyParsing/ExceptionHandler.cs deleted file mode 100644 index 36e9058..0000000 --- a/GrEmit/MethodBodyParsing/ExceptionHandler.cs +++ /dev/null @@ -1,56 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System.Text; - -namespace GrEmit.MethodBodyParsing -{ - public enum ExceptionHandlerType - { - Catch = 0, - Filter = 1, - Finally = 2, - Fault = 4, - } - - public sealed class ExceptionHandler - { - public ExceptionHandler(ExceptionHandlerType handlerType) - { - HandlerType = handlerType; - } - - public Instruction TryStart { get; set; } - - public Instruction TryEnd { get; set; } - - public Instruction FilterStart { get; set; } - - public Instruction HandlerStart { get; set; } - - public Instruction HandlerEnd { get; set; } - - public object CatchType { get; set; } - - public ExceptionHandlerType HandlerType { get; } - - public override string ToString() - { - var result = new StringBuilder(); - - result.AppendLine(HandlerType.ToString()); - result.AppendLine($"TryStart: {TryStart}, TryEnd: {TryEnd}"); - result.AppendLine($"HandlerStart: {HandlerStart}, HandlerEnd: {HandlerEnd}"); - - return result.ToString(); - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/ExceptionsBaker.cs b/GrEmit/MethodBodyParsing/ExceptionsBaker.cs deleted file mode 100644 index b63f7e7..0000000 --- a/GrEmit/MethodBodyParsing/ExceptionsBaker.cs +++ /dev/null @@ -1,157 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - internal sealed class ExceptionsBaker : ByteBuffer - { - public ExceptionsBaker(Collection exceptionHandlers, - Collection instructions, - Func tokenBuilder) - : base(0) - { - handlers = exceptionHandlers; - this.instructions = instructions; - this.tokenBuilder = tokenBuilder; - } - - public byte[] BakeExceptions() - { - if (handlers.IsNullOrEmpty()) - return Empty.Array; - - WriteExceptions(); - - var temp = new byte[length]; - Array.Copy(buffer, temp, length); - return temp; - } - - private void WriteExceptions() - { - if (handlers.Count < 0x15 && !RequiresFatSection(handlers)) - WriteSmallSection(); - else - WriteFatSection(); - } - - private static bool RequiresFatSection(Collection handlers) - { - foreach (var handler in handlers) - { - if (IsFatRange(handler.TryStart, handler.TryEnd)) - return true; - - if (IsFatRange(handler.HandlerStart, handler.HandlerEnd)) - return true; - - if (handler.HandlerType == ExceptionHandlerType.Filter - && IsFatRange(handler.FilterStart, handler.HandlerStart)) - return true; - } - - return false; - } - - private static bool IsFatRange(Instruction start, Instruction end) - { - if (start == null) - throw new ArgumentException(); - - if (end == null) - return true; - - return end.Offset - start.Offset > 255 || start.Offset > 65535; - } - - private void WriteSmallSection() - { - const byte eh_table = 0x1; - - WriteByte(eh_table); - WriteByte((byte)(handlers.Count * 12 + 4)); - WriteBytes(2); - - WriteExceptionHandlers( - i => WriteUInt16((ushort)i), - i => WriteByte((byte)i)); - } - - private void WriteFatSection() - { - const byte eh_table = 0x1; - const byte fat_format = 0x40; - - WriteByte(eh_table | fat_format); - - int size = handlers.Count * 24 + 4; - WriteByte((byte)(size & 0xff)); - WriteByte((byte)((size >> 8) & 0xff)); - WriteByte((byte)((size >> 16) & 0xff)); - - WriteExceptionHandlers(WriteInt32, WriteInt32); - } - - private void WriteExceptionHandlers(Action writeEntry, Action writeLength) - { - foreach (var handler in handlers) - { - writeEntry((int)handler.HandlerType); - - writeEntry(handler.TryStart.Offset); - writeLength(GetTargetOffset(handler.TryEnd) - handler.TryStart.Offset); - - writeEntry(handler.HandlerStart.Offset); - writeLength(GetTargetOffset(handler.HandlerEnd) - handler.HandlerStart.Offset); - - WriteExceptionHandlerSpecific(handler); - } - } - - private void WriteExceptionHandlerSpecific(ExceptionHandler handler) - { - switch (handler.HandlerType) - { - case ExceptionHandlerType.Catch: - WriteMetadataToken(tokenBuilder(default(OpCode), handler.CatchType)); - break; - case ExceptionHandlerType.Filter: - WriteInt32(handler.FilterStart.Offset); - break; - default: - WriteInt32(0); - break; - } - } - - private int GetTargetOffset(Instruction instruction) - { - if (instruction == null) - { - var last = instructions[instructions.size - 1]; - return last.Offset + last.GetSize(); - } - - return instruction.Offset; - } - - private void WriteMetadataToken(MetadataToken token) - { - WriteUInt32(token.ToUInt32()); - } - - private readonly Collection instructions; - private readonly Func tokenBuilder; - private readonly Collection handlers; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/ExceptionsInfoReader.cs b/GrEmit/MethodBodyParsing/ExceptionsInfoReader.cs deleted file mode 100644 index c31cc54..0000000 --- a/GrEmit/MethodBodyParsing/ExceptionsInfoReader.cs +++ /dev/null @@ -1,129 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - internal sealed unsafe class ExceptionsInfoReader : UnmanagedByteBuffer - { - public ExceptionsInfoReader(byte* buffer, Func tokenResolver, bool resolveTokens) - : base(buffer) - { - this.tokenResolver = tokenResolver; - this.resolveTokens = resolveTokens; - } - - public static void Read(byte[] buffer, Func tokenResolver, bool resolveTokens, MethodBody body) - { - if (buffer != null && buffer.Length > 0) - { - fixed (byte* b = &buffer[0]) - new ExceptionsInfoReader(b, tokenResolver, resolveTokens).Read(body); - } - } - - public void Read(MethodBody body) - { - this.body = body; - ReadSection(); - } - - private void ReadSection() - { - position = 0; - const byte fat_format = 0x40; - const byte more_sects = 0x80; - - var flags = ReadByte(); - if ((flags & fat_format) == 0) - ReadSmallSection(); - else - ReadFatSection(); - - if ((flags & more_sects) != 0) - ReadSection(); - } - - private void ReadSmallSection() - { - var count = ReadByte() / 12; - Advance(2); - - ReadExceptionHandlers( - count, - () => (int)ReadUInt16(), - () => (int)ReadByte()); - } - - private void ReadFatSection() - { - position--; - var count = (ReadInt32() >> 8) / 24; - - ReadExceptionHandlers( - count, - ReadInt32, - ReadInt32); - } - - private void ReadExceptionHandlers(int count, Func read_entry, Func read_length) - { - for (int i = 0; i < count; i++) - { - var handler = new ExceptionHandler( - (ExceptionHandlerType)(read_entry() & 0x7)); - - handler.TryStart = GetInstruction(read_entry()); - handler.TryEnd = GetInstruction(handler.TryStart.Offset + read_length()); - - handler.HandlerStart = GetInstruction(read_entry()); - handler.HandlerEnd = GetInstruction(handler.HandlerStart.Offset + read_length()); - - ReadExceptionHandlerSpecific(handler); - - body.ExceptionHandlers.Add(handler); - } - } - - private void ReadExceptionHandlerSpecific(ExceptionHandler handler) - { - switch (handler.HandlerType) - { - case ExceptionHandlerType.Catch: - handler.CatchType = ReadToken(); - break; - case ExceptionHandlerType.Filter: - handler.FilterStart = GetInstruction(ReadInt32()); - break; - default: - Advance(4); - break; - } - } - - private Instruction GetInstruction(int offset) - { - return body.Instructions.GetInstruction(offset); - } - - private object ReadToken() - { - var token = new MetadataToken(ReadUInt32()); - return resolveTokens ? tokenResolver(token) : token; - } - - private readonly Func tokenResolver; - private readonly bool resolveTokens; - - private MethodBody body; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/FullMethodBodyBaker.cs b/GrEmit/MethodBodyParsing/FullMethodBodyBaker.cs deleted file mode 100644 index d8a44e0..0000000 --- a/GrEmit/MethodBodyParsing/FullMethodBodyBaker.cs +++ /dev/null @@ -1,99 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - internal sealed class FullMethodBodyBaker : ByteBuffer - { - public FullMethodBodyBaker(MethodBody body, Func tokenBuilder) - : base(0) - { - this.body = body; - this.tokenBuilder = (opCode, operand) => - { - if (operand is MetadataToken) - return (MetadataToken)operand; - if (tokenBuilder == null) - throw new InvalidOperationException($"Operand {operand} is not resolved to metadata token"); - return tokenBuilder(opCode, operand); - }; - } - - public byte[] BakeMethodBody() - { - WriteMethodBody(); - - var temp = new byte[length]; - Array.Copy(buffer, temp, length); - return temp; - } - - private void WriteMethodBody() - { - var ilCode = new ILCodeBaker(body.Instructions, tokenBuilder).BakeILCode(); - codeSize = ilCode.Length; - - if (RequiresFatHeader()) - WriteFatHeader(); - else - WriteByte((byte)(0x2 | (codeSize << 2))); // tiny - - WriteBytes(ilCode); - - if (body.HasExceptionHandlers) - { - Align(4); - WriteBytes(new ExceptionsBaker(body.ExceptionHandlers, body.Instructions, tokenBuilder).BakeExceptions()); - } - } - - private void WriteFatHeader() - { - byte flags = 0x3; // fat - if (body.InitLocals) - flags |= 0x10; // init locals - if (body.HasExceptionHandlers) - flags |= 0x8; // more sections - - WriteByte(flags); - WriteByte(0x30); - WriteInt16((short)body.MaxStack); - WriteInt32(codeSize); - WriteMetadataToken(body.LocalVarToken); - } - - private bool RequiresFatHeader() - { - return codeSize >= 64 - || body.InitLocals - || body.LocalVariablesCount() > 0 - || body.HasExceptionHandlers - || body.MaxStack > 8; - } - - private void WriteMetadataToken(MetadataToken token) - { - WriteUInt32(token.ToUInt32()); - } - - private void Align(int align) - { - align--; - WriteBytes(((position + align) & ~align) - position); - } - - private readonly MethodBody body; - private readonly Func tokenBuilder; - private int codeSize; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/ILCodeBaker.cs b/GrEmit/MethodBodyParsing/ILCodeBaker.cs deleted file mode 100644 index 8055461..0000000 --- a/GrEmit/MethodBodyParsing/ILCodeBaker.cs +++ /dev/null @@ -1,154 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - internal sealed class ILCodeBaker : ByteBuffer - { - public ILCodeBaker(Collection instructions, Func tokenBuilder) - : base(0) - { - this.instructions = instructions; - this.tokenBuilder = tokenBuilder; - } - - public byte[] BakeILCode() - { - WriteInstructions(); - - var temp = new byte[length]; - Array.Copy(buffer, temp, length); - return temp; - } - - private void WriteInstructions() - { - foreach (var instruction in instructions) - { - WriteOpCode(instruction.OpCode); - WriteOperand(instruction); - } - } - - private void WriteOpCode(OpCode opcode) - { - if (opcode.Size == 1) - WriteByte(opcode.Op2); - else - { - WriteByte(opcode.Op1); - WriteByte(opcode.Op2); - } - } - - private void WriteOperand(Instruction instruction) - { - var opcode = instruction.OpCode; - var operandType = opcode.OperandType; - if (operandType == OperandType.InlineNone) - return; - - var operand = instruction.Operand; - if (operand == null) - throw new ArgumentException(); - - switch (operandType) - { - case OperandType.InlineSwitch: - { - var targets = (Instruction[])operand; - WriteInt32(targets.Length); - var diff = instruction.Offset + opcode.Size + (4 * (targets.Length + 1)); - foreach (var target in targets) - WriteInt32(GetTargetOffset(target) - diff); - break; - } - case OperandType.ShortInlineBrTarget: - { - var target = (Instruction)operand; - WriteSByte((sbyte)(GetTargetOffset(target) - (instruction.Offset + opcode.Size + 1))); - break; - } - case OperandType.InlineBrTarget: - { - var target = (Instruction)operand; - WriteInt32(GetTargetOffset(target) - (instruction.Offset + opcode.Size + 4)); - break; - } - case OperandType.ShortInlineVar: - WriteByte((byte)(int)operand); - break; - case OperandType.ShortInlineArg: - WriteByte((byte)(int)operand); - break; - case OperandType.InlineVar: - WriteInt16((short)(int)operand); - break; - case OperandType.InlineArg: - WriteInt16((short)(int)operand); - break; - case OperandType.InlineSig: - WriteMetadataToken(tokenBuilder(opcode, operand)); - break; - case OperandType.ShortInlineI: - if (opcode == OpCodes.Ldc_I4_S) - WriteSByte((sbyte)operand); - else - WriteByte((byte)operand); - break; - case OperandType.InlineI: - WriteInt32((int)operand); - break; - case OperandType.InlineI8: - WriteInt64((long)operand); - break; - case OperandType.ShortInlineR: - WriteSingle((float)operand); - break; - case OperandType.InlineR: - WriteDouble((double)operand); - break; - case OperandType.InlineString: - WriteMetadataToken(tokenBuilder(opcode, operand)); - break; - case OperandType.InlineType: - case OperandType.InlineField: - case OperandType.InlineMethod: - case OperandType.InlineTok: - WriteMetadataToken(tokenBuilder(opcode, operand)); - break; - default: - throw new ArgumentException(); - } - } - - private int GetTargetOffset(Instruction instruction) - { - if (instruction == null) - { - var last = instructions[instructions.size - 1]; - return last.Offset + last.GetSize(); - } - - return instruction.Offset; - } - - private void WriteMetadataToken(MetadataToken token) - { - WriteUInt32(token.ToUInt32()); - } - - private readonly Collection instructions; - private readonly Func tokenBuilder; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/ILCodeReader.cs b/GrEmit/MethodBodyParsing/ILCodeReader.cs deleted file mode 100644 index 6ddc118..0000000 --- a/GrEmit/MethodBodyParsing/ILCodeReader.cs +++ /dev/null @@ -1,159 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - internal sealed unsafe class ILCodeReader : UnmanagedByteBuffer - { - public ILCodeReader(byte* buffer, int codeSize, Func tokenResolver, bool resolveTokens) - : base(buffer) - { - this.codeSize = codeSize; - this.tokenResolver = tokenResolver; - this.resolveTokens = resolveTokens; - } - - public static void Read(byte[] buffer, Func tokenResolver, bool resolveTokens, MethodBody body) - { - fixed (byte* b = &buffer[0]) - new ILCodeReader(b, buffer.Length, tokenResolver, resolveTokens).Read(body); - } - - public void Read(MethodBody body) - { - this.body = body; - ReadCode(); - } - - private void ReadCode() - { - position = 0; - var end = codeSize; - var instructions = body.Instructions; - - while (position < end) - { - var offset = position; - var opcode = ReadOpCode(); - var current = new Instruction(offset, opcode); - - if (opcode.OperandType != OperandType.InlineNone) - current.Operand = ReadOperand(current); - - instructions.Add(current); - } - - ResolveBranches(instructions); - } - - private OpCode ReadOpCode() - { - var il_opcode = ReadByte(); - return il_opcode != 0xfe - ? OpCodes.OneByteOpCode[il_opcode] - : OpCodes.TwoBytesOpCode[ReadByte()]; - } - - private object ReadOperand(Instruction instruction) - { - switch (instruction.OpCode.OperandType) - { - case OperandType.InlineSwitch: - var length = ReadInt32(); - var base_offset = position + (4 * length); - var branches = new int[length]; - for (int i = 0; i < length; i++) - branches[i] = base_offset + ReadInt32(); - return branches; - case OperandType.ShortInlineBrTarget: - return ReadSByte() + position; - case OperandType.InlineBrTarget: - return ReadInt32() + position; - case OperandType.ShortInlineI: - if (instruction.OpCode == OpCodes.Ldc_I4_S) - return ReadSByte(); - - return ReadByte(); - case OperandType.InlineI: - return ReadInt32(); - case OperandType.ShortInlineR: - return ReadSingle(); - case OperandType.InlineR: - return ReadDouble(); - case OperandType.InlineI8: - return ReadInt64(); - case OperandType.ShortInlineVar: - return (int)ReadByte(); - case OperandType.InlineVar: - return (int)ReadUInt16(); - case OperandType.ShortInlineArg: - return (int)ReadByte(); - case OperandType.InlineArg: - return (int)ReadUInt16(); - case OperandType.InlineSig: - return ReadToken(); - case OperandType.InlineString: - return ReadToken(); - case OperandType.InlineTok: - case OperandType.InlineType: - case OperandType.InlineMethod: - case OperandType.InlineField: - return ReadToken(); - default: - throw new NotSupportedException(); - } - } - - private void ResolveBranches(Collection instructions) - { - var items = instructions.items; - var size = instructions.size; - - for (int i = 0; i < size; i++) - { - var instruction = items[i]; - switch (instruction.OpCode.OperandType) - { - case OperandType.ShortInlineBrTarget: - case OperandType.InlineBrTarget: - instruction.Operand = GetInstruction((int)instruction.Operand); - break; - case OperandType.InlineSwitch: - var offsets = (int[])instruction.Operand; - var branches = new Instruction[offsets.Length]; - for (int j = 0; j < offsets.Length; j++) - branches[j] = GetInstruction(offsets[j]); - - instruction.Operand = branches; - break; - } - } - } - - private Instruction GetInstruction(int offset) - { - return body.Instructions.GetInstruction(offset); - } - - private object ReadToken() - { - var token = new MetadataToken(ReadUInt32()); - return resolveTokens ? tokenResolver(token) : token; - } - - private readonly int codeSize; - private readonly Func tokenResolver; - private readonly bool resolveTokens; - private MethodBody body; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/Instruction.cs b/GrEmit/MethodBodyParsing/Instruction.cs deleted file mode 100644 index 8214d5b..0000000 --- a/GrEmit/MethodBodyParsing/Instruction.cs +++ /dev/null @@ -1,269 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; -using System.Reflection; -using System.Text; - -namespace GrEmit.MethodBodyParsing -{ - public sealed class Instruction - { - internal Instruction(int offset, OpCode opCode) - { - Offset = offset; - OpCode = opCode; - } - - internal Instruction(OpCode opcode, object operand) - { - OpCode = opcode; - Operand = operand; - } - - public int Offset { get; set; } - - public OpCode OpCode { get; set; } - - public object Operand { get; set; } - - public Instruction Previous { get; set; } - - public Instruction Next { get; set; } - - public int GetSize() - { - int size = OpCode.Size; - - switch (OpCode.OperandType) - { - case OperandType.InlineSwitch: - return size + (1 + ((Instruction[])Operand).Length) * 4; - case OperandType.InlineI8: - case OperandType.InlineR: - return size + 8; - case OperandType.InlineBrTarget: - case OperandType.InlineField: - case OperandType.InlineI: - case OperandType.InlineMethod: - case OperandType.InlineString: - case OperandType.InlineTok: - case OperandType.InlineType: - case OperandType.ShortInlineR: - case OperandType.InlineSig: - return size + 4; - case OperandType.InlineArg: - case OperandType.InlineVar: - return size + 2; - case OperandType.ShortInlineBrTarget: - case OperandType.ShortInlineI: - case OperandType.ShortInlineArg: - case OperandType.ShortInlineVar: - return size + 1; - default: - return size; - } - } - - public override string ToString() - { - var instruction = new StringBuilder(); - - AppendLabel(instruction, this); - instruction.Append(':'); - instruction.Append(' '); - instruction.Append(OpCode.Name); - - if (Operand == null) - return instruction.ToString(); - - instruction.Append(' '); - - switch (OpCode.OperandType) - { - case OperandType.ShortInlineBrTarget: - case OperandType.InlineBrTarget: - AppendLabel(instruction, (Instruction)Operand); - break; - case OperandType.InlineSwitch: - var labels = (Instruction[])Operand; - for (int i = 0; i < labels.Length; i++) - { - if (i > 0) - instruction.Append(','); - - AppendLabel(instruction, labels[i]); - } - break; - case OperandType.InlineString: - instruction.Append('\"'); - instruction.Append(Operand); - instruction.Append('\"'); - break; - default: - instruction.Append(Operand); - break; - } - - return instruction.ToString(); - } - - private static void AppendLabel(StringBuilder builder, Instruction instruction) - { - builder.Append("IL_"); - builder.Append(instruction.Offset.ToString("D4")); - } - - public static Instruction Create(OpCode opcode) - { - if (opcode.OperandType != OperandType.InlineNone) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, null); - } - - public static Instruction Create(OpCode opcode, MetadataToken value) - { - if (value == MetadataToken.Zero) - throw new ArgumentNullException(nameof(value)); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, Type type) - { - if (type == null) - throw new ArgumentNullException(nameof(type)); - if (opcode.OperandType != OperandType.InlineType - && opcode.OperandType != OperandType.InlineTok) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, type); - } - - public static Instruction Create(OpCode opcode, byte[] site) - { - if (site == null) - throw new ArgumentNullException(nameof(site)); - if (opcode.Code != Code.Calli) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, site); - } - - public static Instruction Create(OpCode opcode, MethodBase method) - { - if (method == null) - throw new ArgumentNullException(nameof(method)); - if (opcode.OperandType != OperandType.InlineMethod - && opcode.OperandType != OperandType.InlineTok) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, method); - } - - public static Instruction Create(OpCode opcode, FieldInfo field) - { - if (field == null) - throw new ArgumentNullException(nameof(field)); - if (opcode.OperandType != OperandType.InlineField - && opcode.OperandType != OperandType.InlineTok) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, field); - } - - public static Instruction Create(OpCode opcode, string value) - { - if (value == null) - throw new ArgumentNullException(nameof(value)); - if (opcode.OperandType != OperandType.InlineString) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, sbyte value) - { - if (opcode.OperandType != OperandType.ShortInlineI - && opcode != OpCodes.Ldc_I4_S) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, byte value) - { - if (opcode.OperandType != OperandType.ShortInlineI || - opcode == OpCodes.Ldc_I4_S) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, int value) - { - if (opcode.OperandType != OperandType.InlineI - && opcode.OperandType != OperandType.InlineVar - && opcode.OperandType != OperandType.ShortInlineVar - && opcode.OperandType != OperandType.InlineArg - && opcode.OperandType != OperandType.ShortInlineArg) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, long value) - { - if (opcode.OperandType != OperandType.InlineI8) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, float value) - { - if (opcode.OperandType != OperandType.ShortInlineR) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, double value) - { - if (opcode.OperandType != OperandType.InlineR) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, value); - } - - public static Instruction Create(OpCode opcode, Instruction target) - { - if (target == null) - throw new ArgumentNullException(nameof(target)); - if (opcode.OperandType != OperandType.InlineBrTarget && - opcode.OperandType != OperandType.ShortInlineBrTarget) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, target); - } - - public static Instruction Create(OpCode opcode, Instruction[] targets) - { - if (targets == null) - throw new ArgumentNullException(nameof(targets)); - if (opcode.OperandType != OperandType.InlineSwitch) - throw new ArgumentException("opcode"); - - return new Instruction(opcode, targets); - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/InstructionCollection.cs b/GrEmit/MethodBodyParsing/InstructionCollection.cs deleted file mode 100644 index a3f297a..0000000 --- a/GrEmit/MethodBodyParsing/InstructionCollection.cs +++ /dev/null @@ -1,112 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -namespace GrEmit.MethodBodyParsing -{ - public class InstructionCollection : Collection - { - internal InstructionCollection() - { - } - - internal InstructionCollection(int capacity) - : base(capacity) - { - } - - public Instruction GetInstruction(int offset) - { - var size = this.size; - var items = this.items; - if (offset < 0 || offset > items[size - 1].Offset) - return null; - - int min = 0; - int max = size - 1; - while (min <= max) - { - int mid = min + ((max - min) / 2); - var instruction = items[mid]; - var instruction_offset = instruction.Offset; - - if (offset == instruction_offset) - return instruction; - - if (offset < instruction_offset) - max = mid - 1; - else - min = mid + 1; - } - - return null; - } - - protected override void OnAdd(Instruction item, int index) - { - if (index == 0) - return; - - var previous = items[index - 1]; - previous.Next = item; - item.Previous = previous; - } - - protected override void OnInsert(Instruction item, int index) - { - if (size == 0) - return; - - var current = items[index]; - if (current == null) - { - var last = items[index - 1]; - last.Next = item; - item.Previous = last; - return; - } - - var previous = current.Previous; - if (previous != null) - { - previous.Next = item; - item.Previous = previous; - } - - current.Previous = item; - item.Next = current; - } - - protected override void OnSet(Instruction item, int index) - { - var current = items[index]; - - item.Previous = current.Previous; - item.Next = current.Next; - - current.Previous = null; - current.Next = null; - } - - protected override void OnRemove(Instruction item, int index) - { - var previous = item.Previous; - if (previous != null) - previous.Next = item.Next; - - var next = item.Next; - if (next != null) - next.Previous = item.Previous; - - item.Previous = null; - item.Next = null; - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/LocalInfo.cs b/GrEmit/MethodBodyParsing/LocalInfo.cs deleted file mode 100644 index 91828a9..0000000 --- a/GrEmit/MethodBodyParsing/LocalInfo.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace GrEmit.MethodBodyParsing -{ - public class LocalInfo - { - public LocalInfo(byte[] signature) - { - Signature = signature; - } - - public LocalInfo(Type localType, bool isPinned) - { - LocalType = localType; - IsPinned = isPinned; - } - - public int LocalIndex = -1; - public byte[] Signature; - public Type LocalType; - public bool IsPinned; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/LocalInfoCollection.cs b/GrEmit/MethodBodyParsing/LocalInfoCollection.cs deleted file mode 100644 index 2a80812..0000000 --- a/GrEmit/MethodBodyParsing/LocalInfoCollection.cs +++ /dev/null @@ -1,40 +0,0 @@ -namespace GrEmit.MethodBodyParsing -{ - internal class LocalInfoCollection : Collection - { - internal LocalInfoCollection() - { - } - - internal LocalInfoCollection(int capacity) - : base(capacity) - { - } - - protected override void OnAdd(LocalInfo item, int index) - { - item.LocalIndex = index; - } - - protected override void OnInsert(LocalInfo item, int index) - { - item.LocalIndex = index; - - for (int i = index; i < size; i++) - items[i].LocalIndex = i + 1; - } - - protected override void OnSet(LocalInfo item, int index) - { - item.LocalIndex = index; - } - - protected override void OnRemove(LocalInfo item, int index) - { - item.LocalIndex = -1; - - for (int i = index + 1; i < size; i++) - items[i].LocalIndex = i - 1; - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/LocalVarSigBuilder.cs b/GrEmit/MethodBodyParsing/LocalVarSigBuilder.cs deleted file mode 100644 index 9276370..0000000 --- a/GrEmit/MethodBodyParsing/LocalVarSigBuilder.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - internal class LocalVarSigBuilder - { - public LocalVarSigBuilder() - { - localVariables = new LocalInfoCollection(); - } - - public LocalVarSigBuilder(byte[] oldLocalSignature) - { - localVariables = new SignatureReader(oldLocalSignature).ReadLocalVarSig(); - } - - public LocalInfo AddLocalVariable(byte[] signature) - { - var localInfo = new LocalInfo(signature); - localVariables.Add(localInfo); - return localInfo; - } - - public LocalInfo AddLocalVariable(Type localType, bool isPinned = false) - { - var localInfo = new LocalInfo(localType, isPinned); - localVariables.Add(localInfo); - return localInfo; - } - - public byte[] GetSignature() - { - var writer = new ByteBuffer(); - writer.WriteByte(0x7); - writer.WriteCompressedUInt32((uint)Count); - - foreach (var localInfo in localVariables) - writer.WriteBytes(localInfo.Signature ?? BakeLocal(localInfo)); - - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - private static byte[] BakeLocal(LocalInfo localInfo) - { - if (localInfo.LocalType == null) - throw new ArgumentException(); - - var sigHelper = SignatureHelper.GetLocalVarSigHelper(); - sigHelper.AddArgument(localInfo.LocalType, localInfo.IsPinned); - - var withHeader = sigHelper.GetSignature(); - - byte[] result = new byte[withHeader.Length - 1 - 1]; // first byte is 0x7 (LOCAL_SIG) and second is 0x1 (Count) - - Array.Copy(withHeader, 2, result, 0, result.Length); - - return result; - } - - public int Count => localVariables.Count; - - public LocalInfo this[int index] => localVariables[index]; - - private readonly LocalInfoCollection localVariables; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MaxStackSizeCalculator.cs b/GrEmit/MethodBodyParsing/MaxStackSizeCalculator.cs deleted file mode 100644 index 8c491ae..0000000 --- a/GrEmit/MethodBodyParsing/MaxStackSizeCalculator.cs +++ /dev/null @@ -1,250 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; -using System.Collections.Generic; -using System.Reflection; - -namespace GrEmit.MethodBodyParsing -{ - public class MaxStackSizeCalculator - { - public MaxStackSizeCalculator(MethodBody body, Func tokenResolver) - { - this.body = body; - this.tokenResolver = tokenResolver; - } - - public int ComputeMaxStack() - { - var stack_size = 0; - var max_stack = 0; - Dictionary stack_sizes = null; - - if (body.HasExceptionHandlers) - ComputeExceptionHandlerStackSize(ref stack_sizes); - - foreach (var instruction in body.Instructions) - ComputeStackSize(instruction, ref stack_sizes, ref stack_size, ref max_stack); - - return max_stack; - } - - private void ComputeExceptionHandlerStackSize(ref Dictionary stack_sizes) - { - foreach (var handler in body.ExceptionHandlers) - { - switch (handler.HandlerType) - { - case ExceptionHandlerType.Catch: - AddExceptionStackSize(handler.HandlerStart, ref stack_sizes); - break; - case ExceptionHandlerType.Filter: - AddExceptionStackSize(handler.FilterStart, ref stack_sizes); - AddExceptionStackSize(handler.HandlerStart, ref stack_sizes); - break; - } - } - } - - private static void AddExceptionStackSize(Instruction handler_start, ref Dictionary stack_sizes) - { - if (handler_start == null) - return; - - if (stack_sizes == null) - stack_sizes = new Dictionary(); - - stack_sizes[handler_start] = 1; - } - - private void ComputeStackSize(Instruction instruction, ref Dictionary stack_sizes, ref int stack_size, ref int max_stack) - { - int computed_size; - if (stack_sizes != null && stack_sizes.TryGetValue(instruction, out computed_size)) - stack_size = computed_size; - - max_stack = Math.Max(max_stack, stack_size); - ComputeStackDelta(instruction, ref stack_size); - max_stack = Math.Max(max_stack, stack_size); - - CopyBranchStackSize(instruction, ref stack_sizes, stack_size); - ComputeStackSize(instruction, ref stack_size); - } - - private static void CopyBranchStackSize(Instruction instruction, ref Dictionary stack_sizes, int stack_size) - { - if (stack_size == 0) - return; - - switch (instruction.OpCode.OperandType) - { - case OperandType.ShortInlineBrTarget: - case OperandType.InlineBrTarget: - CopyBranchStackSize(ref stack_sizes, (Instruction)instruction.Operand, stack_size); - break; - case OperandType.InlineSwitch: - var targets = (Instruction[])instruction.Operand; - for (int i = 0; i < targets.Length; i++) - CopyBranchStackSize(ref stack_sizes, targets[i], stack_size); - break; - } - } - - private static void CopyBranchStackSize(ref Dictionary stack_sizes, Instruction target, int stack_size) - { - if (stack_sizes == null) - stack_sizes = new Dictionary(); - - int branch_stack_size = stack_size; - - int computed_size; - if (stack_sizes.TryGetValue(target, out computed_size)) - branch_stack_size = Math.Max(branch_stack_size, computed_size); - - stack_sizes[target] = branch_stack_size; - } - - private static void ComputeStackSize(Instruction instruction, ref int stack_size) - { - switch (instruction.OpCode.FlowControl) - { - case FlowControl.Branch: - case FlowControl.Break: - case FlowControl.Throw: - case FlowControl.Return: - stack_size = 0; - break; - } - } - - private static MethodSignature GetMethodSignature(MethodBase method) - { - return new MethodSignature - { - hasThis = method.CallingConvention.HasFlag(CallingConventions.HasThis) - && !method.CallingConvention.HasFlag(CallingConventions.ExplicitThis), - parametersCount = method.GetParameters().Length, - hasReturnType = method is MethodInfo && ((MethodInfo)method).ReturnType != typeof(void) - }; - } - - private MethodSignature GetMethodSignature(MetadataToken token, OpCode opCode) - { - if (opCode.Code != Code.Calli) - return GetMethodSignature((MethodBase)tokenResolver(token)); - var signature = (byte[])tokenResolver(token); - var parsedSignature = new SignatureReader(signature).ReadAndParseMethodSignature(); - return new MethodSignature - { - hasThis = parsedSignature.HasThis && !parsedSignature.ExplicitThis, - parametersCount = parsedSignature.ParamCount, - hasReturnType = parsedSignature.HasReturnType - }; - } - - private MethodSignature GetMethodSignature(Instruction instruction) - { - if (instruction.Operand is MetadataToken) - return GetMethodSignature((MetadataToken)instruction.Operand, instruction.OpCode); - return GetMethodSignature((MethodBase)instruction.Operand); - } - - private void ComputeStackDelta(Instruction instruction, ref int stack_size) - { - switch (instruction.OpCode.FlowControl) - { - case FlowControl.Call: - { - var methodSignature = GetMethodSignature(instruction); - - // pop 'this' argument - if (methodSignature.hasThis && instruction.OpCode.Code != Code.Newobj) - stack_size--; - // pop normal arguments - stack_size -= methodSignature.parametersCount; - // pop function pointer - if (instruction.OpCode.Code == Code.Calli) - stack_size--; - // push return value - if (methodSignature.hasReturnType || instruction.OpCode.Code == Code.Newobj) - stack_size++; - break; - } - default: - ComputePopDelta(instruction.OpCode.StackBehaviourPop, ref stack_size); - ComputePushDelta(instruction.OpCode.StackBehaviourPush, ref stack_size); - break; - } - } - - private static void ComputePopDelta(StackBehaviour pop_behavior, ref int stack_size) - { - switch (pop_behavior) - { - case StackBehaviour.Popi: - case StackBehaviour.Popref: - case StackBehaviour.Pop1: - stack_size--; - break; - case StackBehaviour.Pop1_pop1: - case StackBehaviour.Popi_pop1: - case StackBehaviour.Popi_popi: - case StackBehaviour.Popi_popi8: - case StackBehaviour.Popi_popr4: - case StackBehaviour.Popi_popr8: - case StackBehaviour.Popref_pop1: - case StackBehaviour.Popref_popi: - stack_size -= 2; - break; - case StackBehaviour.Popi_popi_popi: - case StackBehaviour.Popref_popi_popi: - case StackBehaviour.Popref_popi_popi8: - case StackBehaviour.Popref_popi_popr4: - case StackBehaviour.Popref_popi_popr8: - case StackBehaviour.Popref_popi_popref: - stack_size -= 3; - break; - case StackBehaviour.PopAll: - stack_size = 0; - break; - } - } - - private static void ComputePushDelta(StackBehaviour push_behaviour, ref int stack_size) - { - switch (push_behaviour) - { - case StackBehaviour.Push1: - case StackBehaviour.Pushi: - case StackBehaviour.Pushi8: - case StackBehaviour.Pushr4: - case StackBehaviour.Pushr8: - case StackBehaviour.Pushref: - stack_size++; - break; - case StackBehaviour.Push1_push1: - stack_size += 2; - break; - } - } - - private readonly MethodBody body; - private readonly Func tokenResolver; - - private class MethodSignature - { - public bool hasThis; - public int parametersCount; - public bool hasReturnType; - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MetadataExtensions.cs b/GrEmit/MethodBodyParsing/MetadataExtensions.cs deleted file mode 100644 index ee38f83..0000000 --- a/GrEmit/MethodBodyParsing/MetadataExtensions.cs +++ /dev/null @@ -1,99 +0,0 @@ -using System; -using System.Linq; -using System.Reflection; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - public static class MetadataExtensions - { - internal static object Resolve(this Module module, MetadataToken token) - { - switch (token.TokenType) - { - case TokenType.Method: - case TokenType.MethodSpec: - return module.ResolveMethod(token.ToInt32(), universalArguments, universalArguments); - case TokenType.MemberRef: - return module.ResolveMember(token.ToInt32(), universalArguments, universalArguments); - case TokenType.Field: - return module.ResolveField(token.ToInt32(), universalArguments, universalArguments); - case TokenType.TypeDef: - case TokenType.TypeRef: - case TokenType.TypeSpec: - return module.ResolveType(token.ToInt32(), universalArguments, universalArguments); - case TokenType.Signature: - return module.ResolveSignature(token.ToInt32()); - case TokenType.String: - return module.ResolveString(token.ToInt32()); - default: - throw new NotSupportedException($"Token type ${token.TokenType} is not supported"); - } - } - - public static MethodBase ResolveMethod(Module module, MetadataToken token) - { - switch (token.TokenType) - { - case TokenType.MethodSpec: - case TokenType.Method: - return module.ResolveMethod(token.ToInt32(), universalArguments, universalArguments); - case TokenType.MemberRef: - var member = module.ResolveMember(token.ToInt32(), universalArguments, universalArguments); - switch (member.MemberType) - { - case MemberTypes.Constructor: - case MemberTypes.Method: - return (MethodBase)member; - default: - return null; - } - default: - return null; - } - } - - internal static SignatureHelper BuildMemberRefSignature(MethodBase methodBase) - { - return BuildMemberRefSignature(methodBase.CallingConvention, - GetReturnType(methodBase), - methodBase.GetParameters().Select(p => p.ParameterType).ToArray(), - null); - } - - private static SignatureHelper BuildMemberRefSignature( - CallingConventions call, - Type returnType, - Type[] parameterTypes, - Type[] optionalParameterTypes) - { - var sig = SignatureHelper.GetMethodSigHelper(call, returnType); - if (parameterTypes != null) - { - foreach (var parameterType in parameterTypes) - sig.AddArgument(parameterType); - } - if (optionalParameterTypes != null && optionalParameterTypes.Length != 0) - { - // add the sentinel - sig.AddSentinel(); - foreach (var optionalParameterType in optionalParameterTypes) - sig.AddArgument(optionalParameterType); - } - return sig; - } - - private static Type GetReturnType(MethodBase methodBase) - { - var methodInfo = methodBase as MethodInfo; - if (methodInfo != null) - return methodInfo.ReturnType; - if (methodBase is ConstructorInfo) - return typeof(void); - throw new InvalidOperationException($"{methodBase.GetType()} is not supported"); - } - - private static readonly Type __Canon = typeof(object).Assembly.GetType("System.__Canon"); - private static readonly Type[] universalArguments = Enumerable.Repeat(__Canon, 1024).ToArray(); - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MetadataToken.cs b/GrEmit/MethodBodyParsing/MetadataToken.cs deleted file mode 100644 index eb63ba6..0000000 --- a/GrEmit/MethodBodyParsing/MetadataToken.cs +++ /dev/null @@ -1,92 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - public struct MetadataToken : IEquatable - { - public MetadataToken(uint token) - { - this.token = token; - } - - public MetadataToken(TokenType type) - : this(type, 0) - { - } - - public MetadataToken(TokenType type, uint rid) - { - token = (uint)type | rid; - } - - public MetadataToken(TokenType type, int rid) - { - token = (uint)type | (uint)rid; - } - - public uint RID => token & 0x00ffffff; - - public TokenType TokenType => (TokenType)(token & 0xff000000); - - public int ToInt32() - { - return (int)token; - } - - public uint ToUInt32() - { - return token; - } - - public override int GetHashCode() - { - return (int)token; - } - - public bool Equals(MetadataToken other) - { - return other.token == token; - } - - public override bool Equals(object obj) - { - if (obj is MetadataToken) - { - var other = (MetadataToken)obj; - return other.token == token; - } - - return false; - } - - public static bool operator ==(MetadataToken one, MetadataToken other) - { - return one.token == other.token; - } - - public static bool operator !=(MetadataToken one, MetadataToken other) - { - return one.token != other.token; - } - - public override string ToString() - { - return $"[{TokenType}:0x{RID.ToString("x4")}]"; - } - - readonly uint token; - - public static readonly MetadataToken Zero = new MetadataToken((uint)0); - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodBody.cs b/GrEmit/MethodBodyParsing/MethodBody.cs deleted file mode 100644 index 4e54506..0000000 --- a/GrEmit/MethodBodyParsing/MethodBody.cs +++ /dev/null @@ -1,249 +0,0 @@ -using System; -using System.Linq; -using System.Reflection; -using System.Reflection.Emit; -using System.Text; - -using GrEmit.Utils; - -namespace GrEmit.MethodBodyParsing -{ - public abstract class MethodBody - { - protected MethodBody(byte[] methodSignature, bool resolveTokens) - { - this.resolveTokens = resolveTokens; - Instructions = new InstructionCollection(); - ExceptionHandlers = new Collection(); - MethodSignature = methodSignature; - } - - protected abstract object ResolveToken(MetadataToken token); - - public static unsafe MethodBody Read(byte* rawMethodBody, Module module, MetadataToken methodSignatureToken, bool resolveTokens) - { - return new MethodBodyOnUnmanagedBuffer(rawMethodBody, module, methodSignatureToken, resolveTokens); - } - - public static MethodBody Read(MethodBase method, bool resolveTokens) - { - var dynamicMethod = tryCastToDynamicMethod(method); - return dynamicMethod == null - ? new MethodBodyOnMethodBase(method, resolveTokens) - : Read(dynamicMethod, resolveTokens); - } - - public static unsafe MethodBody Read(DynamicMethod dynamicMethod, bool resolveTokens) - { - var wrapper = new DynamicMethodWrapper(dynamicMethod); - var dynamicILInfo = wrapper.m_DynamicILInfo; - if (dynamicILInfo != null) - return new MethodBodyOnDynamicILInfo(dynamicMethod, dynamicILInfo, resolveTokens); - var ilGenerator = wrapper.m_ilGenerator; - if (ilGenerator != null) - return new MethodBodyOnDynamicILGenerator(dynamicMethod, ilGenerator, resolveTokens); - return new MethodBodyOnUnmanagedBuffer(null, null, MetadataToken.Zero, resolveTokens); - } - - protected Instruction GetInstruction(int offset) - { - return Instructions.GetInstruction(offset); - } - - public int MaxStack { get; set; } - - public bool InitLocals { get; set; } - - internal MetadataToken LocalVarToken { get; set; } - - public byte[] MethodSignature { get; private set; } - - public bool HasExceptionHandlers { get { return !ExceptionHandlers.IsNullOrEmpty(); } } - - protected void SetLocalSignature(byte[] localSignature) - { - localVarSigBuilder = new LocalVarSigBuilder(localSignature); - if (resolveTokens) - { - for (int i = 0; i < localVarSigBuilder.Count; ++i) - { - var local = localVarSigBuilder[i]; - var resolved = new TypeSignatureReader(local.Signature, ResolveToken).Resolve(); - local.LocalType = resolved.Key; - local.IsPinned = resolved.Value; - local.Signature = null; - } - } - } - - public LocalInfo AddLocalVariable(byte[] signature) - { - if (localVarSigBuilder == null) - localVarSigBuilder = new LocalVarSigBuilder(); - return localVarSigBuilder.AddLocalVariable(signature); - } - - public LocalInfo AddLocalVariable(Type localType, bool isPinned = false) - { - if (localVarSigBuilder == null) - localVarSigBuilder = new LocalVarSigBuilder(); - return localVarSigBuilder.AddLocalVariable(localType, isPinned); - } - - public byte[] GetLocalSignature() - { - if (localVarSigBuilder == null) - localVarSigBuilder = new LocalVarSigBuilder(); - return localVarSigBuilder.GetSignature(); - } - - public int LocalVariablesCount() - { - if (localVarSigBuilder == null) - localVarSigBuilder = new LocalVarSigBuilder(); - return localVarSigBuilder.Count; - } - - public void WriteToDynamicMethod(DynamicMethod dynamicMethod, int? maxStack) - { - Seal(); - MaxStack = maxStack ?? new MaxStackSizeCalculator(this, ResolveToken).ComputeMaxStack(); - var dynamicILInfo = new DynamicMethodWrapper(dynamicMethod).GetDynamicILInfoWithOldScope(); - var wrapper = new DynamicILInfoWrapper(dynamicILInfo); - var code = new ILCodeBaker(Instructions, wrapper.GetTokenFor).BakeILCode(); - dynamicILInfo.SetCode(code, MaxStack); - dynamicILInfo.SetLocalSignature(GetLocalSignature()); - if (HasExceptionHandlers) - dynamicILInfo.SetExceptions(new ExceptionsBaker(ExceptionHandlers, Instructions, wrapper.GetTokenFor).BakeExceptions()); - } - - public Delegate CreateDelegate(Type delegateType, int? maxStack = null) - { - var invokeMethod = delegateType.GetMethod("Invoke"); - if (invokeMethod == null) - throw new InvalidOperationException(string.Format("Type '{0}' is not a delegate", Formatter.Format(delegateType))); - var parameterTypes = invokeMethod.GetParameters().Select(p => p.ParameterType).ToArray(); - var dynamicMethod = new DynamicMethod(Guid.NewGuid().ToString(), invokeMethod.ReturnType, parameterTypes, typeof(string), true); - WriteToDynamicMethod(dynamicMethod, maxStack); - return dynamicMethod.CreateDelegate(delegateType); - } - - public TDelegate CreateDelegate(int? maxStack = null) - where TDelegate : class - { - return (TDelegate)(object)CreateDelegate(typeof(TDelegate), maxStack); - } - - public Delegate CreateDelegate(Type returnType, Type[] parameterTypes, int? maxStack = null) - { - return CreateDelegate(GetDelegateType(returnType, parameterTypes), maxStack); - } - - private static Type GetDelegateType(Type returnType, Type[] parameterTypes) - { - if (returnType == typeof(void) && parameterTypes.Length == 0) - return typeof(Action); - bool isFunc = returnType != typeof(void); - - var typeName = isFunc - ? $"System.{"Func"}`{parameterTypes.Length + 1}" - : $"System.{"Action"}`{parameterTypes.Length}"; - - var type = typeof(Action).Assembly.GetType(typeName) ?? typeof(Action<,,,,,,,,>).Assembly.GetType(typeName); - if (type == null) - throw new NotSupportedException("Too many paramters"); - return type.MakeGenericType(isFunc ? parameterTypes.Concat(new[] {returnType}).ToArray() : parameterTypes); - } - - public byte[] GetFullMethodBody(Func signatureTokenBuilder, int? maxStack) - { - if (resolveTokens) - throw new InvalidOperationException("Token builder must be supplied"); - Seal(); - MaxStack = maxStack ?? new MaxStackSizeCalculator(this, ResolveToken).ComputeMaxStack(); - LocalVarToken = GetVariablesSignature(signatureTokenBuilder); - return new FullMethodBodyBaker(this, null).BakeMethodBody(); - } - - public byte[] GetFullMethodBody(Func tokenBuilder, int? maxStack) - { - Seal(); - MaxStack = maxStack ?? new MaxStackSizeCalculator(this, ResolveToken).ComputeMaxStack(); - LocalVarToken = GetVariablesSignature(signature => tokenBuilder(default(OpCode), signature)); - return new FullMethodBodyBaker(this, tokenBuilder).BakeMethodBody(); - } - - private MetadataToken GetVariablesSignature(Func signatureTokenBuilder) - { - return LocalVariablesCount() == 0 ? MetadataToken.Zero : signatureTokenBuilder(GetLocalSignature()); - } - - public void Seal() - { - Instructions.SimplifyMacros(); - Instructions.OptimizeMacros(); - - isSealed = true; - } - - public override string ToString() - { - var result = new StringBuilder(); - - result.AppendLine("Instructions:"); - foreach (var instruction in Instructions) - result.AppendLine(instruction.ToString()); - - result.AppendLine(); - - result.AppendLine("Exception handlers:"); - foreach (var exceptionHandler in ExceptionHandlers) - result.AppendLine(exceptionHandler.ToString()); - - return result.ToString(); - } - - public InstructionCollection Instructions { get; private set; } - public Collection ExceptionHandlers { get; private set; } - - public static void Init() - { - DynamicMethodWrapper.Init(); - DynamicILGenerator.Init(); - DynamicILInfoWrapper.Init(); - DynamicScope.Init(); - DynamicResolver.Init(); - } - - private static Func EmitTryCastToDynamicMethod() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(DynamicMethod), new[] {typeof(MethodBase)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - var RTDynamicMethod_t = typeof(DynamicMethod).GetNestedType("RTDynamicMethod", BindingFlags.NonPublic); - if (RTDynamicMethod_t == null) - throw new InvalidOperationException("Missing type 'System.Reflection.Emit.DynamicMethod.RTDynamicMethod'"); - il.Ldarg(0); // stack: [method] - il.Isinst(RTDynamicMethod_t); // stack: [method as RTDynamicMethod] - il.Dup(); // stack: [method as RTDynamicMethod, method as RTDynamicMethod] - var retLabel = il.DefineLabel("ret"); - il.Brfalse(retLabel); // if(!(method is RTDynamicMethod)] goto ret; stack: [method as RTDynamicMethod] - var m_owner_f = RTDynamicMethod_t.GetField("m_owner", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_owner_f == null) - throw new InvalidOperationException("Missing field 'System.Reflection.Emit.DynamicMethod.RTDynamicMethod.m_owner'"); - il.Ldfld(m_owner_f); // stack: [((RTDynamicMethod)method).m_owner] - il.MarkLabel(retLabel); - il.Ret(); - } - return (Func)method.CreateDelegate(typeof(Func)); - } - - private static readonly Func tryCastToDynamicMethod = EmitTryCastToDynamicMethod(); - - protected readonly bool resolveTokens; - - private bool isSealed; - - private LocalVarSigBuilder localVarSigBuilder; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodBodyOnDynamicILGenerator.cs b/GrEmit/MethodBodyParsing/MethodBodyOnDynamicILGenerator.cs deleted file mode 100644 index 106ae14..0000000 --- a/GrEmit/MethodBodyParsing/MethodBodyOnDynamicILGenerator.cs +++ /dev/null @@ -1,230 +0,0 @@ -using System; -using System.Reflection; -using System.Reflection.Emit; - -using GrEmit.Utils; - -namespace GrEmit.MethodBodyParsing -{ - internal static class DynamicMethodHelpers - { - public static object Resolve(object value) - { - return ResolveRuntimeMethodHandle(value) - ?? ResolveRuntimeFieldHandle(value) - ?? ResolveRuntimeTypeHandle(value) - ?? resolveGenericMethodInfo(value) - ?? resolveVarArgsMethod(value) - ?? resolveGenericFieldInfo(value) - ?? value; - } - - private static object ResolveRuntimeMethodHandle(object value) - { - if (value is RuntimeMethodHandle) - return MethodBase.GetMethodFromHandle((RuntimeMethodHandle)value); - return null; - } - - private static object ResolveRuntimeFieldHandle(object value) - { - if (value is RuntimeFieldHandle) - return FieldInfo.GetFieldFromHandle((RuntimeFieldHandle)value); - return null; - } - - private static object ResolveRuntimeTypeHandle(object value) - { - if (value is RuntimeTypeHandle) - return Type.GetTypeFromHandle((RuntimeTypeHandle)value); - return null; - } - - private static Func EmitResolveGenericMethodInfo() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(object), new[] {typeof(object)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - var GenericMethodInfo_t = typeof(DynamicMethod).Assembly.GetType("System.Reflection.Emit.GenericMethodInfo"); - if (GenericMethodInfo_t == null) - throw new InvalidOperationException("Missing type 'System.Reflection.Emit.GenericMethodInfo'"); - il.Ldarg(0); // stack: [value] - il.Isinst(GenericMethodInfo_t); // stack: [(GenericMethodInfo)value] - var retLabel = il.DefineLabel("ret"); - il.Dup(); // stack: [(GenericMethodInfo)value, (GenericMethodInfo)value] - il.Brfalse(retLabel); // if(!(value is GenericMethodInfo)) goto ret; stack: [value as GenericMethodInfo] - var m_methodHandle_f = GenericMethodInfo_t.GetField("m_methodHandle", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_methodHandle_f == null) - throw new InvalidOperationException("Missing field 'System.Reflection.Emit.GenericMethodInfo.m_methodHandle'"); - var m_context_f = GenericMethodInfo_t.GetField("m_context", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_context_f == null) - throw new InvalidOperationException("Missing field 'System.Reflection.Emit.GenericMethodInfo.m_context'"); - var temp = il.DeclareLocal(GenericMethodInfo_t); - il.Dup(); - il.Stloc(temp); // temp = (GenericMethodInfo)value; stack: [(GenericMethodInfo)value] - il.Ldfld(m_methodHandle_f); // stack: [((GenericMethodInfo)value).m_methodHandle] - il.Ldloc(temp); // stack: [((GenericMethodInfo)value).m_methodHandle, (GenericMethodInfo)value] - il.Ldfld(m_context_f); // stack: [((GenericMethodInfo)value).m_methodHandle, ((GenericMethodInfo)value).m_context] - var getMethodFromHandle_m = HackHelpers.GetMethodDefinition(x => MethodBase.GetMethodFromHandle(default(RuntimeMethodHandle), default(RuntimeTypeHandle))); - il.Call(getMethodFromHandle_m); // stack: [MethodBase.GetMethodFromHandle(((GenericMethodInfo)value).m_methodHandle, ((GenericMethodInfo)value).m_context)] - il.MarkLabel(retLabel); - il.Ret(); - } - return (Func)method.CreateDelegate(typeof(Func)); - } - - private static Func EmitResolveVarArgsMethod() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(object), new[] {typeof(object)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - var VarArgMethod_t = typeof(DynamicMethod).Assembly.GetType("System.Reflection.Emit.VarArgMethod"); - if (VarArgMethod_t == null) - throw new InvalidOperationException("Missing type 'System.Reflection.Emit.VarArgMethod'"); - il.Ldarg(0); // stack: [value] - il.Isinst(VarArgMethod_t); // stack: [(VarArgMethod)value] - var retLabel = il.DefineLabel("ret"); - il.Dup(); // stack: [(VarArgMethod)value, (VarArgMethod)value] - il.Brfalse(retLabel); // if(!(value is VarArgMethod)) goto ret; stack: [value as VarArgMethod] - var m_method_f = VarArgMethod_t.GetField("m_method", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_method_f == null) - throw new InvalidOperationException("Missing field 'System.Reflection.Emit.VarArgMethod.m_method'"); - var m_dynamicMethod_f = VarArgMethod_t.GetField("m_dynamicMethod", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_dynamicMethod_f == null) - throw new InvalidOperationException("Missing field 'System.Reflection.Emit.VarArgMethod.m_dynamicMethod'"); - var temp = il.DeclareLocal(VarArgMethod_t); - il.Dup(); - il.Stloc(temp); // temp = (VarArgMethod)value; stack: [(VarArgMethod)value] - il.Ldfld(m_method_f); // stack: [((VarArgMethod)value).m_method] - il.Dup(); // stack: [((VarArgMethod)value).m_method, ((VarArgMethod)value).m_method] - il.Brtrue(retLabel); // if(((VarArgMethod)value).m_method != null) goto ret; stack: [((VarArgMethod)value).m_method] - il.Pop(); // stack: [] - il.Ldloc(temp); // stack: [(VarArgMethod)value] - il.Ldfld(m_dynamicMethod_f); // stack: [((VarArgMethod)value).m_dynamicMethod] - il.MarkLabel(retLabel); - il.Ret(); - } - return (Func)method.CreateDelegate(typeof(Func)); - } - - private static Func EmitResolveGenericFieldInfo() - { - var method = new DynamicMethod(Guid.NewGuid().ToString(), typeof(object), new[] {typeof(object)}, typeof(string), true); - using (var il = new GroboIL(method)) - { - var GenericFieldInfo_t = typeof(DynamicMethod).Assembly.GetType("System.Reflection.Emit.GenericFieldInfo"); - if (GenericFieldInfo_t == null) - throw new InvalidOperationException("Missing type 'System.Reflection.Emit.GenericFieldInfo'"); - il.Ldarg(0); // stack: [value] - il.Isinst(GenericFieldInfo_t); // stack: [(GenericFieldInfo)value] - var retLabel = il.DefineLabel("ret"); - il.Dup(); // stack: [(GenericFieldInfo)value, (GenericFieldInfo)value] - il.Brfalse(retLabel); // if(!(value is GenericFieldInfo)) goto ret; stack: [value as GenericFieldInfo] - var m_fieldHandle_f = GenericFieldInfo_t.GetField("m_fieldHandle", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_fieldHandle_f == null) - throw new InvalidOperationException("Missing field 'System.Reflection.Emit.GenericFieldInfo.m_fieldHandle'"); - var m_context_f = GenericFieldInfo_t.GetField("m_context", BindingFlags.Instance | BindingFlags.NonPublic); - if (m_context_f == null) - throw new InvalidOperationException("Missing field 'System.Reflection.Emit.GenericFieldInfo.m_context'"); - var temp = il.DeclareLocal(GenericFieldInfo_t); - il.Dup(); - il.Stloc(temp); // temp = (GenericFieldInfo)value; stack: [(GenericFieldInfo)value] - il.Ldfld(m_fieldHandle_f); // stack: [((GenericFieldInfo)value).m_fieldHandle] - il.Ldloc(temp); // stack: [((GenericFieldInfo)value).m_fieldHandle, (GenericFieldInfo)value] - il.Ldfld(m_context_f); // stack: [((GenericFieldInfo)value).m_fieldHandle, ((GenericFieldInfo)value).m_context] - var getFieldFromHandle_m = HackHelpers.GetMethodDefinition(x => FieldInfo.GetFieldFromHandle(default(RuntimeFieldHandle), default(RuntimeTypeHandle))); - il.Call(getFieldFromHandle_m); // stack: [MethodBase.GetMethodFromHandle(((GenericFieldInfo)value).m_fieldHandle, ((GenericFieldInfo)value).m_context)] - il.MarkLabel(retLabel); - il.Ret(); - } - return (Func)method.CreateDelegate(typeof(Func)); - } - - private static readonly Func resolveGenericMethodInfo = EmitResolveGenericMethodInfo(); - - private static readonly Func resolveVarArgsMethod = EmitResolveVarArgsMethod(); - - private static readonly Func resolveGenericFieldInfo = EmitResolveGenericFieldInfo(); - } - - internal class MethodBodyOnDynamicILGenerator : MethodBody - { - public MethodBodyOnDynamicILGenerator(DynamicMethod dynamicMethod, ILGenerator ilGenerator, bool resolveTokens) - : base(GetMethodSignature(ilGenerator), resolveTokens) - { - scope = new DynamicILGenerator(ilGenerator).m_scope; - using (var dynamicResolver = new DynamicResolver(dynamicMethod, ilGenerator)) - { - int stackSize; - int initLocals; - int EHCount; - - var code = dynamicResolver.GetCodeInfo(out stackSize, out initLocals, out EHCount); - - MaxStack = stackSize; - InitLocals = initLocals != 0; - - SetLocalSignature(dynamicResolver.m_localSignature); - - ILCodeReader.Read(code, ResolveToken, resolveTokens, this); - - ReadExceptions(dynamicResolver, EHCount); - } - } - - protected override object ResolveToken(MetadataToken token) - { - return DynamicMethodHelpers.Resolve(scope[token.ToInt32()]); - } - - private struct CORINFO_EH_CLAUSE - { - internal int Flags; - internal int TryOffset; - internal int TryLength; - internal int HandlerOffset; - internal int HandlerLength; - internal int ClassTokenOrFilterOffset; - } - - private unsafe void ReadExceptions(DynamicResolver dynamicResolver, int excCount) - { - var buf = stackalloc CORINFO_EH_CLAUSE[1]; - var exceptionClause = &buf[0]; - - for (int i = 0; i < excCount; ++i) - { - dynamicResolver.GetEHInfo(i, exceptionClause); - - var handler = new ExceptionHandler((ExceptionHandlerType)exceptionClause->Flags); - - handler.TryStart = GetInstruction(exceptionClause->TryOffset); - handler.TryEnd = GetInstruction(handler.TryStart.Offset + exceptionClause->TryLength); - - handler.HandlerStart = GetInstruction(exceptionClause->HandlerOffset); - handler.HandlerEnd = GetInstruction(handler.HandlerStart.Offset + exceptionClause->HandlerLength); - - switch (handler.HandlerType) - { - case ExceptionHandlerType.Catch: - var token = new MetadataToken((uint)exceptionClause->ClassTokenOrFilterOffset); - handler.CatchType = resolveTokens ? ResolveToken(token) : token; - break; - case ExceptionHandlerType.Filter: - handler.FilterStart = GetInstruction(exceptionClause->ClassTokenOrFilterOffset); - break; - } - - ExceptionHandlers.Add(handler); - } - } - - private static byte[] GetMethodSignature(ILGenerator ilGenerator) - { - var wrapper = new DynamicILGenerator(ilGenerator); - return (byte[])wrapper.m_scope[wrapper.m_methodSigToken]; - } - - private readonly DynamicScope scope; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodBodyOnDynamicILInfo.cs b/GrEmit/MethodBodyParsing/MethodBodyOnDynamicILInfo.cs deleted file mode 100644 index f053941..0000000 --- a/GrEmit/MethodBodyParsing/MethodBodyOnDynamicILInfo.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - internal class MethodBodyOnDynamicILInfo : MethodBody - { - public MethodBodyOnDynamicILInfo(DynamicMethod dynamicMethod, GrEmit.Utils.DynamicILInfo dynamicILInfo, bool resolveTokens) - : base(GetMethodSignature(dynamicILInfo), resolveTokens) - { - scope = new DynamicILInfoWrapper(dynamicILInfo).m_scope; - using (var dynamicResolver = new DynamicResolver(dynamicMethod, dynamicILInfo)) - { - int stackSize; - int initLocals; - int EHCount; - - var code = dynamicResolver.GetCodeInfo(out stackSize, out initLocals, out EHCount); - - MaxStack = stackSize; - InitLocals = initLocals != 0; - - SetLocalSignature(dynamicResolver.m_localSignature); - - ILCodeReader.Read(code, ResolveToken, resolveTokens, this); - - ExceptionsInfoReader.Read(dynamicResolver.GetRawEHInfo(), ResolveToken, resolveTokens, this); - } - } - - protected override object ResolveToken(MetadataToken token) - { - return DynamicMethodHelpers.Resolve(scope[token.ToInt32()]); - } - - private static byte[] GetMethodSignature(GrEmit.Utils.DynamicILInfo dynamicILInfo) - { - var wrapper = new DynamicILInfoWrapper(dynamicILInfo); - return (byte[])wrapper.m_scope[wrapper.m_methodSignature]; - } - - private readonly DynamicScope scope; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodBodyOnMethodBase.cs b/GrEmit/MethodBodyParsing/MethodBodyOnMethodBase.cs deleted file mode 100644 index a4325f5..0000000 --- a/GrEmit/MethodBodyParsing/MethodBodyOnMethodBase.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System.Collections.Generic; -using System.Reflection; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - internal class MethodBodyOnMethodBase : MethodBody - { - public MethodBodyOnMethodBase(MethodBase method, bool resolveTokens) - : base(GetMethodSignature(method), resolveTokens) - { - module = method.Module; - var methodBody = method.GetMethodBody(); - MaxStack = methodBody.MaxStackSize; - InitLocals = methodBody.InitLocals; - - var localSignature = methodBody.LocalSignatureMetadataToken != 0 - ? method.Module.ResolveSignature(methodBody.LocalSignatureMetadataToken) - : SignatureHelper.GetLocalVarSigHelper().GetSignature(); // null is invalid value - SetLocalSignature(localSignature); - - ILCodeReader.Read(methodBody.GetILAsByteArray(), ResolveToken, resolveTokens, this); - - ReadExceptions(methodBody.ExceptionHandlingClauses); - } - - protected override object ResolveToken(MetadataToken token) - { - return module.Resolve(token); - } - - private static byte[] GetMethodSignature(MethodBase method) - { - return method.Module.ResolveSignature(method.MetadataToken); - } - - private void ReadExceptions(IList exceptionClauses) - { - foreach (var exceptionClause in exceptionClauses) - { - var handler = new ExceptionHandler((ExceptionHandlerType)exceptionClause.Flags); - - handler.TryStart = GetInstruction(exceptionClause.TryOffset); - handler.TryEnd = GetInstruction(handler.TryStart.Offset + exceptionClause.TryLength); - - handler.HandlerStart = GetInstruction(exceptionClause.HandlerOffset); - handler.HandlerEnd = GetInstruction(handler.HandlerStart.Offset + exceptionClause.HandlerLength); - - switch (handler.HandlerType) - { - case ExceptionHandlerType.Catch: - var token = new MetadataToken((uint)exceptionClause.CatchType.MetadataToken); - handler.CatchType = resolveTokens ? ResolveToken(token) : token; - break; - case ExceptionHandlerType.Filter: - handler.FilterStart = GetInstruction(exceptionClause.FilterOffset); - break; - } - - ExceptionHandlers.Add(handler); - } - } - - private readonly Module module; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodBodyOnUnmanagedBuffer.cs b/GrEmit/MethodBodyParsing/MethodBodyOnUnmanagedBuffer.cs deleted file mode 100644 index c2e529d..0000000 --- a/GrEmit/MethodBodyParsing/MethodBodyOnUnmanagedBuffer.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Reflection; - -namespace GrEmit.MethodBodyParsing -{ - internal class MethodBodyOnUnmanagedBuffer : MethodBody - { - public unsafe MethodBodyOnUnmanagedBuffer(byte* rawMethodBody, Module module, MetadataToken methodSignatureToken, bool resolveTokens) - : base(GetMethodSignature(module, methodSignatureToken), resolveTokens) - { - this.module = module; - if (rawMethodBody != null) - Read(rawMethodBody); - } - - protected override object ResolveToken(MetadataToken token) - { - return module.Resolve(token); - } - - private static byte[] GetMethodSignature(Module module, MetadataToken methodSignatureToken) - { - return module == null || methodSignatureToken.RID == 0 - ? new byte[0] - : module.ResolveSignature(methodSignatureToken.ToInt32()); - } - - private unsafe void Read(byte* rawMethodBody) - { - var header = new MethodHeaderReader(rawMethodBody).Read(); - MaxStack = header.MaxStack; - InitLocals = header.InitLocals; - LocalVarToken = header.LocalVarToken; - if (LocalVarToken.RID != 0 && module != null) - SetLocalSignature(module.ResolveSignature(LocalVarToken.ToInt32())); - new ILCodeReader(rawMethodBody + header.HeaderSize, header.CodeSize, ResolveToken, resolveTokens).Read(this); - if (header.HasExceptions) - new ExceptionsInfoReader(rawMethodBody + Align(header.HeaderSize + header.CodeSize, 4), ResolveToken, resolveTokens).Read(this); - } - - private static int Align(int position, int align) - { - align--; - return (position + align) & ~align; - } - - private readonly Module module; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodBodyRocks.cs b/GrEmit/MethodBodyParsing/MethodBodyRocks.cs deleted file mode 100644 index 0b43b52..0000000 --- a/GrEmit/MethodBodyParsing/MethodBodyRocks.cs +++ /dev/null @@ -1,386 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - public static class MethodBodyRocks - { - public static void SimplifyMacros(this Collection instructions) - { - if (instructions == null) - throw new ArgumentNullException(nameof(instructions)); - - foreach (var instruction in instructions) - { - if (instruction.OpCode.OpCodeType != OpCodeType.Macro) - continue; - - switch (instruction.OpCode.Code) - { - case Code.Ldarg_0: - ExpandMacro(instruction, OpCodes.Ldarg, 0); - break; - case Code.Ldarg_1: - ExpandMacro(instruction, OpCodes.Ldarg, 1); - break; - case Code.Ldarg_2: - ExpandMacro(instruction, OpCodes.Ldarg, 2); - break; - case Code.Ldarg_3: - ExpandMacro(instruction, OpCodes.Ldarg, 3); - break; - case Code.Ldloc_0: - ExpandMacro(instruction, OpCodes.Ldloc, 0); - break; - case Code.Ldloc_1: - ExpandMacro(instruction, OpCodes.Ldloc, 1); - break; - case Code.Ldloc_2: - ExpandMacro(instruction, OpCodes.Ldloc, 2); - break; - case Code.Ldloc_3: - ExpandMacro(instruction, OpCodes.Ldloc, 3); - break; - case Code.Stloc_0: - ExpandMacro(instruction, OpCodes.Stloc, 0); - break; - case Code.Stloc_1: - ExpandMacro(instruction, OpCodes.Stloc, 1); - break; - case Code.Stloc_2: - ExpandMacro(instruction, OpCodes.Stloc, 2); - break; - case Code.Stloc_3: - ExpandMacro(instruction, OpCodes.Stloc, 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; - } - } - } - - private static void ExpandMacro(Instruction instruction, OpCode opcode, object operand) - { - instruction.OpCode = opcode; - instruction.Operand = operand; - } - - private static void MakeMacro(Instruction instruction, OpCode opcode) - { - instruction.OpCode = opcode; - instruction.Operand = null; - } - - public static void OptimizeMacros(this Collection instructions) - { - if (instructions == null) - throw new ArgumentNullException(nameof(instructions)); - - foreach (var instruction in instructions) - { - int index; - switch (instruction.OpCode.Code) - { - case Code.Ldarg: - index = (int)instruction.Operand; - - 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 = (int)instruction.Operand; - 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 = (int)instruction.Operand; - 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 = (int)instruction.Operand; - if (index < 256) - ExpandMacro(instruction, OpCodes.Ldarga_S, instruction.Operand); - break; - case Code.Ldloca: - if (((int)instruction.Operand) < 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(instructions); - } - - public static void OptimizeBranches(Collection instructions) - { - ComputeOffsets(instructions); - - foreach (var instruction in instructions) - { - if (instruction.OpCode.OperandType != OperandType.InlineBrTarget) - continue; - - if (OptimizeBranch(instruction)) - ComputeOffsets(instructions); - } - } - - private 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; - } - - private static void ComputeOffsets(Collection instructions) - { - var offset = 0; - foreach (var instruction in instructions) - { - instruction.Offset = offset; - offset += instruction.GetSize(); - } - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodHeader.cs b/GrEmit/MethodBodyParsing/MethodHeader.cs deleted file mode 100644 index 96901fb..0000000 --- a/GrEmit/MethodBodyParsing/MethodHeader.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace GrEmit.MethodBodyParsing -{ - internal class MethodHeader - { - public int HeaderSize { get; set; } - public int CodeSize { get; set; } - public int MaxStack { get; set; } - public bool InitLocals { get; set; } - public MetadataToken LocalVarToken { get; set; } - public bool HasExceptions { get; set; } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/MethodHeaderReader.cs b/GrEmit/MethodBodyParsing/MethodHeaderReader.cs deleted file mode 100644 index 2cfa442..0000000 --- a/GrEmit/MethodBodyParsing/MethodHeaderReader.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; - -namespace GrEmit.MethodBodyParsing -{ - internal sealed unsafe class MethodHeaderReader : UnmanagedByteBuffer - { - public MethodHeaderReader(byte* buffer) - : base(buffer) - { - } - - public MethodHeader Read() - { - position = 0; - var header = new MethodHeader(); - - var flags = ReadByte(); - switch (flags & 0x3) - { - case CorILMethod_TinyFormat: - header.CodeSize = flags >> 2; - header.MaxStack = 8; - break; - case CorILMethod_FatFormat: - var hi = ReadByte(); - header.MaxStack = ReadUInt16(); - header.CodeSize = (int)ReadUInt32(); - header.LocalVarToken = new MetadataToken(ReadUInt32()); - header.InitLocals = (flags & CorILMethod_InitLocals) != 0; - header.HasExceptions = (flags & CorILMethod_MoreSects) != 0; - break; - default: - throw new InvalidOperationException(); - } - header.HeaderSize = position; - - return header; - } - - public const byte CorILMethod_TinyFormat = 0x2; - public const byte CorILMethod_FatFormat = 0x3; - public const byte CorILMethod_InitLocals = 0x10; - public const byte CorILMethod_MoreSects = 0x8; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/OpCode.cs b/GrEmit/MethodBodyParsing/OpCode.cs deleted file mode 100644 index 301a604..0000000 --- a/GrEmit/MethodBodyParsing/OpCode.cs +++ /dev/null @@ -1,421 +0,0 @@ -// -// 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; - -namespace GrEmit.MethodBodyParsing -{ - public enum FlowControl - { - Branch, - Break, - Call, - Cond_Branch, - Meta, - Next, - Phi, - Return, - Throw, - } - - public enum OpCodeType - { - Annotation, - Macro, - Nternal, - Objmodel, - Prefix, - Primitive, - } - - public enum OperandType - { - InlineBrTarget, - InlineField, - InlineI, - InlineI8, - InlineMethod, - InlineNone, - InlinePhi, - InlineR, - InlineSig, - InlineString, - InlineSwitch, - InlineTok, - InlineType, - InlineVar, - InlineArg, - ShortInlineBrTarget, - ShortInlineI, - ShortInlineR, - ShortInlineVar, - ShortInlineArg, - } - - public enum StackBehaviour - { - Pop0, - Pop1, - Pop1_pop1, - Popi, - Popi_pop1, - Popi_popi, - Popi_popi8, - Popi_popi_popi, - Popi_popr4, - Popi_popr8, - Popref, - Popref_pop1, - Popref_popi, - Popref_popi_popi, - Popref_popi_popi8, - Popref_popi_popr4, - Popref_popi_popr8, - Popref_popi_popref, - PopAll, - Push0, - Push1, - Push1_push1, - Pushi, - Pushi8, - Pushr4, - Pushr8, - Pushref, - Varpop, - Varpush, - } - - public struct OpCode : IEquatable - { - internal OpCode(int x, int y) - { - this.Op1 = (byte)((x >> 0) & 0xff); - this.Op2 = (byte)((x >> 8) & 0xff); - this.code = (byte)((x >> 16) & 0xff); - this.flow_control = (byte)((x >> 24) & 0xff); - - this.opcode_type = (byte)((y >> 0) & 0xff); - this.operand_type = (byte)((y >> 8) & 0xff); - this.stack_behavior_pop = (byte)((y >> 16) & 0xff); - this.stack_behavior_push = (byte)((y >> 24) & 0xff); - - if (Op1 == 0xff) - OpCodes.OneByteOpCode[Op2] = this; - else - OpCodes.TwoBytesOpCode[Op2] = this; - } - - public string Name { get { return OpCodeNames.names[(int)Code]; } } - - public int Size { get { return Op1 == 0xff ? 1 : 2; } } - - public short Value { get { return Op1 == 0xff ? Op2 : (short)((Op1 << 8) | Op2); } } - - public Code Code { get { return (Code)code; } } - - public FlowControl FlowControl { get { return (FlowControl)flow_control; } } - - public OpCodeType OpCodeType { get { return (OpCodeType)opcode_type; } } - - public OperandType OperandType { get { return (OperandType)operand_type; } } - - public StackBehaviour StackBehaviourPop { get { return (StackBehaviour)stack_behavior_pop; } } - - public StackBehaviour StackBehaviourPush { get { return (StackBehaviour)stack_behavior_push; } } - - public override int GetHashCode() - { - return Value; - } - - public override bool Equals(object obj) - { - if (!(obj is OpCode)) - return false; - - var opcode = (OpCode)obj; - return Op1 == opcode.Op1 && Op2 == opcode.Op2; - } - - public bool Equals(OpCode opcode) - { - return Op1 == opcode.Op1 && Op2 == opcode.Op2; - } - - public static bool operator ==(OpCode one, OpCode other) - { - return one.Op1 == other.Op1 && one.Op2 == other.Op2; - } - - public static bool operator !=(OpCode one, OpCode other) - { - return one.Op1 != other.Op1 || one.Op2 != other.Op2; - } - - public override string ToString() - { - return Name; - } - - public byte Op1; - - public byte Op2; - - private readonly byte code; - private readonly byte flow_control; - private readonly byte opcode_type; - private readonly byte operand_type; - private readonly byte stack_behavior_pop; - private readonly byte stack_behavior_push; - } - - internal static class OpCodeNames - { - static OpCodeNames() - { - var table = new byte[] - { - 3, 110, 111, 112, - 5, 98, 114, 101, 97, 107, - 7, 108, 100, 97, 114, 103, 46, 48, - 7, 108, 100, 97, 114, 103, 46, 49, - 7, 108, 100, 97, 114, 103, 46, 50, - 7, 108, 100, 97, 114, 103, 46, 51, - 7, 108, 100, 108, 111, 99, 46, 48, - 7, 108, 100, 108, 111, 99, 46, 49, - 7, 108, 100, 108, 111, 99, 46, 50, - 7, 108, 100, 108, 111, 99, 46, 51, - 7, 115, 116, 108, 111, 99, 46, 48, - 7, 115, 116, 108, 111, 99, 46, 49, - 7, 115, 116, 108, 111, 99, 46, 50, - 7, 115, 116, 108, 111, 99, 46, 51, - 7, 108, 100, 97, 114, 103, 46, 115, - 8, 108, 100, 97, 114, 103, 97, 46, 115, - 7, 115, 116, 97, 114, 103, 46, 115, - 7, 108, 100, 108, 111, 99, 46, 115, - 8, 108, 100, 108, 111, 99, 97, 46, 115, - 7, 115, 116, 108, 111, 99, 46, 115, - 6, 108, 100, 110, 117, 108, 108, - 9, 108, 100, 99, 46, 105, 52, 46, 109, 49, - 8, 108, 100, 99, 46, 105, 52, 46, 48, - 8, 108, 100, 99, 46, 105, 52, 46, 49, - 8, 108, 100, 99, 46, 105, 52, 46, 50, - 8, 108, 100, 99, 46, 105, 52, 46, 51, - 8, 108, 100, 99, 46, 105, 52, 46, 52, - 8, 108, 100, 99, 46, 105, 52, 46, 53, - 8, 108, 100, 99, 46, 105, 52, 46, 54, - 8, 108, 100, 99, 46, 105, 52, 46, 55, - 8, 108, 100, 99, 46, 105, 52, 46, 56, - 8, 108, 100, 99, 46, 105, 52, 46, 115, - 6, 108, 100, 99, 46, 105, 52, - 6, 108, 100, 99, 46, 105, 56, - 6, 108, 100, 99, 46, 114, 52, - 6, 108, 100, 99, 46, 114, 56, - 3, 100, 117, 112, - 3, 112, 111, 112, - 3, 106, 109, 112, - 4, 99, 97, 108, 108, - 5, 99, 97, 108, 108, 105, - 3, 114, 101, 116, - 4, 98, 114, 46, 115, - 9, 98, 114, 102, 97, 108, 115, 101, 46, 115, - 8, 98, 114, 116, 114, 117, 101, 46, 115, - 5, 98, 101, 113, 46, 115, - 5, 98, 103, 101, 46, 115, - 5, 98, 103, 116, 46, 115, - 5, 98, 108, 101, 46, 115, - 5, 98, 108, 116, 46, 115, - 8, 98, 110, 101, 46, 117, 110, 46, 115, - 8, 98, 103, 101, 46, 117, 110, 46, 115, - 8, 98, 103, 116, 46, 117, 110, 46, 115, - 8, 98, 108, 101, 46, 117, 110, 46, 115, - 8, 98, 108, 116, 46, 117, 110, 46, 115, - 2, 98, 114, - 7, 98, 114, 102, 97, 108, 115, 101, - 6, 98, 114, 116, 114, 117, 101, - 3, 98, 101, 113, - 3, 98, 103, 101, - 3, 98, 103, 116, - 3, 98, 108, 101, - 3, 98, 108, 116, - 6, 98, 110, 101, 46, 117, 110, - 6, 98, 103, 101, 46, 117, 110, - 6, 98, 103, 116, 46, 117, 110, - 6, 98, 108, 101, 46, 117, 110, - 6, 98, 108, 116, 46, 117, 110, - 6, 115, 119, 105, 116, 99, 104, - 8, 108, 100, 105, 110, 100, 46, 105, 49, - 8, 108, 100, 105, 110, 100, 46, 117, 49, - 8, 108, 100, 105, 110, 100, 46, 105, 50, - 8, 108, 100, 105, 110, 100, 46, 117, 50, - 8, 108, 100, 105, 110, 100, 46, 105, 52, - 8, 108, 100, 105, 110, 100, 46, 117, 52, - 8, 108, 100, 105, 110, 100, 46, 105, 56, - 7, 108, 100, 105, 110, 100, 46, 105, - 8, 108, 100, 105, 110, 100, 46, 114, 52, - 8, 108, 100, 105, 110, 100, 46, 114, 56, - 9, 108, 100, 105, 110, 100, 46, 114, 101, 102, - 9, 115, 116, 105, 110, 100, 46, 114, 101, 102, - 8, 115, 116, 105, 110, 100, 46, 105, 49, - 8, 115, 116, 105, 110, 100, 46, 105, 50, - 8, 115, 116, 105, 110, 100, 46, 105, 52, - 8, 115, 116, 105, 110, 100, 46, 105, 56, - 8, 115, 116, 105, 110, 100, 46, 114, 52, - 8, 115, 116, 105, 110, 100, 46, 114, 56, - 3, 97, 100, 100, - 3, 115, 117, 98, - 3, 109, 117, 108, - 3, 100, 105, 118, - 6, 100, 105, 118, 46, 117, 110, - 3, 114, 101, 109, - 6, 114, 101, 109, 46, 117, 110, - 3, 97, 110, 100, - 2, 111, 114, - 3, 120, 111, 114, - 3, 115, 104, 108, - 3, 115, 104, 114, - 6, 115, 104, 114, 46, 117, 110, - 3, 110, 101, 103, - 3, 110, 111, 116, - 7, 99, 111, 110, 118, 46, 105, 49, - 7, 99, 111, 110, 118, 46, 105, 50, - 7, 99, 111, 110, 118, 46, 105, 52, - 7, 99, 111, 110, 118, 46, 105, 56, - 7, 99, 111, 110, 118, 46, 114, 52, - 7, 99, 111, 110, 118, 46, 114, 56, - 7, 99, 111, 110, 118, 46, 117, 52, - 7, 99, 111, 110, 118, 46, 117, 56, - 8, 99, 97, 108, 108, 118, 105, 114, 116, - 5, 99, 112, 111, 98, 106, - 5, 108, 100, 111, 98, 106, - 5, 108, 100, 115, 116, 114, - 6, 110, 101, 119, 111, 98, 106, - 9, 99, 97, 115, 116, 99, 108, 97, 115, 115, - 6, 105, 115, 105, 110, 115, 116, - 9, 99, 111, 110, 118, 46, 114, 46, 117, 110, - 5, 117, 110, 98, 111, 120, - 5, 116, 104, 114, 111, 119, - 5, 108, 100, 102, 108, 100, - 6, 108, 100, 102, 108, 100, 97, - 5, 115, 116, 102, 108, 100, - 6, 108, 100, 115, 102, 108, 100, - 7, 108, 100, 115, 102, 108, 100, 97, - 6, 115, 116, 115, 102, 108, 100, - 5, 115, 116, 111, 98, 106, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49, 46, 117, 110, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50, 46, 117, 110, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52, 46, 117, 110, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56, 46, 117, 110, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49, 46, 117, 110, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50, 46, 117, 110, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52, 46, 117, 110, - 14, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56, 46, 117, 110, - 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 46, 117, 110, - 13, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 46, 117, 110, - 3, 98, 111, 120, - 6, 110, 101, 119, 97, 114, 114, - 5, 108, 100, 108, 101, 110, - 7, 108, 100, 101, 108, 101, 109, 97, - 9, 108, 100, 101, 108, 101, 109, 46, 105, 49, - 9, 108, 100, 101, 108, 101, 109, 46, 117, 49, - 9, 108, 100, 101, 108, 101, 109, 46, 105, 50, - 9, 108, 100, 101, 108, 101, 109, 46, 117, 50, - 9, 108, 100, 101, 108, 101, 109, 46, 105, 52, - 9, 108, 100, 101, 108, 101, 109, 46, 117, 52, - 9, 108, 100, 101, 108, 101, 109, 46, 105, 56, - 8, 108, 100, 101, 108, 101, 109, 46, 105, - 9, 108, 100, 101, 108, 101, 109, 46, 114, 52, - 9, 108, 100, 101, 108, 101, 109, 46, 114, 56, - 10, 108, 100, 101, 108, 101, 109, 46, 114, 101, 102, - 8, 115, 116, 101, 108, 101, 109, 46, 105, - 9, 115, 116, 101, 108, 101, 109, 46, 105, 49, - 9, 115, 116, 101, 108, 101, 109, 46, 105, 50, - 9, 115, 116, 101, 108, 101, 109, 46, 105, 52, - 9, 115, 116, 101, 108, 101, 109, 46, 105, 56, - 9, 115, 116, 101, 108, 101, 109, 46, 114, 52, - 9, 115, 116, 101, 108, 101, 109, 46, 114, 56, - 10, 115, 116, 101, 108, 101, 109, 46, 114, 101, 102, - 10, 108, 100, 101, 108, 101, 109, 46, 97, 110, 121, - 10, 115, 116, 101, 108, 101, 109, 46, 97, 110, 121, - 9, 117, 110, 98, 111, 120, 46, 97, 110, 121, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 49, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 49, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 50, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 50, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 52, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 52, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, 56, - 11, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, 56, - 9, 114, 101, 102, 97, 110, 121, 118, 97, 108, - 8, 99, 107, 102, 105, 110, 105, 116, 101, - 8, 109, 107, 114, 101, 102, 97, 110, 121, - 7, 108, 100, 116, 111, 107, 101, 110, - 7, 99, 111, 110, 118, 46, 117, 50, - 7, 99, 111, 110, 118, 46, 117, 49, - 6, 99, 111, 110, 118, 46, 105, - 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 105, - 10, 99, 111, 110, 118, 46, 111, 118, 102, 46, 117, - 7, 97, 100, 100, 46, 111, 118, 102, - 10, 97, 100, 100, 46, 111, 118, 102, 46, 117, 110, - 7, 109, 117, 108, 46, 111, 118, 102, - 10, 109, 117, 108, 46, 111, 118, 102, 46, 117, 110, - 7, 115, 117, 98, 46, 111, 118, 102, - 10, 115, 117, 98, 46, 111, 118, 102, 46, 117, 110, - 10, 101, 110, 100, 102, 105, 110, 97, 108, 108, 121, - 5, 108, 101, 97, 118, 101, - 7, 108, 101, 97, 118, 101, 46, 115, - 7, 115, 116, 105, 110, 100, 46, 105, - 6, 99, 111, 110, 118, 46, 117, - 7, 97, 114, 103, 108, 105, 115, 116, - 3, 99, 101, 113, - 3, 99, 103, 116, - 6, 99, 103, 116, 46, 117, 110, - 3, 99, 108, 116, - 6, 99, 108, 116, 46, 117, 110, - 5, 108, 100, 102, 116, 110, - 9, 108, 100, 118, 105, 114, 116, 102, 116, 110, - 5, 108, 100, 97, 114, 103, - 6, 108, 100, 97, 114, 103, 97, - 5, 115, 116, 97, 114, 103, - 5, 108, 100, 108, 111, 99, - 6, 108, 100, 108, 111, 99, 97, - 5, 115, 116, 108, 111, 99, - 8, 108, 111, 99, 97, 108, 108, 111, 99, - 9, 101, 110, 100, 102, 105, 108, 116, 101, 114, - 10, 117, 110, 97, 108, 105, 103, 110, 101, 100, 46, - 9, 118, 111, 108, 97, 116, 105, 108, 101, 46, - 5, 116, 97, 105, 108, 46, - 7, 105, 110, 105, 116, 111, 98, 106, - 12, 99, 111, 110, 115, 116, 114, 97, 105, 110, 101, 100, 46, - 5, 99, 112, 98, 108, 107, - 7, 105, 110, 105, 116, 98, 108, 107, - 3, 110, 111, 46, - 7, 114, 101, 116, 104, 114, 111, 119, - 6, 115, 105, 122, 101, 111, 102, - 10, 114, 101, 102, 97, 110, 121, 116, 121, 112, 101, - 9, 114, 101, 97, 100, 111, 110, 108, 121, 46, - }; - - names = new string[219]; - - for (int i = 0, p = 0; i < names.Length; i++) - { - var buffer = new char[table[p++]]; - - for (int j = 0; j < buffer.Length; j++) - buffer[j] = (char)table[p++]; - - names[i] = new string(buffer); - } - } - - internal static readonly string[] names; - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/OpCodes.cs b/GrEmit/MethodBodyParsing/OpCodes.cs deleted file mode 100644 index 12719f5..0000000 --- a/GrEmit/MethodBodyParsing/OpCodes.cs +++ /dev/null @@ -1,894 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// -// Licensed under the MIT/X11 license. -// - -namespace GrEmit.MethodBodyParsing -{ - public static class OpCodes - { - internal static readonly OpCode[] OneByteOpCode = new OpCode[0xe0 + 1]; - internal static readonly OpCode[] TwoBytesOpCode = new OpCode[0x1e + 1]; - - public static readonly OpCode Nop = new OpCode( - 0xff << 0 | 0x00 << 8 | (byte)Code.Nop << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Break = new OpCode( - 0xff << 0 | 0x01 << 8 | (byte)Code.Break << 16 | (byte)FlowControl.Break << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldarg_0 = new OpCode( - 0xff << 0 | 0x02 << 8 | (byte)Code.Ldarg_0 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldarg_1 = new OpCode( - 0xff << 0 | 0x03 << 8 | (byte)Code.Ldarg_1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldarg_2 = new OpCode( - 0xff << 0 | 0x04 << 8 | (byte)Code.Ldarg_2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldarg_3 = new OpCode( - 0xff << 0 | 0x05 << 8 | (byte)Code.Ldarg_3 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldloc_0 = new OpCode( - 0xff << 0 | 0x06 << 8 | (byte)Code.Ldloc_0 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldloc_1 = new OpCode( - 0xff << 0 | 0x07 << 8 | (byte)Code.Ldloc_1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldloc_2 = new OpCode( - 0xff << 0 | 0x08 << 8 | (byte)Code.Ldloc_2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldloc_3 = new OpCode( - 0xff << 0 | 0x09 << 8 | (byte)Code.Ldloc_3 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Stloc_0 = new OpCode( - 0xff << 0 | 0x0a << 8 | (byte)Code.Stloc_0 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stloc_1 = new OpCode( - 0xff << 0 | 0x0b << 8 | (byte)Code.Stloc_1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stloc_2 = new OpCode( - 0xff << 0 | 0x0c << 8 | (byte)Code.Stloc_2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stloc_3 = new OpCode( - 0xff << 0 | 0x0d << 8 | (byte)Code.Stloc_3 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldarg_S = new OpCode( - 0xff << 0 | 0x0e << 8 | (byte)Code.Ldarg_S << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineArg << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldarga_S = new OpCode( - 0xff << 0 | 0x0f << 8 | (byte)Code.Ldarga_S << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineArg << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Starg_S = new OpCode( - 0xff << 0 | 0x10 << 8 | (byte)Code.Starg_S << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineArg << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldloc_S = new OpCode( - 0xff << 0 | 0x11 << 8 | (byte)Code.Ldloc_S << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineVar << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldloca_S = new OpCode( - 0xff << 0 | 0x12 << 8 | (byte)Code.Ldloca_S << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineVar << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Stloc_S = new OpCode( - 0xff << 0 | 0x13 << 8 | (byte)Code.Stloc_S << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineVar << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldnull = new OpCode( - 0xff << 0 | 0x14 << 8 | (byte)Code.Ldnull << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Ldc_I4_M1 = new OpCode( - 0xff << 0 | 0x15 << 8 | (byte)Code.Ldc_I4_M1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_0 = new OpCode( - 0xff << 0 | 0x16 << 8 | (byte)Code.Ldc_I4_0 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_1 = new OpCode( - 0xff << 0 | 0x17 << 8 | (byte)Code.Ldc_I4_1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_2 = new OpCode( - 0xff << 0 | 0x18 << 8 | (byte)Code.Ldc_I4_2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_3 = new OpCode( - 0xff << 0 | 0x19 << 8 | (byte)Code.Ldc_I4_3 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_4 = new OpCode( - 0xff << 0 | 0x1a << 8 | (byte)Code.Ldc_I4_4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_5 = new OpCode( - 0xff << 0 | 0x1b << 8 | (byte)Code.Ldc_I4_5 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_6 = new OpCode( - 0xff << 0 | 0x1c << 8 | (byte)Code.Ldc_I4_6 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_7 = new OpCode( - 0xff << 0 | 0x1d << 8 | (byte)Code.Ldc_I4_7 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_8 = new OpCode( - 0xff << 0 | 0x1e << 8 | (byte)Code.Ldc_I4_8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4_S = new OpCode( - 0xff << 0 | 0x1f << 8 | (byte)Code.Ldc_I4_S << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineI << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I4 = new OpCode( - 0xff << 0 | 0x20 << 8 | (byte)Code.Ldc_I4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineI << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldc_I8 = new OpCode( - 0xff << 0 | 0x21 << 8 | (byte)Code.Ldc_I8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineI8 << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Ldc_R4 = new OpCode( - 0xff << 0 | 0x22 << 8 | (byte)Code.Ldc_R4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.ShortInlineR << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushr4 << 24); - - public static readonly OpCode Ldc_R8 = new OpCode( - 0xff << 0 | 0x23 << 8 | (byte)Code.Ldc_R8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineR << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushr8 << 24); - - public static readonly OpCode Dup = new OpCode( - 0xff << 0 | 0x25 << 8 | (byte)Code.Dup << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push1_push1 << 24); - - public static readonly OpCode Pop = new OpCode( - 0xff << 0 | 0x26 << 8 | (byte)Code.Pop << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Jmp = new OpCode( - 0xff << 0 | 0x27 << 8 | (byte)Code.Jmp << 16 | (byte)FlowControl.Call << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineMethod << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Call = new OpCode( - 0xff << 0 | 0x28 << 8 | (byte)Code.Call << 16 | (byte)FlowControl.Call << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineMethod << 8 | (byte)StackBehaviour.Varpop << 16 | (byte)StackBehaviour.Varpush << 24); - - public static readonly OpCode Calli = new OpCode( - 0xff << 0 | 0x29 << 8 | (byte)Code.Calli << 16 | (byte)FlowControl.Call << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineSig << 8 | (byte)StackBehaviour.Varpop << 16 | (byte)StackBehaviour.Varpush << 24); - - public static readonly OpCode Ret = new OpCode( - 0xff << 0 | 0x2a << 8 | (byte)Code.Ret << 16 | (byte)FlowControl.Return << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Varpop << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Br_S = new OpCode( - 0xff << 0 | 0x2b << 8 | (byte)Code.Br_S << 16 | (byte)FlowControl.Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Brfalse_S = new OpCode( - 0xff << 0 | 0x2c << 8 | (byte)Code.Brfalse_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Brtrue_S = new OpCode( - 0xff << 0 | 0x2d << 8 | (byte)Code.Brtrue_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Beq_S = new OpCode( - 0xff << 0 | 0x2e << 8 | (byte)Code.Beq_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bge_S = new OpCode( - 0xff << 0 | 0x2f << 8 | (byte)Code.Bge_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bgt_S = new OpCode( - 0xff << 0 | 0x30 << 8 | (byte)Code.Bgt_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ble_S = new OpCode( - 0xff << 0 | 0x31 << 8 | (byte)Code.Ble_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Blt_S = new OpCode( - 0xff << 0 | 0x32 << 8 | (byte)Code.Blt_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bne_Un_S = new OpCode( - 0xff << 0 | 0x33 << 8 | (byte)Code.Bne_Un_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bge_Un_S = new OpCode( - 0xff << 0 | 0x34 << 8 | (byte)Code.Bge_Un_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bgt_Un_S = new OpCode( - 0xff << 0 | 0x35 << 8 | (byte)Code.Bgt_Un_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ble_Un_S = new OpCode( - 0xff << 0 | 0x36 << 8 | (byte)Code.Ble_Un_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Blt_Un_S = new OpCode( - 0xff << 0 | 0x37 << 8 | (byte)Code.Blt_Un_S << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Br = new OpCode( - 0xff << 0 | 0x38 << 8 | (byte)Code.Br << 16 | (byte)FlowControl.Branch << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Brfalse = new OpCode( - 0xff << 0 | 0x39 << 8 | (byte)Code.Brfalse << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Brtrue = new OpCode( - 0xff << 0 | 0x3a << 8 | (byte)Code.Brtrue << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Beq = new OpCode( - 0xff << 0 | 0x3b << 8 | (byte)Code.Beq << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bge = new OpCode( - 0xff << 0 | 0x3c << 8 | (byte)Code.Bge << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bgt = new OpCode( - 0xff << 0 | 0x3d << 8 | (byte)Code.Bgt << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ble = new OpCode( - 0xff << 0 | 0x3e << 8 | (byte)Code.Ble << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Blt = new OpCode( - 0xff << 0 | 0x3f << 8 | (byte)Code.Blt << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bne_Un = new OpCode( - 0xff << 0 | 0x40 << 8 | (byte)Code.Bne_Un << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bge_Un = new OpCode( - 0xff << 0 | 0x41 << 8 | (byte)Code.Bge_Un << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Bgt_Un = new OpCode( - 0xff << 0 | 0x42 << 8 | (byte)Code.Bgt_Un << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ble_Un = new OpCode( - 0xff << 0 | 0x43 << 8 | (byte)Code.Ble_Un << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Blt_Un = new OpCode( - 0xff << 0 | 0x44 << 8 | (byte)Code.Blt_Un << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Switch = new OpCode( - 0xff << 0 | 0x45 << 8 | (byte)Code.Switch << 16 | (byte)FlowControl.Cond_Branch << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineSwitch << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldind_I1 = new OpCode( - 0xff << 0 | 0x46 << 8 | (byte)Code.Ldind_I1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldind_U1 = new OpCode( - 0xff << 0 | 0x47 << 8 | (byte)Code.Ldind_U1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldind_I2 = new OpCode( - 0xff << 0 | 0x48 << 8 | (byte)Code.Ldind_I2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldind_U2 = new OpCode( - 0xff << 0 | 0x49 << 8 | (byte)Code.Ldind_U2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldind_I4 = new OpCode( - 0xff << 0 | 0x4a << 8 | (byte)Code.Ldind_I4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldind_U4 = new OpCode( - 0xff << 0 | 0x4b << 8 | (byte)Code.Ldind_U4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldind_I8 = new OpCode( - 0xff << 0 | 0x4c << 8 | (byte)Code.Ldind_I8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Ldind_I = new OpCode( - 0xff << 0 | 0x4d << 8 | (byte)Code.Ldind_I << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldind_R4 = new OpCode( - 0xff << 0 | 0x4e << 8 | (byte)Code.Ldind_R4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushr4 << 24); - - public static readonly OpCode Ldind_R8 = new OpCode( - 0xff << 0 | 0x4f << 8 | (byte)Code.Ldind_R8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushr8 << 24); - - public static readonly OpCode Ldind_Ref = new OpCode( - 0xff << 0 | 0x50 << 8 | (byte)Code.Ldind_Ref << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Stind_Ref = new OpCode( - 0xff << 0 | 0x51 << 8 | (byte)Code.Stind_Ref << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stind_I1 = new OpCode( - 0xff << 0 | 0x52 << 8 | (byte)Code.Stind_I1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stind_I2 = new OpCode( - 0xff << 0 | 0x53 << 8 | (byte)Code.Stind_I2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stind_I4 = new OpCode( - 0xff << 0 | 0x54 << 8 | (byte)Code.Stind_I4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stind_I8 = new OpCode( - 0xff << 0 | 0x55 << 8 | (byte)Code.Stind_I8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi8 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stind_R4 = new OpCode( - 0xff << 0 | 0x56 << 8 | (byte)Code.Stind_R4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popr4 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stind_R8 = new OpCode( - 0xff << 0 | 0x57 << 8 | (byte)Code.Stind_R8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popr8 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Add = new OpCode( - 0xff << 0 | 0x58 << 8 | (byte)Code.Add << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Sub = new OpCode( - 0xff << 0 | 0x59 << 8 | (byte)Code.Sub << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Mul = new OpCode( - 0xff << 0 | 0x5a << 8 | (byte)Code.Mul << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Div = new OpCode( - 0xff << 0 | 0x5b << 8 | (byte)Code.Div << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Div_Un = new OpCode( - 0xff << 0 | 0x5c << 8 | (byte)Code.Div_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Rem = new OpCode( - 0xff << 0 | 0x5d << 8 | (byte)Code.Rem << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Rem_Un = new OpCode( - 0xff << 0 | 0x5e << 8 | (byte)Code.Rem_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode And = new OpCode( - 0xff << 0 | 0x5f << 8 | (byte)Code.And << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Or = new OpCode( - 0xff << 0 | 0x60 << 8 | (byte)Code.Or << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Xor = new OpCode( - 0xff << 0 | 0x61 << 8 | (byte)Code.Xor << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Shl = new OpCode( - 0xff << 0 | 0x62 << 8 | (byte)Code.Shl << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Shr = new OpCode( - 0xff << 0 | 0x63 << 8 | (byte)Code.Shr << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Shr_Un = new OpCode( - 0xff << 0 | 0x64 << 8 | (byte)Code.Shr_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Neg = new OpCode( - 0xff << 0 | 0x65 << 8 | (byte)Code.Neg << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Not = new OpCode( - 0xff << 0 | 0x66 << 8 | (byte)Code.Not << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Conv_I1 = new OpCode( - 0xff << 0 | 0x67 << 8 | (byte)Code.Conv_I1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_I2 = new OpCode( - 0xff << 0 | 0x68 << 8 | (byte)Code.Conv_I2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_I4 = new OpCode( - 0xff << 0 | 0x69 << 8 | (byte)Code.Conv_I4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_I8 = new OpCode( - 0xff << 0 | 0x6a << 8 | (byte)Code.Conv_I8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Conv_R4 = new OpCode( - 0xff << 0 | 0x6b << 8 | (byte)Code.Conv_R4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushr4 << 24); - - public static readonly OpCode Conv_R8 = new OpCode( - 0xff << 0 | 0x6c << 8 | (byte)Code.Conv_R8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushr8 << 24); - - public static readonly OpCode Conv_U4 = new OpCode( - 0xff << 0 | 0x6d << 8 | (byte)Code.Conv_U4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_U8 = new OpCode( - 0xff << 0 | 0x6e << 8 | (byte)Code.Conv_U8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Callvirt = new OpCode( - 0xff << 0 | 0x6f << 8 | (byte)Code.Callvirt << 16 | (byte)FlowControl.Call << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineMethod << 8 | (byte)StackBehaviour.Varpop << 16 | (byte)StackBehaviour.Varpush << 24); - - public static readonly OpCode Cpobj = new OpCode( - 0xff << 0 | 0x70 << 8 | (byte)Code.Cpobj << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldobj = new OpCode( - 0xff << 0 | 0x71 << 8 | (byte)Code.Ldobj << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldstr = new OpCode( - 0xff << 0 | 0x72 << 8 | (byte)Code.Ldstr << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineString << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Newobj = new OpCode( - 0xff << 0 | 0x73 << 8 | (byte)Code.Newobj << 16 | (byte)FlowControl.Call << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineMethod << 8 | (byte)StackBehaviour.Varpop << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Castclass = new OpCode( - 0xff << 0 | 0x74 << 8 | (byte)Code.Castclass << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Isinst = new OpCode( - 0xff << 0 | 0x75 << 8 | (byte)Code.Isinst << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_R_Un = new OpCode( - 0xff << 0 | 0x76 << 8 | (byte)Code.Conv_R_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushr8 << 24); - - public static readonly OpCode Unbox = new OpCode( - 0xff << 0 | 0x79 << 8 | (byte)Code.Unbox << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Throw = new OpCode( - 0xff << 0 | 0x7a << 8 | (byte)Code.Throw << 16 | (byte)FlowControl.Throw << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldfld = new OpCode( - 0xff << 0 | 0x7b << 8 | (byte)Code.Ldfld << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineField << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldflda = new OpCode( - 0xff << 0 | 0x7c << 8 | (byte)Code.Ldflda << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineField << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Stfld = new OpCode( - 0xff << 0 | 0x7d << 8 | (byte)Code.Stfld << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineField << 8 | (byte)StackBehaviour.Popref_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldsfld = new OpCode( - 0xff << 0 | 0x7e << 8 | (byte)Code.Ldsfld << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineField << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldsflda = new OpCode( - 0xff << 0 | 0x7f << 8 | (byte)Code.Ldsflda << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineField << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Stsfld = new OpCode( - 0xff << 0 | 0x80 << 8 | (byte)Code.Stsfld << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineField << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stobj = new OpCode( - 0xff << 0 | 0x81 << 8 | (byte)Code.Stobj << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popi_pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Conv_Ovf_I1_Un = new OpCode( - 0xff << 0 | 0x82 << 8 | (byte)Code.Conv_Ovf_I1_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_I2_Un = new OpCode( - 0xff << 0 | 0x83 << 8 | (byte)Code.Conv_Ovf_I2_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_I4_Un = new OpCode( - 0xff << 0 | 0x84 << 8 | (byte)Code.Conv_Ovf_I4_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_I8_Un = new OpCode( - 0xff << 0 | 0x85 << 8 | (byte)Code.Conv_Ovf_I8_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Conv_Ovf_U1_Un = new OpCode( - 0xff << 0 | 0x86 << 8 | (byte)Code.Conv_Ovf_U1_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U2_Un = new OpCode( - 0xff << 0 | 0x87 << 8 | (byte)Code.Conv_Ovf_U2_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U4_Un = new OpCode( - 0xff << 0 | 0x88 << 8 | (byte)Code.Conv_Ovf_U4_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U8_Un = new OpCode( - 0xff << 0 | 0x89 << 8 | (byte)Code.Conv_Ovf_U8_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Conv_Ovf_I_Un = new OpCode( - 0xff << 0 | 0x8a << 8 | (byte)Code.Conv_Ovf_I_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U_Un = new OpCode( - 0xff << 0 | 0x8b << 8 | (byte)Code.Conv_Ovf_U_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Box = new OpCode( - 0xff << 0 | 0x8c << 8 | (byte)Code.Box << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Newarr = new OpCode( - 0xff << 0 | 0x8d << 8 | (byte)Code.Newarr << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Ldlen = new OpCode( - 0xff << 0 | 0x8e << 8 | (byte)Code.Ldlen << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelema = new OpCode( - 0xff << 0 | 0x8f << 8 | (byte)Code.Ldelema << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_I1 = new OpCode( - 0xff << 0 | 0x90 << 8 | (byte)Code.Ldelem_I1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_U1 = new OpCode( - 0xff << 0 | 0x91 << 8 | (byte)Code.Ldelem_U1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_I2 = new OpCode( - 0xff << 0 | 0x92 << 8 | (byte)Code.Ldelem_I2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_U2 = new OpCode( - 0xff << 0 | 0x93 << 8 | (byte)Code.Ldelem_U2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_I4 = new OpCode( - 0xff << 0 | 0x94 << 8 | (byte)Code.Ldelem_I4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_U4 = new OpCode( - 0xff << 0 | 0x95 << 8 | (byte)Code.Ldelem_U4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_I8 = new OpCode( - 0xff << 0 | 0x96 << 8 | (byte)Code.Ldelem_I8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Ldelem_I = new OpCode( - 0xff << 0 | 0x97 << 8 | (byte)Code.Ldelem_I << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldelem_R4 = new OpCode( - 0xff << 0 | 0x98 << 8 | (byte)Code.Ldelem_R4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushr4 << 24); - - public static readonly OpCode Ldelem_R8 = new OpCode( - 0xff << 0 | 0x99 << 8 | (byte)Code.Ldelem_R8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushr8 << 24); - - public static readonly OpCode Ldelem_Ref = new OpCode( - 0xff << 0 | 0x9a << 8 | (byte)Code.Ldelem_Ref << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Pushref << 24); - - public static readonly OpCode Stelem_I = new OpCode( - 0xff << 0 | 0x9b << 8 | (byte)Code.Stelem_I << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stelem_I1 = new OpCode( - 0xff << 0 | 0x9c << 8 | (byte)Code.Stelem_I1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stelem_I2 = new OpCode( - 0xff << 0 | 0x9d << 8 | (byte)Code.Stelem_I2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stelem_I4 = new OpCode( - 0xff << 0 | 0x9e << 8 | (byte)Code.Stelem_I4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stelem_I8 = new OpCode( - 0xff << 0 | 0x9f << 8 | (byte)Code.Stelem_I8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popi8 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stelem_R4 = new OpCode( - 0xff << 0 | 0xa0 << 8 | (byte)Code.Stelem_R4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popr4 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stelem_R8 = new OpCode( - 0xff << 0 | 0xa1 << 8 | (byte)Code.Stelem_R8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popr8 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stelem_Ref = new OpCode( - 0xff << 0 | 0xa2 << 8 | (byte)Code.Stelem_Ref << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popref_popi_popref << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldelem_Any = new OpCode( - 0xff << 0 | 0xa3 << 8 | (byte)Code.Ldelem_Any << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popref_popi << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Stelem_Any = new OpCode( - 0xff << 0 | 0xa4 << 8 | (byte)Code.Stelem_Any << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popref_popi_popref << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Unbox_Any = new OpCode( - 0xff << 0 | 0xa5 << 8 | (byte)Code.Unbox_Any << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Conv_Ovf_I1 = new OpCode( - 0xff << 0 | 0xb3 << 8 | (byte)Code.Conv_Ovf_I1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U1 = new OpCode( - 0xff << 0 | 0xb4 << 8 | (byte)Code.Conv_Ovf_U1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_I2 = new OpCode( - 0xff << 0 | 0xb5 << 8 | (byte)Code.Conv_Ovf_I2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U2 = new OpCode( - 0xff << 0 | 0xb6 << 8 | (byte)Code.Conv_Ovf_U2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_I4 = new OpCode( - 0xff << 0 | 0xb7 << 8 | (byte)Code.Conv_Ovf_I4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U4 = new OpCode( - 0xff << 0 | 0xb8 << 8 | (byte)Code.Conv_Ovf_U4 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_I8 = new OpCode( - 0xff << 0 | 0xb9 << 8 | (byte)Code.Conv_Ovf_I8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Conv_Ovf_U8 = new OpCode( - 0xff << 0 | 0xba << 8 | (byte)Code.Conv_Ovf_U8 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi8 << 24); - - public static readonly OpCode Refanyval = new OpCode( - 0xff << 0 | 0xc2 << 8 | (byte)Code.Refanyval << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ckfinite = new OpCode( - 0xff << 0 | 0xc3 << 8 | (byte)Code.Ckfinite << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushr8 << 24); - - public static readonly OpCode Mkrefany = new OpCode( - 0xff << 0 | 0xc6 << 8 | (byte)Code.Mkrefany << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldtoken = new OpCode( - 0xff << 0 | 0xd0 << 8 | (byte)Code.Ldtoken << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineTok << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_U2 = new OpCode( - 0xff << 0 | 0xd1 << 8 | (byte)Code.Conv_U2 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_U1 = new OpCode( - 0xff << 0 | 0xd2 << 8 | (byte)Code.Conv_U1 << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_I = new OpCode( - 0xff << 0 | 0xd3 << 8 | (byte)Code.Conv_I << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_I = new OpCode( - 0xff << 0 | 0xd4 << 8 | (byte)Code.Conv_Ovf_I << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Conv_Ovf_U = new OpCode( - 0xff << 0 | 0xd5 << 8 | (byte)Code.Conv_Ovf_U << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Add_Ovf = new OpCode( - 0xff << 0 | 0xd6 << 8 | (byte)Code.Add_Ovf << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Add_Ovf_Un = new OpCode( - 0xff << 0 | 0xd7 << 8 | (byte)Code.Add_Ovf_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Mul_Ovf = new OpCode( - 0xff << 0 | 0xd8 << 8 | (byte)Code.Mul_Ovf << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Mul_Ovf_Un = new OpCode( - 0xff << 0 | 0xd9 << 8 | (byte)Code.Mul_Ovf_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Sub_Ovf = new OpCode( - 0xff << 0 | 0xda << 8 | (byte)Code.Sub_Ovf << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Sub_Ovf_Un = new OpCode( - 0xff << 0 | 0xdb << 8 | (byte)Code.Sub_Ovf_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Endfinally = new OpCode( - 0xff << 0 | 0xdc << 8 | (byte)Code.Endfinally << 16 | (byte)FlowControl.Return << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Leave = new OpCode( - 0xff << 0 | 0xdd << 8 | (byte)Code.Leave << 16 | (byte)FlowControl.Branch << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineBrTarget << 8 | (byte)StackBehaviour.PopAll << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Leave_S = new OpCode( - 0xff << 0 | 0xde << 8 | (byte)Code.Leave_S << 16 | (byte)FlowControl.Branch << 24, - (byte)OpCodeType.Macro << 0 | (byte)OperandType.ShortInlineBrTarget << 8 | (byte)StackBehaviour.PopAll << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Stind_I = new OpCode( - 0xff << 0 | 0xdf << 8 | (byte)Code.Stind_I << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Conv_U = new OpCode( - 0xff << 0 | 0xe0 << 8 | (byte)Code.Conv_U << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Arglist = new OpCode( - 0xfe << 0 | 0x00 << 8 | (byte)Code.Arglist << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ceq = new OpCode( - 0xfe << 0 | 0x01 << 8 | (byte)Code.Ceq << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Cgt = new OpCode( - 0xfe << 0 | 0x02 << 8 | (byte)Code.Cgt << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Cgt_Un = new OpCode( - 0xfe << 0 | 0x03 << 8 | (byte)Code.Cgt_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Clt = new OpCode( - 0xfe << 0 | 0x04 << 8 | (byte)Code.Clt << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Clt_Un = new OpCode( - 0xfe << 0 | 0x05 << 8 | (byte)Code.Clt_Un << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1_pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldftn = new OpCode( - 0xfe << 0 | 0x06 << 8 | (byte)Code.Ldftn << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineMethod << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldvirtftn = new OpCode( - 0xfe << 0 | 0x07 << 8 | (byte)Code.Ldvirtftn << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineMethod << 8 | (byte)StackBehaviour.Popref << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Ldarg = new OpCode( - 0xfe << 0 | 0x09 << 8 | (byte)Code.Ldarg << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineArg << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldarga = new OpCode( - 0xfe << 0 | 0x0a << 8 | (byte)Code.Ldarga << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineArg << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Starg = new OpCode( - 0xfe << 0 | 0x0b << 8 | (byte)Code.Starg << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineArg << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Ldloc = new OpCode( - 0xfe << 0 | 0x0c << 8 | (byte)Code.Ldloc << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineVar << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push1 << 24); - - public static readonly OpCode Ldloca = new OpCode( - 0xfe << 0 | 0x0d << 8 | (byte)Code.Ldloca << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineVar << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Stloc = new OpCode( - 0xfe << 0 | 0x0e << 8 | (byte)Code.Stloc << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineVar << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Localloc = new OpCode( - 0xfe << 0 | 0x0f << 8 | (byte)Code.Localloc << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Endfilter = new OpCode( - 0xfe << 0 | 0x11 << 8 | (byte)Code.Endfilter << 16 | (byte)FlowControl.Return << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Unaligned = new OpCode( - 0xfe << 0 | 0x12 << 8 | (byte)Code.Unaligned << 16 | (byte)FlowControl.Meta << 24, - (byte)OpCodeType.Prefix << 0 | (byte)OperandType.ShortInlineI << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Volatile = new OpCode( - 0xfe << 0 | 0x13 << 8 | (byte)Code.Volatile << 16 | (byte)FlowControl.Meta << 24, - (byte)OpCodeType.Prefix << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Tail = new OpCode( - 0xfe << 0 | 0x14 << 8 | (byte)Code.Tail << 16 | (byte)FlowControl.Meta << 24, - (byte)OpCodeType.Prefix << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Initobj = new OpCode( - 0xfe << 0 | 0x15 << 8 | (byte)Code.Initobj << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Constrained = new OpCode( - 0xfe << 0 | 0x16 << 8 | (byte)Code.Constrained << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Prefix << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Cpblk = new OpCode( - 0xfe << 0 | 0x17 << 8 | (byte)Code.Cpblk << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Initblk = new OpCode( - 0xfe << 0 | 0x18 << 8 | (byte)Code.Initblk << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Popi_popi_popi << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode No = new OpCode( - 0xfe << 0 | 0x19 << 8 | (byte)Code.No << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Prefix << 0 | (byte)OperandType.ShortInlineI << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Rethrow = new OpCode( - 0xfe << 0 | 0x1a << 8 | (byte)Code.Rethrow << 16 | (byte)FlowControl.Throw << 24, - (byte)OpCodeType.Objmodel << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - - public static readonly OpCode Sizeof = new OpCode( - 0xfe << 0 | 0x1c << 8 | (byte)Code.Sizeof << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineType << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Refanytype = new OpCode( - 0xfe << 0 | 0x1d << 8 | (byte)Code.Refanytype << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Primitive << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop1 << 16 | (byte)StackBehaviour.Pushi << 24); - - public static readonly OpCode Readonly = new OpCode( - 0xfe << 0 | 0x1e << 8 | (byte)Code.Readonly << 16 | (byte)FlowControl.Next << 24, - (byte)OpCodeType.Prefix << 0 | (byte)OperandType.InlineNone << 8 | (byte)StackBehaviour.Pop0 << 16 | (byte)StackBehaviour.Push0 << 24); - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/SignatureReader.cs b/GrEmit/MethodBodyParsing/SignatureReader.cs deleted file mode 100644 index e1ec988..0000000 --- a/GrEmit/MethodBodyParsing/SignatureReader.cs +++ /dev/null @@ -1,272 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// Copyright (c) 2016 Igor Chevdar -// -// Licensed under the MIT/X11 license. -// - -using System; - -namespace GrEmit.MethodBodyParsing -{ - public class ParsedMethodSignature - { - public bool HasReturnType => !(ReturnTypeSignature.Length == 1 && ReturnTypeSignature[0] == (byte)ElementType.Void); - public byte CallingConvention; - public bool HasThis; - public bool ExplicitThis; - public byte[] ReturnTypeSignature; - public int ParamCount; - } - - public class SignatureReader : ByteBuffer - { - public SignatureReader(byte[] signature) - : base(signature) - { - } - - internal LocalInfoCollection ReadLocalVarSig() - { - const byte local_sig = 0x7; - - if (ReadByte() != local_sig) - throw new NotSupportedException(); - - var localVariables = new LocalInfoCollection(); - - var count = ReadCompressedUInt32(); - - for (int i = 0; i < count; ++i) - localVariables.Add(new LocalInfo(ReadTypeSignature())); - - return localVariables; - } - - public ParsedMethodSignature ReadAndParseMethodSignature() - { - var method = new ParsedMethodSignature(); - - var calling_convention = ReadByte(); - - const byte has_this = 0x20; - const byte explicit_this = 0x40; - - if ((calling_convention & has_this) != 0) - { - method.HasThis = true; - calling_convention = (byte)(calling_convention & ~has_this); - } - - if ((calling_convention & explicit_this) != 0) - { - method.ExplicitThis = true; - calling_convention = (byte)(calling_convention & ~explicit_this); - } - - method.CallingConvention = calling_convention; - - if ((calling_convention & 0x10) != 0) - { - // arity - ReadCompressedUInt32(); - } - - var param_count = ReadCompressedUInt32(); - method.ParamCount = (int)param_count; - - while (buffer[position] == (byte)ElementType.CModOpt - || buffer[position] == (byte)ElementType.CModReqD) - { - ReadByte(); - ReadTypeTokenSignature(); - } - - method.ReturnTypeSignature = ReadTypeSignature(); - - if (param_count == 0) - return method; - - for (int i = 0; i < param_count; i++) - ReadTypeSignature(); - - return method; - } - - private byte[] ReadTypeSignature() - { - var writer = new ByteBuffer(); - - var elementType = ReadByte(); - writer.WriteByte(elementType); - - writer.WriteBytes(ReadTypeSignature((ElementType)elementType)); - - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - private byte[] ReadTypeTokenSignature() - { - var writer = new ByteBuffer(); - - writer.WriteCompressedUInt32(ReadCompressedUInt32()); - - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - private byte[] ReadMethodSignature() - { - var writer = new ByteBuffer(); - - var calling_convention = ReadByte(); - writer.WriteByte(calling_convention); - - if ((calling_convention & 0x10) != 0) - { - // arity - writer.WriteCompressedUInt32(ReadCompressedUInt32()); - } - - var param_count = ReadCompressedUInt32(); - writer.WriteCompressedUInt32(param_count); - - // return type - writer.WriteBytes(ReadTypeSignature()); - - if (param_count == 0) - { - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - for (int i = 0; i < param_count; i++) - writer.WriteBytes(ReadTypeSignature()); - - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - private byte[] ReadTypeSignature(ElementType etype) - { - var writer = new ByteBuffer(); - - switch (etype) - { - case ElementType.ValueType: - writer.WriteBytes(ReadTypeTokenSignature()); - break; - case ElementType.Class: - writer.WriteBytes(ReadTypeTokenSignature()); - break; - case ElementType.Ptr: - writer.WriteBytes(ReadTypeSignature()); - break; - case ElementType.FnPtr: - writer.WriteBytes(ReadMethodSignature()); - break; - case ElementType.ByRef: - writer.WriteBytes(ReadTypeSignature()); - break; - case ElementType.Pinned: - writer.WriteBytes(ReadTypeSignature()); - break; - case ElementType.SzArray: - writer.WriteBytes(ReadTypeSignature()); - break; - case ElementType.Array: - writer.WriteBytes(ReadArrayTypeSignature()); - break; - case ElementType.CModOpt: - writer.WriteBytes(ReadTypeTokenSignature()); - writer.WriteBytes(ReadTypeSignature()); - break; - case ElementType.CModReqD: - writer.WriteBytes(ReadTypeTokenSignature()); - writer.WriteBytes(ReadTypeSignature()); - break; - case ElementType.Sentinel: - writer.WriteBytes(ReadTypeSignature()); - break; - case ElementType.Var: - writer.WriteCompressedUInt32(ReadCompressedUInt32()); - break; - case ElementType.MVar: - writer.WriteCompressedUInt32(ReadCompressedUInt32()); - break; - case ElementType.GenericInst: - { - // attrs - var readByte = ReadByte(); - writer.WriteByte(readByte); - // element_type - if ((ElementType)readByte == ElementType.Internal) - writer.WriteBytes(ReadBytes(IntPtr.Size)); - else - writer.WriteBytes(ReadTypeTokenSignature()); - - writer.WriteBytes(ReadGenericInstanceSignature()); - break; - } - case ElementType.Internal: - writer.WriteBytes(ReadBytes(IntPtr.Size)); - break; - default: - writer.WriteBytes(ReadBuiltInType()); - break; - } - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - private byte[] ReadGenericInstanceSignature() - { - var writer = new ByteBuffer(); - - var arity = ReadCompressedUInt32(); - writer.WriteCompressedUInt32(arity); - - for (int i = 0; i < arity; i++) - writer.WriteBytes(ReadTypeSignature()); - - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - private byte[] ReadArrayTypeSignature() - { - var writer = new ByteBuffer(); - - // element_type - writer.WriteBytes(ReadTypeSignature()); - - // rank - writer.WriteCompressedUInt32(ReadCompressedUInt32()); - - var sizes = ReadCompressedUInt32(); - writer.WriteCompressedUInt32(sizes); - - for (int i = 0; i < sizes; i++) - writer.WriteCompressedUInt32(ReadCompressedUInt32()); - - var low_bounds = ReadCompressedUInt32(); - writer.WriteCompressedUInt32(low_bounds); - - for (int i = 0; i < low_bounds; i++) - writer.WriteCompressedInt32(ReadCompressedInt32()); - - writer.position = 0; - return writer.ReadBytes(writer.length); - } - - private byte[] ReadBuiltInType() - { - return new byte[0]; - } - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/TokenType.cs b/GrEmit/MethodBodyParsing/TokenType.cs deleted file mode 100644 index e791b93..0000000 --- a/GrEmit/MethodBodyParsing/TokenType.cs +++ /dev/null @@ -1,39 +0,0 @@ -// -// Author: -// Jb Evain (jbevain@gmail.com) -// -// Copyright (c) 2008 - 2015 Jb Evain -// Copyright (c) 2008 - 2011 Novell, Inc. -// -// Licensed under the MIT/X11 license. -// - -namespace GrEmit.MethodBodyParsing -{ - public enum TokenType : uint - { - Module = 0x00000000, - TypeRef = 0x01000000, - TypeDef = 0x02000000, - Field = 0x04000000, - Method = 0x06000000, - Param = 0x08000000, - InterfaceImpl = 0x09000000, - MemberRef = 0x0a000000, - CustomAttribute = 0x0c000000, - Permission = 0x0e000000, - Signature = 0x11000000, - Event = 0x14000000, - Property = 0x17000000, - ModuleRef = 0x1a000000, - TypeSpec = 0x1b000000, - Assembly = 0x20000000, - AssemblyRef = 0x23000000, - File = 0x26000000, - ExportedType = 0x27000000, - ManifestResource = 0x28000000, - GenericParam = 0x2a000000, - MethodSpec = 0x2b000000, - String = 0x70000000, - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/TypeSignatureReader.cs b/GrEmit/MethodBodyParsing/TypeSignatureReader.cs deleted file mode 100644 index 8db4b6a..0000000 --- a/GrEmit/MethodBodyParsing/TypeSignatureReader.cs +++ /dev/null @@ -1,174 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Reflection.Emit; - -namespace GrEmit.MethodBodyParsing -{ - public class TypeSignatureReader : ByteBuffer - { - public TypeSignatureReader(byte[] signature, Func tokenResolver) - : base(signature) - { - this.tokenResolver = tokenResolver; - } - - public KeyValuePair Resolve() - { - var elementType = (ElementType)ReadByte(); - return elementType == ElementType.Pinned - ? new KeyValuePair(ReadTypeSignature(), true) - : new KeyValuePair(ReadTypeSignature(elementType), false); - } - - private Type ReadTypeSignature() - { - var elementType = ReadByte(); - - return ReadTypeSignature((ElementType)elementType); - } - - private Type ReadTypeTokenSignature() - { - var encodedToken = ReadCompressedUInt32(); - TokenType tokenType; - switch (encodedToken & 3) - { - case 0: - tokenType = TokenType.TypeDef; - break; - case 1: - tokenType = TokenType.TypeRef; - break; - case 2: - tokenType = TokenType.TypeSpec; - break; - default: - throw new InvalidOperationException(); - } - - var token = new MetadataToken(tokenType, encodedToken >> 2); - return (Type)tokenResolver(token); - } - - private Type ReadTypeSignature(ElementType etype) - { - switch (etype) - { - case ElementType.ValueType: - return ReadTypeTokenSignature(); - case ElementType.Class: - return ReadTypeTokenSignature(); - case ElementType.Ptr: - return ReadTypeSignature().MakePointerType(); - case ElementType.FnPtr: - case ElementType.Pinned: - throw new NotSupportedException(); - case ElementType.ByRef: - return ReadTypeSignature().MakeByRefType(); - case ElementType.SzArray: - return ReadTypeSignature().MakeArrayType(); - case ElementType.Array: - throw new NotSupportedException(); - case ElementType.CModOpt: - throw new NotSupportedException(); - case ElementType.CModReqD: - throw new NotSupportedException(); - case ElementType.Sentinel: - throw new NotSupportedException(); - case ElementType.Var: - throw new NotSupportedException(); - case ElementType.MVar: - throw new NotSupportedException(); - case ElementType.GenericInst: - { - Type genericType; - var readByte = ReadByte(); - // element_type - if ((ElementType)readByte == ElementType.Internal) - genericType = ReadInternal(); - else - genericType = ReadTypeTokenSignature(); - - var inst = ReadGenericInstanceSignature(); - return genericType.MakeGenericType(inst); - } - - case ElementType.Internal: - return ReadInternal(); - case ElementType.Void: - return typeof(void); - case ElementType.Boolean: - return typeof(bool); - case ElementType.Char: - return typeof(char); - case ElementType.I1: - return typeof(sbyte); - case ElementType.I2: - return typeof(short); - case ElementType.I4: - return typeof(int); - case ElementType.I8: - return typeof(long); - case ElementType.I: - return typeof(IntPtr); - case ElementType.U1: - return typeof(byte); - case ElementType.U2: - return typeof(ushort); - case ElementType.U4: - return typeof(uint); - case ElementType.U8: - return typeof(ulong); - case ElementType.U: - return typeof(UIntPtr); - case ElementType.String: - return typeof(string); - case ElementType.R4: - return typeof(float); - case ElementType.R8: - return typeof(double); - default: - throw new NotSupportedException(); - } - } - - private Type[] ReadGenericInstanceSignature() - { - var arity = ReadCompressedUInt32(); - var result = new Type[arity]; - - for (int i = 0; i < arity; i++) - result[i] = ReadTypeSignature(); - return result; - } - - private Type ReadInternal() - { - var typeRef = IntPtr.Size == 4 - ? new IntPtr(ReadInt32()) - : new IntPtr(ReadInt64()); - return Type.GetTypeFromHandle(runtimeTypeHandleCreator(typeRef)); - } - - private static Func EmitRuntimeTypeHandleCreator() - { - var dynamicMethod = new DynamicMethod(Guid.NewGuid().ToString(), typeof(RuntimeTypeHandle), new[] {typeof(IntPtr)}, typeof(string), true); - using (var il = new GroboIL(dynamicMethod)) - { - var GetTypeFromHandleUnsafe_m = typeof(Type).GetMethod("GetTypeFromHandleUnsafe", BindingFlags.Static | BindingFlags.NonPublic); - il.Ldarg(0); // stack: [ptr] - il.Call(GetTypeFromHandleUnsafe_m); // stack: [Type.GetTypeFromHandleUnsafe(ptr)] - var runtimeType = typeof(Type).Assembly.GetType("System.RuntimeType"); - var constructor = typeof(RuntimeTypeHandle).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {runtimeType}, null); - il.Newobj(constructor); - il.Ret(); - } - return (Func)dynamicMethod.CreateDelegate(typeof(Func)); - } - - private readonly Func tokenResolver; - - private static readonly Func runtimeTypeHandleCreator = EmitRuntimeTypeHandleCreator(); - } -} \ No newline at end of file diff --git a/GrEmit/MethodBodyParsing/UnmanagedByteBuffer.cs b/GrEmit/MethodBodyParsing/UnmanagedByteBuffer.cs deleted file mode 100644 index 75e4908..0000000 --- a/GrEmit/MethodBodyParsing/UnmanagedByteBuffer.cs +++ /dev/null @@ -1,143 +0,0 @@ -using System; -using System.Runtime.InteropServices; - -namespace GrEmit.MethodBodyParsing -{ - internal unsafe class UnmanagedByteBuffer - { - public UnmanagedByteBuffer(byte* buffer) - { - this.buffer = buffer; - } - - public void Advance(int length) - { - position += length; - } - - public byte ReadByte() - { - return buffer[position++]; - } - - public sbyte ReadSByte() - { - return (sbyte)ReadByte(); - } - - public byte[] ReadBytes(int length) - { - var bytes = new byte[length]; - Marshal.Copy((IntPtr)(buffer + position), bytes, 0, length); - position += length; - return bytes; - } - - public ushort ReadUInt16() - { - ushort value = (ushort)(buffer[position] - | (buffer[position + 1] << 8)); - position += 2; - return value; - } - - public short ReadInt16() - { - return (short)ReadUInt16(); - } - - public uint ReadUInt32() - { - uint value = (uint)(buffer[position] - | (buffer[position + 1] << 8) - | (buffer[position + 2] << 16) - | (buffer[position + 3] << 24)); - position += 4; - return value; - } - - public int ReadInt32() - { - return (int)ReadUInt32(); - } - - public ulong ReadUInt64() - { - uint low = ReadUInt32(); - uint high = ReadUInt32(); - - return (((ulong)high) << 32) | low; - } - - public long ReadInt64() - { - return (long)ReadUInt64(); - } - - public uint ReadCompressedUInt32() - { - byte first = ReadByte(); - if ((first & 0x80) == 0) - return first; - - if ((first & 0x40) == 0) - { - return ((uint)(first & ~0x80) << 8) - | ReadByte(); - } - - return ((uint)(first & ~0xc0) << 24) - | (uint)ReadByte() << 16 - | (uint)ReadByte() << 8 - | ReadByte(); - } - - public int ReadCompressedInt32() - { - var value = (int)(ReadCompressedUInt32() >> 1); - if ((value & 1) == 0) - return value; - if (value < 0x40) - return value - 0x40; - if (value < 0x2000) - return value - 0x2000; - if (value < 0x10000000) - return value - 0x10000000; - return value - 0x20000000; - } - - public float ReadSingle() - { - if (!BitConverter.IsLittleEndian) - { - var bytes = ReadBytes(4); - Array.Reverse(bytes); - return BitConverter.ToSingle(bytes, 0); - } - - //float value = BitConverter.ToSingle(buffer, position); - float value = *(float*)(buffer + position); - position += 4; - return value; - } - - public double ReadDouble() - { - if (!BitConverter.IsLittleEndian) - { - var bytes = ReadBytes(8); - Array.Reverse(bytes); - return BitConverter.ToDouble(bytes, 0); - } - -// double value = BitConverter.ToDouble(buffer, position); - double value = *(double*)(buffer + position); - position += 8; - return value; - } - - // TODO rewrite - internal byte* buffer; - internal int position; - } -} \ No newline at end of file diff --git a/GrEmit/Utils/DynamicILInfo.cs b/GrEmit/Utils/DynamicILInfo.cs deleted file mode 100644 index c81505b..0000000 --- a/GrEmit/Utils/DynamicILInfo.cs +++ /dev/null @@ -1,239 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Reflection.Emit; - -namespace GrEmit.Utils -{ - public class DynamicILInfo - { - #region Constructor - - internal DynamicILInfo(DynamicScope scope, DynamicMethod method, byte[] methodSignature) - { - m_method = method; - DynamicScope = scope; - m_methodSignature = DynamicScope.GetTokenFor(methodSignature); - m_exceptions = new byte[0]; - m_code = new byte[0]; - m_localSignature = new byte[0]; - } - - #endregion - - #region Private Data Members - - private DynamicMethod m_method; - private byte[] m_exceptions; - private byte[] m_code; - private byte[] m_localSignature; - private int m_maxStackSize; - private int m_methodSignature; - - #endregion - - #region Public ILGenerator Methods - - internal DynamicScope DynamicScope { get; } - - public void SetCode(byte[] code, int maxStackSize) - { - if (code == null) - code = new byte[0]; - - m_code = (byte[])code.Clone(); - m_maxStackSize = maxStackSize; - } - - public void SetExceptions(byte[] exceptions) - { - if (exceptions == null) - exceptions = new byte[0]; - - m_exceptions = (byte[])exceptions.Clone(); - } - - public void SetLocalSignature(byte[] localSignature) - { - if (localSignature == null) - localSignature = new byte[0]; - - m_localSignature = (byte[])localSignature.Clone(); - } - - #endregion - - #region Public Scope Methods - - public int GetTokenFor(RuntimeMethodHandle method) - { - return DynamicScope.GetTokenFor(method); - } - - public int GetTokenFor(RuntimeMethodHandle method, RuntimeTypeHandle contextType) - { - return DynamicScope.GetTokenFor(method, contextType); - } - - public int GetTokenFor(RuntimeFieldHandle field) - { - return DynamicScope.GetTokenFor(field); - } - - public int GetTokenFor(RuntimeFieldHandle field, RuntimeTypeHandle contextType) - { - return DynamicScope.GetTokenFor(field, contextType); - } - - public int GetTokenFor(RuntimeTypeHandle type) - { - return DynamicScope.GetTokenFor(type); - } - - public int GetTokenFor(string literal) - { - return DynamicScope.GetTokenFor(literal); - } - - public int GetTokenFor(byte[] signature) - { - return DynamicScope.GetTokenFor(signature); - } - - #endregion - } - - internal class DynamicScope - { - #region Constructor - - internal DynamicScope() - { - m_tokens = new List(); - m_tokens.Add(null); - } - - #endregion - - #region Private Data Members - - internal List m_tokens; - - #endregion - - #region Public Methods - - public int GetTokenFor(RuntimeMethodHandle method) - { - //IRuntimeMethodInfo methodReal = method.GetMethodInfo(); - //RuntimeMethodHandleInternal rmhi = methodReal.Value; - - //if (methodReal != null && !RuntimeMethodHandle.IsDynamicMethod(rmhi)) - //{ - // RuntimeType type = RuntimeMethodHandle.GetDeclaringType(rmhi); - // if ((type != null) && RuntimeTypeHandle.IsGenericType(type)) - // { - // // Do we really need to retrieve this much info just to throw an exception? - // MethodBase m = RuntimeType.GetMethodBase(methodReal); - // Type t = m.DeclaringType.GetGenericTypeDefinition(); - - // throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Argument_MethodDeclaringTypeGenericLcg", m, t)); - // } - //} - - m_tokens.Add(method); - return m_tokens.Count - 1 | (int)MetadataTokenType.MethodDef; - } - - public int GetTokenFor(RuntimeMethodHandle method, RuntimeTypeHandle typeContext) - { - m_tokens.Add(new GenericMethodInfo(method, typeContext)); - return m_tokens.Count - 1 | (int)MetadataTokenType.MethodDef; - } - - public int GetTokenFor(RuntimeFieldHandle field) - { - m_tokens.Add(field); - return m_tokens.Count - 1 | (int)MetadataTokenType.FieldDef; - } - - public int GetTokenFor(RuntimeFieldHandle field, RuntimeTypeHandle typeContext) - { - m_tokens.Add(new GenericFieldInfo(field, typeContext)); - return m_tokens.Count - 1 | (int)MetadataTokenType.FieldDef; - } - - public int GetTokenFor(RuntimeTypeHandle type) - { - m_tokens.Add(type); - return m_tokens.Count - 1 | (int)MetadataTokenType.TypeDef; - } - - public int GetTokenFor(string literal) - { - m_tokens.Add(literal); - return m_tokens.Count - 1 | (int)MetadataTokenType.String; - } - - public int GetTokenFor(byte[] signature) - { - m_tokens.Add(signature); - return m_tokens.Count - 1 | (int)MetadataTokenType.Signature; - } - - #endregion - } - - internal sealed class GenericMethodInfo - { - internal GenericMethodInfo(RuntimeMethodHandle methodHandle, RuntimeTypeHandle context) - { - m_methodHandle = methodHandle; - m_context = context; - } - - internal RuntimeMethodHandle m_methodHandle; - internal RuntimeTypeHandle m_context; - } - - internal sealed class GenericFieldInfo - { - internal GenericFieldInfo(RuntimeFieldHandle fieldHandle, RuntimeTypeHandle context) - { - m_fieldHandle = fieldHandle; - m_context = context; - } - - internal RuntimeFieldHandle m_fieldHandle; - internal RuntimeTypeHandle m_context; - } - - internal enum MetadataTokenType - { - Module = 0x00000000, - TypeRef = 0x01000000, - TypeDef = 0x02000000, - FieldDef = 0x04000000, - MethodDef = 0x06000000, - ParamDef = 0x08000000, - InterfaceImpl = 0x09000000, - MemberRef = 0x0a000000, - CustomAttribute = 0x0c000000, - Permission = 0x0e000000, - Signature = 0x11000000, - Event = 0x14000000, - Property = 0x17000000, - ModuleRef = 0x1a000000, - TypeSpec = 0x1b000000, - Assembly = 0x20000000, - AssemblyRef = 0x23000000, - File = 0x26000000, - ExportedType = 0x27000000, - ManifestResource = 0x28000000, - GenericPar = 0x2a000000, - MethodSpec = 0x2b000000, - String = 0x70000000, - Name = 0x71000000, - BaseType = 0x72000000, - Invalid = 0x7FFFFFFF, - } -} \ No newline at end of file From 0e8c9924e8c1d18d53c7355605248470d0ccb7c6 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 15:22:20 +0500 Subject: [PATCH 02/10] string interpolation --- AsmToBytes/EntryPoint.cs | 6 +++--- GrEmit.Tests/Test.cs | 2 +- GrEmit.sln.DotSettings | 3 ++- GrEmit/GroboIL.cs | 14 +++++++------- GrEmit/ILCode.cs | 6 +++--- .../LabelsILInstructionParameter.cs | 2 +- GrEmit/StackMutator.cs | 12 ++++++------ GrEmit/StackMutators/BinOpStackMutator.cs | 6 +++--- GrEmit/StackMutators/CallStackMutator.cs | 16 ++++++++-------- GrEmit/StackMutators/CalliStackMutator.cs | 4 ++-- GrEmit/StackMutators/CkfiniteStackMutator.cs | 2 +- GrEmit/StackMutators/ConvertStackMutator.cs | 4 ++-- GrEmit/StackMutators/LdelemStackMutator.cs | 2 +- GrEmit/StackMutators/LdelemaStackMutator.cs | 2 +- GrEmit/StackMutators/LdfldStackMutator.cs | 8 ++++---- GrEmit/StackMutators/LdfldaStackMutator.cs | 8 ++++---- GrEmit/StackMutators/LdindStackMutator.cs | 2 +- GrEmit/StackMutators/LdlenStackMutator.cs | 2 +- GrEmit/StackMutators/NegStackMutator.cs | 2 +- GrEmit/StackMutators/NewobjStackMutator.cs | 2 +- GrEmit/StackMutators/NotStackMutator.cs | 2 +- GrEmit/StackMutators/StelemStackMutator.cs | 2 +- GrEmit/StackMutators/StfldStackMutator.cs | 10 +++++----- GrEmit/Utils/EmitHelpers.cs | 8 ++++---- GrEmit/Utils/Formatter.cs | 2 +- GrEmit/Utils/HackHelpers.cs | 10 +++++----- 26 files changed, 70 insertions(+), 69 deletions(-) diff --git a/AsmToBytes/EntryPoint.cs b/AsmToBytes/EntryPoint.cs index 1062b52..0d26d2e 100644 --- a/AsmToBytes/EntryPoint.cs +++ b/AsmToBytes/EntryPoint.cs @@ -23,7 +23,7 @@ public static void Main(string[] args) var tempFile = Path.Combine(dirName, "temp.asm"); var dllFile = Path.Combine(dirName, "temp.dll"); var marker = Guid.NewGuid().ToByteArray(); - var stringMarker = string.Join(Environment.NewLine, marker.Select(x => string.Format("db 0{0:x2}h", x))); + var stringMarker = string.Join(Environment.NewLine, marker.Select(x => $"db 0{x:x2}h")); string content; if (architecture == "x86") content = string.Format(@" @@ -92,7 +92,7 @@ end data RedirectStandardOutput = true, RedirectStandardInput = true, UseShellExecute = false, - Arguments = string.Format(@"""{0}"" ""{1}""", tempFile, dllFile), + Arguments = $@"""{tempFile}"" ""{dllFile}""", FileName = Path.Combine(pathToFasm, "fasm.exe") }; var process = new Process @@ -139,7 +139,7 @@ end data { var result = new StringBuilder(); for (var j = i; j < k; ++j) - result.Append(string.Format("0x{0:x2},", dllContent[j])); + result.Append($"0x{dllContent[j]:x2},"); result.AppendLine(); result.AppendLine(Convert.ToBase64String(dllContent, i, k - i)); File.WriteAllText(outputFile, result.ToString()); diff --git a/GrEmit.Tests/Test.cs b/GrEmit.Tests/Test.cs index 0ca6e6d..c4999cf 100644 --- a/GrEmit.Tests/Test.cs +++ b/GrEmit.Tests/Test.cs @@ -581,7 +581,7 @@ private static void CheckDifferent(string[] values) foreach (var str in values) { if (hashSet.Contains(str)) - throw new InvalidOperationException(string.Format("Duplicate value '{0}'", str)); + throw new InvalidOperationException($"Duplicate value '{str}'"); hashSet.Add(str); } } diff --git a/GrEmit.sln.DotSettings b/GrEmit.sln.DotSettings index 3cadf8c..393e966 100644 --- a/GrEmit.sln.DotSettings +++ b/GrEmit.sln.DotSettings @@ -5,4 +5,5 @@ C:\projects\open-source\skbkontur\gremit\Common.DotSettings ..\Common.DotSettings True - 1 \ No newline at end of file + 1 + True \ No newline at end of file diff --git a/GrEmit/GroboIL.cs b/GrEmit/GroboIL.cs index 76cbc49..d290838 100644 --- a/GrEmit/GroboIL.cs +++ b/GrEmit/GroboIL.cs @@ -464,14 +464,14 @@ public void Jmp(MethodInfo method) if (method == null) throw new ArgumentNullException("method"); if (method.ReturnType != methodReturnType) - throw new ArgumentException(string.Format("The return type must be of type '{0}'", methodReturnType), "method"); + throw new ArgumentException($"The return type must be of type '{methodReturnType}'", "method"); var parameterTypes = method.GetParameters().Select(info => info.ParameterType).ToArray(); if (parameterTypes.Length != methodParameterTypes.Length) - throw new ArgumentException(string.Format("The number of arguments must be equal to {0}", methodParameterTypes.Length), "method"); + throw new ArgumentException($"The number of arguments must be equal to {methodParameterTypes.Length}", "method"); for (var i = 0; i < parameterTypes.Length; ++i) { if (parameterTypes[i] != methodParameterTypes[i]) - throw new ArgumentException(string.Format("The argument #{0} must be of type '{1}'", i + 1, methodParameterTypes[i]), "method"); + throw new ArgumentException($"The argument #{i + 1} must be of type '{methodParameterTypes[i]}'", "method"); } Emit(OpCodes.Jmp, method); } @@ -1749,7 +1749,7 @@ public void Conv() opCode = OpCodes.Conv_R8; break; default: - throw new ArgumentException(string.Format("Expected numeric type but was '{0}'", type), "type"); + throw new ArgumentException($"Expected numeric type but was '{type}'", "type"); } } Emit(opCode); @@ -1821,7 +1821,7 @@ public void Conv_Ovf(bool unsigned) opCode = OpCodes.Conv_Ovf_U8; break; default: - throw new ArgumentException(string.Format("Expected integer type but was '{0}'", type), "T"); + throw new ArgumentException($"Expected integer type but was '{type}'", "T"); } } } @@ -1860,7 +1860,7 @@ public void Conv_Ovf(bool unsigned) opCode = OpCodes.Conv_Ovf_U8_Un; break; default: - throw new ArgumentException(string.Format("Expected integer type but was '{0}'", type), "T"); + throw new ArgumentException($"Expected integer type but was '{type}'", "T"); } } } @@ -2077,7 +2077,7 @@ private void Emit(OpCode opCode, ILInstructionParameter parameter) il.Emit(opCode, (float)primitiveParameter.Value); break; default: - throw new InvalidOperationException(string.Format("Type code '{0}' is not valid at this point", typeCode)); + throw new InvalidOperationException($"Type code '{typeCode}' is not valid at this point"); } } } diff --git a/GrEmit/ILCode.cs b/GrEmit/ILCode.cs index a02a7c7..fadb2ff 100644 --- a/GrEmit/ILCode.cs +++ b/GrEmit/ILCode.cs @@ -13,7 +13,7 @@ internal class ILCode public int MarkLabel(GroboIL.Label label, ILInstructionComment comment) { if (labelLineNumbers.ContainsKey(label)) - throw new InvalidOperationException(string.Format("The label '{0}' has already been marked", label.Name)); + throw new InvalidOperationException($"The label '{label.Name}' has already been marked"); labelLineNumbers.Add(label, Count); instructions.Add(new ILInstruction(InstructionKind.Label, default(OpCode), new LabelILInstructionParameter(label), comment)); return Count++; @@ -27,14 +27,14 @@ public void CheckLabels() { var label = (LabelILInstructionParameter)instruction.Parameter; if (!labelLineNumbers.ContainsKey(label.Label)) - throw new InvalidOperationException(string.Format("The label '{0}' has not been marked", label.Label.Name)); + throw new InvalidOperationException($"The label '{label.Label.Name}' has not been marked"); } if (instruction.Parameter is LabelsILInstructionParameter) { foreach (var label in ((LabelsILInstructionParameter)instruction.Parameter).Labels) { if (!labelLineNumbers.ContainsKey(label)) - throw new InvalidOperationException(string.Format("The label '{0}' has not been marked", label.Name)); + throw new InvalidOperationException($"The label '{label.Name}' has not been marked"); } } } diff --git a/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs b/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs index e788022..5a854a9 100644 --- a/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs +++ b/GrEmit/InstructionParameters/LabelsILInstructionParameter.cs @@ -11,7 +11,7 @@ public LabelsILInstructionParameter(GroboIL.Label[] labels) public override string Format() { - return string.Format("({0})", string.Join(", ", Labels.Select(label => label.Name).ToArray())); + return $"({string.Join(", ", Labels.Select(label => label.Name).ToArray())})"; } public GroboIL.Label[] Labels { get; private set; } diff --git a/GrEmit/StackMutator.cs b/GrEmit/StackMutator.cs index 80cc420..7f57897 100644 --- a/GrEmit/StackMutator.cs +++ b/GrEmit/StackMutator.cs @@ -104,7 +104,7 @@ protected static void ThrowError(GroboIL il, string message) protected static void CheckNotStruct(GroboIL il, ESType type) { if (ToCLIType(type) == CLIType.Struct) - ThrowError(il, string.Format("Struct of type '{0}' is not valid at this point", type)); + ThrowError(il, $"Struct of type '{type}' is not valid at this point"); } protected static void CheckNotEmpty(GroboIL il, EvaluationStack stack, Func message) @@ -117,19 +117,19 @@ protected static void CheckIsAPointer(GroboIL il, ESType type) { var cliType = ToCLIType(type); if (cliType != CLIType.Pointer && cliType != CLIType.NativeInt) - ThrowError(il, string.Format("A pointer type expected but was '{0}'", type)); + ThrowError(il, $"A pointer type expected but was '{type}'"); } protected static void CheckCanBeAssigned(GroboIL il, Type to, ESType from) { if (!CanBeAssigned(to, from, il.VerificationKind)) - ThrowError(il, string.Format("Unable to set a value of type '{0}' to an instance of type '{1}'", from, Formatter.Format(to))); + ThrowError(il, $"Unable to set a value of type '{@from}' to an instance of type '{Formatter.Format(to)}'"); } protected static void CheckCanBeAssigned(GroboIL il, Type to, Type from) { if (!CanBeAssigned(to, from, il.VerificationKind)) - ThrowError(il, string.Format("Unable to set a value of type '{0}' to an instance of type '{1}'", Formatter.Format(from), Formatter.Format(to))); + ThrowError(il, $"Unable to set a value of type '{Formatter.Format(@from)}' to an instance of type '{Formatter.Format(to)}'"); } protected static bool CanBeAssigned(Type to, Type from, TypesAssignabilityVerificationKind verificationKind) @@ -327,7 +327,7 @@ private static bool CanBeAssigned(Type to, ESType esFrom, TypesAssignabilityVeri case CLIType.Zero: return true; default: - throw new InvalidOperationException(string.Format("CLI type '{0}' is not valid at this point", cliTo)); + throw new InvalidOperationException($"CLI type '{cliTo}' is not valid at this point"); } } @@ -509,7 +509,7 @@ private static ESType FindCommonType(CLIType cliType, ESType first, ESType secon return new ComplexESType(baseType, intersected.ToArray()); } default: - throw new InvalidOperationException(string.Format("CLI type '{0}' is not valid at this point", cliType)); + throw new InvalidOperationException($"CLI type '{cliType}' is not valid at this point"); } } diff --git a/GrEmit/StackMutators/BinOpStackMutator.cs b/GrEmit/StackMutators/BinOpStackMutator.cs index 012f92a..24521e5 100644 --- a/GrEmit/StackMutators/BinOpStackMutator.cs +++ b/GrEmit/StackMutators/BinOpStackMutator.cs @@ -12,12 +12,12 @@ protected BinOpStackMutator(OpCode opCode) public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref EvaluationStack stack) { - CheckNotEmpty(il, stack, () => string.Format("Expected two arguments for the operation '{0}'", opCode)); + CheckNotEmpty(il, stack, () => $"Expected two arguments for the operation '{opCode}'"); var right = stack.Pop(); - CheckNotEmpty(il, stack, () => string.Format("Expected two arguments for the operation '{0}'", opCode)); + CheckNotEmpty(il, stack, () => $"Expected two arguments for the operation '{opCode}'"); var left = stack.Pop(); if (!IsAllowed(ToCLIType(left), ToCLIType(right))) - ThrowError(il, string.Format("Cannot perform the instruction '{0}' on types '{1}' and '{2}'", opCode, left, right)); + ThrowError(il, $"Cannot perform the instruction '{opCode}' on types '{left}' and '{right}'"); var result = Canonize(GetResultType(Canonize(left), Canonize(right))); if (result != typeof(void)) stack.Push(result); diff --git a/GrEmit/StackMutators/CallStackMutator.cs b/GrEmit/StackMutators/CallStackMutator.cs index c66b476..2422a5e 100644 --- a/GrEmit/StackMutators/CallStackMutator.cs +++ b/GrEmit/StackMutators/CallStackMutator.cs @@ -47,40 +47,40 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev } for (var i = parameterTypes.Length - 1; i >= 0; --i) { - CheckNotEmpty(il, stack, () => string.Format("Parameter #{0} for call to the method '{1}' is not loaded on the evaluation stack", i + 1, formattedMethodGetter())); + CheckNotEmpty(il, stack, () => $"Parameter #{i + 1} for call to the method '{formattedMethodGetter()}' is not loaded on the evaluation stack"); CheckCanBeAssigned(il, parameterTypes[i], stack.Pop()); } if (!isStatic) { - CheckNotEmpty(il, stack, () => string.Format("An instance to call the method '{0}' is not loaded on the evaluation stack", formattedMethodGetter())); + CheckNotEmpty(il, stack, () => $"An instance to call the method '{formattedMethodGetter()}' is not loaded on the evaluation stack"); var instance = stack.Pop(); var instanceBaseType = instance.ToType(); if (instanceBaseType != null) { if (instanceBaseType.IsValueType) - ThrowError(il, string.Format("In order to call the method '{0}' on a value type '{1}' load an instance by ref or box it", formattedMethodGetter(), instance)); + ThrowError(il, $"In order to call the method '{formattedMethodGetter()}' on a value type '{instance}' load an instance by ref or box it"); else if (!instanceBaseType.IsByRef) CheckCanBeAssigned(il, declaringType, instance); else { var elementType = instanceBaseType.GetElementType(); if (!elementType.IsValueType) - ThrowError(il, string.Format("Cannot call the method '{0}' on an instance of type '{1}'", formattedMethodGetter(), instance)); + ThrowError(il, $"Cannot call the method '{formattedMethodGetter()}' on an instance of type '{instance}'"); else { if (declaringType.IsInterface) { if (ReflectionExtensions.GetInterfaces(elementType).All(type => type != declaringType)) - ThrowError(il, string.Format("The type '{0}' does not implement interface '{1}'", Formatter.Format(elementType), Formatter.Format(declaringType))); + ThrowError(il, $"The type '{Formatter.Format(elementType)}' does not implement interface '{Formatter.Format(declaringType)}'"); } else if (declaringType != typeof(object) && declaringType != elementType) - ThrowError(il, string.Format("Cannot call the method '{0}' on an instance of type '{1}'", formattedMethodGetter(), elementType)); + ThrowError(il, $"Cannot call the method '{formattedMethodGetter()}' on an instance of type '{elementType}'"); if (isVirtual && callvirt) { if (constrained == null) - ThrowError(il, string.Format("In order to call a virtual method '{0}' on a value type '{1}' specify 'constrained' parameter", formattedMethodGetter(), Formatter.Format(elementType))); + ThrowError(il, $"In order to call a virtual method '{formattedMethodGetter()}' on a value type '{Formatter.Format(elementType)}' specify 'constrained' parameter"); if (constrained != elementType) - ThrowError(il, string.Format("Invalid 'constrained' parameter to call a virtual method '{0}'. Expected '{1}' but was '{2}'", formattedMethodGetter(), Formatter.Format(constrained), Formatter.Format(elementType))); + ThrowError(il, $"Invalid 'constrained' parameter to call a virtual method '{formattedMethodGetter()}'. Expected '{Formatter.Format(constrained)}' but was '{Formatter.Format(elementType)}'"); } } } diff --git a/GrEmit/StackMutators/CalliStackMutator.cs b/GrEmit/StackMutators/CalliStackMutator.cs index 70846a0..cc0fc4c 100644 --- a/GrEmit/StackMutators/CalliStackMutator.cs +++ b/GrEmit/StackMutators/CalliStackMutator.cs @@ -12,10 +12,10 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev CheckNotEmpty(il, stack, () => "In order to perform the 'calli' instruction an entry point must be loaded onto the evaluation stack"); var entryPoint = stack.Pop(); if (ToCLIType(entryPoint) != CLIType.NativeInt) - ThrowError(il, string.Format("An entry point must be a native int but was '{0}'", entryPoint)); + ThrowError(il, $"An entry point must be a native int but was '{entryPoint}'"); for (var i = parameterTypes.Length - 1; i >= 0; --i) { - CheckNotEmpty(il, stack, () => string.Format("Expected exactly {0} parameters, but the evaluation stack is empty", parameterTypes.Length)); + CheckNotEmpty(il, stack, () => $"Expected exactly {parameterTypes.Length} parameters, but the evaluation stack is empty"); CheckCanBeAssigned(il, parameterTypes[i], stack.Pop()); } if (returnType != typeof(void)) diff --git a/GrEmit/StackMutators/CkfiniteStackMutator.cs b/GrEmit/StackMutators/CkfiniteStackMutator.cs index 6eb7b8b..2fd7b67 100644 --- a/GrEmit/StackMutators/CkfiniteStackMutator.cs +++ b/GrEmit/StackMutators/CkfiniteStackMutator.cs @@ -7,7 +7,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev CheckNotEmpty(il, stack, () => "In order to perform the 'ckfinite' instruction an instance must be loaded onto the evaluation stack"); var peek = stack.Peek(); if (ToCLIType(peek) != CLIType.Float) - ThrowError(il, string.Format("It is only allowed to check if value is finite for floating point values but was '{0}'", peek)); + ThrowError(il, $"It is only allowed to check if value is finite for floating point values but was '{peek}'"); } } } \ No newline at end of file diff --git a/GrEmit/StackMutators/ConvertStackMutator.cs b/GrEmit/StackMutators/ConvertStackMutator.cs index 088dc40..a5d4a69 100644 --- a/GrEmit/StackMutators/ConvertStackMutator.cs +++ b/GrEmit/StackMutators/ConvertStackMutator.cs @@ -13,10 +13,10 @@ protected ConvertStackMutator(OpCode opCode, Type to) public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref EvaluationStack stack) { - CheckNotEmpty(il, stack, () => string.Format("In order to perform the instruction '{0}' an instance must be loaded onto the evaluation stack", opCode)); + CheckNotEmpty(il, stack, () => $"In order to perform the instruction '{opCode}' an instance must be loaded onto the evaluation stack"); var type = stack.Pop(); if (!Allowed(type)) - ThrowError(il, string.Format("Unable to perform the instruction '{0}' on type '{1}'", opCode, type)); + ThrowError(il, $"Unable to perform the instruction '{opCode}' on type '{type}'"); stack.Push(to); } diff --git a/GrEmit/StackMutators/LdelemStackMutator.cs b/GrEmit/StackMutators/LdelemStackMutator.cs index 0c656d7..bee24f5 100644 --- a/GrEmit/StackMutators/LdelemStackMutator.cs +++ b/GrEmit/StackMutators/LdelemStackMutator.cs @@ -15,7 +15,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev var esType = stack.Pop(); var array = esType.ToType(); if (!array.IsArray && array != typeof(Array)) - throw new InvalidOperationException(string.Format("An array expected to perform the 'ldelem' instruction but was '{0}'", esType)); + throw new InvalidOperationException($"An array expected to perform the 'ldelem' instruction but was '{esType}'"); CheckCanBeAssigned(il, elementType, array == typeof(Array) ? typeof(object) : array.GetElementType()); stack.Push(elementType); } diff --git a/GrEmit/StackMutators/LdelemaStackMutator.cs b/GrEmit/StackMutators/LdelemaStackMutator.cs index 6e45b82..8406770 100644 --- a/GrEmit/StackMutators/LdelemaStackMutator.cs +++ b/GrEmit/StackMutators/LdelemaStackMutator.cs @@ -15,7 +15,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev var esType = stack.Pop(); var array = esType.ToType(); if (!array.IsArray && array != typeof(Array)) - throw new InvalidOperationException(string.Format("An array expected to perform the 'ldelema' instruction but was '{0}'", esType)); + throw new InvalidOperationException($"An array expected to perform the 'ldelema' instruction but was '{esType}'"); stack.Push(elementType.MakeByRefType()); } } diff --git a/GrEmit/StackMutators/LdfldStackMutator.cs b/GrEmit/StackMutators/LdfldStackMutator.cs index 84973f7..9305245 100644 --- a/GrEmit/StackMutators/LdfldStackMutator.cs +++ b/GrEmit/StackMutators/LdfldStackMutator.cs @@ -11,13 +11,13 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev if (!field.IsStatic) { var declaringType = field.DeclaringType; - CheckNotEmpty(il, stack, () => string.Format("In order to load the field '{0}' an instance must be put onto the evaluation stack", Formatter.Format(field))); + CheckNotEmpty(il, stack, () => $"In order to load the field '{Formatter.Format(field)}' an instance must be put onto the evaluation stack"); var instance = stack.Pop().ToType(); if (instance != null) { if (instance.IsValueType) - ThrowError(il, string.Format("In order to load the field '{0}' of a value type '{1}' load an instance by ref", Formatter.Format(field), Formatter.Format(instance))); + ThrowError(il, $"In order to load the field '{Formatter.Format(field)}' of a value type '{Formatter.Format(instance)}' load an instance by ref"); else if (!instance.IsByRef) CheckCanBeAssigned(il, declaringType, instance); else @@ -26,10 +26,10 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev if (elementType.IsValueType) { if (declaringType != elementType) - ThrowError(il, string.Format("Cannot load the field '{0}' of an instance of type '{1}'", Formatter.Format(field), Formatter.Format(elementType))); + ThrowError(il, $"Cannot load the field '{Formatter.Format(field)}' of an instance of type '{Formatter.Format(elementType)}'"); } else - ThrowError(il, string.Format("Cannot load the field '{0}' of an instance of type '{1}'", Formatter.Format(field), Formatter.Format(instance))); + ThrowError(il, $"Cannot load the field '{Formatter.Format(field)}' of an instance of type '{Formatter.Format(instance)}'"); } } } diff --git a/GrEmit/StackMutators/LdfldaStackMutator.cs b/GrEmit/StackMutators/LdfldaStackMutator.cs index e89f007..ac0dee0 100644 --- a/GrEmit/StackMutators/LdfldaStackMutator.cs +++ b/GrEmit/StackMutators/LdfldaStackMutator.cs @@ -11,13 +11,13 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev if (!field.IsStatic) { var declaringType = field.DeclaringType; - CheckNotEmpty(il, stack, () => string.Format("In order to load a reference to the field '{0}' an instance must be put onto the evaluation stack", Formatter.Format(field))); + CheckNotEmpty(il, stack, () => $"In order to load a reference to the field '{Formatter.Format(field)}' an instance must be put onto the evaluation stack"); var instance = stack.Pop().ToType(); if (instance != null) { if (instance.IsValueType) - ThrowError(il, string.Format("In order to load a reference to the field '{0}' of a value type '{1}' load an instance by ref", Formatter.Format(field), Formatter.Format(instance))); + ThrowError(il, $"In order to load a reference to the field '{Formatter.Format(field)}' of a value type '{Formatter.Format(instance)}' load an instance by ref"); else if (!instance.IsByRef) CheckCanBeAssigned(il, declaringType, instance); else @@ -26,10 +26,10 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev if (elementType.IsValueType) { if (declaringType != elementType) - ThrowError(il, string.Format("Cannot load a reference to the field '{0}' of an instance of type '{1}'", Formatter.Format(field), Formatter.Format(elementType))); + ThrowError(il, $"Cannot load a reference to the field '{Formatter.Format(field)}' of an instance of type '{Formatter.Format(elementType)}'"); } else - ThrowError(il, string.Format("Cannot load a reference to the field '{0}' of an instance of type '{1}'", Formatter.Format(field), Formatter.Format(instance))); + ThrowError(il, $"Cannot load a reference to the field '{Formatter.Format(field)}' of an instance of type '{Formatter.Format(instance)}'"); } } } diff --git a/GrEmit/StackMutators/LdindStackMutator.cs b/GrEmit/StackMutators/LdindStackMutator.cs index 18c4204..e2f2b7b 100644 --- a/GrEmit/StackMutators/LdindStackMutator.cs +++ b/GrEmit/StackMutators/LdindStackMutator.cs @@ -29,7 +29,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev CheckCanBeAssigned(il, type, elementType); } else if (!type.IsPrimitive && type != typeof(object)) - ThrowError(il, string.Format("Unable to load an instance of type '{0}' from a pointer of type '{1}' indirectly", Formatter.Format(type), Formatter.Format(pointer))); + ThrowError(il, $"Unable to load an instance of type '{Formatter.Format(type)}' from a pointer of type '{Formatter.Format(pointer)}' indirectly"); stack.Push(type); } } diff --git a/GrEmit/StackMutators/LdlenStackMutator.cs b/GrEmit/StackMutators/LdlenStackMutator.cs index 256fc31..8adc44d 100644 --- a/GrEmit/StackMutators/LdlenStackMutator.cs +++ b/GrEmit/StackMutators/LdlenStackMutator.cs @@ -10,7 +10,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev var esType = stack.Pop(); var array = esType.ToType(); if (!array.IsArray && array != typeof(Array)) - ThrowError(il, string.Format("An array expected but was '{0}'", esType)); + ThrowError(il, $"An array expected but was '{esType}'"); stack.Push(typeof(int)); } } diff --git a/GrEmit/StackMutators/NegStackMutator.cs b/GrEmit/StackMutators/NegStackMutator.cs index 44c7b9c..8f258d8 100644 --- a/GrEmit/StackMutators/NegStackMutator.cs +++ b/GrEmit/StackMutators/NegStackMutator.cs @@ -8,7 +8,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev var esType = stack.Pop(); var cliType = ToCLIType(esType); if (cliType != CLIType.Int32 && cliType != CLIType.Int64 && cliType != CLIType.NativeInt && cliType != CLIType.Float && cliType != CLIType.Zero) - ThrowError(il, string.Format("Unable to perform the 'neg' operation on type '{0}'", esType)); + ThrowError(il, $"Unable to perform the 'neg' operation on type '{esType}'"); stack.Push(Canonize(esType)); } } diff --git a/GrEmit/StackMutators/NewobjStackMutator.cs b/GrEmit/StackMutators/NewobjStackMutator.cs index 342c4fa..e4b4652 100644 --- a/GrEmit/StackMutators/NewobjStackMutator.cs +++ b/GrEmit/StackMutators/NewobjStackMutator.cs @@ -11,7 +11,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev var parameterTypes = ReflectionExtensions.GetParameterTypes(constructor); for (var i = parameterTypes.Length - 1; i >= 0; --i) { - CheckNotEmpty(il, stack, () => string.Format("Expected exactly {0} parameters to call the constructor '{1}'", parameterTypes.Length, Formatter.Format(constructor))); + CheckNotEmpty(il, stack, () => $"Expected exactly {parameterTypes.Length} parameters to call the constructor '{Formatter.Format(constructor)}'"); CheckCanBeAssigned(il, parameterTypes[i], stack.Pop()); } stack.Push(constructor.ReflectedType); diff --git a/GrEmit/StackMutators/NotStackMutator.cs b/GrEmit/StackMutators/NotStackMutator.cs index 538888f..7f82787 100644 --- a/GrEmit/StackMutators/NotStackMutator.cs +++ b/GrEmit/StackMutators/NotStackMutator.cs @@ -10,7 +10,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev var esType = stack.Pop(); var cliType = ToCLIType(esType); if (cliType != CLIType.Int32 && cliType != CLIType.Int64 && cliType != CLIType.NativeInt && cliType != CLIType.Zero) - ThrowError(il, string.Format("Unable to perform the 'not' operation on type '{0}'", esType)); + ThrowError(il, $"Unable to perform the 'not' operation on type '{esType}'"); // !zero = -1 -> native int stack.Push(cliType == CLIType.Zero ? typeof(IntPtr) : Canonize(esType)); } diff --git a/GrEmit/StackMutators/StelemStackMutator.cs b/GrEmit/StackMutators/StelemStackMutator.cs index 013afb5..9f95ef6 100644 --- a/GrEmit/StackMutators/StelemStackMutator.cs +++ b/GrEmit/StackMutators/StelemStackMutator.cs @@ -17,7 +17,7 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev var esType = stack.Pop(); var array = esType.ToType(); if (!array.IsArray && array != typeof(Array)) - ThrowError(il, string.Format("An array expected to perform the 'stelem' instruction but was '{0}'", esType)); + ThrowError(il, $"An array expected to perform the 'stelem' instruction but was '{esType}'"); } } } \ No newline at end of file diff --git a/GrEmit/StackMutators/StfldStackMutator.cs b/GrEmit/StackMutators/StfldStackMutator.cs index 576b9e0..13c09a6 100644 --- a/GrEmit/StackMutators/StfldStackMutator.cs +++ b/GrEmit/StackMutators/StfldStackMutator.cs @@ -8,18 +8,18 @@ internal class StfldStackMutator : StackMutator public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref EvaluationStack stack) { var field = ((FieldILInstructionParameter)parameter).Field; - CheckNotEmpty(il, stack, () => string.Format("In order to store the field '{0}' a value must be put onto the evaluation stack", Formatter.Format(field))); + CheckNotEmpty(il, stack, () => $"In order to store the field '{Formatter.Format(field)}' a value must be put onto the evaluation stack"); CheckCanBeAssigned(il, field.FieldType, stack.Pop()); if (!field.IsStatic) { var declaringType = field.DeclaringType; - CheckNotEmpty(il, stack, () => string.Format("In order to store the field '{0}' an instance must be put onto the evaluation stack", Formatter.Format(field))); + CheckNotEmpty(il, stack, () => $"In order to store the field '{Formatter.Format(field)}' an instance must be put onto the evaluation stack"); var instance = stack.Pop().ToType(); if (instance != null) { if (instance.IsValueType) - ThrowError(il, string.Format("In order to store the field '{0}' of a value type '{1}' load an instance by ref", Formatter.Format(field), Formatter.Format(instance))); + ThrowError(il, $"In order to store the field '{Formatter.Format(field)}' of a value type '{Formatter.Format(instance)}' load an instance by ref"); else if (!instance.IsByRef) CheckCanBeAssigned(il, declaringType, instance); else @@ -28,10 +28,10 @@ public override void Mutate(GroboIL il, ILInstructionParameter parameter, ref Ev if (elementType.IsValueType) { if (declaringType != elementType) - ThrowError(il, string.Format("Cannot store the field '{0}' to an instance of type '{1}'", Formatter.Format(field), Formatter.Format(elementType))); + ThrowError(il, $"Cannot store the field '{Formatter.Format(field)}' to an instance of type '{Formatter.Format(elementType)}'"); } else - ThrowError(il, string.Format("Cannot store the field '{0}' to an instance of type '{1}'", Formatter.Format(field), Formatter.Format(instance))); + ThrowError(il, $"Cannot store the field '{Formatter.Format(field)}' to an instance of type '{Formatter.Format(instance)}'"); } } } diff --git a/GrEmit/Utils/EmitHelpers.cs b/GrEmit/Utils/EmitHelpers.cs index e6d6689..ad18005 100644 --- a/GrEmit/Utils/EmitHelpers.cs +++ b/GrEmit/Utils/EmitHelpers.cs @@ -12,7 +12,7 @@ public static T CreateDelegate(DynamicMethod dm, object target) where T : cla var @delegate = dm.CreateDelegate(typeof(T), target); var result = (@delegate as T); if (result == null) - throw new ArgumentException(String.Format("Type {0} is not a delegate", typeof(T))); + throw new ArgumentException($"Type {typeof(T)} is not a delegate"); return result; } @@ -21,7 +21,7 @@ public static T CreateDelegate(DynamicMethod dm) where T : class var @delegate = dm.CreateDelegate(typeof(T)); var result = (@delegate as T); if (result == null) - throw new ArgumentException(String.Format("Type {0} is not a delegate", typeof(T))); + throw new ArgumentException($"Type {typeof(T)} is not a delegate"); return result; } @@ -31,7 +31,7 @@ public static T EmitDynamicMethod(string name, Module m, Action emit //HACK var methodInfo = delegateType.GetMethod("Invoke", BindingFlags.Public | BindingFlags.Instance); if (methodInfo == null) - throw new ArgumentException(String.Format("Type {0} is not a Delegate", delegateType)); + throw new ArgumentException($"Type {delegateType} is not a Delegate"); var dynamicMethod = new DynamicMethod(name, methodInfo.ReturnType, ReflectionExtensions.GetParameterTypes(methodInfo), m, true); using (var il = new GroboIL(dynamicMethod)) @@ -45,7 +45,7 @@ public static T EmitDynamicMethod(string name, Module m, Action> callExpr) var memberInfo = ((MemberExpression)expression).Member; var propertyInfo = memberInfo as PropertyInfo; if (propertyInfo == null) - throw new ArgumentException(string.Format("Bad expression. {0} is not a PropertyInfo", memberInfo)); + throw new ArgumentException($"Bad expression. {memberInfo} is not a PropertyInfo"); return propertyInfo; } @@ -102,7 +102,7 @@ public static FieldInfo GetStaticField(Expression> callExpr) var memberInfo = ((MemberExpression)expression).Member; var fi = memberInfo as FieldInfo; if (fi == null) - throw new ArgumentException(string.Format("Bad expression. {0} is not a FieldInfo", memberInfo)); + throw new ArgumentException($"Bad expression. {memberInfo} is not a FieldInfo"); return fi; } @@ -112,7 +112,7 @@ public static PropertyInfo GetProp(Expression> readPropFunc, var memberInfo = ((MemberExpression)expression).Member; var propertyInfo = memberInfo as PropertyInfo; if (propertyInfo == null) - throw new ArgumentException(string.Format("Bad expression. {0} is not a PropertyInfo", memberInfo)); + throw new ArgumentException($"Bad expression. {memberInfo} is not a PropertyInfo"); if (classGenericArgs.Length == 0) return propertyInfo; var mt = propertyInfo.MetadataToken; @@ -134,7 +134,7 @@ public static FieldInfo GetField(Expression> readPropFunc) var memberInfo = ((MemberExpression)expression).Member; var propertyInfo = memberInfo as FieldInfo; if (propertyInfo == null) - throw new ArgumentException(string.Format("Bad expression. {0} is not a FieldInfo", memberInfo)); + throw new ArgumentException($"Bad expression. {memberInfo} is not a FieldInfo"); return propertyInfo; } From ad83a488e71ed2d413a3c0243af1240229cea82a Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 15:28:12 +0500 Subject: [PATCH 03/10] version bump --- version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.json b/version.json index 4e5f343..d475226 100644 --- a/version.json +++ b/version.json @@ -1,6 +1,6 @@ { "$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", - "version": "2.3", + "version": "3.0", "assemblyVersion": { "precision": "build" }, From 3ae5cc31d2b7af58fa26750c135d827f0c7a4705 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 15:37:34 +0500 Subject: [PATCH 04/10] update GitVersioning dep --- Directory.Build.props | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 07b6ba4..0d582cb 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,9 +12,8 @@ $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - - - + + \ No newline at end of file From f99130e90cfbcc960c6be0a05a611493ff21d0d4 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 15:51:12 +0500 Subject: [PATCH 05/10] Nerdbank.GitVersioning v2.3.38 still does not work on Ubuntu+Mono --- Directory.Build.props | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Directory.Build.props b/Directory.Build.props index 0d582cb..b412b23 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,7 +12,8 @@ $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - + + From 19a326f99aebf03996d7aa48b9dc319aa9687e70 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 15:58:06 +0500 Subject: [PATCH 06/10] update nunit deps --- GrEmit.Tests/GrEmit.Tests.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/GrEmit.Tests/GrEmit.Tests.csproj b/GrEmit.Tests/GrEmit.Tests.csproj index 30e0742..76b5b02 100644 --- a/GrEmit.Tests/GrEmit.Tests.csproj +++ b/GrEmit.Tests/GrEmit.Tests.csproj @@ -6,9 +6,9 @@ - - - + + + From 19d2ebe71118ada89cf9fc07883b68d31ee3c3a9 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 16:10:50 +0500 Subject: [PATCH 07/10] switch tests to run on .net core 2.2 --- .travis.yml | 2 +- AsmToBytes/AsmToBytes.csproj | 2 +- GrEmit.Tests/GrEmit.Tests.csproj | 2 +- GrEmit.Tests/TestGrobufUsages.cs | 8 ++++---- global.json | 2 +- test-by-travis.sh | 4 ++-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5e2f995..1b153f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ sudo: false language: csharp matrix: include: - - dotnet: 2.1.400 + - dotnet: 2.2.100 mono: none env: DOTNETCORE=1 - mono: latest diff --git a/AsmToBytes/AsmToBytes.csproj b/AsmToBytes/AsmToBytes.csproj index 016d150..e50a26f 100644 --- a/AsmToBytes/AsmToBytes.csproj +++ b/AsmToBytes/AsmToBytes.csproj @@ -3,7 +3,7 @@ Exe false - netcoreapp2.0;net45 + netcoreapp2.2;net45 \ No newline at end of file diff --git a/GrEmit.Tests/GrEmit.Tests.csproj b/GrEmit.Tests/GrEmit.Tests.csproj index 76b5b02..47967fb 100644 --- a/GrEmit.Tests/GrEmit.Tests.csproj +++ b/GrEmit.Tests/GrEmit.Tests.csproj @@ -2,7 +2,7 @@ false - netcoreapp2.0;net45 + netcoreapp2.2;net45 diff --git a/GrEmit.Tests/TestGrobufUsages.cs b/GrEmit.Tests/TestGrobufUsages.cs index af2448a..53a9d44 100644 --- a/GrEmit.Tests/TestGrobufUsages.cs +++ b/GrEmit.Tests/TestGrobufUsages.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Reflection; using System.Reflection.Emit; @@ -45,10 +45,10 @@ private static void AssertDateTimeOffsetFields(DateTime dateTime, short offsetMi private static string SelectName(string netcoreName, string net45Name) { -#if NETCOREAPP2_0 - return netcoreName; -#else +#if NET45 return net45Name; +#else + return netcoreName; #endif } diff --git a/global.json b/global.json index c0dbd1a..a4a01b2 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "2.1.400" + "version": "2.2.100" } } \ No newline at end of file diff --git a/test-by-travis.sh b/test-by-travis.sh index cf67ced..60e4170 100755 --- a/test-by-travis.sh +++ b/test-by-travis.sh @@ -6,8 +6,8 @@ if [ ${DOTNETCORE} -eq 1 ] then dotnet restore ./GrEmit.sln --verbosity m dotnet build --configuration Release --framework netstandard2.0 ./GrEmit/GrEmit.csproj - dotnet build --configuration Release --framework netcoreapp2.0 ./GrEmit.Tests/GrEmit.Tests.csproj - dotnet test --no-build --configuration Release --framework netcoreapp2.0 ./GrEmit.Tests/GrEmit.Tests.csproj + dotnet build --configuration Release --framework netcoreapp2.2 ./GrEmit.Tests/GrEmit.Tests.csproj + dotnet test --no-build --configuration Release --framework netcoreapp2.2 ./GrEmit.Tests/GrEmit.Tests.csproj else nuget install NUnit.ConsoleRunner -Version 3.7.0 -OutputDirectory testrunner msbuild /t:Restore /p:Configuration=Release /p:TargetFramework=net45 ./GrEmit.sln From 2f2bf063bf740097c90ef5af16a287d44f7289bf Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 16:41:19 +0500 Subject: [PATCH 08/10] try to fix build on travis as suggested here: https://travis-ci.community/t/dotnet-core-2-2/1216/5 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1b153f2..921e54b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,4 @@ -dist: trusty +dist: xenial sudo: false language: csharp matrix: From 5a9bbe4b3aaa99d1fae055fb1db959d748831372 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 17:28:31 +0500 Subject: [PATCH 09/10] try to fix "The current .NET SDK does not support targeting .NET Core 2.2" error on travis in mono environment --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 921e54b..29871c3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,10 @@ dist: xenial sudo: false language: csharp +dotnet: 2.2.100 matrix: include: - - dotnet: 2.2.100 - mono: none + - mono: none env: DOTNETCORE=1 - mono: latest script: From d03cb7c62378a557f0009d9ea00195f925b95866 Mon Sep 17 00:00:00 2001 From: avk Date: Sun, 13 Jan 2019 18:21:59 +0500 Subject: [PATCH 10/10] update nunit console runner --- test-by-travis.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-by-travis.sh b/test-by-travis.sh index 60e4170..0242cf0 100755 --- a/test-by-travis.sh +++ b/test-by-travis.sh @@ -9,8 +9,8 @@ then dotnet build --configuration Release --framework netcoreapp2.2 ./GrEmit.Tests/GrEmit.Tests.csproj dotnet test --no-build --configuration Release --framework netcoreapp2.2 ./GrEmit.Tests/GrEmit.Tests.csproj else - nuget install NUnit.ConsoleRunner -Version 3.7.0 -OutputDirectory testrunner - msbuild /t:Restore /p:Configuration=Release /p:TargetFramework=net45 ./GrEmit.sln + nuget install NUnit.ConsoleRunner -Version 3.9.0 -OutputDirectory testrunner + msbuild /t:Restore ./GrEmit.sln msbuild /t:Build /p:Configuration=Release /p:TargetFramework=net45 ./GrEmit.sln - mono ./testrunner/NUnit.ConsoleRunner.3.7.0/tools/nunit3-console.exe ./GrEmit.Tests/bin/Release/net45/GrEmit.Tests.dll + mono ./testrunner/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe ./GrEmit.Tests/bin/Release/net45/GrEmit.Tests.dll fi