koute / polkavm

A fast and secure RISC-V based virtual machine
Apache License 2.0
199 stars 44 forks source link

support (u32, u32) as return value #116

Open xlc opened 2 months ago

xlc commented 2 months ago

I got the guest program to compile with this

struct ReturnValue(u32, u32);
impl IntoHost for ReturnValue {
    type Regs = (u32, u32);
    type Destructor = ();

    #[inline(always)]
    fn into_host(value: Self) -> (Self::Regs, ()) {
        ((value.0, value.1), ())
    }
}

but I see no reason to not support (u32, u32) directly without the extra work

But from the host side, I am not very sure how to use call_typed with it

poc/executor/src/lib.rs:69:64
     |
69   |         let (res_ptr, res_len) = instance.call_typed::<(u32,), (u32, u32)>(&mut self.context, "main", (input_ptr,))?;
     |                                           ----------           ^^^^^^^^^^ the trait `polkavm::api::AbiTy` is not implemented for `(u32, u32)`, which is required by `(u32, u32): polkavm::api::FuncResult`
     |                                           |
     |                                           required by a bound introduced by this call
     |
     = help: the following other types implement trait `polkavm::api::AbiTy`:
               i32
               i64
               u32
               u64
     = note: required for `(u32, u32)` to implement `polkavm::api::FuncResult`
koute commented 2 months ago

You can call into the guest without call_typed.

https://docs.rs/polkavm/latest/src/polkavm/api.rs.html#2226-2252

Then you can extract the return values with get_reg(Reg::A0) and get_reg(Reg::A1).