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...");
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;

View File

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

View File

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