Remove runtime dependence for BrTable

This commit is contained in:
Heyang Zhou 2019-05-04 07:25:11 -07:00
parent af58ea5642
commit 7a4b2172a5

View File

@ -134,7 +134,6 @@ pub struct X64FunctionCode {
assembler: Option<Assembler>, assembler: Option<Assembler>,
function_labels: Option<HashMap<usize, (DynamicLabel, Option<AssemblyOffset>)>>, function_labels: Option<HashMap<usize, (DynamicLabel, Option<AssemblyOffset>)>>,
br_table_data: Option<Vec<Vec<usize>>>,
breakpoints: Option<HashMap<AssemblyOffset, Box<Fn(BkptInfo) + Send + Sync + 'static>>>, breakpoints: Option<HashMap<AssemblyOffset, Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
returns: SmallVec<[WpType; 1]>, returns: SmallVec<[WpType; 1]>,
locals: Vec<Location>, locals: Vec<Location>,
@ -161,7 +160,6 @@ pub struct X64ExecutionContext {
function_pointers: Vec<FuncPtr>, function_pointers: Vec<FuncPtr>,
function_offsets: Vec<AssemblyOffset>, function_offsets: Vec<AssemblyOffset>,
signatures: Arc<Map<SigIndex, FuncSig>>, signatures: Arc<Map<SigIndex, FuncSig>>,
_br_table_data: Vec<Vec<usize>>,
breakpoints: Arc<HashMap<usize, Box<Fn(BkptInfo) + Send + Sync + 'static>>>, breakpoints: Arc<HashMap<usize, Box<Fn(BkptInfo) + Send + Sync + 'static>>>,
func_import_count: usize, func_import_count: usize,
} }
@ -309,18 +307,16 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
} }
fn next_function(&mut self) -> Result<&mut X64FunctionCode, CodegenError> { fn next_function(&mut self) -> Result<&mut X64FunctionCode, CodegenError> {
let (mut assembler, mut function_labels, br_table_data, breakpoints) = let (mut assembler, mut function_labels, breakpoints) =
match self.functions.last_mut() { match self.functions.last_mut() {
Some(x) => ( Some(x) => (
x.assembler.take().unwrap(), x.assembler.take().unwrap(),
x.function_labels.take().unwrap(), x.function_labels.take().unwrap(),
x.br_table_data.take().unwrap(),
x.breakpoints.take().unwrap(), x.breakpoints.take().unwrap(),
), ),
None => ( None => (
self.assembler.take().unwrap(), self.assembler.take().unwrap(),
self.function_labels.take().unwrap(), self.function_labels.take().unwrap(),
vec![],
HashMap::new(), HashMap::new(),
), ),
}; };
@ -343,7 +339,6 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
assembler: Some(assembler), assembler: Some(assembler),
function_labels: Some(function_labels), function_labels: Some(function_labels),
br_table_data: Some(br_table_data),
breakpoints: Some(breakpoints), breakpoints: Some(breakpoints),
returns: smallvec![], returns: smallvec![],
locals: vec![], locals: vec![],
@ -359,10 +354,9 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
} }
fn finalize(mut self, _: &ModuleInfo) -> Result<X64ExecutionContext, CodegenError> { fn finalize(mut self, _: &ModuleInfo) -> Result<X64ExecutionContext, CodegenError> {
let (assembler, mut br_table_data, breakpoints) = match self.functions.last_mut() { let (assembler, breakpoints) = match self.functions.last_mut() {
Some(x) => ( Some(x) => (
x.assembler.take().unwrap(), x.assembler.take().unwrap(),
x.br_table_data.take().unwrap(),
x.breakpoints.take().unwrap(), x.breakpoints.take().unwrap(),
), ),
None => { None => {
@ -373,12 +367,6 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
}; };
let output = assembler.finalize().unwrap(); let output = assembler.finalize().unwrap();
for table in &mut br_table_data {
for entry in table {
*entry = output.ptr(AssemblyOffset(*entry)) as usize;
}
}
let function_labels = if let Some(x) = self.functions.last() { let function_labels = if let Some(x) = self.functions.last() {
x.function_labels.as_ref().unwrap() x.function_labels.as_ref().unwrap()
} else { } else {
@ -419,7 +407,6 @@ impl ModuleCodeGenerator<X64FunctionCode, X64ExecutionContext, CodegenError>
code: output, code: output,
functions: self.functions, functions: self.functions,
signatures: self.signatures.as_ref().unwrap().clone(), signatures: self.signatures.as_ref().unwrap().clone(),
_br_table_data: br_table_data,
breakpoints: breakpoints, breakpoints: breakpoints,
func_import_count: self.func_import_count, func_import_count: self.func_import_count,
function_pointers: out_labels, function_pointers: out_labels,
@ -4051,7 +4038,8 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
let (targets, default_target) = table.read_table().unwrap(); let (targets, default_target) = table.read_table().unwrap();
let cond = let cond =
get_location_released(a, &mut self.machine, self.value_stack.pop().unwrap()); get_location_released(a, &mut self.machine, self.value_stack.pop().unwrap());
let mut table = vec![0usize; targets.len()]; let mut table_label = a.get_label();
let mut table: Vec<DynamicLabel> = vec![];
let default_br = a.get_label(); let default_br = a.get_label();
Self::emit_relaxed_binop( Self::emit_relaxed_binop(
a, a,
@ -4063,19 +4051,19 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
); );
a.emit_jmp(Condition::AboveEqual, default_br); a.emit_jmp(Condition::AboveEqual, default_br);
a.emit_mov( a.emit_lea_label(
Size::S64, table_label,
Location::Imm64(table.as_ptr() as usize as u64),
Location::GPR(GPR::RCX), Location::GPR(GPR::RCX),
); );
a.emit_mov(Size::S32, cond, Location::GPR(GPR::RDX)); a.emit_mov(Size::S32, cond, Location::GPR(GPR::RDX));
a.emit_shl(Size::S32, Location::Imm8(3), Location::GPR(GPR::RDX)); a.emit_imul_imm32_gpr64(5, GPR::RDX);
a.emit_add(Size::S64, Location::GPR(GPR::RCX), Location::GPR(GPR::RDX)); a.emit_add(Size::S64, Location::GPR(GPR::RCX), Location::GPR(GPR::RDX));
a.emit_jmp_location(Location::Memory(GPR::RDX, 0)); a.emit_jmp_location(Location::GPR(GPR::RDX));
for (i, target) in targets.iter().enumerate() { for (i, target) in targets.iter().enumerate() {
let AssemblyOffset(offset) = a.offset(); let label = a.get_label();
table[i] = offset; a.emit_label(label);
table.push(label);
let frame = let frame =
&self.control_stack[self.control_stack.len() - 1 - (*target as usize)]; &self.control_stack[self.control_stack.len() - 1 - (*target as usize)];
if !frame.loop_like && frame.returns.len() > 0 { if !frame.loop_like && frame.returns.len() > 0 {
@ -4110,7 +4098,10 @@ impl FunctionCodeGenerator<CodegenError> for X64FunctionCode {
a.emit_jmp(Condition::None, frame.label); a.emit_jmp(Condition::None, frame.label);
} }
self.br_table_data.as_mut().unwrap().push(table); a.emit_label(table_label);
for x in table {
a.emit_jmp(Condition::None, x);
}
self.unreachable_depth = 1; self.unreachable_depth = 1;
} }
Operator::Drop => { Operator::Drop => {