File size: 4,218 Bytes
119e586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/// SoulVM bytecode β€” the instruction set consumed by the Cranelift JIT.
/// Stack-based at the source level; the JIT compiler lifts it to SSA.

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Instruction {
    // ── Constants ────────────────────────────────────────────────────────────
    I64Const(i64),
    F64Const(f64),

    // ── Integer arithmetic (pop 2, push 1) ───────────────────────────────────
    I64Add,
    I64Sub,
    I64Mul,
    I64DivS, // signed divide
    I64And,
    I64Or,
    I64Xor,
    I64Shl,
    I64ShrS,

    // ── Integer comparisons (pop 2, push i64: 1=true 0=false) ────────────────
    I64Eq,
    I64Ne,
    I64LtS,
    I64GtS,
    I64LeS,
    I64GeS,

    // ── Float arithmetic ─────────────────────────────────────────────────────
    F64Add,
    F64Sub,
    F64Mul,
    F64Div,

    // ── Locals ───────────────────────────────────────────────────────────────
    LocalGet(u32),
    LocalSet(u32),

    // ── Control flow ─────────────────────────────────────────────────────────
    /// Unconditional jump to instruction index
    Br(usize),
    /// Pop i64; jump if non-zero
    BrIf(usize),
    /// Pop i64; jump if zero
    BrIfZ(usize),
    /// Call a function by index in the SoulFunc table
    Call(u32),
    /// Pop top-of-stack and return it
    Return,
    /// Push nothing, used as a jump target
    Nop,

    // ── GC heap ──────────────────────────────────────────────────────────────
    /// Allocate an object: (size_bytes: u32, type_tag: u32) β†’ GcPtr (i64)
    Alloc { size: u32, type_tag: u32 },
    /// Load i64 from GcPtr + offset
    Load { offset: u32 },
    /// Store i64 at GcPtr + offset; pops value then ptr
    Store { offset: u32 },

    // ── Capability-gated operations ───────────────────────────────────────────
    /// Assert the caller holds cap_slot with at least `rights` bits set.
    /// Traps if the check fails; no stack effect on success.
    CapCheck { cap_slot: u32, rights: u8 },
    /// Invoke the endpoint at cap_slot, passing top-of-stack as the argument.
    CapInvoke { cap_slot: u32 },
}

/// Type of a value on the SoulVM operand stack.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ValType {
    I64,
    F64,
}

/// A compiled function ready for JIT translation.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SoulFunc {
    pub name:    String,
    pub params:  Vec<ValType>,
    pub returns: Vec<ValType>,
    pub locals:  Vec<ValType>, // extra locals beyond params
    pub body:    Vec<Instruction>,
}

impl SoulFunc {
    pub fn new(name: impl Into<String>, params: Vec<ValType>, returns: Vec<ValType>) -> Self {
        Self { name: name.into(), params, returns, locals: vec![], body: vec![] }
    }

    pub fn push(&mut self, instr: Instruction) -> &mut Self {
        self.body.push(instr);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn build_add_func() {
        let mut f = SoulFunc::new("add", vec![ValType::I64, ValType::I64], vec![ValType::I64]);
        f.push(Instruction::LocalGet(0))
         .push(Instruction::LocalGet(1))
         .push(Instruction::I64Add)
         .push(Instruction::Return);
        assert_eq!(f.body.len(), 4);
    }
}