1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
* Fernflower - The Analytical Java Decompiler
* http://www.reversed-java.com
*
* (C) 2008 - 2010, Stiver
*
* This software is NEITHER public domain NOR free software
* as per GNU License. See license.txt for more details.
*
* This software is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE.
*/
package de.fernflower.modules.decompiler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import de.fernflower.modules.decompiler.stats.DoStatement;
import de.fernflower.modules.decompiler.stats.IfStatement;
import de.fernflower.modules.decompiler.stats.SequenceStatement;
import de.fernflower.modules.decompiler.stats.Statement;
public class LoopExtractHelper {
public static boolean extractLoops(Statement root) {
boolean res = (extractLoopsRec(root) != 0);
if(res) {
SequenceHelper.condenseSequences(root);
}
return res;
}
private static int extractLoopsRec(Statement stat) {
boolean res = false;
for(;;) {
boolean updated = false;
for(Statement st: stat.getStats()) {
int extr = extractLoopsRec(st);
res |= (extr != 0);
if(extr == 2) {
updated = true;
break;
}
}
if(!updated) {
break;
}
}
if(stat.type == Statement.TYPE_DO) {
if(extractLoop((DoStatement)stat)) {
return 2;
}
}
return res?1:0;
}
private static boolean extractLoop(DoStatement stat) {
if(stat.getLooptype() != DoStatement.LOOP_DO) {
return false;
}
for(StatEdge edge: stat.getLabelEdges()) {
if(edge.getType() != StatEdge.TYPE_CONTINUE && edge.getDestination().type != Statement.TYPE_DUMMYEXIT) {
return false;
}
}
if(!extractLastIf(stat)) {
return extractFirstIf(stat);
} else {
return true;
}
}
private static boolean extractLastIf(DoStatement stat) {
// search for an if condition at the end of the loop
Statement last = stat.getFirst();
while(last.type == Statement.TYPE_SEQUENCE) {
last = last.getStats().getLast();
}
if(last.type == Statement.TYPE_IF) {
IfStatement lastif = (IfStatement)last;
if(lastif.iftype == IfStatement.IFTYPE_IF && lastif.getIfstat() != null) {
Statement ifstat = lastif.getIfstat();
StatEdge elseedge = lastif.getAllSuccessorEdges().get(0);
if(elseedge.getType() == StatEdge.TYPE_CONTINUE && elseedge.closure == stat) {
Set<Statement> set = stat.getNeighboursSet(StatEdge.TYPE_CONTINUE, Statement.DIRECTION_BACKWARD);
set.remove(last);
if(set.isEmpty()) { // no direct continues in a do{}while loop
if(isExternStatement(stat, ifstat, ifstat)) {
extractIfBlock(stat, lastif);
return true;
}
}
}
}
}
return false;
}
private static boolean extractFirstIf(DoStatement stat) {
// search for an if condition at the entrance of the loop
Statement first = stat.getFirst();
while(first.type == Statement.TYPE_SEQUENCE) {
first = first.getFirst();
}
// found an if statement
if(first.type == Statement.TYPE_IF) {
IfStatement firstif = (IfStatement)first;
if(firstif.getFirst().getExprents().isEmpty()) {
if(firstif.iftype == IfStatement.IFTYPE_IF && firstif.getIfstat()!=null) {
Statement ifstat = firstif.getIfstat();
if(isExternStatement(stat, ifstat, ifstat)) {
extractIfBlock(stat, firstif);
return true;
}
}
}
}
return false;
}
private static boolean isExternStatement(DoStatement loop, Statement block, Statement stat) {
for(StatEdge edge: stat.getAllSuccessorEdges()) {
if(loop.containsStatement(edge.getDestination()) &&
!block.containsStatement(edge.getDestination())) {
return false;
}
}
for(Statement st: stat.getStats()) {
if(!isExternStatement(loop, block, st)) {
return false;
}
}
return true;
}
private static void extractIfBlock(DoStatement loop, IfStatement ifstat) {
Statement target = ifstat.getIfstat();
StatEdge ifedge = ifstat.getIfEdge();
ifstat.setIfstat(null);
ifedge.getSource().changeEdgeType(Statement.DIRECTION_FORWARD, ifedge, StatEdge.TYPE_BREAK);
ifedge.closure = loop;
ifstat.getStats().removeWithKey(target.id);
loop.addLabeledEdge(ifedge);
SequenceStatement block = new SequenceStatement(Arrays.asList(new Statement[] {loop, target}));
loop.getParent().replaceStatement(loop, block);
block.setAllParent();
loop.addSuccessor(new StatEdge(StatEdge.TYPE_REGULAR, loop, target));
for(StatEdge edge: new ArrayList<StatEdge>(block.getLabelEdges())) {
if(edge.getType() == StatEdge.TYPE_CONTINUE || edge == ifedge) {
loop.addLabeledEdge(edge);
}
}
for(StatEdge edge: block.getPredecessorEdges(StatEdge.TYPE_CONTINUE)) {
if(loop.containsStatementStrict(edge.getSource())) {
block.removePredecessor(edge);
edge.getSource().changeEdgeNode(Statement.DIRECTION_FORWARD, edge, loop);
loop.addPredecessor(edge);
}
}
}
}
|