From 6cf4d48802287b720d919dd701ac4495a0c83499 Mon Sep 17 00:00:00 2001 From: Patrick LaFontaine <32135464+Pat-Lafon@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:16:33 -0700 Subject: [PATCH 1/3] minor cleanup --- brilirs/src/basic_block.rs | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/brilirs/src/basic_block.rs b/brilirs/src/basic_block.rs index 7294f58c..d82ac304 100644 --- a/brilirs/src/basic_block.rs +++ b/brilirs/src/basic_block.rs @@ -220,23 +220,11 @@ impl BBFunction { } curr_block.label = Some(label); } - bril_rs::Code::Instruction(bril_rs::Instruction::Effect { - op, - args, - funcs, - labels, - pos, - }) if op == bril_rs::EffectOps::Jump - || op == bril_rs::EffectOps::Branch - || op == bril_rs::EffectOps::Return => + bril_rs::Code::Instruction(i @ bril_rs::Instruction::Effect { op, .. }) + if op == bril_rs::EffectOps::Jump + || op == bril_rs::EffectOps::Branch + || op == bril_rs::EffectOps::Return => { - let i = bril_rs::Instruction::Effect { - op, - args, - funcs, - labels, - pos, - }; curr_block.numified_instrs.push(NumifiedInstruction::new( &i, &mut num_of_vars, From 3368738d67f9901356c2f86dd79c9c97ad57444e Mon Sep 17 00:00:00 2001 From: Patrick LaFontaine <32135464+Pat-Lafon@users.noreply.github.com> Date: Thu, 25 Jul 2024 21:34:17 -0700 Subject: [PATCH 2/3] Add the new label, not the old one for simplicity --- brilirs/src/basic_block.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/brilirs/src/basic_block.rs b/brilirs/src/basic_block.rs index d82ac304..3d17f058 100644 --- a/brilirs/src/basic_block.rs +++ b/brilirs/src/basic_block.rs @@ -212,12 +212,10 @@ impl BBFunction { match instr { bril_rs::Code::Label { label, pos: _ } => { if !curr_block.instrs.is_empty() || curr_block.label.is_some() { - if let Some(old_label) = curr_block.label.as_ref() { - label_map.insert(old_label.to_string(), blocks.len()); - } blocks.push(curr_block); curr_block = BasicBlock::new(); } + label_map.insert(label.to_string(), blocks.len()); curr_block.label = Some(label); } bril_rs::Code::Instruction(i @ bril_rs::Instruction::Effect { op, .. }) @@ -232,9 +230,6 @@ impl BBFunction { func_map, )?); curr_block.instrs.push(i); - if let Some(l) = curr_block.label.as_ref() { - label_map.insert(l.to_string(), blocks.len()); - } blocks.push(curr_block); curr_block = BasicBlock::new(); } @@ -251,9 +246,6 @@ impl BBFunction { } if !curr_block.instrs.is_empty() || curr_block.label.is_some() { - if let Some(l) = curr_block.label.as_ref() { - label_map.insert(l.to_string(), blocks.len()); - } blocks.push(curr_block); } From 1158ba57d7e9dc6bb025f3f0b0bf17b3e4158cda Mon Sep 17 00:00:00 2001 From: Patrick LaFontaine <32135464+Pat-Lafon@users.noreply.github.com> Date: Thu, 25 Jul 2024 22:00:14 -0700 Subject: [PATCH 3/3] Check and error on duplicate labels --- brilirs/src/basic_block.rs | 6 ++++-- brilirs/src/error.rs | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/brilirs/src/basic_block.rs b/brilirs/src/basic_block.rs index 3d17f058..bb38d9c6 100644 --- a/brilirs/src/basic_block.rs +++ b/brilirs/src/basic_block.rs @@ -210,12 +210,14 @@ impl BBFunction { let mut curr_block = BasicBlock::new(); for instr in func.instrs { match instr { - bril_rs::Code::Label { label, pos: _ } => { + bril_rs::Code::Label { label, pos } => { if !curr_block.instrs.is_empty() || curr_block.label.is_some() { blocks.push(curr_block); curr_block = BasicBlock::new(); } - label_map.insert(label.to_string(), blocks.len()); + if label_map.insert(label.to_string(), blocks.len()).is_some() { + return Err(InterpError::DuplicateLabel(label).add_pos(pos)); + } curr_block.label = Some(label); } bril_rs::Code::Instruction(i @ bril_rs::Instruction::Effect { op, .. }) diff --git a/brilirs/src/error.rs b/brilirs/src/error.rs index 7aaa3d03..5d34e010 100644 --- a/brilirs/src/error.rs +++ b/brilirs/src/error.rs @@ -25,6 +25,8 @@ pub enum InterpError { NotOneChar, #[error("multiple functions of the same name found")] DuplicateFunction, + #[error("duplicate label `{0}` found")] + DuplicateLabel(String), #[error("Expected empty return for `{0}`, found value")] NonEmptyRetForFunc(String), #[error("cannot allocate `{0}` entries")]