001: package net.sf.jdec.jvminstructions.commands;
002:
003: import net.sf.jdec.blockhelpers.BranchHelper;
004: import net.sf.jdec.blockhelpers.IFHelper;
005: import net.sf.jdec.blockhelpers.LoopHelper;
006: import net.sf.jdec.blocks.IFBlock;
007: import net.sf.jdec.blocks.Loop;
008: import net.sf.jdec.core.GlobalVariableStore;
009: import net.sf.jdec.core.JvmOpCodes;
010: import net.sf.jdec.core.Operand;
011: import net.sf.jdec.core.OperandStack;
012: import net.sf.jdec.reflection.Behaviour;
013: import net.sf.jdec.util.Util;
014:
015: import java.util.ArrayList;
016: import java.util.Iterator;
017:
018: public class IFICMPGTCommand extends AbstractInstructionCommand {
019:
020: public IFICMPGTCommand(Behaviour context) {
021: super (context);
022:
023: }
024:
025: public int getSkipBytes() {
026: return 2;
027: }
028:
029: public void execute() {
030: int currentForIndex = getCurrentInstPosInCode();
031: OperandStack opStack = getStack();
032: int i = currentForIndex;
033: String tempString = "";
034: byte[] info = getCode();
035: Behaviour behavior = getContext();
036: Loop this Loop;
037: boolean encounteredAndOrComp = GlobalVariableStore
038: .isEncounteredAndOrComp();
039: boolean isIfInScope = GlobalVariableStore.isIfInScope();
040:
041: Operand op = (Operand) opStack.pop();
042: Operand op1 = (Operand) opStack.pop();
043: int classIndex = getGenericFinder().getJumpAddress(i);
044: i += 2;
045:
046: ArrayList list = getContext().getBehaviourLoops();
047:
048: IFBlock ifst = new IFBlock();
049: ifst.setIfStart(currentForIndex);
050: ifst.setHasIfBeenGenerated(true);
051: getContext().getMethodIfs().add(ifst);
052: BranchHelper.addBranchLabel(classIndex, i, ifst,
053: currentForIndex, info);
054: boolean addBreak = LoopHelper.checkForParentLoopForIF(ifst);
055: boolean beyondLoop = LoopHelper.isBeyondLoop(ifst
056: .getIfCloseLineNumber(), list, info);
057: this Loop = GlobalVariableStore.getThisLoop();
058: boolean isEndOfLoop = LoopHelper.isIndexEndOfLoop(list, ifst
059: .getIfCloseLineNumber());
060: boolean correctIf = false;
061: int loopstart = -1;
062: boolean processIF = true;// checkForTernaryIf(ifst,info,op1.getOperandValue()+"
063: // <= "+op.getOperandValue());
064: if (processIF) {
065: if (ifst.getDonotclose() == false
066: && ifst.getIfCloseLineNumber() == -1) {
067: int if_end = IFHelper.checkIFEndIfUnset(ifst, info,
068: currentForIndex);
069: ifst.setIfCloseLineNumber(if_end);
070: }
071: if (isEndOfLoop) {
072: loopstart = LoopHelper.getLoopStartForEnd(ifst
073: .getIfCloseLineNumber(), list);
074: if (currentForIndex > loopstart) {
075: boolean ifinstcodepresent = IFHelper.getIfinst(
076: loopstart, info, currentForIndex);
077: if (ifinstcodepresent) {
078: correctIf = false;
079: } else
080: correctIf = true;
081: }
082: }
083: if ((ifst.getIfCloseLineNumber() > 0 && ifst
084: .getIfCloseLineNumber() < info.length)
085: && info[ifst.getIfCloseLineNumber()] == JvmOpCodes.GOTO
086: && isEndOfLoop && correctIf) {
087: int t = ifst.getIfCloseLineNumber();
088: int gotoIndex = getGenericFinder().getJumpAddress(t);// ((info[t+1]
089: // << 8)
090: // |
091: // info[t+2])
092: // +
093: // (ifst.getIfCloseLineNumber());
094: if (gotoIndex < (t + 3))
095:
096: {
097: boolean isInfiniteLoop = false;
098: Iterator infLoop = getContext().getBehaviourLoops()
099: .iterator();
100: while (infLoop.hasNext()) {
101: Loop iloop = (Loop) infLoop.next();
102: if (iloop.getStartIndex() == gotoIndex
103: && iloop.isInfinite()) {
104: isInfiniteLoop = true;
105: /*
106: * ifLevel++; ifst = new IFBlock();
107: * ifst.setIfStart(currentForIndex);
108: * ifst.setHasIfBeenGenerated(true);
109: * //fst.setIfCloseLineNumber(classIndex-3);
110: */
111: ifst.setElseCloseLineNumber(gotoIndex);
112: isIfInScope = true;
113: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
114: boolean bb = LoopHelper.isBeyondLoop(
115: getGenericFinder().getJumpAddress(
116: currentForIndex),
117: getContext().getBehaviourLoops(),
118: info);
119: this Loop = GlobalVariableStore
120: .getThisLoop();
121: boolean print = true;
122: boolean addifbreak = false;
123: if (bb && this Loop != null
124: && this Loop.isInfinite()
125: && !encounteredAndOrComp
126: && addBreak) {
127: Loop dowl = LoopHelper.isIfInADoWhile(
128: currentForIndex, ifst,
129: getContext()
130: .getBehaviourLoops());
131: if (dowl != null) {
132: tempString = "";
133: } else {
134: addifbreak = true;
135: tempString = "\nif("
136: + op1.getOperandValue()
137: + " > "
138: + op.getOperandValue()
139: + ")\n{\n" + "break;\n"
140: + "}\n";
141: // codeStatements
142: // +=Util.formatDecompiledStatement(tempString);
143: }
144: // ifst.setIfHasBeenClosed(true);
145: print = false;
146: }
147: boolean last = IFHelper
148: .lastIFinShortCutChain(info, ifst,
149: currentForIndex);
150: boolean c = IFHelper
151: .addCodeStatementWRTShortcutOR(
152: ifst,
153: op1.getOperandValue()
154: + " > "
155: + op
156: .getOperandValue(),
157: print,
158: "if",
159: last,
160: op1.getOperandValue()
161: + " <= "
162: + op
163: .getOperandValue());
164: if (c) {
165: if (addifbreak) {
166: behavior
167: .appendToBuffer(Util
168: .formatDecompiledStatement(tempString));
169: }
170: tempString = "\nif("
171: + op1.getOperandValue()
172: + " <= " + op.getOperandValue()
173: + ")\n{\n";
174: behavior
175: .appendToBuffer(Util
176: .formatDecompiledStatement(tempString));
177: } else {
178: boolean firstIfForLoop = LoopHelper
179: .isIfFirstIfInLoopCondition(
180: info, currentForIndex);
181: if (firstIfForLoop) {
182: IFHelper
183: .registerElseBreakForIfChain(currentForIndex);
184: }
185: }
186:
187: break;
188: }
189: }
190: if (isInfiniteLoop) {
191: GlobalVariableStore
192: .setEncounteredAndOrComp(encounteredAndOrComp);
193: GlobalVariableStore.setIfInScope(isIfInScope);
194: GlobalVariableStore.setThisLoop(this Loop);
195:
196: return;
197: }
198: boolean last = IFHelper.lastIFinShortCutChain(info,
199: ifst, currentForIndex);
200: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
201: ifst, op1.getOperandValue() + " > "
202: + op.getOperandValue(), true,
203: "while", last, op1.getOperandValue()
204: + " <= " + op.getOperandValue());
205: if (c) {
206: tempString = "\nwhile(" + op1.getOperandValue()
207: + " <= " + op.getOperandValue()
208: + ")\n{\n";
209: behavior.appendToBuffer(Util
210: .formatDecompiledStatement(tempString));
211: } else {
212: boolean firstIfForLoop = LoopHelper
213: .isIfFirstIfInLoopCondition(info,
214: currentForIndex);
215: if (firstIfForLoop) {
216: IFHelper
217: .registerElseBreakForIfChain(currentForIndex);
218: }
219: }
220:
221: }
222: // int x = 0;
223: // Added by belurs: There was no else here
224: else {
225:
226: /*
227: * ifLevel++; ifst = new IFBlock();
228: * ifst.setIfStart(currentForIndex);
229: * ifst.setHasIfBeenGenerated(true); //
230: * ifst.setIfCloseLineNumber(classIndex-3);
231: */
232: ifst.setElseCloseLineNumber(gotoIndex);
233: // ifHashTable.put(""+(ifLevel),ifst);
234: isIfInScope = true;
235: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
236: boolean bb = LoopHelper.isBeyondLoop(
237: getGenericFinder().getJumpAddress(
238: currentForIndex), getContext()
239: .getBehaviourLoops(), info);
240: this Loop = GlobalVariableStore.getThisLoop();
241: boolean print = true;
242: boolean addifbreak = false;
243: if (bb && this Loop != null && this Loop.isInfinite()
244: && !encounteredAndOrComp && addBreak) {
245: Loop dowl = LoopHelper.isIfInADoWhile(
246: currentForIndex, ifst, getContext()
247: .getBehaviourLoops());
248: if (dowl != null) {
249: tempString = "";
250: } else {
251: addifbreak = true;
252: tempString = "\nif("
253: + op1.getOperandValue() + " > "
254: + op.getOperandValue() + ")\n{\n"
255: + "break;\n" + "}\n";
256: // codeStatements
257: // +=Util.formatDecompiledStatement(tempString);
258: }
259: // ifst.setIfHasBeenClosed(true);
260: print = false;
261: }
262: boolean last = IFHelper.lastIFinShortCutChain(info,
263: ifst, currentForIndex);
264: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
265: ifst, op1.getOperandValue() + " > "
266: + op.getOperandValue(), print,
267: "if", last, op1.getOperandValue() + " <= "
268: + op.getOperandValue());
269: if (c) {
270: if (addifbreak) {
271: behavior
272: .appendToBuffer(Util
273: .formatDecompiledStatement(tempString));
274: }
275: tempString = "\nif(" + op1.getOperandValue()
276: + " <=" + op.getOperandValue()
277: + ")\n{\n";
278: behavior.appendToBuffer(Util
279: .formatDecompiledStatement(tempString));
280: } else {
281: boolean firstIfForLoop = LoopHelper
282: .isIfFirstIfInLoopCondition(info,
283: currentForIndex);
284: if (firstIfForLoop) {
285: IFHelper
286: .registerElseBreakForIfChain(currentForIndex);
287: }
288: }
289:
290: }
291: } else {
292: /*
293: * ifLevel++; ifst = new IFBlock();
294: * ifst.setIfStart(currentForIndex);
295: * ifst.setHasIfBeenGenerated(true);
296: * ifHashTable.put(""+(ifLevel),ifst);
297: */
298: isIfInScope = true;
299: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
300: boolean bb = LoopHelper.isBeyondLoop(getGenericFinder()
301: .getJumpAddress(currentForIndex), getContext()
302: .getBehaviourLoops(), info);
303: this Loop = GlobalVariableStore.getThisLoop();
304: boolean print = true;
305: boolean addifbreak = false;
306:
307: if (bb && this Loop != null && this Loop.isInfinite()
308: && !encounteredAndOrComp && addBreak) {
309: Loop dowl = LoopHelper.isIfInADoWhile(
310: currentForIndex, ifst, getContext()
311: .getBehaviourLoops());
312: if (dowl != null) {
313: tempString = "";
314: } else {
315: addifbreak = true;
316: tempString = "\nif(" + op1.getOperandValue()
317: + " > " + op.getOperandValue()
318: + ")\n{\n" + "break;\n" + "}\n";
319: // codeStatements
320: // +=Util.formatDecompiledStatement(tempString);
321: }
322: print = false;
323: // ifst.setIfHasBeenClosed(true);
324: }
325: boolean last = IFHelper.lastIFinShortCutChain(info,
326: ifst, currentForIndex);
327: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
328: ifst, op1.getOperandValue() + " > "
329: + op.getOperandValue(), print, "if",
330: last, op1.getOperandValue() + " <= "
331: + op.getOperandValue());
332: if (c) {
333: if (addifbreak) {
334: behavior.appendToBuffer(Util
335: .formatDecompiledStatement(tempString));
336: }
337: tempString = "\nif(" + op1.getOperandValue()
338: + " <= " + op.getOperandValue() + ")\n{\n";
339: behavior.appendToBuffer(Util
340: .formatDecompiledStatement(tempString));
341: } else {
342: boolean firstIfForLoop = LoopHelper
343: .isIfFirstIfInLoopCondition(info,
344: currentForIndex);
345: if (firstIfForLoop) {
346: IFHelper
347: .registerElseBreakForIfChain(currentForIndex);
348: }
349: }
350:
351: }
352: }
353: GlobalVariableStore
354: .setEncounteredAndOrComp(encounteredAndOrComp);
355: GlobalVariableStore.setIfInScope(isIfInScope);
356: GlobalVariableStore.setThisLoop(thisLoop);
357:
358: }
359:
360: }
|