Skip to content
Snippets Groups Projects
Commit bab57b3e authored by Russel Arbore's avatar Russel Arbore
Browse files

Remove unnecessary control edges

parent 09291f0c
No related branches found
No related tags found
No related merge requests found
......@@ -69,31 +69,21 @@ fn write_node<W: std::fmt::Write>(
write!(w, "{} [label=\"{:?}\"];\n", name, constants[id.idx()])?;
visited
}
Node::Add {
control,
left,
right,
} => {
let (control_name, visited) =
write_node(i, control.idx(), module, constants, visited, w)?;
Node::Add { left, right } => {
let (left_name, visited) =
write_node(i, left.idx(), module, constants, visited, w)?;
let (right_name, visited) =
write_node(i, right.idx(), module, constants, visited, w)?;
write!(w, "{} [label=\"add\"];\n", name)?;
write!(w, "{} -> {} [style=\"dashed\"];\n", control_name, name)?;
write!(w, "{} -> {};\n", left_name, name)?;
write!(w, "{} -> {};\n", right_name, name)?;
visited
}
Node::Call {
control,
function,
dynamic_constants,
args,
} => {
let (control_name, mut visited) =
write_node(i, control.idx(), module, constants, visited, w)?;
for arg in args.iter() {
let (arg_name, tmp_visited) =
write_node(i, arg.idx(), module, constants, visited, w)?;
......@@ -106,7 +96,6 @@ fn write_node<W: std::fmt::Write>(
name,
module.functions[function.idx()].name
)?;
write!(w, "{} -> {} [style=\"dashed\"];\n", control_name, name)?;
write!(
w,
"{} -> start_{}_0 [lhead={}];\n",
......@@ -149,28 +138,11 @@ fn get_string_node_kind(node: &Node) -> &'static str {
Node::Parameter { index: _ } => "parameter",
Node::DynamicConstant { id: _ } => "dynamic_constant",
Node::Constant { id: _ } => "constant",
Node::Add {
control: _,
left: _,
right: _,
} => "add",
Node::Sub {
control: _,
left: _,
right: _,
} => "sub",
Node::Mul {
control: _,
left: _,
right: _,
} => "mul",
Node::Div {
control: _,
left: _,
right: _,
} => "div",
Node::Add { left: _, right: _ } => "add",
Node::Sub { left: _, right: _ } => "sub",
Node::Mul { left: _, right: _ } => "mul",
Node::Div { left: _, right: _ } => "div",
Node::Call {
control: _,
function: _,
dynamic_constants: _,
args: _,
......
......@@ -94,27 +94,22 @@ pub enum Node {
id: DynamicConstantID,
},
Add {
control: NodeID,
left: NodeID,
right: NodeID,
},
Sub {
control: NodeID,
left: NodeID,
right: NodeID,
},
Mul {
control: NodeID,
left: NodeID,
right: NodeID,
},
Div {
control: NodeID,
left: NodeID,
right: NodeID,
},
Call {
control: NodeID,
function: FunctionID,
dynamic_constants: Box<[DynamicConstantID]>,
args: Box<[NodeID]>,
......
......@@ -112,15 +112,13 @@ fn parse_module<'a>(ir_text: &'a str, context: Context<'a>) -> nom::IResult<&'a
for (dynamic_constant, id) in context.interned_dynamic_constants {
dynamic_constants[id.idx()] = dynamic_constant;
}
Ok((
rest,
Module {
functions: fixed_functions,
types,
constants,
dynamic_constants,
},
))
let module = Module {
functions: fixed_functions,
types,
constants,
dynamic_constants,
};
Ok((rest, module))
}
fn parse_function<'a>(
......@@ -255,10 +253,6 @@ fn parse_add<'a>(ir_text: &'a str, context: &RefCell<Context<'a>>) -> nom::IResu
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::character::complete::char('(')(ir_text)?.0;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let (ir_text, control) = nom::character::complete::alphanumeric1(ir_text)?;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::character::complete::char(',')(ir_text)?.0;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let (ir_text, left) = nom::character::complete::alphanumeric1(ir_text)?;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::character::complete::char(',')(ir_text)?.0;
......@@ -266,17 +260,9 @@ fn parse_add<'a>(ir_text: &'a str, context: &RefCell<Context<'a>>) -> nom::IResu
let (ir_text, right) = nom::character::complete::alphanumeric1(ir_text)?;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::character::complete::char(')')(ir_text)?.0;
let control = context.borrow_mut().get_node_id(control);
let left = context.borrow_mut().get_node_id(left);
let right = context.borrow_mut().get_node_id(right);
Ok((
ir_text,
Node::Add {
control,
left,
right,
},
))
Ok((ir_text, Node::Add { left, right }))
}
fn parse_call<'a>(ir_text: &'a str, context: &RefCell<Context<'a>>) -> nom::IResult<&'a str, Node> {
......@@ -301,10 +287,6 @@ fn parse_call<'a>(ir_text: &'a str, context: &RefCell<Context<'a>>) -> nom::IRes
let dynamic_constants = dynamic_constants.unwrap_or(vec![]);
let ir_text = nom::character::complete::char('(')(ir_text)?.0;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let (ir_text, control) = nom::character::complete::alphanumeric1(ir_text)?;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::character::complete::char(',')(ir_text)?.0;
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let (ir_text, mut function_and_args) = nom::multi::separated_list1(
nom::sequence::tuple((
nom::character::complete::multispace0,
......@@ -320,12 +302,10 @@ fn parse_call<'a>(ir_text: &'a str, context: &RefCell<Context<'a>>) -> nom::IRes
.collect();
let ir_text = nom::character::complete::multispace0(ir_text)?.0;
let ir_text = nom::character::complete::char(')')(ir_text)?.0;
let control = context.borrow_mut().get_node_id(control);
let function = context.borrow_mut().get_function_id(function);
Ok((
ir_text,
Node::Call {
control,
function,
dynamic_constants: dynamic_constants.into_boxed_slice(),
args: args.into_boxed_slice(),
......@@ -743,14 +723,14 @@ mod tests {
parse(
"
fn myfunc(x: i32) -> i32
y = call<0>(start, add, x, x)
y = call<0>(add, x, x)
r = return(start, y)
fn add<1>(x: i32, y: i32) -> i32
c = constant(i8, 5)
r = return(start, w)
w = add(start, z, c)
z = add(start, x, y)
w = add(z, c)
z = add(x, y)
",
);
}
......
fn myfunc(x: i32) -> i32
y = call(start, add, x, x)
y = call(add, x, x)
r = return(start, y)
fn add(x: i32, y: i32) -> i32
c = constant(i8, 5)
r = return(start, w)
w = add(start, z, c)
z = add(start, x, y)
w = add(z, c)
z = add(x, y)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment