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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* 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 org.jetbrains.java.decompiler.modules.decompiler.vars;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.modules.decompiler.exps.AssignmentExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.ConstExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.Exprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.FunctionExprent;
import org.jetbrains.java.decompiler.modules.decompiler.exps.VarExprent;
import org.jetbrains.java.decompiler.modules.decompiler.sforms.DirectGraph;
import org.jetbrains.java.decompiler.modules.decompiler.stats.CatchAllStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.CatchStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
import org.jetbrains.java.decompiler.struct.StructClass;
import org.jetbrains.java.decompiler.struct.StructMethod;
import org.jetbrains.java.decompiler.struct.gen.MethodDescriptor;
import org.jetbrains.java.decompiler.struct.gen.VarType;
public class VarTypeProcessor {
public static final int VAR_NONFINAL = 1;
public static final int VAR_FINALEXPLICIT = 2;
public static final int VAR_FINAL = 3;
private HashMap<VarVersionPaar, VarType> mapExprentMinTypes = new HashMap<VarVersionPaar, VarType>();
private HashMap<VarVersionPaar, VarType> mapExprentMaxTypes = new HashMap<VarVersionPaar, VarType>();
private HashMap<VarVersionPaar, Integer> mapFinalVars = new HashMap<VarVersionPaar, Integer>();
private void setInitVars(RootStatement root) {
StructMethod mt = (StructMethod)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD);
// method descriptor
boolean thisvar = (mt.getAccessFlags() & CodeConstants.ACC_STATIC) == 0;
MethodDescriptor md = (MethodDescriptor)DecompilerContext.getProperty(DecompilerContext.CURRENT_METHOD_DESCRIPTOR);
if(thisvar) {
VarType cltype = new VarType(CodeConstants.TYPE_OBJECT, 0,
((StructClass)DecompilerContext.getProperty(DecompilerContext.CURRENT_CLASS)).qualifiedName);
mapExprentMinTypes.put(new VarVersionPaar(0,1), cltype);
mapExprentMaxTypes.put(new VarVersionPaar(0,1), cltype);
}
int varindex = 0;
for(int i=0;i<md.params.length;i++) {
mapExprentMinTypes.put(new VarVersionPaar(varindex+(thisvar?1:0), 1), md.params[i]);
mapExprentMaxTypes.put(new VarVersionPaar(varindex+(thisvar?1:0), 1), md.params[i]);
varindex+=md.params[i].stack_size;
}
// catch variables
LinkedList<Statement> stack = new LinkedList<Statement>();
stack.add(root);
while(!stack.isEmpty()) {
Statement stat = stack.removeFirst();
List<VarExprent> lstVars = null;
if(stat.type == Statement.TYPE_CATCHALL) {
lstVars = ((CatchAllStatement)stat).getVars();
} else if(stat.type == Statement.TYPE_TRYCATCH) {
lstVars = ((CatchStatement)stat).getVars();
}
if(lstVars != null) {
for(VarExprent var: lstVars) {
mapExprentMinTypes.put(new VarVersionPaar(var.getIndex(), 1), var.getVartype());
mapExprentMaxTypes.put(new VarVersionPaar(var.getIndex(), 1), var.getVartype());
}
}
stack.addAll(stat.getStats());
}
}
public void calculateVarTypes(RootStatement root, DirectGraph dgraph) {
setInitVars(root);
resetExprentTypes(dgraph);
while(!processVarTypes(dgraph));
}
private void resetExprentTypes(DirectGraph dgraph) {
dgraph.iterateExprents(new DirectGraph.ExprentIterator() {
public int processExprent(Exprent exprent) {
List<Exprent> lst = exprent.getAllExprents(true);
lst.add(exprent);
for(Exprent expr: lst) {
if(expr.type == Exprent.EXPRENT_VAR) {
((VarExprent)expr).setVartype(VarType.VARTYPE_UNKNOWN);
} else if(expr.type == Exprent.EXPRENT_CONST) {
ConstExprent cexpr = (ConstExprent)expr;
if(cexpr.getConsttype().type_family == CodeConstants.TYPE_FAMILY_INTEGER) {
cexpr.setConsttype(new ConstExprent(cexpr.getIntValue(), cexpr.isBoolPermitted()).getConsttype());
}
}
}
return 0;
}
});
}
private boolean processVarTypes(DirectGraph dgraph) {
return dgraph.iterateExprents(new DirectGraph.ExprentIterator() {
public int processExprent(Exprent exprent) {
return checkTypeExprent(exprent)?0:1;
}
});
}
private boolean checkTypeExprent(Exprent exprent) {
for(Exprent expr: exprent.getAllExprents()) {
if(!checkTypeExprent(expr)) {
return false;
}
}
if(exprent.type == Exprent.EXPRENT_CONST) {
ConstExprent cexpr = (ConstExprent)exprent;
if(cexpr.getConsttype().type_family <= CodeConstants.TYPE_FAMILY_INTEGER) { // boolean or integer
VarVersionPaar cpaar = new VarVersionPaar(cexpr.id, -1);
if(!mapExprentMinTypes.containsKey(cpaar)) {
mapExprentMinTypes.put(cpaar, cexpr.getConsttype());
}
}
}
CheckTypesResult result = exprent.checkExprTypeBounds();
for(CheckTypesResult.ExprentTypePair entry: result.getLstMaxTypeExprents()) {
if(entry.type.type_family != CodeConstants.TYPE_FAMILY_OBJECT) {
changeExprentType(entry.exprent, entry.type, 1);
}
}
boolean res = true;
for(CheckTypesResult.ExprentTypePair entry: result.getLstMinTypeExprents()) {
res &= changeExprentType(entry.exprent, entry.type, 0);
}
return res;
}
private boolean changeExprentType(Exprent exprent, VarType newtype, int minmax) {
boolean res = true;
switch(exprent.type) {
case Exprent.EXPRENT_CONST:
ConstExprent cexpr = (ConstExprent)exprent;
VarType consttype = cexpr.getConsttype();
if(newtype.type_family > CodeConstants.TYPE_FAMILY_INTEGER || consttype.type_family > CodeConstants.TYPE_FAMILY_INTEGER) {
return true;
} else if(newtype.type_family == CodeConstants.TYPE_FAMILY_INTEGER) {
VarType mininteger = new ConstExprent((Integer)((ConstExprent)exprent).getValue(), false).getConsttype();
if(mininteger.isStrictSuperset(newtype)) {
newtype = mininteger;
}
}
case Exprent.EXPRENT_VAR:
VarVersionPaar varpaar = null;
if(exprent.type == Exprent.EXPRENT_CONST) {
varpaar = new VarVersionPaar(((ConstExprent)exprent).id, -1);
} else if(exprent.type == Exprent.EXPRENT_VAR) {
varpaar = new VarVersionPaar((VarExprent)exprent);
}
if(minmax == 0) { // min
VarType currentMinType = mapExprentMinTypes.get(varpaar);
VarType newMinType;
if(currentMinType==null || newtype.type_family > currentMinType.type_family) {
newMinType = newtype;
} else if(newtype.type_family < currentMinType.type_family) {
return true;
} else {
newMinType = VarType.getCommonSupertype(currentMinType, newtype);
}
mapExprentMinTypes.put(varpaar, newMinType);
if(exprent.type == Exprent.EXPRENT_CONST) {
((ConstExprent)exprent).setConsttype(newMinType);
}
if(currentMinType != null && (newMinType.type_family > currentMinType.type_family ||
newMinType.isStrictSuperset(currentMinType))) {
return false;
}
} else { // max
VarType currentMaxType = mapExprentMaxTypes.get(varpaar);
VarType newMaxType;
if(currentMaxType==null || newtype.type_family < currentMaxType.type_family) {
newMaxType = newtype;
} else if(newtype.type_family > currentMaxType.type_family) {
return true;
} else {
newMaxType = VarType.getCommonMinType(currentMaxType, newtype);
}
mapExprentMaxTypes.put(varpaar, newMaxType);
}
break;
case Exprent.EXPRENT_ASSIGNMENT:
return changeExprentType(((AssignmentExprent)exprent).getRight(), newtype, minmax);
case Exprent.EXPRENT_FUNCTION:
FunctionExprent func = (FunctionExprent)exprent;
switch(func.getFunctype()){
case FunctionExprent.FUNCTION_IIF: // FIXME:
res &= changeExprentType(func.getLstOperands().get(1), newtype, minmax);
res &= changeExprentType(func.getLstOperands().get(2), newtype, minmax);
break;
case FunctionExprent.FUNCTION_AND:
case FunctionExprent.FUNCTION_OR:
case FunctionExprent.FUNCTION_XOR:
res &= changeExprentType(func.getLstOperands().get(0), newtype, minmax);
res &= changeExprentType(func.getLstOperands().get(1), newtype, minmax);
}
}
return res;
}
public HashMap<VarVersionPaar, VarType> getMapExprentMaxTypes() {
return mapExprentMaxTypes;
}
public HashMap<VarVersionPaar, VarType> getMapExprentMinTypes() {
return mapExprentMinTypes;
}
public HashMap<VarVersionPaar, Integer> getMapFinalVars() {
return mapFinalVars;
}
public void setVarType(VarVersionPaar varpaar, VarType type) {
mapExprentMinTypes.put(varpaar, type);
}
public VarType getVarType(VarVersionPaar varpaar) {
return mapExprentMinTypes.get(varpaar);
}
}
|