monkey-asm 2.0.0

AOT AArch64 assembly backend for monkeylang (Linux ELF and macOS Mach-O)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Native `extern "C"` runtime the generated `.s` links against (design §8).
//!
//! Every entry point is a thin shell: decode the raw FFI arguments, run the
//! shared semantics from [`crate::runtime_core`] on a [`PointerStore`], and
//! turn any [`RuntimeFailure`] into `rt_fatal` (observer error record when
//! initialized, stderr message, `exit(1)`). Panics never cross the FFI
//! boundary: shells run under `catch_unwind` and report `InternalError`.
//!
//! Heap objects are owned by one process-wide `PointerStore`. A mutex keeps
//! its safe reference API exclusive across FFI entries and threads; generated
//! function calls happen only after the guard has been released.

use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
use std::sync::{Mutex, OnceLock};

use crate::runtime_backend::{CodeHandle, PointerStore};
use crate::runtime_core::{
    self, CallDispatch, OutputSink, ReturnPolicy, RuntimeErrorKind, RuntimeFailure, RuntimeResult,
    Value, NULL_VALUE,
};

/// Observer channel fd registered by `rt_observer_init`; -1 = not installed.
static OBSERVER_FD: AtomicI64 = AtomicI64::new(-1);

/// Globals area registered by `rt_globals_init` (future GC root scanning).
static GLOBALS_BASE: AtomicU64 = AtomicU64::new(0);
static GLOBALS_COUNT: AtomicU64 = AtomicU64::new(0);

fn native_store() -> &'static Mutex<PointerStore> {
    static STORE: OnceLock<Mutex<PointerStore>> = OnceLock::new();
    STORE.get_or_init(|| Mutex::new(PointerStore::new()))
}

struct StdoutSink;

impl OutputSink for StdoutSink {
    fn write_line(&mut self, line: &str) {
        use std::io::Write;
        let stdout = std::io::stdout();
        let mut handle = stdout.lock();
        // stdout must stay the exact byte stream of puts/print (design §10.2);
        // flush per line so exit(1)/exit paths cannot drop buffered bytes.
        let _ = handle.write_all(line.as_bytes());
        let _ = handle.write_all(b"\n");
        let _ = handle.flush();
    }
}

/// Writes the single framed observer record: u64 big-endian payload length,
/// then the UTF-8 JSON payload (design §10.2).
#[cfg(unix)]
fn observer_write(payload: &str) {
    use std::io::Write;
    use std::os::unix::io::{FromRawFd, IntoRawFd};

    let fd = OBSERVER_FD.load(Ordering::SeqCst);
    if fd < 0 {
        return;
    }
    let mut file = unsafe { std::fs::File::from_raw_fd(fd as i32) };
    let length = (payload.len() as u64).to_be_bytes();
    let _ = file.write_all(&length);
    let _ = file.write_all(payload.as_bytes());
    let _ = file.flush();
    // The harness owns the fd; keep it open.
    let _ = file.into_raw_fd();
}

#[cfg(not(unix))]
fn observer_write(_payload: &str) {}

/// Terminal error path shared by all shells (design §8): optional observer
/// error record, human-readable stderr line, `exit(1)`.
fn fatal(kind: RuntimeErrorKind, message: &str) -> ! {
    observer_write(&format!("{{\"status\":\"error\",\"kind\":\"{}\"}}", kind.name()));
    eprintln!("monkey: {}: {}", kind.name(), message);
    std::process::exit(1);
}

/// Runs one FFI shell body: panics become `InternalError`, `RuntimeFailure`
/// becomes `rt_fatal` semantics. Only `Ok` values return to generated code.
fn ffi_shell<T>(body: impl FnOnce(&mut PointerStore) -> RuntimeResult<T>) -> T {
    let outcome = catch_unwind(AssertUnwindSafe(|| {
        let mut store = native_store()
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        body(&mut store)
    }));
    match outcome {
        Ok(Ok(value)) => value,
        Ok(Err(RuntimeFailure {
            kind,
            message,
        })) => fatal(kind, &message),
        Err(_) => fatal(RuntimeErrorKind::InternalError, "runtime panicked"),
    }
}

/// `(ptr, len)` decoding per §8: zero length may be null and is an empty
/// slice; non-zero length must be a valid, aligned region.
unsafe fn value_slice<'a>(ptr: *const Value, len: u64) -> &'a [Value] {
    if len == 0 {
        &[]
    } else {
        std::slice::from_raw_parts(ptr, len as usize)
    }
}

unsafe fn byte_slice<'a>(ptr: *const u8, len: u64) -> &'a [u8] {
    if len == 0 {
        &[]
    } else {
        std::slice::from_raw_parts(ptr, len as usize)
    }
}

fn name_from_bytes(bytes: &[u8]) -> RuntimeResult<&str> {
    std::str::from_utf8(bytes).map_err(|_| RuntimeFailure {
        kind: RuntimeErrorKind::InternalError,
        message: "identifier bytes are not valid UTF-8".to_string(),
    })
}

/// Calls generated code for a `CallDispatch::Invoke` (design §8.1): pick the
/// `extern "C"` signature by arity, closure in `x0`, arguments in `x1..x7`.
unsafe fn invoke_code(code: CodeHandle, closure: Value, args: &[Value]) -> Value {
    use std::mem::transmute;
    let address = code as usize;
    match args {
        [] => transmute::<usize, extern "C" fn(Value) -> Value>(address)(closure),
        [a1] => transmute::<usize, extern "C" fn(Value, Value) -> Value>(address)(closure, *a1),
        [a1, a2] => transmute::<usize, extern "C" fn(Value, Value, Value) -> Value>(address)(
            closure, *a1, *a2,
        ),
        [a1, a2, a3] => transmute::<usize, extern "C" fn(Value, Value, Value, Value) -> Value>(
            address,
        )(closure, *a1, *a2, *a3),
        [a1, a2, a3, a4] => transmute::<
            usize,
            extern "C" fn(Value, Value, Value, Value, Value) -> Value,
        >(address)(closure, *a1, *a2, *a3, *a4),
        [a1, a2, a3, a4, a5] => transmute::<
            usize,
            extern "C" fn(Value, Value, Value, Value, Value, Value) -> Value,
        >(address)(closure, *a1, *a2, *a3, *a4, *a5),
        [a1, a2, a3, a4, a5, a6] => transmute::<
            usize,
            extern "C" fn(Value, Value, Value, Value, Value, Value, Value) -> Value,
        >(address)(closure, *a1, *a2, *a3, *a4, *a5, *a6),
        [a1, a2, a3, a4, a5, a6, a7] => {
            transmute::<
                usize,
                extern "C" fn(Value, Value, Value, Value, Value, Value, Value, Value) -> Value,
            >(address)(closure, *a1, *a2, *a3, *a4, *a5, *a6, *a7)
        }
        _ => fatal(
            RuntimeErrorKind::ResourceLimit,
            "call requires more arguments than the calling convention allows",
        ),
    }
}

/// Finishes a dispatch outside any store borrow: `Invoke` re-enters generated
/// code, which will recursively call back into these shells.
fn complete_dispatch(dispatch: CallDispatch) -> Value {
    match dispatch {
        CallDispatch::Return(value) => value,
        CallDispatch::Invoke {
            code,
            closure,
            args,
            return_policy,
        } => {
            let returned = unsafe { invoke_code(code, closure, &args) };
            match return_policy {
                ReturnPolicy::Direct => returned,
                ReturnPolicy::ConstructorInstance(instance) => instance,
            }
        }
    }
}

#[no_mangle]
/// # Safety
/// `base` must be writable for `count` consecutive, properly aligned values
/// and remain live for the generated program's execution.
pub unsafe extern "C" fn rt_globals_init(base: *mut Value, count: u64) {
    ffi_shell(|_store| {
        for index in 0..count as usize {
            unsafe {
                *base.add(index) = NULL_VALUE;
            }
        }
        GLOBALS_BASE.store(base as u64, Ordering::SeqCst);
        GLOBALS_COUNT.store(count, Ordering::SeqCst);
        Ok(())
    })
}

