编译为 java 字节码(不使用 Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/688098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile to java bytecode (without using Java)
提问by Allyn
My compilers class is creating a language that we intend to compile to Java Bytecode. We have made plenty of progress and are nearing the time where it's time for code generation.
我的编译器类正在创建一种我们打算编译为 Java 字节码的语言。我们已经取得了很大的进展,并且接近代码生成的时间。
We are having problems locating information on how to create .class files from our compiler. Do you have any resources that can give us some assistance? We already have plenty of documentation on the instruction set, but need information on how to directly fill out the class file/the writing of hex.
我们在查找有关如何从我们的编译器创建 .class 文件的信息时遇到问题。您是否有任何资源可以为我们提供帮助?我们已经有大量关于指令集的文档,但是需要关于如何直接填写类文件/十六进制编写的信息。
We do not need information or suggestions on decompiling the .class files.
我们不需要有关反编译 .class 文件的信息或建议。
Even a simple example of writing out a .class file from scratch would be excellent.
即使是从头开始写出 .class 文件的简单示例也会非常好。
The JVM spec is not what we're after.What we really need is an example or a walkthrough.
JVM 规范不是我们所追求的。我们真正需要的是一个例子或一个演练。
采纳答案by TofuBeer
The VM Spec: The Class File Formatand the The Java Virtual Machine Instruction Setshould do it.
VM 规范:类文件格式和Java 虚拟机指令集应该这样做。
You might look at the Byte Code Engineering Library (BCEL) for some inspiration as well as Findbugs(it has to read/understand class files).
回答by Jon
There are a number of projects out there that provide a high level interface to creating Java class files without you having to write the class files yourself. Take a look at the following:
有许多项目提供了创建 Java 类文件的高级接口,而您无需自己编写类文件。看看以下内容:
- ASM - http://asm.objectweb.org/
- BCEL - http://jakarta.apache.org/bcel/
- Trove - http://teatrove.sourceforge.net/trove.html
- ASM - http://asm.objectweb.org/
- BCEL - http://jakarta.apache.org/bcel/
- 宝藏 - http://teatrove.sourceforge.net/trove.html
All provide an API to create class files. You could always look at the code they've written to do this and write some similar code for your compiler although I would imagine that it's a fair amount of work.
所有这些都提供了一个 API 来创建类文件。您可以随时查看他们为执行此操作而编写的代码,并为您的编译器编写一些类似的代码,尽管我认为这是相当多的工作。
With BCEL take a look at ClassGen, that should enable you to write out class files in the format you want, a simple example follows:
使用 BCEL 看看 ClassGen,它应该能让你以你想要的格式写出类文件,一个简单的例子如下:
ClassGen cg = new ClassGen("HelloWorld", "java.lang.Object",
"<generated>", ACC_PUBLIC | ACC_SUPER,
null);
回答by Bombe
I'm sorry to disappoint you but the VM specsare exactlywhat you're after. If you can not handle specifications then maybe you shouldn't be writing compilers after all.
我很抱歉让你们失望,但虚拟机的规格是正是你追求的。如果您无法处理规范,那么也许您根本不应该编写编译器。
回答by McDowell
I guess you could try using the existing tools and examine the effect of incremental changes to the resulting bytecode.
我想您可以尝试使用现有工具并检查对生成的字节码进行增量更改的效果。
Source:
来源:
public class Hello {
public static void main(String[] args) {
System.out.println("H");
}
}
javapoutput:
javap输出:
Compiled from "Hello.java"
public class Hello extends java.lang.Object{
public Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String H
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}
Binary:
二进制:
CA FE BA BE 00 00 00 32 00 1D 0A 00 06 00 0F 09 _______2________
00 10 00 11 08 00 12 0A 00 13 00 14 07 00 15 07 ________________
00 16 01 00 06 3C 69 6E 69 74 3E 01 00 03 28 29 _____<init>___()
56 01 00 04 43 6F 64 65 01 00 0F 4C 69 6E 65 4E V___Code___LineN
75 6D 62 65 72 54 61 62 6C 65 01 00 04 6D 61 69 umberTable___mai
6E 01 00 16 28 5B 4C 6A 61 76 61 2F 6C 61 6E 67 n___([Ljava/lang
2F 53 74 72 69 6E 67 3B 29 56 01 00 0A 53 6F 75 /String;)V___Sou
72 63 65 46 69 6C 65 01 00 0A 48 65 6C 6C 6F 2E rceFile___Hello.
6A 61 76 61 0C 00 07 00 08 07 00 17 0C 00 18 00 java____________
19 01 00 01 48 07 00 1A 0C 00 1B 00 1C 01 00 05 ____H___________
48 65 6C 6C 6F 01 00 10 6A 61 76 61 2F 6C 61 6E Hello___java/lan
67 2F 4F 62 6A 65 63 74 01 00 10 6A 61 76 61 2F g/Object___java/
6C 61 6E 67 2F 53 79 73 74 65 6D 01 00 03 6F 75 lang/System___ou
74 01 00 15 4C 6A 61 76 61 2F 69 6F 2F 50 72 69 t___Ljava/io/Pri
6E 74 53 74 72 65 61 6D 3B 01 00 13 6A 61 76 61 ntStream;___java
2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D 01 /io/PrintStream_
00 07 70 72 69 6E 74 6C 6E 01 00 15 28 4C 6A 61 __println___(Lja
76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 va/lang/String;)
56 00 21 00 05 00 06 00 00 00 00 00 02 00 01 00 V_!_____________
07 00 08 00 01 00 09 00 00 00 1D 00 01 00 01 00 ________________
00 00 05 2A B7 00 01 B1 00 00 00 01 00 0A 00 00 ___*____________
00 06 00 01 00 00 00 01 00 09 00 0B 00 0C 00 01 ________________
00 09 00 00 00 25 00 02 00 01 00 00 00 09 B2 00 _____%__________
02 12 03 B6 00 04 B1 00 00 00 01 00 0A 00 00 00 ________________
0A 00 02 00 00 00 03 00 08 00 04 00 01 00 0D 00 ________________
00 00 02 00 0E _____
回答by Jon Skeet
The JVM specificationis probably what you're looking for, and in particular chapter 4 - the class file format.
回答by michael aubert
SmartEiffel contains an open source java .class file generator.
SmartEiffel 包含一个开源的 java .class 文件生成器。

