예제 #1
0
 public void RemoveFromSuccessors(InstructionBlock toBeRemoved)
 {
     V_0 = new InstructionBlock[(int)this.get_Successors().Length - 1];
     V_1 = 0;
     V_2 = 0;
     while (V_1 < (int)this.get_Successors().Length)
     {
         if (InstructionBlock.op_Inequality(this.get_Successors()[V_1], toBeRemoved))
         {
             stackVariable24      = V_2;
             V_2                  = stackVariable24 + 1;
             V_0[stackVariable24] = this.get_Successors()[V_1];
         }
         V_1 = V_1 + 1;
     }
     this.set_Successors(V_0);
     return;
 }
        private void MarkBlockEnds(Collection <Instruction> instructions)
        {
            InstructionBlock[] blocks = ToArray();
            if (blocks.Length <= 0)
            {
                return;
            }

            InstructionBlock current = blocks[0];

            for (int i = 1; i < blocks.Length; ++i)
            {
                var block = blocks[i];
                current.Last = block.First.Previous;
                current      = block;
            }

            current.Last = instructions[instructions.Count - 1];
        }
예제 #3
0
        protected internal static IEnumerable <InstructionBlock> GetChildTree(InstructionBlock block, HashSet <int> visited)
        {
            visited.Add(block.Index);
            yield return(block);

            var successors = block.Successors;

            if (successors.Length == 0)
            {
                yield break;
            }

            for (int i = 0; i < successors.Length; i++)
            {
                if ((successors[i].Index > block.Index) && (!visited.Contains(successors[i].Index)))
                {
                    foreach (var nested in GetChildTree(successors[i], visited))
                    {
                        yield return(nested);
                    }
                }
            }
        }
예제 #4
0
        private InstructionBlock FindMostDistantSuccessor(InstructionBlock currentBlock)
        {
            InstructionBlock mostDistantSuccessor = null;

            foreach (InstructionBlock successor in currentBlock.Successors)
            {
                if (mostDistantSuccessor == null)
                {
                    if (successor.Index > currentBlock.Index)
                    {
                        mostDistantSuccessor = successor;
                    }
                }
                else
                {
                    if (successor.Index > mostDistantSuccessor.Index)
                    {
                        mostDistantSuccessor = successor;
                    }
                }
            }
            return(mostDistantSuccessor);
        }
예제 #5
0
 public BlockRange(InstructionBlock start, InstructionBlock end)
 {
     Start = start;
     End   = end;
 }
예제 #6
0
 public int GetHashCode(InstructionBlock obj)
 {
     return(first.Offset.GetHashCode());
 }
예제 #7
0
 public int CompareTo(InstructionBlock block)
 {
     return(first.Offset - block.First.Offset);
 }
 private void RegisterBlock(InstructionBlock block)
 {
     blocks.Add(block.First.Offset, block);
 }
        private void ConnectBlock(InstructionBlock block)
        {
            if (block.Last == null)
            {
                throw new ArgumentException("Undelimited block at offset " + block.First.Offset);
            }

            Instruction instruction = block.Last;

            switch (instruction.OpCode.FlowControl)
            {
            case FlowControl.Branch:
            case FlowControl.Cond_Branch:
            {
                if (HasMultipleBranches(instruction))
                {
                    InstructionBlock[] blocks      = GetBranchTargetsBlocks(instruction);
                    InstructionBlock   defaultExit = null;
                    if (instruction.Next != null)
                    {
                        defaultExit = GetBlock(instruction.Next);
                    }

                    switchBlocksInformation.Add(block, new SwitchData(block, defaultExit, blocks));

                    if (defaultExit != null)
                    {
                        blocks = AddBlock(defaultExit, blocks);
                    }

                    block.Successors = blocks;
                    break;
                }

                InstructionBlock target = GetBranchTargetBlock(instruction);
                if (instruction.OpCode.FlowControl == FlowControl.Cond_Branch && instruction.Next != null)
                {
                    block.Successors = new[] { target, GetBlock(instruction.Next) };
                }
                else
                {
                    block.Successors = new[] { target };
                }
                break;
            }

            case FlowControl.Call:
            case FlowControl.Next:
                if (null != instruction.Next)
                {
                    block.Successors = new[] { GetBlock(instruction.Next) };
                }
                break;

            case FlowControl.Return:
            case FlowControl.Throw:
                break;

            default:
                throw new NotSupportedException(
                          string.Format("Unhandled instruction flow behavior {0}: {1}",
                                        instruction.OpCode.FlowControl,
                                        Formatter.FormatInstruction(instruction)));
            }
        }
