mirror of
https://github.com/gbdev/rgbds.git
synced 2026-01-21 07:51:51 +00:00
Refactor to combine similar functions into one
This commit is contained in:
@@ -25,7 +25,12 @@ void sect_ForEach(void (*callback)(Section &)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void checkAgainstFixedAddress(Section const &target, Section const &other, uint16_t org) {
|
static void checkPieceCompat(Section &target, Section const &other, size_t delta) {
|
||||||
|
assume(other.modifier != SECTION_UNION || delta == 0);
|
||||||
|
|
||||||
|
if (other.isAddressFixed) {
|
||||||
|
uint16_t org = other.org - delta;
|
||||||
|
|
||||||
if (target.isAddressFixed) {
|
if (target.isAddressFixed) {
|
||||||
if (target.org != org) {
|
if (target.org != org) {
|
||||||
fatalTwoAt(
|
fatalTwoAt(
|
||||||
@@ -52,9 +57,12 @@ static void checkAgainstFixedAddress(Section const &target, Section const &other
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static bool checkAgainstFixedAlign(Section const &target, Section const &other, uint32_t ofs) {
|
target.isAddressFixed = true;
|
||||||
|
target.org = org;
|
||||||
|
} else if (other.isAlignFixed) {
|
||||||
|
uint32_t ofs = (other.alignOfs - delta) & other.alignMask;
|
||||||
|
|
||||||
if (target.isAddressFixed) {
|
if (target.isAddressFixed) {
|
||||||
if ((target.org - ofs) & other.alignMask) {
|
if ((target.org - ofs) & other.alignMask) {
|
||||||
fatalTwoAt(
|
fatalTwoAt(
|
||||||
@@ -68,7 +76,6 @@ static bool checkAgainstFixedAlign(Section const &target, Section const &other,
|
|||||||
other.alignOfs
|
other.alignOfs
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
} else if (target.isAlignFixed
|
} else if (target.isAlignFixed
|
||||||
&& (other.alignMask & target.alignOfs) != (target.alignMask & ofs)) {
|
&& (other.alignMask & target.alignOfs) != (target.alignMask & ofs)) {
|
||||||
fatalTwoAt(
|
fatalTwoAt(
|
||||||
@@ -82,34 +89,7 @@ static bool checkAgainstFixedAlign(Section const &target, Section const &other,
|
|||||||
other.alignMask + 1,
|
other.alignMask + 1,
|
||||||
other.alignOfs
|
other.alignOfs
|
||||||
);
|
);
|
||||||
} else {
|
} else if (!target.isAlignFixed || other.alignMask > target.alignMask) {
|
||||||
return !target.isAlignFixed || (other.alignMask > target.alignMask);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void checkSectUnionCompat(Section &target, Section &other) {
|
|
||||||
if (other.isAddressFixed) {
|
|
||||||
checkAgainstFixedAddress(target, other, other.org);
|
|
||||||
target.isAddressFixed = true;
|
|
||||||
target.org = other.org;
|
|
||||||
} else if (other.isAlignFixed) {
|
|
||||||
if (checkAgainstFixedAlign(target, other, other.alignOfs)) {
|
|
||||||
target.isAlignFixed = true;
|
|
||||||
target.alignMask = other.alignMask;
|
|
||||||
target.alignOfs = other.alignOfs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void checkFragmentCompat(Section &target, Section &other) {
|
|
||||||
if (other.isAddressFixed) {
|
|
||||||
uint16_t org = other.org - target.size;
|
|
||||||
checkAgainstFixedAddress(target, other, org);
|
|
||||||
target.isAddressFixed = true;
|
|
||||||
target.org = org;
|
|
||||||
} else if (other.isAlignFixed) {
|
|
||||||
uint32_t ofs = (other.alignOfs - target.size) & other.alignMask;
|
|
||||||
if (checkAgainstFixedAlign(target, other, ofs)) {
|
|
||||||
target.isAlignFixed = true;
|
target.isAlignFixed = true;
|
||||||
target.alignMask = other.alignMask;
|
target.alignMask = other.alignMask;
|
||||||
target.alignOfs = ofs;
|
target.alignOfs = ofs;
|
||||||
@@ -158,14 +138,14 @@ static void mergeSections(Section &target, std::unique_ptr<Section> &&other) {
|
|||||||
|
|
||||||
switch (other->modifier) {
|
switch (other->modifier) {
|
||||||
case SECTION_UNION:
|
case SECTION_UNION:
|
||||||
checkSectUnionCompat(target, *other);
|
checkPieceCompat(target, *other, 0);
|
||||||
if (other->size > target.size) {
|
if (other->size > target.size) {
|
||||||
target.size = other->size;
|
target.size = other->size;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SECTION_FRAGMENT:
|
case SECTION_FRAGMENT:
|
||||||
checkFragmentCompat(target, *other);
|
checkPieceCompat(target, *other, target.size);
|
||||||
// Append `other` to `target`
|
// Append `other` to `target`
|
||||||
other->offset = target.size;
|
other->offset = target.size;
|
||||||
target.size += other->size;
|
target.size += other->size;
|
||||||
|
|||||||
Reference in New Issue
Block a user