mimium-lang 4.0.0-alpha

mimium(minimal-musical-medium) an infrastructural programming language for sound and music.
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
421
422
423
424
425
426
427
428
// Mid-level intermediate representation that is more like imperative form than hir.
use crate::{
    compiler::IoChannelInfo,
    interner::{Symbol, TypeNodeId},
    types::{PType, RecordTypeField, Type, TypeSize},
};
use std::{cell::OnceCell, path::PathBuf, sync::Arc};
// Import StateTreeSkeleton for function state information
use state_tree::tree::{SizedType, StateTreeSkeleton};

pub mod print;

// #[derive(Debug, Clone, PartialEq)]
// pub struct Global(VPtr);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Argument(pub Symbol, pub TypeNodeId);

pub type VReg = u64;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub enum Value {
    Global(VPtr),
    // Argument(usize, Arc<Argument>), //index,
    Argument(usize),
    /// holds SSA index(position in infinite registers)
    Register(VReg),
    State(VPtr),
    // idx of the function in the program
    Function(usize),
    /// native function (Rust function item or closure)
    ExtFunction(Symbol, TypeNodeId),
    /// Constructor with payload: (name, tag_index, sum_type)
    Constructor(Symbol, usize, TypeNodeId),
    /// internal state
    None,
}

pub type VPtr = Arc<Value>;
type Bbindex = u64;
#[derive(Debug, Clone, PartialEq)]
pub enum Instruction {
    Uinteger(u64),
    Integer(i64),
    //constant float
    Float(f64),
    String(Symbol),
    // allocate memory from stack depending on the size
    Alloc(TypeNodeId),
    // load value to register from the pointer type
    Load(VPtr, TypeNodeId),
    // Store value to stack(destination,source, type)
    Store(VPtr, VPtr, TypeNodeId),
    // Instruction for computing destination address like LLVM's GetElementPtr.
    // This instruction does no actual computation on runtime.
    GetElement {
        value: VPtr,
        ty: TypeNodeId, // type of the composite value like tuple or struct
        tuple_offset: u64,
    },
    // call function, arguments, type of return value
    Call(VPtr, Vec<(VPtr, TypeNodeId)>, TypeNodeId),
    CallCls(VPtr, Vec<(VPtr, TypeNodeId)>, TypeNodeId),
    GetGlobal(VPtr, TypeNodeId),
    SetGlobal(VPtr, VPtr, TypeNodeId),
    // make closure with upindexes
    Closure(VPtr),
    //closes upvalues of specific closure. Always inserted right before Return instruction.
    CloseUpValues(VPtr, TypeNodeId),

    // New heap-based closure instructions (Phase 2)
    /// Create a closure and allocate it on the heap
    /// MakeClosure(fn_proto, closure_size_in_words)
    /// Returns a HeapIdx that references the allocated closure
    MakeClosure {
        fn_proto: VPtr, // Function prototype index
        size: u64,      // Total size in words (for heap allocation)
    },
    /// Close upvalues of a heap-based closure (like CloseUpValues but for heap closures)
    /// CloseHeapClosure(heap_addr)
    /// This closes upvalues when the closure escapes its defining scope
    CloseHeapClosure(VPtr),
    /// Increment the reference count of a heap-allocated object.
    /// Inserted whenever a heap object value is duplicated (call-by-value cloning).
    /// At runtime this calls `heap_retain` and registers the object for release
    /// when the current scope exits.
    CloneHeap(VPtr),
    /// Call a closure indirectly through heap storage
    /// CallIndirect(heap_addr, args, return_type)
    CallIndirect(VPtr, Vec<(VPtr, TypeNodeId)>, TypeNodeId),

    //label to funcproto  and localvar offset?
    GetUpValue(u64, TypeNodeId),
    SetUpValue(u64, VPtr, TypeNodeId),
    //internal state: feed and delay
    PushStateOffset(u64),
    PopStateOffset(u64),
    //load internal state to register(destination)
    GetState(TypeNodeId),

    //condition,  basic block index for then statement, else statement, and merge block
    JmpIf(VPtr, Bbindex, Bbindex, Bbindex),
    // basic block index (for return statement)
    Jmp(i16),
    //merge
    Phi(VPtr, VPtr),
    /// Multi-way branch for match expressions
    /// Switch(scrutinee, cases, default_block, merge_block)
    /// cases: Vec<(literal_value_as_i64, block_index)>
    Switch {
        scrutinee: VPtr,
        /// Each case is (literal_value as i64, block_index)
        cases: Vec<(i64, Bbindex)>,
        /// Default block for non-exhaustive matches; None if exhaustive
        default_block: Option<Bbindex>,
        merge_block: Bbindex,
    },
    /// Phi for switch - merges values from multiple branches
    PhiSwitch(Vec<VPtr>),

