如何创建JVM编程语言? [英] How to approach creating a JVM programming language?

查看:75
本文介绍了如何创建JVM编程语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用C语言(使用Lex& Bison)创建了一种编译器,用于一种动态类型的编程语言,该语言支持循环,函数内部的函数声明,递归调用等。我还创建了一个虚拟机,该虚拟机运行由

I have created a compiler in C (using Lex & Bison) for a dynamic typed programming language that supports loops, functions declarations inside functions, recursive calls, etc. I also created a virtual machine that runs the intermediate code created by the compiler.

我正在考虑将其编译为Java字节码而不是自己的中间代码。

I was thinking about compiling it to Java bytecode instead of my own intermediate code.

我看到了有关创建JVM的问题语言已经要求,但是我没有找到

I saw that the question about creating a JVM language has already been asked, but I don’t find the answer very informative.

所以这是我的问题:


  1. 我想为JVM创建一种语言是要阅读 JVM规范,您还能建议其他哪些书籍(当然还有《龙书》除外)?我最关心的是有关如何创建JVM语言的书籍或教程,而不是一般的编译器。

  2. 有许多Java库可以读取,编写和更改 .class 文件,例如 jclasslib bcel gnu字节码,等等。您会建议哪一个?另外,您是否知道C库也可以完成相同的工作?

  3. 我当时正在考虑看看另一种针对JVM的语言,例如Clojure,Jython或JRuby。但是所有这些语言都是非常高级和复杂的(为它们创建编译器)。我一直在寻找一种针对JVM且其编译器是开源的更简单的编程语言(不管它是未知的还是未使用的,我不介意)。有什么想法吗?

  1. I guess to create a language for JVM a must is to read the JVM specification book, what other books can you suggest (except Dragon Book of course)? I’m mostly concerned about books or tutorials on how to create a JVM language, not a compiler in general.
  2. There are many Java libraries to read, write and change .class files like jclasslib, bcel, gnu bytecode, etc. Which one would you suggest? Also, are you aware of C libraries that do the same job?
  3. I was thinking about having a look at maybe another language that targets the JVM like Clojure, Jython or JRuby. But all these languages are very high level and complicated (to create a compiler for them). I was looking for a simpler (I don't mind if it's unknown or unused) programming language that targets the JVM and it's compiler is open source. Any ideas?


推荐答案

我也建议使用ASM,但请参阅 Jasmin ,我在大学项目中使用了它(或者,不得不使用它),并且效果很好。我为使用Java和Jasmin的编程语言编写了lexer-parser-analyzer-optimizer-generator组合,因此它正在生成JVM代码。我在此处上传了代码;有趣的部分应该是源代码本身。在文件夹 bytecode / InsanelyFastByteCodeCreator.java 中,找到一段将AST Tree转换为Jasmin汇编器输入格式的代码。

I would also recommend ASM, but have a look at Jasmin, I used it (or, rather, had to use it) for a university project, and it worked quite well. I wrote a lexer-parser-analyzer-optimizer-generator combination for a programing language using Java and Jasmin, so it was generating JVM Code. I uploaded the code here; the interesting part should be the source code itself. In the folder bytecode/InsanelyFastByteCodeCreator.java, you find a piece of code which transforms an AST Tree into the input format of Jasmin assembler. It is quite straightforward.

源语言(由lexer-parser-analyzer转换为AST)是Java的一个子集,称为MiniJava。它缺少一些复杂功能,例如继承,构造函数,静态方法,私有字段和方法。这些功能都不是很难实现的,但是还有一个任务是编写x86后端(以便生成机器汇编程序),如果没有JVM来处理其中的某些事情,这些事情就会变得很困难。

The source language (which was transformed to the AST by the lexer-parser-analyzer) is a subset of Java called MiniJava. It lacks some "complicated" features like inheritance, constructors, static methods, private fields and methods. None of those features are difficult to implement, but there was another task to write an x86 backend (so to generate machine assembler), and those things tend to get difficult if you got no JVM which handles some of those things.

如果您想知道奇怪的类名:大学项目的任务是将AST转换为 SSA 图形(代表输入代码),优化图形,然后将其转换为Java字节码。这大约是项目工作的¾, InsanlyFastByteCodeCreator 只是测试所有内容的捷径。

In case you wonder about the strange class name: The task of the university project was to transform the AST into an SSA Graph (representing the input code), optimize the graph, and then turn it into Java bytecode. That was about ¾ of the work of the project and the InsanlyFastByteCodeCreator was just a short-cut to test everything.

看看Jon Meyer和Troy Downing的 Java虚拟机书。本书大量引用了Jasmin汇编程序。对于了解JVM内部结构很有帮助。

Have a look at the "Java Virtual Machine" book from Jon Meyer and Troy Downing. This book heavily references the Jasmin Assembler; it’s quite helpful for understanding the JVM internals.

这篇关于如何创建JVM编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