summaryrefslogtreecommitdiffstats
path: root/src/org/jetbrains/java/decompiler/modules/decompiler/PPandMMHelper.java
blob: be9062524c8010a334cd4e144ed9e4bc7af47b43 (plain)
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
/*
 *    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;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;

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.sforms.DirectGraph;
import org.jetbrains.java.decompiler.modules.decompiler.sforms.DirectNode;
import org.jetbrains.java.decompiler.modules.decompiler.sforms.FlattenStatementsHelper;
import org.jetbrains.java.decompiler.modules.decompiler.stats.RootStatement;
import org.jetbrains.java.decompiler.struct.gen.VarType;

public class PPandMMHelper {

	private boolean exprentReplaced;
	
	public boolean findPPandMM(RootStatement root) {
		
		FlattenStatementsHelper flatthelper = new FlattenStatementsHelper();
		DirectGraph dgraph = flatthelper.buildDirectGraph(root);

		LinkedList<DirectNode> stack = new LinkedList<DirectNode>();
		stack.add(dgraph.first);
		
		HashSet<DirectNode> setVisited = new HashSet<DirectNode>(); 
		
		boolean res = false;
		
		while(!stack.isEmpty()) {
			
			DirectNode node = stack.removeFirst();
			
			if(setVisited.contains(node)) {
				continue;
			}
			setVisited.add(node);
			
			res |= processExprentList(node.exprents);
			
			stack.addAll(node.succs);
		}
		
		return res;
	}
	
	private boolean processExprentList(List<Exprent> lst) {
		
		boolean result = false;

		for(int i=0;i<lst.size();i++) {
			Exprent exprent = lst.get(i);
			exprentReplaced = false;
			
			Exprent retexpr = processExprentRecursive(exprent);
			if(retexpr != null) {
				lst.set(i, retexpr);

				result = true;
				i--; // process the same exprent again
			}
			
			result |= exprentReplaced;
		}

		return result;
	}

	private Exprent processExprentRecursive(Exprent exprent) {

		boolean replaced = true;
		while(replaced) {
			replaced = false;

			for(Exprent expr: exprent.getAllExprents()) {
				Exprent retexpr = processExprentRecursive(expr);
				if(retexpr != null) {
					exprent.replaceExprent(expr, retexpr);
					replaced = true;
					exprentReplaced = true;
					break;
				}
			}
		}

		if(exprent.type == Exprent.EXPRENT_ASSIGNMENT) {
			AssignmentExprent as = (AssignmentExprent)exprent;

			if(as.getRight().type == Exprent.EXPRENT_FUNCTION) {
				FunctionExprent func = (FunctionExprent)as.getRight();

				VarType midlayer = null;
				if(func.getFunctype() >= FunctionExprent.FUNCTION_I2L && 
						func.getFunctype() <= FunctionExprent.FUNCTION_I2S) {
					midlayer = func.getSimpleCastType();
					if(func.getLstOperands().get(0).type == Exprent.EXPRENT_FUNCTION) {
						func = (FunctionExprent)func.getLstOperands().get(0);
					} else {
						return null;
					}
				}

				if(func.getFunctype() == FunctionExprent.FUNCTION_ADD ||
						func.getFunctype() == FunctionExprent.FUNCTION_SUB) {
					Exprent econd = func.getLstOperands().get(0);
					Exprent econst = func.getLstOperands().get(1);

					if(econst.type != Exprent.EXPRENT_CONST && econd.type == Exprent.EXPRENT_CONST &&
							func.getFunctype() == FunctionExprent.FUNCTION_ADD) {
						econd = econst;
						econst = func.getLstOperands().get(0);
					}

					if(econst.type == Exprent.EXPRENT_CONST && ((ConstExprent)econst).hasValueOne()) {
						Exprent left = as.getLeft();

						VarType condtype = econd.getExprType();
						if(left.equals(econd) && (midlayer == null || midlayer.equals(condtype))) {
							FunctionExprent ret = new FunctionExprent(
									func.getFunctype() == FunctionExprent.FUNCTION_ADD?FunctionExprent.FUNCTION_PPI:FunctionExprent.FUNCTION_MMI,
											Arrays.asList(new Exprent[]{econd}));
							ret.setImplicitType(condtype);

							exprentReplaced = true;
							return ret;
						}
					}
				}
			}
		}

		return null;
	}
	
}