diff --git a/src/test/java/haidnor/jvm/test/TestJVM.java b/src/test/java/haidnor/jvm/test/TestJVM.java index b5541e5..69381a2 100644 --- a/src/test/java/haidnor/jvm/test/TestJVM.java +++ b/src/test/java/haidnor/jvm/test/TestJVM.java @@ -72,6 +72,26 @@ public class TestJVM { runMainClass(Demo7.class); } + @Test + public void demo_while() throws Exception { + runMainClass(demo_while.class); + } + + @Test + public void demo_doWhile() throws Exception { + runMainClass(demo_doWhile.class); + } + + @Test + public void demo_for() throws Exception { + runMainClass(demo_for.class); + } + + @Test + public void demo_foreach() throws Exception { + runMainClass(demo_foreach.class); + } + @Test public void test_8() throws Exception { runMainClass(Demo8.class); diff --git a/src/test/java/haidnor/jvm/test/demo/demo_doWhile.java b/src/test/java/haidnor/jvm/test/demo/demo_doWhile.java new file mode 100644 index 0000000..3794892 --- /dev/null +++ b/src/test/java/haidnor/jvm/test/demo/demo_doWhile.java @@ -0,0 +1,27 @@ +package haidnor.jvm.test.demo; + +public class demo_doWhile { + public static void main(String[] args) { + int a = 0; + do { + a++; + } while (a < 2); + System.out.println(a); + } +} + +/* +main(String[] args): + 0 iconst_0 + 1 istore_1 + + 2 iinc 1 by 1 + 5 iload_1 + 6 iconst_2 + 7 if_icmplt 2 (-5) < while (a < 2); 符合条件则向前跳转 + + 10 getstatic #7 + 13 iload_1 + 14 invokevirtual #13 + 17 return + */ \ No newline at end of file diff --git a/src/test/java/haidnor/jvm/test/demo/demo_exception_3.java b/src/test/java/haidnor/jvm/test/demo/demo_exception_3.java index f86fd8c..57f9893 100644 --- a/src/test/java/haidnor/jvm/test/demo/demo_exception_3.java +++ b/src/test/java/haidnor/jvm/test/demo/demo_exception_3.java @@ -21,12 +21,12 @@ public class demo_exception_3 { } /* main(String[] args) - 0 iconst_0 - 1 istore_1 - 2 iload_1 - 3 invokestatic #7 - 6 goto 18 (+12) - 9 astore_2 + 0 iconst_0 + 1 istore_1 + 2 iload_1 + 3 invokestatic #7 + 6 goto 18 (+12) + 9 astore_2 10 getstatic #15 13 ldc #21 15 invokevirtual #23 @@ -40,12 +40,11 @@ main(String[] args) fun(int a) - 0 iload_0 - 1 ifne 12 (+11) - 4 new #29 - 7 dup - 8 invokespecial #31 : ()V> - 11 athrow - 12 return + 0 getstatic #15 + 3 iconst_1 + 4 iload_0 + 5 idiv + 6 invokevirtual #29 + 9 return */ \ No newline at end of file diff --git a/src/test/java/haidnor/jvm/test/demo/demo_exception_4.java b/src/test/java/haidnor/jvm/test/demo/demo_exception_4.java index 12fa687..c22e908 100644 --- a/src/test/java/haidnor/jvm/test/demo/demo_exception_4.java +++ b/src/test/java/haidnor/jvm/test/demo/demo_exception_4.java @@ -21,12 +21,12 @@ public class demo_exception_4 { } /* main(String[] args) - 0 iconst_0 - 1 istore_1 - 2 iload_1 - 3 invokestatic #7 - 6 goto 18 (+12) - 9 astore_2 + 0 iconst_0 + 1 istore_1 + 2 iload_1 + 3 invokestatic #7 + 6 goto 18 (+12) + 9 astore_2 10 getstatic #15 13 ldc #21 15 invokevirtual #23 @@ -40,12 +40,11 @@ main(String[] args) fun(int a) - 0 iload_0 - 1 ifne 12 (+11) - 4 new #29 - 7 dup - 8 invokespecial #31 : ()V> - 11 athrow - 12 return + 0 getstatic #15 + 3 iconst_1 + 4 iload_0 + 5 idiv + 6 invokevirtual #29 + 9 return */ \ No newline at end of file diff --git a/src/test/java/haidnor/jvm/test/demo/demo_for.java b/src/test/java/haidnor/jvm/test/demo/demo_for.java new file mode 100644 index 0000000..4f93b21 --- /dev/null +++ b/src/test/java/haidnor/jvm/test/demo/demo_for.java @@ -0,0 +1,31 @@ +package haidnor.jvm.test.demo; + +public class demo_for { + public static void main(String[] args) { + int a = 0; + for (int i = 0; i < 3; i++) { + a++; + } + System.out.println(a); + } +} + +/* +main(String[] args): + 0 iconst_0 < int a = 0 + 1 istore_1 + 2 iconst_0 < int i =0 + 3 istore_2 + + 4 iload_2 + 5 iconst_3 < 比较值3 + 6 if_icmpge 18 (+12) < (i < 3) + 9 iinc 1 by 1 < a++ + 12 iinc 2 by 1 < i++ + 15 goto 4 (-11) < 向前跳转代码 + + 18 getstatic #7 + 21 iload_1 + 22 invokevirtual #13 + 25 return + */ \ No newline at end of file diff --git a/src/test/java/haidnor/jvm/test/demo/demo_foreach.java b/src/test/java/haidnor/jvm/test/demo/demo_foreach.java new file mode 100644 index 0000000..061610c --- /dev/null +++ b/src/test/java/haidnor/jvm/test/demo/demo_foreach.java @@ -0,0 +1,62 @@ +package haidnor.jvm.test.demo; + +public class demo_foreach { + public static void main(String[] args) { + int[] arr = new int[]{999, 333}; + for (int i : arr) { + System.out.println(i); + } + } +} + +/* +main(String[] args): + 0 iconst_2 < 操作数栈压入2,这个是 arr[] 的长度 + 1 newarray 10 (int) < 创建数组 + 3 dup + + 4 iconst_0 + 5 sipush 999 + 8 iastore < arr[0] = 999 + + 9 dup < 复制栈顶的 arr[], 压入操作数栈 + + 10 iconst_1 + 11 sipush 333 + 14 iastore < arr[1] = 333 + + 15 astore_1 + 16 aload_1 + 17 astore_2 + 18 aload_2 + 19 arraylength + 20 istore_3 + 21 iconst_0 + 22 istore 4 + + 24 iload 4 + 26 iload_3 + 27 if_icmpge 50 (+23) + 30 aload_2 + 31 iload 4 + 33 iaload + 34 istore 5 + 36 getstatic #7 + 39 iload 5 + 41 invokevirtual #13 + 44 iinc 4 by 1 + 47 goto 24 (-23) + + 50 return + ++--------+-------+--------+--------+-----------+-------------------------+ +| Nr. | 起始PC | 长度 | 序号 | 名字 | 描述符(存储类型) | ++--------+-------+--------+--------+-----------+-------------------------+ +| 0 | 0 | 51 | 0 | args | [Ljava/lang/String; | ++--------+-------+--------+--------+-----------+-------------------------+ +| 1 | 16 | 35 | 1 | arr | [I | ++--------+-------+--------+--------+-----------+-------------------------+ +| 2 | 36 | 8 | 5 | i | I | ++--------+-------+--------+--------+-----------+-------------------------+ + + */ \ No newline at end of file diff --git a/src/test/java/haidnor/jvm/test/demo/demo_while.java b/src/test/java/haidnor/jvm/test/demo/demo_while.java new file mode 100644 index 0000000..bb08e63 --- /dev/null +++ b/src/test/java/haidnor/jvm/test/demo/demo_while.java @@ -0,0 +1,29 @@ +package haidnor.jvm.test.demo; + +public class demo_while { + public static void main(String[] args) { + int a = 0; + while (a < 2) { + a++; + } + System.out.println(a); + } +} + +/* +main(String[] args): + 0 iconst_0 + 1 istore_1 + 2 iload_1 + 3 iconst_2 + + 4 if_icmpge 13 (+9) < 符合条件就进入循环体 + 7 iinc 1 by 1 < a++ + 10 goto 2 (-8) < 向前跳转指令 + + 13 getstatic #7 + 16 iload_1 + 17 invokevirtual #13 + 20 return + + */ \ No newline at end of file