From d87bcccfc43e66b9d673c2ccee0132ab729faab9 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Tue, 6 May 2025 08:34:42 -0600 Subject: [PATCH] wip --- src/compiler.h | 1 + src/emitter.cpp | 15 +++++++++++++++ src/llvm.cpp | 4 ++-- src/llvm.h | 22 +++++++++++++++++++++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/compiler.h b/src/compiler.h index a4dc6f2..486e7e3 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -1,6 +1,7 @@ #pragma once #include +#include #define report_error() trap_raw() diff --git a/src/emitter.cpp b/src/emitter.cpp index 105e6d8..b822a73 100644 --- a/src/emitter.cpp +++ b/src/emitter.cpp @@ -1,5 +1,20 @@ #include + +fn void llvm_initialize(Module* module) +{ + llvm_initialize_all(); +} + void emit(Module* module) { + llvm_initialize(module); + auto context = LLVMContextCreate(); + auto m = llvm_context_create_module(context, module->name); + llvm::DIBuilder* di_builder = 0; + if (module->has_debug_info) + { + di_builder = LLVMCreateDIBuilder(m); + } + trap_raw(); } diff --git a/src/llvm.cpp b/src/llvm.cpp index 60324b0..62d4d3b 100644 --- a/src/llvm.cpp +++ b/src/llvm.cpp @@ -33,9 +33,9 @@ fn StringRef string_ref(String string) return StringRef((char*)string.pointer, string.length); } -EXPORT Module* llvm_context_create_module(LLVMContext& context, String name) +EXPORT Module* llvm_context_create_module(LLVMContext* context, String name) { - return new Module(string_ref(name), context); + return new Module(string_ref(name), *context); } EXPORT unsigned llvm_integer_type_get_bit_count(const IntegerType& integer_type) diff --git a/src/llvm.h b/src/llvm.h index 8ab155a..55ddd84 100644 --- a/src/llvm.h +++ b/src/llvm.h @@ -1,9 +1,15 @@ +#pragma once + #include namespace llvm { class Type; class Value; + class Module; + class Builder; + class DIBuilder; + class LLVMContext; } fn bool llvm_initialized = false; @@ -19,6 +25,11 @@ extern "C" String llvm_default_target_triple(); extern "C" String llvm_host_cpu_name(); extern "C" String llvm_host_cpu_features(); +extern "C" llvm::LLVMContext* LLVMContextCreate(); +extern "C" llvm::Module* llvm_context_create_module(llvm::LLVMContext* context, String name); +extern "C" llvm::Builder* LLVMCreateBuilderInContext(llvm::LLVMContext* context, String name); +extern "C" llvm::DIBuilder* LLVMCreateDIBuilder(llvm::Module* module); + struct LLVMGlobal { String host_triple; @@ -28,9 +39,10 @@ struct LLVMGlobal global_variable LLVMGlobal llvm_global; -fn void initialize_all() +fn void llvm_initialize_all_raw() { assert(!llvm_initialized); + LLVMInitializeX86TargetInfo(); LLVMInitializeX86Target(); LLVMInitializeX86TargetMC(); @@ -44,3 +56,11 @@ fn void initialize_all() .host_cpu_features = llvm_host_cpu_features(), }; } + +fn void llvm_initialize_all() +{ + if (!llvm_initialized) + { + llvm_initialize_all_raw(); + } +}