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