From c677f108ff340c5bca67b428aa6e56b47f62da8c Mon Sep 17 00:00:00 2001 From: xleroy Date: Fri, 28 Mar 2014 08:20:14 +0000 Subject: C: Support array initializers that are too short + default init for remainder. Elab: Handle C99 designated initializers. C2C, Initializers: more precise intermediate AST for initializers. git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@2439 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e --- test/regression/Makefile | 7 +-- test/regression/Results/initializers | 1 + test/regression/Results/initializers2 | 12 +++++ test/regression/Results/initializers3 | 11 +++++ test/regression/initializers.c | 5 ++ test/regression/initializers2.c | 74 ++++++++++++++++++++++++++++ test/regression/initializers3.c | 90 +++++++++++++++++++++++++++++++++++ 7 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 test/regression/Results/initializers2 create mode 100644 test/regression/Results/initializers3 create mode 100644 test/regression/initializers2.c create mode 100644 test/regression/initializers3.c (limited to 'test') diff --git a/test/regression/Makefile b/test/regression/Makefile index 4d9683c..57d5db3 100644 --- a/test/regression/Makefile +++ b/test/regression/Makefile @@ -9,7 +9,8 @@ LIBS=$(LIBMATH) # and have reference output in Results TESTS=int32 int64 floats floats-basics \ - expr1 expr6 funptr2 initializers volatile1 volatile2 volatile3 \ + expr1 expr6 funptr2 initializers initializers2 initializers3 \ + volatile1 volatile2 volatile3 \ funct3 expr5 struct7 struct8 struct11 casts1 casts2 char1 \ sizeof1 sizeof2 binops bool for1 @@ -27,8 +28,8 @@ TESTS_DIFF=NaNs # Other tests: should compile to .s without errors (but expect warnings) -EXTRAS=annot1 commaprec expr2 expr3 expr4 extern1 funct2 funptr1 init1 \ - init2 init3 init4 pragmas ptrs1 ptrs2 struct1 struct2 struct3 \ +EXTRAS=annot1 commaprec expr2 expr3 expr4 extern1 funct2 funptr1 \ + pragmas ptrs1 ptrs2 struct1 struct2 struct3 \ struct4 struct5 struct6 struct9 struct10 types1 seqops # Test known to fail diff --git a/test/regression/Results/initializers b/test/regression/Results/initializers index 7474148..a263012 100644 --- a/test/regression/Results/initializers +++ b/test/regression/Results/initializers @@ -21,3 +21,4 @@ x19 = { "Hello", "world!" } x20 = { 'H', 'e', 'l', } x21 = { 'H', 'e', 'l', 'l', 'o', '!', 0, 0, 0, 0, } x22 ok +x23 = { hd = 8, tl = ok } diff --git a/test/regression/Results/initializers2 b/test/regression/Results/initializers2 new file mode 100644 index 0000000..9ba5232 --- /dev/null +++ b/test/regression/Results/initializers2 @@ -0,0 +1,12 @@ +a1 = { 0, 3, 100, 0, 0 } +a2 = { 10, 0, -2, -1, -3 } +a3 = { 1, 2, 5, 6, 7 } (size = 5) +s1 = { 0, 0.00, abc } +s2 = { 13, 4.50, xxx } +s3 = { 0, 0.00, abc } +u1.c = abc +u2.a = 15 +u3.b = 3.14 +u4.c = {0,0,1,0} +pv1 = { {1,2,3}, {11,12,13}, {0,0,0}, {0,3,0}, } +t = { {0,42,43}, {{1,0,0}, {0,1,0}, {0,0,1}, } } diff --git a/test/regression/Results/initializers3 b/test/regression/Results/initializers3 new file mode 100644 index 0000000..8742f8e --- /dev/null +++ b/test/regression/Results/initializers3 @@ -0,0 +1,11 @@ +x5 = { 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, } +x17[7] = { 'H', 'e', 'l', 'l', 'o', '!', 0, } +x18 = "Hello!" +x19 = { "Hello", "world!" } +x20 = { 'H', 'e', 'l', } +x21 = { 'H', 'e', 'l', 'l', 'o', '!', 0, 0, 0, 0, } +f(0,42) = 42, f(1,42) = 43, f(2,42) = 44, f(3,42) = 44, f(4,42) = 44 +s1 = { tag = 0, a = {66,77}, u = {0,0} } +s2 = { tag = 0, a = {0,1}, u = {66,77} } +s3 = { tag = 1, a = {1,0}, u = {'H', 'e', 'l', 'l', 'o', '!', 0, 'X', } } +s4 = { tag = 0, a = {66,77}, u = {88,99} } diff --git a/test/regression/initializers.c b/test/regression/initializers.c index d0a35f3..938795a 100644 --- a/test/regression/initializers.c +++ b/test/regression/initializers.c @@ -54,6 +54,9 @@ char x21[10] = "Hello!"; char * x22 = &(x10.u.y); +/* Initializer can refer to ident just declared */ +struct list { int hd; struct list * tl; } x23 = { sizeof(x23), &x23 }; + static void print_chars(char * s, int sz) { int i; @@ -115,6 +118,8 @@ int main() printf("x22 ok\n"); else printf("x22 error\n"); + printf("x23 = { hd = %d, tl = %s }\n", + x23.hd, x23.tl == &x23 ? "ok" : "ERROR"); return 0; } diff --git a/test/regression/initializers2.c b/test/regression/initializers2.c new file mode 100644 index 0000000..f8d5caf --- /dev/null +++ b/test/regression/initializers2.c @@ -0,0 +1,74 @@ +#include + +/* Examples of designated initializers from Harbison & Steele */ + +int a1[5] = { [2] = 100, [1] = 3 }; +int a2[5] = { [0] = 10, [2] = -2, -1, -3 }; +int a3[] = { 1, 2, 3, [2] = 5, 6, 7 }; + +struct S { int a; float b; char c[4]; }; +struct S s1 = { .c = "abc" }; +struct S s2 = { 13, 3.3, "xxx", .b = 4.5 }; +struct S s3 = { .c = { 'a', 'b', 'c', '\0' } }; + +union U { int a; float b; char c[4]; }; +union U u1 = { .c = "abc" }; +union U u2 = { .a = 15 }; +union U u3 = { .b = 3.14 }; +union U u4 = { .a = 42, .c[2] = 1 }; + +struct Point { int x; int y; int z; }; +typedef struct Point PointVector[4]; +PointVector pv1 = { + [0].x = 1, [0].y = 2, [0].z = 3, + [1] = { .x = 11, .y = 12, .z = 13 }, + [3] = { .y = 3 } +}; + +typedef int Vector[3]; +typedef int Matrix[3][3]; +struct Trio { Vector v; Matrix m; }; +struct Trio t = { + .m = { [0][0] = 1, [1][1] = 1, [2][2] = 1 }, + .v = { [1] = 42, 43 } +}; + +int main() +{ + int i; + + printf("a1 = { %d, %d, %d, %d, %d }\n", + a1[0], a1[1], a1[2], a1[3], a1[4]); + printf("a2 = { %d, %d, %d, %d, %d }\n", + a2[0], a2[1], a2[2], a2[3], a2[4]); + printf("a3 = { %d, %d, %d, %d, %d } (size = %d)\n", + a3[0], a3[1], a3[2], a3[3], a3[4], + sizeof(a3) / sizeof(int)); + + printf("s1 = { %d, %.2f, %s }\n", + s1.a, s1.b, s1.c); + printf("s2 = { %d, %.2f, %s }\n", + s2.a, s2.b, s2.c); + printf("s3 = { %d, %.2f, %s }\n", + s3.a, s3.b, s3.c); + + printf("u1.c = %s\n", u1.c); + printf("u2.a = %d\n", u2.a); + printf("u3.b = %.2f\n", u3.b); + printf("u4.c = {%d,%d,%d,%d}\n", u4.c[0], u4.c[1], u4.c[2], u4.c[3]); + + printf("pv1 = { "); + for (i = 0; i < 4; i++) + printf("{%d,%d,%d}, ", pv1[i].x, pv1[i].y, pv1[i].z); + printf("}\n"); + + printf("t = { {%d,%d,%d}, ", t.v[0], t.v[1], t.v[2]); + printf("{"); + for (i = 0; i < 3; i++) + printf("{%d,%d,%d}, ", t.m[i][0], t.m[i][1], t.m[i][2]); + printf("} }\n"); + + return 0; +} + + diff --git a/test/regression/initializers3.c b/test/regression/initializers3.c new file mode 100644 index 0000000..359a0f7 --- /dev/null +++ b/test/regression/initializers3.c @@ -0,0 +1,90 @@ +/* Initialization of local variables */ + +#include + +static void print_chars(char * s, int sz) +{ + int i; + for (i = 0; i < sz; i++) { + if (s[i] >= 32 && s[i] < 127) + printf("'%c', ", s[i]); + else + printf("%d, ", s[i]); + } +} + +/* Initialization of local const array */ + +int f(int x, int y) +{ + const int dfl = 2; + const int tbl[3] = { y, y + 1, y + 2 }; + return tbl[x >= 0 && x < 3 ? x : dfl]; +} + +struct P { int x, y; }; + +struct S { + int tag; + struct P a; + union { + struct P b; + char c[8]; + } u; +}; + +static void print_S(char * name, struct S * s) +{ + printf("%s = { tag = %d, a = {%d,%d}, u = ", name, s->tag, s->a.x, s->a.y); + switch(s->tag) { + case 0: + printf("{%d,%d} }\n", s->u.b.x, s->u.b.y); + break; + case 1: + printf("{"); print_chars(s->u.c, 8); printf("} }\n"); + break; + default: + printf("BAD }\n"); + break; + } +} + + +int main() +{ + /* Initialization of arrays */ + const int x5[10] = { 1, 2, 3 }; + char x17[] = "Hello!"; + char * x18 = "Hello!"; + char * x19[2] = { "Hello", "world!" }; + char x20[3] = "Hello!"; + char x21[10] = "Hello!"; + printf("x5 = { "); + for (int i = 0; i < 10; i++) printf("%d, ", x5[i]); + printf("}\n"); + printf("x17[%d] = { ", (int) sizeof(x17)); + print_chars(x17, sizeof(x17)); + printf("}\n"); + printf("x18 = \"%s\"\n", x18); + printf("x19 = { \"%s\", \"%s\" }\n", x19[0], x19[1]); + printf("x20 = { "); + print_chars(x20, sizeof(x20)); + printf("}\n"); + printf("x21 = { "); + print_chars(x21, sizeof(x21)); + printf("}\n"); + /* Local const arrays */ + printf("f(0,42) = %d, f(1,42) = %d, f(2,42) = %d, f(3,42) = %d, f(4,42) = %d\n", + f(0,42), f(1, 42), f(2, 42), f(3, 42), f(4, 42)); + /* Structs/unions */ + struct P p1 = { 66, 77 }; + struct S s1 = { 0, p1 }; + print_S("s1", &s1); + struct S s2 = { .a.y = 1, .u.c[4] = 'x', .u.b = p1 }; + print_S("s2", &s2); + struct S s3 = { .tag = 1, .a = p1, .a.x = 1, .u.c = "Hello!", .u.c[7] = 'X' }; + print_S("s3", &s3); + struct S s4 = { .tag = 0, .a.x = 1, .a = p1, .u.b = 88, 99 }; + print_S("s4", &s4); + return 0; +} -- cgit v1.2.3