blob: c74c96a33c905b205082f1a7b3a12a0a482b821b [file] [log] [blame]
Chris Lattner2243a1652019-04-07 13:17:16 +00001=============================================
Chris Lattner13d35052019-04-07 13:42:29 +00002My First Language Frontend with LLVM Tutorial
Chris Lattner2243a1652019-04-07 13:17:16 +00003=============================================
4
Chris Lattner13d35052019-04-07 13:42:29 +00005Welcome to the "My First Language Frontend with LLVM" tutorial. Here we
6run through the implementation of a simple language, showing
7how fun and easy it can be. This tutorial will get you up and running
8fast and show a concrete example of something that uses LLVM to generate
9code.
Chris Lattner2243a1652019-04-07 13:17:16 +000010
Chris Lattner13d35052019-04-07 13:42:29 +000011This tutorial introduces the simple "Kaleidoscope" language, building it
12iteratively over the course of several chapters, showing how it is built
13over time. This lets us cover a range of language design and LLVM-specific
14ideas, showing and explaining the code for it all along the way,
15and reduces the amount of overwhelming details up front. We strongly
16encourage that you *work with this code* - make a copy and hack it up and
17experiment.
Chris Lattner2243a1652019-04-07 13:17:16 +000018
Chris Lattner13d35052019-04-07 13:42:29 +000019Warning: In order to focus on teaching compiler techniques and LLVM
20specifically,
21this tutorial does *not* show best practices in software engineering
22principles. For example, the code uses global variables
Chris Lattner2243a1652019-04-07 13:17:16 +000023all over the place, doesn't use nice design patterns like
24`visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but
Chris Lattner13d35052019-04-07 13:42:29 +000025instead keeps things simple and focuses on the topics at hand.
Chris Lattner2243a1652019-04-07 13:17:16 +000026
Chris Lattner13d35052019-04-07 13:42:29 +000027This tutorial is structured into chapters covering individual topics,
28allowing you to skip ahead or over things as you wish:
Chris Lattner2243a1652019-04-07 13:17:16 +000029
Chris Lattner13d35052019-04-07 13:42:29 +000030- `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 Lattner2243a1652019-04-07 13:17:16 +000035- `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 Lattner13d35052019-04-07 13:42:29 +000038 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 Lattner2243a1652019-04-07 13:17:16 +000042- `Chapter #4 <LangImpl04.html>`_: Adding JIT and Optimizer Support
Chris Lattner13d35052019-04-07 13:42:29 +000043 - One great thing about LLVM is its support for JIT compilation, so
Chris Lattner2243a1652019-04-07 13:17:16 +000044 we'll dive right into it and show you the 3 lines it takes to add JIT
Chris Lattner13d35052019-04-07 13:42:29 +000045 support. Later chapters show how to generate .o files.
Chris Lattner2243a1652019-04-07 13:17:16 +000046- `Chapter #5 <LangImpl05.html>`_: Extending the Language: Control
Chris Lattner13d35052019-04-07 13:42:29 +000047 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 Lattner2243a1652019-04-07 13:17:16 +000050 flow.
51- `Chapter #6 <LangImpl06.html>`_: Extending the Language:
Chris Lattner13d35052019-04-07 13:42:29 +000052 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 Lattner2243a1652019-04-07 13:17:16 +000056- `Chapter #7 <LangImpl07.html>`_: Extending the Language: Mutable
57 Variables - This chapter talks about adding user-defined local
Chris Lattner13d35052019-04-07 13:42:29 +000058 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 Lattner2243a1652019-04-07 13:17:16 +000061- `Chapter #8 <LangImpl08.html>`_: Compiling to Object Files - This
62 chapter explains how to take LLVM IR and compile it down to object
Chris Lattner13d35052019-04-07 13:42:29 +000063 files, like a static compiler does.
Chris Lattner2243a1652019-04-07 13:17:16 +000064- `Chapter #9 <LangImpl09.html>`_: Extending the Language: Debug
Chris Lattner13d35052019-04-07 13:42:29 +000065 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 Lattner2243a1652019-04-07 13:17:16 +000068- `Chapter #10 <LangImpl10.html>`_: Conclusion and other useful LLVM
69 tidbits - This chapter wraps up the series by talking about
Chris Lattner13d35052019-04-07 13:42:29 +000070 potential ways to extend the language, and includes some
71 pointers to info on "special topics" like adding garbage
Chris Lattner2243a1652019-04-07 13:17:16 +000072 collection support, exceptions, debugging, support for "spaghetti
Chris Lattner13d35052019-04-07 13:42:29 +000073 stacks", and random tips and tricks.
Chris Lattner2243a1652019-04-07 13:17:16 +000074
75By the end of the tutorial, we'll have written a bit less than 1000 lines
76of non-comment, non-blank, lines of code. With this small amount of
Chris Lattner13d35052019-04-07 13:42:29 +000077code, we'll have built up a nice little compiler for a non-trivial
Chris Lattner2243a1652019-04-07 13:17:16 +000078language including a hand-written lexer, parser, AST, as well as code
Chris Lattner13d35052019-04-07 13:42:29 +000079generation support with a JIT compiler. The breadth of this
80tutorial is a great testament to the strengths of LLVM and shows why
81it is such a popular target for language designers.