Skip to content

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

Merged
merged 14 commits into from
May 19, 2025
Merged

Initial pass at new host ABI #7795

merged 14 commits into from
May 19, 2025

Conversation

rtfeldman
Copy link
Contributor

No description provided.

length: usize,
ret: *anyopaque,
ops: *RocOps,
};
Copy link
Contributor Author

@rtfeldman rtfeldman May 18, 2025

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 with roc_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 call roc_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).
Copy link
Contributor Author

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,
Copy link
Contributor Author

@rtfeldman rtfeldman May 18, 2025

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.

@rtfeldman rtfeldman force-pushed the host-abi branch 2 times, most recently from 4c88c6c to ea748bb Compare May 18, 2025 16:02
bhansconnect
bhansconnect previously approved these changes May 18, 2025
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,
Copy link
Contributor

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.

Copy link
Contributor Author

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. 🤘

@rtfeldman rtfeldman merged commit e1f4ac1 into main May 19, 2025
23 checks passed
@rtfeldman rtfeldman deleted the host-abi branch May 19, 2025 23:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants