views:

853

answers:

6

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.

+2  A: 

The JVM specification is probably what you're looking for, and in particular chapter 4 - the class file format.

Jon Skeet
Anybody else gives this answer and they get a negative vote for recommending the JVM spec in spite of the phrase in bold - but Jon gets three up votes. He's right, but I'll bet that someone without the mad cred that Jon's accumulated gets dissed. Props to you, Jon.
duffymo
hard to say, cus both Jon's answer and the edit occurred "an hour ago" but the bolded line about not wanting the JVM spec is edited in, so its likely Jon's answer occurred before the edit.
shsteimer
Jon and I suggest the VM spec. The voting down happened and then the edit or the edit and the voting down. Either way the edit and the down vote happened at pretty much the same time.
TofuBeer
The edit was certainly after my answer - and I'd like to point out that I got a downvote too. This answer is on +4 -1 at the time of this writing.
Jon Skeet
yup - I was very confused that you had 4 and I had 1 then it was 3 and 0 :-) Took me a bit to figure out the seemingly random down votes (I really wish people would leave comments when they vote down) :-)
TofuBeer
It did occur before the edit.
Allyn
@Latest Downvoter: Care to give a reason?
Jon Skeet
+3  A: 

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).

TofuBeer
I'd suggest ASM rather than BCEL for your own sanity.
Tom Hawtin - tackline
haven't used either of them, just suggested it as a reference to see how the class file was written (I still say chapters 4 and 6 is all that is needed though) unless specific issues arise.
TofuBeer
+5  A: 

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:

ClassGen cg = new ClassGen("HelloWorld", "java.lang.Object",
                             "<generated>", ACC_PUBLIC | ACC_SUPER,
                             null);
Jon
+4  A: 

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.

Bombe
+3  A: 

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");
        }
}

javap output:

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                                          _____
McDowell
+1  A: 

SmartEiffel contains an open source java .class file generator.

http://smarteiffel.loria.fr/

QuickRecipesOnSymbianOS