From a968b2a7a5f4ca44b7c829d678434be0da6a8df8 Mon Sep 17 00:00:00 2001 From: Haled Odat <8566042+HalidOdat@users.noreply.github.com> Date: Fri, 21 Jul 2023 20:59:35 +0200 Subject: [PATCH] Fix flowgraph generation --- boa_engine/src/vm/flowgraph/mod.rs | 139 ++++++---------------- boa_engine/src/vm/opcode/generator/mod.rs | 6 +- 2 files changed, 39 insertions(+), 106 deletions(-) diff --git a/boa_engine/src/vm/flowgraph/mod.rs b/boa_engine/src/vm/flowgraph/mod.rs index d86aae2f6be..b870c360bd5 100644 --- a/boa_engine/src/vm/flowgraph/mod.rs +++ b/boa_engine/src/vm/flowgraph/mod.rs @@ -28,8 +28,6 @@ impl CodeBlock { graph.set_label(name); - let mut environments = Vec::new(); - let mut pc = 0; while pc < self.bytecode.len() { let opcode: Opcode = self.bytecode[pc].into(); @@ -258,41 +256,6 @@ impl CodeBlock { ); graph.add_edge(previous_pc, pc, None, Color::Green, EdgeStyle::Line); } - // => { - // let next_address = self.read::(pc); - // pc += size_of::(); - // let finally_address = self.read::(pc); - // pc += size_of::(); - - // try_entries.push(( - // previous_pc, - // next_address, - // if finally_address == 0 { - // None - // } else { - // Some(finally_address) - // }, - // )); - - // graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); - // graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); - // graph.add_edge( - // previous_pc, - // next_address as usize, - // Some("NEXT".into()), - // Color::None, - // EdgeStyle::Line, - // ); - // if finally_address != 0 { - // graph.add_edge( - // previous_pc, - // finally_address as usize, - // Some("FINALLY".into()), - // Color::None, - // EdgeStyle::Line, - // ); - // } - // } Opcode::CopyDataProperties => { let operand1 = self.read::(pc); pc += size_of::(); @@ -305,7 +268,6 @@ impl CodeBlock { } Opcode::PushDeclarativeEnvironment | Opcode::PushFunctionEnvironment => { let random = rand::random(); - environments.push((previous_pc, random)); pc += size_of::(); @@ -318,22 +280,8 @@ impl CodeBlock { graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); } Opcode::PopEnvironment => { - let (environment_push, random) = environments - .pop() - .expect("There should be a push evironment before"); - - let color = Color::from_random_number(random); - graph.add_node(previous_pc, NodeShape::None, label.into(), color); + graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); - graph - .add_edge( - previous_pc, - environment_push, - None, - color, - EdgeStyle::Dotted, - ) - .set_type(EdgeType::None); } Opcode::GetArrowFunction | Opcode::GetAsyncArrowFunction @@ -416,36 +364,32 @@ impl CodeBlock { graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); } Opcode::ThrowNewTypeError => { - // FIXME: - // pc += size_of::(); - // graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); - // if let Some((_try_pc, next, _finally)) = try_entries.last() { - // graph.add_edge( - // previous_pc, - // *next as usize, - // Some("CAUGHT".into()), - // Color::None, - // EdgeStyle::Line, - // ); - // } else { - // returns.push(previous_pc); - // } + + graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); + if let Some(handler) = self.find_handler(previous_pc as u32) { + graph.add_edge( + previous_pc, + handler.handler() as usize, + Some("CAUGHT".into()), + Color::None, + EdgeStyle::Line, + ); + } } Opcode::Throw | Opcode::ReThrow => { - // FIXME: - // graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); - // if let Some((_try_pc, next, _finally)) = try_entries.last() { - // graph.add_edge( - // previous_pc, - // *next as usize, - // Some("CAUGHT".into()), - // Color::None, - // EdgeStyle::Line, - // ); - // } else { - // returns.push(previous_pc); - // } + if let Some(handler) = self.find_handler(previous_pc as u32) { + graph.add_node(previous_pc, NodeShape::Record, label.into(), Color::None); + graph.add_edge( + previous_pc, + handler.handler() as usize, + Some("CAUGHT".into()), + Color::None, + EdgeStyle::Line, + ); + } else { + graph.add_node(previous_pc, NodeShape::Diamond, label.into(), Color::None); + } } Opcode::PushPrivateEnvironment => { let count = self.read::(pc); @@ -455,10 +399,19 @@ impl CodeBlock { } Opcode::JumpTable => { let count = self.read::(pc); - pc += size_of::() * (count as usize + 2); + pc += size_of::(); + let default = self.read::(pc); + pc += size_of::(); graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); - graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); + graph.add_edge(previous_pc, default as usize, Some("DEFAULT".into()), Color::None, EdgeStyle::Line); + + for i in 0..count { + let address = self.read::(pc); + pc += size_of::(); + + graph.add_edge(previous_pc, address as usize, Some(format!("Index: {i}").into()), Color::None, EdgeStyle::Line); + } } Opcode::Pop | Opcode::Dup @@ -577,20 +530,7 @@ impl CodeBlock { graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line); } Opcode::Return => { - // FIXME: - // - // graph.add_node(previous_pc, NodeShape::None, label.into(), Color::None); - // if let Some((_try_pc, _next, Some(finally))) = try_entries.last() { - // graph.add_edge( - // previous_pc, - // *finally as usize, - // None, - // Color::None, - // EdgeStyle::Line, - // ); - // } else { - // returns.push(previous_pc); - // } + graph.add_node(previous_pc, NodeShape::Diamond, label.into(), Color::Red); } Opcode::Reserved1 | Opcode::Reserved2 @@ -659,13 +599,6 @@ impl CodeBlock { } } - // TODO: FIXME: - // for ret in returns { - // graph.add_edge(ret, pc, None, Color::None, EdgeStyle::Line); - // } - - graph.add_node(pc, NodeShape::Diamond, "End".into(), Color::Red); - for function in self.functions.as_ref() { let subgraph = graph.subgraph(String::new()); function.to_graph(interner, subgraph); diff --git a/boa_engine/src/vm/opcode/generator/mod.rs b/boa_engine/src/vm/opcode/generator/mod.rs index e358862cea6..6b060c415d1 100644 --- a/boa_engine/src/vm/opcode/generator/mod.rs +++ b/boa_engine/src/vm/opcode/generator/mod.rs @@ -103,7 +103,7 @@ impl Operation for GeneratorDelegateNext { )?; context.vm.push(false); context.vm.push(result); - context.vm.push(GeneratorResumeKind::Normal as i32); + context.vm.push(GeneratorResumeKind::Normal); } GeneratorResumeKind::Throw => { let throw = iterator_record @@ -117,7 +117,7 @@ impl Operation for GeneratorDelegateNext { )?; context.vm.push(false); context.vm.push(result); - context.vm.push(GeneratorResumeKind::Normal as i32); + context.vm.push(GeneratorResumeKind::Normal); } else { let error = JsNativeError::typ() .with_message("iterator does not have a throw method") @@ -138,7 +138,7 @@ impl Operation for GeneratorDelegateNext { )?; context.vm.push(true); context.vm.push(result); - context.vm.push(GeneratorResumeKind::Normal as i32); + context.vm.push(GeneratorResumeKind::Normal); } else { context.vm.push(received); context.vm.frame_mut().pc = return_method_undefined;