Пример #1
0
        internal static UnaryMath UnaryRMath(OpCodeValue ocv, Expression operand)
        {
            var b = false;

            operand.Type.MeetWith(new Types.Real(), ref b);
            return(new(ocv, operand));
        }
Пример #2
0
 public Jump(OpCodeValue code, bool isInTable, int address, bool isConditional)
     : base(code)
 {
     this.Address       = address;
     this.IsInTable     = isInTable;
     this.IsConditional = isConditional;
 }
Пример #3
0
        public static BinaryMath BinaryRMath(OpCodeValue ocv, Expression left, Expression right)
        {
            var b = false;

            left.Type.MeetWith(new Types.Real(), ref b);
            right.Type.MeetWith(new Types.Real(), ref b);
            return(new(ocv, left, right));
        }
Пример #4
0
        public ConditionalItem(ByteCodeGenerator generator, OpCodeValue opCode, Chain trueChain, Chain falseChain)
            : base(generator, PrimativeTypes.Boolean)
        {
            OpCode = opCode;

            TrueJumps = trueChain;
            FalseJumps = falseChain;
        }
 public Key(Type type, string methodName, OpCodeValue callOpCode, Type returnType, Type[] methodParameterTypes, Type[] methodGenericArguments)
 {
     Type                   = type;
     MethodName             = methodName;
     CallOpCode             = callOpCode;
     ReturnType             = returnType;
     MethodParameterTypes   = methodParameterTypes;
     MethodGenericArguments = methodGenericArguments;
 }
Пример #6
0
 private MethodBuilder(Module resolutionModule, int mdToken, int opCode, string methodName)
 {
     _resolutionModule      = resolutionModule;
     _mdToken               = mdToken;
     _opCode                = (OpCodeValue)opCode;
     _originalOpCodeValue   = opCode;
     _methodName            = methodName;
     _forceMethodDefResolve = false;
 }
Пример #7
0
 private MethodBuilder(Assembly resolutionAssembly, int mdToken, int opCode, string methodName)
 {
     _resolutionAssembly    = resolutionAssembly;
     _mdToken               = mdToken;
     _opCode                = (OpCodeValue)opCode;
     _originalOpCodeValue   = opCode;
     _methodName            = methodName;
     _forceMethodDefResolve = false;
 }
Пример #8
0
        public static BinaryMath BinaryIMath(OpCodeValue ocv, Expression left, Expression right)
        {
            var b = false;

            if (!left.Type.ResolvesTo <Types.Pointer>())
            {
                left.Type.MeetWith(new Types.Integer(), ref b);
            }
            if (!right.Type.ResolvesTo <Types.Pointer>())
            {
                right.Type.MeetWith(new Types.Integer(), ref b);
            }
            return(new(ocv, left, right));
        }
Пример #9
0
        public unsafe void Arithmetic_LDC_I2_Test(float expected, OpCodeValue actor)
        {
            using var ctx = CreateContext();
            var result = ctx.Execute((gen, _) =>
            {
                gen.Emit(OpCodes.LDC_I2_S, 12);
                gen.Emit(OpCodes.LDC_I2_S, 2);
                gen.Emit(OpCodes.all[actor]);
                gen.Emit(OpCodes.RET);
            });

            Validate(result);
            Assert.AreEqual(VeinTypeCode.TYPE_R4, (*result.returnValue).type);
            Assert.AreEqual(expected, (*result.returnValue).data.f_r4);
        }
Пример #10
0
        public unsafe void Arithmetic_Double_Test(int expected, OpCodeValue actor)
        {
            using var ctx = CreateContext();
            var result = ctx.Execute((gen, _) =>
            {
                gen.Emit(OpCodes.LDC_F8, 12d);
                gen.Emit(OpCodes.LDC_F8, 2d);
                gen.Emit(OpCodes.all[actor]);
                gen.Emit(OpCodes.RET);
            });

            Validate(result);
            Assert.AreEqual(VeinTypeCode.TYPE_R8, (*result.returnValue).type);
            Assert.AreEqual((double)expected, (*result.returnValue).data.f);
        }
Пример #11
0
        public unsafe void DIV_Test(OpCodeValue op, VeinTypeCode code)
        {
            using var ctx = CreateContext();
            var result = ctx.Execute((gen, _) =>
            {
                gen.Emit(OpCodes.all[op]);
                gen.Emit(OpCodes.all[op]);
                gen.Emit(OpCodes.DIV);
                gen.Emit(OpCodes.RET);
            });

            Validate(result);
            Assert.AreEqual(code, (*result.returnValue).type);
            Assert.AreEqual(5 / 5, (*result.returnValue).data.l);
        }
Пример #12
0
        internal OpCode(OpCodeValue bc, NormalizedOpCodeValues normalizedValue, int arg, OpCodeMode reg,
                        OpCodeModeWide wide, bool cannotThrow)
        {
            Value = bc;
            Mode = reg;
            WideMode = wide;
            NormalizedValue = normalizedValue;

            this.arg = arg;
            Flags = OpCodeFlags.FixedArg;
            if (cannotThrow)
            {
                Flags |= OpCodeFlags.CannotThrow;
            }
        }
Пример #13
0
 /// <summary>
 /// Gets a previously cache delegate used to call the specified method,
 /// or creates and caches a new delegate if not found.
 /// </summary>
 /// <param name="type">The <see cref="Type"/> that contains the method.</param>
 /// <param name="methodName">The name of the method.</param>
 /// <param name="callOpCode">The OpCode to use in the method call.</param>
 /// <param name="returnType">The method's return type.</param>
 /// <param name="methodParameterTypes">optional types for the method parameters</param>
 /// <param name="methodGenericArguments">optional generic type arguments for a generic method</param>
 /// <returns>A <see cref="Delegate"/> that can be used to execute the dynamic method.</returns>
 public static TDelegate GetOrCreateMethodCallDelegate(
     Type type,
     string methodName,
     OpCodeValue callOpCode,
     Type returnType               = null,
     Type[] methodParameterTypes   = null,
     Type[] methodGenericArguments = null) =>
 Cache.GetOrAdd(
     new Key(type, methodName, callOpCode, returnType, methodParameterTypes, methodGenericArguments),
     key => CreateMethodCallDelegate(
         key.Type,
         key.MethodName,
         key.CallOpCode,
         key.MethodParameterTypes,
         key.MethodGenericArguments));
Пример #14
0
    public static int Main()
    {
        OpCodeValue test = new OpCodeValue();

        TestLibrary.TestFramework.BeginTestCase("OpCodeValue");

        if (test.RunTests())
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("PASS");
            return 100;
        }
        else
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("FAIL");
            return 0;
        }
    }
Пример #15
0
    public static int Main()
    {
        OpCodeValue test = new OpCodeValue();

        TestLibrary.TestFramework.BeginTestCase("OpCodeValue");

        if (test.RunTests())
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("PASS");
            return(100);
        }
        else
        {
            TestLibrary.TestFramework.EndTestCase();
            TestLibrary.TestFramework.LogInformation("FAIL");
            return(0);
        }
    }
Пример #16
0
            public Key(
                Module callingModule,
                int mdToken,
                OpCodeValue callOpCode,
                Type concreteType,
                Type[] explicitParameterTypes,
                Type[] methodGenerics,
                Type[] declaringTypeGenerics)
            {
                CallingModuleMetadataToken = callingModule.MetadataToken;
                MethodMetadataToken        = mdToken;
                CallOpCode       = callOpCode;
                ConcreteTypeName = concreteType.AssemblyQualifiedName;

                GenericSpec = "_gArgs_";

                if (methodGenerics != null)
                {
                    for (var i = 0; i < methodGenerics.Length; i++)
                    {
                        GenericSpec = string.Concat(GenericSpec, $"_{methodGenerics[i].FullName}_");
                    }
                }

                GenericSpec = string.Concat(GenericSpec, "_gParams_");

                if (declaringTypeGenerics != null)
                {
                    for (var i = 0; i < declaringTypeGenerics.Length; i++)
                    {
                        GenericSpec = string.Concat(GenericSpec, $"_{declaringTypeGenerics[i].FullName}_");
                    }
                }

                ExplicitParams = string.Empty;

                if (explicitParameterTypes != null)
                {
                    ExplicitParams = string.Join("_", explicitParameterTypes.Select(ept => ept.FullName));
                }
            }
Пример #17
0
 public CountByte(OpCodeValue code, int count) : base(code) => this.Count = count;
Пример #18
0
        // ReSharper restore InconsistentNaming
        public static OpCodeValue Negate(OpCodeValue opcode)
        {
            if (opcode == OpCodeValue.ifnull) return OpCodeValue.ifnonnull;
            if (opcode == OpCodeValue.ifnonnull) return OpCodeValue.ifnull;

            return (OpCodeValue)(((int)opcode + 1) ^ 1) - 1;
        }
