beamr 0.16.2

A Rust runtime with the BEAM's execution model, targeting Gleam
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
420
//! Guard, test, and branching BEAM opcode handlers.

use std::cmp::Ordering;

use crate::atom::{Atom, AtomTable};
use crate::error::{ExecError, GuardBifResolution};
use crate::interpreter::InstructionOutcome;
use crate::loader::decode::compact::Operand;
use crate::loader::decode::{BifOp, ComparisonOp, TypeTestOp};
use crate::module::{Module, ResolvedImport, ResolvedImportTarget};
use crate::native::ProcessContext;
use crate::process::{CodePosition, Process};
use crate::term::boxed::{Closure, Cons, Float, Map, Tuple};
use crate::term::reference_ref::ReferenceRef;
use crate::term::{Term, binary_ref::BinaryRef, compare};

use super::core;

pub fn get_hd(
    process: &mut Process,
    module: &Module,
    source: &Operand,
    destination: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let cons = Cons::new(core::read_term(process, module, source)?).ok_or(ExecError::Badarg)?;
    core::write_term(process, destination, cons.head())?;
    Ok(InstructionOutcome::Continue)
}

pub fn get_list(
    process: &mut Process,
    module: &Module,
    source: &Operand,
    head_dest: &Operand,
    tail_dest: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let cons = Cons::new(core::read_term(process, module, source)?).ok_or(ExecError::Badarg)?;
    core::write_term(process, head_dest, cons.head())?;
    core::write_term(process, tail_dest, cons.tail())?;
    Ok(InstructionOutcome::Continue)
}

pub fn get_tl(
    process: &mut Process,
    module: &Module,
    source: &Operand,
    destination: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let cons = Cons::new(core::read_term(process, module, source)?).ok_or(ExecError::Badarg)?;
    core::write_term(process, destination, cons.tail())?;
    Ok(InstructionOutcome::Continue)
}

pub fn type_test(
    process: &Process,
    module: &Module,
    op: TypeTestOp,
    fail: &Operand,
    value: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let (value, arity) = function_test_value_and_arity(process, module, op, value)?;
    branch_if_false(module, fail, matches_type(op, value, arity)?)
}

pub fn comparison(
    process: &Process,
    module: &Module,
    op: ComparisonOp,
    fail: &Operand,
    left: &Operand,
    right: &Operand,
    atom_table: Option<&AtomTable>,
) -> Result<InstructionOutcome, ExecError> {
    let left = core::read_term(process, module, left)?;
    let right = core::read_term(process, module, right)?;
    let order = || {
        atom_table.map_or_else(
            || compare::raw_cmp(left, right),
            |table| compare::cmp(left, right, table),
        )
    };
    let passed = match op {
        ComparisonOp::Lt => order() == Ordering::Less,
        ComparisonOp::Ge => order() != Ordering::Less,
        ComparisonOp::Eq => compare::numeric_eq(left, right),
        ComparisonOp::Ne => !compare::numeric_eq(left, right),
        ComparisonOp::EqExact => compare::exact_eq(left, right),
        ComparisonOp::NeExact => !compare::exact_eq(left, right),
    };
    branch_if_false(module, fail, passed)
}

pub fn test_arity(
    process: &Process,
    module: &Module,
    fail: &Operand,
    tuple: &Operand,
    arity: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let tuple = core::read_term(process, module, tuple)?;
    let expected = core::operand_usize(arity, "tuple arity")?;
    let passed = Tuple::new(tuple).is_some_and(|tuple| tuple.arity() == expected);
    branch_if_false(module, fail, passed)
}

pub fn is_tagged_tuple(
    process: &Process,
    module: &Module,
    fail: &Operand,
    value: &Operand,
    arity: &Operand,
    tag: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let value = core::read_term(process, module, value)?;
    let expected_arity = core::operand_usize(arity, "tagged tuple arity")?;
    let tag = Term::atom(core::operand_atom(tag)?);
    let passed = Tuple::new(value).is_some_and(|tuple| {
        tuple.arity() == expected_arity
            && tuple
                .get(0)
                .is_some_and(|candidate| compare::exact_eq(candidate, tag))
    });
    branch_if_false(module, fail, passed)
}

pub fn select_val(
    process: &Process,
    module: &Module,
    value: &Operand,
    fail: &Operand,
    list: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let value = core::read_term(process, module, value)?;
    for pair in select_pairs(list, "select_val list")? {
        let (candidate, label) = pair?;
        if compare::exact_eq(value, core::read_term(process, module, candidate)?) {
            return jump_label(module, label);
        }
    }
    jump_label(module, fail)
}

