mirror of
				https://github.com/c64scene-ar/llvm-6502.git
				synced 2025-10-31 08:16:47 +00:00 
			
		
		
		
	Sync c++ kaleidoscope tutorial with test.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82572 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
		| @@ -84,7 +84,7 @@ public: | |||||||
| class NumberExprAST : public ExprAST { | class NumberExprAST : public ExprAST { | ||||||
|   double Val; |   double Val; | ||||||
| public: | public: | ||||||
|   explicit NumberExprAST(double val) : Val(val) {} |   NumberExprAST(double val) : Val(val) {} | ||||||
| }; | }; | ||||||
| </pre> | </pre> | ||||||
| </div> | </div> | ||||||
| @@ -107,7 +107,7 @@ in the basic form of the Kaleidoscope language: | |||||||
| class VariableExprAST : public ExprAST { | class VariableExprAST : public ExprAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
| public: | public: | ||||||
|   explicit VariableExprAST(const std::string &name) : Name(name) {} |   VariableExprAST(const std::string &name) : Name(name) {} | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /// BinaryExprAST - Expression class for a binary operator. | /// BinaryExprAST - Expression class for a binary operator. | ||||||
| @@ -833,7 +833,7 @@ enum Token { | |||||||
|   tok_def = -2, tok_extern = -3, |   tok_def = -2, tok_extern = -3, | ||||||
|  |  | ||||||
|   // primary |   // primary | ||||||
|   tok_identifier = -4, tok_number = -5, |   tok_identifier = -4, tok_number = -5 | ||||||
| }; | }; | ||||||
|  |  | ||||||
| static std::string IdentifierStr;  // Filled in if tok_identifier | static std::string IdentifierStr;  // Filled in if tok_identifier | ||||||
| @@ -901,14 +901,14 @@ public: | |||||||
| class NumberExprAST : public ExprAST { | class NumberExprAST : public ExprAST { | ||||||
|   double Val; |   double Val; | ||||||
| public: | public: | ||||||
|   explicit NumberExprAST(double val) : Val(val) {} |   NumberExprAST(double val) : Val(val) {} | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /// VariableExprAST - Expression class for referencing a variable, like "a". | /// VariableExprAST - Expression class for referencing a variable, like "a". | ||||||
| class VariableExprAST : public ExprAST { | class VariableExprAST : public ExprAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
| public: | public: | ||||||
|   explicit VariableExprAST(const std::string &name) : Name(name) {} |   VariableExprAST(const std::string &name) : Name(name) {} | ||||||
| }; | }; | ||||||
|  |  | ||||||
| /// BinaryExprAST - Expression class for a binary operator. | /// BinaryExprAST - Expression class for a binary operator. | ||||||
| @@ -1150,7 +1150,7 @@ static PrototypeAST *ParseExtern() { | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| static void HandleDefinition() { | static void HandleDefinition() { | ||||||
|   if (FunctionAST *F = ParseDefinition()) { |   if (ParseDefinition()) { | ||||||
|     fprintf(stderr, "Parsed a function definition.\n"); |     fprintf(stderr, "Parsed a function definition.\n"); | ||||||
|   } else { |   } else { | ||||||
|     // Skip token for error recovery. |     // Skip token for error recovery. | ||||||
| @@ -1159,7 +1159,7 @@ static void HandleDefinition() { | |||||||
| } | } | ||||||
|  |  | ||||||
| static void HandleExtern() { | static void HandleExtern() { | ||||||
|   if (PrototypeAST *P = ParseExtern()) { |   if (ParseExtern()) { | ||||||
|     fprintf(stderr, "Parsed an extern\n"); |     fprintf(stderr, "Parsed an extern\n"); | ||||||
|   } else { |   } else { | ||||||
|     // Skip token for error recovery. |     // Skip token for error recovery. | ||||||
| @@ -1169,7 +1169,7 @@ static void HandleExtern() { | |||||||
|  |  | ||||||
| static void HandleTopLevelExpression() { | 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 (ParseTopLevelExpr()) { | ||||||
|     fprintf(stderr, "Parsed a top-level expr\n"); |     fprintf(stderr, "Parsed a top-level expr\n"); | ||||||
|   } else { |   } else { | ||||||
|     // Skip token for error recovery. |     // Skip token for error recovery. | ||||||
| @@ -1207,7 +1207,9 @@ int main() { | |||||||
|   fprintf(stderr, "ready> "); |   fprintf(stderr, "ready> "); | ||||||
|   getNextToken(); |   getNextToken(); | ||||||
|  |  | ||||||
|  |   // Run the main "interpreter loop" now. | ||||||
|   MainLoop(); |   MainLoop(); | ||||||
|  |  | ||||||
|   return 0; |   return 0; | ||||||
| } | } | ||||||
| </pre> | </pre> | ||||||
|   | |||||||
| @@ -79,7 +79,7 @@ public: | |||||||
| class NumberExprAST : public ExprAST { | class NumberExprAST : public ExprAST { | ||||||
|   double Val; |   double Val; | ||||||
| public: | public: | ||||||
|   explicit NumberExprAST(double val) : Val(val) {} |   NumberExprAST(double val) : Val(val) {} | ||||||
|   <b>virtual Value *Codegen();</b> |   <b>virtual Value *Codegen();</b> | ||||||
| }; | }; | ||||||
| ... | ... | ||||||
| @@ -467,6 +467,7 @@ block at this point.  We'll fix this in <a href="LangImpl5.html">Chapter 5</a> : | |||||||
|  |  | ||||||
|     // Validate the generated code, checking for consistency. |     // Validate the generated code, checking for consistency. | ||||||
|     verifyFunction(*TheFunction); |     verifyFunction(*TheFunction); | ||||||
|  |  | ||||||
|     return TheFunction; |     return TheFunction; | ||||||
|   } |   } | ||||||
| </pre> | </pre> | ||||||
| @@ -708,7 +709,7 @@ enum Token { | |||||||
|   tok_def = -2, tok_extern = -3, |   tok_def = -2, tok_extern = -3, | ||||||
|  |  | ||||||
|   // primary |   // primary | ||||||
|   tok_identifier = -4, tok_number = -5, |   tok_identifier = -4, tok_number = -5 | ||||||
| }; | }; | ||||||
|  |  | ||||||
| static std::string IdentifierStr;  // Filled in if tok_identifier | static std::string IdentifierStr;  // Filled in if tok_identifier | ||||||
| @@ -777,7 +778,7 @@ public: | |||||||
| class NumberExprAST : public ExprAST { | class NumberExprAST : public ExprAST { | ||||||
|   double Val; |   double Val; | ||||||
| public: | public: | ||||||
|   explicit NumberExprAST(double val) : Val(val) {} |   NumberExprAST(double val) : Val(val) {} | ||||||
|   virtual Value *Codegen(); |   virtual Value *Codegen(); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -785,7 +786,7 @@ public: | |||||||
| class VariableExprAST : public ExprAST { | class VariableExprAST : public ExprAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
| public: | public: | ||||||
|   explicit VariableExprAST(const std::string &name) : Name(name) {} |   VariableExprAST(const std::string &name) : Name(name) {} | ||||||
|   virtual Value *Codegen(); |   virtual Value *Codegen(); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -810,7 +811,8 @@ public: | |||||||
| }; | }; | ||||||
|  |  | ||||||
| /// PrototypeAST - This class represents the "prototype" for a function, | /// 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 { | class PrototypeAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
|   std::vector<std::string> Args; |   std::vector<std::string> Args; | ||||||
| @@ -837,7 +839,7 @@ public: | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current | /// 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. | /// lexer and updates CurTok with its results. | ||||||
| static int CurTok; | static int CurTok; | ||||||
| static int getNextToken() { | static int getNextToken() { | ||||||
| @@ -1058,7 +1060,8 @@ Value *BinaryExprAST::Codegen() { | |||||||
|   case '<': |   case '<': | ||||||
|     L = Builder.CreateFCmpULT(L, R, "cmptmp"); |     L = Builder.CreateFCmpULT(L, R, "cmptmp"); | ||||||
|     // Convert bool 0/1 to double 0.0 or 1.0 |     // 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"); |   default: return ErrorV("invalid binary operator"); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -1141,6 +1144,7 @@ Function *FunctionAST::Codegen() { | |||||||
|  |  | ||||||
|     // Validate the generated code, checking for consistency. |     // Validate the generated code, checking for consistency. | ||||||
|     verifyFunction(*TheFunction); |     verifyFunction(*TheFunction); | ||||||
|  |  | ||||||
|     return TheFunction; |     return TheFunction; | ||||||
|   } |   } | ||||||
|    |    | ||||||
| @@ -1178,7 +1182,7 @@ static void HandleExtern() { | |||||||
| } | } | ||||||
|  |  | ||||||
| static void HandleTopLevelExpression() { | 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 (FunctionAST *F = ParseTopLevelExpr()) { | ||||||
|     if (Function *LF = F->Codegen()) { |     if (Function *LF = F->Codegen()) { | ||||||
|       fprintf(stderr, "Read top-level expression:"); |       fprintf(stderr, "Read top-level expression:"); | ||||||
| @@ -1196,7 +1200,7 @@ static void MainLoop() { | |||||||
|     fprintf(stderr, "ready> "); |     fprintf(stderr, "ready> "); | ||||||
|     switch (CurTok) { |     switch (CurTok) { | ||||||
|     case tok_eof:    return; |     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_def:    HandleDefinition(); break; | ||||||
|     case tok_extern: HandleExtern(); break; |     case tok_extern: HandleExtern(); break; | ||||||
|     default:         HandleTopLevelExpression(); break; |     default:         HandleTopLevelExpression(); break; | ||||||
| @@ -1204,8 +1208,6 @@ static void MainLoop() { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| // "Library" functions that can be "extern'd" from user code. | // "Library" functions that can be "extern'd" from user code. | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| @@ -1222,7 +1224,7 @@ double putchard(double X) { | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| int main() { | int main() { | ||||||
|   TheModule = new Module("my cool jit", getGlobalContext()); |   LLVMContext &Context = getGlobalContext(); | ||||||
|  |  | ||||||
|   // Install standard binary operators. |   // Install standard binary operators. | ||||||
|   // 1 is lowest precedence. |   // 1 is lowest precedence. | ||||||
| @@ -1235,8 +1237,15 @@ int main() { | |||||||
|   fprintf(stderr, "ready> "); |   fprintf(stderr, "ready> "); | ||||||
|   getNextToken(); |   getNextToken(); | ||||||
|  |  | ||||||
|  |   // Make the module, which holds all the code. | ||||||
|  |   TheModule = new Module("my cool jit", Context); | ||||||
|  |  | ||||||
|  |   // Run the main "interpreter loop" now. | ||||||
|   MainLoop(); |   MainLoop(); | ||||||
|  |  | ||||||
|  |   // Print out all of the generated code. | ||||||
|   TheModule->dump(); |   TheModule->dump(); | ||||||
|  |  | ||||||
|   return 0; |   return 0; | ||||||
| } | } | ||||||
| </pre> | </pre> | ||||||
|   | |||||||
| @@ -324,7 +324,7 @@ top-level expression to look like this:</p> | |||||||
| <div class="doc_code"> | <div class="doc_code"> | ||||||
| <pre> | <pre> | ||||||
| static void HandleTopLevelExpression() { | 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 (FunctionAST *F = ParseTopLevelExpr()) { | ||||||
|     if (Function *LF = F->Codegen()) { |     if (Function *LF = F->Codegen()) { | ||||||
|       LF->dump();  // Dump the function for exposition purposes. |       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 |       // Cast it to the right type (takes no arguments, returns a double) so we | ||||||
|       // can call it as a native function. |       // can call it as a native function. | ||||||
|       double (*FP)() = (double (*)())FPtr; |       double (*FP)() = (double (*)())(intptr_t)FPtr; | ||||||
|       fprintf(stderr, "Evaluated to %f\n", FP());</b> |       fprintf(stderr, "Evaluated to %f\n", FP());</b> | ||||||
|     } |     } | ||||||
| </pre> | </pre> | ||||||
| @@ -363,7 +363,7 @@ entry: | |||||||
|  |  | ||||||
| <p>Well this looks like it is basically working.  The dump of the function | <p>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 | 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?</p> | functionality, but can we do more?</p> | ||||||
|  |  | ||||||
| <div class="doc_code"> | <div class="doc_code"> | ||||||
| @@ -499,7 +499,7 @@ LLVM JIT and optimizer.  To build this example, use: | |||||||
| <div class="doc_code"> | <div class="doc_code"> | ||||||
| <pre> | <pre> | ||||||
|    # Compile |    # 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 |    # Run | ||||||
|    ./toy |    ./toy | ||||||
| </pre> | </pre> | ||||||
| @@ -546,7 +546,7 @@ enum Token { | |||||||
|   tok_def = -2, tok_extern = -3, |   tok_def = -2, tok_extern = -3, | ||||||
|  |  | ||||||
|   // primary |   // primary | ||||||
|   tok_identifier = -4, tok_number = -5, |   tok_identifier = -4, tok_number = -5 | ||||||
| }; | }; | ||||||
|  |  | ||||||
| static std::string IdentifierStr;  // Filled in if tok_identifier | static std::string IdentifierStr;  // Filled in if tok_identifier | ||||||
| @@ -648,7 +648,8 @@ public: | |||||||
| }; | }; | ||||||
|  |  | ||||||
| /// PrototypeAST - This class represents the "prototype" for a function, | /// 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 { | class PrototypeAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
|   std::vector<std::string> Args; |   std::vector<std::string> Args; | ||||||
| @@ -675,7 +676,7 @@ public: | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current | /// 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. | /// lexer and updates CurTok with its results. | ||||||
| static int CurTok; | static int CurTok; | ||||||
| static int getNextToken() { | static int getNextToken() { | ||||||
| @@ -1024,7 +1025,7 @@ static void HandleExtern() { | |||||||
| } | } | ||||||
|  |  | ||||||
| static void HandleTopLevelExpression() { | 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 (FunctionAST *F = ParseTopLevelExpr()) { | ||||||
|     if (Function *LF = F->Codegen()) { |     if (Function *LF = F->Codegen()) { | ||||||
|       // JIT the function, returning a function pointer. |       // JIT the function, returning a function pointer. | ||||||
| @@ -1047,7 +1048,7 @@ static void MainLoop() { | |||||||
|     fprintf(stderr, "ready> "); |     fprintf(stderr, "ready> "); | ||||||
|     switch (CurTok) { |     switch (CurTok) { | ||||||
|     case tok_eof:    return; |     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_def:    HandleDefinition(); break; | ||||||
|     case tok_extern: HandleExtern(); break; |     case tok_extern: HandleExtern(); break; | ||||||
|     default:         HandleTopLevelExpression(); break; |     default:         HandleTopLevelExpression(); break; | ||||||
| @@ -1055,8 +1056,6 @@ static void MainLoop() { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| // "Library" functions that can be "extern'd" from user code. | // "Library" functions that can be "extern'd" from user code. | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|   | |||||||
| @@ -472,7 +472,8 @@ are emitted, we can finish up with the merge code:</p> | |||||||
|   // Emit merge block. |   // Emit merge block. | ||||||
|   TheFunction->getBasicBlockList().push_back(MergeBB); |   TheFunction->getBasicBlockList().push_back(MergeBB); | ||||||
|   Builder.SetInsertPoint(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(ThenV, ThenBB); | ||||||
|   PN->addIncoming(ElseV, ElseBB); |   PN->addIncoming(ElseV, ElseBB); | ||||||
| @@ -1062,7 +1063,8 @@ public: | |||||||
| }; | }; | ||||||
|  |  | ||||||
| /// PrototypeAST - This class represents the "prototype" for a function, | /// 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 { | class PrototypeAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
|   std::vector<std::string> Args; |   std::vector<std::string> Args; | ||||||
| @@ -1089,7 +1091,7 @@ public: | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current | /// 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. | /// lexer and updates CurTok with its results. | ||||||
| static int CurTok; | static int CurTok; | ||||||
| static int getNextToken() { | static int getNextToken() { | ||||||
| @@ -1239,7 +1241,6 @@ static ExprAST *ParseForExpr() { | |||||||
|   return new ForExprAST(IdName, Start, End, Step, Body); |   return new ForExprAST(IdName, Start, End, Step, Body); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /// primary | /// primary | ||||||
| ///   ::= identifierexpr | ///   ::= identifierexpr | ||||||
| ///   ::= numberexpr | ///   ::= numberexpr | ||||||
| @@ -1550,7 +1551,7 @@ Value *ForExprAST::Codegen() { | |||||||
|  |  | ||||||
|    |    | ||||||
|   // for expr always returns 0.0. |   // for expr always returns 0.0. | ||||||
|   return getGlobalContext().getNullValue(Type::getDoubleTy(getGlobalContext())); |   return Constant::getNullValue(Type::getDoubleTy(getGlobalContext())); | ||||||
| } | } | ||||||
|  |  | ||||||
| Function *PrototypeAST::Codegen() { | Function *PrototypeAST::Codegen() { | ||||||
| @@ -1655,7 +1656,7 @@ static void HandleExtern() { | |||||||
| } | } | ||||||
|  |  | ||||||
| static void HandleTopLevelExpression() { | 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 (FunctionAST *F = ParseTopLevelExpr()) { | ||||||
|     if (Function *LF = F->Codegen()) { |     if (Function *LF = F->Codegen()) { | ||||||
|       // JIT the function, returning a function pointer. |       // JIT the function, returning a function pointer. | ||||||
| @@ -1678,7 +1679,7 @@ static void MainLoop() { | |||||||
|     fprintf(stderr, "ready> "); |     fprintf(stderr, "ready> "); | ||||||
|     switch (CurTok) { |     switch (CurTok) { | ||||||
|     case tok_eof:    return; |     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_def:    HandleDefinition(); break; | ||||||
|     case tok_extern: HandleExtern(); break; |     case tok_extern: HandleExtern(); break; | ||||||
|     default:         HandleTopLevelExpression(); break; |     default:         HandleTopLevelExpression(); break; | ||||||
| @@ -1686,8 +1687,6 @@ static void MainLoop() { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| // "Library" functions that can be "extern'd" from user code. | // "Library" functions that can be "extern'd" from user code. | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| @@ -1704,6 +1703,9 @@ double putchard(double X) { | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| int main() { | int main() { | ||||||
|  |   InitializeNativeTarget(); | ||||||
|  |   LLVMContext &Context = getGlobalContext(); | ||||||
|  |  | ||||||
|   // Install standard binary operators. |   // Install standard binary operators. | ||||||
|   // 1 is lowest precedence. |   // 1 is lowest precedence. | ||||||
|   BinopPrecedence['<'] = 10; |   BinopPrecedence['<'] = 10; | ||||||
| @@ -1716,7 +1718,7 @@ int main() { | |||||||
|   getNextToken(); |   getNextToken(); | ||||||
|  |  | ||||||
|   // Make the module, which holds all the code. |   // Make the module, which holds all the code. | ||||||
|   TheModule = new Module("my cool jit", getGlobalContext()); |   TheModule = new Module("my cool jit", Context); | ||||||
|  |  | ||||||
|   ExistingModuleProvider *OurModuleProvider = |   ExistingModuleProvider *OurModuleProvider = | ||||||
|       new ExistingModuleProvider(TheModule); |       new ExistingModuleProvider(TheModule); | ||||||
|   | |||||||
| @@ -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 | functions (because the "prototype" boils down to a function with the right | ||||||
| name) everything falls into place.</p> | name) everything falls into place.</p> | ||||||
|  |  | ||||||
| <p>The final piece of code we are missing, is a bit of top level magic:</p> | <p>The final piece of code we are missing, is a bit of top-level magic:</p> | ||||||
|  |  | ||||||
| <div class="doc_code"> | <div class="doc_code"> | ||||||
| <pre> | <pre> | ||||||
| @@ -795,7 +795,6 @@ add variable mutation without building SSA in your front-end.</p> | |||||||
|  |  | ||||||
| </div> | </div> | ||||||
|  |  | ||||||
|  |  | ||||||
| <!-- *********************************************************************** --> | <!-- *********************************************************************** --> | ||||||
| <div class="doc_section"><a name="code">Full Code Listing</a></div> | <div class="doc_section"><a name="code">Full Code Listing</a></div> | ||||||
| <!-- *********************************************************************** --> | <!-- *********************************************************************** --> | ||||||
| @@ -998,7 +997,8 @@ public: | |||||||
| }; | }; | ||||||
|  |  | ||||||
| /// PrototypeAST - This class represents the "prototype" for a function, | /// 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 { | class PrototypeAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
|   std::vector<std::string> Args; |   std::vector<std::string> Args; | ||||||
| @@ -1038,7 +1038,7 @@ public: | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current | /// 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. | /// lexer and updates CurTok with its results. | ||||||
| static int CurTok; | static int CurTok; | ||||||
| static int getNextToken() { | static int getNextToken() { | ||||||
| @@ -1188,7 +1188,6 @@ static ExprAST *ParseForExpr() { | |||||||
|   return new ForExprAST(IdName, Start, End, Step, Body); |   return new ForExprAST(IdName, Start, End, Step, Body); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /// primary | /// primary | ||||||
| ///   ::= identifierexpr | ///   ::= identifierexpr | ||||||
| ///   ::= numberexpr | ///   ::= numberexpr | ||||||
| @@ -1272,7 +1271,7 @@ static ExprAST *ParseExpression() { | |||||||
| static PrototypeAST *ParsePrototype() { | static PrototypeAST *ParsePrototype() { | ||||||
|   std::string FnName; |   std::string FnName; | ||||||
|    |    | ||||||
|   unsigned Kind = 0;  // 0 = identifier, 1 = unary, 2 = binary. |   unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. | ||||||
|   unsigned BinaryPrecedence = 30; |   unsigned BinaryPrecedence = 30; | ||||||
|    |    | ||||||
|   switch (CurTok) { |   switch (CurTok) { | ||||||
| @@ -1389,7 +1388,6 @@ Value *UnaryExprAST::Codegen() { | |||||||
|   return Builder.CreateCall(F, OperandV, "unop"); |   return Builder.CreateCall(F, OperandV, "unop"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Value *BinaryExprAST::Codegen() { | Value *BinaryExprAST::Codegen() { | ||||||
|   Value *L = LHS->Codegen(); |   Value *L = LHS->Codegen(); | ||||||
|   Value *R = RHS->Codegen(); |   Value *R = RHS->Codegen(); | ||||||
| @@ -1402,7 +1400,8 @@ Value *BinaryExprAST::Codegen() { | |||||||
|   case '<': |   case '<': | ||||||
|     L = Builder.CreateFCmpULT(L, R, "cmptmp"); |     L = Builder.CreateFCmpULT(L, R, "cmptmp"); | ||||||
|     // Convert bool 0/1 to double 0.0 or 1.0 |     // 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; |   default: break; | ||||||
|   } |   } | ||||||
|    |    | ||||||
| @@ -1687,7 +1686,7 @@ static void HandleExtern() { | |||||||
| } | } | ||||||
|  |  | ||||||
| static void HandleTopLevelExpression() { | 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 (FunctionAST *F = ParseTopLevelExpr()) { | ||||||
|     if (Function *LF = F->Codegen()) { |     if (Function *LF = F->Codegen()) { | ||||||
|       // JIT the function, returning a function pointer. |       // JIT the function, returning a function pointer. | ||||||
| @@ -1710,7 +1709,7 @@ static void MainLoop() { | |||||||
|     fprintf(stderr, "ready> "); |     fprintf(stderr, "ready> "); | ||||||
|     switch (CurTok) { |     switch (CurTok) { | ||||||
|     case tok_eof:    return; |     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_def:    HandleDefinition(); break; | ||||||
|     case tok_extern: HandleExtern(); break; |     case tok_extern: HandleExtern(); break; | ||||||
|     default:         HandleTopLevelExpression(); break; |     default:         HandleTopLevelExpression(); break; | ||||||
| @@ -1718,8 +1717,6 @@ static void MainLoop() { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| // "Library" functions that can be "extern'd" from user code. | // "Library" functions that can be "extern'd" from user code. | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| @@ -1743,6 +1740,9 @@ double printd(double X) { | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| int main() { | int main() { | ||||||
|  |   InitializeNativeTarget(); | ||||||
|  |   LLVMContext &Context = getGlobalContext(); | ||||||
|  |  | ||||||
|   // Install standard binary operators. |   // Install standard binary operators. | ||||||
|   // 1 is lowest precedence. |   // 1 is lowest precedence. | ||||||
|   BinopPrecedence['<'] = 10; |   BinopPrecedence['<'] = 10; | ||||||
| @@ -1755,7 +1755,7 @@ int main() { | |||||||
|   getNextToken(); |   getNextToken(); | ||||||
|  |  | ||||||
|   // Make the module, which holds all the code. |   // Make the module, which holds all the code. | ||||||
|   TheModule = new Module("my cool jit", getGlobalContext()); |   TheModule = new Module("my cool jit", Context); | ||||||
|  |  | ||||||
|   ExistingModuleProvider *OurModuleProvider = |   ExistingModuleProvider *OurModuleProvider = | ||||||
|       new ExistingModuleProvider(TheModule); |       new ExistingModuleProvider(TheModule); | ||||||
|   | |||||||
| @@ -1197,7 +1197,8 @@ public: | |||||||
| }; | }; | ||||||
|  |  | ||||||
| /// PrototypeAST - This class represents the "prototype" for a function, | /// 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 { | class PrototypeAST { | ||||||
|   std::string Name; |   std::string Name; | ||||||
|   std::vector<std::string> Args; |   std::vector<std::string> Args; | ||||||
| @@ -1239,7 +1240,7 @@ public: | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current | /// 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. | /// lexer and updates CurTok with its results. | ||||||
| static int CurTok; | static int CurTok; | ||||||
| static int getNextToken() { | static int getNextToken() { | ||||||
| @@ -1434,7 +1435,6 @@ static ExprAST *ParseVarExpr() { | |||||||
|   return new VarExprAST(VarNames, Body); |   return new VarExprAST(VarNames, Body); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /// primary | /// primary | ||||||
| ///   ::= identifierexpr | ///   ::= identifierexpr | ||||||
| ///   ::= numberexpr | ///   ::= numberexpr | ||||||
| @@ -1520,7 +1520,7 @@ static ExprAST *ParseExpression() { | |||||||
| static PrototypeAST *ParsePrototype() { | static PrototypeAST *ParsePrototype() { | ||||||
|   std::string FnName; |   std::string FnName; | ||||||
|    |    | ||||||
|   int Kind = 0;  // 0 = identifier, 1 = unary, 2 = binary. |   unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. | ||||||
|   unsigned BinaryPrecedence = 30; |   unsigned BinaryPrecedence = 30; | ||||||
|    |    | ||||||
|   switch (CurTok) { |   switch (CurTok) { | ||||||
| @@ -1622,10 +1622,10 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, | |||||||
|                                           const std::string &VarName) { |                                           const std::string &VarName) { | ||||||
|   IRBuilder<> TmpB(&TheFunction->getEntryBlock(), |   IRBuilder<> TmpB(&TheFunction->getEntryBlock(), | ||||||
|                  TheFunction->getEntryBlock().begin()); |                  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() { | Value *NumberExprAST::Codegen() { | ||||||
|   return ConstantFP::get(getGlobalContext(), APFloat(Val)); |   return ConstantFP::get(getGlobalContext(), APFloat(Val)); | ||||||
| } | } | ||||||
| @@ -1650,7 +1650,6 @@ Value *UnaryExprAST::Codegen() { | |||||||
|   return Builder.CreateCall(F, OperandV, "unop"); |   return Builder.CreateCall(F, OperandV, "unop"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Value *BinaryExprAST::Codegen() { | Value *BinaryExprAST::Codegen() { | ||||||
|   // Special case '=' because we don't want to emit the LHS as an expression. |   // Special case '=' because we don't want to emit the LHS as an expression. | ||||||
|   if (Op == '=') { |   if (Op == '=') { | ||||||
| @@ -1670,7 +1669,6 @@ Value *BinaryExprAST::Codegen() { | |||||||
|     return Val; |     return Val; | ||||||
|   } |   } | ||||||
|    |    | ||||||
|    |  | ||||||
|   Value *L = LHS->Codegen(); |   Value *L = LHS->Codegen(); | ||||||
|   Value *R = RHS->Codegen(); |   Value *R = RHS->Codegen(); | ||||||
|   if (L == 0 || R == 0) return 0; |   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 |   // Make the new basic block for the loop header, inserting after current | ||||||
|   // block. |   // block. | ||||||
|   BasicBlock *PreheaderBB = Builder.GetInsertBlock(); |  | ||||||
|   BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction); |   BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction); | ||||||
|    |    | ||||||
|   // Insert an explicit fall through from the current block to the LoopBB. |   // Insert an explicit fall through from the current block to the LoopBB. | ||||||
| @@ -1847,7 +1844,6 @@ Value *ForExprAST::Codegen() { | |||||||
|                                   "loopcond"); |                                   "loopcond"); | ||||||
|    |    | ||||||
|   // Create the "after loop" block and insert it. |   // Create the "after loop" block and insert it. | ||||||
|   BasicBlock *LoopEndBB = Builder.GetInsertBlock(); |  | ||||||
|   BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); |   BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction); | ||||||
|    |    | ||||||
|   // Insert the conditional branch into the end of LoopEndBB. |   // Insert the conditional branch into the end of LoopEndBB. | ||||||
| @@ -1913,7 +1909,6 @@ Value *VarExprAST::Codegen() { | |||||||
|   return BodyVal; |   return BodyVal; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Function *PrototypeAST::Codegen() { | Function *PrototypeAST::Codegen() { | ||||||
|   // Make the function type:  double(double,double) etc. |   // Make the function type:  double(double,double) etc. | ||||||
|   std::vector<const Type*> Doubles(Args.size(), |   std::vector<const Type*> Doubles(Args.size(), | ||||||
| @@ -1968,7 +1963,6 @@ void PrototypeAST::CreateArgumentAllocas(Function *F) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Function *FunctionAST::Codegen() { | Function *FunctionAST::Codegen() { | ||||||
|   NamedValues.clear(); |   NamedValues.clear(); | ||||||
|    |    | ||||||
| @@ -2039,7 +2033,7 @@ static void HandleExtern() { | |||||||
| } | } | ||||||
|  |  | ||||||
| static void HandleTopLevelExpression() { | 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 (FunctionAST *F = ParseTopLevelExpr()) { | ||||||
|     if (Function *LF = F->Codegen()) { |     if (Function *LF = F->Codegen()) { | ||||||
|       // JIT the function, returning a function pointer. |       // JIT the function, returning a function pointer. | ||||||
| @@ -2062,7 +2056,7 @@ static void MainLoop() { | |||||||
|     fprintf(stderr, "ready> "); |     fprintf(stderr, "ready> "); | ||||||
|     switch (CurTok) { |     switch (CurTok) { | ||||||
|     case tok_eof:    return; |     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_def:    HandleDefinition(); break; | ||||||
|     case tok_extern: HandleExtern(); break; |     case tok_extern: HandleExtern(); break; | ||||||
|     default:         HandleTopLevelExpression(); break; |     default:         HandleTopLevelExpression(); break; | ||||||
| @@ -2070,8 +2064,6 @@ static void MainLoop() { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| // "Library" functions that can be "extern'd" from user code. | // "Library" functions that can be "extern'd" from user code. | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| @@ -2095,6 +2087,9 @@ double printd(double X) { | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| int main() { | int main() { | ||||||
|  |   InitializeNativeTarget(); | ||||||
|  |   LLVMContext &Context = getGlobalContext(); | ||||||
|  |  | ||||||
|   // Install standard binary operators. |   // Install standard binary operators. | ||||||
|   // 1 is lowest precedence. |   // 1 is lowest precedence. | ||||||
|   BinopPrecedence['='] = 2; |   BinopPrecedence['='] = 2; | ||||||
| @@ -2108,7 +2103,7 @@ int main() { | |||||||
|   getNextToken(); |   getNextToken(); | ||||||
|  |  | ||||||
|   // Make the module, which holds all the code. |   // Make the module, which holds all the code. | ||||||
|   TheModule = new Module("my cool jit", getGlobalContext()); |   TheModule = new Module("my cool jit", Context); | ||||||
|  |  | ||||||
|   ExistingModuleProvider *OurModuleProvider = |   ExistingModuleProvider *OurModuleProvider = | ||||||
|       new ExistingModuleProvider(TheModule); |       new ExistingModuleProvider(TheModule); | ||||||
|   | |||||||
| @@ -235,7 +235,7 @@ public: | |||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|  |  | ||||||
| /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current | /// 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. | /// lexer and updates CurTok with its results. | ||||||
| static int CurTok; | static int CurTok; | ||||||
| static int getNextToken() { | static int getNextToken() { | ||||||
| @@ -430,7 +430,6 @@ static ExprAST *ParseVarExpr() { | |||||||
|   return new VarExprAST(VarNames, Body); |   return new VarExprAST(VarNames, Body); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| /// primary | /// primary | ||||||
| ///   ::= identifierexpr | ///   ::= identifierexpr | ||||||
| ///   ::= numberexpr | ///   ::= numberexpr | ||||||
| @@ -516,7 +515,7 @@ static ExprAST *ParseExpression() { | |||||||
| static PrototypeAST *ParsePrototype() { | static PrototypeAST *ParsePrototype() { | ||||||
|   std::string FnName; |   std::string FnName; | ||||||
|    |    | ||||||
|   unsigned Kind = 0;            // 0 = identifier, 1 = unary, 2 = binary. |   unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. | ||||||
|   unsigned BinaryPrecedence = 30; |   unsigned BinaryPrecedence = 30; | ||||||
|    |    | ||||||
|   switch (CurTok) { |   switch (CurTok) { | ||||||
| @@ -622,7 +621,6 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, | |||||||
|                            VarName.c_str()); |                            VarName.c_str()); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Value *NumberExprAST::Codegen() { | Value *NumberExprAST::Codegen() { | ||||||
|   return ConstantFP::get(getGlobalContext(), APFloat(Val)); |   return ConstantFP::get(getGlobalContext(), APFloat(Val)); | ||||||
| } | } | ||||||
| @@ -647,7 +645,6 @@ Value *UnaryExprAST::Codegen() { | |||||||
|   return Builder.CreateCall(F, OperandV, "unop"); |   return Builder.CreateCall(F, OperandV, "unop"); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Value *BinaryExprAST::Codegen() { | Value *BinaryExprAST::Codegen() { | ||||||
|   // Special case '=' because we don't want to emit the LHS as an expression. |   // Special case '=' because we don't want to emit the LHS as an expression. | ||||||
|   if (Op == '=') { |   if (Op == '=') { | ||||||
| @@ -667,7 +664,6 @@ Value *BinaryExprAST::Codegen() { | |||||||
|     return Val; |     return Val; | ||||||
|   } |   } | ||||||
|    |    | ||||||
|    |  | ||||||
|   Value *L = LHS->Codegen(); |   Value *L = LHS->Codegen(); | ||||||
|   Value *R = RHS->Codegen(); |   Value *R = RHS->Codegen(); | ||||||
|   if (L == 0 || R == 0) return 0; |   if (L == 0 || R == 0) return 0; | ||||||
| @@ -908,7 +904,6 @@ Value *VarExprAST::Codegen() { | |||||||
|   return BodyVal; |   return BodyVal; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Function *PrototypeAST::Codegen() { | Function *PrototypeAST::Codegen() { | ||||||
|   // Make the function type:  double(double,double) etc. |   // Make the function type:  double(double,double) etc. | ||||||
|   std::vector<const Type*> Doubles(Args.size(),  |   std::vector<const Type*> Doubles(Args.size(),  | ||||||
| @@ -963,7 +958,6 @@ void PrototypeAST::CreateArgumentAllocas(Function *F) { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| Function *FunctionAST::Codegen() { | Function *FunctionAST::Codegen() { | ||||||
|   NamedValues.clear(); |   NamedValues.clear(); | ||||||
|    |    | ||||||
| @@ -1034,7 +1028,7 @@ static void HandleExtern() { | |||||||
| } | } | ||||||
|  |  | ||||||
| static void HandleTopLevelExpression() { | 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 (FunctionAST *F = ParseTopLevelExpr()) { | ||||||
|     if (Function *LF = F->Codegen()) { |     if (Function *LF = F->Codegen()) { | ||||||
|       // JIT the function, returning a function pointer. |       // JIT the function, returning a function pointer. | ||||||
| @@ -1057,7 +1051,7 @@ static void MainLoop() { | |||||||
|     fprintf(stderr, "ready> "); |     fprintf(stderr, "ready> "); | ||||||
|     switch (CurTok) { |     switch (CurTok) { | ||||||
|     case tok_eof:    return; |     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_def:    HandleDefinition(); break; | ||||||
|     case tok_extern: HandleExtern(); break; |     case tok_extern: HandleExtern(); break; | ||||||
|     default:         HandleTopLevelExpression(); break; |     default:         HandleTopLevelExpression(); break; | ||||||
| @@ -1065,8 +1059,6 @@ static void MainLoop() { | |||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
| // "Library" functions that can be "extern'd" from user code. | // "Library" functions that can be "extern'd" from user code. | ||||||
| //===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user