use crate::environ::{WasmError, WasmResult};
use core::u32;
use cranelift_codegen::entity::entity_impl;
use cranelift_codegen::ir;
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use wasmparser;
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct FuncIndex(u32);
entity_impl!(FuncIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct DefinedFuncIndex(u32);
entity_impl!(DefinedFuncIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct DefinedTableIndex(u32);
entity_impl!(DefinedTableIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct DefinedMemoryIndex(u32);
entity_impl!(DefinedMemoryIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct DefinedGlobalIndex(u32);
entity_impl!(DefinedGlobalIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct TableIndex(u32);
entity_impl!(TableIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct GlobalIndex(u32);
entity_impl!(GlobalIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct MemoryIndex(u32);
entity_impl!(MemoryIndex);
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct SignatureIndex(u32);
entity_impl!(SignatureIndex);
#[derive(Debug, Clone, Copy)]
pub struct Global {
pub ty: ir::Type,
pub mutability: bool,
pub initializer: GlobalInit,
}
#[derive(Debug, Clone, Copy)]
pub enum GlobalInit {
I32Const(i32),
I64Const(i64),
F32Const(u32),
F64Const(u64),
GetGlobal(GlobalIndex),
Import,
}
#[derive(Debug, Clone, Copy)]
pub struct Table {
pub ty: TableElementType,
pub minimum: u32,
pub maximum: Option<u32>,
}
#[derive(Debug, Clone, Copy)]
pub enum TableElementType {
Val(ir::Type),
Func,
}
#[derive(Debug, Clone, Copy)]
pub struct Memory {
pub minimum: u32,
pub maximum: Option<u32>,
pub shared: bool,
}
pub fn type_to_type(ty: wasmparser::Type) -> WasmResult<ir::Type> {
Ok(match ty {
wasmparser::Type::I32 => ir::types::I32,
wasmparser::Type::I64 => ir::types::I64,
wasmparser::Type::F32 => ir::types::F32,
wasmparser::Type::F64 => ir::types::F64,
_ => return Err(WasmError::Unsupported("unsupported wasm type")),
})
}
pub fn tabletype_to_type(ty: wasmparser::Type) -> WasmResult<Option<ir::Type>> {
Ok(match ty {
wasmparser::Type::I32 => Some(ir::types::I32),
wasmparser::Type::I64 => Some(ir::types::I64),
wasmparser::Type::F32 => Some(ir::types::F32),
wasmparser::Type::F64 => Some(ir::types::F64),
wasmparser::Type::AnyFunc => None,
_ => return Err(WasmError::Unsupported("unsupported table wasm type")),
})
}
pub fn blocktype_to_type(ty: wasmparser::TypeOrFuncType) -> WasmResult<ir::Type> {
match ty {
wasmparser::TypeOrFuncType::Type(ty) => type_to_type(ty),
wasmparser::TypeOrFuncType::FuncType(_) => {
Err(WasmError::Unsupported("multi-value block signatures"))
}
}
}
pub fn f32_translation(x: wasmparser::Ieee32) -> ir::immediates::Ieee32 {
ir::immediates::Ieee32::with_bits(x.bits())
}
pub fn f64_translation(x: wasmparser::Ieee64) -> ir::immediates::Ieee64 {
ir::immediates::Ieee64::with_bits(x.bits())
}
pub fn num_return_values(ty: wasmparser::TypeOrFuncType) -> WasmResult<usize> {
match ty {
wasmparser::TypeOrFuncType::Type(ty) => match ty {
wasmparser::Type::EmptyBlockType => Ok(0),
wasmparser::Type::I32
| wasmparser::Type::F32
| wasmparser::Type::I64
| wasmparser::Type::F64 => Ok(1),
_ => Err(WasmError::Unsupported("unsupported return value type")),
},
wasmparser::TypeOrFuncType::FuncType(_) => {
Err(WasmError::Unsupported("multi-value block signatures"))
}
}
}
pub fn get_vmctx_value_label() -> ir::ValueLabel {
const VMCTX_LABEL: u32 = 0xffff_fffe;
ir::ValueLabel::from_u32(VMCTX_LABEL)
}