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.
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.
We do not need information or suggestions on decompiling the .class files.
Even a simple example of writing out a .class file from scratch would be excellent.
The JVM spec is not what we're after. What we really need is an example or a walkthrough.
The VM Spec: The Class File Format and the The Java Virtual Machine Instruction Set should do it.
You might look at the Byte Code Engineering Library (BCEL) for some inspiration as well as Findbugs (it has to read/understand class files).
I’m sorry to disappoint you but the VM specs are exactly what you’re after. If you can not handle specifications then maybe you shouldn’t be writing compilers after all.
I guess you could try using the existing tools and examine the effect of incremental changes to the resulting bytecode.
Source:
javap output:
Binary:
SmartEiffel contains an open source java .class file generator.
http://smarteiffel.loria.fr/
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:
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.
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:
The JVM specification is probably what you're looking for, and in particular chapter 4 - the class file format.