1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // polymorphism.c -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Tue Oct 17 12:19:48 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Dec 25 14:40:24 2018 |
---|
13 | // Update Count : 3 |
---|
14 | // |
---|
15 | |
---|
16 | #include <assert.h> |
---|
17 | #include <inttypes.h> |
---|
18 | #include <fstream.hfa> |
---|
19 | |
---|
20 | forall(T) |
---|
21 | T f(T x, T y) { |
---|
22 | x = y; |
---|
23 | return x; |
---|
24 | } |
---|
25 | |
---|
26 | forall(T) T ident(T x) { |
---|
27 | return x; |
---|
28 | } |
---|
29 | |
---|
30 | forall( T, U ) |
---|
31 | size_t struct_size( T i, U j ) { |
---|
32 | struct S { T i; U j; }; |
---|
33 | return sizeof(S); |
---|
34 | } |
---|
35 | |
---|
36 | forall( T, U ) |
---|
37 | size_t union_size( T i, U j ) { |
---|
38 | union B { T i; U j; }; |
---|
39 | return sizeof(B); |
---|
40 | } |
---|
41 | |
---|
42 | // perform some simple operations on aggregates of T and U |
---|
43 | forall( T | { void print(T); int ?==?(T, T); }, U | { void print(U); U ?=?(U&, zero_t); } ) |
---|
44 | U foo(T i, U j) { |
---|
45 | struct S { T i; U j; }; |
---|
46 | union B { T i; U j; }; |
---|
47 | |
---|
48 | S s; |
---|
49 | s.i = i; |
---|
50 | assertf(s.i == i, "struct operation fails in polymorphic context."); |
---|
51 | |
---|
52 | B b; |
---|
53 | b.j = 0; |
---|
54 | b.i = s.i; |
---|
55 | return b.j; |
---|
56 | } |
---|
57 | |
---|
58 | void checkPlan9offsets() { |
---|
59 | |
---|
60 | forall( T ) |
---|
61 | struct thing { |
---|
62 | T q; // variable-sized padding |
---|
63 | inline double; |
---|
64 | inline float; |
---|
65 | }; |
---|
66 | |
---|
67 | #define SHOW_OFFSETS \ |
---|
68 | double & x_inner_double = x; \ |
---|
69 | float & x_inner_float = x; \ |
---|
70 | printf(" offset of inner double: %ld\n", ((char *) & x_inner_double) - ((char *) & x) ); \ |
---|
71 | printf(" offset of inner float: %ld\n", ((char *) & x_inner_float ) - ((char *) & x) ); |
---|
72 | |
---|
73 | void showStatic( thing(int) & x ) { |
---|
74 | printf("static:\n"); |
---|
75 | SHOW_OFFSETS |
---|
76 | } |
---|
77 | |
---|
78 | forall( T ) |
---|
79 | void showDynamic( thing(T) & x ) { |
---|
80 | printf("dynamic:\n"); |
---|
81 | SHOW_OFFSETS |
---|
82 | } |
---|
83 | |
---|
84 | #undef SHOW_OFFSETS |
---|
85 | |
---|
86 | printf("=== checkPlan9offsets\n"); |
---|
87 | thing(int) x; |
---|
88 | showStatic(x); |
---|
89 | showDynamic(x); |
---|
90 | } |
---|
91 | |
---|
92 | int main() { |
---|
93 | { |
---|
94 | // ensure that x is not changed by the invocation of a polymorphic function |
---|
95 | int x = 123; |
---|
96 | int y = 456; |
---|
97 | int z = f(x, y); |
---|
98 | sout | x | y | z; |
---|
99 | } |
---|
100 | { |
---|
101 | // explicitly specialize function |
---|
102 | int (*f)(int) = ident; |
---|
103 | ((int(*)(int))ident); |
---|
104 | sout | f(5) | ((int(*)(int))ident)(5); |
---|
105 | } |
---|
106 | { |
---|
107 | // test aggregates with polymorphic members |
---|
108 | typedef __attribute__((aligned(8))) uint32_t x_type; |
---|
109 | typedef __attribute__((aligned(8))) uint64_t y_type; |
---|
110 | |
---|
111 | x_type x = 3; |
---|
112 | y_type y = 3; |
---|
113 | |
---|
114 | struct S { |
---|
115 | x_type f1; |
---|
116 | y_type f2; |
---|
117 | }; |
---|
118 | union U { |
---|
119 | x_type f1; |
---|
120 | y_type f2; |
---|
121 | }; |
---|
122 | // ensure that the size of aggregates with polymorphic members |
---|
123 | // matches the size of the aggregates in a monomorphic context |
---|
124 | size_t ssz = struct_size(x, y); |
---|
125 | size_t usz = union_size(x, y); |
---|
126 | assertf( ssz == sizeof(S), "struct size differs in polymorphic context: %zd / %zd", ssz, sizeof(S)); |
---|
127 | assertf( usz == sizeof(U), "union size differs in polymorphic context: %zd / %zd", usz, sizeof(U)); |
---|
128 | |
---|
129 | y_type ?=?(y_type & this, zero_t) { |
---|
130 | this = (int)0; |
---|
131 | return this; |
---|
132 | } |
---|
133 | |
---|
134 | void print(x_type x) { |
---|
135 | sout | x; |
---|
136 | } |
---|
137 | |
---|
138 | void print(y_type y) { |
---|
139 | sout | y; |
---|
140 | } |
---|
141 | |
---|
142 | y_type ret = foo(x, y); |
---|
143 | |
---|
144 | // duplicate logic from inside of foo to ensure the same results |
---|
145 | U u; |
---|
146 | u.f2 = 0; |
---|
147 | u.f1 = x; |
---|
148 | assertf(ret == u.f2, "union operation fails in polymorphic context."); |
---|
149 | } |
---|
150 | |
---|
151 | checkPlan9offsets(); |
---|
152 | } |
---|
153 | |
---|
154 | // Local Variables: // |
---|
155 | // tab-width: 4 // |
---|
156 | // End: // |
---|