Пример #19
0
 public ExternalWord(OpCodeValue code, string segment, int segNum, int offset)
     : base(code, offset)
 {
     this.Segment   = segment;
     this.SegNumber = segNum;
 }
        /// <summary>
        /// Creates a simple <see cref="DynamicMethod"/> using <see cref="System.Reflection.Emit"/> that
        /// calls a method with the specified name and parameter types.
        /// </summary>
        /// <param name="type">The <see cref="Type"/> that contains the method to call when the returned delegate is executed..</param>
        /// <param name="methodName">The name of the method to call when the returned delegate is executed.</param>
        /// <param name="callOpCode">The OpCode to use in the method call.</param>
        /// <param name="methodParameterTypes">If not null, use method overload that matches the specified parameters.</param>
        /// <param name="methodGenericArguments">If not null, use method overload that has the same number of generic arguments.</param>
        /// <returns>A <see cref="Delegate"/> that can be used to execute the dynamic method.</returns>
        public static TDelegate CreateMethodCallDelegate(
            Type type,
            string methodName,
            OpCodeValue callOpCode,
            Type[] methodParameterTypes   = null,
            Type[] methodGenericArguments = null)
        {
            Type delegateType = typeof(TDelegate);

            Type[] genericTypeArguments = delegateType.GenericTypeArguments;

            Type[] parameterTypes;
            Type   returnType;

            if (delegateType.Name.StartsWith("Func`"))
            {
                // last generic type argument is the return type
                int parameterCount = genericTypeArguments.Length - 1;
                parameterTypes = new Type[parameterCount];
                Array.Copy(genericTypeArguments, parameterTypes, parameterCount);

                returnType = genericTypeArguments[parameterCount];
            }
            else if (delegateType.Name.StartsWith("Action`"))
            {
                parameterTypes = genericTypeArguments;
                returnType     = typeof(void);
            }
            else
            {
                throw new Exception($"Only Func<> or Action<> are supported in {nameof(CreateMethodCallDelegate)}.");
            }

            // find any method that matches by name and parameter types
            IEnumerable <MethodInfo> methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
                                               .Where(m => m.Name == methodName);

            // if methodParameterTypes was specified, check for a method that matches
            if (methodParameterTypes != null)
            {
                methods = methods.Where(
                    m =>
                {
                    var ps = m.GetParameters();
                    if (ps.Length != methodParameterTypes.Length)
                    {
                        return(false);
                    }

                    for (var i = 0; i < ps.Length; i++)
                    {
                        var t1 = ps[i].ParameterType;
                        var t2 = methodParameterTypes[i];

                        // generics can be tricky to compare for type equality
                        // so we will just check the namespace and name
                        if (t1.Namespace != t2.Namespace || t1.Name != t2.Name)
                        {
                            return(false);
                        }
                    }

                    return(true);
                });
            }

            if (methodGenericArguments != null)
            {
                methods = methods.Where(
                    m => m.IsGenericMethodDefinition &&
                    m.GetGenericArguments().Length == methodGenericArguments.Length);
            }

            MethodInfo methodInfo = methods.FirstOrDefault();

            if (methodInfo == null)
            {
                // method not found
                // TODO: logging
                return(null);
            }

            if (methodGenericArguments != null)
            {
                methodInfo = methodInfo.MakeGenericMethod(methodGenericArguments);
            }

            Type[] effectiveParameterTypes;

            IEnumerable <Type> reflectedParameterTypes = methodInfo.GetParameters()
                                                         .Select(p => p.ParameterType);

            if (methodInfo.IsStatic)
            {
                effectiveParameterTypes = reflectedParameterTypes.ToArray();
            }
            else
            {
                // for instance methods, insert object's type as first element in array
                effectiveParameterTypes = new[] { type }
                .Concat(reflectedParameterTypes)
                .ToArray();
            }

            DynamicMethod dynamicMethod = new DynamicMethod(methodInfo.Name, returnType, parameterTypes, ObjectExtensions.Module, skipVisibility: true);
            ILGenerator   il            = dynamicMethod.GetILGenerator();

            // load each argument and cast or unbox as necessary
            for (ushort argumentIndex = 0; argumentIndex < parameterTypes.Length; argumentIndex++)
            {
                Type delegateParameterType   = parameterTypes[argumentIndex];
                Type underlyingParameterType = effectiveParameterTypes[argumentIndex];

                switch (argumentIndex)
                {
                case 0:
                    il.Emit(OpCodes.Ldarg_0);
                    break;

                case 1:
                    il.Emit(OpCodes.Ldarg_1);
                    break;

                case 2:
                    il.Emit(OpCodes.Ldarg_2);
                    break;

                case 3:
                    il.Emit(OpCodes.Ldarg_3);
                    break;

                default:
                    il.Emit(OpCodes.Ldarg_S, argumentIndex);
                    break;
                }

                if (underlyingParameterType.IsValueType && delegateParameterType == typeof(object))
                {
                    il.Emit(OpCodes.Unbox_Any, underlyingParameterType);
                }
                else if (underlyingParameterType != delegateParameterType)
                {
                    il.Emit(OpCodes.Castclass, underlyingParameterType);
                }
            }

            if (callOpCode == OpCodeValue.Call || methodInfo.IsStatic)
            {
                // non-virtual call (e.g. static method, or method override calling overriden implementation)
                il.Emit(OpCodes.Call, methodInfo);
            }
            else if (callOpCode == OpCodeValue.Callvirt)
            {
                // Note: C# compiler uses CALLVIRT for non-virtual
                // instance methods to get the cheap null check
                il.Emit(OpCodes.Callvirt, methodInfo);
            }
            else
            {
                throw new NotSupportedException($"OpCode {callOpCode} not supported when calling a method.");
            }

            if (methodInfo.ReturnType.IsValueType && !returnType.IsValueType)
            {
                il.Emit(OpCodes.Box, methodInfo.ReturnType);
            }
            else if (methodInfo.ReturnType.IsValueType && returnType.IsValueType && methodInfo.ReturnType != returnType)
            {
                throw new ArgumentException($"Cannot convert the target method's return type {methodInfo.ReturnType.FullName} (valuetype) to the delegate method's return type {returnType.FullName} (valuetype)");
            }
            else if (!methodInfo.ReturnType.IsValueType && returnType.IsValueType)
            {
                throw new ArgumentException($"Cannot reliably convert the target method's return type {methodInfo.ReturnType.FullName} (reference type) to the delegate method's return type {returnType.FullName} (value type)");
            }
            else if (!methodInfo.ReturnType.IsValueType && !returnType.IsValueType && methodInfo.ReturnType != returnType)
            {
                il.Emit(OpCodes.Castclass, returnType);
            }

            il.Emit(OpCodes.Ret);
            return((TDelegate)dynamicMethod.CreateDelegate(delegateType));
        }
