Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 1 | ============================================= |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 2 | My First Language Frontend with LLVM Tutorial |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 3 | ============================================= |
| 4 | |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 5 | Welcome to the "My First Language Frontend with LLVM" tutorial. Here we |
| 6 | run through the implementation of a simple language, showing |
| 7 | how fun and easy it can be. This tutorial will get you up and running |
| 8 | fast and show a concrete example of something that uses LLVM to generate |
| 9 | code. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 10 | |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 11 | This tutorial introduces the simple "Kaleidoscope" language, building it |
| 12 | iteratively over the course of several chapters, showing how it is built |
| 13 | over time. This lets us cover a range of language design and LLVM-specific |
| 14 | ideas, showing and explaining the code for it all along the way, |
| 15 | and reduces the amount of overwhelming details up front. We strongly |
| 16 | encourage that you *work with this code* - make a copy and hack it up and |
| 17 | experiment. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 19 | Warning: In order to focus on teaching compiler techniques and LLVM |
| 20 | specifically, |
| 21 | this tutorial does *not* show best practices in software engineering |
| 22 | principles. For example, the code uses global variables |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 23 | all over the place, doesn't use nice design patterns like |
| 24 | `visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 25 | instead keeps things simple and focuses on the topics at hand. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 27 | This tutorial is structured into chapters covering individual topics, |
| 28 | allowing you to skip ahead or over things as you wish: |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 30 | - `Chapter #1 <LangImpl01.html>`_: Introduction to the Kaleidoscope |
| 31 | language, and the definition of its Lexer. This shows where we are |
| 32 | going and the basic functionality that we want to build. A lexer |
| 33 | is also the first part of building a parser for a language, and we |
| 34 | use a simple C++ lexer which is easy to understand. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 35 | - `Chapter #2 <LangImpl02.html>`_: Implementing a Parser and AST - |
| 36 | With the lexer in place, we can talk about parsing techniques and |
| 37 | basic AST construction. This tutorial describes recursive descent |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 38 | parsing and operator precedence parsing. |
| 39 | - `Chapter #3 <LangImpl03.html>`_: Code generation to LLVM IR - with |
| 40 | the AST ready, we show how easy it is to generate LLVM IR, and show |
| 41 | a simple way to incorporate LLVM into your project. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 42 | - `Chapter #4 <LangImpl04.html>`_: Adding JIT and Optimizer Support |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 43 | - One great thing about LLVM is its support for JIT compilation, so |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 44 | we'll dive right into it and show you the 3 lines it takes to add JIT |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 45 | support. Later chapters show how to generate .o files. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 46 | - `Chapter #5 <LangImpl05.html>`_: Extending the Language: Control |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 47 | Flow - With the basic language up and running, we show how to extend |
| 48 | it with control flow operations ('if' statement and a 'for' loop). This |
| 49 | gives us a chance to talk about SSA construction and control |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 50 | flow. |
| 51 | - `Chapter #6 <LangImpl06.html>`_: Extending the Language: |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 52 | User-defined Operators - This chapter extends the language to let |
| 53 | users define arbitrary unary and binary operators (with assignable |
| 54 | precedence!). This lets us build a significant piece of the |
| 55 | "language" as library routines. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 56 | - `Chapter #7 <LangImpl07.html>`_: Extending the Language: Mutable |
| 57 | Variables - This chapter talks about adding user-defined local |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 58 | variables along with an assignment operator. This shows how easy it is |
| 59 | to construct SSA form in LLVM: LLVM does *not* require your front-end |
| 60 | to construct SSA form in order to use it! |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 61 | - `Chapter #8 <LangImpl08.html>`_: Compiling to Object Files - This |
| 62 | chapter explains how to take LLVM IR and compile it down to object |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 63 | files, like a static compiler does. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 64 | - `Chapter #9 <LangImpl09.html>`_: Extending the Language: Debug |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 65 | Information - A real language needs to support debuggers, so we add |
| 66 | debug information that allows setting breakpoints in Kaleidoscope |
| 67 | functions, print out argument variables, and call functions! |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 68 | - `Chapter #10 <LangImpl10.html>`_: Conclusion and other useful LLVM |
| 69 | tidbits - This chapter wraps up the series by talking about |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 70 | potential ways to extend the language, and includes some |
| 71 | pointers to info on "special topics" like adding garbage |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 72 | collection support, exceptions, debugging, support for "spaghetti |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 73 | stacks", and random tips and tricks. |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 74 | |
| 75 | By the end of the tutorial, we'll have written a bit less than 1000 lines |
| 76 | of non-comment, non-blank, lines of code. With this small amount of |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 77 | code, we'll have built up a nice little compiler for a non-trivial |
Chris Lattner | 2243a165 | 2019-04-07 13:17:16 +0000 | [diff] [blame] | 78 | language including a hand-written lexer, parser, AST, as well as code |
Chris Lattner | 13d3505 | 2019-04-07 13:42:29 +0000 | [diff] [blame] | 79 | generation support with a JIT compiler. The breadth of this |
| 80 | tutorial is a great testament to the strengths of LLVM and shows why |
| 81 | it is such a popular target for language designers. |