Exemplo n.º 1
0
 private static FunctionSpec MakeFun(IntrinsicFunction ifun, TypeDescriptor returnType)
 {
     Contract.Requires <ArgumentNullException>(ifun != null);
     Contract.Requires <ArgumentNullException>(returnType != null);
     return(new FunctionSpec(returnType)
     {
         IntrinsicRep = ifun
     });
 }
        public Expression TransformFunction(FunctionCall expr)
        {
            if (expr.Callee is IntrinsicFunction)
            {
                IntrinsicFunction ifun = (IntrinsicFunction)expr.Callee;
                switch (ifun.Action)
                {
                case IntrinsicFunction.EAction.Sin:
                {
                    Expression   x = expr.Arguments[0];
                    FunctionCall f = IntrinsicFunctions.Cos(x);
                    return(f * der(x));
                }

                case IntrinsicFunction.EAction.Cos:
                {
                    Expression   x = expr.Arguments[0];
                    FunctionCall f = IntrinsicFunctions.Sin(x);
                    return(-f *der(x));
                }

                case IntrinsicFunction.EAction.Sqrt:
                {
                    return(der(expr.Arguments[0]) / expr);
                }

                case IntrinsicFunction.EAction.Sign:
                    return(SpecialConstant.ScalarZero);

                case IntrinsicFunction.EAction.GetArrayElement:
                    throw new ArgumentException("Expression is not differentiatable");

                default:
                    throw new NotImplementedException();
                }
            }
            else
            {
                throw new NotImplementedException("Only intrinsic functions can be derived");
            }
        }
Exemplo n.º 3
0
 private static FunctionSpec MakeFun(IntrinsicFunction ifun, TypeDescriptor returnType)
 {
     Contract.Requires<ArgumentNullException>(ifun != null);
     Contract.Requires<ArgumentNullException>(returnType != null);
     return new FunctionSpec(returnType)
     {
         IntrinsicRep = ifun
     };
 }
        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)
        {
            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;
        }
 /// <summary>
 /// Creates the attribute.
 /// </summary>
 /// <param name="kind">SysDOM-intrinsic function code</param>
 /// <param name="skipFirstArg">whether to drop first argument (i.e. "this" instance for non-static methods)</param>
 public MapToIntrinsicFunction(IntrinsicFunction.EAction kind, bool skipFirstArg)
 {
     Kind = kind;
     SkipFirstArg = skipFirstArg;
 }
 /// <summary>
 /// Creates the attribute.
 /// </summary>
 /// <param name="kind">SysDOM-intrinsic function code</param>
 /// <param name="param">optional function parameter</param>
 public MapToIntrinsicFunction(IntrinsicFunction.EAction kind, object param)
 {
     Kind = kind;
     Parameter = param;
     if (kind == IntrinsicFunction.EAction.Slice)
         throw new ArgumentException("Use MapToSlice instead");
 }
 /// <summary>
 /// Creates the attribute.
 /// </summary>
 /// <param name="kind">SysDOM-intrinsic function code</param>
 public MapToIntrinsicFunction(IntrinsicFunction.EAction kind)
 {
     Kind = kind;
     if (kind == IntrinsicFunction.EAction.Slice)
         throw new ArgumentException("Use MapToSlice instead");
 }