Пример #21
0
        //public void Emit(OpCodeValue opcode, Label l)
        //{
        //    var capacity = opcode == OpCodeValue.goto_w || opcode == OpCodeValue.jsr_w ? 5 : 3;
        //    EnsureCapacity(capacity);
        //    var labelValue = l.GetLabelValue();
        //    var i = labelList[labelValue] - length;
        //    EmitOp(opcode);
        //    if (!alive) return;
        //    if (labelList[labelValue] == -1)
        //    {
        //        labelRefs[labelValue].Add(new Tuple<int, int>(length, capacity - 1));
        //        i = int.MaxValue;
        //    }
        //    if (capacity == 5)
        //    {
        //        EmitInt(i);
        //    }
        //    else
        //    {
        //        EmitShort((short)i);
        //    }
        //    UpdateStackBranch(opcode);
        //}
        public void EmitWide(OpCodeValue opcode, short s)
        {
            if (s > 0xFF)
            {
                EnsureCapacity(4);

                EmitOp(OpCodeValue.wide);
                EmitOp(opcode);
                EmitShort(s);
            }
            else
            {
                EnsureCapacity(2);

                EmitOp(opcode);
                EmitByte((byte)s);
            }

            if (!alive) return;
            UpdateStackWide(opcode, s);

            PostOp();
        }
Пример #22
0
        private void EmitOp(OpCodeValue opcode)
        {
            if (pendingJumps != null)
            {
                ResolvePendingJumps();
            }
            if (!alive) return;

            if (pendingStackMap)
            {
                pendingStackMap = false;
                EmitStackMapFrame();
            }

            EmitByte((byte)opcode);
        }
Пример #23
0
        internal DhcpPacket(Frame parentFrame, int packetStartIndex, int packetEndIndex)
            : base(parentFrame, packetStartIndex, packetEndIndex, "DHCP (Bootstrap protocol)")
        {
            this.opCode = (OpCodeValue)parentFrame.Data[packetStartIndex];
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("OpCode", this.OpCode.ToString());
            }
            //skip hardware type
            //skip hlen
            //skip hops
            this.transactionID = Utils.ByteConverter.ToUInt32(parentFrame.Data, packetStartIndex + 4);
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("Transaction ID", "0x" + this.transactionID.ToString("X2"));
            }
            this.secondsElapsed = Utils.ByteConverter.ToUInt16(parentFrame.Data, packetStartIndex + 8);
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("Seconds elapsed", this.secondsElapsed.ToString());
            }
            //skip flags (unused in BOOTP)

            byte[] ipAddrArray = new byte[4];
            Array.ConstrainedCopy(parentFrame.Data, packetStartIndex + 12, ipAddrArray, 0, 4);
            clientIpAddress = new System.Net.IPAddress(ipAddrArray);
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("Client IP Address", this.clientIpAddress.ToString());
            }
            Array.ConstrainedCopy(parentFrame.Data, packetStartIndex + 16, ipAddrArray, 0, 4);//do I need to create a new byte[4] before this one?
            yourIpAddress = new System.Net.IPAddress(ipAddrArray);
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("Your IP Address", this.yourIpAddress.ToString());
            }
            Array.ConstrainedCopy(parentFrame.Data, packetStartIndex + 20, ipAddrArray, 0, 4);
            serverIpAddress = new System.Net.IPAddress(ipAddrArray);
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("Server IP Address", this.serverIpAddress.ToString());
            }
            Array.ConstrainedCopy(parentFrame.Data, packetStartIndex + 24, ipAddrArray, 0, 4);
            gatewayIpAddress = new System.Net.IPAddress(ipAddrArray);
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("Gateway IP Address", this.gatewayIpAddress.ToString());
            }

            byte[] macAddrArray = new byte[6];
            Array.ConstrainedCopy(parentFrame.Data, packetStartIndex + 28, macAddrArray, 0, 6);
            clientMacAddress = new System.Net.NetworkInformation.PhysicalAddress(macAddrArray);
            if (!this.ParentFrame.QuickParse)
            {
                base.Attributes.Add("Client MAC Address", this.clientMacAddress.ToString());
            }
            //skip extra 10 bytes of client hardware address

            //Skip 192 octets of 0's. BOOTP legacy
            //64  - optional server host name
            //128 - boot file name
            //skip magic cookie (4 bytes)
            optionList = new List <Option>();
            int index = packetStartIndex + 240;//0x011A - 0x002A = 0xF0

            while (index < packetEndIndex)
            {
                Option option = new Option(parentFrame.Data, index);
                if (option.OptionCode == 0xff)//check for "End Option"
                {
                    break;
                }
                if (option.OptionCode == 3)//Default Gateway (router)
                {
                    this.gatewayIpAddress = new System.Net.IPAddress(option.OptionValue);
                }
                else if (option.OptionCode == 53)//extract the DHCP Message Type: 1=Discover, 2=Offer, 3=Request, 5=Ack, 8=Inform
                {
                    if (option.OptionValue != null && option.OptionValue.Length == 1)
                    {
                        this.dhcpMessageType = option.OptionValue[0];
                    }
                }
                optionList.Add(option);
                if (!this.ParentFrame.QuickParse)
                {
                    base.Attributes.Add("DHCP Options", option.OptionCode.ToString());
                }
                index += option.OptionValue.Length + 2;
            }
        }
Пример #24
0
        public void Emit(OpCodeValue opcode, int b)
        {
            EnsureCapacity(2);

            EmitOp(opcode);
            if (!alive) return;
            EmitInt(b);

            switch (opcode)
            {
                case OpCodeValue.goto_w:
                    MarkDead();
                    break;
                case OpCodeValue.jsr_w:
                    break;
                default:
                    throw new InvalidOperationException();
            }
        }
