Exemple #1
0
 /// <summary>
 /// Constructs a new analysis context.
 /// </summary>
 /// <param name="valueMapping">The parent value mapping.</param>
 /// <param name="onStack">The internal block set.</param>
 public ValueAnalysisContext(
     AnalysisValueMapping <T> valueMapping,
     BasicBlockSet onStack)
     : base(onStack)
 {
     mapping = valueMapping;
 }
Exemple #2
0
 /// <summary>
 /// Constructs a new block analysis context.
 /// </summary>
 /// <param name="dataMapping">The block mapping.</param>
 /// <param name="onStack">The internal block set.</param>
 public BlockAnalysisContext(
     BasicBlockMap <TData> dataMapping,
     BasicBlockSet onStack)
     : base(onStack)
 {
     mapping = dataMapping;
 }
Exemple #3
0
 public ValueAnalysisContext(
     Dictionary <Value, TData> valueMapping,
     BasicBlockSet onStack)
     : base(onStack)
 {
     ValueMapping = valueMapping;
 }
        /// <summary>
        /// Computes a traversal using the current order.
        /// </summary>
        /// <typeparam name="TVisitor">The visitor type.</typeparam>
        /// <typeparam name="TSuccessorProvider">The successor provider.</typeparam>
        /// <typeparam name="TDirection">The control-flow direction.</typeparam>
        /// <param name="entryBlock">The entry block.</param>
        /// <param name="visitor">The visitor instance.</param>
        /// <param name="successorProvider">The successor provider.</param>
        /// <returns>The created traversal.</returns>
        public readonly void Traverse <
            TVisitor,
            TSuccessorProvider,
            TDirection>(
            BasicBlock entryBlock,
            ref TVisitor visitor,
            TSuccessorProvider successorProvider)
            where TVisitor : struct, ITraversalVisitor
            where TSuccessorProvider : ITraversalSuccessorsProvider <TDirection>
            where TDirection : struct, IControlFlowDirection
        {
            var visited = BasicBlockSet.Create(entryBlock);
            var stack   = new Stack <(BasicBlock, int)>(TraversalOrder.InitStackSize);
            var current = (Block : entryBlock, Child : 0);

            while (true)
            {
                var currentBlock = current.Block;

                if (current.Child == 0)
                {
                    if (!visited.Add(currentBlock))
                    {
                        goto next;
                    }
                }

                var successors = successorProvider.GetSuccessors(currentBlock);
                if (current.Child >= successors.Length)
                {
                    visitor.Visit(currentBlock);
                    goto next;
                }
                else
                {
                    stack.Push((currentBlock, current.Child + 1));
                    current = (successors[current.Child], 0);
                }

                continue;
next:
                if (stack.Count < 1)
                {
                    break;
                }
                current = stack.Pop();
            }
        }
Exemple #5
0
        private static bool MergeChain(
            Method.Builder builder,
            BasicBlock root,
            ref BasicBlockSet visited)
        {
            if (root.Successors.Length != 1 || visited.Contains(root))
            {
                return(false);
            }

            // Mark node as seen
            visited.Add(root);

            // Init initial builder and successors list
            var  rootBlockBuilder = builder[root];
            var  successors       = root.Successors;
            bool result           = false;

            do
            {
                var nextBlock = successors[0];

                // We cannot merge jump targets in div. control-flow or in the case
                // of a block that we have already seen
                if (nextBlock.Predecessors.Length > 1 || visited.Contains(nextBlock))
                {
                    break;
                }

                // Mark next block as seen
                visited.Add(nextBlock);

                // Merge block
                successors = nextBlock.Successors;
                rootBlockBuilder.MergeBlock(nextBlock);

                // Return true as we have changed the IR
                result = true;
            }while (successors.Length == 1);

            // Return the success status
            return(result);
        }
        /// <summary>
        /// Computes a traversal using the current order.
        /// </summary>
        /// <typeparam name="TVisitor">The visitor type.</typeparam>
        /// <typeparam name="TSuccessorProvider">The successor provider.</typeparam>
        /// <typeparam name="TDirection">The control-flow direction.</typeparam>
        /// <param name="entryBlock">The entry block.</param>
        /// <param name="visitor">The visitor instance.</param>
        /// <param name="successorProvider">The successor provider.</param>
        /// <returns>The created traversal.</returns>
        public readonly void Traverse <
            TVisitor,
            TSuccessorProvider,
            TDirection>(
            BasicBlock entryBlock,
            ref TVisitor visitor,
            TSuccessorProvider successorProvider)
            where TVisitor : struct, ITraversalVisitor
            where TSuccessorProvider : ITraversalSuccessorsProvider <TDirection>
            where TDirection : struct, IControlFlowDirection
        {
            var visited      = BasicBlockSet.Create(entryBlock);
            var stack        = new Stack <BasicBlock>(TraversalOrder.InitStackSize);
            var currentBlock = entryBlock;

            while (true)
            {
                if (visited.Add(currentBlock))
                {
                    visitor.Visit(currentBlock);
                    var successors = successorProvider.GetSuccessors(currentBlock);
                    if (successors.Length > 0)
                    {
                        for (int i = successors.Length - 1; i >= 1; --i)
                        {
                            stack.Push(successors[i]);
                        }
                        currentBlock = successors[0];
                        continue;
                    }
                }

                if (stack.Count < 1)
                {
                    break;
                }
                currentBlock = stack.Pop();
            }
        }
Exemple #7
0
 /// <summary>
 /// Constructs an abstract analysis context.
 /// </summary>
 /// <param name="onStack">The block set.</param>
 protected BaseAnalysisContext(BasicBlockSet onStack)
 {
     OnStack = onStack;
     Stack   = new Stack <BasicBlock>();
 }