Exemple #1
0
        private void Constructed()
        {
            InstructionCustomFunction icfunc = d_instruction as InstructionCustomFunction;
            InstructionCustomOperator icop   = d_instruction as InstructionCustomOperator;

            Cdn.Function f = null;

            if (icfunc != null)
            {
                f = icfunc.Function;
            }
            else if (icop != null)
            {
                f = icop.Operator.PrimaryFunction;
            }

            if (f == null)
            {
                return;
            }

            var args   = f.Arguments;
            int offset = 0;

            for (int i = 0; i < args.Length; ++i)
            {
                if (args[i].Unused)
                {
                    d_children.RemoveAt(i - offset);
                    ++offset;
                }
            }
        }
Exemple #2
0
		public InstructionCustomFunction (Cdn.Function function, Cdn.StackArgs argdim) : base (IntPtr.Zero)
		{
			if (GetType () != typeof (InstructionCustomFunction)) {
				throw new InvalidOperationException ("Can't override this constructor.");
			}
			Raw = cdn_instruction_custom_function_new(function == null ? IntPtr.Zero : function.Handle, argdim == null ? IntPtr.Zero : argdim.Handle);
		}
Exemple #3
0
        public Cdn.Function LookupFunction()
        {
            IntPtr raw_ret = cdn_compile_context_lookup_function(Handle);

            Cdn.Function ret = GLib.Object.GetObject(raw_ret) as Cdn.Function;
            return(ret);
        }
Exemple #4
0
        public Cdn.Function ForDimension(Cdn.StackArgs argdim)
        {
            IntPtr raw_ret = cdn_function_for_dimension(Handle, argdim == null ? IntPtr.Zero : argdim.Handle);

            Cdn.Function ret = GLib.Object.GetObject(raw_ret) as Cdn.Function;
            return(ret);
        }
Exemple #5
0
        public Cdn.Function GetFunction(out int idx, int numidx)
        {
            IntPtr raw_ret = cdn_operator_get_function(Handle, out idx, numidx);

            Cdn.Function ret = GLib.Object.GetObject(raw_ret) as Cdn.Function;
            return(ret);
        }
Exemple #6
0
        private int[] FunctionSparsity(Cdn.Function function, SparsityInfo[] children, Dictionary <Variable, SparsityInfo> mapping)
        {
            // Get the sparsity of the result of evaluating the custom function expression
            // under it's variables having the sparsity specified by children
            var vmap = new Dictionary <Variable, SparsityInfo>();

            int i = 0;

            foreach (var arg in function.Arguments)
            {
                var v = arg.Variable;

                if (!arg.Explicit)
                {
                    break;
                }

                if (i >= children.Length)
                {
                    vmap[v] = CalculateSparsity(arg.DefaultValue.Instructions);
                }
                else
                {
                    vmap[v] = children[i];
                }

                i++;
            }

            var ret = CalculateSparsity(function.Expression.Instructions, vmap);

            return(ret.Sparsity);
        }
Exemple #7
0
        public Cdn.Function LookupFunction(string name)
        {
            IntPtr native_name = GLib.Marshaller.StringToPtrGStrdup(name);
            IntPtr raw_ret     = cdn_compile_context_lookup_function(Handle, native_name);

            Cdn.Function ret = GLib.Object.GetObject(raw_ret) as Cdn.Function;
            GLib.Marshaller.Free(native_name);
            return(ret);
        }
Exemple #8
0
        private void Scan(Cdn.Object obj)
        {
            Cdn.Function f = obj as Cdn.Function;

            if (f != null)
            {
                foreach (var v in obj.Variables)
                {
                    if ((v.Flags & Cdn.VariableFlags.FunctionArgument) == 0)
                    {
                        d_functionHelperVariables.Add(v);
                    }
                }

                return;
            }
            else
            {
                d_variables.AddRange(obj.Variables);
            }

            Cdn.Node grp = obj as Cdn.Node;

            if (grp != null && grp.InitialState != null)
            {
                var states = AddEventStateContainer(grp);
                EventStateContainerAdd(states, grp, grp.InitialState);
            }

            ScanEvent(obj as Cdn.Event);

            foreach (Cdn.Variable prop in obj.Variables)
            {
                if (prop.Actions.Length != 0)
                {
                    d_actionedVariables[prop] = prop.Actions;
                }

                AddFlaggedVariable(prop);
            }

            if (grp == null)
            {
                return;
            }

            if (grp.HasSelfEdge)
            {
                Scan((Cdn.Object)grp.SelfEdge);
            }

            foreach (Cdn.Object child in grp.Children)
            {
                Scan(child);
            }
        }
