1
0
mirror of https://github.com/cc65/cc65.git synced 2025-01-14 00:32:08 +00:00

Fixed a problem with the range check.

git-svn-id: svn://svn.cc65.org/cc65/trunk@5170 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz 2011-08-15 17:19:05 +00:00
parent d02c1cc417
commit cd50385b72

View File

@ -328,6 +328,13 @@ unsigned char GetSegAddrSize (unsigned SegNum)
void SegCheck (void) void SegCheck (void)
/* Check the segments for range and other errors */ /* Check the segments for range and other errors */
{ {
static const unsigned long U_Hi[4] = {
0x000000FFL, 0x0000FFFFL, 0x00FFFFFFL, 0xFFFFFFFFL
};
static const long S_Hi[4] = {
0x0000007FL, 0x00007FFFL, 0x007FFFFFL, 0x7FFFFFFFL
};
unsigned I; unsigned I;
for (I = 0; I < CollCount (&SegmentList); ++I) { for (I = 0; I < CollCount (&SegmentList); ++I) {
Segment* S = CollAtUnchecked (&SegmentList, I); Segment* S = CollAtUnchecked (&SegmentList, I);
@ -346,44 +353,31 @@ void SegCheck (void)
/* Check if the expression is constant */ /* Check if the expression is constant */
if (ED_IsConst (&ED)) { if (ED_IsConst (&ED)) {
/* The expression is constant. Check for range errors. */ long Hi, Lo;
int Abs = (F->Type != FRAG_SEXPR); unsigned J;
long Val = ED.Val;
unsigned I;
if (F->Len == 1) { /* The expression is constant. Check for range errors. */
if (Abs) { CHECK (F->Len <= 4);
/* Absolute value */ if (F->Type == FRAG_SEXPR) {
if (Val > 255) { Hi = S_Hi[F->Len-1];
LIError (&F->LI, "Range error (%ld not in [0..255])", Val); Lo = ~Hi;
}
} else { } else {
/* PC relative value */ Hi = U_Hi[F->Len-1];
if (Val < -128 || Val > 127) { Lo = 0;
LIError (&F->LI, "Range error (%ld not in [-128..127])", Val);
}
}
} else if (F->Len == 2) {
if (Abs) {
/* Absolute value */
if (Val > 65535) {
LIError (&F->LI, "Range error (%ld not in [0..65535])", Val);
}
} else {
/* PC relative value */
if (Val < -32768 || Val > 32767) {
LIError (&F->LI, "Range error (%ld not in [-32768..32767])", Val);
}
} }
if (ED.Val > Hi || ED.Val < Lo) {
LIError (&F->LI,
"Range error (%ld not in [%ld..%ld])",
ED.Val, Lo, Hi);
} }
/* We don't need the expression tree any longer */ /* We don't need the expression tree any longer */
FreeExpr (F->V.Expr); FreeExpr (F->V.Expr);
/* Convert the fragment into a literal fragment */ /* Convert the fragment into a literal fragment */
for (I = 0; I < F->Len; ++I) { for (J = 0; J < F->Len; ++J) {
F->V.Data [I] = Val & 0xFF; F->V.Data[J] = ED.Val & 0xFF;
Val >>= 8; ED.Val >>= 8;
} }
F->Type = FRAG_LITERAL; F->Type = FRAG_LITERAL;