How can a JVM be written in Java

后端 未结 10 602
再見小時候
再見小時候 2020-12-13 09:10

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 i

相关标签:
10条回答
  • 2020-12-13 09:35

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

    0 讨论(0)
  • 2020-12-13 09:35

    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

    0 讨论(0)
  • 2020-12-13 09:36

    Here is a good paper on bootstraping a self-hosted VM. It's not Java, but javascript, but the principles are the same.

    Bootstrapping a self-hosted research virtual machine for JavaScript: an experience report

    Note that while bootstraping a self-host compiler and bootstraping a self-hosted VM are somewhat similar, I believe they do not raise the exact same challenges.

    0 讨论(0)
  • 2020-12-13 09:43

    You are asking about the chicken and the egg.

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

    0 讨论(0)
  • 2020-12-13 09:43

    I know this post is old but I thought I might add a little to the discussion as they are points that have been missed. So future readers may find this helpful.

    I wonder if everyone is missing the point here. You can write most any kind of compiler, interpreter, or virtual machine in almost any language. When using C to write a C compiler a C compiler is needed to compile the new compiler. However, the output is native code that runs on the designated platform. Just because the JVM is written in the language that runs on the JVM doesn't mean the output must result in code that runs on the JVM. For instance you can write C, Basic, Pascal Compilers or even assemblers in Java. In this case you will need the JVM to create the compiler or assembler but once created you may no longer need the JVM if the initial code resulted in native code. Another approach is to write a translator that takes an input language and converts it to a native machine language so that you write your program in language A which compiles into language B which which is then compiled into machine code. In the micro controller world you see this a lot. Someone wants to write programs in Basic or Java so they write the Basic/Java compiler to produce C code for an existing C compiler. Then the resultant C code is compiled into machine language providing the native Basic/Java compiler. This approach is usually easier than writing the Basic/Java compiler directly in machine code.

    Many years ago I wrote BasicA and GWBasic programs that produced assembly code to 6800 and Z80 micros. My point is that the output need not be of the same ilk as the input or target. I.E. Just because you're writing a JVM in Java doesn't mean the final result must be ran under a Java JVM.

    0 讨论(0)
  • 2020-12-13 09:44

    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.

    0 讨论(0)
提交回复
热议问题