Move TargetData to DataLayout.
[oota-llvm.git] / docs / tutorial / OCamlLangImpl7.html
index 6047ad38c7b2ba5c09e533f57b090a0f68897ecb..fd66b10c1a1437bae7189ee176544a3900fa5c32 100644 (file)
@@ -8,12 +8,12 @@
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <meta name="author" content="Chris Lattner">
   <meta name="author" content="Erick Tryzelaar">
-  <link rel="stylesheet" href="../llvm.css" type="text/css">
+  <link rel="stylesheet" href="../_static/llvm.css" type="text/css">
 </head>
 
 <body>
 
-<div class="doc_title">Kaleidoscope: Extending the Language: Mutable Variables</div>
+<h1>Kaleidoscope: Extending the Language: Mutable Variables</h1>
 
 <ul>
 <li><a href="index.html">Up to Tutorial Index</a></li>
@@ -30,7 +30,7 @@
     <li><a href="#code">Full Code Listing</a></li>
   </ol>
 </li>
-<li><a href="LangImpl8.html">Chapter 8</a>: Conclusion and other useful LLVM
+<li><a href="OCamlLangImpl8.html">Chapter 8</a>: Conclusion and other useful LLVM
  tidbits</li>
 </ul>
 
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="intro">Chapter 7 Introduction</a></div>
+<h2><a name="intro">Chapter 7 Introduction</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Welcome to Chapter 7 of the "<a href="index.html">Implementing a language
 with LLVM</a>" tutorial.  In chapters 1 through 6, we've built a very
@@ -70,10 +70,10 @@ support for this, though the way it works is a bit unexpected for some.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="why">Why is this a hard problem?</a></div>
+<h2><a name="why">Why is this a hard problem?</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 To understand why mutable variables cause complexities in SSA construction,
@@ -144,10 +144,10 @@ logic.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="memory">Memory in LLVM</a></div>
+<h2><a name="memory">Memory in LLVM</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>The 'trick' here is that while LLVM does require all register values to be
 in SSA form, it does not require (or permit) memory objects to be in SSA form.
@@ -325,11 +325,10 @@ variables now!
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="kalvars">Mutable Variables in
-Kaleidoscope</a></div>
+<h2><a name="kalvars">Mutable Variables in Kaleidoscope</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Now that we know the sort of problem we want to tackle, lets see what this
 looks like in the context of our little Kaleidoscope language.  We're going to
@@ -382,11 +381,10 @@ Kaleidoscope to support new variable definitions.
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="adjustments">Adjusting Existing Variables for
-Mutation</a></div>
+<h2><a name="adjustments">Adjusting Existing Variables for Mutation</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 The symbol table in Kaleidoscope is managed at code generation time by the
@@ -543,11 +541,11 @@ good codegen once again:</p>
 <pre>
 let main () =
   ...
-  let the_fpm = PassManager.create_function the_module_provider in
+  let the_fpm = PassManager.create_function Codegen.the_module in
 
   (* Set up the optimizer pipeline.  Start with registering info about how the
    * target lays out data structures. *)
-  TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
+  DataLayout.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
 
   <b>(* Promote allocas to registers. *)
   add_memory_to_register_promotion the_fpm;</b>
@@ -582,10 +580,10 @@ then:    ; preds = %entry
 else:    ; preds = %entry
   <b>%x3 = load double* %x1</b>
   %subtmp = fsub double %x3, 1.000000e+00
-  %calltmp = call double @fib( double %subtmp )
+  %calltmp = call double @fib(double %subtmp)
   <b>%x4 = load double* %x1</b>
   %subtmp5 = fsub double %x4, 2.000000e+00
-  %calltmp6 = call double @fib( double %subtmp5 )
+  %calltmp6 = call double @fib(double %subtmp5)
   %addtmp = fadd double %calltmp, %calltmp6
   br label %ifcont
 
@@ -620,9 +618,9 @@ then:
 
 else:
   %subtmp = fsub double <b>%x</b>, 1.000000e+00
-  %calltmp = call double @fib( double %subtmp )
+  %calltmp = call double @fib(double %subtmp)
   %subtmp5 = fsub double <b>%x</b>, 2.000000e+00
-  %calltmp6 = call double @fib( double %subtmp5 )
+  %calltmp6 = call double @fib(double %subtmp5)
   %addtmp = fadd double %calltmp, %calltmp6
   br label %ifcont
 
@@ -650,9 +648,9 @@ entry:
 
 else:
   %subtmp = fsub double %x, 1.000000e+00
-  %calltmp = call double @fib( double %subtmp )
+  %calltmp = call double @fib(double %subtmp)
   %subtmp5 = fsub double %x, 2.000000e+00
-  %calltmp6 = call double @fib( double %subtmp5 )
+  %calltmp6 = call double @fib(double %subtmp5)
   %addtmp = fadd double %calltmp, %calltmp6
   ret double %addtmp
 
@@ -672,10 +670,10 @@ we'll add the assignment operator.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="assignment">New Assignment Operator</a></div>
+<h2><a name="assignment">New Assignment Operator</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>With our current framework, adding a new assignment operator is really
 simple.  We will parse it just like any other binary operator, but handle it
@@ -773,11 +771,10 @@ add this next!
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="localvars">User-defined Local
-Variables</a></div>
+<h2><a name="localvars">User-defined Local Variables</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Adding var/in is just like any other other extensions we made to
 Kaleidoscope: we extend the lexer, the parser, the AST and the code generator.
