xref: /aosp_15_r20/external/llvm/docs/tutorial/LangImpl05.rst (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker==================================================
2*9880d681SAndroid Build Coastguard WorkerKaleidoscope: Extending the Language: Control Flow
3*9880d681SAndroid Build Coastguard Worker==================================================
4*9880d681SAndroid Build Coastguard Worker
5*9880d681SAndroid Build Coastguard Worker.. contents::
6*9880d681SAndroid Build Coastguard Worker   :local:
7*9880d681SAndroid Build Coastguard Worker
8*9880d681SAndroid Build Coastguard WorkerChapter 5 Introduction
9*9880d681SAndroid Build Coastguard Worker======================
10*9880d681SAndroid Build Coastguard Worker
11*9880d681SAndroid Build Coastguard WorkerWelcome to Chapter 5 of the "`Implementing a language with
12*9880d681SAndroid Build Coastguard WorkerLLVM <index.html>`_" tutorial. Parts 1-4 described the implementation of
13*9880d681SAndroid Build Coastguard Workerthe simple Kaleidoscope language and included support for generating
14*9880d681SAndroid Build Coastguard WorkerLLVM IR, followed by optimizations and a JIT compiler. Unfortunately, as
15*9880d681SAndroid Build Coastguard Workerpresented, Kaleidoscope is mostly useless: it has no control flow other
16*9880d681SAndroid Build Coastguard Workerthan call and return. This means that you can't have conditional
17*9880d681SAndroid Build Coastguard Workerbranches in the code, significantly limiting its power. In this episode
18*9880d681SAndroid Build Coastguard Workerof "build that compiler", we'll extend Kaleidoscope to have an
19*9880d681SAndroid Build Coastguard Workerif/then/else expression plus a simple 'for' loop.
20*9880d681SAndroid Build Coastguard Worker
21*9880d681SAndroid Build Coastguard WorkerIf/Then/Else
22*9880d681SAndroid Build Coastguard Worker============
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard WorkerExtending Kaleidoscope to support if/then/else is quite straightforward.
25*9880d681SAndroid Build Coastguard WorkerIt basically requires adding support for this "new" concept to the
26*9880d681SAndroid Build Coastguard Workerlexer, parser, AST, and LLVM code emitter. This example is nice, because
27*9880d681SAndroid Build Coastguard Workerit shows how easy it is to "grow" a language over time, incrementally
28*9880d681SAndroid Build Coastguard Workerextending it as new ideas are discovered.
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard WorkerBefore we get going on "how" we add this extension, lets talk about
31*9880d681SAndroid Build Coastguard Worker"what" we want. The basic idea is that we want to be able to write this
32*9880d681SAndroid Build Coastguard Workersort of thing:
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker::
35*9880d681SAndroid Build Coastguard Worker
36*9880d681SAndroid Build Coastguard Worker    def fib(x)
37*9880d681SAndroid Build Coastguard Worker      if x < 3 then
38*9880d681SAndroid Build Coastguard Worker        1
39*9880d681SAndroid Build Coastguard Worker      else
40*9880d681SAndroid Build Coastguard Worker        fib(x-1)+fib(x-2);
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard WorkerIn Kaleidoscope, every construct is an expression: there are no
43*9880d681SAndroid Build Coastguard Workerstatements. As such, the if/then/else expression needs to return a value
44*9880d681SAndroid Build Coastguard Workerlike any other. Since we're using a mostly functional form, we'll have
45*9880d681SAndroid Build Coastguard Workerit evaluate its conditional, then return the 'then' or 'else' value
46*9880d681SAndroid Build Coastguard Workerbased on how the condition was resolved. This is very similar to the C
47*9880d681SAndroid Build Coastguard Worker"?:" expression.
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard WorkerThe semantics of the if/then/else expression is that it evaluates the
50*9880d681SAndroid Build Coastguard Workercondition to a boolean equality value: 0.0 is considered to be false and
51*9880d681SAndroid Build Coastguard Workereverything else is considered to be true. If the condition is true, the
52*9880d681SAndroid Build Coastguard Workerfirst subexpression is evaluated and returned, if the condition is
53*9880d681SAndroid Build Coastguard Workerfalse, the second subexpression is evaluated and returned. Since
54*9880d681SAndroid Build Coastguard WorkerKaleidoscope allows side-effects, this behavior is important to nail
55*9880d681SAndroid Build Coastguard Workerdown.
56*9880d681SAndroid Build Coastguard Worker
57*9880d681SAndroid Build Coastguard WorkerNow that we know what we "want", lets break this down into its
58*9880d681SAndroid Build Coastguard Workerconstituent pieces.
59*9880d681SAndroid Build Coastguard Worker
60*9880d681SAndroid Build Coastguard WorkerLexer Extensions for If/Then/Else
61*9880d681SAndroid Build Coastguard Worker---------------------------------
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard WorkerThe lexer extensions are straightforward. First we add new enum values
64*9880d681SAndroid Build Coastguard Workerfor the relevant tokens:
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
67*9880d681SAndroid Build Coastguard Worker
68*9880d681SAndroid Build Coastguard Worker      // control
69*9880d681SAndroid Build Coastguard Worker      tok_if = -6,
70*9880d681SAndroid Build Coastguard Worker      tok_then = -7,
71*9880d681SAndroid Build Coastguard Worker      tok_else = -8,
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard WorkerOnce we have that, we recognize the new keywords in the lexer. This is
74*9880d681SAndroid Build Coastguard Workerpretty simple stuff:
75*9880d681SAndroid Build Coastguard Worker
76*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
77*9880d681SAndroid Build Coastguard Worker
78*9880d681SAndroid Build Coastguard Worker        ...
79*9880d681SAndroid Build Coastguard Worker        if (IdentifierStr == "def")
80*9880d681SAndroid Build Coastguard Worker          return tok_def;
81*9880d681SAndroid Build Coastguard Worker        if (IdentifierStr == "extern")
82*9880d681SAndroid Build Coastguard Worker          return tok_extern;
83*9880d681SAndroid Build Coastguard Worker        if (IdentifierStr == "if")
84*9880d681SAndroid Build Coastguard Worker          return tok_if;
85*9880d681SAndroid Build Coastguard Worker        if (IdentifierStr == "then")
86*9880d681SAndroid Build Coastguard Worker          return tok_then;
87*9880d681SAndroid Build Coastguard Worker        if (IdentifierStr == "else")
88*9880d681SAndroid Build Coastguard Worker          return tok_else;
89*9880d681SAndroid Build Coastguard Worker        return tok_identifier;
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard WorkerAST Extensions for If/Then/Else
92*9880d681SAndroid Build Coastguard Worker-------------------------------
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard WorkerTo represent the new expression we add a new AST node for it:
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker    /// IfExprAST - Expression class for if/then/else.
99*9880d681SAndroid Build Coastguard Worker    class IfExprAST : public ExprAST {
100*9880d681SAndroid Build Coastguard Worker      std::unique_ptr<ExprAST> Cond, Then, Else;
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker    public:
103*9880d681SAndroid Build Coastguard Worker      IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,
104*9880d681SAndroid Build Coastguard Worker                std::unique_ptr<ExprAST> Else)
105*9880d681SAndroid Build Coastguard Worker        : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
106*9880d681SAndroid Build Coastguard Worker      virtual Value *codegen();
107*9880d681SAndroid Build Coastguard Worker    };
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard WorkerThe AST node just has pointers to the various subexpressions.
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard WorkerParser Extensions for If/Then/Else
112*9880d681SAndroid Build Coastguard Worker----------------------------------
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard WorkerNow that we have the relevant tokens coming from the lexer and we have
115*9880d681SAndroid Build Coastguard Workerthe AST node to build, our parsing logic is relatively straightforward.
116*9880d681SAndroid Build Coastguard WorkerFirst we define a new parsing function:
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker    /// ifexpr ::= 'if' expression 'then' expression 'else' expression
121*9880d681SAndroid Build Coastguard Worker    static std::unique_ptr<ExprAST> ParseIfExpr() {
122*9880d681SAndroid Build Coastguard Worker      getNextToken();  // eat the if.
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker      // condition.
125*9880d681SAndroid Build Coastguard Worker      auto Cond = ParseExpression();
126*9880d681SAndroid Build Coastguard Worker      if (!Cond)
127*9880d681SAndroid Build Coastguard Worker        return nullptr;
128*9880d681SAndroid Build Coastguard Worker
129*9880d681SAndroid Build Coastguard Worker      if (CurTok != tok_then)
130*9880d681SAndroid Build Coastguard Worker        return LogError("expected then");
131*9880d681SAndroid Build Coastguard Worker      getNextToken();  // eat the then
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker      auto Then = ParseExpression();
134*9880d681SAndroid Build Coastguard Worker      if (!Then)
135*9880d681SAndroid Build Coastguard Worker        return nullptr;
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker      if (CurTok != tok_else)
138*9880d681SAndroid Build Coastguard Worker        return LogError("expected else");
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker      getNextToken();
141*9880d681SAndroid Build Coastguard Worker
142*9880d681SAndroid Build Coastguard Worker      auto Else = ParseExpression();
143*9880d681SAndroid Build Coastguard Worker      if (!Else)
144*9880d681SAndroid Build Coastguard Worker        return nullptr;
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker      return llvm::make_unique<IfExprAST>(std::move(Cond), std::move(Then),
147*9880d681SAndroid Build Coastguard Worker                                          std::move(Else));
148*9880d681SAndroid Build Coastguard Worker    }
149*9880d681SAndroid Build Coastguard Worker
150*9880d681SAndroid Build Coastguard WorkerNext we hook it up as a primary expression:
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
153*9880d681SAndroid Build Coastguard Worker
154*9880d681SAndroid Build Coastguard Worker    static std::unique_ptr<ExprAST> ParsePrimary() {
155*9880d681SAndroid Build Coastguard Worker      switch (CurTok) {
156*9880d681SAndroid Build Coastguard Worker      default:
157*9880d681SAndroid Build Coastguard Worker        return LogError("unknown token when expecting an expression");
158*9880d681SAndroid Build Coastguard Worker      case tok_identifier:
159*9880d681SAndroid Build Coastguard Worker        return ParseIdentifierExpr();
160*9880d681SAndroid Build Coastguard Worker      case tok_number:
161*9880d681SAndroid Build Coastguard Worker        return ParseNumberExpr();
162*9880d681SAndroid Build Coastguard Worker      case '(':
163*9880d681SAndroid Build Coastguard Worker        return ParseParenExpr();
164*9880d681SAndroid Build Coastguard Worker      case tok_if:
165*9880d681SAndroid Build Coastguard Worker        return ParseIfExpr();
166*9880d681SAndroid Build Coastguard Worker      }
167*9880d681SAndroid Build Coastguard Worker    }
168*9880d681SAndroid Build Coastguard Worker
169*9880d681SAndroid Build Coastguard WorkerLLVM IR for If/Then/Else
170*9880d681SAndroid Build Coastguard Worker------------------------
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard WorkerNow that we have it parsing and building the AST, the final piece is
173*9880d681SAndroid Build Coastguard Workeradding LLVM code generation support. This is the most interesting part
174*9880d681SAndroid Build Coastguard Workerof the if/then/else example, because this is where it starts to
175*9880d681SAndroid Build Coastguard Workerintroduce new concepts. All of the code above has been thoroughly
176*9880d681SAndroid Build Coastguard Workerdescribed in previous chapters.
177*9880d681SAndroid Build Coastguard Worker
178*9880d681SAndroid Build Coastguard WorkerTo motivate the code we want to produce, lets take a look at a simple
179*9880d681SAndroid Build Coastguard Workerexample. Consider:
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker::
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker    extern foo();
184*9880d681SAndroid Build Coastguard Worker    extern bar();
185*9880d681SAndroid Build Coastguard Worker    def baz(x) if x then foo() else bar();
186*9880d681SAndroid Build Coastguard Worker
187*9880d681SAndroid Build Coastguard WorkerIf you disable optimizations, the code you'll (soon) get from
188*9880d681SAndroid Build Coastguard WorkerKaleidoscope looks like this:
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker    declare double @foo()
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker    declare double @bar()
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker    define double @baz(double %x) {
197*9880d681SAndroid Build Coastguard Worker    entry:
198*9880d681SAndroid Build Coastguard Worker      %ifcond = fcmp one double %x, 0.000000e+00
199*9880d681SAndroid Build Coastguard Worker      br i1 %ifcond, label %then, label %else
200*9880d681SAndroid Build Coastguard Worker
201*9880d681SAndroid Build Coastguard Worker    then:       ; preds = %entry
202*9880d681SAndroid Build Coastguard Worker      %calltmp = call double @foo()
203*9880d681SAndroid Build Coastguard Worker      br label %ifcont
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker    else:       ; preds = %entry
206*9880d681SAndroid Build Coastguard Worker      %calltmp1 = call double @bar()
207*9880d681SAndroid Build Coastguard Worker      br label %ifcont
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker    ifcont:     ; preds = %else, %then
210*9880d681SAndroid Build Coastguard Worker      %iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ]
211*9880d681SAndroid Build Coastguard Worker      ret double %iftmp
212*9880d681SAndroid Build Coastguard Worker    }
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard WorkerTo visualize the control flow graph, you can use a nifty feature of the
215*9880d681SAndroid Build Coastguard WorkerLLVM '`opt <http://llvm.org/cmds/opt.html>`_' tool. If you put this LLVM
216*9880d681SAndroid Build Coastguard WorkerIR into "t.ll" and run "``llvm-as < t.ll | opt -analyze -view-cfg``", `a
217*9880d681SAndroid Build Coastguard Workerwindow will pop up <../ProgrammersManual.html#viewing-graphs-while-debugging-code>`_ and you'll
218*9880d681SAndroid Build Coastguard Workersee this graph:
219*9880d681SAndroid Build Coastguard Worker
220*9880d681SAndroid Build Coastguard Worker.. figure:: LangImpl05-cfg.png
221*9880d681SAndroid Build Coastguard Worker   :align: center
222*9880d681SAndroid Build Coastguard Worker   :alt: Example CFG
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker   Example CFG
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard WorkerAnother way to get this is to call "``F->viewCFG()``" or
227*9880d681SAndroid Build Coastguard Worker"``F->viewCFGOnly()``" (where F is a "``Function*``") either by
228*9880d681SAndroid Build Coastguard Workerinserting actual calls into the code and recompiling or by calling these
229*9880d681SAndroid Build Coastguard Workerin the debugger. LLVM has many nice features for visualizing various
230*9880d681SAndroid Build Coastguard Workergraphs.
231*9880d681SAndroid Build Coastguard Worker
232*9880d681SAndroid Build Coastguard WorkerGetting back to the generated code, it is fairly simple: the entry block
233*9880d681SAndroid Build Coastguard Workerevaluates the conditional expression ("x" in our case here) and compares
234*9880d681SAndroid Build Coastguard Workerthe result to 0.0 with the "``fcmp one``" instruction ('one' is "Ordered
235*9880d681SAndroid Build Coastguard Workerand Not Equal"). Based on the result of this expression, the code jumps
236*9880d681SAndroid Build Coastguard Workerto either the "then" or "else" blocks, which contain the expressions for
237*9880d681SAndroid Build Coastguard Workerthe true/false cases.
238*9880d681SAndroid Build Coastguard Worker
239*9880d681SAndroid Build Coastguard WorkerOnce the then/else blocks are finished executing, they both branch back
240*9880d681SAndroid Build Coastguard Workerto the 'ifcont' block to execute the code that happens after the
241*9880d681SAndroid Build Coastguard Workerif/then/else. In this case the only thing left to do is to return to the
242*9880d681SAndroid Build Coastguard Workercaller of the function. The question then becomes: how does the code
243*9880d681SAndroid Build Coastguard Workerknow which expression to return?
244*9880d681SAndroid Build Coastguard Worker
245*9880d681SAndroid Build Coastguard WorkerThe answer to this question involves an important SSA operation: the
246*9880d681SAndroid Build Coastguard Worker`Phi
247*9880d681SAndroid Build Coastguard Workeroperation <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_.
248*9880d681SAndroid Build Coastguard WorkerIf you're not familiar with SSA, `the wikipedia
249*9880d681SAndroid Build Coastguard Workerarticle <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_
250*9880d681SAndroid Build Coastguard Workeris a good introduction and there are various other introductions to it
251*9880d681SAndroid Build Coastguard Workeravailable on your favorite search engine. The short version is that
252*9880d681SAndroid Build Coastguard Worker"execution" of the Phi operation requires "remembering" which block
253*9880d681SAndroid Build Coastguard Workercontrol came from. The Phi operation takes on the value corresponding to
254*9880d681SAndroid Build Coastguard Workerthe input control block. In this case, if control comes in from the
255*9880d681SAndroid Build Coastguard Worker"then" block, it gets the value of "calltmp". If control comes from the
256*9880d681SAndroid Build Coastguard Worker"else" block, it gets the value of "calltmp1".
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard WorkerAt this point, you are probably starting to think "Oh no! This means my
259*9880d681SAndroid Build Coastguard Workersimple and elegant front-end will have to start generating SSA form in
260*9880d681SAndroid Build Coastguard Workerorder to use LLVM!". Fortunately, this is not the case, and we strongly
261*9880d681SAndroid Build Coastguard Workeradvise *not* implementing an SSA construction algorithm in your
262*9880d681SAndroid Build Coastguard Workerfront-end unless there is an amazingly good reason to do so. In
263*9880d681SAndroid Build Coastguard Workerpractice, there are two sorts of values that float around in code
264*9880d681SAndroid Build Coastguard Workerwritten for your average imperative programming language that might need
265*9880d681SAndroid Build Coastguard WorkerPhi nodes:
266*9880d681SAndroid Build Coastguard Worker
267*9880d681SAndroid Build Coastguard Worker#. Code that involves user variables: ``x = 1; x = x + 1;``
268*9880d681SAndroid Build Coastguard Worker#. Values that are implicit in the structure of your AST, such as the
269*9880d681SAndroid Build Coastguard Worker   Phi node in this case.
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard WorkerIn `Chapter 7 <LangImpl7.html>`_ of this tutorial ("mutable variables"),
272*9880d681SAndroid Build Coastguard Workerwe'll talk about #1 in depth. For now, just believe me that you don't
273*9880d681SAndroid Build Coastguard Workerneed SSA construction to handle this case. For #2, you have the choice
274*9880d681SAndroid Build Coastguard Workerof using the techniques that we will describe for #1, or you can insert
275*9880d681SAndroid Build Coastguard WorkerPhi nodes directly, if convenient. In this case, it is really
276*9880d681SAndroid Build Coastguard Workereasy to generate the Phi node, so we choose to do it directly.
277*9880d681SAndroid Build Coastguard Worker
278*9880d681SAndroid Build Coastguard WorkerOkay, enough of the motivation and overview, lets generate code!
279*9880d681SAndroid Build Coastguard Worker
280*9880d681SAndroid Build Coastguard WorkerCode Generation for If/Then/Else
281*9880d681SAndroid Build Coastguard Worker--------------------------------
282*9880d681SAndroid Build Coastguard Worker
283*9880d681SAndroid Build Coastguard WorkerIn order to generate code for this, we implement the ``codegen`` method
284*9880d681SAndroid Build Coastguard Workerfor ``IfExprAST``:
285*9880d681SAndroid Build Coastguard Worker
286*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
287*9880d681SAndroid Build Coastguard Worker
288*9880d681SAndroid Build Coastguard Worker    Value *IfExprAST::codegen() {
289*9880d681SAndroid Build Coastguard Worker      Value *CondV = Cond->codegen();
290*9880d681SAndroid Build Coastguard Worker      if (!CondV)
291*9880d681SAndroid Build Coastguard Worker        return nullptr;
292*9880d681SAndroid Build Coastguard Worker
293*9880d681SAndroid Build Coastguard Worker      // Convert condition to a bool by comparing equal to 0.0.
294*9880d681SAndroid Build Coastguard Worker      CondV = Builder.CreateFCmpONE(
295*9880d681SAndroid Build Coastguard Worker          CondV, ConstantFP::get(LLVMContext, APFloat(0.0)), "ifcond");
296*9880d681SAndroid Build Coastguard Worker
297*9880d681SAndroid Build Coastguard WorkerThis code is straightforward and similar to what we saw before. We emit
298*9880d681SAndroid Build Coastguard Workerthe expression for the condition, then compare that value to zero to get
299*9880d681SAndroid Build Coastguard Workera truth value as a 1-bit (bool) value.
300*9880d681SAndroid Build Coastguard Worker
301*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
302*9880d681SAndroid Build Coastguard Worker
303*9880d681SAndroid Build Coastguard Worker      Function *TheFunction = Builder.GetInsertBlock()->getParent();
304*9880d681SAndroid Build Coastguard Worker
305*9880d681SAndroid Build Coastguard Worker      // Create blocks for the then and else cases.  Insert the 'then' block at the
306*9880d681SAndroid Build Coastguard Worker      // end of the function.
307*9880d681SAndroid Build Coastguard Worker      BasicBlock *ThenBB =
308*9880d681SAndroid Build Coastguard Worker          BasicBlock::Create(LLVMContext, "then", TheFunction);
309*9880d681SAndroid Build Coastguard Worker      BasicBlock *ElseBB = BasicBlock::Create(LLVMContext, "else");
310*9880d681SAndroid Build Coastguard Worker      BasicBlock *MergeBB = BasicBlock::Create(LLVMContext, "ifcont");
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard Worker      Builder.CreateCondBr(CondV, ThenBB, ElseBB);
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard WorkerThis code creates the basic blocks that are related to the if/then/else
315*9880d681SAndroid Build Coastguard Workerstatement, and correspond directly to the blocks in the example above.
316*9880d681SAndroid Build Coastguard WorkerThe first line gets the current Function object that is being built. It
317*9880d681SAndroid Build Coastguard Workergets this by asking the builder for the current BasicBlock, and asking
318*9880d681SAndroid Build Coastguard Workerthat block for its "parent" (the function it is currently embedded
319*9880d681SAndroid Build Coastguard Workerinto).
320*9880d681SAndroid Build Coastguard Worker
321*9880d681SAndroid Build Coastguard WorkerOnce it has that, it creates three blocks. Note that it passes
322*9880d681SAndroid Build Coastguard Worker"TheFunction" into the constructor for the "then" block. This causes the
323*9880d681SAndroid Build Coastguard Workerconstructor to automatically insert the new block into the end of the
324*9880d681SAndroid Build Coastguard Workerspecified function. The other two blocks are created, but aren't yet
325*9880d681SAndroid Build Coastguard Workerinserted into the function.
326*9880d681SAndroid Build Coastguard Worker
327*9880d681SAndroid Build Coastguard WorkerOnce the blocks are created, we can emit the conditional branch that
328*9880d681SAndroid Build Coastguard Workerchooses between them. Note that creating new blocks does not implicitly
329*9880d681SAndroid Build Coastguard Workeraffect the IRBuilder, so it is still inserting into the block that the
330*9880d681SAndroid Build Coastguard Workercondition went into. Also note that it is creating a branch to the
331*9880d681SAndroid Build Coastguard Worker"then" block and the "else" block, even though the "else" block isn't
332*9880d681SAndroid Build Coastguard Workerinserted into the function yet. This is all ok: it is the standard way
333*9880d681SAndroid Build Coastguard Workerthat LLVM supports forward references.
334*9880d681SAndroid Build Coastguard Worker
335*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker      // Emit then value.
338*9880d681SAndroid Build Coastguard Worker      Builder.SetInsertPoint(ThenBB);
339*9880d681SAndroid Build Coastguard Worker
340*9880d681SAndroid Build Coastguard Worker      Value *ThenV = Then->codegen();
341*9880d681SAndroid Build Coastguard Worker      if (!ThenV)
342*9880d681SAndroid Build Coastguard Worker        return nullptr;
343*9880d681SAndroid Build Coastguard Worker
344*9880d681SAndroid Build Coastguard Worker      Builder.CreateBr(MergeBB);
345*9880d681SAndroid Build Coastguard Worker      // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
346*9880d681SAndroid Build Coastguard Worker      ThenBB = Builder.GetInsertBlock();
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard WorkerAfter the conditional branch is inserted, we move the builder to start
349*9880d681SAndroid Build Coastguard Workerinserting into the "then" block. Strictly speaking, this call moves the
350*9880d681SAndroid Build Coastguard Workerinsertion point to be at the end of the specified block. However, since
351*9880d681SAndroid Build Coastguard Workerthe "then" block is empty, it also starts out by inserting at the
352*9880d681SAndroid Build Coastguard Workerbeginning of the block. :)
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard WorkerOnce the insertion point is set, we recursively codegen the "then"
355*9880d681SAndroid Build Coastguard Workerexpression from the AST. To finish off the "then" block, we create an
356*9880d681SAndroid Build Coastguard Workerunconditional branch to the merge block. One interesting (and very
357*9880d681SAndroid Build Coastguard Workerimportant) aspect of the LLVM IR is that it `requires all basic blocks
358*9880d681SAndroid Build Coastguard Workerto be "terminated" <../LangRef.html#functionstructure>`_ with a `control
359*9880d681SAndroid Build Coastguard Workerflow instruction <../LangRef.html#terminators>`_ such as return or
360*9880d681SAndroid Build Coastguard Workerbranch. This means that all control flow, *including fall throughs* must
361*9880d681SAndroid Build Coastguard Workerbe made explicit in the LLVM IR. If you violate this rule, the verifier
362*9880d681SAndroid Build Coastguard Workerwill emit an error.
363*9880d681SAndroid Build Coastguard Worker
364*9880d681SAndroid Build Coastguard WorkerThe final line here is quite subtle, but is very important. The basic
365*9880d681SAndroid Build Coastguard Workerissue is that when we create the Phi node in the merge block, we need to
366*9880d681SAndroid Build Coastguard Workerset up the block/value pairs that indicate how the Phi will work.
367*9880d681SAndroid Build Coastguard WorkerImportantly, the Phi node expects to have an entry for each predecessor
368*9880d681SAndroid Build Coastguard Workerof the block in the CFG. Why then, are we getting the current block when
369*9880d681SAndroid Build Coastguard Workerwe just set it to ThenBB 5 lines above? The problem is that the "Then"
370*9880d681SAndroid Build Coastguard Workerexpression may actually itself change the block that the Builder is
371*9880d681SAndroid Build Coastguard Workeremitting into if, for example, it contains a nested "if/then/else"
372*9880d681SAndroid Build Coastguard Workerexpression. Because calling ``codegen()`` recursively could arbitrarily change
373*9880d681SAndroid Build Coastguard Workerthe notion of the current block, we are required to get an up-to-date
374*9880d681SAndroid Build Coastguard Workervalue for code that will set up the Phi node.
375*9880d681SAndroid Build Coastguard Worker
376*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
377*9880d681SAndroid Build Coastguard Worker
378*9880d681SAndroid Build Coastguard Worker      // Emit else block.
379*9880d681SAndroid Build Coastguard Worker      TheFunction->getBasicBlockList().push_back(ElseBB);
380*9880d681SAndroid Build Coastguard Worker      Builder.SetInsertPoint(ElseBB);
381*9880d681SAndroid Build Coastguard Worker
382*9880d681SAndroid Build Coastguard Worker      Value *ElseV = Else->codegen();
383*9880d681SAndroid Build Coastguard Worker      if (!ElseV)
384*9880d681SAndroid Build Coastguard Worker        return nullptr;
385*9880d681SAndroid Build Coastguard Worker
386*9880d681SAndroid Build Coastguard Worker      Builder.CreateBr(MergeBB);
387*9880d681SAndroid Build Coastguard Worker      // codegen of 'Else' can change the current block, update ElseBB for the PHI.
388*9880d681SAndroid Build Coastguard Worker      ElseBB = Builder.GetInsertBlock();
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard WorkerCode generation for the 'else' block is basically identical to codegen
391*9880d681SAndroid Build Coastguard Workerfor the 'then' block. The only significant difference is the first line,
392*9880d681SAndroid Build Coastguard Workerwhich adds the 'else' block to the function. Recall previously that the
393*9880d681SAndroid Build Coastguard Worker'else' block was created, but not added to the function. Now that the
394*9880d681SAndroid Build Coastguard Worker'then' and 'else' blocks are emitted, we can finish up with the merge
395*9880d681SAndroid Build Coastguard Workercode:
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
398*9880d681SAndroid Build Coastguard Worker
399*9880d681SAndroid Build Coastguard Worker      // Emit merge block.
400*9880d681SAndroid Build Coastguard Worker      TheFunction->getBasicBlockList().push_back(MergeBB);
401*9880d681SAndroid Build Coastguard Worker      Builder.SetInsertPoint(MergeBB);
402*9880d681SAndroid Build Coastguard Worker      PHINode *PN =
403*9880d681SAndroid Build Coastguard Worker        Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, "iftmp");
404*9880d681SAndroid Build Coastguard Worker
405*9880d681SAndroid Build Coastguard Worker      PN->addIncoming(ThenV, ThenBB);
406*9880d681SAndroid Build Coastguard Worker      PN->addIncoming(ElseV, ElseBB);
407*9880d681SAndroid Build Coastguard Worker      return PN;
408*9880d681SAndroid Build Coastguard Worker    }
409*9880d681SAndroid Build Coastguard Worker
410*9880d681SAndroid Build Coastguard WorkerThe first two lines here are now familiar: the first adds the "merge"
411*9880d681SAndroid Build Coastguard Workerblock to the Function object (it was previously floating, like the else
412*9880d681SAndroid Build Coastguard Workerblock above). The second changes the insertion point so that newly
413*9880d681SAndroid Build Coastguard Workercreated code will go into the "merge" block. Once that is done, we need
414*9880d681SAndroid Build Coastguard Workerto create the PHI node and set up the block/value pairs for the PHI.
415*9880d681SAndroid Build Coastguard Worker
416*9880d681SAndroid Build Coastguard WorkerFinally, the CodeGen function returns the phi node as the value computed
417*9880d681SAndroid Build Coastguard Workerby the if/then/else expression. In our example above, this returned
418*9880d681SAndroid Build Coastguard Workervalue will feed into the code for the top-level function, which will
419*9880d681SAndroid Build Coastguard Workercreate the return instruction.
420*9880d681SAndroid Build Coastguard Worker
421*9880d681SAndroid Build Coastguard WorkerOverall, we now have the ability to execute conditional code in
422*9880d681SAndroid Build Coastguard WorkerKaleidoscope. With this extension, Kaleidoscope is a fairly complete
423*9880d681SAndroid Build Coastguard Workerlanguage that can calculate a wide variety of numeric functions. Next up
424*9880d681SAndroid Build Coastguard Workerwe'll add another useful expression that is familiar from non-functional
425*9880d681SAndroid Build Coastguard Workerlanguages...
426*9880d681SAndroid Build Coastguard Worker
427*9880d681SAndroid Build Coastguard Worker'for' Loop Expression
428*9880d681SAndroid Build Coastguard Worker=====================
429*9880d681SAndroid Build Coastguard Worker
430*9880d681SAndroid Build Coastguard WorkerNow that we know how to add basic control flow constructs to the
431*9880d681SAndroid Build Coastguard Workerlanguage, we have the tools to add more powerful things. Lets add
432*9880d681SAndroid Build Coastguard Workersomething more aggressive, a 'for' expression:
433*9880d681SAndroid Build Coastguard Worker
434*9880d681SAndroid Build Coastguard Worker::
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard Worker     extern putchard(char)
437*9880d681SAndroid Build Coastguard Worker     def printstar(n)
438*9880d681SAndroid Build Coastguard Worker       for i = 1, i < n, 1.0 in
439*9880d681SAndroid Build Coastguard Worker         putchard(42);  # ascii 42 = '*'
440*9880d681SAndroid Build Coastguard Worker
441*9880d681SAndroid Build Coastguard Worker     # print 100 '*' characters
442*9880d681SAndroid Build Coastguard Worker     printstar(100);
443*9880d681SAndroid Build Coastguard Worker
444*9880d681SAndroid Build Coastguard WorkerThis expression defines a new variable ("i" in this case) which iterates
445*9880d681SAndroid Build Coastguard Workerfrom a starting value, while the condition ("i < n" in this case) is
446*9880d681SAndroid Build Coastguard Workertrue, incrementing by an optional step value ("1.0" in this case). If
447*9880d681SAndroid Build Coastguard Workerthe step value is omitted, it defaults to 1.0. While the loop is true,
448*9880d681SAndroid Build Coastguard Workerit executes its body expression. Because we don't have anything better
449*9880d681SAndroid Build Coastguard Workerto return, we'll just define the loop as always returning 0.0. In the
450*9880d681SAndroid Build Coastguard Workerfuture when we have mutable variables, it will get more useful.
451*9880d681SAndroid Build Coastguard Worker
452*9880d681SAndroid Build Coastguard WorkerAs before, lets talk about the changes that we need to Kaleidoscope to
453*9880d681SAndroid Build Coastguard Workersupport this.
454*9880d681SAndroid Build Coastguard Worker
455*9880d681SAndroid Build Coastguard WorkerLexer Extensions for the 'for' Loop
456*9880d681SAndroid Build Coastguard Worker-----------------------------------
457*9880d681SAndroid Build Coastguard Worker
458*9880d681SAndroid Build Coastguard WorkerThe lexer extensions are the same sort of thing as for if/then/else:
459*9880d681SAndroid Build Coastguard Worker
460*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
461*9880d681SAndroid Build Coastguard Worker
462*9880d681SAndroid Build Coastguard Worker      ... in enum Token ...
463*9880d681SAndroid Build Coastguard Worker      // control
464*9880d681SAndroid Build Coastguard Worker      tok_if = -6, tok_then = -7, tok_else = -8,
465*9880d681SAndroid Build Coastguard Worker      tok_for = -9, tok_in = -10
466*9880d681SAndroid Build Coastguard Worker
467*9880d681SAndroid Build Coastguard Worker      ... in gettok ...
468*9880d681SAndroid Build Coastguard Worker      if (IdentifierStr == "def")
469*9880d681SAndroid Build Coastguard Worker        return tok_def;
470*9880d681SAndroid Build Coastguard Worker      if (IdentifierStr == "extern")
471*9880d681SAndroid Build Coastguard Worker        return tok_extern;
472*9880d681SAndroid Build Coastguard Worker      if (IdentifierStr == "if")
473*9880d681SAndroid Build Coastguard Worker        return tok_if;
474*9880d681SAndroid Build Coastguard Worker      if (IdentifierStr == "then")
475*9880d681SAndroid Build Coastguard Worker        return tok_then;
476*9880d681SAndroid Build Coastguard Worker      if (IdentifierStr == "else")
477*9880d681SAndroid Build Coastguard Worker        return tok_else;
478*9880d681SAndroid Build Coastguard Worker      if (IdentifierStr == "for")
479*9880d681SAndroid Build Coastguard Worker        return tok_for;
480*9880d681SAndroid Build Coastguard Worker      if (IdentifierStr == "in")
481*9880d681SAndroid Build Coastguard Worker        return tok_in;
482*9880d681SAndroid Build Coastguard Worker      return tok_identifier;
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard WorkerAST Extensions for the 'for' Loop
485*9880d681SAndroid Build Coastguard Worker---------------------------------
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard WorkerThe AST node is just as simple. It basically boils down to capturing the
488*9880d681SAndroid Build Coastguard Workervariable name and the constituent expressions in the node.
489*9880d681SAndroid Build Coastguard Worker
490*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
491*9880d681SAndroid Build Coastguard Worker
492*9880d681SAndroid Build Coastguard Worker    /// ForExprAST - Expression class for for/in.
493*9880d681SAndroid Build Coastguard Worker    class ForExprAST : public ExprAST {
494*9880d681SAndroid Build Coastguard Worker      std::string VarName;
495*9880d681SAndroid Build Coastguard Worker      std::unique_ptr<ExprAST> Start, End, Step, Body;
496*9880d681SAndroid Build Coastguard Worker
497*9880d681SAndroid Build Coastguard Worker    public:
498*9880d681SAndroid Build Coastguard Worker      ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
499*9880d681SAndroid Build Coastguard Worker                 std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
500*9880d681SAndroid Build Coastguard Worker                 std::unique_ptr<ExprAST> Body)
501*9880d681SAndroid Build Coastguard Worker        : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
502*9880d681SAndroid Build Coastguard Worker          Step(std::move(Step)), Body(std::move(Body)) {}
503*9880d681SAndroid Build Coastguard Worker      virtual Value *codegen();
504*9880d681SAndroid Build Coastguard Worker    };
505*9880d681SAndroid Build Coastguard Worker
506*9880d681SAndroid Build Coastguard WorkerParser Extensions for the 'for' Loop
507*9880d681SAndroid Build Coastguard Worker------------------------------------
508*9880d681SAndroid Build Coastguard Worker
509*9880d681SAndroid Build Coastguard WorkerThe parser code is also fairly standard. The only interesting thing here
510*9880d681SAndroid Build Coastguard Workeris handling of the optional step value. The parser code handles it by
511*9880d681SAndroid Build Coastguard Workerchecking to see if the second comma is present. If not, it sets the step
512*9880d681SAndroid Build Coastguard Workervalue to null in the AST node:
513*9880d681SAndroid Build Coastguard Worker
514*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
515*9880d681SAndroid Build Coastguard Worker
516*9880d681SAndroid Build Coastguard Worker    /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
517*9880d681SAndroid Build Coastguard Worker    static std::unique_ptr<ExprAST> ParseForExpr() {
518*9880d681SAndroid Build Coastguard Worker      getNextToken();  // eat the for.
519*9880d681SAndroid Build Coastguard Worker
520*9880d681SAndroid Build Coastguard Worker      if (CurTok != tok_identifier)
521*9880d681SAndroid Build Coastguard Worker        return LogError("expected identifier after for");
522*9880d681SAndroid Build Coastguard Worker
523*9880d681SAndroid Build Coastguard Worker      std::string IdName = IdentifierStr;
524*9880d681SAndroid Build Coastguard Worker      getNextToken();  // eat identifier.
525*9880d681SAndroid Build Coastguard Worker
526*9880d681SAndroid Build Coastguard Worker      if (CurTok != '=')
527*9880d681SAndroid Build Coastguard Worker        return LogError("expected '=' after for");
528*9880d681SAndroid Build Coastguard Worker      getNextToken();  // eat '='.
529*9880d681SAndroid Build Coastguard Worker
530*9880d681SAndroid Build Coastguard Worker
531*9880d681SAndroid Build Coastguard Worker      auto Start = ParseExpression();
532*9880d681SAndroid Build Coastguard Worker      if (!Start)
533*9880d681SAndroid Build Coastguard Worker        return nullptr;
534*9880d681SAndroid Build Coastguard Worker      if (CurTok != ',')
535*9880d681SAndroid Build Coastguard Worker        return LogError("expected ',' after for start value");
536*9880d681SAndroid Build Coastguard Worker      getNextToken();
537*9880d681SAndroid Build Coastguard Worker
538*9880d681SAndroid Build Coastguard Worker      auto End = ParseExpression();
539*9880d681SAndroid Build Coastguard Worker      if (!End)
540*9880d681SAndroid Build Coastguard Worker        return nullptr;
541*9880d681SAndroid Build Coastguard Worker
542*9880d681SAndroid Build Coastguard Worker      // The step value is optional.
543*9880d681SAndroid Build Coastguard Worker      std::unique_ptr<ExprAST> Step;
544*9880d681SAndroid Build Coastguard Worker      if (CurTok == ',') {
545*9880d681SAndroid Build Coastguard Worker        getNextToken();
546*9880d681SAndroid Build Coastguard Worker        Step = ParseExpression();
547*9880d681SAndroid Build Coastguard Worker        if (!Step)
548*9880d681SAndroid Build Coastguard Worker          return nullptr;
549*9880d681SAndroid Build Coastguard Worker      }
550*9880d681SAndroid Build Coastguard Worker
551*9880d681SAndroid Build Coastguard Worker      if (CurTok != tok_in)
552*9880d681SAndroid Build Coastguard Worker        return LogError("expected 'in' after for");
553*9880d681SAndroid Build Coastguard Worker      getNextToken();  // eat 'in'.
554*9880d681SAndroid Build Coastguard Worker
555*9880d681SAndroid Build Coastguard Worker      auto Body = ParseExpression();
556*9880d681SAndroid Build Coastguard Worker      if (!Body)
557*9880d681SAndroid Build Coastguard Worker        return nullptr;
558*9880d681SAndroid Build Coastguard Worker
559*9880d681SAndroid Build Coastguard Worker      return llvm::make_unique<ForExprAST>(IdName, std::move(Start),
560*9880d681SAndroid Build Coastguard Worker                                           std::move(End), std::move(Step),
561*9880d681SAndroid Build Coastguard Worker                                           std::move(Body));
562*9880d681SAndroid Build Coastguard Worker    }
563*9880d681SAndroid Build Coastguard Worker
564*9880d681SAndroid Build Coastguard WorkerLLVM IR for the 'for' Loop
565*9880d681SAndroid Build Coastguard Worker--------------------------
566*9880d681SAndroid Build Coastguard Worker
567*9880d681SAndroid Build Coastguard WorkerNow we get to the good part: the LLVM IR we want to generate for this
568*9880d681SAndroid Build Coastguard Workerthing. With the simple example above, we get this LLVM IR (note that
569*9880d681SAndroid Build Coastguard Workerthis dump is generated with optimizations disabled for clarity):
570*9880d681SAndroid Build Coastguard Worker
571*9880d681SAndroid Build Coastguard Worker.. code-block:: llvm
572*9880d681SAndroid Build Coastguard Worker
573*9880d681SAndroid Build Coastguard Worker    declare double @putchard(double)
574*9880d681SAndroid Build Coastguard Worker
575*9880d681SAndroid Build Coastguard Worker    define double @printstar(double %n) {
576*9880d681SAndroid Build Coastguard Worker    entry:
577*9880d681SAndroid Build Coastguard Worker      ; initial value = 1.0 (inlined into phi)
578*9880d681SAndroid Build Coastguard Worker      br label %loop
579*9880d681SAndroid Build Coastguard Worker
580*9880d681SAndroid Build Coastguard Worker    loop:       ; preds = %loop, %entry
581*9880d681SAndroid Build Coastguard Worker      %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ]
582*9880d681SAndroid Build Coastguard Worker      ; body
583*9880d681SAndroid Build Coastguard Worker      %calltmp = call double @putchard(double 4.200000e+01)
584*9880d681SAndroid Build Coastguard Worker      ; increment
585*9880d681SAndroid Build Coastguard Worker      %nextvar = fadd double %i, 1.000000e+00
586*9880d681SAndroid Build Coastguard Worker
587*9880d681SAndroid Build Coastguard Worker      ; termination test
588*9880d681SAndroid Build Coastguard Worker      %cmptmp = fcmp ult double %i, %n
589*9880d681SAndroid Build Coastguard Worker      %booltmp = uitofp i1 %cmptmp to double
590*9880d681SAndroid Build Coastguard Worker      %loopcond = fcmp one double %booltmp, 0.000000e+00
591*9880d681SAndroid Build Coastguard Worker      br i1 %loopcond, label %loop, label %afterloop
592*9880d681SAndroid Build Coastguard Worker
593*9880d681SAndroid Build Coastguard Worker    afterloop:      ; preds = %loop
594*9880d681SAndroid Build Coastguard Worker      ; loop always returns 0.0
595*9880d681SAndroid Build Coastguard Worker      ret double 0.000000e+00
596*9880d681SAndroid Build Coastguard Worker    }
597*9880d681SAndroid Build Coastguard Worker
598*9880d681SAndroid Build Coastguard WorkerThis loop contains all the same constructs we saw before: a phi node,
599*9880d681SAndroid Build Coastguard Workerseveral expressions, and some basic blocks. Lets see how this fits
600*9880d681SAndroid Build Coastguard Workertogether.
601*9880d681SAndroid Build Coastguard Worker
602*9880d681SAndroid Build Coastguard WorkerCode Generation for the 'for' Loop
603*9880d681SAndroid Build Coastguard Worker----------------------------------
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard WorkerThe first part of codegen is very simple: we just output the start
606*9880d681SAndroid Build Coastguard Workerexpression for the loop value:
607*9880d681SAndroid Build Coastguard Worker
608*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
609*9880d681SAndroid Build Coastguard Worker
610*9880d681SAndroid Build Coastguard Worker    Value *ForExprAST::codegen() {
611*9880d681SAndroid Build Coastguard Worker      // Emit the start code first, without 'variable' in scope.
612*9880d681SAndroid Build Coastguard Worker      Value *StartVal = Start->codegen();
613*9880d681SAndroid Build Coastguard Worker      if (StartVal == 0) return 0;
614*9880d681SAndroid Build Coastguard Worker
615*9880d681SAndroid Build Coastguard WorkerWith this out of the way, the next step is to set up the LLVM basic
616*9880d681SAndroid Build Coastguard Workerblock for the start of the loop body. In the case above, the whole loop
617*9880d681SAndroid Build Coastguard Workerbody is one block, but remember that the body code itself could consist
618*9880d681SAndroid Build Coastguard Workerof multiple blocks (e.g. if it contains an if/then/else or a for/in
619*9880d681SAndroid Build Coastguard Workerexpression).
620*9880d681SAndroid Build Coastguard Worker
621*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
622*9880d681SAndroid Build Coastguard Worker
623*9880d681SAndroid Build Coastguard Worker      // Make the new basic block for the loop header, inserting after current
624*9880d681SAndroid Build Coastguard Worker      // block.
625*9880d681SAndroid Build Coastguard Worker      Function *TheFunction = Builder.GetInsertBlock()->getParent();
626*9880d681SAndroid Build Coastguard Worker      BasicBlock *PreheaderBB = Builder.GetInsertBlock();
627*9880d681SAndroid Build Coastguard Worker      BasicBlock *LoopBB =
628*9880d681SAndroid Build Coastguard Worker          BasicBlock::Create(LLVMContext, "loop", TheFunction);
629*9880d681SAndroid Build Coastguard Worker
630*9880d681SAndroid Build Coastguard Worker      // Insert an explicit fall through from the current block to the LoopBB.
631*9880d681SAndroid Build Coastguard Worker      Builder.CreateBr(LoopBB);
632*9880d681SAndroid Build Coastguard Worker
633*9880d681SAndroid Build Coastguard WorkerThis code is similar to what we saw for if/then/else. Because we will
634*9880d681SAndroid Build Coastguard Workerneed it to create the Phi node, we remember the block that falls through
635*9880d681SAndroid Build Coastguard Workerinto the loop. Once we have that, we create the actual block that starts
636*9880d681SAndroid Build Coastguard Workerthe loop and create an unconditional branch for the fall-through between
637*9880d681SAndroid Build Coastguard Workerthe two blocks.
638*9880d681SAndroid Build Coastguard Worker
639*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
640*9880d681SAndroid Build Coastguard Worker
641*9880d681SAndroid Build Coastguard Worker      // Start insertion in LoopBB.
642*9880d681SAndroid Build Coastguard Worker      Builder.SetInsertPoint(LoopBB);
643*9880d681SAndroid Build Coastguard Worker
644*9880d681SAndroid Build Coastguard Worker      // Start the PHI node with an entry for Start.
645*9880d681SAndroid Build Coastguard Worker      PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(LLVMContext),
646*9880d681SAndroid Build Coastguard Worker                                            2, VarName.c_str());
647*9880d681SAndroid Build Coastguard Worker      Variable->addIncoming(StartVal, PreheaderBB);
648*9880d681SAndroid Build Coastguard Worker
649*9880d681SAndroid Build Coastguard WorkerNow that the "preheader" for the loop is set up, we switch to emitting
650*9880d681SAndroid Build Coastguard Workercode for the loop body. To begin with, we move the insertion point and
651*9880d681SAndroid Build Coastguard Workercreate the PHI node for the loop induction variable. Since we already
652*9880d681SAndroid Build Coastguard Workerknow the incoming value for the starting value, we add it to the Phi
653*9880d681SAndroid Build Coastguard Workernode. Note that the Phi will eventually get a second value for the
654*9880d681SAndroid Build Coastguard Workerbackedge, but we can't set it up yet (because it doesn't exist!).
655*9880d681SAndroid Build Coastguard Worker
656*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
657*9880d681SAndroid Build Coastguard Worker
658*9880d681SAndroid Build Coastguard Worker      // Within the loop, the variable is defined equal to the PHI node.  If it
659*9880d681SAndroid Build Coastguard Worker      // shadows an existing variable, we have to restore it, so save it now.
660*9880d681SAndroid Build Coastguard Worker      Value *OldVal = NamedValues[VarName];
661*9880d681SAndroid Build Coastguard Worker      NamedValues[VarName] = Variable;
662*9880d681SAndroid Build Coastguard Worker
663*9880d681SAndroid Build Coastguard Worker      // Emit the body of the loop.  This, like any other expr, can change the
664*9880d681SAndroid Build Coastguard Worker      // current BB.  Note that we ignore the value computed by the body, but don't
665*9880d681SAndroid Build Coastguard Worker      // allow an error.
666*9880d681SAndroid Build Coastguard Worker      if (!Body->codegen())
667*9880d681SAndroid Build Coastguard Worker        return nullptr;
668*9880d681SAndroid Build Coastguard Worker
669*9880d681SAndroid Build Coastguard WorkerNow the code starts to get more interesting. Our 'for' loop introduces a
670*9880d681SAndroid Build Coastguard Workernew variable to the symbol table. This means that our symbol table can
671*9880d681SAndroid Build Coastguard Workernow contain either function arguments or loop variables. To handle this,
672*9880d681SAndroid Build Coastguard Workerbefore we codegen the body of the loop, we add the loop variable as the
673*9880d681SAndroid Build Coastguard Workercurrent value for its name. Note that it is possible that there is a
674*9880d681SAndroid Build Coastguard Workervariable of the same name in the outer scope. It would be easy to make
675*9880d681SAndroid Build Coastguard Workerthis an error (emit an error and return null if there is already an
676*9880d681SAndroid Build Coastguard Workerentry for VarName) but we choose to allow shadowing of variables. In
677*9880d681SAndroid Build Coastguard Workerorder to handle this correctly, we remember the Value that we are
678*9880d681SAndroid Build Coastguard Workerpotentially shadowing in ``OldVal`` (which will be null if there is no
679*9880d681SAndroid Build Coastguard Workershadowed variable).
680*9880d681SAndroid Build Coastguard Worker
681*9880d681SAndroid Build Coastguard WorkerOnce the loop variable is set into the symbol table, the code
682*9880d681SAndroid Build Coastguard Workerrecursively codegen's the body. This allows the body to use the loop
683*9880d681SAndroid Build Coastguard Workervariable: any references to it will naturally find it in the symbol
684*9880d681SAndroid Build Coastguard Workertable.
685*9880d681SAndroid Build Coastguard Worker
686*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
687*9880d681SAndroid Build Coastguard Worker
688*9880d681SAndroid Build Coastguard Worker      // Emit the step value.
689*9880d681SAndroid Build Coastguard Worker      Value *StepVal = nullptr;
690*9880d681SAndroid Build Coastguard Worker      if (Step) {
691*9880d681SAndroid Build Coastguard Worker        StepVal = Step->codegen();
692*9880d681SAndroid Build Coastguard Worker        if (!StepVal)
693*9880d681SAndroid Build Coastguard Worker          return nullptr;
694*9880d681SAndroid Build Coastguard Worker      } else {
695*9880d681SAndroid Build Coastguard Worker        // If not specified, use 1.0.
696*9880d681SAndroid Build Coastguard Worker        StepVal = ConstantFP::get(LLVMContext, APFloat(1.0));
697*9880d681SAndroid Build Coastguard Worker      }
698*9880d681SAndroid Build Coastguard Worker
699*9880d681SAndroid Build Coastguard Worker      Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
700*9880d681SAndroid Build Coastguard Worker
701*9880d681SAndroid Build Coastguard WorkerNow that the body is emitted, we compute the next value of the iteration
702*9880d681SAndroid Build Coastguard Workervariable by adding the step value, or 1.0 if it isn't present.
703*9880d681SAndroid Build Coastguard Worker'``NextVar``' will be the value of the loop variable on the next
704*9880d681SAndroid Build Coastguard Workeriteration of the loop.
705*9880d681SAndroid Build Coastguard Worker
706*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
707*9880d681SAndroid Build Coastguard Worker
708*9880d681SAndroid Build Coastguard Worker      // Compute the end condition.
709*9880d681SAndroid Build Coastguard Worker      Value *EndCond = End->codegen();
710*9880d681SAndroid Build Coastguard Worker      if (!EndCond)
711*9880d681SAndroid Build Coastguard Worker        return nullptr;
712*9880d681SAndroid Build Coastguard Worker
713*9880d681SAndroid Build Coastguard Worker      // Convert condition to a bool by comparing equal to 0.0.
714*9880d681SAndroid Build Coastguard Worker      EndCond = Builder.CreateFCmpONE(
715*9880d681SAndroid Build Coastguard Worker          EndCond, ConstantFP::get(LLVMContext, APFloat(0.0)), "loopcond");
716*9880d681SAndroid Build Coastguard Worker
717*9880d681SAndroid Build Coastguard WorkerFinally, we evaluate the exit value of the loop, to determine whether
718*9880d681SAndroid Build Coastguard Workerthe loop should exit. This mirrors the condition evaluation for the
719*9880d681SAndroid Build Coastguard Workerif/then/else statement.
720*9880d681SAndroid Build Coastguard Worker
721*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
722*9880d681SAndroid Build Coastguard Worker
723*9880d681SAndroid Build Coastguard Worker      // Create the "after loop" block and insert it.
724*9880d681SAndroid Build Coastguard Worker      BasicBlock *LoopEndBB = Builder.GetInsertBlock();
725*9880d681SAndroid Build Coastguard Worker      BasicBlock *AfterBB =
726*9880d681SAndroid Build Coastguard Worker          BasicBlock::Create(LLVMContext, "afterloop", TheFunction);
727*9880d681SAndroid Build Coastguard Worker
728*9880d681SAndroid Build Coastguard Worker      // Insert the conditional branch into the end of LoopEndBB.
729*9880d681SAndroid Build Coastguard Worker      Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
730*9880d681SAndroid Build Coastguard Worker
731*9880d681SAndroid Build Coastguard Worker      // Any new code will be inserted in AfterBB.
732*9880d681SAndroid Build Coastguard Worker      Builder.SetInsertPoint(AfterBB);
733*9880d681SAndroid Build Coastguard Worker
734*9880d681SAndroid Build Coastguard WorkerWith the code for the body of the loop complete, we just need to finish
735*9880d681SAndroid Build Coastguard Workerup the control flow for it. This code remembers the end block (for the
736*9880d681SAndroid Build Coastguard Workerphi node), then creates the block for the loop exit ("afterloop"). Based
737*9880d681SAndroid Build Coastguard Workeron the value of the exit condition, it creates a conditional branch that
738*9880d681SAndroid Build Coastguard Workerchooses between executing the loop again and exiting the loop. Any
739*9880d681SAndroid Build Coastguard Workerfuture code is emitted in the "afterloop" block, so it sets the
740*9880d681SAndroid Build Coastguard Workerinsertion position to it.
741*9880d681SAndroid Build Coastguard Worker
742*9880d681SAndroid Build Coastguard Worker.. code-block:: c++
743*9880d681SAndroid Build Coastguard Worker
744*9880d681SAndroid Build Coastguard Worker      // Add a new entry to the PHI node for the backedge.
745*9880d681SAndroid Build Coastguard Worker      Variable->addIncoming(NextVar, LoopEndBB);
746*9880d681SAndroid Build Coastguard Worker
747*9880d681SAndroid Build Coastguard Worker      // Restore the unshadowed variable.
748*9880d681SAndroid Build Coastguard Worker      if (OldVal)
749*9880d681SAndroid Build Coastguard Worker        NamedValues[VarName] = OldVal;
750*9880d681SAndroid Build Coastguard Worker      else
751*9880d681SAndroid Build Coastguard Worker        NamedValues.erase(VarName);
752*9880d681SAndroid Build Coastguard Worker
753*9880d681SAndroid Build Coastguard Worker      // for expr always returns 0.0.
754*9880d681SAndroid Build Coastguard Worker      return Constant::getNullValue(Type::getDoubleTy(LLVMContext));
755*9880d681SAndroid Build Coastguard Worker    }
756*9880d681SAndroid Build Coastguard Worker
757*9880d681SAndroid Build Coastguard WorkerThe final code handles various cleanups: now that we have the "NextVar"
758*9880d681SAndroid Build Coastguard Workervalue, we can add the incoming value to the loop PHI node. After that,
759*9880d681SAndroid Build Coastguard Workerwe remove the loop variable from the symbol table, so that it isn't in
760*9880d681SAndroid Build Coastguard Workerscope after the for loop. Finally, code generation of the for loop
761*9880d681SAndroid Build Coastguard Workeralways returns 0.0, so that is what we return from
762*9880d681SAndroid Build Coastguard Worker``ForExprAST::codegen()``.
763*9880d681SAndroid Build Coastguard Worker
764*9880d681SAndroid Build Coastguard WorkerWith this, we conclude the "adding control flow to Kaleidoscope" chapter
765*9880d681SAndroid Build Coastguard Workerof the tutorial. In this chapter we added two control flow constructs,
766*9880d681SAndroid Build Coastguard Workerand used them to motivate a couple of aspects of the LLVM IR that are
767*9880d681SAndroid Build Coastguard Workerimportant for front-end implementors to know. In the next chapter of our
768*9880d681SAndroid Build Coastguard Workersaga, we will get a bit crazier and add `user-defined
769*9880d681SAndroid Build Coastguard Workeroperators <LangImpl6.html>`_ to our poor innocent language.
770*9880d681SAndroid Build Coastguard Worker
771*9880d681SAndroid Build Coastguard WorkerFull Code Listing
772*9880d681SAndroid Build Coastguard Worker=================
773*9880d681SAndroid Build Coastguard Worker
774*9880d681SAndroid Build Coastguard WorkerHere is the complete code listing for our running example, enhanced with
775*9880d681SAndroid Build Coastguard Workerthe if/then/else and for expressions.. To build this example, use:
776*9880d681SAndroid Build Coastguard Worker
777*9880d681SAndroid Build Coastguard Worker.. code-block:: bash
778*9880d681SAndroid Build Coastguard Worker
779*9880d681SAndroid Build Coastguard Worker    # Compile
780*9880d681SAndroid Build Coastguard Worker    clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core mcjit native` -O3 -o toy
781*9880d681SAndroid Build Coastguard Worker    # Run
782*9880d681SAndroid Build Coastguard Worker    ./toy
783*9880d681SAndroid Build Coastguard Worker
784*9880d681SAndroid Build Coastguard WorkerHere is the code:
785*9880d681SAndroid Build Coastguard Worker
786*9880d681SAndroid Build Coastguard Worker.. literalinclude:: ../../examples/Kaleidoscope/Chapter5/toy.cpp
787*9880d681SAndroid Build Coastguard Worker   :language: c++
788*9880d681SAndroid Build Coastguard Worker
789*9880d681SAndroid Build Coastguard Worker`Next: Extending the language: user-defined operators <LangImpl06.html>`_
790*9880d681SAndroid Build Coastguard Worker
791