예제 #1
0
            public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
            {
                var ufixSample = (UFix)args[0].Sample;
                var ufixType = args[0].Expr.ResultType;
                var slvuType = TypeDescriptor.GetTypeOf(ufixSample.SLVValue);

                object[] outArgs;
                object rsample;
                stack.TryGetReturnValueSample((MethodInfo)callee, args, out outArgs, out rsample);
                if (rsample == null)
                    throw new ArgumentException("Unable to infer result sample");

                var sfixSample = (SFix)rsample;
                var sfixType = TypeDescriptor.GetTypeOf(sfixSample);
                var slvsType = TypeDescriptor.GetTypeOf(sfixSample.SLVValue);
                var zlit = LiteralReference.CreateConstant(StdLogic._0);

                var eargs = args.Select(arg => arg.Expr).ToArray();
                var cast1 = IntrinsicFunctions.Cast(eargs, ufixType.CILType, slvuType, true);
                var cast2 = Expression.Concat(zlit, cast1);
                var cast3 = IntrinsicFunctions.Cast(new Expression[] { cast2 }, slvsType.CILType, sfixType, true);

                stack.Push(cast3, rsample);
                return true;
            }
 public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
 {
     object[] vargs = args.Select(arg => arg.Sample).ToArray();
     Expression[] eargs = args.Select(arg => arg.Expr).ToArray();
     object sample = null;
     try
     {
         sample = callee.Invoke(vargs);
     }
     catch (Exception)
     {
     }
     Expression result = new BinOp()
     {
         Operation = Kind
     };
     Array.Copy(eargs, result.Children, 2);
     if (sample != null)
         result.ResultType = TypeDescriptor.GetTypeOf(sample);
     else
     {
         Type rtype;
         callee.IsFunction(out rtype);
         result.ResultType = (TypeDescriptor)rtype;
     }
     stack.Push(result, sample);
     return true;
 }
