ffi_gen/abi/
export.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
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
429
use super::VarGen;
use crate::{Abi, AbiFunction, AbiType, FunctionType, NumType, Return, Var};

#[derive(Clone, Debug)]
pub struct Export {
    pub symbol: String,
    pub ffi_args: Vec<Var>,
    pub instr: Vec<Instr>,
    pub ffi_ret: Return,
}

impl Abi {
    fn export_arg(
        self,
        out: Var,
        gen: &mut VarGen,
        instr: &mut Vec<Instr>,
        ffi_args: &mut Vec<Var>,
    ) {
        match &out.ty {
            AbiType::RefEnum(_) => todo!(),
            AbiType::Num(num)
                if matches!((self, num), (Abi::Wasm32, NumType::U64 | NumType::I64)) =>
            {
                let num_type = *num;
                let low = gen.gen_num(NumType::U32);
                let high = gen.gen_num(NumType::U32);
                ffi_args.extend_from_slice(&[low.clone(), high.clone()]);
                instr.push(Instr::LiftNumAsU32Tuple(low, high, out, num_type));
            }
            AbiType::Num(num) => {
                let arg = gen.gen_num(*num);
                ffi_args.push(arg.clone());
                instr.push(Instr::LiftNum(arg, out));
            }
            AbiType::Isize => {
                let arg = gen.gen_num(NumType::IPtr);
                ffi_args.push(arg.clone());
                instr.push(Instr::LiftIsize(arg, out));
            }
            AbiType::Usize => {
                let arg = gen.gen_num(NumType::UPtr);
                ffi_args.push(arg.clone());
                instr.push(Instr::LiftUsize(arg, out));
            }
            AbiType::Bool => {
                let arg = gen.gen_num(NumType::U8);
                ffi_args.push(arg.clone());
                instr.push(Instr::LiftBool(arg, out));
            }
            AbiType::RefStr => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                ffi_args.extend_from_slice(&[ptr.clone(), len.clone()]);
                instr.push(Instr::LiftStr(ptr, len, out));
            }
            AbiType::String => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                let cap = gen.gen_num(NumType::UPtr);
                ffi_args.extend_from_slice(&[ptr.clone(), len.clone(), cap.clone()]);
                instr.push(Instr::LiftString(ptr, len, cap, out));
            }
            AbiType::RefSlice(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                ffi_args.extend_from_slice(&[ptr.clone(), len.clone()]);
                let ty = *ty;
                instr.push(Instr::LiftSlice(ptr, len, out, ty));
            }
            AbiType::Vec(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                let cap = gen.gen_num(NumType::UPtr);
                ffi_args.extend_from_slice(&[ptr.clone(), len.clone(), cap.clone()]);
                let ty = *ty;
                instr.push(Instr::LiftVec(ptr, len, cap, out, ty));
            }
            AbiType::RefObject(object) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let object = object.clone();
                instr.push(Instr::LiftRefObject(ptr, out, object));
            }
            AbiType::Object(object) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let object = object.clone();
                instr.push(Instr::LiftObject(ptr, out, object));
            }
            AbiType::Option(ty) => {
                let opt = gen.gen_num(NumType::U8);
                ffi_args.push(opt.clone());
                let some = gen.gen((**ty).clone());
                let mut some_instr = vec![];
                self.export_arg(some.clone(), gen, &mut some_instr, ffi_args);
                instr.push(Instr::LiftOption(opt, out, some, some_instr));
            }
            AbiType::Result(_) => todo!(),
            AbiType::RefIter(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LiftRefIter(ptr, out, ty));
            }
            AbiType::Iter(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LiftIter(ptr, out, ty));
            }
            AbiType::RefFuture(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LiftRefFuture(ptr, out, ty));
            }
            AbiType::Future(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LiftFuture(ptr, out, ty));
            }
            AbiType::RefStream(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LiftRefStream(ptr, out, ty));
            }
            AbiType::Stream(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LiftStream(ptr, out, ty));
            }
            AbiType::Tuple(tys) => {
                let mut vars = vec![];
                for ty in tys {
                    let arg = gen.gen(ty.clone());
                    vars.push(arg.clone());
                    self.export_arg(arg, gen, instr, ffi_args);
                }
                instr.push(Instr::LiftTuple(vars, out));
            }
            AbiType::Buffer(_) => unimplemented!("\"buffer\" can only be used as return value"),
            AbiType::List(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                instr.push(Instr::LiftRefObject(
                    ptr,
                    out.clone(),
                    format!("Vec<{}>", ty),
                ));
            }
        }
    }

    fn export_return(
        self,
        ret: Var,
        gen: &mut VarGen,
        instr: &mut Vec<Instr>,
        ffi_rets: &mut Vec<Var>,
    ) {
        match &ret.ty {
            AbiType::Num(num)
                if matches!((self, num), (Abi::Wasm32, NumType::U64 | NumType::I64)) =>
            {
                let low = gen.gen_num(NumType::UPtr);
                let high = gen.gen_num(NumType::UPtr);
                ffi_rets.extend_from_slice(&[low.clone(), high.clone()]);
                let num_type = *num;
                instr.push(Instr::LowerNumAsU32Tuple(ret, low, high, num_type));
            }
            AbiType::Num(num) => {
                let out = gen.gen_num(*num);
                ffi_rets.push(out.clone());
                instr.push(Instr::LowerNum(ret, out));
            }
            AbiType::Isize => {
                let out = gen.gen_num(NumType::IPtr);
                ffi_rets.push(out.clone());
                instr.push(Instr::LowerIsize(ret, out));
            }
            AbiType::Usize => {
                let out = gen.gen_num(NumType::UPtr);
                ffi_rets.push(out.clone());
                instr.push(Instr::LowerUsize(ret, out));
            }
            AbiType::Bool => {
                let out = gen.gen_num(NumType::U8);
                ffi_rets.push(out.clone());
                instr.push(Instr::LowerBool(ret, out));
            }
            AbiType::RefStr => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                ffi_rets.extend_from_slice(&[ptr.clone(), len.clone()]);
                instr.push(Instr::LowerStr(ret, ptr, len));
            }
            AbiType::String => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                let cap = gen.gen_num(NumType::UPtr);
                ffi_rets.extend_from_slice(&[ptr.clone(), len.clone(), cap.clone()]);
                instr.push(Instr::LowerString(ret, ptr, len, cap));
            }
            AbiType::RefSlice(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                let ty = *ty;
                ffi_rets.extend_from_slice(&[ptr.clone(), len.clone()]);
                instr.push(Instr::LowerSlice(ret, ptr, len, ty));
            }
            AbiType::Vec(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                let len = gen.gen_num(NumType::UPtr);
                let cap = gen.gen_num(NumType::UPtr);
                let ty = *ty;
                ffi_rets.extend_from_slice(&[ptr.clone(), len.clone(), cap.clone()]);
                instr.push(Instr::LowerVec(ret, ptr, len, cap, ty));
            }
            AbiType::RefObject(_object) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                instr.push(Instr::LowerRefObject(ret, ptr));
            }
            AbiType::Object(_object) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                instr.push(Instr::LowerObject(ret, ptr));
            }
            AbiType::Option(ty) => {
                let var = gen.gen_num(NumType::U8);
                let some = gen.gen((**ty).clone());
                ffi_rets.push(var.clone());
                let mut some_instr = vec![];
                self.export_return(some.clone(), gen, &mut some_instr, ffi_rets);
                instr.push(Instr::LowerOption(ret, var, some, some_instr));
            }
            AbiType::Result(ty) => {
                let var = gen.gen_num(NumType::U8);
                let ok = gen.gen((**ty).clone());
                let err = gen.gen(AbiType::String);
                ffi_rets.push(var.clone());
                let mut err_instr = vec![];
                self.export_return(err.clone(), gen, &mut err_instr, ffi_rets);
                let mut ok_instr = vec![];
                self.export_return(ok.clone(), gen, &mut ok_instr, ffi_rets);
                instr.push(Instr::LowerResult(ret, var, ok, ok_instr, err, err_instr));
            }
            AbiType::RefIter(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LowerRefIter(ret, ptr, ty));
            }
            AbiType::Iter(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LowerIter(ret, ptr, ty));
            }
            AbiType::RefFuture(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LowerRefFuture(ret, ptr, ty));
            }
            AbiType::Future(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LowerFuture(ret, ptr, ty));
            }
            AbiType::RefStream(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LowerRefStream(ret, ptr, ty));
            }
            AbiType::Stream(ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                let ty = (**ty).clone();
                instr.push(Instr::LowerStream(ret, ptr, ty));
            }
            AbiType::Tuple(tys) => {
                let mut vars = vec![];
                let mut instr_inner = vec![];
                for ty in tys {
                    let ret = gen.gen(ty.clone());
                    vars.push(ret.clone());
                    self.export_return(ret, gen, &mut instr_inner, ffi_rets);
                }
                instr.push(Instr::LowerTuple(ret, vars));
                instr.extend(instr_inner);
            }
            AbiType::Buffer(_ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                instr.push(Instr::LowerObject(ret, ptr));
            }
            AbiType::List(ty) => {
                instr.push(Instr::AssertType(ret.clone(), format!("Vec<{}>", ty)));
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                instr.push(Instr::LowerObject(ret, ptr));
            }
            AbiType::RefEnum(_ty) => {
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_rets.push(ptr.clone());
                instr.push(Instr::LowerObject(ret.clone(), ptr))
            }
        }
    }

    pub(crate) fn export(self, func: &AbiFunction) -> Export {
        let mut gen = VarGen::new();
        let mut ffi_args = vec![];
        let mut ffi_ffi_rets = vec![];
        let mut abi_args = vec![];
        let mut instr = vec![];
        let self_ = match &func.ty {
            FunctionType::Method(object) => {
                let out = gen.gen(AbiType::RefObject(object.clone()));
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                instr.push(Instr::LiftRefObject(ptr, out.clone(), object.clone()));
                Some(out)
            }
            FunctionType::NextIter(_, ty) => {
                let out = gen.gen(AbiType::RefIter(Box::new(ty.clone())));
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                instr.push(Instr::LiftRefIter(ptr, out.clone(), ty.clone()));
                Some(out)
            }
            FunctionType::PollFuture(_, ty) => {
                let out = gen.gen(AbiType::RefFuture(Box::new(ty.clone())));
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                instr.push(Instr::LiftRefFuture(ptr, out.clone(), ty.clone()));
                Some(out)
            }
            FunctionType::PollStream(_, ty) => {
                let out = gen.gen(AbiType::RefStream(Box::new(ty.clone())));
                let ptr = gen.gen_num(NumType::IPtr);
                ffi_args.push(ptr.clone());
                instr.push(Instr::LiftRefStream(ptr, out.clone(), ty.clone()));
                Some(out)
            }
            _ => None,
        };
        for (_, ty) in func.args.iter() {
            let out = gen.gen(ty.clone());
            abi_args.push(out.clone());
            self.export_arg(out, &mut gen, &mut instr, &mut ffi_args);
        }
        let abi_ret = func.ret.as_ref().map(|ret| gen.gen(ret.clone()));
        instr.push(Instr::CallAbi(
            func.ty.clone(),
            self_,
            func.name.clone(),
            abi_ret.clone(),
            abi_args,
        ));
        if let Some(abi_ret) = abi_ret {
            let mut instr_ret = vec![];
            self.export_return(abi_ret, &mut gen, &mut instr_ret, &mut ffi_ffi_rets);
            if !ffi_ffi_rets.is_empty() {
                instr.push(Instr::DefineRets(ffi_ffi_rets.clone()));
            }
            instr.extend(instr_ret);
        }
        Export {
            symbol: func.symbol(),
            instr,
            ffi_args,
            ffi_ret: func.ret(ffi_ffi_rets),
        }
    }
}

