1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
6 <title>Kaleidoscope: Extending the Language: Mutable Variables / SSA
8 <meta http-equiv=
"Content-Type" content=
"text/html; charset=utf-8">
9 <meta name=
"author" content=
"Chris Lattner">
10 <meta name=
"author" content=
"Erick Tryzelaar">
11 <link rel=
"stylesheet" href=
"../llvm.css" type=
"text/css">
16 <h1>Kaleidoscope: Extending the Language: Mutable Variables
</h1>
19 <li><a href=
"index.html">Up to Tutorial Index
</a></li>
22 <li><a href=
"#intro">Chapter
7 Introduction
</a></li>
23 <li><a href=
"#why">Why is this a hard problem?
</a></li>
24 <li><a href=
"#memory">Memory in LLVM
</a></li>
25 <li><a href=
"#kalvars">Mutable Variables in Kaleidoscope
</a></li>
26 <li><a href=
"#adjustments">Adjusting Existing Variables for
28 <li><a href=
"#assignment">New Assignment Operator
</a></li>
29 <li><a href=
"#localvars">User-defined Local Variables
</a></li>
30 <li><a href=
"#code">Full Code Listing
</a></li>
33 <li><a href=
"OCamlLangImpl8.html">Chapter
8</a>: Conclusion and other useful LLVM
37 <div class=
"doc_author">
39 Written by
<a href=
"mailto:sabre@nondot.org">Chris Lattner
</a>
40 and
<a href=
"mailto:idadesub@users.sourceforge.net">Erick Tryzelaar
</a>
44 <!-- *********************************************************************** -->
45 <h2><a name=
"intro">Chapter
7 Introduction
</a></h2>
46 <!-- *********************************************************************** -->
50 <p>Welcome to Chapter
7 of the
"<a href="index.html
">Implementing a language
51 with LLVM</a>" tutorial. In chapters
1 through
6, we've built a very
52 respectable, albeit simple,
<a
53 href=
"http://en.wikipedia.org/wiki/Functional_programming">functional
54 programming language
</a>. In our journey, we learned some parsing techniques,
55 how to build and represent an AST, how to build LLVM IR, and how to optimize
56 the resultant code as well as JIT compile it.
</p>
58 <p>While Kaleidoscope is interesting as a functional language, the fact that it
59 is functional makes it
"too easy" to generate LLVM IR for it. In particular, a
60 functional language makes it very easy to build LLVM IR directly in
<a
61 href=
"http://en.wikipedia.org/wiki/Static_single_assignment_form">SSA form
</a>.
62 Since LLVM requires that the input code be in SSA form, this is a very nice
63 property and it is often unclear to newcomers how to generate code for an
64 imperative language with mutable variables.
</p>
66 <p>The short (and happy) summary of this chapter is that there is no need for
67 your front-end to build SSA form: LLVM provides highly tuned and well tested
68 support for this, though the way it works is a bit unexpected for some.
</p>
72 <!-- *********************************************************************** -->
73 <h2><a name=
"why">Why is this a hard problem?
</a></h2>
74 <!-- *********************************************************************** -->
79 To understand why mutable variables cause complexities in SSA construction,
80 consider this extremely simple C example:
83 <div class=
"doc_code">
86 int test(_Bool Condition) {
97 <p>In this case, we have the variable
"X", whose value depends on the path
98 executed in the program. Because there are two different possible values for X
99 before the return instruction, a PHI node is inserted to merge the two values.
100 The LLVM IR that we want for this example looks like this:
</p>
102 <div class=
"doc_code">
104 @G = weak global i32
0 ; type of @G is i32*
105 @H = weak global i32
0 ; type of @H is i32*
107 define i32 @test(i1 %Condition) {
109 br i1 %Condition, label %cond_true, label %cond_false
120 %X
.2 = phi i32 [ %X
.1, %cond_false ], [ %X
.0, %cond_true ]
126 <p>In this example, the loads from the G and H global variables are explicit in
127 the LLVM IR, and they live in the then/else branches of the if statement
128 (cond_true/cond_false). In order to merge the incoming values, the X
.2 phi node
129 in the cond_next block selects the right value to use based on where control
130 flow is coming from: if control flow comes from the cond_false block, X
.2 gets
131 the value of X
.1. Alternatively, if control flow comes from cond_true, it gets
132 the value of X
.0. The intent of this chapter is not to explain the details of
133 SSA form. For more information, see one of the many
<a
134 href=
"http://en.wikipedia.org/wiki/Static_single_assignment_form">online
137 <p>The question for this article is
"who places the phi nodes when lowering
138 assignments to mutable variables?". The issue here is that LLVM
139 <em>requires
</em> that its IR be in SSA form: there is no
"non-ssa" mode for it.
140 However, SSA construction requires non-trivial algorithms and data structures,
141 so it is inconvenient and wasteful for every front-end to have to reproduce this
146 <!-- *********************************************************************** -->
147 <h2><a name=
"memory">Memory in LLVM
</a></h2>
148 <!-- *********************************************************************** -->
152 <p>The 'trick' here is that while LLVM does require all register values to be
153 in SSA form, it does not require (or permit) memory objects to be in SSA form.
154 In the example above, note that the loads from G and H are direct accesses to
155 G and H: they are not renamed or versioned. This differs from some other
156 compiler systems, which do try to version memory objects. In LLVM, instead of
157 encoding dataflow analysis of memory into the LLVM IR, it is handled with
<a
158 href=
"../WritingAnLLVMPass.html">Analysis Passes
</a> which are computed on
162 With this in mind, the high-level idea is that we want to make a stack variable
163 (which lives in memory, because it is on the stack) for each mutable object in
164 a function. To take advantage of this trick, we need to talk about how LLVM
165 represents stack variables.
168 <p>In LLVM, all memory accesses are explicit with load/store instructions, and
169 it is carefully designed not to have (or need) an
"address-of" operator. Notice
170 how the type of the @G/@H global variables is actually
"i32*" even though the
171 variable is defined as
"i32". What this means is that @G defines
<em>space
</em>
172 for an i32 in the global data area, but its
<em>name
</em> actually refers to the
173 address for that space. Stack variables work the same way, except that instead of
174 being declared with global variable definitions, they are declared with the
175 <a href=
"../LangRef.html#i_alloca">LLVM alloca instruction
</a>:
</p>
177 <div class=
"doc_code">
179 define i32 @example() {
181 %X = alloca i32 ; type of %X is i32*.
183 %tmp = load i32* %X ; load the stack value %X from the stack.
184 %tmp2 = add i32 %tmp,
1 ; increment it
185 store i32 %tmp2, i32* %X ; store it back
190 <p>This code shows an example of how you can declare and manipulate a stack
191 variable in the LLVM IR. Stack memory allocated with the alloca instruction is
192 fully general: you can pass the address of the stack slot to functions, you can
193 store it in other variables, etc. In our example above, we could rewrite the
194 example to use the alloca technique to avoid using a PHI node:
</p>
196 <div class=
"doc_code">
198 @G = weak global i32
0 ; type of @G is i32*
199 @H = weak global i32
0 ; type of @H is i32*
201 define i32 @test(i1 %Condition) {
203 %X = alloca i32 ; type of %X is i32*.
204 br i1 %Condition, label %cond_true, label %cond_false
208 store i32 %X
.0, i32* %X ; Update X
213 store i32 %X
.1, i32* %X ; Update X
217 %X
.2 = load i32* %X ; Read X
223 <p>With this, we have discovered a way to handle arbitrary mutable variables
224 without the need to create Phi nodes at all:
</p>
227 <li>Each mutable variable becomes a stack allocation.
</li>
228 <li>Each read of the variable becomes a load from the stack.
</li>
229 <li>Each update of the variable becomes a store to the stack.
</li>
230 <li>Taking the address of a variable just uses the stack address directly.
</li>
233 <p>While this solution has solved our immediate problem, it introduced another
234 one: we have now apparently introduced a lot of stack traffic for very simple
235 and common operations, a major performance problem. Fortunately for us, the
236 LLVM optimizer has a highly-tuned optimization pass named
"mem2reg" that handles
237 this case, promoting allocas like this into SSA registers, inserting Phi nodes
238 as appropriate. If you run this example through the pass, for example, you'll
241 <div class=
"doc_code">
243 $
<b>llvm-as
< example.ll | opt -mem2reg | llvm-dis
</b>
244 @G = weak global i32
0
245 @H = weak global i32
0
247 define i32 @test(i1 %Condition) {
249 br i1 %Condition, label %cond_true, label %cond_false
260 %X
.01 = phi i32 [ %X
.1, %cond_false ], [ %X
.0, %cond_true ]
266 <p>The mem2reg pass implements the standard
"iterated dominance frontier"
267 algorithm for constructing SSA form and has a number of optimizations that speed
268 up (very common) degenerate cases. The mem2reg optimization pass is the answer
269 to dealing with mutable variables, and we highly recommend that you depend on
270 it. Note that mem2reg only works on variables in certain circumstances:
</p>
273 <li>mem2reg is alloca-driven: it looks for allocas and if it can handle them, it
274 promotes them. It does not apply to global variables or heap allocations.
</li>
276 <li>mem2reg only looks for alloca instructions in the entry block of the
277 function. Being in the entry block guarantees that the alloca is only executed
278 once, which makes analysis simpler.
</li>
280 <li>mem2reg only promotes allocas whose uses are direct loads and stores. If
281 the address of the stack object is passed to a function, or if any funny pointer
282 arithmetic is involved, the alloca will not be promoted.
</li>
284 <li>mem2reg only works on allocas of
<a
285 href=
"../LangRef.html#t_classifications">first class
</a>
286 values (such as pointers, scalars and vectors), and only if the array size
287 of the allocation is
1 (or missing in the .ll file). mem2reg is not capable of
288 promoting structs or arrays to registers. Note that the
"scalarrepl" pass is
289 more powerful and can promote structs,
"unions", and arrays in many cases.
</li>
294 All of these properties are easy to satisfy for most imperative languages, and
295 we'll illustrate it below with Kaleidoscope. The final question you may be
296 asking is: should I bother with this nonsense for my front-end? Wouldn't it be
297 better if I just did SSA construction directly, avoiding use of the mem2reg
298 optimization pass? In short, we strongly recommend that you use this technique
299 for building SSA form, unless there is an extremely good reason not to. Using
300 this technique is:
</p>
303 <li>Proven and well tested: llvm-gcc and clang both use this technique for local
304 mutable variables. As such, the most common clients of LLVM are using this to
305 handle a bulk of their variables. You can be sure that bugs are found fast and
308 <li>Extremely Fast: mem2reg has a number of special cases that make it fast in
309 common cases as well as fully general. For example, it has fast-paths for
310 variables that are only used in a single block, variables that only have one
311 assignment point, good heuristics to avoid insertion of unneeded phi nodes, etc.
314 <li>Needed for debug info generation:
<a href=
"../SourceLevelDebugging.html">
315 Debug information in LLVM
</a> relies on having the address of the variable
316 exposed so that debug info can be attached to it. This technique dovetails
317 very naturally with this style of debug info.
</li>
320 <p>If nothing else, this makes it much easier to get your front-end up and
321 running, and is very simple to implement. Lets extend Kaleidoscope with mutable
327 <!-- *********************************************************************** -->
328 <h2><a name=
"kalvars">Mutable Variables in Kaleidoscope
</a></h2>
329 <!-- *********************************************************************** -->
333 <p>Now that we know the sort of problem we want to tackle, lets see what this
334 looks like in the context of our little Kaleidoscope language. We're going to
335 add two features:
</p>
338 <li>The ability to mutate variables with the '=' operator.
</li>
339 <li>The ability to define new variables.
</li>
342 <p>While the first item is really what this is about, we only have variables
343 for incoming arguments as well as for induction variables, and redefining those only
344 goes so far :). Also, the ability to define new variables is a
345 useful thing regardless of whether you will be mutating them. Here's a
346 motivating example that shows how we could use these:
</p>
348 <div class=
"doc_code">
350 # Define ':' for sequencing: as a low-precedence operator that ignores operands
351 # and just returns the RHS.
352 def binary :
1 (x y) y;
354 # Recursive fib, we could do this before.
363 <b>var a =
1, b =
1, c in
</b>
364 (for i =
3, i
< x in
376 In order to mutate variables, we have to change our existing variables to use
377 the
"alloca trick". Once we have that, we'll add our new operator, then extend
378 Kaleidoscope to support new variable definitions.
383 <!-- *********************************************************************** -->
384 <h2><a name=
"adjustments">Adjusting Existing Variables for Mutation
</a></h2>
385 <!-- *********************************************************************** -->
390 The symbol table in Kaleidoscope is managed at code generation time by the
391 '
<tt>named_values
</tt>' map. This map currently keeps track of the LLVM
392 "Value*" that holds the double value for the named variable. In order to
393 support mutation, we need to change this slightly, so that it
394 <tt>named_values
</tt> holds the
<em>memory location
</em> of the variable in
395 question. Note that this change is a refactoring: it changes the structure of
396 the code, but does not (by itself) change the behavior of the compiler. All of
397 these changes are isolated in the Kaleidoscope code generator.
</p>
400 At this point in Kaleidoscope's development, it only supports variables for two
401 things: incoming arguments to functions and the induction variable of 'for'
402 loops. For consistency, we'll allow mutation of these variables in addition to
403 other user-defined variables. This means that these will both need memory
407 <p>To start our transformation of Kaleidoscope, we'll change the
408 <tt>named_values
</tt> map so that it maps to AllocaInst* instead of Value*.
409 Once we do this, the C++ compiler will tell us what parts of the code we need to
412 <p><b>Note:
</b> the ocaml bindings currently model both
<tt>Value*
</tt>s and
413 <tt>AllocInst*
</tt>s as
<tt>Llvm.llvalue
</tt>s, but this may change in the
414 future to be more type safe.
</p>
416 <div class=
"doc_code">
418 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create
10
422 <p>Also, since we will need to create these alloca's, we'll use a helper
423 function that ensures that the allocas are created in the entry block of the
426 <div class=
"doc_code">
428 (* Create an alloca instruction in the entry block of the function. This
429 * is used for mutable variables etc. *)
430 let create_entry_block_alloca the_function var_name =
431 let builder = builder_at (instr_begin (entry_block the_function)) in
432 build_alloca double_type var_name builder
436 <p>This funny looking code creates an
<tt>Llvm.llbuilder
</tt> object that is
437 pointing at the first instruction of the entry block. It then creates an alloca
438 with the expected name and returns it. Because all values in Kaleidoscope are
439 doubles, there is no need to pass in a type to use.
</p>
441 <p>With this in place, the first functionality change we want to make is to
442 variable references. In our new scheme, variables live on the stack, so code
443 generating a reference to them actually needs to produce a load from the stack
446 <div class=
"doc_code">
448 let rec codegen_expr = function
450 | Ast.Variable name -
>
451 let v = try Hashtbl.find named_values name with
452 | Not_found -
> raise (Error
"unknown variable name")
454 <b>(* Load the value. *)
455 build_load v name builder
</b>
459 <p>As you can see, this is pretty straightforward. Now we need to update the
460 things that define the variables to set up the alloca. We'll start with
461 <tt>codegen_expr Ast.For ...
</tt> (see the
<a href=
"#code">full code listing
</a>
462 for the unabridged code):
</p>
464 <div class=
"doc_code">
466 | Ast.For (var_name, start, end_, step, body) -
>
467 let the_function = block_parent (insertion_block builder) in
469 (* Create an alloca for the variable in the entry block. *)
470 <b>let alloca = create_entry_block_alloca the_function var_name in
</b>
472 (* Emit the start code first, without 'variable' in scope. *)
473 let start_val = codegen_expr start in
475 <b>(* Store the value into the alloca. *)
476 ignore(build_store start_val alloca builder);
</b>
480 (* Within the loop, the variable is defined equal to the PHI node. If it
481 * shadows an existing variable, we have to restore it, so save it
484 try Some (Hashtbl.find named_values var_name) with Not_found -
> None
486 <b>Hashtbl.add named_values var_name alloca;
</b>
490 (* Compute the end condition. *)
491 let end_cond = codegen_expr end_ in
493 <b>(* Reload, increment, and restore the alloca. This handles the case where
494 * the body of the loop mutates the variable. *)
495 let cur_var = build_load alloca var_name builder in
496 let next_var = build_add cur_var step_val
"nextvar" builder in
497 ignore(build_store next_var alloca builder);
</b>
502 <p>This code is virtually identical to the code
<a
503 href=
"OCamlLangImpl5.html#forcodegen">before we allowed mutable variables
</a>.
504 The big difference is that we no longer have to construct a PHI node, and we use
505 load/store to access the variable as needed.
</p>
507 <p>To support mutable argument variables, we need to also make allocas for them.
508 The code for this is also pretty simple:
</p>
510 <div class=
"doc_code">
512 (* Create an alloca for each argument and register the argument in the symbol
513 * table so that references to it will succeed. *)
514 let create_argument_allocas the_function proto =
515 let args = match proto with
516 | Ast.Prototype (_, args) | Ast.BinOpPrototype (_, args, _) -
> args
518 Array.iteri (fun i ai -
>
519 let var_name = args.(i) in
520 (* Create an alloca for this variable. *)
521 let alloca = create_entry_block_alloca the_function var_name in
523 (* Store the initial value into the alloca. *)
524 ignore(build_store ai alloca builder);
526 (* Add arguments to variable symbol table. *)
527 Hashtbl.add named_values var_name alloca;
528 ) (params the_function)
532 <p>For each argument, we make an alloca, store the input value to the function
533 into the alloca, and register the alloca as the memory location for the
534 argument. This method gets invoked by
<tt>Codegen.codegen_func
</tt> right after
535 it sets up the entry block for the function.
</p>
537 <p>The final missing piece is adding the mem2reg pass, which allows us to get
538 good codegen once again:
</p>
540 <div class=
"doc_code">
544 let the_fpm = PassManager.create_function Codegen.the_module in
546 (* Set up the optimizer pipeline. Start with registering info about how the
547 * target lays out data structures. *)
548 TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
550 <b>(* Promote allocas to registers. *)
551 add_memory_to_register_promotion the_fpm;
</b>
553 (* Do simple
"peephole" optimizations and bit-twiddling optzn. *)
554 add_instruction_combining the_fpm;
556 (* reassociate expressions. *)
557 add_reassociation the_fpm;
561 <p>It is interesting to see what the code looks like before and after the
562 mem2reg optimization runs. For example, this is the before/after code for our
563 recursive fib function. Before the optimization:
</p>
565 <div class=
"doc_code">
567 define double @fib(double %x) {
569 <b>%x1 = alloca double
570 store double %x, double* %x1
571 %x2 = load double* %x1
</b>
572 %cmptmp = fcmp ult double %x2,
3.000000e+00
573 %booltmp = uitofp i1 %cmptmp to double
574 %ifcond = fcmp one double %booltmp,
0.000000e+00
575 br i1 %ifcond, label %then, label %else
577 then: ; preds = %entry
580 else: ; preds = %entry
581 <b>%x3 = load double* %x1
</b>
582 %subtmp = fsub double %x3,
1.000000e+00
583 %calltmp = call double @fib(double %subtmp)
584 <b>%x4 = load double* %x1
</b>
585 %subtmp5 = fsub double %x4,
2.000000e+00
586 %calltmp6 = call double @fib(double %subtmp5)
587 %addtmp = fadd double %calltmp, %calltmp6
590 ifcont: ; preds = %else, %then
591 %iftmp = phi double [
1.000000e+00, %then ], [ %addtmp, %else ]
597 <p>Here there is only one variable (x, the input argument) but you can still
598 see the extremely simple-minded code generation strategy we are using. In the
599 entry block, an alloca is created, and the initial input value is stored into
600 it. Each reference to the variable does a reload from the stack. Also, note
601 that we didn't modify the if/then/else expression, so it still inserts a PHI
602 node. While we could make an alloca for it, it is actually easier to create a
603 PHI node for it, so we still just make the PHI.
</p>
605 <p>Here is the code after the mem2reg pass runs:
</p>
607 <div class=
"doc_code">
609 define double @fib(double %x) {
611 %cmptmp = fcmp ult double
<b>%x
</b>,
3.000000e+00
612 %booltmp = uitofp i1 %cmptmp to double
613 %ifcond = fcmp one double %booltmp,
0.000000e+00
614 br i1 %ifcond, label %then, label %else
620 %subtmp = fsub double
<b>%x
</b>,
1.000000e+00
621 %calltmp = call double @fib(double %subtmp)
622 %subtmp5 = fsub double
<b>%x
</b>,
2.000000e+00
623 %calltmp6 = call double @fib(double %subtmp5)
624 %addtmp = fadd double %calltmp, %calltmp6
627 ifcont: ; preds = %else, %then
628 %iftmp = phi double [
1.000000e+00, %then ], [ %addtmp, %else ]
634 <p>This is a trivial case for mem2reg, since there are no redefinitions of the
635 variable. The point of showing this is to calm your tension about inserting
636 such blatent inefficiencies :).
</p>
638 <p>After the rest of the optimizers run, we get:
</p>
640 <div class=
"doc_code">
642 define double @fib(double %x) {
644 %cmptmp = fcmp ult double %x,
3.000000e+00
645 %booltmp = uitofp i1 %cmptmp to double
646 %ifcond = fcmp ueq double %booltmp,
0.000000e+00
647 br i1 %ifcond, label %else, label %ifcont
650 %subtmp = fsub double %x,
1.000000e+00
651 %calltmp = call double @fib(double %subtmp)
652 %subtmp5 = fsub double %x,
2.000000e+00
653 %calltmp6 = call double @fib(double %subtmp5)
654 %addtmp = fadd double %calltmp, %calltmp6
658 ret double
1.000000e+00
663 <p>Here we see that the simplifycfg pass decided to clone the return instruction
664 into the end of the 'else' block. This allowed it to eliminate some branches
665 and the PHI node.
</p>
667 <p>Now that all symbol table references are updated to use stack variables,
668 we'll add the assignment operator.
</p>
672 <!-- *********************************************************************** -->
673 <h2><a name=
"assignment">New Assignment Operator
</a></h2>
674 <!-- *********************************************************************** -->
678 <p>With our current framework, adding a new assignment operator is really
679 simple. We will parse it just like any other binary operator, but handle it
680 internally (instead of allowing the user to define it). The first step is to
681 set a precedence:
</p>
683 <div class=
"doc_code">
686 (* Install standard binary operators.
687 *
1 is the lowest precedence. *)
688 <b>Hashtbl.add Parser.binop_precedence '='
2;
</b>
689 Hashtbl.add Parser.binop_precedence '
<'
10;
690 Hashtbl.add Parser.binop_precedence '+'
20;
691 Hashtbl.add Parser.binop_precedence '-'
20;
696 <p>Now that the parser knows the precedence of the binary operator, it takes
697 care of all the parsing and AST generation. We just need to implement codegen
698 for the assignment operator. This looks like:
</p>
700 <div class=
"doc_code">
702 let rec codegen_expr = function
705 (* Special case '=' because we don't want to emit the LHS as an
709 | Ast.Variable name -
> name
710 | _ -
> raise (Error
"destination of '=' must be a variable")
715 <p>Unlike the rest of the binary operators, our assignment operator doesn't
716 follow the
"emit LHS, emit RHS, do computation" model. As such, it is handled
717 as a special case before the other binary operators are handled. The other
718 strange thing is that it requires the LHS to be a variable. It is invalid to
719 have
"(x+1) = expr" - only things like
"x = expr" are allowed.
723 <div class=
"doc_code">
725 (* Codegen the rhs. *)
726 let val_ = codegen_expr rhs in
728 (* Lookup the name. *)
729 let variable = try Hashtbl.find named_values name with
730 | Not_found -
> raise (Error
"unknown variable name")
732 ignore(build_store val_ variable builder);
739 <p>Once we have the variable, codegen'ing the assignment is straightforward:
740 we emit the RHS of the assignment, create a store, and return the computed
741 value. Returning a value allows for chained assignments like
"X = (Y = Z)".
</p>
743 <p>Now that we have an assignment operator, we can mutate loop variables and
744 arguments. For example, we can now run code like this:
</p>
746 <div class=
"doc_code">
748 # Function to print a double.
751 # Define ':' for sequencing: as a low-precedence operator that ignores operands
752 # and just returns the RHS.
753 def binary :
1 (x y) y;
764 <p>When run, this example prints
"123" and then
"4", showing that we did
765 actually mutate the value! Okay, we have now officially implemented our goal:
766 getting this to work requires SSA construction in the general case. However,
767 to be really useful, we want the ability to define our own local variables, lets
773 <!-- *********************************************************************** -->
774 <h2><a name=
"localvars">User-defined Local Variables
</a></h2>
775 <!-- *********************************************************************** -->
779 <p>Adding var/in is just like any other other extensions we made to
780 Kaleidoscope: we extend the lexer, the parser, the AST and the code generator.
781 The first step for adding our new 'var/in' construct is to extend the lexer.
782 As before, this is pretty trivial, the code looks like this:
</p>
784 <div class=
"doc_code">
788 <b>(* var definition *)
793 and lex_ident buffer = parser
795 |
"in" -
> [
< 'Token.In; stream
>]
796 |
"binary" -
> [
< 'Token.Binary; stream
>]
797 |
"unary" -
> [
< 'Token.Unary; stream
>]
798 <b>|
"var" -
> [
< 'Token.Var; stream
>]
</b>
803 <p>The next step is to define the AST node that we will construct. For var/in,
804 it looks like this:
</p>
806 <div class=
"doc_code">
810 (* variant for var/in. *)
811 | Var of (string * expr option) array * expr
816 <p>var/in allows a list of names to be defined all at once, and each name can
817 optionally have an initializer value. As such, we capture this information in
818 the VarNames vector. Also, var/in has a body, this body is allowed to access
819 the variables defined by the var/in.
</p>
821 <p>With this in place, we can define the parser pieces. The first thing we do
822 is add it as a primary expression:
</p>
824 <div class=
"doc_code">
832 <b>* ::= varexpr
</b> *)
833 let rec parse_primary = parser
836 * ::= 'var' identifier ('=' expression?
837 * (',' identifier ('=' expression)?)* 'in' expression *)
839 (* At least one variable name is required. *)
840 'Token.Ident id ??
"expected identifier after var";
842 var_names=parse_var_names [(id, init)];
843 (* At this point, we have to have 'in'. *)
844 'Token.In ??
"expected 'in' keyword after 'var'";
845 body=parse_expr
>] -
>
846 Ast.Var (Array.of_list (List.rev var_names), body)
</b>
850 and parse_var_init = parser
851 (* read in the optional initializer. *)
852 | [
< 'Token.Kwd '='; e=parse_expr
>] -
> Some e
853 | [
< >] -
> None
855 and parse_var_names accumulator = parser
856 | [
< 'Token.Kwd ',';
857 'Token.Ident id ??
"expected identifier list after var";
859 e=parse_var_names ((id, init) :: accumulator)
>] -
> e
860 | [
< >] -
> accumulator
864 <p>Now that we can parse and represent the code, we need to support emission of
865 LLVM IR for it. This code starts out with:
</p>
867 <div class=
"doc_code">
869 let rec codegen_expr = function
871 | Ast.Var (var_names, body)
872 let old_bindings = ref [] in
874 let the_function = block_parent (insertion_block builder) in
876 (* Register all variables and emit their initializer. *)
877 Array.iter (fun (var_name, init) -
>
881 <p>Basically it loops over all the variables, installing them one at a time.
882 For each variable we put into the symbol table, we remember the previous value
883 that we replace in OldBindings.
</p>
885 <div class=
"doc_code">
887 (* Emit the initializer before adding the variable to scope, this
888 * prevents the initializer from referencing the variable itself, and
889 * permits stuff like this:
891 * var a = a in ... # refers to outer 'a'. *)
894 | Some init -
> codegen_expr init
895 (* If not specified, use
0.0. *)
896 | None -
> const_float double_type
0.0
899 let alloca = create_entry_block_alloca the_function var_name in
900 ignore(build_store init_val alloca builder);
902 (* Remember the old variable binding so that we can restore the binding
903 * when we unrecurse. *)
907 let old_value = Hashtbl.find named_values var_name in
908 old_bindings := (var_name, old_value) :: !old_bindings;
909 with Not_found
> ()
912 (* Remember this binding. *)
913 Hashtbl.add named_values var_name alloca;
918 <p>There are more comments here than code. The basic idea is that we emit the
919 initializer, create the alloca, then update the symbol table to point to it.
920 Once all the variables are installed in the symbol table, we evaluate the body
921 of the var/in expression:
</p>
923 <div class=
"doc_code">
925 (* Codegen the body, now that all vars are in scope. *)
926 let body_val = codegen_expr body in
930 <p>Finally, before returning, we restore the previous variable bindings:
</p>
932 <div class=
"doc_code">
934 (* Pop all our variables from scope. *)
935 List.iter (fun (var_name, old_value) -
>
936 Hashtbl.add named_values var_name old_value
939 (* Return the body computation. *)
944 <p>The end result of all of this is that we get properly scoped variable
945 definitions, and we even (trivially) allow mutation of them :).
</p>
947 <p>With this, we completed what we set out to do. Our nice iterative fib
948 example from the intro compiles and runs just fine. The mem2reg pass optimizes
949 all of our stack variables into SSA registers, inserting PHI nodes where needed,
950 and our front-end remains simple: no
"iterated dominance frontier" computation
951 anywhere in sight.
</p>
955 <!-- *********************************************************************** -->
956 <h2><a name=
"code">Full Code Listing
</a></h2>
957 <!-- *********************************************************************** -->
962 Here is the complete code listing for our running example, enhanced with mutable
963 variables and var/in support. To build this example, use:
966 <div class=
"doc_code">
975 <p>Here is the code:
</p>
979 <dd class=
"doc_code">
981 <{lexer,parser}.ml
>: use_camlp4, pp(camlp4of)
982 <*.{byte,native}
>: g++, use_llvm, use_llvm_analysis
983 <*.{byte,native}
>: use_llvm_executionengine, use_llvm_target
984 <*.{byte,native}
>: use_llvm_scalar_opts, use_bindings
988 <dt>myocamlbuild.ml:
</dt>
989 <dd class=
"doc_code">
991 open Ocamlbuild_plugin;;
993 ocaml_lib ~extern:true
"llvm";;
994 ocaml_lib ~extern:true
"llvm_analysis";;
995 ocaml_lib ~extern:true
"llvm_executionengine";;
996 ocaml_lib ~extern:true
"llvm_target";;
997 ocaml_lib ~extern:true
"llvm_scalar_opts";;
999 flag [
"link";
"ocaml";
"g++"] (S[A
"-cc"; A
"g++"; A
"-cclib"; A
"-rdynamic"]);;
1000 dep [
"link";
"ocaml";
"use_bindings"] [
"bindings.o"];;
1005 <dd class=
"doc_code">
1007 (*===----------------------------------------------------------------------===
1009 *===----------------------------------------------------------------------===*)
1011 (* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of
1012 * these others for known things. *)
1018 | Ident of string | Number of float
1030 (* var definition *)
1036 <dd class=
"doc_code">
1038 (*===----------------------------------------------------------------------===
1040 *===----------------------------------------------------------------------===*)
1042 let rec lex = parser
1043 (* Skip any whitespace. *)
1044 | [
< ' (' ' | '\n' | '\r' | '\t'); stream
>] -
> lex stream
1046 (* identifier: [a-zA-Z][a-zA-Z0-
9] *)
1047 | [
< ' ('A' .. 'Z' | 'a' .. 'z' as c); stream
>] -
>
1048 let buffer = Buffer.create
1 in
1049 Buffer.add_char buffer c;
1050 lex_ident buffer stream
1052 (* number: [
0-
9.]+ *)
1053 | [
< ' ('
0' .. '
9' as c); stream
>] -
>
1054 let buffer = Buffer.create
1 in
1055 Buffer.add_char buffer c;
1056 lex_number buffer stream
1058 (* Comment until end of line. *)
1059 | [
< ' ('#'); stream
>] -
>
1062 (* Otherwise, just return the character as its ascii value. *)
1063 | [
< 'c; stream
>] -
>
1064 [
< 'Token.Kwd c; lex stream
>]
1066 (* end of stream. *)
1067 | [
< >] -
> [
< >]
1069 and lex_number buffer = parser
1070 | [
< ' ('
0' .. '
9' | '.' as c); stream
>] -
>
1071 Buffer.add_char buffer c;
1072 lex_number buffer stream
1073 | [
< stream=lex
>] -
>
1074 [
< 'Token.Number (float_of_string (Buffer.contents buffer)); stream
>]
1076 and lex_ident buffer = parser
1077 | [
< ' ('A' .. 'Z' | 'a' .. 'z' | '
0' .. '
9' as c); stream
>] -
>
1078 Buffer.add_char buffer c;
1079 lex_ident buffer stream
1080 | [
< stream=lex
>] -
>
1081 match Buffer.contents buffer with
1082 |
"def" -
> [
< 'Token.Def; stream
>]
1083 |
"extern" -
> [
< 'Token.Extern; stream
>]
1084 |
"if" -
> [
< 'Token.If; stream
>]
1085 |
"then" -
> [
< 'Token.Then; stream
>]
1086 |
"else" -
> [
< 'Token.Else; stream
>]
1087 |
"for" -
> [
< 'Token.For; stream
>]
1088 |
"in" -
> [
< 'Token.In; stream
>]
1089 |
"binary" -
> [
< 'Token.Binary; stream
>]
1090 |
"unary" -
> [
< 'Token.Unary; stream
>]
1091 |
"var" -
> [
< 'Token.Var; stream
>]
1092 | id -
> [
< 'Token.Ident id; stream
>]
1094 and lex_comment = parser
1095 | [
< ' ('\n'); stream=lex
>] -
> stream
1096 | [
< 'c; e=lex_comment
>] -
> e
1097 | [
< >] -
> [
< >]
1102 <dd class=
"doc_code">
1104 (*===----------------------------------------------------------------------===
1105 * Abstract Syntax Tree (aka Parse Tree)
1106 *===----------------------------------------------------------------------===*)
1108 (* expr - Base type for all expression nodes. *)
1110 (* variant for numeric literals like
"1.0". *)
1113 (* variant for referencing a variable, like
"a". *)
1114 | Variable of string
1116 (* variant for a unary operator. *)
1117 | Unary of char * expr
1119 (* variant for a binary operator. *)
1120 | Binary of char * expr * expr
1122 (* variant for function calls. *)
1123 | Call of string * expr array
1125 (* variant for if/then/else. *)
1126 | If of expr * expr * expr
1128 (* variant for for/in. *)
1129 | For of string * expr * expr * expr option * expr
1131 (* variant for var/in. *)
1132 | Var of (string * expr option) array * expr
1134 (* proto - This type represents the
"prototype" for a function, which captures
1135 * its name, and its argument names (thus implicitly the number of arguments the
1136 * function takes). *)
1138 | Prototype of string * string array
1139 | BinOpPrototype of string * string array * int
1141 (* func - This type represents a function definition itself. *)
1142 type func = Function of proto * expr
1147 <dd class=
"doc_code">
1149 (*===---------------------------------------------------------------------===
1151 *===---------------------------------------------------------------------===*)
1153 (* binop_precedence - This holds the precedence for each binary operator that is
1155 let binop_precedence:(char, int) Hashtbl.t = Hashtbl.create
10
1157 (* precedence - Get the precedence of the pending binary operator token. *)
1158 let precedence c = try Hashtbl.find binop_precedence c with Not_found -
> -
1
1167 let rec parse_primary = parser
1168 (* numberexpr ::= number *)
1169 | [
< 'Token.Number n
>] -
> Ast.Number n
1171 (* parenexpr ::= '(' expression ')' *)
1172 | [
< 'Token.Kwd '('; e=parse_expr; 'Token.Kwd ')' ??
"expected ')'" >] -
> e
1176 * ::= identifier '(' argumentexpr ')' *)
1177 | [
< 'Token.Ident id; stream
>] -
>
1178 let rec parse_args accumulator = parser
1179 | [
< e=parse_expr; stream
>] -
>
1181 | [
< 'Token.Kwd ','; e=parse_args (e :: accumulator)
>] -
> e
1182 | [
< >] -
> e :: accumulator
1184 | [
< >] -
> accumulator
1186 let rec parse_ident id = parser
1188 | [
< 'Token.Kwd '(';
1190 'Token.Kwd ')' ??
"expected ')'">] -
>
1191 Ast.Call (id, Array.of_list (List.rev args))
1193 (* Simple variable ref. *)
1194 | [
< >] -
> Ast.Variable id
1196 parse_ident id stream
1198 (* ifexpr ::= 'if' expr 'then' expr 'else' expr *)
1199 | [
< 'Token.If; c=parse_expr;
1200 'Token.Then ??
"expected 'then'"; t=parse_expr;
1201 'Token.Else ??
"expected 'else'"; e=parse_expr
>] -
>
1205 ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression *)
1207 'Token.Ident id ??
"expected identifier after for";
1208 'Token.Kwd '=' ??
"expected '=' after for";
1213 'Token.Kwd ',' ??
"expected ',' after for";
1218 | [
< 'Token.Kwd ','; step=parse_expr
>] -
> Some step
1219 | [
< >] -
> None
1223 | [
< 'Token.In; body=parse_expr
>] -
>
1224 Ast.For (id, start, end_, step, body)
1226 raise (Stream.Error
"expected 'in' after for")
1229 raise (Stream.Error
"expected '=' after for")
1233 * ::= 'var' identifier ('=' expression?
1234 * (',' identifier ('=' expression)?)* 'in' expression *)
1236 (* At least one variable name is required. *)
1237 'Token.Ident id ??
"expected identifier after var";
1238 init=parse_var_init;
1239 var_names=parse_var_names [(id, init)];
1240 (* At this point, we have to have 'in'. *)
1241 'Token.In ??
"expected 'in' keyword after 'var'";
1242 body=parse_expr
>] -
>
1243 Ast.Var (Array.of_list (List.rev var_names), body)
1245 | [
< >] -
> raise (Stream.Error
"unknown token when expecting an expression.")
1250 and parse_unary = parser
1251 (* If this is a unary operator, read it. *)
1252 | [
< 'Token.Kwd op when op != '('
&& op != ')'; operand=parse_expr
>] -
>
1253 Ast.Unary (op, operand)
1255 (* If the current token is not an operator, it must be a primary expr. *)
1256 | [
< stream
>] -
> parse_primary stream
1259 * ::= ('+' primary)* *)
1260 and parse_bin_rhs expr_prec lhs stream =
1261 match Stream.peek stream with
1262 (* If this is a binop, find its precedence. *)
1263 | Some (Token.Kwd c) when Hashtbl.mem binop_precedence c -
>
1264 let token_prec = precedence c in
1266 (* If this is a binop that binds at least as tightly as the current binop,
1267 * consume it, otherwise we are done. *)
1268 if token_prec
< expr_prec then lhs else begin
1269 (* Eat the binop. *)
1272 (* Parse the primary expression after the binary operator. *)
1273 let rhs = parse_unary stream in
1275 (* Okay, we know this is a binop. *)
1277 match Stream.peek stream with
1278 | Some (Token.Kwd c2) -
>
1279 (* If BinOp binds less tightly with rhs than the operator after
1280 * rhs, let the pending operator take rhs as its lhs. *)
1281 let next_prec = precedence c2 in
1282 if token_prec
< next_prec
1283 then parse_bin_rhs (token_prec +
1) rhs stream
1288 (* Merge lhs/rhs. *)
1289 let lhs = Ast.Binary (c, lhs, rhs) in
1290 parse_bin_rhs expr_prec lhs stream
1294 and parse_var_init = parser
1295 (* read in the optional initializer. *)
1296 | [
< 'Token.Kwd '='; e=parse_expr
>] -
> Some e
1297 | [
< >] -
> None
1299 and parse_var_names accumulator = parser
1300 | [
< 'Token.Kwd ',';
1301 'Token.Ident id ??
"expected identifier list after var";
1302 init=parse_var_init;
1303 e=parse_var_names ((id, init) :: accumulator)
>] -
> e
1304 | [
< >] -
> accumulator
1307 * ::= primary binoprhs *)
1308 and parse_expr = parser
1309 | [
< lhs=parse_unary; stream
>] -
> parse_bin_rhs
0 lhs stream
1312 * ::= id '(' id* ')'
1313 * ::= binary LETTER number? (id, id)
1314 * ::= unary LETTER number? (id) *)
1315 let parse_prototype =
1316 let rec parse_args accumulator = parser
1317 | [
< 'Token.Ident id; e=parse_args (id::accumulator)
>] -
> e
1318 | [
< >] -
> accumulator
1320 let parse_operator = parser
1321 | [
< 'Token.Unary
>] -
> "unary",
1
1322 | [
< 'Token.Binary
>] -
> "binary",
2
1324 let parse_binary_precedence = parser
1325 | [
< 'Token.Number n
>] -
> int_of_float n
1326 | [
< >] -
> 30
1329 | [
< 'Token.Ident id;
1330 'Token.Kwd '(' ??
"expected '(' in prototype";
1332 'Token.Kwd ')' ??
"expected ')' in prototype" >] -
>
1334 Ast.Prototype (id, Array.of_list (List.rev args))
1335 | [
< (prefix, kind)=parse_operator;
1336 'Token.Kwd op ??
"expected an operator";
1337 (* Read the precedence if present. *)
1338 binary_precedence=parse_binary_precedence;
1339 'Token.Kwd '(' ??
"expected '(' in prototype";
1341 'Token.Kwd ')' ??
"expected ')' in prototype" >] -
>
1342 let name = prefix ^ (String.make
1 op) in
1343 let args = Array.of_list (List.rev args) in
1345 (* Verify right number of arguments for operator. *)
1346 if Array.length args != kind
1347 then raise (Stream.Error
"invalid number of operands for operator")
1350 Ast.Prototype (name, args)
1352 Ast.BinOpPrototype (name, args, binary_precedence)
1354 raise (Stream.Error
"expected function name in prototype")
1356 (* definition ::= 'def' prototype expression *)
1357 let parse_definition = parser
1358 | [
< 'Token.Def; p=parse_prototype; e=parse_expr
>] -
>
1361 (* toplevelexpr ::= expression *)
1362 let parse_toplevel = parser
1363 | [
< e=parse_expr
>] -
>
1364 (* Make an anonymous proto. *)
1365 Ast.Function (Ast.Prototype (
"", [||]), e)
1367 (* external ::= 'extern' prototype *)
1368 let parse_extern = parser
1369 | [
< 'Token.Extern; e=parse_prototype
>] -
> e
1373 <dt>codegen.ml:
</dt>
1374 <dd class=
"doc_code">
1376 (*===----------------------------------------------------------------------===
1378 *===----------------------------------------------------------------------===*)
1382 exception Error of string
1384 let context = global_context ()
1385 let the_module = create_module context
"my cool jit"
1386 let builder = builder context
1387 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create
10
1388 let double_type = double_type context
1390 (* Create an alloca instruction in the entry block of the function. This
1391 * is used for mutable variables etc. *)
1392 let create_entry_block_alloca the_function var_name =
1393 let builder = builder_at context (instr_begin (entry_block the_function)) in
1394 build_alloca double_type var_name builder
1396 let rec codegen_expr = function
1397 | Ast.Number n -
> const_float double_type n
1398 | Ast.Variable name -
>
1399 let v = try Hashtbl.find named_values name with
1400 | Not_found -
> raise (Error
"unknown variable name")
1402 (* Load the value. *)
1403 build_load v name builder
1404 | Ast.Unary (op, operand) -
>
1405 let operand = codegen_expr operand in
1406 let callee =
"unary" ^ (String.make
1 op) in
1408 match lookup_function callee the_module with
1409 | Some callee -
> callee
1410 | None -
> raise (Error
"unknown unary operator")
1412 build_call callee [|operand|]
"unop" builder
1413 | Ast.Binary (op, lhs, rhs) -
>
1416 (* Special case '=' because we don't want to emit the LHS as an
1420 | Ast.Variable name -
> name
1421 | _ -
> raise (Error
"destination of '=' must be a variable")
1424 (* Codegen the rhs. *)
1425 let val_ = codegen_expr rhs in
1427 (* Lookup the name. *)
1428 let variable = try Hashtbl.find named_values name with
1429 | Not_found -
> raise (Error
"unknown variable name")
1431 ignore(build_store val_ variable builder);
1434 let lhs_val = codegen_expr lhs in
1435 let rhs_val = codegen_expr rhs in
1438 | '+' -
> build_add lhs_val rhs_val
"addtmp" builder
1439 | '-' -
> build_sub lhs_val rhs_val
"subtmp" builder
1440 | '*' -
> build_mul lhs_val rhs_val
"multmp" builder
1442 (* Convert bool
0/
1 to double
0.0 or
1.0 *)
1443 let i = build_fcmp Fcmp.Ult lhs_val rhs_val
"cmptmp" builder in
1444 build_uitofp i double_type
"booltmp" builder
1446 (* If it wasn't a builtin binary operator, it must be a user defined
1447 * one. Emit a call to it. *)
1448 let callee =
"binary" ^ (String.make
1 op) in
1450 match lookup_function callee the_module with
1451 | Some callee -
> callee
1452 | None -
> raise (Error
"binary operator not found!")
1454 build_call callee [|lhs_val; rhs_val|]
"binop" builder
1457 | Ast.Call (callee, args) -
>
1458 (* Look up the name in the module table. *)
1460 match lookup_function callee the_module with
1461 | Some callee -
> callee
1462 | None -
> raise (Error
"unknown function referenced")
1464 let params = params callee in
1466 (* If argument mismatch error. *)
1467 if Array.length params == Array.length args then () else
1468 raise (Error
"incorrect # arguments passed");
1469 let args = Array.map codegen_expr args in
1470 build_call callee args
"calltmp" builder
1471 | Ast.If (cond, then_, else_) -
>
1472 let cond = codegen_expr cond in
1474 (* Convert condition to a bool by comparing equal to
0.0 *)
1475 let zero = const_float double_type
0.0 in
1476 let cond_val = build_fcmp Fcmp.One cond zero
"ifcond" builder in
1478 (* Grab the first block so that we might later add the conditional branch
1479 * to it at the end of the function. *)
1480 let start_bb = insertion_block builder in
1481 let the_function = block_parent start_bb in
1483 let then_bb = append_block context
"then" the_function in
1485 (* Emit 'then' value. *)
1486 position_at_end then_bb builder;
1487 let then_val = codegen_expr then_ in
1489 (* Codegen of 'then' can change the current block, update then_bb for the
1490 * phi. We create a new name because one is used for the phi node, and the
1491 * other is used for the conditional branch. *)
1492 let new_then_bb = insertion_block builder in
1494 (* Emit 'else' value. *)
1495 let else_bb = append_block context
"else" the_function in
1496 position_at_end else_bb builder;
1497 let else_val = codegen_expr else_ in
1499 (* Codegen of 'else' can change the current block, update else_bb for the
1501 let new_else_bb = insertion_block builder in
1503 (* Emit merge block. *)
1504 let merge_bb = append_block context
"ifcont" the_function in
1505 position_at_end merge_bb builder;
1506 let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
1507 let phi = build_phi incoming
"iftmp" builder in
1509 (* Return to the start block to add the conditional branch. *)
1510 position_at_end start_bb builder;
1511 ignore (build_cond_br cond_val then_bb else_bb builder);
1513 (* Set a unconditional branch at the end of the 'then' block and the
1514 * 'else' block to the 'merge' block. *)
1515 position_at_end new_then_bb builder; ignore (build_br merge_bb builder);
1516 position_at_end new_else_bb builder; ignore (build_br merge_bb builder);
1518 (* Finally, set the builder to the end of the merge block. *)
1519 position_at_end merge_bb builder;
1522 | Ast.For (var_name, start, end_, step, body) -
>
1524 * var = alloca double
1527 * store start -
> var
1538 * nextvar = curvar + step
1539 * store nextvar -
> var
1540 * br endcond, loop, endloop
1543 let the_function = block_parent (insertion_block builder) in
1545 (* Create an alloca for the variable in the entry block. *)
1546 let alloca = create_entry_block_alloca the_function var_name in
1548 (* Emit the start code first, without 'variable' in scope. *)
1549 let start_val = codegen_expr start in
1551 (* Store the value into the alloca. *)
1552 ignore(build_store start_val alloca builder);
1554 (* Make the new basic block for the loop header, inserting after current
1556 let loop_bb = append_block context
"loop" the_function in
1558 (* Insert an explicit fall through from the current block to the
1560 ignore (build_br loop_bb builder);
1562 (* Start insertion in loop_bb. *)
1563 position_at_end loop_bb builder;
1565 (* Within the loop, the variable is defined equal to the PHI node. If it
1566 * shadows an existing variable, we have to restore it, so save it
1569 try Some (Hashtbl.find named_values var_name) with Not_found -
> None
1571 Hashtbl.add named_values var_name alloca;
1573 (* Emit the body of the loop. This, like any other expr, can change the
1574 * current BB. Note that we ignore the value computed by the body, but
1575 * don't allow an error *)
1576 ignore (codegen_expr body);
1578 (* Emit the step value. *)
1581 | Some step -
> codegen_expr step
1582 (* If not specified, use
1.0. *)
1583 | None -
> const_float double_type
1.0
1586 (* Compute the end condition. *)
1587 let end_cond = codegen_expr end_ in
1589 (* Reload, increment, and restore the alloca. This handles the case where
1590 * the body of the loop mutates the variable. *)
1591 let cur_var = build_load alloca var_name builder in
1592 let next_var = build_add cur_var step_val
"nextvar" builder in
1593 ignore(build_store next_var alloca builder);
1595 (* Convert condition to a bool by comparing equal to
0.0. *)
1596 let zero = const_float double_type
0.0 in
1597 let end_cond = build_fcmp Fcmp.One end_cond zero
"loopcond" builder in
1599 (* Create the
"after loop" block and insert it. *)
1600 let after_bb = append_block context
"afterloop" the_function in
1602 (* Insert the conditional branch into the end of loop_end_bb. *)
1603 ignore (build_cond_br end_cond loop_bb after_bb builder);
1605 (* Any new code will be inserted in after_bb. *)
1606 position_at_end after_bb builder;
1608 (* Restore the unshadowed variable. *)
1609 begin match old_val with
1610 | Some old_val -
> Hashtbl.add named_values var_name old_val
1614 (* for expr always returns
0.0. *)
1615 const_null double_type
1616 | Ast.Var (var_names, body) -
>
1617 let old_bindings = ref [] in
1619 let the_function = block_parent (insertion_block builder) in
1621 (* Register all variables and emit their initializer. *)
1622 Array.iter (fun (var_name, init) -
>
1623 (* Emit the initializer before adding the variable to scope, this
1624 * prevents the initializer from referencing the variable itself, and
1625 * permits stuff like this:
1627 * var a = a in ... # refers to outer 'a'. *)
1630 | Some init -
> codegen_expr init
1631 (* If not specified, use
0.0. *)
1632 | None -
> const_float double_type
0.0
1635 let alloca = create_entry_block_alloca the_function var_name in
1636 ignore(build_store init_val alloca builder);
1638 (* Remember the old variable binding so that we can restore the binding
1639 * when we unrecurse. *)
1642 let old_value = Hashtbl.find named_values var_name in
1643 old_bindings := (var_name, old_value) :: !old_bindings;
1644 with Not_found -
> ()
1647 (* Remember this binding. *)
1648 Hashtbl.add named_values var_name alloca;
1651 (* Codegen the body, now that all vars are in scope. *)
1652 let body_val = codegen_expr body in
1654 (* Pop all our variables from scope. *)
1655 List.iter (fun (var_name, old_value) -
>
1656 Hashtbl.add named_values var_name old_value
1659 (* Return the body computation. *)
1662 let codegen_proto = function
1663 | Ast.Prototype (name, args) | Ast.BinOpPrototype (name, args, _) -
>
1664 (* Make the function type: double(double,double) etc. *)
1665 let doubles = Array.make (Array.length args) double_type in
1666 let ft = function_type double_type doubles in
1668 match lookup_function name the_module with
1669 | None -
> declare_function name ft the_module
1671 (* If 'f' conflicted, there was already something named 'name'. If it
1672 * has a body, don't allow redefinition or reextern. *)
1674 (* If 'f' already has a body, reject this. *)
1675 if block_begin f
<> At_end f then
1676 raise (Error
"redefinition of function");
1678 (* If 'f' took a different number of arguments, reject. *)
1679 if element_type (type_of f)
<> ft then
1680 raise (Error
"redefinition of function with different # args");
1684 (* Set names for all arguments. *)
1685 Array.iteri (fun i a -
>
1688 Hashtbl.add named_values n a;
1692 (* Create an alloca for each argument and register the argument in the symbol
1693 * table so that references to it will succeed. *)
1694 let create_argument_allocas the_function proto =
1695 let args = match proto with
1696 | Ast.Prototype (_, args) | Ast.BinOpPrototype (_, args, _) -
> args
1698 Array.iteri (fun i ai -
>
1699 let var_name = args.(i) in
1700 (* Create an alloca for this variable. *)
1701 let alloca = create_entry_block_alloca the_function var_name in
1703 (* Store the initial value into the alloca. *)
1704 ignore(build_store ai alloca builder);
1706 (* Add arguments to variable symbol table. *)
1707 Hashtbl.add named_values var_name alloca;
1708 ) (params the_function)
1710 let codegen_func the_fpm = function
1711 | Ast.Function (proto, body) -
>
1712 Hashtbl.clear named_values;
1713 let the_function = codegen_proto proto in
1715 (* If this is an operator, install it. *)
1716 begin match proto with
1717 | Ast.BinOpPrototype (name, args, prec) -
>
1718 let op = name.[String.length name -
1] in
1719 Hashtbl.add Parser.binop_precedence op prec;
1723 (* Create a new basic block to start insertion into. *)
1724 let bb = append_block context
"entry" the_function in
1725 position_at_end bb builder;
1728 (* Add all arguments to the symbol table and create their allocas. *)
1729 create_argument_allocas the_function proto;
1731 let ret_val = codegen_expr body in
1733 (* Finish off the function. *)
1734 let _ = build_ret ret_val builder in
1736 (* Validate the generated code, checking for consistency. *)
1737 Llvm_analysis.assert_valid_function the_function;
1739 (* Optimize the function. *)
1740 let _ = PassManager.run_function the_function the_fpm in
1744 delete_function the_function;
1749 <dt>toplevel.ml:
</dt>
1750 <dd class=
"doc_code">
1752 (*===----------------------------------------------------------------------===
1753 * Top-Level parsing and JIT Driver
1754 *===----------------------------------------------------------------------===*)
1757 open Llvm_executionengine
1759 (* top ::= definition | external | expression | ';' *)
1760 let rec main_loop the_fpm the_execution_engine stream =
1761 match Stream.peek stream with
1764 (* ignore top-level semicolons. *)
1765 | Some (Token.Kwd ';') -
>
1767 main_loop the_fpm the_execution_engine stream
1771 try match token with
1773 let e = Parser.parse_definition stream in
1774 print_endline
"parsed a function definition.";
1775 dump_value (Codegen.codegen_func the_fpm e);
1776 | Token.Extern -
>
1777 let e = Parser.parse_extern stream in
1778 print_endline
"parsed an extern.";
1779 dump_value (Codegen.codegen_proto e);
1781 (* Evaluate a top-level expression into an anonymous function. *)
1782 let e = Parser.parse_toplevel stream in
1783 print_endline
"parsed a top-level expr";
1784 let the_function = Codegen.codegen_func the_fpm e in
1785 dump_value the_function;
1787 (* JIT the function, returning a function pointer. *)
1788 let result = ExecutionEngine.run_function the_function [||]
1789 the_execution_engine in
1791 print_string
"Evaluated to ";
1792 print_float (GenericValue.as_float Codegen.double_type result);
1794 with Stream.Error s | Codegen.Error s -
>
1795 (* Skip token for error recovery. *)
1799 print_string
"ready> "; flush stdout;
1800 main_loop the_fpm the_execution_engine stream
1805 <dd class=
"doc_code">
1807 (*===----------------------------------------------------------------------===
1809 *===----------------------------------------------------------------------===*)
1812 open Llvm_executionengine
1814 open Llvm_scalar_opts
1817 ignore (initialize_native_target ());
1819 (* Install standard binary operators.
1820 *
1 is the lowest precedence. *)
1821 Hashtbl.add Parser.binop_precedence '='
2;
1822 Hashtbl.add Parser.binop_precedence '
<'
10;
1823 Hashtbl.add Parser.binop_precedence '+'
20;
1824 Hashtbl.add Parser.binop_precedence '-'
20;
1825 Hashtbl.add Parser.binop_precedence '*'
40; (* highest. *)
1827 (* Prime the first token. *)
1828 print_string
"ready> "; flush stdout;
1829 let stream = Lexer.lex (Stream.of_channel stdin) in
1831 (* Create the JIT. *)
1832 let the_execution_engine = ExecutionEngine.create Codegen.the_module in
1833 let the_fpm = PassManager.create_function Codegen.the_module in
1835 (* Set up the optimizer pipeline. Start with registering info about how the
1836 * target lays out data structures. *)
1837 TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
1839 (* Promote allocas to registers. *)
1840 add_memory_to_register_promotion the_fpm;
1842 (* Do simple
"peephole" optimizations and bit-twiddling optzn. *)
1843 add_instruction_combination the_fpm;
1845 (* reassociate expressions. *)
1846 add_reassociation the_fpm;
1848 (* Eliminate Common SubExpressions. *)
1851 (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
1852 add_cfg_simplification the_fpm;
1854 ignore (PassManager.initialize the_fpm);
1856 (* Run the main
"interpreter loop" now. *)
1857 Toplevel.main_loop the_fpm the_execution_engine stream;
1859 (* Print out all the generated code. *)
1860 dump_module Codegen.the_module
1868 <dd class=
"doc_code">
1870 #include
<stdio.h
>
1872 /* putchard - putchar that takes a double and returns
0. */
1873 extern double putchard(double X) {
1878 /* printd - printf that takes a double prints it as
"%f\n", returning
0. */
1879 extern double printd(double X) {
1887 <a href=
"OCamlLangImpl8.html">Next: Conclusion and other useful LLVM tidbits
</a>
1890 <!-- *********************************************************************** -->
1893 <a href=
"http://jigsaw.w3.org/css-validator/check/referer"><img
1894 src=
"http://jigsaw.w3.org/css-validator/images/vcss" alt=
"Valid CSS!"></a>
1895 <a href=
"http://validator.w3.org/check/referer"><img
1896 src=
"http://www.w3.org/Icons/valid-html401" alt=
"Valid HTML 4.01!"></a>
1898 <a href=
"mailto:sabre@nondot.org">Chris Lattner
</a><br>
1899 <a href=
"http://llvm.org/">The LLVM Compiler Infrastructure
</a><br>
1900 <a href=
"mailto:idadesub@users.sourceforge.net">Erick Tryzelaar
</a><br>
1901 Last modified: $Date$