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
| #include <iostream>
#include <llvm/ADT/APFloat.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/Verifier.h>
using namespace llvm;
int main() {
LLVMContext context;
IRBuilder<> builder(context);
std::unique_ptr<Module> module = std::make_unique<Module>("helloworld", context);
// Declare "puts" function
std::vector<Type *> puts_args;
puts_args.push_back(Type::getInt8PtrTy(context));
FunctionType *puts_func_type = FunctionType::get(Type::getInt32Ty(context), puts_args, false);
Function *puts_func = Function::Create(puts_func_type, Function::ExternalLinkage, "puts", module.get());
// Create main function
FunctionType *main_func_type = FunctionType::get(Type::getInt32Ty(context), false);
Function *main_func = Function::Create(main_func_type, Function::ExternalLinkage, "main", module.get());
// Create basic block for main function
BasicBlock *entry_block = BasicBlock::Create(context, "entry", main_func);
builder.SetInsertPoint(entry_block);
// Create "Hello, World!" constant string
Constant *hello_world = builder.CreateGlobalStringPtr("Hello, World!\n");
// Call "puts" function with "Hello, World!" as argument
builder.CreateCall(puts_func, hello_world);
// Return 0
builder.CreateRet(ConstantInt::get(Type::getInt32Ty(context), 0));
// Verify main function
verifyFunction(*main_func);
// Print generated IR
module->print(outs(), nullptr);
return 0;
}
|