Esempio n. 1
0
        protected virtual string TranslateDelayAssignment(Computation.Assignment node, Context context)
        {
            string eq = InstructionTranslator.QuickTranslate((Context)context.Base().Push(node.State, node.Equation));

            StringBuilder ret = new StringBuilder();

            var ds = (DelayedState)node.State;

            uint size = (uint)System.Math.Round(ds.Delay / Cdn.RawC.Options.Instance.DelayTimeStep);

            DataTable.DataItem counter = context.Program.DelayedCounters[new DelayedState.Size(size)];
            DataTable          table   = context.Program.DelayHistoryTable(ds);

            ret.AppendFormat("{0}[{1}] = {2}[{3}[{4}]];",
                             context.This(node.Item.Table),
                             node.Item.AliasOrIndex,
                             context.This(table),
                             context.This(context.Program.DelayedCounters),
                             counter.DataIndex);

            ret.AppendLine();
            ret.AppendFormat("{0}[{1}[{2}]] = {3};",
                             context.This(table),
                             context.This(context.Program.DelayedCounters),
                             counter.DataIndex,
                             eq);

            return(ret.ToString());
        }
Esempio n. 2
0
        protected virtual string Translate(Computation.Loop node, Context context)
        {
            StringBuilder ret = new StringBuilder();

            Context ctx = context.Clone(context.Program, context.Options, node.Expression, node.Mapping);

            ret.AppendFormat("for ({0} = 0; i < {1}; ++i)",
                             context.DeclareValueVariable("int", "i"),
                             node.Items.Count);

            ret.AppendLine();
            ret.AppendLine("{");

            if (Cdn.RawC.Options.Instance.Verbose)
            {
                var dt = (node.IndexTable[0].Object as Computation.Loop.Index).DataItem;

                ret.AppendFormat("\t{0} {1}[{2}] = {3}({4}",
                                 context.BeginComment,
                                 context.This(context.Program.StateTable),
                                 dt.AliasOrIndex.Replace(context.BeginComment, "//").Replace(context.EndComment, "//"),
                                 context.ThisCall(node.Function.Name),
                                 context.This(context.Program.StateTable));

                var eq = node.Items[0].Equation;

                foreach (Tree.Embedding.Argument arg in node.Function.OrderedArguments)
                {
                    Tree.Node          subnode = eq.FromPath(arg.Path);
                    DataTable.DataItem it      = context.Program.StateTable[subnode];

                    ret.AppendFormat(", {0}[{1}]",
                                     context.This(context.Program.StateTable),
                                     it.AliasOrIndex.Replace(context.BeginComment, "//").Replace(context.EndComment, "//"));
                }

                ret.Append("); ");
                ret.Append(context.EndComment);
                ret.AppendLine();
            }

            ret.AppendLine(Context.Reindent(TranslateAssignment(String.Format("{0}[i][0]",
                                                                              context.This(node.IndexTable)),
                                                                ctx),
                                            "\t"));
            ret.Append("}");

            return(ret.ToString());
        }
Esempio n. 3
0
        public string AddedIndex(DataTable.DataItem item, int added)
        {
            if (added == 0)
            {
                return(item.AliasOrIndex);
            }

            if (d_options.SymbolicNames)
            {
                return(String.Format("{0} + {1}", item.AliasOrIndex, added));
            }
            else
            {
                return((item.DataIndex + added).ToString());
            }
        }
Esempio n. 4
0
        /*
         * Delay operator instructions are translated to a simple lookup in
         * the statetable where they delayed expressions are stored.
         */
        protected virtual string TranslateDelayed(InstructionCustomOperator instruction, Context context)
        {
            OperatorDelayed delayed = (OperatorDelayed)instruction.Operator;
            double          delay;

            if (!Knowledge.Instance.LookupDelay(instruction, out delay))
            {
                throw new NotSupportedException("Unable to determine delay of delayed operator");
            }

            DataTable.DataItem item = context.Program.StateTable[new DelayedState.Key(delayed, delay)];

            return(String.Format("{0}[{1}]",
                                 context.This(context.Program.StateTable),
                                 item.AliasOrIndex));
        }
