From dfdb944514296cd85158ab6d738ae58ff5c543c1 Mon Sep 17 00:00:00 2001 From: Yamakaky Date: Fri, 23 Dec 2016 13:58:21 -0500 Subject: [PATCH] --remove-unused. --- CHANGELOG.md | 1 + src/assembler/cleaner.rs | 18 +++++++++++++++--- src/assembler/mod.rs | 2 +- src/bin/assembler.rs | 9 ++++++++- src/bin/disassembler.rs | 3 ++- src/bin/emulator.rs | 3 ++- 6 files changed, 29 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46d52914..6d23ab69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Limited C API - clock v2 support - Feature to use the old device id +- Remove/warn for unused labels ## Changed diff --git a/src/assembler/cleaner.rs b/src/assembler/cleaner.rs index 1c9a0e55..49d7f243 100644 --- a/src/assembler/cleaner.rs +++ b/src/assembler/cleaner.rs @@ -1,5 +1,20 @@ use assembler::types::*; +pub fn print_unused(ast: &[ParsedItem]) { + let used_labels: Vec = ast.iter().flat_map(|i| i.used_labels().into_iter()).collect(); + for item in ast { + match *item { + ParsedItem::LabelDecl(ref l) | + ParsedItem::Directive(Directive::Lcomm(ref l, _))=> { + if !used_labels.contains(&l) { + println!("Unused label: {}", l); + } + } + _ => (), + } + } +} + pub fn clean(ast: Vec) -> Vec { let used_labels: Vec = ast.clone().into_iter().flat_map(|i| i.used_labels().into_iter()).collect(); let mut res = vec![]; @@ -9,9 +24,6 @@ pub fn clean(ast: Vec) -> Vec { &ParsedItem::LabelDecl(ref l) | &ParsedItem::Directive(Directive::Lcomm(ref l, _))=> { keep = used_labels.contains(&l); - if !keep { - println!("Removing {}", l); - } } _ => (), } diff --git a/src/assembler/mod.rs b/src/assembler/mod.rs index 2ffd4a81..16161e63 100644 --- a/src/assembler/mod.rs +++ b/src/assembler/mod.rs @@ -7,4 +7,4 @@ pub mod types; pub use self::preprocessor::preprocess; pub use self::linker::link; pub use self::parser::parse; -pub use self::cleaner::clean; +pub use self::cleaner::{clean, print_unused}; diff --git a/src/bin/assembler.rs b/src/bin/assembler.rs index 48176021..ddbd0a18 100644 --- a/src/bin/assembler.rs +++ b/src/bin/assembler.rs @@ -30,6 +30,7 @@ Options: --no-cpp Disable gcc preprocessor pass. --ast Show the file AST. --hex Show in hexadecimal instead of binary. + --remove-unused Remove unused labels and associated code. --symbols Write the resolved symbols to this file. File to use instead of stdin. -o File to use instead of stdout. @@ -43,6 +44,7 @@ struct Args { flag_no_cpp: bool, flag_ast: bool, flag_hex: bool, + flag_remove_unused: bool, flag_symbols: Option, arg_file: Option, flag_o: Option, @@ -85,7 +87,12 @@ fn main_ret() -> i32 { die!(0, "{:?}", ast); } - let ast = assembler::clean(ast); + let ast = if args.flag_remove_unused { + assembler::clean(ast) + } else { + assembler::print_unused(&ast); + ast + }; let (bin, symbols) = match assembler::link(&ast) { Ok(v) => v, diff --git a/src/bin/disassembler.rs b/src/bin/disassembler.rs index a775b0ba..0f960d01 100644 --- a/src/bin/disassembler.rs +++ b/src/bin/disassembler.rs @@ -43,7 +43,8 @@ struct Args { #[cfg(feature = "bins")] fn main_ret() -> i32 { - simplelog::TermLogger::init(simplelog::LogLevelFilter::Info).unwrap(); + simplelog::TermLogger::init(simplelog::LogLevelFilter::Info, + Default::default()).unwrap(); let version = option_env!("CARGO_PKG_VERSION").map(|s| s.into()); let args: Args = Docopt::new(USAGE) diff --git a/src/bin/emulator.rs b/src/bin/emulator.rs index 26436297..58e0743a 100644 --- a/src/bin/emulator.rs +++ b/src/bin/emulator.rs @@ -61,7 +61,8 @@ struct Args { #[cfg(feature = "bins")] fn main_ret() -> i32 { - simplelog::TermLogger::init(simplelog::LogLevelFilter::Info).unwrap(); + simplelog::TermLogger::init(simplelog::LogLevelFilter::Info, + Default::default()).unwrap(); let version = option_env!("CARGO_PKG_VERSION").map(|s| s.into()); let args: Args = Docopt::new(USAGE)