we're writing some sort of compiler from Pascal to JVM Bytecode. And we've already implemented an expression tree generation, so the next step should be the creation of .class
file. Can you suggest any guide/tutorial of how to generate any .class
file at least from some static data? Because I've googled for 2 hours already and read JVM specification, but I really need some even simplest example to start developing the whole stuff.

- 13,134
- 6
- 58
- 106
4 Answers
Someone has already written a widely-used byte code generation library: CGLIB.
You'd have it knocked if you could figure out how to get your AST into CGLIB.

- 305,152
- 44
- 369
- 561
-
thank you! the only sad thing is that there are no tutorials about code generation :( only about `.class` files manipulations. Anyways I'll try to come up with some solution – Uko Nov 20 '11 at 17:38
-
Would it work if you could figure out how to map your Pascal AST onto Java Beans? That's the hard part, since Pascal doesn't support objects as far as I know. CGLIB can't help you with that; no library will do that automagically. That's where you and your domain knowledge come in. – duffymo Nov 21 '11 at 01:40
-
Pascal doesn't support objects? It's not true for decades. – avra Nov 21 '11 at 09:11
-
Been decades since I wrote it, so that's my mistake. I have no idea of how object-oriented the source code is, and neither does anyone else except the OP. – duffymo Nov 21 '11 at 10:24
-
avra: pascal doesn't, object Pascal (Apple, later turbo pascal and Delphi) does – Marco van de Voort Nov 22 '11 at 09:03
-
@avra object pascal is crappy, it seams that it's pre-delphi state of pascal :) Moreover when working with non-OOP it's quite easy to gen java `.class`, you just make one file and put all the procedures/functions to methods, and all the app code in **main** method and all the variables as class fields :) – Uko Nov 22 '11 at 10:39
Actually there is an example file inside ASM folder that you download. It's called Helloworld and it's located in examples
subfolder. It shows how to compile (generate from scratch) .class
file that corresponds to simple hello world app. It also shows how to get date from .class
files but it's another story.
Maybe this is'n the best way, but when you need to start with java byte code generation and you need some basic examples it's a good idea to have a look at ASM and the examples that are bundled within standard package.
Moreover Groovy uses ASM to generate its code :)
I don't know if you are aware, but there is a backend for the FPC that generates bytecode compliant to the JDK 1.5. The development looks fairly recent (November 2011). You should have a look at it.

- 1,820
- 1
- 21
- 23
-
+1. Note that it is GPL though, not an ideal source if you want to sell the result. – Marco van de Voort Nov 22 '11 at 09:02
-
Thans, it's really interesting stuff. I won't sell it. It's a project made during the compilers course in the university. But as all out schools teach **pascal** maybe I'll make some **NetBeans** module, so the pupils will have some nice IDE – Uko Nov 22 '11 at 10:43
-
Why not directly use Lazarus? It is customizable, and they are working on an educational edition? http://lazarus.freepascal.org http://wiki.freepascal.org/Lazarus_for_education – Marco van de Voort Nov 23 '11 at 08:38
There are a couple of widely-used bytecode generation projects.
ASM and CGLib are probably the two best examples.
You probably don't want to build a generation library for yourself from scratch - it's a lot of work, difficult to get right and probably doesn't offer you much over using an existing project.
ASM is widely used by non-Java languages on the JVM, has OK-ish documentation and is not too bad to get going.
I haven't used CGLib as much, but I didn't find it as easy to get started with.
As a final data point, the Java 8 team are prototyping some of the new Java features (including lambda expressions) with ASM.

- 5,640
- 2
- 23
- 36