From 76b85c062242afbcc6a41ece4dc54cf251885a1b Mon Sep 17 00:00:00 2001 From: thisisnotnull <47349766+thisisnotnull@users.noreply.github.com> Date: Mon, 21 Nov 2022 21:39:43 +0100 Subject: [PATCH 1/5] fix wprintf return value --- core/fmt/fmt.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin index 9dd38eb29..79b575482 100644 --- a/core/fmt/fmt.odin +++ b/core/fmt/fmt.odin @@ -258,7 +258,7 @@ wprintf :: proc(w: io.Writer, fmt: string, args: ..any) -> int { was_prev_index := false loop: for i := 0; i < end; /**/ { - fi = Info{writer = w, good_arg_index = true, reordered = fi.reordered} + fi = Info{writer = w, good_arg_index = true, reordered = fi.reordered, n = fi.n} prev_i := i for i < end && !(fmt[i] == '%' || fmt[i] == '{' || fmt[i] == '}') { From e45401bfb40f076e0193fb88cef34789b0846d42 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 23 Nov 2022 14:14:22 +0000 Subject: [PATCH 2/5] Fix #2207 --- core/os/os2/heap_linux.odin | 2 +- core/os/os2/heap_windows.odin | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/os/os2/heap_linux.odin b/core/os/os2/heap_linux.odin index 5fdad5329..b631268a1 100644 --- a/core/os/os2/heap_linux.odin +++ b/core/os/os2/heap_linux.odin @@ -191,7 +191,7 @@ _heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, } switch mode { - case .Alloc: + case .Alloc, .Alloc_Non_Zeroed: return aligned_alloc(size, alignment) case .Free: diff --git a/core/os/os2/heap_windows.odin b/core/os/os2/heap_windows.odin index 90f0ae110..0f154cd8c 100644 --- a/core/os/os2/heap_windows.odin +++ b/core/os/os2/heap_windows.odin @@ -4,17 +4,17 @@ package os2 import "core:mem" import win32 "core:sys/windows" -heap_alloc :: proc(size: int) -> rawptr { - return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, uint(size)) +heap_alloc :: proc(size: int, zero_memory: bool) -> rawptr { + return win32.HeapAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY if zero_memory else 0, uint(size)) } -heap_resize :: proc(ptr: rawptr, new_size: int) -> rawptr { +heap_resize :: proc(ptr: rawptr, new_size: int, zero_memory: bool) -> rawptr { if new_size == 0 { heap_free(ptr) return nil } if ptr == nil { - return heap_alloc(new_size) + return heap_alloc(new_size, zero_memory) } return win32.HeapReAlloc(win32.GetProcessHeap(), win32.HEAP_ZERO_MEMORY, ptr, uint(new_size)) @@ -36,16 +36,16 @@ _heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, // the pointer we return to the user. // - aligned_alloc :: proc(size, alignment: int, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) { + aligned_alloc :: proc(size, alignment: int, zero_memory: bool, old_ptr: rawptr = nil) -> ([]byte, mem.Allocator_Error) { a := max(alignment, align_of(rawptr)) space := size + a - 1 allocated_mem: rawptr if old_ptr != nil { original_old_ptr := mem.ptr_offset((^rawptr)(old_ptr), -1)^ - allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr)) + allocated_mem = heap_resize(original_old_ptr, space+size_of(rawptr), zero_memory) } else { - allocated_mem = heap_alloc(space+size_of(rawptr)) + allocated_mem = heap_alloc(space+size_of(rawptr), zero_memory) } aligned_mem := rawptr(mem.ptr_offset((^u8)(allocated_mem), size_of(rawptr))) @@ -72,12 +72,12 @@ _heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, if p == nil { return nil, nil } - return aligned_alloc(new_size, new_alignment, p) + return aligned_alloc(new_size, new_alignment, true, p) } switch mode { - case .Alloc: - return aligned_alloc(size, alignment) + case .Alloc, .Alloc_Non_Zeroed: + return aligned_alloc(size, alignment, mode == .Alloc) case .Free: aligned_free(old_memory) @@ -87,7 +87,7 @@ _heap_allocator_proc :: proc(allocator_data: rawptr, mode: mem.Allocator_Mode, case .Resize: if old_memory == nil { - return aligned_alloc(size, alignment) + return aligned_alloc(size, alignment, true) } return aligned_resize(old_memory, old_size, size, alignment) From 51c705edf1f83b25c2997cc8a88107ed07756f92 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 23 Nov 2022 21:59:53 +0000 Subject: [PATCH 3/5] Add extra check to debug information of named composite types --- src/llvm_backend_debug.cpp | 41 +++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 60978d321..8da0697a7 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -569,14 +569,41 @@ LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) { case Type_Struct: case Type_Union: case Type_BitSet: - LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( - m->debug_builder, tag, name_text, name_len, nullptr, nullptr, 0, 0, size_in_bits, align_in_bits, flags, "", 0 - ); - idt.metadata = temp_forward_decl; + { + LLVMMetadataRef temp_forward_decl = LLVMDIBuilderCreateReplaceableCompositeType( + m->debug_builder, tag, name_text, name_len, nullptr, nullptr, 0, 0, size_in_bits, align_in_bits, flags, "", 0 + ); + idt.metadata = temp_forward_decl; - array_add(&m->debug_incomplete_types, idt); - lb_set_llvm_metadata(m, type, temp_forward_decl); - return temp_forward_decl; + array_add(&m->debug_incomplete_types, idt); + lb_set_llvm_metadata(m, type, temp_forward_decl); + + LLVMMetadataRef dummy = nullptr; + switch (bt->kind) { + case Type_Slice: + dummy = lb_debug_type(m, bt->Slice.elem); + dummy = lb_debug_type(m, t_int); + break; + case Type_DynamicArray: + dummy = lb_debug_type(m, bt->DynamicArray.elem); + dummy = lb_debug_type(m, t_int); + dummy = lb_debug_type(m, t_allocator); + break; + case Type_Map: + dummy = lb_debug_type(m, bt->Map.key); + dummy = lb_debug_type(m, bt->Map.value); + dummy = lb_debug_type(m, t_int); + dummy = lb_debug_type(m, t_allocator); + dummy = lb_debug_type(m, t_uintptr); + break; + case Type_BitSet: + if (bt->BitSet.elem) dummy = lb_debug_type(m, bt->BitSet.elem); + if (bt->BitSet.underlying) dummy = lb_debug_type(m, bt->BitSet.underlying); + break; + } + + return temp_forward_decl; + } } } From 22bcf1ba70690c4ba446327a54b3ad562f2a747a Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 23 Nov 2022 22:31:21 +0000 Subject: [PATCH 4/5] Extra check for slices and dynamic arrays for `-debug` --- src/llvm_backend_debug.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 8da0697a7..768c4cd1e 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -582,10 +582,12 @@ LLVMMetadataRef lb_debug_type(lbModule *m, Type *type) { switch (bt->kind) { case Type_Slice: dummy = lb_debug_type(m, bt->Slice.elem); + dummy = lb_debug_type(m, alloc_type_pointer(bt->Slice.elem)); dummy = lb_debug_type(m, t_int); break; case Type_DynamicArray: dummy = lb_debug_type(m, bt->DynamicArray.elem); + dummy = lb_debug_type(m, alloc_type_pointer(bt->DynamicArray.elem)); dummy = lb_debug_type(m, t_int); dummy = lb_debug_type(m, t_allocator); break; From 5ac36b5f25435a495d8516d5c9df06c09d3ca20e Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 23 Nov 2022 22:46:02 +0000 Subject: [PATCH 5/5] HACK: Get around debugging type generation for slices and dynamic arrays of *nix systems --- src/llvm_backend_debug.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/llvm_backend_debug.cpp b/src/llvm_backend_debug.cpp index 768c4cd1e..65cefcd39 100644 --- a/src/llvm_backend_debug.cpp +++ b/src/llvm_backend_debug.cpp @@ -688,13 +688,25 @@ void lb_debug_complete_types(lbModule *m) { case Type_Slice: element_count = 2; elements = gb_alloc_array(temporary_allocator(), LLVMMetadataRef, element_count); - elements[0] = lb_debug_struct_field(m, str_lit("data"), alloc_type_pointer(bt->Slice.elem), 0*word_bits); + #if defined(GB_SYSTEM_WINDOWS) + elements[0] = lb_debug_struct_field(m, str_lit("data"), alloc_type_pointer(bt->Slice.elem), 0*word_bits); + #else + // FIX HACK TODO(bill): For some reason this causes a crash in *nix systems due to the reference counting + // of the debug type information + elements[0] = lb_debug_struct_field(m, str_lit("data"), t_rawptr, 0*word_bits); + #endif elements[1] = lb_debug_struct_field(m, str_lit("len"), t_int, 1*word_bits); break; case Type_DynamicArray: element_count = 4; elements = gb_alloc_array(temporary_allocator(), LLVMMetadataRef, element_count); - elements[0] = lb_debug_struct_field(m, str_lit("data"), alloc_type_pointer(bt->DynamicArray.elem), 0*word_bits); + #if defined(GB_SYSTEM_WINDOWS) + elements[0] = lb_debug_struct_field(m, str_lit("data"), alloc_type_pointer(bt->DynamicArray.elem), 0*word_bits); + #else + // FIX HACK TODO(bill): For some reason this causes a crash in *nix systems due to the reference counting + // of the debug type information + elements[0] = lb_debug_struct_field(m, str_lit("data"), t_rawptr, 0*word_bits); + #endif elements[1] = lb_debug_struct_field(m, str_lit("len"), t_int, 1*word_bits); elements[2] = lb_debug_struct_field(m, str_lit("cap"), t_int, 2*word_bits); elements[3] = lb_debug_struct_field(m, str_lit("allocator"), t_allocator, 3*word_bits);