Allocate and free stack frames

This commit is contained in:
Brian Carroll 2021-09-17 21:17:05 +01:00
parent 036503c750
commit 4f55b7a56e
3 changed files with 43 additions and 21 deletions

View file

@ -25,7 +25,7 @@ pub const ALIGN_8: u32 = 3;
pub const STACK_POINTER_GLOBAL_ID: u32 = 0;
#[derive(Clone, Copy, Debug)]
struct LocalId(u32);
pub struct LocalId(u32);
pub struct Env<'a> {
pub arena: &'a Bump, // not really using this much, parity_wasm works with std::vec a lot
@ -152,3 +152,23 @@ fn copy_memory(
}
Ok(())
}
pub fn allocate_stack_frame(instructions: &mut Vec<Instruction>, size: i32) {
if size == 0 {
return;
}
instructions.push(GetGlobal(STACK_POINTER_GLOBAL_ID));
instructions.push(I32Const(size));
instructions.push(I32Sub);
instructions.push(SetGlobal(STACK_POINTER_GLOBAL_ID));
}
pub fn free_stack_frame(instructions: &mut Vec<Instruction>, size: i32) {
if size == 0 {
return;
}
instructions.push(GetGlobal(STACK_POINTER_GLOBAL_ID));
instructions.push(I32Const(size));
instructions.push(I32Add);
instructions.push(SetGlobal(STACK_POINTER_GLOBAL_ID));
}