diff options
author | Himbeer <himbeer@disroot.org> | 2024-07-31 15:53:30 +0200 |
---|---|---|
committer | Himbeer <himbeer@disroot.org> | 2024-07-31 15:53:30 +0200 |
commit | 4f7b3a33d10e0878d164f002fd93f8b5742e2bba (patch) | |
tree | 9588ecfb85327938d4af3bc4c049dddd51b08c20 | |
parent | 1160c3a1b2c9d5532994898266371f1b042089a3 (diff) |
root: Automatically cast syscall arguments depending on their type
-rw-r--r-- | src/root.zig | 14 |
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; |