Esempio n. 5
0
        public void Add(DataTable.DataItem target, Tree.Node equation)
        {
            d_items.Add(new Item(target, equation));

            // Add row to index table
            d_indextable.Add(new Index((ulong)target.DataIndex, target));
            d_indextable.IntegerTypeSize = (ulong)target.DataIndex;

            foreach (Tree.Embedding.Argument arg in d_function.OrderedArguments)
            {
                Tree.Node subnode = equation.FromPath(arg.Path);

                DataTable.DataItem it = d_program.StateTable[subnode];

                d_indextable.Add(new Index((ulong)it.DataIndex, it)).Type = (it.Type | DataTable.DataItem.Flags.Index);
                d_indextable.IntegerTypeSize = (ulong)it.DataIndex;
            }
        }
Esempio n. 6
0
        protected virtual string Translate(InstructionVariable instruction, Context context)
        {
            Cdn.Variable prop = instruction.Variable;

            if (!context.Program.StateTable.Contains(prop))
            {
                throw new NotImplementedException(String.Format("The variable `{0}' is not implemented", prop.FullName));
            }

            DataTable.DataItem item = context.Program.StateTable[prop];

            if (instruction.HasSlice)
            {
                Cdn.Dimension dim;
                int[]         slice = instruction.GetSlice(out dim);

                if (context.SupportsFirstClassArrays)
                {
                    return(String.Format("{0}[{1}][{2}]",
                                         context.This(context.Program.StateTable),
                                         item.AliasOrIndex,
                                         slice[0]));
                }
                else
                {
                    return(String.Format("{0}[{1}]",
                                         context.This(context.Program.StateTable),
                                         context.AddedIndex(item, slice[0])));
                }
            }
            else
            {
                return(String.Format("{0}[{1}]",
                                     context.This(context.Program.StateTable),
                                     item.AliasOrIndex));
            }
        }
Esempio n. 7
0
 public State(DataTable.DataItem item)
 {
     d_item = item;
 }
Esempio n. 8
0
        protected virtual string Translate(Computation.IncrementDelayedCounters node, Context context)
        {
            StringBuilder ret = new StringBuilder();

            ret.AppendFormat("for ({0} = 0; i < {1}; ++i)",
                             context.DeclareValueVariable("int", "i"),
                             node.Counters.Count);

            ret.AppendLine();
            ret.AppendLine("{");
            ret.AppendFormat("\tif ({0}[i] == {1}[i] - 1)", context.This(node.Counters), context.This(node.CountersSize));
            ret.AppendLine();
            ret.AppendLine("\t{");
            ret.AppendFormat("\t\t{0}[i] = 0;", context.This(node.Counters));
            ret.AppendLine();
            ret.AppendLine("\t}");
            ret.AppendLine("\telse");
            ret.AppendLine("\t{");
            ret.AppendFormat("\t\t++{0}[i];", context.This(node.Counters));
            ret.AppendLine();
            ret.AppendLine("\t}");
            ret.Append("}");

            Dictionary <DataTable, bool> seen = new Dictionary <DataTable, bool>();
            bool first = true;

            // Update counter loop indices
            foreach (Computation.Loop loop in context.Program.Loops)
            {
                DataTable table = loop.IndexTable;

                if (seen.ContainsKey(table))
                {
                    continue;
                }

                for (int i = 0; i < table.Count; ++i)
                {
                    DataTable.DataItem item = table[i];

                    if (item.HasType(DataTable.DataItem.Flags.Delayed))
                    {
                        int r = i % table.Columns;
                        int c = i / table.Columns;
                        Computation.Loop.Index sidx = (Computation.Loop.Index)item.Key;

                        DataTable.DataItem state   = context.Program.StateTable[(int)sidx.Value];
                        DelayedState.Key   delayed = (DelayedState.Key)state.Key;
                        DataTable.DataItem idx     = context.Program.DelayedCounters[delayed.Size];

                        if (first)
                        {
                            ret.AppendLine();
                            first = false;
                        }

                        ret.AppendLine();
                        ret.AppendFormat("{0}[{1}][{2}] = {3} + {4}[{5}];",
                                         context.This(table), r, c,
                                         sidx.Value,
                                         context.This(context.Program.DelayedCounters), idx.DataIndex);
                    }
                }

                seen[table] = true;
            }

            return(ret.ToString());
        }
