llvm.org GIT mirror llvm / c599324
various improvements in wording, also unbreak the bot git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357867 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 4 months ago
3 changed file(s) with 44 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
729729
730730 Here is the code:
731731
732 .. literalinclude:: ../../examples/Kaleidoscope/Chapter2/toy.cpp
732 .. literalinclude:: ../../../examples/Kaleidoscope/Chapter2/toy.cpp
733733 :language: c++
734734
735735 `Next: Implementing Code Generation to LLVM IR `_
77 The Kaleidoscope Language
88 =========================
99
10 This tutorial will be illustrated with a toy language that we'll call
10 This tutorial is illustrated with a toy language called
1111 "`Kaleidoscope `_" (derived
1212 from "meaning beautiful, form, and view"). Kaleidoscope is a procedural
1313 language that allows you to define functions, use conditionals, math,
1414 etc. Over the course of the tutorial, we'll extend Kaleidoscope to
1515 support the if/then/else construct, a for loop, user defined operators,
16 JIT compilation with a simple command line interface, etc.
16 JIT compilation with a simple command line interface, debug info, etc.
1717
18 Because we want to keep things simple, the only datatype in Kaleidoscope
18 We want to keep things simple, so the only datatype in Kaleidoscope
1919 is a 64-bit floating point type (aka 'double' in C parlance). As such,
2020 all values are implicitly double precision and the language doesn't
2121 require type declarations. This gives the language a very nice and
3434 # This expression will compute the 40th number.
3535 fib(40)
3636
37 We also allow Kaleidoscope to call into standard library functions (the
38 LLVM JIT makes this completely trivial). This means that you can use the
37 We also allow Kaleidoscope to call into standard library functions - the
38 LLVM JIT makes this really easy. This means that you can use the
3939 'extern' keyword to define a function before you use it (this is also
40 useful for mutually recursive functions). For example:
40 useful for mutually recursive functions). For example:
4141
4242 ::
4343
5151 little Kaleidoscope application that `displays a Mandelbrot
5252 Set `_ at various levels of magnification.
5353
54 Lets dive into the implementation of this language!
54 Let's dive into the implementation of this language!
5555
5656 The Lexer
5757 =========
8888 as its ASCII value. If the current token is an identifier, the
8989 ``IdentifierStr`` global variable holds the name of the identifier. If
9090 the current token is a numeric literal (like 1.0), ``NumVal`` holds its
91 value. Note that we use global variables for simplicity, this is not the
91 value. We use global variables for simplicity, but this is not the
9292 best choice for a real language implementation :).
9393
9494 The actual implementation of the lexer is a single function named
146146 return tok_number;
147147 }
148148
149 This is all pretty straight-forward code for processing input. When
149 This is all pretty straightforward code for processing input. When
150150 reading a numeric value from input, we use the C ``strtod`` function to
151151 convert it to a numeric value that we store in ``NumVal``. Note that
152152 this isn't doing sufficient error checking: it will incorrectly read
153153 "1.23.45.67" and handle it as if you typed in "1.23". Feel free to
154 extend it :). Next we handle comments:
154 extend it! Next we handle comments:
155155
156156 .. code-block:: c++
157157
0 =============================================
11 My First Language Frontend with LLVM Tutorial
22 =============================================
3
4 **Requirements:** This tutorial assumes you know C++, but no previous
5 compiler experience is necessary.
36
47 Welcome to the "My First Language Frontend with LLVM" tutorial. Here we
58 run through the implementation of a simple language, showing
1114 iteratively over the course of several chapters, showing how it is built
1215 over time. This lets us cover a range of language design and LLVM-specific
1316 ideas, showing and explaining the code for it all along the way,
14 and reduces the amount of overwhelming details up front. We strongly
17 and reduces the overwhelming amount of details up front. We strongly
1518 encourage that you *work with this code* - make a copy and hack it up and
1619 experiment.
1720
18 Warning: In order to focus on teaching compiler techniques and LLVM
21 **Warning**: In order to focus on teaching compiler techniques and LLVM
1922 specifically,
2023 this tutorial does *not* show best practices in software engineering
2124 principles. For example, the code uses global variables
22 all over the place, doesn't use nice design patterns like
25 pervasively, doesn't use
2326 `visitors `_, etc... but
2427 instead keeps things simple and focuses on the topics at hand.
2528
2629 This tutorial is structured into chapters covering individual topics,
27 allowing you to skip ahead or over things as you wish:
30 allowing you to skip ahead as you wish:
2831
29 - `Chapter #1 `_: Introduction to the Kaleidoscope
30 language, and the definition of its Lexer. This shows where we are
32 - `Chapter #1: Kaleidoscope language and Lexer `_ -
33 This shows where we are
3134 going and the basic functionality that we want to build. A lexer
3235 is also the first part of building a parser for a language, and we
3336 use a simple C++ lexer which is easy to understand.
34 - `Chapter #2 `_: Implementing a Parser and AST -
37 - `Chapter #2: Implementing a Parser and AST `_ -
3538 With the lexer in place, we can talk about parsing techniques and
3639 basic AST construction. This tutorial describes recursive descent
3740 parsing and operator precedence parsing.
38 - `Chapter #3 `_: Code generation to LLVM IR - with
41 - `Chapter #3: Code generation to LLVM IR `_ - with
3942 the AST ready, we show how easy it is to generate LLVM IR, and show
4043 a simple way to incorporate LLVM into your project.
41 - `Chapter #4 `_: Adding JIT and Optimizer Support
42 - One great thing about LLVM is its support for JIT compilation, so
44 - `Chapter #4: Adding JIT and Optimizer Support `_ -
45 One great thing about LLVM is its support for JIT compilation, so
4346 we'll dive right into it and show you the 3 lines it takes to add JIT
4447 support. Later chapters show how to generate .o files.
45 - `Chapter #5 `_: Extending the Language: Control
46 Flow - With the basic language up and running, we show how to extend
48 - `Chapter #5: Extending the Language: Control Flow `_ - With the basic language up and running, we show how to extend
4749 it with control flow operations ('if' statement and a 'for' loop). This
4850 gives us a chance to talk about SSA construction and control
4951 flow.
50 - `Chapter #6 `_: Extending the Language:
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
52 - `Chapter #6: Extending the Language: User-defined Operators
53 `_ - This chapter extends the language to let
54 users define arbitrary unary and binary operators - with assignable
55 precedence! This allows us to build a significant piece of the
5456 "language" as library routines.
55 - `Chapter #7 `_: Extending the Language: Mutable
56 Variables - This chapter talks about adding user-defined local
57 - `Chapter #7: Extending the Language: Mutable Variables
58 `_ - This chapter talks about adding user-defined local
5759 variables along with an assignment operator. This shows how easy it is
5860 to construct SSA form in LLVM: LLVM does *not* require your front-end
5961 to construct SSA form in order to use it!
60 - `Chapter #8 `_: Compiling to Object Files - This
62 - `Chapter #8: Compiling to Object Files `_ - This
6163 chapter explains how to take LLVM IR and compile it down to object
6264 files, like a static compiler does.
63 - `Chapter #9 `_: Extending the Language: Debug
64 Information - A real language needs to support debuggers, so we add
65 debug information that allows setting breakpoints in Kaleidoscope
65 - `Chapter #9: Extending the Language: Debug Information
66 `_ - A real language needs to support debuggers, so we
67 add debug information that allows setting breakpoints in Kaleidoscope
6668 functions, print out argument variables, and call functions!
67 - `Chapter #10 `_: Conclusion and other useful LLVM
68 tidbits - This chapter wraps up the series by talking about
69 potential ways to extend the language, and includes some
70 pointers to info on "special topics" like adding garbage
69 - `Chapter #10: Conclusion and other tidbits `_ - This
70 chapter wraps up the series by discussing ways to extend the language
71 and includes pointers to info on "special topics" like adding garbage
7172 collection support, exceptions, debugging, support for "spaghetti
72 stacks", and random tips and tricks.
73 stacks", etc.
7374
7475 By the end of the tutorial, we'll have written a bit less than 1000 lines
75 of non-comment, non-blank, lines of code. With this small amount of
76 of (non-comment, non-blank) lines of code. With this small amount of
7677 code, we'll have built up a nice little compiler for a non-trivial
7778 language including a hand-written lexer, parser, AST, as well as code
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.
79 generation support - both static and JIT! The breadth of this is a great
80 testament to the strengths of LLVM and shows why it is such a popular
81 target for language designers and others who need high performance code
82 generation.