From 48110c85f20745d1b7461d10e6c4113f8f75bce5 Mon Sep 17 00:00:00 2001 From: servostar Date: Tue, 28 May 2024 11:51:24 +0200 Subject: [PATCH] added function definition block --- src/llvm/func.c | 35 ++++++++++++++++++++++++++++++++++- src/llvm/parser.h | 15 ++------------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/llvm/func.c b/src/llvm/func.c index 5ac63d8..c9a4e65 100644 --- a/src/llvm/func.c +++ b/src/llvm/func.c @@ -7,6 +7,7 @@ #include #include #include +#include BackendError impl_param_type(LLVMBackendCompileUnit* unit, LLVMGlobalScope* scope, Paramer* param, @@ -72,10 +73,42 @@ BackendError impl_func_decl(LLVMBackendCompileUnit* unit, return err; } -BackendError impl_func(LLVMBackendCompileUnit* unit, LLVMGlobalScope* scope, +BackendError impl_func(LLVMBackendCompileUnit* unit, LLVMGlobalScope* global_scope, FunctionDefinition* fundef, const char* name) { BackendError err = SUCCESS; + LLVMValueRef llvm_func = NULL; + err = impl_func_decl(unit, global_scope, fundef, &llvm_func, name); + + if (err.kind == Success) { + // create local function scope + // NOTE: lives till the end of the function + LLVMFuncScope* func_scope = alloca(sizeof(LLVMFuncScope)); + + func_scope->llvm_func = llvm_func; + func_scope->global_scope = global_scope; + func_scope->params = g_hash_table_new(g_str_hash, g_str_equal); + + // store function type in global scope + g_hash_table_insert(global_scope->functions, (gpointer) name, llvm_func); + + // create function body builder + LLVMBasicBlockRef body = LLVMAppendBasicBlockInContext(unit->context, llvm_func, "entry"); + LLVMBuilderRef builder = LLVMCreateBuilderInContext(unit->context); + LLVMPositionBuilderAtEnd(builder, body); + + // create value references for parameter + const size_t params = fundef->parameter->len; + for (size_t i = 0; i < params; i++) { + const Paramer* param = ((Paramer*) fundef->parameter) + i; + g_hash_table_insert(func_scope->params, (gpointer) param->name, LLVMGetParam(llvm_func, i)); + } + + // parse function body + + // delete function scope GLib structs + g_hash_table_destroy(func_scope->params); + } return err; } diff --git a/src/llvm/parser.h b/src/llvm/parser.h index 9a51e60..f045f43 100644 --- a/src/llvm/parser.h +++ b/src/llvm/parser.h @@ -14,7 +14,9 @@ typedef struct LLVMBackendCompileUnit_t { typedef struct LLVMGlobalScope_t { GHashTable* types; + // of type LLVMValueRef GHashTable* variables; + // of type LLVMTypeRef GHashTable* functions; } LLVMGlobalScope; @@ -22,19 +24,6 @@ LLVMGlobalScope* new_global_scope(); void delete_global_scope(LLVMGlobalScope* scope); -typedef struct LLVMLocalScope_t LLVMLocalScope; - -typedef struct LLVMLocalScope_t { - LLVMGlobalScope* global_scope; - LLVMLocalScope* parent_scope; - GHashTable* params; - GHashTable* variables; -} LLVMLocalScope; - -LLVMLocalScope* new_local_scope(LLVMGlobalScope* global_scope, LLVMLocalScope* parent_scope); - -void delete_local_scope(LLVMLocalScope* scope); - BackendError parse_module(const Module* module, void**); #endif // LLVM_BACKEND_PARSE_H_