字节码、库和 Java
如果我想为 Java 创建一种新语言,我应该制作一个能够生成与 JVM 规范兼容的字节码的编译器,对吗?还有 JDK 库吗?
我在哪里可以找到一些信息?
谢谢。
If I wanted to create a new language for Java I should make a compiler that is able to generate the byte-code compatible with the JVM spec, right? and also for the JDK libraries?
Where can I find some info?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
取决于你所说的“为Java创建一种新语言”的意思——你的意思是一种编译为字节码的语言,并且它生成的代码可以在任何Java应用程序(例如Groovy)中使用,还是一种解释性语言(你想要用 Java 编写一个解析器)?
如果是前一种,那么 @Joachim 是对的,请查看 JVM 规范;对于后者,请查看 JavaCC 等为您的语言语法创建解析器的工具。
Depends what you mean by "create a new language for Java" -- do you mean a language that compiles to bytecode and the code it generates can be used from any Java app (e.g. Groovy) or an interpreted language (for which you want to write a parser in Java)?
If it is the former one then @Joachim is right, look at the JVM spec; for the latter look at the likes of JavaCC for creating a parser for your language grammar.
我将从生成 Java 源代码的编译器开始。您可能会发现这更容易阅读/理解/调试。稍后您可以优化它以生成字节代码。
编辑:
如果您具有无法轻松转换为 Java 代码的功能,您应该能够使用 Jasmin 创建少量字节代码类,并具有您可以测试到死的所有奇异功能。从生成的 Java 代码来看,这看起来像一个普通的方法调用。 JVM 仍然可以内联该方法,因此这可能根本不会影响性能。
I would start with a compiler which produced Java source. You may find this easier to read/understand/debug. Later you can optimise it to produce byte code.
EDIT:
If you have features which cannot be easily translated to Java code, you should be able to create a small number of byte code classes using Jasmin with all the exotic functionality which you can test to death. From the generated Java code this will look like a plain method call. The JVM can still inline the method so this might not impact performance at all.
Java 虚拟机规范 应该包含大部分你需要什么。
The Java Virtual Machine Spec should have most of what you need.
ASM 是一个优秀的字节码生成/操作库:http://asm.ow2.org。
它用途广泛且灵活。但请注意,它的 API 基于事件(类似于 Sax 解析器) - 它会读取 .class 文件并在遇到新实体(类声明、方法声明、语句等)时调用方法。一开始这可能看起来有点尴尬,但它节省了大量内存(与替代方案相比:库读取输入,吐出一个完全进化的树结构,然后你必须迭代它)。
An excellent library for bytecode generation/manipulation is ASM: http://asm.ow2.org.
It is very versatile and flexible. Note however that it's API is based on events (similar to Sax parsers) - it reads .class files and invokes a method whenever it encounters a new entity (class declaration, method declaration, statements, etc.). This may seem a bit awkward at first, but it saves a lot of memory (compared to the alternative: the library read the input, spits out a fully-evolved tree structure and then you have to iterate over it).
我认为这在实践中没有多大帮助,但它有很多有趣的理论知识,我认为你会发现它们很有用。
http://www.codeproject.com/KB/recipes/B32Machine1.aspx
I don't think this will help much practically, but it has a lot of sweet theoretical stuff that I think you'll find useful.
http://www.codeproject.com/KB/recipes/B32Machine1.aspx