Пример #25
0
 public OpCode(OpCodeValue code) => this.Id = code;
Пример #26
0
 protected Constant(OpCodeValue code) : base(code)
 {
 }
Пример #27
0
 public ConditionalItem(ByteCodeGenerator generator, OpCodeValue opCode)
     : this(generator, opCode, null, null)
 {
     OpCode = opCode;
 }
Пример #28
0
 public GlobalWord(OpCodeValue code, int offset) : base(code, offset)
 {
 }
 public IntermediateWord(OpCodeValue code, int level, int offset) : base(code, offset) => this.Level = level;
Пример #30
0
 protected OffsetWord(OpCodeValue code, int offset)
     : base(code) => this.Offset = offset;
Пример #31
0
 public Exit(OpCodeValue code, bool isNormal = true)
     : base(code) => this.IsNormal = isNormal;
Пример #32
0
        public void Emit(OpCodeValue opcode, short s)
        {
            EmitOp(opcode);

            if (!alive) return;
            EmitShort(s);

            UpdateStack2(opcode, s);
        }
Пример #33
0
 public Type(OpCodeValue code, int type) : base(code) => this.TypeCode = type;
Пример #34
0
        public int EmitJump(OpCodeValue opcode)
        {
            if (fatcode)
            {
                if (opcode == OpCodeValue.@goto || opcode == OpCodeValue.jsr)
                {
                    Emit((OpCodeValue)(opcode + (byte)OpCodeValue.@goto_w - OpCodeValue.@goto), (int)0);
                }
                else
                {
                    Emit(OpCodes.Negate(opcode), (short)8);
                    Emit(OpCodeValue.@goto_w, (int)0);
                    alive = true;
                    pendingStackMap = true;
                }
                return length - 5;
            }

            Emit(opcode, (short)0);
            return length - 3;
        }
Пример #35
0
        public Chain Branch(OpCodeValue opcode)
        {
            Chain result = null;

            if (opcode == OpCodeValue.@goto)
            {
                result = pendingJumps;
                pendingJumps = null;
            }
            if (opcode != OpCodeValue.jsr && alive)
            {
                result = new Chain(EmitJump(opcode), result, state.Clone());

                fixedPc = fatcode;

                if (opcode == OpCodeValue.@goto) alive = false;
            }

            return result;
        }
Пример #36
0
        public void EmitWide(OpCodeValue opcode, short s1, short s2)
        {
            EnsureCapacity(6);

            if (s1 > 0xFF || s2 < -128 || s2 > 127)
            {
                EmitOp(OpCodeValue.wide);
                EmitOp(opcode);
                EmitShort(s1);
                EmitShort(s2);
            }
            else
            {
                EmitOp(opcode);
                EmitByte((byte)s1);
                EmitByte((byte)s2);
            }
        }
Пример #37
0
 private void UpdateStack1(OpCodeValue opcode, byte s)
 {
     switch (opcode)
     {
         case OpCodeValue.bipush:
             state.Push(PrimativeTypes.Int);
             break;
         case OpCodeValue.ldc:
             state.Push(TypeForConstant(Manager.ConstantPool[s - 1]));
             break;
         case OpCodeValue.lstore:
         case OpCodeValue.dstore:
             state.Pop(2);
             break;
         case OpCodeValue.istore:
         case OpCodeValue.fstore:
         case OpCodeValue.astore:
             state.Pop(1);
             break;
         default:
             throw new NotImplementedException();
     }
 }
