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
use crate::{
    encoder::WastEncoder,
    helpers::{EmitConstant, EmitDefault, ToWasiType},
    operations::branch::EnumerationTable,
    Identifier, JumpBranch, JumpTable, WasiType, WasiValue,
};
use std::fmt::Write;

pub mod branch;

pub(crate) trait Emit {
    fn emit<W: Write>(&self, w: &mut WastEncoder<W>) -> std::fmt::Result;
}

pub(crate) trait EmitValue {
    fn emit_default<W: Write>(&self, w: &mut WastEncoder<W>) -> std::fmt::Result;
    fn emit_constant<W: Write>(&self, w: &mut WastEncoder<W>) -> std::fmt::Result;
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum WasiInstruction {
    /// Create the default value for a given type
    Default(WasiType),
    /// Create a constant value
    Constant(WasiValue),
    /// Convert stack value to WASI type
    Convert {
        /// The target type after convert
        into: WasiType,
    },
    /// Transmute stack value to WASI type
    Transmute {
        /// The target type after transmute
        into: WasiType,
    },
    GetField,
    SetField,
    CallFunction {
        symbol: Identifier,
    },
    NativeSum {
        terms: Vec<WasiInstruction>,
    },
    NativeProduct {
        terms: Vec<WasiInstruction>,
    },
    /// `if cond { } else { }`
    JumpBranch(JumpBranch),
    /// `if c1 { } else if c2 { } else { }`
    JumpTable(JumpTable),
    /// `case 0: ... else: ...`
    JumpEnumeration(EnumerationTable),
    Goto {},
    Return {},
    Drop {
        objects: usize,
    },
}

impl WasiInstruction {
    /// Create a const type
    pub fn constant<T>(value: T) -> Self
    where
        T: Into<WasiValue>,
    {
        Self::Constant(value.into())
    }
}

impl<'a, W: Write> WastEncoder<'a, W> {
    pub fn emit_instructions(&mut self, instruction: &[WasiInstruction]) -> std::fmt::Result {
        for i in instruction {
            i.emit(self)?;
        }
        Ok(())
    }
}

impl Emit for WasiInstruction {
    fn emit<W: Write>(&self, w: &mut WastEncoder<W>) -> std::fmt::Result {
        match self {
            Self::Default(v) => {
                w.newline()?;
                v.emit_default(w)?;
                w.stack.push(v.clone())
            }
            Self::Constant(v) => {
                w.newline()?;
                v.emit_constant(w)?;
                w.stack.push(v.to_wasi_type())
            }
            Self::Convert { into } => {
                let last = w.stack.pop();
                match last {
                    Some(last) => {
                        w.newline()?;
                        last.emit_convert(into, w)?;
                        w.stack.push(into.clone())
                    }
                    None => {
                        panic!("no item on stack!")
                    }
                }
            }
            Self::Transmute { into } => {
                let last = w.stack.pop();
                match last {
                    Some(last) => {
                        last.emit_transmute(into, w)?;
                        w.stack.push(into.clone())
                    }
                    None => {
                        panic!("no item on stack!")
                    }
                }
            }
            Self::GetField => {
                todo!()
            }
            Self::SetField => {
                todo!()
            }
            Self::CallFunction { symbol } => match w.source.get_function(symbol) {
                Some(s) => {
                    write!(w, "call {}", symbol.wasi_id())?;
                    for input in s.inputs.iter() {
                        match w.stack.pop() {
                            Some(s) => {
                                if s.ne(&input.r#type) {
                                    panic!("Mismatch type")
                                }
                            }
                            None => {
                                panic!("Missing parameter")
                            }
                        }
                    }
                    for output in s.output.clone() {
                        w.stack.push(output.r#type)
                    }
                }
                None => {
                    panic!("Missing function")
                }
            },
            // (drop drop ...)
            Self::Drop { objects } => write!(w, "({})", "drop ".repeat(*objects).trim_end())?,
            Self::Goto { .. } => {
                todo!()
            }
            Self::Return { .. } => {
                todo!()
            }
            Self::NativeSum { .. } => {
                todo!()
            }
            Self::NativeProduct { .. } => {
                todo!()
            }
            Self::JumpBranch(v) => v.emit(w)?,
            Self::JumpTable(_) => {
                todo!()
            }
            Self::JumpEnumeration(_) => {
                todo!()
            }
        }
        Ok(())
    }
}

#[derive(Copy, Clone, Debug)]
pub enum VariableKind {
    Global,
    Local,
    Table,
}