From 159a4ec4a64a8a5d791f40eb577afcbe7ee7303e Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 27 Aug 2025 23:38:18 +0200 Subject: nasm: Implement the -s/--stats option MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This option prints further information on how segments are laid out. In particular, for now it prints the amount of space being filled for each segment. Signed-off-by: Miquel Sabaté Solà --- crates/nasm/src/main.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'crates/nasm/src/main.rs') diff --git a/crates/nasm/src/main.rs b/crates/nasm/src/main.rs index be1a386..3864587 100644 --- a/crates/nasm/src/main.rs +++ b/crates/nasm/src/main.rs @@ -14,6 +14,7 @@ struct Args { out: Option, werror: bool, stdout: bool, + stats: bool, defines: Vec<(String, u8)>, } @@ -25,6 +26,7 @@ fn print_help() { println!(" -c, --config \tLinker configuration to be used, whether an identifier or a file path."); println!(" -D (=VALUE)\tDefine an 8-bit variable on the global scope (default: 1)"); println!(" -o, --out \tFile path where the output should be located after execution."); + println!(" -s, --stats\t\tPrint the statistics on how segments have been filled."); println!(" --stdout\t\tPrint the output binary to the standard output."); println!(" -Werror\t\tWarnings should be treated as errors."); std::process::exit(0); @@ -100,6 +102,7 @@ fn parse_arguments() -> Args { } } }, + "-s" | "--stats" => res.stats = true, "--stdout" => match res.out { Some(_) => die("you cannot mix '-o/--out' and '--stdout'".to_string()), None => { @@ -214,5 +217,26 @@ fn main() { } } + if args.stats { + println!("== Statistics ==\n"); + println!("=> Amount of space on each segment (in bytes):\n"); + + for mapping in res.mappings { + let perc = (mapping.offset * 100) as f64 / mapping.size as f64; + + if perc.fract().abs() < f64::EPSILON { + println!( + "{}: {}/{} ({:.0}%)", + mapping.name, mapping.offset, mapping.size, perc + ); + } else { + println!( + "{}: {}/{} ({:.2}%)", + mapping.name, mapping.offset, mapping.size, perc + ); + } + } + } + std::process::exit(error_count); } -- cgit v1.2.3