diff --git a/CMakeLists.txt b/CMakeLists.txt index 68808f8..896b8bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -100,6 +100,12 @@ include_directories(${PROJECT_SOURCE_DIR}/dep/tomlc99) link_libraries(tomlc99) +# ------------------------------------------------ # +# Standard library # +# ------------------------------------------------ # + +add_subdirectory(lib) + # ------------------------------------------------ # # LLD-C-Layer # # ------------------------------------------------ # diff --git a/examples/lib_common.c b/examples/lib_common.c new file mode 100644 index 0000000..29357f4 --- /dev/null +++ b/examples/lib_common.c @@ -0,0 +1,18 @@ +// +// Created by servostar on 6/10/24. +// + +#include + +extern void swap(float*, float*); + +int main(int argc, char* argv[]) { + float a = 2.0; + float b = 7.0; + + swap(&a, &b); + + printf("%f, %f\n", a, b); + + return 0; +} diff --git a/examples/lib_common.txt b/examples/lib_common.txt new file mode 100644 index 0000000..936ea72 --- /dev/null +++ b/examples/lib_common.txt @@ -0,0 +1,6 @@ + +fun swap(in out float: a, in out float: b) { + float: c = a + a = b + b = c +} diff --git a/examples/lib_fibonacci.c b/examples/lib_fibonacci.c new file mode 100644 index 0000000..6a422a7 --- /dev/null +++ b/examples/lib_fibonacci.c @@ -0,0 +1,18 @@ +// +// Created by servostar on 6/10/24. +// + +#include + +extern void fib(int*, int); + +int main(int argc, char* argv[]) { + + for (int i = 0; i < 7; i++) { + int r = 0; + fib(&r, i); + printf("%d\n", r); + } + + return 0; +} diff --git a/examples/lib_fibonacci.txt b/examples/lib_fibonacci.txt new file mode 100644 index 0000000..d5a02ff --- /dev/null +++ b/examples/lib_fibonacci.txt @@ -0,0 +1,17 @@ + +fun fib(in int: x, out int: c) { + + int: i = 0 + int: k = 1 + int: p = 1 + + while x < i { + int: t = k + k = k + p + p = t + + i = i + 1 + } + + c = k +} diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 6bc0d38..8be6998 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -22,4 +22,4 @@ file(GLOB_RECURSE STDLIB_MEM_SOURCE_FILES src/mem/*.c) add_library(mem ${STDLIB_MEM_SOURCE_FILES}) file(GLOB_RECURSE STDLIB_OS_SOURCE_FILES src/os/*.c) -add_library(os ${STDLIB_OS_SOURCE_FILES}) \ No newline at end of file +add_library(os ${STDLIB_OS_SOURCE_FILES}) diff --git a/lib/src/entry/entrypoint.c b/lib/src/entry/entrypoint.c new file mode 100644 index 0000000..c41e482 --- /dev/null +++ b/lib/src/entry/entrypoint.c @@ -0,0 +1,10 @@ +// +// Created by servostar on 6/10/24. +// + +extern void entry(void); + +int main(int argc, char* argv[]) { + entry(); + return 0; +} diff --git a/src/llvm/link/lld.c b/src/llvm/link/lld.c index a14c9bf..44cd3dc 100644 --- a/src/llvm/link/lld.c +++ b/src/llvm/link/lld.c @@ -61,8 +61,8 @@ TargetLinkConfig* lld_create_link_config(const Target* target, const TargetConfi { // output file after linking - gchar* basename = g_strjoin(".", target_config->name, "out", NULL); - gchar* filename = g_build_filename(target_config->output_directory, basename, NULL); + basename = g_strjoin(".", target_config->name, "out", NULL); + filename = g_build_filename(target_config->output_directory, basename, NULL); config->output_file = filename; } @@ -70,6 +70,12 @@ TargetLinkConfig* lld_create_link_config(const Target* target, const TargetConfi g_array_append_val(config->object_file_names, target_object); INFO("resolved path of target object: %s", target_object); + // if it is an app, add entrypoint library + if (target_config->mode == Application) { + char* entrypoint = g_strdup("libentrypoint.a"); + g_array_append_val(module->imports, entrypoint); + } + // resolve absolute paths to dependent library object files DEBUG("resolving target dependencies..."); for (guint i = 0; i < module->imports->len; i++) { diff --git a/src/llvm/llvm-ir/expr.c b/src/llvm/llvm-ir/expr.c index 4712d4f..ebdd8d5 100644 --- a/src/llvm/llvm-ir/expr.c +++ b/src/llvm/llvm-ir/expr.c @@ -75,7 +75,7 @@ BackendError impl_logical_operation(LLVMBackendCompileUnit *unit, LLVMValueRef llvm_rhs = NULL; LLVMValueRef llvm_lhs = NULL; - if (operation->impl.arithmetic == LogicalNot) { + if (operation->impl.logical == LogicalNot) { // single operand rhs = g_array_index(operation->operands, Expression*, 0); impl_expr(unit, scope, builder, rhs, &llvm_rhs); @@ -354,7 +354,23 @@ BackendError impl_variable_load(LLVMBackendCompileUnit *unit, LLVMLocalScope *sc return new_backend_impl_error(Implementation, NULL, "Variable not found"); } - *llvm_result = llvm_variable; + if (LLVMGetTypeKind(LLVMTypeOf(llvm_variable)) == LLVMPointerTypeKind) { + + Type* type; + LLVMTypeRef llvm_type; + + if (variable->kind == VariableKindDefinition) { + type = variable->impl.definiton.declaration.type; + } else { + type = variable->impl.declaration.type; + } + + get_type_impl(unit, scope->func_scope->global_scope, type, &llvm_type); + + *llvm_result = LLVMBuildLoad2(builder, llvm_type, llvm_variable, ""); + } else { + *llvm_result = llvm_variable; + } return SUCCESS; } diff --git a/src/llvm/parser.c b/src/llvm/parser.c index c1f9f23..ff2201a 100644 --- a/src/llvm/parser.c +++ b/src/llvm/parser.c @@ -244,11 +244,13 @@ BackendError parse_module(const Module* module, const TargetConfig* config) { err = export_module(unit, &target, config); if (err.kind == Success) { - TargetLinkConfig* link_config = lld_create_link_config(&target, config, module); + if (config->mode == Application) { + TargetLinkConfig* link_config = lld_create_link_config(&target, config, module); - lld_link_target(link_config); + lld_link_target(link_config); - lld_delete_link_config(link_config); + lld_delete_link_config(link_config); + } } delete_target(target);