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 ++++++++++++++++++++++++ lib/xixanta/src/assembler.rs | 9 +++++++++ 2 files changed, 33 insertions(+) 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); } diff --git a/lib/xixanta/src/assembler.rs b/lib/xixanta/src/assembler.rs index c12773b..1d08b57 100644 --- a/lib/xixanta/src/assembler.rs +++ b/lib/xixanta/src/assembler.rs @@ -88,6 +88,7 @@ pub struct AssemblerResult { pub bundles: Vec, pub errors: Vec, pub warnings: Vec, + pub mappings: Vec, } /// Read the contents from the `reader` as a source file and produce a list of @@ -114,6 +115,7 @@ pub fn assemble( source, }], warnings: vec![], + mappings: vec![], }; } }; @@ -143,6 +145,7 @@ pub fn assemble_with_mapping( bundles: vec![], errors, warnings: asm.warnings, + mappings: asm.mappings, }; } @@ -157,6 +160,7 @@ pub fn assemble_with_mapping( bundles: vec![], errors: e.into(), warnings: vec![], + mappings: asm.mappings, }; } } @@ -168,6 +172,7 @@ pub fn assemble_with_mapping( bundles: vec![], errors, warnings: asm.warnings, + mappings: asm.mappings, }; } @@ -180,6 +185,7 @@ pub fn assemble_with_mapping( bundles: vec![], errors, warnings: asm.warnings, + mappings: asm.mappings, }; } @@ -190,6 +196,7 @@ pub fn assemble_with_mapping( bundles: vec![], errors, warnings: asm.warnings, + mappings: asm.mappings, }; } @@ -199,11 +206,13 @@ pub fn assemble_with_mapping( bundles, errors: vec![], warnings: asm.warnings, + mappings: asm.mappings, }, Err(errors) => AssemblerResult { bundles: vec![], errors, warnings: asm.warnings, + mappings: asm.mappings, }, } } -- cgit v1.2.3