backend parses type and function declarations

This commit is contained in:
Sven Vogel 2024-05-21 13:08:41 +02:00
parent 4a3b974d9f
commit fa32df1010
4 changed files with 71 additions and 2 deletions

View File

@ -1,4 +1,9 @@
#include <llvm/function/function-types.h>
#include <llvm/function/function.h>
#include <llvm/types/scope.h>
#include <llvm/types/structs.h>
#include <llvm/types/type.h>
#include <codegen/backend.h>
#include <sys/log.h>
#include <ast/ast.h>
@ -10,12 +15,37 @@ typedef enum LLVMBackendError_t {
UnresolvedImport
} LLVMBackendError;
static BackendError llvm_backend_codegen(const AST_NODE_PTR, void**) {
static BackendError llvm_backend_codegen(const AST_NODE_PTR module_node, void**) {
// we start with a LLVM module
LLVMContextRef context = LLVMContextCreate();
LLVMModuleRef module = LLVMModuleCreateWithNameInContext("gemstone application", context);
TypeScopeRef global_scope = type_scope_new();
for (size_t i = 0; i < module_node->child_count; i++) {
// iterate over all nodes in module
// can either be a function, box, definition, declaration or typedefine
AST_NODE_PTR global_node = AST_get_node(module_node, i);
GemstoneTypedefRef typedefref;
GemstoneFunRef funref;
switch (global_node->kind) {
case AST_Typedef:
typedefref = get_type_def_from_ast(global_scope, global_node);
type_scope_append_type(global_scope, typedefref);
break;
case AST_Fun:
funref = fun_from_ast(global_scope, global_node);
type_scope_add_fun(global_scope, funref);
break;
default:
PANIC("NOT IMPLEMENTED");
}
}
type_scope_delete(global_scope);
LLVMDisposeModule(module);
LLVMContextDispose(context);

View File

@ -1,5 +1,7 @@
#include "llvm/function/function-types.h"
#include <ast/ast.h>
#include <llvm-c/Core.h>
#include <llvm/types/scope.h>
#include <llvm/function/function.h>
#include <llvm/types/type.h>
@ -105,3 +107,16 @@ void fun_delete(const GemstoneFunRef fun) {
g_array_free(fun->params, TRUE);
free(fun);
}
LLVMTypeRef get_gemstone_function_llvm_signature(LLVMContextRef context, GemstoneFunRef function) {
unsigned int param_count = function->params->len;
LLVMTypeRef* params = malloc(sizeof(LLVMTypeRef));
for (size_t i = 0; i < param_count; i++) {
GemstoneParam* gem_param = ((GemstoneParam*) function->params->data) + i;
params[i] = llvm_type_from_gemstone_type(context, gem_param->typename);
}
return LLVMFunctionType(LLVMVoidType(), params, param_count, 0);
}

View File

@ -71,3 +71,17 @@ GemstoneTypedefRef get_type_def_from_ast(const TypeScopeRef scope, const AST_NOD
return new_typedefref(type, name);
}
LLVMTypeRef llvm_type_from_gemstone_type(LLVMContextRef context, GemstoneTypeRef type) {
LLVMTypeRef llvmTypeRef = NULL;
switch (type->kind) {
case TypeComposite:
llvmTypeRef = llvm_type_from_composite(context, &type->specs.composite);
break;
default:
PANIC("NOT IMPLEMENTED");
}
return llvmTypeRef;
}

View File

@ -3,6 +3,7 @@
#define GEMSTONE_TYPE_H_
#include <ast/ast.h>
#include <llvm-c/Types.h>
#include <llvm/types/composite.h>
#include <llvm/types/structs.h>
#include <llvm/types/scope.h>
@ -32,6 +33,15 @@ GemstoneTypedefRef get_type_def_from_ast(const TypeScopeRef scope, const AST_NOD
*/
GemstoneTypedefRef new_typedefref(GemstoneTypeRef type, const char* name);
/**
* @brief Create the LLVM function signature
*
* @param context
* @param type
* @return LLVMTypeRef
*/
LLVMTypeRef llvm_type_from_gemstone_type(LLVMContextRef context, GemstoneTypeRef type);
/**
* @brief Free the type definition reference and its underlying type
*