authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-12-15 11:24:44+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-15 14:42:53-05:00
logf1407b4b7ebe33f00cc8222e875ce271716671ad
tree6de02da9460658c297e0de3322dc4b0b166719cb
parent19ddbd9e9e691ff7ab8789e8c6962497fbba4b88

Generate the fn pointers into the correct address space

Fixes #3645

4 files changed, 12 insertions(+), 4 deletions(-)

src/analyze.cpp+4-2
...@@ -8779,7 +8779,8 @@ static void resolve_llvm_types_fn_type(CodeGen *g, ZigType *fn_type) {...@@ -8779,7 +8779,8 @@ static void resolve_llvm_types_fn_type(CodeGen *g, ZigType *fn_type) {
87798779
8780 fn_type->data.fn.raw_type_ref = LLVMFunctionType(get_llvm_type(g, gen_return_type),8780 fn_type->data.fn.raw_type_ref = LLVMFunctionType(get_llvm_type(g, gen_return_type),
8781 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);8781 gen_param_types.items, (unsigned int)gen_param_types.length, fn_type_id->is_var_args);
8782 fn_type->llvm_type = LLVMPointerType(fn_type->data.fn.raw_type_ref, 0);8782 const unsigned fn_addrspace = ZigLLVMDataLayoutGetProgramAddressSpace(g->target_data_ref);
8783 fn_type->llvm_type = LLVMPointerType(fn_type->data.fn.raw_type_ref, fn_addrspace);
8783 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);8784 fn_type->data.fn.raw_di_type = ZigLLVMCreateSubroutineType(g->dbuilder, param_di_types.items, (int)param_di_types.length, 0);
8784 fn_type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,8785 fn_type->llvm_di_type = ZigLLVMCreateDebugPointerType(g->dbuilder, fn_type->data.fn.raw_di_type,
8785 LLVMStoreSizeOfType(g->target_data_ref, fn_type->llvm_type),8786 LLVMStoreSizeOfType(g->target_data_ref, fn_type->llvm_type),
...@@ -8888,7 +8889,8 @@ static void resolve_llvm_types_any_frame(CodeGen *g, ZigType *any_frame_type, Re...@@ -8888,7 +8889,8 @@ static void resolve_llvm_types_any_frame(CodeGen *g, ZigType *any_frame_type, Re
88888889
8889 ZigType *result_type = any_frame_type->data.any_frame.result_type;8890 ZigType *result_type = any_frame_type->data.any_frame.result_type;
8890 ZigType *ptr_result_type = (result_type == nullptr) ? nullptr : get_pointer_to_type(g, result_type, false);8891 ZigType *ptr_result_type = (result_type == nullptr) ? nullptr : get_pointer_to_type(g, result_type, false);
8891 LLVMTypeRef ptr_fn_llvm_type = LLVMPointerType(fn_type, 0);8892 const unsigned fn_addrspace = ZigLLVMDataLayoutGetProgramAddressSpace(g->target_data_ref);
8893 LLVMTypeRef ptr_fn_llvm_type = LLVMPointerType(fn_type, fn_addrspace);
8892 if (result_type == nullptr) {8894 if (result_type == nullptr) {
8893 g->anyframe_fn_type = ptr_fn_llvm_type;8895 g->anyframe_fn_type = ptr_fn_llvm_type;
8894 }8896 }
src/codegen.cpp+3-2
...@@ -422,9 +422,10 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {...@@ -422,9 +422,10 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
422 LLVMTypeRef fn_llvm_type = fn->raw_type_ref;422 LLVMTypeRef fn_llvm_type = fn->raw_type_ref;
423 LLVMValueRef llvm_fn = nullptr;423 LLVMValueRef llvm_fn = nullptr;
424 if (fn->body_node == nullptr) {424 if (fn->body_node == nullptr) {
425 const unsigned fn_addrspace = ZigLLVMDataLayoutGetProgramAddressSpace(g->target_data_ref);
425 LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, symbol_name);426 LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, symbol_name);
426 if (existing_llvm_fn) {427 if (existing_llvm_fn) {
427 return LLVMConstBitCast(existing_llvm_fn, LLVMPointerType(fn_llvm_type, 0));428 return LLVMConstBitCast(existing_llvm_fn, LLVMPointerType(fn_llvm_type, fn_addrspace));
428 } else {429 } else {
429 Buf *buf_symbol_name = buf_create_from_str(symbol_name);430 Buf *buf_symbol_name = buf_create_from_str(symbol_name);
430 auto entry = g->exported_symbol_names.maybe_get(buf_symbol_name);431 auto entry = g->exported_symbol_names.maybe_get(buf_symbol_name);
...@@ -447,7 +448,7 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {...@@ -447,7 +448,7 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
447 resolve_llvm_types_fn(g, tld_fn->fn_entry);448 resolve_llvm_types_fn(g, tld_fn->fn_entry);
448 tld_fn->fn_entry->llvm_value = LLVMAddFunction(g->module, symbol_name,449 tld_fn->fn_entry->llvm_value = LLVMAddFunction(g->module, symbol_name,
449 tld_fn->fn_entry->raw_type_ref);450 tld_fn->fn_entry->raw_type_ref);
450 llvm_fn = LLVMConstBitCast(tld_fn->fn_entry->llvm_value, LLVMPointerType(fn_llvm_type, 0));451 llvm_fn = LLVMConstBitCast(tld_fn->fn_entry->llvm_value, LLVMPointerType(fn_llvm_type, fn_addrspace));
451 return llvm_fn;452 return llvm_fn;
452 }453 }
453 }454 }
src/zig_llvm.cpp+4
...@@ -156,6 +156,10 @@ unsigned ZigLLVMDataLayoutGetStackAlignment(LLVMTargetDataRef TD) {...@@ -156,6 +156,10 @@ unsigned ZigLLVMDataLayoutGetStackAlignment(LLVMTargetDataRef TD) {
156 return unwrap(TD)->getStackAlignment();156 return unwrap(TD)->getStackAlignment();
157}157}
158158
159unsigned ZigLLVMDataLayoutGetProgramAddressSpace(LLVMTargetDataRef TD) {
160 return unwrap(TD)->getProgramAddressSpace();
161}
162
159bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,163bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
160 const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug,164 const char *filename, ZigLLVM_EmitOutputType output_type, char **error_message, bool is_debug,
161 bool is_small, bool time_report)165 bool is_small, bool time_report)
src/zig_llvm.h+1
...@@ -475,5 +475,6 @@ ZIG_EXTERN_C void ZigLLVMGetNativeTarget(enum ZigLLVM_ArchType *arch_type, enum...@@ -475,5 +475,6 @@ ZIG_EXTERN_C void ZigLLVMGetNativeTarget(enum ZigLLVM_ArchType *arch_type, enum
475 enum ZigLLVM_ObjectFormatType *oformat);475 enum ZigLLVM_ObjectFormatType *oformat);
476476
477ZIG_EXTERN_C unsigned ZigLLVMDataLayoutGetStackAlignment(LLVMTargetDataRef TD);477ZIG_EXTERN_C unsigned ZigLLVMDataLayoutGetStackAlignment(LLVMTargetDataRef TD);
478ZIG_EXTERN_C unsigned ZigLLVMDataLayoutGetProgramAddressSpace(LLVMTargetDataRef TD);
478479
479#endif480#endif