1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

Sync c++ kaleidoscope tutorial with test.

llvm-svn: 82572
This commit is contained in:
Erick Tryzelaar 2009-09-22 21:14:49 +00:00
parent d3d28d0f25
commit 83d984101e
7 changed files with 103 additions and 104 deletions

View File

@ -84,7 +84,7 @@ public:
class NumberExprAST : public ExprAST {
double Val;
public:
explicit NumberExprAST(double val) : Val(val) {}
NumberExprAST(double val) : Val(val) {}
};
</pre>
</div>
@ -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 &amp;name) : Name(name) {}
VariableExprAST(const std::string &amp;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 &amp;name) : Name(name) {}
VariableExprAST(const std::string &amp;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&gt; ");
getNextToken();
// Run the main "interpreter loop" now.
MainLoop();
return 0;
}
</pre>

View File

@ -79,7 +79,7 @@ public:
class NumberExprAST : public ExprAST {
double Val;
public:
explicit NumberExprAST(double val) : Val(val) {}
NumberExprAST(double val) : Val(val) {}
<b>virtual Value *Codegen();</b>
};
...
@ -464,9 +464,10 @@ block at this point. We'll fix this in <a href="LangImpl5.html">Chapter 5</a> :
if (Value *RetVal = Body-&gt;Codegen()) {
// Finish off the function.
Builder.CreateRet(RetVal);
// Validate the generated code, checking for consistency.
verifyFunction(*TheFunction);
return TheFunction;
}
</pre>
@ -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 &amp;name) : Name(name) {}
VariableExprAST(const std::string &amp;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&lt;std::string&gt; 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 '&lt;':
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-&gt;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-&gt;Codegen()) {
fprintf(stderr, "Read top-level expression:");
@ -1196,7 +1200,7 @@ static void MainLoop() {
fprintf(stderr, "ready&gt; ");
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 &amp;Context = getGlobalContext();
// Install standard binary operators.
// 1 is lowest precedence.
@ -1235,8 +1237,15 @@ int main() {
fprintf(stderr, "ready&gt; ");
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-&gt;dump();
return 0;
}
</pre>

View File

@ -324,7 +324,7 @@ top-level expression to look like this:</p>
<div class="doc_code">
<pre>
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-&gt;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());</b>
}
</pre>
@ -363,7 +363,7 @@ entry:
<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
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>
<div class="doc_code">
@ -499,7 +499,7 @@ LLVM JIT and optimizer. To build this example, use:
<div class="doc_code">
<pre>
# 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
</pre>
@ -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&lt;std::string&gt; 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-&gt;Codegen()) {
// JIT the function, returning a function pointer.
@ -1047,7 +1048,7 @@ static void MainLoop() {
fprintf(stderr, "ready&gt; ");
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.
//===----------------------------------------------------------------------===//

View File

@ -472,7 +472,8 @@ are emitted, we can finish up with the merge code:</p>
// 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&lt;std::string&gt; 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-&gt;Codegen()) {
// JIT the function, returning a function pointer.
@ -1678,7 +1679,7 @@ static void MainLoop() {
fprintf(stderr, "ready&gt; ");
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 &amp;Context = getGlobalContext();
// Install standard binary operators.
// 1 is lowest precedence.
BinopPrecedence['&lt;'] = 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);

View File

@ -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.</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">
<pre>
@ -795,7 +795,6 @@ add variable mutation without building SSA in your front-end.</p>
</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,
/// 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&lt;std::string&gt; 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-&gt;Codegen();
Value *R = RHS-&gt;Codegen();
@ -1402,7 +1400,8 @@ Value *BinaryExprAST::Codegen() {
case '&lt;':
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-&gt;Codegen()) {
// JIT the function, returning a function pointer.
@ -1710,7 +1709,7 @@ static void MainLoop() {
fprintf(stderr, "ready&gt; ");
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 &amp;Context = getGlobalContext();
// Install standard binary operators.
// 1 is lowest precedence.
BinopPrecedence['&lt;'] = 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);

View File

@ -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&lt;std::string&gt; 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 &amp;VarName) {
IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(),
TheFunction-&gt;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-&gt;Codegen();
Value *R = RHS-&gt;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&lt;const Type*&gt; 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-&gt;CreateArgumentAllocas(TheFunction);
if (Value *RetVal = Body-&gt;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-&gt;Codegen()) {
// JIT the function, returning a function pointer.
@ -2062,7 +2056,7 @@ static void MainLoop() {
fprintf(stderr, "ready&gt; ");
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 &amp;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);

View File

@ -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<const Type*> 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;