AMDGPU: Add core backend files for R600/SI codegen v6
[oota-llvm.git] / docs / tutorial / OCamlLangImpl3.html
index b396ef07ae5ccf6fa5b9dcb604a60c4a05a366f8..a49a0b5d9c6155bf65354343c34710f761add983 100644 (file)
@@ -7,12 +7,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: Code generation to LLVM IR</div>
+<h1>Kaleidoscope: Code generation to LLVM IR</h1>
 
 <ul>
 <li><a href="index.html">Up to Tutorial Index</a></li>
@@ -26,7 +26,7 @@
     <li><a href="#code">Full Code Listing</a></li>
   </ol>
 </li>
-<li><a href="LangImpl4.html">Chapter 4</a>: Adding JIT and Optimizer
+<li><a href="OCamlLangImpl4.html">Chapter 4</a>: Adding JIT and Optimizer
 Support</li>
 </ul>
 
@@ -38,10 +38,10 @@ Support</li>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="intro">Chapter 3 Introduction</a></div>
+<h2><a name="intro">Chapter 3 Introduction</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Welcome to Chapter 3 of the "<a href="index.html">Implementing a language
 with LLVM</a>" tutorial.  This chapter shows you how to transform the <a
@@ -57,10 +57,10 @@ LLVM SVN to work.  LLVM 2.2 and before will not work with it.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="basics">Code Generation Setup</a></div>
+<h2><a name="basics">Code Generation Setup</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 In order to generate LLVM IR, we want some simple setup to get started.  First
@@ -95,9 +95,11 @@ an undeclared parameter):</p>
 <pre>
 exception Error of string
 
-let the_module = create_module "my cool jit"
-let builder = builder ()
+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
 </pre>
 </div>
 
@@ -127,10 +129,10 @@ that this has already been done, and we'll just use it to emit code.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="exprs">Expression Code Generation</a></div>
+<h2><a name="exprs">Expression Code Generation</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Generating LLVM code for expression nodes is very straightforward: less
 than 30 lines of commented code for all four of our expression nodes.  First
@@ -148,7 +150,7 @@ internally (<tt>APFloat</tt> has the capability of holding floating point
 constants of <em>A</em>rbitrary <em>P</em>recision).  This code basically just
 creates and returns a <tt>ConstantFP</tt>.  Note that in the LLVM IR
 that constants are all uniqued together and shared.  For this reason, the API
-uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::create(..)".</p>
+uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::Create(..)".</p>
 
 <div class="doc_code">
 <pre>
@@ -159,7 +161,7 @@ uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::create(..)".</p>
 </div>
 
 <p>References to variables are also quite simple using LLVM.  In the simple
