Skip to content

Commit

Permalink
Merge pull request #331 from Pat-Lafon/main
Browse files Browse the repository at this point in the history
brilirs: error on duplicate labels
  • Loading branch information
sampsyo authored Jul 27, 2024
2 parents f3fc3a6 + 1158ba5 commit 5416bb7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
34 changes: 8 additions & 26 deletions brilirs/src/basic_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,43 +210,28 @@ 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() {
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();
}
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(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,
&mut num_var_map,
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();
}
Expand All @@ -263,9 +248,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);
}

Expand Down
2 changes: 2 additions & 0 deletions brilirs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit 5416bb7

Please sign in to comment.