Пример #1
0
        internal sealed override void CompileIKVM(IKVMCompilationContext context, IKVM.Reflection.Universe universe)
        {
            var stack = context.Stack;

            if (stack.Count < 1)
            {
                throw new StackTooSmallException(OpCode.Int32CountTrailingZeroes, 1, stack.Count);
            }

            var type = stack.Peek(); //Assuming validation passes, the remaining type will be this.

            if (type != ValueType.Int32)
            {
                throw new StackTypeInvalidException(OpCode.Int32CountTrailingZeroes, ValueType.Int32, type);
            }

            context.Emit(IKVM.Reflection.Emit.OpCodes.Call, context[HelperMethod.Int32CountTrailingZeroes, (helper, c) =>
                                                                    {
                                                                        Assert(c != null);

                                                                        var result = context.ExportsBuilder.DefineMethod(
                                                                            "☣ Int32CountTrailingZeroes",
                                                                            IKVMCompilationContext.HelperMethodAttributes,
                                                                            universe.Import(typeof(int)),
                                                                            new[] { universe.Import(typeof(uint)) });

                                                                        //All modern CPUs have a fast instruction specifically for this process, but there's no way to use it from .NET.
                                                                        //Based on the algorithm found here: http://aggregate.org/MAGIC/#Trailing%20Zero%20Count
                                                                        var il = result.GetILGenerator();
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Neg);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.And);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldc_I4_1);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Sub);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Call, Int32CountOneBits.IKVMCreateHelper(HelperMethod.Int32CountOneBits, context, universe));
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ret);

                                                                        return(result);
                                                                    }
                         ]);
        }
        internal sealed override void CompileIKVM(IKVMCompilationContext context, IKVM.Reflection.Universe universe)
        {
            var stack = context.Stack;

            if (stack.Count < 1)
            {
                throw new StackTooSmallException(OpCode.Int32CountLeadingZeroes, 1, stack.Count);
            }

            var type = stack.Peek(); //Assuming validation passes, the remaining type will be this.

            if (type != ValueType.Int32)
            {
                throw new StackTypeInvalidException(OpCode.Int32CountLeadingZeroes, ValueType.Int32, type);
            }

            context.Emit(IKVM.Reflection.Emit.OpCodes.Call, context[HelperMethod.Int32CountLeadingZeroes, (helper, c) =>
                                                                    {
                                                                        Assert(c != null);

                                                                        var result = context.ExportsBuilder.DefineMethod(
                                                                            "☣ Int32CountLeadingZeroes",
                                                                            IKVMCompilationContext.HelperMethodAttributes,
                                                                            universe.Import(typeof(uint)),
                                                                            new[] { universe.Import(typeof(uint)) });

                                                                        //All modern CPUs have a fast instruction specifically for this process, but there's no way to use it from .NET.
                                                                        //This algorithm is from https://stackoverflow.com/questions/10439242/count-leading-zeroes-in-an-int32
                                                                        var il = result.GetILGenerator();
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldc_I4_1);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Shr_Un);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Or);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Starg_S, 0);

                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldc_I4_2);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Shr_Un);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Or);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Starg_S, 0);

                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldc_I4_4);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Shr_Un);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Or);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Starg_S, 0);

                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldc_I4_8);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Shr_Un);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Or);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Starg_S, 0);

                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldc_I4_S, 16);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Shr_Un);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Or);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Starg_S, 0);

                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldc_I4_S, 32);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ldarg_0);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Call, Int32CountOneBits.IKVMCreateHelper(HelperMethod.Int32CountOneBits, context, universe));
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Sub);
                                                                        il.Emit(IKVM.Reflection.Emit.OpCodes.Ret);

                                                                        return(result);
                                                                    }
                         ]);
        }