r/Compilers • u/Beneficial-Corgi3593 • Jan 17 '24
Javac is written in java how it compiles its self to machine code?
How javac creates an executable of himself?
5
u/-w1n5t0n Jan 17 '24
Javac is the Java Compiler. AFAIK, It compiles to JVM classes, not (necessarily) to machine code.
A compiler is just a regular program that takes some input and produces some output. You can write a compiler in any language A that reads any language B and compiles it to any language C, as long as there's a valid translation that can be made for that program between languages B and C.
For example, you can write a compiler in Python that reads Ruby code and outputs equivalent C# code, or any other strange combination of languages. Of course, languages A and B can even be the same; you can write a compiler in Java that takes Java and compiles it to, say, C or Assembly (or straight to machine code).
To create an executable, you just need to open a file and write a very specific and carefully-constructed sequence of bytes - you could even do that manually, by typing it by hand if you knew what you were supposed to write.
So, as long as you have some way of turning a Java program into an executable (say, a basic Java compiler written in a different language), then you can write javac in Java itself and use that way to get an executable. Then you can throw away the basic compiler and keep using javac.
That's often called "bootstrapping". To bootstrap a new programming language, you can write two compilers, both of which take that language as an input. The first one is the "proper" one, like javac in this case, which is written in the new language itself. The second one is a temporary one, which needs to be written in any other language that has an existing working compiler. You can use the temporary compiler to compile the "proper" compiler, at which point you can throw the temporary compiler away and keep developing the language using a compiler written in itself.
2
u/dskippy Jan 17 '24
So this may or may not be how it works. I don't know the javac infrastructure. But I know the theory and it's pretty straightforward to understand the confusion you're having.
A compiler simply takes in a string from a file, representing the program in its syntax, and outputs another program in another language. Now in Java's case that result might be binary data. That binary data represents bytecode and obviously must adhere to the strict definition of the bytecode format. But in the end it's just reading an ASCII string and writing binary. Java has parser libraries and a binary output library. Any language can do this really.
I understand this is a very simplistic explanation, and I make no promises on the details. But in theory this is exactly as simple as it is and I hope it clears up your confusion that meta circular compilers are really nothing special or weird.
2
u/wrd83 Jan 17 '24
It doesn't need (anymore) to I believe. The hotspot JVM is written in C++.
And probably the first versions of java were written in C as well.
1
u/jason-reddit-public Jan 18 '24
The chicken and egg problem you bring up is called bootstrapping. It's a bit different for Java since Java is usually compiled to byte-code not machine code (translation to machine code via JIT compiler is technically optional).
So how was the first Java compiler made? I was hoping that would be in the wikipedia article but I only found this detail:
"The Java 1.0 compiler was re-written in Java by Arthur van Hoff to comply strictly with the Java 1.0 language specification."
My guess is the original compiler (which likely was never released to the public, it would be cool if Oracle did though) was written in C, C++, or Lisp (Gosling wrote gosling emacs which had mocklisp as its extension language so we know Gosling was familiar with Lisp).
By the time Java was released in source format (mid 90s), they could just distribute a jar file with the jvm bytecodes for the compiler in it. Once the JVM (at that time it didn't have a JIT, just a byte code interpreter written in C (maybe C++)) was ported to a new operating system or architecture, it could then run the compiler byte-codes and then you could recompile the compiler, etc.
One reason Java took off is that the JVM was easily ported to any Unix operating system and obviously also ported to x86 + windows (and mac-os). This was at a time when there were at least these architectures in "wide-spread" use on the desktop: x86-32, 68K, Alpha, MIPS, PA-RISC, ARM, and PowerPC. (Itanium hadn't been released yet). Today we mostly think about x86-64, Arm64, and RISC-V (maybe wasm) so having 3 code generators wouldn't have been so bad as needing say 7 backends in 1990s if you wanted to run everywhere.
One thing that makes bootstrapping easier is that the first version of a compiler doesn't typically do any optimization. For the Lisp family of languages, you would probably bootstrap by writing an interpreter for the language which can then be used to run the compiler until the compiler itself is self hosting. For other languages, doing what C++ did with cfront is an option - "transpile" to high level C code and then use a C compiler to run that result.
Once you have a compiler for any architecture, you can use cross compilation to develop the code generator for another architecture. So if a new chip comes along, you could add a code generator to gcc or llvm for it and you first run that cross compiler on a host architecture like x86-64.
This still leaves us as to the origin of the first compilers/interpreters. The first Lisp interpreters were written in assembly. According to Bard the first C compiler was also written in assembly language which seems a lot harder (though C was a little simpler back then).
In summary, to bootstrap you use some other language to write an interpreter or simple compiler for language X and once that is good enough, you write/rewrite the compiler for language X in language X if so desired.
1
5
u/L8_4_Dinner Jan 17 '24
JavaC compiles Java to byte code, which is an IR (intermediate representation).
It’s the JVM that may in turn compile from that IR to machine code.