KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > BasicBlock


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31
32 package soot.coffi;
33
34 import java.util.Enumeration JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.*;
37 import soot.util.*;
38 import soot.*;
39 import java.util.*;
40
41 /** Represents one basic block in a control flow graph.
42  * @see CFG
43  * @see ClassFile#parse
44  * @author Clark Verbrugge
45  */

46 class BasicBlock {
47    /** Number of instructions in this block. */
48    public int size;
49    /** Head of the list of instructions. */
50    public Instruction head;
51    /** Tail of the list of instructions.
52     * <p>
53     * Normally, the last instruction will have a next pointer with value
54     * <i>null</i>. After a Instruction sequences are reconstructed though,
55     * the instruction lists
56     * are rejoined in order, and so the tail instruction will not
57     * have a <i>null</i> next pointer.
58     * @see CFG#reconstructInstructions
59     */

60    public Instruction tail;
61    /** Vector of predecessor BasicBlocks.
62     * @see java.util.Vector
63     */

64    public Vector JavaDoc succ;
65    /** Vector of successor BasicBlocks.
66     * @see java.util.Vector
67     */

68    public Vector JavaDoc pred;
69
70    public boolean inq;
71    /** Flag for whether starting an exception or not. */
72    public boolean beginException;
73    /** Flag for whether starting main code block or not. */
74    public boolean beginCode;
75    /** Flag for semantic stack analysis fixup pass.
76     * @see CFG#jimplify
77     */

78
79    boolean done;
80
81    /** Next BasicBlock in the CFG, in the parse order. */
82    public BasicBlock next;
83    /** Unique (among basic blocks) id. */
84    public long id; // unique id
85

86    private short wide; // convert indices when parsing jimple
87

88    /** Constructs a BasicBlock consisting of the given list of Instructions.
89     * @param insts list of instructions composing this basic block.
90     */

91
92    private soot.jimple.Stmt stmt; // statement generated
93

94    List statements;
95    Set addressesToFixup = new ArraySet();
96
97    soot.jimple.Stmt getHeadJStmt()
98    {
99       return (soot.jimple.Stmt) statements.get(0);
100    }
101
102    soot.jimple.Stmt getTailJStmt()
103    {
104       return (soot.jimple.Stmt) statements.get(statements.size() - 1);
105    }
106
107    public BasicBlock(Instruction insts) {
108       id = G.v().coffi_BasicBlock_ids++;
109       head = insts;
110       tail = head;
111       size = 0;
112       if (head!=null) {
113          size++;
114          while (tail.next!=null) {
115             size++;
116             tail = tail.next;
117          }
118       }
119       succ = new Vector JavaDoc(2,10);
120       pred = new Vector JavaDoc(2,3);
121    }
122
123     public BasicBlock(Instruction headinsn, Instruction tailinsn)
124     {
125     id = G.v().coffi_BasicBlock_ids++;
126     head = headinsn;
127     tail = tailinsn;
128     succ = new Vector JavaDoc(2,10);
129     pred = new Vector JavaDoc(2,3);
130     }
131
132    /** Computes a hash code for this block from the label of the
133     * first instruction in its contents.
134     * @return the hash code.
135     * @see Instruction#label
136     */

137    public int hashCode() {
138       return (new Integer JavaDoc(head.label)).hashCode();
139    }
140
141    /** True if this block represents the same piece of code. Basically
142     * compares labels of the head instructions.
143     * @param b block to compare against.
144     * @return <i>true</i> if they do, <i>false</i> if they don't.
145     */

146    public boolean equals(BasicBlock b) {
147       return (this == b);
148    }
149
150    /** For printing the string "BB: " + id.
151     */

152    public String JavaDoc toString() { return "BB: " + id; }
153
154    // Returns the index of b given the current wide
155
// wide to 0
156
private int wideIndex(short b) {
157       int i = ((((int)wide)<<8)&0xff00) | (((int)b)&0xff);
158       wide = (byte)0;
159       return i;
160    }
161
162 }
163
Popular Tags