#[no_mangle]
/// # Safety
/// For nonzero `len`, `ptr` must reference `len` readable bytes.
pub unsafe extern "C" fn rt_string_from_bytes(ptr: *const u8, len: u64) -> Value {
    ffi_shell(|store| {
        let bytes = unsafe { byte_slice(ptr, len) };
        runtime_core::string_from_utf8(store, bytes)
    })
}

#[no_mangle]
pub extern "C" fn rt_box_int(raw: i64) -> Value {
    ffi_shell(|store| Ok(runtime_core::make_int(store, raw)))
}

#[no_mangle]
/// # Safety
/// For nonzero `len`, `argv` must reference `len` readable values.
pub unsafe extern "C" fn rt_array(argv: *const Value, len: u64) -> Value {
    ffi_shell(|store| {
        let values = unsafe { value_slice(argv, len) };
        Ok(runtime_core::array_from_values(store, values))
    })
}

#[no_mangle]
/// # Safety
/// `argv` must reference `pairs * 2` readable values, and that multiplication
/// must fit in `u64`.
pub unsafe extern "C" fn rt_hash(argv: *const Value, pairs: u64) -> Value {
    ffi_shell(|store| {
        let values = unsafe { value_slice(argv, pairs * 2) };
        runtime_core::hash_from_pairs(store, values)
    })
}

#[no_mangle]
/// # Safety
/// `code` must be a generated function entry with the runtime calling
/// convention. For nonzero `num_free`, `free` must reference that many values.
pub unsafe extern "C" fn rt_closure(
    code: *const u8,
    num_parameters: u64,
    free: *const Value,
    num_free: u64,
) -> Value {
    ffi_shell(|store| {
        let free_values = unsafe { value_slice(free, num_free) };
        runtime_core::closure_new(store, code as CodeHandle, num_parameters, free_values)
    })
}

#[no_mangle]
pub extern "C" fn rt_get_free(closure: Value, index: u64) -> Value {
    ffi_shell(|store| runtime_core::get_free(store, closure, index))
}

#[no_mangle]
/// # Safety
/// For nonzero `len`, `name` must reference `len` readable bytes.
pub unsafe extern "C" fn rt_class(name: *const u8, len: u64) -> Value {
    ffi_shell(|store| {
        let bytes = unsafe { byte_slice(name, len) };
        let class_name = name_from_bytes(bytes)?;
        Ok(runtime_core::class_new(store, class_name))
    })
}

#[no_mangle]
/// # Safety
/// For nonzero `len`, `name` must reference `len` readable bytes.
pub unsafe extern "C" fn rt_class_add_method(
    class: Value,
    name: *const u8,
    len: u64,
    method: Value,
    is_ctor: u64,
) {
    ffi_shell(|store| {
        let bytes = unsafe { byte_slice(name, len) };
        let method_name = name_from_bytes(bytes)?;
        runtime_core::class_add_method(store, class, method_name, method, is_ctor != 0)
    })
}

#[no_mangle]
/// # Safety
/// For nonzero `len`, `name` must reference `len` readable bytes.
pub unsafe extern "C" fn rt_get_property(obj: Value, name: *const u8, len: u64) -> Value {
    ffi_shell(|store| {
        let bytes = unsafe { byte_slice(name, len) };
        let property = name_from_bytes(bytes)?;
        runtime_core::get_property(store, obj, property)
    })
}

#[no_mangle]
/// # Safety
/// For nonzero `len`, `name` must reference `len` readable bytes.
pub unsafe extern "C" fn rt_set_property(obj: Value, name: *const u8, len: u64, v: Value) {
    ffi_shell(|store| {
        let bytes = unsafe { byte_slice(name, len) };
        let property = name_from_bytes(bytes)?;
        runtime_core::set_property(store, obj, property, v)
    })
}

#[no_mangle]
pub extern "C" fn rt_index(obj: Value, idx: Value) -> Value {
    ffi_shell(|store| runtime_core::index(store, obj, idx))
}

#[no_mangle]
pub extern "C" fn rt_add(l: Value, r: Value) -> Value {
    ffi_shell(|store| runtime_core::add(store, l, r))
}