Exemple #9
0
        public unsafe Cdn.Function GetDerivative(GLib.SList towards, int order, Cdn.ExpressionTreeIterDeriveFlags flags)
        {
            IntPtr error   = IntPtr.Zero;
            IntPtr raw_ret = cdn_function_get_derivative(Handle, towards == null ? IntPtr.Zero : towards.Handle, order, (int)flags, out error);

            Cdn.Function ret = GLib.Object.GetObject(raw_ret) as Cdn.Function;
            if (error != IntPtr.Zero)
            {
                throw new GLib.GException(error);
            }
            return(ret);
        }
Exemple #10
0
 public void PrependFunction(Cdn.Function function)
 {
     cdn_compile_context_prepend_function(Handle, function == null ? IntPtr.Zero : function.Handle);
 }
 void InvokeNative(Cdn.Function func)
 {
     native_cb(func == null ? IntPtr.Zero : func.Handle, __data);
 }
Exemple #12
0
        public static IEnumerable <string> InstructionCodes(Instruction inst, bool strict)
        {
            InstructionFunction       ifunc;
            InstructionCustomOperator icusop;
            InstructionCustomFunction icusf;
            InstructionVariable       ivar;
            InstructionNumber         inum;
            InstructionRand           irand;
            InstructionIndex          iindex;
            InstructionMatrix         imat;

            Programmer.Instructions.SparseOperator isop;

            if (InstructionIs(inst, out icusf))
            {
                // Generate byte code for this function by name

                yield return(InstructionIdentifier(HashMap("f_" + FunctionId(icusf.Function)), inst));
            }
            else if (InstructionIs(inst, out icusop))
            {
                if (icusop.Operator is OperatorDelayed && !strict)
                {
                    // These are actually part of the state table, so we use
                    // a placeholder code here
                    yield return(InstructionIdentifier(PlaceholderCodeLabel, inst));
                }
                else
                {
                    bool ns = strict || icusop.Operator is OperatorDelayed;

                    yield return(InstructionIdentifier(HashMap("co_" + icusop.Operator.Name), inst));

                    Cdn.Function f = icusop.Operator.PrimaryFunction;

                    if (f != null && f.Expression != null)
                    {
                        foreach (Instruction i in f.Expression.Instructions)
                        {
                            foreach (string id in InstructionCodes(i, ns))
                            {
                                yield return(id);
                            }
                        }
                    }
                    else
                    {
                        foreach (Cdn.Expression[] exprs in icusop.Operator.AllExpressions())
                        {
                            foreach (Cdn.Expression e in exprs)
                            {
                                foreach (Instruction i in e.Instructions)
                                {
                                    foreach (string id in InstructionCodes(i, ns))
                                    {
                                        yield return(id);
                                    }
                                }
                            }
                        }

                        foreach (Cdn.Expression[] exprs in icusop.Operator.AllIndices())
                        {
                            foreach (Cdn.Expression e in exprs)
                            {
                                foreach (Instruction i in e.Instructions)
                                {
                                    foreach (string id in InstructionCodes(i, ns))
                                    {
                                        yield return(id);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (InstructionIs(inst, out ifunc))
            {
                // Functions just store the id
                yield return(InstructionIdentifier((uint)ifunc.Id + 1, inst));
            }
            else if (InstructionIs(inst, out isop))
            {
                var argdim  = Array.ConvertAll(isop.Pop, (a) => String.Format("{0}-by-{1}", a.Rows, a.Columns));
                var argspar = Array.ConvertAll(isop.ArgSparsity, (a) => {
                    return(String.Format("[{0}]", String.Join(", ", Array.ConvertAll(a.Sparsity, (b) => b.ToString()))));
                });

                var argdims  = String.Join(", ", argdim);
                var argspars = String.Join(", ", argspar);

                yield return(InstructionIdentifier(String.Format("isop_({0})_({1})_({2})", isop.Original.Id + 1, argdims, argspars), inst));
            }
            else if (InstructionIs(inst, out iindex))
            {
                switch (iindex.IndexType)
                {
                case Cdn.InstructionIndexType.Offset:
                    yield return(InstructionIdentifier(String.Format("index_o{0}", iindex.Offset), inst));

                    break;

                case Cdn.InstructionIndexType.Range:
                    yield return(InstructionIdentifier(String.Format("index_r{0}_{1}_{2}", iindex.Range.Start, iindex.Range.Step, iindex.Range.End), inst));

                    break;

                case Cdn.InstructionIndexType.RangeBlock:
                {
                    IndexRange rows;
                    IndexRange columns;

                    iindex.GetRangeBlock(out rows, out columns);

                    yield return(InstructionIdentifier(String.Format("index_rb{0}_{1}_{2}_{3}_{4}_{5}",
                                                                     rows.Start, rows.Step, rows.End,
                                                                     columns.Start, columns.Step, columns.End), inst));
                }
                break;

                case Cdn.InstructionIndexType.Index:
                {
                    var indices = iindex.Indices;
                    var idx     = Array.ConvertAll <int, string>(indices, a => a.ToString());
                    yield return(InstructionIdentifier(String.Format("index_m[{0}]", String.Join(",", idx)), inst));
                }
                break;
                }
            }
            else if (InstructionIs(inst, out imat))
            {
                var smanip = imat.GetStackManipulation();
                yield return(InstructionIdentifier(String.Format("mat_{0}_{1}", smanip.Push.Dimension.Rows, smanip.Push.Dimension.Columns), inst));
            }
            else if (InstructionIs(inst, out ivar) && ivar.HasSlice)
            {
                Cdn.Dimension dim;
                int[]         slice = ivar.GetSlice(out dim);

                var n = String.Format("var_{0}[{1}]",
                                      ivar.Variable.FullName,
                                      String.Join(",", Array.ConvertAll <int, string>(slice, a => a.ToString())));

                yield return(InstructionIdentifier(HashMap(n), inst));
            }
            else if (strict)
            {
                RawC.Tree.Embedding.Instance     iinst;
                Programmer.Instructions.Function iifunc;
                Programmer.Instructions.Variable iivar;
                Programmer.Instructions.State    iistat;

                if (InstructionIs(inst, out ivar))
                {
                    var n = String.Format("var_{0}", ivar.Variable.FullName);
                    yield return(InstructionIdentifier(HashMap(n), inst));
                }
                else if (InstructionIs(inst, out inum))
                {
                    yield return(InstructionIdentifier(HashMap(String.Format("num_{0}", inum.Value)), inst));
                }
                else if (InstructionIs(inst, out irand))
                {
                    yield return(InstructionIdentifier(HashMap(String.Format("rand_{0}", irand.Handle)), inst));
                }
                else if (InstructionIs(inst, out iinst))
                {
                    yield return(InstructionIdentifier(HashMap(String.Format("emb_{0}", iinst.Handle)), inst));
                }
                else if (InstructionIs(inst, out iifunc))
                {
                    yield return(InstructionIdentifier(HashMap(String.Format("pfu_{0}", iifunc.FunctionCall.Name)), inst));
                }
                else if (InstructionIs(inst, out iivar))
                {
                    yield return(InstructionIdentifier(HashMap(String.Format("ivar_{0}", iivar.Name)), inst));
                }
                else if (InstructionIs(inst, out iistat))
                {
                    yield return(InstructionIdentifier(HashMap(String.Format("ist_{0}_{1}", iistat.Item.Table.Name, iistat.Item.DataIndex)), inst));
                }
                else
                {
                    throw new NotImplementedException(String.Format("Unhandled strict instruction code: {0}", inst.GetType()));
                }
            }
            else
            {
                // Placeholder for numbers, properties and rands
                yield return(InstructionIdentifier(PlaceholderCodeLabel, inst));
            }
        }
Exemple #13
0
        public static string FunctionId(Cdn.Function f, out Cdn.Function rettempl)
        {
            if (s_functionIds == null)
            {
                s_functionIds = new Dictionary <Function, FunctionIdMap>();
            }

            FunctionIdMap retid;

            if (s_functionIds.TryGetValue(f, out retid))
            {
                rettempl = retid.Template;
                return(retid.Id);
            }

            rettempl = null;

            if (!f.IsPure)
            {
                return("ff_" + f.FullIdForDisplay);
            }

            var templs = f.AppliedTemplates;

            if (templs.Length == 0)
            {
                return("ff_" + f.FullIdForDisplay);
            }

            var    templ = templs[templs.Length - 1] as Cdn.Function;
            string fid;

            if (!templ.Expression.Equal(f.Expression, true))
            {
                fid = "ff_" + f.FullIdForDisplay;
            }
            else if (String.IsNullOrEmpty(f.Expression.AsString))
            {
                fid = "ff_" + f.FullIdForDisplay;
            }
            else
            {
                rettempl = templ;

                // Generate id based on template name, function dimensions and
                // function expression
                var smanip = f.StackManipulation;
                fid = "ft_" + templ.FullIdForDisplay + "[" + smanip.Push.Dimension.Rows.ToString() + "," + smanip.Push.Dimension.Columns.ToString() + "](";

                for (int i = 0; i < smanip.Pop.Num; ++i)
                {
                    var p = smanip.GetPopn(i);
                    fid += String.Format("[{0},{1}]", p.Dimension.Rows, p.Dimension.Columns);
                }

                fid += ") " + f.Expression.AsString;
            }

            retid = new FunctionIdMap {
                Id = fid, Template = rettempl
            };
            s_functionIds[f] = retid;
            return(fid);
        }
Exemple #14
0
 public static string FunctionId(Cdn.Function f)
 {
     Cdn.Function templ;
     return(FunctionId(f, out templ));
 }
Exemple #15
0
 protected Function(Cdn.Function obj) : base(obj)
 {
     Renderer = new Renderers.Function(this);
 }