Skip to content

Commit

Permalink
feat: translate Wasm memory.grow op
Browse files Browse the repository at this point in the history
Add `InstBuilder::mem_grow`, make `OpCode::MemGrow` to return 1 result in `OpCode::results`.
  • Loading branch information
greenhat committed Sep 25, 2023
1 parent f333511 commit 9988fed
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
5 changes: 3 additions & 2 deletions frontend-wasm/src/code_translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ pub fn translate_operator(
}
/******************************* Memory management *********************************/
Operator::MemoryGrow { .. } => {
// Do nothing and return total Miden memory size
state.push1(builder.ins().i32(mem_total_pages(), span));
let arg = state.pop1();
state.push1(builder.ins().mem_grow(arg, span));
}
Operator::MemorySize { .. } => {
// Return total Miden memory size
state.push1(builder.ins().i32(mem_total_pages(), span));
}
/******************************* Load instructions ***********************************/
Expand Down
2 changes: 1 addition & 1 deletion frontend-wasm/src/code_translator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ fn memory_grow() {
"#,
expect![[r#"
v0 = const.i32 1 : i32
v1 = const.i32 1048575 : i32
v1 = memory.grow v0 : i32
"#]],
)
}
Expand Down
2 changes: 1 addition & 1 deletion frontend-wasm/tests/expected/dlmalloc.mir
Original file line number Diff line number Diff line change
Expand Up @@ -5362,7 +5362,7 @@ block0(v0: i32, v1: i32, v2: i32):
v6 = cast v4 : u32
v7 = shr v5, v6 : u32
v8 = cast v7 : i32
v9 = const.i32 1048575 : i32
v9 = memory.grow v8 : i32
v10 = const.i32 0 : i32
v11 = const.i32 8 : i32
v12 = add v0, v11 : i32
Expand Down
10 changes: 10 additions & 0 deletions hir/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,16 @@ pub trait InstBuilder<'f>: InstBuilderBase<'f> {
))
}

fn mem_grow(mut self, value: Value, span: SourceSpan) -> Value {
require_integer!(self, value, Type::I32);
let mut vlist = ValueList::default();
{
let pool = &mut self.data_flow_graph_mut().value_lists;
vlist.push(value, pool);
}
into_first_result!(self.PrimOp(Opcode::MemGrow, Type::I32, vlist, span,))
}

/// Get a [GlobalValue] which represents the address of a global variable whose symbol is `name`
///
/// On it's own, this does nothing, you must use the resulting [GlobalValue] with a builder
Expand Down
4 changes: 2 additions & 2 deletions hir/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,6 @@ impl Opcode {
| Self::Assertz
| Self::AssertEq
| Self::Store
| Self::MemGrow
| Self::MemCpy
| Self::Br
| Self::CondBr
Expand Down Expand Up @@ -611,7 +610,8 @@ impl Opcode {
| Self::Shl
| Self::Shr
| Self::Rotl
| Self::Rotr => {
| Self::Rotr
| Self::MemGrow => {
smallvec![ctrl_ty]
}
// The result type of a load is derived from the pointee type
Expand Down

0 comments on commit 9988fed

Please sign in to comment.