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