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