Fixed loaing LLVM zero initialized constants

This commit is contained in:
Dmitry Stogov 2023-12-21 16:42:20 +03:00
parent da85f6b768
commit cf475c92d1
2 changed files with 18 additions and 11 deletions

View File

@ -2160,9 +2160,6 @@ static int ir_load_llvm_module(ir_loader *loader, LLVMModuleRef module)
type = LLVMGlobalGetValueType(sym);
size = LLVMABISizeOfType(target_data, type);
if (init && LLVMGetValueKind(init) != LLVMConstantAggregateZeroValueKind) {
has_data = 1;
}
name = llvm2ir_sym_name(buf, name, name_len);
if (!name) {
fprintf(stderr, "Bad LLVM symbol name \"%s\"\n", LLVMGetValueName2(sym, &name_len));
@ -2171,6 +2168,9 @@ static int ir_load_llvm_module(ir_loader *loader, LLVMModuleRef module)
if (LLVMIsGlobalConstant(sym)) {
flags |= IR_CONST;
}
if (init) {
has_data = 1;
}
if (!loader->sym_dcl(loader, name, flags, size, has_data)) {
fprintf(stderr, "Cannot compile LLVM symbol \"%s\"\n", name);
return 0;

View File

@ -560,35 +560,31 @@ static bool ir_loader_sym_dcl(ir_loader *loader, const char *name, uint32_t flag
static bool ir_loader_sym_data(ir_loader *loader, ir_type type, uint32_t count, const void *data)
{
ir_main_loader *l = (ir_main_loader*) loader;
size_t size = ir_type_size[type] * count;
size_t size = ir_type_size[type];
if ((l->dump & IR_DUMP_SAVE) && (l->dump_file)) {
const void *p = data;
uint32_t i;
switch (ir_type_size[type]) {
switch (size) {
case 1:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%02x,\n", ir_type_cname[type], (uint32_t)*(uint8_t*)p);
p = (void*)((uintptr_t)p + 1);
}
break;
case 2:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%04x,\n", ir_type_cname[type], (uint32_t)*(uint16_t*)p);
p = (void*)((uintptr_t)p + 1);
}
break;
case 4:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%08x,\n", ir_type_cname[type], *(uint32_t*)p);
p = (void*)((uintptr_t)p + 4);
}
break;
case 8:
for (i = 0; i < count; i++) {
fprintf(l->dump_file, "\t%s 0x%016" PRIx64 ",\n", ir_type_cname[type], *(uint64_t*)p);
p = (void*)((uintptr_t)p + 8);
}
break;
}
@ -601,9 +597,20 @@ static bool ir_loader_sym_data(ir_loader *loader, ir_type type, uint32_t count,
}
if (l->dump_asm || l->dump_size || l->run) {
IR_ASSERT(l->data_start);
memcpy((char*)l->data_start + l->data_pos, data, size);
if (count == 1) {
memcpy((char*)l->data_start + l->data_pos, data, size);
} else {
size_t pos = 0;
uint32_t i;
IR_ASSERT(count > 1);
for (i = 0; i < count; i++) {
memcpy((char*)l->data_start + l->data_pos + pos, data, size);
pos += size;
}
}
}
l->data_pos += size;
l->data_pos += size * count;
return 1;
}