Skip to content

abstract sub_index #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 149 additions & 8 deletions third_party/move/move-vm/runtime/src/witnessing/traced_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub enum Integer {
impl From<IntegerValue> for Integer {
fn from(value: IntegerValue) -> Self {
match value {
IntegerValue::U8(v) => { Self::U8(v) }
IntegerValue::U16(v) => { Self::U16(v) }
IntegerValue::U8(v) => Self::U8(v),
IntegerValue::U16(v) => Self::U16(v),
IntegerValue::U32(v) => Self::U32(v),
IntegerValue::U64(v) => Self::U64(v),
IntegerValue::U128(v) => Self::U128(v),
Expand Down Expand Up @@ -106,15 +106,156 @@ impl From<Integer> for (u128 /*lo*/, u128 /*hi*/) {
}
}

const MAX_SUB_INDEX_DEPTH: usize = 8;

#[derive(Clone, Debug, Ord, PartialOrd, PartialEq, Eq, Serialize, Deserialize)]
pub struct SubIndex([u16; MAX_SUB_INDEX_DEPTH]);

impl SubIndex {
pub fn depth(&self) -> usize {
self.0
.iter()
.rposition(|&x| x != 0)
.map_or(0, |pos| pos + 1)
}

/// A depth-n sub_index must have n parents. Return all parents in a vector, in a order
/// starting with direct relatives. For example,
/// [1,2,3,0]'s parents is [[1,2,0,0],[1,0,0,0],[0,0,0,0]]
pub fn parents(&self) -> Vec<Self> {
let depth = self.depth();
let mut parent = self.0;
let mut parents = Vec::with_capacity(depth);

for i in (0..depth).rev() {
parent[i] = 0;
parents.push(SubIndex(parent));
}

parents
}

/// Trim tailing zeros of sub_index and concat with other sub_index. For example,
/// let sub_index = [3,2,0,0,0,0,0,0];
/// let other = [4,1,0,0,0,0,0,0];
/// sub_index.concat(other) = [3,2,4,1,0,0,0,0];
pub fn concat(&self, other: &SubIndex) -> Self {
let mut this = self.0.to_vec();
let other = other.0.to_vec();

// Remove trailing zeros
while this.last() == Some(&0) {
this.pop();
}

this.extend(other);

let mut result = [0; MAX_SUB_INDEX_DEPTH];
for (i, &val) in this.iter().enumerate().take(MAX_SUB_INDEX_DEPTH) {
result[i] = val;
}

SubIndex(result)
}

pub fn push(&mut self, element: u16) {
// Find the first zero element to replace
if let Some(position) = self.0.iter().position(|&x| x == 0) {
self.0[position] = element;
} else {
panic!("SubIndex is full");
}
}

pub fn insert(&mut self, index: usize, element: u16) {
assert!(index < MAX_SUB_INDEX_DEPTH, "Index out of bounds");

// Shift elements to the right, starting from the last element to the index
for i in (index..MAX_SUB_INDEX_DEPTH - 1).rev() {
self.0[i + 1] = self.0[i];
}

self.0[index] = element;
}

pub fn remove(&mut self, index: usize) -> u16 {
assert!(index < MAX_SUB_INDEX_DEPTH, "Index out of bounds");
let removed_element = self.0[index];

// Shift elements to the left
for i in index..MAX_SUB_INDEX_DEPTH - 1 {
self.0[i] = self.0[i + 1];
}

self.0[MAX_SUB_INDEX_DEPTH - 1] = 0;
removed_element
}

pub fn to_vec(&self) -> Vec<u16> {
self.0.to_vec()
}

pub fn to_trimmed_vec(&self) -> Vec<u16> {
let mut vec = self.0.to_vec();

// Remove trailing zeros but keep a single zero if it's the only element
while vec.len() > 1 && vec.last() == Some(&0) {
vec.pop();
}

vec
}
}

impl From<Vec<usize>> for SubIndex {
fn from(value: Vec<usize>) -> Self {
assert!(
value.len() <= MAX_SUB_INDEX_DEPTH,
"Input vector length exceeds MAX_SUB_INDEX_DEPTH"
);
let mut result = [0; MAX_SUB_INDEX_DEPTH];

for (i, &val) in value.iter().enumerate() {
assert!(val <= u16::MAX as usize, "Value {} exceeds u16::MAX", val);
result[i] = val as u16;
}

SubIndex(result)
}
}

/// Convert SubIndex into u128 in little endian order
impl From<SubIndex> for u128 {
fn from(sub_index: SubIndex) -> u128 {
let mut result = 0u128;
for (i, &value) in sub_index.0.iter().enumerate() {
result |= (value as u128) << (i * 16);
}
result
}
}

impl From<u128> for SubIndex {
fn from(value: u128) -> Self {
let mut result = [0u16; MAX_SUB_INDEX_DEPTH];

for i in 0..MAX_SUB_INDEX_DEPTH {
result[i] = ((value >> (i * 16)) & 0xFFFF) as u16;
}

SubIndex(result)
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Reference {
pub frame_index: usize,
pub local_index: usize,
pub sub_index: Vec<usize>,
pub sub_index: SubIndex,
}

impl Reference {
pub fn new(frame_index: usize, local_index: usize, sub_index: Vec<usize>) -> Self {
pub fn new(frame_index: usize, local_index: usize, sub_index: SubIndex) -> Self {
Reference {
frame_index,
local_index,
Expand All @@ -125,7 +266,7 @@ impl Reference {

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ValueItem {
pub sub_index: Vec<usize>,
pub sub_index: SubIndex,
pub header: bool,
pub value: SimpleValue,
}
Expand Down Expand Up @@ -186,7 +327,7 @@ impl TracedValue {
},
};
self.items.push(ValueItem {
sub_index,
sub_index: sub_index.into(),
header: false,
value,
});
Expand Down Expand Up @@ -273,7 +414,7 @@ impl ValueVisitor for TracedValue {
self.visit_stack.push(new_frame);
self.items.push(ValueItem {
header: true,
sub_index: self.current_sub_index(),
sub_index: self.current_sub_index().into(),
value: SimpleValue::U64(len as u64),
});
true
Expand All @@ -288,7 +429,7 @@ impl ValueVisitor for TracedValue {
self.visit_stack.push(new_frame);
self.items.push(ValueItem {
header: true,
sub_index: self.current_sub_index(),
sub_index: self.current_sub_index().into(),
value: SimpleValue::U64(len as u64),
});
true
Expand Down
Loading