    /// Tagged Union operations for sum types
    /// Wrap a value into a tagged union: (tag, value, union_type)
    TaggedUnionWrap {
        tag: u64,
        value: VPtr,
        union_type: TypeNodeId,
    },
    /// Extract tag from a tagged union
    TaggedUnionGetTag(VPtr),
    /// Extract value from a tagged union (assuming correct tag)
    TaggedUnionGetValue(VPtr, TypeNodeId),

    /// Box a value by heap-allocating it.
    /// Stores the given value into a new heap object and returns a HeapIdx.
    /// Used for recursive variant types where self-references are wrapped in Boxed.
    BoxAlloc {
        value: VPtr,
        inner_type: TypeNodeId,
    },
    /// Unbox a heap-allocated value by loading its content from the heap.
    /// Takes a HeapIdx and returns the loaded value of the inner type.
    BoxLoad {
        ptr: VPtr,
        inner_type: TypeNodeId,
    },
    /// Increment the reference count of a boxed heap object.
    /// Inserted when a Boxed value is duplicated (call-by-value cloning).
    BoxClone {
        ptr: VPtr,
    },
    /// Decrement the reference count of a boxed heap object.
    /// When the count reaches 0 the heap object is freed.
    /// Inserted when a Boxed value goes out of scope.
    BoxRelease {
        ptr: VPtr,
        inner_type: TypeNodeId,
    },
    /// Write a value into an already-allocated heap object (destructive update).
    /// Used for future mutable patterns on boxed values.
    BoxStore {
        ptr: VPtr,
        value: VPtr,
        inner_type: TypeNodeId,
    },
    /// Clone all boxed references within a UserSum (variant) value.
    /// This is used when passing UserSum values to functions (call-by-value).
    /// At runtime, walks the value's structure using type information and
    /// increments reference counts for any Boxed fields.
    CloneUserSum {
        value: VPtr,
        ty: TypeNodeId,
    },
    /// Release all boxed references within a UserSum (variant) value.
    /// This is used when a UserSum value goes out of scope.
    /// At runtime, walks the value's structure using type information and
    /// decrements reference counts for any Boxed fields.
    ReleaseUserSum {
        value: VPtr,
        ty: TypeNodeId,
    },

    Return(VPtr, TypeNodeId),
    //value to update state
    ReturnFeed(VPtr, TypeNodeId),

    Delay(u64, VPtr, VPtr),
    Mem(VPtr),

    // Primitive Operations
    AddF(VPtr, VPtr),
    SubF(VPtr, VPtr),
    MulF(VPtr, VPtr),
    DivF(VPtr, VPtr),
    ModF(VPtr, VPtr),
    NegF(VPtr),
    AbsF(VPtr),
    SinF(VPtr),
    CosF(VPtr),
    PowF(VPtr, VPtr),
    LogF(VPtr),
    SqrtF(VPtr),

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

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

    CastFtoI(VPtr),
    CastItoF(VPtr),
    CastItoB(VPtr),

    /// Array Literal. Array in mimium is basically variable-length and immutable.
    /// So VM may allocate array in speacial heap area, or treat it as just as like static data in the program.
    /// MIR does not distinguish how the vm treats the array.
    Array(Vec<VPtr>, TypeNodeId), // array literal, values and type of the array
    GetArrayElem(VPtr, VPtr, TypeNodeId), // get array element, at specific index , type of the element
    SetArrayElem(VPtr, VPtr, VPtr, TypeNodeId), // set array element at index
    Error,
}

#[derive(Debug, Default, Clone, PartialEq)]
pub struct Block(pub Vec<(VPtr, Instruction)>);

