Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Multi Value #72

Open
bushidocodes opened this issue Feb 17, 2022 · 1 comment
Open

Feature: Multi Value #72

bushidocodes opened this issue Feb 17, 2022 · 1 comment

Comments

@bushidocodes
Copy link
Contributor

This feature enables a WebAssembly function to return a tuple of values. When writing WebAssembly by hand, I've found this to be a helpful way to return an (i32, i32) fat pointer to a string or static array of values stored in linear memory.

I am unclear how one can do this with C via WASI-SDK. I've seen code examples that have suggested to me that rightmost arguments can be tagged to be returned as part of a multi-value return. See the C header for WASI itself as an example.

Reference: https://github.com/WebAssembly/multi-value

@bushidocodes
Copy link
Contributor Author

pub fn wasm_func_type_to_llvm_type<'a>(
    ctx: &'a Context,
    f_type: &wasmparser::FuncType,
) -> &'a llvm::Type {
    let return_count = f_type.returns.len();
    let return_type = if return_count > 1 {
        let rets: Vec<&llvm::Type> = f_type
            .returns
            .into_iter()
            .map(|e| wasm_type_to_llvm_type(ctx, *e))
            .collect();
        StructType::new(ctx, &*rets.into_boxed_slice(), false).to_super()
    } else if return_count == 0 {
        <()>::get_type(ctx)
    } else {
        wasm_type_to_llvm_type(ctx, f_type.returns[0])
    };
    let mut params: Vec<&llvm::Type> = Vec::new();
    for t in &*f_type.params {
        params.push(wasm_type_to_llvm_type(ctx, *t))
    }

    llvm::FunctionType::new(return_type, &params).to_super()
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant