aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-07-31 15:53:30 +0200
committerHimbeer <himbeer@disroot.org>2024-07-31 15:53:30 +0200
commit4f7b3a33d10e0878d164f002fd93f8b5742e2bba (patch)
tree9588ecfb85327938d4af3bc4c049dddd51b08c20
parent1160c3a1b2c9d5532994898266371f1b042089a3 (diff)
root: Automatically cast syscall arguments depending on their type
-rw-r--r--src/root.zig14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/root.zig b/src/root.zig
index 2e6ab5d..d4d2011 100644
--- a/src/root.zig
+++ b/src/root.zig
@@ -14,11 +14,21 @@ const Result = struct {
const SyscallError = error{Kernel};
+fn autoCast(value: anytype) usize {
+ return switch (@typeInfo(@TypeOf(value))) {
+ .Type => @compileError("cannot pass type to system call"),
+ .Void => @compileError("cannot pass void to system call"),
+ .NoReturn => @compileError("cannot pass noreturn to system call"),
+ .Pointer => @intFromPtr(value),
+ else => value,
+ };
+}
+
fn ecall(number: usize, args: anytype) Result {
comptime var registers = [_]usize{0} ** max_args;
inline for (args, 0..) |arg, i| {
- if (i >= max_args) @compileError("Too many arguments to system call");
- registers[i] = @bitCast(arg);
+ if (i >= max_args) @compileError("too many arguments to system call");
+ registers[i] = autoCast(arg);
}
var result: Result = undefined;