diff options
Diffstat (limited to 'test/regression')
-rw-r--r-- | test/regression/Makefile | 4 | ||||
-rw-r--r-- | test/regression/Results/attribs1 | 2 | ||||
-rw-r--r-- | test/regression/Results/packedstruct1 | 20 | ||||
-rw-r--r-- | test/regression/attribs1.c | 16 | ||||
-rw-r--r-- | test/regression/packedstruct1.c | 114 |
5 files changed, 151 insertions, 5 deletions
diff --git a/test/regression/Makefile b/test/regression/Makefile index 5de19cc2..215116c5 100644 --- a/test/regression/Makefile +++ b/test/regression/Makefile @@ -2,7 +2,7 @@ include ../../Makefile.config CCOMP=../../ccomp CCOMPFLAGS=-stdlib ../../runtime -dparse -dc -dclight -dasm \ - -fstruct-passing -fstruct-assign -fbitfields + -fstruct-passing -fstruct-assign -fbitfields -fpacked-structs LIBS=$(LIBMATH) @@ -12,7 +12,7 @@ TESTS=attribs1 bitfields1 bitfields2 bitfields3 bitfields4 \ bitfields5 bitfields6 bitfields7 \ expr1 initializers volatile2 \ funct3 expr5 struct7 struct8 casts1 casts2 char1 \ - sizeof1 sizeof2 + sizeof1 sizeof2 packedstructs1 # 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/attribs1 b/test/regression/Results/attribs1 index e9954741..0464ec87 100644 --- a/test/regression/Results/attribs1 +++ b/test/regression/Results/attribs1 @@ -2,3 +2,5 @@ Address of a = 0 mod 16 Address of b = 0 mod 8 Delta d - c = 4 Delta f - e = 4 +Address of u = 0 mod 8 +Address of v = 0 mod 8 diff --git a/test/regression/Results/packedstruct1 b/test/regression/Results/packedstruct1 new file mode 100644 index 00000000..fe19bffc --- /dev/null +++ b/test/regression/Results/packedstruct1 @@ -0,0 +1,20 @@ +sizeof(struct s1) = 14 +offsetof(x) = 0, offsetof(y) = 2, offsetof(z) = 6 +s1 = {x = 123, y = -456, z = 3.14159} + +sizeof(struct s2) = 16 +&s2 mod 16 = 0 +offsetof(x) = 0, offsetof(y) = 2, offsetof(z) = 6 +s2 = {x = 57, y = -456, z = 3.14159} + +sizeof(struct s3) = 13 +s3 = {x = 123, y = 45678, z = 2147483649, v = -456, w = -1234567} + +sizeof(struct s4) = 16 +offsetof(x) = 0, offsetof(y) = 4, offsetof(z) = 8 +s4 = {x = 123, y = -456, z = 3.14159} + +sizeof(struct s5) = 14 +offsetof(x) = 0, offsetof(y) = 2, offsetof(z) = 6 +s5 = {x = 123, y = -456, z = 3.14159} + diff --git a/test/regression/attribs1.c b/test/regression/attribs1.c index a02f7188..b6e5c22d 100644 --- a/test/regression/attribs1.c +++ b/test/regression/attribs1.c @@ -24,6 +24,16 @@ __attribute((__section__("myconst"))) const int f = 34; __attribute((__section__("mycode"))) int myfunc(int x) { return x + 1; } +/* Alignment with typedefs and structs */ + +struct __attribute((__aligned__(8))) mystruct { char c1, c2; }; +char filler5 = 1; +struct mystruct u; + +typedef __attribute((__aligned__(8))) int myint; +char filler6 = 1; +myint v; + /* Test harness */ int main() @@ -32,8 +42,8 @@ int main() printf("Address of b = %u mod 8\n", ((unsigned int) &b) & 0x7); printf("Delta d - c = %u\n", ((unsigned int) &d) - ((unsigned int) &c)); printf("Delta f - e = %u\n", ((unsigned int) &f) - ((unsigned int) &e)); + printf("Address of u = %u mod 8\n", ((unsigned int) &u) & 0x7); + printf("Address of v = %u mod 8\n", ((unsigned int) &v) & 0x7); + return 0; } - - - diff --git a/test/regression/packedstruct1.c b/test/regression/packedstruct1.c new file mode 100644 index 00000000..d5ae404e --- /dev/null +++ b/test/regression/packedstruct1.c @@ -0,0 +1,114 @@ +/* Packed structs */ + +#include <stdio.h> + +#define offsetof(s,f) (int)&(((struct s *)0)->f) + +/* Simple packing */ + +#pragma pack(1) + +struct s1 { unsigned short x; int y; double z; }; + +void test1(void) +{ + struct s1 s1; + printf("sizeof(struct s1) = %d\n", sizeof(struct s1)); + printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n", + offsetof(s1,x), offsetof(s1,y), offsetof(s1,z)); + s1.x = 123; s1.y = -456; s1.z = 3.14159; + printf("s1 = {x = %d, y = %d, z = %.5f}\n\n", s1.x, s1.y, s1.z); +} + +/* Packing plus alignment */ + +#pragma pack(2,16) + +struct s2 { unsigned char x; int y; double z; }; + +char filler1; + +struct s2 s2; + +void test2(void) +{ + printf("sizeof(struct s2) = %d\n", sizeof(struct s2)); + printf("&s2 mod 16 = %d\n", ((int) &s2) & 0xF); + printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n", + offsetof(s2,x), offsetof(s2,y), offsetof(s2,z)); + s2.x = 12345; s2.y = -456; s2.z = 3.14159; + printf("s2 = {x = %d, y = %d, z = %.5f}\n\n", s2.x, s2.y, s2.z); +} + +/* Now with byte-swapped fields */ + +#if defined(__COMPCERT__) && defined(__POWERPC__) +#pragma pack(1,1,1) +#else +#pragma pack(1,1,0) +#endif + +struct s3 { + unsigned char x; + unsigned short y; + unsigned int z; + signed short v; + signed int w; +}; + +struct s3 s3; + +void test3(void) +{ + printf("sizeof(struct s3) = %d\n", sizeof(struct s3)); + s3.x = 123; + s3.y = 45678; + s3.z = 0x80000001U; + s3.v = -456; + s3.w = -1234567; + printf("s3 = {x = %u, y = %u, z = %u, v = %d, w = %d}\n\n", + s3.x, s3.y, s3.z, s3.v, s3.w); +} + +/* Back to normal */ + +#pragma pack() + +struct s4 { unsigned short x; int y; double z; }; + +void test4(void) +{ + struct s4 s4; + printf("sizeof(struct s4) = %d\n", sizeof(struct s4)); + printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n", + offsetof(s4,x), offsetof(s4,y), offsetof(s4,z)); + s4.x = 123; s4.y = -456; s4.z = 3.14159; + printf("s4 = {x = %d, y = %d, z = %.5f}\n\n", s4.x, s4.y, s4.z); +} + +/* One more, with packed attribute */ + +struct __attribute((packed)) s5 { unsigned short x; int y; double z; }; + +void test5(void) +{ + struct s5 s5; + printf("sizeof(struct s5) = %d\n", sizeof(struct s5)); + printf("offsetof(x) = %d, offsetof(y) = %d, offsetof(z) = %d\n", + offsetof(s5,x), offsetof(s5,y), offsetof(s5,z)); + s5.x = 123; s5.y = -456; s5.z = 3.14159; + printf("s5 = {x = %d, y = %d, z = %.5f}\n\n", s5.x, s5.y, s5.z); +} + + +/* Test harness */ + +int main(int argc, char ** argv) +{ + test1(); + test2(); + test3(); + test4(); + test5(); + return 0; +} |