rename CodeBuilder methods

This commit is contained in:
Brian Carroll 2021-10-10 16:21:53 +01:00
parent c5ee41af25
commit 041e26e807
3 changed files with 44 additions and 38 deletions

View file

@ -315,7 +315,7 @@ impl<'a> WasmBackend<'a> {
location: StackMemoryLocation::PointerArg(local_id), location: StackMemoryLocation::PointerArg(local_id),
.. ..
} => { } => {
self.code_builder.push(GetLocal(local_id.0)); self.code_builder.add_one(GetLocal(local_id.0));
self.code_builder.set_top_symbol(*sym); self.code_builder.set_top_symbol(*sym);
} }
@ -323,7 +323,7 @@ impl<'a> WasmBackend<'a> {
location: StackMemoryLocation::FrameOffset(offset), location: StackMemoryLocation::FrameOffset(offset),
.. ..
} => { } => {
self.code_builder.extend(&[ self.code_builder.add_many(&[
GetLocal(self.stack_frame_pointer.unwrap().0), GetLocal(self.stack_frame_pointer.unwrap().0),
I32Const(offset as i32), I32Const(offset as i32),
I32Add, I32Add,
@ -379,9 +379,9 @@ impl<'a> WasmBackend<'a> {
panic!("Cannot store {:?} with alignment of {:?}", value_type, size); panic!("Cannot store {:?} with alignment of {:?}", value_type, size);
} }
}; };
self.code_builder.push(GetLocal(to_ptr.0)); self.code_builder.add_one(GetLocal(to_ptr.0));
self.load_symbols(&[from_symbol]); self.load_symbols(&[from_symbol]);
self.code_builder.push(store_instruction); self.code_builder.add_one(store_instruction);
size size
} }
} }
@ -412,7 +412,7 @@ impl<'a> WasmBackend<'a> {
debug_assert!(to_value_type == from_value_type); debug_assert!(to_value_type == from_value_type);
debug_assert!(to_size == from_size); debug_assert!(to_size == from_size);
self.load_symbols(&[from_symbol]); self.load_symbols(&[from_symbol]);
self.code_builder.push(SetLocal(to_local_id.0)); self.code_builder.add_one(SetLocal(to_local_id.0));
self.symbol_storage_map.insert(from_symbol, to.clone()); self.symbol_storage_map.insert(from_symbol, to.clone());
} }
@ -430,8 +430,8 @@ impl<'a> WasmBackend<'a> {
) => { ) => {
debug_assert!(to_value_type == from_value_type); debug_assert!(to_value_type == from_value_type);
debug_assert!(to_size == from_size); debug_assert!(to_size == from_size);
self.code_builder.push(GetLocal(from_local_id.0)); self.code_builder
self.code_builder.push(SetLocal(to_local_id.0)); .add_many(&[GetLocal(from_local_id.0), SetLocal(to_local_id.0)]);
} }
( (
@ -486,7 +486,7 @@ impl<'a> WasmBackend<'a> {
let local_id = self.get_next_local_id(); let local_id = self.get_next_local_id();
if vm_state != VirtualMachineSymbolState::NotYetPushed { if vm_state != VirtualMachineSymbolState::NotYetPushed {
self.code_builder.load_symbol(symbol, vm_state, local_id); self.code_builder.load_symbol(symbol, vm_state, local_id);
self.code_builder.push(SetLocal(local_id.0)); self.code_builder.add_one(SetLocal(local_id.0));
} }
self.locals.push(Local::new(1, value_type)); self.locals.push(Local::new(1, value_type));
@ -512,17 +512,18 @@ impl<'a> WasmBackend<'a> {
/// start a loop that leaves a value on the stack /// start a loop that leaves a value on the stack
fn start_loop_with_return(&mut self, value_type: ValueType) { fn start_loop_with_return(&mut self, value_type: ValueType) {
self.block_depth += 1; self.block_depth += 1;
self.code_builder.push(Loop(BlockType::Value(value_type))); self.code_builder
.add_one(Loop(BlockType::Value(value_type)));
} }
fn start_block(&mut self, block_type: BlockType) { fn start_block(&mut self, block_type: BlockType) {
self.block_depth += 1; self.block_depth += 1;
self.code_builder.push(Block(block_type)); self.code_builder.add_one(Block(block_type));
} }
fn end_block(&mut self) { fn end_block(&mut self) {
self.block_depth -= 1; self.block_depth -= 1;
self.code_builder.push(End); self.code_builder.add_one(End);
} }
fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) -> Result<(), String> { fn build_stmt(&mut self, stmt: &Stmt<'a>, ret_layout: &Layout<'a>) -> Result<(), String> {
@ -560,7 +561,7 @@ impl<'a> WasmBackend<'a> {
); );
} }
self.code_builder.push(Br(self.block_depth)); // jump to end of function (stack frame pop) self.code_builder.add_one(Br(self.block_depth)); // jump to end of function (stack frame pop)
Ok(()) Ok(())
} }
@ -614,7 +615,7 @@ impl<'a> WasmBackend<'a> {
_ => { _ => {
self.load_symbols(&[*sym]); self.load_symbols(&[*sym]);
self.code_builder.push(Br(self.block_depth)); // jump to end of function (for stack frame pop) self.code_builder.add_one(Br(self.block_depth)); // jump to end of function (for stack frame pop)
} }
} }
@ -647,13 +648,13 @@ impl<'a> WasmBackend<'a> {
// put the cond_symbol on the top of the stack // put the cond_symbol on the top of the stack
self.load_symbols(&[*cond_symbol]); self.load_symbols(&[*cond_symbol]);
self.code_builder.push(I32Const(*value as i32)); self.code_builder.add_one(I32Const(*value as i32));
// compare the 2 topmost values // compare the 2 topmost values
self.code_builder.push(I32Eq); self.code_builder.add_one(I32Eq);
// "break" out of `i` surrounding blocks // "break" out of `i` surrounding blocks
self.code_builder.push(BrIf(i as u32)); self.code_builder.add_one(BrIf(i as u32));
} }
// if we never jumped because a value matched, we're in the default case // if we never jumped because a value matched, we're in the default case
@ -718,7 +719,7 @@ impl<'a> WasmBackend<'a> {
// jump // jump
let levels = self.block_depth - target; let levels = self.block_depth - target;
self.code_builder.push(Br(levels)); self.code_builder.add_one(Br(levels));
Ok(()) Ok(())
} }
@ -756,7 +757,7 @@ impl<'a> WasmBackend<'a> {
let wasm_layout = WasmLayout::new(layout); let wasm_layout = WasmLayout::new(layout);
let push = wasm_layout.stack_memory() == 0; let push = wasm_layout.stack_memory() == 0;
let pops = arguments.len(); let pops = arguments.len();
self.code_builder.call(function_location.body, pops, push); self.code_builder.add_call(function_location.body, pops, push);
Ok(()) Ok(())
} }
@ -800,7 +801,7 @@ impl<'a> WasmBackend<'a> {
return Err(format!("loading literal, {:?}, is not yet implemented", x)); return Err(format!("loading literal, {:?}, is not yet implemented", x));
} }
}; };
self.code_builder.push(instruction); self.code_builder.add_one(instruction);
Ok(()) Ok(())
} }
@ -893,7 +894,7 @@ impl<'a> WasmBackend<'a> {
return Err(format!("unsupported low-level op {:?}", lowlevel)); return Err(format!("unsupported low-level op {:?}", lowlevel));
} }
}; };
self.code_builder.extend(instructions); self.code_builder.add_many(instructions);
Ok(()) Ok(())
} }
} }

