From b369dcb223612a12581e54ddaa5a3b2dc9247de9 Mon Sep 17 00:00:00 2001 From: mark Date: Sun, 8 Jul 2018 18:28:51 -0500 Subject: [PATCH 1/2] add a bit on llvm --- src/codegen.md | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/codegen.md b/src/codegen.md index f5894baa7..279875995 100644 --- a/src/codegen.md +++ b/src/codegen.md @@ -1 +1,48 @@ -# Generating LLVM IR +# Code generation + +Code generation or "codegen" is the part of the compiler that actually generates +an executable binary. rustc uses LLVM for code generation. + +## What is LLVM? + +All of the preceeding chapters of this guide have one thing in common: we never +generated any executable machine code at all! With this chapter, all of that +changes. + +Like most compilers, rustc is composed of a "frontend" and a "backend". The +"frontend" is responsible for taking raw source code, checking it for +correctness, and getting it into a format `X` from which we can generate +executable machine code. The "backend" then takes that format `X` and produces +(possibly optimized) executable machine code for some platform. All of the +previous chapters deal with rustc's frontend. + +rustc's backend is [LLVM](https://llvm.org), "a collection of modular and +reusable compiler and toolchain technologies". In particular, the LLVM project +contains a pluggable compiler backend (also called "LLVM"), which is used by +many compiler projects, including the `clang` C compiler and our beloved +`rustc`. + +LLVM's "format `X`" is called LLVM IR. It is basically assembly code with +additional low-level types and annotations added. These annotations are helpful +for doing optimizations on the LLVM IR and outputed machine code. The end result +of all this is (at long last) something executable (e.g. an ELF object or wasm). + +There are a few benefits to using LLVM: + +- We don't have to write a whole compiler backend. This reduces implementation + and maintainance burden. +- We benefit from the large suite of advanced optimizations that the LLVM + project has been collecting. +- We automatically can compile Rust to any of the platforms for which LLVM has + support. For example, as soon as LLVM added support for wasm, voila! rustc, + clang, and a bunch of other languages were able to compile to wasm! (Well, + there was some extra stuff to be done, but we were 90% there anyway). +- We and other compiler projects benefit from each other. For example, when the + [Spectre and Meltdown security vulnerabilities][spectre] were discovered, only LLVM + needed to be patched. + +[spectre]: https://meltdownattack.com/ + +## Generating LLVM IR + +TODO From 217537b15fff2b70bbe002ddcce5f67c4b40015a Mon Sep 17 00:00:00 2001 From: mark Date: Sun, 8 Jul 2018 18:31:56 -0500 Subject: [PATCH 2/2] line length --- src/codegen.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/codegen.md b/src/codegen.md index 279875995..cb99e4d38 100644 --- a/src/codegen.md +++ b/src/codegen.md @@ -1,7 +1,7 @@ # Code generation -Code generation or "codegen" is the part of the compiler that actually generates -an executable binary. rustc uses LLVM for code generation. +Code generation or "codegen" is the part of the compiler that actually +generates an executable binary. rustc uses LLVM for code generation. ## What is LLVM? @@ -24,8 +24,9 @@ many compiler projects, including the `clang` C compiler and our beloved LLVM's "format `X`" is called LLVM IR. It is basically assembly code with additional low-level types and annotations added. These annotations are helpful -for doing optimizations on the LLVM IR and outputed machine code. The end result -of all this is (at long last) something executable (e.g. an ELF object or wasm). +for doing optimizations on the LLVM IR and outputed machine code. The end +result of all this is (at long last) something executable (e.g. an ELF object +or wasm). There are a few benefits to using LLVM: @@ -38,8 +39,8 @@ There are a few benefits to using LLVM: clang, and a bunch of other languages were able to compile to wasm! (Well, there was some extra stuff to be done, but we were 90% there anyway). - We and other compiler projects benefit from each other. For example, when the - [Spectre and Meltdown security vulnerabilities][spectre] were discovered, only LLVM - needed to be patched. + [Spectre and Meltdown security vulnerabilities][spectre] were discovered, + only LLVM needed to be patched. [spectre]: https://meltdownattack.com/