Treat signed int bit-fields as signed

Prior to this PR, `int`, `signed int`, and `unsigned int`
bitfields are all treated as `unsigned int`.

With this PR, `signed int` will be treated as `signed int`,
and the others remain unsigned.

Since `Type` does not distinguish between `int` and `signed int`,
add an extra `int* SignenessSpecified` param to `ParseTypeSpec`
so we can tell these apart for bit-fields and treat plain `int : N`
as `unsigned int : N` since it is more efficient to zero-extend
than sign-extend.

Fixes #1095
This commit is contained in:
Jesse Rosenstock
2020-07-19 22:59:44 +02:00
committed by Oliver Schmidt
parent 55cebc7b9e
commit ff535b8e1a
7 changed files with 218 additions and 40 deletions

View File

@@ -31,7 +31,10 @@ static struct signed_ints {
signed int b : 3;
signed int c : 3;
signed int d : 10;
} si = {-4, -1, 3, -500};
signed int : 0;
signed int e : 8;
signed int f : 16;
} si = {-4, -1, 3, -500, -100, -5000};
static void test_signed_bitfield(void)
{
@@ -53,7 +56,7 @@ static void test_signed_bitfield(void)
failures++;
}
if (si.b <= 0) {
if (si.c <= 0) {
printf("Got si.c = %d, expected positive.\n", si.c);
failures++;
}
@@ -71,10 +74,30 @@ static void test_signed_bitfield(void)
failures++;
}
if (si.e >= 0) {
printf("Got si.e = %d, expected negative.\n", si.e);
failures++;
}
if (si.e != -100) {
printf("Got si.e = %d, expected -100.\n", si.e);
failures++;
}
if (si.f >= 0) {
printf("Got si.f = %d, expected negative.\n", si.f);
failures++;
}
if (si.f != -5000) {
printf("Got si.f = %d, expected -5000.\n", si.f);
failures++;
}
si.a = -3;
si.b = 1;
si.c = -2;
si.d = 500;
si.e = 100;
si.f = 5000;
if (si.a >= 0) {
printf("Got si.a = %d, expected negative.\n", si.a);
@@ -94,7 +117,7 @@ static void test_signed_bitfield(void)
failures++;
}
if (si.b >= 0) {
if (si.c >= 0) {
printf("Got si.c = %d, expected negative.\n", si.c);
failures++;
}
@@ -111,6 +134,24 @@ static void test_signed_bitfield(void)
printf("Got si.d = %d, expected 500.\n", si.d);
failures++;
}
if (si.e <= 0) {
printf("Got si.e = %d, expected positive.\n", si.e);
failures++;
}
if (si.e != 100) {
printf("Got si.e = %d, expected 100.\n", si.e);
failures++;
}
if (si.f <= 0) {
printf("Got si.f = %d, expected positive.\n", si.f);
failures++;
}
if (si.f != 5000) {
printf("Got si.f = %d, expected 5000.\n", si.f);
failures++;
}
}
int main(void)