Skip to content

Commit

Permalink
refactor: Optimize local stack initialization assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
brew0722 committed Jan 13, 2021
1 parent dd69438 commit 6d5f169
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/compiler-singlepass/src/emitter_x64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ pub trait Emitter {
fn emit_xchg(&mut self, sz: Size, src: Location, dst: Location);
fn emit_lock_xadd(&mut self, sz: Size, src: Location, dst: Location);
fn emit_lock_cmpxchg(&mut self, sz: Size, src: Location, dst: Location);
fn emit_rep_stosq(&mut self);

fn emit_btc_gpr_imm8_32(&mut self, src: u8, dst: GPR);
fn emit_btc_gpr_imm8_64(&mut self, src: u8, dst: GPR);
Expand Down Expand Up @@ -1176,6 +1177,9 @@ impl Emitter for Assembler {
}
}

fn emit_rep_stosq(&mut self) {
dynasm!(self ; rep stosq);
}
fn emit_btc_gpr_imm8_32(&mut self, src: u8, dst: GPR) {
dynasm!(self ; btc Rd(dst as u8), BYTE src as i8);
}
Expand Down
29 changes: 24 additions & 5 deletions lib/compiler-singlepass/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use smallvec::smallvec;
use smallvec::SmallVec;
use std::collections::HashSet;
use wasmer_compiler::wasmparser::Type as WpType;
use std::cmp;

struct MachineStackOffset(usize);

Expand Down Expand Up @@ -447,17 +448,35 @@ impl Machine {
}
}

// Initialize all normal locals to zero.
for i in n_params..n {
a.emit_mov(Size::S64, Location::Imm32(0), locations[i]);
}

// Load vmctx into R15.
a.emit_mov(
Size::S64,
Self::get_param_location(0),
Location::GPR(GPR::R15),
);

//Initialize all normal locals to zero.
let mut init_stack_loc_cnt = 0;
let mut last_stack_loc = Location::Memory(GPR::RBP, i32::MAX);
for i in n_params..n {
match locations[i] {
Location::Memory(_, _) => {
init_stack_loc_cnt+=1;
last_stack_loc = cmp::min(last_stack_loc, locations[i]);
},
Location::GPR(_) => {
a.emit_mov(Size::S64, Location::Imm32(0), locations[i]);
},
_ => unreachable!(),
}
}
if init_stack_loc_cnt > 0 {
// Since this assemblies takes up 24 bytes, If initialize more than 2 slots, These assemblies are smallar.
a.emit_mov(Size::S64, Location::Imm64(init_stack_loc_cnt as u64), Location::GPR(GPR::RCX));
a.emit_xor(Size::S64, Location::GPR(GPR::RAX), Location::GPR(GPR::RAX));
a.emit_lea(Size::S64, last_stack_loc, Location::GPR(GPR::RDI));
a.emit_rep_stosq();
}

// Add the size of all locals allocated to stack.
self.stack_offset.0 += static_area_size - callee_saved_regs_size;
Expand Down

0 comments on commit 6d5f169

Please sign in to comment.