[NotNull] public static IModel BuildSAT([NotNull] this IBasicBlock block, [NotNull] ITypeAssignments types) { var ctx = new Context(); var solver = ctx.MkSolver(); var model = new Model(ctx, solver); foreach (var stmt in block.Statements) { Assert(model, types, stmt); } return(model); }
private static void Assert([NotNull] Model model, [NotNull] ITypeAssignments types, [NotNull] BaseStatement stmt) { switch (stmt) { default: throw new ArgumentOutOfRangeException(stmt.GetType().Name); case ExpressionWrapper _: case CompoundAssignment _: case If _: throw new NotSupportedException(stmt.GetType().Name); case EmptyStatement _: return; case Conditional conditional: Assert(model, conditional); return; case ErrorStatement errorStatement: Assert(model, errorStatement); return; case Assignment assignment: Assert(model, types, assignment); return; case Goto @goto: Assert(model, @goto); return; case StatementList statementList: foreach (var sub in statementList.Statements) { Assert(model, types, sub); } return; } }
public OpNumByConstNumCompressor(ITypeAssignments types) { _types = types; }
#pragma warning restore IDE0052 // Remove unread private members public BooleanExpressionInspector(HashSet <VariableName> bools, ITypeAssignments types) { _bools = bools; _types = types; }
public FindBooleanVariables(HashSet <VariableName> names, ISingleStaticAssignmentTable ssa, ITypeAssignments types) { _names = names; _ssa = ssa; _types = types; }
public ExpressionTypeInference(ITypeAssignments types) { _types = types; }
[NotNull] public static IControlFlowGraph FlowTypingAssignment( [NotNull] this IControlFlowGraph graph, [NotNull] ISingleStaticAssignmentTable ssa, // We require the SSA object because SSA must be done before flow typing [NotNull] out ITypeAssignments types, [NotNull] params (VariableName, Execution.Type)[] hints)
public IfAssignmentCompression(ITypeAssignments types) { _types = types; }
/// <summary> /// Replace inc/dec operations on numbers with a+=1 and a-=1 /// </summary> /// <param name="cfg"></param> /// <param name="types"></param> /// <returns></returns> public static IControlFlowGraph SimplifyModificationExpressions(this IControlFlowGraph cfg, ITypeAssignments types) { return(cfg.VisitBlocks(() => new SimplifyModify(types))); }
/// <summary> /// Removed error/continue edges which we know cannot happen due to type info /// </summary> /// <param name="graph"></param> /// <param name="types"></param> /// <returns></returns> public static IControlFlowGraph TypeDrivenEdgeTrimming(this IControlFlowGraph graph, ITypeAssignments types) { return(graph.Trim(edge => { // Find last statement in previous block var stmt = edge.Start.Statements.LastOrDefault(); var tass = stmt as TypedAssignment; var err = stmt as ErrorStatement; // If type is unassigned we can't make a judgement if (tass?.Type == Execution.Type.Unassigned) { return true; } if (edge.Type == EdgeType.RuntimeError) { // If it's an error statement keep it if (err != null) { return true; } // If there is no statement at all then it can't be an error if (tass == null) { return false; } // Only keep edge if type has an error return tass.Type.HasFlag(Execution.Type.Error); } else { // If it's an error statement remove it if (err != null) { return false; } // If there is no typed assignment we can't judge if (tass == null) { return true; } return tass.Type != Execution.Type.Error; } })); }
public SimplifyModify(ITypeAssignments types) { _types = types; }
public CompoundCompressor(ITypeAssignments types) { _types = types; }
private static void Assert([NotNull] Model model, [NotNull] ITypeAssignments types, [NotNull] Assignment assignment) { var l = model.GetOrCreateVariable(assignment.Left); l.AssertEq(assignment.Right); }
public static Type InferType([NotNull] this BaseExpression expr, ITypeAssignments types) { return(new ExpressionTypeInference(types).Visit(expr)); }
/// <summary> /// Find variables which are guaranteed to be `0` or `1` /// </summary> /// <param name="cfg"></param> /// <param name="ssa"></param> /// <param name="types"></param> /// <returns></returns> [NotNull] public static ISet <VariableName> FindBooleanVariables([NotNull] this IControlFlowGraph cfg, ISingleStaticAssignmentTable ssa, ITypeAssignments types) { var booleans = new HashSet <VariableName>(); // Keep finding more constants until no more are found var count = -1; while (count != booleans.Count) { count = booleans.Count; cfg.VisitBlocks(() => new FindBooleanVariables(booleans, ssa, types)); } var result = new HashSet <VariableName>(booleans.Where(n => !n.IsExternal)); return(result); }