All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.royale.abc.semantics.ControlFlowGraph Maven / Gradle / Ivy

There is a newer version: 0.9.10
Show newest version
/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.apache.royale.abc.semantics;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.royale.abc.ABCConstants;
import org.apache.royale.abc.graph.IBasicBlock;
import org.apache.royale.abc.graph.IFlowgraph;
import org.apache.royale.abc.graph.algorithms.DepthFirstPreorderIterator;
import org.apache.royale.abc.graph.algorithms.DominatorTree;
import org.apache.royale.abc.visitors.IFlowGraphVisitor;

/**
 * A ControlFlowGraph represents the flow of control through a sequence of
 * instructions. The instructions are organized into a set of Blocks --
 * sequences of instructions where normal control flow proceeds linearly from
 * one instruction to the next -- and a set of edges, representing the
 * discontinuous transfer of control points.

 */
public class ControlFlowGraph implements IFlowgraph
{
    /**
     * Search direction in the initial block of a block-by-block search
     * (typically for debug information).
     */
    private enum SearchDirection { Forward, Backward };

    /**
     * Create a ControlFlowGraph from the instructions in the MethodBodyInfo.
     * @param mbi  - the MethodBodyInfo of the method whose control flow graph is to be computed.
     */
    ControlFlowGraph(MethodBodyInfo mbi)
    {
        this.mbi = mbi;
        this.startBlock = newBlock();
        buildCfg();
    }

    /**
     *  This ControlFlowGraph's MethodBodyInfo.
     */
    private final MethodBodyInfo mbi;

    /**
     *  Blocks of the flow graph in entry order.
     */
    private ArrayList blocks = new ArrayList();

    /**
     * The entry point of this routine; known in
     * graph theory as the start block.
     */
    private Block startBlock;

    /**
     *  Blocks keyed by their Labels.  Multiple Labels may map to a Block.
     */
    private Map blocksByLabel = new HashMap();

    /**
     *  Catch targets defined in this method.
     */
    private ArrayList catchTargets = new ArrayList();

    /**
     *  The graph's dominator tree, built on demand.
     */
    private DominatorTree dominatorTree = null;

    /**
     * Build the CFG.
     */
    private void buildCfg()
    {
        Block current_block = startBlock;

        boolean last_block_transferred_control = false;

        //  Sort the labels by position.
        Collections.sort(mbi.getInstructionList().getActiveLabels());
        Iterator




© 2015 - 2024 Weber Informatics LLC | Privacy Policy