X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=docs%2Ftutorial%2FLangImpl7.html;h=b2b26bdfa07087ed9259ed742a4db624eca61548;hb=10077194ece7fe7201a4a35b1e93a74112aae60a;hp=040d6e0e232932c5fe0145fa124b006a6c332f7b;hpb=422094c38dfe6878de40f4ad443dcd77542fac22;p=oota-llvm.git diff --git a/docs/tutorial/LangImpl7.html b/docs/tutorial/LangImpl7.html index 040d6e0e232..b2b26bdfa07 100644 --- a/docs/tutorial/LangImpl7.html +++ b/docs/tutorial/LangImpl7.html @@ -12,7 +12,7 @@
-Welcome to Chapter 7 of the "Implementing a language with LLVM" tutorial. In chapters 1 through 6, we've built a very @@ -66,10 +66,10 @@ support for this, though the way it works is a bit unexpected for some.
To understand why mutable variables cause complexities in SSA construction, @@ -140,10 +140,10 @@ logic.
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. @@ -321,11 +321,10 @@ variables now!
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 @@ -378,11 +377,10 @@ Kaleidoscope to support new variable definitions.
The symbol table in Kaleidoscope is managed at code generation time by the @@ -480,7 +478,7 @@ the unabridged code):
// Reload, increment, and restore the alloca. This handles the case where // the body of the loop mutates the variable. Value *CurVar = Builder.CreateLoad(Alloca); - Value *NextVar = Builder.CreateAdd(CurVar, StepVal, "nextvar"); + Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); Builder.CreateStore(NextVar, Alloca); ... @@ -557,12 +555,12 @@ then: ; preds = %entry else: ; preds = %entry %x3 = load double* %x1 - %subtmp = sub double %x3, 1.000000e+00 - %calltmp = call double @fib( double %subtmp ) + %subtmp = fsub double %x3, 1.000000e+00 + %calltmp = call double @fib(double %subtmp) %x4 = load double* %x1 - %subtmp5 = sub double %x4, 2.000000e+00 - %calltmp6 = call double @fib( double %subtmp5 ) - %addtmp = add double %calltmp, %calltmp6 + %subtmp5 = fsub double %x4, 2.000000e+00 + %calltmp6 = call double @fib(double %subtmp5) + %addtmp = fadd double %calltmp, %calltmp6 br label %ifcont ifcont: ; preds = %else, %then @@ -595,11 +593,11 @@ then: br label %ifcont else: - %subtmp = sub double %x, 1.000000e+00 - %calltmp = call double @fib( double %subtmp ) - %subtmp5 = sub double %x, 2.000000e+00 - %calltmp6 = call double @fib( double %subtmp5 ) - %addtmp = add double %calltmp, %calltmp6 + %subtmp = fsub double %x, 1.000000e+00 + %calltmp = call double @fib(double %subtmp) + %subtmp5 = fsub double %x, 2.000000e+00 + %calltmp6 = call double @fib(double %subtmp5) + %addtmp = fadd double %calltmp, %calltmp6 br label %ifcont ifcont: ; preds = %else, %then @@ -625,11 +623,11 @@ entry: br i1 %ifcond, label %else, label %ifcont else: - %subtmp = sub double %x, 1.000000e+00 - %calltmp = call double @fib( double %subtmp ) - %subtmp5 = sub double %x, 2.000000e+00 - %calltmp6 = call double @fib( double %subtmp5 ) - %addtmp = add double %calltmp, %calltmp6 + %subtmp = fsub double %x, 1.000000e+00 + %calltmp = call double @fib(double %subtmp) + %subtmp5 = fsub double %x, 2.000000e+00 + %calltmp6 = call double @fib(double %subtmp5) + %addtmp = fadd double %calltmp, %calltmp6 ret double %addtmp ifcont: @@ -648,10 +646,10 @@ we'll add the assignment operator.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 @@ -745,11 +743,10 @@ add this next!
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. @@ -979,10 +976,10 @@ anywhere in sight.
Here is the complete code listing for our running example, enhanced with mutable @@ -1004,13 +1001,12 @@ variables and var/in support. To build this example, use:
#include "llvm/DerivedTypes.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" -#include "llvm/ExecutionEngine/Interpreter.h" #include "llvm/ExecutionEngine/JIT.h" #include "llvm/LLVMContext.h" #include "llvm/Module.h" -#include "llvm/ModuleProvider.h" #include "llvm/PassManager.h" #include "llvm/Analysis/Verifier.h" +#include "llvm/Analysis/Passes.h" #include "llvm/Target/TargetData.h" #include "llvm/Target/TargetSelect.h" #include "llvm/Transforms/Scalar.h" @@ -1197,7 +1193,8 @@ public: }; /// PrototypeAST - This class represents the "prototype" for a function, -/// which captures its argument names as well as if it is an operator. +/// which captures its name, and its argument names (thus implicitly the number +/// of arguments the function takes), as well as if it is an operator. class PrototypeAST { std::string Name; std::vector<std::string> Args; @@ -1239,7 +1236,7 @@ public: //===----------------------------------------------------------------------===// /// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current -/// token the parser it looking at. getNextToken reads another token from the +/// token the parser is looking at. getNextToken reads another token from the /// lexer and updates CurTok with its results. static int CurTok; static int getNextToken() { @@ -1287,9 +1284,9 @@ static ExprAST *ParseIdentifierExpr() { ExprAST *Arg = ParseExpression(); if (!Arg) return 0; Args.push_back(Arg); - + if (CurTok == ')') break; - + if (CurTok != ',') return Error("Expected ')' or ',' in argument list"); getNextToken(); @@ -1434,7 +1431,6 @@ static ExprAST *ParseVarExpr() { return new VarExprAST(VarNames, Body); } - /// primary /// ::= identifierexpr /// ::= numberexpr @@ -1520,7 +1516,7 @@ static ExprAST *ParseExpression() { static PrototypeAST *ParsePrototype() { std::string FnName; - int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. + unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. unsigned BinaryPrecedence = 30; switch (CurTok) { @@ -1622,10 +1618,10 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, const std::string &VarName) { IRBuilder<> TmpB(&TheFunction->getEntryBlock(), TheFunction->getEntryBlock().begin()); - return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0, VarName.c_str()); + return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0, + VarName.c_str()); } - Value *NumberExprAST::Codegen() { return ConstantFP::get(getGlobalContext(), APFloat(Val)); } @@ -1650,7 +1646,6 @@ Value *UnaryExprAST::Codegen() { return Builder.CreateCall(F, OperandV, "unop"); } - Value *BinaryExprAST::Codegen() { // Special case '=' because we don't want to emit the LHS as an expression. if (Op == '=') { @@ -1670,15 +1665,14 @@ Value *BinaryExprAST::Codegen() { return Val; } - Value *L = LHS->Codegen(); Value *R = RHS->Codegen(); if (L == 0 || R == 0) return 0; switch (Op) { - case '+': return Builder.CreateAdd(L, R, "addtmp"); - case '-': return Builder.CreateSub(L, R, "subtmp"); - case '*': return Builder.CreateMul(L, R, "multmp"); + case '+': return Builder.CreateFAdd(L, R, "addtmp"); + case '-': return Builder.CreateFSub(L, R, "subtmp"); + case '*': return Builder.CreateFMul(L, R, "multmp"); case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 @@ -1758,7 +1752,7 @@ Value *IfExprAST::Codegen() { // Emit merge block. TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); - PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), + PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp"); PN->addIncoming(ThenV, ThenBB); @@ -1801,7 +1795,6 @@ Value *ForExprAST::Codegen() { // Make the new basic block for the loop header, inserting after current // block. - BasicBlock *PreheaderBB = Builder.GetInsertBlock(); BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction); // Insert an explicit fall through from the current block to the LoopBB. @@ -1838,7 +1831,7 @@ Value *ForExprAST::Codegen() { // Reload, increment, and restore the alloca. This handles the case where // the body of the loop mutates the variable. Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str()); - Value *NextVar = Builder.CreateAdd(CurVar, StepVal, "nextvar"); + Value *NextVar = Builder.CreateFAdd(CurVar, StepVal, "nextvar"); Builder.CreateStore(NextVar, Alloca); // Convert condition to a bool by comparing equal to 0.0. @@ -1847,7 +1840,6 @@ Value *ForExprAST::Codegen() { "loopcond"); // Create the "after loop" block and insert it. - BasicBlock *LoopEndBB = Builder.GetInsertBlock(); BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); // Insert the conditional branch into the end of LoopEndBB. @@ -1913,7 +1905,6 @@ Value *VarExprAST::Codegen() { return BodyVal; } - Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. std::vector<const Type*> Doubles(Args.size(), @@ -1968,7 +1959,6 @@ void PrototypeAST::CreateArgumentAllocas(Function *F) { } } - Function *FunctionAST::Codegen() { NamedValues.clear(); @@ -1986,7 +1976,7 @@ Function *FunctionAST::Codegen() { // Add all arguments to the symbol table and create their allocas. Proto->CreateArgumentAllocas(TheFunction); - + if (Value *RetVal = Body->Codegen()) { // Finish off the function. Builder.CreateRet(RetVal); @@ -2039,7 +2029,7 @@ static void HandleExtern() { } static void HandleTopLevelExpression() { - // Evaluate a top level expression into an anonymous function. + // Evaluate a top-level expression into an anonymous function. if (FunctionAST *F = ParseTopLevelExpr()) { if (Function *LF = F->Codegen()) { // JIT the function, returning a function pointer. @@ -2062,7 +2052,7 @@ static void MainLoop() { fprintf(stderr, "ready> "); switch (CurTok) { case tok_eof: return; - case ';': getNextToken(); break; // ignore top level semicolons. + case ';': getNextToken(); break; // ignore top-level semicolons. case tok_def: HandleDefinition(); break; case tok_extern: HandleExtern(); break; default: HandleTopLevelExpression(); break; @@ -2070,8 +2060,6 @@ static void MainLoop() { } } - - //===----------------------------------------------------------------------===// // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// @@ -2095,6 +2083,9 @@ double printd(double X) { //===----------------------------------------------------------------------===// int main() { + InitializeNativeTarget(); + LLVMContext &Context = getGlobalContext(); + // Install standard binary operators. // 1 is lowest precedence. BinopPrecedence['='] = 2; @@ -2108,19 +2099,23 @@ int main() { getNextToken(); // Make the module, which holds all the code. - TheModule = new Module("my cool jit", getGlobalContext()); - - ExistingModuleProvider *OurModuleProvider = - new ExistingModuleProvider(TheModule); - - // Create the JIT. This takes ownership of the module and module provider. - TheExecutionEngine = EngineBuilder(OurModuleProvider).create(); + TheModule = new Module("my cool jit", Context); + + // Create the JIT. This takes ownership of the module. + std::string ErrStr; + TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create(); + if (!TheExecutionEngine) { + fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str()); + exit(1); + } - FunctionPassManager OurFPM(OurModuleProvider); + FunctionPassManager OurFPM(TheModule); // Set up the optimizer pipeline. Start with registering info about how the // target lays out data structures. OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData())); + // Provide basic AliasAnalysis support for GVN. + OurFPM.add(createBasicAliasAnalysisPass()); // Promote allocas to registers. OurFPM.add(createPromoteMemoryToRegisterPass()); // Do simple "peephole" optimizations and bit-twiddling optzns. @@ -2162,8 +2157,8 @@ int main() { src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"> Chris Lattner
- The LLVM Compiler Infrastructure
- Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $ + The LLVM Compiler Infrastructure
+ Last modified: $Date$