pub fn select_tuple_arity(
    process: &Process,
    module: &Module,
    value: &Operand,
    fail: &Operand,
    list: &Operand,
) -> Result<InstructionOutcome, ExecError> {
    let arity = Tuple::new(core::read_term(process, module, value)?).map(Tuple::arity);
    if let Some(arity) = arity {
        for pair in select_pairs(list, "select_tuple_arity list")? {
            let (candidate, label) = pair?;
            if core::operand_usize(candidate, "tuple arity")? == arity {
                return jump_label(module, label);
            }
        }
    }
    jump_label(module, fail)
}

pub fn jump(module: &Module, target: &Operand) -> Result<InstructionOutcome, ExecError> {
    jump_label(module, target)
}

/// Build the diagnosable guard-BIF refusal from the import that failed to
/// resolve to a native entry, carrying its MFA and the resolution state.
fn guard_bif_unavailable(resolved: &ResolvedImport, resolution: GuardBifResolution) -> ExecError {
    ExecError::GuardBifUnavailable {
        module: resolved.module,
        function: resolved.function,
        arity: resolved.arity,
        resolution,
    }
}

pub fn bif(
    process: &mut Process,
    module: &Module,
    op: BifOp,
    operands: &[Operand],
    services: Option<&crate::interpreter::NativeServices>,
) -> Result<InstructionOutcome, ExecError> {
    let parsed = parse_bif_operands(op, operands)?;
    // gc_bifs collect with the compiler-stated live register count; plain
    // bifs get a conservative full-register live set in case a native
    // allocates anyway (all registers stay rooted).
    let live = match parsed.live {
        Some(operand) => core::operand_usize(operand, "gc_bif live")?,
        None => 256,
    };

    let import_index = core::operand_usize(parsed.import, "bif import index")?;
    let resolved = module
        .resolved_imports
        .get(import_index)
        .ok_or(ExecError::InvalidImport {
            index: import_index,
        })?;
    if resolved.arity != parsed.expected_arity {
        return Err(ExecError::InvalidOperand("bif arity mismatch"));
    }

    // A guard/BIF opcode can only call a native entry. Every other resolution
    // shape is a composition or bytecode-shape error: name the MFA and the
    // resolution state rather than a bare operand refusal (composition-honesty,
    // extended from `ServiceUnavailable`). Same mint point, same process-fatal
    // outcome — the fail-label handling below is untouched.
    let entry = match resolved.target {
        ResolvedImportTarget::Native(entry) => entry,
        ResolvedImportTarget::Deferred { .. } => {
            return Err(guard_bif_unavailable(
                resolved,
                GuardBifResolution::Deferred,
            ));
        }
        ResolvedImportTarget::Denied { .. } => {
            return Err(guard_bif_unavailable(resolved, GuardBifResolution::Denied));
        }
        ResolvedImportTarget::Code { .. } => {
            return Err(guard_bif_unavailable(
                resolved,
                GuardBifResolution::CodeTarget,
            ));
        }
        ResolvedImportTarget::Unresolved { .. } => {
            return Err(guard_bif_unavailable(
                resolved,
                GuardBifResolution::Unresolved,
            ));
        }
    };

    let mut args = Vec::with_capacity(parsed.args.len());
    for arg in parsed.args {
        args.push(core::read_term(process, module, arg)?);
    }

    let mut context = ProcessContext::new();
    context.set_pid(Some(process.pid()));
    // Comparison BIFs need the atom table for atom ordering.
    if let Some(services) = services {
        context.set_atom_table(services.atom_table.clone());
    }
    context.attach_process(process, live);
    match (entry.function)(&args, &mut context) {
        Ok(result) => {
            context.detach_process();
            core::write_term(process, parsed.destination, result)?;
            Ok(InstructionOutcome::Continue)
        }
        Err(_) => {
            context.detach_process();
            let label = core::operand_label(parsed.fail)?;
            if label == 0 {
                return Err(ExecError::Badarg);
            }
            jump_label(module, parsed.fail)
        }
    }
}

fn function_test_value_and_arity(
    process: &Process,
    module: &Module,
    op: TypeTestOp,
    value: &Operand,
) -> Result<(Term, Option<usize>), ExecError> {
    if op == TypeTestOp::IsFunction2 {
        let Operand::List(operands) = value else {
            return Err(ExecError::InvalidOperand("is_function2 operands"));
        };
        let [function, arity] = operands.as_slice() else {
            return Err(ExecError::InvalidOperand("is_function2 operands"));
        };
        Ok((
            core::read_term(process, module, function)?,
            Some(core::operand_usize(arity, "is_function2 arity")?),
        ))
    } else {
        Ok((core::read_term(process, module, value)?, None))
    }
}

fn branch_if_false(
    module: &Module,
    fail: &Operand,
    passed: bool,
) -> Result<InstructionOutcome, ExecError> {
    if passed {
        Ok(InstructionOutcome::Continue)
    } else {
        jump_label(module, fail)
    }
}

