-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter03-4.cpp
More file actions
63 lines (50 loc) · 1.78 KB
/
chapter03-4.cpp
File metadata and controls
63 lines (50 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include <vector>
using namespace llvm;
LLVMContext Context;
static Module *ModuleOb = new Module("my compiler", Context);
static std::vector<std::string> FunArgs;
Function *createFunc(IRBuilder<> &Builder, std::string Name) {
Type *u32Ty = Type::getInt32Ty(Context);
Type *vecTy = VectorType::get(u32Ty, 2);
Type *ptrTy = vecTy->getPointerTo(0);
FunctionType *funcType =
llvm::FunctionType::get(Builder.getInt32Ty(),ptrTy, false);
Function *fooFunc = llvm::Function::Create(
funcType, llvm::Function::ExternalLinkage, Name, ModuleOb);
return fooFunc;
}
void setFuncArgs(Function *fooFunc, std::vector<std::string> FunArgs) {
unsigned Idx = 0;
Function::arg_iterator AI, AE;
for(AI = fooFunc->arg_begin(), AE = fooFunc->arg_end(); AI != AE;
++AI, ++Idx)
AI->setName(FunArgs[Idx]);
}
BasicBlock *createBB(Function *fooFunc, std::string Name) {
return BasicBlock::Create(Context, Name, fooFunc);
}
Value *getInsertElement(IRBuilder<> &Builder, Value *Vec, Value *Val,
Value *Index) {
return Builder.CreateInsertElement(Vec, Val, Index);
}
int main(int argc, char *argv[])
{
FunArgs.push_back("a");
static IRBuilder<> Builder(Context);
Function *fooFunc = createFunc(Builder, "foo");
setFuncArgs(fooFunc, FunArgs);
//Value *Base = fooFunc->arg_begin();
BasicBlock *entry = createBB(fooFunc, "entry");
Builder.SetInsertPoint(entry);
Value *Vec = fooFunc->arg_begin();
for(unsigned int i=0; i<4;i++)
Value *V = getInsertElement(Builder, Vec, Builder.getInt32((i+1)*10), Builder.getInt32(i));
Builder.CreateRet(Builder.getInt32(0));
verifyFunction(*fooFunc);
ModuleOb->print(llvm::outs(), nullptr);
return 0;
}