Skip to content

Commit 202c914

Browse files
Rangi42ISSOtm
authored andcommitted
Fix alignment compatibility with current lower alignment
1 parent e14f68d commit 202c914

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/asm/section.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -583,30 +583,33 @@ void sect_AlignPC(uint8_t alignment, uint16_t offset) {
583583
actualOffset
584584
);
585585
}
586-
} else if (uint32_t actualOffset =
587-
((sect->alignOfs + curOffset) % (1u << sect->align)) % alignSize;
588-
sect->align != 0 && actualOffset != offset) {
589-
error(
590-
"Section is misaligned ($%04" PRIx32 " bytes into the section, expected ALIGN[%" PRIu32
591-
", %" PRIu32 "], got ALIGN[%" PRIu32 ", %" PRIu32 "])\n",
592-
curOffset,
593-
alignment,
594-
offset,
595-
alignment,
596-
actualOffset
597-
);
598-
} else if (alignment >= 16) {
599-
// Treat an alignment large enough as fixing the address.
600-
// Note that this also ensures that a section's alignment never becomes 16 or greater.
601-
if (alignment > 16) {
602-
error("Alignment must be between 0 and 16, not %u\n", alignment);
586+
} else {
587+
if (uint32_t actualOffset = (sect->alignOfs + curOffset) % alignSize,
588+
sectAlignSize = 1 << sect->align;
589+
sect->align != 0 && actualOffset % sectAlignSize != offset % sectAlignSize) {
590+
error(
591+
"Section is misaligned ($%04" PRIx32
592+
" bytes into the section, expected ALIGN[%" PRIu32 ", %" PRIu32
593+
"], got ALIGN[%" PRIu32 ", %" PRIu32 "])\n",
594+
curOffset,
595+
alignment,
596+
offset,
597+
alignment,
598+
actualOffset
599+
);
600+
} else if (alignment >= 16) {
601+
// Treat an alignment large enough as fixing the address.
602+
// Note that this also ensures that a section's alignment never becomes 16 or greater.
603+
if (alignment > 16) {
604+
error("Alignment must be between 0 and 16, not %u\n", alignment);
605+
}
606+
sect->align = 0; // Reset the alignment, since we're fixing the address.
607+
sect->org = offset - curOffset;
608+
} else if (alignment > sect->align) {
609+
sect->align = alignment;
610+
// We need `(sect->alignOfs + curOffset) % alignSize == offset`
611+
sect->alignOfs = (offset - curOffset) % alignSize;
603612
}
604-
sect->align = 0; // Reset the alignment, since we're fixing the address.
605-
sect->org = offset - curOffset;
606-
} else if (alignment > sect->align) {
607-
sect->align = alignment;
608-
// We need `(sect->alignOfs + curOffset) % alignSize == offset`
609-
sect->alignOfs = (offset - curOffset) % alignSize;
610613
}
611614
}
612615

test/asm/align-increasing.asm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SECTION "test1", ROM0
2+
align 4 ; PC = $xxx0
3+
dw $0123
4+
align 4, 2 ; PC = $xxx2
5+
ds align [8, $C2] ; PC = $xxC2 (ds 0)
6+
align 8, $C2 ; PC = $xxC2
7+
dw $4567
8+
9+
SECTION "test2", ROM0[$01C0]
10+
align 4 ; PC = $01C0
11+
dw $89ab
12+
align 4, 2 ; PC = $01C2
13+
ds align [8, $C2] ; PC = $01C2 (ds 0)
14+
align 8, $C2 ; PC = $01C2
15+
dw $cdef

test/asm/align-increasing.out.bin

452 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)