Update codegen to include depth on positions

This commit is contained in:
Pranav Gaddamadugu 2024-04-18 17:31:00 -07:00
parent 3de4d9ec27
commit efd12d5b09
4 changed files with 14 additions and 4 deletions

View File

@ -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> {
@ -83,6 +85,7 @@ impl<'a> CodeGenerator<'a> {
program,
program_id: None,
next_label: 0u64,
conditional_depth: 0u64,
}
}
}

View File

@ -224,14 +224,15 @@ 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)
}
@ -239,6 +240,9 @@ impl<'a> CodeGenerator<'a> {
}
};
// 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"));
@ -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
}
}

View File

@ -13,6 +13,6 @@ outputs:
destructured_ast: 4d8b69be245b6a2e60293c7ddb538830edbe75ed3b6c28eea0891b6122e15ed1
inlined_ast: 4d8b69be245b6a2e60293c7ddb538830edbe75ed3b6c28eea0891b6122e15ed1
dce_ast: 4d8b69be245b6a2e60293c7ddb538830edbe75ed3b6c28eea0891b6122e15ed1
bytecode: 88e42e3bd227a4dc84b51ace3ef83aa886aacf7f5ab53088667dda8754c8553d
bytecode: 458db8252b38f698e38938e87f24157a1843de705c8bb55537902a6ea32934c9
errors: ""
warnings: ""

View File

@ -13,7 +13,7 @@ outputs:
destructured_ast: 17bb3b101a6fd5936f1eb879e3cbb2938f93f53ae3291ac02a1f5ce66f5532be
inlined_ast: 17bb3b101a6fd5936f1eb879e3cbb2938f93f53ae3291ac02a1f5ce66f5532be
dce_ast: 17bb3b101a6fd5936f1eb879e3cbb2938f93f53ae3291ac02a1f5ce66f5532be
bytecode: 01ee7eef78f58ef8030ff3174b7f5793ff148d0f6ead04ef915c5f6eb2223ca3
bytecode: 1714432c88873553dfc5e23b3097d205011de6a60cae026ff319b139e8b12d7b
errors: ""
warnings: ""
execute: