tags:

views:

409

answers:

9

I was briefly reading about Maxine which is an open source JVM implementation that written in Java. This sounds circular to me. If java requires a virtual machine to run in, how can the virtual machine itself be written in Java (won't the VM code require a VM in which to run, and so on?).

Edit: Ok, so I see I overlooked the fact that Java doesn't have to run in a VM. How then does one explain how a LISP compiler can be written in LISP? Or should this be a new question altogether?

+3  A: 

Your assumption that Java requires a virtual machine is incorrect to begin with.

Ignacio Vazquez-Abrams
The java *language* may not require a VM, but the term "java" covers more than just the language, it also includes the VM.
skaffman
And Maxine at least seems to require a JDK1.6 JVM to run on top of.
Thilo
@kji: I think I was mistaken. It seems that Maxine does compile to a native executable, so no need for the JDK JVM after building Maxine initally (it still needs to class library, I believe).
Thilo
A: 

Java code can be compiled directly to machine code so that a virtual machine is not needed.

macleojw
A: 

You just compile the Java to machine code rather than Java byte code.

ecounysis
+1  A: 

You can have a look at the well-established method of bootstrapping compilers. I think it started in the 70s...

mlaverd
+2  A: 

The JVM that you need to bootstrap a JVM written in Java probably does not need a lot of features (such as garbage collection and JIT), could be very simple. All the more advanced features could then be implemented in Java (which seems to be exactly the point of Maxine, to experiment with new ideas in JVM technology).

Also, Maxine does contain C code, which I guess makes up a minimal runtime environment that is used to get the rest of Maxine going. I take it that the interesting bits (JIT compiler, garbage collection) are then completely implemented in Java.

Thilo
good explanation, thanks
kji
Nope. The C code in Maxine is more used like a data definition language, it doesn't actually implement anything interesting. The operating system expects certain structures to be laid out in a certain specific way in memory, and the C compiler knows how to do this, so Maxine uses C to get these structures laid out correctly. But there are only few places that use C: a minimal launcher, which loads the VM image into memory, writes the load address into a specific location inside the image and then jumps to another specific location. The debugger, because Maxine uses the OS's C debug facilities.
Jörg W Mittag
The low-level part of JNI, because, well tho whole point of JNI is to integrate with C. And the low-level part of threading, because Maxine uses native threads which are wildly different from system to system.
Jörg W Mittag
@Jörg W Mittag: so, does Maxine still run the normal JVM? the VM image that is being loaded is a native binary, right? How is that being created?
Thilo
All modern JVMs have a JIT compiler, which compiles JVM bytecode to native machinecode. Normally, only the "hot" parts of the code are being compiled (the rest is being interpreted) and normally, the code is held in memory and discarded when the program exits. But there is no reason why you couldn't JIT compile *all* the code and there is no reason why you couldn't write the code to disk, which is exactly what Maxine does. Oracle's JRockijt JVM and Google's V8 JavaScript compiler for example also keep compiled native code on disk, although in their case it's just for caching reasons.
Jörg W Mittag
+2  A: 

See bootstrapping.

yawn
+3  A: 

You are asking about the chicken and the egg.

Read: http://en.wikipedia.org/wiki/Bootstrapping_%28compilers%29

Yuval A
A: 

It is kinda 'whooaoaa man, how can that work???' - but I think you are describing the phenomenon known as 'self-hosting':

Languages (or toolchains/platforms) don't start out as self-hosting - they start off life having been built on an existing platform: at a certain point they become functional enough to allow programs to be written which understand the syntax which it itself happens to be written in.

There is a great example in the classic AWK book, which introduces an AWK program which can parse (a cut-down version as it happens) other AWK programs: see link below.

There is another example in the book "Beautiful Code" which has a Javascript program which can parse Javascript.

I think the thing to remember on this - if you have (say) a JVM written in Java which can therefore run Java Byte code: the JVM which runs the Java JVM itself has to be hosted natively (perhaps this JVM was written in 'C' and then compiled to machine code) : this is true in any case of a self-hosting program eventually - somewhere along the line.

So the mystery is removed - because at some point, there is a native machine-code program running below everything.

It kinda of equivalent of being able to describe the English (etc) language using the English language itself....maybe...

http://www.amazon.co.uk/AWK-Programming-Language-Alfred-Aho/dp/020107981X/ref=sr_1_fkmr0_3?ie=UTF8&qid=1266397076&sr=8-3-fkmr0

http://www.amazon.co.uk/gp/search/ref=a9_sc_1?rh=i%3Astripbooks%2Ck%3Abeautiful+code&keywords=beautiful+code&ie=UTF8&qid=1266397435

http://en.wikipedia.org/wiki/Self-hosting

monojohnny
+1  A: 

I had a look at Maxine last week and was wondering the same :)

From the Maxine documentation:

1 Building the boot image

Now let's build a [boot image]. In this step, Maxine runs on a host JVM to configure a prototype, then compiles its own code and data to create an executable program for the target platform.

2 Running Maxine

Now that Maxine has compiled itself, we can run it as a standard Java VM. The max vm command handles the details of class and library paths and provides an interface similar to the standard java launcher command.

ewernli