From 1c476cd5618ab1d9c4700c9b80ee750011e58176 Mon Sep 17 00:00:00 2001 From: servostar Date: Tue, 4 Jun 2024 14:58:40 +0200 Subject: [PATCH] fixed test not passing --- src/llvm/expr.c | 21 ++++++++++----------- src/llvm/stmt.c | 15 ++++++++++----- tests/llvm/CMakeLists.txt | 8 ++++---- tests/llvm/global_vars.c | 12 +++++++----- 4 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/llvm/expr.c b/src/llvm/expr.c index e95ffa9..beee0d3 100644 --- a/src/llvm/expr.c +++ b/src/llvm/expr.c @@ -6,8 +6,8 @@ #include #include -BackendError impl_bitwise_operation(LLVMBackendCompileUnit *unit, - LLVMLocalScope *scope, +BackendError impl_bitwise_operation([[maybe_unused]] LLVMBackendCompileUnit *unit, + [[maybe_unused]] LLVMLocalScope *scope, LLVMBuilderRef builder, Operation *operation, LLVMValueRef *llvm_result) { @@ -57,8 +57,8 @@ static LLVMValueRef convert_integral_to_boolean( return LLVMBuildICmp(builder, LLVMIntNE, zero, integral, "to boolean"); } -BackendError impl_logical_operation(LLVMBackendCompileUnit *unit, - LLVMLocalScope *scope, +BackendError impl_logical_operation([[maybe_unused]] LLVMBackendCompileUnit *unit, + [[maybe_unused]] LLVMLocalScope *scope, LLVMBuilderRef builder, Operation *operation, LLVMValueRef *llvm_result) { @@ -66,7 +66,7 @@ BackendError impl_logical_operation(LLVMBackendCompileUnit *unit, LLVMValueRef rhs = NULL; LLVMValueRef lhs = NULL; - if (operation->kind == LogicalNot) { + if (operation->impl.logical == LogicalNot) { // single operand rhs = convert_integral_to_boolean(builder, rhs); } else { @@ -75,7 +75,7 @@ BackendError impl_logical_operation(LLVMBackendCompileUnit *unit, rhs = convert_integral_to_boolean(builder, rhs); } - switch (operation->impl.bitwise) { + switch (operation->impl.logical) { case LogicalAnd: *llvm_result = LLVMBuildAnd(builder, lhs, rhs, "logical and"); break; @@ -108,8 +108,8 @@ static LLVMBool is_integral(LLVMValueRef value) { return typeKind == LLVMIntegerTypeKind; } -BackendError impl_relational_operation(LLVMBackendCompileUnit *unit, - LLVMLocalScope *scope, +BackendError impl_relational_operation([[maybe_unused]] LLVMBackendCompileUnit *unit, + [[maybe_unused]] LLVMLocalScope *scope, LLVMBuilderRef builder, Operation *operation, LLVMValueRef *llvm_result) { @@ -158,8 +158,8 @@ BackendError impl_relational_operation(LLVMBackendCompileUnit *unit, return SUCCESS; } -BackendError impl_arithmetic_operation(LLVMBackendCompileUnit *unit, - LLVMLocalScope *scope, +BackendError impl_arithmetic_operation([[maybe_unused]] LLVMBackendCompileUnit *unit, + [[maybe_unused]] LLVMLocalScope *scope, LLVMBuilderRef builder, Operation *operation, LLVMValueRef *llvm_result) { @@ -169,7 +169,6 @@ BackendError impl_arithmetic_operation(LLVMBackendCompileUnit *unit, if ((is_integral(lhs) && is_integral(rhs)) == 1) { // integral type - LLVMIntPredicate operator = 0; switch (operation->impl.arithmetic) { case Add: diff --git a/src/llvm/stmt.c b/src/llvm/stmt.c index c712ae5..0e1b470 100644 --- a/src/llvm/stmt.c +++ b/src/llvm/stmt.c @@ -9,7 +9,7 @@ #include #include -BackendError impl_assign_stmt(LLVMBackendCompileUnit *unit, +BackendError impl_assign_stmt([[maybe_unused]] LLVMBackendCompileUnit *unit, const LLVMBuilderRef builder, const LLVMLocalScope *scope, const Assignment *assignment) { BackendError err = SUCCESS; @@ -45,6 +45,7 @@ BackendError impl_basic_block(LLVMBackendCompileUnit *unit, LLVMPositionBuilderAtEnd(builder, *llvm_block); for (size_t i = 0; i < block->statemnts->len; i++) { + [[maybe_unused]] Statement *stmt = ((Statement *) block->statemnts->data) + i; // TODO: implement statement @@ -66,7 +67,7 @@ BackendError impl_while(LLVMBackendCompileUnit *unit, LLVMPositionBuilderAtEnd(builder, while_cond_block); // Resolve condition in block to a variable LLVMValueRef cond_result = NULL; - impl_expr(unit, scope, builder, &while_stmt->conditon, &cond_result); + impl_expr(unit, scope, builder, (Expression*) &while_stmt->conditon, &cond_result); // build body of loop LLVMBasicBlockRef while_body_block = NULL; @@ -103,6 +104,7 @@ BackendError impl_func_call(LLVMBackendCompileUnit *unit, break; } + [[maybe_unused]] Paramer* parameter = (Paramer*) call->function->parameter->data + i; // TODO: create a pointer to LLVMValueRef in case parameter is `out` @@ -123,7 +125,7 @@ BackendError impl_func_call(LLVMBackendCompileUnit *unit, BackendError impl_cond_block(LLVMBackendCompileUnit *unit, LLVMBuilderRef builder, LLVMLocalScope *scope, Expression *cond, - Block *block, LLVMBasicBlockRef *cond_block, LLVMBasicBlockRef *body_block, + const Block *block, LLVMBasicBlockRef *cond_block, LLVMBasicBlockRef *body_block, LLVMValueRef *llvm_cond) { BackendError err; @@ -155,7 +157,7 @@ BackendError impl_branch(LLVMBackendCompileUnit *unit, LLVMBasicBlockRef body_block = NULL; LLVMValueRef cond_value = NULL; - err = impl_cond_block(unit, builder, scope, &branch->ifBranch.conditon, &branch->ifBranch.block, &cond_block, + err = impl_cond_block(unit, builder, scope, (Expression*) &branch->ifBranch.conditon, &branch->ifBranch.block, &cond_block, &body_block, &cond_value); g_array_append_val(cond_blocks, cond_block); @@ -215,4 +217,7 @@ BackendError impl_branch(LLVMBackendCompileUnit *unit, return err; } -BackendError impl_stmt(LLVMBackendCompileUnit *unit, Statement *stmt) {} +BackendError impl_stmt([[maybe_unused]] LLVMBackendCompileUnit *unit, [[maybe_unused]] Statement *stmt) { + // TODO: implement + return SUCCESS; +} diff --git a/tests/llvm/CMakeLists.txt b/tests/llvm/CMakeLists.txt index bc72f78..ac84d6e 100644 --- a/tests/llvm/CMakeLists.txt +++ b/tests/llvm/CMakeLists.txt @@ -5,7 +5,9 @@ include_directories(PRIVATE ${GLIB_INCLUDE_DIRS}) find_package(PkgConfig REQUIRED) pkg_search_module(GLIB REQUIRED IMPORTED_TARGET glib-2.0) -link_libraries(PkgConfig::GLIB) +link_libraries(PkgConfig::GLIB) + +include_directories(${PROJECT_SOURCE_DIR}/dep/tomlc99) # ------------------------------------------------ # # LLVM backend # @@ -30,15 +32,13 @@ include_directories(${LLVM_INCLUDE_DIR}) file(GLOB_RECURSE SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/*.c) list(REMOVE_ITEM SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/main.c) -list(REMOVE_ITEM SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/yacc/parser.tab.c) -list(REMOVE_ITEM SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/lex/lexer.ll.c) -list(REMOVE_ITEM SOURCE_FILES ${PROJECT_SOURCE_DIR}/src/lex/util.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) +target_link_libraries(global_vars tomlc99) 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 index 52a104b..691a64a 100644 --- a/tests/llvm/global_vars.c +++ b/tests/llvm/global_vars.c @@ -5,9 +5,6 @@ #include #include -// NOTE: unused -AST_NODE_PTR root; - [[gnu::always_inline]] [[clang::always_inline]] inline Variable* create_variable_decl(const char* name, StorageQualifier qualifier, Type type) { @@ -85,7 +82,8 @@ inline Module* create_module() { return module; } -int main() { +int main(int argc, char* argv[]) { + parse_options(argc, argv); log_init(); // no need to clean up ;-) @@ -99,11 +97,15 @@ int main() { PANIC("%ld: at [%p] %s", err.kind, err.impl.ast_node, err.impl.message); } - err = generate_code(module, NULL); + TargetConfig* config = default_target_config(); + + err = generate_code(module, config); if (err.kind != Success) { PANIC("%ld: at [%p] %s", err.kind, err.impl.ast_node, err.impl.message); } + delete_target_config(config); + err = deinit_backend(); if (err.kind != Success) { PANIC("%ld: at [%p] %s", err.kind, err.impl.ast_node, err.impl.message);