Example #1
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)
        {
            var side = args[0].Sample as IXilinxBlockMemSide;
            if (side == null)
                return false;

            var sample = StdLogicVector._0s(side.DataReadWidth);
            var code = IntrinsicFunctions.XILOpCode(
                DefaultInstructionSet.Instance.RdMem(side),
                TypeDescriptor.GetTypeOf(sample),
                args[1].Expr);
            stack.Push(code, sample);
            return true;
        }
Example #3
0
            public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
            {
                if (args[0].Sample == null)
                {
                    throw new InvalidOperationException("null sample");
                }

                ConstSignalSourceBase src = (ConstSignalSourceBase)callee.Invoke(args[0].Sample);

                src.ValueExpr = args[0].Expr;
                Expression srcex = LiteralReference.CreateConstant(src);

                stack.Push(srcex, src);
                return(true);
            }
Example #4
0
 public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args,
                              IDecompiler decomp, IFunctionBuilder builder)
 {
     Expression[] elements = null;
     if (args.Length == 1)
     {
         Expression valarr = args[0].Expr;
         valarr = decomp.ResolveVariableReference(decomp.CurrentILIndex, valarr);
         FunctionCall newarrCall = valarr as FunctionCall;
         if (newarrCall != null)
         {
             FunctionSpec      fspec = newarrCall.Callee as FunctionSpec;
             IntrinsicFunction ifun  = fspec == null ? null : fspec.IntrinsicRep;
             if (ifun != null && ifun.Action == IntrinsicFunction.EAction.NewArray)
             {
                 ArrayParams aparams = (ArrayParams)ifun.Parameter;
                 if (aparams.IsStatic)
                 {
                     newarrCall.IsInlined = true;
                     for (int i = 0; i < aparams.Elements.Length; i++)
                     {
                         aparams.Elements[i].IsInlined = true;
                     }
                     elements = aparams.Elements;
                 }
             }
         }
     }
     else
     {
         elements = args.Select(arg => arg.Expr).ToArray();
     }
     if (elements == null)
     {
         throw new NotImplementedException();
     }
     MakeStringArray(elements);
     decomp.Push(
         IntrinsicFunctions.StringConcat(elements),
         "");
     return(true);
 }
Example #5
0
        public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
        {
            if (args[0].Sample == null)
            {
                return(false);
            }

            ISized one, rsample;

            if (IsSigned)
            {
                Signed sample = (Signed)args[0].Sample;
                Signed sone   = Signed.FromLong(1, (int)sample.Size);
                one     = sone;
                rsample = sample + sone;
            }
            else
            {
                Unsigned sample = (Unsigned)args[0].Sample;
                Unsigned uone   = Unsigned.FromULong(1, (int)sample.Size);
                one     = uone;
                rsample = sample + uone;
            }
            LiteralReference oneLit = LiteralReference.CreateConstant(one);
            Expression       inc;

            if (IsDecrement)
            {
                inc = args[0].Expr - oneLit;
            }
            else
            {
                inc = args[0].Expr + oneLit;
            }
            inc.ResultType = TypeDescriptor.GetTypeOf(rsample);
            inc            = IntrinsicFunctions.Resize(inc, (int)one.Size, TypeDescriptor.GetTypeOf(one));
            stack.Push(new StackElement(inc, one, Analysis.Msil.EVariability.ExternVariable));
            return(true);
        }
Example #6
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)
 {
     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;
 }
 public override void RewriteRead(CodeDescriptor decompilee, FieldInfo field, object instance, IDecompiler stack, IFunctionBuilder builder)
 {
     var me = stack.QueryAttribute<AsyncMethodDecompiler>();
     var v = me._argFields[field.Name];
     if (!me._declared.Contains(v))
     {
         builder.DeclareLocal(v);
         me._declared.Add(v);
     }
     var lr = (LiteralReference)v;
     stack.Push(lr, v.Type.GetSampleInstance(ETypeCreationOptions.ForceCreation));
 }
        public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
        {
            var ctx = stack.QueryAttribute<AsyncMethodDecompiler>();
            if (ctx == null)
                throw new InvalidOperationException("Method must be decompiled using AsyncMethodDecompiler.");

            var style = ctx.ImplStyle;

            if (style == EAsyncImplStyle.Sequential)
            {
                ctx.ImplementAwait(decompilee, callee, args, stack, builder);
            }

            bool flag = style == EAsyncImplStyle.Sequential;
            var lr = LiteralReference.CreateConstant(flag);
            stack.Push(new StackElement(lr, flag, Msil.EVariability.Constant));
            return true;
        }
Example #10
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)
        {
            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;
        }
 public override void RewriteRead(CodeDescriptor decompilee, FieldInfo field, object instance, IDecompiler stack, IFunctionBuilder builder)
 {
     SignalBase sigInst = (SignalBase)field.GetValue(instance);
     SignalRef sigRef = new SignalRef(sigInst.Descriptor, SignalRef.EReferencedProperty.Instance);
     stack.Push(sigRef, sigInst);
 }
        public override bool Rewrite(
            CodeDescriptor decompilee,
            MethodBase callee,
            StackElement[] args,
            IDecompiler stack,
            IFunctionBuilder builder)
        {
            if (args.Length == 0)
                throw new InvalidOperationException("The attribute SignalProperty 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");

            ISignal sigSample = args[0].Sample as ISignal;

            SignalRef sigRef = null;
            LambdaLiteralVisitor llv = new LambdaLiteralVisitor()
            {
                OnVisitConstant = x =>
                {
                    sigRef = ((ISignal)x.ConstantValue).ToSignalRef(Prop);
                },
                OnVisitSignalRef = x =>
                {
                    sigRef = new SignalRef(x.Desc, Prop, x.Indices, x.IndexSample, x.IsStaticIndex);
                },
                OnVisitVariable = x =>
                {
                    SignalArgumentDescriptor desc = decompilee.GetSignalArguments().Where(y => y.Name.Equals(x.Name)).Single();
                    sigRef = new SignalRef(desc, Prop);
                },
                OnVisitFieldRef = x =>
                {
                    sigRef = ((ISignal)x.FieldDesc.ConstantValue).ToSignalRef(Prop);
                },
                OnVisitThisRef = x => { throw new InvalidOperationException(); },
                OnVisitArrayRef = x => { throw new InvalidOperationException(); }
            };
            refExpr.ReferencedObject.Accept(llv);

            ParameterInfo[] pis = callee.GetParameters();
            switch (Prop)
            {
                case SignalRef.EReferencedProperty.ChangedEvent:
                case SignalRef.EReferencedProperty.Cur:
                case SignalRef.EReferencedProperty.FallingEdge:
                case SignalRef.EReferencedProperty.Pre:
                case SignalRef.EReferencedProperty.RisingEdge:
                    //if (pis.Length != 0 || callee.IsStatic)
                    //    throw new InvalidOperationException("The attribute SignalProperty was applied to the wrong method.");

                    switch (Prop)
                    {
                        case SignalRef.EReferencedProperty.Cur:
                        case SignalRef.EReferencedProperty.Pre:
                            stack.Push(
                                sigRef,
                                sigSample != null ? sigSample.InitialValueObject : null);
                            break;

                        case SignalRef.EReferencedProperty.ChangedEvent:
                            stack.Push(
                                sigRef,
                                null);
                            break;

                        case SignalRef.EReferencedProperty.FallingEdge:
                        case SignalRef.EReferencedProperty.RisingEdge:
                            stack.Push(
                                sigRef,
                                false);
                            break;

                        default:
                            throw new NotImplementedException();
                    }
                    break;

                case SignalRef.EReferencedProperty.Next:
                    if (pis.Length != 1 || callee.IsStatic)
                        throw new InvalidOperationException("The attribute SignalProperty was applied to the wrong method.");

                    builder.Store(sigRef, args[1].Expr);
                    decompilee.AddDrivenSignal(sigRef.Desc);
                    break;

                default:
                    throw new NotImplementedException();
            }
            return true;
        }
 public override void RewriteRead(CodeDescriptor decompilee, FieldInfo field, object instance, IDecompiler stack, IFunctionBuilder builder)
 {
     object value = field.GetValue(instance);
     stack.Push(LiteralReference.CreateConstant(value),
         value);
 }
