diff --git a/tests/llvm/CMakeLists.txt b/tests/llvm/CMakeLists.txt index 5f7867d..fe495c7 100644 --- a/tests/llvm/CMakeLists.txt +++ b/tests/llvm/CMakeLists.txt @@ -3,3 +3,4 @@ include(CTest) # Provide test to run here or include another CMakeLists.txt add_subdirectory(typedef) +add_subdirectory(params) diff --git a/tests/llvm/params/CMakeLists.txt b/tests/llvm/params/CMakeLists.txt new file mode 100644 index 0000000..712438d --- /dev/null +++ b/tests/llvm/params/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 parameter declarations + +add_executable(params + ${SOURCE_FILES} + ${LEX_GENERATED_SOURCE_FILE} + ${YACC_GENERATED_SOURCE_FILE} + params.c) +set_target_properties(params + PROPERTIES + OUTPUT_NAME "params" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/llvm) +target_link_libraries(params PkgConfig::GLIB) +add_test(NAME params + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/tests/llvm/params + COMMAND ${GEMSTONE_BINARY_DIR}/tests/llvm/params test.txt) diff --git a/tests/llvm/params/params.c b/tests/llvm/params/params.c new file mode 100644 index 0000000..8aa96d3 --- /dev/null +++ b/tests/llvm/params/params.c @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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(); +} + +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(); + + TypeScopeRef scope = type_scope_new(); + + AST_NODE_PTR fun = AST_get_node(root, 0); + AST_NODE_PTR list = AST_get_node(fun, 1); + + for (size_t i = 0; i < list->child_count; i++) { + AST_NODE_PTR param_list = AST_get_node(list, i); + + for (size_t k = 0; k < param_list->child_count; k++) { + AST_NODE_PTR param = AST_get_node(param_list, i); + + GemstoneParam par = param_from_ast(scope, param); + } + } + + type_scope_delete(scope); + + AST_delete_node(root); + return 0; +} diff --git a/tests/llvm/params/test.txt b/tests/llvm/params/test.txt new file mode 100644 index 0000000..fa2dc57 --- /dev/null +++ b/tests/llvm/params/test.txt @@ -0,0 +1,4 @@ + +fun a(in int: a, in float: b) { + a = 0 +} \ No newline at end of file