diff --git a/cranelift/codegen/src/ir/jumptable.rs b/cranelift/codegen/src/ir/jumptable.rs index 70d0b1ddde..4a847a15eb 100644 --- a/cranelift/codegen/src/ir/jumptable.rs +++ b/cranelift/codegen/src/ir/jumptable.rs @@ -15,11 +15,10 @@ use serde::{Deserialize, Serialize}; /// /// All jump tables use 0-based indexing and are densely populated. /// -/// The default block for the jump table is stored as the last element of the underlying vector, -/// and is not included in the length of the jump table. It can be accessed through the -/// `default_block` and `default_block_mut` functions. All blocks may be iterated using the -/// `all_branches` and `all_branches_mut` functions, which will both iterate over the default block -/// last. +/// The default block for the jump table is stored as the first element of the underlying vector. +/// It can be accessed through the `default_block` and `default_block_mut` functions. All blocks +/// may be iterated using the `all_branches` and `all_branches_mut` functions, which will both +/// iterate over the default block first. #[derive(Clone, PartialEq, Hash)] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] pub struct JumpTableData { @@ -29,42 +28,41 @@ pub struct JumpTableData { impl JumpTableData { /// Create a new jump table with the provided blocks - pub fn new(def: Block, mut table: Vec) -> Self { - table.push(def); - Self { table } + pub fn new(def: Block, table: &[Block]) -> Self { + Self { + table: std::iter::once(def).chain(table.iter().copied()).collect(), + } } /// Fetch the default block for this jump table. pub fn default_block(&self) -> Block { - *self.table.last().unwrap() + *self.table.first().unwrap() } /// Mutable access to the default block of this jump table. pub fn default_block_mut(&mut self) -> &mut Block { - self.table.last_mut().unwrap() + self.table.first_mut().unwrap() } - /// The jump table and default block as a single slice. The default block will always be last. + /// The jump table and default block as a single slice. The default block will always be first. pub fn all_branches(&self) -> &[Block] { self.table.as_slice() } /// The jump table and default block as a single mutable slice. The default block will always - /// be last. + /// be first. pub fn all_branches_mut(&mut self) -> &mut [Block] { self.table.as_mut_slice() } /// Access the jump table as a slice. This excludes the default block. pub fn as_slice(&self) -> &[Block] { - let last = self.table.len() - 1; - &self.table.as_slice()[0..last] + &self.table.as_slice()[1..] } /// Access the jump table as a mutable slice. This excludes the default block. pub fn as_mut_slice(&mut self) -> &mut [Block] { - let last = self.table.len() - 1; - &mut self.table.as_mut_slice()[0..last] + &mut self.table.as_mut_slice()[1..] } /// Returns an iterator to the jump table, excluding the default block. @@ -81,7 +79,7 @@ impl JumpTableData { /// Clears all entries in this jump table, except for the default block. pub fn clear(&mut self) { - self.table.drain(0..self.table.len() - 1); + self.table.drain(1..); } } @@ -109,7 +107,7 @@ mod tests { fn empty() { let def = Block::new(0); - let jt = JumpTableData::new(def, vec![]); + let jt = JumpTableData::new(def, &[]); assert_eq!(jt.all_branches().get(0), Some(&def)); @@ -128,12 +126,12 @@ mod tests { let e1 = Block::new(1); let e2 = Block::new(2); - let jt = JumpTableData::new(def, vec![e1, e2, e1]); + let jt = JumpTableData::new(def, &[e1, e2, e1]); assert_eq!(jt.default_block(), def); assert_eq!(jt.to_string(), "block0, [block1, block2, block1]"); - assert_eq!(jt.all_branches(), [e1, e2, e1, def]); + assert_eq!(jt.all_branches(), [def, e1, e2, e1]); assert_eq!(jt.as_slice(), [e1, e2, e1]); } } diff --git a/cranelift/codegen/src/isa/aarch64/mod.rs b/cranelift/codegen/src/isa/aarch64/mod.rs index 8345491028..6f6f30e11a 100644 --- a/cranelift/codegen/src/isa/aarch64/mod.rs +++ b/cranelift/codegen/src/isa/aarch64/mod.rs @@ -363,7 +363,7 @@ mod test { pos.insert_block(bb0); let jt = pos .func - .create_jump_table(JumpTableData::new(bb3, vec![bb1, bb2])); + .create_jump_table(JumpTableData::new(bb3, &[bb1, bb2])); pos.ins().br_table(arg0, jt); pos.insert_block(bb1); diff --git a/cranelift/codegen/src/isa/x64/mod.rs b/cranelift/codegen/src/isa/x64/mod.rs index a7bda0a466..d43e58d31a 100644 --- a/cranelift/codegen/src/isa/x64/mod.rs +++ b/cranelift/codegen/src/isa/x64/mod.rs @@ -410,7 +410,7 @@ mod test { pos.insert_block(bb0); let jt = pos .func - .create_jump_table(JumpTableData::new(bb3, vec![bb1, bb2])); + .create_jump_table(JumpTableData::new(bb3, &[bb1, bb2])); pos.ins().br_table(arg0, jt); pos.insert_block(bb1); diff --git a/cranelift/frontend/src/ssa.rs b/cranelift/frontend/src/ssa.rs index c388ee425a..9b17484b3a 100644 --- a/cranelift/frontend/src/ssa.rs +++ b/cranelift/frontend/src/ssa.rs @@ -1010,7 +1010,7 @@ mod tests { ssa.def_var(x_var, x1, block0); ssa.use_var(&mut func, x_var, I32, block0).0; let br_table = { - let jump_table = JumpTableData::new(block2, vec![block2, block1]); + let jump_table = JumpTableData::new(block2, &[block2, block1]); let jt = func.create_jump_table(jump_table); let mut cur = FuncCursor::new(&mut func).at_bottom(block0); cur.ins().br_table(x1, jt) diff --git a/cranelift/frontend/src/switch.rs b/cranelift/frontend/src/switch.rs index 7788283286..01f5ab905e 100644 --- a/cranelift/frontend/src/switch.rs +++ b/cranelift/frontend/src/switch.rs @@ -220,7 +220,7 @@ impl Switch { "Jump tables bigger than 2^32-1 are not yet supported" ); - let jt_data = JumpTableData::new(otherwise, Vec::from(blocks)); + let jt_data = JumpTableData::new(otherwise, blocks); let jump_table = bx.create_jump_table(jt_data); let discr = if first_index == 0 { diff --git a/cranelift/fuzzgen/src/function_generator.rs b/cranelift/fuzzgen/src/function_generator.rs index 1c4b68b187..2285b0332c 100644 --- a/cranelift/fuzzgen/src/function_generator.rs +++ b/cranelift/fuzzgen/src/function_generator.rs @@ -1614,7 +1614,7 @@ where } BlockTerminator::BrTable(default, targets) => { // Create jump tables on demand - let jt = builder.create_jump_table(JumpTableData::new(default, targets)); + let jt = builder.create_jump_table(JumpTableData::new(default, &targets)); // br_table only supports I32 let val = builder.use_var(self.get_variable_of_type(I32)?); diff --git a/cranelift/reader/src/parser.rs b/cranelift/reader/src/parser.rs index b0cb2baac5..91f4d50a64 100644 --- a/cranelift/reader/src/parser.rs +++ b/cranelift/reader/src/parser.rs @@ -1799,7 +1799,7 @@ impl<'a> Parser<'a> { self.consume(); - Ok(JumpTableData::new(def, data)) + Ok(JumpTableData::new(def, &data)) } // Parse a constant decl. diff --git a/cranelift/wasm/src/code_translator.rs b/cranelift/wasm/src/code_translator.rs index 1d7fd2d959..556c5d63c8 100644 --- a/cranelift/wasm/src/code_translator.rs +++ b/cranelift/wasm/src/code_translator.rs @@ -534,7 +534,7 @@ pub fn translate_operator( frame.set_branched_to_exit(); frame.br_destination() }; - let jt = builder.create_jump_table(JumpTableData::new(block, data)); + let jt = builder.create_jump_table(JumpTableData::new(block, &data)); builder.ins().br_table(val, jt); } else { // Here we have jump arguments, but Cranelift's br_table doesn't support them @@ -562,7 +562,7 @@ pub fn translate_operator( *entry.insert(block) } }; - let jt = builder.create_jump_table(JumpTableData::new(default_branch_block, data)); + let jt = builder.create_jump_table(JumpTableData::new(default_branch_block, &data)); builder.ins().br_table(val, jt); for (depth, dest_block) in dest_block_sequence { builder.switch_to_block(dest_block);