Example #15
0
        public override bool Rewrite(CodeDescriptor decompilee, MethodBase callee, StackElement[] args, IDecompiler stack, IFunctionBuilder builder)
        {
            if (args[0].Sample == null)
                return false;

            ISized one, rsample;
            if (IsSigned)
            {
                Signed sample = (Signed)args[0].Sample;
                Signed sone = Signed.FromLong(1, (int)sample.Size);
                one = sone;
                rsample = sample + sone;
            }
            else
            {
                Unsigned sample = (Unsigned)args[0].Sample;
                Unsigned uone = Unsigned.FromULong(1, (int)sample.Size);
                one = uone;
                rsample = sample + uone;
            }
            LiteralReference oneLit = LiteralReference.CreateConstant(one);
            Expression inc;
            if (IsDecrement)
                inc = args[0].Expr - oneLit;
            else
                inc = args[0].Expr + oneLit;
            inc.ResultType = TypeDescriptor.GetTypeOf(rsample);
            inc = IntrinsicFunctions.Resize(inc, (int)one.Size, TypeDescriptor.GetTypeOf(one));
            stack.Push(new StackElement(inc, one, Analysis.Msil.EVariability.ExternVariable));
            return true;
        }
Example #16
0
 public override bool Rewrite(Meta.CodeDescriptor decompilee, System.Reflection.MethodBase callee, StackElement[] args, IDecompiler stack, SysDOM.IFunctionBuilder builder)
 {
     stack.Push(LiteralReference.CreateConstant(false), false);
     System.Diagnostics.Debugger.Break();
     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)
        {
            IntrinsicFunction ifun = new IntrinsicFunction(Kind, Parameter)
            {
                MethodModel = callee
            };

            int skip = SkipFirstArg ? 1 : 0;
            Expression[] eargs = args.Skip(skip).Select(arg => arg.Expr).ToArray();
            Type returnType;
            if (!callee.IsFunctionOrCtor(out returnType))
            {
                FunctionSpec fspec = new FunctionSpec(typeof(void))
                {
                    CILRep = callee,
                    IntrinsicRep = ifun
                };
                builder.Call(fspec, eargs);
            }
            else
            {
                object[] outArgs;
                object rsample = null;
                if (callee is MethodInfo &&
                    !callee.HasCustomOrInjectedAttribute<IDoNotCallOnDecompilation>())
                {
                    stack.TryGetReturnValueSample((MethodInfo)callee, args, out outArgs, out rsample);
                }
                TypeDescriptor rtype;
                if (rsample != null)
                    rtype = TypeDescriptor.GetTypeOf(rsample);
                else
                    rtype = returnType;
                FunctionSpec fspec = new FunctionSpec(rtype)
                {
                    CILRep = callee,
                    IntrinsicRep = ifun
                };
                FunctionCall fcall = new FunctionCall()
                {
                    Callee = fspec,
                    Arguments = eargs,
                    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;
            if (callee.ReturnsSomething(out returnType))
            {
                dynamic awaiter = args[0].Sample;
                if ((object)awaiter != null)
                {
                    if (!awaiter.IsCompleted)
                        throw new InvalidOperationException("Task not completed - what are you awaiting for?");

                    object resultSample = awaiter.GetResult();
                    var resultType = resultSample.GetType();
                    var fspec = new FunctionSpec(resultType)
                    {
                        IntrinsicRep = IntrinsicFunctions.GetAsyncResult(awaiter)
                    };
                    var fcall = new FunctionCall()
                    {
                        Callee = fspec,
                        Arguments = new Expression[0],
                        ResultType = resultType
                    };
                    stack.Push(fcall, resultSample);
                }
            }
            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 void RewriteRead(CodeDescriptor decompilee, FieldInfo field, object instance, IDecompiler stack, IFunctionBuilder builder)
 {
     var me = stack.QueryAttribute<AsyncMethodDecompiler>();
     var awaiter = field.GetValue(me._fsmInstance);
     var lr = LiteralReference.CreateConstant(awaiter);
     stack.Push(new StackElement(lr, awaiter, Msil.EVariability.Constant));
 }
        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 void RewriteRead(CodeDescriptor decompilee, FieldInfo field, object instance, IDecompiler stack, IFunctionBuilder builder)
 {
     var me = stack.QueryAttribute<AsyncMethodDecompiler>();
     var elem = (StackElement)me._tasks[field.Name];
     stack.Push(elem);
 }
 public override bool Rewrite(Meta.CodeDescriptor decompilee, System.Reflection.MethodBase callee, StackElement[] args, IDecompiler stack, SysDOM.IFunctionBuilder builder)
 {
     stack.Push(LiteralReference.CreateConstant(false), false);
     System.Diagnostics.Debugger.Break();
     return true;
 }