xref: /llvm-project/llvm/docs/tutorial/MyFirstLanguageFrontend/index.rst (revision 0c660256eb41fb0ba44277a32f39d2a028f797f2)
12243a165SChris Lattner=============================================
213d3505aSChris LattnerMy First Language Frontend with LLVM Tutorial
32243a165SChris Lattner=============================================
42243a165SChris Lattner
5*63cf7040Skristina.. toctree::
6*63cf7040Skristina   :hidden:
7*63cf7040Skristina
8*63cf7040Skristina   LangImpl01
9*63cf7040Skristina   LangImpl02
10*63cf7040Skristina   LangImpl03
11*63cf7040Skristina   LangImpl04
12*63cf7040Skristina   LangImpl05
13*63cf7040Skristina   LangImpl06
14*63cf7040Skristina   LangImpl07
15*63cf7040Skristina   LangImpl08
16*63cf7040Skristina   LangImpl09
17*63cf7040Skristina   LangImpl10
18*63cf7040Skristina
190fa6c158SChris Lattner**Requirements:** This tutorial assumes you know C++, but no previous
200fa6c158SChris Lattnercompiler experience is necessary.
210fa6c158SChris Lattner
2213d3505aSChris LattnerWelcome to the "My First Language Frontend with LLVM" tutorial. Here we
2313d3505aSChris Lattnerrun through the implementation of a simple language, showing
2413d3505aSChris Lattnerhow fun and easy it can be.  This tutorial will get you up and running
2513d3505aSChris Lattnerfast and show a concrete example of something that uses LLVM to generate
2613d3505aSChris Lattnercode.
272243a165SChris Lattner
2813d3505aSChris LattnerThis tutorial introduces the simple "Kaleidoscope" language, building it
2913d3505aSChris Lattneriteratively over the course of several chapters, showing how it is built
3013d3505aSChris Lattnerover time. This lets us cover a range of language design and LLVM-specific
3113d3505aSChris Lattnerideas, showing and explaining the code for it all along the way,
320fa6c158SChris Lattnerand reduces the overwhelming amount of details up front.  We strongly
3313d3505aSChris Lattnerencourage that you *work with this code* - make a copy and hack it up and
3413d3505aSChris Lattnerexperiment.
352243a165SChris Lattner
360fa6c158SChris Lattner**Warning**: In order to focus on teaching compiler techniques and LLVM
3713d3505aSChris Lattnerspecifically,
3813d3505aSChris Lattnerthis tutorial does *not* show best practices in software engineering
3913d3505aSChris Lattnerprinciples.  For example, the code uses global variables
400fa6c158SChris Lattnerpervasively, doesn't use
412243a165SChris Lattner`visitors <http://en.wikipedia.org/wiki/Visitor_pattern>`_, etc... but
4213d3505aSChris Lattnerinstead keeps things simple and focuses on the topics at hand.
432243a165SChris Lattner
4413d3505aSChris LattnerThis tutorial is structured into chapters covering individual topics,
450fa6c158SChris Lattnerallowing you to skip ahead as you wish:
462243a165SChris Lattner
470fa6c158SChris Lattner-  `Chapter #1: Kaleidoscope language and Lexer <LangImpl01.html>`_ -
480fa6c158SChris Lattner   This shows where we are
4913d3505aSChris Lattner   going and the basic functionality that we want to build.  A lexer
5013d3505aSChris Lattner   is also the first part of building a parser for a language, and we
5113d3505aSChris Lattner   use a simple C++ lexer which is easy to understand.
520fa6c158SChris Lattner-  `Chapter #2: Implementing a Parser and AST <LangImpl02.html>`_ -
532243a165SChris Lattner   With the lexer in place, we can talk about parsing techniques and
542243a165SChris Lattner   basic AST construction. This tutorial describes recursive descent
5513d3505aSChris Lattner   parsing and operator precedence parsing.
560fa6c158SChris Lattner-  `Chapter #3: Code generation to LLVM IR <LangImpl03.html>`_ - with
5713d3505aSChris Lattner   the AST ready, we show how easy it is to generate LLVM IR, and show
5813d3505aSChris Lattner   a simple way to incorporate LLVM into your project.
590fa6c158SChris Lattner-  `Chapter #4: Adding JIT and Optimizer Support <LangImpl04.html>`_ -
600fa6c158SChris Lattner   One great thing about LLVM is its support for JIT compilation, so
612243a165SChris Lattner   we'll dive right into it and show you the 3 lines it takes to add JIT
6213d3505aSChris Lattner   support. Later chapters show how to generate .o files.
63*63cf7040Skristina-  `Chapter #5: Extending the Language: Control Flow <LangImpl05.html>`_ - With
64*63cf7040Skristina   the basic language up and running, we show how to extend
6513d3505aSChris Lattner   it with control flow operations ('if' statement and a 'for' loop). This
6613d3505aSChris Lattner   gives us a chance to talk about SSA construction and control
672243a165SChris Lattner   flow.
680fa6c158SChris Lattner-  `Chapter #6: Extending the Language: User-defined Operators
690fa6c158SChris Lattner   <LangImpl06.html>`_ - This chapter extends the language to let
700fa6c158SChris Lattner   users define arbitrary unary and binary operators - with assignable
710fa6c158SChris Lattner   precedence!  This allows us to build a significant piece of the
7213d3505aSChris Lattner   "language" as library routines.
730fa6c158SChris Lattner-  `Chapter #7: Extending the Language: Mutable Variables
740fa6c158SChris Lattner   <LangImpl07.html>`_ - This chapter talks about adding user-defined local
7513d3505aSChris Lattner   variables along with an assignment operator. This shows how easy it is
7613d3505aSChris Lattner   to construct SSA form in LLVM: LLVM does *not* require your front-end
7713d3505aSChris Lattner   to construct SSA form in order to use it!
780fa6c158SChris Lattner-  `Chapter #8: Compiling to Object Files <LangImpl08.html>`_ - This
792243a165SChris Lattner   chapter explains how to take LLVM IR and compile it down to object
8013d3505aSChris Lattner   files, like a static compiler does.
8132a8e742SChris Lattner-  `Chapter #9: Debug Information <LangImpl09.html>`_ - A real language
8232a8e742SChris Lattner   needs to support debuggers, so we
830fa6c158SChris Lattner   add debug information that allows setting breakpoints in Kaleidoscope
8413d3505aSChris Lattner   functions, print out argument variables, and call functions!
850fa6c158SChris Lattner-  `Chapter #10: Conclusion and other tidbits <LangImpl10.html>`_ - This
860fa6c158SChris Lattner   chapter wraps up the series by discussing ways to extend the language
870fa6c158SChris Lattner   and includes pointers to info on "special topics" like adding garbage
882243a165SChris Lattner   collection support, exceptions, debugging, support for "spaghetti
890fa6c158SChris Lattner   stacks", etc.
902243a165SChris Lattner
912243a165SChris LattnerBy the end of the tutorial, we'll have written a bit less than 1000 lines
920fa6c158SChris Lattnerof (non-comment, non-blank) lines of code. With this small amount of
9313d3505aSChris Lattnercode, we'll have built up a nice little compiler for a non-trivial
942243a165SChris Lattnerlanguage including a hand-written lexer, parser, AST, as well as code
950fa6c158SChris Lattnergeneration support - both static and JIT!  The breadth of this is a great
960fa6c158SChris Lattnertestament to the strengths of LLVM and shows why it is such a popular
970fa6c158SChris Lattnertarget for language designers and others who need high performance code
980fa6c158SChris Lattnergeneration.
99