Skip to content

Commit

Permalink
Unused label removal is now one step.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrueger committed Jan 19, 2025
1 parent 592be7f commit 0c35fc0
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 13 deletions.
3 changes: 3 additions & 0 deletions crates/icy_board_engine/src/decompiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::collections::{HashMap, HashSet};

use reconstruct::strip_unused_labels;

use crate::{
ast::{
Ast, AstNode, BinOp, BinaryExpression, BlockStatement, CommentAstNode, Constant, ConstantExpression, Expression, FunctionCallExpression,
Expand Down Expand Up @@ -531,6 +533,7 @@ pub fn decompile(executable: Executable, raw: bool) -> Res<(Ast, Vec<DecompilerI
}
}
ast = reconstruct::finish_ast(&mut ast);
ast = strip_unused_labels(&mut ast);
ast = relabel_visitor::relabel_ast(&mut ast);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub fn scan_for_next(statements: &mut Vec<Statement>) {
};
for_block.pop();
let continue_label = continue_label_stmt.get_label().clone();
super::reconstruct_block(&mut for_block);
super::optimize_block(&mut for_block);
super::handle_break_continue(skip_label, continue_label, &mut for_block);
if step_expr.to_string() == "1" {
statements.insert(i, ForStatement::create_empty_statement(var_name, from_expr, to_expr, None, for_block));
Expand Down
5 changes: 2 additions & 3 deletions crates/icy_board_engine/src/decompiler/reconstruct/if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ ENDIF
*/

pub fn scan_if_else(statements: &mut Vec<Statement>) {
super::strip_unused_labels(statements);
let mut i = 0;
if statements.len() < 1 {
return;
}
while i < statements.len() - 1 {
let start = i;
if let Statement::Empty = statements[i] {
if let Statement::Label(_) = statements[i] {
i += 1;
continue;
}
Expand All @@ -91,7 +90,7 @@ pub fn scan_if_else(statements: &mut Vec<Statement>) {
let mut j = i + 1;
let mut has_else = true;
while j < statements.len() - 1 {
if let Statement::Empty = statements[j] {
if let Statement::Label(_) = statements[j] {
j += 1;
continue;
}
Expand Down
11 changes: 3 additions & 8 deletions crates/icy_board_engine/src/decompiler/reconstruct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ fn optimize_block(statements: &mut Vec<Statement>) {
optimize_loops(statements);
optimize_ifs(statements);
scan_select_statements(statements);
strip_unused_labels(statements);
}

fn optimize_ifs(statements: &mut Vec<Statement>) {
Expand Down Expand Up @@ -112,16 +111,12 @@ fn get_label_index(statements: &[Statement], from: i32, to: i32, label: &String)
None
}

pub fn strip_unused_labels(statements: &mut Vec<Statement>) {
pub fn strip_unused_labels(ast: &mut Ast) -> Ast {
let mut visitor = unused_label_visitor::UnusedLabelVisitor::default();
for stmt in statements.clone() {
stmt.visit(&mut visitor);
}
ast.visit(&mut visitor);
let unused_labels = visitor.get_unused_labels();
let mut visitor = remove_label_visitor::RemoveLabelVisitor::new(unused_labels.clone());
for stmt in statements {
*stmt = stmt.visit_mut(&mut visitor);
}
ast.visit_mut(&mut visitor)
}

#[must_use]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ pub fn scan_do_while(statements: &mut Vec<Statement>) {

// reconstruct while…do block
let mut while_block = statements.drain((i + 2)..matching_goto as usize).collect();

statements.drain(i + 1..i + 3);
let continue_label = super::get_last_label(&statements[i..i + 1]);
super::handle_break_continue(break_label, continue_label, statements);
super::handle_break_continue(break_label, continue_label, &mut while_block);
optimize_block(&mut while_block);

if while_block.len() == 1 {
Expand Down

0 comments on commit 0c35fc0

Please sign in to comment.