//! Rust mirror of the layouts in `include/louise_rt.h`. //! //! Every type here must match the C definition byte for byte; the const //! assertions below and the `_Static_assert`s in the header check both sides. use std::ffi::c_void; use std::mem::{offset_of, size_of}; /// `TL_ABI_VERSION`: generated code checks it with [`tl_abi_check`]. pub const ABI_VERSION: i64 = 12; /// A Louise value: a tagged 63-bit int (low bit 1) or a block pointer. pub type Value = usize; pub const UNIT: Value = 1; #[inline] pub fn is_ptr(v: Value) -> bool { v & 1 == 0 } #[inline] pub fn int(n: i64) -> Value { ((n as u64) << 1) as usize | 1 } #[inline] pub fn int_get(v: Value) -> i64 { (v as i64) >> 1 } #[repr(C)] pub struct Header { /// >0: local count (1 = unique); <0: shared, accessed atomically. pub rc: i32, pub tag: u16, pub scan_fsize: u8, pub flags: u8, } /// Header followed by `scan_fsize` scanned fields and a tag-defined raw area. #[repr(C)] pub struct Block { pub header: Header, fields: [Value; 0], } impl Block { /// Address of the first field (pure address arithmetic, no access). #[inline] pub fn fields_ptr(this: *mut Block) -> *mut Value { this.cast::() .wrapping_add(offset_of!(Block, fields)) .cast() } } pub const SCAN_LARGE: u8 = 0xFF; pub const FLAG_PINNED: u8 = 0x01; /// ADR 0040 Revision 2 (issue #193): a reuse token whose kept fields are dead. pub const FLAG_TOKEN: u8 = 0x02; pub const TAG_RUNTIME_MIN: u16 = 0xFF00; pub const TAG_STRING: u16 = 0xFF01; pub const TAG_BYTES: u16 = 0xFF02; pub const TAG_FLOAT: u16 = 0xFF03; pub const TAG_CLOSURE: u16 = 0xFF04; pub const TAG_REF: u16 = 0xFF05; pub const TAG_REGION: u16 = 0xFF06; pub const TAG_FRAME: u16 = 0xFF07; pub const TAG_RESOURCE: u16 = 0xFF08; /// A handler's capability: its clauses, then its marker. Never sent. pub const TAG_CAP: u16 = 0xFF09; /// An `Array` (`scan_fsize` [`SCAN_LARGE`]): the count, the elements, then /// the spare capacity in the raw area (`crate::array`). pub const TAG_ARRAY: u16 = 0xFF0A; /// An `Int` outside 63 bits: a signed limb count, then the limbs /// (`crate::int`). pub const TAG_BIGINT: u16 = 0xFF0B; /// A `bytes.Builder`: a length, a capacity, then the bytes /// (`crate::builder`). pub const TAG_BUILDER: u16 = 0xFF0C; /// A `U64` (`std/u64`, ADR 0035): one raw `u64` (`crate::u64`). pub const TAG_U64: u16 = 0xFF0D; /// A `Dec` (`std/dec`, ADR 0070): an `i128` as two raw `u64` limbs (a /// raw area is 8-byte aligned, an `i128` wants 16), 10^18-scaled /// (`crate::dec`). pub const TAG_DEC: u16 = 0xFF0E; pub const YIELD_NONE: i32 = 0; pub const YIELD_FINAL: i32 = 1; pub const YIELD_PANIC: i32 = 2; pub const YIELD_KILL: i32 = 3; /// A suspended `ctl` handler body is being cancelled (never resumed). pub const YIELD_CANCEL: i32 = 4; /// A suspended `multi ctl` continuation is being copied: its frames take /// one more reference for each one they hold (ADR 0017). pub const YIELD_CLONE: i32 = 5; /// A suspended `multi ctl` continuation is released without running its /// `defer` cleanups (its copies ran them). pub const YIELD_DISCARD: i32 = 6; /// Per-actor execution context, first argument of every generated function. #[repr(C)] pub struct Ctx { pub yielding: i32, pub reductions: i32, pub yield_marker: u64, pub yield_value: Value, pub evv: *mut c_void, /// Points at the owning `sched::Actor`. pub rt: *mut c_void, /// Arguments after the sixth of a call (ABI Revision 1): `XARGS` /// slots, the running scheduler thread's (ABI 3, ADR 0028). The /// scheduler points it at its thread's slots each time the actor /// resumes, and a callee copies its arguments on entry, before it can /// suspend, so slots shared by a thread's actors are never read across /// a switch. Kept in the context, 26 slots took 208 of the 336 bytes /// of every actor. pub xargs: *mut Value, } /// Room for call arguments beyond the six passed in registers: functions /// take up to 128 parameters, and a call through a closure passes the /// closure too (ABI 12, issue #434). Per scheduler thread, not per actor. pub const XARGS: usize = 123; /// A Louise function passed to C for the duration of an extern call /// (`tl_callback`, ADR 0015): the environment of a C trampoline. #[repr(C)] pub struct Callback { pub ctx: *mut Ctx, /// The closure, borrowed from the extern's argument. pub fun: Value, /// Identifies the thread of the extern call (0 once it returned). pub thread: usize, } /// Stop unless the program was compiled for this runtime's ABI: a stale /// runtime would misread the program's values. Called first by generated /// `main`, as `tl_root_cap` checks the number of operations of each root /// effect. #[unsafe(no_mangle)] pub extern "C" fn tl_abi_check(version: i64) { if version != ABI_VERSION { eprintln!( "louise: this program was compiled for runtime ABI {version}, but the runtime has ABI {ABI_VERSION}: rebuild the runtime (cargo build -p louise_rt) with the same Louise version" ); std::process::abort(); } } const _: () = { assert!(size_of::() == 8); assert!(size_of::
() == 8); assert!(offset_of!(Block, fields) == 8); assert!(offset_of!(Ctx, yielding) == 0); assert!(offset_of!(Ctx, reductions) == 4); assert!(offset_of!(Ctx, yield_marker) == 8); assert!(offset_of!(Ctx, yield_value) == 16); assert!(offset_of!(Ctx, xargs) == 40); assert!(size_of::() == 48); assert!(size_of::() == 24); }; #[cfg(test)] mod tests { use super::*; #[test] fn small_int_round_trip() { for n in [0, 1, -1, 42, (1 << 62) - 1, -(1 << 62)] { let v = int(n); assert!(!is_ptr(v)); assert_eq!(int_get(v), n); } assert_eq!(int(0), UNIT); } }