9

What library/api does the Clojure compiler (which is just a jar/java application) uses to generate the JVM bytecode? I'm curious since the bytecode is being generated at runtime by java and not javac, so its a java program that outputs jvm bytecode.

0

2 Answers 2

9

Clojure uses a Java library called ASM to generate bytecode. A fork of a part of the lib is embedded into the clojure project here: https://github.com/clojure/clojure/tree/master/src/jvm/clojure/asm

Sign up to request clarification or add additional context in comments.

1 Comment

It's actually a subset of ASM. As part of Clojure 1.6, the subset was recently updated to ASM 4.1 in master.
3

How does the Clojure compiler generates JVM bytecode?

It is just Java programming (unless they have bootstrapped their code generator to Clojure ... which seems unlikely). As a gross simplification1, is opening a file, writing out the code in "classfile" format as specified by the JVM spec, and then closing the file. (Or writing the code to a ByteArrayOutputStream or equivalent.)

For the record, there is nothing qualitatively different between what the Clojure compiler is doing and the Java compiler can do if you call it at runtime. But obviously there will be differences in the fine-grain details, because of differences in the languages, and different ideas and priorities for the respective developers.


1 - If you want to know how code generators in general work, read a textbook on compiler writing. For all of the gory details of how Clojure does it, look at the source code ...

2 Comments

The one exception I know of regarding Clojure and Java bytecode generation is Clojure locals clearing, which has no Java analog iirc.
There is a CinC (Clojure in Clojure) project, that would generate Clojure byte code without using any Java code, but it is not yet mature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.