llvm.org GIT mirror llvm / e0a373b
remove some unhelpful language from the tutorial git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357863 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 4 months ago
2 changed file(s) with 106 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
173173 -------------------------------
175175 One thing about LLVM that turns off many people is that it does not
176 solve all the world's problems in one system (sorry 'world hunger',
177 someone else will have to solve you some other day). One specific
176 solve all the world's problems in one system. One specific
178177 complaint is that people perceive LLVM as being incapable of performing
179178 high-level language-specific optimization: LLVM "loses too much
180 information".
182 Unfortunately, this is really not the place to give you a full and
183 unified version of "Chris Lattner's theory of compiler design". Instead,
184 I'll make a few observations:
179 information". Here are a few observations about this:
186181 First, you're right that LLVM does lose information. For example, as of
187182 this writing, there is no way to distinguish in the LLVM IR whether an
0 =============================================
1 My First Language Frontend: Table of Contents
2 =============================================
4 Introduction to the "Kaleidoscope" Language Tutorial
5 ====================================================
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.
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.
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
25 all over the place, doesn't use nice design patterns like
26 `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.
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:
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.
41 - `Chapter #2 `_: Implementing a Parser and AST -
42 With the lexer in place, we can talk about parsing techniques and
43 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.
50 - `Chapter #4 `_: Adding JIT and Optimizer Support
51 - Because a lot of people are interested in using LLVM as a JIT,
52 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. :)
55 - `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
59 flow.
60 - `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.
66 - `Chapter #7 `_: Extending the Language: Mutable
67 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!
72 - `Chapter #8 `_: Compiling to Object Files - This
73 chapter explains how to take LLVM IR and compile it down to object
74 files.
75 - `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!
82 - `Chapter #10 `_: Conclusion and other useful LLVM
83 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
86 collection support, exceptions, debugging, support for "spaghetti
87 stacks", and a bunch of other tips and tricks.
89 By the end of the tutorial, we'll have written a bit less than 1000 lines
90 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
92 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.
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!