[COFF] Zig (was Re: more about Brian... [really Rust])

Derek Fawcus dfawcus+lists-coff at employees.org
Sun Feb 6 09:09:19 AEST 2022


On Fri, Feb 04, 2022 at 06:18:09PM -0500, Dan Cross wrote:
> [TUHS to Bcc, +COFF <coff at minnie.tuhs.org> ]
> 
> This isn't exactly COFF material, but I don't know what list is more
> appropriate.
> 

[snip]

> However, right now? I think it
> sits at a local maxima for systems languages targeting bare-metal.

Have you played with Zig?  I've only just started, but it does seem to
be trying to address a number of the issues with C ub, and safety, 
while sticking closer to the 'C' space vs where I see Rust targetting
the 'C++' space.

It doesn't have Rust's ownership / borrow checker stuff, it does seem
to have bounds checking on arrays.

e.g. the UB for multiply example you give ends up as a run time panic
(which I suspect can be caught), or one can use a different (wrapping)
multiply operator similar to in Rust.

i.e. see the below test program and its output.

DF

$ cat main.zig
const std = @import("std");

pub fn mulOverflow(a: u16, b: u16) u16 {
    return a * b;
}

pub fn mulWrap(a: u16, b: u16) u16 {
    return a *% b;
}

pub fn main() void {
    const result1 = mulWrap(65535, 4);
    std.debug.print("mulWrap is {d}\n", .{result1});

    const result2 = mulOverflow(65535, 4);
    std.debug.print("mulOverflow is {d}\n", .{result2});
}

$ ./main
mulWrap is 65532
thread 32589 panic: integer overflow
/home/derek/Code/zig-play/main.zig:4:14: 0x2347bd in mulOverflow (main)
    return a * b;
             ^
/home/derek/Code/zig-play/main.zig:15:32: 0x22cfda in main (main)
    const result2 = mulOverflow(65535, 4);
                               ^
/usr/local/zig-linux-x86_64-0.9.0/lib/std/start.zig:543:22: 0x225d5c in std.start.callMain (main)
            root.main();
                     ^
/usr/local/zig-linux-x86_64-0.9.0/lib/std/start.zig:495:12: 0x20713e in std.start.callMainWithArgs (main)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/usr/local/zig-linux-x86_64-0.9.0/lib/std/start.zig:409:17: 0x2061d6 in std.start.posixCallMainAndExit (main)
    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
                ^
/usr/local/zig-linux-x86_64-0.9.0/lib/std/start.zig:322:5: 0x205fe2 in std.start._start (main)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
Aborted

$ zig build-exe -O ReleaseFast main.zig
$ ./main
mulWrap is 65532
mulOverflow is 65532
$ zig build-exe -O ReleaseSafe main.zig
$ ./main
mulWrap is 65532
thread 32608 panic: integer overflow
Aborted

-- 



More information about the COFF mailing list