added test for llvm vars

This commit is contained in:
Sven Vogel 2024-05-27 16:47:32 +02:00
parent e01d4c4808
commit 0fcb0d7af8
6 changed files with 91 additions and 2 deletions

1
.gitignore vendored
View File

@ -18,3 +18,4 @@ build
CTestTestfile.cmake
DartConfiguration.tcl
*.cmake
*.ll

View File

@ -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);

View File

@ -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) {

View File

@ -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)
add_subdirectory(glib)
add_subdirectory(llvm)

36
tests/llvm/CMakeLists.txt Normal file
View File

@ -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)

44
tests/llvm/global_vars.c Normal file
View File

@ -0,0 +1,44 @@
#include <codegen/backend.h>
#include <llvm/backend.h>
#include <sys/log.h>
#include <set/types.h>
// 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);
}
}