From: Erick Tryzelaar Date: Tue, 22 Sep 2009 21:14:49 +0000 (+0000) Subject: Sync c++ kaleidoscope tutorial with test. X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=commitdiff_plain;h=fd1ec5e68b593a4f4d5497b150e677ebef36c231 Sync c++ kaleidoscope tutorial with test. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82572 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/docs/tutorial/LangImpl2.html b/docs/tutorial/LangImpl2.html index 30215608a8c..5bcd0dd2c7f 100644 --- a/docs/tutorial/LangImpl2.html +++ b/docs/tutorial/LangImpl2.html @@ -84,7 +84,7 @@ public: class NumberExprAST : public ExprAST { double Val; public: - explicit NumberExprAST(double val) : Val(val) {} + NumberExprAST(double val) : Val(val) {} }; @@ -107,7 +107,7 @@ in the basic form of the Kaleidoscope language: class VariableExprAST : public ExprAST { std::string Name; public: - explicit VariableExprAST(const std::string &name) : Name(name) {} + VariableExprAST(const std::string &name) : Name(name) {} }; /// BinaryExprAST - Expression class for a binary operator. @@ -333,9 +333,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(); @@ -833,7 +833,7 @@ enum Token { tok_def = -2, tok_extern = -3, // primary - tok_identifier = -4, tok_number = -5, + tok_identifier = -4, tok_number = -5 }; static std::string IdentifierStr; // Filled in if tok_identifier @@ -901,14 +901,14 @@ public: class NumberExprAST : public ExprAST { double Val; public: - explicit NumberExprAST(double val) : Val(val) {} + NumberExprAST(double val) : Val(val) {} }; /// VariableExprAST - Expression class for referencing a variable, like "a". class VariableExprAST : public ExprAST { std::string Name; public: - explicit VariableExprAST(const std::string &name) : Name(name) {} + VariableExprAST(const std::string &name) : Name(name) {} }; /// BinaryExprAST - Expression class for a binary operator. @@ -1004,9 +1004,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(); @@ -1150,7 +1150,7 @@ static PrototypeAST *ParseExtern() { //===----------------------------------------------------------------------===// static void HandleDefinition() { - if (FunctionAST *F = ParseDefinition()) { + if (ParseDefinition()) { fprintf(stderr, "Parsed a function definition.\n"); } else { // Skip token for error recovery. @@ -1159,7 +1159,7 @@ static void HandleDefinition() { } static void HandleExtern() { - if (PrototypeAST *P = ParseExtern()) { + if (ParseExtern()) { fprintf(stderr, "Parsed an extern\n"); } else { // Skip token for error recovery. @@ -1169,7 +1169,7 @@ static void HandleExtern() { static void HandleTopLevelExpression() { // Evaluate a top-level expression into an anonymous function. - if (FunctionAST *F = ParseTopLevelExpr()) { + if (ParseTopLevelExpr()) { fprintf(stderr, "Parsed a top-level expr\n"); } else { // Skip token for error recovery. @@ -1207,7 +1207,9 @@ int main() { fprintf(stderr, "ready> "); getNextToken(); + // Run the main "interpreter loop" now. MainLoop(); + return 0; } diff --git a/docs/tutorial/LangImpl3.html b/docs/tutorial/LangImpl3.html index 056e2134fb5..bc5db46d905 100644 --- a/docs/tutorial/LangImpl3.html +++ b/docs/tutorial/LangImpl3.html @@ -79,7 +79,7 @@ public: class NumberExprAST : public ExprAST { double Val; public: - explicit NumberExprAST(double val) : Val(val) {} + NumberExprAST(double val) : Val(val) {} virtual Value *Codegen(); }; ... @@ -464,9 +464,10 @@ block at this point. We'll fix this in Chapter 5 : if (Value *RetVal = Body->Codegen()) { // Finish off the function. Builder.CreateRet(RetVal); - + // Validate the generated code, checking for consistency. verifyFunction(*TheFunction); + return TheFunction; } @@ -708,7 +709,7 @@ enum Token { tok_def = -2, tok_extern = -3, // primary - tok_identifier = -4, tok_number = -5, + tok_identifier = -4, tok_number = -5 }; static std::string IdentifierStr; // Filled in if tok_identifier @@ -777,7 +778,7 @@ public: class NumberExprAST : public ExprAST { double Val; public: - explicit NumberExprAST(double val) : Val(val) {} + NumberExprAST(double val) : Val(val) {} virtual Value *Codegen(); }; @@ -785,7 +786,7 @@ public: class VariableExprAST : public ExprAST { std::string Name; public: - explicit VariableExprAST(const std::string &name) : Name(name) {} + VariableExprAST(const std::string &name) : Name(name) {} virtual Value *Codegen(); }; @@ -810,7 +811,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). class PrototypeAST { std::string Name; std::vector<std::string> Args; @@ -837,7 +839,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() { @@ -885,9 +887,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(); @@ -1058,7 +1060,8 @@ Value *BinaryExprAST::Codegen() { case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 - return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), "booltmp"); + return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), + "booltmp"); default: return ErrorV("invalid binary operator"); } } @@ -1138,9 +1141,10 @@ Function *FunctionAST::Codegen() { if (Value *RetVal = Body->Codegen()) { // Finish off the function. Builder.CreateRet(RetVal); - + // Validate the generated code, checking for consistency. verifyFunction(*TheFunction); + return TheFunction; } @@ -1178,7 +1182,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()) { fprintf(stderr, "Read top-level expression:"); @@ -1196,7 +1200,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; @@ -1204,8 +1208,6 @@ static void MainLoop() { } } - - //===----------------------------------------------------------------------===// // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// @@ -1222,7 +1224,7 @@ double putchard(double X) { //===----------------------------------------------------------------------===// int main() { - TheModule = new Module("my cool jit", getGlobalContext()); + LLVMContext &Context = getGlobalContext(); // Install standard binary operators. // 1 is lowest precedence. @@ -1235,8 +1237,15 @@ int main() { fprintf(stderr, "ready> "); getNextToken(); + // Make the module, which holds all the code. + TheModule = new Module("my cool jit", Context); + + // Run the main "interpreter loop" now. MainLoop(); + + // Print out all of the generated code. TheModule->dump(); + return 0; } diff --git a/docs/tutorial/LangImpl4.html b/docs/tutorial/LangImpl4.html index d05c7ca082a..8310b61a75d 100644 --- a/docs/tutorial/LangImpl4.html +++ b/docs/tutorial/LangImpl4.html @@ -324,7 +324,7 @@ top-level expression to look like this:

 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()) {
       LF->dump();  // Dump the function for exposition purposes.
@@ -334,7 +334,7 @@ static void HandleTopLevelExpression() {
       
       // Cast it to the right type (takes no arguments, returns a double) so we
       // can call it as a native function.
-      double (*FP)() = (double (*)())FPtr;
+      double (*FP)() = (double (*)())(intptr_t)FPtr;
       fprintf(stderr, "Evaluated to %f\n", FP());
     }
 
@@ -363,7 +363,7 @@ entry:

Well this looks like it is basically working. The dump of the function shows the "no argument function that always returns double" that we synthesize -for each top level expression that is typed in. This demonstrates very basic +for each top-level expression that is typed in. This demonstrates very basic functionality, but can we do more?

@@ -499,7 +499,7 @@ LLVM JIT and optimizer. To build this example, use:
    # Compile
-   g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
+   g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit interpreter native` -O3 -o toy
    # Run
    ./toy
 
@@ -546,7 +546,7 @@ enum Token { tok_def = -2, tok_extern = -3, // primary - tok_identifier = -4, tok_number = -5, + tok_identifier = -4, tok_number = -5 }; static std::string IdentifierStr; // Filled in if tok_identifier @@ -648,7 +648,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). class PrototypeAST { std::string Name; std::vector<std::string> Args; @@ -675,7 +676,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() { @@ -723,9 +724,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(); @@ -1024,7 +1025,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. @@ -1047,7 +1048,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; @@ -1055,8 +1056,6 @@ static void MainLoop() { } } - - //===----------------------------------------------------------------------===// // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// diff --git a/docs/tutorial/LangImpl5.html b/docs/tutorial/LangImpl5.html index ea70e2c12d8..f93b59be0dc 100644 --- a/docs/tutorial/LangImpl5.html +++ b/docs/tutorial/LangImpl5.html @@ -472,7 +472,8 @@ are emitted, we can finish up with the merge code:

// Emit merge block. TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); - PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), "iftmp"); + PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), + "iftmp"); PN->addIncoming(ThenV, ThenBB); PN->addIncoming(ElseV, ElseBB); @@ -1062,7 +1063,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). class PrototypeAST { std::string Name; std::vector<std::string> Args; @@ -1089,7 +1091,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() { @@ -1137,9 +1139,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(); @@ -1239,7 +1241,6 @@ static ExprAST *ParseForExpr() { return new ForExprAST(IdName, Start, End, Step, Body); } - /// primary /// ::= identifierexpr /// ::= numberexpr @@ -1550,7 +1551,7 @@ Value *ForExprAST::Codegen() { // for expr always returns 0.0. - return getGlobalContext().getNullValue(Type::getDoubleTy(getGlobalContext())); + return Constant::getNullValue(Type::getDoubleTy(getGlobalContext())); } Function *PrototypeAST::Codegen() { @@ -1655,7 +1656,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. @@ -1678,7 +1679,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; @@ -1686,8 +1687,6 @@ static void MainLoop() { } } - - //===----------------------------------------------------------------------===// // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// @@ -1704,6 +1703,9 @@ double putchard(double X) { //===----------------------------------------------------------------------===// int main() { + InitializeNativeTarget(); + LLVMContext &Context = getGlobalContext(); + // Install standard binary operators. // 1 is lowest precedence. BinopPrecedence['<'] = 10; @@ -1716,7 +1718,7 @@ int main() { getNextToken(); // Make the module, which holds all the code. - TheModule = new Module("my cool jit", getGlobalContext()); + TheModule = new Module("my cool jit", Context); ExistingModuleProvider *OurModuleProvider = new ExistingModuleProvider(TheModule); diff --git a/docs/tutorial/LangImpl6.html b/docs/tutorial/LangImpl6.html index c2ac84c866b..f113e96651e 100644 --- a/docs/tutorial/LangImpl6.html +++ b/docs/tutorial/LangImpl6.html @@ -306,7 +306,7 @@ function call to it. Since user-defined operators are just built as normal functions (because the "prototype" boils down to a function with the right name) everything falls into place.

-

The final piece of code we are missing, is a bit of top level magic:

+

The final piece of code we are missing, is a bit of top-level magic:

@@ -795,7 +795,6 @@ add variable mutation without building SSA in your front-end.

- @@ -998,7 +997,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; @@ -1038,7 +1038,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() { @@ -1086,9 +1086,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(); @@ -1188,7 +1188,6 @@ static ExprAST *ParseForExpr() { return new ForExprAST(IdName, Start, End, Step, Body); } - /// primary /// ::= identifierexpr /// ::= numberexpr @@ -1272,7 +1271,7 @@ static ExprAST *ParseExpression() { static PrototypeAST *ParsePrototype() { std::string FnName; - unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. + unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. unsigned BinaryPrecedence = 30; switch (CurTok) { @@ -1389,7 +1388,6 @@ Value *UnaryExprAST::Codegen() { return Builder.CreateCall(F, OperandV, "unop"); } - Value *BinaryExprAST::Codegen() { Value *L = LHS->Codegen(); Value *R = RHS->Codegen(); @@ -1402,7 +1400,8 @@ Value *BinaryExprAST::Codegen() { case '<': L = Builder.CreateFCmpULT(L, R, "cmptmp"); // Convert bool 0/1 to double 0.0 or 1.0 - return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), "booltmp"); + return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()), + "booltmp"); default: break; } @@ -1687,7 +1686,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. @@ -1710,7 +1709,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; @@ -1718,8 +1717,6 @@ static void MainLoop() { } } - - //===----------------------------------------------------------------------===// // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// @@ -1743,6 +1740,9 @@ double printd(double X) { //===----------------------------------------------------------------------===// int main() { + InitializeNativeTarget(); + LLVMContext &Context = getGlobalContext(); + // Install standard binary operators. // 1 is lowest precedence. BinopPrecedence['<'] = 10; @@ -1755,7 +1755,7 @@ int main() { getNextToken(); // Make the module, which holds all the code. - TheModule = new Module("my cool jit", getGlobalContext()); + TheModule = new Module("my cool jit", Context); ExistingModuleProvider *OurModuleProvider = new ExistingModuleProvider(TheModule); diff --git a/docs/tutorial/LangImpl7.html b/docs/tutorial/LangImpl7.html index 040d6e0e232..ec07fa88d4b 100644 --- a/docs/tutorial/LangImpl7.html +++ b/docs/tutorial/LangImpl7.html @@ -1197,7 +1197,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 +1240,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 +1288,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 +1435,6 @@ static ExprAST *ParseVarExpr() { return new VarExprAST(VarNames, Body); } - /// primary /// ::= identifierexpr /// ::= numberexpr @@ -1520,7 +1520,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 +1622,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 +1650,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,7 +1669,6 @@ Value *BinaryExprAST::Codegen() { return Val; } - Value *L = LHS->Codegen(); Value *R = RHS->Codegen(); if (L == 0 || R == 0) return 0; @@ -1801,7 +1799,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. @@ -1847,7 +1844,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 +1909,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 +1963,6 @@ void PrototypeAST::CreateArgumentAllocas(Function *F) { } } - Function *FunctionAST::Codegen() { NamedValues.clear(); @@ -1986,7 +1980,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 +2033,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 +2056,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 +2064,6 @@ static void MainLoop() { } } - - //===----------------------------------------------------------------------===// // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// @@ -2095,6 +2087,9 @@ double printd(double X) { //===----------------------------------------------------------------------===// int main() { + InitializeNativeTarget(); + LLVMContext &Context = getGlobalContext(); + // Install standard binary operators. // 1 is lowest precedence. BinopPrecedence['='] = 2; @@ -2108,7 +2103,7 @@ int main() { getNextToken(); // Make the module, which holds all the code. - TheModule = new Module("my cool jit", getGlobalContext()); + TheModule = new Module("my cool jit", Context); ExistingModuleProvider *OurModuleProvider = new ExistingModuleProvider(TheModule); diff --git a/examples/Kaleidoscope/toy.cpp b/examples/Kaleidoscope/toy.cpp index 8e02e9ab61f..8b0c321c06c 100644 --- a/examples/Kaleidoscope/toy.cpp +++ b/examples/Kaleidoscope/toy.cpp @@ -235,7 +235,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() { @@ -283,9 +283,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(); @@ -430,7 +430,6 @@ static ExprAST *ParseVarExpr() { return new VarExprAST(VarNames, Body); } - /// primary /// ::= identifierexpr /// ::= numberexpr @@ -516,7 +515,7 @@ static ExprAST *ParseExpression() { static PrototypeAST *ParsePrototype() { std::string FnName; - unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. + unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. unsigned BinaryPrecedence = 30; switch (CurTok) { @@ -622,7 +621,6 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, VarName.c_str()); } - Value *NumberExprAST::Codegen() { return ConstantFP::get(getGlobalContext(), APFloat(Val)); } @@ -647,7 +645,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 == '=') { @@ -667,7 +664,6 @@ Value *BinaryExprAST::Codegen() { return Val; } - Value *L = LHS->Codegen(); Value *R = RHS->Codegen(); if (L == 0 || R == 0) return 0; @@ -908,7 +904,6 @@ Value *VarExprAST::Codegen() { return BodyVal; } - Function *PrototypeAST::Codegen() { // Make the function type: double(double,double) etc. std::vector Doubles(Args.size(), @@ -963,7 +958,6 @@ void PrototypeAST::CreateArgumentAllocas(Function *F) { } } - Function *FunctionAST::Codegen() { NamedValues.clear(); @@ -981,7 +975,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); @@ -1034,7 +1028,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. @@ -1057,7 +1051,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; @@ -1065,8 +1059,6 @@ static void MainLoop() { } } - - //===----------------------------------------------------------------------===// // "Library" functions that can be "extern'd" from user code. //===----------------------------------------------------------------------===// @@ -1092,7 +1084,7 @@ double printd(double X) { int main() { InitializeNativeTarget(); LLVMContext &Context = getGlobalContext(); - + // Install standard binary operators. // 1 is lowest precedence. BinopPrecedence['='] = 2;