From 60b6624ae2b28ebe9fb30c2aa6115e4d5c1ab436 Mon Sep 17 00:00:00 2001 From: xleroy Date: Sat, 26 Nov 2011 15:40:57 +0000 Subject: cparser/*: refactoring of the expansion of read-modify-write operators cparser/PackedStructs: treat r-m-w operations over byte-swapped fields cparser/PackedStructs: allow static initialization of packed structs test/regression: more packedstruct tests git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@1738 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e --- test/regression/Makefile | 2 +- test/regression/Results/packedstruct1 | 5 ++- test/regression/Results/packedstruct2 | 6 +++ test/regression/packedstruct1.c | 9 ++++- test/regression/packedstruct2.c | 69 +++++++++++++++++++++++++++++++++++ 5 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 test/regression/Results/packedstruct2 create mode 100644 test/regression/packedstruct2.c (limited to 'test/regression') diff --git a/test/regression/Makefile b/test/regression/Makefile index 103901ed..044e5936 100644 --- a/test/regression/Makefile +++ b/test/regression/Makefile @@ -11,7 +11,7 @@ TESTS=attribs1 bitfields1 bitfields2 bitfields3 bitfields4 \ bitfields5 bitfields6 bitfields7 \ expr1 expr6 initializers volatile1 volatile2 volatile3 \ funct3 expr5 struct7 struct8 struct11 casts1 casts2 char1 \ - sizeof1 sizeof2 packedstruct1 + sizeof1 sizeof2 packedstruct1 packedstruct2 # Other tests: should compile to .s without errors (but expect warnings) EXTRAS=annot1 commaprec expr2 expr3 expr4 extern1 funct2 funptr1 init1 \ diff --git a/test/regression/Results/packedstruct1 b/test/regression/Results/packedstruct1 index 75491328..0595cc38 100644 --- a/test/regression/Results/packedstruct1 +++ b/test/regression/Results/packedstruct1 @@ -7,8 +7,9 @@ sizeof(struct s2) = 16 offsetof(x) = 0, offsetof(y) = 2, offsetof(z) = 6 s2 = {x = 57, y = -456, z = 3.14159} -sizeof(struct s3) = 29 -s3 = {x = 123, y = 45678, z = 2147483649, v = -456, w = -1234567, p is ok, t = {111,222,333}} +sizeof(struct s3) = 31 +offsetof(s) = 29 +s3 = {x = 123, y = 45678, z = 2147483649, v = -456, w = -1234567, p is ok, t = {111,222,333}, s = {'o','k'}} sizeof(struct s4) = 16 offsetof(x) = 0, offsetof(y) = 4, offsetof(z) = 8 diff --git a/test/regression/Results/packedstruct2 b/test/regression/Results/packedstruct2 new file mode 100644 index 00000000..360a5c6c --- /dev/null +++ b/test/regression/Results/packedstruct2 @@ -0,0 +1,6 @@ +s1 = {x = 2345, y = -12345678, z = 'x'} + +s3 = {x = 42, y = 123, z = 456789, v = -333, w = -314159, p is ok, t = {111,222,333}, s = {'o','k'}} + +s4 = {x = 123, y = -456789, z = 3.14159} + diff --git a/test/regression/packedstruct1.c b/test/regression/packedstruct1.c index cecd1f30..66c8c9e1 100644 --- a/test/regression/packedstruct1.c +++ b/test/regression/packedstruct1.c @@ -52,6 +52,7 @@ struct s3 { signed int w; char * p; unsigned int t[3]; + unsigned char s[2]; }; struct s3 s3; @@ -61,6 +62,7 @@ void test3(void) char xx; printf("sizeof(struct s3) = %d\n", sizeof(struct s3)); + printf("offsetof(s) = %d\n", offsetof(s3,s)); s3.x = 123; s3.y = 45678; s3.z = 0x80000001U; @@ -70,10 +72,13 @@ void test3(void) s3.t[0] = 111; s3.t[1] = 222; s3.t[2] = 333; - printf("s3 = {x = %u, y = %u, z = %u, v = %d, w = %d, p is %s, t = {%d,%d,%d}}\n\n", + s3.s[0] = 'o'; + s3.s[1] = 'k'; + printf("s3 = {x = %u, y = %u, z = %u, v = %d, w = %d, p is %s, t = {%d,%d,%d}, s = {'%c','%c'}}\n\n", s3.x, s3.y, s3.z, s3.v, s3.w, (s3.p == &xx ? "ok" : "BAD"), - s3.t[0], s3.t[1], s3.t[2]); + s3.t[0], s3.t[1], s3.t[2], + s3.s[0], s3.s[1]); } /* Back to normal */ diff --git a/test/regression/packedstruct2.c b/test/regression/packedstruct2.c new file mode 100644 index 00000000..0c383a47 --- /dev/null +++ b/test/regression/packedstruct2.c @@ -0,0 +1,69 @@ +/* Initialization of packed structs */ + +#include + +/* Simple packing */ + +#pragma pack(1) + +struct s1 { unsigned short x; int y; char z; }; + +struct s1 s1 = { 2345, -12345678, 'x' }; + +void test1(void) +{ + printf("s1 = {x = %d, y = %d, z = '%c'}\n\n", s1.x, s1.y, s1.z); +} + +/* Now with byte-swapped fields */ + +#pragma pack(1,1,1) + +struct s3 { + unsigned char x; + unsigned short y; + unsigned int z; + signed short v; + signed int w; + char * p; + unsigned int t[3]; + unsigned char s[2]; +}; + +struct s3 s3 = { + 42, 123, 456789, -333, -314159, 0, + { 111, 222, 333 }, + { 'o', 'k' } +}; + +void test3(void) +{ + printf("s3 = {x = %u, y = %u, z = %u, v = %d, w = %d, p is %s, t = {%d,%d,%d}, s = {'%c','%c'}}\n\n", + s3.x, s3.y, s3.z, s3.v, s3.w, + (s3.p == 0 ? "ok" : "BAD"), + s3.t[0], s3.t[1], s3.t[2], + s3.s[0], s3.s[1]); +} + +/* Back to normal */ + +#pragma pack() + +struct s4 { unsigned short x; int y; double z; }; + +struct s4 s4 = { 123, -456789, 3.14159 }; + +void test4(void) +{ + printf("s4 = {x = %d, y = %d, z = %.5f}\n\n", s4.x, s4.y, s4.z); +} + +/* Test harness */ + +int main(int argc, char ** argv) +{ + test1(); + test3(); + test4(); + return 0; +} -- cgit