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 IFICMPEQCommand extends AbstractInstructionCommand {
019:
020: public IFICMPEQCommand(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: byte[] info = getCode();
034: Behaviour behavior = getContext();
035: Loop this Loop;
036: boolean encounteredAndOrComp = GlobalVariableStore
037: .isEncounteredAndOrComp();
038: boolean isIfInScope = GlobalVariableStore.isIfInScope();
039:
040: Operand op = (Operand) opStack.pop();
041: Operand op1 = (Operand) opStack.pop();
042: IFHelper.checkForBooleanOperandValue(op, op1);
043:
044: /*
045: * classIndex = ((i4 << 8) | i5); if(classIndex <
046: * 0)classIndex=(i4+1)*256-Math.abs(i5);
047: */
048: int classIndex = getGenericFinder().getJumpAddress(i);
049: i++;
050: i++;
051:
052: ArrayList list = getContext().getBehaviourLoops();
053:
054: IFBlock ifst = new IFBlock();
055: ifst.setIfStart(currentForIndex);
056: ifst.setHasIfBeenGenerated(true);
057: getContext().getMethodIfs().add(ifst);
058: isIfInScope = true;
059: BranchHelper.addBranchLabel(classIndex, i, ifst,
060: currentForIndex, info);
061: boolean addBreak = LoopHelper.checkForParentLoopForIF(ifst);
062: // isEndOfLoop=isIndexEndOfLoop(list,(classIndex-3));
063: boolean isEndOfLoop = LoopHelper.isIndexEndOfLoop(list, ifst
064: .getIfCloseLineNumber());
065: int loopstart = -1;
066: boolean correctIf = false;
067: String tempString = "";
068: boolean beyondLoop = LoopHelper.isBeyondLoop(ifst
069: .getIfCloseLineNumber(), list, info);
070: this Loop = GlobalVariableStore.getThisLoop();
071: boolean processIF = true;//checkForTernaryIf(ifst, info, op1.getOperandValue() + " != " + op.getOperandValue());
072: if (processIF) {
073: if (ifst.getDonotclose() == false
074: && ifst.getIfCloseLineNumber() == -1) {
075: int if_end = IFHelper.checkIFEndIfUnset(ifst, info,
076: currentForIndex);
077: ifst.setIfCloseLineNumber(if_end);
078: }
079: if (isEndOfLoop) {
080: loopstart = LoopHelper.getLoopStartForEnd(ifst
081: .getIfCloseLineNumber(), list);
082: if (currentForIndex > loopstart) {
083: boolean ifinstcodepresent = IFHelper.getIfinst(
084: loopstart, info, currentForIndex);
085: if (ifinstcodepresent) {
086: correctIf = false;
087: } else
088: correctIf = true;
089: }
090: }
091:
092: if ((ifst.getIfCloseLineNumber() > 0 && ifst
093: .getIfCloseLineNumber() < info.length)
094: && info[ifst.getIfCloseLineNumber()] == JvmOpCodes.GOTO
095: && isEndOfLoop && correctIf)
096:
097: {
098: int t = ifst.getIfCloseLineNumber();
099: int gotoIndex = getGenericFinder().getJumpAddress(t);// (info[t+1] << 8) |
100: // info[t+2]) +
101: // (ifst.getIfCloseLineNumber());
102: if (gotoIndex < (t + 3)) {
103: boolean isInfiniteLoop = false;
104: Iterator infLoop = getContext().getBehaviourLoops()
105: .iterator();
106: while (infLoop.hasNext()) {
107: Loop iloop = (Loop) infLoop.next();
108: if (iloop.getStartIndex() == gotoIndex
109: && iloop.isInfinite()) {
110: isInfiniteLoop = true;
111: /*
112: * ifLevel++; ifst = new IFBlock();
113: * ifst.setIfStart(currentForIndex);
114: * ifst.setHasIfBeenGenerated(true);
115: */
116: // ifst.setIfCloseLineNumber(classIndex-3);
117: ifst.setElseCloseLineNumber(gotoIndex);
118: // ifHashTable.put(""+(ifLevel),ifst);
119: // isIfInScope=true;
120: // addBranchLabel(classIndex,i,ifst,currentForIndex,info);
121: boolean bb = LoopHelper.isBeyondLoop(
122: getGenericFinder().getJumpAddress(
123: currentForIndex),
124: getContext().getBehaviourLoops(),
125: info);
126: this Loop = GlobalVariableStore
127: .getThisLoop();
128: isIfInScope = true;
129: boolean print = true;
130: boolean addifbreak = false;
131: if (bb && this Loop != null
132: && this Loop.isInfinite()
133: && !encounteredAndOrComp
134: && addBreak) {
135: Loop dowl = LoopHelper.isIfInADoWhile(
136: currentForIndex, ifst,
137: getContext()
138: .getBehaviourLoops());
139: if (dowl != null) {
140: tempString = "";
141: } else {
142: tempString = "\nif("
143: + op1.getOperandValue()
144: + " == "
145: + op.getOperandValue()
146: + ")\n{\nbreak;\n}\n";
147: // codeStatements
148: // +=Util.formatDecompiledStatement(tempString);
149: addifbreak = true;
150: }
151: print = false;
152: // ifst.setIfHasBeenClosed(true);
153: }
154: boolean last = IFHelper
155: .lastIFinShortCutChain(info, ifst,
156: currentForIndex);
157: boolean c = IFHelper
158: .addCodeStatementWRTShortcutOR(
159: ifst,
160: op1.getOperandValue()
161: + " == "
162: + op
163: .getOperandValue(),
164: print,
165: "if",
166: last,
167: op1.getOperandValue()
168: + " != "
169: + op
170: .getOperandValue());
171: if (c) {
172: if (addifbreak) {
173: behavior
174: .appendToBuffer(Util
175: .formatDecompiledStatement(tempString));
176: }
177: tempString = "\nif("
178: + op1.getOperandValue()
179: + " != " + op.getOperandValue()
180: + ")\n{\n";
181: behavior
182: .appendToBuffer(Util
183: .formatDecompiledStatement(tempString));
184: } else {
185: boolean firstIfForLoop = LoopHelper
186: .isIfFirstIfInLoopCondition(
187: info, currentForIndex);
188: if (firstIfForLoop) {
189: IFHelper
190: .registerElseBreakForIfChain(currentForIndex);
191: }
192: }
193:
194: break;
195: }
196: }
197: if (isInfiniteLoop) {
198: GlobalVariableStore
199: .setEncounteredAndOrComp(encounteredAndOrComp);
200: GlobalVariableStore.setIfInScope(isIfInScope);
201: GlobalVariableStore.setThisLoop(this Loop);
202:
203: return;
204: }
205: boolean last = IFHelper.lastIFinShortCutChain(info,
206: ifst, currentForIndex);
207: boolean c = IFHelper.addCodeStatementWRTShortcutOR(
208: ifst, op1.getOperandValue() + " == "
209: + op.getOperandValue(), true,
210: "while", last, op1.getOperandValue()
211: + " != " + op.getOperandValue());
212: if (c) {
213: tempString = "\nwhile(" + op1.getOperandValue()
214: + "!= " + op.getOperandValue()
215: + ")\n{\n";
216: behavior.appendToBuffer(Util
217: .formatDecompiledStatement(tempString));
218: } else {
219: boolean firstIfForLoop = LoopHelper
220: .isIfFirstIfInLoopCondition(info,
221: currentForIndex);
222: if (firstIfForLoop) {
223: IFHelper
224: .registerElseBreakForIfChain(currentForIndex);
225: }
226: }
227:
228: }
229: // int x = 0;
230: else {
231:
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: if (bb && this Loop != null && this Loop.isInfinite()
250: && !encounteredAndOrComp && addBreak) {
251: Loop dowl = LoopHelper.isIfInADoWhile(
252: currentForIndex, ifst, getContext()
253: .getBehaviourLoops());
254: if (dowl != null) {
255: tempString = "";
256: } else {
257: tempString = "\nif("
258: + op1.getOperandValue() + " == "
259: + op.getOperandValue() + ")\n{\n"
260: + "break;\n" + "}\n";
261: // codeStatements
262: // +=Util.formatDecompiledStatement(tempString);
263: addifbreak = true;
264: }
265: print = false;
266: // ifst.setIfHasBeenClosed(true);
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); isIfInScope=true;
303: * addBranchLabel(classIndex,i,ifst,currentForIndex,info);
304: */
305: isIfInScope = true;
306: boolean bb = LoopHelper.isBeyondLoop(getGenericFinder()
307: .getJumpAddress(currentForIndex), getContext()
308: .getBehaviourLoops(), info);
309: boolean print = true;
310: this Loop = GlobalVariableStore.getThisLoop();
311: boolean addifbreak = false;
312: if (bb && this Loop != null && this Loop.isInfinite()
313: && !encounteredAndOrComp && addBreak) {
314: Loop dowl = LoopHelper.isIfInADoWhile(
315: currentForIndex, ifst, getContext()
316: .getBehaviourLoops());
317: if (dowl != null) {
318: tempString = "";
319: } else {
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: }
|