-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchapter03-1.cpp
More file actions
57 lines (46 loc) · 1.62 KB
/
chapter03-1.cpp
File metadata and controls
57 lines (46 loc) · 1.62 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
#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 *getGEP(IRBuilder<> &Builder, Value *Base, Value *Offset) {
return Builder.CreateGEP(Builder.getInt32Ty(), Base, Offset, "a1");
}
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 *gep = getGEP(Builder, Base, Builder.getInt32(1));
verifyFunction(*fooFunc);
ModuleOb->print(llvm::outs(), nullptr);
return 0;
}