Пример #38
0
        private void UpdateStack0(OpCodeValue opcode)
        {
            switch (opcode)
            {
                case OpCodeValue.aaload:
                    {
                        state.Pop(1);// index

                        var a = state.Peek();

                        state.Pop(1);

                        //sometimes 'null type' is treated as a one-dimensional array type
                        //see Gen.visitLiteral - we should handle this case OpCodeValue.OpCodeValue.accordingly
                        var stackType = a == PrimativeTypes.Void ? BuiltinTypes.Object : a;
                        state.Push(stackType);
                    }
                    break;
                case OpCodeValue.@goto:
                    MarkDead();
                    break;
                case OpCodeValue.nop:
                case OpCodeValue.ineg:
                case OpCodeValue.lneg:
                case OpCodeValue.fneg:
                case OpCodeValue.dneg:
                    break;
                case OpCodeValue.aconst_null:
                    state.Push(PrimativeTypes.Void);
                    break;
                case OpCodeValue.iconst_m1:
                case OpCodeValue.iconst_0:
                case OpCodeValue.iconst_1:
                case OpCodeValue.iconst_2:
                case OpCodeValue.iconst_3:
                case OpCodeValue.iconst_4:
                case OpCodeValue.iconst_5:
                case OpCodeValue.iload_0:
                case OpCodeValue.iload_1:
                case OpCodeValue.iload_2:
                case OpCodeValue.iload_3:
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.lconst_0:
                case OpCodeValue.lconst_1:
                case OpCodeValue.lload_0:
                case OpCodeValue.lload_1:
                case OpCodeValue.lload_2:
                case OpCodeValue.lload_3:
                    state.Push(PrimativeTypes.Long);
                    break;
                case OpCodeValue.fconst_0:
                case OpCodeValue.fconst_1:
                case OpCodeValue.fconst_2:
                case OpCodeValue.fload_0:
                case OpCodeValue.fload_1:
                case OpCodeValue.fload_2:
                case OpCodeValue.fload_3:
                    state.Push(PrimativeTypes.Float);
                    break;
                case OpCodeValue.dconst_0:
                case OpCodeValue.dconst_1:
                case OpCodeValue.dload_0:
                case OpCodeValue.dload_1:
                case OpCodeValue.dload_2:
                case OpCodeValue.dload_3:
                    state.Push(PrimativeTypes.Double);
                    break;
                case OpCodeValue.aload_0:
                    state.Push(GetVariable(0).Type);
                    break;
                case OpCodeValue.aload_1:
                    state.Push(GetVariable(1).Type);
                    break;
                case OpCodeValue.aload_2:
                    state.Push(GetVariable(2).Type);
                    break;
                case OpCodeValue.aload_3:
                    state.Push(GetVariable(3).Type);
                    break;
                case OpCodeValue.iaload:
                case OpCodeValue.baload:
                case OpCodeValue.caload:
                case OpCodeValue.saload:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.laload:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Long);
                    break;
                case OpCodeValue.faload:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Float);
                    break;
                case OpCodeValue.daload:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Double);
                    break;
                case OpCodeValue.istore_0:
                case OpCodeValue.istore_1:
                case OpCodeValue.istore_2:
                case OpCodeValue.istore_3:
                case OpCodeValue.fstore_0:
                case OpCodeValue.fstore_1:
                case OpCodeValue.fstore_2:
                case OpCodeValue.fstore_3:
                case OpCodeValue.astore_0:
                case OpCodeValue.astore_1:
                case OpCodeValue.astore_2:
                case OpCodeValue.astore_3:
                case OpCodeValue.pop:
                case OpCodeValue.lshr:
                case OpCodeValue.lshl:
                case OpCodeValue.lushr:
                    state.Pop(1);
                    break;
                case OpCodeValue.areturn:
                case OpCodeValue.ireturn:
                case OpCodeValue.freturn:
                    //TODO: Assert.check(state.nlocks == 0);
                    state.Pop(1);
                    MarkDead();
                    break;
                case OpCodeValue.athrow:
                    state.Pop(1);
                    MarkDead();
                    break;
                case OpCodeValue.lstore_0:
                case OpCodeValue.lstore_1:
                case OpCodeValue.lstore_2:
                case OpCodeValue.lstore_3:
                case OpCodeValue.dstore_0:
                case OpCodeValue.dstore_1:
                case OpCodeValue.dstore_2:
                case OpCodeValue.dstore_3:
                case OpCodeValue.pop2:
                    state.Pop(2);
                    break;
                case OpCodeValue.lreturn:
                case OpCodeValue.dreturn:
                    //TODO: Assert.check(state.nlocks == 0);
                    state.Pop(2);
                    MarkDead();
                    break;
                case OpCodeValue.dup:
                    state.Push(state.Peek());
                    break;
                case OpCodeValue.@return:
                    //TODO: Assert.check(state.nlocks == 0);
                    MarkDead();
                    break;
                case OpCodeValue.arraylength:
                    state.Pop(1);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.isub:
                case OpCodeValue.iadd:
                case OpCodeValue.imul:
                case OpCodeValue.idiv:
                case OpCodeValue.irem:
                case OpCodeValue.ishl:
                case OpCodeValue.ishr:
                case OpCodeValue.iushr:
                case OpCodeValue.iand:
                case OpCodeValue.ior:
                case OpCodeValue.ixor:
                    state.Pop(1);
                    break;
                case OpCodeValue.aastore:
                    state.Pop(3);
                    break;
                case OpCodeValue.land:
                case OpCodeValue.lor:
                case OpCodeValue.lxor:
                case OpCodeValue.lrem:
                case OpCodeValue.ldiv:
                case OpCodeValue.lmul:
                case OpCodeValue.lsub:
                case OpCodeValue.ladd:
                    state.Pop(2);
                    break;
                case OpCodeValue.lcmp:
                    state.Pop(4);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.l2i:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.i2l:
                    state.Pop(1);
                    state.Push(PrimativeTypes.Long);
                    break;
                case OpCodeValue.i2f:
                    state.Pop(1);
                    state.Push(PrimativeTypes.Float);
                    break;
                case OpCodeValue.i2d:
                    state.Pop(1);
                    state.Push(PrimativeTypes.Double);
                    break;
                case OpCodeValue.l2f:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Float);
                    break;
                case OpCodeValue.l2d:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Double);
                    break;
                case OpCodeValue.f2i:
                    state.Pop(1);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.f2l:
                    state.Pop(1);
                    state.Push(PrimativeTypes.Long);
                    break;
                case OpCodeValue.f2d:
                    state.Pop(1);
                    state.Push(PrimativeTypes.Double);
                    break;
                case OpCodeValue.d2i:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.d2l:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Long);
                    break;
                case OpCodeValue.d2f:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Float);
                    break;
                case OpCodeValue.tableswitch:
                case OpCodeValue.lookupswitch:
                    state.Pop(1);
                    // the caller is responsible for patching up the state
                    break;
                case OpCodeValue.dup_x1:
                    {
                        Type val1 = state.Pop1();
                        Type val2 = state.Pop1();
                        state.Push(val1);
                        state.Push(val2);
                        state.Push(val1);
                        break;
                    }
                case OpCodeValue.bastore:
                    state.Pop(3);
                    break;
                case OpCodeValue.i2b:
                case OpCodeValue.i2c:
                case OpCodeValue.i2s:
                    break;
                case OpCodeValue.fmul:
                case OpCodeValue.fadd:
                case OpCodeValue.fsub:
                case OpCodeValue.fdiv:
                case OpCodeValue.frem:
                    state.Pop(1);
                    break;
                case OpCodeValue.castore:
                case OpCodeValue.iastore:
                case OpCodeValue.fastore:
                case OpCodeValue.sastore:
                    state.Pop(3);
                    break;
                case OpCodeValue.lastore:
                case OpCodeValue.dastore:
                    state.Pop(4);
                    break;
                case OpCodeValue.dup2:
                    if (state.Peek() != null)
                    {
                        var value1 = state.Pop1();
                        var value2 = state.Pop1();

                        state.Push(value2);
                        state.Push(value1);
                        state.Push(value2);
                        state.Push(value1);
                    }
                    else
                    {
                        var value = state.Pop2();

                        state.Push(value);
                        state.Push(value);
                    }
                    break;
                case OpCodeValue.dup2_x1:
                    if (state.Peek() != null)
                    {
                        var value1 = state.Pop1();
                        var value2 = state.Pop1();
                        var value3 = state.Pop1();

                        state.Push(value2);
                        state.Push(value1);
                        state.Push(value3);
                        state.Push(value2);
                        state.Push(value1);
                    }
                    else
                    {
                        var value1 = state.Pop2();
                        var value2 = state.Pop1();

                        state.Push(value1);
                        state.Push(value2);
                        state.Push(value1);
                    }
                    break;
                case OpCodeValue.dup2_x2:
                    if (state.Peek() != null)
                    {
                        var value1 = state.Pop1();
                        var value2 = state.Pop1();

                        if (state.Peek() != null)
                        {
                            // form 1
                            var value3 = state.Pop1();
                            var value4 = state.Pop1();

                            state.Push(value2);
                            state.Push(value1);
                            state.Push(value4);
                            state.Push(value3);
                            state.Push(value2);
                            state.Push(value1);
                        }
                        else
                        {
                            // form 3
                            var value3 = state.Pop2();

                            state.Push(value2);
                            state.Push(value1);
                            state.Push(value3);
                            state.Push(value2);
                            state.Push(value1);
                        }
                    }
                    else
                    {
                        var value1 = state.Pop2();

                        if (state.Peek() != null)
                        {
                            // form 2
                            var value2 = state.Pop1();
                            var value3 = state.Pop1();

                            state.Push(value1);
                            state.Push(value3);
                            state.Push(value2);
                            state.Push(value1);
                        }
                        else
                        {
                            // form 4
                            var value2 = state.Pop2();

                            state.Push(value1);
                            state.Push(value2);
                            state.Push(value1);
                        }
                    }
                    break;
                case OpCodeValue.dup_x2:
                    {
                        var value1 = state.Pop1();

                        if (state.Peek() != null)
                        {
                            // form 1
                            var value2 = state.Pop1();
                            var value3 = state.Pop1();

                            state.Push(value1);
                            state.Push(value3);
                            state.Push(value2);
                            state.Push(value1);
                        }
                        else
                        {
                            // form 2
                            var value2 = state.Pop2();

                            state.Push(value1);
                            state.Push(value2);
                            state.Push(value1);
                        }
                    }
                    break;
                case OpCodeValue.fcmpl:
                case OpCodeValue.fcmpg:
                    state.Pop(2);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.dcmpl:
                case OpCodeValue.dcmpg:
                    state.Pop(4);
                    state.Push(PrimativeTypes.Int);
                    break;
                case OpCodeValue.swap:
                    {
                        Type value1 = state.Pop1();
                        Type value2 = state.Pop1();
                        state.Push(value1);
                        state.Push(value2);
                        break;
                    }
                case OpCodeValue.dadd:
                case OpCodeValue.dsub:
                case OpCodeValue.dmul:
                case OpCodeValue.ddiv:
                case OpCodeValue.drem:
                    state.Pop(2);
                    break;
                case OpCodeValue.ret:
                    MarkDead();
                    break;
                case OpCodeValue.wide:
                    // must be handled by the caller.
                    return;
                case OpCodeValue.monitorenter:
                case OpCodeValue.monitorexit:
                    state.Pop(1);
                    break;
                default:
                    throw new NotImplementedException();
            }
        }
