brintos

brintos / llvm-project-archived public Read only

0
0
Text · 28.3 KiB · 0039547 Raw
815 lines · plain
1==================================================2Kaleidoscope: Extending the Language: Control Flow3==================================================4 5.. contents::6   :local:7 8Chapter 5 Introduction9======================10 11Welcome to Chapter 5 of the "`Implementing a language with12LLVM <index.html>`_" tutorial. Parts 1-4 described the implementation of13the simple Kaleidoscope language and included support for generating14LLVM IR, followed by optimizations and a JIT compiler. Unfortunately, as15presented, Kaleidoscope is mostly useless: it has no control flow other16than call and return. This means that you can't have conditional17branches in the code, significantly limiting its power. In this episode18of "build that compiler", we'll extend Kaleidoscope to have an19if/then/else expression plus a simple 'for' loop.20 21If/Then/Else22============23 24Extending Kaleidoscope to support if/then/else is quite straightforward.25It basically requires adding support for this "new" concept to the26lexer, parser, AST, and LLVM code emitter. This example is nice, because27it shows how easy it is to "grow" a language over time, incrementally28extending it as new ideas are discovered.29 30Before we get going on "how" we add this extension, let's talk about31"what" we want. The basic idea is that we want to be able to write this32sort of thing:33 34::35 36    def fib(x)37      if x < 3 then38        139      else40        fib(x-1)+fib(x-2);41 42In Kaleidoscope, every construct is an expression: there are no43statements. As such, the if/then/else expression needs to return a value44like any other. Since we're using a mostly functional form, we'll have45it evaluate its conditional, then return the 'then' or 'else' value46based on how the condition was resolved. This is very similar to the C47"?:" expression.48 49The semantics of the if/then/else expression is that it evaluates the50condition to a boolean equality value: 0.0 is considered to be false and51everything else is considered to be true. If the condition is true, the52first subexpression is evaluated and returned, if the condition is53false, the second subexpression is evaluated and returned. Since54Kaleidoscope allows side-effects, this behavior is important to nail55down.56 57Now that we know what we "want", let's break this down into its58constituent pieces.59 60Lexer Extensions for If/Then/Else61---------------------------------62 63The lexer extensions are straightforward. First we add new enum values64for the relevant tokens:65 66.. code-block:: c++67 68      // control69      tok_if = -6,70      tok_then = -7,71      tok_else = -8,72 73Once we have that, we recognize the new keywords in the lexer. This is74pretty simple stuff:75 76.. code-block:: c++77 78        ...79        if (IdentifierStr == "def")80          return tok_def;81        if (IdentifierStr == "extern")82          return tok_extern;83        if (IdentifierStr == "if")84          return tok_if;85        if (IdentifierStr == "then")86          return tok_then;87        if (IdentifierStr == "else")88          return tok_else;89        return tok_identifier;90 91AST Extensions for If/Then/Else92-------------------------------93 94To represent the new expression we add a new AST node for it:95 96.. code-block:: c++97 98    /// IfExprAST - Expression class for if/then/else.99    class IfExprAST : public ExprAST {100      std::unique_ptr<ExprAST> Cond, Then, Else;101 102    public:103      IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,104                std::unique_ptr<ExprAST> Else)105        : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}106 107      Value *codegen() override;108    };109 110The AST node just has pointers to the various subexpressions.111 112Parser Extensions for If/Then/Else113----------------------------------114 115Now that we have the relevant tokens coming from the lexer and we have116the AST node to build, our parsing logic is relatively straightforward.117First we define a new parsing function:118 119.. code-block:: c++120 121    /// ifexpr ::= 'if' expression 'then' expression 'else' expression122    static std::unique_ptr<ExprAST> ParseIfExpr() {123      getNextToken();  // eat the if.124 125      // condition.126      auto Cond = ParseExpression();127      if (!Cond)128        return nullptr;129 130      if (CurTok != tok_then)131        return LogError("expected then");132      getNextToken();  // eat the then133 134      auto Then = ParseExpression();135      if (!Then)136        return nullptr;137 138      if (CurTok != tok_else)139        return LogError("expected else");140 141      getNextToken();142 143      auto Else = ParseExpression();144      if (!Else)145        return nullptr;146 147      return std::make_unique<IfExprAST>(std::move(Cond), std::move(Then),148                                          std::move(Else));149    }150 151Next we hook it up as a primary expression:152 153.. code-block:: c++154 155    static std::unique_ptr<ExprAST> ParsePrimary() {156      switch (CurTok) {157      default:158        return LogError("unknown token when expecting an expression");159      case tok_identifier:160        return ParseIdentifierExpr();161      case tok_number:162        return ParseNumberExpr();163      case '(':164        return ParseParenExpr();165      case tok_if:166        return ParseIfExpr();167      }168    }169 170LLVM IR for If/Then/Else171------------------------172 173Now that we have it parsing and building the AST, the final piece is174adding LLVM code generation support. This is the most interesting part175of the if/then/else example, because this is where it starts to176introduce new concepts. All of the code above has been thoroughly177described in previous chapters.178 179To motivate the code we want to produce, let's take a look at a simple180example. Consider:181 182::183 184    extern foo();185    extern bar();186    def baz(x) if x then foo() else bar();187 188If you disable optimizations, the code you'll (soon) get from189Kaleidoscope looks like this:190 191.. code-block:: llvm192 193    declare double @foo()194 195    declare double @bar()196 197    define double @baz(double %x) {198    entry:199      %ifcond = fcmp one double %x, 0.000000e+00200      br i1 %ifcond, label %then, label %else201 202    then:       ; preds = %entry203      %calltmp = call double @foo()204      br label %ifcont205 206    else:       ; preds = %entry207      %calltmp1 = call double @bar()208      br label %ifcont209 210    ifcont:     ; preds = %else, %then211      %iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ]212      ret double %iftmp213    }214 215To visualize the control flow graph, you can use a nifty feature of the216LLVM '`opt <https://llvm.org/cmds/opt.html>`_' tool. If you put this LLVM217IR into "t.ll" and run "``llvm-as < t.ll | opt -passes=view-cfg``", `a218window will pop up <../../ProgrammersManual.html#viewing-graphs-while-debugging-code>`_ and you'll219see this graph:220 221.. figure:: LangImpl05-cfg.png222   :align: center223   :alt: Example CFG224 225   Example CFG226 227Another way to get this is to call "``F->viewCFG()``" or228"``F->viewCFGOnly()``" (where F is a "``Function*``") either by229inserting actual calls into the code and recompiling or by calling these230in the debugger. LLVM has many nice features for visualizing various231graphs.232 233Getting back to the generated code, it is fairly simple: the entry block234evaluates the conditional expression ("x" in our case here) and compares235the result to 0.0 with the "``fcmp one``" instruction ('one' is "Ordered236and Not Equal"). Based on the result of this expression, the code jumps237to either the "then" or "else" blocks, which contain the expressions for238the true/false cases.239 240Once the then/else blocks are finished executing, they both branch back241to the 'ifcont' block to execute the code that happens after the242if/then/else. In this case the only thing left to do is to return to the243caller of the function. The question then becomes: how does the code244know which expression to return?245 246The answer to this question involves an important SSA operation: the247`Phi248operation <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_.249If you're not familiar with SSA, `the wikipedia250article <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_251is a good introduction and there are various other introductions to it252available on your favorite search engine. The short version is that253"execution" of the Phi operation requires "remembering" which block254control came from. The Phi operation takes on the value corresponding to255the input control block. In this case, if control comes in from the256"then" block, it gets the value of "calltmp". If control comes from the257"else" block, it gets the value of "calltmp1".258 259At this point, you are probably starting to think "Oh no! This means my260simple and elegant front-end will have to start generating SSA form in261order to use LLVM!". Fortunately, this is not the case, and we strongly262advise *not* implementing an SSA construction algorithm in your263front-end unless there is an amazingly good reason to do so. In264practice, there are two sorts of values that float around in code265written for your average imperative programming language that might need266Phi nodes:267 268#. Code that involves user variables: ``x = 1; x = x + 1;``269#. Values that are implicit in the structure of your AST, such as the270   Phi node in this case.271 272In `Chapter 7 <LangImpl07.html>`_ of this tutorial ("mutable variables"),273we'll talk about #1 in depth. For now, just believe me that you don't274need SSA construction to handle this case. For #2, you have the choice275of using the techniques that we will describe for #1, or you can insert276Phi nodes directly, if convenient. In this case, it is really277easy to generate the Phi node, so we choose to do it directly.278 279Okay, enough of the motivation and overview, let's generate code!280 281Code Generation for If/Then/Else282--------------------------------283 284In order to generate code for this, we implement the ``codegen`` method285for ``IfExprAST``:286 287.. code-block:: c++288 289    Value *IfExprAST::codegen() {290      Value *CondV = Cond->codegen();291      if (!CondV)292        return nullptr;293 294      // Convert condition to a bool by comparing non-equal to 0.0.295      CondV = Builder->CreateFCmpONE(296          CondV, ConstantFP::get(*TheContext, APFloat(0.0)), "ifcond");297 298This code is straightforward and similar to what we saw before. We emit299the expression for the condition, then compare that value to zero to get300a truth value as a 1-bit (bool) value.301 302.. code-block:: c++303 304      Function *TheFunction = Builder->GetInsertBlock()->getParent();305 306      // Create blocks for the then and else cases.  Insert the 'then' block at the307      // end of the function.308      BasicBlock *ThenBB =309          BasicBlock::Create(*TheContext, "then", TheFunction);310      BasicBlock *ElseBB = BasicBlock::Create(*TheContext, "else");311      BasicBlock *MergeBB = BasicBlock::Create(*TheContext, "ifcont");312 313      Builder->CreateCondBr(CondV, ThenBB, ElseBB);314 315This code creates the basic blocks that are related to the if/then/else316statement, and correspond directly to the blocks in the example above.317The first line gets the current Function object that is being built. It318gets this by asking the builder for the current BasicBlock, and asking319that block for its "parent" (the function it is currently embedded320into).321 322Once it has that, it creates three blocks. Note that it passes323"TheFunction" into the constructor for the "then" block. This causes the324constructor to automatically insert the new block into the end of the325specified function. The other two blocks are created, but aren't yet326inserted into the function.327 328Once the blocks are created, we can emit the conditional branch that329chooses between them. Note that creating new blocks does not implicitly330affect the IRBuilder, so it is still inserting into the block that the331condition went into. Also note that it is creating a branch to the332"then" block and the "else" block, even though the "else" block isn't333inserted into the function yet. This is all ok: it is the standard way334that LLVM supports forward references.335 336.. code-block:: c++337 338      // Emit then value.339      Builder->SetInsertPoint(ThenBB);340 341      Value *ThenV = Then->codegen();342      if (!ThenV)343        return nullptr;344 345      Builder->CreateBr(MergeBB);346      // Codegen of 'Then' can change the current block, update ThenBB for the PHI.347      ThenBB = Builder->GetInsertBlock();348 349After the conditional branch is inserted, we move the builder to start350inserting into the "then" block. Strictly speaking, this call moves the351insertion point to be at the end of the specified block. However, since352the "then" block is empty, it also starts out by inserting at the353beginning of the block. :)354 355Once the insertion point is set, we recursively codegen the "then"356expression from the AST. To finish off the "then" block, we create an357unconditional branch to the merge block. One interesting (and very358important) aspect of the LLVM IR is that it :ref:`requires all basic359blocks to be "terminated" <functionstructure>` with a :ref:`control360flow instruction <terminators>`  such as return or branch. This means361that all control flow, *including fall throughs* must be made explicit362in the LLVM IR. If you violate this rule, the verifier will emit an363error.364 365The final line here is quite subtle, but is very important. The basic366issue is that when we create the Phi node in the merge block, we need to367set up the block/value pairs that indicate how the Phi will work.368Importantly, the Phi node expects to have an entry for each predecessor369of the block in the CFG. Why then, are we getting the current block when370we just set it to ThenBB 5 lines above? The problem is that the "Then"371expression may actually itself change the block that the Builder is372emitting into if, for example, it contains a nested "if/then/else"373expression. Because calling ``codegen()`` recursively could arbitrarily change374the notion of the current block, we are required to get an up-to-date375value for code that will set up the Phi node.376 377.. code-block:: c++378 379      // Emit else block.380      TheFunction->insert(TheFunction->end(), ElseBB);381      Builder->SetInsertPoint(ElseBB);382 383      Value *ElseV = Else->codegen();384      if (!ElseV)385        return nullptr;386 387      Builder->CreateBr(MergeBB);388      // codegen of 'Else' can change the current block, update ElseBB for the PHI.389      ElseBB = Builder->GetInsertBlock();390 391Code generation for the 'else' block is basically identical to codegen392for the 'then' block. The only significant difference is the first line,393which adds the 'else' block to the function. Recall previously that the394'else' block was created, but not added to the function. Now that the395'then' and 'else' blocks are emitted, we can finish up with the merge396code:397 398.. code-block:: c++399 400      // Emit merge block.401      TheFunction->insert(TheFunction->end(), MergeBB);402      Builder->SetInsertPoint(MergeBB);403      PHINode *PN =404        Builder->CreatePHI(Type::getDoubleTy(*TheContext), 2, "iftmp");405 406      PN->addIncoming(ThenV, ThenBB);407      PN->addIncoming(ElseV, ElseBB);408      return PN;409    }410 411The first two lines here are now familiar: the first adds the "merge"412block to the Function object (it was previously floating, like the else413block above). The second changes the insertion point so that newly414created code will go into the "merge" block. Once that is done, we need415to create the PHI node and set up the block/value pairs for the PHI.416 417Finally, the CodeGen function returns the phi node as the value computed418by the if/then/else expression. In our example above, this returned419value will feed into the code for the top-level function, which will420create the return instruction.421 422Overall, we now have the ability to execute conditional code in423Kaleidoscope. With this extension, Kaleidoscope is a fairly complete424language that can calculate a wide variety of numeric functions. Next up425we'll add another useful expression that is familiar from non-functional426languages...427 428'for' Loop Expression429=====================430 431Now that we know how to add basic control flow constructs to the432language, we have the tools to add more powerful things. Let's add433something more aggressive, a 'for' expression:434 435::436 437     extern putchard(char);438     def printstar(n)439       for i = 1, i < n, 1.0 in440         putchard(42);  # ascii 42 = '*'441 442     # print 100 '*' characters443     printstar(100);444 445This expression defines a new variable ("i" in this case) which iterates446from a starting value, while the condition ("i < n" in this case) is447true, incrementing by an optional step value ("1.0" in this case). If448the step value is omitted, it defaults to 1.0. While the loop is true,449it executes its body expression. Because we don't have anything better450to return, we'll just define the loop as always returning 0.0. In the451future when we have mutable variables, it will get more useful.452 453As before, let's talk about the changes that we need to Kaleidoscope to454support this.455 456Lexer Extensions for the 'for' Loop457-----------------------------------458 459The lexer extensions are the same sort of thing as for if/then/else:460 461.. code-block:: c++462 463      ... in enum Token ...464      // control465      tok_if = -6, tok_then = -7, tok_else = -8,466      tok_for = -9, tok_in = -10467 468      ... in gettok ...469      if (IdentifierStr == "def")470        return tok_def;471      if (IdentifierStr == "extern")472        return tok_extern;473      if (IdentifierStr == "if")474        return tok_if;475      if (IdentifierStr == "then")476        return tok_then;477      if (IdentifierStr == "else")478        return tok_else;479      if (IdentifierStr == "for")480        return tok_for;481      if (IdentifierStr == "in")482        return tok_in;483      return tok_identifier;484 485AST Extensions for the 'for' Loop486---------------------------------487 488The AST node is just as simple. It basically boils down to capturing the489variable name and the constituent expressions in the node.490 491.. code-block:: c++492 493    /// ForExprAST - Expression class for for/in.494    class ForExprAST : public ExprAST {495      std::string VarName;496      std::unique_ptr<ExprAST> Start, End, Step, Body;497 498    public:499      ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,500                 std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,501                 std::unique_ptr<ExprAST> Body)502        : VarName(VarName), Start(std::move(Start)), End(std::move(End)),503          Step(std::move(Step)), Body(std::move(Body)) {}504 505      Value *codegen() override;506    };507 508Parser Extensions for the 'for' Loop509------------------------------------510 511The parser code is also fairly standard. The only interesting thing here512is handling of the optional step value. The parser code handles it by513checking to see if the second comma is present. If not, it sets the step514value to null in the AST node:515 516.. code-block:: c++517 518    /// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression519    static std::unique_ptr<ExprAST> ParseForExpr() {520      getNextToken();  // eat the for.521 522      if (CurTok != tok_identifier)523        return LogError("expected identifier after for");524 525      std::string IdName = IdentifierStr;526      getNextToken();  // eat identifier.527 528      if (CurTok != '=')529        return LogError("expected '=' after for");530      getNextToken();  // eat '='.531 532 533      auto Start = ParseExpression();534      if (!Start)535        return nullptr;536      if (CurTok != ',')537        return LogError("expected ',' after for start value");538      getNextToken();539 540      auto End = ParseExpression();541      if (!End)542        return nullptr;543 544      // The step value is optional.545      std::unique_ptr<ExprAST> Step;546      if (CurTok == ',') {547        getNextToken();548        Step = ParseExpression();549        if (!Step)550          return nullptr;551      }552 553      if (CurTok != tok_in)554        return LogError("expected 'in' after for");555      getNextToken();  // eat 'in'.556 557      auto Body = ParseExpression();558      if (!Body)559        return nullptr;560 561      return std::make_unique<ForExprAST>(IdName, std::move(Start),562                                           std::move(End), std::move(Step),563                                           std::move(Body));564    }565 566And again we hook it up as a primary expression:567 568.. code-block:: c++569 570    static std::unique_ptr<ExprAST> ParsePrimary() {571      switch (CurTok) {572      default:573        return LogError("unknown token when expecting an expression");574      case tok_identifier:575        return ParseIdentifierExpr();576      case tok_number:577        return ParseNumberExpr();578      case '(':579        return ParseParenExpr();580      case tok_if:581        return ParseIfExpr();582      case tok_for:583        return ParseForExpr();584      }585    }586 587LLVM IR for the 'for' Loop588--------------------------589 590Now we get to the good part: the LLVM IR we want to generate for this591thing. With the simple example above, we get this LLVM IR (note that592this dump is generated with optimizations disabled for clarity):593 594.. code-block:: llvm595 596    declare double @putchard(double)597 598    define double @printstar(double %n) {599    entry:600      ; initial value = 1.0 (inlined into phi)601      br label %loop602 603    loop:       ; preds = %loop, %entry604      %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ]605      ; body606      %calltmp = call double @putchard(double 4.200000e+01)607      ; increment608      %nextvar = fadd double %i, 1.000000e+00609 610      ; termination test611      %cmptmp = fcmp ult double %i, %n612      %booltmp = uitofp i1 %cmptmp to double613      %loopcond = fcmp one double %booltmp, 0.000000e+00614      br i1 %loopcond, label %loop, label %afterloop615 616    afterloop:      ; preds = %loop617      ; loop always returns 0.0618      ret double 0.000000e+00619    }620 621This loop contains all the same constructs we saw before: a phi node,622several expressions, and some basic blocks. Let's see how this fits623together.624 625Code Generation for the 'for' Loop626----------------------------------627 628The first part of codegen is very simple: we just output the start629expression for the loop value:630 631.. code-block:: c++632 633    Value *ForExprAST::codegen() {634      // Emit the start code first, without 'variable' in scope.635      Value *StartVal = Start->codegen();636      if (!StartVal)637        return nullptr;638 639With this out of the way, the next step is to set up the LLVM basic640block for the start of the loop body. In the case above, the whole loop641body is one block, but remember that the body code itself could consist642of multiple blocks (e.g. if it contains an if/then/else or a for/in643expression).644 645.. code-block:: c++646 647      // Make the new basic block for the loop header, inserting after current648      // block.649      Function *TheFunction = Builder->GetInsertBlock()->getParent();650      BasicBlock *PreheaderBB = Builder->GetInsertBlock();651      BasicBlock *LoopBB =652          BasicBlock::Create(*TheContext, "loop", TheFunction);653 654      // Insert an explicit fall through from the current block to the LoopBB.655      Builder->CreateBr(LoopBB);656 657This code is similar to what we saw for if/then/else. Because we will658need it to create the Phi node, we remember the block that falls through659into the loop. Once we have that, we create the actual block that starts660the loop and create an unconditional branch for the fall-through between661the two blocks.662 663.. code-block:: c++664 665      // Start insertion in LoopBB.666      Builder->SetInsertPoint(LoopBB);667 668      // Start the PHI node with an entry for Start.669      PHINode *Variable = Builder->CreatePHI(Type::getDoubleTy(*TheContext),670                                             2, VarName);671      Variable->addIncoming(StartVal, PreheaderBB);672 673Now that the "preheader" for the loop is set up, we switch to emitting674code for the loop body. To begin with, we move the insertion point and675create the PHI node for the loop induction variable. Since we already676know the incoming value for the starting value, we add it to the Phi677node. Note that the Phi will eventually get a second value for the678backedge, but we can't set it up yet (because it doesn't exist!).679 680.. code-block:: c++681 682      // Within the loop, the variable is defined equal to the PHI node.  If it683      // shadows an existing variable, we have to restore it, so save it now.684      Value *OldVal = NamedValues[VarName];685      NamedValues[VarName] = Variable;686 687      // Emit the body of the loop.  This, like any other expr, can change the688      // current BB.  Note that we ignore the value computed by the body, but don't689      // allow an error.690      if (!Body->codegen())691        return nullptr;692 693Now the code starts to get more interesting. Our 'for' loop introduces a694new variable to the symbol table. This means that our symbol table can695now contain either function arguments or loop variables. To handle this,696before we codegen the body of the loop, we add the loop variable as the697current value for its name. Note that it is possible that there is a698variable of the same name in the outer scope. It would be easy to make699this an error (emit an error and return null if there is already an700entry for VarName) but we choose to allow shadowing of variables. In701order to handle this correctly, we remember the Value that we are702potentially shadowing in ``OldVal`` (which will be null if there is no703shadowed variable).704 705Once the loop variable is set into the symbol table, the code706recursively codegen's the body. This allows the body to use the loop707variable: any references to it will naturally find it in the symbol708table.709 710.. code-block:: c++711 712      // Emit the step value.713      Value *StepVal = nullptr;714      if (Step) {715        StepVal = Step->codegen();716        if (!StepVal)717          return nullptr;718      } else {719        // If not specified, use 1.0.720        StepVal = ConstantFP::get(*TheContext, APFloat(1.0));721      }722 723      Value *NextVar = Builder->CreateFAdd(Variable, StepVal, "nextvar");724 725Now that the body is emitted, we compute the next value of the iteration726variable by adding the step value, or 1.0 if it isn't present.727'``NextVar``' will be the value of the loop variable on the next728iteration of the loop.729 730.. code-block:: c++731 732      // Compute the end condition.733      Value *EndCond = End->codegen();734      if (!EndCond)735        return nullptr;736 737      // Convert condition to a bool by comparing non-equal to 0.0.738      EndCond = Builder->CreateFCmpONE(739          EndCond, ConstantFP::get(*TheContext, APFloat(0.0)), "loopcond");740 741Finally, we evaluate the exit value of the loop, to determine whether742the loop should exit. This mirrors the condition evaluation for the743if/then/else statement.744 745.. code-block:: c++746 747      // Create the "after loop" block and insert it.748      BasicBlock *LoopEndBB = Builder->GetInsertBlock();749      BasicBlock *AfterBB =750          BasicBlock::Create(*TheContext, "afterloop", TheFunction);751 752      // Insert the conditional branch into the end of LoopEndBB.753      Builder->CreateCondBr(EndCond, LoopBB, AfterBB);754 755      // Any new code will be inserted in AfterBB.756      Builder->SetInsertPoint(AfterBB);757 758With the code for the body of the loop complete, we just need to finish759up the control flow for it. This code remembers the end block (for the760phi node), then creates the block for the loop exit ("afterloop"). Based761on the value of the exit condition, it creates a conditional branch that762chooses between executing the loop again and exiting the loop. Any763future code is emitted in the "afterloop" block, so it sets the764insertion position to it.765 766.. code-block:: c++767 768      // Add a new entry to the PHI node for the backedge.769      Variable->addIncoming(NextVar, LoopEndBB);770 771      // Restore the unshadowed variable.772      if (OldVal)773        NamedValues[VarName] = OldVal;774      else775        NamedValues.erase(VarName);776 777      // for expr always returns 0.0.778      return Constant::getNullValue(Type::getDoubleTy(*TheContext));779    }780 781The final code handles various cleanups: now that we have the "NextVar"782value, we can add the incoming value to the loop PHI node. After that,783we remove the loop variable from the symbol table, so that it isn't in784scope after the for loop. Finally, code generation of the for loop785always returns 0.0, so that is what we return from786``ForExprAST::codegen()``.787 788With this, we conclude the "adding control flow to Kaleidoscope" chapter789of the tutorial. In this chapter we added two control flow constructs,790and used them to motivate a couple of aspects of the LLVM IR that are791important for front-end implementors to know. In the next chapter of our792saga, we will get a bit crazier and add `user-defined793operators <LangImpl06.html>`_ to our poor innocent language.794 795Full Code Listing796=================797 798Here is the complete code listing for our running example, enhanced with799the if/then/else and for expressions. To build this example, use:800 801.. code-block:: bash802 803    # Compile804    clang++ -g toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core orcjit native` -O3 -o toy805    # Run806    ./toy807 808Here is the code:809 810.. literalinclude:: ../../../examples/Kaleidoscope/Chapter5/toy.cpp811   :language: c++812 813`Next: Extending the language: user-defined operators <LangImpl06.html>`_814 815