Java bytecode specification [closed]

2020-05-11 10:59发布

问题:

Is there a nice place for learning the JVM bytecode instruction set. The specification perhaps and maybe some tutorials?

I ask because I would like to design a toy language and a compiler for it that generates JVM bytecode.

Thanks for your knowledge and perhaps googling.

回答1:

A good reference for Java bytecode specification is the The Java Virtual Machine Specification.

See Chapter 4. The class File Format and Chapter 6. The Java Virtual Machine Instruction Set.



回答2:

A little more "graphic" explanation, IBM developer works: Understanding bytecode makes you a better programmer.



回答3:

Also useful are the javap disassembler and bytecode manipulation frameworks like ASM and BCEL, even if all you want to do is verify your classes.



回答4:

The book Programming for the Java Virtual Machine explains the JVM instruction set and how to write code for it. It also introduces a bytecode assembler called Oolong, which I have not been able to download. You can, however, use Jasmin, the predecessor of Oolong. Essentially, you write a text file with instructions and Jasmin will spit out a .class file. The book was published in 1999, but it is still a good and gentle introduction to the VM.



回答5:

The Java Virtual Machine Specification is a good place to start.

See Chapter 4 The class File Format. The updates cover new attributes added since the 2nd edition was made.



回答6:

To start with, I suggest generating Java code from your language.

This will make reading and debugging much simpler.



回答7:

A new version of the specification has been released:

The Java Virtual Machine Specification, Java SE 7 Edition

This new addition does exclude the chapter on Java Programming Language Concepts which folks may or may not find useful. See here for that chapter in the Second Edition of the spec.



回答8:

Perhaps check out Preon's example on how to parse a Java class file. It has a fairly complete representation of the bytecode in a Java object model.



回答9:

This is a bit more specialized, but here is an on-line presentation on how to optimize generated bytecode for running on the JVM. It was presented at the recent JVM Languages Summit conferences. InfoQ has a collection of presentations from that conference which might be of help to someone wanting to bring up a language on the JVM (or to see what's already been done).