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