impl Block {
    pub fn get_total_stateskeleton(
        &self,
        functions: &[Function],
    ) -> Vec<StateTreeSkeleton<StateType>> {
        let mut children = vec![];
        for (_v, instr) in &self.0 {
            match instr {
                Instruction::Delay(len, _, _) => {
                    children.push(Box::new(StateTreeSkeleton::Delay { len: *len }))
                }
                Instruction::Mem(_) => {
                    children.push(Box::new(StateTreeSkeleton::Mem(StateType(1))))
                }
                Instruction::ReturnFeed(_, ty) => {
                    children.push(Box::new(StateTreeSkeleton::Feed(StateType::from(*ty))))
                }
                Instruction::Call(v, _, _) => {
                    if let Value::Function(idx) = **v {
                        let func = &functions[idx];
                        if func.is_stateful() {
                            children.push(Box::new(func.state_skeleton.clone()))
                        }
                    }
                }
                _ => {}
            }
        }
        children.into_iter().map(|e| *e).collect()
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum UpIndex {
    Local(usize),   // index of local variables in upper functions
    Upvalue(usize), // index of upvalues in upper functions
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub struct OpenUpValue {
    pub pos: usize,
    pub size: TypeSize,
    pub is_closure: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct StateType(pub u64);
impl state_tree::tree::SizedType for StateType {
    fn word_size(&self) -> u64 {
        self.0
    }
}
impl From<TypeNodeId> for StateType {
    fn from(t: TypeNodeId) -> Self {
        match t.to_type() {
            Type::Primitive(PType::Unit) => StateType(0),
            Type::Primitive(PType::Numeric) | Type::Function { .. } => StateType(1),
            Type::Record(fields) => StateType(
                fields
                    .iter()
                    .map(|RecordTypeField { ty, .. }| ty.word_size() as u64)
                    .sum(),
            ),
            Type::Tuple(elems) => StateType(elems.iter().map(|ty| ty.word_size() as u64).sum()),
            Type::Array(_elem_ty) => StateType(1),
            _ => todo!(),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Function {
    pub index: usize,
    pub label: Symbol,
    pub args: Vec<Argument>,
    // pub argtypes: Vec<TypeNodeId>,
    pub return_type: OnceCell<TypeNodeId>, // TODO: None is the state when the type is not inferred yet.
    pub upindexes: Vec<Arc<Value>>,
    pub upperfn_i: Option<usize>,
    pub body: Vec<Block>,
    /// StateTree skeleton information for this function's state layout
    pub state_skeleton: StateTreeSkeleton<StateType>,
}

impl Function {
    pub fn new(
        index: usize,
        name: Symbol,
        args: &[Argument],
        // argtypes: &[TypeNodeId],
        state_skeleton: Vec<StateTreeSkeleton<StateType>>,
        upperfn_i: Option<usize>,
    ) -> Self {
        let state_boxed = state_skeleton.into_iter().map(Box::new).collect();
        Self {
            index,
            label: name,
            args: args.to_vec(),
            // argtypes: argtypes.to_vec(),
            return_type: OnceCell::new(),
            upindexes: vec![],
            upperfn_i,
            body: vec![Block::default()],
            state_skeleton: StateTreeSkeleton::FnCall(state_boxed),
        }
    }
    pub fn add_new_basicblock(&mut self) -> usize {
        self.body.push(Block(vec![]));
        self.body.len() - 1
    }
    pub fn get_argtypes(&self) -> Vec<TypeNodeId> {
        self.args.iter().map(|a| a.1).collect()
    }
    pub fn get_or_insert_upvalue(&mut self, v: &Arc<Value>) -> usize {
        self.upindexes
            .iter()
            .position(|vt| v == vt)
            .unwrap_or_else(|| {
                self.upindexes.push(v.clone());
                self.upindexes.len() - 1
            })
    }
    pub fn push_state_skeleton(&mut self, skeleton: StateTreeSkeleton<StateType>) {
        if let StateTreeSkeleton::FnCall(children) = &mut self.state_skeleton {
            children.push(Box::new(skeleton))
        } else {
            panic!("State skeleton for function must be FnCall type");
        }
    }
    pub fn is_stateful(&self) -> bool {
        if let StateTreeSkeleton::FnCall(children) = &self.state_skeleton {
            !children.is_empty()
        } else {
            panic!("State skeleton for function must be FnCall type");
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct Mir {
    pub functions: Vec<Function>,
    pub file_path: Option<PathBuf>,
}

impl Mir {
    pub fn new(file_path: Option<PathBuf>) -> Self {
        Self {
            file_path,
            ..Default::default()
        }
    }
    pub fn get_dsp_iochannels(&self) -> Option<IoChannelInfo> {
        self.functions
            .iter()
            .find(|f| f.label.as_str() == "dsp")
            .and_then(|f| {
                // log::info!("input_type:{:?} output_type:{:?}", f.get_argtypes().as_slice(), f.return_type.get().as_ref());
                let input = match f.get_argtypes().as_slice() {
                    [] => Some(0),
                    [t] => t.to_type().get_iochannel_count(),
                    _ => None,
                };
                let output = f
                    .return_type
                    .get()
                    .and_then(|t| t.to_type().get_iochannel_count());
                input.and_then(|input| output.map(|output| IoChannelInfo { input, output }))
            })
    }

    /// Get the StateTreeSkeleton for a specific function by name
    pub fn get_function_state_skeleton(
        &self,
        function_name: &str,
    ) -> Option<&StateTreeSkeleton<StateType>> {
        self.functions.iter().find_map(|f| {
            if f.label.as_str() == function_name {
                Some(&f.state_skeleton)
            } else {
                None
            }
        })
    }

    /// Get the StateTreeSkeleton for the dsp function (commonly used for audio processing)
    pub fn get_dsp_state_skeleton(&self) -> Option<&StateTreeSkeleton<StateType>> {
        self.get_function_state_skeleton("dsp")
    }
}