#[no_mangle]
pub extern "C" fn rt_sub(l: Value, r: Value) -> Value {
    ffi_shell(|store| runtime_core::sub(store, l, r))
}

#[no_mangle]
pub extern "C" fn rt_mul(l: Value, r: Value) -> Value {
    ffi_shell(|store| runtime_core::mul(store, l, r))
}

#[no_mangle]
pub extern "C" fn rt_div(l: Value, r: Value) -> Value {
    ffi_shell(|store| runtime_core::div(store, l, r))
}

#[no_mangle]
pub extern "C" fn rt_eq(l: Value, r: Value) -> Value {
    ffi_shell(|store| runtime_core::eq_values(store, l, r).map(runtime_core::bool_value))
}

#[no_mangle]
pub extern "C" fn rt_neq(l: Value, r: Value) -> Value {
    ffi_shell(|store| {
        runtime_core::eq_values(store, l, r).map(|equal| runtime_core::bool_value(!equal))
    })
}

#[no_mangle]
pub extern "C" fn rt_gt(l: Value, r: Value) -> Value {
    ffi_shell(|store| runtime_core::gt(store, l, r))
}

#[no_mangle]
pub extern "C" fn rt_minus(v: Value) -> Value {
    ffi_shell(|store| runtime_core::minus(store, v))
}

#[no_mangle]
pub extern "C" fn rt_bang(v: Value) -> Value {
    ffi_shell(|_store| Ok(runtime_core::bang(v)))
}

#[no_mangle]
pub extern "C" fn rt_truthy(v: Value) -> u64 {
    ffi_shell(|_store| Ok(if runtime_core::truthy(v) { 1 } else { 0 }))
}

#[no_mangle]
/// # Safety
/// For nonzero `argc`, `argv` must reference `argc` readable values. Any
/// closure reached through `callee` must contain a valid generated code entry.
pub unsafe extern "C" fn rt_call(callee: Value, argc: u64, argv: *const Value) -> Value {
    let dispatch = ffi_shell(|store| {
        let args = unsafe { value_slice(argv, argc) };
        runtime_core::dispatch_call(store, &mut StdoutSink, callee, args)
    });
    complete_dispatch(dispatch)
}

#[no_mangle]
/// # Safety
/// For nonzero `argc`, `argv` must reference `argc` readable values. Any
/// constructor reached through `callee` must contain a valid code entry.
pub unsafe extern "C" fn rt_construct(callee: Value, argc: u64, argv: *const Value) -> Value {
    let dispatch = ffi_shell(|store| {
        let args = unsafe { value_slice(argv, argc) };
        runtime_core::dispatch_construct(store, callee, args)
    });
    complete_dispatch(dispatch)
}

#[no_mangle]
/// # Safety
/// `fd` must be a valid writable Unix file descriptor owned by the harness
/// for as long as observer output can be emitted.
pub unsafe extern "C" fn rt_observer_init(fd: u64) {
    OBSERVER_FD.store(fd as i64, Ordering::SeqCst);
}

#[no_mangle]
pub extern "C" fn rt_observe_result(v: Value) {
    let payload = ffi_shell(|store| {
        let value = runtime_core::canonical_value(store, v)?;
        Ok(format!("{{\"status\":\"ok\",\"value\":{}}}", value))
    });
    observer_write(&payload);
}

#[no_mangle]
/// # Safety
/// For nonzero `len`, `msg` must reference `len` readable bytes.
pub unsafe extern "C" fn rt_fatal(kind: u64, msg: *const u8, len: u64) -> ! {
    let outcome = catch_unwind(|| {
        let kind = RuntimeErrorKind::from_u64(kind).unwrap_or(RuntimeErrorKind::InternalError);
        let bytes = unsafe { byte_slice(msg, len) };
        (kind, String::from_utf8_lossy(bytes).into_owned())
    });
    match outcome {
        Ok((kind, message)) => fatal(kind, &message),
        Err(_) => fatal(RuntimeErrorKind::InternalError, "runtime panicked"),
    }
}