Skip to content

Commit

Permalink
Fix flowgraph generation
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Jul 21, 2023
1 parent a1c545e commit a968b2a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 106 deletions.
139 changes: 36 additions & 103 deletions boa_engine/src/vm/flowgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -258,41 +256,6 @@ impl CodeBlock {
);
graph.add_edge(previous_pc, pc, None, Color::Green, EdgeStyle::Line);
}
// => {
// let next_address = self.read::<u32>(pc);
// pc += size_of::<u32>();
// let finally_address = self.read::<u32>(pc);
// pc += size_of::<u32>();

// 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::<u32>(pc);
pc += size_of::<u32>();
Expand All @@ -305,7 +268,6 @@ impl CodeBlock {
}
Opcode::PushDeclarativeEnvironment | Opcode::PushFunctionEnvironment => {
let random = rand::random();
environments.push((previous_pc, random));

pc += size_of::<u32>();

Expand All @@ -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
Expand Down Expand Up @@ -416,36 +364,32 @@ impl CodeBlock {
graph.add_edge(previous_pc, pc, None, Color::None, EdgeStyle::Line);
}
Opcode::ThrowNewTypeError => {
// FIXME:
//
pc += size_of::<u32>();
// 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::<u32>(pc);
Expand All @@ -455,10 +399,19 @@ impl CodeBlock {
}
Opcode::JumpTable => {
let count = self.read::<u32>(pc);
pc += size_of::<u32>() * (count as usize + 2);
pc += size_of::<u32>();
let default = self.read::<u32>(pc);
pc += size_of::<u32>();

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::<u32>(pc);
pc += size_of::<u32>();

graph.add_edge(previous_pc, address as usize, Some(format!("Index: {i}").into()), Color::None, EdgeStyle::Line);
}
}
Opcode::Pop
| Opcode::Dup
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions boa_engine/src/vm/opcode/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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;
Expand Down

0 comments on commit a968b2a

Please sign in to comment.