From cc1dc790e1390dc98e3f2720b068b60988327e16 Mon Sep 17 00:00:00 2001 From: servostar Date: Tue, 21 May 2024 00:41:03 +0200 Subject: [PATCH] added test for typedef --- src/llvm/types/composite.c | 5 +- tests/CMakeLists.txt | 3 +- tests/llvm/CMakeLists.txt | 5 ++ tests/llvm/typedef/CMakeLists.txt | 51 +++++++++++++ tests/llvm/typedef/test.txt | 6 ++ tests/llvm/typedef/typedef.c | 114 ++++++++++++++++++++++++++++++ 6 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 tests/llvm/CMakeLists.txt create mode 100644 tests/llvm/typedef/CMakeLists.txt create mode 100644 tests/llvm/typedef/test.txt create mode 100644 tests/llvm/typedef/typedef.c diff --git a/src/llvm/types/composite.c b/src/llvm/types/composite.c index f58d2c3..c1cbfed 100644 --- a/src/llvm/types/composite.c +++ b/src/llvm/types/composite.c @@ -68,7 +68,7 @@ enum Scale_t collapse_scale_list(const AST_NODE_PTR list, double base) { enum Sign_t string_to_sign(const char* keyword) { if (strcmp(keyword, "signed") == 0) { return Signed; - } else if (strcmp(keyword, "signed") == 0) { + } else if (strcmp(keyword, "unsigned") == 0) { return Unsigned; } @@ -139,7 +139,7 @@ struct CompositeType_t ast_type_to_composite(const TypeScopeRef scope, AST_NODE_ } } else if (count == 3) { - const char* typename = AST_get_node(type, 3)->value; + const char* typename = AST_get_node(type, 2)->value; GemstoneTypedefRef known_type = type_scope_get_type_from_name(scope, typename); if (known_type == NULL) { @@ -156,7 +156,6 @@ struct CompositeType_t ast_type_to_composite(const TypeScopeRef scope, AST_NODE_ // sign, scale and type composite.sign = string_to_sign(AST_get_node(type, 0)->value); composite.scale = collapse_scale_list(AST_get_node(type, 1), (double) composite.scale); - composite.prim = resolve_primitive(AST_get_node(type, 2)->value); } return composite; 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..5f7867d --- /dev/null +++ b/tests/llvm/CMakeLists.txt @@ -0,0 +1,5 @@ +include(CTest) + +# Provide test to run here or include another CMakeLists.txt + +add_subdirectory(typedef) diff --git a/tests/llvm/typedef/CMakeLists.txt b/tests/llvm/typedef/CMakeLists.txt new file mode 100644 index 0000000..84646ce --- /dev/null +++ b/tests/llvm/typedef/CMakeLists.txt @@ -0,0 +1,51 @@ +include(CTest) + +# ------------------------------------------------ # +# Setup Glib 2.0 # +# ------------------------------------------------ # + +find_package(PkgConfig REQUIRED) +pkg_search_module(GLIB REQUIRED IMPORTED_TARGET glib-2.0) + +# ------------------------------------------------ # +# 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}) + +# ------------------------------------------------ # +# Source # +# ------------------------------------------------ # + +include_directories(${PROJECT_SOURCE_DIR}/src) +include_directories(PRIVATE ${GLIB_INCLUDE_DIRS}) + +file(GLOB_RECURSE SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/*.c) +list(REMOVE_ITEM SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/main.c) + +set(LEX_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/lex/lexer.ll.c) +set(YACC_GENERATED_SOURCE_FILE ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c) + +# ------------------------------------------------------- # +# CTEST 1 +# test typedef + +add_executable(typedef + ${SOURCE_FILES} + ${LEX_GENERATED_SOURCE_FILE} + ${YACC_GENERATED_SOURCE_FILE} + typedef.c) +set_target_properties(typedef + PROPERTIES + OUTPUT_NAME "typedef" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/llvm) +target_link_libraries(typedef PkgConfig::GLIB) +add_test(NAME typedef + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests/llvm/typedef + COMMAND ${GEMSTONE_BINARY_DIR}/tests/llvm/typedef test.txt) diff --git a/tests/llvm/typedef/test.txt b/tests/llvm/typedef/test.txt new file mode 100644 index 0000000..dc36998 --- /dev/null +++ b/tests/llvm/typedef/test.txt @@ -0,0 +1,6 @@ + +type unsigned half half int: u8 +type unsigned half int: u16 +type unsigned int: u32 +type unsigned double int: u64 +type unsigned double double int: u128 diff --git a/tests/llvm/typedef/typedef.c b/tests/llvm/typedef/typedef.c new file mode 100644 index 0000000..44d8b62 --- /dev/null +++ b/tests/llvm/typedef/typedef.c @@ -0,0 +1,114 @@ +#include "llvm/types/scope.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOG_LEVEL LOG_LEVEL_DEBUG + +extern FILE *yyin; +AST_NODE_PTR root; + +/** + * @brief Log a debug message to inform about beginning exit procedures + * + */ +void notify_exit(void) { DEBUG("Exiting gemstone..."); } + +/** + * @brief Closes File after compiling. + * + */ + +void close_file(void) { + if (NULL != yyin) { + fclose(yyin); + } +} + +/** + * @brief Run compiler setup here + * + */ +void setup(void) { + // setup preample + + log_init(); + DEBUG("starting gemstone..."); + +#if LOG_LEVEL <= LOG_LEVEL_DEBUG + atexit(¬ify_exit); +#endif + + // actual setup + AST_init(); + + col_init(); + + lex_init(); + + DEBUG("finished starting up gemstone..."); +} + +void run_backend_codegen() { + llvm_backend_init(); + + BackendError err; + err = init_backend(); + if (err.kind != Success) { + return; + } + + void* code = NULL; + err = generate_code(root, &code); + if (err.kind != Success) { + return; + } + + err = deinit_backend(); +} + +void check_typedef(const AST_NODE_PTR node) { + TypeScopeRef scope = type_scope_new(); + + GemstoneTypedefRef typdef = get_type_def_from_ast(scope, node); + + type_scope_delete(scope); +} + +int main(int argc, char *argv[]) { + + setup(); + atexit(close_file); + + // Check for file input as argument + if (2 != argc) { + INFO("Usage: %s \n", argv[0]); + PANIC("No File could be found"); + } + + // filename as first argument + char *filename = argv[1]; + + FILE *file = fopen(filename, "r"); + + if (NULL == file) { + PANIC("File couldn't be opened!"); + } + yyin = file; + + root = AST_new_node(AST_Module, NULL); + yyparse(); + + for (size_t i = 0; i < root->child_count; i++) { + check_typedef(root->children[i]); + } + + AST_delete_node(root); + return 0; +}