Implement a simple Hello World with LLVM

This commit is contained in:
Sofia 2026-04-01 20:46:38 +03:00
commit 721f996413
4 changed files with 109 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build

16
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -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
}

26
CMakeLists.txt Normal file
View File

@ -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)

66
src/main.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <llvm/IR/LLVMContext.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/TargetSelect.h>
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <llvm/MC/TargetRegistry.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <llvm/TargetParser/Host.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Verifier.h>
#include <iostream>
int main() {
auto Context = std::make_unique<llvm::LLVMContext>();
auto Module = std::make_unique<llvm::Module>("my cool module", *Context);
auto Builder = std::make_unique<llvm::IRBuilder<>>(*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;
}