Delete the Module object.
[oota-llvm.git] / docs / tutorial / LangImpl3.html
index b29f422a9e8ae19b56dfd906e108e21c1ce0fb65..f77f8f83032ca927e6f3815695301842b35ead66 100644 (file)
@@ -59,8 +59,8 @@ LLVM SVN to work.  LLVM 2.1 and before will not work with it.</p>
 <div class="doc_text">
 
 <p>
-In order to generate LLVM IR, we want some simple setup to get started.  First,
-we define virtual codegen methods in each AST class:</p>
+In order to generate LLVM IR, we want some simple setup to get started.  First
+we define virtual code generation (codegen) methods in each AST class:</p>
 
 <div class="doc_code">
 <pre>
@@ -95,9 +95,11 @@ href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
 Assignment</a> - the concepts are really quite natural once you grok them.</p>
 
 <p>Note that instead of adding virtual methods to the ExprAST class hierarchy,
-it could also make sense to use a visitor pattern or some other way to model
-this.  Again, this tutorial won't dwell on good software engineering practices:
-for our purposes, adding a virtual method is simplest.</p>
+it could also make sense to use a <a
+href="http://en.wikipedia.org/wiki/Visitor_pattern">visitor pattern</a> or some
+other way to model this.  Again, this tutorial won't dwell on good software
+engineering practices: for our purposes, adding a virtual method is
+simplest.</p>
 
 <p>The
 second thing we want is an "Error" method like we used for the parser, which will
@@ -109,7 +111,7 @@ undeclared parameter):</p>
 Value *ErrorV(const char *Str) { Error(Str); return 0; }
 
 static Module *TheModule;
-static LLVMBuilder Builder;
+static IRBuilder Builder;
 static std::map&lt;std::string, Value*&gt; NamedValues;
 </pre>
 </div>
@@ -121,16 +123,15 @@ uses to contain code.</p>
 
 <p>The <tt>Builder</tt> object is a helper object that makes it easy to generate
 LLVM instructions.  Instances of the <a 
-href="http://llvm.org/doxygen/LLVMBuilder_8h-source.html"><tt>LLVMBuilder</tt
-class</a> keep track of the current place to
-insert instructions and has methods to create new instructions.</p>
+href="http://llvm.org/doxygen/IRBuilder_8h-source.html"><tt>IRBuilder</tt></a
+class keep track of the current place to insert instructions and has methods to
+create new instructions.</p>
 
 <p>The <tt>NamedValues</tt> map keeps track of which values are defined in the
-current scope and what their LLVM representation is (in other words, it is a 
-symbol table for the code).  In this form of
-Kaleidoscope, the only things that can be referenced are function parameters.
-As such, function parameters will be in this map when generating code for their
-function body.</p>
+current scope and what their LLVM representation is.  (In other words, it is a
+symbol table for the code).  In this form of Kaleidoscope, the only things that
+can be referenced are function parameters.  As such, function parameters will
+be in this map when generating code for their function body.</p>
 
 <p>
 With these basics in place, we can start talking about how to generate code for
@@ -148,7 +149,7 @@ has already been done, and we'll just use it to emit code.
 <div class="doc_text">
 
 <p>Generating LLVM code for expression nodes is very straightforward: less
-than 45 lines of commented code for all four of our expression nodes.  First,
+than 45 lines of commented code for all four of our expression nodes.  First
 we'll do numeric literals:</p>
 
 <div class="doc_code">
@@ -215,14 +216,16 @@ code, we do a simple switch on the opcode to create the right LLVM instruction.
 </p>
 
 <p>In the example above, the LLVM builder class is starting to show its value.  
-LLVMBuilder knows where to insert the newly created instruction, all you have to
+IRBuilder knows where to insert the newly created instruction, all you have to
 do is specify what instruction to create (e.g. with <tt>CreateAdd</tt>), which
 operands to use (<tt>L</tt> and <tt>R</tt> here) and optionally provide a name
