-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.cc
60 lines (50 loc) · 1.89 KB
/
generator.cc
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
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/Argument.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/ToolOutputFile.h"
#include <memory>
using namespace llvm;
int main() {
LLVMContext context;
// Create sum module to put out function into it.
auto owner = std::make_unique<Module>("sum", context);
Module *m = owner.get();
// Create the sum function entry and insert this entry into module m.
// The function will have a return type of "int" and take two arguments of
// "int".
Function *sum = Function::Create(
FunctionType::get(Type::getInt32Ty(context),
{Type::getInt32Ty(context), Type::getInt32Ty(context)},
false),
Function::ExternalLinkage, "sum", m);
// Add a basic block to the function. As before, it automatically
// inserts after the last parameter.
BasicBlock *bb = BasicBlock::Create(context, "EntryBlock", sum);
// Create a basic block builder. The builder will automatically append
// instructions to the basic block.
IRBuilder<> builder(bb);
Argument *arg_x = sum->arg_begin();
arg_x->setName("x");
Argument *arg_y = sum->arg_begin() + 1;
arg_y->setName("y");
// Create the add instruction, inserting in into the end of bb.
Value *add = builder.CreateAdd(arg_x, arg_y);
// Create the return instruction and add it to the basic block.
builder.CreateRet(add);
// Validate the generatord code.
verifyFunction(*sum);
std::error_code error_code;
outs() << "Write bitcode to file: sum.bc\n\n";
std::unique_ptr<ToolOutputFile> out(
new ToolOutputFile("./sum.bc", error_code, sys::fs::F_None));
WriteBitcodeToFile(*m, out->os());
out->keep();
return 0;
}