hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit ece30f31b9fbe130142a8ddccde0d3f456d4bd00
parent c0f80a5f0917714c0c0aca5a2c53f8da8ebd9777
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 25 May 2024 11:27:39 +0200

rt: rewrite platform_abort to use writev

Cleans up the code substantially and prints the abort message in a
single syscall.

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mrt/+freebsd/platform_abort.ha | 44++++++++++++++++++++++++++------------------
Mrt/+linux/platform_abort.ha | 44++++++++++++++++++++++++++------------------
Mrt/+netbsd/platform_abort.ha | 44++++++++++++++++++++++++++------------------
Mrt/+openbsd/platform_abort.ha | 44++++++++++++++++++++++++++------------------
Mrt/u64tos.ha | 7++++---
Mrt/unknown_errno.ha | 24++++++++++++++----------
6 files changed, 122 insertions(+), 85 deletions(-)

diff --git a/rt/+freebsd/platform_abort.ha b/rt/+freebsd/platform_abort.ha @@ -1,23 +1,31 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> +fn strvec(s: str) iovec = { + return iovec { + iov_base = *(&s: **opaque), + iov_len = len(s), + }; +}; + fn platform_abort(path: *str, line: u64, col: u64, msg: str) never = { - const prefix = "Abort: "; - const sep = ":"; - const sepspace = ": "; - const linefeed = "\n"; - write(STDERR_FILENO, *(&prefix: **opaque): *const u8, len(prefix)): void; - write(STDERR_FILENO, *(path: **opaque): *const u8, len(path)): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (line, z) = u64tos(line); - write(STDERR_FILENO, line, z): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (col, z) = u64tos(col); - write(STDERR_FILENO, col, z): void; - write(STDERR_FILENO, *(&sepspace: **opaque): *const u8, - len(sepspace)): void; - write(STDERR_FILENO, *(&msg: **opaque): *const u8, len(msg)): void; - write(STDERR_FILENO, *(&linefeed: **opaque): *const u8, 1): void; - kill(getpid(), SIGABRT): void; - for (true) void; + let linebuf: [U64_BUFSZ]u8 = [0...]; + let colbuf: [U64_BUFSZ]u8 = [0...]; + + const iov = [ + strvec("Abort: "), + strvec(*path), + strvec(":"), + strvec(u64tos(linebuf, line)), + strvec(":"), + strvec(u64tos(colbuf, col)), + strvec(": "), + strvec(msg), + strvec("\n"), + ]; + writev(STDERR_FILENO, &iov, len(iov): int): void; + + for (true) { + kill(getpid(), SIGABRT): void; + }; }; diff --git a/rt/+linux/platform_abort.ha b/rt/+linux/platform_abort.ha @@ -1,23 +1,31 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> +fn strvec(s: str) iovec = { + return iovec { + iov_base = *(&s: **opaque), + iov_len = len(s), + }; +}; + fn platform_abort(path: *str, line: u64, col: u64, msg: str) never = { - const prefix = "Abort: "; - const sep = ":"; - const sepspace = ": "; - const linefeed = "\n"; - write(STDERR_FILENO, *(&prefix: **opaque): *const u8, len(prefix)): void; - write(STDERR_FILENO, *(path: **opaque): *const u8, len(path)): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (line, z) = u64tos(line); - write(STDERR_FILENO, line, z): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (col, z) = u64tos(col); - write(STDERR_FILENO, col, z): void; - write(STDERR_FILENO, *(&sepspace: **opaque): *const u8, - len(sepspace)): void; - write(STDERR_FILENO, *(&msg: **opaque): *const u8, len(msg)): void; - write(STDERR_FILENO, *(&linefeed: **opaque): *const u8, 1): void; - kill(getpid(), SIGABRT): void; - for (true) void; + let linebuf: [U64_BUFSZ]u8 = [0...]; + let colbuf: [U64_BUFSZ]u8 = [0...]; + + const iov = [ + strvec("Abort: "), + strvec(*path), + strvec(":"), + strvec(u64tos(linebuf, line)), + strvec(":"), + strvec(u64tos(colbuf, col)), + strvec(": "), + strvec(msg), + strvec("\n"), + ]; + writev(STDERR_FILENO, &iov, len(iov): int): void; + + for (true) { + kill(getpid(), SIGABRT): void; + }; }; diff --git a/rt/+netbsd/platform_abort.ha b/rt/+netbsd/platform_abort.ha @@ -1,23 +1,31 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> +fn strvec(s: str) iovec = { + return iovec { + iov_base = *(&s: **opaque), + iov_len = len(s), + }; +}; + fn platform_abort(path: *str, line: u64, col: u64, msg: str) never = { - const prefix = "Abort: "; - const sep = ":"; - const sepspace = ": "; - const linefeed = "\n"; - write(STDERR_FILENO, *(&prefix: **opaque): *const u8, len(prefix)): void; - write(STDERR_FILENO, *(path: **opaque): *const u8, len(path)): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (line, z) = u64tos(line); - write(STDERR_FILENO, line, z): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (col, z) = u64tos(col); - write(STDERR_FILENO, col, z): void; - write(STDERR_FILENO, *(&sepspace: **opaque): *const u8, - len(sepspace)): void; - write(STDERR_FILENO, *(&msg: **opaque): *const u8, len(msg)): void; - write(STDERR_FILENO, *(&linefeed: **opaque): *const u8, 1): void; - kill(getpid(), SIGABRT): void; - for (true) void; + let linebuf: [U64_BUFSZ]u8 = [0...]; + let colbuf: [U64_BUFSZ]u8 = [0...]; + + const iov = [ + strvec("Abort: "), + strvec(*path), + strvec(":"), + strvec(u64tos(linebuf, line)), + strvec(":"), + strvec(u64tos(colbuf, col)), + strvec(": "), + strvec(msg), + strvec("\n"), + ]; + writev(STDERR_FILENO, &iov, len(iov): int): void; + + for (true) { + kill(getpid(), SIGABRT): void; + }; }; diff --git a/rt/+openbsd/platform_abort.ha b/rt/+openbsd/platform_abort.ha @@ -1,23 +1,31 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> +fn strvec(s: str) iovec = { + return iovec { + iov_base = *(&s: **opaque), + iov_len = len(s), + }; +}; + fn platform_abort(path: *str, line: u64, col: u64, msg: str) never = { - const prefix = "Abort: "; - const sep = ":"; - const sepspace = ": "; - const linefeed = "\n"; - write(STDERR_FILENO, *(&prefix: **opaque): *const u8, len(prefix)): void; - write(STDERR_FILENO, *(path: **opaque): *const u8, len(path)): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (line, z) = u64tos(line); - write(STDERR_FILENO, line, z): void; - write(STDERR_FILENO, *(&sep: **opaque): *const u8, len(sep)): void; - let (col, z) = u64tos(col); - write(STDERR_FILENO, col, z): void; - write(STDERR_FILENO, *(&sepspace: **opaque): *const u8, - len(sepspace)): void; - write(STDERR_FILENO, *(&msg: **opaque): *const u8, len(msg)): void; - write(STDERR_FILENO, *(&linefeed: **opaque): *const u8, 1): void; - kill(getpid(), SIGABRT): void; - for (true) void; + let linebuf: [U64_BUFSZ]u8 = [0...]; + let colbuf: [U64_BUFSZ]u8 = [0...]; + + const iov = [ + strvec("Abort: "), + strvec(*path), + strvec(":"), + strvec(u64tos(linebuf, line)), + strvec(":"), + strvec(u64tos(colbuf, col)), + strvec(": "), + strvec(msg), + strvec("\n"), + ]; + writev(STDERR_FILENO, &iov, len(iov): int): void; + + for (true) { + kill(getpid(), SIGABRT): void; + }; }; diff --git a/rt/u64tos.ha b/rt/u64tos.ha @@ -1,8 +1,9 @@ // SPDX-License-Identifier: MPL-2.0 // (c) Hare authors <https://harelang.org> -fn u64tos(u: u64) (*[*]const u8, size) = { - static let buf: [20]u8 = [0...]; // len("18446744073709551615") +def U64_BUFSZ = 20; + +fn u64tos(buf: []u8, u: u64) str = { let sl = buf[..0]; if (u == 0) { static append(sl, '0'); @@ -18,5 +19,5 @@ fn u64tos(u: u64) (*[*]const u8, size) = { s += 1; e -= 1; }; - return (sl: *[*]const u8, len(sl)); + return *(&sl: *str); }; diff --git a/rt/unknown_errno.ha b/rt/unknown_errno.ha @@ -3,20 +3,24 @@ fn unknown_errno(err: errno) str = { static let buf: [27]u8 = [0...]; + let ubuf: [U64_BUFSZ]u8 = [0...]; + let sl = buf[..0]; + const s = *(&"[unknown errno ": *[]u8); - buf[..len(s)] = s; + static append(sl, s...); + if (err < 0) { - buf[len(s)] = '-'; - const s2 = &u64tos(-err: u64): *string; - buf[len(s) + 1..s2.length + len(s) + 1] = s2.data[..s2.length]; - buf[s2.length + len(s) + 1] = ']'; - return *(&buf[..s2.length + len(s) + 2]: *str); + static append(sl, '-'); + const s = u64tos(ubuf, -err: u64); + static append(sl, *(&s: *[]u8)...); + static append(sl, ']'); } else { - const s2 = &u64tos(err: u64): *string; - buf[len(s)..s2.length + len(s)] = s2.data[..s2.length]; - buf[s2.length + len(s)] = ']'; - return *(&buf[..s2.length + len(s) + 1]: *str); + const s = u64tos(ubuf, err: u64); + static append(sl, *(&s: *[]u8)...); + static append(sl, ']'); }; + + return *(&sl: *str); }; @test fn unknown_errno() void = {