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 IFICMPLTCommand extends AbstractInstructionCommand {
019:
020: public IFICMPLTCommand(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: Loop this Loop;
036: boolean encounteredAndOrComp = GlobalVariableStore
037: .isEncounteredAndOrComp();
038: boolean isIfInScope = GlobalVariableStore.isIfInScope();
039: Behaviour behavior = getContext();
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: int loopstart = -1;
061: boolean correctIf = false;
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: }
084: if ((ifst.getIfCloseLineNumber() > 0 && ifst
085: .getIfCloseLineNumber() < info.length)
086: && info[ifst.getIfCloseLineNumber()] == JvmOpCodes.GOTO
087: && isEndOfLoop && correctIf) {
088: int t = ifst.getIfCloseLineNumber();
089: int gotoIndex = getGenericFinder().getJumpAddress(t);// ((info[t+1]
090: // << 8)
091: // |
092: // info[t+2])
093: // +
094: // (ifst.getIfCloseLineNumber());
095: if (gotoIndex <= (t + 3))
096:
097: {
098: boolean isInfiniteLoop = false;
099: Iterator infLoop = getContext().getBehaviourLoops()
100: .iterator();
101: while (infLoop.hasNext()) {
102: Loop iloop = (Loop) infLoop.next();
103: if (iloop.getStartIndex() == gotoIndex
104: && iloop.isInfinite()) {
105: isInfiniteLoop = true;
106: /*
107: * ifLevel++; ifst = new IFBlock();
108: * ifst.setIfStart(currentForIndex);
109: * ifst.setHasIfBeenGenerated(true); //
110: * ifst.setIfCloseLineNumber(classIndex-3);
111: */
112: ifst.setElseCloseLineNumber(gotoIndex);
113: // ifHashTable.put(""+(ifLevel),ifst);
114: isIfInScope = true;
115: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
116: boolean bb = LoopHelper.isBeyondLoop(
117: getGenericFinder().getJumpAddress(
118: currentForIndex),
119: getContext().getBehaviourLoops(),
120: info);
121: this Loop = GlobalVariableStore
122: .getThisLoop();
123: boolean print = true;
124: boolean addifbreak = false;
125: if (bb && this Loop != null
126: && this Loop.isInfinite()
127: && !encounteredAndOrComp
128: && addBreak) {
129: Loop dowl = LoopHelper.isIfInADoWhile(
130: currentForIndex, ifst,
131: getContext()
132: .getBehaviourLoops());
133: if (dowl != null) {
134: tempString = "";
135: } else {
136: addifbreak = true;
137: tempString = "\nif("
138: + op1.getOperandValue()
139: + " < "
140: + op.getOperandValue()
141: + ")\n{\nbreak;\n}\n";
142: // codeStatements
143: // +=Util.formatDecompiledStatement(tempString);
144: }
145: print = false;
146: // ifst.setIfHasBeenClosed(true);
147: }
148: boolean last = IFHelper
149: .lastIFinShortCutChain(info, ifst,
150: currentForIndex);
151: boolean c = IFHelper
152: .addCodeStatementWRTShortcutOR(
153: ifst,
154: op1.getOperandValue()
155: + " < "
156: + op
157: .getOperandValue(),
158: print,
159: "if",
160: last,
161: op1.getOperandValue()
162: + " >= "
163: + op
164: .getOperandValue());
165: if (c) {
166: if (addifbreak) {
167: behavior
168: .appendToBuffer(Util
169: .formatDecompiledStatement(tempString));
170: }
171: tempString = "\nif("
172: + op1.getOperandValue()
173: + " >= " + op.getOperandValue()
174: + ")\n{";
175: behavior
176: .appendToBuffer(Util
177: .formatDecompiledStatement(tempString));
178: } else {
179: boolean firstIfForLoop = LoopHelper
180: .isIfFirstIfInLoopCondition(
181: info, currentForIndex);
182: if (firstIfForLoop) {
183: IFHelper
184: .registerElseBreakForIfChain(currentForIndex);
185: }
186: }
187:
188: break;
189: }
190: }
191: if (isInfiniteLoop) {
192: GlobalVariableStore
193: .setEncounteredAndOrComp(encounteredAndOrComp);
194: GlobalVariableStore.setIfInScope(isIfInScope);
195: GlobalVariableStore.setThisLoop(this Loop);
196:
197: return;
198: }
199: boolean last = IFHelper.lastIFinShortCutChain(info,
200: ifst, currentForIndex);
201: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
202: ifst, op1.getOperandValue() + " < "
203: + op.getOperandValue(), true,
204: "while", last, op1.getOperandValue()
205: + " >= " + op.getOperandValue());
206: if (c) {
207: tempString = "\nwhile(" + op1.getOperandValue()
208: + " >= " + op.getOperandValue()
209: + ")\n{\n";
210: behavior.appendToBuffer(Util
211: .formatDecompiledStatement(tempString));
212: } else {
213: boolean firstIfForLoop = LoopHelper
214: .isIfFirstIfInLoopCondition(info,
215: currentForIndex);
216: if (firstIfForLoop) {
217: IFHelper
218: .registerElseBreakForIfChain(currentForIndex);
219: }
220: }
221:
222: }
223:
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()
255: + ")\n{\nbreak;\n}\n";
256: // codeStatements
257: // +=Util.formatDecompiledStatement(tempString);
258: }
259: print = false;
260: // ifst.setIfHasBeenClosed(true);
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{";
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:
292: } else {
293: /*
294: * ifLevel++; ifst = new IFBlock();
295: * ifst.setIfStart(currentForIndex);
296: * ifst.setHasIfBeenGenerated(true);
297: * ifHashTable.put(""+(ifLevel),ifst);
298: */
299: isIfInScope = true;
300: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
301: boolean bb = LoopHelper.isBeyondLoop(getGenericFinder()
302: .getJumpAddress(currentForIndex), getContext()
303: .getBehaviourLoops(), info);
304: this Loop = GlobalVariableStore.getThisLoop();
305: boolean print = true;
306: boolean addifbreak = false;
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:
323: print = false;
324: // ifst.setIfHasBeenClosed(true);
325: }
326: boolean last = IFHelper.lastIFinShortCutChain(info,
327: ifst, currentForIndex);
328: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
329: ifst, op1.getOperandValue() + " < "
330: + op.getOperandValue(), print, "if",
331: last, op1.getOperandValue() + " >= "
332: + op.getOperandValue());
333: if (c) {
334: if (addifbreak) {
335: behavior.appendToBuffer(Util
336: .formatDecompiledStatement(tempString));
337: }
338: tempString = "\nif(" + op1.getOperandValue()
339: + " >= " + op.getOperandValue() + ")\n{";
340: behavior.appendToBuffer(Util
341: .formatDecompiledStatement(tempString));
342: } else {
343: boolean firstIfForLoop = LoopHelper
344: .isIfFirstIfInLoopCondition(info,
345: currentForIndex);
346: if (firstIfForLoop) {
347: IFHelper
348: .registerElseBreakForIfChain(currentForIndex);
349: }
350: }
351:
352: }
353: }
354: GlobalVariableStore
355: .setEncounteredAndOrComp(encounteredAndOrComp);
356: GlobalVariableStore.setIfInScope(isIfInScope);
357: GlobalVariableStore.setThisLoop(thisLoop);
358:
359: }
360:
361: }
|