#[derive(Clone, Debug)]
pub enum Instr {
    LiftNum(Var, Var),
    LiftNumAsU32Tuple(Var, Var, Var, NumType),
    LowerNum(Var, Var),
    LowerNumAsU32Tuple(Var, Var, Var, NumType),
    LiftIsize(Var, Var),
    LowerIsize(Var, Var),
    LiftUsize(Var, Var),
    LowerUsize(Var, Var),
    LiftBool(Var, Var),
    LowerBool(Var, Var),
    LiftStr(Var, Var, Var),
    LowerStr(Var, Var, Var),
    LiftString(Var, Var, Var, Var),
    LowerString(Var, Var, Var, Var),
    LiftSlice(Var, Var, Var, NumType),
    LowerSlice(Var, Var, Var, NumType),
    LiftVec(Var, Var, Var, Var, NumType),
    LowerVec(Var, Var, Var, Var, NumType),
    LiftRefObject(Var, Var, String),
    LowerRefObject(Var, Var),
    LiftObject(Var, Var, String),
    LowerObject(Var, Var),
    LiftOption(Var, Var, Var, Vec<Instr>),
    LowerOption(Var, Var, Var, Vec<Instr>),
    LowerResult(Var, Var, Var, Vec<Instr>, Var, Vec<Instr>),
    LiftIter(Var, Var, AbiType),
    LowerIter(Var, Var, AbiType),
    LiftRefIter(Var, Var, AbiType),
    LowerRefIter(Var, Var, AbiType),
    LiftFuture(Var, Var, AbiType),
    LowerFuture(Var, Var, AbiType),
    LiftRefFuture(Var, Var, AbiType),
    LowerRefFuture(Var, Var, AbiType),
    LiftStream(Var, Var, AbiType),
    LowerStream(Var, Var, AbiType),
    LiftRefStream(Var, Var, AbiType),
    LowerRefStream(Var, Var, AbiType),
    LiftTuple(Vec<Var>, Var),
    LowerTuple(Var, Vec<Var>),
    CallAbi(FunctionType, Option<Var>, String, Option<Var>, Vec<Var>),
    DefineRets(Vec<Var>),
    AssertType(Var, String),
}