1use crate::types;
2
3#[derive(Debug, PartialEq, Clone)]
4pub enum NumType {
5 I64,
6}
7
8#[derive(Debug, PartialEq, Clone)]
9pub enum ValType {
10 NumType(NumType),
11}
12
13impl From<types::Type> for ValType {
15 fn from(typ: types::Type) -> Self {
16 match typ {
17 types::Type::Int => Self::NumType(NumType::I64),
18 _ => todo!(),
19 }
20 }
21}
22
23impl<T: Into<ValType>> From<Box<T>> for ValType {
25 fn from(value: Box<T>) -> Self {
26 let unboxed: T = *value;
27 unboxed.into()
28 }
29}
30
31#[derive(Debug, PartialEq)]
32pub enum Const {
33 I64(i64),
34}
35
36#[derive(Debug, PartialEq)]
37pub struct FuncType {
38 pub params: Vec<ValType>,
39 pub results: Vec<ValType>,
40}
41
42impl From<types::FnDef> for FuncType {
44 fn from(func: types::FnDef) -> Self {
45 let typ = func.typ;
46 let params = typ.params.into_iter().map(|p| p.into()).collect();
47 let results = typ.ret.map(|typ| typ.into()).into_iter().collect();
48 Self { params, results }
49 }
50}
51
52#[derive(Debug, PartialEq)]
53pub struct Func {
54 pub typeidx: u32,
55}
56
57#[derive(Debug, PartialEq)]
58pub enum Instr {
59 Const(Const),
60 GetLocal(u32),
61 SetLocal(u32),
62 Call(u32),
63 Drop,
64 AddI64,
65 SubI64,
66 End,
67}
68
69#[derive(Debug, PartialEq)]
70pub struct Code {
71 pub locals: Vec<ValType>,
72 pub body: Vec<Instr>,
73}
74
75#[derive(Debug)]
76pub enum ExportDesc {
77 Func(u32),
78}
79
80#[derive(Debug)]
81pub struct Export {
82 pub name: String,
83 pub desc: ExportDesc,
84}
85
86#[derive(Debug)]
87pub enum ImportedValue {
88 Func(u32),
89}
90
91#[derive(Debug)]
92pub struct Import {
93 pub module: String,
94 pub name: String,
95 pub import: ImportedValue,
96}
97
98#[derive(Debug, Default)]
99pub struct TypeSection(pub Vec<FuncType>);
100
101#[derive(Debug, Default)]
102pub struct ImportSection(pub Vec<Import>);
103
104#[derive(Debug, Default)]
105pub struct FuncSection(pub Vec<Func>);
106
107#[derive(Debug, Default)]
108pub struct ExportSection(pub Vec<Export>);
109
110#[derive(Debug, Default)]
111pub struct CodeSection(pub Vec<Code>);
112
113#[derive(Debug, Default)]
114pub struct StartSection(pub u32);
115
116#[derive(Debug, Default)]
117pub struct Module {
118 pub types: TypeSection,
119 pub imports: ImportSection,
120 pub funcs: FuncSection,
121 pub exports: ExportSection,
122 pub start: StartSection,
123 pub code: CodeSection,
124}