tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

179 lignes · 5929 octetsBrut · Historique
1//! Rust mirror of the layouts in `include/louise_rt.h`.
2//!
3//! Every type here must match the C definition byte for byte; the const
4//! assertions below and the `_Static_assert`s in the header check both sides.
5
6use std::ffi::c_void;
7use std::mem::{offset_of, size_of};
8
9/// `TL_ABI_VERSION`: generated code checks it with [`tl_abi_check`].
10pub const ABI_VERSION: i64 = 12;
11
12/// A Louise value: a tagged 63-bit int (low bit 1) or a block pointer.
13pub type Value = usize;
14
15pub const UNIT: Value = 1;
16
17#[inline]
18pub fn is_ptr(v: Value) -> bool {
19 v & 1 == 0
20}
21
22#[inline]
23pub fn int(n: i64) -> Value {
24 ((n as u64) << 1) as usize | 1
25}
26
27#[inline]
28pub fn int_get(v: Value) -> i64 {
29 (v as i64) >> 1
30}
31
32#[repr(C)]
33pub struct Header {
34 /// >0: local count (1 = unique); <0: shared, accessed atomically.
35 pub rc: i32,
36 pub tag: u16,
37 pub scan_fsize: u8,
38 pub flags: u8,
39}
40
41/// Header followed by `scan_fsize` scanned fields and a tag-defined raw area.
42#[repr(C)]
43pub struct Block {
44 pub header: Header,
45 fields: [Value; 0],
46}
47
48impl Block {
49 /// Address of the first field (pure address arithmetic, no access).
50 #[inline]
51 pub fn fields_ptr(this: *mut Block) -> *mut Value {
52 this.cast::<u8>()
53 .wrapping_add(offset_of!(Block, fields))
54 .cast()
55 }
56}
57
58pub const SCAN_LARGE: u8 = 0xFF;
59pub const FLAG_PINNED: u8 = 0x01;
60/// ADR 0040 Revision 2 (issue #193): a reuse token whose kept fields are dead.
61pub const FLAG_TOKEN: u8 = 0x02;
62
63pub const TAG_RUNTIME_MIN: u16 = 0xFF00;
64pub const TAG_STRING: u16 = 0xFF01;
65pub const TAG_BYTES: u16 = 0xFF02;
66pub const TAG_FLOAT: u16 = 0xFF03;
67pub const TAG_CLOSURE: u16 = 0xFF04;
68pub const TAG_REF: u16 = 0xFF05;
69pub const TAG_REGION: u16 = 0xFF06;
70pub const TAG_FRAME: u16 = 0xFF07;
71pub const TAG_RESOURCE: u16 = 0xFF08;
72/// A handler's capability: its clauses, then its marker. Never sent.
73pub const TAG_CAP: u16 = 0xFF09;
74/// An `Array` (`scan_fsize` [`SCAN_LARGE`]): the count, the elements, then
75/// the spare capacity in the raw area (`crate::array`).
76pub const TAG_ARRAY: u16 = 0xFF0A;
77/// An `Int` outside 63 bits: a signed limb count, then the limbs
78/// (`crate::int`).
79pub const TAG_BIGINT: u16 = 0xFF0B;
80/// A `bytes.Builder`: a length, a capacity, then the bytes
81/// (`crate::builder`).
82pub const TAG_BUILDER: u16 = 0xFF0C;
83/// A `U64` (`std/u64`, ADR 0035): one raw `u64` (`crate::u64`).
84pub const TAG_U64: u16 = 0xFF0D;
85/// A `Dec` (`std/dec`, ADR 0070): an `i128` as two raw `u64` limbs (a
86/// raw area is 8-byte aligned, an `i128` wants 16), 10^18-scaled
87/// (`crate::dec`).
88pub const TAG_DEC: u16 = 0xFF0E;
89
90pub const YIELD_NONE: i32 = 0;
91pub const YIELD_FINAL: i32 = 1;
92pub const YIELD_PANIC: i32 = 2;
93pub const YIELD_KILL: i32 = 3;
94/// A suspended `ctl` handler body is being cancelled (never resumed).
95pub const YIELD_CANCEL: i32 = 4;
96/// A suspended `multi ctl` continuation is being copied: its frames take
97/// one more reference for each one they hold (ADR 0017).
98pub const YIELD_CLONE: i32 = 5;
99/// A suspended `multi ctl` continuation is released without running its
100/// `defer` cleanups (its copies ran them).
101pub const YIELD_DISCARD: i32 = 6;
102
103/// Per-actor execution context, first argument of every generated function.
104#[repr(C)]
105pub struct Ctx {
106 pub yielding: i32,
107 pub reductions: i32,
108 pub yield_marker: u64,
109 pub yield_value: Value,
110 pub evv: *mut c_void,
111 /// Points at the owning `sched::Actor`.
112 pub rt: *mut c_void,
113 /// Arguments after the sixth of a call (ABI Revision 1): `XARGS`
114 /// slots, the running scheduler thread's (ABI 3, ADR 0028). The
115 /// scheduler points it at its thread's slots each time the actor
116 /// resumes, and a callee copies its arguments on entry, before it can
117 /// suspend, so slots shared by a thread's actors are never read across
118 /// a switch. Kept in the context, 26 slots took 208 of the 336 bytes
119 /// of every actor.
120 pub xargs: *mut Value,
121}
122
123/// Room for call arguments beyond the six passed in registers: functions
124/// take up to 128 parameters, and a call through a closure passes the
125/// closure too (ABI 12, issue #434). Per scheduler thread, not per actor.
126pub const XARGS: usize = 123;
127
128/// A Louise function passed to C for the duration of an extern call
129/// (`tl_callback`, ADR 0015): the environment of a C trampoline.
130#[repr(C)]
131pub struct Callback {
132 pub ctx: *mut Ctx,
133 /// The closure, borrowed from the extern's argument.
134 pub fun: Value,
135 /// Identifies the thread of the extern call (0 once it returned).
136 pub thread: usize,
137}
138
139/// Stop unless the program was compiled for this runtime's ABI: a stale
140/// runtime would misread the program's values. Called first by generated
141/// `main`, as `tl_root_cap` checks the number of operations of each root
142/// effect.
143#[unsafe(no_mangle)]
144pub extern "C" fn tl_abi_check(version: i64) {
145 if version != ABI_VERSION {
146 eprintln!(
147 "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"
148 );
149 std::process::abort();
150 }
151}
152
153const _: () = {
154 assert!(size_of::<Value>() == 8);
155 assert!(size_of::<Header>() == 8);
156 assert!(offset_of!(Block, fields) == 8);
157 assert!(offset_of!(Ctx, yielding) == 0);
158 assert!(offset_of!(Ctx, reductions) == 4);
159 assert!(offset_of!(Ctx, yield_marker) == 8);
160 assert!(offset_of!(Ctx, yield_value) == 16);
161 assert!(offset_of!(Ctx, xargs) == 40);
162 assert!(size_of::<Ctx>() == 48);
163 assert!(size_of::<Callback>() == 24);
164};
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169
170 #[test]
171 fn small_int_round_trip() {
172 for n in [0, 1, -1, 42, (1 << 62) - 1, -(1 << 62)] {
173 let v = int(n);
174 assert!(!is_ptr(v));
175 assert_eq!(int_get(v), n);
176 }
177 assert_eq!(int(0), UNIT);
178 }
179}