aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mssola@mssola.com>2026-09-29 23:37:48 +0200
committerMiquel Sabaté Solà <mssola@mssola.com>2026-09-29 23:37:48 +0200
commit54e3fc561add5ce9d776a11a286b988aacb59daa (patch)
tree0e1eca3d85988d389e14525c85917646dd8796ac
parent438c379b9ce4c6a6e4da445c3bf7f94458b1c816 (diff)
downloadtools.nes-54e3fc561add5ce9d776a11a286b988aacb59daa.tar.gz
tools.nes-54e3fc561add5ce9d776a11a286b988aacb59daa.zip
nasm: warn on labels that point out of bounds
Signed-off-by: Miquel Sabaté Solà <mssola@mssola.com>
-rw-r--r--lib/xixanta/src/assembler.rs21
-rwxr-xr-xscripts/test-e2e.sh7
-rw-r--r--tests/expected/out.nes0
-rw-r--r--tests/expected/out.txt1
-rw-r--r--tests/out.s12
5 files changed, 39 insertions, 2 deletions
diff --git a/lib/xixanta/src/assembler.rs b/lib/xixanta/src/assembler.rs
index afd8429..6006234 100644
--- a/lib/xixanta/src/assembler.rs
+++ b/lib/xixanta/src/assembler.rs
@@ -918,7 +918,7 @@ impl<'a> Assembler<'a> {
negative: false,
safe: false,
},
- node: None,
+ node: Some(node.clone()),
mapping: self.current_mapping,
segment: self.current_segment,
object_type,
@@ -1459,8 +1459,25 @@ impl<'a> Assembler<'a> {
// badly formatted addresses. This will not happen at
// this point.
let resolved = self.context.resolve_label(&self.mappings, bundle).unwrap();
-
let val = resolved.bundle.value() as usize;
+
+ // If this is an address, is it pointing out of bounds?
+ if matches!(bundle.object_type, ObjectType::Address) {
+ let mapping = &self.mappings[resolved.mapping];
+ let filled_mapping = mapping.start as usize + mapping.offset;
+ if val >= filled_mapping {
+ self.warnings.push(Error {
+ line: resolved.node.as_ref().unwrap().value.line,
+ message: format!("label '{full_name}' is pointing to out of bounds"),
+ source: self.sources[0].clone(),
+ expanded_from: self.macro_context.clone(),
+ global: false,
+ });
+ }
+ }
+
+ // If not, then create a range out of this object and
+ // push the address.
let end = match self.address_ends.get(&full_name) {
Some(v) => v,
None => &val,
diff --git a/scripts/test-e2e.sh b/scripts/test-e2e.sh
index 4df7128..4b9af90 100755
--- a/scripts/test-e2e.sh
+++ b/scripts/test-e2e.sh
@@ -147,6 +147,13 @@ exit_code=$((exit_code + $?))
diff tests/out/global-labels.nes tests/expected/global-labels.nes
exit_code=$((exit_code + $?))
+echo "test: custom => out.nes"
+./target/debug/nasm -c empty -Werror --asan -o tests/out/out.nes tests/out.s 2>tests/out/out.txt
+diff tests/out/out.txt tests/expected/out.txt
+exit_code=$((exit_code + $?))
+diff tests/out/out.nes tests/expected/out.nes
+exit_code=$((exit_code + $?))
+
##
# code.nes
diff --git a/tests/expected/out.nes b/tests/expected/out.nes
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/expected/out.nes
diff --git a/tests/expected/out.txt b/tests/expected/out.txt
new file mode 100644
index 0000000..7f7a94c
--- /dev/null
+++ b/tests/expected/out.txt
@@ -0,0 +1 @@
+error: label 'label' is pointing to out of bounds (out.s: line 12)
diff --git a/tests/out.s b/tests/out.s
new file mode 100644
index 0000000..d40ec42
--- /dev/null
+++ b/tests/out.s
@@ -0,0 +1,12 @@
+.segment "HEADER"
+ .byte 'N', 'E', 'S', $1A
+ .byte $02, $01
+ .byte $00
+ .byte $00
+
+.segment "CODE"
+;; asan:stack full
+
+jmp label
+nop
+label: