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 IFGTCommand extends AbstractInstructionCommand {
019:
020: public IFGTCommand(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: String tempString = "";
032: String tempstr = "";
033: byte[] info = getCode();
034: int prevStart = getGenericFinder().getPrevStartOfInst(
035: currentForIndex);
036: OperandStack opStack = getStack();
037: Behaviour behavior = getContext();
038:
039: Loop this Loop;
040: boolean encounteredAndOrComp = GlobalVariableStore
041: .isEncounteredAndOrComp();
042: boolean isIfInScope = GlobalVariableStore.isIfInScope();
043: Operand op = (Operand) opStack.pop();
044: // op1 = (Operand)opStack.pop();
045: int i = currentForIndex;
046: int classIndex = getGenericFinder().getJumpAddress(i);
047: i += 2;
048:
049: ArrayList list = getContext().getBehaviourLoops();
050:
051: IFBlock ifst = new IFBlock();
052: ifst.setIfStart(currentForIndex);
053: ifst.setHasIfBeenGenerated(true);
054: getContext().getMethodIfs().add(ifst);
055: boolean addBreak = LoopHelper.checkForParentLoopForIF(ifst);
056: BranchHelper.addBranchLabel(classIndex, i, ifst,
057: currentForIndex, info);
058:
059: boolean isEndOfLoop = LoopHelper.isIndexEndOfLoop(list, ifst
060: .getIfCloseLineNumber());
061: boolean beyondLoop = LoopHelper.isBeyondLoop(ifst
062: .getIfCloseLineNumber(), list, info);
063: boolean correctIf = false;
064: this Loop = GlobalVariableStore.getThisLoop();
065: prevStart = getGenericFinder().getPrevStartOfInst(
066: currentForIndex);
067: if (info[currentForIndex - 1] != JvmOpCodes.DCMPG
068: && info[currentForIndex - 1] != JvmOpCodes.DCMPL
069: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
070: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
071: && info[currentForIndex - 1] != JvmOpCodes.LCMP) {
072:
073: tempstr = op.getOperandValue() + "> 0";
074: } else if (prevStart != currentForIndex - 1) {
075: tempstr = op.getOperandValue() + "> 0";
076: } else {
077:
078: tempstr = op.getOperandValue();
079: }
080: boolean processIF = true;// checkForTernaryIf(ifst, info, tempstr);
081: if (processIF) {
082: if (ifst.getDonotclose() == false
083: && ifst.getIfCloseLineNumber() == -1) {
084: int if_end = IFHelper.checkIFEndIfUnset(ifst, info,
085: currentForIndex);
086: ifst.setIfCloseLineNumber(if_end);
087: }
088: if (isEndOfLoop) {
089: int loopstart = LoopHelper.getLoopStartForEnd(ifst
090: .getIfCloseLineNumber(), list);
091: if (currentForIndex > loopstart) {
092: boolean ifinstcodepresent = IFHelper.getIfinst(
093: loopstart, info, currentForIndex);
094: if (ifinstcodepresent) {
095: correctIf = false;
096: } else
097: correctIf = true;
098: }
099: }
100:
101: if ((ifst.getIfCloseLineNumber() > 0 && ifst
102: .getIfCloseLineNumber() < info.length)
103: && info[ifst.getIfCloseLineNumber()] == JvmOpCodes.GOTO
104: && isEndOfLoop && correctIf) {
105: int t = ifst.getIfCloseLineNumber();
106: int gotoIndex = getGenericFinder().getJumpAddress(t);// ((info[t+1]
107: // << 8)
108: // |
109: // info[t+2])
110: // +
111: // (ifst.getIfCloseLineNumber());
112: if (gotoIndex < (t + 3)) {
113: boolean isInfiniteLoop = false;
114: Iterator infLoop = getContext().getBehaviourLoops()
115: .iterator();
116: while (infLoop.hasNext()) {
117: Loop iloop = (Loop) infLoop.next();
118: if (iloop.getStartIndex() == gotoIndex
119: && iloop.isInfinite()) {
120: isInfiniteLoop = true;
121: /*
122: * ifLevel++; ifst = new IFBlock();
123: * ifst.setIfStart(currentForIndex);
124: * ifst.setHasIfBeenGenerated(true);
125: * //ifst.setIfCloseLineNumber(classIndex-3);
126: * ifst.setElseCloseLineNumber(gotoIndex);
127: * ifHashTable.put(""+(ifLevel),ifst);
128: */
129: ifst.setElseCloseLineNumber(gotoIndex);
130: isIfInScope = true;
131: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
132: boolean bb = LoopHelper.isBeyondLoop(
133: getGenericFinder().getJumpAddress(
134: currentForIndex),
135: getContext().getBehaviourLoops(),
136: info);
137: boolean print = true;
138: this Loop = GlobalVariableStore
139: .getThisLoop();
140: boolean addifbreak = false;
141: java.lang.String tempString2 = "";
142: if (bb && this Loop != null
143: && this Loop.isInfinite()
144: && !encounteredAndOrComp
145: && addBreak) {
146:
147: Loop dowl = LoopHelper.isIfInADoWhile(
148: currentForIndex, ifst,
149: getContext()
150: .getBehaviourLoops());
151: if (dowl != null) {
152: tempString = "";
153: } else {
154: if ((info[currentForIndex - 1] != JvmOpCodes.DCMPG)
155: && (info[currentForIndex - 1] != JvmOpCodes.DCMPL)
156: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
157: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
158: && info[currentForIndex - 1] != JvmOpCodes.LCMP)
159: tempString = "\nif("
160: + op.getOperandValue()
161: + ">0)\n{\nbreak;\n}\n";
162: else if (prevStart != currentForIndex - 1) {
163: tempString = "\nif("
164: + op.getOperandValue()
165: + ">0)\n{\nbreak;\n}\n";
166: } else
167: tempString = "\nif("
168: + op.getOperandValue()
169: + ")\n{\nbreak;\n}\n";
170: addifbreak = true;
171: tempString2 = tempString;
172: // codeStatements
173: // +=Util.formatDecompiledStatement(tempString);
174: }
175: print = false;
176: // ifst.setIfHasBeenClosed(true);
177: }
178: tempstr = "";
179: boolean bc = false;
180:
181: if (info[currentForIndex - 1] != JvmOpCodes.DCMPG
182: && info[currentForIndex - 1] != JvmOpCodes.DCMPL
183: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
184: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
185: && info[currentForIndex - 1] != JvmOpCodes.LCMP) {
186: tempString = "\nif("
187: + op.getOperandValue()
188: + "<=0)\n{\n";
189: tempstr = op.getOperandValue() + ">0";
190: } else if (prevStart != currentForIndex - 1) {
191: tempString = "\nif("
192: + op.getOperandValue()
193: + "<=0)\n{\n";
194: tempstr = op.getOperandValue() + ">0";
195: } else {
196: tempString = "\nif("
197: + op.getOperandValue()
198: + ")\n{\n";
199: tempstr = op.getOperandValue();
200: bc = true;
201: }
202: java.lang.String alt;
203: if (bc == false) {
204: alt = op.getOperandValue() + "<=0";
205: } else {
206: alt = op.getOperandValue();
207: }
208: boolean last = IFHelper
209: .lastIFinShortCutChain(info, ifst,
210: currentForIndex);
211: boolean c = IFHelper
212: .addCodeStatementWRTShortcutOR(
213: ifst, tempstr, print, "if",
214: last, alt);
215: if (c) {
216: if (addifbreak) {
217: behavior
218: .appendToBuffer(Util
219: .formatDecompiledStatement(tempString2));
220: }
221: behavior
222: .appendToBuffer(Util
223: .formatDecompiledStatement(tempString));
224: } else {
225: boolean firstIfForLoop = LoopHelper
226: .isIfFirstIfInLoopCondition(
227: info, currentForIndex);
228: if (firstIfForLoop) {
229: IFHelper
230: .registerElseBreakForIfChain(currentForIndex);
231: }
232: }
233:
234: break;
235: }
236: }
237: if (isInfiniteLoop) {
238: GlobalVariableStore
239: .setEncounteredAndOrComp(encounteredAndOrComp);
240: GlobalVariableStore.setIfInScope(isIfInScope);
241: GlobalVariableStore.setThisLoop(this Loop);
242:
243: return;
244: }
245: tempstr = "";
246: boolean bc = false;
247: if (info[currentForIndex - 1] != JvmOpCodes.DCMPG
248: && info[currentForIndex - 1] != JvmOpCodes.DCMPL
249: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
250: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
251: && info[currentForIndex - 1] != JvmOpCodes.LCMP) {
252: tempString = "\nwhile(" + op.getOperandValue()
253: + "<=0)\n{\n";
254: tempstr = op.getOperandValue() + ">0";
255: } else if (prevStart != currentForIndex - 1) {
256: tempString = "\nwhile(" + op.getOperandValue()
257: + "<=0)\n{\n";
258: tempstr = op.getOperandValue() + ">0";
259: } else {
260: tempString = "\nwhile(" + op.getOperandValue()
261: + ")\n{\n";
262: tempstr = op.getOperandValue();
263: bc = true;
264: }
265: java.lang.String alt;
266: if (bc == false) {
267: alt = op.getOperandValue() + "<=0";
268: } else {
269: alt = op.getOperandValue();
270: }
271: boolean last = IFHelper.lastIFinShortCutChain(info,
272: ifst, currentForIndex);
273: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
274: ifst, tempstr, true, "while", last, alt);
275: if (c)
276: behavior.appendToBuffer(Util
277: .formatDecompiledStatement(tempString));
278:
279: } else {
280: /*
281: * ifLevel++; ifst = new IFBlock();
282: * ifst.setIfStart(currentForIndex);
283: * ifst.setHasIfBeenGenerated(true);
284: * //ifst.setIfCloseLineNumber(classIndex-3);
285: */
286: ifst.setElseCloseLineNumber(gotoIndex);
287: // ifHashTable.put(""+(ifLevel),ifst);
288:
289: isIfInScope = true;
290: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
291: boolean bb = LoopHelper.isBeyondLoop(
292: getGenericFinder().getJumpAddress(
293: currentForIndex), getContext()
294: .getBehaviourLoops(), info);
295: boolean print = true;
296: this Loop = GlobalVariableStore.getThisLoop();
297: boolean addifbreak = false;
298: java.lang.String tempString2 = "";
299: if (bb && this Loop != null && this Loop.isInfinite()
300: && !encounteredAndOrComp && addBreak) {
301:
302: Loop dowl = LoopHelper.isIfInADoWhile(
303: currentForIndex, ifst, getContext()
304: .getBehaviourLoops());
305: if (dowl != null) {
306: tempString = "";
307: } else {
308: if ((info[currentForIndex - 1] != JvmOpCodes.DCMPG)
309: && (info[currentForIndex - 1] != JvmOpCodes.DCMPL)
310: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
311: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
312: && info[currentForIndex - 1] != JvmOpCodes.LCMP)
313: tempString = "\nif("
314: + op.getOperandValue()
315: + ">0)\n{\nbreak;\n}\n";
316: else if (prevStart != currentForIndex - 1) {
317: tempString = "\nif("
318: + op.getOperandValue()
319: + ">0)\n{\nbreak;\n}\n";
320: } else
321: tempString = "\nif("
322: + op.getOperandValue()
323: + ")\n{\nbreak;\n}\n";
324: addifbreak = true;
325: tempString2 = tempString;
326: // codeStatements
327: // +=Util.formatDecompiledStatement(tempString);
328: }
329: print = false;
330: // ifst.setIfHasBeenClosed(true);
331: }
332: tempstr = "";
333: boolean bc = false;
334: if (info[currentForIndex - 1] != JvmOpCodes.DCMPG
335: && info[currentForIndex - 1] != JvmOpCodes.DCMPL
336: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
337: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
338: && info[currentForIndex - 1] != JvmOpCodes.LCMP) {
339: tempString = "\nif(" + op.getOperandValue()
340: + "<=0)\n{\n";
341: tempstr = op.getOperandValue() + ">0";
342: } else if (prevStart != currentForIndex - 1) {
343: tempString = "\nif(" + op.getOperandValue()
344: + "<=0)\n{\n";
345: tempstr = op.getOperandValue() + ">0";
346: } else {
347: tempString = "\nif(" + op.getOperandValue()
348: + ")\n{\n";
349: tempstr = op.getOperandValue();
350: bc = true;
351: }
352: java.lang.String alt;
353: if (bc == false) {
354: alt = op.getOperandValue() + "<=0";
355: } else {
356: alt = op.getOperandValue();
357: }
358: boolean last = IFHelper.lastIFinShortCutChain(info,
359: ifst, currentForIndex);
360: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
361: ifst, tempstr, print, "if", last, alt);
362: if (c) {
363: if (addifbreak) {
364: behavior
365: .appendToBuffer(Util
366: .formatDecompiledStatement(tempString2));
367: }
368: behavior.appendToBuffer(Util
369: .formatDecompiledStatement(tempString));
370: } else {
371: boolean firstIfForLoop = LoopHelper
372: .isIfFirstIfInLoopCondition(info,
373: currentForIndex);
374: if (firstIfForLoop) {
375: IFHelper
376: .registerElseBreakForIfChain(currentForIndex);
377: }
378: }
379:
380: }
381:
382: } else {
383: /*
384: * ifLevel++; ifst = new IFBlock();
385: * ifst.setIfStart(currentForIndex);
386: * ifst.setHasIfBeenGenerated(true);
387: * ifHashTable.put(""+(ifLevel),ifst);
388: */
389: isIfInScope = true;
390: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
391: boolean bb = LoopHelper.isBeyondLoop(getGenericFinder()
392: .getJumpAddress(currentForIndex), getContext()
393: .getBehaviourLoops(), info);
394: boolean print = true;
395: this Loop = GlobalVariableStore.getThisLoop();
396: boolean addifbreak = false;
397: java.lang.String tempString2 = "";
398: tempstr = "";
399: if (bb && this Loop != null && this Loop.isInfinite()
400: && !encounteredAndOrComp && addBreak) {
401: Loop dowl = LoopHelper.isIfInADoWhile(
402: currentForIndex, ifst, getContext()
403: .getBehaviourLoops());
404: if (dowl != null) {
405: tempString = "";
406: } else {
407: if ((info[currentForIndex - 1] != JvmOpCodes.DCMPG)
408: && (info[currentForIndex - 1] != JvmOpCodes.DCMPL)
409: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
410: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
411: && info[currentForIndex - 1] != JvmOpCodes.LCMP)
412: tempString = "\nif(" + op.getOperandValue()
413: + ">0)\n{\nbreak;}\n";
414: else if (prevStart != currentForIndex - 1) {
415: tempString = "\nif(" + op.getOperandValue()
416: + ">0)\n{\nbreak;}\n";
417: } else
418: tempString = "\nif(" + op.getOperandValue()
419: + ")\n{\nbreak;}\n";
420: // codeStatements
421: // +=Util.formatDecompiledStatement(tempString);
422: tempString2 = tempString;
423: addifbreak = true;
424: }
425: print = false;
426: // ifst.setIfHasBeenClosed(true);
427: }
428: boolean bc = false;
429: if (info[currentForIndex - 1] != JvmOpCodes.DCMPG
430: && info[currentForIndex - 1] != JvmOpCodes.DCMPL
431: && info[currentForIndex - 1] != JvmOpCodes.FCMPG
432: && info[currentForIndex - 1] != JvmOpCodes.FCMPL
433: && info[currentForIndex - 1] != JvmOpCodes.LCMP) {
434: tempString = "\nif(" + op.getOperandValue()
435: + "<=0)\n{\n";
436: tempstr = op.getOperandValue() + "> 0";
437: } else if (prevStart != currentForIndex - 1) {
438: tempString = "\nif(" + op.getOperandValue()
439: + "<=0)\n{\n";
440: tempstr = op.getOperandValue() + "> 0";
441: } else {
442: tempString = "\nif(" + op.getOperandValue()
443: + ")\n{\n";
444: tempstr = op.getOperandValue();
445: bc = true;
446: }
447: java.lang.String alt;
448: if (bc == false) {
449: alt = op.getOperandValue() + "<=0";
450: } else {
451: alt = op.getOperandValue();
452: }
453: boolean last = IFHelper.lastIFinShortCutChain(info,
454: ifst, currentForIndex);
455: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
456: ifst, tempstr, print, "if", last, alt);
457: if (c) {
458: if (addifbreak) {
459: behavior
460: .appendToBuffer(Util
461: .formatDecompiledStatement(tempString2));
462: }
463: behavior.appendToBuffer(Util
464: .formatDecompiledStatement(tempString));
465: } else {
466: boolean firstIfForLoop = LoopHelper
467: .isIfFirstIfInLoopCondition(info,
468: currentForIndex);
469: if (firstIfForLoop) {
470: IFHelper
471: .registerElseBreakForIfChain(currentForIndex);
472: }
473: }
474:
475: }
476: }
477: GlobalVariableStore
478: .setEncounteredAndOrComp(encounteredAndOrComp);
479: GlobalVariableStore.setIfInScope(isIfInScope);
480: GlobalVariableStore.setThisLoop(thisLoop);
481:
482: }
483:
484: }
|