From 8d748038c0d16eb106837d591fcf10b79cd60d35 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 10 Oct 2024 20:29:03 +0200 Subject: [PATCH] fix: removed String wrapper in favor of cached memory --- src/llvm/backend.c | 62 ++++++++++++++++------------------------------ src/llvm/backend.h | 13 +++------- src/llvm/parser.c | 12 ++++----- 3 files changed, 31 insertions(+), 56 deletions(-) diff --git a/src/llvm/backend.c b/src/llvm/backend.c index cb2ef3e..0e323fc 100644 --- a/src/llvm/backend.c +++ b/src/llvm/backend.c @@ -13,20 +13,18 @@ Target create_native_target() { DEBUG("creating native target..."); Target target; - target.name.str = "tmp"; - target.name.allocation = NONE; + char* triple = LLVMGetDefaultTargetTriple(); + char* cpu = LLVMGetHostCPUName(); + char* features = LLVMGetHostCPUFeatures(); - target.triple.str = LLVMGetDefaultTargetTriple(); - target.triple.allocation = LLVM; - assert(target.triple.str != NULL); + target.name = "tmp"; + target.triple = mem_strdup(MemoryNamespaceLld, triple); + target.cpu = mem_strdup(MemoryNamespaceLld, cpu); + target.features = mem_strdup(MemoryNamespaceLld, features); - target.cpu.str = LLVMGetHostCPUName(); - target.cpu.allocation = LLVM; - assert(target.cpu.str != NULL); - - target.features.str = LLVMGetHostCPUFeatures(); - target.features.allocation = LLVM; - assert(target.features.str != NULL); + LLVMDisposeMessage(triple); + LLVMDisposeMessage(cpu); + LLVMDisposeMessage(features); target.opt = LLVMCodeGenLevelNone; target.reloc = LLVMRelocDefault; @@ -66,16 +64,13 @@ Target create_target_from_triple(char* triple) { Target target; - target.triple.str = mem_strdup(MemoryNamespaceLld, triple); - target.triple.allocation = NONE; + target.triple = mem_strdup(MemoryNamespaceLld, triple); // select default - target.cpu.str = ""; - target.cpu.allocation = NONE; + target.cpu = ""; // select default - target.features.str = ""; - target.features.allocation = NONE; + target.features = ""; return target; } @@ -89,39 +84,24 @@ Target create_target_from_config(TargetConfig* config) { target = create_target_from_triple(config->triple); } else { - config->triple = target.triple.str; + config->triple = target.triple; } - target.name.str = create_target_output_name(config); - target.name.allocation = NONE; // freed later by compiler + target.name = create_target_output_name(config); target.opt = llvm_opt_from_int(config->optimization_level); - INFO("Configured target: %s/%d: (%s) on %s { %s }", target.name.str, - target.opt, target.triple.str, target.cpu.str, target.features.str); + INFO("Configured target: %s/%d: (%s) on %s { %s }", target.name, + target.opt, target.triple, target.cpu, target.features); return target; } -static void delete_string(String string) { - DEBUG("deleting string..."); - switch (string.allocation) { - case LLVM: - LLVMDisposeMessage(string.str); - break; - case LIBC: - free(string.str); - break; - case NONE: - break; - } -} - void delete_target(Target target) { - delete_string(target.name); - delete_string(target.cpu); - delete_string(target.features); - delete_string(target.triple); + mem_free(target.name); + mem_free(target.cpu); + mem_free(target.features); + mem_free(target.triple); } typedef enum LLVMBackendError_t { UnresolvedImport } LLVMBackendError; diff --git a/src/llvm/backend.h b/src/llvm/backend.h index ea789ab..572b041 100644 --- a/src/llvm/backend.h +++ b/src/llvm/backend.h @@ -6,16 +6,11 @@ enum StringAllocation_t { LLVM, LIBC, NONE }; -typedef struct String_t { - enum StringAllocation_t allocation; - char* str; -} String; - typedef struct Target_t { - String name; - String triple; - String cpu; - String features; + char* name; + char* triple; + char* cpu; + char* features; LLVMCodeGenOptLevel opt; LLVMRelocMode reloc; LLVMCodeModel model; diff --git a/src/llvm/parser.c b/src/llvm/parser.c index 7cd256d..22f5d36 100644 --- a/src/llvm/parser.c +++ b/src/llvm/parser.c @@ -26,7 +26,7 @@ BackendError export_IR(LLVMBackendCompileUnit* unit, const Target* target, // convert module to LLVM-IR char* ir = LLVMPrintModuleToString(unit->module); - char* basename = g_strjoin(".", target->name.str, "ll", NULL); + char* basename = g_strjoin(".", target->name, "ll", NULL); // construct file name const char* filename = g_build_filename(config->archive_directory, basename, NULL); @@ -110,8 +110,8 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target, BackendError err = SUCCESS; DEBUG("exporting object file..."); - INFO("Using target (%s): %s with features: %s", target->name.str, - target->triple.str, target->features.str); + INFO("Using target (%s): %s with features: %s", target->name, + target->triple, target->features); LLVMTargetRef llvm_target = NULL; char* error = NULL; @@ -124,7 +124,7 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target, LLVMInitializeAllAsmPrinters(); DEBUG("creating target..."); - if (LLVMGetTargetFromTriple(target->triple.str, &llvm_target, &error) + if (LLVMGetTargetFromTriple(target->triple, &llvm_target, &error) != 0) { ERROR("failed to create target machine: %s", error); err = new_backend_impl_error(Implementation, NULL, @@ -135,10 +135,10 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target, DEBUG("Creating target machine..."); LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine( - llvm_target, target->triple.str, target->cpu.str, target->features.str, + llvm_target, target->triple, target->cpu, target->features, target->opt, target->reloc, target->model); - print_message(Info, "Generating code for: %s", target->triple.str); + print_message(Info, "Generating code for: %s", target->triple); if (config->print_asm) { err = emit_module_to_file(unit, target_machine, LLVMAssemblyFile, error,