fn jump_label(module: &Module, label: &Operand) -> Result<InstructionOutcome, ExecError> {
    let label = core::operand_label(label)?;
    Ok(InstructionOutcome::Jump(CodePosition {
        module: module.name,
        instruction_pointer: core::label_ip(module, label)?,
    }))
}

fn matches_type(op: TypeTestOp, value: Term, arity: Option<usize>) -> Result<bool, ExecError> {
    let matched = match op {
        TypeTestOp::IsInteger => value.is_small_int(),
        TypeTestOp::IsFloat => Float::new(value).is_some(),
        TypeTestOp::IsNumber => value.is_small_int() || Float::new(value).is_some(),
        TypeTestOp::IsAtom => value.is_atom(),
        TypeTestOp::IsPid => value.is_pid(),
        TypeTestOp::IsReference => ReferenceRef::new(value).is_some(),
        TypeTestOp::IsPort => false,
        TypeTestOp::IsNil => value.is_nil(),
        TypeTestOp::IsBinary | TypeTestOp::IsBitstr => BinaryRef::new(value).is_some(),
        TypeTestOp::IsList => value.is_list() || value.is_nil(),
        TypeTestOp::IsNonemptyList => value.is_list(),
        TypeTestOp::IsTuple => Tuple::new(value).is_some(),
        TypeTestOp::IsFunction => Closure::new(value).is_some(),
        TypeTestOp::IsBoolean => matches!(value.as_atom(), Some(Atom::TRUE | Atom::FALSE)),
        TypeTestOp::IsFunction2 => {
            let Some(expected_arity) = arity else {
                return Err(ExecError::InvalidOperand("is_function2 arity"));
            };
            Closure::new(value)
                .is_some_and(|closure| usize::from(closure.arity()) == expected_arity)
        }
        TypeTestOp::IsMap => Map::new(value).is_some(),
    };
    Ok(matched)
}

fn select_pairs<'a>(
    list: &'a Operand,
    context: &'static str,
) -> Result<impl Iterator<Item = Result<(&'a Operand, &'a Operand), ExecError>>, ExecError> {
    let Operand::List(items) = list else {
        return Err(ExecError::InvalidOperand(context));
    };
    if items.len() % 2 != 0 {
        return Err(ExecError::InvalidOperand(context));
    }
    Ok(items
        .chunks_exact(2)
        .map(|chunk| Ok((&chunk[0], &chunk[1]))))
}

static BIF0_NO_FAIL: Operand = Operand::Label(0);

struct ParsedBif<'a> {
    fail: &'a Operand,
    import: &'a Operand,
    args: &'a [Operand],
    destination: &'a Operand,
    /// `gc_bif*` carry the live x-register count for collections the BIF
    /// may trigger; plain `bif*` must not collect, so they have none.
    live: Option<&'a Operand>,
    expected_arity: u8,
}

fn parse_bif_operands(op: BifOp, operands: &[Operand]) -> Result<ParsedBif<'_>, ExecError> {
    let arity = match op {
        BifOp::Bif0 => 0,
        BifOp::Bif1 | BifOp::GcBif1 => 1,
        BifOp::Bif2 | BifOp::GcBif2 => 2,
        BifOp::GcBif3 => 3,
    };
    let non_gc_len = 3 + arity;
    let gc_len = 4 + arity;

    match op {
        BifOp::Bif0 => {
            // bif0 has no fail label: [import, destination]
            if operands.len() != 2 {
                return Err(ExecError::InvalidOperand("bif0 operands"));
            }
            Ok(ParsedBif {
                fail: &BIF0_NO_FAIL,
                import: &operands[0],
                args: &[],
                destination: &operands[1],
                live: None,
                expected_arity: 0,
            })
        }
        BifOp::Bif1 | BifOp::Bif2 => {
            if operands.len() != non_gc_len {
                return Err(ExecError::InvalidOperand("bif operands"));
            }
            Ok(ParsedBif {
                fail: &operands[0],
                import: &operands[1],
                args: &operands[2..2 + arity],
                destination: &operands[2 + arity],
                live: None,
                expected_arity: arity as u8,
            })
        }
        BifOp::GcBif1 | BifOp::GcBif2 | BifOp::GcBif3 => {
            if operands.len() != gc_len && operands.len() != non_gc_len {
                return Err(ExecError::InvalidOperand("gc_bif operands"));
            }
            // OTP form: [Fail, Live, Import, Args.., Dst] — operand 1 is
            // the live x-register count, not a heap need.
            let has_live = operands.len() == gc_len;
            let offset = usize::from(has_live);
            Ok(ParsedBif {
                fail: &operands[0],
                import: &operands[1 + offset],
                args: &operands[2 + offset..2 + offset + arity],
                destination: &operands[2 + offset + arity],
                live: has_live.then_some(&operands[1]),
                expected_arity: arity as u8,
            })
        }
    }
}

#[cfg(test)]
mod tests;