@@ -956,10 +953,10 @@ anywhere in sight.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="code">Full Code Listing</a></div>
+<h2><a name="code">Full Code Listing</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 Here is the complete code listing for our running example, enhanced with mutable
@@ -999,7 +996,7 @@ ocaml_lib ~extern:true "llvm_executionengine";;
 ocaml_lib ~extern:true "llvm_target";;
 ocaml_lib ~extern:true "llvm_scalar_opts";;
 
-flag ["link"; "ocaml"; "g++"] (S[A"-cc"; A"g++"]);;
+flag ["link"; "ocaml"; "g++"] (S[A"-cc"; A"g++"; A"-cclib"; A"-rdynamic"]);;
 dep ["link"; "ocaml"; "use_bindings"] ["bindings.o"];;
 </pre>
 </dd>
@@ -1388,6 +1385,7 @@ let context = global_context ()
 let the_module = create_module context "my cool jit"
 let builder = builder context
 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
+let double_type = double_type context
 
 (* Create an alloca instruction in the entry block of the function. This
  * is used for mutable variables etc. *)
@@ -1482,7 +1480,7 @@ let rec codegen_expr = function
       let start_bb = insertion_block builder in
       let the_function = block_parent start_bb in
 
-      let then_bb = append_block "then" the_function in
+      let then_bb = append_block context "then" the_function in
 
       (* Emit 'then' value. *)
       position_at_end then_bb builder;
@@ -1494,7 +1492,7 @@ let rec codegen_expr = function
       let new_then_bb = insertion_block builder in
 
       (* Emit 'else' value. *)
-      let else_bb = append_block "else" the_function in
+      let else_bb = append_block context "else" the_function in
       position_at_end else_bb builder;
       let else_val = codegen_expr else_ in
 
@@ -1503,7 +1501,7 @@ let rec codegen_expr = function
       let new_else_bb = insertion_block builder in
 
       (* Emit merge block. *)
-      let merge_bb = append_block "ifcont" the_function in
+      let merge_bb = append_block context "ifcont" the_function in
       position_at_end merge_bb builder;
       let incoming = [(then_val, new_then_bb); (else_val, new_else_bb)] in
       let phi = build_phi incoming "iftmp" builder in
@@ -1555,7 +1553,7 @@ let rec codegen_expr = function
 
       (* Make the new basic block for the loop header, inserting after current
        * block. *)
-      let loop_bb = append_block "loop" the_function in
+      let loop_bb = append_block context "loop" the_function in
 
       (* Insert an explicit fall through from the current block to the
        * loop_bb. *)
@@ -1599,7 +1597,7 @@ let rec codegen_expr = function
       let end_cond = build_fcmp Fcmp.One end_cond zero "loopcond" builder in
 
       (* Create the "after loop" block and insert it. *)
-      let after_bb = append_block "afterloop" the_function in
+      let after_bb = append_block context "afterloop" the_function in
 
       (* Insert the conditional branch into the end of loop_end_bb. *)
       ignore (build_cond_br end_cond loop_bb after_bb builder);
@@ -1723,7 +1721,7 @@ let codegen_func the_fpm = function
       end;
 
       (* Create a new basic block to start insertion into. *)
-      let bb = append_block "entry" the_function in
+      let bb = append_block context "entry" the_function in
       position_at_end bb builder;
 
       try
@@ -1791,7 +1789,7 @@ let rec main_loop the_fpm the_execution_engine stream =
               the_execution_engine in
 
             print_string "Evaluated to ";
-            print_float (GenericValue.as_float double_type result);
+            print_float (GenericValue.as_float Codegen.double_type result);
             print_newline ();
         with Stream.Error s | Codegen.Error s -&gt;
           (* Skip token for error recovery. *)
@@ -1831,19 +1829,18 @@ let main () =
   let stream = Lexer.lex (Stream.of_channel stdin) in
 
   (* Create the JIT. *)
-  let the_module_provider = ModuleProvider.create Codegen.the_module in
-  let the_execution_engine = ExecutionEngine.create the_module_provider in
-  let the_fpm = PassManager.create_function the_module_provider in
+  let the_execution_engine = ExecutionEngine.create Codegen.the_module in
+  let the_fpm = PassManager.create_function Codegen.the_module in
 
   (* Set up the optimizer pipeline.  Start with registering info about how the
    * target lays out data structures. *)
-  TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
+  DataLayout.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
 
   (* Promote allocas to registers. *)
   add_memory_to_register_promotion the_fpm;
 
   (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
-  add_instruction_combining the_fpm;
+  add_instruction_combination the_fpm;
 
   (* reassociate expressions. *)
   add_reassociation the_fpm;
@@ -1887,7 +1884,7 @@ extern double printd(double X) {
 </dd>
 </dl>
 
-<a href="LangImpl8.html">Next: Conclusion and other useful LLVM tidbits</a>
+<a href="OCamlLangImpl8.html">Next: Conclusion and other useful LLVM tidbits</a>
 </div>
 
 <!-- *********************************************************************** -->
@@ -1899,7 +1896,7 @@ extern double printd(double X) {
   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
 
   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
-  <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
+  <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
   <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a><br>
   Last modified: $Date$
 </address>