Update the tutorial to match changes to examples/Kaleidoscope.

One change I'm not folding in is the removal of two unused variables that
caused warnings, because those were there for expository purposes.

llvm-svn: 81721
This commit is contained in:
Nick Lewycky 2009-09-13 21:38:54 +00:00
parent 1b12ed4a00
commit 14b1aacf10
5 changed files with 77 additions and 29 deletions

View File

@ -206,7 +206,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");
} }
} }
@ -307,8 +308,10 @@ bodies and external function declarations. The code starts with:</p>
<pre> <pre>
Function *PrototypeAST::Codegen() { Function *PrototypeAST::Codegen() {
// Make the function type: double(double,double) etc. // Make the function type: double(double,double) etc.
std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::getDoubleTy(getGlobalContext())); std::vector&lt;const Type*&gt; Doubles(Args.size(),
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
</pre> </pre>
@ -1081,8 +1084,10 @@ Value *CallExprAST::Codegen() {
Function *PrototypeAST::Codegen() { Function *PrototypeAST::Codegen() {
// Make the function type: double(double,double) etc. // Make the function type: double(double,double) etc.
std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::getDoubleTy(getGlobalContext())); std::vector&lt;const Type*&gt; Doubles(Args.size(),
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);

View File

@ -188,6 +188,8 @@ add a set of optimizations to run. The code looks like this:</p>
// Simplify the control flow graph (deleting unreachable blocks, etc). // Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass()); OurFPM.add(createCFGSimplificationPass());
OurFPM.doInitialization();
// Set the global so the code gen can use this. // Set the global so the code gen can use this.
TheFPM = &amp;OurFPM; TheFPM = &amp;OurFPM;
@ -514,12 +516,15 @@ at runtime.</p>
<pre> <pre>
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/LLVMContext.h" #include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ModuleProvider.h" #include "llvm/ModuleProvider.h"
#include "llvm/PassManager.h" #include "llvm/PassManager.h"
#include "llvm/Analysis/Verifier.h" #include "llvm/Analysis/Verifier.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetSelect.h"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Support/IRBuilder.h" #include "llvm/Support/IRBuilder.h"
#include &lt;cstdio&gt; #include &lt;cstdio&gt;
@ -892,7 +897,8 @@ Value *BinaryExprAST::Codegen() {
case '&lt;': case '&lt;':
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");
} }
} }
@ -918,8 +924,10 @@ Value *CallExprAST::Codegen() {
Function *PrototypeAST::Codegen() { Function *PrototypeAST::Codegen() {
// Make the function type: double(double,double) etc. // Make the function type: double(double,double) etc.
std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::getDoubleTy(getGlobalContext())); std::vector&lt;const Type*&gt; Doubles(Args.size(),
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
@ -1024,7 +1032,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()); fprintf(stderr, "Evaluated to %f\n", FP());
} }
} else { } else {
@ -1065,6 +1073,9 @@ double putchard(double X) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
int main() { int main() {
InitializeNativeTarget();
LLVMContext &amp;Context = getGlobalContext();
// Install standard binary operators. // Install standard binary operators.
// 1 is lowest precedence. // 1 is lowest precedence.
BinopPrecedence['&lt;'] = 10; BinopPrecedence['&lt;'] = 10;
@ -1077,7 +1088,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);
@ -1099,6 +1110,8 @@ int main() {
// Simplify the control flow graph (deleting unreachable blocks, etc). // Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass()); OurFPM.add(createCFGSimplificationPass());
OurFPM.doInitialization();
// Set the global so the code gen can use this. // Set the global so the code gen can use this.
TheFPM = &amp;OurFPM; TheFPM = &amp;OurFPM;

View File

@ -901,12 +901,15 @@ if/then/else and for expressions.. To build this example, use:
<pre> <pre>
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/LLVMContext.h" #include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ModuleProvider.h" #include "llvm/ModuleProvider.h"
#include "llvm/PassManager.h" #include "llvm/PassManager.h"
#include "llvm/Analysis/Verifier.h" #include "llvm/Analysis/Verifier.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetSelect.h"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Support/IRBuilder.h" #include "llvm/Support/IRBuilder.h"
#include &lt;cstdio&gt; #include &lt;cstdio&gt;
@ -1381,7 +1384,8 @@ Value *BinaryExprAST::Codegen() {
case '&lt;': case '&lt;':
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");
} }
} }
@ -1448,7 +1452,8 @@ Value *IfExprAST::Codegen() {
// Emit merge block. // Emit merge block.
TheFunction-&gt;getBasicBlockList().push_back(MergeBB); TheFunction-&gt;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-&gt;addIncoming(ThenV, ThenBB); PN-&gt;addIncoming(ThenV, ThenBB);
PN-&gt;addIncoming(ElseV, ElseBB); PN-&gt;addIncoming(ElseV, ElseBB);
@ -1550,8 +1555,10 @@ Value *ForExprAST::Codegen() {
Function *PrototypeAST::Codegen() { Function *PrototypeAST::Codegen() {
// Make the function type: double(double,double) etc. // Make the function type: double(double,double) etc.
std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::getDoubleTy(getGlobalContext())); std::vector&lt;const Type*&gt; Doubles(Args.size(),
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
@ -1656,7 +1663,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()); fprintf(stderr, "Evaluated to %f\n", FP());
} }
} else { } else {
@ -1731,6 +1738,8 @@ int main() {
// Simplify the control flow graph (deleting unreachable blocks, etc). // Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass()); OurFPM.add(createCFGSimplificationPass());
OurFPM.doInitialization();
// Set the global so the code gen can use this. // Set the global so the code gen can use this.
TheFPM = &amp;OurFPM; TheFPM = &amp;OurFPM;

View File

@ -207,7 +207,7 @@ the prototype for a user-defined operator, we need to parse it:</p>
static PrototypeAST *ParsePrototype() { static PrototypeAST *ParsePrototype() {
std::string FnName; std::string FnName;
<b>int Kind = 0; // 0 = identifier, 1 = unary, 2 = binary. <b>unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
unsigned BinaryPrecedence = 30;</b> unsigned BinaryPrecedence = 30;</b>
switch (CurTok) { switch (CurTok) {
@ -283,7 +283,8 @@ Value *BinaryExprAST::Codegen() {
case '&lt;': case '&lt;':
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");
<b>default: break;</b> <b>default: break;</b>
} }
@ -438,7 +439,7 @@ with:</p>
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) {
@ -821,12 +822,15 @@ if/then/else and for expressions.. To build this example, use:
<pre> <pre>
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/LLVMContext.h" #include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ModuleProvider.h" #include "llvm/ModuleProvider.h"
#include "llvm/PassManager.h" #include "llvm/PassManager.h"
#include "llvm/Analysis/Verifier.h" #include "llvm/Analysis/Verifier.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetSelect.h"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Support/IRBuilder.h" #include "llvm/Support/IRBuilder.h"
#include &lt;cstdio&gt; #include &lt;cstdio&gt;
@ -1268,7 +1272,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) {
@ -1473,7 +1477,8 @@ Value *IfExprAST::Codegen() {
// Emit merge block. // Emit merge block.
TheFunction-&gt;getBasicBlockList().push_back(MergeBB); TheFunction-&gt;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-&gt;addIncoming(ThenV, ThenBB); PN-&gt;addIncoming(ThenV, ThenBB);
PN-&gt;addIncoming(ElseV, ElseBB); PN-&gt;addIncoming(ElseV, ElseBB);
@ -1575,8 +1580,10 @@ Value *ForExprAST::Codegen() {
Function *PrototypeAST::Codegen() { Function *PrototypeAST::Codegen() {
// Make the function type: double(double,double) etc. // Make the function type: double(double,double) etc.
std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::getDoubleTy(getGlobalContext())); std::vector&lt;const Type*&gt; Doubles(Args.size(),
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
@ -1688,7 +1695,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()); fprintf(stderr, "Evaluated to %f\n", FP());
} }
} else { } else {
@ -1770,6 +1777,8 @@ int main() {
// Simplify the control flow graph (deleting unreachable blocks, etc). // Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass()); OurFPM.add(createCFGSimplificationPass());
OurFPM.doInitialization();
// Set the global so the code gen can use this. // Set the global so the code gen can use this.
TheFPM = &amp;OurFPM; TheFPM = &amp;OurFPM;

View File

@ -424,7 +424,8 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
const std::string &amp;VarName) { const std::string &amp;VarName) {
IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(), IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(),
TheFunction-&gt;getEntryBlock().begin()); TheFunction-&gt;getEntryBlock().begin());
return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0, VarName.c_str()); return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
VarName.c_str());
} }
</pre> </pre>
</div> </div>
@ -1003,12 +1004,15 @@ variables and var/in support. To build this example, use:
<pre> <pre>
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/Interpreter.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/LLVMContext.h" #include "llvm/LLVMContext.h"
#include "llvm/Module.h" #include "llvm/Module.h"
#include "llvm/ModuleProvider.h" #include "llvm/ModuleProvider.h"
#include "llvm/PassManager.h" #include "llvm/PassManager.h"
#include "llvm/Analysis/Verifier.h" #include "llvm/Analysis/Verifier.h"
#include "llvm/Target/TargetData.h" #include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetSelect.h"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Support/IRBuilder.h" #include "llvm/Support/IRBuilder.h"
#include &lt;cstdio&gt; #include &lt;cstdio&gt;
@ -1678,7 +1682,8 @@ Value *BinaryExprAST::Codegen() {
case '&lt;': case '&lt;':
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;
} }
@ -1753,7 +1758,8 @@ Value *IfExprAST::Codegen() {
// Emit merge block. // Emit merge block.
TheFunction-&gt;getBasicBlockList().push_back(MergeBB); TheFunction-&gt;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-&gt;addIncoming(ThenV, ThenBB); PN-&gt;addIncoming(ThenV, ThenBB);
PN-&gt;addIncoming(ElseV, ElseBB); PN-&gt;addIncoming(ElseV, ElseBB);
@ -1910,8 +1916,10 @@ Value *VarExprAST::Codegen() {
Function *PrototypeAST::Codegen() { Function *PrototypeAST::Codegen() {
// Make the function type: double(double,double) etc. // Make the function type: double(double,double) etc.
std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::getDoubleTy(getGlobalContext())); std::vector&lt;const Type*&gt; Doubles(Args.size(),
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false); Type::getDoubleTy(getGlobalContext()));
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
Doubles, false);
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule); Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
@ -2039,7 +2047,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()); fprintf(stderr, "Evaluated to %f\n", FP());
} }
} else { } else {
@ -2113,6 +2121,8 @@ int main() {
// Set up the optimizer pipeline. Start with registering info about how the // Set up the optimizer pipeline. Start with registering info about how the
// target lays out data structures. // target lays out data structures.
OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData())); OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
// Promote allocas to registers.
OurFPM.add(createPromoteMemoryToRegisterPass());
// Do simple "peephole" optimizations and bit-twiddling optzns. // Do simple "peephole" optimizations and bit-twiddling optzns.
OurFPM.add(createInstructionCombiningPass()); OurFPM.add(createInstructionCombiningPass());
// Reassociate expressions. // Reassociate expressions.
@ -2122,6 +2132,8 @@ int main() {
// Simplify the control flow graph (deleting unreachable blocks, etc). // Simplify the control flow graph (deleting unreachable blocks, etc).
OurFPM.add(createCFGSimplificationPass()); OurFPM.add(createCFGSimplificationPass());
OurFPM.doInitialization();
// Set the global so the code gen can use this. // Set the global so the code gen can use this.
TheFPM = &amp;OurFPM; TheFPM = &amp;OurFPM;