Refactor section placement algorithm

Massively simplified, by not trying to shoehorn differently-behaving
cases into the same one loop!

This may have introduced bugs (the test suite has caught three different
oversights, two of which via external projects, in fact!), but this also
makes the logic clearer and more streamlined, so that we are also less
likely to have any latent or future bugs.

In particular, previously, the first iteration of the loop could attempt
placement at an address not matching the section's constraints,
which made advancing to the next target address unnecessarily complicated
(https://github.com/gbdev/rgbds/pull/2064#discussion_r3985466596),
among other weirdness. The code ended up being defensive, and thus the
overall logic was murky.

I'm also expecting that this should provide a performance improvement due
to being essentially a form of loop-invariant code motion (and very likely
one a compiler couldn't have performed automatically), though I haven't
measured.
This commit is contained in:
ISSOtm
2026-09-18 21:19:37 +02:00
committed by Eldred Habert
parent ce5c8ce8ac
commit 0e41ace1e3
3 changed files with 114 additions and 104 deletions
+8
View File
@@ -0,0 +1,8 @@
SECTION "A", ROM0[$100]
ds $100, $ff
SECTION "C", ROM0[$150]
ds $20, $aa
SECTION "B", ROM0[$250]
ds $20, $bb
+2
View File
@@ -0,0 +1,2 @@
FATAL: Unable to place "C" (ROM0 section) at address $0150: section overlaps with "A"
Linking aborted with 1 error