| 1 |
use crate::core::*; |
| 2 |
use wasm_encoder::Instruction; |
| 3 |
|
| 4 |
use super::CodeBuilder; |
| 5 |
|
| 6 |
// For loads, we dynamically check whether the load will |
| 7 |
// trap, and if it will then we generate a dummy value to |
| 8 |
// use instead. |
| 9 |
pub(crate) fn load<'a>( |
| 10 |
inst: Instruction<'a>, |
| 11 |
module: &Module, |
| 12 |
builder: &mut CodeBuilder, |
| 13 |
insts: &mut Vec<Instruction<'a>>, |
| 14 |
) { |
| 15 |
let memarg = get_memarg(&inst); |
| 16 |
let memory = &module.memories[memarg.memory_index as usize]; |
| 17 |
let address_type = if memory.memory64 { |
| 18 |
ValType::I64 |
| 19 |
} else { |
| 20 |
ValType::I32 |
| 21 |
}; |
| 22 |
// Add a temporary local to hold this load's address. |
| 23 |
let address_local = builder.alloc_local(address_type); |
| 24 |
|
| 25 |
// Add a temporary local to hold the result of this load. |
| 26 |
let load_type = type_of_memory_access(&inst); |
| 27 |
let result_local = builder.alloc_local(load_type); |
| 28 |
|
| 29 |
// [address:address_type] |
| 30 |
insts.push(Instruction::LocalSet(address_local)); |
| 31 |
// [] |
| 32 |
insts.push(Instruction::Block(wasm_encoder::BlockType::Empty)); |
| 33 |
{ |
| 34 |
// [] |
| 35 |
insts.push(Instruction::Block(wasm_encoder::BlockType::Empty)); |
| 36 |
{ |
| 37 |
// [] |
| 38 |
insts.push(Instruction::MemorySize(memarg.memory_index)); |
| 39 |
// [mem_size_in_pages:address_type] |
| 40 |
insts.push(int_const_inst( |
| 41 |
address_type, |
| 42 |
crate::page_size(memory).into(), |
| 43 |
)); |
| 44 |
// [mem_size_in_pages:address_type wasm_page_size:address_type] |
| 45 |
insts.push(int_mul_inst(address_type)); |
| 46 |
// [mem_size_in_bytes:address_type] |
| 47 |
insts.push(int_const_inst( |
| 48 |
address_type, |
| 49 |
(memarg.offset + size_of_type_in_memory(load_type)) as i64, |
| 50 |
)); |
| 51 |
// [mem_size_in_bytes:address_type offset_and_size:address_type] |
| 52 |
insts.push(Instruction::LocalGet(address_local)); |
| 53 |
// [mem_size_in_bytes:address_type offset_and_size:address_type address:address_type] |
| 54 |
insts.push(int_add_inst(address_type)); |
| 55 |
// [mem_size_in_bytes:address_type highest_byte_accessed:address_type] |
| 56 |
insts.push(int_le_u_inst(address_type)); |
| 57 |
// [load_will_trap:i32] |
| 58 |
insts.push(Instruction::BrIf(0)); |
| 59 |
// [] |
| 60 |
insts.push(Instruction::LocalGet(address_local)); |
| 61 |
// [address:address_type] |
| 62 |
insts.push(int_const_inst(address_type, 0)); |
| 63 |
// [address:address_type 0:address_type] |
| 64 |
insts.push(int_le_s_inst(address_type)); |
| 65 |
// [load_will_trap:i32] |
| 66 |
insts.push(Instruction::BrIf(0)); |
| 67 |
// [] |
| 68 |
insts.push(Instruction::LocalGet(address_local)); |
| 69 |
// [address:address_type] |
| 70 |
insts.push(inst); |
| 71 |
// [result:load_type] |
| 72 |
insts.push(Instruction::LocalSet(result_local)); |
| 73 |
// [] |
| 74 |
insts.push(Instruction::Br(1)); |
| 75 |
// <unreachable> |
| 76 |
} |
| 77 |
// [] |
| 78 |
insts.push(Instruction::End); |
| 79 |
// [] |
| 80 |
insts.push(dummy_value_inst(load_type)); |
| 81 |
// [dummy_value:load_type] |
| 82 |
insts.push(Instruction::LocalSet(result_local)); |
| 83 |
// [] |
| 84 |
} |
| 85 |
// [] |
| 86 |
insts.push(Instruction::End); |
| 87 |
// [] |
| 88 |
insts.push(Instruction::LocalGet(result_local)); |
| 89 |
// [result:load_type] |
| 90 |
} |
| 91 |
|
| 92 |
// Stores are similar to loads: we check whether the store |
| 93 |
// will trap, and if it will then we just drop the value. |
| 94 |
pub(crate) fn store<'a>( |
| 95 |
inst: Instruction<'a>, |
| 96 |
module: &Module, |
| 97 |
builder: &mut CodeBuilder, |
| 98 |
insts: &mut Vec<Instruction<'a>>, |
| 99 |
) { |
| 100 |
let memarg = get_memarg(&inst); |
| 101 |
let memory = &module.memories[memarg.memory_index as usize]; |
| 102 |
let address_type = if memory.memory64 { |
| 103 |
ValType::I64 |
| 104 |
} else { |
| 105 |
ValType::I32 |
| 106 |
}; |
| 107 |
|
| 108 |
// Add a temporary local to hold this store's address. |
| 109 |
let address_local = builder.alloc_local(address_type); |
| 110 |
|
| 111 |
// Add a temporary local to hold the value to store. |
| 112 |
let store_type = type_of_memory_access(&inst); |
| 113 |
let value_local = builder.alloc_local(store_type); |
| 114 |
|
| 115 |
// [address:address_type value:store_type] |
| 116 |
insts.push(Instruction::LocalSet(value_local)); |
| 117 |
// [address:address_type] |
| 118 |
insts.push(Instruction::LocalSet(address_local)); |
| 119 |
// [] |
| 120 |
insts.push(Instruction::MemorySize(memarg.memory_index)); |
| 121 |
// [mem_size_in_pages:address_type] |
| 122 |
insts.push(int_const_inst( |
| 123 |
address_type, |
| 124 |
crate::page_size(memory).into(), |
| 125 |
)); |
| 126 |
// [mem_size_in_pages:address_type wasm_page_size:address_type] |
| 127 |
insts.push(int_mul_inst(address_type)); |
| 128 |
// [mem_size_in_bytes:address_type] |
| 129 |
insts.push(int_const_inst( |
| 130 |
address_type, |
| 131 |
(memarg.offset + size_of_type_in_memory(store_type)) as i64, |
| 132 |
)); |
| 133 |
// [mem_size_in_bytes:address_type offset_and_size:address_type] |
| 134 |
insts.push(Instruction::LocalGet(address_local)); |
| 135 |
// [mem_size_in_bytes:address_type offset_and_size:address_type address:address_type] |
| 136 |
insts.push(int_add_inst(address_type)); |
| 137 |
// [mem_size_in_bytes:address_type highest_byte_accessed:address_type] |
| 138 |
insts.push(int_le_u_inst(address_type)); |
| 139 |
// [store_will_trap:i32] |
| 140 |
insts.push(Instruction::If(BlockType::Empty)); |
| 141 |
insts.push(Instruction::Else); |
| 142 |
{ |
| 143 |
// [] |
| 144 |
insts.push(Instruction::LocalGet(address_local)); |
| 145 |
// [address:address_type] |
| 146 |
insts.push(int_const_inst(address_type, 0)); |
| 147 |
// [address:address_type 0:address_type] |
| 148 |
insts.push(int_le_s_inst(address_type)); |
| 149 |
// [load_will_trap:i32] |
| 150 |
insts.push(Instruction::If(BlockType::Empty)); |
| 151 |
insts.push(Instruction::Else); |
| 152 |
{ |
| 153 |
// [] |
| 154 |
insts.push(Instruction::LocalGet(address_local)); |
| 155 |
// [address:address_type] |
| 156 |
insts.push(Instruction::LocalGet(value_local)); |
| 157 |
// [address:address_type value:store_type] |
| 158 |
insts.push(inst); |
| 159 |
// [] |
| 160 |
} |
| 161 |
insts.push(Instruction::End); |
| 162 |
} |
| 163 |
// [] |
| 164 |
insts.push(Instruction::End); |
| 165 |
} |
| 166 |
|
| 167 |
// Unsigned integer division and remainder will trap when |
| 168 |
// the divisor is 0. To avoid the trap, we will set any 0 |
| 169 |
// divisors to 1 prior to the operation. |
| 170 |
// |
| 171 |
// The code below is equivalent to this expression: |
| 172 |
// |
| 173 |
// local.set $temp_divisor |
| 174 |
// (select (i32.eqz (local.get $temp_divisor) (i32.const 1) (local.get $temp_divisor)) |
| 175 |
pub(crate) fn unsigned_div_rem<'a>( |
| 176 |
inst: Instruction<'a>, |
| 177 |
builder: &mut CodeBuilder, |
| 178 |
insts: &mut Vec<Instruction<'a>>, |
| 179 |
) { |
| 180 |
let op_type = type_of_integer_operation(&inst); |
| 181 |
let temp_divisor = builder.alloc_local(op_type); |
| 182 |
|
| 183 |
// [dividend:op_type divisor:op_type] |
| 184 |
insts.push(Instruction::LocalSet(temp_divisor)); |
| 185 |
// [dividend:op_type] |
| 186 |
insts.push(int_const_inst(op_type, 1)); |
| 187 |
// [dividend:op_type 1:op_type] |
| 188 |
insts.push(Instruction::LocalGet(temp_divisor)); |
| 189 |
// [dividend:op_type 1:op_type divisor:op_type] |
| 190 |
insts.push(Instruction::LocalGet(temp_divisor)); |
| 191 |
// [dividend:op_type 1:op_type divisor:op_type divisor:op_type] |
| 192 |
insts.push(eqz_inst(op_type)); |
| 193 |
// [dividend:op_type 1:op_type divisor:op_type is_zero:i32] |
| 194 |
insts.push(Instruction::Select); |
| 195 |
// [dividend:op_type divisor:op_type] |
| 196 |
insts.push(inst); |
| 197 |
// [result:op_type] |
| 198 |
} |
| 199 |
|
| 200 |
pub(crate) fn trunc<'a>( |
| 201 |
inst: Instruction<'a>, |
| 202 |
builder: &mut CodeBuilder, |
| 203 |
insts: &mut Vec<Instruction<'a>>, |
| 204 |
) { |
| 205 |
// If NaN or ±inf, replace with dummy value. Our method of checking for NaN |
| 206 |
// is to use `ne` because NaN is the only value that is not equal to itself |
| 207 |
let conv_type = type_of_float_conversion(&inst); |
| 208 |
let temp_float = builder.alloc_local(conv_type); |
| 209 |
// [input:conv_type] |
| 210 |
insts.push(Instruction::LocalTee(temp_float)); |
| 211 |
// [input:conv_type] |
| 212 |
insts.push(Instruction::LocalGet(temp_float)); |
| 213 |
// [input:conv_type input:conv_type] |
| 214 |
insts.push(ne_inst(conv_type)); |
| 215 |
// [is_nan:i32] |
| 216 |
insts.push(Instruction::LocalGet(temp_float)); |
| 217 |
// [is_nan:i32 input:conv_type] |
| 218 |
insts.push(flt_inf_const_inst(conv_type)); |
| 219 |
// [is_nan:i32 input:conv_type inf:conv_type] |
| 220 |
insts.push(eq_inst(conv_type)); |
| 221 |
// [is_nan:i32 is_inf:i32] |
| 222 |
insts.push(Instruction::LocalGet(temp_float)); |
| 223 |
// [is_nan:i32 is_inf:i32 input:conv_type] |
| 224 |
insts.push(flt_neg_inf_const_inst(conv_type)); |
| 225 |
// [is_nan:i32 is_inf:i32 input:conv_type neg_inf:conv_type] |
| 226 |
insts.push(eq_inst(conv_type)); |
| 227 |
// [is_nan:i32 is_inf:i32 is_neg_inf:i32] |
| 228 |
insts.push(Instruction::I32Or); |
| 229 |
// [is_nan:i32 is_±inf:i32] |
| 230 |
insts.push(Instruction::I32Or); |
| 231 |
// [is_nan_or_inf:i32] |
| 232 |
insts.push(Instruction::If(BlockType::Empty)); |
| 233 |
{ |
| 234 |
// [] |
| 235 |
insts.push(dummy_value_inst(conv_type)); |
| 236 |
// [0:conv_type] |
| 237 |
insts.push(Instruction::LocalSet(temp_float)); |
| 238 |
// [] |
| 239 |
} |
| 240 |
insts.push(Instruction::End); |
| 241 |
// [] |
| 242 |
insts.push(Instruction::LocalGet(temp_float)); |
| 243 |
// [input_or_0:conv_type] |
| 244 |
|
| 245 |
// first ensure that it is >= the min value of our target type |
| 246 |
insts.push(min_input_const_for_trunc(&inst)); |
| 247 |
// [input_or_0:conv_type min_value_of_target_type:conv_type] |
| 248 |
insts.push(flt_lt_inst(conv_type)); |
| 249 |
// [input_lt_min:i32] |
| 250 |
insts.push(Instruction::If(BlockType::Empty)); |
| 251 |
{ |
| 252 |
// [] |
| 253 |
insts.push(min_input_const_for_trunc(&inst)); |
| 254 |
// [min_value_of_target_type:conv_type] |
| 255 |
insts.push(Instruction::LocalSet(temp_float)); |
| 256 |
} |
| 257 |
insts.push(Instruction::End); |
| 258 |
// [] |
| 259 |
insts.push(Instruction::LocalGet(temp_float)); |
| 260 |
// [coerced_input:conv_type] |
| 261 |
|
| 262 |
// next ensure that it is <= the max value of our target type |
| 263 |
insts.push(max_input_const_for_trunc(&inst)); |
| 264 |
// [input_or_0:conv_type max_value_of_target_type:conv_type] |
| 265 |
insts.push(flt_gt_inst(conv_type)); |
| 266 |
// [input_gt_min:i32] |
| 267 |
insts.push(Instruction::If(BlockType::Empty)); |
| 268 |
{ |
| 269 |
// [] |
| 270 |
insts.push(max_input_const_for_trunc(&inst)); |
| 271 |
// [max_value_of_target_type:conv_type] |
| 272 |
insts.push(Instruction::LocalSet(temp_float)); |
| 273 |
} |
| 274 |
insts.push(Instruction::End); |
| 275 |
// [] |
| 276 |
insts.push(Instruction::LocalGet(temp_float)); |
| 277 |
// [coerced_input:conv_type] |
| 278 |
insts.push(inst); |
| 279 |
} |
| 280 |
|
| 281 |
// Signed division and remainder will trap in the following instances: |
| 282 |
// - The divisor is 0 |
| 283 |
// - The result of the division is 2^(n-1) |
| 284 |
pub(crate) fn signed_div_rem<'a>( |
| 285 |
inst: Instruction<'a>, |
| 286 |
builder: &mut CodeBuilder, |
| 287 |
insts: &mut Vec<Instruction<'a>>, |
| 288 |
) { |
| 289 |
// If divisor is 0, replace with 1 |
| 290 |
let op_type = type_of_integer_operation(&inst); |
| 291 |
let temp_divisor = builder.alloc_local(op_type); |
| 292 |
// [dividend:op_type divisor:op_type] |
| 293 |
insts.push(Instruction::LocalSet(temp_divisor)); |
| 294 |
// [dividend:op_type] |
| 295 |
insts.push(int_const_inst(op_type, 1)); |
| 296 |
// [dividend:op_type 1:op_type] |
| 297 |
insts.push(Instruction::LocalGet(temp_divisor)); |
| 298 |
// [dividend:op_type 1:op_type divisor:op_type] |
| 299 |
insts.push(Instruction::LocalGet(temp_divisor)); |
| 300 |
// [dividend:op_type 1:op_type divisor:op_type divisor:op_type] |
| 301 |
insts.push(eqz_inst(op_type)); |
| 302 |
// [dividend:op_type 1:op_type divisor:op_type is_zero:i32] |
| 303 |
insts.push(Instruction::Select); |
| 304 |
// [dividend:op_type divisor:op_type] |
| 305 |
// If dividend and divisor are -int.max and -1, replace |
| 306 |
// divisor with 1. |
| 307 |
let temp_dividend = builder.alloc_local(op_type); |
| 308 |
insts.push(Instruction::LocalSet(temp_divisor)); |
| 309 |
// [dividend:op_type] |
| 310 |
insts.push(Instruction::LocalSet(temp_dividend)); |
| 311 |
// [] |
| 312 |
insts.push(Instruction::Block(wasm_encoder::BlockType::Empty)); |
| 313 |
{ |
| 314 |
insts.push(Instruction::Block(wasm_encoder::BlockType::Empty)); |
| 315 |
{ |
| 316 |
// [] |
| 317 |
insts.push(Instruction::LocalGet(temp_dividend)); |
| 318 |
// [dividend:op_type] |
| 319 |
insts.push(Instruction::LocalGet(temp_divisor)); |
| 320 |
// [dividend:op_type divisor:op_type] |
| 321 |
insts.push(Instruction::LocalSet(temp_divisor)); |
| 322 |
// [dividend:op_type] |
| 323 |
insts.push(Instruction::LocalTee(temp_dividend)); |
| 324 |
// [dividend:op_type] |
| 325 |
insts.push(int_min_const_inst(op_type)); |
| 326 |
// [dividend:op_type int_min:op_type] |
| 327 |
insts.push(ne_inst(op_type)); |
| 328 |
// [not_int_min:i32] |
| 329 |
insts.push(Instruction::BrIf(0)); |
| 330 |
// [] |
| 331 |
insts.push(Instruction::LocalGet(temp_divisor)); |
| 332 |
// [divisor:op_type] |
| 333 |
insts.push(int_const_inst(op_type, -1)); |
| 334 |
// [divisor:op_type -1:op_type] |
| 335 |
insts.push(ne_inst(op_type)); |
| 336 |
// [not_neg_one:i32] |
| 337 |
insts.push(Instruction::BrIf(0)); |
| 338 |
// [] |
| 339 |
insts.push(int_const_inst(op_type, 1)); |
| 340 |
// [divisor:op_type] |
| 341 |
insts.push(Instruction::LocalSet(temp_divisor)); |
| 342 |
// [] |
| 343 |
insts.push(Instruction::Br(1)); |
| 344 |
} |
| 345 |
// [] |
| 346 |
insts.push(Instruction::End); |
| 347 |
} |
| 348 |
// [] |
| 349 |
insts.push(Instruction::End); |
| 350 |
// [] |
| 351 |
insts.push(Instruction::LocalGet(temp_dividend)); |
| 352 |
// [dividend:op_type] |
| 353 |
insts.push(Instruction::LocalGet(temp_divisor)); |
| 354 |
// [dividend:op_type divisor:op_type] |
| 355 |
insts.push(inst); |
| 356 |
} |
| 357 |
|
| 358 |
fn get_memarg(inst: &Instruction) -> wasm_encoder::MemArg { |
| 359 |
match *inst { |
| 360 |
Instruction::I32Load(memarg) |
| 361 |
| Instruction::I64Load(memarg) |
| 362 |
| Instruction::F32Load(memarg) |
| 363 |
| Instruction::F64Load(memarg) |
| 364 |
| Instruction::I32Load8S(memarg) |
| 365 |
| Instruction::I32Load8U(memarg) |
| 366 |
| Instruction::I32Load16S(memarg) |
| 367 |
| Instruction::I32Load16U(memarg) |
| 368 |
| Instruction::I64Load8S(memarg) |
| 369 |
| Instruction::I64Load8U(memarg) |
| 370 |
| Instruction::I64Load16S(memarg) |
| 371 |
| Instruction::I64Load16U(memarg) |
| 372 |
| Instruction::I64Load32S(memarg) |
| 373 |
| Instruction::I64Load32U(memarg) |
| 374 |
| Instruction::V128Load(memarg) |
| 375 |
| Instruction::V128Load8x8S(memarg) |
| 376 |
| Instruction::V128Load8x8U(memarg) |
| 377 |
| Instruction::V128Load16x4S(memarg) |
| 378 |
| Instruction::V128Load16x4U(memarg) |
| 379 |
| Instruction::V128Load32x2S(memarg) |
| 380 |
| Instruction::V128Load32x2U(memarg) |
| 381 |
| Instruction::V128Load8Splat(memarg) |
| 382 |
| Instruction::V128Load16Splat(memarg) |
| 383 |
| Instruction::V128Load32Splat(memarg) |
| 384 |
| Instruction::V128Load64Splat(memarg) |
| 385 |
| Instruction::V128Load32Zero(memarg) |
| 386 |
| Instruction::V128Load64Zero(memarg) |
| 387 |
| Instruction::I32Store(memarg) |
| 388 |
| Instruction::I64Store(memarg) |
| 389 |
| Instruction::F32Store(memarg) |
| 390 |
| Instruction::F64Store(memarg) |
| 391 |
| Instruction::I32Store8(memarg) |
| 392 |
| Instruction::I32Store16(memarg) |
| 393 |
| Instruction::I64Store8(memarg) |
| 394 |
| Instruction::I64Store16(memarg) |
| 395 |
| Instruction::I64Store32(memarg) |
| 396 |
| Instruction::V128Store(memarg) => memarg, |
| 397 |
_ => unreachable!(), |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
fn dummy_value_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 402 |
match ty { |
| 403 |
ValType::I32 => Instruction::I32Const(0), |
| 404 |
ValType::I64 => Instruction::I64Const(0), |
| 405 |
ValType::F32 => Instruction::F32Const(0.0.into()), |
| 406 |
ValType::F64 => Instruction::F64Const(0.0.into()), |
| 407 |
ValType::V128 => Instruction::V128Const(0), |
| 408 |
ValType::Ref(ty) => { |
| 409 |
assert!(ty.nullable); |
| 410 |
Instruction::RefNull(ty.heap_type) |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
fn eq_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 416 |
match ty { |
| 417 |
ValType::F32 => Instruction::F32Eq, |
| 418 |
ValType::F64 => Instruction::F64Eq, |
| 419 |
ValType::I32 => Instruction::I32Eq, |
| 420 |
ValType::I64 => Instruction::I64Eq, |
| 421 |
_ => panic!("not a numeric type"), |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
fn eqz_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 426 |
match ty { |
| 427 |
ValType::I32 => Instruction::I32Eqz, |
| 428 |
ValType::I64 => Instruction::I64Eqz, |
| 429 |
_ => panic!("not an integer type"), |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
fn type_of_integer_operation(inst: &Instruction) -> ValType { |
| 434 |
match inst { |
| 435 |
Instruction::I32DivU |
| 436 |
| Instruction::I32DivS |
| 437 |
| Instruction::I32RemU |
| 438 |
| Instruction::I32RemS => ValType::I32, |
| 439 |
Instruction::I64RemU |
| 440 |
| Instruction::I64DivU |
| 441 |
| Instruction::I64DivS |
| 442 |
| Instruction::I64RemS => ValType::I64, |
| 443 |
_ => panic!("not integer division or remainder"), |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
fn type_of_float_conversion(inst: &Instruction) -> ValType { |
| 448 |
match inst { |
| 449 |
Instruction::I32TruncF32S |
| 450 |
| Instruction::I32TruncF32U |
| 451 |
| Instruction::I64TruncF32S |
| 452 |
| Instruction::I64TruncF32U => ValType::F32, |
| 453 |
Instruction::I32TruncF64S |
| 454 |
| Instruction::I32TruncF64U |
| 455 |
| Instruction::I64TruncF64S |
| 456 |
| Instruction::I64TruncF64U => ValType::F64, |
| 457 |
_ => panic!("not a float -> integer conversion"), |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
fn min_input_const_for_trunc<'a>(inst: &Instruction) -> Instruction<'a> { |
| 462 |
// This is the minimum float value that is representable as an i64 |
| 463 |
let min_f64 = -9_223_372_036_854_775_000f64; |
| 464 |
let min_f32 = -9_223_372_000_000_000_000f32; |
| 465 |
|
| 466 |
// This is the minimum float value that is representable as as i32 |
| 467 |
let min_f32_as_i32 = -2_147_483_500f32; |
| 468 |
match inst { |
| 469 |
Instruction::I32TruncF32S => Instruction::F32Const(min_f32_as_i32.into()), |
| 470 |
Instruction::I32TruncF32U => Instruction::F32Const(0.0.into()), |
| 471 |
Instruction::I64TruncF32S => Instruction::F32Const(min_f32.into()), |
| 472 |
Instruction::I64TruncF32U => Instruction::F32Const(0.0.into()), |
| 473 |
Instruction::I32TruncF64S => Instruction::F64Const((i32::MIN as f64).into()), |
| 474 |
Instruction::I32TruncF64U => Instruction::F64Const(0.0.into()), |
| 475 |
Instruction::I64TruncF64S => Instruction::F64Const(min_f64.into()), |
| 476 |
Instruction::I64TruncF64U => Instruction::F64Const(0.0.into()), |
| 477 |
_ => panic!("not a trunc instruction"), |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
fn max_input_const_for_trunc<'a>(inst: &Instruction) -> Instruction<'a> { |
| 482 |
// This is the maximum float value that is representable as as i64 |
| 483 |
let max_f64_as_i64 = 9_223_372_036_854_775_000f64; |
| 484 |
let max_f32_as_i64 = 9_223_371_500_000_000_000f32; |
| 485 |
|
| 486 |
// This is the maximum float value that is representable as as i32 |
| 487 |
let max_f32_as_i32 = 2_147_483_500f32; |
| 488 |
match inst { |
| 489 |
Instruction::I32TruncF32S | Instruction::I32TruncF32U => { |
| 490 |
Instruction::F32Const(max_f32_as_i32.into()) |
| 491 |
} |
| 492 |
Instruction::I64TruncF32S | Instruction::I64TruncF32U => { |
| 493 |
Instruction::F32Const(max_f32_as_i64.into()) |
| 494 |
} |
| 495 |
Instruction::I32TruncF64S | Instruction::I32TruncF64U => { |
| 496 |
Instruction::F64Const((i32::MAX as f64).into()) |
| 497 |
} |
| 498 |
Instruction::I64TruncF64S | Instruction::I64TruncF64U => { |
| 499 |
Instruction::F64Const(max_f64_as_i64.into()) |
| 500 |
} |
| 501 |
_ => panic!("not a trunc instruction"), |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
fn type_of_memory_access(inst: &Instruction) -> ValType { |
| 506 |
match inst { |
| 507 |
Instruction::I32Load(_) |
| 508 |
| Instruction::I32Load8S(_) |
| 509 |
| Instruction::I32Load8U(_) |
| 510 |
| Instruction::I32Load16S(_) |
| 511 |
| Instruction::I32Load16U(_) |
| 512 |
| Instruction::I32Store(_) |
| 513 |
| Instruction::I32Store8(_) |
| 514 |
| Instruction::I32Store16(_) => ValType::I32, |
| 515 |
|
| 516 |
Instruction::I64Load(_) |
| 517 |
| Instruction::I64Load8S(_) |
| 518 |
| Instruction::I64Load8U(_) |
| 519 |
| Instruction::I64Load16S(_) |
| 520 |
| Instruction::I64Load16U(_) |
| 521 |
| Instruction::I64Load32S(_) |
| 522 |
| Instruction::I64Load32U(_) |
| 523 |
| Instruction::I64Store(_) |
| 524 |
| Instruction::I64Store8(_) |
| 525 |
| Instruction::I64Store16(_) |
| 526 |
| Instruction::I64Store32(_) => ValType::I64, |
| 527 |
|
| 528 |
Instruction::F32Load(_) | Instruction::F32Store(_) => ValType::F32, |
| 529 |
|
| 530 |
Instruction::F64Load(_) | Instruction::F64Store(_) => ValType::F64, |
| 531 |
|
| 532 |
Instruction::V128Load { .. } |
| 533 |
| Instruction::V128Load8x8S { .. } |
| 534 |
| Instruction::V128Load8x8U { .. } |
| 535 |
| Instruction::V128Load16x4S { .. } |
| 536 |
| Instruction::V128Load16x4U { .. } |
| 537 |
| Instruction::V128Load32x2S { .. } |
| 538 |
| Instruction::V128Load32x2U { .. } |
| 539 |
| Instruction::V128Load8Splat { .. } |
| 540 |
| Instruction::V128Load16Splat { .. } |
| 541 |
| Instruction::V128Load32Splat { .. } |
| 542 |
| Instruction::V128Load64Splat { .. } |
| 543 |
| Instruction::V128Load32Zero { .. } |
| 544 |
| Instruction::V128Load64Zero { .. } |
| 545 |
| Instruction::V128Store { .. } => ValType::V128, |
| 546 |
|
| 547 |
_ => panic!("not a memory access instruction"), |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
fn int_min_const_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 552 |
match ty { |
| 553 |
ValType::I32 => Instruction::I32Const(i32::MIN), |
| 554 |
ValType::I64 => Instruction::I64Const(i64::MIN), |
| 555 |
_ => panic!("not an int type"), |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
fn int_const_inst<'a>(ty: ValType, x: i64) -> Instruction<'a> { |
| 560 |
match ty { |
| 561 |
ValType::I32 => Instruction::I32Const(x as i32), |
| 562 |
ValType::I64 => Instruction::I64Const(x), |
| 563 |
_ => panic!("not an int type"), |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
fn int_mul_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 568 |
match ty { |
| 569 |
ValType::I32 => Instruction::I32Mul, |
| 570 |
ValType::I64 => Instruction::I64Mul, |
| 571 |
_ => panic!("not an int type"), |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
fn int_add_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 576 |
match ty { |
| 577 |
ValType::I32 => Instruction::I32Add, |
| 578 |
ValType::I64 => Instruction::I64Add, |
| 579 |
_ => panic!("not an int type"), |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
fn int_le_u_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 584 |
match ty { |
| 585 |
ValType::I32 => Instruction::I32LeU, |
| 586 |
ValType::I64 => Instruction::I64LeU, |
| 587 |
_ => panic!("not an int type"), |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
fn int_le_s_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 592 |
match ty { |
| 593 |
ValType::I32 => Instruction::I32LeS, |
| 594 |
ValType::I64 => Instruction::I64LeS, |
| 595 |
_ => panic!("not an int type"), |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
fn ne_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 600 |
match ty { |
| 601 |
ValType::I32 => Instruction::I32Ne, |
| 602 |
ValType::I64 => Instruction::I64Ne, |
| 603 |
ValType::F32 => Instruction::F32Ne, |
| 604 |
ValType::F64 => Instruction::F64Ne, |
| 605 |
_ => panic!("not a numeric type"), |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
fn flt_lt_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 610 |
match ty { |
| 611 |
ValType::F32 => Instruction::F32Lt, |
| 612 |
ValType::F64 => Instruction::F64Lt, |
| 613 |
_ => panic!("not a float type"), |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
fn flt_gt_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 618 |
match ty { |
| 619 |
ValType::F32 => Instruction::F32Gt, |
| 620 |
ValType::F64 => Instruction::F64Gt, |
| 621 |
_ => panic!("not a float type"), |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
fn flt_inf_const_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 626 |
match ty { |
| 627 |
ValType::F32 => Instruction::F32Const(f32::INFINITY.into()), |
| 628 |
ValType::F64 => Instruction::F64Const(f64::INFINITY.into()), |
| 629 |
_ => panic!("not a float type"), |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
fn flt_neg_inf_const_inst<'a>(ty: ValType) -> Instruction<'a> { |
| 634 |
match ty { |
| 635 |
ValType::F32 => Instruction::F32Const(f32::NEG_INFINITY.into()), |
| 636 |
ValType::F64 => Instruction::F64Const(f64::NEG_INFINITY.into()), |
| 637 |
_ => panic!("not a float type"), |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
fn size_of_type_in_memory(ty: ValType) -> u64 { |
| 642 |
match ty { |
| 643 |
ValType::I32 => 4, |
| 644 |
ValType::I64 => 8, |
| 645 |
ValType::F32 => 4, |
| 646 |
ValType::F64 => 8, |
| 647 |
ValType::V128 => 16, |
| 648 |
ValType::Ref(_) => panic!("not a memory type"), |
| 649 |
} |
| 650 |
} |