From 0fcb0d7af86e105b6504af64a92d78a14aebbe87 Mon Sep 17 00:00:00 2001 From: servostar Date: Mon, 27 May 2024 16:47:32 +0200 Subject: [PATCH] added test for llvm vars --- .gitignore | 1 + src/llvm/backend.c | 3 +++ src/llvm/parser.c | 6 +++++- tests/CMakeLists.txt | 3 ++- tests/llvm/CMakeLists.txt | 36 ++++++++++++++++++++++++++++++++ tests/llvm/global_vars.c | 44 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/llvm/CMakeLists.txt create mode 100644 tests/llvm/global_vars.c diff --git a/.gitignore b/.gitignore index 10613c6..8c4be57 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ build CTestTestfile.cmake DartConfiguration.tcl *.cmake +*.ll diff --git a/src/llvm/backend.c b/src/llvm/backend.c index dc24897..04c23f0 100644 --- a/src/llvm/backend.c +++ b/src/llvm/backend.c @@ -12,6 +12,9 @@ Target create_native_target() { DEBUG("creating native target..."); Target target; + target.name.str = "tmp"; + target.name.allocation = NONE; + target.triple.str = LLVMGetDefaultTargetTriple(); target.triple.allocation = LLVM; assert(target.triple.str != NULL); diff --git a/src/llvm/parser.c b/src/llvm/parser.c index e7d8287..4bda673 100644 --- a/src/llvm/parser.c +++ b/src/llvm/parser.c @@ -25,7 +25,7 @@ BackendError export_IR(LLVMBackendCompileUnit* unit, const Target* target) { char* ir = LLVMPrintModuleToString(unit->module); // construct file name - char* filename = alloca(strlen(target->name.str) + 2); + char* filename = alloca(strlen(target->name.str) + 4); sprintf(filename, "%s.ll", target->name.str); INFO("Writing LLVM-IR to %s", filename); @@ -64,6 +64,10 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target) { LLVMTargetRef llvm_target = NULL; char* error = NULL; + LLVMInitializeAllTargets(); + LLVMInitializeAllTargetInfos(); + LLVMInitializeAllTargetMCs(); + DEBUG("creating target..."); if (LLVMGetTargetFromTriple(target->triple.str, &llvm_target, &error) != 0) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1228d2b..57c4c80 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,4 +9,5 @@ set(CTEST_BINARY_DIRECTORY ${PROJECT_BINARY_DIR}/tests) add_subdirectory(logging) add_subdirectory(input_file) add_subdirectory(ast) -add_subdirectory(glib) \ No newline at end of file +add_subdirectory(glib) +add_subdirectory(llvm) \ No newline at end of file diff --git a/tests/llvm/CMakeLists.txt b/tests/llvm/CMakeLists.txt new file mode 100644 index 0000000..e4dcdc9 --- /dev/null +++ b/tests/llvm/CMakeLists.txt @@ -0,0 +1,36 @@ +include(CTest) + +include_directories(${PROJECT_SOURCE_DIR}/src) +include_directories(PRIVATE ${GLIB_INCLUDE_DIRS}) + +find_package(PkgConfig REQUIRED) +pkg_search_module(GLIB REQUIRED IMPORTED_TARGET glib-2.0) +link_libraries(PkgConfig::GLIB) + +# ------------------------------------------------ # +# LLVM backend # +# ------------------------------------------------ # + +# Fetch LLVM link configuration +execute_process(COMMAND llvm-config --libs all + OUTPUT_VARIABLE LLVM_LIBS) +# Strip whitespace from output +string(STRIP "${LLVM_LIBS}" LLVM_LIBS) +# Link all targets to LLVM +link_libraries(${LLVM_LIBS}) + +# ------------------------------------------------------- # +# CTEST 1 +# test llvm backend codegen for global variables + +file(GLOB_RECURSE SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/*.c) +list(REMOVE_ITEM SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/main.c) +add_executable(global_vars +global_vars.c ${SOURCE_FILES}) +set_target_properties(global_vars + PROPERTIES + OUTPUT_NAME "global_vars" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/llvm) +add_test(NAME global_vars + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests/llvm + COMMAND ${GEMSTONE_BINARY_DIR}/tests/llvm/global_vars) diff --git a/tests/llvm/global_vars.c b/tests/llvm/global_vars.c new file mode 100644 index 0000000..aca3d42 --- /dev/null +++ b/tests/llvm/global_vars.c @@ -0,0 +1,44 @@ + +#include +#include +#include +#include + +// NOTE: unused +AST_NODE_PTR root; + +Module* create_module() { + Module* module = malloc(sizeof(Module)); + + module->boxes = g_hash_table_new(g_str_hash, g_str_equal); + module->functions = g_hash_table_new(g_str_hash, g_str_equal); + module->imports = g_array_new(FALSE, FALSE, sizeof(Type)); + module->variables = g_hash_table_new(g_str_hash, g_str_equal); + module->types = g_hash_table_new(g_str_hash, g_str_equal); + + return module; +} + +int main() { + log_init(); + + Module* module = create_module(); + + llvm_backend_init(); + + BackendError err; + err = init_backend(); + if (err.kind != Success) { + PANIC("%ld: at [%p] %s", err.kind, err.impl.ast_node, err.impl.message); + } + + err = generate_code(module, NULL); + if (err.kind != Success) { + PANIC("%ld: at [%p] %s", err.kind, err.impl.ast_node, err.impl.message); + } + + err = deinit_backend(); + if (err.kind != Success) { + PANIC("%ld: at [%p] %s", err.kind, err.impl.ast_node, err.impl.message); + } +}