Exemplo n.º 1
0
        [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);
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
 public OpNumByConstNumCompressor(ITypeAssignments types)
 {
     _types = types;
 }
Exemplo n.º 4
0
#pragma warning restore IDE0052 // Remove unread private members

            public BooleanExpressionInspector(HashSet <VariableName> bools, ITypeAssignments types)
            {
                _bools = bools;
                _types = types;
            }
Exemplo n.º 5
0
 public FindBooleanVariables(HashSet <VariableName> names, ISingleStaticAssignmentTable ssa, ITypeAssignments types)
 {
     _names = names;
     _ssa   = ssa;
     _types = types;
 }
Exemplo n.º 6
0
 public ExpressionTypeInference(ITypeAssignments types)
 {
     _types = types;
 }
Exemplo n.º 7
0
 [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)
Exemplo n.º 8
0
 public IfAssignmentCompression(ITypeAssignments types)
 {
     _types = types;
 }
Exemplo n.º 9
0
 /// <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)));
 }
Exemplo n.º 10
0
        /// <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;
                }
            }));
        }
Exemplo n.º 11
0
 public SimplifyModify(ITypeAssignments types)
 {
     _types = types;
 }
Exemplo n.º 12
0
 public CompoundCompressor(ITypeAssignments types)
 {
     _types = types;
 }
Exemplo n.º 13
0
        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));
 }
Exemplo n.º 15
0
        /// <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);
        }