-
Notifications
You must be signed in to change notification settings - Fork 3
/
llvm-demo.cpp
40 lines (34 loc) · 1.16 KB
/
llvm-demo.cpp
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
#include <llvm/ADT/ArrayRef.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/IRBuilder.h>
#include <vector>
#include <string>
using namespace llvm;
using namespace std;
int main()
{
LLVMContext &context = getGlobalContext();
Module module("demo", context);
IRBuilder<> builder(context);
FunctionType *main = FunctionType::get(builder.getInt32Ty(), false);
Function *mainFn =
Function::Create(main, Function::ExternalLinkage, "main", &module);
BasicBlock *mainBlk = BasicBlock::Create(context, "entry", mainFn);
builder.SetInsertPoint(mainBlk);
Value *str = builder.CreateGlobalStringPtr("Hello World!", "str");
vector<Type *> args;
// args.push_back(builder.getInt8Ty()->getPointerTo());
args.push_back(builder.getInt8PtrTy());
FunctionType *puts =
FunctionType::get(builder.getInt32Ty(), args, false);
// Function *putsFn = Function::Create(puts, Function::ExternalLinkage,
// "puts", &module);
Constant *putsFn = module.getOrInsertFunction("puts", puts);
builder.CreateCall(putsFn, str);
builder.CreateRet(builder.getInt32(0));
module.dump();
return 0;
}