KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > ConditionalBlock


1 /* ConditionalBlock Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: ConditionalBlock.java,v 4.17.2.1 2002/05/28 17:34:08 hoenicke Exp $
18  */

19
20 package jode.flow;
21 import jode.decompiler.TabbedPrintWriter;
22 import jode.expr.Expression;
23 import jode.expr.LocalVarOperator;
24
25 /**
26  * An ConditionalBlock is the structured block representing an if
27  * instruction. The else part may be null.
28  */

29 public class ConditionalBlock extends InstructionContainer {
30     /**
31      * The loads that are on the stack before instr is executed.
32      */

33     VariableStack stack;
34
35     EmptyBlock trueBlock;
36     
37     public void checkConsistent() {
38         super.checkConsistent();
39         if (trueBlock.jump == null
40             || !(trueBlock instanceof EmptyBlock))
41             throw new jode.AssertError("Inconsistency");
42     }
43
44     /**
45      * Creates a new if conditional block.
46      */

47     public ConditionalBlock(Expression cond, Jump condJump, Jump elseJump) {
48         super(cond, elseJump);
49         /* cond is a CompareBinary or CompareUnary operator, so no
50          * check for LocalVarOperator (for condJump) is needed here.
51          */

52         trueBlock = new EmptyBlock(condJump);
53         trueBlock.outer = this;
54     }
55
56     /**
57      * Creates a new if conditional block.
58      */

59     public ConditionalBlock(Expression cond) {
60         super(cond);
61         /* cond is a CompareBinary or CompareUnary operator, so no
62          * check for LocalVarOperator (for condJump) is needed here.
63          */

64         trueBlock = new EmptyBlock();
65         trueBlock.outer = this;
66     }
67
68     /* The implementation of getNext[Flow]Block is the standard
69      * implementation
70      */

71
72     /**
73      * Returns all sub block of this structured block.
74      */

75     public StructuredBlock[] getSubBlocks() {
76         return new StructuredBlock[] { trueBlock };
77     }
78
79     /**
80      * Replaces the given sub block with a new block.
81      * @param oldBlock the old sub block.
82      * @param newBlock the new sub block.
83      * @return false, if oldBlock wasn't a direct sub block.
84      */

85     public boolean replaceSubBlock(StructuredBlock oldBlock,
86                                    StructuredBlock newBlock) {
87         throw new jode.AssertError("replaceSubBlock on ConditionalBlock");
88     }
89
90     /**
91      * This does take the instr into account and modifies stack
92      * accordingly. It then calls super.mapStackToLocal.
93      * @param stack the stack before the instruction is called
94      * @return stack the stack afterwards.
95      */

96     public VariableStack mapStackToLocal(VariableStack stack) {
97     VariableStack newStack;
98     int params = instr.getFreeOperandCount();
99     if (params > 0) {
100         this.stack = stack.peek(params);
101         newStack = stack.pop(params);
102     } else
103         newStack = stack;
104
105     trueBlock.jump.stackMap = newStack;
106     if (jump != null) {
107         jump.stackMap = newStack;
108         return null;
109     }
110     return newStack;
111     }
112
113     public void removePush() {
114     if (stack != null)
115         instr = stack.mergeIntoExpression(instr);
116     trueBlock.removePush();
117     }
118
119     /**
120      * Print the source code for this structured block.
121      */

122     public void dumpInstruction(TabbedPrintWriter writer)
123         throws java.io.IOException JavaDoc
124     {
125         writer.print("IF (");
126     instr.dumpExpression(writer.EXPL_PAREN, writer);
127     writer.println(")");
128         writer.tab();
129         trueBlock.dumpSource(writer);
130         writer.untab();
131     }
132
133     public boolean doTransformations() {
134         StructuredBlock last = flowBlock.lastModified;
135         return super.doTransformations()
136             || CombineIfGotoExpressions.transform(this, last)
137             || CreateIfThenElseOperator.createFunny(this, last);
138     }
139 }
140
141
Popular Tags