Skip to content

Commit e1d2b0e

Browse files
committed
cranelift: Implement pinned reg in interpreter
1 parent a25d520 commit e1d2b0e

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
test interpret
2+
set enable_pinned_reg
3+
target x86_64
4+
5+
function %read_write(i64) -> i64 {
6+
block0(v0: i64):
7+
set_pinned_reg v0
8+
v1 = get_pinned_reg.i64
9+
return v1
10+
}
11+
; run: %read_write(0) == 0
12+
; run: %read_write(-1) == -1
13+
; run: %read_write(0xDEADBEEF_C0FFEEEE) == 0xDEADBEEF_C0FFEEEE

cranelift/interpreter/src/interpreter.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ pub struct InterpreterState<'a> {
201201
pub heaps: Vec<HeapBacking>,
202202
pub iflags: HashSet<IntCC>,
203203
pub fflags: HashSet<FloatCC>,
204+
pub pinned_reg: DataValue,
204205
}
205206

206207
impl Default for InterpreterState<'_> {
@@ -213,6 +214,7 @@ impl Default for InterpreterState<'_> {
213214
heaps: Vec::new(),
214215
iflags: HashSet::new(),
215216
fflags: HashSet::new(),
217+
pinned_reg: DataValue::U64(0),
216218
}
217219
}
218220
}
@@ -592,10 +594,18 @@ impl<'a> State<'a, DataValue> for InterpreterState<'a> {
592594
}
593595
}
594596
_ => unimplemented!(),
595-
}
597+
};
596598

597599
Ok(())
598600
}
601+
602+
fn get_pinned_reg(&self) -> DataValue {
603+
self.pinned_reg.clone()
604+
}
605+
606+
fn set_pinned_reg(&mut self, v: DataValue) {
607+
self.pinned_reg = v;
608+
}
599609
}
600610

601611
#[cfg(test)]

cranelift/interpreter/src/state.rs

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ pub trait State<'a, V> {
8686

8787
/// Checks if an address is valid and within a known region of memory
8888
fn validate_address(&self, address: &Address) -> Result<(), MemoryError>;
89+
90+
/// Retrieves the current pinned reg value
91+
fn get_pinned_reg(&self) -> V;
92+
/// Sets a value for the pinned reg
93+
fn set_pinned_reg(&mut self, v: V);
8994
}
9095

9196
#[derive(Error, Debug)]
@@ -187,4 +192,12 @@ where
187192
fn validate_address(&self, _addr: &Address) -> Result<(), MemoryError> {
188193
unimplemented!()
189194
}
195+
196+
fn get_pinned_reg(&self) -> V {
197+
unimplemented!()
198+
}
199+
200+
fn set_pinned_reg(&mut self, _v: V) {
201+
unimplemented!()
202+
}
190203
}

cranelift/interpreter/src/step.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,11 @@ where
407407
unreachable!()
408408
}
409409
}
410-
Opcode::GetPinnedReg => unimplemented!("GetPinnedReg"),
411-
Opcode::SetPinnedReg => unimplemented!("SetPinnedReg"),
410+
Opcode::GetPinnedReg => assign(state.get_pinned_reg()),
411+
Opcode::SetPinnedReg => {
412+
state.set_pinned_reg(arg(0)?);
413+
ControlFlow::Continue
414+
}
412415
Opcode::TableAddr => {
413416
if let InstructionData::TableAddr { table, offset, .. } = inst {
414417
let table = &state.get_current_function().tables[table];

0 commit comments

Comments
 (0)