diff --git a/src/llvm/backend.c b/src/llvm/backend.c index 6c31665..eccdd14 100644 --- a/src/llvm/backend.c +++ b/src/llvm/backend.c @@ -1,4 +1,9 @@ +#include +#include +#include +#include +#include #include #include #include @@ -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); diff --git a/src/llvm/function/function.c b/src/llvm/function/function.c index 40323ba..4e8c643 100644 --- a/src/llvm/function/function.c +++ b/src/llvm/function/function.c @@ -1,5 +1,7 @@ +#include "llvm/function/function-types.h" #include +#include #include #include #include @@ -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); +} diff --git a/src/llvm/types/type.c b/src/llvm/types/type.c index 47f1ef9..f39c053 100644 --- a/src/llvm/types/type.c +++ b/src/llvm/types/type.c @@ -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; +} diff --git a/src/llvm/types/type.h b/src/llvm/types/type.h index c6a2b1b..135319a 100644 --- a/src/llvm/types/type.h +++ b/src/llvm/types/type.h @@ -3,6 +3,7 @@ #define GEMSTONE_TYPE_H_ #include +#include #include #include #include @@ -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 *