Skip to content

Commit

Permalink
[ir-compiler][ir-testsuite] support VecPack and VecUnpack and por…
Browse files Browse the repository at this point in the history
…t the tests
  • Loading branch information
meng-xu-cs authored and bors-libra committed Aug 17, 2021
1 parent ae6d145 commit fef1a4f
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module M {
struct R has store { b: bool }
f() {
let v: vector<Self.R>;
v = vec_empty<Self.R>();
v = vec_pack_0<Self.R>();
return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ main() {
let e_imm1: &u64;
let e_mut2: &mut u64;

v = vec_empty<u64>();
v = vec_pack_0<u64>();
vec_push_back<u64>(&mut v, 0);
vec_push_back<u64>(&mut v, 1);

Expand All @@ -20,7 +20,7 @@ main() {
let e_mut1: &mut u64;
let e_imm2: &u64;

v = vec_empty<u64>();
v = vec_pack_0<u64>();
vec_push_back<u64>(&mut v, 0);
vec_push_back<u64>(&mut v, 1);

Expand All @@ -36,7 +36,7 @@ main() {
let e_mut1: &mut u64;
let e_mut2: &mut u64;

v = vec_empty<u64>();
v = vec_pack_0<u64>();
vec_push_back<u64>(&mut v, 0);
vec_push_back<u64>(&mut v, 1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ main() {
let v: vector<u64>;
let e_imm: &u64;

v = vec_empty<u64>();
v = vec_pack_0<u64>();
vec_push_back<u64>(&mut v, 0);
e_imm = vec_imm_borrow<u64>(&v, 0);

Expand All @@ -16,7 +16,7 @@ main() {
let v: vector<u64>;
let e_mut: &mut u64;

v = vec_empty<u64>();
v = vec_pack_0<u64>();
vec_push_back<u64>(&mut v, 0);
e_mut = vec_mut_borrow<u64>(&mut v, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ main() {
let v: vector<u64>;
let e_imm: &u64;

v = vec_empty<u64>();
v = vec_pack_0<u64>();
vec_push_back<u64>(&mut v, 0);
e_imm = vec_imm_borrow<u64>(&v, 0);

Expand All @@ -16,7 +16,7 @@ main() {
let v: vector<u64>;
let e_mut: &mut u64;

v = vec_empty<u64>();
v = vec_pack_0<u64>();
vec_push_back<u64>(&mut v, 0);
e_mut = vec_mut_borrow<u64>(&mut v, 0);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
//# run
main() {
_ = vec_empty<u64, bool>();
_ = vec_pack_0<u64, bool>();
return;
}

//# run
main() {
_ = vec_empty<>();
_ = vec_pack_0<>();
return;
}

//# run
main() {
_ = vec_empty<&u64>();
_ = vec_pack_0<&u64>();
return;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//# run
main() {
let v: vector<u8>;
v = vec_empty<bool>();
v = vec_pack_0<bool>();
return;
}

Expand All @@ -10,7 +10,7 @@ main() {
let v: vector<u8>;
let v_mut: &mut vector<bool>;

v = vec_empty<u8>();
v = vec_pack_0<u8>();
v_mut = &mut v;

return;
Expand All @@ -22,7 +22,7 @@ main() {
let v_imm: &vector<u8>;
let v_len: u8;

v = vec_empty<u8>();
v = vec_pack_0<u8>();
v_imm = &v;
v_len = vec_len<u8>(move(v_imm));

Expand All @@ -36,7 +36,7 @@ main() {
let e: u64;

e = 0;
v = vec_empty<u8>();
v = vec_pack_0<u8>();
v_mut = &mut v;
vec_push_back<u8>(move(v_mut), move(e));

Expand All @@ -49,7 +49,7 @@ main() {
let v_mut: &mut vector<u8>;
let e: u64;

v = vec_empty<u8>();
v = vec_pack_0<u8>();
v_mut = &mut v;
vec_push_back<u8>(copy(v_mut), 0);

Expand All @@ -66,7 +66,7 @@ main() {

i1 = 0;
i2 = 1;
v = vec_empty<u8>();
v = vec_pack_0<u8>();
v_mut = &mut v;
vec_swap<u8>(move(v_mut), move(i1), move(i2));

Expand Down
18 changes: 12 additions & 6 deletions language/compiler/ir-to-bytecode/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ impl FunctionFrame {

// Manage the stack info for the function
fn push(&mut self) -> Result<()> {
if self.cur_stack_depth == i64::max_value() {
if self.cur_stack_depth == i64::MAX {
bail!("ICE Stack depth accounting overflow. The compiler can only support a maximum stack depth of up to i64::max_value")
}
self.cur_stack_depth += 1;
Expand All @@ -321,7 +321,7 @@ impl FunctionFrame {
}

fn pop(&mut self) -> Result<()> {
if self.cur_stack_depth == i64::min_value() {
if self.cur_stack_depth == i64::MIN {
bail!("ICE Stack depth accounting underflow. The compiler can only support a minimum stack depth of up to i64::min_value")
}
self.cur_stack_depth -= 1;
Expand Down Expand Up @@ -1689,11 +1689,14 @@ fn compile_call(
function_frame.pop()?; // pop the value to be moved
vec_deque![]
}
Builtin::VecEmpty(tys) => {
Builtin::VecPack(tys, num) => {
let tokens = compile_types(context, function_frame.type_parameters(), &tys)?;
let type_actuals_id = context.signature_index(Signature(tokens))?;
push_instr!(call.loc, Bytecode::VecPack(type_actuals_id, 0));
push_instr!(call.loc, Bytecode::VecPack(type_actuals_id, num));

for _ in 0..num {
function_frame.pop()?;
}
function_frame.push()?; // push the return value

// NOTE: we do actually infer the type here because we want to allow the type
Expand Down Expand Up @@ -1751,12 +1754,15 @@ fn compile_call(
function_frame.push()?; // push the value
vec_deque![InferredType::Anything]
}
Builtin::VecDestroyEmpty(tys) => {
Builtin::VecUnpack(tys, num) => {
let tokens = compile_types(context, function_frame.type_parameters(), &tys)?;
let type_actuals_id = context.signature_index(Signature(tokens))?;
push_instr!(call.loc, Bytecode::VecUnpack(type_actuals_id, 0));
push_instr!(call.loc, Bytecode::VecUnpack(type_actuals_id, num));

function_frame.pop()?; // pop the vector ref
for _ in 0..num {
function_frame.push()?;
}
vec_deque![]
}
Builtin::VecSwap(tys) => {
Expand Down
22 changes: 17 additions & 5 deletions language/compiler/ir-to-bytecode/syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ pub enum Tok {
U64,
U128,
Vector,
VecEmpty,
VecPack(u64),
VecLen,
VecImmBorrow,
VecMutBorrow,
VecPushBack,
VecPopBack,
VecDestroyEmpty,
VecUnpack(u64),
VecSwap,
While,
LBrace,
Expand Down Expand Up @@ -269,21 +269,33 @@ impl<'input> Lexer<'input> {
}
Some('<') => match name {
"vector" => (Tok::Vector, len),
"vec_empty" => (Tok::VecEmpty, len),
"vec_len" => (Tok::VecLen, len),
"vec_imm_borrow" => (Tok::VecImmBorrow, len),
"vec_mut_borrow" => (Tok::VecMutBorrow, len),
"vec_push_back" => (Tok::VecPushBack, len),
"vec_pop_back" => (Tok::VecPopBack, len),
"vec_destroy_empty" => (Tok::VecDestroyEmpty, len),
"vec_swap" => (Tok::VecSwap, len),
"borrow_global" => (Tok::BorrowGlobal, len + 1),
"borrow_global_mut" => (Tok::BorrowGlobalMut, len + 1),
"exists" => (Tok::Exists, len + 1),
"move_from" => (Tok::MoveFrom, len + 1),
"move_to" => (Tok::MoveTo, len + 1),
"main" => (Tok::Main, len),
_ => (Tok::NameBeginTyValue, len + 1),
_ => {
if let Some(stripped) = name.strip_prefix("vec_pack_") {
match stripped.parse::<u64>() {
Ok(num) => (Tok::VecPack(num), len),
Err(_) => (Tok::NameBeginTyValue, len + 1),
}
} else if let Some(stripped) = name.strip_prefix("vec_unpack_") {
match stripped.parse::<u64>() {
Ok(num) => (Tok::VecUnpack(num), len),
Err(_) => (Tok::NameBeginTyValue, len + 1),
}
} else {
(Tok::NameBeginTyValue, len + 1)
}
}
},
Some('(') => match name {
"assert" => (Tok::Assert, len + 1),
Expand Down
20 changes: 10 additions & 10 deletions language/compiler/ir-to-bytecode/syntax/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,13 @@ fn parse_qualified_function_name(
| Tok::BorrowGlobalMut
| Tok::MoveFrom
| Tok::MoveTo
| Tok::VecEmpty
| Tok::VecPack(_)
| Tok::VecLen
| Tok::VecImmBorrow
| Tok::VecMutBorrow
| Tok::VecPushBack
| Tok::VecPopBack
| Tok::VecDestroyEmpty
| Tok::VecUnpack(_)
| Tok::VecSwap
| Tok::Freeze
| Tok::ToU8
Expand Down Expand Up @@ -544,13 +544,13 @@ fn parse_call_or_term_(tokens: &mut Lexer) -> Result<Exp_, ParseError<Loc, anyho
| Tok::BorrowGlobalMut
| Tok::MoveFrom
| Tok::MoveTo
| Tok::VecEmpty
| Tok::VecPack(_)
| Tok::VecLen
| Tok::VecImmBorrow
| Tok::VecMutBorrow
| Tok::VecPushBack
| Tok::VecPopBack
| Tok::VecDestroyEmpty
| Tok::VecUnpack(_)
| Tok::VecSwap
| Tok::Freeze
| Tok::DotNameValue
Expand Down Expand Up @@ -742,10 +742,10 @@ fn parse_builtin(tokens: &mut Lexer) -> Result<Builtin, ParseError<Loc, anyhow::
consume_end_of_generics(tokens)?;
Ok(Builtin::MoveTo(StructName(name), type_actuals))
}
Tok::VecEmpty => {
Tok::VecPack(num) => {
tokens.advance()?;
let type_actuals = parse_type_actuals(tokens)?;
Ok(Builtin::VecEmpty(type_actuals))
Ok(Builtin::VecPack(type_actuals, num))
}
Tok::VecLen => {
tokens.advance()?;
Expand All @@ -772,10 +772,10 @@ fn parse_builtin(tokens: &mut Lexer) -> Result<Builtin, ParseError<Loc, anyhow::
let type_actuals = parse_type_actuals(tokens)?;
Ok(Builtin::VecPopBack(type_actuals))
}
Tok::VecDestroyEmpty => {
Tok::VecUnpack(num) => {
tokens.advance()?;
let type_actuals = parse_type_actuals(tokens)?;
Ok(Builtin::VecDestroyEmpty(type_actuals))
Ok(Builtin::VecUnpack(type_actuals, num))
}
Tok::VecSwap => {
tokens.advance()?;
Expand Down Expand Up @@ -954,13 +954,13 @@ fn parse_cmd_(tokens: &mut Lexer) -> Result<Cmd_, ParseError<Loc, anyhow::Error>
| Tok::BorrowGlobalMut
| Tok::MoveFrom
| Tok::MoveTo
| Tok::VecEmpty
| Tok::VecPack(_)
| Tok::VecLen
| Tok::VecImmBorrow
| Tok::VecMutBorrow
| Tok::VecPushBack
| Tok::VecPopBack
| Tok::VecDestroyEmpty
| Tok::VecUnpack(_)
| Tok::VecSwap
| Tok::Freeze
| Tok::DotNameValue
Expand Down
4 changes: 2 additions & 2 deletions language/compiler/src/unit_tests/expression_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ fn compile_builtin_vector_ops() {
let e_imm: &u64;
let e_mut: &mut u64;
v = vec_empty<u64>();
v = vec_pack_0<u64>();
v_imm = &v;
v_len = vec_len<u64>(copy(v_imm));
_ = move(v_imm);
Expand All @@ -294,7 +294,7 @@ fn compile_builtin_vector_ops() {
_ = vec_pop_back<u64>(copy(v_mut));
_ = move(v_mut);
vec_destroy_empty<u64>(move(v));
vec_unpack_0<u64>(move(v));
return;
}
}
Expand Down
14 changes: 7 additions & 7 deletions language/move-ir/types/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ pub enum Builtin {
/// Publish an instantiated struct object into signer's (signer is the first arg) account.
MoveTo(StructName, Vec<Type>),

/// Create an empty vector
VecEmpty(Vec<Type>),
/// Pack a vector fix a fixed number of elements. Zero elements means an empty vector.
VecPack(Vec<Type>, u64),
/// Get the length of a vector
VecLen(Vec<Type>),
/// Acquire an immutable reference to the element at a given index of the vector
Expand All @@ -405,8 +405,8 @@ pub enum Builtin {
VecPushBack(Vec<Type>),
/// Pop and return an element from the end of the vector
VecPopBack(Vec<Type>),
/// Destroy an empty vector
VecDestroyEmpty(Vec<Type>),
/// Destroy a vector of a fixed length. Zero length means destroying an empty vector.
VecUnpack(Vec<Type>, u64),
/// Swap the elements at twi indices in the vector
VecSwap(Vec<Type>),

Expand Down Expand Up @@ -1545,14 +1545,14 @@ impl fmt::Display for Builtin {
}
Builtin::MoveFrom(t, tys) => write!(f, "move_from<{}{}>", t, format_type_actuals(tys)),
Builtin::MoveTo(t, tys) => write!(f, "move_to<{}{}>", t, format_type_actuals(tys)),
Builtin::VecEmpty(tys) => write!(f, "vec_empty{}", format_type_actuals(tys)),
Builtin::VecPack(tys, num) => write!(f, "vec_pack_{}{}", num, format_type_actuals(tys)),
Builtin::VecLen(tys) => write!(f, "vec_len{}", format_type_actuals(tys)),
Builtin::VecImmBorrow(tys) => write!(f, "vec_imm_borrow{}", format_type_actuals(tys)),
Builtin::VecMutBorrow(tys) => write!(f, "vec_mut_borrow{}", format_type_actuals(tys)),
Builtin::VecPushBack(tys) => write!(f, "vec_push_back{}", format_type_actuals(tys)),
Builtin::VecPopBack(tys) => write!(f, "vec_pop_back{}", format_type_actuals(tys)),
Builtin::VecDestroyEmpty(tys) => {
write!(f, "vec_destroy_empty{}", format_type_actuals(tys))
Builtin::VecUnpack(tys, num) => {
write!(f, "vec_unpack_{}{}", num, format_type_actuals(tys))
}
Builtin::VecSwap(tys) => write!(f, "vec_swap{}", format_type_actuals(tys)),
Builtin::Freeze => write!(f, "freeze"),
Expand Down

0 comments on commit fef1a4f

Please sign in to comment.