This commit is contained in:
David Gonzalez Martin 2025-06-08 09:07:52 -06:00
parent 04ca049e6c
commit d2a680332d
4 changed files with 1228 additions and 104 deletions

File diff suppressed because it is too large Load Diff

View File

@ -250,7 +250,7 @@ fn void llvm_initialize(Module* module)
llvm_initialize_all();
auto context = LLVMContextCreate();
auto m = llvm_context_create_module(context, module->name);
auto m = LLVMModuleCreateWithNameInContext((char*)module->name.pointer, context);
auto builder = LLVMCreateBuilderInContext(context);
LLVMDIBuilderRef di_builder = 0;
@ -2364,7 +2364,7 @@ fn Global* get_enum_name_array_global(Module* module, Type* enum_type)
field.name,
};
unsigned address_space = 0;
auto name_global = llvm_module_create_global_variable(module->llvm.module, LLVMArrayType2(u8_type->llvm.abi, field.name.length + null_terminate), is_constant, LLVMInternalLinkage, LLVMConstStringInContext2(module->llvm.context, (char*)field.name.pointer, field.name.length, false), arena_join_string(module->arena, array_to_slice(name_parts)), name_before, LLVMNotThreadLocal, address_space, false);
auto name_global = llvm_module_create_global_variable(module->arena, module->llvm.module, LLVMArrayType2(u8_type->llvm.abi, field.name.length + null_terminate), is_constant, LLVMInternalLinkage, LLVMConstStringInContext2(module->llvm.context, (char*)field.name.pointer, field.name.length, false), arena_join_string(module->arena, array_to_slice(name_parts)), LLVMNotThreadLocal, address_space, false);
name_before = name_global;
LLVMValueRef constants[] = {
name_global,
@ -2380,7 +2380,7 @@ fn Global* get_enum_name_array_global(Module* module, Type* enum_type)
auto name_array_type = LLVMArrayType2(slice_type->llvm.abi, array_element_count);
auto is_constant = true;
unsigned address_space = 0;
auto name_array_variable = llvm_module_create_global_variable(module->llvm.module, name_array_type, is_constant, LLVMInternalLinkage, name_array, string_literal("name.array.enum"), name_before, LLVMNotThreadLocal, address_space, false);
auto name_array_variable = llvm_module_create_global_variable(module->arena, module->llvm.module, name_array_type, is_constant, LLVMInternalLinkage, name_array, string_literal("name.array.enum"), LLVMNotThreadLocal, address_space, false);
LLVMSetAlignment(name_array_variable, get_byte_alignment(slice_type));
LLVMSetUnnamedAddress(name_array_variable, LLVMGlobalUnnamedAddr);
@ -3231,6 +3231,7 @@ fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnal
if (expected_type->array.element_count == 0)
{
// TODO: use existing types?
expected_type->array.element_count = values.length;
assert(expected_type->name.equal(string_literal("")));
expected_type->name = array_name(module, expected_type->array.element_type, expected_type->array.element_count);
@ -4006,11 +4007,10 @@ fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnal
auto value_array = LLVMConstArray2(enum_value_type, value_constant_buffer, array_element_count);
auto value_array_variable_type = LLVMArrayType2(enum_value_type, array_element_count);
auto is_constant = true;
LLVMValueRef before = 0;
LLVMThreadLocalMode thread_local_mode = LLVMNotThreadLocal;
unsigned address_space = 0;
auto externally_initialized = false;
auto value_array_variable = llvm_module_create_global_variable(module->llvm.module, value_array_variable_type, is_constant, LLVMInternalLinkage, value_array, string_literal("value.array.enum"), before, thread_local_mode, address_space, externally_initialized);
auto value_array_variable = llvm_module_create_global_variable(module->arena, module->llvm.module, value_array_variable_type, is_constant, LLVMInternalLinkage, value_array, string_literal("value.array.enum"), thread_local_mode, address_space, externally_initialized);
LLVMSetAlignment(value_array_variable, enum_alignment);
LLVMSetUnnamedAddress(value_array_variable, LLVMGlobalUnnamedAddr);
@ -4897,7 +4897,7 @@ fn SliceEmitResult emit_string_literal(Module* module, Value* value)
LLVMThreadLocalMode tlm = LLVMNotThreadLocal;
bool externally_initialized = false;
unsigned address_space = 0;
auto global = llvm_module_create_global_variable(module->llvm.module, string_type, is_constant, LLVMInternalLinkage, constant_string, string_literal("conststring"), before, tlm, address_space, externally_initialized);
auto global = llvm_module_create_global_variable(module->arena, module->llvm.module, string_type, is_constant, LLVMInternalLinkage, constant_string, string_literal("conststring"), tlm, address_space, externally_initialized);
LLVMSetUnnamedAddress(global, LLVMGlobalUnnamedAddr);
auto slice_type = get_slice_type(module, u8_type);
@ -5081,11 +5081,6 @@ fn LLVMValueRef emit_call(Module* module, Value* value, LLVMValueRef left_llvm,
});
}
if (get_byte_size(semantic_argument_type) > 60 && argument_abi.flags.kind != AbiKind::indirect)
{
trap();
}
resolve_type_in_place(module, semantic_argument_type);
if (is_named_argument)
@ -5263,7 +5258,7 @@ fn LLVMValueRef emit_call(Module* module, Value* value, LLVMValueRef left_llvm,
u32 address_space = 0;
bool externally_initialized = false;
auto global = llvm_module_create_global_variable(module->llvm.module, semantic_argument_type->llvm.memory, is_constant, linkage_type, semantic_call_argument_value->llvm, string_literal("conststruct"), before, thread_local_mode, address_space, externally_initialized);
auto global = llvm_module_create_global_variable(module->arena, module->llvm.module, semantic_argument_type->llvm.memory, is_constant, linkage_type, semantic_call_argument_value->llvm, string_literal("conststruct"), thread_local_mode, address_space, externally_initialized);
LLVMSetUnnamedAddress(global, LLVMGlobalUnnamedAddr);
auto alignment = get_byte_alignment(semantic_argument_type);
@ -5420,7 +5415,7 @@ fn LLVMValueRef emit_call(Module* module, Value* value, LLVMValueRef left_llvm,
u32 address_space = 0;
bool externally_initialized = false;
auto global = llvm_module_create_global_variable(module->llvm.module, semantic_argument_type->llvm.memory, is_constant, linkage_type, semantic_call_argument_value->llvm, string_literal("conststruct"), before, thread_local_mode, address_space, externally_initialized);
auto global = llvm_module_create_global_variable(module->arena, module->llvm.module, semantic_argument_type->llvm.memory, is_constant, linkage_type, semantic_call_argument_value->llvm, string_literal("conststruct"), thread_local_mode, address_space, externally_initialized);
LLVMSetUnnamedAddress(global, LLVMGlobalUnnamedAddr);
auto alignment = get_byte_alignment(semantic_argument_type);
@ -6188,7 +6183,7 @@ fn void emit_assignment(Module* module, LLVMValueRef left_llvm, Type* left_type,
u32 address_space = 0;
bool externally_initialized = false;
auto global = llvm_module_create_global_variable(module->llvm.module, value_type->llvm.memory, is_constant, linkage_type, right->llvm, string_literal("constarray"), before, thread_local_mode, address_space, externally_initialized);
auto global = llvm_module_create_global_variable(module->arena, module->llvm.module, value_type->llvm.memory, is_constant, linkage_type, right->llvm, string_literal("constarray"), thread_local_mode, address_space, externally_initialized);
LLVMSetUnnamedAddress(global, LLVMGlobalUnnamedAddr);
@ -6272,7 +6267,7 @@ fn void emit_assignment(Module* module, LLVMValueRef left_llvm, Type* left_type,
unsigned address_space = 0;
LLVMThreadLocalMode thread_local_mode = LLVMNotThreadLocal;
bool externally_initialized = false;
auto global = llvm_module_create_global_variable(module->llvm.module, value_type->llvm.memory, is_constant, linkage_type, right->llvm, string_literal("constarray"), before, thread_local_mode, address_space, externally_initialized);
auto global = llvm_module_create_global_variable(module->arena, module->llvm.module, value_type->llvm.memory, is_constant, linkage_type, right->llvm, string_literal("constarray"), thread_local_mode, address_space, externally_initialized);
LLVMSetUnnamedAddress(global, LLVMGlobalUnnamedAddr);
LLVMSetAlignment(global, alignment);
LLVMBuildMemCpy(module->llvm.builder, left_llvm, alignment, global, alignment, byte_size_value);
@ -7070,7 +7065,7 @@ fn void emit_value(Module* module, Value* value, TypeKind type_kind, bool expect
auto array_type = resolved_value_type->pointer.element_type;
assert(array_type->id == TypeId::array);
resolve_type_in_place(module, array_type);
auto value_array_variable = llvm_module_create_global_variable(module->llvm.module, array_type->llvm.memory, is_constant, LLVMInternalLinkage, array_value, string_literal("enum.values"), 0, LLVMNotThreadLocal, 0, 0);
auto value_array_variable = llvm_module_create_global_variable(module->arena, module->llvm.module, array_type->llvm.memory, is_constant, LLVMInternalLinkage, array_value, string_literal("enum.values"), LLVMNotThreadLocal, 0, 0);
auto alignment = get_byte_alignment(resolved_value_type);
LLVMSetAlignment(value_array_variable, alignment);
LLVMSetUnnamedAddress(value_array_variable, LLVMGlobalUnnamedAddr);
@ -8036,7 +8031,7 @@ fn void analyze_statement(Module* module, Scope* scope, Statement* statement, u3
auto condition = statement->if_st.condition;
analyze_value(module, condition, 0, TypeKind::abi, false);
auto llvm_condition = emit_condition(module, statement->if_st.condition);
auto llvm_condition = emit_condition(module, condition);
LLVMBuildCondBr(module->llvm.builder, llvm_condition, taken_block, not_taken_block);
LLVMPositionBuilderAtEnd(module->llvm.builder, taken_block);
@ -8125,12 +8120,12 @@ fn void analyze_statement(Module* module, Scope* scope, Statement* statement, u3
LLVMBuildBr(module->llvm.builder, entry_block);
if (llvm_value_use_empty((LLVMValueRef)body_block))
if (!LLVMGetFirstUse((LLVMValueRef)body_block))
{
trap();
}
if (llvm_value_use_empty((LLVMValueRef)exit_block))
if (!LLVMGetFirstUse((LLVMValueRef)exit_block))
{
trap();
}
@ -8621,8 +8616,10 @@ fn void analyze_statement(Module* module, Scope* scope, Statement* statement, u3
LLVMValueRef indices[] = {
body_index_load,
};
auto gep_type = aggregate_type->structure.fields[0].type->pointer.element_type;
resolve_type_in_place(module, gep_type);
auto gep = create_gep(module, {
.type = aggregate_type->structure.fields[0].type->pointer.element_type->llvm.memory,
.type = gep_type->llvm.memory,
.pointer = extract_pointer,
.indices = array_to_slice(indices),
});
@ -9248,7 +9245,7 @@ void emit(Module* module)
unsigned address_space = 0;
bool externally_initialized = false;
auto global_llvm = llvm_module_create_global_variable(module->llvm.module, global_type->llvm.memory, is_constant, linkage, global->variable.initial_value->llvm, global->variable.name, before, thread_local_mode, address_space, externally_initialized);
auto global_llvm = llvm_module_create_global_variable(module->arena, module->llvm.module, global_type->llvm.memory, is_constant, linkage, global->variable.initial_value->llvm, global->variable.name, thread_local_mode, address_space, externally_initialized);
auto alignment = get_byte_alignment(global_type);
LLVMSetAlignment(global_llvm, alignment);
global->variable.storage->llvm = global_llvm;
@ -9552,7 +9549,7 @@ void emit(Module* module)
{
assert(!LLVMGetBasicBlockTerminator(current_basic_block));
if (llvm_basic_block_is_empty(current_basic_block) || llvm_value_use_empty((LLVMValueRef)current_basic_block))
if (!LLVMGetFirstInstruction(current_basic_block) || !LLVMGetFirstUse((LLVMValueRef)current_basic_block))
{
LLVMReplaceAllUsesWith((LLVMValueRef)return_block, (LLVMValueRef)current_basic_block);
LLVMDeleteBasicBlock(return_block);
@ -9633,7 +9630,7 @@ void emit(Module* module)
auto alloca = LLVMGetOperand(store, 1);
assert(alloca == return_alloca);
LLVMInstructionEraseFromParent(store);
assert(llvm_value_use_empty(alloca));
assert(!LLVMGetFirstUse(alloca));
LLVMInstructionEraseFromParent(alloca);
}
else

View File

@ -31,32 +31,6 @@ fn llvm::StringRef string_ref(String string)
return llvm::StringRef((char*)string.pointer, string.length);
}
EXPORT LLVMModuleRef llvm_context_create_module(LLVMContextRef context, String name)
{
auto module = new llvm::Module(string_ref(name), *llvm::unwrap(context));
return wrap(module);
}
EXPORT LLVMValueRef llvm_module_create_global_variable(LLVMModuleRef module, LLVMTypeRef type, bool is_constant, LLVMLinkage linkage_type, LLVMValueRef initial_value, String name, LLVMValueRef before, LLVMThreadLocalMode thread_local_mode, unsigned address_space, bool externally_initialized)
{
llvm::GlobalValue::LinkageTypes linkage;
switch (linkage_type)
{
case LLVMExternalLinkage: linkage = llvm::GlobalValue::ExternalLinkage; break;
case LLVMInternalLinkage: linkage = llvm::GlobalValue::InternalLinkage; break;
default: trap();
}
llvm::GlobalValue::ThreadLocalMode tlm;
switch (thread_local_mode)
{
case LLVMNotThreadLocal: tlm = llvm::GlobalValue::NotThreadLocal; break;
default: trap();
}
auto* global = new llvm::GlobalVariable(*llvm::unwrap(module), llvm::unwrap(type), is_constant, linkage, llvm::unwrap<llvm::Constant>(initial_value), string_ref(name), before ? llvm::unwrap<llvm::GlobalVariable>(before) : 0, tlm, address_space, externally_initialized);
return wrap(global);
}
EXPORT void llvm_subprogram_replace_type(LLVMMetadataRef subprogram, LLVMMetadataRef subroutine_type)
{
auto sp = llvm::unwrap<llvm::DISubprogram>(subprogram);
@ -169,16 +143,6 @@ EXPORT LLVMValueRef llvm_find_return_value_dominating_store(LLVMBuilderRef b, LL
return wrap(store);
}
EXPORT bool llvm_value_use_empty(LLVMValueRef value)
{
return llvm::unwrap(value)->use_empty();
}
EXPORT bool llvm_basic_block_is_empty(LLVMBasicBlockRef basic_block)
{
return llvm::unwrap(basic_block)->empty();
}
EXPORT LLVMValueRef llvm_builder_create_alloca(LLVMBuilderRef b, LLVMTypeRef type, unsigned address_space, u32 alignment, String name)
{
auto& builder = *llvm::unwrap(b);

View File

@ -130,13 +130,22 @@ extern "C" LLVMValueRef llvm_module_create_function(LLVMModuleRef module, LLVMTy
extern "C" LLVMBasicBlockRef llvm_context_create_basic_block(LLVMContextRef context, String name, LLVMValueRef parent_function);
extern "C" LLVMValueRef llvm_module_create_global_variable(LLVMModuleRef module, LLVMTypeRef type, bool is_constant, LLVMLinkage linkage_type, LLVMValueRef initial_value, String name, LLVMValueRef before, LLVMThreadLocalMode thread_local_mode, unsigned address_space, bool externally_initialized);
fn LLVMValueRef llvm_module_create_global_variable(Arena* arena, LLVMModuleRef module, LLVMTypeRef type, bool is_constant, LLVMLinkage linkage_type, LLVMValueRef initial_value, String name, LLVMThreadLocalMode thread_local_mode, unsigned address_space, bool externally_initialized)
{
name = arena_duplicate_string(arena, name);
auto default_address_space = 0;
auto global = LLVMAddGlobalInAddressSpace(module, type, (char*)name.pointer, default_address_space);
LLVMSetGlobalConstant(global, is_constant);
LLVMSetLinkage(global, linkage_type);
LLVMSetInitializer(global, initial_value);
LLVMSetThreadLocalMode(global, thread_local_mode);
LLVMSetExternallyInitialized(global, externally_initialized);
return global;
}
extern "C" LLVMValueRef llvm_builder_create_alloca(LLVMBuilderRef builder, LLVMTypeRef type, unsigned address_space, u32 alignment, String name);
extern "C" bool llvm_basic_block_is_empty(LLVMBasicBlockRef basic_block);
extern "C" LLVMValueRef llvm_find_return_value_dominating_store(LLVMBuilderRef b, LLVMValueRef ra, LLVMTypeRef et);
extern "C" bool llvm_value_use_empty(LLVMValueRef value);
extern "C" bool llvm_function_verify(LLVMValueRef function_value, String* error_message);
extern "C" bool llvm_module_verify(LLVMModuleRef m, String* error_message);