mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Factor out common sanity checks for section union and fragment
This commit is contained in:
@@ -18,10 +18,9 @@ void sect_ForEach(void (*callback)(Section &)) {
|
|||||||
callback(*it->get());
|
callback(*it->get());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void checkSectUnionCompat(Section &target, Section &other) {
|
static void checkAgainstFixedAddress(Section const &target, Section const &other, uint16_t org) {
|
||||||
if (other.isAddressFixed) {
|
|
||||||
if (target.isAddressFixed) {
|
if (target.isAddressFixed) {
|
||||||
if (target.org != other.org)
|
if (target.org != org) {
|
||||||
errx(
|
errx(
|
||||||
"Section \"%s\" is defined with conflicting addresses $%04" PRIx16
|
"Section \"%s\" is defined with conflicting addresses $%04" PRIx16
|
||||||
" and $%04" PRIx16,
|
" and $%04" PRIx16,
|
||||||
@@ -29,8 +28,9 @@ static void checkSectUnionCompat(Section &target, Section &other) {
|
|||||||
target.org,
|
target.org,
|
||||||
other.org
|
other.org
|
||||||
);
|
);
|
||||||
|
}
|
||||||
} else if (target.isAlignFixed) {
|
} else if (target.isAlignFixed) {
|
||||||
if ((other.org - target.alignOfs) & target.alignMask)
|
if ((org - target.alignOfs) & target.alignMask) {
|
||||||
errx(
|
errx(
|
||||||
"Section \"%s\" is defined with conflicting %d-byte alignment (offset %" PRIu16
|
"Section \"%s\" is defined with conflicting %d-byte alignment (offset %" PRIu16
|
||||||
") and address $%04" PRIx16,
|
") and address $%04" PRIx16,
|
||||||
@@ -40,12 +40,12 @@ static void checkSectUnionCompat(Section &target, Section &other) {
|
|||||||
other.org
|
other.org
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
target.isAddressFixed = true;
|
}
|
||||||
target.org = other.org;
|
}
|
||||||
|
|
||||||
} else if (other.isAlignFixed) {
|
static bool checkAgainstFixedAlign(Section const &target, Section const &other, int32_t ofs) {
|
||||||
if (target.isAddressFixed) {
|
if (target.isAddressFixed) {
|
||||||
if ((target.org - other.alignOfs) & other.alignMask)
|
if ((target.org - ofs) & other.alignMask) {
|
||||||
errx(
|
errx(
|
||||||
"Section \"%s\" is defined with conflicting address $%04" PRIx16
|
"Section \"%s\" is defined with conflicting address $%04" PRIx16
|
||||||
" and %d-byte alignment (offset %" PRIu16 ")",
|
" and %d-byte alignment (offset %" PRIu16 ")",
|
||||||
@@ -54,8 +54,10 @@ static void checkSectUnionCompat(Section &target, Section &other) {
|
|||||||
other.alignMask + 1,
|
other.alignMask + 1,
|
||||||
other.alignOfs
|
other.alignOfs
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
} else if (target.isAlignFixed
|
} else if (target.isAlignFixed
|
||||||
&& (other.alignMask & target.alignOfs) != (target.alignMask & other.alignOfs)) {
|
&& (other.alignMask & target.alignOfs) != (target.alignMask & ofs)) {
|
||||||
errx(
|
errx(
|
||||||
"Section \"%s\" is defined with conflicting %d-byte alignment (offset %" PRIu16
|
"Section \"%s\" is defined with conflicting %d-byte alignment (offset %" PRIu16
|
||||||
") and %d-byte alignment (offset %" PRIu16 ")",
|
") and %d-byte alignment (offset %" PRIu16 ")",
|
||||||
@@ -65,7 +67,18 @@ static void checkSectUnionCompat(Section &target, Section &other) {
|
|||||||
other.alignMask + 1,
|
other.alignMask + 1,
|
||||||
other.alignOfs
|
other.alignOfs
|
||||||
);
|
);
|
||||||
} else if (!target.isAlignFixed || (other.alignMask > target.alignMask)) {
|
} else {
|
||||||
|
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.isAlignFixed = true;
|
||||||
target.alignMask = other.alignMask;
|
target.alignMask = other.alignMask;
|
||||||
}
|
}
|
||||||
@@ -75,60 +88,14 @@ static void checkSectUnionCompat(Section &target, Section &other) {
|
|||||||
static void checkFragmentCompat(Section &target, Section &other) {
|
static void checkFragmentCompat(Section &target, Section &other) {
|
||||||
if (other.isAddressFixed) {
|
if (other.isAddressFixed) {
|
||||||
uint16_t org = other.org - target.size;
|
uint16_t org = other.org - target.size;
|
||||||
|
checkAgainstFixedAddress(target, other, org);
|
||||||
if (target.isAddressFixed) {
|
|
||||||
if (target.org != org)
|
|
||||||
errx(
|
|
||||||
"Section \"%s\" is defined with conflicting addresses $%04" PRIx16
|
|
||||||
" and $%04" PRIx16,
|
|
||||||
other.name.c_str(),
|
|
||||||
target.org,
|
|
||||||
other.org
|
|
||||||
);
|
|
||||||
|
|
||||||
} else if (target.isAlignFixed) {
|
|
||||||
if ((org - target.alignOfs) & target.alignMask)
|
|
||||||
errx(
|
|
||||||
"Section \"%s\" is defined with conflicting %d-byte alignment (offset %" PRIu16
|
|
||||||
") and address $%04" PRIx16,
|
|
||||||
other.name.c_str(),
|
|
||||||
target.alignMask + 1,
|
|
||||||
target.alignOfs,
|
|
||||||
other.org
|
|
||||||
);
|
|
||||||
}
|
|
||||||
target.isAddressFixed = true;
|
target.isAddressFixed = true;
|
||||||
target.org = org;
|
target.org = org;
|
||||||
|
|
||||||
} else if (other.isAlignFixed) {
|
} else if (other.isAlignFixed) {
|
||||||
int32_t ofs = (other.alignOfs - target.size) % (other.alignMask + 1);
|
int32_t ofs = (other.alignOfs - target.size) % (other.alignMask + 1);
|
||||||
|
|
||||||
if (ofs < 0)
|
if (ofs < 0)
|
||||||
ofs += other.alignMask + 1;
|
ofs += other.alignMask + 1;
|
||||||
|
if (checkAgainstFixedAlign(target, other, ofs)) {
|
||||||
if (target.isAddressFixed) {
|
|
||||||
if ((target.org - ofs) & other.alignMask)
|
|
||||||
errx(
|
|
||||||
"Section \"%s\" is defined with conflicting address $%04" PRIx16
|
|
||||||
" and %d-byte alignment (offset %" PRIu16 ")",
|
|
||||||
other.name.c_str(),
|
|
||||||
target.org,
|
|
||||||
other.alignMask + 1,
|
|
||||||
other.alignOfs
|
|
||||||
);
|
|
||||||
|
|
||||||
} else if (target.isAlignFixed && (other.alignMask & target.alignOfs) != (target.alignMask & ofs)) {
|
|
||||||
errx(
|
|
||||||
"Section \"%s\" is defined with conflicting %d-byte alignment (offset %" PRIu16
|
|
||||||
") and %d-byte alignment (offset %" PRIu16 ")",
|
|
||||||
other.name.c_str(),
|
|
||||||
target.alignMask + 1,
|
|
||||||
target.alignOfs,
|
|
||||||
other.alignMask + 1,
|
|
||||||
other.alignOfs
|
|
||||||
);
|
|
||||||
|
|
||||||
} else if (!target.isAlignFixed || (other.alignMask > target.alignMask)) {
|
|
||||||
target.isAlignFixed = true;
|
target.isAlignFixed = true;
|
||||||
target.alignMask = other.alignMask;
|
target.alignMask = other.alignMask;
|
||||||
target.alignOfs = ofs;
|
target.alignOfs = ofs;
|
||||||
|
|||||||
Reference in New Issue
Block a user