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