Reputation: 163
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.
Upvotes: 9
Views: 1688
Reputation: 20194
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
Upvotes: 8
Reputation: 718798
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 ...
Upvotes: 3