예제 #3
0
        public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
        {
            var amd = stack.QueryAttribute<AsyncMethodDecompiler>();
            if (amd != null && amd.ImplStyle == EAsyncImplStyle.FSM)
            {
                return false;
            }

            object[] outArgs;
            object result;
            stack.TryGetReturnValueSample((MethodInfo)callee, args, out outArgs, out result);

            var fspec = new FunctionSpec(typeof(Task))
            {
                CILRep = callee
            };
            var fcall = new FunctionCall()
            {
                Callee = fspec,
                Arguments = args.Select(a => a.Expr).ToArray(),
                ResultType = TypeDescriptor.GetTypeOf(result)
            };

            stack.Push(fcall, result);

            return true;
        }
        public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
        {
            Type returnType;
            if (callee.ReturnsSomething(out returnType))
                throw new ArgumentException("The IgnoreOnDecompilation attribute may only be applied to methods returning a void result. Use StaticEvaluation instead.");

            if (_call)
            {
                callee.Invoke(args.Select(a => a.Sample).ToArray());
            }

            return true;
        }
        public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
        {
            Type returnType;
            if (!callee.ReturnsSomething(out returnType))
                throw new ArgumentException("The StaticEvaluation attribute may only be applied to methods returning some result. Use IgnoreOnDecompilation instead.");

            object result = null;
            try
            {
                result = callee.Invoke(args.Select(arg => arg.Sample).ToArray());
            }
            catch (TargetInvocationException)
            {
            }
            Expression resultExpr = ResultGen(result);
            stack.Push(new StackElement(resultExpr, result, EVariability.Constant));
            return true;
        }
        public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
        {
            object[] outArgs;
            object rsample = null;
            if (callee is MethodInfo &&
                !callee.HasCustomOrInjectedAttribute<IDoNotCallOnDecompilation>())
            {
                stack.TryGetReturnValueSample((MethodInfo)callee, args, out outArgs, out rsample);
            }
            Type returnType;
            callee.IsFunctionOrCtor(out returnType);
            TypeDescriptor rtype;
            if (rsample != null)
                rtype = TypeDescriptor.GetTypeOf(rsample);
            else
                rtype = returnType;

            IntrinsicFunction ifun;
            FunctionCall fcall;
            if (args[1].Variability == EVariability.Constant &&
                args[2].Variability == EVariability.Constant)
            {
                // constant arguments case
                int first = (int)TypeConversions.ConvertValue(args[1].Sample, typeof(int));
                int second = (int)TypeConversions.ConvertValue(args[2].Sample, typeof(int));
                var range = new Range(first, second, EDimDirection.Downto);
                ifun = new IntrinsicFunction(IntrinsicFunction.EAction.Slice, range)
                {
                    MethodModel = callee
                };
                var fspec = new FunctionSpec(rtype)
                {
                    CILRep = callee,
                    IntrinsicRep = ifun
                };
                fcall = new FunctionCall()
                {
                    Callee = fspec,
                    Arguments = new Expression[] { args[0].Expr },
                    ResultType = rtype,
                    SetResultTypeClass = EResultTypeClass.ObjectReference
                };
            }
            else
            {
                ifun = new IntrinsicFunction(IntrinsicFunction.EAction.Slice)
                {
                    MethodModel = callee
                };
                var fspec = new FunctionSpec(rtype)
                {
                    CILRep = callee,
                    IntrinsicRep = ifun
                };
                fcall = new FunctionCall()
                {
                    Callee = fspec,
                    Arguments = args.Select(arg => arg.Expr).ToArray(),
                    ResultType = rtype,
                    SetResultTypeClass = EResultTypeClass.ObjectReference
                };
            }
            stack.Push(fcall, rsample);
            return true;
        }
 public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
 {
     Type returnType;
     bool flag = callee.IsFunction(out returnType);
     Debug.Assert(flag);
     var atype = args[0].Expr.ResultType.CILType;
     if (atype.IsPointer)
         atype = atype.GetElementType();
     Debug.Assert(atype.Equals(SourceType));
     object[] outArgs;
     object rsample;
     stack.TryGetReturnValueSample((MethodInfo)callee, args, out outArgs, out rsample);
     TypeDescriptor rtype;
     if (rsample != null)
         rtype = TypeDescriptor.GetTypeOf(rsample);
     else
         rtype = returnType;
     Debug.Assert(rtype.CILType.Equals(DestType));
     Expression[] eargs = args.Select(arg => arg.Expr).ToArray();
     var fcall = IntrinsicFunctions.Cast(eargs, SourceType, rtype, Reinterpret);
     stack.Push(fcall, rsample);
     return true;
 }
        public override bool Rewrite(
            CodeDescriptor decompilee,
            MethodBase callee,
            StackElement[] args,
            IDecompiler stack,
            IFunctionBuilder builder)
        {
            if (args.Length < 2)
                throw new InvalidOperationException("The attribute SignalIndexer was applied to the wrong method");

            LiteralReference refExpr = args[0].Expr as LiteralReference;
            if (refExpr == null)
                throw new InvalidOperationException("Unable to resolve port/signal reference expression");

            DimSpec[] dimSamples = new DimSpec[args.Length - 1];
            Expression[] indices = args.Skip(1).Select(arg => arg.Expr).ToArray();
            bool isStatic = true;
            for (int i = 1; i < args.Length; i++)
            {
                var convidx = TypeConversions.ConvertValue(args[i].Sample, typeof(int));
                if (convidx != null)
                {
                    dimSamples[i - 1] = (int)convidx;
                }
                else if (args[i].Sample is Range)
                {
                    dimSamples[i - 1] = (Range)args[i].Sample;
                }
                else
                {
                    dimSamples = null;
                    break;
                }

                // EVariability.LocalVariable is not static as well, since variables
                // inside for loops will have that variability.
                if (args[i].Variability != EVariability.Constant)
                    isStatic = false;
            }
            IndexSpec indexSpec = null;
            indexSpec = new IndexSpec(dimSamples);

            SignalRef sigRef = null;
            LambdaLiteralVisitor llv = new LambdaLiteralVisitor()
            {
                OnVisitConstant = x =>
                {
                    sigRef = new SignalRef(
                        ((SignalBase)x.ConstantValue).Descriptor,
                        SignalRef.EReferencedProperty.Instance,
                        new Expression[][] { indices }, indexSpec, isStatic);
                },
                OnVisitFieldRef = x => { throw new InvalidOperationException(); },
                OnVisitSignalRef = x =>
                {
                    sigRef = new SignalRef(
                        x.Desc,
                        x.Prop,
                        x.Indices.Concat(new Expression[][] { indices }),
                        indexSpec.Project(x.IndexSample),
                        x.IsStaticIndex && isStatic);
                },
                OnVisitVariable = x => { throw new InvalidOperationException(); },
                OnVisitThisRef = x => { throw new InvalidOperationException(); }
            };
            refExpr.ReferencedObject.Accept(llv);

            object rsample = null;

            Type[] argTypes = args
                .Skip(1)
                .Select(a => a.Expr.ResultType.CILType)
                .ToArray();
            object[] argSamples = args
                .Select(a => a.Sample)
                .ToArray();
            MethodInfo indexerSampleMethod = callee.DeclaringType.GetMethod(
                "GetIndexerSample",
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                argTypes,
                null);
            if (indexerSampleMethod != null && argSamples.All(a => a != null))
            {
                rsample = indexerSampleMethod.Invoke(argSamples);
            }
            else
            {
                try
                {
                    rsample = callee.Invoke(args.Select(x => x.Sample).ToArray());
                }
                catch (TargetInvocationException)
                {
                }
            }
            stack.Push(sigRef, rsample);
            return true;
        }