llvm.org GIT mirror llvm / a932c9a
make a bunch of cleanups in wording and tone git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357865 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 4 months ago
1 changed file(s) with 52 addition(s) and 75 deletion(s). Raw diff Collapse all Expand all
0 =============================================
1 My First Language Frontend: Table of Contents
1 My First Language Frontend with LLVM Tutorial
22 =============================================
33
4 Introduction to the "Kaleidoscope" Language Tutorial
5 ====================================================
4 Welcome to the "My First Language Frontend with LLVM" tutorial. Here we
5 run through the implementation of a simple language, showing
6 how fun and easy it can be. This tutorial will get you up and running
7 fast and show a concrete example of something that uses LLVM to generate
8 code.
69
7 Welcome to the "Implementing a language with LLVM" tutorial. This
8 tutorial runs through the implementation of a simple language, showing
9 how fun and easy it can be. This tutorial will get you up and started as
10 well as help to build a framework you can extend to other languages. The
11 code in this tutorial can also be used as a playground to hack on other
12 LLVM specific things.
10 This tutorial introduces the simple "Kaleidoscope" language, building it
11 iteratively over the course of several chapters, showing how it is built
12 over time. This lets us cover a range of language design and LLVM-specific
13 ideas, showing and explaining the code for it all along the way,
14 and reduces the amount of overwhelming details up front. We strongly
15 encourage that you *work with this code* - make a copy and hack it up and
16 experiment.
1317
14 The goal of this tutorial is to progressively unveil our language,
15 describing how it is built up over time. This will let us cover a fairly
16 broad range of language design and LLVM-specific usage issues, showing
17 and explaining the code for it all along the way, without overwhelming
18 you with tons of details up front.
19
20 It is useful to point out ahead of time that this tutorial is really
21 about teaching compiler techniques and LLVM specifically, *not* about
22 teaching modern and sane software engineering principles. In practice,
23 this means that we'll take a number of shortcuts to simplify the
24 exposition. For example, the code uses global variables
18 Warning: In order to focus on teaching compiler techniques and LLVM
19 specifically,
20 this tutorial does *not* show best practices in software engineering
21 principles. For example, the code uses global variables
2522 all over the place, doesn't use nice design patterns like
2623 `visitors `_, etc... but
27 it is very simple. If you dig in and use the code as a basis for future
28 projects, fixing these deficiencies shouldn't be hard.
24 instead keeps things simple and focuses on the topics at hand.
2925
30 I've tried to put this tutorial together in a way that makes chapters
31 easy to skip over if you are already familiar with or are uninterested
32 in the various pieces. The structure of the tutorial is:
26 This tutorial is structured into chapters covering individual topics,
27 allowing you to skip ahead or over things as you wish:
3328
34 - `Chapter #1 <#language>`_: Introduction to the Kaleidoscope
35 language, and the definition of its Lexer - This shows where we are
36 going and the basic functionality that we want it to do. In order to
37 make this tutorial maximally understandable and hackable, we choose
38 to implement everything in C++ instead of using lexer and parser
39 generators. LLVM works just fine with such tools, feel free
40 to use one if you prefer.
29 - `Chapter #1 `_: Introduction to the Kaleidoscope
30 language, and the definition of its Lexer. This shows where we are
31 going and the basic functionality that we want to build. A lexer
32 is also the first part of building a parser for a language, and we
33 use a simple C++ lexer which is easy to understand.
4134 - `Chapter #2 `_: Implementing a Parser and AST -
4235 With the lexer in place, we can talk about parsing techniques and
4336 basic AST construction. This tutorial describes recursive descent
44 parsing and operator precedence parsing. Nothing in Chapters 1 or 2
45 is LLVM-specific, the code doesn't even link in LLVM at this point.
46 :)
47 - `Chapter #3 `_: Code generation to LLVM IR - With
48 the AST ready, we can show off how easy generation of LLVM IR really
49 is.
37 parsing and operator precedence parsing.
38 - `Chapter #3 `_: Code generation to LLVM IR - with
39 the AST ready, we show how easy it is to generate LLVM IR, and show
40 a simple way to incorporate LLVM into your project.
5041 - `Chapter #4 `_: Adding JIT and Optimizer Support
51 - Because a lot of people are interested in using LLVM as a JIT,
42 - One great thing about LLVM is its support for JIT compilation, so
5243 we'll dive right into it and show you the 3 lines it takes to add JIT
53 support. LLVM is also useful in many other ways, but this is one
54 simple and "sexy" way to show off its power. :)
44 support. Later chapters show how to generate .o files.
5545 - `Chapter #5 `_: Extending the Language: Control
56 Flow - With the language up and running, we show how to extend it
57 with control flow operations (if/then/else and a 'for' loop). This
58 gives us a chance to talk about simple SSA construction and control
46 Flow - With the basic language up and running, we show how to extend
47 it with control flow operations ('if' statement and a 'for' loop). This
48 gives us a chance to talk about SSA construction and control
5949 flow.
6050 - `Chapter #6 `_: Extending the Language:
61 User-defined Operators - This is a silly but fun chapter that talks
62 about extending the language to let the user program define their own
63 arbitrary unary and binary operators (with assignable precedence!).
64 This lets us build a significant piece of the "language" as library
65 routines.
51 User-defined Operators - This chapter extends the language to let
52 users define arbitrary unary and binary operators (with assignable
53 precedence!). This lets us build a significant piece of the
54 "language" as library routines.
6655 - `Chapter #7 `_: Extending the Language: Mutable
6756 Variables - This chapter talks about adding user-defined local
68 variables along with an assignment operator. The interesting part
69 about this is how easy and trivial it is to construct SSA form in
70 LLVM: no, LLVM does *not* require your front-end to construct SSA
71 form!
57 variables along with an assignment operator. This shows how easy it is
58 to construct SSA form in LLVM: LLVM does *not* require your front-end
59 to construct SSA form in order to use it!
7260 - `Chapter #8 `_: Compiling to Object Files - This
7361 chapter explains how to take LLVM IR and compile it down to object
74 files.
62 files, like a static compiler does.
7563 - `Chapter #9 `_: Extending the Language: Debug
76 Information - Having built a decent little programming language with
77 control flow, functions and mutable variables, we consider what it
78 takes to add debug information to standalone executables. This debug
79 information will allow you to set breakpoints in Kaleidoscope
80 functions, print out argument variables, and call functions - all
81 from within the debugger!
64 Information - A real language needs to support debuggers, so we add
65 debug information that allows setting breakpoints in Kaleidoscope
66 functions, print out argument variables, and call functions!
8267 - `Chapter #10 `_: Conclusion and other useful LLVM
8368 tidbits - This chapter wraps up the series by talking about
84 potential ways to extend the language, but also includes a bunch of
85 pointers to info about "special topics" like adding garbage
69 potential ways to extend the language, and includes some
70 pointers to info on "special topics" like adding garbage
8671 collection support, exceptions, debugging, support for "spaghetti
87 stacks", and a bunch of other tips and tricks.
72 stacks", and random tips and tricks.
8873
8974 By the end of the tutorial, we'll have written a bit less than 1000 lines
9075 of non-comment, non-blank, lines of code. With this small amount of
91 code, we'll have built up a very reasonable compiler for a non-trivial
76 code, we'll have built up a nice little compiler for a non-trivial
9277 language including a hand-written lexer, parser, AST, as well as code
93 generation support with a JIT compiler. While other systems may have
94 interesting "hello world" tutorials, I think the breadth of this
95 tutorial is a great testament to the strengths of LLVM and why you
96 should consider it if you're interested in language or compiler design.
97
98 A note about this tutorial: we expect you to extend the language and
99 play with it on your own. Take the code and go crazy hacking away at it,
100 compilers don't need to be scary creatures - it can be a lot of fun to
101 play with languages!
102
103
78 generation support with a JIT compiler. The breadth of this
79 tutorial is a great testament to the strengths of LLVM and shows why
80 it is such a popular target for language designers.