如何用 Java 编写 JVM

发布于 2024-08-22 05:23:21 字数 281 浏览 9 评论 0原文

我简要地阅读了 Maxine,它是一个用 Java 编写的开源 JVM 实现。这对我来说听起来很循环。如果java需要虚拟机来运行,那么虚拟机本身如何用Java编写(VM代码是否需要VM来运行等等?)。

编辑:好的,所以我发现我忽略了 Java 不必在虚拟机中运行的事实。那么如何解释如何用 LISP 编写 LISP 编译器呢?或者这应该是一个新问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

红墙和绿瓦 2024-08-29 05:23:21

您认为 Java 需要虚拟机的假设一开始就是错误的。查看项目 GCJ:GNU 编译器Java 编程语言

Your assumption that Java requires a virtual machine is incorrect to begin with. Check out the project GCJ: The GNU Compiler for the Java Programming Language.

握住你手 2024-08-29 05:23:21

你问的是先有鸡还是先有蛋的问题。

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

You are asking about the chicken and the egg.

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

送君千里 2024-08-29 05:23:21

引导用 Java 编写的 JVM 所需的 JVM 可能不需要很多功能(例如垃圾收集和 JIT),可能非常简单。所有更高级的功能都可以用 Java 实现(这似乎正是 Maxine 的目的,尝试 JVM 技术的新想法)。

另外,Maxine 确实包含 C 代码,我猜它构成了一个最小的运行时环境,用于让 Maxine 的其余部分运行。我认为有趣的部分(JIT 编译器、垃圾收集)随后完全在 Java 中实现。

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.

爱给你人给你 2024-08-29 05:23:21

Java代码可以直接编译为机器代码,因此不需要虚拟机。

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

偏闹i 2024-08-29 05:23:21

上周我看了 Maxine,也有同样的疑问:)

来自 Maxine 文档

1 构建启动镜像

现在让我们构建一个[启动映像]。在
这一步,Maxine运行在主机JVM上
配置原型,然后
编译自己的代码和数据
创建一个可执行程序
目标平台。

2 跑步玛克辛

现在 Maxine 已经自行编译了,
我们可以将它作为标准 Java VM 来运行。
max vm 命令处理细节
类和库路径以及
提供了类似的接口
标准 java 启动器命令。

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.

○愚か者の日 2024-08-29 05:23:21

您可以查看完善的引导编译器的方法。我想这件事是从上世纪70年代开始的……

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

何以笙箫默 2024-08-29 05:23:21

这有点像“哇哦伙计,这怎么行???” - 但我认为你正在描述一种称为“自托管”的现象:

语言(或工具链/平台)并不是从自托管开始的 - 它们一开始就建立在现有平台上:在某个时刻它们的功能足以允许编写的程序能够理解它本身所使用的语法。

经典的 AWK 书中有一个很好的例子,它介绍了一个可以解析的 AWK 程序(一个简化版本,因为它发生)其他 AWK 程序:请参阅下面的链接。

《美丽代码》一书中还有一个例子,其中有一个可以解析Javascript的Javascript程序。

我认为要记住这一点 - 如果你有一个用 Java 编写的 JVM,因此可以运行 Java 字节代码:运行 Java JVM 本身的 JVM 必须本地托管(也许这个 JVM 是用“C”编写的) ',然后编译为机器代码):最终在任何自托管程序的情况下都是如此 - 沿线的某个地方。

所以这个谜团就被消除了——因为在某些时候,有一个本机机器代码程序在所有东西下面运行。

它有点相当于能够使用英语本身来描述英语(等)语言....也许...

http://www.amazon.co.uk/AWK-编程语言-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

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

吹泡泡o 2024-08-29 05:23:21

我知道这篇文章很旧,但我想我可以在讨论中添加一些内容,因为它们是被遗漏的要点。所以未来的读者可能会发现这很有帮助。

我不知道大家是否没有抓住这里的要点。您可以用几乎任何语言编写几乎任何类型的编译器、解释器或虚拟机。当使用C编写C编译器时,需要一个C编译器来编译新的编译器。但是,输出是在指定平台上运行的本机代码。仅仅因为 JVM 是用在 JVM 上运行的语言编写的,并不意味着输出必须生成在 JVM 上运行的代码。例如,您可以用 Java 编写 C、Basic、Pascal 编译器甚至汇编器。在这种情况下,您将需要 JVM 来创建编译器或汇编器,但一旦创建,如果初始代码生成本机代码,您可能不再需要 JVM。另一种方法是编写一个翻译器,将输入语言转换为本地机器语言,以便您用语言 A 编写程序,该程序编译为语言 B,然后语言 B 编译为机器代码。在微控制器世界中,这种情况屡见不鲜。有人想用 Basic 或 Java 编写程序,因此他们编写 Basic/Java 编译器来为现有 C 编译器生成 C 代码。然后,生成的 C 代码被编译为机器语言,提供本地 Basic/Java 编译器。这种方法通常比直接用机器代码编写 Basic/Java 编译器更容易。

许多年前,我编写了 BasicA 和 GWBasic 程序,为 6800 和 Z80 微控制器生成汇编代码。我的观点是,输出不必与输入或目标相同。 IE 仅仅因为您用 Java 编写 JVM 并不意味着最终结果必须在 Java JVM 下运行。

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.

忱杏 2024-08-29 05:23:21

这是一篇关于引导自托管虚拟机的好论文。它不是Java,而是javascript,但原理是一样的。

为 JavaScript 引导自托管研究虚拟机:体验报告

请注意,虽然引导自托管编译器和引导自托管虚拟机有些相似,我相信它们不会提出完全相同的挑战。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文