waffle/copying/
module.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
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
use crate::op_traits::rewrite_mem;
use crate::util::{add_start, new_sig};
use paste::paste;
use std::mem::{replace, take};
use std::{
    backtrace,
    borrow::Cow,
    collections::{BTreeMap, BTreeSet, HashMap},
    hash::Hash,
    iter::empty,
    ops::{Deref, DerefMut},
};
pub fn x2i(x: ExportKind) -> ImportKind {
    match x {
        ExportKind::Table(a) => ImportKind::Table(a),
        ExportKind::Func(a) => ImportKind::Func(a),
        ExportKind::Global(a) => ImportKind::Global(a),
        ExportKind::Memory(a) => ImportKind::Memory(a),
    }
}
pub fn i2x(x: ImportKind) -> ExportKind {
    match x {
        ImportKind::Table(a) => ExportKind::Table(a),
        ImportKind::Func(a) => ExportKind::Func(a),
        ImportKind::Global(a) => ExportKind::Global(a),
        ImportKind::Memory(a) => ExportKind::Memory(a),
    }
}
use crate::{
    entity::EntityRef, ExportKind, Func, FuncDecl, FunctionBody, Global, ImportKind, Memory,
    Module, Operator, Signature, SignatureData, Table, TableData, Type, ValueDef,
};
#[derive(Eq, PartialEq, Clone)]
pub struct IKW(pub ImportKind);
impl Hash for IKW {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        match &self.0 {
            ImportKind::Table(a) => {
                state.write_usize(0);
                a.hash(state);
            }
            ImportKind::Func(a) => {
                state.write_usize(1);
                a.hash(state);
            }
            ImportKind::Global(a) => {
                state.write_usize(2);
                a.hash(state);
            }
            ImportKind::Memory(a) => {
                state.write_usize(3);
                a.hash(state);
            }
        }
    }
}

pub struct State<I> {
    cache: HashMap<IKW, ImportKind>,
    fun_cache: BTreeMap<Func, Func>,
    table_cache: BTreeMap<Table, Table>,
    pub importmap: I,
    pub tables: BTreeSet<Table>,
}
impl<I> State<I> {
    pub fn new(importmap: I, tables: BTreeSet<Table>) -> Self {
        return Self {
            importmap,
            cache: Default::default(),
            fun_cache: Default::default(),
            table_cache: Default::default(),
            tables,
        };
    }
}
pub struct Copier<A, B, S> {
    pub src: A,
    pub dest: B,
    pub state: S,
}
macro_rules! translator {
    ($a:tt) => {
        paste! {
            pub fn [<translate_ $a>](&mut self, m: $a) -> anyhow::Result<$a>{
                let ImportKind::$a(n) = self.translate_import(ImportKind::$a(m))? else{
                    anyhow::bail!("not the right item")
                };
                return Ok(n);
            }
        }
    };
}
pub fn tree_shake(m: &mut Module) -> anyhow::Result<()> {
    let n = replace(m, Module::empty());
    let mut s = Copier::new(
        &n,
        m,
        Box::new(State::new(
            Box::new(import_fn(|_, m, n| {
                Ok(Some(ImportBehavior::Passthrough(m, n)))
            })),
            BTreeSet::new(),
        )),
    );
    for x in n.exports.iter() {
        let i = x2i(x.kind.clone());
        let i = s.translate_import(i)?;
        let mut x = x.clone();
        x.kind = i2x(i);
        s.dest.exports.push(x);
    }
    Ok(())
}
pub trait Imports {
    fn get_import(
        &mut self,
        a: &mut Module<'_>,
        m: String,
        n: String,
    ) -> anyhow::Result<Option<ImportBehavior>>;
}
#[repr(transparent)]
#[derive(Clone, Debug, Copy)]
pub struct ImportFn<F>(pub F);

