Skip to content

Commit

Permalink
cranelift: Implement pinned reg in interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
afonso360 committed Jul 4, 2022
1 parent a2197eb commit 22b89e0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
14 changes: 14 additions & 0 deletions cranelift/filetests/filetests/runtests/pinned-reg.clif
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test interpret
test run
set enable_pinned_reg
target x86_64

function %read_write(i64) -> i64 {
block0(v0: i64):
set_pinned_reg v0
v1 = get_pinned_reg.i64
return v1
}
; run: %read_write(0) == 0
; run: %read_write(-1) == -1
; run: %read_write(0xDEADBEEF_C0FFEEEE) == 0xDEADBEEF_C0FFEEEE
10 changes: 10 additions & 0 deletions cranelift/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ pub struct InterpreterState<'a> {
pub heap: Vec<u8>,
pub iflags: HashSet<IntCC>,
pub fflags: HashSet<FloatCC>,
pub pinned_reg: DataValue,
}

impl Default for InterpreterState<'_> {
Expand All @@ -194,6 +195,7 @@ impl Default for InterpreterState<'_> {
heap: vec![0; 1024],
iflags: HashSet::new(),
fflags: HashSet::new(),
pinned_reg: DataValue::U64(0),
}
}
}
Expand Down Expand Up @@ -351,6 +353,14 @@ impl<'a> State<'a, DataValue> for InterpreterState<'a> {

Ok(v.write_to_slice(dst))
}

fn get_pinned_reg(&self) -> DataValue {
self.pinned_reg.clone()
}

fn set_pinned_reg(&mut self, v: DataValue) {
self.pinned_reg = v;
}
}

#[cfg(test)]
Expand Down
13 changes: 13 additions & 0 deletions cranelift/interpreter/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ pub trait State<'a, V> {
/// Store a value `V` into memory at the given `address`, checking if it belongs either to the
/// stack or to one of the heaps; the number of bytes stored corresponds to the specified [Type].
fn checked_store(&mut self, address: Address, v: V) -> Result<(), MemoryError>;

/// Retrieves the current pinned reg value
fn get_pinned_reg(&self) -> V;
/// Sets a value for the pinned reg
fn set_pinned_reg(&mut self, v: V);
}

#[derive(Error, Debug)]
Expand Down Expand Up @@ -162,4 +167,12 @@ where
fn checked_store(&mut self, _addr: Address, _v: V) -> Result<(), MemoryError> {
unimplemented!()
}

fn get_pinned_reg(&self) -> V {
unimplemented!()
}

fn set_pinned_reg(&mut self, _v: V) {
unimplemented!()
}
}
7 changes: 5 additions & 2 deletions cranelift/interpreter/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,11 @@ where
Opcode::SymbolValue => unimplemented!("SymbolValue"),
Opcode::TlsValue => unimplemented!("TlsValue"),
Opcode::HeapAddr => unimplemented!("HeapAddr"),
Opcode::GetPinnedReg => unimplemented!("GetPinnedReg"),
Opcode::SetPinnedReg => unimplemented!("SetPinnedReg"),
Opcode::GetPinnedReg => assign(state.get_pinned_reg()),
Opcode::SetPinnedReg => {
state.set_pinned_reg(arg(0)?);
ControlFlow::Continue
}
Opcode::TableAddr => unimplemented!("TableAddr"),
Opcode::Iconst => assign(Value::int(imm().into_int()?, ctrl_ty)?),
Opcode::F32const => assign(imm()),
Expand Down

0 comments on commit 22b89e0

Please sign in to comment.