Example #1
0
        public void Compile_LdNull_Test()
        {
            var method = ILMethodBuilder.Compile_LdNull();
            var actual = method.Invoke(null, Type.EmptyTypes);

            Assert.IsNull(actual);
        }
Example #2
0
        public void Compile_Ldloc_0_Test()
        {
            var method   = ILMethodBuilder.Compile_Ldloc_0();
            var actual   = method.Invoke(null, Type.EmptyTypes);
            var expected = 1;

            Assert.IsTrue((int)actual == expected, $"Actual: {actual}\r\n:Expected: {expected}");
        }
 internal ILLazyInvoke(ILCoder coding, ILVariable instance, ILMethodBuilder methodBuilder, Type[] genericArgs, ILData[] parameters)
     : base(coding)
 {
     this._instance      = instance;
     this._methodBuilder = methodBuilder;
     this._parameters    = parameters;
     this._genericArgs   = genericArgs;
     this._method        = null;
 }
Example #4
0
        public void Compile_AddIntLocalsTest()
        {
            var defaultAdd = ILMethodBuilder.Compile_AddIntLocals();

            var actual   = defaultAdd.Invoke(null, Type.EmptyTypes);
            var expected = 2;

            Assert.IsTrue((int)actual == expected, $"Actual: {actual}\r\n:Expected: {expected}");
        }
Example #5
0
        public void Compile_Add_Ovf_UnTest()
        {
            var madd = ILMethodBuilder.Compile_Add_Ovf_Un();

            Assert.IsNotNull(madd);
            var actual   = madd.Invoke(null, Type.EmptyTypes);
            var expected = 2u;

            Assert.IsTrue((uint)actual == expected, $"Actual: {actual}\r\n:Expected: {expected}");
        }
Example #6
0
        public void Compile_AddTypedTest()
        {
            var madd = ILMethodBuilder.Compile_Add(typeof(long), new[] { typeof(long), typeof(long) });

            Assert.IsNotNull(madd);
            var actual   = madd.Invoke(null, new object[] { 1L, 1L });
            var expected = 2L;

            Assert.IsTrue((long)actual == expected, $"Actual: {actual}\r\n:Expected: {expected}");
        }
Example #7
0
        public void Compile_Add_OvfTest()
        {
            var madd = ILMethodBuilder.Compile_Add_Ovf();

            Assert.IsNotNull(madd);
            try
            {
                var actual = madd.Invoke(null, Type.EmptyTypes);
            }
            catch (System.Reflection.TargetInvocationException refEx)
            {
                throw refEx.InnerException;
            }
        }
Example #8
0
        private TranslatedSub TranslateTier0(MemoryManager memory, long position, ExecutionMode mode)
        {
            Block block = Decoder.DecodeBasicBlock(memory, position, mode);

            ILEmitterCtx context = new ILEmitterCtx(_cache, block);

            string subName = GetSubroutineName(position);

            ILMethodBuilder ilMthdBuilder = new ILMethodBuilder(context.GetILBlocks(), subName);

            TranslatedSub subroutine = ilMthdBuilder.GetSubroutine();

            subroutine.SetType(TranslatedSubType.SubTier0);

            _cache.AddOrUpdate(position, subroutine, block.OpCodes.Count);

            return(subroutine);
        }
Example #9
0
        public void Compile_Add_Ovf_UnTest1()
        {
            var madd = ILMethodBuilder.Compile_Add_Ovf_Un(typeof(uint), new[] { typeof(uint), typeof(uint) });

            Assert.IsNotNull(madd);
            var actual   = madd.Invoke(null, new object[] { 1u, 1u });
            var expected = 2u;

            Assert.IsTrue((uint)actual == expected, $"Actual: {actual}\r\n:Expected: {expected}");
            try
            {
                actual = madd.Invoke(null, new object[] { uint.MaxValue, 1u });
            }
            catch (System.Reflection.TargetInvocationException refEx)
            {
                throw refEx.InnerException;
            }
        }
Example #10
0
        public void Compile_Add_OvfTest1()
        {
            var madd = ILMethodBuilder.Compile_Add_Ovf(typeof(long), new[] { typeof(long), typeof(long) });

            Assert.IsNotNull(madd);
            try
            {
                var actual   = madd.Invoke(null, new object[] { 1L, 1L });
                var expected = 2L;
                Assert.IsTrue((long)actual == expected, $"Actual: {actual}\r\n:Expected: {expected}");

                actual = madd.Invoke(null, new object[] { long.MaxValue, long.MaxValue });
            }
            catch (System.Reflection.TargetInvocationException refEx)
            {
                throw refEx.InnerException;
            }
        }
