99 lines · plain
1=============================================2My First Language Frontend with LLVM Tutorial3=============================================4 5.. toctree::6 :hidden:7 8 LangImpl019 LangImpl0210 LangImpl0311 LangImpl0412 LangImpl0513 LangImpl0614 LangImpl0715 LangImpl0816 LangImpl0917 LangImpl1018 19**Requirements:** This tutorial assumes you know C++, but no previous20compiler experience is necessary.21 22Welcome to the "My First Language Frontend with LLVM" tutorial. Here we23run through the implementation of a simple language, showing24how fun and easy it can be. This tutorial will get you up and running25fast and show a concrete example of something that uses LLVM to generate26code.27 28This tutorial introduces the simple "Kaleidoscope" language, building it29iteratively over the course of several chapters, showing how it is built30over time. This lets us cover a range of language design and LLVM-specific31ideas, showing and explaining the code for it all along the way,32and reduces the overwhelming amount of details up front. We strongly33encourage that you *work with this code* - make a copy and hack it up and34experiment.35 36**Warning**: In order to focus on teaching compiler techniques and LLVM37specifically,38this tutorial does *not* show best practices in software engineering39principles. For example, the code uses global variables40pervasively, doesn't use41`visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but42instead keeps things simple and focuses on the topics at hand.43 44This tutorial is structured into chapters covering individual topics,45allowing you to skip ahead as you wish:46 47- `Chapter #1: Kaleidoscope language and Lexer <LangImpl01.html>`_ -48 This shows where we are49 going and the basic functionality that we want to build. A lexer50 is also the first part of building a parser for a language, and we51 use a simple C++ lexer which is easy to understand.52- `Chapter #2: Implementing a Parser and AST <LangImpl02.html>`_ -53 With the lexer in place, we can talk about parsing techniques and54 basic AST construction. This tutorial describes recursive descent55 parsing and operator precedence parsing.56- `Chapter #3: Code generation to LLVM IR <LangImpl03.html>`_ - with57 the AST ready, we show how easy it is to generate LLVM IR, and show58 a simple way to incorporate LLVM into your project.59- `Chapter #4: Adding JIT and Optimizer Support <LangImpl04.html>`_ -60 One great thing about LLVM is its support for JIT compilation, so61 we'll dive right into it and show you the 3 lines it takes to add JIT62 support. Later chapters show how to generate .o files.63- `Chapter #5: Extending the Language: Control Flow <LangImpl05.html>`_ - With64 the basic language up and running, we show how to extend65 it with control flow operations ('if' statement and a 'for' loop). This66 gives us a chance to talk about SSA construction and control67 flow.68- `Chapter #6: Extending the Language: User-defined Operators69 <LangImpl06.html>`_ - This chapter extends the language to let70 users define arbitrary unary and binary operators - with assignable71 precedence! This allows us to build a significant piece of the72 "language" as library routines.73- `Chapter #7: Extending the Language: Mutable Variables74 <LangImpl07.html>`_ - This chapter talks about adding user-defined local75 variables along with an assignment operator. This shows how easy it is76 to construct SSA form in LLVM: LLVM does *not* require your front-end77 to construct SSA form in order to use it!78- `Chapter #8: Compiling to Object Files <LangImpl08.html>`_ - This79 chapter explains how to take LLVM IR and compile it down to object80 files, like a static compiler does.81- `Chapter #9: Debug Information <LangImpl09.html>`_ - A real language82 needs to support debuggers, so we83 add debug information that allows setting breakpoints in Kaleidoscope84 functions, print out argument variables, and call functions!85- `Chapter #10: Conclusion and other tidbits <LangImpl10.html>`_ - This86 chapter wraps up the series by discussing ways to extend the language87 and includes pointers to info on "special topics" like adding garbage88 collection support, exceptions, debugging, support for "spaghetti89 stacks", etc.90 91By the end of the tutorial, we'll have written a bit less than 1000 lines92of (non-comment, non-blank) lines of code. With this small amount of93code, we'll have built up a nice little compiler for a non-trivial94language including a hand-written lexer, parser, AST, as well as code95generation support - both static and JIT! The breadth of this is a great96testament to the strengths of LLVM and shows why it is such a popular97target for language designers and others who need high performance code98generation.99