KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Flow


1 /* Flow Copyright (C) 1999 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU 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 General Public License
14  * along with this program; see the file COPYING. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: Flow.java,v 1.3 1999/08/19 15:16:59 jochen Exp $
18  */

19
20
21 public abstract class Flow {
22     int g;
23     int[] ain;
24
25     void skip() {
26         for (int i=0; i<3; i++) {
27             if (g > 5) {
28                 for (int j=0; j<5; j++) {
29                     g++;
30                 }
31             }
32             i--;
33         }
34     }
35
36     /* This tests the while in a switch problem.
37      */

38     public void switchWhileTest() {
39         int dir = g;
40         int x = 0;
41         int y = 0;
42         boolean done = false;
43         g = 5;
44         switch (dir) {
45         case 1:
46             while (!done) {
47                 done = true;
48         if (g > 7)
49             g = g - 4;
50                 x = g;
51                 y = g;
52                 if (x > 7)
53                     x = x - 4;
54         for (int i=0; i<4; i++) {
55             for (int j=0; j<5; j++) {
56                         if (ain[j] == x + i && ain[j] == y)
57                             done = false;
58                     }
59         }
60             }
61         for (int i=0; i<5; i++) {
62                 ain[g] = x + i;
63                 ain[g] = y;
64                 g += 1;
65             }
66             break;
67         case 2:
68             while (!done) {
69                 done = true;
70                 x = g;
71                 y = g;
72                 if (y > 7)
73                     y = y - 4;
74         for (int i=0; i<4; i++) {
75             for (int j=0; j<4; j++) {
76                         if (ain[j] == x && ain[j] == y + i)
77                             done = false;
78                     }
79                 }
80             }
81         for (int i = 0; i<4; i++) {
82                 ain[g] = x;
83                 ain[g] = y + i;
84                 g += 1;
85             }
86             break;
87     case 3: // Same code as case 2 slightly optimized
88
big:
89             for (;;) {
90                 x = g;
91                 y = g;
92                 if (y > 7)
93                     y = y - 4;
94         for (int i=0; i<4; i++) {
95             for (int j=0; j<4; j++) {
96                         if (ain[j] == x && ain[j] == y + i)
97                             continue big;
98                     }
99                 }
100         break;
101             }
102         for (int i = 0; i<4; i++) {
103                 ain[g] = x;
104                 ain[g] = y + i;
105                 g += 1;
106             }
107             break;
108         }
109     }
110
111     /**
112      * This was an example where our flow analysis didn't find an
113      * elegant solution. The reason is, that we try to make
114      * while(true)-loops as small as possible (you can't see the real
115      * end of the loop, if it is breaked there like here).
116      *
117      * Look at the assembler code and you know why my Decompiler had
118      * problems with this. But the decompiler did produce compilable
119      * code which produces the same assembler code.
120      *
121      * The solution was, to make switches as big as possible, the whole
122      * analyze methods were overworked.
123      */

124     void WhileTrueSwitch() {
125         int i = 1;
126         while (true) {
127             switch (i) {
128             case 0:
129                 return;
130             case 1:
131                 i = 5;
132                 continue;
133             case 2:
134                 i = 6;
135                 continue;
136             case 3:
137                 throw new RuntimeException JavaDoc();
138             default:
139                 i = 7;
140         return;
141             }
142         }
143     }
144
145     abstract int test();
146
147     /**
148      * This tests shorts and empty ifs. Especially the no op ifs can
149      * be optimized to very unusual code.
150      */

151     public void shortIf() {
152     while(g != 7) {
153         if (g == 5)
154         return;
155         else if (g != 4)
156         break;
157         else if (g == 2)
158         shortIf();
159         else
160         return;
161
162         if (g!= 7)
163         shortIf();
164         else {
165         shortIf();
166         return;
167         }
168
169         if (g != 1)
170         break;
171         else if (g == 3)
172         shortIf();
173         else
174         break;
175
176         // javac optimizes this instruction to
177
// test();
178
// jikes reproduces this statement as one would expect
179
if (g + 5 == test()) {
180         }
181
182         // javac -O optimizes this to the following weired statements
183
// PUSH g;
184
// PUSH test();
185
// POP2;
186
// This cannot be decompiled correctly, since the == is lost.
187
if (g == test())
188         continue;
189     }
190     while(g == 3) {
191         // javac:
192
// PUSH test() == 4 || test() == 3 && test() == 2;
193
// POP;
194
if (test() == 4 || test() == 3 && test() == 2);
195         // javac -O:
196
// if (test() != 4 && test() == 3) {
197
// PUSH test()+test() - test();
198
// PUSH g-4;
199
// POP2;
200
// }
201
if (test() == 4 || test() == 3 && test() == 2)
202         continue;
203     }
204     while (g==2) {
205         // javac:
206
// test();
207
// test();
208
// test();
209
if ((long) (test() + test() - test()) == (long)(g-4));
210         // javac -O:
211
// PUSH (long)(test() + test() - test()) <=> (long)(g-4)
212
// POP;
213
if ((long) (test() + test() - test()) == (long)(g-4))
214         continue;
215     }
216     System.err.println("Hallo");
217     }
218 }
219
Popular Tags