package jode; import java.util.Enumeration; /** * This instruction header represents an if instruction. The * linkage of the instructions is as follow: *
 *  A: ....
 * 

* prev = A, next = H, pred = normal, succ = {C,E} * B: if ( instr ) { *

* prev = null, next = D, pred = {B}, succ = {D} * C: then-instr 1 *

* prev = C, next = H!, pred = normal succ = normal * D: instructions of then part *

* } else { *

* prev = null, next = D, pred = {B}, succ = {D} * E: else-instr 1 *

* prev = E, next = null, pred = normal, succ = normal * F: instructions of then part *

* } * prev = B, ..., pred = normal, succ = normal * H: ... *

*/ public class IfInstructionHeader extends InstructionHeader { boolean hasElsePart; /** * Creates a new if statement. There are several conditions that * must be met: * * @param ifHeader the instruction header whichs contains the * if goto statement. * @param thenStart the start of the then part. * @param thenEnd the end of the then part. * @param elseStart the start of the else part. * @param elseEnd the end of the then part. * @param next the next instruction after the if statement. */ public IfInstructionHeader(InstructionHeader ifHeader, boolean hasElsePart, InstructionHeader thenEnd, InstructionHeader elseEnd, InstructionHeader endBlock) { super(IFSTATEMENT, ifHeader.addr, endBlock.addr, ifHeader.successors, ifHeader.outer); this.instr = ((Expression)ifHeader.getInstruction()).negate(); this.movePredecessors(ifHeader); this.outer = ifHeader.outer; this.endBlock = endBlock; successors[0].predecessors.removeElement(ifHeader); successors[1].predecessors.removeElement(ifHeader); successors[0].predecessors.addElement(this); successors[1].predecessors.addElement(this); successors[0].prevInstruction = null; InstructionHeader next = thenEnd.nextInstruction; thenEnd.nextInstruction = null; for (InstructionHeader ih = successors[0]; ih != null; ih = ih.nextInstruction) if (ih.outer == outer) ih.outer = this; this.hasElsePart = hasElsePart; if (hasElsePart) { thenEnd.flowType = thenEnd.NORMAL; successors[1].prevInstruction = null; next = elseEnd.nextInstruction; elseEnd.nextInstruction = null; for (InstructionHeader ih = successors[1]; ih != null; ih = ih.nextInstruction) if (ih.outer == outer) ih.outer = this; } this.nextInstruction = next; if (next != null) next.prevInstruction = this; } public void dumpSource(TabbedPrintWriter writer) throws java.io.IOException { if (Decompiler.isDebugging) { dumpDebugging(writer); writer.tab(); } if (needsLabel()) { writer.untab(); writer.println(getLabel()+": "); writer.tab(); } boolean braces = successors[0].flowType != NORMAL || successors[0].nextInstruction != null; writer.println("if (" + instr.toString() + ")" + (braces ? " {": "")); writer.tab(); for (InstructionHeader ih = successors[0]; ih != null; ih = ih.nextInstruction) ih.dumpSource(writer); writer.untab(); if (hasElsePart) { if (braces) writer.print("} "); braces = successors[1].flowType != NORMAL || successors[1].nextInstruction != null; if (!braces && successors[1].flowType == IFSTATEMENT) { writer.print("else "); successors[1].dumpSource(writer); } else { writer.println("else" + (braces ? " {": "")); writer.tab(); for (InstructionHeader ih = successors[1]; ih != null; ih = ih.nextInstruction) ih.dumpSource(writer); writer.untab(); } } if (braces) writer.println("} "); if (Decompiler.isDebugging) writer.untab(); } public InstructionHeader doTransformations(Transformation[] trafo) { InstructionHeader next; for (InstructionHeader ih = successors[0]; ih != null; ih = next) { if ((next = ih.doTransformations(trafo)) == null) next = ih.getNextInstruction(); } if (hasElsePart) { for (InstructionHeader ih = successors[1]; ih != null; ih = next) { if ((next = ih.doTransformations(trafo)) == null) next = ih.getNextInstruction(); } } return super.doTransformations(trafo); } }