-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen.cpp
More file actions
39 lines (27 loc) · 1.06 KB
/
gen.cpp
File metadata and controls
39 lines (27 loc) · 1.06 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
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
using namespace llvm;
int main() {
LLVMContext C;
Module M("helloworld", C);
// Create the `int main()` function, which is the entry point to the Hello
// World application.
Function *main =
Function::Create(FunctionType::get(Type::getInt32Ty(C), false),
GlobalValue::ExternalLinkage, "main", M);
IRBuilder B(C);
// We need to an entry a basic block to the function, where we'll insert
// instructions.
BasicBlock *entry = BasicBlock::Create(C, "entry", main);
B.SetInsertPoint(entry);
// Get the libc puts function declaration.
FunctionCallee puts = M.getOrInsertFunction(
"puts", FunctionType::get(Type::getInt32Ty(C),
{PointerType::getUnqual(C)}, false));
// Create a null-terminated global string.
Value *string = B.CreateGlobalString("Hello, World!");
B.CreateCall(puts, {string});
B.CreateRet(B.getInt32(0));
M.print(outs(), nullptr);
return 0;
}