Skip to content

Commit 8958c4a

Browse files
committed
fix(codegen): fix duplicate labels
1 parent 5ae218b commit 8958c4a

6 files changed

Lines changed: 34 additions & 21 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
tests
66
test.l2
77
output
8+
test.l3

src/backend/codegen.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ _main:
3131

3232
pub struct CodeGenerator {
3333
ir_graphs: Vec<IRGraph>,
34-
jump_label: HashMap<usize, String>,
34+
jump_label: HashMap<(String, BlockIndex), String>,
3535
}
3636

3737
impl CodeGenerator {
@@ -140,7 +140,10 @@ impl CodeGenerator {
140140
registers: &Registers,
141141
) -> String {
142142
let mut code = String::new();
143-
let block_label = self.jump_label.get(&block_index).unwrap();
143+
let block_label = self
144+
.jump_label
145+
.get(&(ir_graph.name().to_string(), block_index))
146+
.unwrap();
144147
code.push_str(&format!("{}:\n", block_label));
145148

146149
for (node_index, node) in block.get_nodes().iter().enumerate() {
@@ -324,7 +327,10 @@ impl CodeGenerator {
324327
node,
325328
);
326329
let following_block_index = jump_information.get(&node_index).unwrap();
327-
let label = self.jump_label.get(following_block_index).unwrap();
330+
let label = self
331+
.jump_label
332+
.get(&(ir_graph.name().to_string(), *following_block_index))
333+
.unwrap();
328334
code.push_str(&self.generate_phi_moves(
329335
block_index,
330336
*following_block_index,
@@ -372,7 +378,7 @@ impl CodeGenerator {
372378
let following_block_index = jump_information.get(&node_index).unwrap();
373379
let jump_label = self
374380
.jump_label
375-
.get(following_block_index)
381+
.get(&(ir_graph.name().to_string(), *following_block_index))
376382
.expect("Expected jump label for false if");
377383
code.push_str(&self.generate_phi_moves(
378384
block_index,
@@ -620,7 +626,10 @@ impl CodeGenerator {
620626
registers: &Registers,
621627
) -> Option<String> {
622628
let mut code = String::new();
623-
let jump_label = self.jump_label.get(&jump_target).unwrap();
629+
let jump_label = self
630+
.jump_label
631+
.get(&(ir_graph.name().to_string(), jump_target))
632+
.unwrap();
624633
match comparision {
625634
Node::Lower(data)
626635
| Node::LowerEquals(data)
@@ -898,20 +907,24 @@ fn move_stack_variable(register: &Box<dyn Register>) -> String {
898907
code
899908
}
900909

901-
fn calculate_jump_label(ir_graphs: &Vec<IRGraph>) -> HashMap<usize, String> {
910+
fn calculate_jump_label(ir_graphs: &Vec<IRGraph>) -> HashMap<(String, BlockIndex), String> {
902911
let mut jump_label = HashMap::new();
903912
for ir_graph in ir_graphs {
904913
for (block_index, block) in ir_graph.get_blocks().iter().enumerate() {
905-
calculate_jump_label_block(block_index, block, &mut jump_label);
914+
calculate_jump_label_block(block_index, ir_graph, block, &mut jump_label);
906915
}
907916
}
908917
jump_label
909918
}
910919

911920
fn calculate_jump_label_block<'a>(
912921
block_index: BlockIndex,
922+
ir_graph: &IRGraph,
913923
_block: &Block,
914-
current: &mut HashMap<usize, String>,
924+
current: &mut HashMap<(String, BlockIndex), String>,
915925
) {
916-
current.insert(block_index, format!("LC{}", block_index));
926+
current.insert(
927+
(ir_graph.name().to_string(), block_index),
928+
format!("LC{}{}", ir_graph.name(), block_index),
929+
);
917930
}

src/ir/ast/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ pub trait ToIR {
5454
}
5555

5656
impl IRConstructor {
57-
pub fn new() -> IRConstructor {
57+
pub fn new(name: String) -> IRConstructor {
5858
IRConstructor {
59-
graph: IRGraph::new(),
59+
graph: IRGraph::new(name),
6060
current_definitions: HashMap::new(),
6161
incomplete_phis: HashMap::new(),
6262
current_side_effect: HashMap::new(),

src/ir/graph.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ pub const START_BLOCK: usize = 0;
1111
pub const END_BLOCK: usize = 1;
1212

1313
pub struct IRGraph {
14+
name: String,
1415
blocks: Vec<Block>,
1516
}
1617

1718
impl IRGraph {
18-
pub fn new() -> IRGraph {
19+
pub fn new(name: String) -> IRGraph {
1920
IRGraph {
21+
name,
2022
blocks: vec![
2123
Block::new("start".to_string()),
2224
Block::new("end".to_string()),
@@ -69,11 +71,15 @@ impl IRGraph {
6971
pub fn end_block_mut(&mut self) -> &mut Block {
7072
self.blocks.get_mut(END_BLOCK).expect("End Block missing!")
7173
}
74+
75+
pub fn name(&self) -> &String {
76+
&self.name
77+
}
7278
}
7379

7480
impl Default for IRGraph {
7581
fn default() -> Self {
76-
Self::new()
82+
Self::new("default".to_string())
7783
}
7884
}
7985

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn main() {
5454

5555
let mut ir_graphs = Vec::new();
5656
for function in program.functions() {
57-
let mut ir_graph = IRConstructor::new();
57+
let mut ir_graph = IRConstructor::new(function.name_tree().name().as_string().to_string());
5858
function.to_ir(&mut ir_graph);
5959
ir_graphs.push(ir_graph.graph());
6060
}

test.l3

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)