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