Skip to content

Commit

Permalink
feat: Omittable break label.
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Mar 20, 2023
1 parent cf8b475 commit 1a30239
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
17 changes: 13 additions & 4 deletions src/main/java/reincarnation/OperandSwitch.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,24 @@ void analyze(NodeCreator creator) {
cases.remove(defaultNode);

nodes().to(Node::hideIncoming);
nodes().flatMap(node -> node.outgoingRecursively().take(n -> !n.hasDominator(node)).first()).distinct().to().to(node -> {
follow = node;
}, () -> {
List<Node> candidates = nodes().flatMap(node -> node.outgoingRecursively().take(n -> !n.hasDominator(node)).first())
.distinct()
.toList();

if (candidates.isEmpty()) {
follow = defaultNode;
follow.revealIncoming();
defaultNode = null;
});
} else {
for (Node candidate : candidates) {
System.out.println("@@@ " + candidate.id);
follow = candidate;
}
}
nodes().to(Node::revealIncoming);

System.out.println(condition + " " + follow.id + " @ " + defaultNode);

if (defaultNode != null) {
List<Node> cases = nodes().toList();
List<Node> incomings = I.signal(follow.getPureIncoming()).take(n -> n.hasDominatorAny(cases)).toList();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/reincarnation/coder/Coder.java
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,9 @@ public final void writeEnclose(Code code) {
* Break statement.
*
* @param label A label of break point.
* @param omitLabel TODO
*/
public abstract void writeBreak(Optional<String> label);
public abstract void writeBreak(Optional<String> label, boolean omitLabel);

/**
* Continue statement.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/reincarnation/coder/DelegatableCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ public void writeThrow(Code code) {
* {@inheritDoc}
*/
@Override
public void writeBreak(Optional<String> label) {
coder.writeBreak(label);
public void writeBreak(Optional<String> label, boolean omitLabel) {
coder.writeBreak(label, omitLabel);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/reincarnation/coder/java/JavaCoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,8 @@ public void writeThrow(Code code) {
* {@inheritDoc}
*/
@Override
public void writeBreak(Optional<String> label) {
line("break", label.map(v -> space + "l" + v), ";");
public void writeBreak(Optional<String> label, boolean omitLabel) {
line("break", omitLabel ? "" : label.map(v -> space + "l" + v), ";");
}

/**
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/reincarnation/structure/Break.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
*/
package reincarnation.structure;

import java.util.LinkedList;

import kiss.I;
import kiss.Variable;
import reincarnation.Node;
import reincarnation.coder.Coder;

/**
* @version 2018/10/31 0:36:33
*/
public class Break extends Jumpable<Breakable> {

/** The omit state. */
protected final Variable<Boolean> hasFollowers = Variable.of(false);

/**
* Build break statement.
*
Expand All @@ -26,11 +30,22 @@ public Break(Node that, Breakable breakable) {
super(that, breakable);
}

/**
* {@inheritDoc}
*/
@Override
protected void analyze() {
LinkedList<Structure> ancestors = ancestor().takeUntil(s -> s instanceof Loopable).toCollection(new LinkedList());

I.signal(ancestors).skip(breakable).flatMap(Structure::follower).skip(Structure::isEmpty).isEmitted().to(hasFollowers);
I.signal(ancestors).as(Breakable.class).first().is(s -> s == breakable).to(omitLabel);
}

/**
* {@inheritDoc}
*/
@Override
public void writeCode(Coder coder) {
coder.writeBreak(breakable.label());
coder.writeBreak(breakable.label(), omitLabel.v);
}
}
8 changes: 4 additions & 4 deletions src/test/java/reincarnation/decompiler/flow/SwitchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -733,18 +733,18 @@ public int run(@Param(from = 0, to = 10) int param) {
case 0:
switch (param % 3) {
case 0:
value = 3;
value += 1;
break root;

case 1:
value += 1;
value += 2;
break;

default:
value = -3;
value = 3;
break;
}
value += 1;
value += 4;
}
return value;
}
Expand Down

0 comments on commit 1a30239

Please sign in to comment.