wasmtime_cranelift/translate/
translation_utils.rs

1//! Helper functions and structures for the translation.
2use crate::translate::environ::TargetEnvironment;
3use core::u32;
4use cranelift_codegen::ir;
5use cranelift_frontend::FunctionBuilder;
6use wasmparser::{FuncValidator, WasmModuleResources};
7use wasmtime_environ::WasmResult;
8
9/// Get the parameter and result types for the given Wasm blocktype.
10pub fn blocktype_params_results<'a, T>(
11    validator: &'a FuncValidator<T>,
12    ty: wasmparser::BlockType,
13) -> WasmResult<(
14    impl ExactSizeIterator<Item = wasmparser::ValType> + Clone + 'a,
15    impl ExactSizeIterator<Item = wasmparser::ValType> + Clone + 'a,
16)>
17where
18    T: WasmModuleResources,
19{
20    return Ok(match ty {
21        wasmparser::BlockType::Empty => (
22            itertools::Either::Left(std::iter::empty()),
23            itertools::Either::Left(None.into_iter()),
24        ),
25        wasmparser::BlockType::Type(ty) => (
26            itertools::Either::Left(std::iter::empty()),
27            itertools::Either::Left(Some(ty).into_iter()),
28        ),
29        wasmparser::BlockType::FuncType(ty_index) => {
30            let ty = validator
31                .resources()
32                .sub_type_at(ty_index)
33                .expect("should be valid")
34                .unwrap_func();
35
36            (
37                itertools::Either::Right(ty.params().iter().copied()),
38                itertools::Either::Right(ty.results().iter().copied()),
39            )
40        }
41    });
42}
43
44/// Create a `Block` with the given Wasm parameters.
45pub fn block_with_params<PE: TargetEnvironment + ?Sized>(
46    builder: &mut FunctionBuilder,
47    params: impl IntoIterator<Item = wasmparser::ValType>,
48    environ: &PE,
49) -> WasmResult<ir::Block> {
50    let block = builder.create_block();
51    for ty in params {
52        match ty {
53            wasmparser::ValType::I32 => {
54                builder.append_block_param(block, ir::types::I32);
55            }
56            wasmparser::ValType::I64 => {
57                builder.append_block_param(block, ir::types::I64);
58            }
59            wasmparser::ValType::F32 => {
60                builder.append_block_param(block, ir::types::F32);
61            }
62            wasmparser::ValType::F64 => {
63                builder.append_block_param(block, ir::types::F64);
64            }
65            wasmparser::ValType::Ref(rt) => {
66                let hty = environ.convert_heap_type(rt.heap_type());
67                let (ty, needs_stack_map) = environ.reference_type(hty);
68                let val = builder.append_block_param(block, ty);
69                if needs_stack_map {
70                    builder.declare_value_needs_stack_map(val);
71                }
72            }
73            wasmparser::ValType::V128 => {
74                builder.append_block_param(block, ir::types::I8X16);
75            }
76        }
77    }
78    Ok(block)
79}
80
81/// Turns a `wasmparser` `f32` into a `Cranelift` one.
82pub fn f32_translation(x: wasmparser::Ieee32) -> ir::immediates::Ieee32 {
83    ir::immediates::Ieee32::with_bits(x.bits())
84}
85
86/// Turns a `wasmparser` `f64` into a `Cranelift` one.
87pub fn f64_translation(x: wasmparser::Ieee64) -> ir::immediates::Ieee64 {
88    ir::immediates::Ieee64::with_bits(x.bits())
89}
90
91/// Special VMContext value label. It is tracked as 0xffff_fffe label.
92pub fn get_vmctx_value_label() -> ir::ValueLabel {
93    const VMCTX_LABEL: u32 = 0xffff_fffe;
94    ir::ValueLabel::from_u32(VMCTX_LABEL)
95}