cranelift: Add stack support to the interpreter with virtual addresses (#3187)

* cranelift: Add stack support to the interpreter

We also change the approach for heap loads and stores.

Previously we would use the offset as the address to the heap. However,
this approach does not allow using the load/store instructions to
read/write from both the heap and the stack.

This commit changes the addressing mechanism of the interpreter. We now
return the real addresses from the addressing instructions
(stack_addr/heap_addr), and instead check if the address passed into
the load/store instructions points to an area in the heap or the stack.

* cranelift: Add virtual addresses to cranelift interpreter

Adds a  Virtual Addressing scheme that was discussed as a better
alternative to returning the real addresses.

The virtual addresses are split into 4 regions (stack, heap, tables and
global values), and the address itself is composed of an `entry` field
and an `offset` field. In general the `entry` field corresponds to the
instance of the resource (e.g. table5 is entry 5) and the `offset` field
is a byte offset inside that entry.

There is one exception to this which is the stack, where due to only
having one stack, the whole address is an offset field.

The number of bits in entry vs offset fields is variable with respect to
the `region` and the address size (32bits vs 64bits). This is done
because with 32 bit addresses we would have to compromise on heap size,
or have a small number of global values / tables. With 64 bit addresses
we do not have to compromise on this, but we need to support 32 bit
addresses.

* cranelift: Remove interpreter trap codes

* cranelift: Calculate frame_offset when entering or exiting a frame

* cranelift: Add safe read/write interface to DataValue

* cranelift: DataValue write full 128bit slot for booleans

* cranelift: Use DataValue accessors for trampoline.
This commit is contained in:
Afonso Bordado
2021-08-24 17:29:11 +01:00
committed by GitHub
parent f4ff7c350a
commit 2776074dfc
13 changed files with 1094 additions and 157 deletions

View File

@@ -1,8 +1,7 @@
//! Provides functionality for compiling and running CLIF IR for `run` tests.
use core::{mem, ptr};
use core::mem;
use cranelift_codegen::binemit::{NullRelocSink, NullStackMapSink, NullTrapSink};
use cranelift_codegen::data_value::DataValue;
use cranelift_codegen::ir::immediates::{Ieee32, Ieee64};
use cranelift_codegen::ir::{condcodes::IntCC, Function, InstBuilder, Signature, Type};
use cranelift_codegen::isa::{BackendVariant, TargetIsa};
use cranelift_codegen::{ir, settings, CodegenError, Context};
@@ -204,7 +203,7 @@ impl UnboxedValues {
param.value_type
);
unsafe {
Self::write_value_to(arg, slot);
arg.write_value_to(slot);
}
}
@@ -224,50 +223,12 @@ impl UnboxedValues {
// Extract the returned values from this vector.
for (slot, param) in self.0.iter().zip(&signature.returns) {
let value = unsafe { Self::read_value_from(slot, param.value_type) };
let value = unsafe { DataValue::read_value_from(slot, param.value_type) };
returns.push(value);
}
returns
}
/// Write a [DataValue] to a memory location.
unsafe fn write_value_to(v: &DataValue, p: *mut u128) {
match v {
DataValue::B(b) => ptr::write(p as *mut bool, *b),
DataValue::I8(i) => ptr::write(p as *mut i8, *i),
DataValue::I16(i) => ptr::write(p as *mut i16, *i),
DataValue::I32(i) => ptr::write(p as *mut i32, *i),
DataValue::I64(i) => ptr::write(p as *mut i64, *i),
DataValue::F32(f) => ptr::write(p as *mut Ieee32, *f),
DataValue::F64(f) => ptr::write(p as *mut Ieee64, *f),
DataValue::V128(b) => ptr::write(p as *mut [u8; 16], *b),
_ => unimplemented!(),
}
}
/// Read a [DataValue] from a memory location using a given [Type].
unsafe fn read_value_from(p: *const u128, ty: Type) -> DataValue {
match ty {
ir::types::I8 => DataValue::I8(ptr::read(p as *const i8)),
ir::types::I16 => DataValue::I16(ptr::read(p as *const i16)),
ir::types::I32 => DataValue::I32(ptr::read(p as *const i32)),
ir::types::I64 => DataValue::I64(ptr::read(p as *const i64)),
ir::types::F32 => DataValue::F32(ptr::read(p as *const Ieee32)),
ir::types::F64 => DataValue::F64(ptr::read(p as *const Ieee64)),
_ if ty.is_bool() => match ty.bytes() {
1 => DataValue::B(ptr::read(p as *const i8) != 0),
2 => DataValue::B(ptr::read(p as *const i16) != 0),
4 => DataValue::B(ptr::read(p as *const i32) != 0),
8 => DataValue::B(ptr::read(p as *const i64) != 0),
_ => unimplemented!(),
},
_ if ty.is_vector() && ty.bytes() == 16 => {
DataValue::V128(ptr::read(p as *const [u8; 16]))
}
_ => unimplemented!(),
}
}
}
/// Compile a [Function] to its executable bytes in memory.