aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/couscous/util/asm/StackManipulationSwitch.java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-07-11 22:05:58 +0100
committerMichael Williamson <mike@zwobble.org>2016-07-12 22:20:03 +0100
commite6fbc62eb5e7531b74859df02ec6b06d09a8db5a (patch)
treecf145f468c6bdfbc567ac256d63c83d4a4be35d6 /src/main/java/org/zwobble/couscous/util/asm/StackManipulationSwitch.java
parent80cd27b6b9c79829d19ad6a1a04b037241fd0105 (diff)
Generate dynamic visitor
Diffstat (limited to 'src/main/java/org/zwobble/couscous/util/asm/StackManipulationSwitch.java')
-rw-r--r--src/main/java/org/zwobble/couscous/util/asm/StackManipulationSwitch.java72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/main/java/org/zwobble/couscous/util/asm/StackManipulationSwitch.java b/src/main/java/org/zwobble/couscous/util/asm/StackManipulationSwitch.java
new file mode 100644
index 0000000..2dbcd70
--- /dev/null
+++ b/src/main/java/org/zwobble/couscous/util/asm/StackManipulationSwitch.java
@@ -0,0 +1,72 @@
+package org.zwobble.couscous.util.asm;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Ordering;
+import net.bytebuddy.implementation.Implementation;
+import net.bytebuddy.implementation.bytecode.StackManipulation;
+import net.bytebuddy.jar.asm.Label;
+import net.bytebuddy.jar.asm.MethodVisitor;
+
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static org.zwobble.couscous.util.ExtraIterables.lazyMap;
+import static org.zwobble.couscous.util.ExtraLists.list;
+
+public class StackManipulationSwitch implements StackManipulation {
+ public static Case switchCase(int key, StackManipulation stackManipulation) {
+ return new Case(key, stackManipulation);
+ }
+
+ public static class Case {
+ private final int key;
+ private final StackManipulation stackManipulation;
+
+ private Case(int key, StackManipulation stackManipulation) {
+ this.key = key;
+ this.stackManipulation = stackManipulation;
+ }
+ }
+
+ private final StackManipulation defaultCase;
+ private final List<Case> cases;
+
+ public StackManipulationSwitch(StackManipulation defaultCase, List<Case> cases) {
+ this.defaultCase = defaultCase;
+ this.cases = Ordering.<Case>from((first, second) -> first.key - second.key).immutableSortedCopy(cases);
+ }
+
+ @Override
+ public boolean isValid() {
+ return true;
+ }
+
+ @Override
+ public Size apply(MethodVisitor methodVisitor, Implementation.Context implementationContext) {
+ Label defaultLabel = new Label();
+ Label[] caseLabels = Iterables.toArray(lazyMap(cases, switchCase -> new Label()), Label.class);
+
+ return new StackManipulation.Compound(ImmutableList.copyOf(Iterables.concat(
+ list(
+ new StackManipulationLookupTable(
+ defaultLabel,
+ cases.stream().mapToInt(switchCase -> switchCase.key).toArray(),
+ caseLabels
+ )
+ ),
+
+ lazyMap(cases, asList(caseLabels), (switchCase, label) -> new StackManipulation.Compound(
+ new StackManipulationLabel(label),
+ StackManipulationFrame.SAME,
+ switchCase.stackManipulation
+ )),
+
+ list(
+ new StackManipulationLabel(defaultLabel),
+ StackManipulationFrame.SAME,
+ defaultCase
+ )
+ ))).apply(methodVisitor, implementationContext);
+ }
+}