Beispiel #1
0
 private TransformToSsa(ControlFlowGraph cfg, SsaForm ssaForm)
 {
     this.cfg = cfg;
     this.ssaForm = ssaForm;
     this.writeToOriginalVariables = new List<SsaInstruction>[ssaForm.OriginalVariables.Count];
     this.addressTaken = new bool[ssaForm.OriginalVariables.Count];
 }
Beispiel #2
0
        private SsaFormBuilder(MethodDefinition method, ControlFlowGraph cfg)
        {
            this.method = method;
            this.cfg = cfg;

            this.blocks = new SsaBlock[cfg.Nodes.Count];
            this.stackSizeAtBlockStart = new int[cfg.Nodes.Count];
            for (int i = 0; i < stackSizeAtBlockStart.Length; i++) {
                stackSizeAtBlockStart[i] = -1;
            }
            stackSizeAtBlockStart[cfg.EntryPoint.BlockIndex] = 0;

            this.parameters = new SsaVariable[method.Parameters.Count + (method.HasThis ? 1 : 0)];
            if (method.HasThis)
                parameters[0] = new SsaVariable(method.Body.ThisParameter);
            for (int i = 0; i < method.Parameters.Count; i++)
                parameters[i + (method.HasThis ? 1 : 0)] = new SsaVariable(method.Parameters[i]);

            this.locals = new SsaVariable[method.Body.Variables.Count];
            for (int i = 0; i < locals.Length; i++)
                locals[i] = new SsaVariable(method.Body.Variables[i]);

            this.stackLocations = new SsaVariable[method.Body.MaxStackSize];
            for (int i = 0; i < stackLocations.Length; i++) {
                stackLocations[i] = new SsaVariable(i);
            }
        }
Beispiel #3
0
 public static void Transform(ControlFlowGraph cfg, SsaForm ssa, bool optimize = true)
 {
     TransformToSsa transform = new TransformToSsa(cfg, ssa);
     transform.ConvertVariablesToSsa();
     SsaOptimization.RemoveDeadAssignments(ssa); // required so that 'MakeByRefCallsSimple' can detect more cases
     if (SimplifyByRefCalls.MakeByRefCallsSimple(ssa)) {
         transform.ConvertVariablesToSsa();
     }
     if (optimize)
         SsaOptimization.Optimize(ssa);
 }
 // Loop detection works like this:
 // We find a top-level loop by looking for its entry point, which is characterized by a node dominating its own predecessor.
 // Then we determine all other nodes that belong to such a loop (all nodes which lead to the entry point, and are dominated by it).
 // Finally, we check whether our result conforms with potential existing exception structures, and create the substructure for the loop if successful.
 // This algorithm is applied recursively for any substructures (both detected loops and exception blocks)
 // But maybe we should get rid of this complex stuff and instead treat every backward jump as a loop?
 // That should still work with the IL produced by compilers, and has the advantage that the detected loop bodies are consecutive IL regions.
 static void DetectLoops(ControlFlowGraph g, ControlStructure current, CancellationToken cancellationToken)
 {
     if (!current.EntryPoint.IsReachable)
         return;
     g.ResetVisited();
     cancellationToken.ThrowIfCancellationRequested();
     FindLoops(current, current.EntryPoint);
     foreach (ControlStructure loop in current.Children)
         DetectLoops(g, loop, cancellationToken);
 }
        static void DetectExceptionHandling(ControlStructure current, ControlFlowGraph g, IEnumerable<ExceptionHandler> exceptionHandlers)
        {
            // We rely on the fact that the exception handlers are sorted so that the innermost come first.
            // For each exception handler, we determine the nodes and substructures inside that handler, and move them into a new substructure.
            // This is always possible because exception handlers are guaranteed (by the CLR spec) to be properly nested and non-overlapping;
            // so they directly form the tree that we need.
            foreach (ExceptionHandler eh in exceptionHandlers) {
                var tryNodes = FindNodes(current, eh.TryStart, eh.TryEnd);
                current.Nodes.ExceptWith(tryNodes);
                ControlStructure tryBlock = new ControlStructure(
                    tryNodes,
                    g.Nodes.Single(n => n.Start == eh.TryStart),
                    ControlStructureType.Try);
                tryBlock.ExceptionHandler = eh;
                MoveControlStructures(current, tryBlock, eh.TryStart, eh.TryEnd);
                current.Children.Add(tryBlock);

                if (eh.FilterStart != null) {
                    throw new NotSupportedException();
                }

                var handlerNodes = FindNodes(current, eh.HandlerStart, eh.HandlerEnd);
                var handlerNode = current.Nodes.Single(n => n.ExceptionHandler == eh);
                handlerNodes.Add(handlerNode);
                if (handlerNode.EndFinallyOrFaultNode != null)
                    handlerNodes.Add(handlerNode.EndFinallyOrFaultNode);
                current.Nodes.ExceptWith(handlerNodes);
                ControlStructure handlerBlock = new ControlStructure(
                    handlerNodes, handlerNode, ControlStructureType.Handler);
                handlerBlock.ExceptionHandler = eh;
                MoveControlStructures(current, handlerBlock, eh.HandlerStart, eh.HandlerEnd);
                current.Children.Add(handlerBlock);
            }
        }
 public static ControlStructure DetectStructure(ControlFlowGraph g, IEnumerable<ExceptionHandler> exceptionHandlers, CancellationToken cancellationToken)
 {
     ControlStructure root = new ControlStructure(new HashSet<ControlFlowNode>(g.Nodes), g.EntryPoint, ControlStructureType.Root);
     // First build a structure tree out of the exception table
     DetectExceptionHandling(root, g, exceptionHandlers);
     // Then run the loop detection.
     DetectLoops(g, root, cancellationToken);
     return root;
 }
Beispiel #7
0
 public static SsaForm BuildRegisterIL(MethodDefinition method, ControlFlowGraph cfg)
 {
     if (method == null)
         throw new ArgumentNullException("method");
     if (cfg == null)
         throw new ArgumentNullException("cfg");
     return new SsaFormBuilder(method, cfg).Build();
 }