mimium_lang/runtime/vm/
bytecode.rs

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
use crate::{types::TypeSize, utils::half_float::HFloat};

pub type Reg = u8; // register position
pub type ConstPos = u16;
pub type GlobalPos = u8;
pub type Offset = i16;
//24bit unsigned integer for shiftsate
pub type StateOffset = intx::U24;
/// Instructions for bytecode. Currently, each instructon has the 64 bit size(Tag, up to 3 bytes arguments.)
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Instruction {
    /// Move Single Value, Destination, Source
    Move(Reg, Reg),
    /// Load Single Value from Constants. Destination, Source
    MoveConst(Reg, ConstPos),
    /// Load Immediate float from half precision. Destination, Value
    MoveImmF(Reg, HFloat),
    // Move the range of registers (e.g. tuple) Destination, Source, Wordsize
    MoveRange(Reg, Reg, TypeSize),
    /// Call to internal function
    /// Function Address,Nargs,Word Size of Return Value
    Call(Reg, u8, TypeSize),
    //call internal closure
    CallCls(Reg, u8, TypeSize),
    /// Call external rust functions or closure,
    /// Currently, The execution branches into the invocation of a raw function item(function pointer), or Rust Closure depending on the information on the VM.
    /// Previously there was another operation `CallExtCls` for Rust Closure Invocation separately but keeping distinction between raw function pointer and closure at the type checking and program generation stage, makes compiler's design complex thus it was removed.
    /// The distincion may also be resolved statically at linking time.
    /// Function Address,Nargs,Nret
    CallExtFun(Reg, u8, TypeSize),
    /// Create new closure. Destination, index of inner function prototype in global function table.
    Closure(Reg, Reg),
    /// register of the closure to be closed. other local closures will be released with this instruction.
    Close(Reg),
    /// destination,source, size
    GetUpValue(Reg, Reg, TypeSize),
    SetUpValue(Reg, Reg, TypeSize),

    /// destination,source
    GetGlobal(Reg, GlobalPos, TypeSize),
    SetGlobal(GlobalPos, Reg, TypeSize),
    /// Call internal state over time, destination,source
    GetState(Reg, TypeSize),
    SetState(Reg, TypeSize),
    PushStatePos(StateOffset),
    PopStatePos(StateOffset),

    /// Return from current function without return value.
    Return0,
    /// value start position, Nrets
    Return(Reg, TypeSize),
    //dst,src,time,idx
    Delay(Reg, Reg, Reg),
    Mem(Reg, Reg),

    /// jump to instruction over the offset.
    Jmp(Offset),
    /// jump to instruction over the offset if the value in the first argument was negative.
    JmpIfNeg(Reg, Offset),

    /// Primitive Operations.
    /// Destination, Src1, Src2
    AddF(Reg, Reg, Reg),
    SubF(Reg, Reg, Reg),
    MulF(Reg, Reg, Reg),
    DivF(Reg, Reg, Reg),
    ModF(Reg, Reg, Reg),
    NegF(Reg, Reg),
    AbsF(Reg, Reg),
    SqrtF(Reg, Reg),
    SinF(Reg, Reg),
    CosF(Reg, Reg),
    PowF(Reg, Reg, Reg),
    LogF(Reg, Reg),

    // Primitive Operations for int
    AddI(Reg, Reg, Reg),
    SubI(Reg, Reg, Reg),
    MulI(Reg, Reg, Reg),
    DivI(Reg, Reg, Reg),
    ModI(Reg, Reg, Reg),
    NegI(Reg, Reg),
    AbsI(Reg, Reg),

    PowI(Reg, Reg, Reg),
    LogI(Reg, Reg, Reg),
    // primitive Operations for bool
    Not(Reg, Reg),
    Eq(Reg, Reg, Reg),
    Ne(Reg, Reg, Reg),
    Gt(Reg, Reg, Reg),
    Ge(Reg, Reg, Reg),
    Lt(Reg, Reg, Reg),
    Le(Reg, Reg, Reg),
    And(Reg, Reg, Reg),
    Or(Reg, Reg, Reg),

    CastFtoI(Reg, Reg),
    CastItoF(Reg, Reg),
    CastItoB(Reg, Reg),
    Dummy,
}

