From 827fa4d837accc197df730f4a011c7c8a97202d1 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Mon, 14 Jun 2021 17:48:01 +0900 Subject: [PATCH 01/22] c++,wasi: enable libcxx build. Signed-off-by: Takeshi Yoneda --- src/libcxx.zig | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/libcxx.zig b/src/libcxx.zig index 16fad9f498..c2ac2ff32f 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -113,11 +113,13 @@ pub fn buildLibCXX(comp: *Compilation) !void { for (libcxx_files) |cxx_src| { var cflags = std.ArrayList([]const u8).init(arena); - if (target.os.tag == .windows) { - // Filesystem stuff isn't supported on Windows. + if (target.os.tag == .windows or target.os.tag == .wasi) { + // Filesystem stuff isn't supported on Windows and WASI. if (std.mem.startsWith(u8, cxx_src, "src/filesystem/")) continue; - } else { + } + + if (target.os.tag != .windows) { if (std.mem.startsWith(u8, cxx_src, "src/support/win32/")) continue; } @@ -133,7 +135,7 @@ pub fn buildLibCXX(comp: *Compilation) !void { try cflags.append("-fvisibility=hidden"); try cflags.append("-fvisibility-inlines-hidden"); - if (target.abi.isMusl()) { + if (target.abi.isMusl() or target.os.tag == .wasi) { try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); } @@ -252,7 +254,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void { try cflags.append("-fvisibility=hidden"); try cflags.append("-fvisibility-inlines-hidden"); - if (target.abi.isMusl()) { + if (target.abi.isMusl() or target.os.tag == .wasi) { try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); } From c708962386017da349736e25dc689c94b73df251 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Mon, 14 Jun 2021 23:39:39 +0900 Subject: [PATCH 02/22] link/wasm: link libcxx. Signed-off-by: Takeshi Yoneda --- src/link/Wasm.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 83d7eb0ecf..8c38f17826 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -716,6 +716,10 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { )); try argv.append(try comp.get_libc_crt_file(arena, "libc.a")); } + + if (self.base.options.link_libcpp) { + try argv.append(comp.libcxx_static_lib.?.full_object_path); + } } } From bc64664a0cb3dab4374e1053fb765872765f848c Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 15 Jun 2021 01:03:25 +0900 Subject: [PATCH 03/22] add various flags/options, and link libcxxabi. Signed-off-by: Takeshi Yoneda --- src/libcxx.zig | 41 +++++++++++++++++++++++++++++++---------- src/link/Wasm.zig | 1 + 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/libcxx.zig b/src/libcxx.zig index c2ac2ff32f..35cdb71789 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -114,7 +114,7 @@ pub fn buildLibCXX(comp: *Compilation) !void { var cflags = std.ArrayList([]const u8).init(arena); if (target.os.tag == .windows or target.os.tag == .wasi) { - // Filesystem stuff isn't supported on Windows and WASI. + // Filesystem stuff isn't supported on WASI and Windows. if (std.mem.startsWith(u8, cxx_src, "src/filesystem/")) continue; } @@ -131,14 +131,20 @@ pub fn buildLibCXX(comp: *Compilation) !void { try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); try cflags.append("-D_LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS"); - try cflags.append("-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS"); try cflags.append("-fvisibility=hidden"); try cflags.append("-fvisibility-inlines-hidden"); - if (target.abi.isMusl() or target.os.tag == .wasi) { + if (target.abi.isMusl()) { try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); } + if (target.os.tag == .wasi) { + try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); + try cflags.append("-D_LIBCPP_HAS_NO_THREADS"); + try cflags.append("-D_LIBCPP_ABI_VERSION=2"); + try cflags.append("-fno-exceptions"); + } + try cflags.append("-I"); try cflags.append(cxx_include_path); @@ -240,14 +246,29 @@ pub fn buildLibCXXABI(comp: *Compilation) !void { const cxxabi_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", "include" }); const cxx_include_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxx", "include" }); + var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena); + try c_source_files.ensureCapacity(libcxxabi_files.len); - var c_source_files: [libcxxabi_files.len]Compilation.CSourceFile = undefined; - for (libcxxabi_files) |cxxabi_src, i| { + for (libcxxabi_files) |cxxabi_src| { var cflags = std.ArrayList([]const u8).init(arena); - try cflags.append("-DHAVE___CXA_THREAD_ATEXIT_IMPL"); + if (target.os.tag == .wasi) { + if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_thread_atexit.cpp")) + continue; + + if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_exception.cpp")) + continue; + + if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_personality.cpp")) + continue; + + try cflags.append("-fno-exceptions"); + } else { + try cflags.append("-DHAVE___CXA_THREAD_ATEXIT_IMPL"); + try cflags.append("-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS"); + } + try cflags.append("-D_LIBCPP_DISABLE_EXTERN_TEMPLATE"); - try cflags.append("-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS"); try cflags.append("-D_LIBCXXABI_BUILDING_LIBRARY"); try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); @@ -272,10 +293,10 @@ pub fn buildLibCXXABI(comp: *Compilation) !void { try cflags.append("-funwind-tables"); try cflags.append("-std=c++11"); - c_source_files[i] = .{ + c_source_files.appendAssumeCapacity(.{ .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libcxxabi", cxxabi_src }), .extra_flags = cflags.items, - }; + }); } const sub_compilation = try Compilation.create(comp.gpa, .{ @@ -305,7 +326,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void { .is_native_os = comp.bin_file.options.is_native_os, .is_native_abi = comp.bin_file.options.is_native_abi, .self_exe_path = comp.self_exe_path, - .c_source_files = &c_source_files, + .c_source_files = c_source_files.items, .verbose_cc = comp.verbose_cc, .verbose_link = comp.bin_file.options.verbose_link, .verbose_air = comp.verbose_air, diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 8c38f17826..3fcdc04e71 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -719,6 +719,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation) !void { if (self.base.options.link_libcpp) { try argv.append(comp.libcxx_static_lib.?.full_object_path); + try argv.append(comp.libcxxabi_static_lib.?.full_object_path); } } } From 1795472eb71a52d37d73dcf4cac82af44f6ddc3c Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 15 Jun 2021 09:28:19 +0900 Subject: [PATCH 04/22] cleanup, and disable threading completely. Signed-off-by: Takeshi Yoneda --- src/libcxx.zig | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/libcxx.zig b/src/libcxx.zig index 35cdb71789..86d32f7526 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -139,9 +139,8 @@ pub fn buildLibCXX(comp: *Compilation) !void { } if (target.os.tag == .wasi) { - try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); + // WASI doesn't support thread yet. try cflags.append("-D_LIBCPP_HAS_NO_THREADS"); - try cflags.append("-D_LIBCPP_ABI_VERSION=2"); try cflags.append("-fno-exceptions"); } @@ -253,29 +252,27 @@ pub fn buildLibCXXABI(comp: *Compilation) !void { var cflags = std.ArrayList([]const u8).init(arena); if (target.os.tag == .wasi) { - if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_thread_atexit.cpp")) + // WASI doesn't support thread yet. + if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_thread_atexit.cpp") or + std.mem.startsWith(u8, cxxabi_src, "src/cxa_exception.cpp") or + std.mem.startsWith(u8, cxxabi_src, "src/cxa_personality.cpp")) continue; - - if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_exception.cpp")) - continue; - - if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_personality.cpp")) - continue; - + try cflags.append("-D_LIBCXXABI_HAS_NO_THREADS"); + // Also, exception is not supported yet. try cflags.append("-fno-exceptions"); } else { try cflags.append("-DHAVE___CXA_THREAD_ATEXIT_IMPL"); - try cflags.append("-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS"); } try cflags.append("-D_LIBCPP_DISABLE_EXTERN_TEMPLATE"); + try cflags.append("-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS"); try cflags.append("-D_LIBCXXABI_BUILDING_LIBRARY"); try cflags.append("-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS"); try cflags.append("-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS"); try cflags.append("-fvisibility=hidden"); try cflags.append("-fvisibility-inlines-hidden"); - if (target.abi.isMusl() or target.os.tag == .wasi) { + if (target.abi.isMusl()) { try cflags.append("-D_LIBCPP_HAS_MUSL_LIBC"); } From dc7d354ee4c34b9f2f1b2b34fc755d3740878044 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 15 Jun 2021 09:32:48 +0900 Subject: [PATCH 05/22] Add a missing comment. Signed-off-by: Takeshi Yoneda --- src/libcxx.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libcxx.zig b/src/libcxx.zig index 86d32f7526..c6702fd013 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -141,6 +141,7 @@ pub fn buildLibCXX(comp: *Compilation) !void { if (target.os.tag == .wasi) { // WASI doesn't support thread yet. try cflags.append("-D_LIBCPP_HAS_NO_THREADS"); + // Also, exception is not supported yet. try cflags.append("-fno-exceptions"); } From 0063f642d11fa82d4776634df166c9c9cd1f26e1 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Tue, 15 Jun 2021 11:50:13 +0900 Subject: [PATCH 06/22] fix comments. Signed-off-by: Takeshi Yoneda --- src/libcxx.zig | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libcxx.zig b/src/libcxx.zig index c6702fd013..010a8b9ceb 100644 --- a/src/libcxx.zig +++ b/src/libcxx.zig @@ -139,9 +139,8 @@ pub fn buildLibCXX(comp: *Compilation) !void { } if (target.os.tag == .wasi) { - // WASI doesn't support thread yet. + // WASI doesn't support thread and exception yet. try cflags.append("-D_LIBCPP_HAS_NO_THREADS"); - // Also, exception is not supported yet. try cflags.append("-fno-exceptions"); } @@ -253,13 +252,12 @@ pub fn buildLibCXXABI(comp: *Compilation) !void { var cflags = std.ArrayList([]const u8).init(arena); if (target.os.tag == .wasi) { - // WASI doesn't support thread yet. + // WASI doesn't support thread and exception yet. if (std.mem.startsWith(u8, cxxabi_src, "src/cxa_thread_atexit.cpp") or std.mem.startsWith(u8, cxxabi_src, "src/cxa_exception.cpp") or std.mem.startsWith(u8, cxxabi_src, "src/cxa_personality.cpp")) continue; try cflags.append("-D_LIBCXXABI_HAS_NO_THREADS"); - // Also, exception is not supported yet. try cflags.append("-fno-exceptions"); } else { try cflags.append("-DHAVE___CXA_THREAD_ATEXIT_IMPL"); From 7611b80f432be29f3fa83ea1891002fd20f0a07f Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 14 Jun 2021 04:45:44 +0000 Subject: [PATCH 07/22] std: fix auto hash of tagged union with void field --- lib/std/hash/auto_hash.zig | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/std/hash/auto_hash.zig b/lib/std/hash/auto_hash.zig index fe197b7a71..f9bb0ecae1 100644 --- a/lib/std/hash/auto_hash.zig +++ b/lib/std/hash/auto_hash.zig @@ -146,10 +146,12 @@ pub fn hash(hasher: anytype, key: anytype, comptime strat: HashStrategy) void { .Union => |info| { if (info.tag_type) |tag_type| { const tag = meta.activeTag(key); - const s = hash(hasher, tag, strat); + hash(hasher, tag, strat); inline for (info.fields) |field| { if (@field(tag_type, field.name) == tag) { - hash(hasher, @field(key, field.name), strat); + if (field.field_type != void) { + hash(hasher, @field(key, field.name), strat); + } // TODO use a labelled break when it does not crash the compiler. cf #2908 // break :blk; return; @@ -385,17 +387,23 @@ test "testHash union" { A: u32, B: bool, C: u32, + D: void, }; const a = Foo{ .A = 18 }; var b = Foo{ .B = true }; const c = Foo{ .C = 18 }; + const d: Foo = .D; try testing.expect(testHash(a) == testHash(a)); try testing.expect(testHash(a) != testHash(b)); try testing.expect(testHash(a) != testHash(c)); + try testing.expect(testHash(a) != testHash(d)); b = Foo{ .A = 18 }; try testing.expect(testHash(a) == testHash(b)); + + b = .D; + try testing.expect(testHash(d) == testHash(b)); } test "testHash vector" { From 71e99c869012194cc4a23c5842b746533c1ae2bb Mon Sep 17 00:00:00 2001 From: Samadi van Koten Date: Mon, 7 Jun 2021 22:06:45 +0100 Subject: [PATCH 08/22] Add std.os.dup() --- lib/std/os.zig | 10 ++++++++++ lib/std/os/linux.zig | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/lib/std/os.zig b/lib/std/os.zig index 072771b000..c3fb42f782 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -1282,6 +1282,16 @@ pub fn openatW(dir_fd: fd_t, file_path_w: []const u16, flags: u32, mode: mode_t) }; } +pub fn dup(old_fd: fd_t) !fd_t { + const rc = system.dup(old_fd); + return switch (errno(rc)) { + 0 => return @intCast(fd_t, rc), + EMFILE => error.ProcessFdQuotaExceeded, + EBADF => unreachable, // invalid file descriptor + else => |err| return unexpectedErrno(err), + }; +} + pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { while (true) { switch (errno(system.dup2(old_fd, new_fd))) { diff --git a/lib/std/os/linux.zig b/lib/std/os/linux.zig index dfc17d730b..4b3454de52 100644 --- a/lib/std/os/linux.zig +++ b/lib/std/os/linux.zig @@ -94,6 +94,10 @@ pub fn getErrno(r: usize) u12 { return if (signed_r > -4096 and signed_r < 0) @intCast(u12, -signed_r) else 0; } +pub fn dup(old: i32) usize { + return syscall1(.dup, @bitCast(usize, @as(isize, old))); +} + pub fn dup2(old: i32, new: i32) usize { if (@hasField(SYS, "dup2")) { return syscall2(.dup2, @bitCast(usize, @as(isize, old)), @bitCast(usize, @as(isize, new))); From eb7cfe9de6fcc87d84227c945f397a538303e55e Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 12 Jun 2021 20:32:11 +0300 Subject: [PATCH 09/22] add a test for dup and dup2 --- lib/std/os/test.zig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig index 7d67f4f85b..f474c54256 100644 --- a/lib/std/os/test.zig +++ b/lib/std/os/test.zig @@ -756,3 +756,32 @@ test "sigaction" { os.sigaction(os.SIGUSR1, null, &old_sa); try testing.expectEqual(os.SIG_DFL, old_sa.handler.sigaction); } + +test "dup & dup2" { + if (native_os != .linux) return error.SkipZigTest; + + var tmp = tmpDir(.{}); + defer tmp.cleanup(); + + { + var file = try tmp.dir.createFile("os_dup_test", .{}); + defer file.close(); + + var duped = std.fs.File{ .handle = try std.os.dup(file.handle) }; + defer duped.close(); + try duped.writeAll("dup"); + + // Tests aren't run in parallel so using the next fd shouldn't be an issue. + const new_fd = duped.handle + 1; + try std.os.dup2(file.handle, new_fd); + var dup2ed = std.fs.File{ .handle = new_fd }; + defer dup2ed.close(); + try dup2ed.writeAll("dup2"); + } + + var file = try tmp.dir.openFile("os_dup_test", .{}); + defer file.close(); + + var buf: [7]u8 = undefined; + try testing.expectEqualStrings("dupdup2", buf[0..try file.readAll(&buf)]); +} From e3fa18242be2594d98757106fd586d7eada65c97 Mon Sep 17 00:00:00 2001 From: xackus <14938807+xackus@users.noreply.github.com> Date: Mon, 14 Jun 2021 10:14:11 +0200 Subject: [PATCH 10/22] translate-c: remove old code i forgot in last pr --- src/translate_c.zig | 63 +++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/src/translate_c.zig b/src/translate_c.zig index 5e1696d133..2863377aad 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -5331,7 +5331,7 @@ fn parseCPrimaryExprInner(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!N // for handling type macros (EVIL) // TODO maybe detect and treat type macros as typedefs in parseCSpecifierQualifierList? m.i -= 1; - if (try parseCTypeName(c, m, scope)) |type_name| { + if (try parseCTypeName(c, m, scope, true)) |type_name| { return type_name; } try m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(tok)}); @@ -5543,33 +5543,9 @@ fn parseCMulExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { while (true) { switch (m.next().?) { .Asterisk => { - const next = m.peek().?; - if (next == .RParen or next == .Nl or next == .Eof) { - // type *) - - // last token of `node` - const prev_id = m.list[m.i - 1].id; - - if (prev_id == .Keyword_void) { - const ptr = try Tag.single_pointer.create(c.arena, .{ - .is_const = false, - .is_volatile = false, - .elem_type = node, - }); - return Tag.optional_type.create(c.arena, ptr); - } else { - return Tag.c_pointer.create(c.arena, .{ - .is_const = false, - .is_volatile = false, - .elem_type = node, - }); - } - } else { - // expr * expr - const lhs = try macroBoolToInt(c, node); - const rhs = try macroBoolToInt(c, try parseCCastExpr(c, m, scope)); - node = try Tag.mul.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); - } + const lhs = try macroBoolToInt(c, node); + const rhs = try macroBoolToInt(c, try parseCCastExpr(c, m, scope)); + node = try Tag.mul.create(c.arena, .{ .lhs = lhs, .rhs = rhs }); }, .Slash => { const lhs = try macroBoolToInt(c, node); @@ -5592,7 +5568,7 @@ fn parseCMulExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { fn parseCCastExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { switch (m.next().?) { .LParen => { - if (try parseCTypeName(c, m, scope)) |type_name| { + if (try parseCTypeName(c, m, scope, true)) |type_name| { if (m.next().? != .RParen) { try m.fail(c, "unable to translate C expr: expected ')'", .{}); return error.ParseError; @@ -5611,19 +5587,21 @@ fn parseCCastExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { return parseCUnaryExpr(c, m, scope); } -fn parseCTypeName(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!?Node { - if (try parseCSpecifierQualifierList(c, m, scope)) |node| { +// allow_fail is set when unsure if we are parsing a type-name +fn parseCTypeName(c: *Context, m: *MacroCtx, scope: *Scope, allow_fail: bool) ParseError!?Node { + if (try parseCSpecifierQualifierList(c, m, scope, allow_fail)) |node| { return try parseCAbstractDeclarator(c, m, scope, node); } else { return null; } } -fn parseCSpecifierQualifierList(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!?Node { - switch (m.next().?) { +fn parseCSpecifierQualifierList(c: *Context, m: *MacroCtx, scope: *Scope, allow_fail: bool) ParseError!?Node { + const tok = m.next().?; + switch (tok) { .Identifier => { const mangled_name = scope.getAlias(m.slice()); - if (c.typedefs.contains(mangled_name)) { + if (!allow_fail or c.typedefs.contains(mangled_name)) { return try Tag.identifier.create(c.arena, builtin_typedef_map.get(mangled_name) orelse mangled_name); } }, @@ -5657,8 +5635,13 @@ fn parseCSpecifierQualifierList(c: *Context, m: *MacroCtx, scope: *Scope) ParseE else => {}, } - m.i -= 1; - return null; + if (allow_fail) { + m.i -= 1; + return null; + } else { + try m.fail(c, "unable to translate C expr: unexpected token .{s}", .{@tagName(tok)}); + return error.ParseError; + } } fn parseCNumericType(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { @@ -5934,9 +5917,7 @@ fn parseCUnaryExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { .Keyword_sizeof => { const operand = if (m.peek().? == .LParen) blk: { _ = m.next(); - // C grammar says this should be 'type-name' but we have to - // use parseCMulExpr to correctly handle pointer types. - const inner = try parseCMulExpr(c, m, scope); + const inner = (try parseCTypeName(c, m, scope, false)).?; if (m.next().? != .RParen) { try m.fail(c, "unable to translate C expr: expected ')'", .{}); return error.ParseError; @@ -5953,9 +5934,7 @@ fn parseCUnaryExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { try m.fail(c, "unable to translate C expr: expected '('", .{}); return error.ParseError; } - // C grammar says this should be 'type-name' but we have to - // use parseCMulExpr to correctly handle pointer types. - const operand = try parseCMulExpr(c, m, scope); + const operand = (try parseCTypeName(c, m, scope, false)).?; if (m.next().? != .RParen) { try m.fail(c, "unable to translate C expr: expected ')'", .{}); return error.ParseError; From 5df5937bd23fdad355f7e52a3aac1a7e1f4a7192 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Sat, 12 Jun 2021 11:23:04 +0200 Subject: [PATCH 11/22] specify the output lib, exe and include paths with flags --- lib/std/build.zig | 34 ++++++++++++++++++++++++++++---- lib/std/special/build_runner.zig | 24 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index 2d43422997..bd3bfbbb98 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -124,6 +124,12 @@ pub const Builder = struct { description: []const u8, }; + pub const DirList = struct { + lib_dir: ?[]const u8 = null, + exe_dir: ?[]const u8 = null, + include_dir: ?[]const u8 = null, + }; + pub fn create( allocator: *Allocator, zig_exe: []const u8, @@ -192,7 +198,7 @@ pub const Builder = struct { } /// This function is intended to be called by std/special/build_runner.zig, not a build.zig file. - pub fn resolveInstallPrefix(self: *Builder, install_prefix: ?[]const u8) void { + pub fn resolveInstallPrefix(self: *Builder, install_prefix: ?[]const u8, dir_list: DirList) void { if (self.dest_dir) |dest_dir| { self.install_prefix = install_prefix orelse "/usr"; self.install_path = fs.path.join(self.allocator, &[_][]const u8{ dest_dir, self.install_prefix }) catch unreachable; @@ -201,9 +207,29 @@ pub const Builder = struct { (fs.path.join(self.allocator, &[_][]const u8{ self.build_root, "zig-out" }) catch unreachable); self.install_path = self.install_prefix; } - self.lib_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "lib" }) catch unreachable; - self.exe_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "bin" }) catch unreachable; - self.h_dir = fs.path.join(self.allocator, &[_][]const u8{ self.install_path, "include" }) catch unreachable; + + var lib_list = [_][]const u8{ self.install_path, "lib" }; + var exe_list = [_][]const u8{ self.install_path, "bin" }; + var h_list = [_][]const u8{ self.install_path, "include" }; + + if (dir_list.lib_dir) |dir| { + if (std.fs.path.isAbsolute(dir)) lib_list[0] = self.dest_dir orelse ""; + lib_list[1] = dir; + } + + if (dir_list.exe_dir) |dir| { + if (std.fs.path.isAbsolute(dir)) exe_list[0] = self.dest_dir orelse ""; + exe_list[1] = dir; + } + + if (dir_list.include_dir) |dir| { + if (std.fs.path.isAbsolute(dir)) h_list[0] = self.dest_dir orelse ""; + h_list[1] = dir; + } + + self.lib_dir = fs.path.join(self.allocator, &lib_list) catch unreachable; + self.exe_dir = fs.path.join(self.allocator, &exe_list) catch unreachable; + self.h_dir = fs.path.join(self.allocator, &h_list) catch unreachable; } fn convertOptionalPathToFileSource(path: ?[]const u8) ?FileSource { diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index eda98408a1..b6fe55f725 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -61,6 +61,8 @@ pub fn main() !void { const stdout_stream = io.getStdOut().writer(); var install_prefix: ?[]const u8 = null; + var dir_list = Builder.DirList{}; + while (nextArg(args, &arg_idx)) |arg| { if (mem.startsWith(u8, arg, "-D")) { const option_contents = arg[2..]; @@ -87,6 +89,21 @@ pub fn main() !void { warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); }; + } else if (mem.eql(u8, arg, "--lib-dir")) { + dir_list.lib_dir = nextArg(args, &arg_idx) orelse { + warn("Expected argument after {s}\n\n", .{arg}); + return usageAndErr(builder, false, stderr_stream); + }; + } else if (mem.eql(u8, arg, "--exe-dir")) { + dir_list.exe_dir = nextArg(args, &arg_idx) orelse { + warn("Expected argument after {s}\n\n", .{arg}); + return usageAndErr(builder, false, stderr_stream); + }; + } else if (mem.eql(u8, arg, "--include-dir")) { + dir_list.include_dir = nextArg(args, &arg_idx) orelse { + warn("Expected argument after {s}\n\n", .{arg}); + return usageAndErr(builder, false, stderr_stream); + }; } else if (mem.eql(u8, arg, "--search-prefix")) { const search_prefix = nextArg(args, &arg_idx) orelse { warn("Expected argument after --search-prefix\n\n", .{}); @@ -135,7 +152,7 @@ pub fn main() !void { } } - builder.resolveInstallPrefix(install_prefix); + builder.resolveInstallPrefix(install_prefix, dir_list); try runBuild(builder); if (builder.validateUserInputDidItFail()) @@ -163,7 +180,7 @@ fn runBuild(builder: *Builder) anyerror!void { fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void { // run the build script to collect the options if (!already_ran_build) { - builder.resolveInstallPrefix(null); + builder.resolveInstallPrefix(null, .{}); try runBuild(builder); } @@ -189,6 +206,9 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void \\ -h, --help Print this help and exit \\ --verbose Print commands before executing them \\ -p, --prefix [path] Override default install prefix + \\ --lib-dir [path] Override default library directory path + \\ --exe-dir [path] Override default executable directory path + \\ --include-dir [path] Override default include directory path \\ --search-prefix [path] Add a path to look for binaries, libraries, headers \\ --color [auto|off|on] Enable or disable colored error messages \\ From c434a9a573292432b91974fc819bcef6a04eba3e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 14 Jun 2021 11:33:27 -0700 Subject: [PATCH 12/22] CLI: rename --override-lib-dir to --zig-lib-dir This breaking change disambiguates between overriding the lib dir when performing an installation with the Zig Build System, and overriding the lib dir that the Zig installation itself uses. --- CMakeLists.txt | 4 ++-- ci/azure/linux_script | 2 +- ci/azure/macos_arm64_script | 2 +- lib/std/build.zig | 4 ++-- lib/std/crypto/benchmark.zig | 2 +- lib/std/hash/benchmark.zig | 2 +- lib/std/os.zig | 2 +- lib/std/special/build_runner.zig | 6 +++--- src/main.zig | 6 +++--- src/stage1/zig0.cpp | 4 ++-- 10 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4c5aab09ef..a2bf856b9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -782,7 +782,7 @@ set(BUILD_ZIG1_ARGS -target "${ZIG_TARGET_TRIPLE}" "-mcpu=${ZIG_TARGET_MCPU}" --name zig1 - --override-lib-dir "${CMAKE_SOURCE_DIR}/lib" + --zig-lib-dir "${CMAKE_SOURCE_DIR}/lib" "-femit-bin=${ZIG1_OBJECT}" "${ZIG1_RELEASE_ARG}" "${ZIG1_SINGLE_THREADED_ARG}" @@ -836,7 +836,7 @@ set(ZIG_SKIP_INSTALL_LIB_FILES off CACHE BOOL if(NOT ZIG_SKIP_INSTALL_LIB_FILES) set(ZIG_INSTALL_ARGS "build" - --override-lib-dir "${CMAKE_SOURCE_DIR}/lib" + --zig-lib-dir "${CMAKE_SOURCE_DIR}/lib" "-Dlib-files-only" --prefix "${CMAKE_INSTALL_PREFIX}" "-Dconfig_h=${ZIG_CONFIG_H_OUT}" diff --git a/ci/azure/linux_script b/ci/azure/linux_script index 52880f40aa..462457a932 100755 --- a/ci/azure/linux_script +++ b/ci/azure/linux_script @@ -79,7 +79,7 @@ if [ "${BUILD_REASON}" != "PullRequest" ]; then # Produce the experimental std lib documentation. mkdir -p release/docs/std release/bin/zig test ../lib/std/std.zig \ - --override-lib-dir ../lib \ + --zig-lib-dir ../lib \ -femit-docs=release/docs/std \ -fno-emit-bin diff --git a/ci/azure/macos_arm64_script b/ci/azure/macos_arm64_script index 2caf1c94db..d3eb7bdeaa 100755 --- a/ci/azure/macos_arm64_script +++ b/ci/azure/macos_arm64_script @@ -94,7 +94,7 @@ if [ "${BUILD_REASON}" != "PullRequest" ]; then # Produce the experimental std lib documentation. mkdir -p release/docs/std $ZIG test ../lib/std/std.zig \ - --override-lib-dir ../lib \ + --zig-lib-dir ../lib \ -femit-docs=release/docs/std \ -fno-emit-bin diff --git a/lib/std/build.zig b/lib/std/build.zig index bd3bfbbb98..ed9e5e4ece 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -2716,10 +2716,10 @@ pub const LibExeObjStep = struct { } if (self.override_lib_dir) |dir| { - try zig_args.append("--override-lib-dir"); + try zig_args.append("--zig-lib-dir"); try zig_args.append(builder.pathFromRoot(dir)); } else if (self.builder.override_lib_dir) |dir| { - try zig_args.append("--override-lib-dir"); + try zig_args.append("--zig-lib-dir"); try zig_args.append(builder.pathFromRoot(dir)); } diff --git a/lib/std/crypto/benchmark.zig b/lib/std/crypto/benchmark.zig index 49d5b15820..e8e50b95ea 100644 --- a/lib/std/crypto/benchmark.zig +++ b/lib/std/crypto/benchmark.zig @@ -3,7 +3,7 @@ // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. -// zig run benchmark.zig --release-fast --override-lib-dir .. +// zig run benchmark.zig --release-fast --zig-lib-dir .. const std = @import("../std.zig"); const builtin = std.builtin; diff --git a/lib/std/hash/benchmark.zig b/lib/std/hash/benchmark.zig index 4762068c0e..5a86afa056 100644 --- a/lib/std/hash/benchmark.zig +++ b/lib/std/hash/benchmark.zig @@ -3,7 +3,7 @@ // This file is part of [zig](https://ziglang.org/), which is MIT licensed. // The MIT license requires this copyright notice to be included in all copies // and substantial portions of the software. -// zig run benchmark.zig --release-fast --override-lib-dir .. +// zig run benchmark.zig --release-fast --zig-lib-dir .. const builtin = std.builtin; const std = @import("std"); diff --git a/lib/std/os.zig b/lib/std/os.zig index c3fb42f782..dce1f044ca 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -41,7 +41,7 @@ pub const wasi = @import("os/wasi.zig"); pub const windows = @import("os/windows.zig"); comptime { - assert(@import("std") == std); // std lib tests require --override-lib-dir + assert(@import("std") == std); // std lib tests require --zig-lib-dir } test { diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index b6fe55f725..1855d3652d 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -119,9 +119,9 @@ pub fn main() !void { warn("expected [auto|on|off] after --color, found '{s}'", .{next_arg}); return usageAndErr(builder, false, stderr_stream); }; - } else if (mem.eql(u8, arg, "--override-lib-dir")) { + } else if (mem.eql(u8, arg, "--zig-lib-dir")) { builder.override_lib_dir = nextArg(args, &arg_idx) orelse { - warn("Expected argument after --override-lib-dir\n\n", .{}); + warn("Expected argument after --zig-lib-dir\n\n", .{}); return usageAndErr(builder, false, stderr_stream); }; } else if (mem.eql(u8, arg, "--verbose-tokenize")) { @@ -234,7 +234,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void \\Advanced Options: \\ --build-file [file] Override path to build.zig \\ --cache-dir [path] Override path to zig cache directory - \\ --override-lib-dir [arg] Override path to Zig lib directory + \\ --zig-lib-dir [arg] Override path to Zig lib directory \\ --verbose-tokenize Enable compiler debug output for tokenization \\ --verbose-ast Enable compiler debug output for parsing into an AST \\ --verbose-link Enable compiler debug output for linking diff --git a/src/main.zig b/src/main.zig index 512828bd77..ed90f7472d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -310,7 +310,7 @@ const usage_build_generic = \\ --show-builtin Output the source of @import("builtin") then exit \\ --cache-dir [path] Override the local cache directory \\ --global-cache-dir [path] Override the global cache directory - \\ --override-lib-dir [path] Override path to Zig installation lib directory + \\ --zig-lib-dir [path] Override path to Zig installation lib directory \\ --enable-cache Output to cache directory; print path to stdout \\ \\Compile Options: @@ -888,7 +888,7 @@ fn buildOutputType( if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); i += 1; override_global_cache_dir = args[i]; - } else if (mem.eql(u8, arg, "--override-lib-dir")) { + } else if (mem.eql(u8, arg, "--zig-lib-dir")) { if (i + 1 >= args.len) fatal("expected parameter after {s}", .{arg}); i += 1; override_lib_dir = args[i]; @@ -2664,7 +2664,7 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v i += 1; build_file = args[i]; continue; - } else if (mem.eql(u8, arg, "--override-lib-dir")) { + } else if (mem.eql(u8, arg, "--zig-lib-dir")) { if (i + 1 >= args.len) fatal("expected argument after '{s}'", .{arg}); i += 1; override_lib_dir = args[i]; diff --git a/src/stage1/zig0.cpp b/src/stage1/zig0.cpp index 642fb56503..48752adad7 100644 --- a/src/stage1/zig0.cpp +++ b/src/stage1/zig0.cpp @@ -351,7 +351,7 @@ int main(int argc, char **argv) { out_name = argv[i]; } else if (strcmp(arg, "--dynamic-linker") == 0) { dynamic_linker = argv[i]; - } else if (strcmp(arg, "--override-lib-dir") == 0) { + } else if (strcmp(arg, "--zig-lib-dir") == 0) { override_lib_dir = argv[i]; } else if (strcmp(arg, "--library") == 0 || strcmp(arg, "-l") == 0) { if (strcmp(argv[i], "c") == 0) { @@ -433,7 +433,7 @@ int main(int argc, char **argv) { } if (override_lib_dir == nullptr) { - fprintf(stderr, "missing --override-lib-dir\n"); + fprintf(stderr, "missing --zig-lib-dir\n"); return print_error_usage(arg0); } From d6f27725833cfe66f83b31cfdb7557eefdba3d13 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 14 Jun 2021 11:56:58 -0700 Subject: [PATCH 13/22] zig build: rename --lib-dir, --include-dir, --exe-dir To --prefix-lib-dir, --prefix-include-dir, --prefix-exe-dir, respectively. --- lib/std/special/build_runner.zig | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 1855d3652d..06ac832270 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -89,17 +89,17 @@ pub fn main() !void { warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); }; - } else if (mem.eql(u8, arg, "--lib-dir")) { + } else if (mem.eql(u8, arg, "--prefix-lib-dir")) { dir_list.lib_dir = nextArg(args, &arg_idx) orelse { warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); }; - } else if (mem.eql(u8, arg, "--exe-dir")) { + } else if (mem.eql(u8, arg, "--prefix-exe-dir")) { dir_list.exe_dir = nextArg(args, &arg_idx) orelse { warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); }; - } else if (mem.eql(u8, arg, "--include-dir")) { + } else if (mem.eql(u8, arg, "--prefix-include-dir")) { dir_list.include_dir = nextArg(args, &arg_idx) orelse { warn("Expected argument after {s}\n\n", .{arg}); return usageAndErr(builder, false, stderr_stream); @@ -203,13 +203,15 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void try out_stream.writeAll( \\ \\General Options: + \\ -p, --prefix [path] Override default install prefix + \\ --prefix-lib-dir [path] Override default library directory path + \\ --prefix-exe-dir [path] Override default executable directory path + \\ --prefix-include-dir [path] Override default include directory path + \\ + \\ --search-prefix [path] Add a path to look for binaries, libraries, headers + \\ \\ -h, --help Print this help and exit \\ --verbose Print commands before executing them - \\ -p, --prefix [path] Override default install prefix - \\ --lib-dir [path] Override default library directory path - \\ --exe-dir [path] Override default executable directory path - \\ --include-dir [path] Override default include directory path - \\ --search-prefix [path] Add a path to look for binaries, libraries, headers \\ --color [auto|off|on] Enable or disable colored error messages \\ \\Project-Specific Options: From 6fad1d48e4c95295a3f0e7b613306efef666e084 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 14 Jun 2021 12:03:10 -0700 Subject: [PATCH 14/22] ci: azure: update to newer msys2 release --- ci/azure/pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/azure/pipelines.yml b/ci/azure/pipelines.yml index ced8ed45a0..755020a6f4 100644 --- a/ci/azure/pipelines.yml +++ b/ci/azure/pipelines.yml @@ -38,7 +38,7 @@ jobs: timeoutInMinutes: 360 steps: - powershell: | - (New-Object Net.WebClient).DownloadFile("https://github.com/msys2/msys2-installer/releases/download/2021-02-28/msys2-base-x86_64-20210228.sfx.exe", "sfx.exe") + (New-Object Net.WebClient).DownloadFile("https://github.com/msys2/msys2-installer/releases/download/2021-06-04/msys2-base-x86_64-20210604.sfx.exe", "sfx.exe") .\sfx.exe -y -o\ del sfx.exe displayName: Download/Extract/Install MSYS2 From 96d3c4f54fb4d80a44d9319c0054b45f4b856cad Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 14 Jun 2021 12:56:53 +0200 Subject: [PATCH 15/22] zld: clean up parsing section by type and attrs --- src/link/MachO/Object.zig | 37 +++++ src/link/MachO/Zld.zig | 337 ++++++++++++++++++++++++-------------- 2 files changed, 248 insertions(+), 126 deletions(-) diff --git a/src/link/MachO/Object.zig b/src/link/MachO/Object.zig index 4501d63fff..c4a044b446 100644 --- a/src/link/MachO/Object.zig +++ b/src/link/MachO/Object.zig @@ -70,6 +70,43 @@ pub const Section = struct { allocator.free(relocs); } } + + pub fn segname(self: Section) []const u8 { + return parseName(&self.inner.segname); + } + + pub fn sectname(self: Section) []const u8 { + return parseName(&self.inner.sectname); + } + + pub fn flags(self: Section) u32 { + return self.inner.flags; + } + + pub fn sectionType(self: Section) u8 { + return @truncate(u8, self.flags() & 0xff); + } + + pub fn sectionAttrs(self: Section) u32 { + return self.flags() & 0xffffff00; + } + + pub fn isCode(self: Section) bool { + const attr = self.sectionAttrs(); + return attr & macho.S_ATTR_PURE_INSTRUCTIONS != 0 and attr & macho.S_ATTR_SOME_INSTRUCTIONS != 0; + } + + pub fn isDebug(self: Section) bool { + return self.sectionAttrs() & macho.S_ATTR_DEBUG != 0; + } + + pub fn dontDeadStrip(self: Section) bool { + return self.sectionAttrs() & macho.S_ATTR_NO_DEAD_STRIP != 0; + } + + pub fn dontDeadStripIfReferencesLive(self: Section) bool { + return self.sectionAttrs() & macho.S_ATTR_LIVE_SUPPORT != 0; + } }; const DebugInfo = struct { diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index 824ea1bb56..70f219bafd 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -409,7 +409,7 @@ fn mapAndUpdateSections( const offset = mem.alignForwardGeneric(u64, target_sect.size, alignment); const size = mem.alignForwardGeneric(u64, source_sect.inner.size, alignment); - log.debug("{s}: '{s},{s}' mapped to '{s},{s}' from 0x{x} to 0x{x}", .{ + log.warn("{s}: '{s},{s}' mapped to '{s},{s}' from 0x{x} to 0x{x}", .{ object.name.?, parseName(&source_sect.inner.segname), parseName(&source_sect.inner.sectname), @@ -435,19 +435,17 @@ fn updateMetadata(self: *Zld) !void { const data_seg = &self.load_commands.items[self.data_segment_cmd_index.?].Segment; // Create missing metadata - for (object_seg.sections.items) |source_sect, sect_id| { - if (sect_id == object.text_section_index.?) continue; - const segname = parseName(&source_sect.segname); - const sectname = parseName(&source_sect.sectname); - const flags = source_sect.flags; + for (object.sections.items) |sect, sect_id| { + const segname = sect.segname(); + const sectname = sect.sectname(); - switch (flags) { - macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS => { - if (self.text_section_index != null) continue; + switch (sect.sectionType()) { + macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS, macho.S_LITERAL_POINTERS => { + if (self.text_const_section_index != null) continue; - self.text_section_index = @intCast(u16, text_seg.sections.items.len); + self.text_const_section_index = @intCast(u16, text_seg.sections.items.len); try text_seg.addSection(self.allocator, .{ - .sectname = makeStaticString("__text"), + .sectname = makeStaticString("__const"), .segname = makeStaticString("__TEXT"), .addr = 0, .size = 0, @@ -455,70 +453,12 @@ fn updateMetadata(self: *Zld) !void { .@"align" = 0, .reloff = 0, .nreloc = 0, - .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, + .flags = macho.S_REGULAR, .reserved1 = 0, .reserved2 = 0, .reserved3 = 0, }); - }, - macho.S_REGULAR, macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => { - if (mem.eql(u8, segname, "__TEXT")) { - if (mem.eql(u8, sectname, "__ustring")) { - if (self.ustring_section_index != null) continue; - - self.ustring_section_index = @intCast(u16, text_seg.sections.items.len); - try text_seg.addSection(self.allocator, .{ - .sectname = makeStaticString("__ustring"), - .segname = makeStaticString("__TEXT"), - .addr = 0, - .size = 0, - .offset = 0, - .@"align" = 0, - .reloff = 0, - .nreloc = 0, - .flags = macho.S_REGULAR, - .reserved1 = 0, - .reserved2 = 0, - .reserved3 = 0, - }); - } else { - if (self.text_const_section_index != null) continue; - - self.text_const_section_index = @intCast(u16, text_seg.sections.items.len); - try text_seg.addSection(self.allocator, .{ - .sectname = makeStaticString("__const"), - .segname = makeStaticString("__TEXT"), - .addr = 0, - .size = 0, - .offset = 0, - .@"align" = 0, - .reloff = 0, - .nreloc = 0, - .flags = macho.S_REGULAR, - .reserved1 = 0, - .reserved2 = 0, - .reserved3 = 0, - }); - } - } else if (mem.eql(u8, segname, "__DATA") or mem.eql(u8, segname, "__DATA_CONST")) { - if (self.data_const_section_index != null) continue; - - self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len); - try data_const_seg.addSection(self.allocator, .{ - .sectname = makeStaticString("__const"), - .segname = makeStaticString("__DATA_CONST"), - .addr = 0, - .size = 0, - .offset = 0, - .@"align" = 0, - .reloff = 0, - .nreloc = 0, - .flags = macho.S_REGULAR, - .reserved1 = 0, - .reserved2 = 0, - .reserved3 = 0, - }); - } + continue; }, macho.S_CSTRING_LITERALS => { if (self.cstring_section_index != null) continue; @@ -538,6 +478,7 @@ fn updateMetadata(self: *Zld) !void { .reserved2 = 0, .reserved3 = 0, }); + continue; }, macho.S_MOD_INIT_FUNC_POINTERS => { if (self.mod_init_func_section_index != null) continue; @@ -557,6 +498,7 @@ fn updateMetadata(self: *Zld) !void { .reserved2 = 0, .reserved3 = 0, }); + continue; }, macho.S_MOD_TERM_FUNC_POINTERS => { if (self.mod_term_func_section_index != null) continue; @@ -576,6 +518,7 @@ fn updateMetadata(self: *Zld) !void { .reserved2 = 0, .reserved3 = 0, }); + continue; }, macho.S_ZEROFILL => { if (mem.eql(u8, sectname, "__common")) { @@ -615,6 +558,7 @@ fn updateMetadata(self: *Zld) !void { .reserved3 = 0, }); } + continue; }, macho.S_THREAD_LOCAL_VARIABLES => { if (self.tlv_section_index != null) continue; @@ -634,6 +578,7 @@ fn updateMetadata(self: *Zld) !void { .reserved2 = 0, .reserved3 = 0, }); + continue; }, macho.S_THREAD_LOCAL_REGULAR => { if (self.tlv_data_section_index != null) continue; @@ -653,6 +598,7 @@ fn updateMetadata(self: *Zld) !void { .reserved2 = 0, .reserved3 = 0, }); + continue; }, macho.S_THREAD_LOCAL_ZEROFILL => { if (self.tlv_bss_section_index != null) continue; @@ -672,58 +618,185 @@ fn updateMetadata(self: *Zld) !void { .reserved2 = 0, .reserved3 = 0, }); - }, - macho.S_COALESCED | - macho.S_ATTR_NO_TOC | - macho.S_ATTR_STRIP_STATIC_SYMS | - macho.S_ATTR_LIVE_SUPPORT => { - log.debug("TODO __eh_frame section: type 0x{x}, name '{s},{s}'", .{ - flags, segname, sectname, - }); continue; }, - macho.S_REGULAR | macho.S_ATTR_DEBUG => { - if (mem.eql(u8, "__LD", segname) and mem.eql(u8, "__compact_unwind", sectname)) { - log.debug("TODO compact unwind section: type 0x{x}, name '{s},{s}'", .{ - flags, segname, sectname, - }); - } - continue; - }, - else => { - if (mem.eql(u8, "__LLVM", segname) and mem.eql(u8, "__asm", sectname)) { - log.debug("TODO LLVM bitcode section: type 0x{x}, name '{s},{s}'", .{ - flags, segname, sectname, + macho.S_COALESCED => { + if (mem.eql(u8, "__TEXT", segname) and mem.eql(u8, "__eh_frame", sectname)) { + log.debug("TODO __eh_frame section: type 0x{x}, name '{s},{s}'", .{ + sect.flags(), segname, sectname, }); continue; } - log.err("unhandled section type 0x{x} for '{s},{s}'", .{ flags, segname, sectname }); - return error.UnhandledSection; }, + macho.S_REGULAR => { + if (sect.isCode()) { + if (self.text_section_index != null) continue; + + self.text_section_index = @intCast(u16, text_seg.sections.items.len); + try text_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__text"), + .segname = makeStaticString("__TEXT"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); + continue; + } + if (sect.isDebug()) { + if (mem.eql(u8, "__LD", segname) and mem.eql(u8, "__compact_unwind", sectname)) { + log.debug("TODO compact unwind section: type 0x{x}, name '{s},{s}'", .{ + sect.flags(), segname, sectname, + }); + } + continue; + } + + if (mem.eql(u8, segname, "__TEXT")) { + if (mem.eql(u8, sectname, "__ustring")) { + if (self.ustring_section_index != null) continue; + + self.ustring_section_index = @intCast(u16, text_seg.sections.items.len); + try text_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__ustring"), + .segname = makeStaticString("__TEXT"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); + } else { + if (self.text_const_section_index != null) continue; + + self.text_const_section_index = @intCast(u16, text_seg.sections.items.len); + try text_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__const"), + .segname = makeStaticString("__TEXT"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); + } + continue; + } + + if (mem.eql(u8, segname, "__DATA_CONST")) { + if (self.data_const_section_index != null) continue; + + self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len); + try data_const_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__const"), + .segname = makeStaticString("__DATA_CONST"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); + continue; + } + + if (mem.eql(u8, segname, "__DATA")) { + if (mem.eql(u8, sectname, "__const")) { + if (self.data_const_section_index != null) continue; + + self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len); + try data_const_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__const"), + .segname = makeStaticString("__DATA_CONST"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); + } else { + if (self.data_section_index != null) continue; + + self.data_section_index = @intCast(u16, data_seg.sections.items.len); + try data_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__data"), + .segname = makeStaticString("__DATA"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); + } + + continue; + } + + if (mem.eql(u8, "__LLVM", segname) and mem.eql(u8, "__asm", sectname)) { + log.debug("TODO LLVM asm section: type 0x{x}, name '{s},{s}'", .{ + sect.flags(), segname, sectname, + }); + continue; + } + }, + else => {}, } + + log.err("{s}: unhandled section type 0x{x} for '{s},{s}'", .{ + object.name.?, + sect.flags(), + segname, + sectname, + }); + return error.UnhandledSection; } // Find ideal section alignment. - for (object_seg.sections.items) |source_sect| { - if (self.getMatchingSection(source_sect)) |res| { + for (object.sections.items) |sect| { + if (self.getMatchingSection(sect)) |res| { const target_seg = &self.load_commands.items[res.seg].Segment; const target_sect = &target_seg.sections.items[res.sect]; - target_sect.@"align" = math.max(target_sect.@"align", source_sect.@"align"); + target_sect.@"align" = math.max(target_sect.@"align", sect.inner.@"align"); } } // Update section mappings - for (object_seg.sections.items) |source_sect, sect_id| { - const source_sect_id = @intCast(u16, sect_id); - if (self.getMatchingSection(source_sect)) |res| { - try self.mapAndUpdateSections(object, source_sect_id, res.seg, res.sect); + for (object.sections.items) |sect, sect_id| { + if (self.getMatchingSection(sect)) |res| { + try self.mapAndUpdateSections(object, @intCast(u16, sect_id), res.seg, res.sect); continue; } - - log.debug("section '{s},{s}' will be unmapped", .{ - parseName(&source_sect.segname), - parseName(&source_sect.sectname), - }); + log.warn("section '{s},{s}' will be unmapped", .{ sect.segname(), sect.sectname() }); } } @@ -819,13 +892,13 @@ const MatchingSection = struct { sect: u16, }; -fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection { - const segname = parseName(§ion.segname); - const sectname = parseName(§ion.sectname); +fn getMatchingSection(self: *Zld, sect: Object.Section) ?MatchingSection { + const segname = sect.segname(); + const sectname = sect.sectname(); const res: ?MatchingSection = blk: { - switch (section.flags) { - macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS => { + switch (sect.sectionType()) { + macho.S_4BYTE_LITERALS, macho.S_8BYTE_LITERALS, macho.S_16BYTE_LITERALS, macho.S_LITERAL_POINTERS => { break :blk .{ .seg = self.text_segment_cmd_index.?, .sect = self.text_const_section_index.?, @@ -855,11 +928,12 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection { .seg = self.data_segment_cmd_index.?, .sect = self.common_section_index.?, }; + } else { + break :blk .{ + .seg = self.data_segment_cmd_index.?, + .sect = self.bss_section_index.?, + }; } - break :blk .{ - .seg = self.data_segment_cmd_index.?, - .sect = self.bss_section_index.?, - }; }, macho.S_THREAD_LOCAL_VARIABLES => { break :blk .{ @@ -879,31 +953,43 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection { .sect = self.tlv_bss_section_index.?, }; }, - macho.S_REGULAR | macho.S_ATTR_PURE_INSTRUCTIONS | macho.S_ATTR_SOME_INSTRUCTIONS => { - break :blk .{ - .seg = self.text_segment_cmd_index.?, - .sect = self.text_section_index.?, - }; + macho.S_COALESCED => { + // TODO coalesced sections + break :blk null; }, macho.S_REGULAR => { + if (sect.isCode()) { + break :blk .{ + .seg = self.text_segment_cmd_index.?, + .sect = self.text_section_index.?, + }; + } + if (sect.isDebug()) { + // TODO debug attributes + break :blk null; + } + if (mem.eql(u8, segname, "__TEXT")) { if (mem.eql(u8, sectname, "__ustring")) { break :blk .{ .seg = self.text_segment_cmd_index.?, .sect = self.ustring_section_index.?, }; + } else { + break :blk .{ + .seg = self.text_segment_cmd_index.?, + .sect = self.text_const_section_index.?, + }; } - break :blk .{ - .seg = self.text_segment_cmd_index.?, - .sect = self.text_const_section_index.?, - }; } + if (mem.eql(u8, segname, "__DATA_CONST")) { break :blk .{ .seg = self.data_const_segment_cmd_index.?, .sect = self.data_const_section_index.?, }; } + if (mem.eql(u8, segname, "__DATA")) { if (mem.eql(u8, sectname, "__const")) { break :blk .{ @@ -916,11 +1002,10 @@ fn getMatchingSection(self: *Zld, section: macho.section_64) ?MatchingSection { .sect = self.data_section_index.?, }; } + break :blk null; }, - else => { - break :blk null; - }, + else => break :blk null, } }; From 15ff0db794c92dd64c7b585fb5eed1adb4888c5e Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 14 Jun 2021 13:15:30 +0200 Subject: [PATCH 16/22] zld: handle __gcc_except_tab section --- src/link/MachO/Zld.zig | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index 70f219bafd..68ed794377 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -65,6 +65,9 @@ stub_helper_section_index: ?u16 = null, text_const_section_index: ?u16 = null, cstring_section_index: ?u16 = null, ustring_section_index: ?u16 = null, +gcc_except_tab: ?u16 = null, +unwind_info: ?u16 = null, +eh_frame: ?u16 = null, // __DATA_CONST segment sections got_section_index: ?u16 = null, @@ -649,6 +652,7 @@ fn updateMetadata(self: *Zld) !void { }); continue; } + if (sect.isDebug()) { if (mem.eql(u8, "__LD", segname) and mem.eql(u8, "__compact_unwind", sectname)) { log.debug("TODO compact unwind section: type 0x{x}, name '{s},{s}'", .{ @@ -677,6 +681,24 @@ fn updateMetadata(self: *Zld) !void { .reserved2 = 0, .reserved3 = 0, }); + } else if (mem.eql(u8, sectname, "__gcc_except_tab")) { + if (self.gcc_except_tab != null) continue; + + self.gcc_except_tab = @intCast(u16, text_seg.sections.items.len); + try text_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__gcc_except_tab"), + .segname = makeStaticString("__TEXT"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); } else { if (self.text_const_section_index != null) continue; @@ -975,6 +997,11 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) ?MatchingSection { .seg = self.text_segment_cmd_index.?, .sect = self.ustring_section_index.?, }; + } else if (mem.eql(u8, sectname, "__gcc_except_tab")) { + break :blk .{ + .seg = self.text_segment_cmd_index.?, + .sect = self.gcc_except_tab.?, + }; } else { break :blk .{ .seg = self.text_segment_cmd_index.?, @@ -1031,6 +1058,7 @@ fn sortSections(self: *Zld) !void { &self.text_section_index, &self.stubs_section_index, &self.stub_helper_section_index, + &self.gcc_except_tab, &self.text_const_section_index, &self.cstring_section_index, &self.ustring_section_index, From cf26f8c19b2e39a8279360bd4c93e38d4dc6b4d0 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 14 Jun 2021 16:01:58 +0200 Subject: [PATCH 17/22] zld: handle __eh_frame section --- src/link/MachO/Zld.zig | 56 ++++++++++++++++++++++---------- src/link/MachO/reloc.zig | 2 ++ src/link/MachO/reloc/aarch64.zig | 43 ++++++++++++++++++++++-- src/link/MachO/reloc/x86_64.zig | 8 ++++- 4 files changed, 87 insertions(+), 22 deletions(-) diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index 68ed794377..4820c03f6e 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -65,9 +65,9 @@ stub_helper_section_index: ?u16 = null, text_const_section_index: ?u16 = null, cstring_section_index: ?u16 = null, ustring_section_index: ?u16 = null, -gcc_except_tab: ?u16 = null, -unwind_info: ?u16 = null, -eh_frame: ?u16 = null, +gcc_except_tab_section_index: ?u16 = null, +unwind_info_section_index: ?u16 = null, +eh_frame_section_index: ?u16 = null, // __DATA_CONST segment sections got_section_index: ?u16 = null, @@ -412,7 +412,7 @@ fn mapAndUpdateSections( const offset = mem.alignForwardGeneric(u64, target_sect.size, alignment); const size = mem.alignForwardGeneric(u64, source_sect.inner.size, alignment); - log.warn("{s}: '{s},{s}' mapped to '{s},{s}' from 0x{x} to 0x{x}", .{ + log.debug("{s}: '{s},{s}' mapped to '{s},{s}' from 0x{x} to 0x{x}", .{ object.name.?, parseName(&source_sect.inner.segname), parseName(&source_sect.inner.sectname), @@ -625,8 +625,24 @@ fn updateMetadata(self: *Zld) !void { }, macho.S_COALESCED => { if (mem.eql(u8, "__TEXT", segname) and mem.eql(u8, "__eh_frame", sectname)) { - log.debug("TODO __eh_frame section: type 0x{x}, name '{s},{s}'", .{ - sect.flags(), segname, sectname, + // TODO I believe __eh_frame is currently part of __unwind_info section + // in the latest ld64 output. + if (self.eh_frame_section_index != null) continue; + + self.eh_frame_section_index = @intCast(u16, text_seg.sections.items.len); + try text_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__eh_frame"), + .segname = makeStaticString("__TEXT"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, }); continue; } @@ -682,9 +698,9 @@ fn updateMetadata(self: *Zld) !void { .reserved3 = 0, }); } else if (mem.eql(u8, sectname, "__gcc_except_tab")) { - if (self.gcc_except_tab != null) continue; + if (self.gcc_except_tab_section_index != null) continue; - self.gcc_except_tab = @intCast(u16, text_seg.sections.items.len); + self.gcc_except_tab_section_index = @intCast(u16, text_seg.sections.items.len); try text_seg.addSection(self.allocator, .{ .sectname = makeStaticString("__gcc_except_tab"), .segname = makeStaticString("__TEXT"), @@ -818,7 +834,7 @@ fn updateMetadata(self: *Zld) !void { try self.mapAndUpdateSections(object, @intCast(u16, sect_id), res.seg, res.sect); continue; } - log.warn("section '{s},{s}' will be unmapped", .{ sect.segname(), sect.sectname() }); + log.debug("section '{s},{s}' will be unmapped", .{ sect.segname(), sect.sectname() }); } } @@ -976,7 +992,13 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) ?MatchingSection { }; }, macho.S_COALESCED => { - // TODO coalesced sections + if (mem.eql(u8, "__TEXT", segname) and mem.eql(u8, "__eh_frame", sectname)) { + break :blk .{ + .seg = self.text_segment_cmd_index.?, + .sect = self.eh_frame_section_index.?, + }; + } + break :blk null; }, macho.S_REGULAR => { @@ -1000,7 +1022,7 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) ?MatchingSection { } else if (mem.eql(u8, sectname, "__gcc_except_tab")) { break :blk .{ .seg = self.text_segment_cmd_index.?, - .sect = self.gcc_except_tab.?, + .sect = self.gcc_except_tab_section_index.?, }; } else { break :blk .{ @@ -1058,10 +1080,11 @@ fn sortSections(self: *Zld) !void { &self.text_section_index, &self.stubs_section_index, &self.stub_helper_section_index, - &self.gcc_except_tab, - &self.text_const_section_index, + &self.gcc_except_tab_section_index, &self.cstring_section_index, &self.ustring_section_index, + &self.text_const_section_index, + &self.eh_frame_section_index, }; for (indices) |maybe_index| { const new_index: u16 = if (maybe_index.*) |index| blk: { @@ -1846,7 +1869,7 @@ fn resolveStubsAndGotEntries(self: *Zld) !void { for (relocs) |rel| { switch (rel.@"type") { .unsigned => continue, - .got_page, .got_page_off, .got_load, .got => { + .got_page, .got_page_off, .got_load, .got, .pointer_to_got => { const sym = rel.target.symbol.getTopmostAlias(); if (sym.got_index != null) continue; @@ -1864,9 +1887,6 @@ fn resolveStubsAndGotEntries(self: *Zld) !void { if (sym.stubs_index != null) continue; if (sym.@"type" != .proxy) continue; - // if (sym.cast(Symbol.Regular)) |reg| { - // if (!reg.weak_ref) continue; - // } const index = @intCast(u32, self.stubs.items.len); sym.stubs_index = index; @@ -1977,7 +1997,7 @@ fn resolveRelocsAndWriteSections(self: *Zld) !void { }); } }, - .got_page, .got_page_off, .got_load, .got => { + .got_page, .got_page_off, .got_load, .got, .pointer_to_got => { const dc_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; const got = dc_seg.sections.items[self.got_section_index.?]; const final = rel.target.symbol.getTopmostAlias(); diff --git a/src/link/MachO/reloc.zig b/src/link/MachO/reloc.zig index 59aff74a83..1e1b938196 100644 --- a/src/link/MachO/reloc.zig +++ b/src/link/MachO/reloc.zig @@ -52,6 +52,7 @@ pub const Relocation = struct { .page_off => @fieldParentPtr(aarch64.PageOff, "base", base).resolve(args), .got_page => @fieldParentPtr(aarch64.GotPage, "base", base).resolve(args), .got_page_off => @fieldParentPtr(aarch64.GotPageOff, "base", base).resolve(args), + .pointer_to_got => @fieldParentPtr(aarch64.PointerToGot, "base", base).resolve(args), .tlvp_page => @fieldParentPtr(aarch64.TlvpPage, "base", base).resolve(args), .tlvp_page_off => @fieldParentPtr(aarch64.TlvpPageOff, "base", base).resolve(args), .branch_x86_64 => @fieldParentPtr(x86_64.Branch, "base", base).resolve(args), @@ -70,6 +71,7 @@ pub const Relocation = struct { got_page, got_page_off, tlvp_page, + pointer_to_got, tlvp_page_off, branch_x86_64, signed, diff --git a/src/link/MachO/reloc/aarch64.zig b/src/link/MachO/reloc/aarch64.zig index c4b3104879..16b982bf90 100644 --- a/src/link/MachO/reloc/aarch64.zig +++ b/src/link/MachO/reloc/aarch64.zig @@ -141,6 +141,20 @@ pub const GotPageOff = struct { } }; +pub const PointerToGot = struct { + base: Relocation, + + pub const base_type: Relocation.Type = .pointer_to_got; + + pub fn resolve(ptr_to_got: PointerToGot, args: Relocation.ResolveArgs) !void { + const result = try math.cast(i32, @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr)); + + log.debug(" | calculated value 0x{x}", .{result}); + + mem.writeIntLittle(u32, ptr_to_got.base.code[0..4], @bitCast(u32, result)); + } +}; + pub const TlvpPage = struct { base: Relocation, /// Always .PCRelativeAddress @@ -228,9 +242,7 @@ pub const Parser = struct { try parser.parseTlvpLoadPageOff(rel); }, .ARM64_RELOC_POINTER_TO_GOT => { - // TODO Handle pointer to GOT. This reloc seems to appear in - // __LD,__compact_unwind section which we currently don't handle. - log.debug("Unhandled relocation ARM64_RELOC_POINTER_TO_GOT", .{}); + try parser.parsePointerToGot(rel); }, } } @@ -583,6 +595,31 @@ pub const Parser = struct { log.debug(" | emitting {}", .{unsigned}); try parser.parsed.append(&unsigned.base); } + + fn parsePointerToGot(parser: *Parser, rel: macho.relocation_info) !void { + const rel_type = @intToEnum(macho.reloc_type_arm64, rel.r_type); + assert(rel_type == .ARM64_RELOC_POINTER_TO_GOT); + assert(rel.r_pcrel == 1); + assert(rel.r_length == 2); + + var ptr_to_got = try parser.allocator.create(PointerToGot); + errdefer parser.allocator.destroy(ptr_to_got); + + const target = Relocation.Target.from_reloc(rel, parser.symbols); + const offset = @intCast(u32, rel.r_address); + + ptr_to_got.* = .{ + .base = .{ + .@"type" = .pointer_to_got, + .code = parser.code[offset..][0..4], + .offset = offset, + .target = target, + }, + }; + + log.debug(" | emitting {}", .{ptr_to_got}); + try parser.parsed.append(&ptr_to_got.base); + } }; inline fn isArithmeticOp(inst: *const [4]u8) bool { diff --git a/src/link/MachO/reloc/x86_64.zig b/src/link/MachO/reloc/x86_64.zig index a5e3ff2825..2a457fdea2 100644 --- a/src/link/MachO/reloc/x86_64.zig +++ b/src/link/MachO/reloc/x86_64.zig @@ -66,11 +66,15 @@ pub const GotLoad = struct { pub const Got = struct { base: Relocation, + addend: i32, pub const base_type: Relocation.Type = .got; pub fn resolve(got: Got, args: Relocation.ResolveArgs) !void { - const displacement = try math.cast(i32, @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4); + const displacement = try math.cast( + i32, + @intCast(i64, args.target_addr) - @intCast(i64, args.source_addr) - 4 + got.addend, + ); log.debug(" | displacement 0x{x}", .{displacement}); mem.writeIntLittle(u32, got.base.code[0..4], @bitCast(u32, displacement)); } @@ -237,6 +241,7 @@ pub const Parser = struct { const offset = @intCast(u32, rel.r_address); const inst = parser.code[offset..][0..4]; const target = Relocation.Target.from_reloc(rel, parser.symbols); + const addend = mem.readIntLittle(i32, inst); var got = try parser.allocator.create(Got); errdefer parser.allocator.destroy(got); @@ -248,6 +253,7 @@ pub const Parser = struct { .offset = offset, .target = target, }, + .addend = addend, }; log.debug(" | emitting {}", .{got}); From 6362b82497731a64b8ceae6af577b50660a87fec Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Mon, 14 Jun 2021 20:21:54 +0200 Subject: [PATCH 18/22] zld: map coalesced sections and handle undefines --- src/link/MachO/Zld.zig | 45 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/link/MachO/Zld.zig b/src/link/MachO/Zld.zig index 4820c03f6e..c0b5f03c58 100644 --- a/src/link/MachO/Zld.zig +++ b/src/link/MachO/Zld.zig @@ -646,6 +646,26 @@ fn updateMetadata(self: *Zld) !void { }); continue; } + + // TODO audit this: is this the right mapping? + if (self.data_const_section_index != null) continue; + + self.data_const_section_index = @intCast(u16, data_const_seg.sections.items.len); + try data_const_seg.addSection(self.allocator, .{ + .sectname = makeStaticString("__const"), + .segname = makeStaticString("__DATA_CONST"), + .addr = 0, + .size = 0, + .offset = 0, + .@"align" = 0, + .reloff = 0, + .nreloc = 0, + .flags = macho.S_REGULAR, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + }); + continue; }, macho.S_REGULAR => { if (sect.isCode()) { @@ -999,7 +1019,10 @@ fn getMatchingSection(self: *Zld, sect: Object.Section) ?MatchingSection { }; } - break :blk null; + break :blk .{ + .seg = self.data_const_segment_cmd_index.?, + .sect = self.data_const_section_index.?, + }; }, macho.S_REGULAR => { if (sect.isCode()) { @@ -2001,7 +2024,15 @@ fn resolveRelocsAndWriteSections(self: *Zld) !void { const dc_seg = self.load_commands.items[self.data_const_segment_cmd_index.?].Segment; const got = dc_seg.sections.items[self.got_section_index.?]; const final = rel.target.symbol.getTopmostAlias(); - args.target_addr = got.addr + final.got_index.? * @sizeOf(u64); + const got_index = final.got_index orelse { + // TODO remove this when we can link against TAPI files. + log.err("undefined reference to symbol '{s}'", .{final.name}); + log.err(" | referenced in {s}", .{ + rel.target.symbol.cast(Symbol.Unresolved).?.file.name.?, + }); + return error.UndefinedSymbolReference; + }; + args.target_addr = got.addr + got_index * @sizeOf(u64); }, else => |tt| { if (tt == .signed and rel.target == .section) { @@ -2067,7 +2098,15 @@ fn relocTargetAddr(self: *Zld, object: *const Object, target: reloc.Relocation.T log.debug(" | symbol stub '{s}'", .{sym.name}); const segment = self.load_commands.items[self.text_segment_cmd_index.?].Segment; const stubs = segment.sections.items[self.stubs_section_index.?]; - break :blk stubs.addr + proxy.base.stubs_index.? * stubs.reserved2; + const stubs_index = proxy.base.stubs_index orelse { + // TODO remove this when we can link against TAPI files. + log.err("undefined reference to symbol '{s}'", .{final.name}); + log.err(" | referenced in {s}", .{ + sym.cast(Symbol.Unresolved).?.file.name.?, + }); + return error.UndefinedSymbolReference; + }; + break :blk stubs.addr + stubs_index * stubs.reserved2; } else { log.err("failed to resolve symbol '{s}' as a relocation target", .{sym.name}); return error.FailedToResolveRelocationTarget; From 1fe6a34f699f64dc7ac6597f337c61d04c7de812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Linse?= Date: Mon, 14 Jun 2021 23:13:30 +0200 Subject: [PATCH 19/22] std: don't reference non-existant ComptimeStringHashMap type --- lib/std/comptime_string_map.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/comptime_string_map.zig b/lib/std/comptime_string_map.zig index 1dca59e997..0bf86167f9 100644 --- a/lib/std/comptime_string_map.zig +++ b/lib/std/comptime_string_map.zig @@ -6,7 +6,7 @@ const std = @import("std.zig"); const mem = std.mem; -/// Like ComptimeStringHashMap but optimized for small sets of disparate string keys. +/// Comptime string map optimized for small sets of disparate string keys. /// Works by separating the keys by length at comptime and only checking strings of /// equal length at runtime. /// From c54af9774d4d395a6dabbe8580a93a04c355c0bc Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 12 Jun 2021 21:30:36 +0300 Subject: [PATCH 20/22] translate-c: move utility functions to a separate namespace --- lib/std/c.zig | 1 - lib/std/meta.zig | 376 ----------------- lib/std/zig.zig | 4 + .../{c/builtins.zig => zig/c_builtins.zig} | 0 lib/std/zig/c_translation.zig | 385 ++++++++++++++++++ src/translate_c.zig | 58 +-- src/translate_c/ast.zig | 87 ++-- test/translate_c.zig | 66 +-- 8 files changed, 501 insertions(+), 476 deletions(-) rename lib/std/{c/builtins.zig => zig/c_builtins.zig} (100%) create mode 100644 lib/std/zig/c_translation.zig diff --git a/lib/std/c.zig b/lib/std/c.zig index f0e53d8747..74b92ad28a 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -10,7 +10,6 @@ const page_size = std.mem.page_size; pub const tokenizer = @import("c/tokenizer.zig"); pub const Token = tokenizer.Token; pub const Tokenizer = tokenizer.Tokenizer; -pub const builtins = @import("c/builtins.zig"); test { _ = tokenizer; diff --git a/lib/std/meta.zig b/lib/std/meta.zig index e738ad6efb..e19208cd84 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -884,319 +884,6 @@ pub fn Vector(comptime len: u32, comptime child: type) type { }); } -/// Given a type and value, cast the value to the type as c would. -/// This is for translate-c and is not intended for general use. -pub fn cast(comptime DestType: type, target: anytype) DestType { - // this function should behave like transCCast in translate-c, except it's for macros and enums - const SourceType = @TypeOf(target); - switch (@typeInfo(DestType)) { - .Pointer => return castToPtr(DestType, SourceType, target), - .Optional => |dest_opt| { - if (@typeInfo(dest_opt.child) == .Pointer) { - return castToPtr(DestType, SourceType, target); - } - }, - .Enum => |enum_type| { - if (@typeInfo(SourceType) == .Int or @typeInfo(SourceType) == .ComptimeInt) { - const intermediate = cast(enum_type.tag_type, target); - return @intToEnum(DestType, intermediate); - } - }, - .Int => { - switch (@typeInfo(SourceType)) { - .Pointer => { - return castInt(DestType, @ptrToInt(target)); - }, - .Optional => |opt| { - if (@typeInfo(opt.child) == .Pointer) { - return castInt(DestType, @ptrToInt(target)); - } - }, - .Enum => { - return castInt(DestType, @enumToInt(target)); - }, - .Int => { - return castInt(DestType, target); - }, - else => {}, - } - }, - else => {}, - } - return @as(DestType, target); -} - -fn castInt(comptime DestType: type, target: anytype) DestType { - const dest = @typeInfo(DestType).Int; - const source = @typeInfo(@TypeOf(target)).Int; - - if (dest.bits < source.bits) - return @bitCast(DestType, @truncate(Int(source.signedness, dest.bits), target)) - else - return @bitCast(DestType, @as(Int(source.signedness, dest.bits), target)); -} - -fn castPtr(comptime DestType: type, target: anytype) DestType { - const dest = ptrInfo(DestType); - const source = ptrInfo(@TypeOf(target)); - - if (source.is_const and !dest.is_const or source.is_volatile and !dest.is_volatile) - return @intToPtr(DestType, @ptrToInt(target)) - else if (@typeInfo(dest.child) == .Opaque) - // dest.alignment would error out - return @ptrCast(DestType, target) - else - return @ptrCast(DestType, @alignCast(dest.alignment, target)); -} - -fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype) DestType { - switch (@typeInfo(SourceType)) { - .Int => { - return @intToPtr(DestType, castInt(usize, target)); - }, - .ComptimeInt => { - if (target < 0) - return @intToPtr(DestType, @bitCast(usize, @intCast(isize, target))) - else - return @intToPtr(DestType, @intCast(usize, target)); - }, - .Pointer => { - return castPtr(DestType, target); - }, - .Optional => |target_opt| { - if (@typeInfo(target_opt.child) == .Pointer) { - return castPtr(DestType, target); - } - }, - else => {}, - } - return @as(DestType, target); -} - -fn ptrInfo(comptime PtrType: type) TypeInfo.Pointer { - return switch (@typeInfo(PtrType)) { - .Optional => |opt_info| @typeInfo(opt_info.child).Pointer, - .Pointer => |ptr_info| ptr_info, - else => unreachable, - }; -} - -test "std.meta.cast" { - const E = enum(u2) { - Zero, - One, - Two, - }; - - var i = @as(i64, 10); - - try testing.expect(cast(*u8, 16) == @intToPtr(*u8, 16)); - try testing.expect(cast(*u64, &i).* == @as(u64, 10)); - try testing.expect(cast(*i64, @as(?*align(1) i64, &i)) == &i); - - try testing.expect(cast(?*u8, 2) == @intToPtr(*u8, 2)); - try testing.expect(cast(?*i64, @as(*align(1) i64, &i)) == &i); - try testing.expect(cast(?*i64, @as(?*align(1) i64, &i)) == &i); - - try testing.expect(cast(E, 1) == .One); - - try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(*u32, 4))); - try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(?*u32, 4))); - try testing.expectEqual(@as(u32, 10), cast(u32, @as(u64, 10))); - try testing.expectEqual(@as(u8, 2), cast(u8, E.Two)); - - try testing.expectEqual(@bitCast(i32, @as(u32, 0x8000_0000)), cast(i32, @as(u32, 0x8000_0000))); - - try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*const u8, 2))); - try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*volatile u8, 2))); - - try testing.expectEqual(@intToPtr(?*c_void, 2), cast(?*c_void, @intToPtr(*u8, 2))); - - const C_ENUM = enum(c_int) { - A = 0, - B, - C, - _, - }; - try testing.expectEqual(cast(C_ENUM, @as(i64, -1)), @intToEnum(C_ENUM, -1)); - try testing.expectEqual(cast(C_ENUM, @as(i8, 1)), .B); - try testing.expectEqual(cast(C_ENUM, @as(u64, 1)), .B); - try testing.expectEqual(cast(C_ENUM, @as(u64, 42)), @intToEnum(C_ENUM, 42)); - - var foo: c_int = -1; - try testing.expect(cast(*c_void, -1) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1)))); - try testing.expect(cast(*c_void, foo) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1)))); - try testing.expect(cast(?*c_void, -1) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1)))); - try testing.expect(cast(?*c_void, foo) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1)))); -} - -/// Given a value returns its size as C's sizeof operator would. -/// This is for translate-c and is not intended for general use. -pub fn sizeof(target: anytype) usize { - const T: type = if (@TypeOf(target) == type) target else @TypeOf(target); - switch (@typeInfo(T)) { - .Float, .Int, .Struct, .Union, .Enum, .Array, .Bool, .Vector => return @sizeOf(T), - .Fn => { - // sizeof(main) returns 1, sizeof(&main) returns pointer size. - // We cannot distinguish those types in Zig, so use pointer size. - return @sizeOf(T); - }, - .Null => return @sizeOf(*c_void), - .Void => { - // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC. - return 1; - }, - .Opaque => { - if (T == c_void) { - // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC. - return 1; - } else { - @compileError("Cannot use C sizeof on opaque type " ++ @typeName(T)); - } - }, - .Optional => |opt| { - if (@typeInfo(opt.child) == .Pointer) { - return sizeof(opt.child); - } else { - @compileError("Cannot use C sizeof on non-pointer optional " ++ @typeName(T)); - } - }, - .Pointer => |ptr| { - if (ptr.size == .Slice) { - @compileError("Cannot use C sizeof on slice type " ++ @typeName(T)); - } - // for strings, sizeof("a") returns 2. - // normal pointer decay scenarios from C are handled - // in the .Array case above, but strings remain literals - // and are therefore always pointers, so they need to be - // specially handled here. - if (ptr.size == .One and ptr.is_const and @typeInfo(ptr.child) == .Array) { - const array_info = @typeInfo(ptr.child).Array; - if ((array_info.child == u8 or array_info.child == u16) and - array_info.sentinel != null and - array_info.sentinel.? == 0) - { - // length of the string plus one for the null terminator. - return (array_info.len + 1) * @sizeOf(array_info.child); - } - } - // When zero sized pointers are removed, this case will no - // longer be reachable and can be deleted. - if (@sizeOf(T) == 0) { - return @sizeOf(*c_void); - } - return @sizeOf(T); - }, - .ComptimeFloat => return @sizeOf(f64), // TODO c_double #3999 - .ComptimeInt => { - // TODO to get the correct result we have to translate - // `1073741824 * 4` as `int(1073741824) *% int(4)` since - // sizeof(1073741824 * 4) != sizeof(4294967296). - - // TODO test if target fits in int, long or long long - return @sizeOf(c_int); - }, - else => @compileError("std.meta.sizeof does not support type " ++ @typeName(T)), - } -} - -test "sizeof" { - const E = enum(c_int) { One, _ }; - const S = extern struct { a: u32 }; - - const ptr_size = @sizeOf(*c_void); - - try testing.expect(sizeof(u32) == 4); - try testing.expect(sizeof(@as(u32, 2)) == 4); - try testing.expect(sizeof(2) == @sizeOf(c_int)); - - try testing.expect(sizeof(2.0) == @sizeOf(f64)); - - try testing.expect(sizeof(E) == @sizeOf(c_int)); - try testing.expect(sizeof(E.One) == @sizeOf(c_int)); - - try testing.expect(sizeof(S) == 4); - - try testing.expect(sizeof([_]u32{ 4, 5, 6 }) == 12); - try testing.expect(sizeof([3]u32) == 12); - try testing.expect(sizeof([3:0]u32) == 16); - try testing.expect(sizeof(&[_]u32{ 4, 5, 6 }) == ptr_size); - - try testing.expect(sizeof(*u32) == ptr_size); - try testing.expect(sizeof([*]u32) == ptr_size); - try testing.expect(sizeof([*c]u32) == ptr_size); - try testing.expect(sizeof(?*u32) == ptr_size); - try testing.expect(sizeof(?[*]u32) == ptr_size); - try testing.expect(sizeof(*c_void) == ptr_size); - try testing.expect(sizeof(*void) == ptr_size); - try testing.expect(sizeof(null) == ptr_size); - - try testing.expect(sizeof("foobar") == 7); - try testing.expect(sizeof(&[_:0]u16{ 'f', 'o', 'o', 'b', 'a', 'r' }) == 14); - try testing.expect(sizeof(*const [4:0]u8) == 5); - try testing.expect(sizeof(*[4:0]u8) == ptr_size); - try testing.expect(sizeof([*]const [4:0]u8) == ptr_size); - try testing.expect(sizeof(*const *const [4:0]u8) == ptr_size); - try testing.expect(sizeof(*const [4]u8) == ptr_size); - - try testing.expect(sizeof(sizeof) == @sizeOf(@TypeOf(sizeof))); - - try testing.expect(sizeof(void) == 1); - try testing.expect(sizeof(c_void) == 1); -} - -pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal }; - -fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type { - const signed_decimal = [_]type{ c_int, c_long, c_longlong }; - const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong }; - const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong }; - - const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned) - &unsigned - else if (radix == .decimal) - &signed_decimal - else - &signed_oct_hex; - - var pos = mem.indexOfScalar(type, list, SuffixType).?; - - while (pos < list.len) : (pos += 1) { - if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) { - return list[pos]; - } - } - @compileError("Integer literal is too large"); -} - -/// Promote the type of an integer literal until it fits as C would. -/// This is for translate-c and is not intended for general use. -pub fn promoteIntLiteral( - comptime SuffixType: type, - comptime number: comptime_int, - comptime radix: CIntLiteralRadix, -) PromoteIntLiteralReturnType(SuffixType, number, radix) { - return number; -} - -test "promoteIntLiteral" { - const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal); - try testing.expectEqual(c_uint, @TypeOf(signed_hex)); - - if (math.maxInt(c_longlong) == math.maxInt(c_int)) return; - - const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal); - const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal); - - if (math.maxInt(c_long) > math.maxInt(c_int)) { - try testing.expectEqual(c_long, @TypeOf(signed_decimal)); - try testing.expectEqual(c_ulong, @TypeOf(unsigned)); - } else { - try testing.expectEqual(c_longlong, @TypeOf(signed_decimal)); - try testing.expectEqual(c_ulonglong, @TypeOf(unsigned)); - } -} - /// For a given function type, returns a tuple type which fields will /// correspond to the argument types. /// @@ -1316,38 +1003,6 @@ pub fn globalOption(comptime name: []const u8, comptime T: type) ?T { return @as(T, @field(root, name)); } -/// This function is for translate-c and is not intended for general use. -/// Convert from clang __builtin_shufflevector index to Zig @shuffle index -/// clang requires __builtin_shufflevector index arguments to be integer constants. -/// negative values for `this_index` indicate "don't care" so we arbitrarily choose 0 -/// clang enforces that `this_index` is less than the total number of vector elements -/// See https://ziglang.org/documentation/master/#shuffle -/// See https://clang.llvm.org/docs/LanguageExtensions.html#langext-builtin-shufflevector -pub fn shuffleVectorIndex(comptime this_index: c_int, comptime source_vector_len: usize) i32 { - if (this_index <= 0) return 0; - - const positive_index = @intCast(usize, this_index); - if (positive_index < source_vector_len) return @intCast(i32, this_index); - const b_index = positive_index - source_vector_len; - return ~@intCast(i32, b_index); -} - -test "shuffleVectorIndex" { - const vector_len: usize = 4; - - try testing.expect(shuffleVectorIndex(-1, vector_len) == 0); - - try testing.expect(shuffleVectorIndex(0, vector_len) == 0); - try testing.expect(shuffleVectorIndex(1, vector_len) == 1); - try testing.expect(shuffleVectorIndex(2, vector_len) == 2); - try testing.expect(shuffleVectorIndex(3, vector_len) == 3); - - try testing.expect(shuffleVectorIndex(4, vector_len) == -1); - try testing.expect(shuffleVectorIndex(5, vector_len) == -2); - try testing.expect(shuffleVectorIndex(6, vector_len) == -3); - try testing.expect(shuffleVectorIndex(7, vector_len) == -4); -} - /// Returns whether `error_union` contains an error. pub fn isError(error_union: anytype) bool { return if (error_union) |_| false else |_| true; @@ -1357,34 +1012,3 @@ test "isError" { try std.testing.expect(isError(math.absInt(@as(i8, -128)))); try std.testing.expect(!isError(math.absInt(@as(i8, -127)))); } - -/// This function is for translate-c and is not intended for general use. -/// Constructs a [*c] pointer with the const and volatile annotations -/// from SelfType for pointing to a C flexible array of ElementType. -pub fn FlexibleArrayType(comptime SelfType: type, ElementType: type) type { - switch (@typeInfo(SelfType)) { - .Pointer => |ptr| { - return @Type(TypeInfo{ .Pointer = .{ - .size = .C, - .is_const = ptr.is_const, - .is_volatile = ptr.is_volatile, - .alignment = @alignOf(ElementType), - .child = ElementType, - .is_allowzero = true, - .sentinel = null, - } }); - }, - else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)), - } -} - -test "Flexible Array Type" { - const Container = extern struct { - size: usize, - }; - - try testing.expectEqual(FlexibleArrayType(*Container, c_int), [*c]c_int); - try testing.expectEqual(FlexibleArrayType(*const Container, c_int), [*c]const c_int); - try testing.expectEqual(FlexibleArrayType(*volatile Container, c_int), [*c]volatile c_int); - try testing.expectEqual(FlexibleArrayType(*const volatile Container, c_int), [*c]const volatile c_int); -} diff --git a/lib/std/zig.zig b/lib/std/zig.zig index 083803e0c8..ebb16c9c15 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -16,6 +16,10 @@ pub const ast = @import("zig/ast.zig"); pub const system = @import("zig/system.zig"); pub const CrossTarget = @import("zig/cross_target.zig").CrossTarget; +// Files needed by translate-c. +pub const c_builtins = @import("zig/c_builtins.zig"); +pub const c_translation = @import("zig/c_translation.zig"); + pub const SrcHash = [16]u8; pub fn hashSrc(src: []const u8) SrcHash { diff --git a/lib/std/c/builtins.zig b/lib/std/zig/c_builtins.zig similarity index 100% rename from lib/std/c/builtins.zig rename to lib/std/zig/c_builtins.zig diff --git a/lib/std/zig/c_translation.zig b/lib/std/zig/c_translation.zig new file mode 100644 index 0000000000..6e0f721b4f --- /dev/null +++ b/lib/std/zig/c_translation.zig @@ -0,0 +1,385 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2015-2021 Zig Contributors +// This file is part of [zig](https://ziglang.org/), which is MIT licensed. +// The MIT license requires this copyright notice to be included in all copies +// and substantial portions of the software. + +const std = @import("std"); +const testing = std.testing; +const math = std.math; +const mem = std.mem; + +/// Given a type and value, cast the value to the type as c would. +pub fn cast(comptime DestType: type, target: anytype) DestType { + // this function should behave like transCCast in translate-c, except it's for macros and enums + const SourceType = @TypeOf(target); + switch (@typeInfo(DestType)) { + .Fn, .Pointer => return castToPtr(DestType, SourceType, target), + .Optional => |dest_opt| { + if (@typeInfo(dest_opt.child) == .Pointer or @typeInfo(dest_opt.child) == .Fn) { + return castToPtr(DestType, SourceType, target); + } + }, + .Enum => |enum_type| { + if (@typeInfo(SourceType) == .Int or @typeInfo(SourceType) == .ComptimeInt) { + const intermediate = cast(enum_type.tag_type, target); + return @intToEnum(DestType, intermediate); + } + }, + .Int => { + switch (@typeInfo(SourceType)) { + .Pointer => { + return castInt(DestType, @ptrToInt(target)); + }, + .Optional => |opt| { + if (@typeInfo(opt.child) == .Pointer) { + return castInt(DestType, @ptrToInt(target)); + } + }, + .Enum => { + return castInt(DestType, @enumToInt(target)); + }, + .Int => { + return castInt(DestType, target); + }, + else => {}, + } + }, + else => {}, + } + return @as(DestType, target); +} + +fn castInt(comptime DestType: type, target: anytype) DestType { + const dest = @typeInfo(DestType).Int; + const source = @typeInfo(@TypeOf(target)).Int; + + if (dest.bits < source.bits) + return @bitCast(DestType, @truncate(std.meta.Int(source.signedness, dest.bits), target)) + else + return @bitCast(DestType, @as(std.meta.Int(source.signedness, dest.bits), target)); +} + +fn castPtr(comptime DestType: type, target: anytype) DestType { + const dest = ptrInfo(DestType); + const source = ptrInfo(@TypeOf(target)); + + if (source.is_const and !dest.is_const or source.is_volatile and !dest.is_volatile) + return @intToPtr(DestType, @ptrToInt(target)) + else if (@typeInfo(dest.child) == .Opaque) + // dest.alignment would error out + return @ptrCast(DestType, target) + else + return @ptrCast(DestType, @alignCast(dest.alignment, target)); +} + +fn castToPtr(comptime DestType: type, comptime SourceType: type, target: anytype) DestType { + switch (@typeInfo(SourceType)) { + .Int => { + return @intToPtr(DestType, castInt(usize, target)); + }, + .ComptimeInt => { + if (target < 0) + return @intToPtr(DestType, @bitCast(usize, @intCast(isize, target))) + else + return @intToPtr(DestType, @intCast(usize, target)); + }, + .Pointer => { + return castPtr(DestType, target); + }, + .Optional => |target_opt| { + if (@typeInfo(target_opt.child) == .Pointer) { + return castPtr(DestType, target); + } + }, + else => {}, + } + return @as(DestType, target); +} + +fn ptrInfo(comptime PtrType: type) std.builtin.TypeInfo.Pointer { + return switch (@typeInfo(PtrType)) { + .Optional => |opt_info| @typeInfo(opt_info.child).Pointer, + .Pointer => |ptr_info| ptr_info, + else => unreachable, + }; +} + +test "cast" { + const E = enum(u2) { + Zero, + One, + Two, + }; + + var i = @as(i64, 10); + + try testing.expect(cast(*u8, 16) == @intToPtr(*u8, 16)); + try testing.expect(cast(*u64, &i).* == @as(u64, 10)); + try testing.expect(cast(*i64, @as(?*align(1) i64, &i)) == &i); + + try testing.expect(cast(?*u8, 2) == @intToPtr(*u8, 2)); + try testing.expect(cast(?*i64, @as(*align(1) i64, &i)) == &i); + try testing.expect(cast(?*i64, @as(?*align(1) i64, &i)) == &i); + + try testing.expect(cast(E, 1) == .One); + + try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(*u32, 4))); + try testing.expectEqual(@as(u32, 4), cast(u32, @intToPtr(?*u32, 4))); + try testing.expectEqual(@as(u32, 10), cast(u32, @as(u64, 10))); + try testing.expectEqual(@as(u8, 2), cast(u8, E.Two)); + + try testing.expectEqual(@bitCast(i32, @as(u32, 0x8000_0000)), cast(i32, @as(u32, 0x8000_0000))); + + try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*const u8, 2))); + try testing.expectEqual(@intToPtr(*u8, 2), cast(*u8, @intToPtr(*volatile u8, 2))); + + try testing.expectEqual(@intToPtr(?*c_void, 2), cast(?*c_void, @intToPtr(*u8, 2))); + + const C_ENUM = enum(c_int) { + A = 0, + B, + C, + _, + }; + try testing.expectEqual(cast(C_ENUM, @as(i64, -1)), @intToEnum(C_ENUM, -1)); + try testing.expectEqual(cast(C_ENUM, @as(i8, 1)), .B); + try testing.expectEqual(cast(C_ENUM, @as(u64, 1)), .B); + try testing.expectEqual(cast(C_ENUM, @as(u64, 42)), @intToEnum(C_ENUM, 42)); + + var foo: c_int = -1; + try testing.expect(cast(*c_void, -1) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1)))); + try testing.expect(cast(*c_void, foo) == @intToPtr(*c_void, @bitCast(usize, @as(isize, -1)))); + try testing.expect(cast(?*c_void, -1) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1)))); + try testing.expect(cast(?*c_void, foo) == @intToPtr(?*c_void, @bitCast(usize, @as(isize, -1)))); + + const FnPtr = ?fn (*c_void) void; + try testing.expect(cast(FnPtr, 0) == @intToPtr(FnPtr, @as(usize, 0))); + try testing.expect(cast(FnPtr, foo) == @intToPtr(FnPtr, @bitCast(usize, @as(isize, -1)))); +} + +/// Given a value returns its size as C's sizeof operator would. +pub fn sizeof(target: anytype) usize { + const T: type = if (@TypeOf(target) == type) target else @TypeOf(target); + switch (@typeInfo(T)) { + .Float, .Int, .Struct, .Union, .Enum, .Array, .Bool, .Vector => return @sizeOf(T), + .Fn => { + // sizeof(main) returns 1, sizeof(&main) returns pointer size. + // We cannot distinguish those types in Zig, so use pointer size. + return @sizeOf(T); + }, + .Null => return @sizeOf(*c_void), + .Void => { + // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC. + return 1; + }, + .Opaque => { + if (T == c_void) { + // Note: sizeof(void) is 1 on clang/gcc and 0 on MSVC. + return 1; + } else { + @compileError("Cannot use C sizeof on opaque type " ++ @typeName(T)); + } + }, + .Optional => |opt| { + if (@typeInfo(opt.child) == .Pointer) { + return sizeof(opt.child); + } else { + @compileError("Cannot use C sizeof on non-pointer optional " ++ @typeName(T)); + } + }, + .Pointer => |ptr| { + if (ptr.size == .Slice) { + @compileError("Cannot use C sizeof on slice type " ++ @typeName(T)); + } + // for strings, sizeof("a") returns 2. + // normal pointer decay scenarios from C are handled + // in the .Array case above, but strings remain literals + // and are therefore always pointers, so they need to be + // specially handled here. + if (ptr.size == .One and ptr.is_const and @typeInfo(ptr.child) == .Array) { + const array_info = @typeInfo(ptr.child).Array; + if ((array_info.child == u8 or array_info.child == u16) and + array_info.sentinel != null and + array_info.sentinel.? == 0) + { + // length of the string plus one for the null terminator. + return (array_info.len + 1) * @sizeOf(array_info.child); + } + } + // When zero sized pointers are removed, this case will no + // longer be reachable and can be deleted. + if (@sizeOf(T) == 0) { + return @sizeOf(*c_void); + } + return @sizeOf(T); + }, + .ComptimeFloat => return @sizeOf(f64), // TODO c_double #3999 + .ComptimeInt => { + // TODO to get the correct result we have to translate + // `1073741824 * 4` as `int(1073741824) *% int(4)` since + // sizeof(1073741824 * 4) != sizeof(4294967296). + + // TODO test if target fits in int, long or long long + return @sizeOf(c_int); + }, + else => @compileError("std.meta.sizeof does not support type " ++ @typeName(T)), + } +} + +test "sizeof" { + const E = enum(c_int) { One, _ }; + const S = extern struct { a: u32 }; + + const ptr_size = @sizeOf(*c_void); + + try testing.expect(sizeof(u32) == 4); + try testing.expect(sizeof(@as(u32, 2)) == 4); + try testing.expect(sizeof(2) == @sizeOf(c_int)); + + try testing.expect(sizeof(2.0) == @sizeOf(f64)); + + try testing.expect(sizeof(E) == @sizeOf(c_int)); + try testing.expect(sizeof(E.One) == @sizeOf(c_int)); + + try testing.expect(sizeof(S) == 4); + + try testing.expect(sizeof([_]u32{ 4, 5, 6 }) == 12); + try testing.expect(sizeof([3]u32) == 12); + try testing.expect(sizeof([3:0]u32) == 16); + try testing.expect(sizeof(&[_]u32{ 4, 5, 6 }) == ptr_size); + + try testing.expect(sizeof(*u32) == ptr_size); + try testing.expect(sizeof([*]u32) == ptr_size); + try testing.expect(sizeof([*c]u32) == ptr_size); + try testing.expect(sizeof(?*u32) == ptr_size); + try testing.expect(sizeof(?[*]u32) == ptr_size); + try testing.expect(sizeof(*c_void) == ptr_size); + try testing.expect(sizeof(*void) == ptr_size); + try testing.expect(sizeof(null) == ptr_size); + + try testing.expect(sizeof("foobar") == 7); + try testing.expect(sizeof(&[_:0]u16{ 'f', 'o', 'o', 'b', 'a', 'r' }) == 14); + try testing.expect(sizeof(*const [4:0]u8) == 5); + try testing.expect(sizeof(*[4:0]u8) == ptr_size); + try testing.expect(sizeof([*]const [4:0]u8) == ptr_size); + try testing.expect(sizeof(*const *const [4:0]u8) == ptr_size); + try testing.expect(sizeof(*const [4]u8) == ptr_size); + + try testing.expect(sizeof(sizeof) == @sizeOf(@TypeOf(sizeof))); + + try testing.expect(sizeof(void) == 1); + try testing.expect(sizeof(c_void) == 1); +} + +pub const CIntLiteralRadix = enum { decimal, octal, hexadecimal }; + +fn PromoteIntLiteralReturnType(comptime SuffixType: type, comptime number: comptime_int, comptime radix: CIntLiteralRadix) type { + const signed_decimal = [_]type{ c_int, c_long, c_longlong }; + const signed_oct_hex = [_]type{ c_int, c_uint, c_long, c_ulong, c_longlong, c_ulonglong }; + const unsigned = [_]type{ c_uint, c_ulong, c_ulonglong }; + + const list: []const type = if (@typeInfo(SuffixType).Int.signedness == .unsigned) + &unsigned + else if (radix == .decimal) + &signed_decimal + else + &signed_oct_hex; + + var pos = mem.indexOfScalar(type, list, SuffixType).?; + + while (pos < list.len) : (pos += 1) { + if (number >= math.minInt(list[pos]) and number <= math.maxInt(list[pos])) { + return list[pos]; + } + } + @compileError("Integer literal is too large"); +} + +/// Promote the type of an integer literal until it fits as C would. +pub fn promoteIntLiteral( + comptime SuffixType: type, + comptime number: comptime_int, + comptime radix: CIntLiteralRadix, +) PromoteIntLiteralReturnType(SuffixType, number, radix) { + return number; +} + +test "promoteIntLiteral" { + const signed_hex = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .hexadecimal); + try testing.expectEqual(c_uint, @TypeOf(signed_hex)); + + if (math.maxInt(c_longlong) == math.maxInt(c_int)) return; + + const signed_decimal = promoteIntLiteral(c_int, math.maxInt(c_int) + 1, .decimal); + const unsigned = promoteIntLiteral(c_uint, math.maxInt(c_uint) + 1, .hexadecimal); + + if (math.maxInt(c_long) > math.maxInt(c_int)) { + try testing.expectEqual(c_long, @TypeOf(signed_decimal)); + try testing.expectEqual(c_ulong, @TypeOf(unsigned)); + } else { + try testing.expectEqual(c_longlong, @TypeOf(signed_decimal)); + try testing.expectEqual(c_ulonglong, @TypeOf(unsigned)); + } +} + +/// Convert from clang __builtin_shufflevector index to Zig @shuffle index +/// clang requires __builtin_shufflevector index arguments to be integer constants. +/// negative values for `this_index` indicate "don't care" so we arbitrarily choose 0 +/// clang enforces that `this_index` is less than the total number of vector elements +/// See https://ziglang.org/documentation/master/#shuffle +/// See https://clang.llvm.org/docs/LanguageExtensions.html#langext-builtin-shufflevector +pub fn shuffleVectorIndex(comptime this_index: c_int, comptime source_vector_len: usize) i32 { + if (this_index <= 0) return 0; + + const positive_index = @intCast(usize, this_index); + if (positive_index < source_vector_len) return @intCast(i32, this_index); + const b_index = positive_index - source_vector_len; + return ~@intCast(i32, b_index); +} + +test "shuffleVectorIndex" { + const vector_len: usize = 4; + + try testing.expect(shuffleVectorIndex(-1, vector_len) == 0); + + try testing.expect(shuffleVectorIndex(0, vector_len) == 0); + try testing.expect(shuffleVectorIndex(1, vector_len) == 1); + try testing.expect(shuffleVectorIndex(2, vector_len) == 2); + try testing.expect(shuffleVectorIndex(3, vector_len) == 3); + + try testing.expect(shuffleVectorIndex(4, vector_len) == -1); + try testing.expect(shuffleVectorIndex(5, vector_len) == -2); + try testing.expect(shuffleVectorIndex(6, vector_len) == -3); + try testing.expect(shuffleVectorIndex(7, vector_len) == -4); +} + +/// Constructs a [*c] pointer with the const and volatile annotations +/// from SelfType for pointing to a C flexible array of ElementType. +pub fn FlexibleArrayType(comptime SelfType: type, ElementType: type) type { + switch (@typeInfo(SelfType)) { + .Pointer => |ptr| { + return @Type(.{ .Pointer = .{ + .size = .C, + .is_const = ptr.is_const, + .is_volatile = ptr.is_volatile, + .alignment = @alignOf(ElementType), + .child = ElementType, + .is_allowzero = true, + .sentinel = null, + } }); + }, + else => |info| @compileError("Invalid self type \"" ++ @tagName(info) ++ "\" for flexible array getter: " ++ @typeName(SelfType)), + } +} + +test "Flexible Array Type" { + const Container = extern struct { + size: usize, + }; + + try testing.expectEqual(FlexibleArrayType(*Container, c_int), [*c]c_int); + try testing.expectEqual(FlexibleArrayType(*const Container, c_int), [*c]const c_int); + try testing.expectEqual(FlexibleArrayType(*volatile Container, c_int), [*c]volatile c_int); + try testing.expectEqual(FlexibleArrayType(*const volatile Container, c_int), [*c]const volatile c_int); +} diff --git a/src/translate_c.zig b/src/translate_c.zig index 2863377aad..4b07618391 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -12,7 +12,6 @@ const meta = std.meta; const ast = @import("translate_c/ast.zig"); const Node = ast.Node; const Tag = Node.Tag; -const c_builtins = std.c.builtins; const CallingConvention = std.builtin.CallingConvention; @@ -863,7 +862,7 @@ fn buildFlexibleArrayFn( defer block_scope.deinit(); const intermediate_type_name = try block_scope.makeMangledName(c, "Intermediate"); - const intermediate_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = u8_type }); + const intermediate_type = try Tag.helpers_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = u8_type }); const intermediate_type_decl = try Tag.var_simple.create(c.arena, .{ .name = intermediate_type_name, .init = intermediate_type, @@ -872,7 +871,7 @@ fn buildFlexibleArrayFn( const intermediate_type_ident = try Tag.identifier.create(c.arena, intermediate_type_name); const return_type_name = try block_scope.makeMangledName(c, "ReturnType"); - const return_type = try Tag.std_meta_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = element_type }); + const return_type = try Tag.helpers_flexible_array_type.create(c.arena, .{ .lhs = self_type, .rhs = element_type }); const return_type_decl = try Tag.var_simple.create(c.arena, .{ .name = return_type_name, .init = return_type, @@ -1290,9 +1289,19 @@ fn transStmt( return maybeSuppressResult(c, scope, result_used, shuffle_vec_node); }, // When adding new cases here, see comment for maybeBlockify() - else => { - return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)}); - }, + .GCCAsmStmtClass, + .GotoStmtClass, + .IndirectGotoStmtClass, + .AttributedStmtClass, + .AddrLabelExprClass, + .AtomicExprClass, + .BlockExprClass, + .UserDefinedLiteralClass, + .BuiltinBitCastExprClass, + .DesignatedInitExprClass, + .LabelStmtClass, + => return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "TODO implement translation of stmt class {s}", .{@tagName(sc)}), + else => return fail(c, error.UnsupportedTranslation, stmt.getBeginLoc(), "unsupported stmt class {s}", .{@tagName(sc)}), } } @@ -1374,7 +1383,7 @@ fn makeShuffleMask(c: *Context, scope: *Scope, expr: *const clang.ShuffleVectorE for (init_list) |*init, i| { const index_expr = try transExprCoercing(c, scope, expr.getExpr(@intCast(c_uint, i + 2)), .used); - const converted_index = try Tag.std_meta_shuffle_vector_index.create(c.arena, .{ .lhs = index_expr, .rhs = vector_len }); + const converted_index = try Tag.helpers_shuffle_vector_index.create(c.arena, .{ .lhs = index_expr, .rhs = vector_len }); init.* = converted_index; } @@ -1820,13 +1829,10 @@ fn transDeclStmtOne( .Function => { try visitFnDecl(c, @ptrCast(*const clang.FunctionDecl, decl)); }, - else => |kind| return fail( - c, - error.UnsupportedTranslation, - decl.getLocation(), - "TODO implement translation of DeclStmt kind {s}", - .{@tagName(kind)}, - ), + else => { + const decl_name = try c.str(decl.getDeclKindName()); + try warn(c, &c.global_scope.base, decl.getLocation(), "ignoring {s} declaration", .{decl_name}); + }, } } @@ -1902,7 +1908,7 @@ fn transImplicitCastExpr( const ne = try Tag.not_equal.create(c.arena, .{ .lhs = ptr_to_int, .rhs = Tag.zero_literal.init() }); return maybeSuppressResult(c, scope, result_used, ne); }, - .IntegralToBoolean => { + .IntegralToBoolean, .FloatingToBoolean => { const sub_expr_node = try transExpr(c, scope, sub_expr, .used); // The expression is already a boolean one, return it as-is @@ -1924,14 +1930,14 @@ fn transImplicitCastExpr( c, error.UnsupportedTranslation, @ptrCast(*const clang.Stmt, expr).getBeginLoc(), - "TODO implement translation of CastKind {s}", + "unsupported CastKind {s}", .{@tagName(kind)}, ), } } fn isBuiltinDefined(name: []const u8) bool { - inline for (meta.declarations(c_builtins)) |decl| { + inline for (meta.declarations(std.zig.c_builtins)) |decl| { if (std.mem.eql(u8, name, decl.name)) return true; } return false; @@ -2358,8 +2364,10 @@ fn transCCast( return Tag.float_to_int.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (!cIsFloating(src_type) and cIsFloating(dst_type)) { + var rhs = expr; + if (qualTypeIsBoolean(src_type)) rhs = try Tag.bool_to_int.create(c.arena, expr); // @intToFloat(dest_type, val) - return Tag.int_to_float.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); + return Tag.int_to_float.create(c.arena, .{ .lhs = dst_node, .rhs = rhs }); } if (qualTypeIsBoolean(src_type) and !qualTypeIsBoolean(dst_type)) { // @boolToInt returns either a comptime_int or a u1 @@ -2370,7 +2378,7 @@ fn transCCast( } if (cIsEnum(dst_type)) { // import("std").meta.cast(dest_type, val) - return Tag.std_meta_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); + return Tag.helpers_cast.create(c.arena, .{ .lhs = dst_node, .rhs = expr }); } if (cIsEnum(src_type) and !cIsEnum(dst_type)) { // @enumToInt(val) @@ -4547,6 +4555,10 @@ fn transType(c: *Context, scope: *Scope, ty: *const clang.Type, source_loc: clan .rhs = try transQualType(c, scope, element_qt, source_loc), }); }, + .ExtInt, .ExtVector => { + const type_name = c.str(ty.getTypeClassName()); + return fail(c, error.UnsupportedType, source_loc, "TODO implement translation of type: '{s}'", .{type_name}); + }, else => { const type_name = c.str(ty.getTypeClassName()); return fail(c, error.UnsupportedType, source_loc, "unsupported type: '{s}'", .{type_name}); @@ -4982,7 +4994,7 @@ fn transMacroFnDefine(c: *Context, m: *MacroCtx) ParseError!void { break :blk br.data.val; } else expr; - const return_type = if (typeof_arg.castTag(.std_meta_cast) orelse typeof_arg.castTag(.std_mem_zeroinit)) |some| + const return_type = if (typeof_arg.castTag(.helpers_cast) orelse typeof_arg.castTag(.std_mem_zeroinit)) |some| some.data.lhs else if (typeof_arg.castTag(.std_mem_zeroes)) |some| some.data @@ -5095,7 +5107,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { if (guaranteed_to_fit) { return Tag.as.create(c.arena, .{ .lhs = type_node, .rhs = literal_node }); } else { - return Tag.std_meta_promoteIntLiteral.create(c.arena, .{ + return Tag.helpers_promoteIntLiteral.create(c.arena, .{ .type = type_node, .value = literal_node, .radix = try Tag.enum_literal.create(c.arena, radix), @@ -5578,7 +5590,7 @@ fn parseCCastExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { return parseCPostfixExpr(c, m, scope, type_name); } const node_to_cast = try parseCCastExpr(c, m, scope); - return Tag.std_meta_cast.create(c.arena, .{ .lhs = type_name, .rhs = node_to_cast }); + return Tag.helpers_cast.create(c.arena, .{ .lhs = type_name, .rhs = node_to_cast }); } }, else => {}, @@ -5925,7 +5937,7 @@ fn parseCUnaryExpr(c: *Context, m: *MacroCtx, scope: *Scope) ParseError!Node { break :blk inner; } else try parseCUnaryExpr(c, m, scope); - return Tag.std_meta_sizeof.create(c.arena, operand); + return Tag.helpers_sizeof.create(c.arena, operand); }, .Keyword_alignof => { // TODO this won't work if using 's diff --git a/src/translate_c/ast.zig b/src/translate_c/ast.zig index 8ee8b670c3..59e84c6ba0 100644 --- a/src/translate_c/ast.zig +++ b/src/translate_c/ast.zig @@ -74,7 +74,7 @@ pub const Node = extern union { tuple, container_init, container_init_dot, - std_meta_cast, + helpers_cast, /// _ = operand; discard, @@ -124,8 +124,8 @@ pub const Node = extern union { std_math_Log2Int, /// @intCast(lhs, rhs) int_cast, - /// @import("std").meta.promoteIntLiteral(value, type, radix) - std_meta_promoteIntLiteral, + /// @import("std").zig.c_translation.promoteIntLiteral(value, type, radix) + helpers_promoteIntLiteral, /// @import("std").meta.alignment(value) std_meta_alignment, /// @rem(lhs, rhs) @@ -193,12 +193,12 @@ pub const Node = extern union { array_type, null_sentinel_array_type, - /// @import("std").meta.sizeof(operand) - std_meta_sizeof, - /// @import("std").meta.FlexibleArrayType(lhs, rhs) - std_meta_flexible_array_type, - /// @import("std").meta.shuffleVectorIndex(lhs, rhs) - std_meta_shuffle_vector_index, + /// @import("std").zig.c_translation.sizeof(operand) + helpers_sizeof, + /// @import("std").zig.c_translation.FlexibleArrayType(lhs, rhs) + helpers_flexible_array_type, + /// @import("std").zig.c_translation.shuffleVectorIndex(lhs, rhs) + helpers_shuffle_vector_index, /// @import("std").meta.Vector(lhs, rhs) std_meta_vector, /// @import("std").mem.zeroes(operand) @@ -272,7 +272,7 @@ pub const Node = extern union { .if_not_break, .switch_else, .block_single, - .std_meta_sizeof, + .helpers_sizeof, .std_meta_alignment, .bool_to_int, .sizeof, @@ -332,13 +332,13 @@ pub const Node = extern union { .align_cast, .array_access, .std_mem_zeroinit, - .std_meta_flexible_array_type, - .std_meta_shuffle_vector_index, + .helpers_flexible_array_type, + .helpers_shuffle_vector_index, .std_meta_vector, .ptr_cast, .div_exact, .offset_of, - .std_meta_cast, + .helpers_cast, => Payload.BinOp, .integer_literal, @@ -362,7 +362,7 @@ pub const Node = extern union { .tuple => Payload.TupleInit, .container_init => Payload.ContainerInit, .container_init_dot => Payload.ContainerInitDot, - .std_meta_promoteIntLiteral => Payload.PromoteIntLiteral, + .helpers_promoteIntLiteral => Payload.PromoteIntLiteral, .block => Payload.Block, .c_pointer, .single_pointer => Payload.Pointer, .array_type, .null_sentinel_array_type => Payload.Array, @@ -868,7 +868,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { // pub usingnamespace @import("std").c.builtins; _ = try c.addToken(.keyword_pub, "pub"); const usingnamespace_token = try c.addToken(.keyword_usingnamespace, "usingnamespace"); - const import_node = try renderStdImport(c, "c", "builtins"); + const import_node = try renderStdImport(c, &.{ "zig", "c_builtins" }); _ = try c.addToken(.semicolon, ";"); return c.addNode(.{ @@ -882,52 +882,52 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex { }, .std_math_Log2Int => { const payload = node.castTag(.std_math_Log2Int).?.data; - const import_node = try renderStdImport(c, "math", "Log2Int"); + const import_node = try renderStdImport(c, &.{ "math", "Log2Int" }); return renderCall(c, import_node, &.{payload}); }, - .std_meta_cast => { - const payload = node.castTag(.std_meta_cast).?.data; - const import_node = try renderStdImport(c, "meta", "cast"); + .helpers_cast => { + const payload = node.castTag(.helpers_cast).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "cast" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, - .std_meta_promoteIntLiteral => { - const payload = node.castTag(.std_meta_promoteIntLiteral).?.data; - const import_node = try renderStdImport(c, "meta", "promoteIntLiteral"); + .helpers_promoteIntLiteral => { + const payload = node.castTag(.helpers_promoteIntLiteral).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "promoteIntLiteral" }); return renderCall(c, import_node, &.{ payload.type, payload.value, payload.radix }); }, .std_meta_alignment => { const payload = node.castTag(.std_meta_alignment).?.data; - const import_node = try renderStdImport(c, "meta", "alignment"); + const import_node = try renderStdImport(c, &.{ "meta", "alignment" }); return renderCall(c, import_node, &.{payload}); }, - .std_meta_sizeof => { - const payload = node.castTag(.std_meta_sizeof).?.data; - const import_node = try renderStdImport(c, "meta", "sizeof"); + .helpers_sizeof => { + const payload = node.castTag(.helpers_sizeof).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "sizeof" }); return renderCall(c, import_node, &.{payload}); }, .std_mem_zeroes => { const payload = node.castTag(.std_mem_zeroes).?.data; - const import_node = try renderStdImport(c, "mem", "zeroes"); + const import_node = try renderStdImport(c, &.{ "mem", "zeroes" }); return renderCall(c, import_node, &.{payload}); }, .std_mem_zeroinit => { const payload = node.castTag(.std_mem_zeroinit).?.data; - const import_node = try renderStdImport(c, "mem", "zeroInit"); + const import_node = try renderStdImport(c, &.{ "mem", "zeroInit" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, - .std_meta_flexible_array_type => { - const payload = node.castTag(.std_meta_flexible_array_type).?.data; - const import_node = try renderStdImport(c, "meta", "FlexibleArrayType"); + .helpers_flexible_array_type => { + const payload = node.castTag(.helpers_flexible_array_type).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "FlexibleArrayType" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, - .std_meta_shuffle_vector_index => { - const payload = node.castTag(.std_meta_shuffle_vector_index).?.data; - const import_node = try renderStdImport(c, "meta", "shuffleVectorIndex"); + .helpers_shuffle_vector_index => { + const payload = node.castTag(.helpers_shuffle_vector_index).?.data; + const import_node = try renderStdImport(c, &.{ "zig", "c_translation", "shuffleVectorIndex" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, .std_meta_vector => { const payload = node.castTag(.std_meta_vector).?.data; - const import_node = try renderStdImport(c, "meta", "Vector"); + const import_node = try renderStdImport(c, &.{ "meta", "Vector" }); return renderCall(c, import_node, &.{ payload.lhs, payload.rhs }); }, .call => { @@ -2269,13 +2269,13 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex { .alignof, .typeof, .typeinfo, - .std_meta_sizeof, .std_meta_alignment, - .std_meta_cast, - .std_meta_promoteIntLiteral, .std_meta_vector, - .std_meta_shuffle_vector_index, - .std_meta_flexible_array_type, + .helpers_sizeof, + .helpers_cast, + .helpers_promoteIntLiteral, + .helpers_shuffle_vector_index, + .helpers_flexible_array_type, .std_mem_zeroinit, .integer_literal, .float_literal, @@ -2442,7 +2442,7 @@ fn renderBinOp(c: *Context, node: Node, tag: std.zig.ast.Node.Tag, tok_tag: Toke }); } -fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeIndex { +fn renderStdImport(c: *Context, parts: []const []const u8) !NodeIndex { const import_tok = try c.addToken(.builtin, "@import"); _ = try c.addToken(.l_paren, "("); const std_tok = try c.addToken(.string_literal, "\"std\""); @@ -2463,8 +2463,9 @@ fn renderStdImport(c: *Context, first: []const u8, second: []const u8) !NodeInde }); var access_chain = import_node; - access_chain = try renderFieldAccess(c, access_chain, first); - access_chain = try renderFieldAccess(c, access_chain, second); + for (parts) |part| { + access_chain = try renderFieldAccess(c, access_chain, part); + } return access_chain; } diff --git a/test/translate_c.zig b/test/translate_c.zig index d49d4329d2..21350a8600 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -133,7 +133,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ const A = @enumToInt(enum_Foo.A); \\ const B = @enumToInt(enum_Foo.B); \\ const C = @enumToInt(enum_Foo.C); - \\ var a: enum_Foo = @import("std").meta.cast(enum_Foo, B); + \\ var a: enum_Foo = @import("std").zig.c_translation.cast(enum_Foo, B); \\ { \\ const enum_Foo = extern enum( ++ default_enum_type ++ @@ -146,7 +146,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ const A_2 = @enumToInt(enum_Foo.A); \\ const B_3 = @enumToInt(enum_Foo.B); \\ const C_4 = @enumToInt(enum_Foo.C); - \\ var a_5: enum_Foo = @import("std").meta.cast(enum_Foo, B_3); + \\ var a_5: enum_Foo = @import("std").zig.c_translation.cast(enum_Foo, B_3); \\ } \\} }); @@ -242,7 +242,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED) , &[_][]const u8{ \\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*c_void { - \\ return @import("std").meta.cast(?*c_void, @import("std").meta.cast(u32, x) + SYS_BASE_CACHED); + \\ return @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED); \\} }); @@ -282,8 +282,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , \\pub const VALUE = ((((@as(c_int, 1) + (@as(c_int, 2) * @as(c_int, 3))) + (@as(c_int, 4) * @as(c_int, 5))) + @as(c_int, 6)) << @as(c_int, 7)) | @boolToInt(@as(c_int, 8) == @as(c_int, 9)); , - \\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf((@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) { - \\ return (@import("std").meta.cast([*c]u8, p).* | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").meta.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16)); + \\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf((@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) { + \\ return (@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16)); \\} }); @@ -438,17 +438,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub const struct_foo = extern struct { \\ x: c_int align(4), - \\ pub fn y(self: anytype) @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int) { - \\ const Intermediate = @import("std").meta.FlexibleArrayType(@TypeOf(self), u8); - \\ const ReturnType = @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int); + \\ pub fn y(self: anytype) @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int) { + \\ const Intermediate = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), u8); + \\ const ReturnType = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int); \\ return @ptrCast(ReturnType, @alignCast(@alignOf(c_int), @ptrCast(Intermediate, self) + 4)); \\ } \\}; \\pub const struct_bar = extern struct { \\ x: c_int align(4), - \\ pub fn y(self: anytype) @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int) { - \\ const Intermediate = @import("std").meta.FlexibleArrayType(@TypeOf(self), u8); - \\ const ReturnType = @import("std").meta.FlexibleArrayType(@TypeOf(self), c_int); + \\ pub fn y(self: anytype) @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int) { + \\ const Intermediate = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), u8); + \\ const ReturnType = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int); \\ return @ptrCast(ReturnType, @alignCast(@alignOf(c_int), @ptrCast(Intermediate, self) + 4)); \\ } \\}; @@ -583,7 +583,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { cases.add("#define hex literal with capital X", \\#define VAL 0XF00D , &[_][]const u8{ - \\pub const VAL = @import("std").meta.promoteIntLiteral(c_int, 0xF00D, .hexadecimal); + \\pub const VAL = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0xF00D, .hexadecimal); }); cases.add("anonymous struct & unions", @@ -1738,7 +1738,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const e = @enumToInt(enum_unnamed_1.e); \\pub const f = @enumToInt(enum_unnamed_1.f); \\pub const g = @enumToInt(enum_unnamed_1.g); - \\pub export var h: enum_unnamed_1 = @import("std").meta.cast(enum_unnamed_1, e); + \\pub export var h: enum_unnamed_1 = @import("std").zig.c_translation.cast(enum_unnamed_1, e); \\const enum_unnamed_2 = extern enum( ++ default_enum_type ++ \\) { @@ -1882,7 +1882,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\typedef struct { int dummy; } NRF_GPIO_Type; \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) , &[_][]const u8{ - \\pub const NRF_GPIO = @import("std").meta.cast([*c]NRF_GPIO_Type, NRF_GPIO_BASE); + \\pub const NRF_GPIO = @import("std").zig.c_translation.cast([*c]NRF_GPIO_Type, NRF_GPIO_BASE); }); cases.add("basic macro function", @@ -2379,7 +2379,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ var a = arg_a; \\ var b = arg_b; \\ var c = arg_c; - \\ var d: enum_Foo = @import("std").meta.cast(enum_Foo, FooA); + \\ var d: enum_Foo = @import("std").zig.c_translation.cast(enum_Foo, FooA); \\ var e: c_int = @boolToInt((a != 0) and (b != 0)); \\ var f: c_int = @boolToInt((b != 0) and (c != null)); \\ var g: c_int = @boolToInt((a != 0) and (c != null)); @@ -3182,13 +3182,13 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define BAR (void*) a \\#define BAZ (uint32_t)(2) , &[_][]const u8{ - \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").meta.cast(?*c_void, baz))) { - \\ return baz(@import("std").meta.cast(?*c_void, baz)); + \\pub inline fn FOO(bar: anytype) @TypeOf(baz(@import("std").zig.c_translation.cast(?*c_void, baz))) { + \\ return baz(@import("std").zig.c_translation.cast(?*c_void, baz)); \\} , - \\pub const BAR = @import("std").meta.cast(?*c_void, a); + \\pub const BAR = @import("std").zig.c_translation.cast(?*c_void, a); , - \\pub const BAZ = @import("std").meta.cast(u32, @as(c_int, 2)); + \\pub const BAZ = @import("std").zig.c_translation.cast(u32, @as(c_int, 2)); }); cases.add("macro with cast to unsigned short, long, and long long", @@ -3196,9 +3196,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define CURLAUTH_BASIC ((unsigned long) 1) \\#define CURLAUTH_BASIC_BUT_ULONGLONG ((unsigned long long) 1) , &[_][]const u8{ - \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").meta.cast(c_ushort, @as(c_int, 1)); - \\pub const CURLAUTH_BASIC = @import("std").meta.cast(c_ulong, @as(c_int, 1)); - \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").meta.cast(c_ulonglong, @as(c_int, 1)); + \\pub const CURLAUTH_BASIC_BUT_USHORT = @import("std").zig.c_translation.cast(c_ushort, @as(c_int, 1)); + \\pub const CURLAUTH_BASIC = @import("std").zig.c_translation.cast(c_ulong, @as(c_int, 1)); + \\pub const CURLAUTH_BASIC_BUT_ULONGLONG = @import("std").zig.c_translation.cast(c_ulonglong, @as(c_int, 1)); }); cases.add("macro conditional operator", @@ -3413,8 +3413,8 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define DefaultScreen(dpy) (((_XPrivDisplay)(dpy))->default_screen) \\ , &[_][]const u8{ - \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen) { - \\ return @import("std").meta.cast(_XPrivDisplay, dpy).*.default_screen; + \\pub inline fn DefaultScreen(dpy: anytype) @TypeOf(@import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen) { + \\ return @import("std").zig.c_translation.cast(_XPrivDisplay, dpy).*.default_screen; \\} }); @@ -3422,9 +3422,9 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define NULL ((void*)0) \\#define FOO ((int)0x8000) , &[_][]const u8{ - \\pub const NULL = @import("std").meta.cast(?*c_void, @as(c_int, 0)); + \\pub const NULL = @import("std").zig.c_translation.cast(?*c_void, @as(c_int, 0)); , - \\pub const FOO = @import("std").meta.cast(c_int, @import("std").meta.promoteIntLiteral(c_int, 0x8000, .hexadecimal)); + \\pub const FOO = @import("std").zig.c_translation.cast(c_int, @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x8000, .hexadecimal)); }); if (std.Target.current.abi == .msvc) { @@ -3503,11 +3503,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\pub const GUARANTEED_TO_FIT_1 = @as(c_int, 1024); \\pub const GUARANTEED_TO_FIT_2 = @as(c_long, 10241024); \\pub const GUARANTEED_TO_FIT_3 = @as(c_ulong, 20482048); - \\pub const MAY_NEED_PROMOTION_1 = @import("std").meta.promoteIntLiteral(c_int, 10241024, .decimal); - \\pub const MAY_NEED_PROMOTION_2 = @import("std").meta.promoteIntLiteral(c_long, 307230723072, .decimal); - \\pub const MAY_NEED_PROMOTION_3 = @import("std").meta.promoteIntLiteral(c_ulong, 819281928192, .decimal); - \\pub const MAY_NEED_PROMOTION_HEX = @import("std").meta.promoteIntLiteral(c_int, 0x80000000, .hexadecimal); - \\pub const MAY_NEED_PROMOTION_OCT = @import("std").meta.promoteIntLiteral(c_int, 0o20000000000, .octal); + \\pub const MAY_NEED_PROMOTION_1 = @import("std").zig.c_translation.promoteIntLiteral(c_int, 10241024, .decimal); + \\pub const MAY_NEED_PROMOTION_2 = @import("std").zig.c_translation.promoteIntLiteral(c_long, 307230723072, .decimal); + \\pub const MAY_NEED_PROMOTION_3 = @import("std").zig.c_translation.promoteIntLiteral(c_ulong, 819281928192, .decimal); + \\pub const MAY_NEED_PROMOTION_HEX = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0x80000000, .hexadecimal); + \\pub const MAY_NEED_PROMOTION_OCT = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0o20000000000, .octal); }); // See __builtin_alloca_with_align comment in std.c.builtins @@ -3642,7 +3642,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\typedef long long LONG_PTR; \\#define INVALID_HANDLE_VALUE ((void *)(LONG_PTR)-1) , &[_][]const u8{ - \\pub const MAP_FAILED = @import("std").meta.cast(?*c_void, -@as(c_int, 1)); - \\pub const INVALID_HANDLE_VALUE = @import("std").meta.cast(?*c_void, @import("std").meta.cast(LONG_PTR, -@as(c_int, 1))); + \\pub const MAP_FAILED = @import("std").zig.c_translation.cast(?*c_void, -@as(c_int, 1)); + \\pub const INVALID_HANDLE_VALUE = @import("std").zig.c_translation.cast(?*c_void, @import("std").zig.c_translation.cast(LONG_PTR, -@as(c_int, 1))); }); } From d892d3bbe782c7e5c5a5907c0afd37c3a3efa97f Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Sat, 12 Jun 2021 23:35:30 +0300 Subject: [PATCH 21/22] replace usage of meta.cast with builtins You weren't supposed to use these >:( --- lib/std/crypto/pcurves/p256/p256_64.zig | 317 +++++++++--------- .../crypto/pcurves/p256/p256_scalar_64.zig | 289 ++++++++-------- 2 files changed, 302 insertions(+), 304 deletions(-) diff --git a/lib/std/crypto/pcurves/p256/p256_64.zig b/lib/std/crypto/pcurves/p256/p256_64.zig index c60ca6c110..11c7652968 100644 --- a/lib/std/crypto/pcurves/p256/p256_64.zig +++ b/lib/std/crypto/pcurves/p256/p256_64.zig @@ -18,7 +18,6 @@ // if x1 & (2^256-1) < 2^255 then x1 & (2^256-1) else (x1 & (2^256-1)) - 2^256 const std = @import("std"); -const cast = std.meta.cast; const mode = std.builtin.mode; // Checked arithmetic is disabled in non-debug modes to avoid side channels // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. @@ -148,7 +147,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x17: u64 = undefined; var x18: u1 = undefined; addcarryxU64(&x17, &x18, x16, x8, x5); - const x19 = (cast(u64, x18) + x6); + const x19 = (@as(u64, x18) + x6); var x20: u64 = undefined; var x21: u64 = undefined; mulxU64(&x20, &x21, x11, 0xffffffff00000001); @@ -161,7 +160,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x26: u64 = undefined; var x27: u1 = undefined; addcarryxU64(&x26, &x27, 0x0, x25, x22); - const x28 = (cast(u64, x27) + x23); + const x28 = (@as(u64, x27) + x23); var x29: u64 = undefined; var x30: u1 = undefined; addcarryxU64(&x29, &x30, 0x0, x11, x24); @@ -198,7 +197,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x51: u64 = undefined; var x52: u1 = undefined; addcarryxU64(&x51, &x52, x50, x42, x39); - const x53 = (cast(u64, x52) + x40); + const x53 = (@as(u64, x52) + x40); var x54: u64 = undefined; var x55: u1 = undefined; addcarryxU64(&x54, &x55, 0x0, x31, x45); @@ -213,7 +212,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme addcarryxU64(&x60, &x61, x59, x37, x51); var x62: u64 = undefined; var x63: u1 = undefined; - addcarryxU64(&x62, &x63, x61, cast(u64, x38), x53); + addcarryxU64(&x62, &x63, x61, @as(u64, x38), x53); var x64: u64 = undefined; var x65: u64 = undefined; mulxU64(&x64, &x65, x54, 0xffffffff00000001); @@ -226,7 +225,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x70: u64 = undefined; var x71: u1 = undefined; addcarryxU64(&x70, &x71, 0x0, x69, x66); - const x72 = (cast(u64, x71) + x67); + const x72 = (@as(u64, x71) + x67); var x73: u64 = undefined; var x74: u1 = undefined; addcarryxU64(&x73, &x74, 0x0, x54, x68); @@ -242,7 +241,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x81: u64 = undefined; var x82: u1 = undefined; addcarryxU64(&x81, &x82, x80, x62, x65); - const x83 = (cast(u64, x82) + cast(u64, x63)); + const x83 = (@as(u64, x82) + @as(u64, x63)); var x84: u64 = undefined; var x85: u64 = undefined; mulxU64(&x84, &x85, x2, (arg2[3])); @@ -264,7 +263,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x96: u64 = undefined; var x97: u1 = undefined; addcarryxU64(&x96, &x97, x95, x87, x84); - const x98 = (cast(u64, x97) + x85); + const x98 = (@as(u64, x97) + x85); var x99: u64 = undefined; var x100: u1 = undefined; addcarryxU64(&x99, &x100, 0x0, x75, x90); @@ -292,7 +291,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x115: u64 = undefined; var x116: u1 = undefined; addcarryxU64(&x115, &x116, 0x0, x114, x111); - const x117 = (cast(u64, x116) + x112); + const x117 = (@as(u64, x116) + x112); var x118: u64 = undefined; var x119: u1 = undefined; addcarryxU64(&x118, &x119, 0x0, x99, x113); @@ -308,7 +307,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x126: u64 = undefined; var x127: u1 = undefined; addcarryxU64(&x126, &x127, x125, x107, x110); - const x128 = (cast(u64, x127) + cast(u64, x108)); + const x128 = (@as(u64, x127) + @as(u64, x108)); var x129: u64 = undefined; var x130: u64 = undefined; mulxU64(&x129, &x130, x3, (arg2[3])); @@ -330,7 +329,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x141: u64 = undefined; var x142: u1 = undefined; addcarryxU64(&x141, &x142, x140, x132, x129); - const x143 = (cast(u64, x142) + x130); + const x143 = (@as(u64, x142) + x130); var x144: u64 = undefined; var x145: u1 = undefined; addcarryxU64(&x144, &x145, 0x0, x120, x135); @@ -358,7 +357,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x160: u64 = undefined; var x161: u1 = undefined; addcarryxU64(&x160, &x161, 0x0, x159, x156); - const x162 = (cast(u64, x161) + x157); + const x162 = (@as(u64, x161) + x157); var x163: u64 = undefined; var x164: u1 = undefined; addcarryxU64(&x163, &x164, 0x0, x144, x158); @@ -374,7 +373,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x171: u64 = undefined; var x172: u1 = undefined; addcarryxU64(&x171, &x172, x170, x152, x155); - const x173 = (cast(u64, x172) + cast(u64, x153)); + const x173 = (@as(u64, x172) + @as(u64, x153)); var x174: u64 = undefined; var x175: u1 = undefined; subborrowxU64(&x174, &x175, 0x0, x165, 0xffffffffffffffff); @@ -383,13 +382,13 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme subborrowxU64(&x176, &x177, x175, x167, 0xffffffff); var x178: u64 = undefined; var x179: u1 = undefined; - subborrowxU64(&x178, &x179, x177, x169, cast(u64, 0x0)); + subborrowxU64(&x178, &x179, x177, x169, @as(u64, 0x0)); var x180: u64 = undefined; var x181: u1 = undefined; subborrowxU64(&x180, &x181, x179, x171, 0xffffffff00000001); var x182: u64 = undefined; var x183: u1 = undefined; - subborrowxU64(&x182, &x183, x181, x173, cast(u64, 0x0)); + subborrowxU64(&x182, &x183, x181, x173, @as(u64, 0x0)); var x184: u64 = undefined; cmovznzU64(&x184, x183, x174, x165); var x185: u64 = undefined; @@ -440,7 +439,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x17: u64 = undefined; var x18: u1 = undefined; addcarryxU64(&x17, &x18, x16, x8, x5); - const x19 = (cast(u64, x18) + x6); + const x19 = (@as(u64, x18) + x6); var x20: u64 = undefined; var x21: u64 = undefined; mulxU64(&x20, &x21, x11, 0xffffffff00000001); @@ -453,7 +452,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x26: u64 = undefined; var x27: u1 = undefined; addcarryxU64(&x26, &x27, 0x0, x25, x22); - const x28 = (cast(u64, x27) + x23); + const x28 = (@as(u64, x27) + x23); var x29: u64 = undefined; var x30: u1 = undefined; addcarryxU64(&x29, &x30, 0x0, x11, x24); @@ -490,7 +489,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x51: u64 = undefined; var x52: u1 = undefined; addcarryxU64(&x51, &x52, x50, x42, x39); - const x53 = (cast(u64, x52) + x40); + const x53 = (@as(u64, x52) + x40); var x54: u64 = undefined; var x55: u1 = undefined; addcarryxU64(&x54, &x55, 0x0, x31, x45); @@ -505,7 +504,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl addcarryxU64(&x60, &x61, x59, x37, x51); var x62: u64 = undefined; var x63: u1 = undefined; - addcarryxU64(&x62, &x63, x61, cast(u64, x38), x53); + addcarryxU64(&x62, &x63, x61, @as(u64, x38), x53); var x64: u64 = undefined; var x65: u64 = undefined; mulxU64(&x64, &x65, x54, 0xffffffff00000001); @@ -518,7 +517,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x70: u64 = undefined; var x71: u1 = undefined; addcarryxU64(&x70, &x71, 0x0, x69, x66); - const x72 = (cast(u64, x71) + x67); + const x72 = (@as(u64, x71) + x67); var x73: u64 = undefined; var x74: u1 = undefined; addcarryxU64(&x73, &x74, 0x0, x54, x68); @@ -534,7 +533,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x81: u64 = undefined; var x82: u1 = undefined; addcarryxU64(&x81, &x82, x80, x62, x65); - const x83 = (cast(u64, x82) + cast(u64, x63)); + const x83 = (@as(u64, x82) + @as(u64, x63)); var x84: u64 = undefined; var x85: u64 = undefined; mulxU64(&x84, &x85, x2, (arg1[3])); @@ -556,7 +555,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x96: u64 = undefined; var x97: u1 = undefined; addcarryxU64(&x96, &x97, x95, x87, x84); - const x98 = (cast(u64, x97) + x85); + const x98 = (@as(u64, x97) + x85); var x99: u64 = undefined; var x100: u1 = undefined; addcarryxU64(&x99, &x100, 0x0, x75, x90); @@ -584,7 +583,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x115: u64 = undefined; var x116: u1 = undefined; addcarryxU64(&x115, &x116, 0x0, x114, x111); - const x117 = (cast(u64, x116) + x112); + const x117 = (@as(u64, x116) + x112); var x118: u64 = undefined; var x119: u1 = undefined; addcarryxU64(&x118, &x119, 0x0, x99, x113); @@ -600,7 +599,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x126: u64 = undefined; var x127: u1 = undefined; addcarryxU64(&x126, &x127, x125, x107, x110); - const x128 = (cast(u64, x127) + cast(u64, x108)); + const x128 = (@as(u64, x127) + @as(u64, x108)); var x129: u64 = undefined; var x130: u64 = undefined; mulxU64(&x129, &x130, x3, (arg1[3])); @@ -622,7 +621,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x141: u64 = undefined; var x142: u1 = undefined; addcarryxU64(&x141, &x142, x140, x132, x129); - const x143 = (cast(u64, x142) + x130); + const x143 = (@as(u64, x142) + x130); var x144: u64 = undefined; var x145: u1 = undefined; addcarryxU64(&x144, &x145, 0x0, x120, x135); @@ -650,7 +649,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x160: u64 = undefined; var x161: u1 = undefined; addcarryxU64(&x160, &x161, 0x0, x159, x156); - const x162 = (cast(u64, x161) + x157); + const x162 = (@as(u64, x161) + x157); var x163: u64 = undefined; var x164: u1 = undefined; addcarryxU64(&x163, &x164, 0x0, x144, x158); @@ -666,7 +665,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x171: u64 = undefined; var x172: u1 = undefined; addcarryxU64(&x171, &x172, x170, x152, x155); - const x173 = (cast(u64, x172) + cast(u64, x153)); + const x173 = (@as(u64, x172) + @as(u64, x153)); var x174: u64 = undefined; var x175: u1 = undefined; subborrowxU64(&x174, &x175, 0x0, x165, 0xffffffffffffffff); @@ -675,13 +674,13 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl subborrowxU64(&x176, &x177, x175, x167, 0xffffffff); var x178: u64 = undefined; var x179: u1 = undefined; - subborrowxU64(&x178, &x179, x177, x169, cast(u64, 0x0)); + subborrowxU64(&x178, &x179, x177, x169, @as(u64, 0x0)); var x180: u64 = undefined; var x181: u1 = undefined; subborrowxU64(&x180, &x181, x179, x171, 0xffffffff00000001); var x182: u64 = undefined; var x183: u1 = undefined; - subborrowxU64(&x182, &x183, x181, x173, cast(u64, 0x0)); + subborrowxU64(&x182, &x183, x181, x173, @as(u64, 0x0)); var x184: u64 = undefined; cmovznzU64(&x184, x183, x174, x165); var x185: u64 = undefined; @@ -728,13 +727,13 @@ pub fn add(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme subborrowxU64(&x11, &x12, x10, x3, 0xffffffff); var x13: u64 = undefined; var x14: u1 = undefined; - subborrowxU64(&x13, &x14, x12, x5, cast(u64, 0x0)); + subborrowxU64(&x13, &x14, x12, x5, @as(u64, 0x0)); var x15: u64 = undefined; var x16: u1 = undefined; subborrowxU64(&x15, &x16, x14, x7, 0xffffffff00000001); var x17: u64 = undefined; var x18: u1 = undefined; - subborrowxU64(&x17, &x18, x16, cast(u64, x8), cast(u64, 0x0)); + subborrowxU64(&x17, &x18, x16, @as(u64, x8), @as(u64, 0x0)); var x19: u64 = undefined; cmovznzU64(&x19, x18, x9, x1); var x20: u64 = undefined; @@ -774,7 +773,7 @@ pub fn sub(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x8: u1 = undefined; subborrowxU64(&x7, &x8, x6, (arg1[3]), (arg2[3])); var x9: u64 = undefined; - cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff); + cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff); var x10: u64 = undefined; var x11: u1 = undefined; addcarryxU64(&x10, &x11, 0x0, x1, x9); @@ -783,7 +782,7 @@ pub fn sub(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme addcarryxU64(&x12, &x13, x11, x3, (x9 & 0xffffffff)); var x14: u64 = undefined; var x15: u1 = undefined; - addcarryxU64(&x14, &x15, x13, x5, cast(u64, 0x0)); + addcarryxU64(&x14, &x15, x13, x5, @as(u64, 0x0)); var x16: u64 = undefined; var x17: u1 = undefined; addcarryxU64(&x16, &x17, x15, x7, (x9 & 0xffffffff00000001)); @@ -806,18 +805,18 @@ pub fn opp(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x1: u64 = undefined; var x2: u1 = undefined; - subborrowxU64(&x1, &x2, 0x0, cast(u64, 0x0), (arg1[0])); + subborrowxU64(&x1, &x2, 0x0, @as(u64, 0x0), (arg1[0])); var x3: u64 = undefined; var x4: u1 = undefined; - subborrowxU64(&x3, &x4, x2, cast(u64, 0x0), (arg1[1])); + subborrowxU64(&x3, &x4, x2, @as(u64, 0x0), (arg1[1])); var x5: u64 = undefined; var x6: u1 = undefined; - subborrowxU64(&x5, &x6, x4, cast(u64, 0x0), (arg1[2])); + subborrowxU64(&x5, &x6, x4, @as(u64, 0x0), (arg1[2])); var x7: u64 = undefined; var x8: u1 = undefined; - subborrowxU64(&x7, &x8, x6, cast(u64, 0x0), (arg1[3])); + subborrowxU64(&x7, &x8, x6, @as(u64, 0x0), (arg1[3])); var x9: u64 = undefined; - cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff); + cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff); var x10: u64 = undefined; var x11: u1 = undefined; addcarryxU64(&x10, &x11, 0x0, x1, x9); @@ -826,7 +825,7 @@ pub fn opp(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme addcarryxU64(&x12, &x13, x11, x3, (x9 & 0xffffffff)); var x14: u64 = undefined; var x15: u1 = undefined; - addcarryxU64(&x14, &x15, x13, x5, cast(u64, 0x0)); + addcarryxU64(&x14, &x15, x13, x5, @as(u64, 0x0)); var x16: u64 = undefined; var x17: u1 = undefined; addcarryxU64(&x16, &x17, x15, x7, (x9 & 0xffffffff00000001)); @@ -865,7 +864,7 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x10, &x11, 0x0, x1, x6); var x12: u64 = undefined; var x13: u1 = undefined; - addcarryxU64(&x12, &x13, x11, cast(u64, 0x0), x8); + addcarryxU64(&x12, &x13, x11, @as(u64, 0x0), x8); var x14: u64 = undefined; var x15: u1 = undefined; addcarryxU64(&x14, &x15, 0x0, x12, (arg1[1])); @@ -886,10 +885,10 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x24, &x25, 0x0, x14, x20); var x26: u64 = undefined; var x27: u1 = undefined; - addcarryxU64(&x26, &x27, x25, (cast(u64, x15) + (cast(u64, x13) + (cast(u64, x9) + x5))), x22); + addcarryxU64(&x26, &x27, x25, (@as(u64, x15) + (@as(u64, x13) + (@as(u64, x9) + x5))), x22); var x28: u64 = undefined; var x29: u1 = undefined; - addcarryxU64(&x28, &x29, x27, x2, (cast(u64, x23) + x19)); + addcarryxU64(&x28, &x29, x27, x2, (@as(u64, x23) + x19)); var x30: u64 = undefined; var x31: u1 = undefined; addcarryxU64(&x30, &x31, x29, x3, x16); @@ -898,10 +897,10 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x32, &x33, 0x0, x26, (arg1[2])); var x34: u64 = undefined; var x35: u1 = undefined; - addcarryxU64(&x34, &x35, x33, x28, cast(u64, 0x0)); + addcarryxU64(&x34, &x35, x33, x28, @as(u64, 0x0)); var x36: u64 = undefined; var x37: u1 = undefined; - addcarryxU64(&x36, &x37, x35, x30, cast(u64, 0x0)); + addcarryxU64(&x36, &x37, x35, x30, @as(u64, 0x0)); var x38: u64 = undefined; var x39: u64 = undefined; mulxU64(&x38, &x39, x32, 0xffffffff00000001); @@ -922,19 +921,19 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x48, &x49, x47, x34, x44); var x50: u64 = undefined; var x51: u1 = undefined; - addcarryxU64(&x50, &x51, x49, x36, (cast(u64, x45) + x41)); + addcarryxU64(&x50, &x51, x49, x36, (@as(u64, x45) + x41)); var x52: u64 = undefined; var x53: u1 = undefined; - addcarryxU64(&x52, &x53, x51, (cast(u64, x37) + (cast(u64, x31) + x17)), x38); + addcarryxU64(&x52, &x53, x51, (@as(u64, x37) + (@as(u64, x31) + x17)), x38); var x54: u64 = undefined; var x55: u1 = undefined; addcarryxU64(&x54, &x55, 0x0, x48, (arg1[3])); var x56: u64 = undefined; var x57: u1 = undefined; - addcarryxU64(&x56, &x57, x55, x50, cast(u64, 0x0)); + addcarryxU64(&x56, &x57, x55, x50, @as(u64, 0x0)); var x58: u64 = undefined; var x59: u1 = undefined; - addcarryxU64(&x58, &x59, x57, x52, cast(u64, 0x0)); + addcarryxU64(&x58, &x59, x57, x52, @as(u64, 0x0)); var x60: u64 = undefined; var x61: u64 = undefined; mulxU64(&x60, &x61, x54, 0xffffffff00000001); @@ -955,11 +954,11 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x70, &x71, x69, x56, x66); var x72: u64 = undefined; var x73: u1 = undefined; - addcarryxU64(&x72, &x73, x71, x58, (cast(u64, x67) + x63)); + addcarryxU64(&x72, &x73, x71, x58, (@as(u64, x67) + x63)); var x74: u64 = undefined; var x75: u1 = undefined; - addcarryxU64(&x74, &x75, x73, (cast(u64, x59) + (cast(u64, x53) + x39)), x60); - const x76 = (cast(u64, x75) + x61); + addcarryxU64(&x74, &x75, x73, (@as(u64, x59) + (@as(u64, x53) + x39)), x60); + const x76 = (@as(u64, x75) + x61); var x77: u64 = undefined; var x78: u1 = undefined; subborrowxU64(&x77, &x78, 0x0, x70, 0xffffffffffffffff); @@ -968,13 +967,13 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo subborrowxU64(&x79, &x80, x78, x72, 0xffffffff); var x81: u64 = undefined; var x82: u1 = undefined; - subborrowxU64(&x81, &x82, x80, x74, cast(u64, 0x0)); + subborrowxU64(&x81, &x82, x80, x74, @as(u64, 0x0)); var x83: u64 = undefined; var x84: u1 = undefined; subborrowxU64(&x83, &x84, x82, x76, 0xffffffff00000001); var x85: u64 = undefined; var x86: u1 = undefined; - subborrowxU64(&x85, &x86, x84, cast(u64, 0x0), cast(u64, 0x0)); + subborrowxU64(&x85, &x86, x84, @as(u64, 0x0), @as(u64, 0x0)); var x87: u64 = undefined; cmovznzU64(&x87, x86, x77, x70); var x88: u64 = undefined; @@ -1045,13 +1044,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x29, &x30, x28, x13, x25); var x31: u64 = undefined; var x32: u1 = undefined; - addcarryxU64(&x31, &x32, x30, x15, (cast(u64, x26) + x22)); + addcarryxU64(&x31, &x32, x30, x15, (@as(u64, x26) + x22)); var x33: u64 = undefined; var x34: u1 = undefined; addcarryxU64(&x33, &x34, x32, x17, x19); var x35: u64 = undefined; var x36: u1 = undefined; - addcarryxU64(&x35, &x36, x34, (cast(u64, x18) + x6), x20); + addcarryxU64(&x35, &x36, x34, (@as(u64, x18) + x6), x20); var x37: u64 = undefined; var x38: u64 = undefined; mulxU64(&x37, &x38, x1, 0x4fffffffd); @@ -1105,13 +1104,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x69, &x70, x68, x53, x65); var x71: u64 = undefined; var x72: u1 = undefined; - addcarryxU64(&x71, &x72, x70, x55, (cast(u64, x66) + x62)); + addcarryxU64(&x71, &x72, x70, x55, (@as(u64, x66) + x62)); var x73: u64 = undefined; var x74: u1 = undefined; addcarryxU64(&x73, &x74, x72, x57, x59); var x75: u64 = undefined; var x76: u1 = undefined; - addcarryxU64(&x75, &x76, x74, ((cast(u64, x58) + cast(u64, x36)) + (cast(u64, x50) + x38)), x60); + addcarryxU64(&x75, &x76, x74, ((@as(u64, x58) + @as(u64, x36)) + (@as(u64, x50) + x38)), x60); var x77: u64 = undefined; var x78: u64 = undefined; mulxU64(&x77, &x78, x2, 0x4fffffffd); @@ -1165,13 +1164,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x109, &x110, x108, x93, x105); var x111: u64 = undefined; var x112: u1 = undefined; - addcarryxU64(&x111, &x112, x110, x95, (cast(u64, x106) + x102)); + addcarryxU64(&x111, &x112, x110, x95, (@as(u64, x106) + x102)); var x113: u64 = undefined; var x114: u1 = undefined; addcarryxU64(&x113, &x114, x112, x97, x99); var x115: u64 = undefined; var x116: u1 = undefined; - addcarryxU64(&x115, &x116, x114, ((cast(u64, x98) + cast(u64, x76)) + (cast(u64, x90) + x78)), x100); + addcarryxU64(&x115, &x116, x114, ((@as(u64, x98) + @as(u64, x76)) + (@as(u64, x90) + x78)), x100); var x117: u64 = undefined; var x118: u64 = undefined; mulxU64(&x117, &x118, x3, 0x4fffffffd); @@ -1225,13 +1224,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x149, &x150, x148, x133, x145); var x151: u64 = undefined; var x152: u1 = undefined; - addcarryxU64(&x151, &x152, x150, x135, (cast(u64, x146) + x142)); + addcarryxU64(&x151, &x152, x150, x135, (@as(u64, x146) + x142)); var x153: u64 = undefined; var x154: u1 = undefined; addcarryxU64(&x153, &x154, x152, x137, x139); var x155: u64 = undefined; var x156: u1 = undefined; - addcarryxU64(&x155, &x156, x154, ((cast(u64, x138) + cast(u64, x116)) + (cast(u64, x130) + x118)), x140); + addcarryxU64(&x155, &x156, x154, ((@as(u64, x138) + @as(u64, x116)) + (@as(u64, x130) + x118)), x140); var x157: u64 = undefined; var x158: u1 = undefined; subborrowxU64(&x157, &x158, 0x0, x149, 0xffffffffffffffff); @@ -1240,13 +1239,13 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma subborrowxU64(&x159, &x160, x158, x151, 0xffffffff); var x161: u64 = undefined; var x162: u1 = undefined; - subborrowxU64(&x161, &x162, x160, x153, cast(u64, 0x0)); + subborrowxU64(&x161, &x162, x160, x153, @as(u64, 0x0)); var x163: u64 = undefined; var x164: u1 = undefined; subborrowxU64(&x163, &x164, x162, x155, 0xffffffff00000001); var x165: u64 = undefined; var x166: u1 = undefined; - subborrowxU64(&x165, &x166, x164, cast(u64, x156), cast(u64, 0x0)); + subborrowxU64(&x165, &x166, x164, @as(u64, x156), @as(u64, 0x0)); var x167: u64 = undefined; cmovznzU64(&x167, x166, x157, x149); var x168: u64 = undefined; @@ -1325,62 +1324,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { const x2 = (arg1[2]); const x3 = (arg1[1]); const x4 = (arg1[0]); - const x5 = cast(u8, (x4 & cast(u64, 0xff))); + const x5 = @truncate(u8, (x4 & @as(u64, 0xff))); const x6 = (x4 >> 8); - const x7 = cast(u8, (x6 & cast(u64, 0xff))); + const x7 = @truncate(u8, (x6 & @as(u64, 0xff))); const x8 = (x6 >> 8); - const x9 = cast(u8, (x8 & cast(u64, 0xff))); + const x9 = @truncate(u8, (x8 & @as(u64, 0xff))); const x10 = (x8 >> 8); - const x11 = cast(u8, (x10 & cast(u64, 0xff))); + const x11 = @truncate(u8, (x10 & @as(u64, 0xff))); const x12 = (x10 >> 8); - const x13 = cast(u8, (x12 & cast(u64, 0xff))); + const x13 = @truncate(u8, (x12 & @as(u64, 0xff))); const x14 = (x12 >> 8); - const x15 = cast(u8, (x14 & cast(u64, 0xff))); + const x15 = @truncate(u8, (x14 & @as(u64, 0xff))); const x16 = (x14 >> 8); - const x17 = cast(u8, (x16 & cast(u64, 0xff))); - const x18 = cast(u8, (x16 >> 8)); - const x19 = cast(u8, (x3 & cast(u64, 0xff))); + const x17 = @truncate(u8, (x16 & @as(u64, 0xff))); + const x18 = @truncate(u8, (x16 >> 8)); + const x19 = @truncate(u8, (x3 & @as(u64, 0xff))); const x20 = (x3 >> 8); - const x21 = cast(u8, (x20 & cast(u64, 0xff))); + const x21 = @truncate(u8, (x20 & @as(u64, 0xff))); const x22 = (x20 >> 8); - const x23 = cast(u8, (x22 & cast(u64, 0xff))); + const x23 = @truncate(u8, (x22 & @as(u64, 0xff))); const x24 = (x22 >> 8); - const x25 = cast(u8, (x24 & cast(u64, 0xff))); + const x25 = @truncate(u8, (x24 & @as(u64, 0xff))); const x26 = (x24 >> 8); - const x27 = cast(u8, (x26 & cast(u64, 0xff))); + const x27 = @truncate(u8, (x26 & @as(u64, 0xff))); const x28 = (x26 >> 8); - const x29 = cast(u8, (x28 & cast(u64, 0xff))); + const x29 = @truncate(u8, (x28 & @as(u64, 0xff))); const x30 = (x28 >> 8); - const x31 = cast(u8, (x30 & cast(u64, 0xff))); - const x32 = cast(u8, (x30 >> 8)); - const x33 = cast(u8, (x2 & cast(u64, 0xff))); + const x31 = @truncate(u8, (x30 & @as(u64, 0xff))); + const x32 = @truncate(u8, (x30 >> 8)); + const x33 = @truncate(u8, (x2 & @as(u64, 0xff))); const x34 = (x2 >> 8); - const x35 = cast(u8, (x34 & cast(u64, 0xff))); + const x35 = @truncate(u8, (x34 & @as(u64, 0xff))); const x36 = (x34 >> 8); - const x37 = cast(u8, (x36 & cast(u64, 0xff))); + const x37 = @truncate(u8, (x36 & @as(u64, 0xff))); const x38 = (x36 >> 8); - const x39 = cast(u8, (x38 & cast(u64, 0xff))); + const x39 = @truncate(u8, (x38 & @as(u64, 0xff))); const x40 = (x38 >> 8); - const x41 = cast(u8, (x40 & cast(u64, 0xff))); + const x41 = @truncate(u8, (x40 & @as(u64, 0xff))); const x42 = (x40 >> 8); - const x43 = cast(u8, (x42 & cast(u64, 0xff))); + const x43 = @truncate(u8, (x42 & @as(u64, 0xff))); const x44 = (x42 >> 8); - const x45 = cast(u8, (x44 & cast(u64, 0xff))); - const x46 = cast(u8, (x44 >> 8)); - const x47 = cast(u8, (x1 & cast(u64, 0xff))); + const x45 = @truncate(u8, (x44 & @as(u64, 0xff))); + const x46 = @truncate(u8, (x44 >> 8)); + const x47 = @truncate(u8, (x1 & @as(u64, 0xff))); const x48 = (x1 >> 8); - const x49 = cast(u8, (x48 & cast(u64, 0xff))); + const x49 = @truncate(u8, (x48 & @as(u64, 0xff))); const x50 = (x48 >> 8); - const x51 = cast(u8, (x50 & cast(u64, 0xff))); + const x51 = @truncate(u8, (x50 & @as(u64, 0xff))); const x52 = (x50 >> 8); - const x53 = cast(u8, (x52 & cast(u64, 0xff))); + const x53 = @truncate(u8, (x52 & @as(u64, 0xff))); const x54 = (x52 >> 8); - const x55 = cast(u8, (x54 & cast(u64, 0xff))); + const x55 = @truncate(u8, (x54 & @as(u64, 0xff))); const x56 = (x54 >> 8); - const x57 = cast(u8, (x56 & cast(u64, 0xff))); + const x57 = @truncate(u8, (x56 & @as(u64, 0xff))); const x58 = (x56 >> 8); - const x59 = cast(u8, (x58 & cast(u64, 0xff))); - const x60 = cast(u8, (x58 >> 8)); + const x59 = @truncate(u8, (x58 & @as(u64, 0xff))); + const x60 = @truncate(u8, (x58 >> 8)); out1[0] = x5; out1[1] = x7; out1[2] = x9; @@ -1430,60 +1429,60 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { pub fn fromBytes(out1: *[4]u64, arg1: [32]u8) void { @setRuntimeSafety(mode == .Debug); - const x1 = (cast(u64, (arg1[31])) << 56); - const x2 = (cast(u64, (arg1[30])) << 48); - const x3 = (cast(u64, (arg1[29])) << 40); - const x4 = (cast(u64, (arg1[28])) << 32); - const x5 = (cast(u64, (arg1[27])) << 24); - const x6 = (cast(u64, (arg1[26])) << 16); - const x7 = (cast(u64, (arg1[25])) << 8); + const x1 = (@as(u64, (arg1[31])) << 56); + const x2 = (@as(u64, (arg1[30])) << 48); + const x3 = (@as(u64, (arg1[29])) << 40); + const x4 = (@as(u64, (arg1[28])) << 32); + const x5 = (@as(u64, (arg1[27])) << 24); + const x6 = (@as(u64, (arg1[26])) << 16); + const x7 = (@as(u64, (arg1[25])) << 8); const x8 = (arg1[24]); - const x9 = (cast(u64, (arg1[23])) << 56); - const x10 = (cast(u64, (arg1[22])) << 48); - const x11 = (cast(u64, (arg1[21])) << 40); - const x12 = (cast(u64, (arg1[20])) << 32); - const x13 = (cast(u64, (arg1[19])) << 24); - const x14 = (cast(u64, (arg1[18])) << 16); - const x15 = (cast(u64, (arg1[17])) << 8); + const x9 = (@as(u64, (arg1[23])) << 56); + const x10 = (@as(u64, (arg1[22])) << 48); + const x11 = (@as(u64, (arg1[21])) << 40); + const x12 = (@as(u64, (arg1[20])) << 32); + const x13 = (@as(u64, (arg1[19])) << 24); + const x14 = (@as(u64, (arg1[18])) << 16); + const x15 = (@as(u64, (arg1[17])) << 8); const x16 = (arg1[16]); - const x17 = (cast(u64, (arg1[15])) << 56); - const x18 = (cast(u64, (arg1[14])) << 48); - const x19 = (cast(u64, (arg1[13])) << 40); - const x20 = (cast(u64, (arg1[12])) << 32); - const x21 = (cast(u64, (arg1[11])) << 24); - const x22 = (cast(u64, (arg1[10])) << 16); - const x23 = (cast(u64, (arg1[9])) << 8); + const x17 = (@as(u64, (arg1[15])) << 56); + const x18 = (@as(u64, (arg1[14])) << 48); + const x19 = (@as(u64, (arg1[13])) << 40); + const x20 = (@as(u64, (arg1[12])) << 32); + const x21 = (@as(u64, (arg1[11])) << 24); + const x22 = (@as(u64, (arg1[10])) << 16); + const x23 = (@as(u64, (arg1[9])) << 8); const x24 = (arg1[8]); - const x25 = (cast(u64, (arg1[7])) << 56); - const x26 = (cast(u64, (arg1[6])) << 48); - const x27 = (cast(u64, (arg1[5])) << 40); - const x28 = (cast(u64, (arg1[4])) << 32); - const x29 = (cast(u64, (arg1[3])) << 24); - const x30 = (cast(u64, (arg1[2])) << 16); - const x31 = (cast(u64, (arg1[1])) << 8); + const x25 = (@as(u64, (arg1[7])) << 56); + const x26 = (@as(u64, (arg1[6])) << 48); + const x27 = (@as(u64, (arg1[5])) << 40); + const x28 = (@as(u64, (arg1[4])) << 32); + const x29 = (@as(u64, (arg1[3])) << 24); + const x30 = (@as(u64, (arg1[2])) << 16); + const x31 = (@as(u64, (arg1[1])) << 8); const x32 = (arg1[0]); - const x33 = (x31 + cast(u64, x32)); + const x33 = (x31 + @as(u64, x32)); const x34 = (x30 + x33); const x35 = (x29 + x34); const x36 = (x28 + x35); const x37 = (x27 + x36); const x38 = (x26 + x37); const x39 = (x25 + x38); - const x40 = (x23 + cast(u64, x24)); + const x40 = (x23 + @as(u64, x24)); const x41 = (x22 + x40); const x42 = (x21 + x41); const x43 = (x20 + x42); const x44 = (x19 + x43); const x45 = (x18 + x44); const x46 = (x17 + x45); - const x47 = (x15 + cast(u64, x16)); + const x47 = (x15 + @as(u64, x16)); const x48 = (x14 + x47); const x49 = (x13 + x48); const x50 = (x12 + x49); const x51 = (x11 + x50); const x52 = (x10 + x51); const x53 = (x9 + x52); - const x54 = (x7 + cast(u64, x8)); + const x54 = (x7 + @as(u64, x8)); const x55 = (x6 + x54); const x56 = (x5 + x55); const x57 = (x4 + x56); @@ -1505,7 +1504,7 @@ pub fn fromBytes(out1: *[4]u64, arg1: [32]u8) void { pub fn setOne(out1: *MontgomeryDomainFieldElement) void { @setRuntimeSafety(mode == .Debug); - out1[0] = cast(u64, 0x1); + out1[0] = @as(u64, 0x1); out1[1] = 0xffffffff00000000; out1[2] = 0xffffffffffffffff; out1[3] = 0xfffffffe; @@ -1524,9 +1523,9 @@ pub fn msat(out1: *[5]u64) void { out1[0] = 0xffffffffffffffff; out1[1] = 0xffffffff; - out1[2] = cast(u64, 0x0); + out1[2] = @as(u64, 0x0); out1[3] = 0xffffffff00000001; - out1[4] = cast(u64, 0x0); + out1[4] = @as(u64, 0x0); } /// The function divstep computes a divstep. @@ -1562,11 +1561,11 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; - addcarryxU64(&x1, &x2, 0x0, (~arg1), cast(u64, 0x1)); - const x3 = (cast(u1, (x1 >> 63)) & cast(u1, ((arg3[0]) & cast(u64, 0x1)))); + addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1)); + const x3 = (@truncate(u1, (x1 >> 63)) & @truncate(u1, ((arg3[0]) & @as(u64, 0x1)))); var x4: u64 = undefined; var x5: u1 = undefined; - addcarryxU64(&x4, &x5, 0x0, (~arg1), cast(u64, 0x1)); + addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1)); var x6: u64 = undefined; cmovznzU64(&x6, x3, arg1, x4); var x7: u64 = undefined; @@ -1581,19 +1580,19 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x11, x3, (arg2[4]), (arg3[4])); var x12: u64 = undefined; var x13: u1 = undefined; - addcarryxU64(&x12, &x13, 0x0, cast(u64, 0x1), (~(arg2[0]))); + addcarryxU64(&x12, &x13, 0x0, @as(u64, 0x1), (~(arg2[0]))); var x14: u64 = undefined; var x15: u1 = undefined; - addcarryxU64(&x14, &x15, x13, cast(u64, 0x0), (~(arg2[1]))); + addcarryxU64(&x14, &x15, x13, @as(u64, 0x0), (~(arg2[1]))); var x16: u64 = undefined; var x17: u1 = undefined; - addcarryxU64(&x16, &x17, x15, cast(u64, 0x0), (~(arg2[2]))); + addcarryxU64(&x16, &x17, x15, @as(u64, 0x0), (~(arg2[2]))); var x18: u64 = undefined; var x19: u1 = undefined; - addcarryxU64(&x18, &x19, x17, cast(u64, 0x0), (~(arg2[3]))); + addcarryxU64(&x18, &x19, x17, @as(u64, 0x0), (~(arg2[3]))); var x20: u64 = undefined; var x21: u1 = undefined; - addcarryxU64(&x20, &x21, x19, cast(u64, 0x0), (~(arg2[4]))); + addcarryxU64(&x20, &x21, x19, @as(u64, 0x0), (~(arg2[4]))); var x22: u64 = undefined; cmovznzU64(&x22, x3, (arg3[0]), x12); var x23: u64 = undefined; @@ -1632,31 +1631,31 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ subborrowxU64(&x41, &x42, x40, x33, 0xffffffff); var x43: u64 = undefined; var x44: u1 = undefined; - subborrowxU64(&x43, &x44, x42, x35, cast(u64, 0x0)); + subborrowxU64(&x43, &x44, x42, x35, @as(u64, 0x0)); var x45: u64 = undefined; var x46: u1 = undefined; subborrowxU64(&x45, &x46, x44, x37, 0xffffffff00000001); var x47: u64 = undefined; var x48: u1 = undefined; - subborrowxU64(&x47, &x48, x46, cast(u64, x38), cast(u64, 0x0)); + subborrowxU64(&x47, &x48, x46, @as(u64, x38), @as(u64, 0x0)); const x49 = (arg4[3]); const x50 = (arg4[2]); const x51 = (arg4[1]); const x52 = (arg4[0]); var x53: u64 = undefined; var x54: u1 = undefined; - subborrowxU64(&x53, &x54, 0x0, cast(u64, 0x0), x52); + subborrowxU64(&x53, &x54, 0x0, @as(u64, 0x0), x52); var x55: u64 = undefined; var x56: u1 = undefined; - subborrowxU64(&x55, &x56, x54, cast(u64, 0x0), x51); + subborrowxU64(&x55, &x56, x54, @as(u64, 0x0), x51); var x57: u64 = undefined; var x58: u1 = undefined; - subborrowxU64(&x57, &x58, x56, cast(u64, 0x0), x50); + subborrowxU64(&x57, &x58, x56, @as(u64, 0x0), x50); var x59: u64 = undefined; var x60: u1 = undefined; - subborrowxU64(&x59, &x60, x58, cast(u64, 0x0), x49); + subborrowxU64(&x59, &x60, x58, @as(u64, 0x0), x49); var x61: u64 = undefined; - cmovznzU64(&x61, x60, cast(u64, 0x0), 0xffffffffffffffff); + cmovznzU64(&x61, x60, @as(u64, 0x0), 0xffffffffffffffff); var x62: u64 = undefined; var x63: u1 = undefined; addcarryxU64(&x62, &x63, 0x0, x53, x61); @@ -1665,7 +1664,7 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ addcarryxU64(&x64, &x65, x63, x55, (x61 & 0xffffffff)); var x66: u64 = undefined; var x67: u1 = undefined; - addcarryxU64(&x66, &x67, x65, x57, cast(u64, 0x0)); + addcarryxU64(&x66, &x67, x65, x57, @as(u64, 0x0)); var x68: u64 = undefined; var x69: u1 = undefined; addcarryxU64(&x68, &x69, x67, x59, (x61 & 0xffffffff00000001)); @@ -1677,17 +1676,17 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x72, x3, (arg5[2]), x66); var x73: u64 = undefined; cmovznzU64(&x73, x3, (arg5[3]), x68); - const x74 = cast(u1, (x22 & cast(u64, 0x1))); + const x74 = @truncate(u1, (x22 & @as(u64, 0x1))); var x75: u64 = undefined; - cmovznzU64(&x75, x74, cast(u64, 0x0), x7); + cmovznzU64(&x75, x74, @as(u64, 0x0), x7); var x76: u64 = undefined; - cmovznzU64(&x76, x74, cast(u64, 0x0), x8); + cmovznzU64(&x76, x74, @as(u64, 0x0), x8); var x77: u64 = undefined; - cmovznzU64(&x77, x74, cast(u64, 0x0), x9); + cmovznzU64(&x77, x74, @as(u64, 0x0), x9); var x78: u64 = undefined; - cmovznzU64(&x78, x74, cast(u64, 0x0), x10); + cmovznzU64(&x78, x74, @as(u64, 0x0), x10); var x79: u64 = undefined; - cmovznzU64(&x79, x74, cast(u64, 0x0), x11); + cmovznzU64(&x79, x74, @as(u64, 0x0), x11); var x80: u64 = undefined; var x81: u1 = undefined; addcarryxU64(&x80, &x81, 0x0, x22, x75); @@ -1704,13 +1703,13 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x89: u1 = undefined; addcarryxU64(&x88, &x89, x87, x26, x79); var x90: u64 = undefined; - cmovznzU64(&x90, x74, cast(u64, 0x0), x27); + cmovznzU64(&x90, x74, @as(u64, 0x0), x27); var x91: u64 = undefined; - cmovznzU64(&x91, x74, cast(u64, 0x0), x28); + cmovznzU64(&x91, x74, @as(u64, 0x0), x28); var x92: u64 = undefined; - cmovznzU64(&x92, x74, cast(u64, 0x0), x29); + cmovznzU64(&x92, x74, @as(u64, 0x0), x29); var x93: u64 = undefined; - cmovznzU64(&x93, x74, cast(u64, 0x0), x30); + cmovznzU64(&x93, x74, @as(u64, 0x0), x30); var x94: u64 = undefined; var x95: u1 = undefined; addcarryxU64(&x94, &x95, 0x0, x70, x90); @@ -1731,16 +1730,16 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ subborrowxU64(&x104, &x105, x103, x96, 0xffffffff); var x106: u64 = undefined; var x107: u1 = undefined; - subborrowxU64(&x106, &x107, x105, x98, cast(u64, 0x0)); + subborrowxU64(&x106, &x107, x105, x98, @as(u64, 0x0)); var x108: u64 = undefined; var x109: u1 = undefined; subborrowxU64(&x108, &x109, x107, x100, 0xffffffff00000001); var x110: u64 = undefined; var x111: u1 = undefined; - subborrowxU64(&x110, &x111, x109, cast(u64, x101), cast(u64, 0x0)); + subborrowxU64(&x110, &x111, x109, @as(u64, x101), @as(u64, 0x0)); var x112: u64 = undefined; var x113: u1 = undefined; - addcarryxU64(&x112, &x113, 0x0, x6, cast(u64, 0x1)); + addcarryxU64(&x112, &x113, 0x0, x6, @as(u64, 0x1)); const x114 = ((x80 >> 1) | ((x82 << 63) & 0xffffffffffffffff)); const x115 = ((x82 >> 1) | ((x84 << 63) & 0xffffffffffffffff)); const x116 = ((x84 >> 1) | ((x86 << 63) & 0xffffffffffffffff)); diff --git a/lib/std/crypto/pcurves/p256/p256_scalar_64.zig b/lib/std/crypto/pcurves/p256/p256_scalar_64.zig index 491988f8ee..d16bdb1316 100644 --- a/lib/std/crypto/pcurves/p256/p256_scalar_64.zig +++ b/lib/std/crypto/pcurves/p256/p256_scalar_64.zig @@ -18,7 +18,6 @@ // if x1 & (2^256-1) < 2^255 then x1 & (2^256-1) else (x1 & (2^256-1)) - 2^256 const std = @import("std"); -const cast = std.meta.cast; const mode = std.builtin.mode; // Checked arithmetic is disabled in non-debug modes to avoid side channels // The type MontgomeryDomainFieldElement is a field element in the Montgomery domain. @@ -148,7 +147,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x17: u64 = undefined; var x18: u1 = undefined; addcarryxU64(&x17, &x18, x16, x8, x5); - const x19 = (cast(u64, x18) + x6); + const x19 = (@as(u64, x18) + x6); var x20: u64 = undefined; var x21: u64 = undefined; mulxU64(&x20, &x21, x11, 0xccd1c8aaee00bc4f); @@ -173,7 +172,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x34: u64 = undefined; var x35: u1 = undefined; addcarryxU64(&x34, &x35, x33, x25, x22); - const x36 = (cast(u64, x35) + x23); + const x36 = (@as(u64, x35) + x23); var x37: u64 = undefined; var x38: u1 = undefined; addcarryxU64(&x37, &x38, 0x0, x11, x28); @@ -210,7 +209,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x59: u64 = undefined; var x60: u1 = undefined; addcarryxU64(&x59, &x60, x58, x50, x47); - const x61 = (cast(u64, x60) + x48); + const x61 = (@as(u64, x60) + x48); var x62: u64 = undefined; var x63: u1 = undefined; addcarryxU64(&x62, &x63, 0x0, x39, x53); @@ -225,7 +224,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme addcarryxU64(&x68, &x69, x67, x45, x59); var x70: u64 = undefined; var x71: u1 = undefined; - addcarryxU64(&x70, &x71, x69, cast(u64, x46), x61); + addcarryxU64(&x70, &x71, x69, @as(u64, x46), x61); var x72: u64 = undefined; var x73: u64 = undefined; mulxU64(&x72, &x73, x62, 0xccd1c8aaee00bc4f); @@ -250,7 +249,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x86: u64 = undefined; var x87: u1 = undefined; addcarryxU64(&x86, &x87, x85, x77, x74); - const x88 = (cast(u64, x87) + x75); + const x88 = (@as(u64, x87) + x75); var x89: u64 = undefined; var x90: u1 = undefined; addcarryxU64(&x89, &x90, 0x0, x62, x80); @@ -266,7 +265,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x97: u64 = undefined; var x98: u1 = undefined; addcarryxU64(&x97, &x98, x96, x70, x88); - const x99 = (cast(u64, x98) + cast(u64, x71)); + const x99 = (@as(u64, x98) + @as(u64, x71)); var x100: u64 = undefined; var x101: u64 = undefined; mulxU64(&x100, &x101, x2, (arg2[3])); @@ -288,7 +287,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x112: u64 = undefined; var x113: u1 = undefined; addcarryxU64(&x112, &x113, x111, x103, x100); - const x114 = (cast(u64, x113) + x101); + const x114 = (@as(u64, x113) + x101); var x115: u64 = undefined; var x116: u1 = undefined; addcarryxU64(&x115, &x116, 0x0, x91, x106); @@ -328,7 +327,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x139: u64 = undefined; var x140: u1 = undefined; addcarryxU64(&x139, &x140, x138, x130, x127); - const x141 = (cast(u64, x140) + x128); + const x141 = (@as(u64, x140) + x128); var x142: u64 = undefined; var x143: u1 = undefined; addcarryxU64(&x142, &x143, 0x0, x115, x133); @@ -344,7 +343,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x150: u64 = undefined; var x151: u1 = undefined; addcarryxU64(&x150, &x151, x149, x123, x141); - const x152 = (cast(u64, x151) + cast(u64, x124)); + const x152 = (@as(u64, x151) + @as(u64, x124)); var x153: u64 = undefined; var x154: u64 = undefined; mulxU64(&x153, &x154, x3, (arg2[3])); @@ -366,7 +365,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x165: u64 = undefined; var x166: u1 = undefined; addcarryxU64(&x165, &x166, x164, x156, x153); - const x167 = (cast(u64, x166) + x154); + const x167 = (@as(u64, x166) + x154); var x168: u64 = undefined; var x169: u1 = undefined; addcarryxU64(&x168, &x169, 0x0, x144, x159); @@ -406,7 +405,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x192: u64 = undefined; var x193: u1 = undefined; addcarryxU64(&x192, &x193, x191, x183, x180); - const x194 = (cast(u64, x193) + x181); + const x194 = (@as(u64, x193) + x181); var x195: u64 = undefined; var x196: u1 = undefined; addcarryxU64(&x195, &x196, 0x0, x168, x186); @@ -422,7 +421,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x203: u64 = undefined; var x204: u1 = undefined; addcarryxU64(&x203, &x204, x202, x176, x194); - const x205 = (cast(u64, x204) + cast(u64, x177)); + const x205 = (@as(u64, x204) + @as(u64, x177)); var x206: u64 = undefined; var x207: u1 = undefined; subborrowxU64(&x206, &x207, 0x0, x197, 0xf3b9cac2fc632551); @@ -437,7 +436,7 @@ pub fn mul(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme subborrowxU64(&x212, &x213, x211, x203, 0xffffffff00000000); var x214: u64 = undefined; var x215: u1 = undefined; - subborrowxU64(&x214, &x215, x213, x205, cast(u64, 0x0)); + subborrowxU64(&x214, &x215, x213, x205, @as(u64, 0x0)); var x216: u64 = undefined; cmovznzU64(&x216, x215, x206, x197); var x217: u64 = undefined; @@ -488,7 +487,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x17: u64 = undefined; var x18: u1 = undefined; addcarryxU64(&x17, &x18, x16, x8, x5); - const x19 = (cast(u64, x18) + x6); + const x19 = (@as(u64, x18) + x6); var x20: u64 = undefined; var x21: u64 = undefined; mulxU64(&x20, &x21, x11, 0xccd1c8aaee00bc4f); @@ -513,7 +512,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x34: u64 = undefined; var x35: u1 = undefined; addcarryxU64(&x34, &x35, x33, x25, x22); - const x36 = (cast(u64, x35) + x23); + const x36 = (@as(u64, x35) + x23); var x37: u64 = undefined; var x38: u1 = undefined; addcarryxU64(&x37, &x38, 0x0, x11, x28); @@ -550,7 +549,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x59: u64 = undefined; var x60: u1 = undefined; addcarryxU64(&x59, &x60, x58, x50, x47); - const x61 = (cast(u64, x60) + x48); + const x61 = (@as(u64, x60) + x48); var x62: u64 = undefined; var x63: u1 = undefined; addcarryxU64(&x62, &x63, 0x0, x39, x53); @@ -565,7 +564,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl addcarryxU64(&x68, &x69, x67, x45, x59); var x70: u64 = undefined; var x71: u1 = undefined; - addcarryxU64(&x70, &x71, x69, cast(u64, x46), x61); + addcarryxU64(&x70, &x71, x69, @as(u64, x46), x61); var x72: u64 = undefined; var x73: u64 = undefined; mulxU64(&x72, &x73, x62, 0xccd1c8aaee00bc4f); @@ -590,7 +589,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x86: u64 = undefined; var x87: u1 = undefined; addcarryxU64(&x86, &x87, x85, x77, x74); - const x88 = (cast(u64, x87) + x75); + const x88 = (@as(u64, x87) + x75); var x89: u64 = undefined; var x90: u1 = undefined; addcarryxU64(&x89, &x90, 0x0, x62, x80); @@ -606,7 +605,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x97: u64 = undefined; var x98: u1 = undefined; addcarryxU64(&x97, &x98, x96, x70, x88); - const x99 = (cast(u64, x98) + cast(u64, x71)); + const x99 = (@as(u64, x98) + @as(u64, x71)); var x100: u64 = undefined; var x101: u64 = undefined; mulxU64(&x100, &x101, x2, (arg1[3])); @@ -628,7 +627,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x112: u64 = undefined; var x113: u1 = undefined; addcarryxU64(&x112, &x113, x111, x103, x100); - const x114 = (cast(u64, x113) + x101); + const x114 = (@as(u64, x113) + x101); var x115: u64 = undefined; var x116: u1 = undefined; addcarryxU64(&x115, &x116, 0x0, x91, x106); @@ -668,7 +667,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x139: u64 = undefined; var x140: u1 = undefined; addcarryxU64(&x139, &x140, x138, x130, x127); - const x141 = (cast(u64, x140) + x128); + const x141 = (@as(u64, x140) + x128); var x142: u64 = undefined; var x143: u1 = undefined; addcarryxU64(&x142, &x143, 0x0, x115, x133); @@ -684,7 +683,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x150: u64 = undefined; var x151: u1 = undefined; addcarryxU64(&x150, &x151, x149, x123, x141); - const x152 = (cast(u64, x151) + cast(u64, x124)); + const x152 = (@as(u64, x151) + @as(u64, x124)); var x153: u64 = undefined; var x154: u64 = undefined; mulxU64(&x153, &x154, x3, (arg1[3])); @@ -706,7 +705,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x165: u64 = undefined; var x166: u1 = undefined; addcarryxU64(&x165, &x166, x164, x156, x153); - const x167 = (cast(u64, x166) + x154); + const x167 = (@as(u64, x166) + x154); var x168: u64 = undefined; var x169: u1 = undefined; addcarryxU64(&x168, &x169, 0x0, x144, x159); @@ -746,7 +745,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x192: u64 = undefined; var x193: u1 = undefined; addcarryxU64(&x192, &x193, x191, x183, x180); - const x194 = (cast(u64, x193) + x181); + const x194 = (@as(u64, x193) + x181); var x195: u64 = undefined; var x196: u1 = undefined; addcarryxU64(&x195, &x196, 0x0, x168, x186); @@ -762,7 +761,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl var x203: u64 = undefined; var x204: u1 = undefined; addcarryxU64(&x203, &x204, x202, x176, x194); - const x205 = (cast(u64, x204) + cast(u64, x177)); + const x205 = (@as(u64, x204) + @as(u64, x177)); var x206: u64 = undefined; var x207: u1 = undefined; subborrowxU64(&x206, &x207, 0x0, x197, 0xf3b9cac2fc632551); @@ -777,7 +776,7 @@ pub fn square(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEl subborrowxU64(&x212, &x213, x211, x203, 0xffffffff00000000); var x214: u64 = undefined; var x215: u1 = undefined; - subborrowxU64(&x214, &x215, x213, x205, cast(u64, 0x0)); + subborrowxU64(&x214, &x215, x213, x205, @as(u64, 0x0)); var x216: u64 = undefined; cmovznzU64(&x216, x215, x206, x197); var x217: u64 = undefined; @@ -830,7 +829,7 @@ pub fn add(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme subborrowxU64(&x15, &x16, x14, x7, 0xffffffff00000000); var x17: u64 = undefined; var x18: u1 = undefined; - subborrowxU64(&x17, &x18, x16, cast(u64, x8), cast(u64, 0x0)); + subborrowxU64(&x17, &x18, x16, @as(u64, x8), @as(u64, 0x0)); var x19: u64 = undefined; cmovznzU64(&x19, x18, x9, x1); var x20: u64 = undefined; @@ -870,7 +869,7 @@ pub fn sub(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x8: u1 = undefined; subborrowxU64(&x7, &x8, x6, (arg1[3]), (arg2[3])); var x9: u64 = undefined; - cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff); + cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff); var x10: u64 = undefined; var x11: u1 = undefined; addcarryxU64(&x10, &x11, 0x0, x1, (x9 & 0xf3b9cac2fc632551)); @@ -902,18 +901,18 @@ pub fn opp(out1: *MontgomeryDomainFieldElement, arg1: MontgomeryDomainFieldEleme var x1: u64 = undefined; var x2: u1 = undefined; - subborrowxU64(&x1, &x2, 0x0, cast(u64, 0x0), (arg1[0])); + subborrowxU64(&x1, &x2, 0x0, @as(u64, 0x0), (arg1[0])); var x3: u64 = undefined; var x4: u1 = undefined; - subborrowxU64(&x3, &x4, x2, cast(u64, 0x0), (arg1[1])); + subborrowxU64(&x3, &x4, x2, @as(u64, 0x0), (arg1[1])); var x5: u64 = undefined; var x6: u1 = undefined; - subborrowxU64(&x5, &x6, x4, cast(u64, 0x0), (arg1[2])); + subborrowxU64(&x5, &x6, x4, @as(u64, 0x0), (arg1[2])); var x7: u64 = undefined; var x8: u1 = undefined; - subborrowxU64(&x7, &x8, x6, cast(u64, 0x0), (arg1[3])); + subborrowxU64(&x7, &x8, x6, @as(u64, 0x0), (arg1[3])); var x9: u64 = undefined; - cmovznzU64(&x9, x8, cast(u64, 0x0), 0xffffffffffffffff); + cmovznzU64(&x9, x8, @as(u64, 0x0), 0xffffffffffffffff); var x10: u64 = undefined; var x11: u1 = undefined; addcarryxU64(&x10, &x11, 0x0, x1, (x9 & 0xf3b9cac2fc632551)); @@ -973,22 +972,22 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x18, &x19, 0x0, x1, x10); var x20: u64 = undefined; var x21: u1 = undefined; - addcarryxU64(&x20, &x21, x19, cast(u64, 0x0), x12); + addcarryxU64(&x20, &x21, x19, @as(u64, 0x0), x12); var x22: u64 = undefined; var x23: u1 = undefined; - addcarryxU64(&x22, &x23, x21, cast(u64, 0x0), x14); + addcarryxU64(&x22, &x23, x21, @as(u64, 0x0), x14); var x24: u64 = undefined; var x25: u1 = undefined; - addcarryxU64(&x24, &x25, x23, cast(u64, 0x0), x16); + addcarryxU64(&x24, &x25, x23, @as(u64, 0x0), x16); var x26: u64 = undefined; var x27: u1 = undefined; addcarryxU64(&x26, &x27, 0x0, x20, (arg1[1])); var x28: u64 = undefined; var x29: u1 = undefined; - addcarryxU64(&x28, &x29, x27, x22, cast(u64, 0x0)); + addcarryxU64(&x28, &x29, x27, x22, @as(u64, 0x0)); var x30: u64 = undefined; var x31: u1 = undefined; - addcarryxU64(&x30, &x31, x29, x24, cast(u64, 0x0)); + addcarryxU64(&x30, &x31, x29, x24, @as(u64, 0x0)); var x32: u64 = undefined; var x33: u64 = undefined; mulxU64(&x32, &x33, x26, 0xccd1c8aaee00bc4f); @@ -1024,16 +1023,16 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x52, &x53, x51, x30, x44); var x54: u64 = undefined; var x55: u1 = undefined; - addcarryxU64(&x54, &x55, x53, (cast(u64, x31) + (cast(u64, x25) + (cast(u64, x17) + x5))), x46); + addcarryxU64(&x54, &x55, x53, (@as(u64, x31) + (@as(u64, x25) + (@as(u64, x17) + x5))), x46); var x56: u64 = undefined; var x57: u1 = undefined; addcarryxU64(&x56, &x57, 0x0, x50, (arg1[2])); var x58: u64 = undefined; var x59: u1 = undefined; - addcarryxU64(&x58, &x59, x57, x52, cast(u64, 0x0)); + addcarryxU64(&x58, &x59, x57, x52, @as(u64, 0x0)); var x60: u64 = undefined; var x61: u1 = undefined; - addcarryxU64(&x60, &x61, x59, x54, cast(u64, 0x0)); + addcarryxU64(&x60, &x61, x59, x54, @as(u64, 0x0)); var x62: u64 = undefined; var x63: u64 = undefined; mulxU64(&x62, &x63, x56, 0xccd1c8aaee00bc4f); @@ -1069,16 +1068,16 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x82, &x83, x81, x60, x74); var x84: u64 = undefined; var x85: u1 = undefined; - addcarryxU64(&x84, &x85, x83, (cast(u64, x61) + (cast(u64, x55) + (cast(u64, x47) + x35))), x76); + addcarryxU64(&x84, &x85, x83, (@as(u64, x61) + (@as(u64, x55) + (@as(u64, x47) + x35))), x76); var x86: u64 = undefined; var x87: u1 = undefined; addcarryxU64(&x86, &x87, 0x0, x80, (arg1[3])); var x88: u64 = undefined; var x89: u1 = undefined; - addcarryxU64(&x88, &x89, x87, x82, cast(u64, 0x0)); + addcarryxU64(&x88, &x89, x87, x82, @as(u64, 0x0)); var x90: u64 = undefined; var x91: u1 = undefined; - addcarryxU64(&x90, &x91, x89, x84, cast(u64, 0x0)); + addcarryxU64(&x90, &x91, x89, x84, @as(u64, 0x0)); var x92: u64 = undefined; var x93: u64 = undefined; mulxU64(&x92, &x93, x86, 0xccd1c8aaee00bc4f); @@ -1114,8 +1113,8 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo addcarryxU64(&x112, &x113, x111, x90, x104); var x114: u64 = undefined; var x115: u1 = undefined; - addcarryxU64(&x114, &x115, x113, (cast(u64, x91) + (cast(u64, x85) + (cast(u64, x77) + x65))), x106); - const x116 = (cast(u64, x115) + (cast(u64, x107) + x95)); + addcarryxU64(&x114, &x115, x113, (@as(u64, x91) + (@as(u64, x85) + (@as(u64, x77) + x65))), x106); + const x116 = (@as(u64, x115) + (@as(u64, x107) + x95)); var x117: u64 = undefined; var x118: u1 = undefined; subborrowxU64(&x117, &x118, 0x0, x110, 0xf3b9cac2fc632551); @@ -1130,7 +1129,7 @@ pub fn fromMontgomery(out1: *NonMontgomeryDomainFieldElement, arg1: MontgomeryDo subborrowxU64(&x123, &x124, x122, x116, 0xffffffff00000000); var x125: u64 = undefined; var x126: u1 = undefined; - subborrowxU64(&x125, &x126, x124, cast(u64, 0x0), cast(u64, 0x0)); + subborrowxU64(&x125, &x126, x124, @as(u64, 0x0), @as(u64, 0x0)); var x127: u64 = undefined; cmovznzU64(&x127, x126, x117, x110); var x128: u64 = undefined; @@ -1219,7 +1218,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x41, &x42, x40, x17, x33); var x43: u64 = undefined; var x44: u1 = undefined; - addcarryxU64(&x43, &x44, x42, (cast(u64, x18) + x6), (cast(u64, x34) + x22)); + addcarryxU64(&x43, &x44, x42, (@as(u64, x18) + x6), (@as(u64, x34) + x22)); var x45: u64 = undefined; var x46: u64 = undefined; mulxU64(&x45, &x46, x1, 0x66e12d94f3d95620); @@ -1291,7 +1290,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x89, &x90, x88, x65, x81); var x91: u64 = undefined; var x92: u1 = undefined; - addcarryxU64(&x91, &x92, x90, ((cast(u64, x66) + cast(u64, x44)) + (cast(u64, x58) + x46)), (cast(u64, x82) + x70)); + addcarryxU64(&x91, &x92, x90, ((@as(u64, x66) + @as(u64, x44)) + (@as(u64, x58) + x46)), (@as(u64, x82) + x70)); var x93: u64 = undefined; var x94: u64 = undefined; mulxU64(&x93, &x94, x2, 0x66e12d94f3d95620); @@ -1363,7 +1362,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x137, &x138, x136, x113, x129); var x139: u64 = undefined; var x140: u1 = undefined; - addcarryxU64(&x139, &x140, x138, ((cast(u64, x114) + cast(u64, x92)) + (cast(u64, x106) + x94)), (cast(u64, x130) + x118)); + addcarryxU64(&x139, &x140, x138, ((@as(u64, x114) + @as(u64, x92)) + (@as(u64, x106) + x94)), (@as(u64, x130) + x118)); var x141: u64 = undefined; var x142: u64 = undefined; mulxU64(&x141, &x142, x3, 0x66e12d94f3d95620); @@ -1435,7 +1434,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma addcarryxU64(&x185, &x186, x184, x161, x177); var x187: u64 = undefined; var x188: u1 = undefined; - addcarryxU64(&x187, &x188, x186, ((cast(u64, x162) + cast(u64, x140)) + (cast(u64, x154) + x142)), (cast(u64, x178) + x166)); + addcarryxU64(&x187, &x188, x186, ((@as(u64, x162) + @as(u64, x140)) + (@as(u64, x154) + x142)), (@as(u64, x178) + x166)); var x189: u64 = undefined; var x190: u1 = undefined; subborrowxU64(&x189, &x190, 0x0, x181, 0xf3b9cac2fc632551); @@ -1450,7 +1449,7 @@ pub fn toMontgomery(out1: *MontgomeryDomainFieldElement, arg1: NonMontgomeryDoma subborrowxU64(&x195, &x196, x194, x187, 0xffffffff00000000); var x197: u64 = undefined; var x198: u1 = undefined; - subborrowxU64(&x197, &x198, x196, cast(u64, x188), cast(u64, 0x0)); + subborrowxU64(&x197, &x198, x196, @as(u64, x188), @as(u64, 0x0)); var x199: u64 = undefined; cmovznzU64(&x199, x198, x189, x181); var x200: u64 = undefined; @@ -1529,62 +1528,62 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { const x2 = (arg1[2]); const x3 = (arg1[1]); const x4 = (arg1[0]); - const x5 = cast(u8, (x4 & cast(u64, 0xff))); + const x5 = @truncate(u8, (x4 & @as(u64, 0xff))); const x6 = (x4 >> 8); - const x7 = cast(u8, (x6 & cast(u64, 0xff))); + const x7 = @truncate(u8, (x6 & @as(u64, 0xff))); const x8 = (x6 >> 8); - const x9 = cast(u8, (x8 & cast(u64, 0xff))); + const x9 = @truncate(u8, (x8 & @as(u64, 0xff))); const x10 = (x8 >> 8); - const x11 = cast(u8, (x10 & cast(u64, 0xff))); + const x11 = @truncate(u8, (x10 & @as(u64, 0xff))); const x12 = (x10 >> 8); - const x13 = cast(u8, (x12 & cast(u64, 0xff))); + const x13 = @truncate(u8, (x12 & @as(u64, 0xff))); const x14 = (x12 >> 8); - const x15 = cast(u8, (x14 & cast(u64, 0xff))); + const x15 = @truncate(u8, (x14 & @as(u64, 0xff))); const x16 = (x14 >> 8); - const x17 = cast(u8, (x16 & cast(u64, 0xff))); - const x18 = cast(u8, (x16 >> 8)); - const x19 = cast(u8, (x3 & cast(u64, 0xff))); + const x17 = @truncate(u8, (x16 & @as(u64, 0xff))); + const x18 = @truncate(u8, (x16 >> 8)); + const x19 = @truncate(u8, (x3 & @as(u64, 0xff))); const x20 = (x3 >> 8); - const x21 = cast(u8, (x20 & cast(u64, 0xff))); + const x21 = @truncate(u8, (x20 & @as(u64, 0xff))); const x22 = (x20 >> 8); - const x23 = cast(u8, (x22 & cast(u64, 0xff))); + const x23 = @truncate(u8, (x22 & @as(u64, 0xff))); const x24 = (x22 >> 8); - const x25 = cast(u8, (x24 & cast(u64, 0xff))); + const x25 = @truncate(u8, (x24 & @as(u64, 0xff))); const x26 = (x24 >> 8); - const x27 = cast(u8, (x26 & cast(u64, 0xff))); + const x27 = @truncate(u8, (x26 & @as(u64, 0xff))); const x28 = (x26 >> 8); - const x29 = cast(u8, (x28 & cast(u64, 0xff))); + const x29 = @truncate(u8, (x28 & @as(u64, 0xff))); const x30 = (x28 >> 8); - const x31 = cast(u8, (x30 & cast(u64, 0xff))); - const x32 = cast(u8, (x30 >> 8)); - const x33 = cast(u8, (x2 & cast(u64, 0xff))); + const x31 = @truncate(u8, (x30 & @as(u64, 0xff))); + const x32 = @truncate(u8, (x30 >> 8)); + const x33 = @truncate(u8, (x2 & @as(u64, 0xff))); const x34 = (x2 >> 8); - const x35 = cast(u8, (x34 & cast(u64, 0xff))); + const x35 = @truncate(u8, (x34 & @as(u64, 0xff))); const x36 = (x34 >> 8); - const x37 = cast(u8, (x36 & cast(u64, 0xff))); + const x37 = @truncate(u8, (x36 & @as(u64, 0xff))); const x38 = (x36 >> 8); - const x39 = cast(u8, (x38 & cast(u64, 0xff))); + const x39 = @truncate(u8, (x38 & @as(u64, 0xff))); const x40 = (x38 >> 8); - const x41 = cast(u8, (x40 & cast(u64, 0xff))); + const x41 = @truncate(u8, (x40 & @as(u64, 0xff))); const x42 = (x40 >> 8); - const x43 = cast(u8, (x42 & cast(u64, 0xff))); + const x43 = @truncate(u8, (x42 & @as(u64, 0xff))); const x44 = (x42 >> 8); - const x45 = cast(u8, (x44 & cast(u64, 0xff))); - const x46 = cast(u8, (x44 >> 8)); - const x47 = cast(u8, (x1 & cast(u64, 0xff))); + const x45 = @truncate(u8, (x44 & @as(u64, 0xff))); + const x46 = @truncate(u8, (x44 >> 8)); + const x47 = @truncate(u8, (x1 & @as(u64, 0xff))); const x48 = (x1 >> 8); - const x49 = cast(u8, (x48 & cast(u64, 0xff))); + const x49 = @truncate(u8, (x48 & @as(u64, 0xff))); const x50 = (x48 >> 8); - const x51 = cast(u8, (x50 & cast(u64, 0xff))); + const x51 = @truncate(u8, (x50 & @as(u64, 0xff))); const x52 = (x50 >> 8); - const x53 = cast(u8, (x52 & cast(u64, 0xff))); + const x53 = @truncate(u8, (x52 & @as(u64, 0xff))); const x54 = (x52 >> 8); - const x55 = cast(u8, (x54 & cast(u64, 0xff))); + const x55 = @truncate(u8, (x54 & @as(u64, 0xff))); const x56 = (x54 >> 8); - const x57 = cast(u8, (x56 & cast(u64, 0xff))); + const x57 = @truncate(u8, (x56 & @as(u64, 0xff))); const x58 = (x56 >> 8); - const x59 = cast(u8, (x58 & cast(u64, 0xff))); - const x60 = cast(u8, (x58 >> 8)); + const x59 = @truncate(u8, (x58 & @as(u64, 0xff))); + const x60 = @truncate(u8, (x58 >> 8)); out1[0] = x5; out1[1] = x7; out1[2] = x9; @@ -1634,60 +1633,60 @@ pub fn toBytes(out1: *[32]u8, arg1: [4]u64) void { pub fn fromBytes(out1: *[4]u64, arg1: [32]u8) void { @setRuntimeSafety(mode == .Debug); - const x1 = (cast(u64, (arg1[31])) << 56); - const x2 = (cast(u64, (arg1[30])) << 48); - const x3 = (cast(u64, (arg1[29])) << 40); - const x4 = (cast(u64, (arg1[28])) << 32); - const x5 = (cast(u64, (arg1[27])) << 24); - const x6 = (cast(u64, (arg1[26])) << 16); - const x7 = (cast(u64, (arg1[25])) << 8); + const x1 = (@as(u64, (arg1[31])) << 56); + const x2 = (@as(u64, (arg1[30])) << 48); + const x3 = (@as(u64, (arg1[29])) << 40); + const x4 = (@as(u64, (arg1[28])) << 32); + const x5 = (@as(u64, (arg1[27])) << 24); + const x6 = (@as(u64, (arg1[26])) << 16); + const x7 = (@as(u64, (arg1[25])) << 8); const x8 = (arg1[24]); - const x9 = (cast(u64, (arg1[23])) << 56); - const x10 = (cast(u64, (arg1[22])) << 48); - const x11 = (cast(u64, (arg1[21])) << 40); - const x12 = (cast(u64, (arg1[20])) << 32); - const x13 = (cast(u64, (arg1[19])) << 24); - const x14 = (cast(u64, (arg1[18])) << 16); - const x15 = (cast(u64, (arg1[17])) << 8); + const x9 = (@as(u64, (arg1[23])) << 56); + const x10 = (@as(u64, (arg1[22])) << 48); + const x11 = (@as(u64, (arg1[21])) << 40); + const x12 = (@as(u64, (arg1[20])) << 32); + const x13 = (@as(u64, (arg1[19])) << 24); + const x14 = (@as(u64, (arg1[18])) << 16); + const x15 = (@as(u64, (arg1[17])) << 8); const x16 = (arg1[16]); - const x17 = (cast(u64, (arg1[15])) << 56); - const x18 = (cast(u64, (arg1[14])) << 48); - const x19 = (cast(u64, (arg1[13])) << 40); - const x20 = (cast(u64, (arg1[12])) << 32); - const x21 = (cast(u64, (arg1[11])) << 24); - const x22 = (cast(u64, (arg1[10])) << 16); - const x23 = (cast(u64, (arg1[9])) << 8); + const x17 = (@as(u64, (arg1[15])) << 56); + const x18 = (@as(u64, (arg1[14])) << 48); + const x19 = (@as(u64, (arg1[13])) << 40); + const x20 = (@as(u64, (arg1[12])) << 32); + const x21 = (@as(u64, (arg1[11])) << 24); + const x22 = (@as(u64, (arg1[10])) << 16); + const x23 = (@as(u64, (arg1[9])) << 8); const x24 = (arg1[8]); - const x25 = (cast(u64, (arg1[7])) << 56); - const x26 = (cast(u64, (arg1[6])) << 48); - const x27 = (cast(u64, (arg1[5])) << 40); - const x28 = (cast(u64, (arg1[4])) << 32); - const x29 = (cast(u64, (arg1[3])) << 24); - const x30 = (cast(u64, (arg1[2])) << 16); - const x31 = (cast(u64, (arg1[1])) << 8); + const x25 = (@as(u64, (arg1[7])) << 56); + const x26 = (@as(u64, (arg1[6])) << 48); + const x27 = (@as(u64, (arg1[5])) << 40); + const x28 = (@as(u64, (arg1[4])) << 32); + const x29 = (@as(u64, (arg1[3])) << 24); + const x30 = (@as(u64, (arg1[2])) << 16); + const x31 = (@as(u64, (arg1[1])) << 8); const x32 = (arg1[0]); - const x33 = (x31 + cast(u64, x32)); + const x33 = (x31 + @as(u64, x32)); const x34 = (x30 + x33); const x35 = (x29 + x34); const x36 = (x28 + x35); const x37 = (x27 + x36); const x38 = (x26 + x37); const x39 = (x25 + x38); - const x40 = (x23 + cast(u64, x24)); + const x40 = (x23 + @as(u64, x24)); const x41 = (x22 + x40); const x42 = (x21 + x41); const x43 = (x20 + x42); const x44 = (x19 + x43); const x45 = (x18 + x44); const x46 = (x17 + x45); - const x47 = (x15 + cast(u64, x16)); + const x47 = (x15 + @as(u64, x16)); const x48 = (x14 + x47); const x49 = (x13 + x48); const x50 = (x12 + x49); const x51 = (x11 + x50); const x52 = (x10 + x51); const x53 = (x9 + x52); - const x54 = (x7 + cast(u64, x8)); + const x54 = (x7 + @as(u64, x8)); const x55 = (x6 + x54); const x56 = (x5 + x55); const x57 = (x4 + x56); @@ -1711,7 +1710,7 @@ pub fn setOne(out1: *MontgomeryDomainFieldElement) void { out1[0] = 0xc46353d039cdaaf; out1[1] = 0x4319055258e8617b; - out1[2] = cast(u64, 0x0); + out1[2] = @as(u64, 0x0); out1[3] = 0xffffffff; } @@ -1730,7 +1729,7 @@ pub fn msat(out1: *[5]u64) void { out1[1] = 0xbce6faada7179e84; out1[2] = 0xffffffffffffffff; out1[3] = 0xffffffff00000000; - out1[4] = cast(u64, 0x0); + out1[4] = @as(u64, 0x0); } /// The function divstep computes a divstep. @@ -1766,11 +1765,11 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x1: u64 = undefined; var x2: u1 = undefined; - addcarryxU64(&x1, &x2, 0x0, (~arg1), cast(u64, 0x1)); - const x3 = (cast(u1, (x1 >> 63)) & cast(u1, ((arg3[0]) & cast(u64, 0x1)))); + addcarryxU64(&x1, &x2, 0x0, (~arg1), @as(u64, 0x1)); + const x3 = (@as(u1, (x1 >> 63)) & @as(u1, ((arg3[0]) & @as(u64, 0x1)))); var x4: u64 = undefined; var x5: u1 = undefined; - addcarryxU64(&x4, &x5, 0x0, (~arg1), cast(u64, 0x1)); + addcarryxU64(&x4, &x5, 0x0, (~arg1), @as(u64, 0x1)); var x6: u64 = undefined; cmovznzU64(&x6, x3, arg1, x4); var x7: u64 = undefined; @@ -1785,19 +1784,19 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x11, x3, (arg2[4]), (arg3[4])); var x12: u64 = undefined; var x13: u1 = undefined; - addcarryxU64(&x12, &x13, 0x0, cast(u64, 0x1), (~(arg2[0]))); + addcarryxU64(&x12, &x13, 0x0, @as(u64, 0x1), (~(arg2[0]))); var x14: u64 = undefined; var x15: u1 = undefined; - addcarryxU64(&x14, &x15, x13, cast(u64, 0x0), (~(arg2[1]))); + addcarryxU64(&x14, &x15, x13, @as(u64, 0x0), (~(arg2[1]))); var x16: u64 = undefined; var x17: u1 = undefined; - addcarryxU64(&x16, &x17, x15, cast(u64, 0x0), (~(arg2[2]))); + addcarryxU64(&x16, &x17, x15, @as(u64, 0x0), (~(arg2[2]))); var x18: u64 = undefined; var x19: u1 = undefined; - addcarryxU64(&x18, &x19, x17, cast(u64, 0x0), (~(arg2[3]))); + addcarryxU64(&x18, &x19, x17, @as(u64, 0x0), (~(arg2[3]))); var x20: u64 = undefined; var x21: u1 = undefined; - addcarryxU64(&x20, &x21, x19, cast(u64, 0x0), (~(arg2[4]))); + addcarryxU64(&x20, &x21, x19, @as(u64, 0x0), (~(arg2[4]))); var x22: u64 = undefined; cmovznzU64(&x22, x3, (arg3[0]), x12); var x23: u64 = undefined; @@ -1842,25 +1841,25 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ subborrowxU64(&x45, &x46, x44, x37, 0xffffffff00000000); var x47: u64 = undefined; var x48: u1 = undefined; - subborrowxU64(&x47, &x48, x46, cast(u64, x38), cast(u64, 0x0)); + subborrowxU64(&x47, &x48, x46, @as(u64, x38), @as(u64, 0x0)); const x49 = (arg4[3]); const x50 = (arg4[2]); const x51 = (arg4[1]); const x52 = (arg4[0]); var x53: u64 = undefined; var x54: u1 = undefined; - subborrowxU64(&x53, &x54, 0x0, cast(u64, 0x0), x52); + subborrowxU64(&x53, &x54, 0x0, @as(u64, 0x0), x52); var x55: u64 = undefined; var x56: u1 = undefined; - subborrowxU64(&x55, &x56, x54, cast(u64, 0x0), x51); + subborrowxU64(&x55, &x56, x54, @as(u64, 0x0), x51); var x57: u64 = undefined; var x58: u1 = undefined; - subborrowxU64(&x57, &x58, x56, cast(u64, 0x0), x50); + subborrowxU64(&x57, &x58, x56, @as(u64, 0x0), x50); var x59: u64 = undefined; var x60: u1 = undefined; - subborrowxU64(&x59, &x60, x58, cast(u64, 0x0), x49); + subborrowxU64(&x59, &x60, x58, @as(u64, 0x0), x49); var x61: u64 = undefined; - cmovznzU64(&x61, x60, cast(u64, 0x0), 0xffffffffffffffff); + cmovznzU64(&x61, x60, @as(u64, 0x0), 0xffffffffffffffff); var x62: u64 = undefined; var x63: u1 = undefined; addcarryxU64(&x62, &x63, 0x0, x53, (x61 & 0xf3b9cac2fc632551)); @@ -1881,17 +1880,17 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ cmovznzU64(&x72, x3, (arg5[2]), x66); var x73: u64 = undefined; cmovznzU64(&x73, x3, (arg5[3]), x68); - const x74 = cast(u1, (x22 & cast(u64, 0x1))); + const x74 = @as(u1, (x22 & @as(u64, 0x1))); var x75: u64 = undefined; - cmovznzU64(&x75, x74, cast(u64, 0x0), x7); + cmovznzU64(&x75, x74, @as(u64, 0x0), x7); var x76: u64 = undefined; - cmovznzU64(&x76, x74, cast(u64, 0x0), x8); + cmovznzU64(&x76, x74, @as(u64, 0x0), x8); var x77: u64 = undefined; - cmovznzU64(&x77, x74, cast(u64, 0x0), x9); + cmovznzU64(&x77, x74, @as(u64, 0x0), x9); var x78: u64 = undefined; - cmovznzU64(&x78, x74, cast(u64, 0x0), x10); + cmovznzU64(&x78, x74, @as(u64, 0x0), x10); var x79: u64 = undefined; - cmovznzU64(&x79, x74, cast(u64, 0x0), x11); + cmovznzU64(&x79, x74, @as(u64, 0x0), x11); var x80: u64 = undefined; var x81: u1 = undefined; addcarryxU64(&x80, &x81, 0x0, x22, x75); @@ -1908,13 +1907,13 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ var x89: u1 = undefined; addcarryxU64(&x88, &x89, x87, x26, x79); var x90: u64 = undefined; - cmovznzU64(&x90, x74, cast(u64, 0x0), x27); + cmovznzU64(&x90, x74, @as(u64, 0x0), x27); var x91: u64 = undefined; - cmovznzU64(&x91, x74, cast(u64, 0x0), x28); + cmovznzU64(&x91, x74, @as(u64, 0x0), x28); var x92: u64 = undefined; - cmovznzU64(&x92, x74, cast(u64, 0x0), x29); + cmovznzU64(&x92, x74, @as(u64, 0x0), x29); var x93: u64 = undefined; - cmovznzU64(&x93, x74, cast(u64, 0x0), x30); + cmovznzU64(&x93, x74, @as(u64, 0x0), x30); var x94: u64 = undefined; var x95: u1 = undefined; addcarryxU64(&x94, &x95, 0x0, x70, x90); @@ -1941,10 +1940,10 @@ pub fn divstep(out1: *u64, out2: *[5]u64, out3: *[5]u64, out4: *[4]u64, out5: *[ subborrowxU64(&x108, &x109, x107, x100, 0xffffffff00000000); var x110: u64 = undefined; var x111: u1 = undefined; - subborrowxU64(&x110, &x111, x109, cast(u64, x101), cast(u64, 0x0)); + subborrowxU64(&x110, &x111, x109, @as(u64, x101), @as(u64, 0x0)); var x112: u64 = undefined; var x113: u1 = undefined; - addcarryxU64(&x112, &x113, 0x0, x6, cast(u64, 0x1)); + addcarryxU64(&x112, &x113, 0x0, x6, @as(u64, 0x1)); const x114 = ((x80 >> 1) | ((x82 << 63) & 0xffffffffffffffff)); const x115 = ((x82 >> 1) | ((x84 << 63) & 0xffffffffffffffff)); const x116 = ((x84 >> 1) | ((x86 << 63) & 0xffffffffffffffff)); From fb0ecdbe1accdb8521fa3539101941a880216292 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Sun, 13 Jun 2021 04:49:54 +0000 Subject: [PATCH 22/22] zig build: add --libc general option This new option sets a default libc paths file to be used for all LibExeObjSteps. Setting LibExeObjStep.libc_file overrides this default. This is required to allow users to cross compile projects linking system libraries without needing to patch the build.zig. --- lib/std/build.zig | 4 ++++ lib/std/special/build_runner.zig | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/lib/std/build.zig b/lib/std/build.zig index ed9e5e4ece..96af4a3071 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -58,6 +58,7 @@ pub const Builder = struct { h_dir: []const u8, install_path: []const u8, search_prefixes: ArrayList([]const u8), + libc_file: ?[]const u8 = null, installed_files: ArrayList(InstalledFile), build_root: []const u8, cache_root: []const u8, @@ -2474,6 +2475,9 @@ pub const LibExeObjStep = struct { if (self.libc_file) |libc_file| { try zig_args.append("--libc"); try zig_args.append(libc_file.getPath(self.builder)); + } else if (builder.libc_file) |libc_file| { + try zig_args.append("--libc"); + try zig_args.append(libc_file); } switch (self.build_mode) { diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 06ac832270..510dd578c3 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -110,6 +110,12 @@ pub fn main() !void { return usageAndErr(builder, false, stderr_stream); }; builder.addSearchPrefix(search_prefix); + } else if (mem.eql(u8, arg, "--libc")) { + const libc_file = nextArg(args, &arg_idx) orelse { + warn("Expected argument after --libc\n\n", .{}); + return usageAndErr(builder, false, stderr_stream); + }; + builder.libc_file = libc_file; } else if (mem.eql(u8, arg, "--color")) { const next_arg = nextArg(args, &arg_idx) orelse { warn("expected [auto|on|off] after --color", .{}); @@ -209,6 +215,7 @@ fn usage(builder: *Builder, already_ran_build: bool, out_stream: anytype) !void \\ --prefix-include-dir [path] Override default include directory path \\ \\ --search-prefix [path] Add a path to look for binaries, libraries, headers + \\ --libc [file] Provide a file which specifies libc paths \\ \\ -h, --help Print this help and exit \\ --verbose Print commands before executing them