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()); }
protected virtual string Translate(Computation.CallAPI node, Context context) { StringBuilder ret = new StringBuilder(); bool isfirst = true; ret.AppendFormat("{0}(", context.APIName(node)); if (context.This("") == "") { ret.Append("data"); isfirst = false; } for (int i = 0; i < node.Arguments.Length; ++i) { if (!isfirst) { ret.Append(", "); } else { isfirst = false; } var arg = node.Arguments[i]; string eq = InstructionTranslator.QuickTranslate((Context)context.Base().Push(arg)); ret.Append(eq); } ret.AppendFormat(");"); return(ret.ToString()); }
private string TranslateAssignment(string index, Context ctx) { string eq; string ret; var slice = ctx.Node.Slice; ctx.SaveTemporaryStack(); // Compute the equation if (ctx.Node.Dimension.IsOne) { eq = InstructionTranslator.QuickTranslate(ctx); if (slice != null && ctx.SupportsFirstClassArrays) { ret = String.Format("{0}[{1}][{2}] = {3};", ctx.This(ctx.Program.StateTable), index, slice[0], eq); } else if (slice != null && slice[0] != 0) { ret = String.Format("{0}[{1} + {2}] = {3};", ctx.This(ctx.Program.StateTable), index, slice[0], eq); } else { ret = String.Format("{0}[{1}] = {2};", ctx.This(ctx.Program.StateTable), index, eq); } } else { string retval = null; bool hastmp = false; bool contidx = (slice != null && Context.IndicesAreContinuous(slice)); if (ctx.SupportsPointers) { if (slice != null) { // If a slice is continous, than just have the offset // into the state table and write there directly if (contidx) { retval = String.Format("{0} + {1} + {2}", ctx.This(ctx.Program.StateTable), index, slice[0]); } else { // No magic, here we allocate a temporary to write // to which will afterwards be copied to the slice retval = ctx.AcquireTemporary(ctx.Node); hastmp = true; } } else { // No slice, just write directly into the state table retval = String.Format("{0} + {1}", ctx.This(ctx.Program.StateTable), index); } } else if (!ctx.SupportsFirstClassArrays || (slice != null && !contidx)) { // For now, just make a temporary and copy back afterwards retval = ctx.AcquireTemporary(ctx.Node); hastmp = true; } ctx.PushRet(retval); ret = InstructionTranslator.QuickTranslate(ctx); ctx.PopRet(); if (hastmp) { if (slice != null && !contidx) { StringBuilder sret = new StringBuilder("("); if (ctx.SupportsFirstClassArrays) { sret.AppendFormat("{0} = ", retval); } sret.Append(ret); // Copy temporary to slice for (int i = 0; i < slice.Length; ++i) { string writeloc; if (ctx.SupportsFirstClassArrays) { writeloc = String.Format("[{0}][{1}]", index, slice[i]); } else { writeloc = String.Format("[{0} + {1}]", index, slice[i]); } sret.AppendFormat(", {0}{1} = {2}[{3}]", ctx.This(ctx.Program.StateTable), writeloc, retval, i); } sret.Append(");"); ret = sret.ToString(); } else { StringBuilder sret = new StringBuilder("("); sret.Append(ret); sret.Append(", "); if (slice == null) { // Just copy the whole thing sret.Append(ctx.MemCpy(ctx.This(ctx.Program.StateTable), index, retval, "0", "ValueType", ctx.Node.Dimension.Size())); } else { // We have a slice, but it has continous indices sret.Append(ctx.MemCpy(ctx.This(ctx.Program.StateTable), index, retval, slice[0].ToString(), "ValueType", slice[slice.Length - 1] - slice[0])); } sret.Append(");"); ret = sret.ToString(); } } else if (retval == null) { if (slice != null) { // Memcpy to the slice ret = ctx.MemCpy(String.Format("{0}[{1}]", ctx.This(ctx.Program.StateTable), index), slice[0].ToString(), ret, "0", "ValueType", slice.Length) + ";"; } else { // Simply assign ret = String.Format("{0}[{1}] = {2};", ctx.This(ctx.Program.StateTable), index, ret); } } else { ret = String.Format("{0};", ret); } } var reteq = WriteWithTemporaryStorage(ctx, ret.ToString()); ctx.RestoreTemporaryStack(); return(reteq); }
protected virtual string Translate(Computation.InitializeDelayHistory node, Context context) { StringBuilder ret = new StringBuilder(); string eq = InstructionTranslator.QuickTranslate((Context)context.Base().Push(node.State, node.Equation)); if (node.State.Operator.InitialValue == null) { ret.AppendLine(Translate(new Computation.ZeroMemory(node.History), context)); ret.AppendFormat("{0}[{1}] = 0.0;", context.This(context.Program.StateTable), context.Program.StateTable[node.State].AliasOrIndex); return(ret.ToString()); } else if (!node.OnTime) { ret.AppendLine(context.BeginBlock); ret.AppendFormat("\t{0} = {1};", context.DeclareValueVariable("ValueType", "_tmp"), eq); ret.AppendLine(); ret.AppendLine(); ret.AppendFormat("\tfor ({0} = 0; i < {0}; ++i)", context.DeclareValueVariable("int", "i"), node.History.Count); ret.AppendLine(); ret.AppendLine("\t{"); ret.AppendFormat("\t\t{0}[i] = _tmp;", context.This(node.History)); ret.AppendLine(); ret.AppendLine("\t}"); ret.AppendLine(); ret.AppendFormat("\t{0}[{1}] = _tmp;", context.This(context.Program.StateTable), context.Program.StateTable[node.State].AliasOrIndex); ret.AppendLine(); ret.Append("}"); return(ret.ToString()); } var n = new Tree.Node(null, new InstructionVariable(Knowledge.Instance.Time.Object as Variable)); var ctx = new Context(context.Program, context.Options, n, null); string ss = InstructionTranslator.QuickTranslate(ctx); ret.AppendFormat("{0} = t - {1};", ss, context.Program.Options.DelayTimeStep * (node.History.Count + 1)); ret.AppendLine(); ret.AppendLine(); ret.AppendFormat("for ({0} = 0; i < {1}; ++i)", context.DeclareValueVariable("int", "i"), node.History.Count + 1); ret.AppendLine(); ret.AppendLine("{"); ret.AppendFormat("\t{0} += {1};", ss, context.Program.Options.DelayTimeStep); ret.AppendLine(); ret.AppendLine(); foreach (Computation.INode dep in node.Dependencies) { ret.AppendLine(Context.Reindent(Translate(dep, context), "\t")); } ret.AppendLine(); ret.AppendFormat("\tif (i != 0)"); ret.AppendLine(); ret.AppendLine("\t{"); ret.AppendFormat("\t\t{0}[i] = {1};", context.This(node.History), eq); ret.AppendLine(); ret.AppendLine("\t}"); ret.AppendLine("\telse"); ret.AppendLine("\t{"); ret.AppendFormat("\t\t{0}[{1}] = {2};", context.This(context.Program.StateTable), context.Program.StateTable[node.State].AliasOrIndex, eq); ret.AppendLine(); ret.AppendLine("\t}"); ret.Append("}"); return(ret.ToString()); }