-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.cpp
71 lines (62 loc) · 2.28 KB
/
compiler.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/raw_ostream.h"
/*
* Uses LLVM to compile a simple "Hello World!" program and prints it to stdout as LLVM IR.
*/
int main(int argc, char* argv[]) {
std::unique_ptr<llvm::LLVMContext> context = llvm::make_unique<llvm::LLVMContext>();
llvm::IRBuilder<> builder(*context);
std::unique_ptr<llvm::Module> module = llvm::make_unique<llvm::Module>("Module", *context);
std::map<std::string, llvm::Value*> namedValues;
// Declare printf as an extern.
std::vector<llvm::Type*> printfArgs = {
llvm::PointerType::getInt8PtrTy(*context) // string type
};
llvm::FunctionType* printfType = llvm::FunctionType::get(
llvm::IntegerType::getInt32Ty(*context),
printfArgs,
false /* isVarArgs */
);
llvm::Function* printf = llvm::Function::Create(
printfType,
llvm::Function::ExternalLinkage,
"printf",
module.get()
);
// Create the main function.
const auto mainType = llvm::FunctionType::get(
llvm::IntegerType::getInt32Ty(*context),
std::vector<llvm::Type*>(),
false /* isVarArgs */
);
const auto main = llvm::Function::Create(
mainType,
llvm::Function::ExternalLinkage,
"main",
module.get()
);
const auto bb = llvm::BasicBlock::Create(*context, "entry", main);
builder.SetInsertPoint(bb);
// Read stdin line-by-line and prepend "foo: " to each.
std::string content = "";
for (std::string line; std::getline(std::cin, line); /* no op */) {
content += "foo: " + line + "\n";
}
// Call printf() with the "Hello World!" string.
llvm::Value* helloWorld = builder.CreateGlobalStringPtr(content, "globalstr");
std::vector<llvm::Value*> args;
args.push_back(helloWorld);
builder.CreateCall(printf, args);
llvm::APInt retVal(32 /* bitSize */, (uint32_t) 0, true /* signed */);
builder.CreateRet(llvm::ConstantInt::get(*context, retVal));
llvm::verifyFunction(*main);
// Print the LLVM IR to stdout.
module->print(llvm::outs(), nullptr);
}