blob: 203fb6f73b027cb634ea4f03f47358f0483e03c2 [file] [log] [blame]
Sean Silvaee47edf2012-12-05 00:26:32 +00001==================================================
2Kaleidoscope: Extending the Language: Control Flow
3==================================================
4
5.. contents::
6 :local:
7
8Written by `Chris Lattner <mailto:sabre@nondot.org>`_ and `Erick
9Tryzelaar <mailto:idadesub@users.sourceforge.net>`_
10
11Chapter 5 Introduction
12======================
13
14Welcome to Chapter 5 of the "`Implementing a language with
15LLVM <index.html>`_" tutorial. Parts 1-4 described the implementation of
16the simple Kaleidoscope language and included support for generating
17LLVM IR, followed by optimizations and a JIT compiler. Unfortunately, as
18presented, Kaleidoscope is mostly useless: it has no control flow other
19than call and return. This means that you can't have conditional
20branches in the code, significantly limiting its power. In this episode
21of "build that compiler", we'll extend Kaleidoscope to have an
22if/then/else expression plus a simple 'for' loop.
23
24If/Then/Else
25============
26
27Extending Kaleidoscope to support if/then/else is quite straightforward.
28It basically requires adding lexer support for this "new" concept to the
29lexer, parser, AST, and LLVM code emitter. This example is nice, because
30it shows how easy it is to "grow" a language over time, incrementally
31extending it as new ideas are discovered.
32
33Before we get going on "how" we add this extension, lets talk about
34"what" we want. The basic idea is that we want to be able to write this
35sort of thing:
36
37::
38
39 def fib(x)
40 if x < 3 then
41 1
42 else
43 fib(x-1)+fib(x-2);
44
45In Kaleidoscope, every construct is an expression: there are no
46statements. As such, the if/then/else expression needs to return a value
47like any other. Since we're using a mostly functional form, we'll have
48it evaluate its conditional, then return the 'then' or 'else' value
49based on how the condition was resolved. This is very similar to the C
50"?:" expression.
51
52The semantics of the if/then/else expression is that it evaluates the
53condition to a boolean equality value: 0.0 is considered to be false and
54everything else is considered to be true. If the condition is true, the
55first subexpression is evaluated and returned, if the condition is
56false, the second subexpression is evaluated and returned. Since
57Kaleidoscope allows side-effects, this behavior is important to nail
58down.
59
60Now that we know what we "want", lets break this down into its
61constituent pieces.
62
63Lexer Extensions for If/Then/Else
64---------------------------------
65
66The lexer extensions are straightforward. First we add new variants for
67the relevant tokens:
68
69.. code-block:: ocaml
70
71 (* control *)
72 | If | Then | Else | For | In
73
74Once we have that, we recognize the new keywords in the lexer. This is
75pretty simple stuff:
76
77.. code-block:: ocaml
78
79 ...
80 match Buffer.contents buffer with
81 | "def" -> [< 'Token.Def; stream >]
82 | "extern" -> [< 'Token.Extern; stream >]
83 | "if" -> [< 'Token.If; stream >]
84 | "then" -> [< 'Token.Then; stream >]
85 | "else" -> [< 'Token.Else; stream >]
86 | "for" -> [< 'Token.For; stream >]
87 | "in" -> [< 'Token.In; stream >]
88 | id -> [< 'Token.Ident id; stream >]
89
90AST Extensions for If/Then/Else
91-------------------------------
92
93To represent the new expression we add a new AST variant for it:
94
95.. code-block:: ocaml
96
97 type expr =
98 ...
99 (* variant for if/then/else. *)
100 | If of expr * expr * expr
101
102The AST variant just has pointers to the various subexpressions.
103
104Parser Extensions for If/Then/Else
105----------------------------------
106
107Now that we have the relevant tokens coming from the lexer and we have
108the AST node to build, our parsing logic is relatively straightforward.
109First we define a new parsing function:
110
111.. code-block:: ocaml
112
113 let rec parse_primary = parser
114 ...
115 (* ifexpr ::= 'if' expr 'then' expr 'else' expr *)
116 | [< 'Token.If; c=parse_expr;
117 'Token.Then ?? "expected 'then'"; t=parse_expr;
118 'Token.Else ?? "expected 'else'"; e=parse_expr >] ->
119 Ast.If (c, t, e)
120
121Next we hook it up as a primary expression:
122
123.. code-block:: ocaml
124
125 let rec parse_primary = parser
126 ...
127 (* ifexpr ::= 'if' expr 'then' expr 'else' expr *)
128 | [< 'Token.If; c=parse_expr;
129 'Token.Then ?? "expected 'then'"; t=parse_expr;
130 'Token.Else ?? "expected 'else'"; e=parse_expr >] ->
131 Ast.If (c, t, e)
132
133LLVM IR for If/Then/Else
134------------------------
135
136Now that we have it parsing and building the AST, the final piece is
137adding LLVM code generation support. This is the most interesting part
138of the if/then/else example, because this is where it starts to
139introduce new concepts. All of the code above has been thoroughly
140described in previous chapters.
141
142To motivate the code we want to produce, lets take a look at a simple
143example. Consider:
144
145::
146
147 extern foo();
148 extern bar();
149 def baz(x) if x then foo() else bar();
150
151If you disable optimizations, the code you'll (soon) get from
152Kaleidoscope looks like this:
153
154.. code-block:: llvm
155
156 declare double @foo()
157
158 declare double @bar()
159
160 define double @baz(double %x) {
161 entry:
162 %ifcond = fcmp one double %x, 0.000000e+00
163 br i1 %ifcond, label %then, label %else
164
165 then: ; preds = %entry
166 %calltmp = call double @foo()
167 br label %ifcont
168
169 else: ; preds = %entry
170 %calltmp1 = call double @bar()
171 br label %ifcont
172
173 ifcont: ; preds = %else, %then
174 %iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ]
175 ret double %iftmp
176 }
177
178To visualize the control flow graph, you can use a nifty feature of the
179LLVM '`opt <http://llvm.org/cmds/opt.html>`_' tool. If you put this LLVM
180IR into "t.ll" and run "``llvm-as < t.ll | opt -analyze -view-cfg``", `a
181window will pop up <../ProgrammersManual.html#ViewGraph>`_ and you'll
182see this graph:
183
184.. figure:: LangImpl5-cfg.png
185 :align: center
186 :alt: Example CFG
187
188 Example CFG
189
190Another way to get this is to call
191"``Llvm_analysis.view_function_cfg f``" or
192"``Llvm_analysis.view_function_cfg_only f``" (where ``f`` is a
193"``Function``") either by inserting actual calls into the code and
194recompiling or by calling these in the debugger. LLVM has many nice
195features for visualizing various graphs.
196
197Getting back to the generated code, it is fairly simple: the entry block
198evaluates the conditional expression ("x" in our case here) and compares
199the result to 0.0 with the "``fcmp one``" instruction ('one' is "Ordered
200and Not Equal"). Based on the result of this expression, the code jumps
201to either the "then" or "else" blocks, which contain the expressions for
202the true/false cases.
203
204Once the then/else blocks are finished executing, they both branch back
205to the 'ifcont' block to execute the code that happens after the
206if/then/else. In this case the only thing left to do is to return to the
207caller of the function. The question then becomes: how does the code
208know which expression to return?
209
210The answer to this question involves an important SSA operation: the
211`Phi
212operation <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_.
213If you're not familiar with SSA, `the wikipedia
214article <http://en.wikipedia.org/wiki/Static_single_assignment_form>`_
215is a good introduction and there are various other introductions to it
216available on your favorite search engine. The short version is that
217"execution" of the Phi operation requires "remembering" which block
218control came from. The Phi operation takes on the value corresponding to
219the input control block. In this case, if control comes in from the
220"then" block, it gets the value of "calltmp". If control comes from the
221"else" block, it gets the value of "calltmp1".
222
223At this point, you are probably starting to think "Oh no! This means my
224simple and elegant front-end will have to start generating SSA form in
225order to use LLVM!". Fortunately, this is not the case, and we strongly
226advise *not* implementing an SSA construction algorithm in your
227front-end unless there is an amazingly good reason to do so. In
228practice, there are two sorts of values that float around in code
229written for your average imperative programming language that might need
230Phi nodes:
231
232#. Code that involves user variables: ``x = 1; x = x + 1;``
233#. Values that are implicit in the structure of your AST, such as the
234 Phi node in this case.
235
236In `Chapter 7 <OCamlLangImpl7.html>`_ of this tutorial ("mutable
237variables"), we'll talk about #1 in depth. For now, just believe me that
238you don't need SSA construction to handle this case. For #2, you have
239the choice of using the techniques that we will describe for #1, or you
240can insert Phi nodes directly, if convenient. In this case, it is really
241really easy to generate the Phi node, so we choose to do it directly.
242
243Okay, enough of the motivation and overview, lets generate code!
244
245Code Generation for If/Then/Else
246--------------------------------
247
248In order to generate code for this, we implement the ``Codegen`` method
249for ``IfExprAST``:
250
251.. code-block:: ocaml
252
253 let rec codegen_expr = function
254 ...
255 | Ast.If (cond, then_, else_) ->
256 let cond = codegen_expr cond in
257
258 (* Convert condition to a bool by comparing equal to 0.0 *)
259 let zero = const_float double_type 0.0 in
260 let cond_val = build_fcmp Fcmp.One cond zero "ifcond" builder in
261
262This code is straightforward and similar to what we saw before. We emit
263the expression for the condition, then compare that value to zero to get
264a truth value as a 1-bit (bool) value.
265
266.. code-block:: ocaml
267
268 (* Grab the first block so that we might later add the conditional branch
269 * to it at the end of the function. *)
270 let start_bb = insertion_block builder in
271 let the_function = block_parent start_bb in
272
273 let then_bb = append_block context "then" the_function in
274 position_at_end then_bb builder;
275
276As opposed to the `C++ tutorial <LangImpl5.html>`_, we have to build our
277basic blocks bottom up since we can't have dangling BasicBlocks. We
278start off by saving a pointer to the first block (which might not be the
279entry block), which we'll need to build a conditional branch later. We
280do this by asking the ``builder`` for the current BasicBlock. The fourth
281line gets the current Function object that is being built. It gets this
282by the ``start_bb`` for its "parent" (the function it is currently
283embedded into).
284
285Once it has that, it creates one block. It is automatically appended
286into the function's list of blocks.
287
288.. code-block:: ocaml
289
290 (* Emit 'then' value. *)
291 position_at_end then_bb builder;
292 let then_val = codegen_expr then_ in
293
294 (* Codegen of 'then' can change the current block, update then_bb for the
295 * phi. We create a new name because one is used for the phi node, and the
296 * other is used for the conditional branch. *)
297 let new_then_bb = insertion_block builder in
298
299We move the builder to start inserting into the "then" block. Strictly
300speaking, this call moves the insertion point to be at the end of the
301specified block. However, since the "then" block is empty, it also
302starts out by inserting at the beginning of the block. :)
303
304Once the insertion point is set, we recursively codegen the "then"
305expression from the AST.
306
307The final line here is quite subtle, but is very important. The basic
308issue is that when we create the Phi node in the merge block, we need to
309set up the block/value pairs that indicate how the Phi will work.
310Importantly, the Phi node expects to have an entry for each predecessor
311of the block in the CFG. Why then, are we getting the current block when
312we just set it to ThenBB 5 lines above? The problem is that the "Then"
313expression may actually itself change the block that the Builder is
314emitting into if, for example, it contains a nested "if/then/else"
315expression. Because calling Codegen recursively could arbitrarily change
316the notion of the current block, we are required to get an up-to-date
317value for code that will set up the Phi node.
318
319.. code-block:: ocaml
320
321 (* Emit 'else' value. *)
322 let else_bb = append_block context "else" the_function in
323 position_at_end else_bb builder;
324 let else_val = codegen_expr else_ in
325
326 (* Codegen of 'else' can change the current block, update else_bb for the
327 * phi. *)
328 let new_else_bb = insertion_block builder in
329
330Code generation for the 'else' block is basically identical to codegen
331for the 'then' block.
332
333.. code-block:: ocaml
334
335 (* Emit merge block. *)
336 let merge_bb = append_block context "ifcont" the_function in
337 position_at_end merge_bb builder;
338 let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
339 let phi = build_phi incoming "iftmp" builder in
340
341The first two lines here are now familiar: the first adds the "merge"
342block to the Function object. The second block changes the insertion
343point so that newly created code will go into the "merge" block. Once
344that is done, we need to create the PHI node and set up the block/value
345pairs for the PHI.
346
347.. code-block:: ocaml
348
349 (* Return to the start block to add the conditional branch. *)
350 position_at_end start_bb builder;
351 ignore (build_cond_br cond_val then_bb else_bb builder);
352
353Once the blocks are created, we can emit the conditional branch that
354chooses between them. Note that creating new blocks does not implicitly
355affect the IRBuilder, so it is still inserting into the block that the
356condition went into. This is why we needed to save the "start" block.
357
358.. code-block:: ocaml
359
360 (* Set a unconditional branch at the end of the 'then' block and the
361 * 'else' block to the 'merge' block. *)
362 position_at_end new_then_bb builder; ignore (build_br merge_bb builder);
363 position_at_end new_else_bb builder; ignore (build_br merge_bb builder);
364
365 (* Finally, set the builder to the end of the merge block. *)
366 position_at_end merge_bb builder;
367
368 phi
369
370To finish off the blocks, we create an unconditional branch to the merge
371block. One interesting (and very important) aspect of the LLVM IR is
372that it `requires all basic blocks to be
373"terminated" <../LangRef.html#functionstructure>`_ with a `control flow
374instruction <../LangRef.html#terminators>`_ such as return or branch.
375This means that all control flow, *including fall throughs* must be made
376explicit in the LLVM IR. If you violate this rule, the verifier will
377emit an error.
378
379Finally, the CodeGen function returns the phi node as the value computed
380by the if/then/else expression. In our example above, this returned
381value will feed into the code for the top-level function, which will
382create the return instruction.
383
384Overall, we now have the ability to execute conditional code in
385Kaleidoscope. With this extension, Kaleidoscope is a fairly complete
386language that can calculate a wide variety of numeric functions. Next up
387we'll add another useful expression that is familiar from non-functional
388languages...
389
390'for' Loop Expression
391=====================
392
393Now that we know how to add basic control flow constructs to the
394language, we have the tools to add more powerful things. Lets add
395something more aggressive, a 'for' expression:
396
397::
398
399 extern putchard(char);
400 def printstar(n)
401 for i = 1, i < n, 1.0 in
402 putchard(42); # ascii 42 = '*'
403
404 # print 100 '*' characters
405 printstar(100);
406
407This expression defines a new variable ("i" in this case) which iterates
408from a starting value, while the condition ("i < n" in this case) is
409true, incrementing by an optional step value ("1.0" in this case). If
410the step value is omitted, it defaults to 1.0. While the loop is true,
411it executes its body expression. Because we don't have anything better
412to return, we'll just define the loop as always returning 0.0. In the
413future when we have mutable variables, it will get more useful.
414
415As before, lets talk about the changes that we need to Kaleidoscope to
416support this.
417
418Lexer Extensions for the 'for' Loop
419-----------------------------------
420
421The lexer extensions are the same sort of thing as for if/then/else:
422
423.. code-block:: ocaml
424
425 ... in Token.token ...
426 (* control *)
427 | If | Then | Else
428 | For | In
429
430 ... in Lexer.lex_ident...
431 match Buffer.contents buffer with
432 | "def" -> [< 'Token.Def; stream >]
433 | "extern" -> [< 'Token.Extern; stream >]
434 | "if" -> [< 'Token.If; stream >]
435 | "then" -> [< 'Token.Then; stream >]
436 | "else" -> [< 'Token.Else; stream >]
437 | "for" -> [< 'Token.For; stream >]
438 | "in" -> [< 'Token.In; stream >]
439 | id -> [< 'Token.Ident id; stream >]
440
441AST Extensions for the 'for' Loop
442---------------------------------
443
444The AST variant is just as simple. It basically boils down to capturing
445the variable name and the constituent expressions in the node.
446
447.. code-block:: ocaml
448
449 type expr =
450 ...
451 (* variant for for/in. *)
452 | For of string * expr * expr * expr option * expr
453
454Parser Extensions for the 'for' Loop
455------------------------------------
456
457The parser code is also fairly standard. The only interesting thing here
458is handling of the optional step value. The parser code handles it by
459checking to see if the second comma is present. If not, it sets the step
460value to null in the AST node:
461
462.. code-block:: ocaml
463
464 let rec parse_primary = parser
465 ...
466 (* forexpr
467 ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression *)
468 | [< 'Token.For;
469 'Token.Ident id ?? "expected identifier after for";
470 'Token.Kwd '=' ?? "expected '=' after for";
471 stream >] ->
472 begin parser
473 | [<
474 start=parse_expr;
475 'Token.Kwd ',' ?? "expected ',' after for";
476 end_=parse_expr;
477 stream >] ->
478 let step =
479 begin parser
480 | [< 'Token.Kwd ','; step=parse_expr >] -> Some step
481 | [< >] -> None
482 end stream
483 in
484 begin parser
485 | [< 'Token.In; body=parse_expr >] ->
486 Ast.For (id, start, end_, step, body)
487 | [< >] ->
488 raise (Stream.Error "expected 'in' after for")
489 end stream
490 | [< >] ->
491 raise (Stream.Error "expected '=' after for")
492 end stream
493
494LLVM IR for the 'for' Loop
495--------------------------
496
497Now we get to the good part: the LLVM IR we want to generate for this
498thing. With the simple example above, we get this LLVM IR (note that
499this dump is generated with optimizations disabled for clarity):
500
501.. code-block:: llvm
502
503 declare double @putchard(double)
504
505 define double @printstar(double %n) {
506 entry:
507 ; initial value = 1.0 (inlined into phi)
508 br label %loop
509
510 loop: ; preds = %loop, %entry
511 %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ]
512 ; body
513 %calltmp = call double @putchard(double 4.200000e+01)
514 ; increment
515 %nextvar = fadd double %i, 1.000000e+00
516
517 ; termination test
518 %cmptmp = fcmp ult double %i, %n
519 %booltmp = uitofp i1 %cmptmp to double
520 %loopcond = fcmp one double %booltmp, 0.000000e+00
521 br i1 %loopcond, label %loop, label %afterloop
522
523 afterloop: ; preds = %loop
524 ; loop always returns 0.0
525 ret double 0.000000e+00
526 }
527
528This loop contains all the same constructs we saw before: a phi node,
529several expressions, and some basic blocks. Lets see how this fits
530together.
531
532Code Generation for the 'for' Loop
533----------------------------------
534
535The first part of Codegen is very simple: we just output the start
536expression for the loop value:
537
538.. code-block:: ocaml
539
540 let rec codegen_expr = function
541 ...
542 | Ast.For (var_name, start, end_, step, body) ->
543 (* Emit the start code first, without 'variable' in scope. *)
544 let start_val = codegen_expr start in
545
546With this out of the way, the next step is to set up the LLVM basic
547block for the start of the loop body. In the case above, the whole loop
548body is one block, but remember that the body code itself could consist
549of multiple blocks (e.g. if it contains an if/then/else or a for/in
550expression).
551
552.. code-block:: ocaml
553
554 (* Make the new basic block for the loop header, inserting after current
555 * block. *)
556 let preheader_bb = insertion_block builder in
557 let the_function = block_parent preheader_bb in
558 let loop_bb = append_block context "loop" the_function in
559
560 (* Insert an explicit fall through from the current block to the
561 * loop_bb. *)
562 ignore (build_br loop_bb builder);
563
564This code is similar to what we saw for if/then/else. Because we will
565need it to create the Phi node, we remember the block that falls through
566into the loop. Once we have that, we create the actual block that starts
567the loop and create an unconditional branch for the fall-through between
568the two blocks.
569
570.. code-block:: ocaml
571
572 (* Start insertion in loop_bb. *)
573 position_at_end loop_bb builder;
574
575 (* Start the PHI node with an entry for start. *)
576 let variable = build_phi [(start_val, preheader_bb)] var_name builder in
577
578Now that the "preheader" for the loop is set up, we switch to emitting
579code for the loop body. To begin with, we move the insertion point and
580create the PHI node for the loop induction variable. Since we already
581know the incoming value for the starting value, we add it to the Phi
582node. Note that the Phi will eventually get a second value for the
583backedge, but we can't set it up yet (because it doesn't exist!).
584
585.. code-block:: ocaml
586
587 (* Within the loop, the variable is defined equal to the PHI node. If it
588 * shadows an existing variable, we have to restore it, so save it
589 * now. *)
590 let old_val =
591 try Some (Hashtbl.find named_values var_name) with Not_found -> None
592 in
593 Hashtbl.add named_values var_name variable;
594
595 (* Emit the body of the loop. This, like any other expr, can change the
596 * current BB. Note that we ignore the value computed by the body, but
597 * don't allow an error *)
598 ignore (codegen_expr body);
599
600Now the code starts to get more interesting. Our 'for' loop introduces a
601new variable to the symbol table. This means that our symbol table can
602now contain either function arguments or loop variables. To handle this,
603before we codegen the body of the loop, we add the loop variable as the
604current value for its name. Note that it is possible that there is a
605variable of the same name in the outer scope. It would be easy to make
606this an error (emit an error and return null if there is already an
607entry for VarName) but we choose to allow shadowing of variables. In
608order to handle this correctly, we remember the Value that we are
609potentially shadowing in ``old_val`` (which will be None if there is no
610shadowed variable).
611
612Once the loop variable is set into the symbol table, the code
613recursively codegen's the body. This allows the body to use the loop
614variable: any references to it will naturally find it in the symbol
615table.
616
617.. code-block:: ocaml
618
619 (* Emit the step value. *)
620 let step_val =
621 match step with
622 | Some step -> codegen_expr step
623 (* If not specified, use 1.0. *)
624 | None -> const_float double_type 1.0
625 in
626
627 let next_var = build_add variable step_val "nextvar" builder in
628
629Now that the body is emitted, we compute the next value of the iteration
630variable by adding the step value, or 1.0 if it isn't present.
631'``next_var``' will be the value of the loop variable on the next
632iteration of the loop.
633
634.. code-block:: ocaml
635
636 (* Compute the end condition. *)
637 let end_cond = codegen_expr end_ in
638
639 (* Convert condition to a bool by comparing equal to 0.0. *)
640 let zero = const_float double_type 0.0 in
641 let end_cond = build_fcmp Fcmp.One end_cond zero "loopcond" builder in
642
643Finally, we evaluate the exit value of the loop, to determine whether
644the loop should exit. This mirrors the condition evaluation for the
645if/then/else statement.
646
647.. code-block:: ocaml
648
649 (* Create the "after loop" block and insert it. *)
650 let loop_end_bb = insertion_block builder in
651 let after_bb = append_block context "afterloop" the_function in
652
653 (* Insert the conditional branch into the end of loop_end_bb. *)
654 ignore (build_cond_br end_cond loop_bb after_bb builder);
655
656 (* Any new code will be inserted in after_bb. *)
657 position_at_end after_bb builder;
658
659With the code for the body of the loop complete, we just need to finish
660up the control flow for it. This code remembers the end block (for the
661phi node), then creates the block for the loop exit ("afterloop"). Based
662on the value of the exit condition, it creates a conditional branch that
663chooses between executing the loop again and exiting the loop. Any
664future code is emitted in the "afterloop" block, so it sets the
665insertion position to it.
666
667.. code-block:: ocaml
668
669 (* Add a new entry to the PHI node for the backedge. *)
670 add_incoming (next_var, loop_end_bb) variable;
671
672 (* Restore the unshadowed variable. *)
673 begin match old_val with
674 | Some old_val -> Hashtbl.add named_values var_name old_val
675 | None -> ()
676 end;
677
678 (* for expr always returns 0.0. *)
679 const_null double_type
680
681The final code handles various cleanups: now that we have the
682"``next_var``" value, we can add the incoming value to the loop PHI
683node. After that, we remove the loop variable from the symbol table, so
684that it isn't in scope after the for loop. Finally, code generation of
685the for loop always returns 0.0, so that is what we return from
686``Codegen.codegen_expr``.
687
688With this, we conclude the "adding control flow to Kaleidoscope" chapter
689of the tutorial. In this chapter we added two control flow constructs,
690and used them to motivate a couple of aspects of the LLVM IR that are
691important for front-end implementors to know. In the next chapter of our
692saga, we will get a bit crazier and add `user-defined
693operators <OCamlLangImpl6.html>`_ to our poor innocent language.
694
695Full Code Listing
696=================
697
698Here is the complete code listing for our running example, enhanced with
699the if/then/else and for expressions.. To build this example, use:
700
701.. code-block:: bash
702
703 # Compile
704 ocamlbuild toy.byte
705 # Run
706 ./toy.byte
707
708Here is the code:
709
710\_tags:
711 ::
712
713 <{lexer,parser}.ml>: use_camlp4, pp(camlp4of)
714 <*.{byte,native}>: g++, use_llvm, use_llvm_analysis
715 <*.{byte,native}>: use_llvm_executionengine, use_llvm_target
716 <*.{byte,native}>: use_llvm_scalar_opts, use_bindings
717
718myocamlbuild.ml:
719 .. code-block:: ocaml
720
721 open Ocamlbuild_plugin;;
722
723 ocaml_lib ~extern:true "llvm";;
724 ocaml_lib ~extern:true "llvm_analysis";;
725 ocaml_lib ~extern:true "llvm_executionengine";;
726 ocaml_lib ~extern:true "llvm_target";;
727 ocaml_lib ~extern:true "llvm_scalar_opts";;
728
729 flag ["link"; "ocaml"; "g++"] (S[A"-cc"; A"g++"]);;
730 dep ["link"; "ocaml"; "use_bindings"] ["bindings.o"];;
731
732token.ml:
733 .. code-block:: ocaml
734
735 (*===----------------------------------------------------------------------===
736 * Lexer Tokens
737 *===----------------------------------------------------------------------===*)
738
739 (* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of
740 * these others for known things. *)
741 type token =
742 (* commands *)
743 | Def | Extern
744
745 (* primary *)
746 | Ident of string | Number of float
747
748 (* unknown *)
749 | Kwd of char
750
751 (* control *)
752 | If | Then | Else
753 | For | In
754
755lexer.ml:
756 .. code-block:: ocaml
757
758 (*===----------------------------------------------------------------------===
759 * Lexer
760 *===----------------------------------------------------------------------===*)
761
762 let rec lex = parser
763 (* Skip any whitespace. *)
764 | [< ' (' ' | '\n' | '\r' | '\t'); stream >] -> lex stream
765
766 (* identifier: [a-zA-Z][a-zA-Z0-9] *)
767 | [< ' ('A' .. 'Z' | 'a' .. 'z' as c); stream >] ->
768 let buffer = Buffer.create 1 in
769 Buffer.add_char buffer c;
770 lex_ident buffer stream
771
772 (* number: [0-9.]+ *)
773 | [< ' ('0' .. '9' as c); stream >] ->
774 let buffer = Buffer.create 1 in
775 Buffer.add_char buffer c;
776 lex_number buffer stream
777
778 (* Comment until end of line. *)
779 | [< ' ('#'); stream >] ->
780 lex_comment stream
781
782 (* Otherwise, just return the character as its ascii value. *)
783 | [< 'c; stream >] ->
784 [< 'Token.Kwd c; lex stream >]
785
786 (* end of stream. *)
787 | [< >] -> [< >]
788
789 and lex_number buffer = parser
790 | [< ' ('0' .. '9' | '.' as c); stream >] ->
791 Buffer.add_char buffer c;
792 lex_number buffer stream
793 | [< stream=lex >] ->
794 [< 'Token.Number (float_of_string (Buffer.contents buffer)); stream >]
795
796 and lex_ident buffer = parser
797 | [< ' ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' as c); stream >] ->
798 Buffer.add_char buffer c;
799 lex_ident buffer stream
800 | [< stream=lex >] ->
801 match Buffer.contents buffer with
802 | "def" -> [< 'Token.Def; stream >]
803 | "extern" -> [< 'Token.Extern; stream >]
804 | "if" -> [< 'Token.If; stream >]
805 | "then" -> [< 'Token.Then; stream >]
806 | "else" -> [< 'Token.Else; stream >]
807 | "for" -> [< 'Token.For; stream >]
808 | "in" -> [< 'Token.In; stream >]
809 | id -> [< 'Token.Ident id; stream >]
810
811 and lex_comment = parser
812 | [< ' ('\n'); stream=lex >] -> stream
813 | [< 'c; e=lex_comment >] -> e
814 | [< >] -> [< >]
815
816ast.ml:
817 .. code-block:: ocaml
818
819 (*===----------------------------------------------------------------------===
820 * Abstract Syntax Tree (aka Parse Tree)
821 *===----------------------------------------------------------------------===*)
822
823 (* expr - Base type for all expression nodes. *)
824 type expr =
825 (* variant for numeric literals like "1.0". *)
826 | Number of float
827
828 (* variant for referencing a variable, like "a". *)
829 | Variable of string
830
831 (* variant for a binary operator. *)
832 | Binary of char * expr * expr
833
834 (* variant for function calls. *)
835 | Call of string * expr array
836
837 (* variant for if/then/else. *)
838 | If of expr * expr * expr
839
840 (* variant for for/in. *)
841 | For of string * expr * expr * expr option * expr
842
843 (* proto - This type represents the "prototype" for a function, which captures
844 * its name, and its argument names (thus implicitly the number of arguments the
845 * function takes). *)
846 type proto = Prototype of string * string array
847
848 (* func - This type represents a function definition itself. *)
849 type func = Function of proto * expr
850
851parser.ml:
852 .. code-block:: ocaml
853
854 (*===---------------------------------------------------------------------===
855 * Parser
856 *===---------------------------------------------------------------------===*)
857
858 (* binop_precedence - This holds the precedence for each binary operator that is
859 * defined *)
860 let binop_precedence:(char, int) Hashtbl.t = Hashtbl.create 10
861
862 (* precedence - Get the precedence of the pending binary operator token. *)
863 let precedence c = try Hashtbl.find binop_precedence c with Not_found -> -1
864
865 (* primary
866 * ::= identifier
867 * ::= numberexpr
868 * ::= parenexpr
869 * ::= ifexpr
870 * ::= forexpr *)
871 let rec parse_primary = parser
872 (* numberexpr ::= number *)
873 | [< 'Token.Number n >] -> Ast.Number n
874
875 (* parenexpr ::= '(' expression ')' *)
876 | [< 'Token.Kwd '('; e=parse_expr; 'Token.Kwd ')' ?? "expected ')'" >] -> e
877
878 (* identifierexpr
879 * ::= identifier
880 * ::= identifier '(' argumentexpr ')' *)
881 | [< 'Token.Ident id; stream >] ->
882 let rec parse_args accumulator = parser
883 | [< e=parse_expr; stream >] ->
884 begin parser
885 | [< 'Token.Kwd ','; e=parse_args (e :: accumulator) >] -> e
886 | [< >] -> e :: accumulator
887 end stream
888 | [< >] -> accumulator
889 in
890 let rec parse_ident id = parser
891 (* Call. *)
892 | [< 'Token.Kwd '(';
893 args=parse_args [];
894 'Token.Kwd ')' ?? "expected ')'">] ->
895 Ast.Call (id, Array.of_list (List.rev args))
896
897 (* Simple variable ref. *)
898 | [< >] -> Ast.Variable id
899 in
900 parse_ident id stream
901
902 (* ifexpr ::= 'if' expr 'then' expr 'else' expr *)
903 | [< 'Token.If; c=parse_expr;
904 'Token.Then ?? "expected 'then'"; t=parse_expr;
905 'Token.Else ?? "expected 'else'"; e=parse_expr >] ->
906 Ast.If (c, t, e)
907
908 (* forexpr
909 ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression *)
910 | [< 'Token.For;
911 'Token.Ident id ?? "expected identifier after for";
912 'Token.Kwd '=' ?? "expected '=' after for";
913 stream >] ->
914 begin parser
915 | [<
916 start=parse_expr;
917 'Token.Kwd ',' ?? "expected ',' after for";
918 end_=parse_expr;
919 stream >] ->
920 let step =
921 begin parser
922 | [< 'Token.Kwd ','; step=parse_expr >] -> Some step
923 | [< >] -> None
924 end stream
925 in
926 begin parser
927 | [< 'Token.In; body=parse_expr >] ->
928 Ast.For (id, start, end_, step, body)
929 | [< >] ->
930 raise (Stream.Error "expected 'in' after for")
931 end stream
932 | [< >] ->
933 raise (Stream.Error "expected '=' after for")
934 end stream
935
936 | [< >] -> raise (Stream.Error "unknown token when expecting an expression.")
937
938 (* binoprhs
939 * ::= ('+' primary)* *)
940 and parse_bin_rhs expr_prec lhs stream =
941 match Stream.peek stream with
942 (* If this is a binop, find its precedence. *)
943 | Some (Token.Kwd c) when Hashtbl.mem binop_precedence c ->
944 let token_prec = precedence c in
945
946 (* If this is a binop that binds at least as tightly as the current binop,
947 * consume it, otherwise we are done. *)
948 if token_prec < expr_prec then lhs else begin
949 (* Eat the binop. *)
950 Stream.junk stream;
951
952 (* Parse the primary expression after the binary operator. *)
953 let rhs = parse_primary stream in
954
955 (* Okay, we know this is a binop. *)
956 let rhs =
957 match Stream.peek stream with
958 | Some (Token.Kwd c2) ->
959 (* If BinOp binds less tightly with rhs than the operator after
960 * rhs, let the pending operator take rhs as its lhs. *)
961 let next_prec = precedence c2 in
962 if token_prec < next_prec
963 then parse_bin_rhs (token_prec + 1) rhs stream
964 else rhs
965 | _ -> rhs
966 in
967
968 (* Merge lhs/rhs. *)
969 let lhs = Ast.Binary (c, lhs, rhs) in
970 parse_bin_rhs expr_prec lhs stream
971 end
972 | _ -> lhs
973
974 (* expression
975 * ::= primary binoprhs *)
976 and parse_expr = parser
977 | [< lhs=parse_primary; stream >] -> parse_bin_rhs 0 lhs stream
978
979 (* prototype
980 * ::= id '(' id* ')' *)
981 let parse_prototype =
982 let rec parse_args accumulator = parser
983 | [< 'Token.Ident id; e=parse_args (id::accumulator) >] -> e
984 | [< >] -> accumulator
985 in
986
987 parser
988 | [< 'Token.Ident id;
989 'Token.Kwd '(' ?? "expected '(' in prototype";
990 args=parse_args [];
991 'Token.Kwd ')' ?? "expected ')' in prototype" >] ->
992 (* success. *)
993 Ast.Prototype (id, Array.of_list (List.rev args))
994
995 | [< >] ->
996 raise (Stream.Error "expected function name in prototype")
997
998 (* definition ::= 'def' prototype expression *)
999 let parse_definition = parser
1000 | [< 'Token.Def; p=parse_prototype; e=parse_expr >] ->
1001 Ast.Function (p, e)
1002
1003 (* toplevelexpr ::= expression *)
1004 let parse_toplevel = parser
1005 | [< e=parse_expr >] ->
1006 (* Make an anonymous proto. *)
1007 Ast.Function (Ast.Prototype ("", [||]), e)
1008
1009 (* external ::= 'extern' prototype *)
1010 let parse_extern = parser
1011 | [< 'Token.Extern; e=parse_prototype >] -> e
1012
1013codegen.ml:
1014 .. code-block:: ocaml
1015
1016 (*===----------------------------------------------------------------------===
1017 * Code Generation
1018 *===----------------------------------------------------------------------===*)
1019
1020 open Llvm
1021
1022 exception Error of string
1023
1024 let context = global_context ()
1025 let the_module = create_module context "my cool jit"
1026 let builder = builder context
1027 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
1028 let double_type = double_type context
1029
1030 let rec codegen_expr = function
1031 | Ast.Number n -> const_float double_type n
1032 | Ast.Variable name ->
1033 (try Hashtbl.find named_values name with
1034 | Not_found -> raise (Error "unknown variable name"))
1035 | Ast.Binary (op, lhs, rhs) ->
1036 let lhs_val = codegen_expr lhs in
1037 let rhs_val = codegen_expr rhs in
1038 begin
1039 match op with
1040 | '+' -> build_add lhs_val rhs_val "addtmp" builder
1041 | '-' -> build_sub lhs_val rhs_val "subtmp" builder
1042 | '*' -> build_mul lhs_val rhs_val "multmp" builder
1043 | '<' ->
1044 (* Convert bool 0/1 to double 0.0 or 1.0 *)
1045 let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in
1046 build_uitofp i double_type "booltmp" builder
1047 | _ -> raise (Error "invalid binary operator")
1048 end
1049 | Ast.Call (callee, args) ->
1050 (* Look up the name in the module table. *)
1051 let callee =
1052 match lookup_function callee the_module with
1053 | Some callee -> callee
1054 | None -> raise (Error "unknown function referenced")
1055 in
1056 let params = params callee in
1057
1058 (* If argument mismatch error. *)
1059 if Array.length params == Array.length args then () else
1060 raise (Error "incorrect # arguments passed");
1061 let args = Array.map codegen_expr args in
1062 build_call callee args "calltmp" builder
1063 | Ast.If (cond, then_, else_) ->
1064 let cond = codegen_expr cond in
1065
1066 (* Convert condition to a bool by comparing equal to 0.0 *)
1067 let zero = const_float double_type 0.0 in
1068 let cond_val = build_fcmp Fcmp.One cond zero "ifcond" builder in
1069
1070 (* Grab the first block so that we might later add the conditional branch
1071 * to it at the end of the function. *)
1072 let start_bb = insertion_block builder in
1073 let the_function = block_parent start_bb in
1074
1075 let then_bb = append_block context "then" the_function in
1076
1077 (* Emit 'then' value. *)
1078 position_at_end then_bb builder;
1079 let then_val = codegen_expr then_ in
1080
1081 (* Codegen of 'then' can change the current block, update then_bb for the
1082 * phi. We create a new name because one is used for the phi node, and the
1083 * other is used for the conditional branch. *)
1084 let new_then_bb = insertion_block builder in
1085
1086 (* Emit 'else' value. *)
1087 let else_bb = append_block context "else" the_function in
1088 position_at_end else_bb builder;
1089 let else_val = codegen_expr else_ in
1090
1091 (* Codegen of 'else' can change the current block, update else_bb for the
1092 * phi. *)
1093 let new_else_bb = insertion_block builder in
1094
1095 (* Emit merge block. *)
1096 let merge_bb = append_block context "ifcont" the_function in
1097 position_at_end merge_bb builder;
1098 let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
1099 let phi = build_phi incoming "iftmp" builder in
1100
1101 (* Return to the start block to add the conditional branch. *)
1102 position_at_end start_bb builder;
1103 ignore (build_cond_br cond_val then_bb else_bb builder);
1104
1105 (* Set a unconditional branch at the end of the 'then' block and the
1106 * 'else' block to the 'merge' block. *)
1107 position_at_end new_then_bb builder; ignore (build_br merge_bb builder);
1108 position_at_end new_else_bb builder; ignore (build_br merge_bb builder);
1109
1110 (* Finally, set the builder to the end of the merge block. *)
1111 position_at_end merge_bb builder;
1112
1113 phi
1114 | Ast.For (var_name, start, end_, step, body) ->
1115 (* Emit the start code first, without 'variable' in scope. *)
1116 let start_val = codegen_expr start in
1117
1118 (* Make the new basic block for the loop header, inserting after current
1119 * block. *)
1120 let preheader_bb = insertion_block builder in
1121 let the_function = block_parent preheader_bb in
1122 let loop_bb = append_block context "loop" the_function in
1123
1124 (* Insert an explicit fall through from the current block to the
1125 * loop_bb. *)
1126 ignore (build_br loop_bb builder);
1127
1128 (* Start insertion in loop_bb. *)
1129 position_at_end loop_bb builder;
1130
1131 (* Start the PHI node with an entry for start. *)
1132 let variable = build_phi [(start_val, preheader_bb)] var_name builder in
1133
1134 (* Within the loop, the variable is defined equal to the PHI node. If it
1135 * shadows an existing variable, we have to restore it, so save it
1136 * now. *)
1137 let old_val =
1138 try Some (Hashtbl.find named_values var_name) with Not_found -> None
1139 in
1140 Hashtbl.add named_values var_name variable;
1141
1142 (* Emit the body of the loop. This, like any other expr, can change the
1143 * current BB. Note that we ignore the value computed by the body, but
1144 * don't allow an error *)
1145 ignore (codegen_expr body);
1146
1147 (* Emit the step value. *)
1148 let step_val =
1149 match step with
1150 | Some step -> codegen_expr step
1151 (* If not specified, use 1.0. *)
1152 | None -> const_float double_type 1.0
1153 in
1154
1155 let next_var = build_add variable step_val "nextvar" builder in
1156
1157 (* Compute the end condition. *)
1158 let end_cond = codegen_expr end_ in
1159
1160 (* Convert condition to a bool by comparing equal to 0.0. *)
1161 let zero = const_float double_type 0.0 in
1162 let end_cond = build_fcmp Fcmp.One end_cond zero "loopcond" builder in
1163
1164 (* Create the "after loop" block and insert it. *)
1165 let loop_end_bb = insertion_block builder in
1166 let after_bb = append_block context "afterloop" the_function in
1167
1168 (* Insert the conditional branch into the end of loop_end_bb. *)
1169 ignore (build_cond_br end_cond loop_bb after_bb builder);
1170
1171 (* Any new code will be inserted in after_bb. *)
1172 position_at_end after_bb builder;
1173
1174 (* Add a new entry to the PHI node for the backedge. *)
1175 add_incoming (next_var, loop_end_bb) variable;
1176
1177 (* Restore the unshadowed variable. *)
1178 begin match old_val with
1179 | Some old_val -> Hashtbl.add named_values var_name old_val
1180 | None -> ()
1181 end;
1182
1183 (* for expr always returns 0.0. *)
1184 const_null double_type
1185
1186 let codegen_proto = function
1187 | Ast.Prototype (name, args) ->
1188 (* Make the function type: double(double,double) etc. *)
1189 let doubles = Array.make (Array.length args) double_type in
1190 let ft = function_type double_type doubles in
1191 let f =
1192 match lookup_function name the_module with
1193 | None -> declare_function name ft the_module
1194
1195 (* If 'f' conflicted, there was already something named 'name'. If it
1196 * has a body, don't allow redefinition or reextern. *)
1197 | Some f ->
1198 (* If 'f' already has a body, reject this. *)
1199 if block_begin f <> At_end f then
1200 raise (Error "redefinition of function");
1201
1202 (* If 'f' took a different number of arguments, reject. *)
1203 if element_type (type_of f) <> ft then
1204 raise (Error "redefinition of function with different # args");
1205 f
1206 in
1207
1208 (* Set names for all arguments. *)
1209 Array.iteri (fun i a ->
1210 let n = args.(i) in
1211 set_value_name n a;
1212 Hashtbl.add named_values n a;
1213 ) (params f);
1214 f
1215
1216 let codegen_func the_fpm = function
1217 | Ast.Function (proto, body) ->
1218 Hashtbl.clear named_values;
1219 let the_function = codegen_proto proto in
1220
1221 (* Create a new basic block to start insertion into. *)
1222 let bb = append_block context "entry" the_function in
1223 position_at_end bb builder;
1224
1225 try
1226 let ret_val = codegen_expr body in
1227
1228 (* Finish off the function. *)
1229 let _ = build_ret ret_val builder in
1230
1231 (* Validate the generated code, checking for consistency. *)
1232 Llvm_analysis.assert_valid_function the_function;
1233
1234 (* Optimize the function. *)
1235 let _ = PassManager.run_function the_function the_fpm in
1236
1237 the_function
1238 with e ->
1239 delete_function the_function;
1240 raise e
1241
1242toplevel.ml:
1243 .. code-block:: ocaml
1244
1245 (*===----------------------------------------------------------------------===
1246 * Top-Level parsing and JIT Driver
1247 *===----------------------------------------------------------------------===*)
1248
1249 open Llvm
1250 open Llvm_executionengine
1251
1252 (* top ::= definition | external | expression | ';' *)
1253 let rec main_loop the_fpm the_execution_engine stream =
1254 match Stream.peek stream with
1255 | None -> ()
1256
1257 (* ignore top-level semicolons. *)
1258 | Some (Token.Kwd ';') ->
1259 Stream.junk stream;
1260 main_loop the_fpm the_execution_engine stream
1261
1262 | Some token ->
1263 begin
1264 try match token with
1265 | Token.Def ->
1266 let e = Parser.parse_definition stream in
1267 print_endline "parsed a function definition.";
1268 dump_value (Codegen.codegen_func the_fpm e);
1269 | Token.Extern ->
1270 let e = Parser.parse_extern stream in
1271 print_endline "parsed an extern.";
1272 dump_value (Codegen.codegen_proto e);
1273 | _ ->
1274 (* Evaluate a top-level expression into an anonymous function. *)
1275 let e = Parser.parse_toplevel stream in
1276 print_endline "parsed a top-level expr";
1277 let the_function = Codegen.codegen_func the_fpm e in
1278 dump_value the_function;
1279
1280 (* JIT the function, returning a function pointer. *)
1281 let result = ExecutionEngine.run_function the_function [||]
1282 the_execution_engine in
1283
1284 print_string "Evaluated to ";
1285 print_float (GenericValue.as_float Codegen.double_type result);
1286 print_newline ();
1287 with Stream.Error s | Codegen.Error s ->
1288 (* Skip token for error recovery. *)
1289 Stream.junk stream;
1290 print_endline s;
1291 end;
1292 print_string "ready> "; flush stdout;
1293 main_loop the_fpm the_execution_engine stream
1294
1295toy.ml:
1296 .. code-block:: ocaml
1297
1298 (*===----------------------------------------------------------------------===
1299 * Main driver code.
1300 *===----------------------------------------------------------------------===*)
1301
1302 open Llvm
1303 open Llvm_executionengine
1304 open Llvm_target
1305 open Llvm_scalar_opts
1306
1307 let main () =
1308 ignore (initialize_native_target ());
1309
1310 (* Install standard binary operators.
1311 * 1 is the lowest precedence. *)
1312 Hashtbl.add Parser.binop_precedence '<' 10;
1313 Hashtbl.add Parser.binop_precedence '+' 20;
1314 Hashtbl.add Parser.binop_precedence '-' 20;
1315 Hashtbl.add Parser.binop_precedence '*' 40; (* highest. *)
1316
1317 (* Prime the first token. *)
1318 print_string "ready> "; flush stdout;
1319 let stream = Lexer.lex (Stream.of_channel stdin) in
1320
1321 (* Create the JIT. *)
1322 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
1323 let the_fpm = PassManager.create_function Codegen.the_module in
1324
1325 (* Set up the optimizer pipeline. Start with registering info about how the
1326 * target lays out data structures. *)
1327 DataLayout.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
1328
1329 (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
1330 add_instruction_combination the_fpm;
1331
1332 (* reassociate expressions. *)
1333 add_reassociation the_fpm;
1334
1335 (* Eliminate Common SubExpressions. *)
1336 add_gvn the_fpm;
1337
1338 (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
1339 add_cfg_simplification the_fpm;
1340
1341 ignore (PassManager.initialize the_fpm);
1342
1343 (* Run the main "interpreter loop" now. *)
1344 Toplevel.main_loop the_fpm the_execution_engine stream;
1345
1346 (* Print out all the generated code. *)
1347 dump_module Codegen.the_module
1348 ;;
1349
1350 main ()
1351
1352bindings.c
1353 .. code-block:: c
1354
1355 #include <stdio.h>
1356
1357 /* putchard - putchar that takes a double and returns 0. */
1358 extern double putchard(double X) {
1359 putchar((char)X);
1360 return 0;
1361 }
1362
1363`Next: Extending the language: user-defined
1364operators <OCamlLangImpl6.html>`_
1365