Example #11
0
        private void TranslateTier1(MemoryManager memory, long position, ExecutionMode mode)
        {
            Block graph = Decoder.DecodeSubroutine(_cache, memory, position, mode);

            ILEmitterCtx context = new ILEmitterCtx(_cache, graph);

            ILBlock[] ilBlocks = context.GetILBlocks();

            string subName = GetSubroutineName(position);

            ILMethodBuilder ilMthdBuilder = new ILMethodBuilder(ilBlocks, subName);

            TranslatedSub subroutine = ilMthdBuilder.GetSubroutine();

            subroutine.SetType(TranslatedSubType.SubTier1);

            int ilOpCount = 0;

            foreach (ILBlock ilBlock in ilBlocks)
            {
                ilOpCount += ilBlock.Count;
            }

            _cache.AddOrUpdate(position, subroutine, ilOpCount);

            //Mark all methods that calls this method for ReJiting,
            //since we can now call it directly which is faster.
            if (_cache.TryGetSubroutine(position, out TranslatedSub oldSub))
            {
                foreach (long callerPos in oldSub.GetCallerPositions())
                {
                    if (_cache.TryGetSubroutine(position, out TranslatedSub callerSub))
                    {
                        callerSub.MarkForReJit();
                    }
                }
            }
        }
        private void invokeInstanceBuilder()
        {
            ILVariable      obj           = this._instance;
            ILMethodBuilder methodBuilder = this._methodBuilder;

            Type[]   genericArgs  = this._genericArgs;
            ILData[] invokeParams = this._parameters;

            MethodInfo method = methodBuilder.MethodBuilder;

            if (genericArgs == null || genericArgs.Length == 0)
            {
                if (obj.ILType.IsValueType)
                {
                    ((IILPusher)obj).PushAddress();
                }
                else
                {
                    if (obj.ILType.IsGenericParameter)
                    {
                        ((IILPusher)obj).PushAddress();
                        obj.Coding.Generator.Emit(OpCodes.Constrained, obj.ILType);
                    }
                    else
                    {
                        ((IILPusher)obj).Push();
                    }
                }

                if (invokeParams != null)
                {
                    Type   typeOfObject   = typeof(object);
                    Type[] parameterTypes = methodBuilder.GetParameterTypes();
                    for (int i = 0; i < invokeParams.Length; i++)
                    {
                        ILData invokeParam   = invokeParams[i];
                        Type   parameterType = parameterTypes[i];
                        if (parameterType.IsByRef)
                        {
                            ((IILPusher)invokeParam).PushAddress();
                        }
                        else
                        {
                            if (parameterType == typeOfObject)
                            {
                                ((IILPusher)invokeParam).Push();
                                obj.Generator.Emit(OpCodes.Box, invokeParam.ILType);
                            }
                            else
                            {
                                ((IILPusher)invokeParam).Push();
                            }
                        }
                    }
                }

                //if (method.IsVirtual)
                //    obj.Generator.Emit(OpCodes.Callvirt, method);
                //else
                obj.Generator.Emit(OpCodes.Call, method);
            }
            else
            {
                if (obj.ILType.IsValueType)
                {
                    ((IILPusher)obj).PushAddress();
                }
                else
                {
                    if (obj.ILType.IsGenericParameter)
                    {
                        ((IILPusher)obj).PushAddress();
                        obj.Coding.Generator.Emit(OpCodes.Constrained, obj.ILType);
                    }
                    else
                    {
                        ((IILPusher)obj).Push();
                    }
                }

                if (invokeParams != null)
                {
                    Type   typeOfObject   = typeof(object);
                    Type[] parameterTypes = methodBuilder.GetParameterTypes();
                    for (int i = 0; i < invokeParams.Length; i++)
                    {
                        ILData invokeParam = invokeParams[i];
                        if (parameterTypes[i] == typeOfObject)
                        {
                            ((IILPusher)invokeParam).Push();
                            obj.Generator.Emit(OpCodes.Box, invokeParam.ILType);
                        }
                        else
                        {
                            ((IILPusher)invokeParam).Push();
                        }
                    }
                }
                method = method.MakeGenericMethod(genericArgs);
                //if (method.IsVirtual)
                //    obj.Generator.Emit(OpCodes.Callvirt, method);
                //else
                obj.Generator.Emit(OpCodes.Call, method);
            }
        }
        private void invokeStaticBuilder()
        {
            ILMethodBuilder staticMethodBuilder = this._methodBuilder;

            Type[]   genericArgs  = this._genericArgs;
            ILData[] invokeParams = this._parameters;

            if (genericArgs == null || genericArgs.Length == 0)
            {
                if (invokeParams != null)
                {
                    Type   typeOfObject   = typeof(object);
                    Type[] parameterTypes = staticMethodBuilder.GetParameterTypes();
                    for (int i = 0; i < invokeParams.Length; i++)
                    {
                        ILData invokeParam   = invokeParams[i];
                        Type   parameterType = parameterTypes[i];
                        if (parameterType.IsByRef)
                        {
                            ((IILPusher)invokeParam).PushAddress();
                        }
                        else
                        {
                            if (parameterTypes[i] == typeOfObject)
                            {
                                ((IILPusher)invokeParam).Push();
                                this.Generator.Emit(OpCodes.Box, invokeParam.ILType);
                            }
                            else
                            {
                                ((IILPusher)invokeParam).Push();
                            }
                        }
                    }
                }
                this.Generator.Emit(OpCodes.Call, staticMethodBuilder.MethodBuilder);
            }
            else
            {
                if (invokeParams != null)
                {
                    Type   typeOfObject   = typeof(object);
                    Type[] parameterTypes = staticMethodBuilder.GetParameterTypes();
                    for (int i = 0; i < invokeParams.Length; i++)
                    {
                        ILData invokeParam   = invokeParams[i];
                        Type   parameterType = parameterTypes[i];
                        if (parameterType.IsByRef)
                        {
                            ((IILPusher)invokeParam).PushAddress();
                        }
                        else
                        {
                            if (parameterTypes[i] == typeOfObject)
                            {
                                ((IILPusher)invokeParam).Push();
                                this.Generator.Emit(OpCodes.Box, invokeParam.ILType);
                            }
                            else
                            {
                                ((IILPusher)invokeParam).Push();
                            }
                        }
                    }
                }
                MethodInfo method = staticMethodBuilder.MethodBuilder.MakeGenericMethod(genericArgs);
                this.Generator.Emit(OpCodes.Call, method);
            }
        }
