Skip to content

Commit

Permalink
Merge Terminator Jump types
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jun 15, 2023
1 parent 1b91bc3 commit f572038
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions boa_engine/src/optimizer/control_flow_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,14 @@ pub enum Terminator {
/// TODO: doc
None,
/// TODO: doc
JumpUnconditional(u32),
/// TODO: doc
JumpConditional(Opcode, u32),
Jump(Opcode, u32),
}

impl Debug for Terminator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Terminator::None => write!(f, "None"),
Terminator::JumpUnconditional(target) => write!(f, "Jump B{target}"),
Terminator::JumpConditional(opcode, target) => {
Terminator::Jump(opcode, target) => {
write!(f, "{} B{target}", opcode.as_str())
}
}
Expand All @@ -467,6 +464,21 @@ impl Terminator {
pub fn is_none(&self) -> bool {
matches!(self, Terminator::None)
}

/// Check if [`Terminator::Jump`].
pub fn is_jump(&self) -> bool {
matches!(self, Terminator::Jump(_, _))
}

/// Check if unconditional [`Terminator::Jump`].
pub fn is_unconditional_jump(&self) -> bool {
matches!(self, Terminator::Jump(Opcode::Jump, _))
}

/// Check if conditional [`Terminator::Jump`].
pub fn is_conditional_jump(&self) -> bool {
matches!(self, Terminator::Jump(opcode, _) if *opcode != Opcode::Jump)
}
}

/// TODO: doc
Expand Down Expand Up @@ -633,15 +645,15 @@ impl ControlFlowGraph {
basic_block.terminator = if result.opcode == Opcode::Jump {
references[*index as usize].push(i as u32);

Terminator::JumpUnconditional(*index)
Terminator::Jump(Opcode::Jump, *index)
} else {
references[*index as usize].push(i as u32);

if i + 1 != references.len() {
references[*index as usize].push(i as u32 + 1);
}

Terminator::JumpConditional(result.opcode, *index)
Terminator::Jump(result.opcode, *index)
};

basic_block.bytecode.truncate(len - 5);
Expand Down Expand Up @@ -712,13 +724,7 @@ impl ControlFlowGraph {
results.extend(basic_block.bytecode);
match basic_block.terminator {
Terminator::None => {}
Terminator::JumpUnconditional(target) => {
results.extend_from_slice(&[Opcode::Jump as u8]);
let start = results.len();
results.extend_from_slice(&[0, 0, 0, 0]);
labels.push((start as u32, target));
}
Terminator::JumpConditional(opcode, target) => {
Terminator::Jump(opcode, target) => {
results.extend_from_slice(&[opcode as u8]);
let start = results.len();
results.extend_from_slice(&[0, 0, 0, 0]);
Expand Down

0 comments on commit f572038

Please sign in to comment.