pub fn import_fn(
    a: impl FnMut(&mut Module<'_>, String, String) -> anyhow::Result<Option<ImportBehavior>>,
) -> impl Imports {
    ImportFn(a)
}

impl<F: FnMut(&mut Module<'_>, String, String) -> anyhow::Result<Option<ImportBehavior>>> Imports
    for ImportFn<F>
{
    fn get_import(
        &mut self,
        a: &mut Module<'_>,
        m: String,
        n: String,
    ) -> anyhow::Result<Option<ImportBehavior>> {
        return (self.0)(a, m, n);
    }
}

pub enum ImportBehavior {
    Bind(ImportKind),
    Passthrough(String, String),
}
impl<
        'a: 'b,
        'b,
        A: Deref<Target = crate::Module<'a>>,
        B: Deref<Target = crate::Module<'b>> + DerefMut,
        S: Deref<Target = State<I>> + DerefMut,
        I: Deref<Target = J> + DerefMut,
        J: Imports + ?Sized,
    > Copier<A, B, S>
{
    // pub fn collect(&mut self) -> anyhow::Result<BTreeMap<String, ExportKind>> {
    //     let e = get_exports(&self.src);
    //     // let mut m = HashMap::new();
    //     let mut m = BTreeMap::new();
    //     for (s, e) in e {
    //         let e = x2i(e);
    //         let r = self.translate_import(e.clone())?;
    //         m.insert(s, i2x(r));
    //     }
    //     return Ok(m);
    // }
    pub fn new(
        src: A,
        dest: B,
        // importmap: BTreeMap<(String, String), ImportKind>,
        cache: S,
    ) -> Self {
        return Self {
            src,
            dest,
            // importmap,
            state: cache,
        };
    }
    pub fn resolve_import(&mut self, a: &ImportKind) -> anyhow::Result<Option<ImportBehavior>> {
        for i in self.src.imports.iter() {
            if i.kind == *a {
                return self.state.importmap.get_import(
                    &mut self.dest,
                    i.module.clone(),
                    i.name.clone(),
                );
            }
        }
        return Ok(None);
    }
    pub fn internal_translate_mem(&mut self, a: crate::Memory) -> anyhow::Result<crate::Memory> {
        let d = self.src.memories[a].clone();
        return Ok(self.dest.memories.push(d));
    }
    pub fn internal_translate_global(&mut self, a: crate::Global) -> anyhow::Result<crate::Global> {
        let d = self.src.globals[a].clone();
        return Ok(self.dest.globals.push(d));
    }
    pub fn translate_import(&mut self, a: ImportKind) -> anyhow::Result<ImportKind> {
        let i = match self.resolve_import(&a)? {
            None => None,
            Some(ImportBehavior::Bind(b)) => return Ok(b),
            Some(ImportBehavior::Passthrough(a, b)) => Some((a, b)),
        };
        if let Some(j) = i.clone() {
            for m in self.dest.imports.iter() {
                if m.module == j.0 && m.name == j.1 {
                    return Ok(m.kind.clone());
                }
            }
        }
        if let Some(b) = self.state.cache.get(&IKW(a.clone())) {
            return Ok(b.clone());
        }
        let c = match a {
            ImportKind::Table(t) => ImportKind::Table(self.internal_translate_table(t)?),
            ImportKind::Func(f) => ImportKind::Func(self.internal_translate_func(f)?),
            ImportKind::Global(g) => ImportKind::Global(self.internal_translate_global(g)?),
            ImportKind::Memory(m) => ImportKind::Memory(self.internal_translate_mem(m)?),
        };
        self.state.cache.insert(IKW(a.clone()), c.clone());
        if let Some(j) = i {
            self.dest.imports.push(crate::Import {
                module: j.0,
                name: j.1,
                kind: c.clone(),
            })
        }
        return Ok(c);
    }
    pub fn internal_translate_table(&mut self, tk: Table) -> anyhow::Result<Table> {
        // if let Some(c) = self.state.table_cache.get(&tk) {
        //     return Ok(*c);
        // }
        let mut t = self.src.tables[tk].clone();
        if let Type::TypedFuncRef { sig_index, .. } = &mut t.ty {
            *sig_index = self.translate_sig(*sig_index)?;
        }
        // let nt = self.dest.tables.push(t.clone());
        // self.state.table_cache.insert(tk, nt);
        if let Some(u) = t.func_elements.as_mut() {
            for w in u.iter_mut() {
                *w = self.translate_Func(*w)?;
            }
        }
        // self.dest.tables[nt] = t;
        return Ok(self.dest.tables.push(t));
    }
    pub fn translate_sig(&mut self, s: Signature) -> anyhow::Result<Signature> {
        let mut d = self.src.signatures[s].clone();
        for x in d.params.iter_mut().chain(d.returns.iter_mut()) {
            if let Type::TypedFuncRef { sig_index, .. } = x {
                *sig_index = self.translate_sig(*sig_index)?;
            }
        }
        return Ok(new_sig(&mut *self.dest, d));
    }
    pub fn internal_translate_func(&mut self, f: Func) -> anyhow::Result<Func> {
        if f == Func::invalid() {
            return Ok(f);
        }
        if let Some(x) = self.state.fun_cache.get(&f) {
            return Ok(*x);
        }
        let a = self.dest.funcs.push(crate::FuncDecl::None);
        self.state.fun_cache.insert(f, a);
        for t in &self.state.tables {
            self.dest.tables[*t]
                .func_elements
                .get_or_insert(vec![])
                .push(a);
        }
        if Some(f) == self.src.start_func {
            add_start(&mut self.dest, a);
        }
        let mut f = self.src.funcs[f].clone();
        if let Some(b) = f.body_mut() {
            for v in b.values.iter() {
                let mut k = b.values[v].clone();
                if let ValueDef::BlockParam(_, _, x) = &mut k {
                    if let Type::TypedFuncRef { sig_index, .. } = x {
                        *sig_index = self.translate_sig(*sig_index)?;
                    }
                }
                if let ValueDef::PickOutput(_, _, x) = &mut k {
                    if let Type::TypedFuncRef { sig_index, .. } = x {
                        *sig_index = self.translate_sig(*sig_index)?;
                    }
                }
                if let ValueDef::Operator(a, vs, c) = &mut k {
                    let mut w = b.arg_pool[*vs].to_vec();
                    let mut e = None;
                    rewrite_mem(a, &mut w, |m, _| {
                        *m = self.translate_Memory(*m)?;
                        Ok::<_, anyhow::Error>(())
                    })?;
                    if let Some(e) = e {
                        return Err(e);
                    }
                    match a {
                        crate::Operator::Call { function_index } => {
                            *function_index = self.translate_Func(*function_index)?;
                        }
                        crate::Operator::RefFunc { func_index } => {
                            *func_index = self.translate_Func(*func_index)?;
                        }
                        crate::Operator::RefNull { ty } => {
                            if let Type::TypedFuncRef { sig_index, .. } = ty {
                                *sig_index = self.translate_sig(*sig_index)?;
                            }
                        }
                        crate::Operator::TypedSelect { ty } => {
                            if let Type::TypedFuncRef { sig_index, .. } = ty {
                                *sig_index = self.translate_sig(*sig_index)?;
                            }
                        }
                        Operator::CallRef { sig_index } => {
                            *sig_index = self.translate_sig(*sig_index)?;
                        }
                        crate::Operator::CallIndirect {
                            sig_index,
                            table_index,
                        } => {
                            *sig_index = self.translate_sig(*sig_index)?;
                            *table_index = self.translate_Table(*table_index)?;
                        }
                        crate::Operator::GlobalGet { global_index } => {
                            *global_index = self.translate_Global(*global_index)?;
                        }
                        crate::Operator::GlobalSet { global_index } => {
                            *global_index = self.translate_Global(*global_index)?;
                        }
                        crate::Operator::TableGet { table_index } => {
                            *table_index = self.translate_Table(*table_index)?;
                        }
                        crate::Operator::TableSet { table_index } => {
                            *table_index = self.translate_Table(*table_index)?;
                        }
                        crate::Operator::TableGrow { table_index } => {
                            *table_index = self.translate_Table(*table_index)?;
                        }
                        crate::Operator::TableSize { table_index } => {
                            *table_index = self.translate_Table(*table_index)?;
                        }
                        _ => {}
                    }
                    *vs = b.arg_pool.from_iter(w.into_iter());
                }
                b.values[v] = k;
            }
            for k in b.blocks.iter() {
                let mut kv = b.blocks[k].clone();
                match &mut kv.terminator {
                    crate::Terminator::ReturnCall { func, args } => {
                        *func = self.translate_Func(*func)?;
                    }
                    crate::Terminator::ReturnCallIndirect { sig, table, args } => {
                        *sig = self.translate_sig(*sig)?;
                        *table = self.translate_Table(*table)?;
                    }
                    crate::Terminator::ReturnCallRef { sig, args } => {
                        *sig = self.translate_sig(*sig)?;
                        // *table = self.translate_Table(*table)?;
                    }
                    _ => {}
                }
                b.blocks[k] = kv;
            }
            for x in b.type_pool.storage.iter_mut() {
                if let Type::TypedFuncRef { sig_index, .. } = x {
                    *sig_index = self.translate_sig(*sig_index)?;
                }
            }
            // (self.state.instrument)(&mut self.dest,b)?;
        }
        match &mut f {
            crate::FuncDecl::Import(a, _) => {
                *a = self.translate_sig(*a)?;
            }
            crate::FuncDecl::Lazy(_, _, _) => todo!(),
            crate::FuncDecl::Body(a, _, _) => {
                *a = self.translate_sig(*a)?;
            }
            crate::FuncDecl::Compiled(_, _, _) => todo!(),
            crate::FuncDecl::None => {}
        }
        self.dest.funcs[a] = f;
        return Ok(a);
    }
    translator!(Memory);
    translator!(Table);
    translator!(Global);
    translator!(Func);
}

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