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
use wasmparser;
use cretonne;
use std::u32;
pub type FunctionIndex = usize;
pub type TableIndex = usize;
pub type GlobalIndex = usize;
pub type MemoryIndex = usize;
pub type SignatureIndex = usize;
#[derive(Debug, Clone, Copy)]
pub struct Global {
pub ty: cretonne::ir::Type,
pub mutability: bool,
pub initializer: GlobalInit,
}
#[derive(Debug, Clone, Copy)]
pub enum GlobalInit {
I32Const(i32),
I64Const(i64),
F32Const(u32),
F64Const(u64),
GlobalRef(GlobalIndex),
Import(),
}
#[derive(Debug, Clone, Copy)]
pub struct Table {
pub ty: TableElementType,
pub size: usize,
pub maximum: Option<usize>,
}
#[derive(Debug, Clone, Copy)]
pub enum TableElementType {
Val(cretonne::ir::Type),
Func(),
}
#[derive(Debug, Clone, Copy)]
pub struct Memory {
pub pages_count: usize,
pub maximum: Option<usize>,
pub shared: bool,
}
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Local(pub u32);
impl cretonne::entity::EntityRef for Local {
fn new(index: usize) -> Self {
debug_assert!(index < (u32::MAX as usize));
Local(index as u32)
}
fn index(self) -> usize {
self.0 as usize
}
}
impl Default for Local {
fn default() -> Self {
Local(u32::MAX)
}
}
pub fn type_to_type(ty: &wasmparser::Type) -> Result<cretonne::ir::Type, ()> {
match *ty {
wasmparser::Type::I32 => Ok(cretonne::ir::types::I32),
wasmparser::Type::I64 => Ok(cretonne::ir::types::I64),
wasmparser::Type::F32 => Ok(cretonne::ir::types::F32),
wasmparser::Type::F64 => Ok(cretonne::ir::types::F64),
_ => Err(()),
}
}
pub fn f32_translation(x: wasmparser::Ieee32) -> cretonne::ir::immediates::Ieee32 {
cretonne::ir::immediates::Ieee32::with_bits(x.bits())
}
pub fn f64_translation(x: wasmparser::Ieee64) -> cretonne::ir::immediates::Ieee64 {
cretonne::ir::immediates::Ieee64::with_bits(x.bits())
}
pub fn num_return_values(ty: wasmparser::Type) -> usize {
match ty {
wasmparser::Type::EmptyBlockType => 0,
wasmparser::Type::I32 |
wasmparser::Type::F32 |
wasmparser::Type::I64 |
wasmparser::Type::F64 => 1,
_ => panic!("unsupported return value type"),
}
}