added function definition block

This commit is contained in:
Sven Vogel 2024-05-28 11:51:24 +02:00
parent 68622fbd00
commit 48110c85f2
2 changed files with 36 additions and 14 deletions

View File

@ -7,6 +7,7 @@
#include <llvm/types.h> #include <llvm/types.h>
#include <set/types.h> #include <set/types.h>
#include <sys/log.h> #include <sys/log.h>
#include <llvm/func.h>
BackendError impl_param_type(LLVMBackendCompileUnit* unit, BackendError impl_param_type(LLVMBackendCompileUnit* unit,
LLVMGlobalScope* scope, Paramer* param, LLVMGlobalScope* scope, Paramer* param,
@ -72,10 +73,42 @@ BackendError impl_func_decl(LLVMBackendCompileUnit* unit,
return err; return err;
} }
BackendError impl_func(LLVMBackendCompileUnit* unit, LLVMGlobalScope* scope, BackendError impl_func(LLVMBackendCompileUnit* unit, LLVMGlobalScope* global_scope,
FunctionDefinition* fundef, const char* name) { FunctionDefinition* fundef, const char* name) {
BackendError err = SUCCESS; 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; return err;
} }

View File

@ -14,7 +14,9 @@ typedef struct LLVMBackendCompileUnit_t {
typedef struct LLVMGlobalScope_t { typedef struct LLVMGlobalScope_t {
GHashTable* types; GHashTable* types;
// of type LLVMValueRef
GHashTable* variables; GHashTable* variables;
// of type LLVMTypeRef
GHashTable* functions; GHashTable* functions;
} LLVMGlobalScope; } LLVMGlobalScope;
@ -22,19 +24,6 @@ LLVMGlobalScope* new_global_scope();
void delete_global_scope(LLVMGlobalScope* 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**); BackendError parse_module(const Module* module, void**);
#endif // LLVM_BACKEND_PARSE_H_ #endif // LLVM_BACKEND_PARSE_H_