-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
49 lines (38 loc) · 1.76 KB
/
Copy pathmain.cpp
File metadata and controls
49 lines (38 loc) · 1.76 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
40
41
42
43
44
45
46
47
48
49
#include <llvm-c/Core.h>
int main() {
// create context, module and builder
LLVMContextRef context = LLVMContextCreate();
LLVMModuleRef module = LLVMModuleCreateWithNameInContext("hello", context);
LLVMBuilderRef builder = LLVMCreateBuilderInContext(context);
// types
LLVMTypeRef int_8_type = LLVMInt8TypeInContext(context);
LLVMTypeRef int_8_type_ptr = LLVMPointerType(int_8_type, 0);
LLVMTypeRef int_32_type = LLVMInt32TypeInContext(context);
// puts function
LLVMTypeRef puts_function_args_type[] {
int_8_type_ptr
};
LLVMTypeRef puts_function_type = LLVMFunctionType(int_32_type, puts_function_args_type, 1, false);
LLVMValueRef puts_function = LLVMAddFunction(module, "puts", puts_function_type);
// end
// main function
LLVMTypeRef main_function_type = LLVMFunctionType(int_32_type, nullptr, 0, false);
LLVMValueRef main_function = LLVMAddFunction(module, "main", main_function_type);
LLVMBasicBlockRef entry = LLVMAppendBasicBlockInContext(context, main_function, "entry");
LLVMPositionBuilderAtEnd(builder, entry);
LLVMValueRef puts_function_args[] = {
LLVMBuildPointerCast(builder, // cast [14 x i8] type to int8 pointer
LLVMBuildGlobalString(builder, "Hello, World!", "hello"), // build hello string constant
int_8_type_ptr, "0")
};
LLVMBuildCall2(builder, puts_function_type, puts_function, puts_function_args, 1, "i");
LLVMBuildRet(builder, LLVMConstInt(int_32_type, 0, false));
// end
//LLVMDumpModule(module); // dump module to STDOUT
LLVMPrintModuleToFile(module, "hello.ll", nullptr);
// clean memory
LLVMDisposeBuilder(builder);
LLVMDisposeModule(module);
LLVMContextDispose(context);
return 0;
}