-for the generated instruction.  One nice thing about LLVM is that the name is 
-just a hint: if there are multiple additions in a single function, the first
-will be named "addtmp" and the second will be "autorenamed" by adding a suffix,
-giving it a name like "addtmp42".  Local value names for instructions are purely
-optional, but it makes it much easier to read the IR dumps.</p>
+for the generated instruction.</p>
+
+<p>One nice thing about LLVM is that the name is just a hint.  For instance, if
+the code above emits multiple "addtmp" variables, LLVM will automatically
+provide each one with an increasing, unique numeric suffix.  Local value names
+for instructions are purely optional, but it makes it much easier to read the
+IR dumps.</p>
 
 <p><a href="../LangRef.html#instref">LLVM instructions</a> are constrained by
 strict rules: for example, the Left and Right operators of
@@ -303,7 +306,7 @@ Function *PrototypeAST::Codegen() {
   std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::DoubleTy);
   FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
   
-  Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
+  Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
 </pre>
 </div>
 
@@ -324,7 +327,7 @@ don't "new" a type, you "get" it.</p>
 
 <p>The final line above actually creates the function that the prototype will
 correspond to.  This indicates the type, linkage and name to use, as well as which
-module to insert into.  "<a href="LangRef.html#linkage">external linkage</a>"
+module to insert into.  "<a href="../LangRef.html#linkage">external linkage</a>"
 means that the function may be defined outside the current module and/or that it
 is callable by functions outside the module.  The Name passed in is the name the
 user specified: since "<tt>TheModule</tt>" is specified, this name is registered
@@ -432,7 +435,7 @@ 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.
-  BasicBlock *BB = new BasicBlock("entry", TheFunction);
+  BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
   Builder.SetInsertPoint(BB);
   
   if (Value *RetVal = Body-&gt;Codegen()) {
@@ -677,7 +680,7 @@ our makefile/command line about which options to use:</p>
 #include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
 #include "llvm/Analysis/Verifier.h"
-#include "llvm/Support/LLVMBuilder.h"
+#include "llvm/Support/IRBuilder.h"
 #include &lt;cstdio&gt;
 #include &lt;string&gt;
 #include &lt;map&gt;
@@ -735,7 +738,7 @@ static int gettok() {
   if (LastChar == '#') {
     // Comment until end of line.
     do LastChar = getchar();
-    while (LastChar != EOF &amp;&amp; LastChar != '\n' &amp; LastChar != '\r');
+    while (LastChar != EOF &amp;&amp; LastChar != '\n' &amp;&amp; LastChar != '\r');
     
     if (LastChar != EOF)
       return gettok();
@@ -878,7 +881,7 @@ static ExprAST *ParseIdentifierExpr() {
       if (CurTok == ')') break;
     
       if (CurTok != ',')
-        return Error("Expected ')'");
+        return Error("Expected ')' or ',' in argument list");
       getNextToken();
     }
   }
@@ -1020,7 +1023,7 @@ static PrototypeAST *ParseExtern() {
 //===----------------------------------------------------------------------===//
 
 static Module *TheModule;
-static LLVMBuilder Builder;
+static IRBuilder Builder;
 static std::map&lt;std::string, Value*&gt; NamedValues;
 
 Value *ErrorV(const char *Str) { Error(Str); return 0; }
@@ -1076,7 +1079,7 @@ Function *PrototypeAST::Codegen() {
   std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::DoubleTy);
   FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
   
-  Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
+  Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
   
   // If F conflicted, there was already something named 'Name'.  If it has a
   // body, don't allow redefinition or reextern.
@@ -1119,7 +1122,7 @@ Function *FunctionAST::Codegen() {
     return 0;
   
   // Create a new basic block to start insertion into.
-  BasicBlock *BB = new BasicBlock("entry", TheFunction);
+  BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
   Builder.SetInsertPoint(BB);
   
   if (Value *RetVal = Body-&gt;Codegen()) {
@@ -1228,6 +1231,7 @@ int main() {
 }
 </pre>
 </div>
+<a href="LangImpl4.html">Next: Adding JIT and Optimizer Support</a>
 </div>
 
 <!-- *********************************************************************** -->