added examples

This commit is contained in:
Sven Vogel 2024-06-10 02:12:20 +02:00
parent aae9204863
commit 158a9797ca
10 changed files with 107 additions and 8 deletions

View File

@ -100,6 +100,12 @@ include_directories(${PROJECT_SOURCE_DIR}/dep/tomlc99)
link_libraries(tomlc99)
# ------------------------------------------------ #
# Standard library #
# ------------------------------------------------ #
add_subdirectory(lib)
# ------------------------------------------------ #
# LLD-C-Layer #
# ------------------------------------------------ #

18
examples/lib_common.c Normal file
View File

@ -0,0 +1,18 @@
//
// Created by servostar on 6/10/24.
//
#include <stdio.h>
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;
}

6
examples/lib_common.txt Normal file
View File

@ -0,0 +1,6 @@
fun swap(in out float: a, in out float: b) {
float: c = a
a = b
b = c
}

18
examples/lib_fibonacci.c Normal file
View File

@ -0,0 +1,18 @@
//
// Created by servostar on 6/10/24.
//
#include <stdio.h>
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;
}

View File

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

View File

@ -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})
add_library(os ${STDLIB_OS_SOURCE_FILES})

View File

@ -0,0 +1,10 @@
//
// Created by servostar on 6/10/24.
//
extern void entry(void);
int main(int argc, char* argv[]) {
entry();
return 0;
}

View File

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

View File

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

View File

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