From 3b78d117b5e8d0e79f5b4b7a36b3e189e42d1667 Mon Sep 17 00:00:00 2001 From: servostar Date: Tue, 21 May 2024 15:55:22 +0200 Subject: [PATCH] added default values for composites --- src/llvm/backend.c | 2 ++ src/llvm/decl/variable.c | 29 ++++++++++++++++++++++++++++- src/llvm/types/type.c | 33 +++++++++++++++++++++++++++++++++ src/llvm/types/type.h | 2 ++ 4 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/llvm/backend.c b/src/llvm/backend.c index 3621338..204e06a 100644 --- a/src/llvm/backend.c +++ b/src/llvm/backend.c @@ -48,6 +48,8 @@ static BackendError llvm_backend_codegen(const AST_NODE_PTR module_node, void**) GemstoneDeclRef decl = ((GemstoneDeclRef*) decls->data)[i]; type_scope_add_variable(global_scope, decl); } + // TODO: create LLVMValueRef of global/static variables + break; default: PANIC("NOT IMPLEMENTED"); diff --git a/src/llvm/decl/variable.c b/src/llvm/decl/variable.c index 15e902b..878dc2d 100644 --- a/src/llvm/decl/variable.c +++ b/src/llvm/decl/variable.c @@ -1,9 +1,12 @@ +#include "llvm/types/scope.h" #include "llvm/types/structs.h" +#include #include #include #include #include #include +#include static StorageQualifier get_storage_qualifier_from_ast(AST_NODE_PTR storageQualifierNode) { if (storageQualifierNode->kind != AST_Storage) { @@ -34,7 +37,7 @@ GArray* declaration_from_ast(TypeScopeRef scope, const AST_NODE_PTR node) { AST_NODE_PTR first_child = AST_get_node(node, 0); - StorageQualifier qualifier = StorageQualifierLocal; + StorageQualifier qualifier = StorageQualifierStatic; GemstoneTypeRef type = NULL; AST_NODE_PTR list = NULL; @@ -63,3 +66,27 @@ GArray* declaration_from_ast(TypeScopeRef scope, const AST_NODE_PTR node) { return decls; } + +LLVMValueRef create_declaration_reference(LLVMModuleRef module, LLVMBuilderRef builder, GemstoneDeclRef decl) { + LLVMContextRef context = LLVMGetModuleContext(module); + LLVMTypeRef llvmTypeRef = llvm_type_from_gemstone_type(context, decl->type); + LLVMValueRef defaultValue = llvm_default_value_of_type(context, decl->type); + LLVMValueRef variable = NULL; + + switch(decl->storageQualifier) { + case StorageQualifierLocal: + variable = LLVMBuildAlloca(builder, llvmTypeRef, decl->name); + LLVMBuildStore(builder, defaultValue, variable); + break; + case StorageQualifierStatic: + // add global + variable = LLVMAddGlobal(module, llvmTypeRef, decl->name); + LLVMSetInitializer(variable, defaultValue); + break; + case StorageQualifierGlobal: + PANIC("Global not implemented"); + break; + } + + return variable; +} diff --git a/src/llvm/types/type.c b/src/llvm/types/type.c index f39c053..5bcfd97 100644 --- a/src/llvm/types/type.c +++ b/src/llvm/types/type.c @@ -1,5 +1,8 @@ +#include "llvm/types/composite-types.h" #include "llvm/types/structs.h" +#include +#include #include #include #include @@ -85,3 +88,33 @@ LLVMTypeRef llvm_type_from_gemstone_type(LLVMContextRef context, GemstoneTypeRef return llvmTypeRef; } + +LLVMValueRef llvm_default_value_of_composite(LLVMContextRef context, CompositeRef composite) { + LLVMTypeRef type = llvm_type_from_composite(context, composite); + LLVMValueRef value; + + if (composite->prim == Int) { + value = LLVMConstInt(type, 0, 0); + } else if (composite->prim == Float) { + value = LLVMConstReal(type, 0.0); + } else { + PANIC("Invalid composite type: %d", composite->prim); + } + + return value; +} + +LLVMValueRef llvm_default_value_of_type(LLVMContextRef context, GemstoneTypeRef ref) { + LLVMValueRef value = NULL; + + switch (ref->kind) { + case TypeComposite: + value = llvm_default_value_of_composite(context, &ref->specs.composite); + break; + default: + PANIC("type not implemented"); + break; + } + + return value; +} diff --git a/src/llvm/types/type.h b/src/llvm/types/type.h index 135319a..3b55050 100644 --- a/src/llvm/types/type.h +++ b/src/llvm/types/type.h @@ -49,4 +49,6 @@ LLVMTypeRef llvm_type_from_gemstone_type(LLVMContextRef context, GemstoneTypeRef */ void delete_typedefref(GemstoneTypedefRef ref); +LLVMValueRef llvm_default_value_of_type(LLVMContextRef context, GemstoneTypeRef ref); + #endif // GEMSTONE_TYPE_H_