commit 721f9964137f4091048e359bfe9f6674020d4d35 Author: Sofia Date: Wed Apr 1 20:46:38 2026 +0300 Implement a simple Hello World with LLVM diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..5444ab6 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++20", + "intelliSenseMode": "linux-clang-x86" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3545b8c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.20.0) +project(SimpleProject) + +set(PROJECT_NAME llvm_c_compiler) +project(${PROJECT_NAME} VERSION 1.0.0 LANGUAGES CXX) + +# Include LLVM as library +find_package(LLVM 21.1.0 CONFIG) + +message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}") +message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}") + +include_directories(${LLVM_INCLUDE_DIRS}) +separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS}) +add_definitions(${LLVM_DEFINITIONS_LIST}) + +# Executable +add_executable(${PROJECT_NAME} src/main.cpp) +target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20) + +# Find the libraries that correspond to the LLVM components +# that we wish to use +llvm_map_components_to_libnames(llvm_libs support core irreader) + +# Link +target_link_libraries(${PROJECT_NAME} LLVM) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..24de88e --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetOptions.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +int main() { + auto Context = std::make_unique(); + auto Module = std::make_unique("my cool module", *Context); + + auto Builder = std::make_unique>(*Context); + + llvm::FunctionType* FunType = llvm::FunctionType::get(llvm::Type::getInt32Ty(*Context), false); + llvm::Function* Function = llvm::Function::Create( + FunType, llvm::GlobalValue::LinkageTypes::ExternalLinkage, "main", *Module); + + llvm::BasicBlock* BB = llvm::BasicBlock::Create(*Context, "entry", Function, nullptr); + + Builder->SetInsertPoint(BB); + llvm::Value* value = llvm::ConstantInt::get(llvm::Type::getInt32Ty(*Context), 5); + Builder->CreateRet(value); + + llvm::verifyFunction(*Function); + + llvm::InitializeAllTargetInfos(); + llvm::InitializeAllTargets(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllAsmParsers(); + llvm::InitializeAllAsmPrinters(); + + auto TargetTriple = llvm::sys::getDefaultTargetTriple(); + std::string Error; + auto Target = llvm::TargetRegistry::lookupTarget(TargetTriple, Error); + + llvm::TargetOptions opt; + auto TargetMachine = Target->createTargetMachine(TargetTriple, "generic", "", opt, llvm::Reloc::PIC_); + Module->setDataLayout(TargetMachine->createDataLayout()); + + // auto Filename = "output.o"; + // std::error_code EC; + // llvm::raw_fd_ostream dest(Filename, EC, llvm::sys::fs::OpenFlags::OF_Text); + + // if (EC) { + // llvm::errs() << "Could not open file: " << EC.message(); + // return 1; + // } + std::string out; + llvm::raw_string_ostream dest{ out }; + + Module->print(dest, nullptr); + dest.flush(); + + std::cout << out << std::endl; + std::cout << "hello" << std::endl; + return 0; +} \ No newline at end of file