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 Aug 10, 2022
1 parent a25d520 commit e1d2b0e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
13 changes: 13 additions & 0 deletions cranelift/filetests/filetests/runtests/pinned-reg.clif
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test interpret
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
12 changes: 11 additions & 1 deletion cranelift/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ pub struct InterpreterState<'a> {
pub heaps: Vec<HeapBacking>,
pub iflags: HashSet<IntCC>,
pub fflags: HashSet<FloatCC>,
pub pinned_reg: DataValue,
}

impl Default for InterpreterState<'_> {
Expand All @@ -213,6 +214,7 @@ impl Default for InterpreterState<'_> {
heaps: Vec::new(),
iflags: HashSet::new(),
fflags: HashSet::new(),
pinned_reg: DataValue::U64(0),
}
}
}
Expand Down Expand Up @@ -592,10 +594,18 @@ impl<'a> State<'a, DataValue> for InterpreterState<'a> {
}
}
_ => unimplemented!(),
}
};

Ok(())
}

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 @@ -86,6 +86,11 @@ pub trait State<'a, V> {

/// Checks if an address is valid and within a known region of memory
fn validate_address(&self, address: &Address) -> 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 @@ -187,4 +192,12 @@ where
fn validate_address(&self, _addr: &Address) -> 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 @@ -407,8 +407,11 @@ where
unreachable!()
}
}
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 => {
if let InstructionData::TableAddr { table, offset, .. } = inst {
let table = &state.get_current_function().tables[table];
Expand Down

0 comments on commit e1d2b0e

Please sign in to comment.