From 404c87b06afbeebfea6c839665918c7bd1328d74 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 14 Jan 2019 18:11:17 -0500 Subject: [PATCH 1/6] fix incorrect parameter names for std.math.atan2 --- std/math/atan2.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/std/math/atan2.zig b/std/math/atan2.zig index b3e45ba045..a7757132d7 100644 --- a/std/math/atan2.zig +++ b/std/math/atan2.zig @@ -22,10 +22,10 @@ const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; -pub fn atan2(comptime T: type, x: T, y: T) T { +pub fn atan2(comptime T: type, y: T, x: T) T { return switch (T) { - f32 => atan2_32(x, y), - f64 => atan2_64(x, y), + f32 => atan2_32(y, x), + f64 => atan2_64(y, x), else => @compileError("atan2 not implemented for " ++ @typeName(T)), }; } From 5ab8db7b3ed902159f45a1c76e9c096439f6f0d7 Mon Sep 17 00:00:00 2001 From: Sahnvour Date: Sat, 12 Jan 2019 20:13:06 +0100 Subject: [PATCH 2/6] removed unnecessary cast --- std/debug/index.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/std/debug/index.zig b/std/debug/index.zig index 3050d15c59..7948f5a388 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -247,8 +247,7 @@ pub fn writeCurrentStackTraceWindows( start_addr: ?usize, ) !void { var addr_buf: [1024]usize = undefined; - const casted_len = @intCast(u32, addr_buf.len); // TODO shouldn't need this cast - const n = windows.RtlCaptureStackBackTrace(0, casted_len, @ptrCast(**c_void, &addr_buf), null); + const n = windows.RtlCaptureStackBackTrace(0, addr_buf.len, @ptrCast(**c_void, &addr_buf), null); const addrs = addr_buf[0..n]; var start_i: usize = if (start_addr) |saddr| blk: { for (addrs) |addr, i| { From a60ecdc6815555c59cd671e7846b1058be3f07fd Mon Sep 17 00:00:00 2001 From: Sahnvour Date: Thu, 17 Jan 2019 01:06:26 +0100 Subject: [PATCH 3/6] Hopefully fixed #1503 (at least improved) line accuracy of stack traces on windows. --- std/debug/index.zig | 68 ++++++++++++++++++++++++++++++--------------- std/pdb.zig | 12 ++++++++ 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/std/debug/index.zig b/std/debug/index.zig index 7948f5a388..445f943594 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -337,50 +337,74 @@ fn printSourceAtAddressWindows(di: *DebugInfo, out_stream: var, relocated_addres switch (subsect_hdr.Kind) { pdb.DebugSubsectionKind.Lines => { - var line_index: usize = sect_offset; + var line_index = sect_offset; const line_hdr = @ptrCast(*pdb.LineFragmentHeader, &subsect_info[line_index]); if (line_hdr.RelocSegment == 0) return error.MissingDebugInfo; line_index += @sizeOf(pdb.LineFragmentHeader); - - const block_hdr = @ptrCast(*pdb.LineBlockFragmentHeader, &subsect_info[line_index]); - line_index += @sizeOf(pdb.LineBlockFragmentHeader); - - const has_column = line_hdr.Flags.LF_HaveColumns; - const frag_vaddr_start = coff_section.header.virtual_address + line_hdr.RelocOffset; const frag_vaddr_end = frag_vaddr_start + line_hdr.CodeSize; - if (relative_address >= frag_vaddr_start and relative_address < frag_vaddr_end) { - var line_i: usize = 0; + + // There is an unknown number of LineBlockFragmentHeaders (and their accompanying line and column records) + // from now on. We will iterate through them, and eventually find a LineInfo that we're interested in, + // breaking out to :subsections. If not, we will make sure to not read anything outside of this subsection. + const subsection_end_index = sect_offset + subsect_hdr.Length; + while (line_index < subsection_end_index) { + const block_hdr = @ptrCast(*pdb.LineBlockFragmentHeader, &subsect_info[line_index]); + line_index += @sizeOf(pdb.LineBlockFragmentHeader); const start_line_index = line_index; - while (line_i < block_hdr.NumLines) : (line_i += 1) { - const line_num_entry = @ptrCast(*pdb.LineNumberEntry, &subsect_info[line_index]); - line_index += @sizeOf(pdb.LineNumberEntry); - const flags = @ptrCast(*pdb.LineNumberEntry.Flags, &line_num_entry.Flags); - const vaddr_start = frag_vaddr_start + line_num_entry.Offset; - const vaddr_end = if (flags.End == 0) frag_vaddr_end else vaddr_start + flags.End; - if (relative_address >= vaddr_start and relative_address < vaddr_end) { + + const has_column = line_hdr.Flags.LF_HaveColumns; + + if (relative_address >= frag_vaddr_start and relative_address < frag_vaddr_end) { + // All line entries are stored inside their line block by ascending start address. + // Heuristic: we want to find the last line entry that has a vaddr_start <= relative_address. + // This is done with a simple linear search. + var line_i: u32 = 0; + while (line_i < block_hdr.NumLines) : (line_i += 1) { + const line_num_entry = @ptrCast(*pdb.LineNumberEntry, &subsect_info[line_index]); + line_index += @sizeOf(pdb.LineNumberEntry); + + const vaddr_start = frag_vaddr_start + line_num_entry.Offset; + if (relative_address <= vaddr_start) { + break; + } + } + + // line_i == 0 would mean that no matching LineNumberEntry was found. + if (line_i > 0) { const subsect_index = checksum_offset + block_hdr.NameIndex; const chksum_hdr = @ptrCast(*pdb.FileChecksumEntryHeader, &mod.subsect_info[subsect_index]); const strtab_offset = @sizeOf(pdb.PDBStringTableHeader) + chksum_hdr.FileNameOffset; try di.pdb.string_table.seekTo(strtab_offset); const source_file_name = try di.pdb.string_table.readNullTermString(allocator); - const line = flags.Start; + + const line_entry_idx = line_i - 1; + const column = if (has_column) blk: { - line_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.NumLines; - line_index += @sizeOf(pdb.ColumnNumberEntry) * line_i; - const col_num_entry = @ptrCast(*pdb.ColumnNumberEntry, &subsect_info[line_index]); + const start_col_index = start_line_index + @sizeOf(pdb.LineNumberEntry) * block_hdr.NumLines; + const col_index = start_col_index + @sizeOf(pdb.ColumnNumberEntry) * line_entry_idx; + const col_num_entry = @ptrCast(*pdb.ColumnNumberEntry, &subsect_info[col_index]); break :blk col_num_entry.StartColumn; } else 0; + + const found_line_index = start_line_index + line_entry_idx * @sizeOf(pdb.LineNumberEntry); + const line_num_entry = @ptrCast(*pdb.LineNumberEntry, &subsect_info[found_line_index]); + const flags = @ptrCast(*pdb.LineNumberEntry.Flags, &line_num_entry.Flags); + break :subsections LineInfo{ .allocator = allocator, .file_name = source_file_name, - .line = line, + .line = flags.Start, .column = column, }; } } - break :subsections null; + } + + // Checking that we are not reading garbage after the (possibly) multiple block fragments. + if (line_index != subsection_end_index) { + return error.InvalidDebugInfo; } }, else => {}, diff --git a/std/pdb.zig b/std/pdb.zig index 0cfc6a6cda..b6e48ad2f7 100644 --- a/std/pdb.zig +++ b/std/pdb.zig @@ -9,6 +9,10 @@ const coff = std.coff; const ArrayList = std.ArrayList; +// Note: most of this is based on information gathered from LLVM source code, +// documentation and/or contributors. + + // https://llvm.org/docs/PDB/DbiStream.html#stream-header pub const DbiStreamHeader = packed struct { VersionSignature: i32, @@ -345,6 +349,10 @@ pub const RecordPrefix = packed struct { RecordKind: SymbolKind, }; +/// The following variable length array appears immediately after the header. +/// The structure definition follows. +/// LineBlockFragmentHeader Blocks[] +/// Each `LineBlockFragmentHeader` as specified below. pub const LineFragmentHeader = packed struct { /// Code offset of line contribution. RelocOffset: u32, @@ -386,7 +394,11 @@ pub const LineNumberEntry = packed struct { /// TODO runtime crash when I make the actual type of Flags this const Flags = packed struct { + /// Start line number Start: u24, + + /// Delta of lines to the end of the expression. Still unclear. + // TODO figure out the point of this field. End: u7, IsStatement: bool, }; From 0d1ce1fb5fb4bcfff0dc24b567220af6ec6b93ab Mon Sep 17 00:00:00 2001 From: kristopher tate Date: Sun, 20 Jan 2019 16:57:13 +0900 Subject: [PATCH 4/6] src/analyze.cpp: return type entry for `ZigTypeIdPointer` if it points to `ZigTypeIdOpaque` ref: ziglang/zig#1883 --- src/analyze.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/analyze.cpp b/src/analyze.cpp index 00eb38de9e..15370983fc 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -5751,6 +5751,13 @@ void eval_min_max_value(CodeGen *g, ZigType *type_entry, ConstExprValue *const_v } void render_const_val_ptr(CodeGen *g, Buf *buf, ConstExprValue *const_val, ZigType *type_entry) { + assert(type_entry->id == ZigTypeIdPointer); + + if (type_entry->data.pointer.child_type->id == ZigTypeIdOpaque) { + buf_append_buf(buf, &type_entry->name); + return; + } + switch (const_val->data.x_ptr.special) { case ConstPtrSpecialInvalid: zig_unreachable(); From 1357bc7430e48137b249c6930b4f03c48efa0f33 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 20 Jan 2019 14:09:17 -0500 Subject: [PATCH 5/6] add test case for previous commit --- test/compile_errors.zig | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 880a96a322..d9603ba4ff 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1,6 +1,15 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add( + "compile log a pointer to an opaque value", + \\export fn entry() void { + \\ @compileLog(@ptrCast(*const c_void, &entry)); + \\} + , + ".tmp_source.zig:2:5: error: found compile log statement", + ); + cases.add( "duplicate boolean switch value", \\comptime { From 3bec3b9f9ba49bbc2e7244737c50bdbaa12a6b14 Mon Sep 17 00:00:00 2001 From: tharvik Date: Sat, 19 Jan 2019 16:47:33 +0100 Subject: [PATCH 6/6] llvm-config sanity check --- cmake/Findllvm.cmake | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cmake/Findllvm.cmake b/cmake/Findllvm.cmake index b847813682..70d50f9843 100644 --- a/cmake/Findllvm.cmake +++ b/cmake/Findllvm.cmake @@ -15,6 +15,15 @@ find_program(LLVM_CONFIG_EXE "c:/msys64/mingw64/bin" "C:/Libraries/llvm-7.0.0/bin") +execute_process( + COMMAND ${LLVM_CONFIG_EXE} --version + OUTPUT_VARIABLE LLVM_CONFIG_VERSION + OUTPUT_STRIP_TRAILING_WHITESPACE) + +if(LLVM_CONFIG_VERSION VERSION_LESS 7) + message(FATAL_ERROR "expected LLVM version >=7 but found ${LLVM_CONFIG_VERSION}") +endif() + if(NOT(CMAKE_BUILD_TYPE STREQUAL "Debug") OR ZIG_STATIC) execute_process( COMMAND ${LLVM_CONFIG_EXE} --libfiles --link-static