Completely delete Wasm BlockType

This commit is contained in:
Brian Carroll 2021-12-21 23:13:49 +00:00
parent 1c20075d7b
commit 471e2c3143
3 changed files with 18 additions and 43 deletions

View file

@ -26,7 +26,7 @@ use crate::wasm_module::sections::{
Import, ImportDesc, ImportSection, MemorySection, TypeSection, WasmModule, Import, ImportDesc, ImportSection, MemorySection, TypeSection, WasmModule,
}; };
use crate::wasm_module::{ use crate::wasm_module::{
code_builder, BlockType, CodeBuilder, ConstExpr, Export, ExportType, Global, GlobalType, code_builder, CodeBuilder, ConstExpr, Export, ExportType, Global, GlobalType,
LinkingSubSection, LocalId, Signature, SymInfo, ValueType, LinkingSubSection, LocalId, Signature, SymInfo, ValueType,
}; };
use crate::{ use crate::{
@ -312,12 +312,12 @@ impl<'a> WasmBackend<'a> {
// The rules are confusing, and implementing them would add complexity and slow down code gen. // The rules are confusing, and implementing them would add complexity and slow down code gen.
// Instead we use local variables to move a value from an inner block to an outer one. // Instead we use local variables to move a value from an inner block to an outer one.
self.block_depth += 1; self.block_depth += 1;
self.code_builder.block(BlockType::NoResult); self.code_builder.block();
} }
fn start_loop(&mut self) { fn start_loop(&mut self) {
self.block_depth += 1; self.block_depth += 1;
self.code_builder.loop_(BlockType::NoResult); self.code_builder.loop_();
} }
fn end_block(&mut self) { fn end_block(&mut self) {
@ -893,7 +893,7 @@ impl<'a> WasmBackend<'a> {
// null check // null check
self.code_builder.i32_eqz(); self.code_builder.i32_eqz();
self.code_builder.if_(BlockType::NoResult); self.code_builder.if_();
self.code_builder.i32_const(*nullable_id as i32); self.code_builder.i32_const(*nullable_id as i32);
self.code_builder.set_local(local_id); self.code_builder.set_local(local_id);
self.code_builder.else_(); self.code_builder.else_();

View file

@ -36,29 +36,7 @@ impl Serialize for ValueType {
} }
} }
#[derive(PartialEq, Eq, Debug)] const BLOCK_NO_RESULT: u8 = 0x40;
pub enum BlockType {
NoResult,
Value(ValueType),
}
impl BlockType {
pub fn as_byte(&self) -> u8 {
match self {
Self::NoResult => 0x40,
Self::Value(t) => *t as u8,
}
}
}
impl From<Option<ValueType>> for BlockType {
fn from(opt: Option<ValueType>) -> Self {
match opt {
Some(ty) => BlockType::Value(ty),
None => BlockType::NoResult,
}
}
}
/// A control block in our model of the VM /// A control block in our model of the VM
/// Child blocks cannot "see" values from their parent block /// Child blocks cannot "see" values from their parent block
@ -579,9 +557,11 @@ impl<'a> CodeBuilder<'a> {
} }
/// Block instruction /// Block instruction
fn inst_block(&mut self, opcode: OpCode, pops: usize, block_type: BlockType) { fn inst_block(&mut self, opcode: OpCode, pops: usize) {
self.inst_base(opcode, pops, false); self.inst_base(opcode, pops, false);
self.code.push(block_type.as_byte());
// We don't support block result types. Too hard to track types through arbitrary control flow.
self.code.push(BLOCK_NO_RESULT);
// Start a new block with a fresh value stack // Start a new block with a fresh value stack
self.vm_block_stack.push(VmBlock { self.vm_block_stack.push(VmBlock {
@ -589,12 +569,7 @@ impl<'a> CodeBuilder<'a> {
value_stack: Vec::with_capacity_in(8, self.arena), value_stack: Vec::with_capacity_in(8, self.arena),
}); });
log_instruction!( log_instruction!("{:10}\t{:?}", format!("{:?}", opcode), &self.vm_block_stack);
"{:10} {:?}\t{:?}",
format!("{:?}", opcode),
block_type,
&self.vm_block_stack
);
} }
fn inst_imm32(&mut self, opcode: OpCode, pops: usize, push: bool, immediate: u32) { fn inst_imm32(&mut self, opcode: OpCode, pops: usize, push: bool, immediate: u32) {
@ -647,14 +622,14 @@ impl<'a> CodeBuilder<'a> {
instruction_no_args!(unreachable_, UNREACHABLE, 0, false); instruction_no_args!(unreachable_, UNREACHABLE, 0, false);
instruction_no_args!(nop, NOP, 0, false); instruction_no_args!(nop, NOP, 0, false);
pub fn block(&mut self, ty: BlockType) { pub fn block(&mut self) {
self.inst_block(BLOCK, 0, ty); self.inst_block(BLOCK, 0);
} }
pub fn loop_(&mut self, ty: BlockType) { pub fn loop_(&mut self) {
self.inst_block(LOOP, 0, ty); self.inst_block(LOOP, 0);
} }
pub fn if_(&mut self, ty: BlockType) { pub fn if_(&mut self) {
self.inst_block(IF, 1, ty); self.inst_block(IF, 1);
} }
pub fn else_(&mut self) { pub fn else_(&mut self) {
// Reuse the 'then' block but clear its value stack // Reuse the 'then' block but clear its value stack
@ -947,7 +922,7 @@ impl<'a> CodeBuilder<'a> {
self.i32_const(expected); self.i32_const(expected);
self.i32_eq(); self.i32_eq();
self.i32_eqz(); self.i32_eqz();
self.if_(BlockType::NoResult); self.if_();
self.unreachable_(); // Tell Wasm runtime to throw an exception self.unreachable_(); // Tell Wasm runtime to throw an exception
self.end(); self.end();
// It matches. Restore the original value to the VM stack and continue the program. // It matches. Restore the original value to the VM stack and continue the program.

View file

@ -4,6 +4,6 @@ pub mod opcodes;
pub mod sections; pub mod sections;
pub mod serialize; pub mod serialize;
pub use code_builder::{Align, BlockType, CodeBuilder, LocalId, ValueType, VmSymbolState}; pub use code_builder::{Align, CodeBuilder, LocalId, ValueType, VmSymbolState};
pub use linking::{LinkingSubSection, SymInfo}; pub use linking::{LinkingSubSection, SymInfo};
pub use sections::{ConstExpr, Export, ExportType, Global, GlobalType, Signature, WasmModule}; pub use sections::{ConstExpr, Export, ExportType, Global, GlobalType, Signature, WasmModule};