コード例 #1
0
        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;
        }
コード例 #2
0
        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;
        }