mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Remove unused labels
This commit is contained in:
@@ -4,6 +4,7 @@ mod kal;
|
||||
mod optimize;
|
||||
mod reduce_instructions;
|
||||
mod remove_meta_lines;
|
||||
mod remove_unused_labels;
|
||||
mod shake_tree;
|
||||
mod simplify;
|
||||
pub mod try_to_kal;
|
||||
|
||||
@@ -5,6 +5,7 @@ use super::collapse_pointers_of_pointers::collapse_pointers_of_pointers;
|
||||
use super::extract_constants::extract_constants;
|
||||
use super::reduce_instructions::reduce_instructions;
|
||||
use super::remove_meta_lines::remove_meta_lines;
|
||||
use super::remove_unused_labels::remove_unused_labels;
|
||||
use super::shake_tree::shake_tree;
|
||||
use super::simplify::simplify;
|
||||
|
||||
@@ -18,6 +19,7 @@ pub fn optimize(module: &mut Module, pointer_allocator: &mut NameAllocator) {
|
||||
}
|
||||
|
||||
remove_meta_lines(module);
|
||||
remove_unused_labels(module);
|
||||
extract_constants(module, pointer_allocator);
|
||||
|
||||
// After possibly repeated optimization, this ensures that the pointers are ordered correctly.
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
use std::{collections::HashSet, mem::take};
|
||||
|
||||
use crate::{
|
||||
asm::{DefinitionContent, FnLine, Function, Module},
|
||||
instruction::InstructionFieldMut,
|
||||
};
|
||||
|
||||
pub fn remove_unused_labels(module: &mut Module) {
|
||||
for defn in &mut module.definitions {
|
||||
if let DefinitionContent::Function(fn_) = &mut defn.content {
|
||||
remove_unused_labels_fn(fn_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_unused_labels_fn(fn_: &mut Function) {
|
||||
let mut used_labels = HashSet::<String>::new();
|
||||
|
||||
for line in &mut fn_.body {
|
||||
match line {
|
||||
FnLine::Instruction(instr) => {
|
||||
instr.visit_fields_mut(&mut |field| match field {
|
||||
InstructionFieldMut::LabelRef(label_ref) => {
|
||||
used_labels.insert(label_ref.name.clone());
|
||||
}
|
||||
InstructionFieldMut::Value(_) | InstructionFieldMut::Register(_) => {}
|
||||
});
|
||||
}
|
||||
FnLine::Label(_) | FnLine::Empty | FnLine::Comment(_) | FnLine::Release(_) => {}
|
||||
}
|
||||
}
|
||||
|
||||
for line in take(&mut fn_.body) {
|
||||
match &line {
|
||||
FnLine::Label(label) => {
|
||||
if !used_labels.contains(&label.name) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
FnLine::Instruction(_) | FnLine::Empty | FnLine::Comment(_) | FnLine::Release(_) => {}
|
||||
}
|
||||
|
||||
fn_.body.push(line);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user