| [aabc60c] | 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
 | 
|---|
| [f498c51] | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
 | 12 | // Last Modified On : Tue Dec 25 14:40:24 2018
 | 
|---|
 | 13 | // Update Count     : 3
 | 
|---|
| [aabc60c] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [3787dc1] | 16 | #include <assert.h>
 | 
|---|
 | 17 | #include <inttypes.h>
 | 
|---|
| [f498c51] | 18 | #include <fstream.hfa>
 | 
|---|
| [3787dc1] | 19 | 
 | 
|---|
| [fd54fef] | 20 | forall(T)
 | 
|---|
| [aabc60c] | 21 | T f(T x, T y) {
 | 
|---|
| [598f50e] | 22 |         x = y;
 | 
|---|
 | 23 |         return x;
 | 
|---|
 | 24 | }
 | 
|---|
 | 25 | 
 | 
|---|
| [fd54fef] | 26 | forall(T) T ident(T x) {
 | 
|---|
| [598f50e] | 27 |         return x;
 | 
|---|
| [aabc60c] | 28 | }
 | 
|---|
 | 29 | 
 | 
|---|
| [fd54fef] | 30 | forall( T, U )
 | 
|---|
| [3787dc1] | 31 | size_t struct_size( T i, U j ) {
 | 
|---|
 | 32 |         struct S { T i; U j; };
 | 
|---|
 | 33 |         return sizeof(S);
 | 
|---|
 | 34 | }
 | 
|---|
 | 35 | 
 | 
|---|
| [fd54fef] | 36 | forall( T, U )
 | 
|---|
| [3787dc1] | 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
 | 
|---|
| [fd54fef] | 43 | forall( T | { void print(T); int ?==?(T, T); }, U | { void print(U); U ?=?(U&, zero_t); } )
 | 
|---|
| [3787dc1] | 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;
 | 
|---|
| [a7caf3d] | 50 |         assertf(s.i == i, "struct operation fails in polymorphic context.");
 | 
|---|
| [3787dc1] | 51 | 
 | 
|---|
 | 52 |         B b;
 | 
|---|
 | 53 |         b.j = 0;
 | 
|---|
 | 54 |         b.i = s.i;
 | 
|---|
 | 55 |         return b.j;
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
| [801978b] | 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 | 
 | 
|---|
| [ba8547e] | 73 |         void showStatic( thing(long long int) & x ) {
 | 
|---|
| [801978b] | 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");
 | 
|---|
| [ba8547e] | 87 |         thing(long long int) x;
 | 
|---|
| [801978b] | 88 |         showStatic(x);
 | 
|---|
 | 89 |         showDynamic(x);
 | 
|---|
 | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
| [aabc60c] | 92 | int main() {
 | 
|---|
| [3787dc1] | 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);
 | 
|---|
| [f498c51] | 98 |                 sout | x | y | z;
 | 
|---|
| [3787dc1] | 99 |         }
 | 
|---|
 | 100 |         {
 | 
|---|
 | 101 |                 // explicitly specialize function
 | 
|---|
 | 102 |                 int (*f)(int) = ident;
 | 
|---|
 | 103 |                 ((int(*)(int))ident);
 | 
|---|
| [f498c51] | 104 |                 sout | f(5) | ((int(*)(int))ident)(5);
 | 
|---|
| [3787dc1] | 105 |         }
 | 
|---|
 | 106 |         {
 | 
|---|
 | 107 |                 // test aggregates with polymorphic members
 | 
|---|
| [a7caf3d] | 108 |                 typedef __attribute__((aligned(8))) uint32_t x_type;
 | 
|---|
 | 109 |                 typedef __attribute__((aligned(8))) uint64_t y_type;
 | 
|---|
| [3787dc1] | 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
 | 
|---|
| [1f370451] | 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));
 | 
|---|
| [3787dc1] | 128 | 
 | 
|---|
 | 129 |                 y_type ?=?(y_type & this, zero_t) {
 | 
|---|
 | 130 |                         this = (int)0;
 | 
|---|
 | 131 |                         return this;
 | 
|---|
 | 132 |                 }
 | 
|---|
 | 133 | 
 | 
|---|
 | 134 |                 void print(x_type x) {
 | 
|---|
| [f498c51] | 135 |                         sout | x;
 | 
|---|
| [3787dc1] | 136 |                 }
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 |                 void print(y_type y) {
 | 
|---|
| [f498c51] | 139 |                         sout | y;
 | 
|---|
| [3787dc1] | 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;
 | 
|---|
| [a7caf3d] | 148 |                 assertf(ret == u.f2, "union operation fails in polymorphic context.");
 | 
|---|
| [3787dc1] | 149 |         }
 | 
|---|
| [801978b] | 150 | 
 | 
|---|
 | 151 |         checkPlan9offsets();
 | 
|---|
| [aabc60c] | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 | // Local Variables: //
 | 
|---|
 | 155 | // tab-width: 4 //
 | 
|---|
 | 156 | // End: //
 | 
|---|