fix: removed String wrapper in favor of cached memory

This commit is contained in:
Sven Vogel 2024-10-10 20:29:03 +02:00
parent 96c38815fe
commit 8d748038c0
3 changed files with 31 additions and 56 deletions

View File

@ -13,20 +13,18 @@ Target create_native_target() {
DEBUG("creating native target..."); DEBUG("creating native target...");
Target target; Target target;
target.name.str = "tmp"; char* triple = LLVMGetDefaultTargetTriple();
target.name.allocation = NONE; char* cpu = LLVMGetHostCPUName();
char* features = LLVMGetHostCPUFeatures();
target.triple.str = LLVMGetDefaultTargetTriple(); target.name = "tmp";
target.triple.allocation = LLVM; target.triple = mem_strdup(MemoryNamespaceLld, triple);
assert(target.triple.str != NULL); target.cpu = mem_strdup(MemoryNamespaceLld, cpu);
target.features = mem_strdup(MemoryNamespaceLld, features);
target.cpu.str = LLVMGetHostCPUName(); LLVMDisposeMessage(triple);
target.cpu.allocation = LLVM; LLVMDisposeMessage(cpu);
assert(target.cpu.str != NULL); LLVMDisposeMessage(features);
target.features.str = LLVMGetHostCPUFeatures();
target.features.allocation = LLVM;
assert(target.features.str != NULL);
target.opt = LLVMCodeGenLevelNone; target.opt = LLVMCodeGenLevelNone;
target.reloc = LLVMRelocDefault; target.reloc = LLVMRelocDefault;
@ -66,16 +64,13 @@ Target create_target_from_triple(char* triple)
{ {
Target target; Target target;
target.triple.str = mem_strdup(MemoryNamespaceLld, triple); target.triple = mem_strdup(MemoryNamespaceLld, triple);
target.triple.allocation = NONE;
// select default // select default
target.cpu.str = ""; target.cpu = "";
target.cpu.allocation = NONE;
// select default // select default
target.features.str = ""; target.features = "";
target.features.allocation = NONE;
return target; return target;
} }
@ -89,39 +84,24 @@ Target create_target_from_config(TargetConfig* config) {
target = create_target_from_triple(config->triple); target = create_target_from_triple(config->triple);
} else } else
{ {
config->triple = target.triple.str; config->triple = target.triple;
} }
target.name.str = create_target_output_name(config); target.name = create_target_output_name(config);
target.name.allocation = NONE; // freed later by compiler
target.opt = llvm_opt_from_int(config->optimization_level); target.opt = llvm_opt_from_int(config->optimization_level);
INFO("Configured target: %s/%d: (%s) on %s { %s }", target.name.str, INFO("Configured target: %s/%d: (%s) on %s { %s }", target.name,
target.opt, target.triple.str, target.cpu.str, target.features.str); target.opt, target.triple, target.cpu, target.features);
return target; 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) { void delete_target(Target target) {
delete_string(target.name); mem_free(target.name);
delete_string(target.cpu); mem_free(target.cpu);
delete_string(target.features); mem_free(target.features);
delete_string(target.triple); mem_free(target.triple);
} }
typedef enum LLVMBackendError_t { UnresolvedImport } LLVMBackendError; typedef enum LLVMBackendError_t { UnresolvedImport } LLVMBackendError;

View File

@ -6,16 +6,11 @@
enum StringAllocation_t { LLVM, LIBC, NONE }; enum StringAllocation_t { LLVM, LIBC, NONE };
typedef struct String_t {
enum StringAllocation_t allocation;
char* str;
} String;
typedef struct Target_t { typedef struct Target_t {
String name; char* name;
String triple; char* triple;
String cpu; char* cpu;
String features; char* features;
LLVMCodeGenOptLevel opt; LLVMCodeGenOptLevel opt;
LLVMRelocMode reloc; LLVMRelocMode reloc;
LLVMCodeModel model; LLVMCodeModel model;

View File

@ -26,7 +26,7 @@ BackendError export_IR(LLVMBackendCompileUnit* unit, const Target* target,
// convert module to LLVM-IR // convert module to LLVM-IR
char* ir = LLVMPrintModuleToString(unit->module); 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 // construct file name
const char* filename = const char* filename =
g_build_filename(config->archive_directory, basename, NULL); g_build_filename(config->archive_directory, basename, NULL);
@ -110,8 +110,8 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target,
BackendError err = SUCCESS; BackendError err = SUCCESS;
DEBUG("exporting object file..."); DEBUG("exporting object file...");
INFO("Using target (%s): %s with features: %s", target->name.str, INFO("Using target (%s): %s with features: %s", target->name,
target->triple.str, target->features.str); target->triple, target->features);
LLVMTargetRef llvm_target = NULL; LLVMTargetRef llvm_target = NULL;
char* error = NULL; char* error = NULL;
@ -124,7 +124,7 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target,
LLVMInitializeAllAsmPrinters(); LLVMInitializeAllAsmPrinters();
DEBUG("creating target..."); DEBUG("creating target...");
if (LLVMGetTargetFromTriple(target->triple.str, &llvm_target, &error) if (LLVMGetTargetFromTriple(target->triple, &llvm_target, &error)
!= 0) { != 0) {
ERROR("failed to create target machine: %s", error); ERROR("failed to create target machine: %s", error);
err = new_backend_impl_error(Implementation, NULL, err = new_backend_impl_error(Implementation, NULL,
@ -135,10 +135,10 @@ BackendError export_object(LLVMBackendCompileUnit* unit, const Target* target,
DEBUG("Creating target machine..."); DEBUG("Creating target machine...");
LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine( 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); 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) { if (config->print_asm) {
err = emit_module_to_file(unit, target_machine, LLVMAssemblyFile, error, err = emit_module_to_file(unit, target_machine, LLVMAssemblyFile, error,