From d6856859d3082d9b66aac7c25ceb2abcd13e2f7c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 23 Mar 2017 02:59:58 -0400 Subject: improvements for windows and libc integration * standard library knows if it is linking against libc and will sometimes call libc functions in that case instead of providing redundant definitions * fix infinite loop bug when resolving use declarations * allow calling the same C function from different C imports. closes #277 * push more logic from compiler to std/bootstrap.zig * standard library provides way to access errno closes #274 * fix compile error in standard library for windows * add implementation of getRandomBytes for windows --- src/codegen.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 3babaac0e4..03629b2da1 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -272,7 +272,17 @@ static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) { } TypeTableEntry *fn_type = fn_table_entry->type_entry; - fn_table_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name), fn_type->data.fn.raw_type_ref); + LLVMTypeRef fn_llvm_type = fn_type->data.fn.raw_type_ref; + if (!fn_table_entry->internal_linkage && fn_table_entry->body_node == nullptr) { + LLVMValueRef existing_llvm_fn = LLVMGetNamedFunction(g->module, buf_ptr(symbol_name)); + if (existing_llvm_fn) { + fn_table_entry->llvm_value = LLVMConstBitCast(existing_llvm_fn, LLVMPointerType(fn_llvm_type, 0)); + } else { + fn_table_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name), fn_llvm_type); + } + } else { + fn_table_entry->llvm_value = LLVMAddFunction(g->module, buf_ptr(symbol_name), fn_llvm_type); + } switch (fn_table_entry->fn_inline) { case FnInlineAlways: -- cgit v1.2.3