Пример #39
0
 private void UpdateStack2(OpCodeValue opcode, short s)
 {
     switch (opcode)
     {
         case OpCodeValue.@new:
         case OpCodeValue.sipush:
             state.Push(PrimativeTypes.Int);
             break;
         case OpCodeValue.ifnull:
         case OpCodeValue.ifnonnull:
         case OpCodeValue.ifeq:
         case OpCodeValue.ifne:
         case OpCodeValue.iflt:
         case OpCodeValue.ifge:
         case OpCodeValue.ifgt:
         case OpCodeValue.ifle:
             state.Pop(1);
             break;
         case OpCodeValue.if_icmpeq:
         case OpCodeValue.if_icmpne:
         case OpCodeValue.if_icmplt:
         case OpCodeValue.if_icmpge:
         case OpCodeValue.if_icmpgt:
         case OpCodeValue.if_icmple:
         case OpCodeValue.if_acmpeq:
         case OpCodeValue.if_acmpne:
             state.Pop(2);
             break;
         case OpCodeValue.@goto:
             MarkDead();
             break;
         case OpCodeValue.ldc2_w:
             state.Push(TypeForConstant(Manager.ConstantPool[s - 1]));
             break;
         case OpCodeValue.instanceof:
             state.Pop(1);
             state.Push(PrimativeTypes.Int);
             break;
         case OpCodeValue.ldc_w:
             state.Push(TypeForConstant(Manager.ConstantPool[s - 1]));
             break;
         case OpCodeValue.jsr:
             break;
         case OpCodeValue.lstore:
         case OpCodeValue.dstore:
             state.Pop(2);
             break;
         case OpCodeValue.istore:
         case OpCodeValue.fstore:
         case OpCodeValue.astore:
             state.Pop(1);
             break;
         default:
             throw new NotImplementedException();
     }
 }