예제 #10
0
        private void MarkPartitioningVertices(int startInstructionBlockIndex)
        {
            InstructionBlock currentBlock = cfg.Blocks[startInstructionBlockIndex];

            InstructionBlock mostDistantSuccessor = FindMostDistantSuccessor(currentBlock);

            if (mostDistantSuccessor == null)
            {
                return;
            }

            if (mostDistantSuccessor.Index == cfg.Blocks.Count() - 1)
            {
                return;
            }

            InstructionBlock mostDistantSuccessorCandidate = null;

            for (int index = currentBlock.Index + 1; index <= mostDistantSuccessor.Index; index++)
            {
                if (mostDistantSuccessorCandidate == null)
                {
                    mostDistantSuccessorCandidate = FindMostDistantSuccessor(cfg.Blocks[index]);
                }
                else
                {
                    InstructionBlock successor = FindMostDistantSuccessor(cfg.Blocks[index]);
                    if (successor.Index > mostDistantSuccessorCandidate.Index)
                    {
                        mostDistantSuccessorCandidate = successor;
                    }
                }
            }


            if (mostDistantSuccessor.Index >= mostDistantInstructionBlockIndexReached)
            {
                if (mostDistantSuccessorCandidate != null)
                {
                    if (mostDistantSuccessorCandidate.Index <= mostDistantSuccessor.Index)
                    {
                        verticesThatPartitionGraph.Add(mostDistantSuccessor);
                    }
                }
                else
                {
                    verticesThatPartitionGraph.Add(mostDistantSuccessor);
                }
            }
            else
            {
                if (mostDistantSuccessorCandidate != null)
                {
                    if (mostDistantSuccessorCandidate.Index > mostDistantInstructionBlockIndexReached)
                    {
                        mostDistantInstructionBlockIndexReached = (uint)mostDistantSuccessorCandidate.Index;
                    }
                }
            }

            MarkPartitioningVertices(mostDistantSuccessor.Index);
        }
예제 #11
0
 public int GetHashCode(InstructionBlock obj)
 {
     return(this.first.get_Offset().GetHashCode());
 }
예제 #12
0
 public int CompareTo(InstructionBlock block)
 {
     return(this.first.get_Offset() - block.get_First().get_Offset());
 }
예제 #13
0
        internal void RemoveBlockAt(int index)
        {
            InstructionBlock removedBlock = Blocks[index];

            Instruction nextInstruction = removedBlock.Last.Next;

            if (removedBlock.Predecessors.Count > 0)
            {
                throw new Exception("The block to be removed cannot have predecessors");
            }

            InstructionBlock[] oldBlocks = Blocks;
            Blocks = new InstructionBlock[Blocks.Length - 1];

            for (int i = 0; i < Blocks.Length; i++)
            {
                int passedIndex = i >= index ? 1 : 0;
                Blocks[i]       = oldBlocks[i + passedIndex];
                Blocks[i].Index = i;
                Blocks[i].Predecessors.Remove(removedBlock);

                //if (i == 0)
                //{
                //    Blocks[i].First.Previous = null;
                //}
                //else
                //{
                if (Blocks[i].First.Previous == removedBlock.Last)
                {
                    Blocks[i].First.Previous = removedBlock.First.Previous;
                }
                if (Blocks[i].Last.Next == removedBlock.First)
                {
                    Blocks[i].Last.Next = removedBlock.Last.Next;
                }
                //}
            }

            InstructionToBlockMapping.Remove(removedBlock.First.Offset);
            SwitchBlocksInformation.Remove(removedBlock);
            removedBlock.Successors = new InstructionBlock[0];

            foreach (Instruction instruction in removedBlock)
            {
                OffsetToInstruction.Remove(instruction.Offset);
            }

            foreach (ExceptionHandler exceptionHandler in MethodBody.ExceptionHandlers)
            {
                if (exceptionHandler.TryStart == removedBlock.First)
                {
                    exceptionHandler.TryStart = nextInstruction;
                }
                if (exceptionHandler.TryEnd == removedBlock.First)
                {
                    exceptionHandler.TryEnd = nextInstruction;
                }
                if (exceptionHandler.HandlerStart == removedBlock.First)
                {
                    exceptionHandler.HandlerStart = nextInstruction;
                }
                if (exceptionHandler.HandlerEnd == removedBlock.First)
                {
                    exceptionHandler.HandlerEnd = nextInstruction;
                }
                if (exceptionHandler.FilterStart == removedBlock.First)
                {
                    exceptionHandler.FilterStart = nextInstruction;
                }
                if (exceptionHandler.FilterEnd == removedBlock.First)
                {
                    exceptionHandler.FilterEnd = nextInstruction;
                }
            }

            removedBlock.Index = -1;
        }