impl std::fmt::Display for Instruction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Instruction::Return0 => write!(f, "ret0"),
            Instruction::Jmp(dst) => write!(f, "{:<10} {}", "jmp", dst),
            Instruction::GetState(dst, size) => write!(f, "{:<10} {} {}", "getstate", dst, size),
            Instruction::SetState(src, size) => write!(f, "{:<10} {} {}", "setstate", src, size),
            Instruction::PushStatePos(v) => write!(f, "{:<10} {}", "pushsttpos", v),
            Instruction::PopStatePos(v) => write!(f, "{:<10} {}", "popsttpos", v),

            Instruction::Move(dst, src) => write!(f, "{:<10} {} {}", "mov", dst, src),
            Instruction::MoveConst(dst, num) => write!(f, "{:<10} {} {}", "movc", dst, num),
            Instruction::MoveImmF(dst, v) => write!(f, "{:<10} {} {}", "movimmF", dst, v),
            Instruction::MoveRange(dst, src, n) => {
                write!(
                    f,
                    "{:<10} {}-{} {}-{}",
                    "mov",
                    dst,
                    dst + n - 1,
                    src,
                    src + n - 1
                )
            }

            Instruction::Closure(dst, src) => {
                write!(f, "{:<10} {} {}", "closure", dst, src)
            }
            Instruction::Close(src) => {
                write!(f, "{:<10} {}", "close", src)
            }
            Instruction::Delay(dst, src, time) => {
                write!(f, "{:<10} {} {} {}", "delay", dst, src, time)
            }
            Instruction::Mem(dst, src) => {
                write!(f, "{:<10} {} {}", "mem", dst, src)
            }
            Instruction::Return(iret, nret) => write!(f, "{:<10} {} {}", "ret", iret, nret),
            Instruction::GetUpValue(dst, srcup, size) => {
                write!(f, "{:<10} {} {} {}", "getupv", dst, srcup, size)
            }
            Instruction::SetUpValue(dstup, src, size) => {
                write!(f, "{:<10} {} {} {}", "setupv", dstup, src, size)
            }
            Instruction::GetGlobal(dst, src, size) => {
                write!(f, "{:<10} {} {} {}", "getglobal", dst, src, size)
            }
            Instruction::SetGlobal(dst, src, size) => {
                write!(f, "{:<10} {} {} {}", "setglobal", dst, src, size)
            }
            Instruction::JmpIfNeg(dst, cond) => write!(f, "{:<10} {} {}", "jmpifneg", dst, cond),
            Instruction::AbsF(dst, src) => write!(f, "{:<10} {} {}", "absf", dst, src),
            Instruction::NegF(dst, src) => write!(f, "{:<10} {} {}", "negf", dst, src),
            Instruction::SinF(dst, src) => write!(f, "{:<10} {} {}", "sin", dst, src),
            Instruction::CosF(dst, src) => write!(f, "{:<10} {} {}", "cos", dst, src),
            Instruction::SqrtF(dst, src) => write!(f, "{:<10} {} {}", "sqrt", dst, src),
            Instruction::LogF(dst, src) => write!(f, "{:<10} {} {} ", "logf", dst, src),

            Instruction::AbsI(dst, src) => write!(f, "{:<10} {} {}", "abs", dst, src),
            Instruction::NegI(dst, src) => write!(f, "{:<10} {} {}", "neg", dst, src),
            Instruction::Not(dst, src) => write!(f, "{:<10} {} {}", "not", dst, src),
            Instruction::CastFtoI(dst, src) => write!(f, "{:<10} {} {}", "f2i", dst, src),
            Instruction::CastItoF(dst, src) => write!(f, "{:<10} {} {}", "i2f", dst, src),
            Instruction::CastItoB(dst, src) => write!(f, "{:<10} {} {}", "i2b", dst, src),
            //3 arguments
            Instruction::Call(func, nargs, nret_req) => {
                write!(f, "{:<10} {} {} {}", "call", func, nargs, nret_req)
            }
            Instruction::CallCls(func, nargs, nret_req) => {
                write!(f, "{:<10} {} {} {}", "callcls", func, nargs, nret_req)
            }
            Instruction::CallExtFun(func, nargs, nret_req) => {
                write!(f, "{:<10} {} {} {}", "callext", func, nargs, nret_req)
            }
            Instruction::LogI(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "logi", dst, lhs, rhs),
            Instruction::PowI(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "powi", dst, lhs, rhs),
            Instruction::AddF(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "addf", dst, lhs, rhs),
            Instruction::SubF(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "subf", dst, lhs, rhs),
            Instruction::MulF(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "mulf", dst, lhs, rhs),
            Instruction::DivF(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "divf", dst, lhs, rhs),
            Instruction::ModF(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "modf", dst, lhs, rhs),
            Instruction::PowF(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "powf", dst, lhs, rhs),
            Instruction::AddI(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "add", dst, lhs, rhs),
            Instruction::SubI(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "sub", dst, lhs, rhs),
            Instruction::MulI(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "mul", dst, lhs, rhs),
            Instruction::DivI(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "div", dst, lhs, rhs),
            Instruction::ModI(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "mod", dst, lhs, rhs),
            Instruction::Eq(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "eq", dst, lhs, rhs),
            Instruction::Ne(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "ne", dst, lhs, rhs),
            Instruction::Gt(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "gt", dst, lhs, rhs),
            Instruction::Ge(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "ge", dst, lhs, rhs),
            Instruction::Lt(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "le", dst, lhs, rhs),
            Instruction::Le(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "lt", dst, lhs, rhs),
            Instruction::And(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "and", dst, lhs, rhs),
            Instruction::Or(dst, lhs, rhs) => write!(f, "{:<10} {} {} {}", "or", dst, lhs, rhs),
            Instruction::Dummy => write!(f, "dummy"),
        }
    }
}

#[cfg(test)]
#[test]
fn ensure_bytecode_size() {
    let size = std::mem::size_of::<Instruction>();
    assert_eq!(4, size);
}