Пример #40
0
 public UnaryMath(OpCodeValue ocv, Expression operand) : base(operand) => this
     .Operation = ocv;
Пример #41
0
 private void UpdateStackWide(OpCodeValue opcode, int s)
 {
     switch (opcode)
     {
         case OpCodeValue.iload:
             state.Push(PrimativeTypes.Int);
             break;
         case OpCodeValue.lload:
             state.Push(PrimativeTypes.Long);
             break;
         case OpCodeValue.fload:
             state.Push(PrimativeTypes.Float);
             break;
         case OpCodeValue.dload:
             state.Push(PrimativeTypes.Double);
             break;
         case OpCodeValue.aload:
             state.Push(GetVariable(s).Type);
             break;
         case OpCodeValue.lstore:
         case OpCodeValue.dstore:
             state.Pop(2);
             break;
         case OpCodeValue.istore:
         case OpCodeValue.fstore:
         case OpCodeValue.astore:
             state.Pop(1);
             break;
         case OpCodeValue.ret:
             MarkDead();
             break;
         default:
             throw new NotImplementedException();
     }
 }
Пример #42
0
 internal OpCode(OpCodeValue bc, NormalizedOpCodeValues normalizedValue, OpCodeMode reg, OpCodeModeWide wide,
                 bool cannotThrow)
     : this(bc, normalizedValue, 0, reg, wide, cannotThrow)
 {
 }
Пример #43
0
        public void Emit(OpCodeValue opcode)
        {
            EnsureCapacity(1);

            EmitOp(opcode);

            if (!alive) return;
            UpdateStack0(opcode);

            PostOp();
        }
Пример #44
0
 public static bool InRange(this OpCode opcode, OpCodeValue start, OpCodeValue end)
 => ((ushort)start..(ushort)end).InRange(opcode.Value);
Пример #45
0
        public void Emit(OpCodeValue opcode, byte b)
        {
            EnsureCapacity(2);

            EmitOp(opcode);
            if (!alive) return;
            EmitByte(b);

            UpdateStack1(opcode, b);

            PostOp();
        }
Пример #46
0
 public CountBig(OpCodeValue code, int count) : base(code, count)
 {
 }
Пример #47
0
 public BinaryMath(OpCodeValue ocv, Expression left, Expression right) : base(left, right) => this
     .Operation = ocv;
Пример #48
0
 internal OpCode(OpCodeValue value, int flags)
 {
     this.value = value;
     this.flags = flags;
 }
        /// <summary>
        /// Execute an asynchronous redis operation.
        /// </summary>
        /// <typeparam name="T">The result type</typeparam>
        /// <param name="multiplexer">The connection multiplexer running the command.</param>
        /// <param name="message">The message to send to redis.</param>
        /// <param name="processor">The processor to handle the result.</param>
        /// <param name="state">The state to use for the task.</param>
        /// <param name="server">The server to call.</param>
        /// <param name="callOpCode">The <see cref="OpCodeValue"/> used in the original method call.</param>
        /// <returns>An asynchronous task.</returns>
        private static async Task <T> ExecuteAsyncImplInternal <T>(object multiplexer, object message, object processor, object state, object server, OpCodeValue callOpCode)
        {
            var genericType     = typeof(T);
            var multiplexerType = multiplexer.GetType();
            var asm             = multiplexerType.Assembly;
            var messageType     = asm.GetType("StackExchange.Redis.Message");
            var processorType   = asm.GetType("StackExchange.Redis.ResultProcessor`1").MakeGenericType(genericType);
            var stateType       = typeof(object);
            var serverType      = asm.GetType("StackExchange.Redis.ServerEndPoint");

            var originalMethod = Emit.DynamicMethodBuilder <Func <object, object, object, object, object, Task <T> > >
                                 .CreateMethodCallDelegate(
                multiplexerType,
                methodName : "ExecuteAsyncImpl",
                callOpCode,
                methodParameterTypes : new[] { messageType, processorType, stateType, serverType },
                methodGenericArguments : new[] { genericType });

            using (var scope = CreateScope(multiplexer, message))
            {
                try
                {
                    return(await originalMethod(multiplexer, message, processor, state, server).ConfigureAwait(false));
                }
                catch (Exception ex) when(scope?.Span.SetExceptionForFilter(ex) ?? false)
                {
                    // unreachable code
                    throw;
                }
            }
        }