mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2026-04-26 12:20:42 +00:00
Push LLVMContexts through the IntegerType APIs.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78948 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
+22
-22
@@ -54,31 +54,31 @@ void BrainF::header(LLVMContext& C) {
|
||||
//Function prototypes
|
||||
|
||||
//declare void @llvm.memset.i32(i8 *, i8, i32, i32)
|
||||
const Type *Tys[] = { Type::Int32Ty };
|
||||
const Type *Tys[] = { Type::getInt32Ty(C) };
|
||||
Function *memset_func = Intrinsic::getDeclaration(module, Intrinsic::memset,
|
||||
Tys, 1);
|
||||
|
||||
//declare i32 @getchar()
|
||||
getchar_func = cast<Function>(module->
|
||||
getOrInsertFunction("getchar", IntegerType::Int32Ty, NULL));
|
||||
getOrInsertFunction("getchar", IntegerType::getInt32Ty(C), NULL));
|
||||
|
||||
//declare i32 @putchar(i32)
|
||||
putchar_func = cast<Function>(module->
|
||||
getOrInsertFunction("putchar", IntegerType::Int32Ty,
|
||||
IntegerType::Int32Ty, NULL));
|
||||
getOrInsertFunction("putchar", IntegerType::getInt32Ty(C),
|
||||
IntegerType::getInt32Ty(C), NULL));
|
||||
|
||||
|
||||
//Function header
|
||||
|
||||
//define void @brainf()
|
||||
brainf_func = cast<Function>(module->
|
||||
getOrInsertFunction("brainf", Type::VoidTy, NULL));
|
||||
getOrInsertFunction("brainf", Type::getVoidTy(C), NULL));
|
||||
|
||||
builder = new IRBuilder<>(BasicBlock::Create(label, brainf_func));
|
||||
builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
|
||||
|
||||
//%arr = malloc i8, i32 %d
|
||||
ConstantInt *val_mem = ConstantInt::get(C, APInt(32, memtotal));
|
||||
ptr_arr = builder->CreateMalloc(IntegerType::Int8Ty, val_mem, "arr");
|
||||
ptr_arr = builder->CreateMalloc(IntegerType::getInt8Ty(C), val_mem, "arr");
|
||||
|
||||
//call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
|
||||
{
|
||||
@@ -110,13 +110,13 @@ void BrainF::header(LLVMContext& C) {
|
||||
//Function footer
|
||||
|
||||
//brainf.end:
|
||||
endbb = BasicBlock::Create(label, brainf_func);
|
||||
endbb = BasicBlock::Create(C, label, brainf_func);
|
||||
|
||||
//free i8 *%arr
|
||||
new FreeInst(ptr_arr, endbb);
|
||||
|
||||
//ret void
|
||||
ReturnInst::Create(endbb);
|
||||
ReturnInst::Create(C, endbb);
|
||||
|
||||
|
||||
|
||||
@@ -125,7 +125,7 @@ void BrainF::header(LLVMContext& C) {
|
||||
{
|
||||
//@aberrormsg = internal constant [%d x i8] c"\00"
|
||||
Constant *msg_0 =
|
||||
ConstantArray::get("Error: The head has left the tape.", true);
|
||||
ConstantArray::get(C, "Error: The head has left the tape.", true);
|
||||
|
||||
GlobalVariable *aberrormsg = new GlobalVariable(
|
||||
*module,
|
||||
@@ -137,15 +137,15 @@ void BrainF::header(LLVMContext& C) {
|
||||
|
||||
//declare i32 @puts(i8 *)
|
||||
Function *puts_func = cast<Function>(module->
|
||||
getOrInsertFunction("puts", IntegerType::Int32Ty,
|
||||
PointerType::getUnqual(IntegerType::Int8Ty), NULL));
|
||||
getOrInsertFunction("puts", IntegerType::getInt32Ty(C),
|
||||
PointerType::getUnqual(IntegerType::getInt8Ty(C)), NULL));
|
||||
|
||||
//brainf.aberror:
|
||||
aberrorbb = BasicBlock::Create(label, brainf_func);
|
||||
aberrorbb = BasicBlock::Create(C, label, brainf_func);
|
||||
|
||||
//call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
|
||||
{
|
||||
Constant *zero_32 = Constant::getNullValue(IntegerType::Int32Ty);
|
||||
Constant *zero_32 = Constant::getNullValue(IntegerType::getInt32Ty(C));
|
||||
|
||||
Constant *gep_params[] = {
|
||||
zero_32,
|
||||
@@ -198,7 +198,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
|
||||
|
||||
//%tape.%d = trunc i32 %tape.%d to i8
|
||||
Value *tape_1 = builder->
|
||||
CreateTrunc(tape_0, IntegerType::Int8Ty, tapereg);
|
||||
CreateTrunc(tape_0, IntegerType::getInt8Ty(C), tapereg);
|
||||
|
||||
//store i8 %tape.%d, i8 *%head.%d
|
||||
builder->CreateStore(tape_1, curhead);
|
||||
@@ -212,7 +212,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
|
||||
|
||||
//%tape.%d = sext i8 %tape.%d to i32
|
||||
Value *tape_1 = builder->
|
||||
CreateSExt(tape_0, IntegerType::Int32Ty, tapereg);
|
||||
CreateSExt(tape_0, IntegerType::getInt32Ty(C), tapereg);
|
||||
|
||||
//call i32 @putchar(i32 %tape.%d)
|
||||
Value *putchar_params[] = {
|
||||
@@ -248,7 +248,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
|
||||
CreateOr(test_0, test_1, testreg);
|
||||
|
||||
//br i1 %test.%d, label %main.%d, label %main.%d
|
||||
BasicBlock *nextbb = BasicBlock::Create(label, brainf_func);
|
||||
BasicBlock *nextbb = BasicBlock::Create(C, label, brainf_func);
|
||||
builder->CreateCondBr(test_2, aberrorbb, nextbb);
|
||||
|
||||
//main.%d:
|
||||
@@ -274,17 +274,17 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
|
||||
case SYM_LOOP:
|
||||
{
|
||||
//br label %main.%d
|
||||
BasicBlock *testbb = BasicBlock::Create(label, brainf_func);
|
||||
BasicBlock *testbb = BasicBlock::Create(C, label, brainf_func);
|
||||
builder->CreateBr(testbb);
|
||||
|
||||
//main.%d:
|
||||
BasicBlock *bb_0 = builder->GetInsertBlock();
|
||||
BasicBlock *bb_1 = BasicBlock::Create(label, brainf_func);
|
||||
BasicBlock *bb_1 = BasicBlock::Create(C, label, brainf_func);
|
||||
builder->SetInsertPoint(bb_1);
|
||||
|
||||
// Make part of PHI instruction now, wait until end of loop to finish
|
||||
PHINode *phi_0 =
|
||||
PHINode::Create(PointerType::getUnqual(IntegerType::Int8Ty),
|
||||
PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C)),
|
||||
headreg, testbb);
|
||||
phi_0->reserveOperandSpace(2);
|
||||
phi_0->addIncoming(curhead, bb_0);
|
||||
@@ -432,7 +432,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
|
||||
ConstantInt::get(C, APInt(8, 0)), testreg);
|
||||
|
||||
//br i1 %test.%d, label %main.%d, label %main.%d
|
||||
BasicBlock *bb_0 = BasicBlock::Create(label, brainf_func);
|
||||
BasicBlock *bb_0 = BasicBlock::Create(C, label, brainf_func);
|
||||
BranchInst::Create(bb_0, oldbb, test_0, testbb);
|
||||
|
||||
//main.%d:
|
||||
@@ -440,7 +440,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb,
|
||||
|
||||
//%head.%d = phi i8 *[%head.%d, %main.%d]
|
||||
PHINode *phi_1 = builder->
|
||||
CreatePHI(PointerType::getUnqual(IntegerType::Int8Ty), headreg);
|
||||
CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), headreg);
|
||||
phi_1->reserveOperandSpace(1);
|
||||
phi_1->addIncoming(head_0, testbb);
|
||||
curhead = phi_1;
|
||||
|
||||
@@ -58,9 +58,10 @@ JIT("jit", cl::desc("Run program Just-In-Time"));
|
||||
void addMainFunction(Module *mod) {
|
||||
//define i32 @main(i32 %argc, i8 **%argv)
|
||||
Function *main_func = cast<Function>(mod->
|
||||
getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty,
|
||||
getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
|
||||
IntegerType::getInt32Ty(mod->getContext()),
|
||||
PointerType::getUnqual(PointerType::getUnqual(
|
||||
IntegerType::Int8Ty)), NULL));
|
||||
IntegerType::getInt8Ty(mod->getContext()))), NULL));
|
||||
{
|
||||
Function::arg_iterator args = main_func->arg_begin();
|
||||
Value *arg_0 = args++;
|
||||
@@ -70,7 +71,7 @@ void addMainFunction(Module *mod) {
|
||||
}
|
||||
|
||||
//main.0:
|
||||
BasicBlock *bb = BasicBlock::Create("main.0", main_func);
|
||||
BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func);
|
||||
|
||||
//call void @brainf()
|
||||
{
|
||||
@@ -80,7 +81,8 @@ void addMainFunction(Module *mod) {
|
||||
}
|
||||
|
||||
//ret i32 0
|
||||
ReturnInst::Create(ConstantInt::get(getGlobalContext(), APInt(32, 0)), bb);
|
||||
ReturnInst::Create(mod->getContext(),
|
||||
ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
@@ -40,31 +40,32 @@ static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
|
||||
// Create the fib function and insert it into module M. This function is said
|
||||
// to return an int and take an int parameter.
|
||||
Function *FibF =
|
||||
cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
|
||||
cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
|
||||
Type::getInt32Ty(Context),
|
||||
(Type *)0));
|
||||
|
||||
// Add a basic block to the function.
|
||||
BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
|
||||
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
|
||||
|
||||
// Get pointers to the constants.
|
||||
Value *One = ConstantInt::get(Type::Int32Ty, 1);
|
||||
Value *Two = ConstantInt::get(Type::Int32Ty, 2);
|
||||
Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
|
||||
Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
|
||||
|
||||
// Get pointer to the integer argument of the add1 function...
|
||||
Argument *ArgX = FibF->arg_begin(); // Get the arg.
|
||||
ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
|
||||
|
||||
// Create the true_block.
|
||||
BasicBlock *RetBB = BasicBlock::Create("return", FibF);
|
||||
BasicBlock *RetBB = BasicBlock::Create(Context, "return", FibF);
|
||||
// Create an exit block.
|
||||
BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF);
|
||||
BasicBlock* RecurseBB = BasicBlock::Create(Context, "recurse", FibF);
|
||||
|
||||
// Create the "if (arg <= 2) goto exitbb"
|
||||
Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
|
||||
BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
|
||||
|
||||
// Create: ret int 1
|
||||
ReturnInst::Create(One, RetBB);
|
||||
ReturnInst::Create(Context, One, RetBB);
|
||||
|
||||
// create fib(x-1)
|
||||
Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
|
||||
@@ -82,7 +83,7 @@ static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
|
||||
"addresult", RecurseBB);
|
||||
|
||||
// Create the return instruction and add it to the basic block
|
||||
ReturnInst::Create(Sum, RecurseBB);
|
||||
ReturnInst::Create(Context, Sum, RecurseBB);
|
||||
|
||||
return FibF;
|
||||
}
|
||||
|
||||
@@ -61,15 +61,16 @@ int main() {
|
||||
// function will have a return type of "int" and take an argument of "int".
|
||||
// The '0' terminates the list of argument types.
|
||||
Function *Add1F =
|
||||
cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
|
||||
cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
|
||||
Type::getInt32Ty(Context),
|
||||
(Type *)0));
|
||||
|
||||
// Add a basic block to the function. As before, it automatically inserts
|
||||
// because of the last argument.
|
||||
BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
|
||||
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", Add1F);
|
||||
|
||||
// Get pointers to the constant `1'.
|
||||
Value *One = ConstantInt::get(Type::Int32Ty, 1);
|
||||
Value *One = ConstantInt::get(Type::getInt32Ty(Context), 1);
|
||||
|
||||
// Get pointers to the integer argument of the add1 function...
|
||||
assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
|
||||
@@ -80,7 +81,7 @@ int main() {
|
||||
Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
|
||||
|
||||
// Create the return instruction and add it to the basic block
|
||||
ReturnInst::Create(Add, BB);
|
||||
ReturnInst::Create(Context, Add, BB);
|
||||
|
||||
// Now, function add1 is ready.
|
||||
|
||||
@@ -88,20 +89,21 @@ int main() {
|
||||
// Now we going to create function `foo', which returns an int and takes no
|
||||
// arguments.
|
||||
Function *FooF =
|
||||
cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0));
|
||||
cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
|
||||
(Type *)0));
|
||||
|
||||
// Add a basic block to the FooF function.
|
||||
BB = BasicBlock::Create("EntryBlock", FooF);
|
||||
BB = BasicBlock::Create(Context, "EntryBlock", FooF);
|
||||
|
||||
// Get pointers to the constant `10'.
|
||||
Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
|
||||
Value *Ten = ConstantInt::get(Type::getInt32Ty(Context), 10);
|
||||
|
||||
// Pass Ten to the call call:
|
||||
CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
|
||||
Add1CallRes->setTailCall(true);
|
||||
|
||||
// Create the return instruction and add it to the basic block.
|
||||
ReturnInst::Create(Add1CallRes, BB);
|
||||
ReturnInst::Create(Context, Add1CallRes, BB);
|
||||
|
||||
// Now we create the JIT.
|
||||
ExecutionEngine* EE = EngineBuilder(M).create();
|
||||
|
||||
@@ -618,7 +618,8 @@ static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
|
||||
const std::string &VarName) {
|
||||
IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
|
||||
TheFunction->getEntryBlock().begin());
|
||||
return TmpB.CreateAlloca(Type::DoubleTy, 0, VarName.c_str());
|
||||
return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
|
||||
VarName.c_str());
|
||||
}
|
||||
|
||||
|
||||
@@ -678,7 +679,8 @@ Value *BinaryExprAST::Codegen() {
|
||||
case '<':
|
||||
L = Builder.CreateFCmpULT(L, R, "cmptmp");
|
||||
// Convert bool 0/1 to double 0.0 or 1.0
|
||||
return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
|
||||
return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
|
||||
"booltmp");
|
||||
default: break;
|
||||
}
|
||||
|
||||
@@ -723,9 +725,9 @@ Value *IfExprAST::Codegen() {
|
||||
|
||||
// Create blocks for the then and else cases. Insert the 'then' block at the
|
||||
// end of the function.
|
||||
BasicBlock *ThenBB = BasicBlock::Create("then", TheFunction);
|
||||
BasicBlock *ElseBB = BasicBlock::Create("else");
|
||||
BasicBlock *MergeBB = BasicBlock::Create("ifcont");
|
||||
BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
|
||||
BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
|
||||
BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
|
||||
|
||||
Builder.CreateCondBr(CondV, ThenBB, ElseBB);
|
||||
|
||||
@@ -753,7 +755,8 @@ Value *IfExprAST::Codegen() {
|
||||
// Emit merge block.
|
||||
TheFunction->getBasicBlockList().push_back(MergeBB);
|
||||
Builder.SetInsertPoint(MergeBB);
|
||||
PHINode *PN = Builder.CreatePHI(Type::DoubleTy, "iftmp");
|
||||
PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
|
||||
"iftmp");
|
||||
|
||||
PN->addIncoming(ThenV, ThenBB);
|
||||
PN->addIncoming(ElseV, ElseBB);
|
||||
@@ -795,7 +798,7 @@ Value *ForExprAST::Codegen() {
|
||||
|
||||
// Make the new basic block for the loop header, inserting after current
|
||||
// block.
|
||||
BasicBlock *LoopBB = BasicBlock::Create("loop", TheFunction);
|
||||
BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
|
||||
|
||||
// Insert an explicit fall through from the current block to the LoopBB.
|
||||
Builder.CreateBr(LoopBB);
|
||||
@@ -840,7 +843,7 @@ Value *ForExprAST::Codegen() {
|
||||
"loopcond");
|
||||
|
||||
// Create the "after loop" block and insert it.
|
||||
BasicBlock *AfterBB = BasicBlock::Create("afterloop", TheFunction);
|
||||
BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
|
||||
|
||||
// Insert the conditional branch into the end of LoopEndBB.
|
||||
Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
|
||||
@@ -856,7 +859,7 @@ Value *ForExprAST::Codegen() {
|
||||
|
||||
|
||||
// for expr always returns 0.0.
|
||||
return Constant::getNullValue(Type::DoubleTy);
|
||||
return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
|
||||
}
|
||||
|
||||
Value *VarExprAST::Codegen() {
|
||||
@@ -908,8 +911,10 @@ Value *VarExprAST::Codegen() {
|
||||
|
||||
Function *PrototypeAST::Codegen() {
|
||||
// Make the function type: double(double,double) etc.
|
||||
std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
|
||||
FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
|
||||
std::vector<const Type*> Doubles(Args.size(),
|
||||
Type::getDoubleTy(getGlobalContext()));
|
||||
FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
|
||||
Doubles, false);
|
||||
|
||||
Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
|
||||
|
||||
@@ -971,7 +976,7 @@ Function *FunctionAST::Codegen() {
|
||||
BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
|
||||
|
||||
// Create a new basic block to start insertion into.
|
||||
BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
|
||||
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
|
||||
Builder.SetInsertPoint(BB);
|
||||
|
||||
// Add all arguments to the symbol table and create their allocas.
|
||||
|
||||
@@ -31,7 +31,7 @@ int main() {
|
||||
|
||||
// Create the main function: first create the type 'int ()'
|
||||
FunctionType *FT =
|
||||
FunctionType::get(Type::Int32Ty, /*not vararg*/false);
|
||||
FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);
|
||||
|
||||
// By passing a module as the last parameter to the Function constructor,
|
||||
// it automatically gets appended to the Module.
|
||||
@@ -39,11 +39,11 @@ int main() {
|
||||
|
||||
// Add a basic block to the function... again, it automatically inserts
|
||||
// because of the last argument.
|
||||
BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
|
||||
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);
|
||||
|
||||
// Get pointers to the constant integers...
|
||||
Value *Two = ConstantInt::get(Type::Int32Ty, 2);
|
||||
Value *Three = ConstantInt::get(Type::Int32Ty, 3);
|
||||
Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
|
||||
Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);
|
||||
|
||||
// Create the add instruction... does not insert...
|
||||
Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
|
||||
@@ -53,7 +53,7 @@ int main() {
|
||||
BB->getInstList().push_back(Add);
|
||||
|
||||
// Create the return instruction and add it to the basic block
|
||||
BB->getInstList().push_back(ReturnInst::Create(Add));
|
||||
BB->getInstList().push_back(ReturnInst::Create(Context, Add));
|
||||
|
||||
// Output the bitcode file to stdout
|
||||
WriteBitcodeToFile(M, std::cout);
|
||||
|
||||
@@ -36,15 +36,17 @@ static Function* createAdd1(Module *M) {
|
||||
// function will have a return type of "int" and take an argument of "int".
|
||||
// The '0' terminates the list of argument types.
|
||||
Function *Add1F =
|
||||
cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
|
||||
cast<Function>(M->getOrInsertFunction("add1",
|
||||
Type::getInt32Ty(M->getContext()),
|
||||
Type::getInt32Ty(M->getContext()),
|
||||
(Type *)0));
|
||||
|
||||
// Add a basic block to the function. As before, it automatically inserts
|
||||
// because of the last argument.
|
||||
BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
|
||||
BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", Add1F);
|
||||
|
||||
// Get pointers to the constant `1'.
|
||||
Value *One = ConstantInt::get(Type::Int32Ty, 1);
|
||||
Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
|
||||
|
||||
// Get pointers to the integer argument of the add1 function...
|
||||
assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
|
||||
@@ -55,7 +57,7 @@ static Function* createAdd1(Module *M) {
|
||||
Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
|
||||
|
||||
// Create the return instruction and add it to the basic block
|
||||
ReturnInst::Create(Add, BB);
|
||||
ReturnInst::Create(M->getContext(), Add, BB);
|
||||
|
||||
// Now, function add1 is ready.
|
||||
return Add1F;
|
||||
@@ -65,31 +67,33 @@ static Function *CreateFibFunction(Module *M) {
|
||||
// Create the fib function and insert it into module M. This function is said
|
||||
// to return an int and take an int parameter.
|
||||
Function *FibF =
|
||||
cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
|
||||
cast<Function>(M->getOrInsertFunction("fib",
|
||||
Type::getInt32Ty(M->getContext()),
|
||||
Type::getInt32Ty(M->getContext()),
|
||||
(Type *)0));
|
||||
|
||||
// Add a basic block to the function.
|
||||
BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
|
||||
BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
|
||||
|
||||
// Get pointers to the constants.
|
||||
Value *One = ConstantInt::get(Type::Int32Ty, 1);
|
||||
Value *Two = ConstantInt::get(Type::Int32Ty, 2);
|
||||
Value *One = ConstantInt::get(Type::getInt32Ty(M->getContext()), 1);
|
||||
Value *Two = ConstantInt::get(Type::getInt32Ty(M->getContext()), 2);
|
||||
|
||||
// Get pointer to the integer argument of the add1 function...
|
||||
Argument *ArgX = FibF->arg_begin(); // Get the arg.
|
||||
ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
|
||||
|
||||
// Create the true_block.
|
||||
BasicBlock *RetBB = BasicBlock::Create("return", FibF);
|
||||
BasicBlock *RetBB = BasicBlock::Create(M->getContext(), "return", FibF);
|
||||
// Create an exit block.
|
||||
BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF);
|
||||
BasicBlock* RecurseBB = BasicBlock::Create(M->getContext(), "recurse", FibF);
|
||||
|
||||
// Create the "if (arg < 2) goto exitbb"
|
||||
Value *CondInst = new ICmpInst(*BB, ICmpInst::ICMP_SLE, ArgX, Two, "cond");
|
||||
BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
|
||||
|
||||
// Create: ret int 1
|
||||
ReturnInst::Create(One, RetBB);
|
||||
ReturnInst::Create(M->getContext(), One, RetBB);
|
||||
|
||||
// create fib(x-1)
|
||||
Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
|
||||
@@ -104,7 +108,7 @@ static Function *CreateFibFunction(Module *M) {
|
||||
BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
|
||||
|
||||
// Create the return instruction and add it to the basic block
|
||||
ReturnInst::Create(Sum, RecurseBB);
|
||||
ReturnInst::Create(M->getContext(), Sum, RecurseBB);
|
||||
|
||||
return FibF;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user