Example #14
0
 public void Emit(ILMethodBuilder context)
 {
     context.Generator.EmitWriteLine(_text);
 }
Example #15
0
 public void Emit(ILMethodBuilder context)
 {
 }
Example #16
0
        public void CompileBinaryTest()
        {
            var madd = ILMethodBuilder.Compile_Add(typeof(uint), typeof(uint), typeof(uint));

            Assert.IsNotNull(madd);
            var actual   = madd.Invoke(null, new object[] { uint.MaxValue, 2u });
            var expected = 1u;

            Assert.IsTrue((uint)actual == expected, $"Actual: {actual}\r\n:Expected: {expected}");


            var test_Add   = ILMethodBuilder.Compile_Add(typeof(uint), typeof(uint), typeof(uint));
            var result_Add = test_Add.Invoke(null, new object[] { uint.MaxValue, 2u });

            Assert.IsTrue((uint)result_Add == expected, $"Actual: {actual}\r\n:Expected: {expected}");

            var test_clt        = ILMethodBuilder.CompileBinary(ILEngine.ILOpCodeValues.Clt, typeof(bool), typeof(uint), typeof(uint));
            var result_test_clt = test_clt.Invoke(null, new object[] { uint.MaxValue, 2u });

            Assert.IsTrue((bool)result_test_clt);
            var result_test_clt2 = test_clt.Invoke(null, new object[] { 2u, uint.MaxValue });

            Assert.IsFalse((bool)result_test_clt2);


            var test_cgt        = ILMethodBuilder.CompileBinary(ILEngine.ILOpCodeValues.Cgt, typeof(bool), typeof(uint), typeof(uint));
            var result_test_cgt = test_cgt.Invoke(null, new object[] { uint.MaxValue, 2u });

            Assert.IsFalse((bool)result_test_cgt);
            var result_test_cgt2 = test_cgt.Invoke(null, new object[] { 2u, uint.MaxValue });

            Assert.IsTrue((bool)result_test_cgt2);

            var test_cgt_un        = ILMethodBuilder.CompileBinary(ILEngine.ILOpCodeValues.Cgt_Un, typeof(bool), typeof(uint), typeof(uint));
            var result_test_cgt_un = test_cgt_un.Invoke(null, new object[] { uint.MaxValue, 2u });

            Assert.IsTrue((bool)result_test_cgt_un);
            var result_test_cgt2_un = test_cgt_un.Invoke(null, new object[] { 2u, uint.MaxValue });

            Assert.IsFalse((bool)result_test_cgt2_un);

            var test_Add_Ovf = ILMethodBuilder.Compile_Add_Ovf(typeof(int), typeof(int), typeof(int));

            Assert.IsNotNull(test_Add_Ovf);
            Exception ex = null;

            try
            {
                var result_Add_Ovf = test_Add_Ovf.Invoke(null, new object[] { int.MaxValue, 1 });
            }
            catch (Exception c)
            {
                ex = c;
            }
            Assert.IsNotNull(ex);
            Assert.IsInstanceOfType(ex, typeof(System.Reflection.TargetInvocationException));
            Assert.IsNotNull(ex.InnerException);
            Assert.IsInstanceOfType(ex.InnerException, typeof(OverflowException));

            var test_Add_Ovf_Un = ILMethodBuilder.Compile_Add_Ovf_Un(typeof(uint), typeof(uint), typeof(uint));

            Assert.IsNotNull(test_Add_Ovf_Un);
            ex = null;

            try
            {
                var result_Add_Ovf_Un = test_Add_Ovf_Un.Invoke(null, new object[] { uint.MaxValue, 2u });
            }
            catch (Exception c)
            {
                ex = c;
            }
            Assert.IsNotNull(ex);
            Assert.IsInstanceOfType(ex, typeof(System.Reflection.TargetInvocationException));
            Assert.IsNotNull(ex.InnerException);
            Assert.IsInstanceOfType(ex.InnerException, typeof(OverflowException));
        }