Skip to content

Commit

Permalink
Update codegen to include depth on positions
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Apr 19, 2024
1 parent 3de4d9e commit efd12d5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions compiler/passes/src/code_generation/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub struct CodeGenerator<'a> {
pub(crate) program_id: Option<ProgramId>,
/// A counter to track the next available label.
pub(crate) next_label: u64,
/// The depth of the current conditional block.
pub(crate) conditional_depth: u64,
}

impl<'a> CodeGenerator<'a> {
Expand Down Expand Up @@ -83,6 +85,7 @@ impl<'a> CodeGenerator<'a> {
program,
program_id: None,
next_label: 0u64,
conditional_depth: 0u64,
}
}
}
11 changes: 9 additions & 2 deletions compiler/passes/src/code_generation/visit_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,21 +224,25 @@ impl<'a> CodeGenerator<'a> {
unreachable!("`ConditionalStatement`s should not be in the AST at this phase of compilation.")
} else {
// Construct a label for the end of the `then` block.
let end_then_label = format!("end_then_{}", self.next_label);
let end_then_label = format!("end_then_{}_{}", self.conditional_depth, self.next_label);
self.next_label += 1;
// Construct a label for the end of the `otherwise` block if it exists.
let (has_otherwise, end_otherwise_label) = {
match _input.otherwise.is_some() {
true => {
// Construct a label for the end of the `otherwise` block.
let end_otherwise_label = { format!("end_otherwise_{}", self.next_label) };
let end_otherwise_label =
{ format!("end_otherwise_{}_{}", self.conditional_depth, self.next_label) };
self.next_label += 1;
(true, end_otherwise_label)
}
false => (false, String::new()),
}
};

// Increment the conditional depth.
self.conditional_depth += 1;

// Create a `branch` instruction.
let (condition, mut instructions) = self.visit_expression(&_input.condition);
instructions.push_str(&format!(" branch.eq {condition} false to {end_then_label};\n"));
Expand All @@ -261,6 +265,9 @@ impl<'a> CodeGenerator<'a> {
instructions.push_str(&format!(" position {end_otherwise_label};\n"));
}

// Decrement the conditional depth.
self.conditional_depth -= 1;

instructions
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ outputs:
destructured_ast: 4d8b69be245b6a2e60293c7ddb538830edbe75ed3b6c28eea0891b6122e15ed1
inlined_ast: 4d8b69be245b6a2e60293c7ddb538830edbe75ed3b6c28eea0891b6122e15ed1
dce_ast: 4d8b69be245b6a2e60293c7ddb538830edbe75ed3b6c28eea0891b6122e15ed1
bytecode: 88e42e3bd227a4dc84b51ace3ef83aa886aacf7f5ab53088667dda8754c8553d
bytecode: 458db8252b38f698e38938e87f24157a1843de705c8bb55537902a6ea32934c9
errors: ""
warnings: ""
2 changes: 1 addition & 1 deletion tests/expectations/execution/cond_exec_in_finalize.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ outputs:
destructured_ast: 17bb3b101a6fd5936f1eb879e3cbb2938f93f53ae3291ac02a1f5ce66f5532be
inlined_ast: 17bb3b101a6fd5936f1eb879e3cbb2938f93f53ae3291ac02a1f5ce66f5532be
dce_ast: 17bb3b101a6fd5936f1eb879e3cbb2938f93f53ae3291ac02a1f5ce66f5532be
bytecode: 01ee7eef78f58ef8030ff3174b7f5793ff148d0f6ead04ef915c5f6eb2223ca3
bytecode: 1714432c88873553dfc5e23b3097d205011de6a60cae026ff319b139e8b12d7b
errors: ""
warnings: ""
execute:
Expand Down

0 comments on commit efd12d5

Please sign in to comment.