mapStackToLocal / removePush added

brace rule changed
fall through flag added and comment printed
default case is no better removed


git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@309 379699f6-c40d-0410-875b-85095c16579e
stable
jochen 26 years ago
parent 8442863349
commit 12e858ca45
  1. 115
      jode/jode/flow/CaseBlock.java

@ -43,19 +43,14 @@ public class CaseBlock extends StructuredBlock {
boolean isDefault = false; boolean isDefault = false;
/** /**
* True, if this is the last case in a switch * True, if the previous case falls through to this case
*/ */
boolean isLastBlock; boolean isFallThrough = false;
/** /**
* All variables used somewhere inside this block. * True, if this is the last case in a switch
*/
VariableSet allUsed;
/**
* Do we want braces around the case block (if a sub block
* declares a variable).
*/ */
boolean wantBraces; boolean isLastBlock = false;
public CaseBlock(int value) { public CaseBlock(int value) {
this.value = value; this.value = value;
@ -89,29 +84,40 @@ public class CaseBlock extends StructuredBlock {
return true; return true;
} }
public VariableSet propagateUsage() {
/* We remember if this sub block uses some variables, to introduce
* braces around this block in that case.
*/
return (allUsed = super.propagateUsage());
}
/** /**
* Make the declarations, i.e. initialize the declare variable * Tells if we want braces around this case. We only need braces
* to correct values. This will declare every variable that * if there is a declaration on the first level (list of
* is marked as used, but not done. * sequential blocks).
* @param done The set of the already declare variables.
*/ */
public void makeDeclaration(VariableSet done) { protected boolean wantBraces() {
java.util.Enumeration enum = allUsed.elements(); StructuredBlock block = subBlock;
while (enum.hasMoreElements()) { if (block == null)
jode.decompiler.LocalInfo li = (jode.decompiler.LocalInfo) enum.nextElement(); return false;
if (!done.contains(li)) { for (;;) {
wantBraces = true; if (!block.declare.isEmpty()) {
break; /* A declaration; we need braces. */
} return true;
} }
super.makeDeclaration(done);
if (!(block instanceof SequentialBlock)) {
/* This was the last block on the first level.
* If we get here, we need no braces.
*/
return false;
}
StructuredBlock[] subBlocks = block.getSubBlocks();
if (subBlocks[0] instanceof InstructionBlock
&& !subBlocks[0].declare.isEmpty()) {
/* An instruction block declares on the same level as
* the surrounding SequentialBlock.
*/
return true;
}
/* continue with the second sub block. */
block = subBlocks[1];
}
} }
/** /**
@ -127,29 +133,58 @@ public class CaseBlock extends StructuredBlock {
throws java.io.IOException throws java.io.IOException
{ {
if (isDefault) { if (isDefault) {
/* If this is the default case and does nothing, we can
* skip this.
* We have to make sure, that nothing flows into the default
* block though. XXX remove if sure.
*/
if (isLastBlock if (isLastBlock
&& subBlock instanceof EmptyBlock && subBlock instanceof EmptyBlock
&& subBlock.jump == null) && subBlock.jump == null)
return; return;
if (subBlock instanceof BreakBlock
&& ((BreakBlock) subBlock).breaksBlock == this) {
/* make sure that the previous block is correctly breaked */
if (isFallThrough) {
writer.tab();
subBlock.dumpSource(writer);
writer.untab();
}
return;
}
if (isFallThrough) {
writer.tab();
writer.print("/* fall through */");
writer.untab();
}
writer.print("default:"); writer.print("default:");
} else { } else {
if (isFallThrough) {
writer.tab();
writer.print("/* fall through */");
writer.untab();
}
ConstOperator constOp = new ConstOperator ConstOperator constOp = new ConstOperator
(((SwitchBlock)outer).getInstruction().getType(), (((SwitchBlock)outer).getInstruction().getType(),
Integer.toString(value)); Integer.toString(value));
constOp.makeInitializer(); constOp.makeInitializer();
writer.print("case " + constOp.toString() + ":"); writer.print("case " + constOp.toString() + ":");
} }
if (wantBraces)
writer.openBrace();
else
writer.println();
if (subBlock != null) { if (subBlock != null) {
writer.tab(); boolean needBraces = wantBraces();
subBlock.dumpSource(writer); if (needBraces)
writer.untab(); writer.openBrace();
} else
if (wantBraces) writer.println();
writer.closeBrace(); if (subBlock != null) {
writer.tab();
subBlock.dumpSource(writer);
writer.untab();
}
if (needBraces)
writer.closeBrace();
} else
writer.println();
} }
/** /**

Loading…
Cancel
Save