-version of Kaleidoscope, we assume that the variable has already been emited
+version of Kaleidoscope, we assume that the variable has already been emitted
 somewhere and its value is available.  In practice, the only values that can be
 in the <tt>Codegen.named_values</tt> map are function arguments.  This code
 simply checks to see that the specified name is in the map (if not, an unknown
@@ -175,9 +177,9 @@ variables</a>.</p>
       let rhs_val = codegen_expr rhs in
       begin
         match op with
-        | '+' -&gt; build_add lhs_val rhs_val "addtmp" builder
-        | '-' -&gt; build_sub lhs_val rhs_val "subtmp" builder
-        | '*' -&gt; build_mul lhs_val rhs_val "multmp" builder
+        | '+' -&gt; build_fadd lhs_val rhs_val "addtmp" builder
+        | '-' -&gt; build_fsub lhs_val rhs_val "subtmp" builder
+        | '*' -&gt; build_fmul lhs_val rhs_val "multmp" builder
         | '&lt;' -&gt;
             (* Convert bool 0/1 to double 0.0 or 1.0 *)
             let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in
@@ -262,10 +264,10 @@ basic framework.</p>
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="funcs">Function Code Generation</a></div>
+<h2><a name="funcs">Function Code Generation</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>Code generation for prototypes and functions must handle a number of
 details, which make their code less beautiful than expression code
@@ -323,7 +325,7 @@ code above.</p>
 first, we want to allow 'extern'ing a function more than once, as long as the
 prototypes for the externs match (since all arguments have the same type, we
 just have to check that the number of arguments match).  Second, we want to
-allow 'extern'ing a function and then definining a body for it.  This is useful
+allow 'extern'ing a function and then defining a body for it.  This is useful
 when defining mutually recursive functions.</p>
 
 <div class="doc_code">
@@ -389,7 +391,7 @@ that there is an LLVM Function object that is ready to go for us.</p>
 <div class="doc_code">
 <pre>
       (* 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
@@ -465,11 +467,10 @@ def bar() foo(1, 2); # error, unknown function "foo"
 </div>
 
 <!-- *********************************************************************** -->
-<div class="doc_section"><a name="driver">Driver Changes and
-Closing Thoughts</a></div>
+<h2><a name="driver">Driver Changes and Closing Thoughts</a></h2>
 <!-- *********************************************************************** -->
 
-<div class="doc_text">
+<div>
 
 <p>
 For now, code generation to LLVM doesn't really get us much, except that we can
@@ -484,14 +485,14 @@ ready&gt; <b>4+5</b>;
 Read top-level expression:
 define double @""() {
 entry:
-        %addtmp = add double 4.000000e+00, 5.000000e+00
+        %addtmp = fadd double 4.000000e+00, 5.000000e+00
         ret double %addtmp
 }
 </pre>
 </div>
 
 <p>Note how the parser turns the top-level expression into anonymous functions
-for us.  This will be handy when we add <a href="LangImpl4.html#jit">JIT
+for us.  This will be handy when we add <a href="OCamlLangImpl4.html#jit">JIT
 support</a> in the next chapter.  Also note that the code is very literally
 transcribed, no optimizations are being performed.  We will
 <a href="OCamlLangImpl4.html#trivialconstfold">add optimizations</a> explicitly
@@ -503,12 +504,12 @@ ready&gt; <b>def foo(a b) a*a + 2*a*b + b*b;</b>
 Read function definition:
 define double @foo(double %a, double %b) {
 entry:
-        %multmp = mul double %a, %a
-        %multmp1 = mul double 2.000000e+00, %a
-        %multmp2 = mul double %multmp1, %b
-        %addtmp = add double %multmp, %multmp2
-        %multmp3 = mul double %b, %b
-        %addtmp4 = add double %addtmp, %multmp3
+        %multmp = fmul double %a, %a
+        %multmp1 = fmul double 2.000000e+00, %a
+        %multmp2 = fmul double %multmp1, %b
+        %addtmp = fadd double %multmp, %multmp2
+        %multmp3 = fmul double %b, %b
+        %addtmp4 = fadd double %addtmp, %multmp3
         ret double %addtmp4
 }
 </pre>
@@ -523,9 +524,9 @@ ready&gt; <b>def bar(a) foo(a, 4.0) + bar(31337);</b>
 Read function definition:
 define double @bar(double %a) {
 entry:
-        %calltmp = call double @foo( double %a, double 4.000000e+00 )
-        %calltmp1 = call double @bar( double 3.133700e+04 )
-        %addtmp = add double %calltmp, %calltmp1
+        %calltmp = call double @foo(double %a, double 4.000000e+00)
+        %calltmp1 = call double @bar(double 3.133700e+04)
+        %addtmp = fadd double %calltmp, %calltmp1
         ret double %addtmp
 }
 </pre>
@@ -545,7 +546,7 @@ ready&gt; <b>cos(1.234);</b>
 Read top-level expression:
 define double @""() {
 entry:
-        %calltmp = call double @cos( double 1.234000e+00 )
+        %calltmp = call double @cos(double 1.234000e+00)
         ret double %calltmp
 }
 </pre>
@@ -561,26 +562,26 @@ ready&gt; <b>^D</b>
 
 define double @""() {
 entry:
-        %addtmp = add double 4.000000e+00, 5.000000e+00
+        %addtmp = fadd double 4.000000e+00, 5.000000e+00
         ret double %addtmp
 }
 
 define double @foo(double %a, double %b) {
 entry:
-        %multmp = mul double %a, %a
-        %multmp1 = mul double 2.000000e+00, %a
-        %multmp2 = mul double %multmp1, %b
-        %addtmp = add double %multmp, %multmp2
-        %multmp3 = mul double %b, %b
-        %addtmp4 = add double %addtmp, %multmp3
+        %multmp = fmul double %a, %a
+        %multmp1 = fmul double 2.000000e+00, %a
+        %multmp2 = fmul double %multmp1, %b
+        %addtmp = fadd double %multmp, %multmp2
+        %multmp3 = fmul double %b, %b
+        %addtmp4 = fadd double %addtmp, %multmp3
         ret double %addtmp4
 }
 
 define double @bar(double %a) {
 entry:
-        %calltmp = call double @foo( double %a, double 4.000000e+00 )
-        %calltmp1 = call double @bar( double 3.133700e+04 )
-        %addtmp = add double %calltmp, %calltmp1
+        %calltmp = call double @foo(double %a, double 4.000000e+00)
+        %calltmp1 = call double @bar(double 3.133700e+04)
+        %addtmp = fadd double %calltmp, %calltmp1
         ret double %addtmp
 }
 
@@ -588,7 +589,7 @@ declare double @cos(double)
 
 define double @""() {
 entry:
-        %calltmp = call double @cos( double 1.234000e+00 )
+        %calltmp = call double @cos(double 1.234000e+00)
         ret double %calltmp
 }
 </pre>
@@ -599,17 +600,17 @@ generated.  Here you can see the big picture with all the functions referencing
 each other.</p>
 
 <p>This wraps up the third chapter of the Kaleidoscope tutorial.  Up next, we'll
-describe how to <a href="LangImpl4.html">add JIT codegen and optimizer
+describe how to <a href="OCamlLangImpl4.html">add JIT codegen and optimizer
 support</a> to this so we can actually start running code!</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 the
@@ -899,9 +900,11 @@ open Llvm
 
 exception Error of string
 
-let the_module = create_module "my cool jit"
-let builder = builder ()
+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
 
 let rec codegen_expr = function
   | Ast.Number n -&gt; const_float double_type n
@@ -973,7 +976,7 @@ let codegen_func = function
       let the_function = codegen_proto proto in
 
       (* 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
@@ -1083,8 +1086,8 @@ main ()
 
   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
   <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a><br>
-  <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
-  Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $
+  <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
+  Last modified: $Date$
 </address>
 </body>
 </html>