private void CreateReducedVariableInformation(IVariableDeclaration ivd, HoistingInfo info) { int arrayDepth = info.maxDepthWhereDimensionCouldMatter.Length; var varInfo = VariableInformation.GetVariableInformation(context, ivd); var reducedVarInfo = VariableInformation.GetVariableInformation(context, info.newVariable); for (int bracket = 0; bracket < varInfo.sizes.Count; bracket++) { List <IExpression> newSizes = new List <IExpression>(); List <IVariableDeclaration> newIndexVars = new List <IVariableDeclaration>(); for (int dim = 0; dim < varInfo.sizes[bracket].Length; dim++) { if (info.maxDepthWhereDimensionCouldMatter[bracket][dim] == arrayDepth) { newSizes.Add(varInfo.sizes[bracket][dim]); if (varInfo.indexVars.Count > bracket && varInfo.indexVars[bracket].Length > dim) { newIndexVars.Add(varInfo.indexVars[bracket][dim]); } } } if (newSizes.Count > 0) { reducedVarInfo.sizes.Add(newSizes.ToArray()); reducedVarInfo.indexVars.Add(newIndexVars.ToArray()); } } }
protected override IExpression ConvertAssign(IAssignExpression iae) { // for(i) array[i] = rhs // becomes: // temp = CopyStorage(array) // for(i) temp[i] = rhs // for(i) array[i] = SetTo(temp[i]) IVariableDeclaration ivd = Recognizer.GetVariableDeclaration(iae.Target); string name = VariableInformation.GenerateName(context, ivd.Name + "_new"); IVariableDeclaration clone = Builder.VarDecl(name, ivd.VariableType); var cloneDeclExpr = Builder.VarDeclExpr(clone); var newTarget = Builder.ReplaceExpression(iae.Target, Builder.VarRefExpr(ivd), Builder.VarRefExpr(clone)); IExpression copyStorage = Builder.StaticGenericMethod( new Func <PlaceHolder, PlaceHolder>(ArrayHelper.CopyStorage), new[] { ivd.VariableType }, Builder.VarRefExpr(ivd)); var cloneDeclStmt = Builder.AssignStmt(cloneDeclExpr, copyStorage); context.OutputAttributes.Set(cloneDeclStmt, new Initializer()); cloneDecls.Add(cloneDeclStmt); IExpression setTo = Builder.StaticGenericMethod( new Func <PlaceHolder, PlaceHolder, PlaceHolder>(ArrayHelper.SetTo), new[] { iae.GetExpressionType() }, iae.Target, newTarget); IStatement setToStmt = Builder.AssignStmt(iae.Target, setTo); setToStmt = Containers.WrapWithContainers(setToStmt, containers); cloneUpdates.Add(setToStmt); return(Builder.AssignExpr(newTarget, iae.Expression)); }
protected override IExpression ConvertVariableDeclExpr(IVariableDeclarationExpression ivde) { context.InputAttributes.Remove <Containers>(ivde.Variable); context.InputAttributes.Set(ivde.Variable, new Containers(context)); IVariableDeclaration ivd = ivde.Variable; if (!CodeRecognizer.IsStochastic(context, ivd)) { ProcessConstant(ivd); if (!context.InputAttributes.Has <DescriptionAttribute>(ivd)) { context.OutputAttributes.Set(ivd, new DescriptionAttribute("The constant '" + ivd.Name + "'")); } return(ivde); } VariableInformation vi = VariableInformation.GetVariableInformation(context, ivd); // Ensure the marginal prototype is set. MarginalPrototype mpa = Context.InputAttributes.Get <MarginalPrototype>(ivd); try { vi.SetMarginalPrototypeFromAttribute(mpa); } catch (ArgumentException ex) { Error(ex.Message); } return(ivde); }
internal async Task <VariableInformation> CreateMIDebuggerVariable(ThreadContext ctx, AD7Engine engine, AD7Thread thread) { VariableInformation vi = new VariableInformation(Name, ctx, engine, thread, IsParameter); await vi.Eval(); return(vi); }
public static Func <IExtractContext, string[]> Apply( VariableInformation localVariable, DecodeContext decodeContext, bool isReference = false) { var local = decodeContext.Method.LocalVariables[localVariable.Index]; return(Apply(local, local.TargetType, decodeContext, isReference)); }
public static ExpressionEmitter Prepare( VariableInformation localVariable, DecodeContext decodeContext, bool isReference = false) { var local = decodeContext.Method.LocalVariables[localVariable.Index]; return(Prepare(local, local.TargetType, decodeContext, isReference)); }
private IExpression GetArraySizeExpression(VariableInformation varInfo) { IExpression size = null; for (int bracket = varInfo.sizes.Count - 1; bracket >= 0; bracket--) { for (int i = varInfo.sizes[bracket].Length - 1; i >= 0; i--) { if (size == null) { size = varInfo.sizes[bracket][i]; } else { var x = Enumerable.Range(0, 4).Sum(j => 5); var rangeExpr = Builder.StaticMethod(new Func <int, int, IEnumerable <int> >(Enumerable.Range), Builder.LiteralExpr(0), varInfo.sizes[bracket][i]); var delegateExpr = Builder.AnonMethodExpr(typeof(Func <int, int>)); delegateExpr.Parameters.Add(Builder.Param(varInfo.indexVars[bracket][i].Name, typeof(int))); delegateExpr.Body = Builder.BlockStmt(); delegateExpr.Body.Statements.Add(Builder.Return(size)); var sumExpr = Builder.Method(rangeExpr, new Func <IEnumerable <int>, Func <int, int>, int>(System.Linq.Enumerable.Sum <int>), delegateExpr); size = sumExpr; } } } if (size == null && typeof(IEnumerable).IsAssignableFrom(varInfo.varType)) { size = Builder.StaticMethod(new Func <IEnumerable, long>(JaggedArray.GetLongLength), varInfo.GetExpression()); } return(size); }
private void WriteArraySize(ICollection <IStatement> outputs, object decl) { string format = "{0} {1}"; IExpression formatExpr = Builder.LiteralExpr(format); VariableInformation varInfo = VariableInformation.GetVariableInformation(context, decl); IExpression varRefExpr; if (decl is IVariableDeclaration) { varRefExpr = Builder.VarRefExpr((IVariableDeclaration)decl); } else { varRefExpr = Builder.FieldRefExpr((IFieldDeclaration)decl); } var nameExpr = Builder.LiteralExpr(varRefExpr.ToString()); var sizeExpr = GetArraySizeExpression(varInfo); if (sizeExpr != null) { var writeExpr = Builder.StaticMethod(new Action <string, object, object>(Console.WriteLine).Method, formatExpr, sizeExpr, nameExpr); var writeStmt = Builder.ExprStatement(writeExpr); outputs.Add(writeStmt); } }
private void ProcessConstant(object decl) { if (context.InputAttributes.Has <IsInferred>(decl)) { VariableInformation vi = VariableInformation.GetVariableInformation(context, decl); vi.DefineAllIndexVars(context); MarginalPrototype mpa = Context.InputAttributes.Get <MarginalPrototype>(decl); if (!vi.SetMarginalPrototypeFromAttribute(mpa, throwIfMissing: false)) { IExpression varRef = null; if (decl is IVariableDeclaration) { varRef = Builder.VarRefExpr((IVariableDeclaration)decl); } else if (decl is IParameterDeclaration) { varRef = Builder.ParamRef((IParameterDeclaration)decl); } else { throw new NotImplementedException(); } vi.marginalPrototypeExpression = Builder.NewObject(typeof(PointMass <>).MakeGenericType(vi.varType), varRef); } } }
private object AddMarginalStatements(IExpression expr) { object decl = Recognizer.GetDeclaration(expr); if (!marginalOfVariable.ContainsKey(decl)) { VariableInformation vi = VariableInformation.GetVariableInformation(context, decl); vi.DefineAllIndexVars(context); IList <IStatement> stmts = Builder.StmtCollection(); CreateMarginalChannel(decl, vi, stmts); CreateUseChannel(decl, vi, stmts); IVariableDeclaration useDecl = useOfVariable[decl]; useOfVariable.Remove(decl); IExpression useExpr = Builder.VarRefExpr(useDecl); IVariableDeclaration marginalDecl = marginalOfVariable[decl]; IExpression marginalExpr = Builder.VarRefExpr(marginalDecl); Type[] genArgs = new Type[] { expr.GetExpressionType() }; IAlgorithm algorithm = this.algorithmDefault; Delegate d = algorithm.GetVariableFactor(true, false); IExpression variableFactorExpr = Builder.StaticGenericMethod(d, genArgs, expr, marginalExpr); //IExpression variableFactorExpr = Builder.StaticGenericMethod(new Func<PlaceHolder, PlaceHolder>(Factor.Copy), genArgs, expr); context.OutputAttributes.Set(variableFactorExpr, new IsVariableFactor()); var assignStmt = Builder.AssignStmt(useExpr, variableFactorExpr); stmts.Add(assignStmt); context.AddStatementsBeforeCurrent(stmts); } return(decl); }
private Traverse GetTraverse(string direction, IVariableInformation node) { Traverse go; var val = node.FindChildByName(direction); if (val == null) { return(null); } if (val.TypeName == node.TypeName) { go = (v) => v.FindChildByName(direction); } else { go = (v) => { ulong addr = MICore.Debugger.ParseAddr(v.Value); if (addr != 0) { var next = v.FindChildByName(direction); next = new VariableInformation("(" + v.TypeName + ")" + next.Value, next, _process.Engine, ""); next.SyncEval(); return(next); } return(null); }; } return(go); }
private string GetExpressionValue(string expression, IVariableInformation variable, IDictionary <string, string> scopedNames) { string processedExpr = ReplaceNamesInExpression(expression, variable, scopedNames); IVariableInformation expressionVariable = new VariableInformation(processedExpr, variable, _process.Engine, null); expressionVariable.SyncEval(); return(FormatDisplayString(expressionVariable)); }
private void CreateUsesChannel(VariableInformation vi, int useCount, VariableToChannelInformation vtci, IList <IStatement> stmts) { vtci.usageChannel = ChannelInfo.UseChannel(vi); vtci.usageChannel.decl = vi.DeriveArrayVariable(stmts, context, vi.Name + "_uses", Builder.LiteralExpr(useCount), Builder.VarDecl("_ind", typeof(int)), useLiteralIndices: true); context.InputAttributes.CopyObjectAttributesTo <InitialiseTo>(vi.declaration, context.OutputAttributes, vtci.usageChannel.decl); context.OutputAttributes.Set(vtci.usageChannel.decl, vtci.usageChannel); context.OutputAttributes.Set(vtci.usageChannel.decl, new DescriptionAttribute("uses of '" + vi.Name + "'")); }
/// <summary> /// Replace child field names in the expression with the childs full expression. /// Then evaluate the new expression. /// </summary> /// <param name="expression"></param> /// <param name="variable"></param> /// <returns></returns> private IVariableInformation GetExpression(string expression, IVariableInformation variable, IDictionary <string, string> scopedNames, string displayName = null) { string processedExpr = ReplaceNamesInExpression(expression, variable, scopedNames); IVariableInformation expressionVariable = new VariableInformation(processedExpr, variable, _process.Engine, displayName); expressionVariable.SyncEval(); return(expressionVariable); }
private HoistingInfo GetOrCreateHoistingInfo(IVariableDeclaration ivd) { HoistingInfo info; if (!infoOfVariable.TryGetValue(ivd, out info)) { info = new HoistingInfo(VariableInformation.GetVariableInformation(context, ivd)); infoOfVariable.Add(ivd, info); } return(info); }
private IForStatement ConvertForWithParallelSchedule(IForStatement ifs, IVariableDeclaration loopVar, ParallelScheduleExpression pse) { // Convert this loop into for(block) Parallel.For(thread) for(indexInBlock) IVariableDeclaration loopVarBlock = VariableInformation.GenerateLoopVar(context, loopVar.Name + "_Block"); Sequential sequentialAttr = new Sequential(); context.OutputAttributes.Set(loopVarBlock, sequentialAttr); IVariableDeclaration loopVarInBlock = VariableInformation.GenerateLoopVar(context, loopVar.Name + "_inBlock"); context.OutputAttributes.Set(loopVarInBlock, sequentialAttr); string paramName = VariableInformation.GenerateName(context, loopVar.Name + "_thread"); var threadParam = Builder.Param(paramName, typeof(int)); if (!pse.scheduleExpression.GetExpressionType().Equals(typeof(int[][][]))) { Error("argument to ParallelSchedule attribute is not of type int[][][]"); } IExpression itemsInThread = Builder.ArrayIndex(pse.scheduleExpression, Builder.ParamRef(threadParam)); IExpression itemsInBlock = Builder.ArrayIndex(itemsInThread, Builder.VarRefExpr(loopVarBlock)); IExpression itemCountInBlock = Builder.PropRefExpr(itemsInBlock, typeof(int[]), "Length"); IExpression threadCount = Builder.PropRefExpr(pse.scheduleExpression, typeof(int[][][]), "Length"); IExpression zero = Builder.LiteralExpr(0); IExpression blockCount = Builder.PropRefExpr(Builder.ArrayIndex(pse.scheduleExpression, zero), typeof(int[][]), "Length"); IForStatement loopInBlock = Builder.ForStmt(loopVarInBlock, itemCountInBlock); bool isBackwardLoop = !Recognizer.IsForwardLoop(ifs); if (isBackwardLoop) { Recognizer.ReverseLoopDirection(loopInBlock); } var assignLoopVar = Builder.AssignStmt(Builder.VarDeclExpr(loopVar), Builder.ArrayIndex(itemsInBlock, Builder.VarRefExpr(loopVarInBlock))); loopInBlock.Body.Statements.Add(assignLoopVar); ConvertStatements(loopInBlock.Body.Statements, ifs.Body.Statements); //loopInBlock.Body.Statements.AddRange(ifs.Body.Statements); IAnonymousMethodExpression bodyDelegate = Builder.AnonMethodExpr(typeof(Action <int>)); bodyDelegate.Body = Builder.BlockStmt(); bodyDelegate.Body.Statements.Add(loopInBlock); bodyDelegate.Parameters.Add(threadParam); Delegate d = new Func <int, int, Action <int>, ParallelLoopResult>(Parallel.For); IMethodInvokeExpression parallelFor = Builder.StaticMethod(d, zero, threadCount, bodyDelegate); IStatement loopThread = Builder.ExprStatement(parallelFor); IForStatement loopBlock = Builder.ForStmt(loopVarBlock, blockCount); loopBlock.Body.Statements.Add(loopThread); if (isBackwardLoop) { Recognizer.ReverseLoopDirection(loopBlock); } return(loopBlock); }
private void CreateMarginalChannel(IVariableDeclaration ivd, VariableInformation vi, IList <IStatement> stmts) { IVariableDeclaration marginalDecl = vi.DeriveIndexedVariable(stmts, context, ivd.Name + "_marginal"); marginalOfVariable[ivd] = marginalDecl; ChannelInfo marginalChannel = ChannelInfo.MarginalChannel(vi); marginalChannel.decl = marginalDecl; context.InputAttributes.CopyObjectAttributesTo <InitialiseTo>(vi.declaration, context.OutputAttributes, marginalChannel.decl); context.OutputAttributes.Set(marginalChannel.decl, marginalChannel); context.OutputAttributes.Set(marginalChannel.decl, new DescriptionAttribute("marginal of '" + ivd.Name + "'")); SetMarginalPrototype(marginalDecl); }
private void CreateMarginalChannel(VariableInformation vi, VariableToChannelInformation vtc, IList <IStatement> stmts) { IVariableDeclaration marginalDecl = vi.DeriveIndexedVariable(stmts, context, vi.Name + "_marginal"); vtc.marginalChannel = ChannelInfo.MarginalChannel(vi); vtc.marginalChannel.decl = marginalDecl; context.InputAttributes.CopyObjectAttributesTo <InitialiseTo>(vi.declaration, context.OutputAttributes, vtc.marginalChannel.decl); context.OutputAttributes.Set(vtc.marginalChannel.decl, vtc.marginalChannel); context.OutputAttributes.Set(vtc.marginalChannel.decl, new DescriptionAttribute("marginal of '" + vi.Name + "'")); VariableInformation marginalInformation = VariableInformation.GetVariableInformation(context, marginalDecl); marginalInformation.IsStochastic = true; }
private void CreateSamplesChannel(VariableInformation vi, VariableToChannelInformation vtci, IList <IStatement> stmts) { vtci.samplesChannel = ChannelInfo.MarginalChannel(vi); vtci.samplesChannel.decl = vi.DeriveIndexedVariable(stmts, context, vi.Name + "_samples"); context.OutputAttributes.Remove <InitialiseTo>(vtci.samplesChannel.decl); context.OutputAttributes.Set(vtci.samplesChannel.decl, vtci.samplesChannel); context.OutputAttributes.Set(vtci.samplesChannel.decl, new DescriptionAttribute("samples of '" + vi.Name + "'")); Type domainType = vi.VariableType.DotNetType; Type samplesType = typeof(List <>).MakeGenericType(domainType); IExpression samples_mpe = Builder.NewObject(samplesType); VariableInformation samples_vi = VariableInformation.GetVariableInformation(context, vtci.samplesChannel.decl); samples_vi.marginalPrototypeExpression = samples_mpe; }
/// <summary> /// Convert random assignments to derived variables /// </summary> /// <param name="iae"></param> /// <returns></returns> protected override IExpression ConvertAssign(IAssignExpression iae) { iae = (IAssignExpression)base.ConvertAssign(iae); IStatement ist = context.FindAncestor <IStatement>(); if (!context.InputAttributes.Has <Models.Constraint>(ist) && (iae.Expression is IMethodInvokeExpression)) { IMethodInvokeExpression imie = (IMethodInvokeExpression)iae.Expression; IVariableDeclaration ivd = Recognizer.GetVariableDeclaration(iae.Target); if (ivd != null) { bool isDerived = context.InputAttributes.Has <DerivedVariable>(ivd); if (isDerived) { FactorManager.FactorInfo info = CodeRecognizer.GetFactorInfo(context, imie); if (!info.IsDeterministicFactor) { // The variable is derived, but this definition is not derived. // Thus we must convert // y = sample() // into // y_random = sample() // y = Copy(y_random) // where y_random is not derived. VariableInformation varInfo = VariableInformation.GetVariableInformation(context, ivd); IList <IStatement> stmts = Builder.StmtCollection(); string name = ivd.Name + "_random" + (Count++); List <IList <IExpression> > indices = Recognizer.GetIndices(iae.Target); IVariableDeclaration cloneVar = varInfo.DeriveIndexedVariable(stmts, context, name, indices, copyInitializer: true); context.OutputAttributes.Remove <DerivedVariable>(cloneVar); stmts.Add(Builder.AssignStmt(Builder.VarRefExpr(cloneVar), iae.Expression)); int ancIndex = context.FindAncestorIndex <IStatement>(); context.AddStatementsBeforeAncestorIndex(ancIndex, stmts); Type tp = iae.Target.GetExpressionType(); if (tp == null) { Error("Could not determine type of expression: " + iae.Target); return(iae); } IExpression copy = Builder.StaticGenericMethod(new Func <PlaceHolder, PlaceHolder>(Clone.Copy), new Type[] { tp }, Builder.VarRefExpr(cloneVar)); iae = Builder.AssignExpr(iae.Target, copy); } } } } return(iae); }
private object AddMarginalStatements(IExpression expr) { object decl = Recognizer.GetDeclaration(expr); if (!context.InputAttributes.Has <VariableToChannelInformation>(decl)) { VariableInformation vi = VariableInformation.GetVariableInformation(context, decl); vi.DefineAllIndexVars(context); IList <IStatement> stmts = Builder.StmtCollection(); VariableToChannelInformation vtci = new VariableToChannelInformation(); vtci.shareAllUses = true; Context.InputAttributes.Set(decl, vtci); CreateMarginalChannel(vi, vtci, stmts); IExpression marginalExpr = Builder.VarRefExpr(vtci.marginalChannel.decl); Type[] genArgs = new Type[] { expr.GetExpressionType() }; IExpression countExpr = Builder.LiteralExpr(0); IExpression variableFactorExpr; if (algorithm is GibbsSampling && ((GibbsSampling)algorithm).UseSideChannels) { CreateSamplesChannel(vi, vtci, stmts); CreateConditionalsChannel(vi, vtci, stmts); IExpression burnInExpr = Builder.LiteralExpr(0); IExpression thinExpr = Builder.LiteralExpr(1); IExpression samplesExpr = Builder.VarRefExpr(vtci.samplesChannel.decl); IExpression conditionalsExpr = Builder.VarRefExpr(vtci.conditionalsChannel.decl); Delegate d = new FuncOut3 <PlaceHolder, int, int, int, PlaceHolder, PlaceHolder, PlaceHolder, PlaceHolder[]>(Clone.ReplicateWithMarginalGibbs); variableFactorExpr = Builder.StaticGenericMethod(d, genArgs, expr, countExpr, burnInExpr, thinExpr, marginalExpr, samplesExpr, conditionalsExpr); } else { vtci.samplesChannel = vtci.marginalChannel; vtci.conditionalsChannel = vtci.marginalChannel; Delegate d = new FuncOut <PlaceHolder, int, PlaceHolder, PlaceHolder[]>(Clone.ReplicateWithMarginal); variableFactorExpr = Builder.StaticGenericMethod(d, genArgs, expr, countExpr, marginalExpr); } CreateUsesChannel(vi, 0, vtci, stmts); IExpression usesExpr = Builder.VarRefExpr(vtci.usageChannel.decl); vtci.usageChannel = null; stmts.Add(Builder.AssignStmt(usesExpr, variableFactorExpr)); context.OutputAttributes.Set(variableFactorExpr, new IsVariableFactor()); context.AddStatementsBeforeCurrent(stmts); } return(decl); }
private void SetMarginalPrototype(IVariableDeclaration ivd) { VariableInformation vi = VariableInformation.GetVariableInformation(context, ivd); // Ensure the marginal prototype is set. MarginalPrototype mpa = Context.InputAttributes.Get <MarginalPrototype>(ivd); try { vi.SetMarginalPrototypeFromAttribute(mpa); } catch (ArgumentException ex) { Error(ex.Message); } }
private void CreateUseChannel(IVariableDeclaration ivd, VariableInformation vi, IList <IStatement> stmts) { IVariableDeclaration useDecl = vi.DeriveIndexedVariable(stmts, context, ivd.Name + "_use"); useOfVariable[ivd] = useDecl; ChannelInfo usageChannel = ChannelInfo.UseChannel(vi); usageChannel.decl = useDecl; context.InputAttributes.CopyObjectAttributesTo <InitialiseTo>(vi.declaration, context.OutputAttributes, useDecl); context.InputAttributes.CopyObjectAttributesTo <DerivMessage>(vi.declaration, context.OutputAttributes, useDecl); context.OutputAttributes.Set(useDecl, usageChannel); context.OutputAttributes.Set(useDecl, new DescriptionAttribute("use of '" + ivd.Name + "'")); context.OutputAttributes.Remove <InitialiseTo>(vi.declaration); SetMarginalPrototype(useDecl); }
private void CreateConditionalsChannel(VariableInformation vi, VariableToChannelInformation vtci, IList <IStatement> stmts) { vtci.conditionalsChannel = ChannelInfo.MarginalChannel(vi); vtci.conditionalsChannel.decl = vi.DeriveIndexedVariable(stmts, context, vi.Name + "_conditionals"); context.OutputAttributes.Remove <InitialiseTo>(vtci.conditionalsChannel.decl); context.OutputAttributes.Set(vtci.conditionalsChannel.decl, vtci.conditionalsChannel); context.OutputAttributes.Set(vtci.conditionalsChannel.decl, new DescriptionAttribute("conditionals of '" + vi.Name + "'")); Type marginalType = MessageTransform.GetDistributionType(vi.varType, vi.InnermostElementType, vi.marginalPrototypeExpression.GetExpressionType(), true); Type conditionalsType = typeof(List <>).MakeGenericType(marginalType); IExpression conditionals_mpe = Builder.NewObject(conditionalsType); VariableInformation conditionals_vi = VariableInformation.GetVariableInformation(context, vtci.conditionalsChannel.decl); conditionals_vi.marginalPrototypeExpression = conditionals_mpe; }
private IVariableDeclaration CreateMarginalChannel(VariableInformation vi, IList <IStatement> stmts) { IVariableDeclaration marginalDecl = vi.DeriveIndexedVariable(stmts, context, vi.Name + "_marginal"); ChannelInfo marginalChannel = ChannelInfo.MarginalChannel(vi); marginalChannel.decl = marginalDecl; context.InputAttributes.CopyObjectAttributesTo <InitialiseTo>(vi.declaration, context.OutputAttributes, marginalDecl); context.OutputAttributes.Set(marginalDecl, marginalChannel); context.OutputAttributes.Set(marginalDecl, new DescriptionAttribute("marginal of '" + vi.Name + "'")); // The following lines are needed for AddMarginalStatements VariableInformation marginalInformation = VariableInformation.GetVariableInformation(context, marginalDecl); marginalInformation.IsStochastic = true; return(marginalDecl); }
private static void AddCopyStatements(ICollection <IStatement> stmts, VariableInformation varInfo, int indexingDepth, IExpression lhs, IExpression rhs, int bracket = 0, Dictionary <IExpression, IExpression> replacements = null) { if (indexingDepth == bracket) { Type tp = rhs.GetExpressionType(); IExpression copy = Builder.StaticGenericMethod(new Func <PlaceHolder, PlaceHolder>(Factor.Copy <PlaceHolder>), new Type[] { tp }, rhs); IStatement copySt = Builder.AssignStmt(lhs, copy); stmts.Add(copySt); } else if (varInfo.LiteralIndexingDepth == bracket + 1 && varInfo.sizes[bracket].All(size => size is ILiteralExpression)) { if (replacements == null) { replacements = new Dictionary <IExpression, IExpression>(); } int[] sizes = Util.ArrayInit(varInfo.sizes[bracket].Length, i => (int)((ILiteralExpression)varInfo.sizes[bracket][i]).Value); ForEachLiteralIndex(sizes, index => { IExpression[] bracketIndices = Util.ArrayInit(index.Length, i => Builder.LiteralExpr(index[i])); var newLhs = Builder.ArrayIndex(lhs, bracketIndices); var newRhs = Builder.ArrayIndex(rhs, bracketIndices); for (int dim = 0; dim < index.Length; dim++) { var indexVarRef = Builder.VarRefExpr(varInfo.indexVars[bracket][dim]); replacements[indexVarRef] = bracketIndices[dim]; } AddCopyStatements(stmts, varInfo, indexingDepth, newLhs, newRhs, bracket + 1, replacements); }); } else { IReadOnlyList <IExpression> replacedSizes = varInfo.sizes[bracket]; if (replacements != null) { replacedSizes = Util.ArrayInit(replacedSizes.Count, i => Replace(replacedSizes[i], replacements)); } IForStatement innerForStatement; var fs = Builder.NestedForStmt(varInfo.indexVars[bracket], replacedSizes, out innerForStatement); IExpression[] bracketIndices = Util.ArrayInit(varInfo.indexVars[bracket].Length, i => Builder.VarRefExpr(varInfo.indexVars[bracket][i]) ); var newLhs = Builder.ArrayIndex(lhs, bracketIndices); var newRhs = Builder.ArrayIndex(rhs, bracketIndices); AddCopyStatements(innerForStatement.Body.Statements, varInfo, indexingDepth, newLhs, newRhs, bracket + 1, replacements); stmts.Add(fs); } }
protected override IExpression ConvertVariableDeclExpr(IVariableDeclarationExpression ivde) { IVariableDeclaration ivd = Recognizer.GetVariableDeclaration(ivde); VariableInformation vi = VariableInformation.GetVariableInformation(context, ivd); if (vi.IsStochastic) { UsageInfo info = new UsageInfo(); info.containers = new Containers(context); usageInfo[ivd] = info; } if (Recognizer.GetAncestorIndexOfLoopBeingInitialized(context) != -1) { loopVars.Add(ivd); } return(base.ConvertVariableDeclExpr(ivde)); }
private IVariableDeclaration CreateUseChannel(VariableInformation vi, IList <IStatement> stmts) { IVariableDeclaration useDecl = vi.DeriveIndexedVariable(stmts, context, vi.Name + "_use"); ChannelInfo usageChannel = ChannelInfo.UseChannel(vi); usageChannel.decl = useDecl; context.InputAttributes.CopyObjectAttributesTo <InitialiseTo>(vi.declaration, context.OutputAttributes, useDecl); context.InputAttributes.CopyObjectAttributesTo <DerivMessage>(vi.declaration, context.OutputAttributes, useDecl); context.OutputAttributes.Set(useDecl, usageChannel); context.OutputAttributes.Set(useDecl, new DescriptionAttribute("use of '" + vi.Name + "'")); context.OutputAttributes.Remove <InitialiseTo>(vi.declaration); // The following lines are needed for AddMarginalStatements VariableInformation useInformation = VariableInformation.GetVariableInformation(context, useDecl); useInformation.IsStochastic = true; return(useDecl); }
protected override IExpression ConvertAssign(IAssignExpression iae) { // set index variables on the converted target, using the unconverted lhs indices // this code is copied from ModelAnalysisTransform IAssignExpression ae = (IAssignExpression)base.ConvertAssign(iae); IParameterDeclaration ipd = null; IVariableDeclaration ivd = Recognizer.GetVariableDeclaration(ae.Target); if (ivd == null) { ipd = Recognizer.GetParameterDeclaration(ae.Target); if (ipd == null) { return(ae); } } if (iae.Target is IArrayIndexerExpression) { // Gather index variables from the left-hand side of the assignment object decl = (ipd == null) ? (object)ivd : ipd; VariableInformation vi = VariableInformation.GetVariableInformation(context, decl); try { List <IVariableDeclaration[]> indVars = new List <IVariableDeclaration[]>(); Recognizer.AddIndexers(context, indVars, iae.Target); // Sets the size of this variable at this array depth int depth = Recognizer.GetIndexingDepth(iae.Target); // if this statement is actually a constraint, then we don't need to enforce matching of index variables bool isConstraint = context.InputAttributes.Has <Models.Constraint>(context.FindAncestor <IStatement>()); for (int i = 0; i < depth; i++) { vi.SetIndexVariablesAtDepth(i, indVars[i], allowMismatch: isConstraint); } } catch (Exception ex) { Error(ex.Message, ex); } if (!context.InputAttributes.Has <VariableInformation>(decl)) { context.InputAttributes.Set(decl, vi); } } return(ae); }
private IExpression GetPrefixIndexedByIndexVars(IExpression expr) { var decl = Recognizer.GetDeclaration(expr); if (decl != null) { var varInfo = VariableInformation.GetVariableInformation(context, decl); var indices = Recognizer.GetIndices(expr); for (int bracket = 0; bracket < indices.Count; bracket++) { if (!IndicesMatchIndexVars(indices[bracket], varInfo.indexVars[bracket])) { return(GetExpressionUpToDepth(expr, bracket, indices.Count)); } } } return(expr); }
internal IVariableInformation GetVariable(string expr, AD7StackFrame frame) { IVariableInformation variable; if (expr.EndsWith(",viz", StringComparison.Ordinal)) { expr = expr.Substring(0, expr.Length - 4); variable = new VariableInformation(expr, frame.ThreadContext, frame.Engine, frame.Thread); variable.SyncEval(); if (!variable.Error) { variable = GetVisualizationWrapper(variable) ?? variable; } } else { variable = new VariableInformation(expr, frame.ThreadContext, frame.Engine, frame.Thread); } return variable; }
public AD7Property(VariableInformation vi) { m_variableInformation = vi; }
private IVariableInformation[] ExpandVisualized(IVariableInformation variable) { VisualizerInfo visualizer = FindType(variable); if (visualizer == null) { return variable.Children; } List<IVariableInformation> children = new List<IVariableInformation>(); ExpandType1 expandType = (ExpandType1)Array.Find(visualizer.Visualizer.Items, (o) => { return o is ExpandType1; }); if (expandType == null) { return variable.Children; } foreach (var i in expandType.Items) { if (i is ItemType) { ItemType item = (ItemType)i; if (!EvalCondition(item.Condition, variable, visualizer.ScopedNames)) { continue; } IVariableInformation expr = GetExpression(item.Value, variable, visualizer.ScopedNames, item.Name); children.Add(expr); } else if (i is ArrayItemsType) { ArrayItemsType item = (ArrayItemsType)i; if (!EvalCondition(item.Condition, variable, visualizer.ScopedNames)) { continue; } uint size = 0; string val = GetExpressionValue(item.Size, variable, visualizer.ScopedNames); size = MICore.Debugger.ParseUint(val, throwOnError: true); size = size > MAX_EXPAND ? MAX_EXPAND : size; // limit expansion ValuePointerType[] vptrs = item.ValuePointer; foreach (var vp in vptrs) { if (EvalCondition(vp.Condition, variable, visualizer.ScopedNames)) { IVariableInformation ptrExpr = GetExpression('*' + vp.Value, variable, visualizer.ScopedNames); string typename = ptrExpr.TypeName; if (String.IsNullOrWhiteSpace(typename)) { continue; } StringBuilder arrayBuilder = new StringBuilder(); arrayBuilder.Append('('); arrayBuilder.Append(typename); arrayBuilder.Append('['); arrayBuilder.Append(size); arrayBuilder.Append("])*("); arrayBuilder.Append(vp.Value); arrayBuilder.Append(')'); string arrayStr = arrayBuilder.ToString(); IVariableInformation arrayExpr = GetExpression(arrayStr, variable, visualizer.ScopedNames); arrayExpr.EnsureChildren(); if (arrayExpr.CountChildren != 0) { children.AddRange(arrayExpr.Children); } break; } } } else if (i is TreeItemsType) { TreeItemsType item = (TreeItemsType)i; if (!EvalCondition(item.Condition, variable, visualizer.ScopedNames)) { continue; } if (String.IsNullOrWhiteSpace(item.Size) || String.IsNullOrWhiteSpace(item.HeadPointer) || String.IsNullOrWhiteSpace(item.LeftPointer) || String.IsNullOrWhiteSpace(item.RightPointer)) { continue; } if (item.ValueNode == null || String.IsNullOrWhiteSpace(item.ValueNode.Value)) { continue; } string val = GetExpressionValue(item.Size, variable, visualizer.ScopedNames); uint size = MICore.Debugger.ParseUint(val, throwOnError: true); size = size > MAX_EXPAND ? MAX_EXPAND : size; // limit expansion IVariableInformation headVal = GetExpression(item.HeadPointer, variable, visualizer.ScopedNames); ulong head = MICore.Debugger.ParseAddr(headVal.Value); var content = new List<IVariableInformation>(); if (head != 0 && size != 0) { headVal.EnsureChildren(); Traverse goLeft = GetTraverse(item.LeftPointer, headVal); Traverse goRight = GetTraverse(item.RightPointer, headVal); Traverse getValue = null; if (item.ValueNode.Value == "this") // TODO: handle condition { getValue = (v) => v; } else if (headVal.FindChildByName(item.ValueNode.Value) != null) { getValue = (v) => v.FindChildByName(item.ValueNode.Value); } if (goLeft == null || goRight == null || getValue == null) { continue; } TraverseTree(headVal, goLeft, goRight, getValue, children, size); } } else if (i is LinkedListItemsType) { // example: // <LinkedListItems> // <Size>m_nElements</Size> -- optional, will go until NextPoint is 0 or == HeadPointer // <HeadPointer>m_pHead</HeadPointer> // <NextPointer>m_pNext</NextPointer> // <ValueNode>m_element</ValueNode> // </LinkedListItems> LinkedListItemsType item = (LinkedListItemsType)i; if (String.IsNullOrWhiteSpace(item.Condition)) { if (!EvalCondition(item.Condition, variable, visualizer.ScopedNames)) continue; } if (String.IsNullOrWhiteSpace(item.HeadPointer) || String.IsNullOrWhiteSpace(item.NextPointer)) { continue; } if (String.IsNullOrWhiteSpace(item.ValueNode)) { continue; } uint size = MAX_EXPAND; if (!String.IsNullOrWhiteSpace(item.Size)) { string val = GetExpressionValue(item.Size, variable, visualizer.ScopedNames); size = MICore.Debugger.ParseUint(val); size = size > MAX_EXPAND ? MAX_EXPAND : size; // limit expansion } IVariableInformation headVal = GetExpression(item.HeadPointer, variable, visualizer.ScopedNames); ulong head = MICore.Debugger.ParseAddr(headVal.Value); var content = new List<IVariableInformation>(); if (head != 0 && size != 0) { headVal.EnsureChildren(); Traverse goNext = GetTraverse(item.NextPointer, headVal); Traverse getValue = null; if (item.ValueNode == "this") { getValue = (v) => v; } else if (headVal.FindChildByName(item.ValueNode) != null) { getValue = (v) => v.FindChildByName(item.ValueNode); } else { var value = GetExpression(item.ValueNode, headVal, visualizer.ScopedNames); if (value != null && !value.Error) { getValue = (v) => GetExpression(item.ValueNode, v, visualizer.ScopedNames); } } if (goNext == null || getValue == null) { continue; } TraverseList(headVal, goNext, getValue, children, size, item.NoValueHeadPointer); } } else if (i is IndexListItemsType) { // example: // <IndexListItems> // <Size>_M_vector._M_index</Size> // <ValueNode>*(_M_vector._M_array[$i])</ValueNode> // </IndexListItems> IndexListItemsType item = (IndexListItemsType)i; if (!EvalCondition(item.Condition, variable, visualizer.ScopedNames)) { continue; } var sizes = item.Size; uint size = 0; if (sizes == null) { continue; } foreach (var s in sizes) { if (string.IsNullOrWhiteSpace(s.Value)) continue; if (EvalCondition(s.Condition, variable, visualizer.ScopedNames)) { string val = GetExpressionValue(s.Value, variable, visualizer.ScopedNames); size = MICore.Debugger.ParseUint(val); size = size > MAX_EXPAND ? MAX_EXPAND : size; // limit expansion break; } } var values = item.ValueNode; if (values == null) { continue; } foreach (var v in values) { if (string.IsNullOrWhiteSpace(v.Value)) continue; if (EvalCondition(v.Condition, variable, visualizer.ScopedNames)) { string processedExpr = ReplaceNamesInExpression(v.Value, variable, visualizer.ScopedNames); Dictionary<string, string> indexDic = new Dictionary<string, string>(); for (uint index = 0; index < size; ++index) { indexDic["$i"] = index.ToString(CultureInfo.InvariantCulture); string finalExpr = ReplaceNamesInExpression(processedExpr, null, indexDic); IVariableInformation expressionVariable = new VariableInformation(finalExpr, variable, _process.Engine, "[" + indexDic["$i"] + "]"); expressionVariable.SyncEval(); children.Add(expressionVariable); } break; } } } else if (i is ExpandedItemType) { ExpandedItemType item = (ExpandedItemType)i; // example: // <Type Name="std::auto_ptr<*>"> // <DisplayString>auto_ptr {*_Myptr}</DisplayString> // <Expand> // <ExpandedItem>_Myptr</ExpandedItem> // </Expand> // </Type> if (item.Condition != null) { if (!EvalCondition(item.Condition, variable, visualizer.ScopedNames)) { continue; } } if (String.IsNullOrWhiteSpace(item.Value)) { continue; } var expand = GetExpression(item.Value, variable, visualizer.ScopedNames); var eChildren = Expand(expand); if (eChildren != null) { children.AddRange(eChildren); } } } if (!(variable is VisualizerWrapper)) // don't stack wrappers { // add the [Raw View] field IVariableInformation rawView = new VisualizerWrapper(ResourceStrings.RawView, _process.Engine, variable, visualizer, isVisualizerView: false); children.Add(rawView); } return children.ToArray(); }
/// <summary> /// Replace child field names in the expression with the childs full expression. /// Then evaluate the new expression. /// </summary> /// <param name="expression"></param> /// <param name="variable"></param> /// <returns></returns> private IVariableInformation GetExpression(string expression, IVariableInformation variable, IDictionary<string, string> scopedNames, string displayName = null) { string processedExpr = ReplaceNamesInExpression(expression, variable, scopedNames); IVariableInformation expressionVariable = new VariableInformation(processedExpr, variable, _process.Engine, displayName); expressionVariable.SyncEval(); return expressionVariable; }
private string GetExpressionValue(string expression, IVariableInformation variable, IDictionary<string, string> scopedNames) { string processedExpr = ReplaceNamesInExpression(expression, variable, scopedNames); IVariableInformation expressionVariable = new VariableInformation(processedExpr, variable, _process.Engine, null); expressionVariable.SyncEval(); return FormatDisplayString(expressionVariable); }
private void InitializeBytes() { if (_bytes != null) return; uint fetched = 0; _bytes = new byte[0]; IDebugMemoryContext2 memAddr; if (GetMemoryContext(out memAddr) != Constants.S_OK) { // no address in the expression value, try casting to a char* VariableInformation v = new VariableInformation("(char*)(" + _variableInformation.FullName() + ")", (VariableInformation)_variableInformation); v.SyncEval(); if (v.Error) { return; } AD7Property p = new AD7Property(v); uint pLen; if (p.GetStringCharLength(out pLen) == Constants.S_OK) { _bytes = new byte[pLen]; p.GetStringRawBytes(pLen, _bytes, out fetched); } return; } IDebugMemoryBytes2 memContent; if (((AD7MemoryAddress)memAddr).Engine.GetMemoryBytes(out memContent) != Constants.S_OK) { return; } fetched = 0; bool eos = false; byte[] bytes = new byte[s_maxChars + 1]; byte[] chunk = new byte[2048]; while (!eos) { // fetched is count of bytes in string so far // eos == false => fetch < s_maxChars // eos == true => string is terminated, that is bytes[fetched-1] == 0 uint bytesRead; uint bytesUnreadable = 0; // get next chunk if (memContent.ReadAt(memAddr, (uint)chunk.Length, chunk, out bytesRead, ref bytesUnreadable) != Constants.S_OK) { break; } // copy chunk to bytes for (uint i = 0; i < bytesRead; ++i) { bytes[fetched++] = chunk[i]; if (bytes[fetched - 1] == 0) { eos = true; // end of string break; } if (fetched == s_maxChars) // buffer is full { bytes[fetched++] = 0; // end the string eos = true; break; } } if (bytesRead != chunk.Length) { // read to end of available memory break; } // advance to next chunk memAddr.Add(bytesRead, out memAddr); } if (!eos) { Debug.Assert(fetched < bytes.Length); bytes[fetched++] = 0; } if (fetched < bytes.Length) { _bytes = new byte[fetched]; Array.Copy(bytes, _bytes, fetched); } else { _bytes = bytes; } }
private Traverse GetTraverse(string direction, IVariableInformation node) { Traverse go; var val = node.FindChildByName(direction); if (val == null) { return null; } if (val.TypeName == node.TypeName) { go = (v) => v.FindChildByName(direction); } else { go = (v) => { ulong addr = MICore.Debugger.ParseAddr(v.Value); if (addr != 0) { var next = v.FindChildByName(direction); next = new VariableInformation("(" + v.TypeName + ")" + next.Value, next, _process.Engine, ""); next.SyncEval(); return next; } return null; }; } return go; }