X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2Ftutorial%2FLangImpl3.html;h=2acd3ddb5314aedaf4995981abaec8d698196016;hb=6fa1c051dc515b6fd1f9a26ac12fed985469bff5;hp=768a7838424e1f6b0914c3fa227c58a0b505165d;hpb=c80c23ff63a2c29ac5cd3ebb4e61c41ea278ddc0;p=oota-llvm.git diff --git a/docs/tutorial/LangImpl3.html b/docs/tutorial/LangImpl3.html index 768a7838424..2acd3ddb531 100644 --- a/docs/tutorial/LangImpl3.html +++ b/docs/tutorial/LangImpl3.html @@ -48,7 +48,11 @@ parser than it is to generate LLVM IR code. :)

Please note: the code in this chapter and later require LLVM 2.2 or -LLVM SVN to work. LLVM 2.1 and before will not work with it.

+later. LLVM 2.1 and before will not work with it. Also note that you need +to use a version of this tutorial that matches your LLVM release: If you are +using an official LLVM release, use the version of the documentation included +with your release or on the llvm.org +releases page.

@@ -59,8 +63,8 @@ LLVM SVN to work. LLVM 2.1 and before will not work with it.

-In order to generate LLVM IR, we want some simple setup to get started. First, -we define virtual codegen methods in each AST class:

+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:

@@ -95,9 +99,11 @@ href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
 Assignment - the concepts are really quite natural once you grok them.

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.

+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.

The second thing we want is an "Error" method like we used for the parser, which will @@ -109,7 +115,7 @@ undeclared parameter):

Value *ErrorV(const char *Str) { Error(Str); return 0; } static Module *TheModule; -static LLVMBuilder Builder; +static IRBuilder Builder; static std::map<std::string, Value*> NamedValues;
@@ -121,16 +127,15 @@ uses to contain code.

The Builder object is a helper object that makes it easy to generate LLVM instructions. Instances of the LLVMBuilder -class keep track of the current place to -insert instructions and has methods to create new instructions.

+href="http://llvm.org/doxygen/IRBuilder_8h-source.html">IRBuilder +class keep track of the current place to insert instructions and has methods to +create new instructions.

The NamedValues 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.

+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.

With these basics in place, we can start talking about how to generate code for @@ -148,13 +153,13 @@ has already been done, and we'll just use it to emit code.

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:

 Value *NumberExprAST::Codegen() {
-  return ConstantFP::get(Type::DoubleTy, APFloat(Val));
+  return ConstantFP::get(APFloat(Val));
 }
 
@@ -165,7 +170,7 @@ internally (APFloat has the capability of holding floating point constants of Arbitrary Precision). This code basically just creates and returns a ConstantFP. 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(..)".

+uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::Create(..)".

@@ -215,14 +220,16 @@ code, we do a simple switch on the opcode to create the right LLVM instruction.
 

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 CreateAdd), which operands to use (L and R 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.

+for the generated instruction.

+ +

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.

LLVM instructions are constrained by strict rules: for example, the Left and Right operators of @@ -303,7 +310,7 @@ Function *PrototypeAST::Codegen() { std::vector<const Type*> 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);

@@ -324,7 +331,7 @@ don't "new" a type, you "get" it.

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. "external linkage" +module to insert into. "external linkage" 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 "TheModule" is specified, this name is registered @@ -432,7 +439,7 @@ is an LLVM Function object that is ready to go for us.

   // 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->Codegen()) {
@@ -677,7 +684,7 @@ our makefile/command line about which options to use:

#include "llvm/DerivedTypes.h" #include "llvm/Module.h" #include "llvm/Analysis/Verifier.h" -#include "llvm/Support/LLVMBuilder.h" +#include "llvm/Support/IRBuilder.h" #include <cstdio> #include <string> #include <map> @@ -878,7 +885,7 @@ static ExprAST *ParseIdentifierExpr() { if (CurTok == ')') break; if (CurTok != ',') - return Error("Expected ')'"); + return Error("Expected ')' or ',' in argument list"); getNextToken(); } } @@ -1020,13 +1027,13 @@ static PrototypeAST *ParseExtern() { //===----------------------------------------------------------------------===// static Module *TheModule; -static LLVMBuilder Builder; +static IRBuilder Builder; static std::map<std::string, Value*> NamedValues; Value *ErrorV(const char *Str) { Error(Str); return 0; } Value *NumberExprAST::Codegen() { - return ConstantFP::get(Type::DoubleTy, APFloat(Val)); + return ConstantFP::get(APFloat(Val)); } Value *VariableExprAST::Codegen() { @@ -1076,7 +1083,7 @@ Function *PrototypeAST::Codegen() { std::vector<const Type*> 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 +1126,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->Codegen()) { @@ -1228,6 +1235,7 @@ int main() { }
+Next: Adding JIT and Optimizer Support