View file

@ -51,7 +51,7 @@ impl CodeBuilder {
self.vm_stack.clear(); self.vm_stack.clear();
} }
pub fn push(&mut self, inst: Instruction) { pub fn add_one(&mut self, inst: Instruction) {
let (pops, push) = get_pops_and_pushes(&inst); let (pops, push) = get_pops_and_pushes(&inst);
let new_len = self.vm_stack.len() - pops as usize; let new_len = self.vm_stack.len() - pops as usize;
self.vm_stack.truncate(new_len); self.vm_stack.truncate(new_len);
@ -64,7 +64,7 @@ impl CodeBuilder {
self.code.push(inst); self.code.push(inst);
} }
pub fn extend(&mut self, instructions: &[Instruction]) { pub fn add_many(&mut self, instructions: &[Instruction]) {
let old_len = self.vm_stack.len(); let old_len = self.vm_stack.len();
let mut len = old_len; let mut len = old_len;
let mut min_len = len; let mut min_len = len;
@ -86,7 +86,7 @@ impl CodeBuilder {
self.code.extend_from_slice(instructions); self.code.extend_from_slice(instructions);
} }
pub fn call(&mut self, function_index: u32, pops: usize, push: bool) { pub fn add_call(&mut self, function_index: u32, pops: usize, push: bool) {
let stack_depth = self.vm_stack.len(); let stack_depth = self.vm_stack.len();
if pops > stack_depth { if pops > stack_depth {
let mut final_code = Vec::with_capacity(self.code.len() + self.insertions.len()); let mut final_code = Vec::with_capacity(self.code.len() + self.insertions.len());
@ -96,8 +96,7 @@ impl CodeBuilder {
function_index, pops, stack_depth, final_code, self.vm_stack function_index, pops, stack_depth, final_code, self.vm_stack
); );
} }
let new_stack_depth = stack_depth - pops as usize; self.vm_stack.truncate(stack_depth - pops);
self.vm_stack.truncate(new_stack_depth);
if push { if push {
self.vm_stack.push(None); self.vm_stack.push(None);
} }
@ -264,7 +263,7 @@ fn get_pops_and_pushes(inst: &Instruction) -> (u8, bool) {
Return => (0, false), Return => (0, false),
Call(_) | CallIndirect(_, _) => { Call(_) | CallIndirect(_, _) => {
panic!("Unknown number of pushes and pops. Use Codebuilder::call() instead."); panic!("Unknown number of pushes and pops. Use add_call()");
} }
Drop => (1, false), Drop => (1, false),

View file

@ -137,24 +137,30 @@ pub fn copy_memory(code_builder: &mut CodeBuilder, config: CopyMemoryConfig) {
let alignment_flag = encode_alignment(config.alignment_bytes); let alignment_flag = encode_alignment(config.alignment_bytes);
let mut i = 0; let mut i = 0;
while config.size - i >= 8 { while config.size - i >= 8 {
code_builder.push(GetLocal(config.to_ptr.0)); code_builder.add_many(&[
code_builder.push(GetLocal(config.from_ptr.0)); GetLocal(config.to_ptr.0),
code_builder.push(I64Load(alignment_flag, i + config.from_offset)); GetLocal(config.from_ptr.0),
code_builder.push(I64Store(alignment_flag, i + config.to_offset)); I64Load(alignment_flag, i + config.from_offset),
I64Store(alignment_flag, i + config.to_offset),
]);
i += 8; i += 8;
} }
if config.size - i >= 4 { if config.size - i >= 4 {
code_builder.push(GetLocal(config.to_ptr.0)); code_builder.add_many(&[
code_builder.push(GetLocal(config.from_ptr.0)); GetLocal(config.to_ptr.0),
code_builder.push(I32Load(alignment_flag, i + config.from_offset)); GetLocal(config.from_ptr.0),
code_builder.push(I32Store(alignment_flag, i + config.to_offset)); I32Load(alignment_flag, i + config.from_offset),
I32Store(alignment_flag, i + config.to_offset),
]);
i += 4; i += 4;
} }
while config.size - i > 0 { while config.size - i > 0 {
code_builder.push(GetLocal(config.to_ptr.0)); code_builder.add_many(&[
code_builder.push(GetLocal(config.from_ptr.0)); GetLocal(config.to_ptr.0),
code_builder.push(I32Load8U(alignment_flag, i + config.from_offset)); GetLocal(config.from_ptr.0),
code_builder.push(I32Store8(alignment_flag, i + config.to_offset)); I32Load8U(alignment_flag, i + config.from_offset),
I32Store8(alignment_flag, i + config.to_offset),
]);
i += 1; i += 1;
} }
} }