changeset 85:6a525c466148

add support for packed and sparse switch - implement packed/sparse switch in Rewriter - enable switch test case
author forax
date Sun, 27 Mar 2011 12:33:41 +0200
parents bd1ebca7cdec
children 953c5b35ad79
files src/main/java/org/icedrobot/daneel/rewriter/DexRewriter.java src/test/java/org/icedrobot/daneel/rewriter/SwitchTest.java
diffstat 2 files changed, 40 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/org/icedrobot/daneel/rewriter/DexRewriter.java	Sat Mar 26 18:45:49 2011 +0100
+++ b/src/main/java/org/icedrobot/daneel/rewriter/DexRewriter.java	Sun Mar 27 12:33:41 2011 +0200
@@ -940,6 +940,45 @@
                     type);
         }
         
+        @Override
+        public void visitInstrPackedSwitch(Opcode opcode, int vsrc,
+                int firstKey, Label[] targets) {
+            fixStackAfterAMethodCallOrAnExceptionHandler();
+            vsrc = registerToSlot(vsrc);
+
+            int length = targets.length;
+            org.objectweb.asm.Label[] asmLabels = new org.objectweb.asm.Label[length];
+            for (int i = 0; i < length; i++) {
+                asmLabels[i] = getASMLabel(targets[i]);
+            }
+
+            org.objectweb.asm.Label defaultLabel = new org.objectweb.asm.Label();
+            mv.visitVarInsn(ILOAD, vsrc);
+            interpreter.load(vsrc, INT_TYPE);
+            mv.visitTableSwitchInsn(firstKey, firstKey + length - 1,
+                    defaultLabel, asmLabels);
+            mv.visitLabel(defaultLabel);
+        }
+
+        @Override
+        public void visitInstrSparseSwitch(Opcode opcode, int vsrc, int[] keys,
+                Label[] targets) {
+            fixStackAfterAMethodCallOrAnExceptionHandler();
+            vsrc = registerToSlot(vsrc);
+            
+            int length = targets.length;
+            org.objectweb.asm.Label[] asmLabels = new org.objectweb.asm.Label[length];
+            for (int i = 0; i < length; i++) {
+                asmLabels[i] = getASMLabel(targets[i]);
+            }
+
+            org.objectweb.asm.Label defaultLabel = new org.objectweb.asm.Label();
+            mv.visitVarInsn(ILOAD, vsrc);
+            interpreter.load(vsrc, INT_TYPE);
+            mv.visitLookupSwitchInsn(defaultLabel, keys, asmLabels);
+            mv.visitLabel(defaultLabel);
+        }
+        
         
         // unimplemented
         
@@ -959,21 +998,7 @@
             throw new UnsupportedOperationException("NYI " + opcode);
         }
 
-        @Override
-        public void visitInstrPackedSwitch(Opcode opcode, int vsrc,
-                int firstKey, Label[] targets) {
-            fixStackAfterAMethodCallOrAnExceptionHandler();
-            vsrc = registerToSlot(vsrc);
-            throw new UnsupportedOperationException("NYI " + opcode);
-        }
-
-        @Override
-        public void visitInstrSparseSwitch(Opcode opcode, int vsrc, int[] keys,
-                Label[] targets) {
-            fixStackAfterAMethodCallOrAnExceptionHandler();
-            vsrc = registerToSlot(vsrc);
-            throw new UnsupportedOperationException("NYI " + opcode);
-        }
+        
 
         @Override
         public void visitEnd() {
--- a/src/test/java/org/icedrobot/daneel/rewriter/SwitchTest.java	Sat Mar 26 18:45:49 2011 +0100
+++ b/src/test/java/org/icedrobot/daneel/rewriter/SwitchTest.java	Sun Mar 27 12:33:41 2011 +0200
@@ -44,7 +44,6 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
-@org.junit.Ignore("Switches not yet implemented")
 @RunWith(DexifyingRunner.class)
 public class SwitchTest {