Game World!

Join A World Of Gamers

Enter your email address:

Delivered by FeedBurner

Followers

Popular Posts

Tuesday 29 June 2021

Is Rust made from C?

 IIRC the original compiler was written in OCaml, very pre-1.0. Eventually it got to a point where rustc written in Rust, could be compiled by the original OCaml-based compiler, and boom: bootstrapped. Ever since then new versions of rustc have been compiled by the previous versions of rustc. New features have to be initially implemented in a way that can't actually use those features.

[–]CUVipernum · rayon · Fedora · RHEL 17 points  

You can see the transition to new features in action by grepping for stage0 configs. Stage 0 is when the prior version of rustc is used to build the current sources, and then stage 1 uses that product to build the current sources again.

[–]BobTreehugger 28 points  

This is actually fairly common for compilers -- GCC is written in C, clang is C++, GHC (the haskell compiler) is written in haskell, etc.

[–]DragonMaus 35 points  

A compiled language is generally not considered "viable" until it can implement its own compiler.

[–]IDidntChooseUsername 15 points  

Any general-purpose language. Niche languages intended for a specific use don't need to implement their own compiler if their purpose is not compiler implementation.

[–]DragonMaus 9 points  

Hence "generally". :P

[–]williewillus 2 points  

well most interpreted/dynamic/scripting languages don't implement their own interpreters (python, clojure, ...), so I think the "compiled" qualifier is valid

[–]BobTreehugger 5 points  

Is javac written in Java? I think scalac is written in Scala so there's no reason they couldn't but I don't know if they actually did.

Also I think it's only recently that the C# compiler was rewritten in C#. For a long time it wasn't and it was already a very popular viable language.

That said, writing a compiler for your language in that language is a milestone that matters for a lot of languages. Of course if you do it too early, you can accidentally make your language designed mainly for writing compilers 😅

[–]Azphreal 17 points  

Javac is written in Java, but for obvious reasons the JVM is not, so iirc that's written in C.

[–]CUVipernum · rayon · Fedora · RHEL 8 points  

FWIW, GCC has migrated itself to C++ too: https://gcc.gnu.org/codingconventions.html#Portability

The directories gcc, libcpp and fixincludes may use C++03.

Here's an article about that migration from a few years ago: https://lwn.net/Articles/542457/

[–]eugene2k 11 points  

Yes, rustc is written in rust. You can find the repo here. The previous version of the compiler builds the next version of the compiler. The first version was written in ocaml AFAIK.

[–]snsmac 6 points  

Doesn't rust use llvm as backend? llvm is written in C++

[–]oconnor663blake3 · duct 14 points  

That's correct. And apart from the LLVM optimizer written in C++, rustc (like most programs) will link against the platform's C standard library for the usual stuff like filesystem access and threading. But we can at least say that all the code in the compiler that knows anything about the Rust language specifically, is written in Rust.

[–]Ruskyrust 10 points  

Yes, but that's neither here nor there as far as this question is concerned. There's an alternative backend in progress, CraneLift, which is written in Rust; and on the other hand LLVM is used as the backend for Clang, a C++ compiler also used to compile LLVM.

[–]Boiethios 2 points  

And is it planed to write a linker in Rust?

[–]ClassikD 12 points  

The first compiler for any language can never be written in that language itself (unless you count straight machine code I guess). But as the language becomes complete, you can re-develop the compiler in the language itself. So originally the compiler for rust would have been written in another language (guessing c++ idk Edit: other comments say ocaml). So after some point of using a c++ or whatever built compiler to compile your compiler, you can use that compiler to compile a new compiler written in rust

[–]llogiqclippy · twir · rust · mutagen · flamer · overflower · bytecount 7 points  

The first version of Pascal was first written in itself, then manually translated to P-code (sort of like a VM for Pascal) which was then interpreted to produce the first self-hosted version.

[–]mpevnev 2 points  

How on earth did they debug that into a working shape?

[–]llogiqclippy · twir · rust · mutagen · flamer · overflower · bytecount 2 points  

They only used a subset of Pascal, so the interpreter could be simple.

[–]zesterer 8 points  

A self-hosting compiler is the ultimate dogfood project. It's an example of the capabilities of the language. Others in this thread have more decent explanations of the specifics.

[–]fgilcherrust-community · rustfest 12 points  

A self-hosting compiler is the ultimate dogfood project.

Hm, not really. Many people say that it shows in languages if their only user was the compiler in the beginning.

Rust had Servo as a dogfood project.

[–]icefoxen 6 points  

A historical perspective: some of the compilers back in the Real Programmer days were written in the target language with no actual implementation, then hand-compiled to assembly to create the first compiler. I'm looking at you, Oberon.

[–]Shadow0133 4 points  

Yes, rustc is written rust. First versions of rustc (before 1.0) were written in OCaml, and then rewritten in rust itself. You can read about current way rustc is compiled here.

[–]pagefault0x16 4 points  

The same way Haskell's compiler is written in Haskell, and C compilers can compile themselves. The bootstrap compiler was written another language, and used to build a compiler written in Rust. This is called going self-hosting.

The first C compiler was written PDP-11 assembly language. The first assembler was manually assembled in CPU opcodes. And the first machine language assemblers were humans working with punch cards.

[–]ortemis 2 points  

Rust compiler is written in Rust and uses bootstrapping ).

Floating Button

Button