KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > InstructionBlock


1 /* InstructionBlock 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: InstructionBlock.java.in,v 4.2.2.2 2002/05/28 17:34:09 hoenicke Exp $
18  */

19
20 package jode.flow;
21 import jode.type.Type;
22 import jode.decompiler.TabbedPrintWriter;
23 import jode.decompiler.LocalInfo;
24 import jode.expr.Expression;
25 import jode.expr.StoreInstruction;
26 import jode.expr.LocalStoreOperator;
27 import jode.util.SimpleSet;
28
29 import java.util.Set JavaDoc;
30
31 /**
32  * This is the structured block for atomic instructions.
33  */

34 public class InstructionBlock extends InstructionContainer {
35     /**
36      * The loads that are on the stack before cond is executed.
37      */

38     VariableStack stack;
39     /**
40      * The local to which we push to, if the instruction is non void
41      */

42     LocalInfo pushedLocal = null;
43
44     /**
45      * Tells if this expression is a initializing declaration. This
46      * can only be set to true and then never be reset. It is changed
47      * by makeDeclaration. */

48     boolean isDeclaration = false;
49
50     public InstructionBlock(Expression instr) {
51         super(instr);
52     }
53
54     public InstructionBlock(Expression instr, Jump jump) {
55         super(instr, jump);
56     }
57
58     /**
59      * This does take the instr into account and modifies stack
60      * accordingly. It then calls super.mapStackToLocal.
61      * @param stack the stack before the instruction is called
62      * @return stack the stack afterwards.
63      */

64     public VariableStack mapStackToLocal(VariableStack stack) {
65     VariableStack newStack;
66     int params = instr.getFreeOperandCount();
67     if (params > 0)
68         this.stack = stack.peek(params);
69
70     if (instr.getType() != Type.tVoid) {
71         pushedLocal = new LocalInfo();
72         pushedLocal.setType(instr.getType());
73         newStack = stack.poppush(params, pushedLocal);
74     } else if (params > 0) {
75         newStack = stack.pop(params);
76     } else
77         newStack = stack;
78     return super.mapStackToLocal(newStack);
79     }
80
81     public void removePush() {
82     if (stack != null)
83         instr = stack.mergeIntoExpression(instr);
84     if (pushedLocal != null) {
85         Expression store = new StoreInstruction
86         (new LocalStoreOperator
87          (pushedLocal.getType(), pushedLocal)).addOperand(instr);
88         instr = store;
89     }
90     super.removePush();
91     }
92
93     /**
94      * Tells if this block needs braces when used in a if or while block.
95      * @return true if this block should be sorrounded by braces.
96      */

97     public boolean needsBraces() {
98         return isDeclaration || (declare != null && !declare.isEmpty());
99     }
100
101     /**
102      * Check if this is an local store instruction to a not yet declared
103      * variable. In that case mark this as declaration and return the
104      * variable.
105      */

106     public void checkDeclaration(Set declareSet) {
107         if (instr instanceof StoreInstruction
108         && (((StoreInstruction)instr).getLValue()
109         instanceof LocalStoreOperator)) {
110         StoreInstruction storeOp = (StoreInstruction) instr;
111         LocalInfo local =
112         ((LocalStoreOperator) storeOp.getLValue()).getLocalInfo();
113             if (declareSet.contains(local)) {
114         /* Special case: This is a variable assignment, and
115          * the variable has not been declared before. We can
116          * change this to a initializing variable declaration.
117          */

118         isDeclaration = true;
119         declareSet.remove(local);
120         }
121     }
122     }
123
124     /**
125      * Make the declarations, i.e. initialize the declare variable
126      * to correct values. This will declare every variable that
127      * is marked as used, but not done.
128      * @param done The set of the already declare variables.
129      */

130     public void makeDeclaration(Set done) {
131     super.makeDeclaration(done);
132     checkDeclaration(declare);
133     }
134
135     public void dumpInstruction(TabbedPrintWriter writer)
136     throws java.io.IOException JavaDoc
137     {
138         if (isDeclaration) {
139         StoreInstruction store = (StoreInstruction) instr;
140             LocalInfo local =
141         ((LocalStoreOperator) store.getLValue()).getLocalInfo();
142         writer.startOp(writer.NO_PAREN, 0);
143         local.dumpDeclaration(writer);
144         writer.breakOp();
145         writer.print(" = ");
146         store.getSubExpressions()[1].makeInitializer(local.getType());
147         store.getSubExpressions()[1].dumpExpression(writer.IMPL_PAREN,
148                             writer);
149         writer.endOp();
150     } else {
151         try {
152
153         if (instr.getType() != Type.tVoid) {
154             writer.print("PUSH ");
155             instr.dumpExpression(writer.IMPL_PAREN, writer);
156         } else
157             instr.dumpExpression(writer.NO_PAREN, writer);
158         } catch (RuntimeException JavaDoc ex) {
159         writer.print("(RUNTIME ERROR IN EXPRESSION)");
160         }
161     }
162     writer.println(";");
163     }
164 }
165
Popular Tags