001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997 Clark Verbrugge
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.coffi;
027:
028: import java.util.*;
029:
030: import soot.jimple.Stmt;
031: import soot.util.*;
032: import soot.*;
033:
034: /** Represents one basic block in a control flow graph.
035: * @see CFG
036: * @see ClassFile#parse
037: * @author Clark Verbrugge
038: */
039: class BasicBlock {
040: /** Number of instructions in this block. */
041: public int size;
042: /** Head of the list of instructions. */
043: public Instruction head;
044: /** Tail of the list of instructions.
045: * <p>
046: * Normally, the last instruction will have a next pointer with value
047: * <i>null</i>. After a Instruction sequences are reconstructed though,
048: * the instruction lists
049: * are rejoined in order, and so the tail instruction will not
050: * have a <i>null</i> next pointer.
051: * @see CFG#reconstructInstructions
052: */
053: public Instruction tail;
054: /** Vector of predecessor BasicBlocks.
055: * @see java.util.Vector
056: */
057: public Vector<BasicBlock> succ;
058: /** Vector of successor BasicBlocks.
059: * @see java.util.Vector
060: */
061: public Vector<BasicBlock> pred;
062:
063: public boolean inq;
064: /** Flag for whether starting an exception or not. */
065: public boolean beginException;
066: /** Flag for whether starting main code block or not. */
067: public boolean beginCode;
068: /** Flag for semantic stack analysis fixup pass.
069: * @see CFG#jimplify
070: */
071:
072: boolean done;
073:
074: /** Next BasicBlock in the CFG, in the parse order. */
075: public BasicBlock next;
076: /** Unique (among basic blocks) id. */
077: public long id; // unique id
078:
079: List<Stmt> statements;
080: Set addressesToFixup = new ArraySet();
081:
082: soot.jimple.Stmt getHeadJStmt() {
083: return statements.get(0);
084: }
085:
086: soot.jimple.Stmt getTailJStmt() {
087: return statements.get(statements.size() - 1);
088: }
089:
090: public BasicBlock(Instruction insts) {
091: id = G.v().coffi_BasicBlock_ids++;
092: head = insts;
093: tail = head;
094: size = 0;
095: if (head != null) {
096: size++;
097: while (tail.next != null) {
098: size++;
099: tail = tail.next;
100: }
101: }
102: succ = new Vector<BasicBlock>(2, 10);
103: pred = new Vector<BasicBlock>(2, 3);
104: }
105:
106: public BasicBlock(Instruction headinsn, Instruction tailinsn) {
107: id = G.v().coffi_BasicBlock_ids++;
108: head = headinsn;
109: tail = tailinsn;
110: succ = new Vector<BasicBlock>(2, 10);
111: pred = new Vector<BasicBlock>(2, 3);
112: }
113:
114: /** Computes a hash code for this block from the label of the
115: * first instruction in its contents.
116: * @return the hash code.
117: * @see Instruction#label
118: */
119: public int hashCode() {
120: return (new Integer(head.label)).hashCode();
121: }
122:
123: /** True if this block represents the same piece of code. Basically
124: * compares labels of the head instructions.
125: * @param b block to compare against.
126: * @return <i>true</i> if they do, <i>false</i> if they don't.
127: */
128: public boolean equals(BasicBlock b) {
129: return (this == b);
130: }
131:
132: /** For printing the string "BB: " + id.
133: */
134: public String toString() {
135: return "BB: " + id;
136: }
137:
138: }
|