From 72cc87f5b6f0964cb0caa518c02eea52cf929050 Mon Sep 17 00:00:00 2001 From: Ugric Date: Mon, 7 Jul 2025 04:03:11 +0100 Subject: [PATCH] create executable function which identifies and loads a cache if available --- CMakeLists.txt | 8 +++++-- src/hashmap/hashmap.c | 2 ++ src/main.c | 54 ++++++++++++++++++++++++------------------- src/runtime/runtime.c | 9 ++++++-- src/runtime/runtime.h | 4 +++- testing.ar | 1 - 6 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 testing.ar diff --git a/CMakeLists.txt b/CMakeLists.txt index 1853bae..ed72893 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,8 @@ add_custom_target(GenerateLexer DEPENDS ${LEXER_C} ${LEXER_H}) # Step 3: Add executable add_executable(argon external/xxhash/xxhash.c external/cwalk/src/cwalk.c ${CFILES} ${LEXER_C}) +set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) +target_include_directories(argon PRIVATE ${CMAKE_SOURCE_DIR}/external/cwalk/include) # Step 4: Build order add_dependencies(argon GenerateLexer) @@ -44,7 +46,7 @@ set_target_properties(argon PROPERTIES find_package(BDWgc REQUIRED) find_package(gmp REQUIRED) -target_compile_options(argon PRIVATE -O3 -Wall -Wextra -Wno-unused-function -Iexternal/cwalk/include -s) +target_compile_options(argon PRIVATE -O3 -Wall -Wextra -Wno-unused-function -s) target_link_options(argon PRIVATE -static) target_link_libraries(argon PRIVATE @@ -55,4 +57,6 @@ target_link_libraries(argon PRIVATE target_include_directories(argon PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/lexer -) \ No newline at end of file +) + +add_custom_command(TARGET argon POST_BUILD COMMAND ${CMAKE_STRIP} $) \ No newline at end of file diff --git a/src/hashmap/hashmap.c b/src/hashmap/hashmap.c index ebb9081..9e9ad88 100644 --- a/src/hashmap/hashmap.c +++ b/src/hashmap/hashmap.c @@ -55,7 +55,9 @@ void resize_hashmap(struct hashmap *t) { while (temp) { hashmap_insert(t, temp->hash, temp->key, temp->val, temp->order); // Will increment count + struct node *temp_temp = temp; temp = temp->next; + free(temp_temp); } } free(old_list); diff --git a/src/main.c b/src/main.c index cb0dbe5..ba21e66 100644 --- a/src/main.c +++ b/src/main.c @@ -160,34 +160,24 @@ FAILED: return 1; } -int main(int argc, char *argv[]) { - generate_siphash_key(siphash_key); +ArgonObject *execute(char*absolute_path) { clock_t start, end; double time_spent, total_time_spent = 0; - setlocale(LC_ALL, ""); - if (argc <= 1) - return -1; - ar_memory_init(); - char *CWD = get_current_directory(); - char *path_non_absolute = argv[1]; - char path[FILENAME_MAX]; - cwk_path_get_absolute(CWD, path_non_absolute, path, - sizeof(path)); const char *basename_ptr; size_t basename_length; - cwk_path_get_basename(path, &basename_ptr, &basename_length); + cwk_path_get_basename(absolute_path, &basename_ptr, &basename_length); - if (!basename_ptr) return -1; + if (!basename_ptr) return NULL; char basename[FILENAME_MAX]; memcpy(basename, basename_ptr, basename_length); size_t parent_directory_length; - cwk_path_get_dirname(path, &parent_directory_length); + cwk_path_get_dirname(absolute_path, &parent_directory_length); char parent_directory[FILENAME_MAX]; - memcpy(parent_directory, path, parent_directory_length); + memcpy(parent_directory, absolute_path, parent_directory_length); parent_directory[parent_directory_length] = '\0'; char cache_folder_path[FILENAME_MAX]; @@ -197,9 +187,9 @@ int main(int argc, char *argv[]) { cwk_path_join(cache_folder_path, basename, cache_file_path, sizeof(cache_file_path)); cwk_path_change_extension(cache_file_path, BYTECODE_EXTENTION, cache_file_path, sizeof(cache_file_path)); - FILE *file = fopen(path, "r"); + FILE *file = fopen(absolute_path, "r"); if (!file) { - return -1; + return NULL; } XXH3_state_t *hash_state = XXH3_createState(); @@ -223,7 +213,7 @@ int main(int argc, char *argv[]) { DArray tokens; darray_init(&tokens, sizeof(Token)); - LexerState state = {path, file, 0, 0, &tokens}; + LexerState state = {absolute_path, file, 0, 0, &tokens}; start = clock(); lexer(state); end = clock(); @@ -237,7 +227,7 @@ int main(int argc, char *argv[]) { darray_init(&ast, sizeof(ParsedValue)); start = clock(); - parser(path, &ast, &tokens, false); + parser(absolute_path, &ast, &tokens, false); end = clock(); time_spent = (double)(end - start) / CLOCKS_PER_SEC; total_time_spent += time_spent; @@ -277,10 +267,9 @@ int main(int argc, char *argv[]) { fclose(file); } - init_types(); - start = clock(); - runtime(translated); + RuntimeState state = init_runtime_state(translated); + ArgonObject *resp = runtime(translated,state); end = clock(); time_spent = (double)(end - start) / CLOCKS_PER_SEC; @@ -289,6 +278,23 @@ int main(int argc, char *argv[]) { printf("total time taken: %f seconds\n", total_time_spent); free_translator(&translated); - free(CWD); - return 0; + return resp; } + +int main(int argc, char *argv[]) { + setlocale(LC_ALL, ""); + ar_memory_init(); + generate_siphash_key(siphash_key); + init_types(); + char *CWD = get_current_directory(); + if (argc <= 1) + return -1; + char *path_non_absolute = argv[1]; + char path[FILENAME_MAX]; + cwk_path_get_absolute(CWD, path_non_absolute, path, + sizeof(path)); + free(CWD); + ArgonObject *resp = execute(path); + if (resp) return 0; + return -1; +} \ No newline at end of file diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 946d015..b6dbec2 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -79,13 +79,18 @@ void run_instruction(Translated *translated, RuntimeState *state, } } -void runtime(Translated translated) { +RuntimeState init_runtime_state(Translated translated) { RuntimeState state = { checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0}; - struct Stack stack = {}; + return state; +} + +ArgonObject *runtime(Translated translated, RuntimeState state) { + struct Stack stack = {NULL,NULL}; state.head = 0; while (state.head < translated.bytecode.size) { run_instruction(&translated, &state, stack); } free(state.registers); + return stack.scope; } \ No newline at end of file diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index cccd85e..87af9c4 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -19,6 +19,8 @@ uint64_t pop_bytecode(Translated *translated, RuntimeState *state); void run_instruction(Translated *translated, RuntimeState *state, struct Stack stack); -void runtime(Translated translated); +RuntimeState init_runtime_state(Translated translated); + +ArgonObject *runtime(Translated translated, RuntimeState state); #endif // RUNTIME_H \ No newline at end of file diff --git a/testing.ar b/testing.ar deleted file mode 100644 index ec747fa..0000000 --- a/testing.ar +++ /dev/null @@ -1 +0,0 @@ -null \ No newline at end of file