Skip to content

Commit

Permalink
Remove redundant ends, fix next_instruction_index
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jul 3, 2023
1 parent 07e2996 commit 0366d09
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions valuescript_compiler/src/optimization/simplify_jumps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ fn simplify_jumps_fn(fn_: &mut Function) {
let mut substitutions = HashMap::<usize, FnLine>::new();

for i in 0..fn_.body.len() {
if let FnLine::Instruction(Instruction::End) = &fn_.body[i] {
if next_instruction_index(&fn_.body, i) == None {
// Remove `end` instructions when we're already at the end of the function.
substitutions.insert(i, FnLine::Comment(fn_.body[i].to_string()));
continue;
}
}

let (conditional, label_ref) = match &fn_.body[i] {
FnLine::Instruction(instr) => match instr {
Instruction::Jmp(label_ref) => (false, label_ref),
Expand Down Expand Up @@ -80,6 +88,8 @@ fn simplify_jumps_fn(fn_: &mut Function) {
}

fn next_instruction_index(body: &Vec<FnLine>, mut i: usize) -> Option<usize> {
i += 1;

while i < body.len() {
match &body[i] {
FnLine::Instruction(_) => return Some(i),
Expand Down

0 comments on commit 0366d09

Please sign in to comment.