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