-
-
Notifications
You must be signed in to change notification settings - Fork 337
Initial pass at new host ABI #7795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
length: usize, | ||
ret: *anyopaque, | ||
ops: *RocOps, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit different from how we're doing it in the Rust compiler. Some notes:
- To avoid needing to get into the C calling convention at all, I continued using the "all functions take a single pointer and return
void
" thing withroc_alloc
and friends.- The one exception is that I have them all take a second argument, the "env pointer" that's a pointer to whatever the host wants it to be - e.g. a pointer to an arena in case the host wants to use arena allocation for everything.
- Since this is now different enough from the signature for something like
malloc
, I figured it would be fine to require that the host handle OOM itself rather than the Roc compiler generating an extra conditional to detect null and callroc_crashed
if so. (All the hosts that anyone will use as templates to copy from will do this anyway, so I don't think it will be a problem.)
/// but it is *not* guaranteed to be null-terminated. | ||
pub const RocDbg = struct { | ||
// TODO make this be structured instead of just a string, so that | ||
// the host can format things more nicely (e.g. syntax highlighting). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I figure we can do the "structured dbg
" and "structured expect
failure" in a future PR. This is enough to unblock Hello World.
src/host_abi.zig
Outdated
/// very simple; the calling convention is just "pass a single pointer". | ||
pub const RocCall = struct { | ||
/// Function pointers that the Roc program uses, e.g. alloc, dealloc, etc. | ||
ops: *RocOps, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ops
pointer is what will actually get threaded through from one Roc function call to another behind the scenes.
arg
and ret
only get used in the outermost function wrapper that the host calls.
This means that having all of these be pointers is a minimal cost, because all of them except for ops
will only get dereferenced once per host call.
4c88c6c
to
ea748bb
Compare
Co-authored-by: Brendan Hansknecht <[email protected]> Signed-off-by: Richard Feldman <[email protected]>
Co-authored-by: Andrew Kelley <[email protected]> Signed-off-by: Richard Feldman <[email protected]>
@@ -65,7 +65,7 @@ pub fn RocOps(comptime CallEnv: type, comptime HostFns: type) type { | |||
/// If it cannot provide a non-null pointer (e.g. due to OOM), it | |||
/// must not return, and must instead do something along the lines | |||
/// of roc_crashed. | |||
pub const RocAlloc = struct { | |||
pub const RocAlloc = extern struct { | |||
alignment: usize, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this helps here since the struct has no padding, but in Zig std lib we've started using an Alignment type that stores the log2 value. It's pretty handy because you only need 6 bits to describe any alignment for a 64-bit address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, that's a cool trick! I agree that it probably doesn't help here, but that's a good one to know for future reference. 🤘
No description provided.