added error handling

This commit is contained in:
Sven Vogel 2024-05-22 16:11:00 +02:00
parent 3b78d117b5
commit 00089a4939
5 changed files with 92 additions and 78 deletions

View File

@ -89,4 +89,6 @@ BackendError new_backend_error(BackendErrorKind kind);
BackendError new_backend_impl_error(BackendErrorKind kind, AST_NODE_PTR node, const char* message);
#define SUCCESS new_backend_error(Success)
#endif // CODEGN_BACKEND_H_

View File

@ -21,6 +21,8 @@ static BackendError llvm_backend_codegen(const AST_NODE_PTR module_node, void**)
LLVMContextRef context = LLVMContextCreate();
LLVMModuleRef module = LLVMModuleCreateWithNameInContext("gemstone application", context);
BackendError err;
TypeScopeRef global_scope = type_scope_new();
for (size_t i = 0; i < module_node->child_count; i++) {
@ -47,8 +49,13 @@ static BackendError llvm_backend_codegen(const AST_NODE_PTR module_node, void**)
for (size_t i = 0; i < decls->len; i++) {
GemstoneDeclRef decl = ((GemstoneDeclRef*) decls->data)[i];
type_scope_add_variable(global_scope, decl);
LLVMValueRef llvm_decl = NULL;
err = llvm_create_declaration(module, NULL, decl, &llvm_decl);
if (err.kind != Success)
break;
}
// TODO: create LLVMValueRef of global/static variables
break;
default:

View File

@ -1,12 +1,12 @@
#include "llvm/types/scope.h"
#include "llvm/types/structs.h"
#include <codegen/backend.h>
#include <llvm/types/scope.h>
#include <llvm/types/structs.h>
#include <llvm-c/Types.h>
#include <llvm/types/type.h>
#include <ast/ast.h>
#include <llvm/decl/variable.h>
#include <sys/log.h>
#include <stdlib.h>
#include <llvm-c/Core.h>
static StorageQualifier get_storage_qualifier_from_ast(AST_NODE_PTR storageQualifierNode) {
if (storageQualifierNode->kind != AST_Storage) {
@ -67,26 +67,28 @@ 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;
BackendError llvm_create_declaration(LLVMModuleRef llvm_module, LLVMBuilderRef llvm_builder, GemstoneDeclRef gem_decl, LLVMValueRef* llvm_decl) {
LLVMContextRef context = LLVMGetModuleContext(llvm_module);
LLVMTypeRef llvmTypeRef = llvm_type_from_gemstone_type(context, gem_decl->type);
LLVMValueRef defaultValue = llvm_default_value_of_type(context, gem_decl->type);
switch(decl->storageQualifier) {
switch(gem_decl->storageQualifier) {
case StorageQualifierLocal:
variable = LLVMBuildAlloca(builder, llvmTypeRef, decl->name);
LLVMBuildStore(builder, defaultValue, variable);
if (llvm_builder == NULL) {
return new_backend_impl_error(Implementation, NULL, "initializing a local variable on non-local scope");
}
*llvm_decl = LLVMBuildAlloca(llvm_builder, llvmTypeRef, gem_decl->name);
LLVMBuildStore(llvm_builder, defaultValue, *llvm_decl);
break;
case StorageQualifierStatic:
// add global
variable = LLVMAddGlobal(module, llvmTypeRef, decl->name);
LLVMSetInitializer(variable, defaultValue);
*llvm_decl = LLVMAddGlobal(llvm_module, llvmTypeRef, gem_decl->name);
LLVMSetInitializer(*llvm_decl, defaultValue);
break;
case StorageQualifierGlobal:
PANIC("Global not implemented");
break;
}
return variable;
return SUCCESS;
}

View File

@ -2,10 +2,15 @@
#ifndef LLVM_DECL_VAR_H_
#define LLVM_DECL_VAR_H_
#include <codegen/backend.h>
#include <ast/ast.h>
#include <llvm-c/Types.h>
#include <llvm/types/structs.h>
#include <llvm/types/scope.h>
#include <llvm-c/Core.h>
GArray* declaration_from_ast(TypeScopeRef scope, const AST_NODE_PTR node);
BackendError llvm_create_declaration(LLVMModuleRef llvm_module, LLVMBuilderRef llvm_builder, GemstoneDeclRef gem_decl, LLVMValueRef* llvm_decl);
#endif // LLVM_DECL_VAR_H_

View File

@ -1,8 +1,8 @@
#include <llvm/decl/variable.h>
#include <llvm/function/function-types.h>
#include <llvm/types/type.h>
#include <llvm/types/scope.h>
#include <llvm/types/type.h>
#include <string.h>
struct TypeScope_t {
@ -39,15 +39,12 @@ GemstoneTypedefRef type_scope_get_type(TypeScopeRef scope, size_t index) {
return ((GemstoneTypedefRef *)scope->types->data)[index];
}
size_t type_scope_types_len(TypeScopeRef scope) {
return scope->types->len;
}
size_t type_scope_types_len(TypeScopeRef scope) { return scope->types->len; }
size_t type_scope_scopes_len(TypeScopeRef scope) {
return scope->scopes->len;
}
size_t type_scope_scopes_len(TypeScopeRef scope) { return scope->scopes->len; }
GemstoneTypedefRef type_scope_get_type_from_name(TypeScopeRef scope, const char* name) {
GemstoneTypedefRef type_scope_get_type_from_name(TypeScopeRef scope,
const char *name) {
for (guint i = 0; i < scope->types->len; i++) {
GemstoneTypedefRef typeref = ((GemstoneTypedefRef *)scope->types->data)[i];
@ -98,7 +95,8 @@ void type_scope_add_fun(TypeScopeRef scope, GemstoneFunRef function) {
g_array_append_val(scope->funcs, function);
}
GemstoneFunRef type_scope_get_fun_from_name(TypeScopeRef scope, const char* name) {
GemstoneFunRef type_scope_get_fun_from_name(TypeScopeRef scope,
const char *name) {
for (guint i = 0; i < scope->funcs->len; i++) {
GemstoneFunRef funref = ((GemstoneFunRef *)scope->funcs->data)[i];