Booting a JVM

Posted by Mike Haller on Tuesday, March 1. 2011 at 23:49 in Java
I'm right in the middle of implementing an interpreter for JVM bytecode. Today was the first time the system printed out the magic "Hello World!" message.

The interpreted boot time for running on a JDK 1.6 host (i'm reusing their classes) is 4667ms. It takes 10ms to execute System.out.printlnt("Hello World!");.

Starting up the Virtual Machine and executing any class is pretty easy:
final VirtualMachine vm = new VirtualMachine();

final Classpath classpath = new Classpath();
classpath.addLoader(new HostVMResourceLoader()); // Re-uses JDK classes
classpath.addFolder("C:/Example/bin"); // The HelloWorld project
vm.setClasspath(classpath);

vm.start();

vm.execute("org/example/HelloWorld");


The output can be seen in the screenshot:




The following is an histogram of the bytecodes being executed, only to start up and initialize the JVM.
That includes booting up the System class, character sets, ThreadGroups, Reflection, NIO, and a lot of native methods which had to be replaced.
[01] aconst_null                3398 invocations
[02] iconst_m1                    31 invocations
[03] iconst_0                  33689 invocations
[04] iconst_1                  11795 invocations
[05] iconst_2                    445 invocations
[06] iconst_3                    181 invocations
[07] iconst_4                    121 invocations
[08] iconst_5                     57 invocations
[09] lconst_0                     18 invocations
[0a] lconst_1                      3 invocations
[0b] fconst_0                     19 invocations
[10] bipush                    58215 invocations
[11] sipush                      222 invocations
[12] ldc                        1287 invocations
[13] ldc_w                       494 invocations
[14] ldc2_w                       15 invocations
[15] iload                    362633 invocations
[16] lload                         5 invocations
[19] aload                     81878 invocations
[1a] iload_0                  100335 invocations
[1b] iload_1                  236106 invocations
[1c] iload_2                    1208 invocations
[1d] iload_3                    1383 invocations
[1e] lload_0                       3 invocations
[1f] lload_1                       4 invocations
[20] lload_2                       6 invocations
[22] fload_0                      15 invocations
[24] fload_2                      52 invocations
[25] fload_3                      24 invocations
[26] dload_0                       1 invocations
[2a] aload_0                  306476 invocations
[2b] aload_1                   43405 invocations
[2c] aload_2                   25744 invocations
[2d] aload_3                   83195 invocations
[32] aaload                      794 invocations
[33] baload                        6 invocations
[34] caload                    78421 invocations
[36] istore                   161005 invocations
[37] lstore                        2 invocations
[3a] astore                    26685 invocations
[3b] istore_0                      2 invocations
[3c] istore_1                    907 invocations
[3d] istore_2                    121 invocations
[3e] istore_3                    185 invocations
[3f] lstore_0                      1 invocations
[40] lstore_1                      2 invocations
[41] lstore_2                      3 invocations
[4b] astore_0                     35 invocations
[4c] astore_1                    742 invocations
[4d] astore_2                   2411 invocations
[4e] astore_3                  19006 invocations
[4f] iastore                      20 invocations
[53] aastore                    1654 invocations
[54] bastore                      19 invocations
[55] castore                     265 invocations
[57] pop                         808 invocations
[58] pop2                          4 invocations
[59] dup                        4418 invocations
[5a] dup_x1                        4 invocations
[5c] dup2                          1 invocations
[60] iadd                      86425 invocations
[61] ladd                          1 invocations
[64] isub                      61816 invocations
[68] imul                        814 invocations
[6a] fmul                         11 invocations
[70] irem                        135 invocations
[78] ishl                          8 invocations
[7a] ishr                          9 invocations
[7c] iushr                         8 invocations
[7e] iand                        163 invocations
[7f] land                          1 invocations
[80] ior                       25086 invocations
[81] lor                           1 invocations
[82] ixor                          8 invocations
[84] iinc                      29597 invocations
[85] i2l                           1 invocations
[86] i2f                          17 invocations
[8b] f2i                          11 invocations
[91] i2b                          13 invocations
[92] i2c                       25083 invocations
[94] lcmp                         13 invocations
[95] fcmpl                        19 invocations
[96] fcmpg                        19 invocations
[99] ifeq                      25358 invocations
[9a] ifne                      42109 invocations
[9b] iflt                     102593 invocations
[9c] ifge                      12405 invocations
[9d] ifgt                         27 invocations
[9e] ifle                       6280 invocations
[9f] if_icmpeq                 51280 invocations
[a0] if_icmpne                   813 invocations
[a1] if_icmplt                 77656 invocations
[a2] if_icmpge                 52068 invocations
[a3] if_icmpgt                    73 invocations
[a4] if_icmple                    52 invocations
[a5] if_acmpeq                  1612 invocations
[a6] if_acmpne                  2072 invocations
[a7] goto                      66816 invocations
[a8] jsr                           1 invocations
[a9] ret                           1 invocations
[ab] lookupswitch                  1 invocations
[ac] ireturn                  164404 invocations
[ad] lreturn                       6 invocations
[b0] areturn                   14314 invocations
[b1] return                     6938 invocations
[b2] getstatic                 39043 invocations
[b3] putstatic                   201 invocations
[b4] getfield                 310885 invocations
[b5] putfield                  15212 invocations
[b6] invokevirtual            102940 invocations
[b7] invokespecial              3603 invocations
[b8] invokestatic              65929 invocations
[b9] invokeinterface           13227 invocations
[bb] new                         950 invocations
[bc] newarray                     40 invocations
[bd] anewarray                   477 invocations
[be] arraylength                 764 invocations
[c0] checkcast                 23360 invocations
[c1] instanceof                   55 invocations
[c2] monitorenter                151 invocations
[c3] monitorexit                 151 invocations
[c6] ifnull                    15595 invocations
[c7] ifnonnull                 19950 invocations

Total: 3118655



Three million bytecode instructions executed to get a little message on the screen.


Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications
 
Submitted comments will be subject to moderation before being displayed.
 

About

My name is Mike Haller and I'm a software developer and architect at Bosch Software Innovations in Germany. I love programming, playing games and reading books. I like good food, making photos and learning and mentoring about the craftsmanship of commercial software development. Stack Overflow profile for mhaller

Quicksearch