KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > CreateExpression


1 /* CreateExpression 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: CreateExpression.java,v 4.17.4.1 2002/05/28 17:34:08 hoenicke Exp $
18  */

19
20 package jode.flow;
21 import jode.GlobalOptions;
22 import jode.expr.*;
23
24 /**
25  * This transformation creates expressions. It transforms
26  * <pre>
27  * Sequ[expr_1, Sequ[expr_2, ..., Sequ[expr_n, op] ...]]
28  * </pre>
29  * to
30  * <pre>
31  * expr(op, [ expr_1, ..., expr_n ])
32  * </pre>
33  */

34 public class CreateExpression {
35
36     /**
37      * This does the transformation.
38      * @param FlowBlock the flow block to transform.
39      * @return true if flow block was simplified.
40      */

41     public static boolean transform(InstructionContainer ic,
42                                     StructuredBlock last) {
43         int params = ic.getInstruction().getFreeOperandCount();
44         if (params == 0)
45             return false;
46
47         if (!(last.outer instanceof SequentialBlock))
48             return false;
49         SequentialBlock sequBlock = (SequentialBlock)last.outer;
50
51         /* First check if Expression can be created, but do nothing yet.
52          */

53         Expression lastExpression = ic.getInstruction();
54     while (true) {
55         if (!(sequBlock.subBlocks[0] instanceof InstructionBlock))
56         return false;
57         
58         Expression expr =
59         ((InstructionBlock) sequBlock.subBlocks[0]).getInstruction();
60
61         if (!expr.isVoid())
62         break;
63
64         if (expr.getFreeOperandCount() > 0
65         || !(expr instanceof CombineableOperator)
66         || lastExpression.canCombine((CombineableOperator) expr) <= 0)
67         return false;
68
69         /* Hmm, we should really set lastExpression to
70          * lastExpression.combine(expr), but that may change the
71          * expressions :-( XXX
72          *
73          * We do a conservative approach and check if there are
74          * no possible side effects with the skipped expressions.
75          * Theoretically we would only have to check expressions,
76          * that are combined at an earlier point.
77          */

78         SequentialBlock block = sequBlock;
79         while (block != last.outer) {
80         block = (SequentialBlock) block.subBlocks[1];
81         if (((InstructionBlock)block.subBlocks[0])
82             .getInstruction().hasSideEffects(expr))
83             return false;
84         }
85
86
87         if (!(sequBlock.outer instanceof SequentialBlock))
88         return false;
89             sequBlock = (SequentialBlock) sequBlock.outer;
90     }
91         
92         /* Now, do the combination. Everything must succeed now.
93          */

94         sequBlock = (SequentialBlock) last.outer;
95     lastExpression = ic.getInstruction();
96     while (true) {
97
98         Expression expr =
99         ((InstructionBlock) sequBlock.subBlocks[0]).getInstruction();
100         
101         if (!expr.isVoid()) {
102         lastExpression = lastExpression.addOperand(expr);
103         break;
104         }
105
106         lastExpression = lastExpression.combine
107         ((CombineableOperator) expr);
108             sequBlock = (SequentialBlock)sequBlock.outer;
109         }
110
111         if (GlobalOptions.verboseLevel > 0
112         && lastExpression.getFreeOperandCount() == 0)
113             GlobalOptions.err.print('x');
114
115     ic.setInstruction(lastExpression);
116         ic.moveDefinitions(sequBlock, last);
117         last.replace(sequBlock);
118         return true;
119     }
120 }
121
Popular Tags