├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.14) 2 | project(llvm_hello) 3 | 4 | set(CMAKE_CXX_FLAGS -lLLVM) # link LLVM libs 5 | add_executable(llvm_hello main.cpp) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Максим Тарасов 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LLVM Hello 2 | 3 | Hello world on LLVM C API example 4 | 5 | ## Building 6 | 7 | Install llvm and compile using cmake 8 | 9 | ```bash 10 | mkdir build && cd build 11 | cmake .. 12 | cmake --build . 13 | ``` 14 | 15 | ## Running 16 | 17 | After building run `llvm_hello` executable from build directory. 18 | 19 | This executable generate `hello.ll` using LLVM C API 20 | 21 | To run `hello.ll` file use this command ``lli hello.ll`` -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | // create context, module and builder 5 | LLVMContextRef context = LLVMContextCreate(); 6 | LLVMModuleRef module = LLVMModuleCreateWithNameInContext("hello", context); 7 | LLVMBuilderRef builder = LLVMCreateBuilderInContext(context); 8 | 9 | // types 10 | LLVMTypeRef int_8_type = LLVMInt8TypeInContext(context); 11 | LLVMTypeRef int_8_type_ptr = LLVMPointerType(int_8_type, 0); 12 | LLVMTypeRef int_32_type = LLVMInt32TypeInContext(context); 13 | 14 | // puts function 15 | LLVMTypeRef puts_function_args_type[] { 16 | int_8_type_ptr 17 | }; 18 | 19 | LLVMTypeRef puts_function_type = LLVMFunctionType(int_32_type, puts_function_args_type, 1, false); 20 | LLVMValueRef puts_function = LLVMAddFunction(module, "puts", puts_function_type); 21 | // end 22 | 23 | // main function 24 | LLVMTypeRef main_function_type = LLVMFunctionType(int_32_type, nullptr, 0, false); 25 | LLVMValueRef main_function = LLVMAddFunction(module, "main", main_function_type); 26 | 27 | LLVMBasicBlockRef entry = LLVMAppendBasicBlockInContext(context, main_function, "entry"); 28 | LLVMPositionBuilderAtEnd(builder, entry); 29 | 30 | LLVMValueRef puts_function_args[] = { 31 | LLVMBuildPointerCast(builder, // cast [14 x i8] type to int8 pointer 32 | LLVMBuildGlobalString(builder, "Hello, World!", "hello"), // build hello string constant 33 | int_8_type_ptr, "0") 34 | }; 35 | 36 | LLVMBuildCall2(builder, puts_function_type, puts_function, puts_function_args, 1, "i"); 37 | LLVMBuildRet(builder, LLVMConstInt(int_32_type, 0, false)); 38 | // end 39 | 40 | //LLVMDumpModule(module); // dump module to STDOUT 41 | LLVMPrintModuleToFile(module, "hello.ll", nullptr); 42 | 43 | // clean memory 44 | LLVMDisposeBuilder(builder); 45 | LLVMDisposeModule(module); 46 | LLVMContextDispose(context); 47 | 48 | return 0; 49 | } --------------------------------------------------------------------------------