Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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:

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                                          _____

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

SmartEiffel contains an open source java .class file generator.

http://smarteiffel.loria.fr/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top