Skip to content

Commit

Permalink
--remove-unused.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamakaky committed Dec 23, 2016
1 parent 723d9c3 commit dfdb944
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 15 additions & 3 deletions src/assembler/cleaner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
use assembler::types::*;

pub fn print_unused(ast: &[ParsedItem]) {
let used_labels: Vec<String> = 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<ParsedItem>) -> Vec<ParsedItem> {
let used_labels: Vec<String> = ast.clone().into_iter().flat_map(|i| i.used_labels().into_iter()).collect();
let mut res = vec![];
Expand All @@ -9,9 +24,6 @@ pub fn clean(ast: Vec<ParsedItem>) -> Vec<ParsedItem> {
&ParsedItem::LabelDecl(ref l) |
&ParsedItem::Directive(Directive::Lcomm(ref l, _))=> {
keep = used_labels.contains(&l);
if !keep {
println!("Removing {}", l);
}
}
_ => (),
}
Expand Down
2 changes: 1 addition & 1 deletion src/assembler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
9 changes: 8 additions & 1 deletion src/bin/assembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <f> Write the resolved symbols to this file.
<file> File to use instead of stdin.
-o <file> File to use instead of stdout.
Expand All @@ -43,6 +44,7 @@ struct Args {
flag_no_cpp: bool,
flag_ast: bool,
flag_hex: bool,
flag_remove_unused: bool,
flag_symbols: Option<String>,
arg_file: Option<String>,
flag_o: Option<String>,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/bin/disassembler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/bin/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dfdb944

Please sign in to comment.