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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::func_environ::TargetEnvironment;
use crate::std::string::ToString;
use core::u32;
use cranelift_codegen::binemit::Reloc;
use cranelift_codegen::ir::{self, AbiParam};
use cranelift_codegen::isa::TargetFrontendConfig;
use cranelift_entity::{EntityRef as CraneliftEntityRef, SecondaryMap as CraneliftSecondaryMap};
use cranelift_frontend::FunctionBuilder;
use wasmer_compiler::wasm_unsupported;
use wasmer_compiler::wasmparser;
use wasmer_compiler::{JumpTable, RelocationKind};
use wasmer_compiler::{WasmError, WasmResult};
use wasmer_types::entity::{EntityRef, SecondaryMap};
use wasmer_types::{FunctionType, Type};
use wasmer_vm::libcalls::LibCall;
pub fn signature_to_cranelift_ir(
signature: &FunctionType,
target_config: TargetFrontendConfig,
) -> ir::Signature {
let mut sig = ir::Signature::new(target_config.default_call_conv);
sig.params.extend(signature.params().iter().map(|&ty| {
let cret_arg: ir::Type = type_to_irtype(ty, target_config)
.expect("only numeric types are supported in function signatures");
AbiParam::new(cret_arg)
}));
sig.returns.extend(signature.results().iter().map(|&ty| {
let cret_arg: ir::Type = type_to_irtype(ty, target_config)
.expect("only numeric types are supported in function signatures");
AbiParam::new(cret_arg)
}));
sig.params.insert(
0,
AbiParam::special(target_config.pointer_type(), ir::ArgumentPurpose::VMContext),
);
sig
}
pub fn reference_type(target_config: TargetFrontendConfig) -> WasmResult<ir::Type> {
match target_config.pointer_type() {
ir::types::I32 => Ok(ir::types::R32),
ir::types::I64 => Ok(ir::types::R64),
_ => Err(WasmError::Unsupported(
"unsupported pointer type".to_string(),
)),
}
}
pub fn type_to_irtype(ty: Type, target_config: TargetFrontendConfig) -> WasmResult<ir::Type> {
match ty {
Type::I32 => Ok(ir::types::I32),
Type::I64 => Ok(ir::types::I64),
Type::F32 => Ok(ir::types::F32),
Type::F64 => Ok(ir::types::F64),
Type::V128 => Ok(ir::types::I8X16),
Type::ExternRef | Type::FuncRef => reference_type(target_config),
}
}
pub fn irlibcall_to_libcall(libcall: ir::LibCall) -> LibCall {
match libcall {
ir::LibCall::Probestack => LibCall::Probestack,
ir::LibCall::CeilF32 => LibCall::CeilF32,
ir::LibCall::CeilF64 => LibCall::CeilF64,
ir::LibCall::FloorF32 => LibCall::FloorF32,
ir::LibCall::FloorF64 => LibCall::FloorF64,
ir::LibCall::TruncF32 => LibCall::TruncF32,
ir::LibCall::TruncF64 => LibCall::TruncF64,
ir::LibCall::NearestF32 => LibCall::NearestF32,
ir::LibCall::NearestF64 => LibCall::NearestF64,
_ => panic!("Unsupported libcall"),
}
}
pub fn irreloc_to_relocationkind(reloc: Reloc) -> RelocationKind {
match reloc {
Reloc::Abs4 => RelocationKind::Abs4,
Reloc::Abs8 => RelocationKind::Abs8,
Reloc::X86PCRel4 => RelocationKind::X86PCRel4,
Reloc::X86PCRelRodata4 => RelocationKind::X86PCRelRodata4,
Reloc::X86CallPCRel4 => RelocationKind::X86CallPCRel4,
Reloc::X86CallPLTRel4 => RelocationKind::X86CallPLTRel4,
Reloc::X86GOTPCRel4 => RelocationKind::X86GOTPCRel4,
Reloc::Arm64Call => RelocationKind::Arm64Call,
_ => panic!("The relocation {} is not yet supported.", reloc),
}
}
pub fn block_with_params<PE: TargetEnvironment + ?Sized>(
builder: &mut FunctionBuilder,
params: &[wasmparser::Type],
environ: &PE,
) -> WasmResult<ir::Block> {
let block = builder.create_block();
for ty in params.iter() {
match ty {
wasmparser::Type::I32 => {
builder.append_block_param(block, ir::types::I32);
}
wasmparser::Type::I64 => {
builder.append_block_param(block, ir::types::I64);
}
wasmparser::Type::F32 => {
builder.append_block_param(block, ir::types::F32);
}
wasmparser::Type::F64 => {
builder.append_block_param(block, ir::types::F64);
}
wasmparser::Type::ExternRef | wasmparser::Type::FuncRef => {
builder.append_block_param(block, environ.reference_type());
}
wasmparser::Type::V128 => {
builder.append_block_param(block, ir::types::I8X16);
}
ty => {
return Err(wasm_unsupported!(
"block_with_params: type {:?} in multi-value block's signature",
ty
))
}
}
}
Ok(block)
}
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 get_vmctx_value_label() -> ir::ValueLabel {
const VMCTX_LABEL: u32 = 0xffff_fffe;
ir::ValueLabel::from_u32(VMCTX_LABEL)
}
pub fn transform_jump_table(
jt_offsets: CraneliftSecondaryMap<ir::JumpTable, u32>,
) -> SecondaryMap<JumpTable, u32> {
let mut func_jt_offsets = SecondaryMap::with_capacity(jt_offsets.capacity());
for (key, value) in jt_offsets.iter() {
let new_key = JumpTable::new(key.index());
func_jt_offsets[new_key] = *value;
}
func_jt_offsets
}