: How a source program is translated into machine language using the rust langue?

Please code you help me how it happen

The Overview of the Compiler chapter of The Rustc Dev Guide walks through the compilation process at a high level, with loads of links to source code and further reading material towards the bottom.

Other than that, you'll probably benefit from reading how compilers are generally implemented. The Rust compiler follows a fairly similar process (parsing -> typechecking -> codegen), but it makes things more complex by using a "query" system to execute each of those phases lazily on individual bits of your code to allow for better caching and avoiding unnecessarily work.

For a really simple introduction into how to turn the text of source code into executable machine instructions, compilation, one of the best resources is the article "Let's Build a Compiler" by Jack Crenshaw : Let's Build a Compiler. It is very understandable even to those who only have a basic understanding of programming. It at least introduces one to the problems to be overcome, with simple solutions. The notions of lexical scanning, expression parsing etc.

The example code in the article is in Pascal but that is similar enough to C or Rust that it should be easy to read.

Sadly the article does not quite get as far as actual code generation. But I managed to fill in the blanks and create my own compiler from it that generated code for x86 and the Parallax Inc Propeller micro-controller. You will need to become a little familiar with assembly language at that point.

After studying that many other more theoretical/technical books and discussions on compiler writing should start to make sense.

1 Like