Esempio n. 9
0
        protected virtual string TranslateV(InstructionVariable instruction, Context context)
        {
            Cdn.Variable prop = instruction.Variable;

            if (!context.Program.StateTable.Contains(prop))
            {
                throw new NotImplementedException(String.Format("The variable `{0}' is not implemented", prop.FullName));
            }

            DataTable.DataItem item = context.Program.StateTable[prop];
            string             ret  = null;

            if (!context.SupportsFirstClassArrays)
            {
                ret = context.PeekRet();
            }

            int size   = prop.Dimension.Size();
            int offset = 0;

            if (instruction.HasSlice)
            {
                Cdn.Dimension dim;
                var           slice = instruction.GetSlice(out dim);

                // This is a multidim slice for sure, check if it's just linear
                if (!Context.IndicesAreContinuous(slice))
                {
                    if (context.SupportsFirstClassArrays)
                    {
                        var v  = context.This(context.Program.StateTable);
                        var vi = String.Format("{0}[{1}]", v, item.AliasOrIndex);

                        return(context.ArraySliceIndices(vi, slice));
                    }
                    else
                    {
                        StringBuilder sret = new StringBuilder("(");

                        // Make single element assignments
                        for (int i = 0; i < slice.Length; ++i)
                        {
                            if (i != 0)
                            {
                                sret.Append(", ");
                            }

                            sret.AppendFormat("({0})[{1}] = {2}[{3}]",
                                              ret,
                                              i,
                                              context.This(context.Program.StateTable),
                                              context.AddedIndex(item, slice[i]));
                        }

                        sret.AppendFormat(", {0})", ret);
                        return(sret.ToString());
                    }
                }
                else
                {
                    offset = slice[0];
                    size   = slice.Length;
                }
            }

            // Here we should return from "offset" to "offset + size"
            if (ret != null)
            {
                // We need to write the results in "ret". Make a memcpy
                return(context.MemCpy(ret,
                                      "0",
                                      context.This(context.Program.StateTable),
                                      context.AddedIndex(item, offset),
                                      "ValueType",
                                      size));
            }
            else if (context.SupportsPointers)
            {
                // Simply return the correct offset of the continous slice
                return(String.Format("({0} + {1})",
                                     context.This(context.Program.StateTable),
                                     context.AddedIndex(item, offset)));
            }
            else if (context.SupportsFirstClassArrays)
            {
                // Simply return the slice
                var v = context.This(context.Program.StateTable);

                if (offset == 0 && size == item.Dimension.Size())
                {
                    return(String.Format("{0}[{1}]", v, item.AliasOrIndex));
                }
                else
                {
                    return(context.ArraySlice(String.Format("{0}[{1}]", v, item.AliasOrIndex),
                                              offset.ToString(),
                                              (offset + size).ToString()));
                }
            }
            else
            {
                throw new Exception("Unsupported multidim variable case!");
            }
        }
Esempio n. 10
0
 public Assignment(State state, DataTable.DataItem item, Tree.Node equation)
 {
     d_state    = state;
     d_item     = item;
     d_equation = equation;
 }
Esempio n. 11
0
 public Index(ulong val, DataTable.DataItem item)
 {
     Value    = val;
     DataItem = item;
 }
Esempio n. 12
0
 public Item(DataTable.DataItem target, Tree.Node equation)
 {
     Target   = target;
     Equation = equation;
 }