Cranelift: Add heap_load and heap_store instructions (#5300)
* Cranelift: Define `heap_load` and `heap_store` instructions
* Cranelift: Implement interpreter support for `heap_load` and `heap_store`
* Cranelift: Add a suite runtests for `heap_{load,store}`
There are so many knobs we can twist for heaps and I wanted to exhaustively test
all of them, so I wrote a script to generate the tests. I've checked in the
script in case we want to make any changes in the future, but I don't think it
is worth adding this to CI to check that scripts are up to date or anything like
that.
* Review feedback
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::state::{MemoryError, State};
|
||||
use crate::value::{Value, ValueConversionKind, ValueError, ValueResult};
|
||||
use cranelift_codegen::data_value::DataValue;
|
||||
use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
|
||||
use cranelift_codegen::ir::immediates::HeapImmData;
|
||||
use cranelift_codegen::ir::{
|
||||
types, AbiParam, Block, ExternalName, FuncRef, Function, InstructionData, Opcode, TrapCode,
|
||||
Type, Value as ValueRef,
|
||||
@@ -484,6 +485,59 @@ where
|
||||
}
|
||||
Opcode::SymbolValue => unimplemented!("SymbolValue"),
|
||||
Opcode::TlsValue => unimplemented!("TlsValue"),
|
||||
Opcode::HeapLoad => {
|
||||
if let InstructionData::HeapLoad { heap_imm, arg, .. } = inst {
|
||||
let HeapImmData {
|
||||
flags,
|
||||
heap,
|
||||
offset,
|
||||
} = state.get_current_function().dfg.heap_imms[heap_imm];
|
||||
let offset = i128::from(u32::from(offset));
|
||||
let addr_ty = state.get_current_function().dfg.value_type(arg);
|
||||
let index = state.get_value(arg).unwrap().into_int().unwrap();
|
||||
let load_ty = inst_context.controlling_type().unwrap();
|
||||
let load_size = i128::from(load_ty.bytes());
|
||||
assign_or_memtrap(AddressSize::try_from(addr_ty).and_then(|addr_size| {
|
||||
let heap_address = index.saturating_add(offset).saturating_add(load_size);
|
||||
let heap_address =
|
||||
u64::try_from(heap_address).map_err(|e| MemoryError::OutOfBoundsLoad {
|
||||
addr: Address::try_from(0).unwrap(),
|
||||
load_size: load_size as usize,
|
||||
})?;
|
||||
let address = state.heap_address(addr_size, heap, heap_address)?;
|
||||
state.checked_load(address, load_ty)
|
||||
}))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
Opcode::HeapStore => {
|
||||
if let InstructionData::HeapStore { heap_imm, args, .. } = inst {
|
||||
let HeapImmData {
|
||||
flags,
|
||||
heap,
|
||||
offset,
|
||||
} = state.get_current_function().dfg.heap_imms[heap_imm];
|
||||
let offset = i128::from(u32::from(offset));
|
||||
let addr_ty = state.get_current_function().dfg.value_type(args[0]);
|
||||
let index = state.get_value(args[0]).unwrap().into_int().unwrap();
|
||||
let value = state.get_value(args[1]).unwrap();
|
||||
let store_ty = state.get_current_function().dfg.value_type(args[1]);
|
||||
let store_size = i128::from(store_ty.bytes());
|
||||
continue_or_memtrap(AddressSize::try_from(addr_ty).and_then(|addr_size| {
|
||||
let heap_address = index.saturating_add(offset).saturating_add(store_size);
|
||||
let heap_address =
|
||||
u64::try_from(heap_address).map_err(|e| MemoryError::OutOfBoundsStore {
|
||||
addr: Address::try_from(0).unwrap(),
|
||||
store_size: store_size as usize,
|
||||
})?;
|
||||
let address = state.heap_address(addr_size, heap, heap_address)?;
|
||||
state.checked_store(address, value)
|
||||
}))
|
||||
} else {
|
||||
unreachable!()
|
||||
}
|
||||
}
|
||||
Opcode::HeapAddr => {
|
||||
if let InstructionData::HeapAddr {
|
||||
heap,
|
||||
|
||||
Reference in New Issue
Block a user