It’s called Bootstrapping.
Compilers
Languages are only specifications. Though related, languages and compilers are different things.
Compilers and libraries form what the specification says. It is obvious that the first implementation of language needs to be written in another language. Then you can use the language itself to create a new implementation written on it.
Compilers are relatively basic algorithms, full of specific complexities, of course. It enters text data, it processes, and there’s complexity, and it generates a data, possibly binary, that a virtual or physical machine knows how to run. It’s just a transformation algorithm following specific rules. So they generate a program that can run and that can be a compiler, a virtual machine, an operating system, anything.
I understand the curiosity, but I find it strange that it seems like something very difficult to achieve. I think the only "secret" is knowing that the first compiler should be done in another language.
Actually some languages are made incrementally. It makes a compiler that treats the minimum, and adds functionality later. So you almost have an initial compiler in the language itself. Of course the first interactions of this development the language will be a little different from the desired one at the end, and a little limited.
Programming languages can produce anything, then it is no secret that a language produces a compiler for itself, as long as there is a first implementation.
Of course some languages are not the most suitable to produce compilers.
The best implementations of Java are actually written in C++. Java today doesn’t seem very suitable for producing compilers. It’s been worse. Java is compiled and not interpreted on its basis. It is possible to have an interpreter.
Interpreters
An interpreter is nothing more than a compiler that at the end instead of generating an executable, it already executes what has been parsed. An interpreter is an executable program like any other. But in this case the compiler generates a bytecode and this is what will be "interpreted" by the virtual machine.
Of course in case the interpreter runs himself he needs to be reentrant, what is not simple.
What I can guarantee is that there is an excerpt of code in another language, an excerpt that precisely does the bootstrap of the virtual machine. Reading the article in Wikipedia, it speaks of this:
A small C Loader is Responsible for loading the boot image at Runtime
The compiler, from the interpreter, is separated from the virtual machine, even if it is in the same executable. The virtual machine does not interpret Java, it plays a bytecode, and from what I understand this Jikes doesn’t even use the bytecode pattern.
Obviously I don’t have details of this project and I can’t say about every specificity of how it works.
More information
C and C++ compilers have been written in C++ for quite some time. Some still have a good part written in C. Someone can do in Assembly (Assembler is not the correct name), but for years no one seriously does this.
C# today has its compiler written in C# and works better than the original written in C++. The Runtime is written basically in C++, but there are experiments in C# with improving results, but below the desired.
I’ve talked about it in The first programming language.
I thought about writing something like this and I already had the answer half-written, but there’s a problem. He’s talking about an interpreter, not a compiler. With that, it gets a little more complicated.
– Victor Stafusa
@Victorstafusa because it gets more complicated?
– Maniero
Because you have to make a program interpret itself without being running on another interpreter, which is what Jikesrvm says to do. The part about not running on another interpreter is that it complicates, according to their website "i.e., its Java code runs on itself without requiring a Second virtual machine.".
– Victor Stafusa
@Victorstafusa did not know this, I will see more and I will improve. You can also answer if you have something that specifically talks about it. But at first I don’t see any problems either.
– Maniero
This idea of Bootstrapping without needing another VM, it’s like Roslyn does, it’s not?
– Jéf Bueno
@jbueno actually no, I edited and talked about it. There they talk that I get better.
– Maniero
@Well, I posted my additional answer.
– Victor Stafusa