llvm.org GIT mirror llvm / e8bf58c
Simplify code, make it print the constructed module before it is run. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@15792 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 15 years ago
3 changed file(s) with 147 addition(s) and 273 deletion(s). Raw diff Collapse all Expand all
99 // This small program provides an example of how to quickly build a small
1010 // module with two functions and execute it with the JIT.
1111 //
12 //===------------------------------------------------------------------------===
13
1412 // Goal:
1513 // The goal of this snippet is to create in the memory
1614 // the LLVM module consisting of two functions as follow:
3331 // conflict of temporary function name with some real
3432 // existing function name?
3533 //
34 //===----------------------------------------------------------------------===//
3635
37 #include
38
39 #include
40 #include
41 #include
42 #include
43 #include
44
36 #include "llvm/Module.h"
37 #include "llvm/Constants.h"
38 #include "llvm/Type.h"
39 #include "llvm/Instructions.h"
40 #include "llvm/ModuleProvider.h"
4541 #include "llvm/ExecutionEngine/ExecutionEngine.h"
4642 #include "llvm/ExecutionEngine/GenericValue.h"
47
48
43 #include
4944 using namespace llvm;
5045
5146 int main() {
52
5347 // Create some module to put our function into it.
5448 Module *M = new Module("test");
5549
50 // Create the add1 function entry and insert this entry into module M. The
51 // function will have a return type of "int" and take an argument of "int".
52 // The '0' terminates the list of argument types.
53 Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy, 0);
5654
57 // We are about to create the add1 function:
58 Function *Add1F;
55 // Add a basic block to the function. As before, it automatically inserts
56 // because of the last argument.
57 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
58
59 // Get pointers to the constant `1'.
60 Value *One = ConstantSInt::get(Type::IntTy, 1);
5961
60 {
61 // first create type for the single argument of add1 function:
62 // the type is 'int ()'
63 std::vector ArgT(1);
64 ArgT[0] = Type::IntTy;
62 // Get pointers to the integer argument of the add1 function...
63 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
64 Argument *ArgX = Add1F->abegin(); // Get the arg
65 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
6566
66 // now create full type of the add1 function:
67 FunctionType *Add1T = FunctionType::get(Type::IntTy, // type of result
68 ArgT,
69 /*not vararg*/false);
70
71 // Now create the add1 function entry and
72 // insert this entry into module M
73 // (By passing a module as the last parameter to the Function constructor,
74 // it automatically gets appended to the Module.)
75 Add1F = new Function(Add1T,
76 Function::ExternalLinkage, // maybe too much
77 "add1", M);
67 // Create the add instruction, inserting it into the end of BB.
68 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
69
70 // Create the return instruction and add it to the basic block
71 new ReturnInst(Add, BB);
7872
79 // Add a basic block to the function... (again, it automatically inserts
80 // because of the last argument.)
81 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", Add1F);
82
83 // Get pointers to the constant `1'...
84 Value *One = ConstantSInt::get(Type::IntTy, 1);
85
86 // Get pointers to the integer argument of the add1 function...
87 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
88 Argument &ArgX = Add1F->afront(); // Get the arg
89
90 // Create the add instruction... does not insert...
91 Instruction *Add = BinaryOperator::create(Instruction::Add, One, &ArgX,
92 "addresult");
93
94 // explicitly insert it into the basic block...
95 BB->getInstList().push_back(Add);
96
97 // Create the return instruction and add it to the basic block
98 BB->getInstList().push_back(new ReturnInst(Add));
99
100 // function add1 is ready
101 }
73 // Now, function add1 is ready.
10274
10375
104 // now we going to create function `foo':
105 Function *FooF;
76 // Now we going to create function `foo', which returns an int and takes no
77 // arguments.
78 Function *FooF = M->getOrInsertFunction("foo", Type::IntTy, 0);
10679
107 {
108 // Create the foo function type:
109 FunctionType *FooT =
110 FunctionType::get(Type::IntTy, // result has type: 'int ()'
111 std::vector(), // no arguments
112 /*not vararg*/false);
80 // Add a basic block to the FooF function.
81 BB = new BasicBlock("EntryBlock", FooF);
82
83 // Get pointers to the constant `10'.
84 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
85
86 // Pass Ten to the call call:
87 std::vector Params;
88 Params.push_back(Ten);
89 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
11390
114 // create the entry for function `foo' and insert
115 // this entry into module M:
116 FooF =
117 new Function(FooT,
118 Function::ExternalLinkage, // too wide?
119 "foo", M);
120
121 // Add a basic block to the FooF function...
122 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", FooF);
91 // Create the return instruction and add it to the basic block.
92 new ReturnInst(Add1CallRes, BB);
12393
124 // Get pointers to the constant `10'...
125 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
94 // Now we create the JIT.
95 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
96 ExecutionEngine* EE = ExecutionEngine::create(MP, true);
12697
127 // Put the argument Ten on stack and make call:
128 // ...
129 std::vector Params;
130 Params.push_back(Ten);
131 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
132
133 // Create the return instruction and add it to the basic block
134 BB->getInstList().push_back(new ReturnInst(Add1CallRes));
135
136 }
137
138 // Now we going to create JIT ??
139 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
140 ExecutionEngine* EE = ExecutionEngine::create( MP, true );
98 std::cout << "We just constructed this LLVM module:\n\n" << *M;
99 std::cout << "\n\nRunning foo: " << std::flush;
141100
142101 // Call the `foo' function with no arguments:
143102 std::vector noargs;
144103 GenericValue gv = EE->runFunction(FooF, noargs);
145104
146 // import result of execution:
147 std::cout << "Result: " << gv.IntVal << std:: endl;
148
105 // Import result of execution:
106 std::cout << "Result: " << gv.IntVal << "\n";
149107 return 0;
150108 }
99 // This small program provides an example of how to quickly build a small
1010 // module with two functions and execute it with the JIT.
1111 //
12 //===------------------------------------------------------------------------===
13
1412 // Goal:
1513 // The goal of this snippet is to create in the memory
1614 // the LLVM module consisting of two functions as follow:
3331 // conflict of temporary function name with some real
3432 // existing function name?
3533 //
34 //===----------------------------------------------------------------------===//
3635
37 #include
38
39 #include
40 #include
41 #include
42 #include
43 #include
44
36 #include "llvm/Module.h"
37 #include "llvm/Constants.h"
38 #include "llvm/Type.h"
39 #include "llvm/Instructions.h"
40 #include "llvm/ModuleProvider.h"
4541 #include "llvm/ExecutionEngine/ExecutionEngine.h"
4642 #include "llvm/ExecutionEngine/GenericValue.h"
47
48
43 #include
4944 using namespace llvm;
5045
5146 int main() {
52
5347 // Create some module to put our function into it.
5448 Module *M = new Module("test");
5549
50 // Create the add1 function entry and insert this entry into module M. The
51 // function will have a return type of "int" and take an argument of "int".
52 // The '0' terminates the list of argument types.
53 Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy, 0);
5654
57 // We are about to create the add1 function:
58 Function *Add1F;
55 // Add a basic block to the function. As before, it automatically inserts
56 // because of the last argument.
57 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
58
59 // Get pointers to the constant `1'.
60 Value *One = ConstantSInt::get(Type::IntTy, 1);
5961
60 {
61 // first create type for the single argument of add1 function:
62 // the type is 'int ()'
63 std::vector ArgT(1);
64 ArgT[0] = Type::IntTy;
62 // Get pointers to the integer argument of the add1 function...
63 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
64 Argument *ArgX = Add1F->abegin(); // Get the arg
65 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
6566
66 // now create full type of the add1 function:
67 FunctionType *Add1T = FunctionType::get(Type::IntTy, // type of result
68 ArgT,
69 /*not vararg*/false);
70
71 // Now create the add1 function entry and
72 // insert this entry into module M
73 // (By passing a module as the last parameter to the Function constructor,
74 // it automatically gets appended to the Module.)
75 Add1F = new Function(Add1T,
76 Function::ExternalLinkage, // maybe too much
77 "add1", M);
67 // Create the add instruction, inserting it into the end of BB.
68 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
69
70 // Create the return instruction and add it to the basic block
71 new ReturnInst(Add, BB);
7872
79 // Add a basic block to the function... (again, it automatically inserts
80 // because of the last argument.)
81 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", Add1F);
82
83 // Get pointers to the constant `1'...
84 Value *One = ConstantSInt::get(Type::IntTy, 1);
85
86 // Get pointers to the integer argument of the add1 function...
87 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
88 Argument &ArgX = Add1F->afront(); // Get the arg
89
90 // Create the add instruction... does not insert...
91 Instruction *Add = BinaryOperator::create(Instruction::Add, One, &ArgX,
92 "addresult");
93
94 // explicitly insert it into the basic block...
95 BB->getInstList().push_back(Add);
96
97 // Create the return instruction and add it to the basic block
98 BB->getInstList().push_back(new ReturnInst(Add));
99
100 // function add1 is ready
101 }
73 // Now, function add1 is ready.
10274
10375
104 // now we going to create function `foo':
105 Function *FooF;
76 // Now we going to create function `foo', which returns an int and takes no
77 // arguments.
78 Function *FooF = M->getOrInsertFunction("foo", Type::IntTy, 0);
10679
107 {
108 // Create the foo function type:
109 FunctionType *FooT =
110 FunctionType::get(Type::IntTy, // result has type: 'int ()'
111 std::vector(), // no arguments
112 /*not vararg*/false);
80 // Add a basic block to the FooF function.
81 BB = new BasicBlock("EntryBlock", FooF);
82
83 // Get pointers to the constant `10'.
84 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
85
86 // Pass Ten to the call call:
87 std::vector Params;
88 Params.push_back(Ten);
89 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
11390
114 // create the entry for function `foo' and insert
115 // this entry into module M:
116 FooF =
117 new Function(FooT,
118 Function::ExternalLinkage, // too wide?
119 "foo", M);
120
121 // Add a basic block to the FooF function...
122 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", FooF);
91 // Create the return instruction and add it to the basic block.
92 new ReturnInst(Add1CallRes, BB);
12393
124 // Get pointers to the constant `10'...
125 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
94 // Now we create the JIT.
95 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
96 ExecutionEngine* EE = ExecutionEngine::create(MP, true);
12697
127 // Put the argument Ten on stack and make call:
128 // ...
129 std::vector Params;
130 Params.push_back(Ten);
131 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
132
133 // Create the return instruction and add it to the basic block
134 BB->getInstList().push_back(new ReturnInst(Add1CallRes));
135
136 }
137
138 // Now we going to create JIT ??
139 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
140 ExecutionEngine* EE = ExecutionEngine::create( MP, true );
98 std::cout << "We just constructed this LLVM module:\n\n" << *M;
99 std::cout << "\n\nRunning foo: " << std::flush;
141100
142101 // Call the `foo' function with no arguments:
143102 std::vector noargs;
144103 GenericValue gv = EE->runFunction(FooF, noargs);
145104
146 // import result of execution:
147 std::cout << "Result: " << gv.IntVal << std:: endl;
148
105 // Import result of execution:
106 std::cout << "Result: " << gv.IntVal << "\n";
149107 return 0;
150108 }
99 // This small program provides an example of how to quickly build a small
1010 // module with two functions and execute it with the JIT.
1111 //
12 //===------------------------------------------------------------------------===
13
1412 // Goal:
1513 // The goal of this snippet is to create in the memory
1614 // the LLVM module consisting of two functions as follow:
3331 // conflict of temporary function name with some real
3432 // existing function name?
3533 //
34 //===----------------------------------------------------------------------===//
3635
37 #include
38
39 #include
40 #include
41 #include
42 #include
43 #include
44
36 #include "llvm/Module.h"
37 #include "llvm/Constants.h"
38 #include "llvm/Type.h"
39 #include "llvm/Instructions.h"
40 #include "llvm/ModuleProvider.h"
4541 #include "llvm/ExecutionEngine/ExecutionEngine.h"
4642 #include "llvm/ExecutionEngine/GenericValue.h"
47
48
43 #include
4944 using namespace llvm;
5045
5146 int main() {
52
5347 // Create some module to put our function into it.
5448 Module *M = new Module("test");
5549
50 // Create the add1 function entry and insert this entry into module M. The
51 // function will have a return type of "int" and take an argument of "int".
52 // The '0' terminates the list of argument types.
53 Function *Add1F = M->getOrInsertFunction("add1", Type::IntTy, Type::IntTy, 0);
5654
57 // We are about to create the add1 function:
58 Function *Add1F;
55 // Add a basic block to the function. As before, it automatically inserts
56 // because of the last argument.
57 BasicBlock *BB = new BasicBlock("EntryBlock", Add1F);
58
59 // Get pointers to the constant `1'.
60 Value *One = ConstantSInt::get(Type::IntTy, 1);
5961
60 {
61 // first create type for the single argument of add1 function:
62 // the type is 'int ()'
63 std::vector ArgT(1);
64 ArgT[0] = Type::IntTy;
62 // Get pointers to the integer argument of the add1 function...
63 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
64 Argument *ArgX = Add1F->abegin(); // Get the arg
65 ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
6566
66 // now create full type of the add1 function:
67 FunctionType *Add1T = FunctionType::get(Type::IntTy, // type of result
68 ArgT,
69 /*not vararg*/false);
70
71 // Now create the add1 function entry and
72 // insert this entry into module M
73 // (By passing a module as the last parameter to the Function constructor,
74 // it automatically gets appended to the Module.)
75 Add1F = new Function(Add1T,
76 Function::ExternalLinkage, // maybe too much
77 "add1", M);
67 // Create the add instruction, inserting it into the end of BB.
68 Instruction *Add = BinaryOperator::createAdd(One, ArgX, "addresult", BB);
69
70 // Create the return instruction and add it to the basic block
71 new ReturnInst(Add, BB);
7872
79 // Add a basic block to the function... (again, it automatically inserts
80 // because of the last argument.)
81 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", Add1F);
82
83 // Get pointers to the constant `1'...
84 Value *One = ConstantSInt::get(Type::IntTy, 1);
85
86 // Get pointers to the integer argument of the add1 function...
87 assert(Add1F->abegin() != Add1F->aend()); // Make sure there's an arg
88 Argument &ArgX = Add1F->afront(); // Get the arg
89
90 // Create the add instruction... does not insert...
91 Instruction *Add = BinaryOperator::create(Instruction::Add, One, &ArgX,
92 "addresult");
93
94 // explicitly insert it into the basic block...
95 BB->getInstList().push_back(Add);
96
97 // Create the return instruction and add it to the basic block
98 BB->getInstList().push_back(new ReturnInst(Add));
99
100 // function add1 is ready
101 }
73 // Now, function add1 is ready.
10274
10375
104 // now we going to create function `foo':
105 Function *FooF;
76 // Now we going to create function `foo', which returns an int and takes no
77 // arguments.
78 Function *FooF = M->getOrInsertFunction("foo", Type::IntTy, 0);
10679
107 {
108 // Create the foo function type:
109 FunctionType *FooT =
110 FunctionType::get(Type::IntTy, // result has type: 'int ()'
111 std::vector(), // no arguments
112 /*not vararg*/false);
80 // Add a basic block to the FooF function.
81 BB = new BasicBlock("EntryBlock", FooF);
82
83 // Get pointers to the constant `10'.
84 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
85
86 // Pass Ten to the call call:
87 std::vector Params;
88 Params.push_back(Ten);
89 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
11390
114 // create the entry for function `foo' and insert
115 // this entry into module M:
116 FooF =
117 new Function(FooT,
118 Function::ExternalLinkage, // too wide?
119 "foo", M);
120
121 // Add a basic block to the FooF function...
122 BasicBlock *BB = new BasicBlock("EntryBlock of add1 function", FooF);
91 // Create the return instruction and add it to the basic block.
92 new ReturnInst(Add1CallRes, BB);
12393
124 // Get pointers to the constant `10'...
125 Value *Ten = ConstantSInt::get(Type::IntTy, 10);
94 // Now we create the JIT.
95 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
96 ExecutionEngine* EE = ExecutionEngine::create(MP, true);
12697
127 // Put the argument Ten on stack and make call:
128 // ...
129 std::vector Params;
130 Params.push_back(Ten);
131 CallInst * Add1CallRes = new CallInst(Add1F, Params, "add1", BB);
132
133 // Create the return instruction and add it to the basic block
134 BB->getInstList().push_back(new ReturnInst(Add1CallRes));
135
136 }
137
138 // Now we going to create JIT ??
139 ExistingModuleProvider* MP = new ExistingModuleProvider(M);
140 ExecutionEngine* EE = ExecutionEngine::create( MP, true );
98 std::cout << "We just constructed this LLVM module:\n\n" << *M;
99 std::cout << "\n\nRunning foo: " << std::flush;
141100
142101 // Call the `foo' function with no arguments:
143102 std::vector noargs;
144103 GenericValue gv = EE->runFunction(FooF, noargs);
145104
146 // import result of execution:
147 std::cout << "Result: " << gv.IntVal << std:: endl;
148
105 // Import result of execution:
106 std::cout << "Result: " << gv.IntVal << "\n";
149107 return 0;
150108 }