source: tests/references.cfa@ 0fba0d4

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since 0fba0d4 was 86d55e66, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed inline assembly on arm

  • Property mode set to 100644
File size: 4.4 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 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// references.c --
8//
9// Author : Rob Schluntz
10// Created On : Wed Aug 23 16:11:50 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Dec 25 14:31:48 2018
13// Update Count : 11
14//
15
16#include <fstream.hfa>
17
18struct Y { int i; };
19void ?{}( Y & y ) { sout | "Default constructing a Y"; }
20void ?{}( Y & y, Y other ) { sout | "Copy constructing a Y"; }
21void ^?{}( Y & y ) { sout | "Destructing a Y"; }
22Y ?=?( Y & y, Y other ) { sout | "Assigning a Y"; return y; }
23void ?{}( Y & y, int i ) { sout | "Value constructing a Y" | i; y.i = i; }
24
25struct X { Y & r; Y y; };
26void ?{}( X & x ) {
27 // ensure that r is not implicitly constructed
28}
29void ?{}( X & x, X other ) {
30 // ensure that r is not implicitly constructed
31}
32void ^?{}( X & x ) {
33 // ensure that r is not implicitly destructed
34}
35X ?=?( X & x, X other ) { return x; }
36
37// ensure that generated functions do not implicitly operate on references
38struct Z { Y & r; Y y; };
39
40// test user-defined reference-returning function
41int & toref( int * p ) { return *p; }
42// test user-defined reference-parameter function
43int * toptr( int & r ) { return &r; }
44
45void changeRef( int & r ) {
46 r++;
47}
48
49int main() {
50 int x = 123456, x2 = 789, *p1 = &x, **p2 = &p1, ***p3 = &p2,
51 &r1 = x, &&r2 = r1, &&&r3 = r2;
52 ***p3 = 3; // change x
53 **p3 = &x; // change p1
54 *p3 = &p1; // change p2
55 int y = 0, z = 11, & ar[3] = { x, y, z }; // initialize array of references
56 // &ar[1] = &z; // change reference array element
57 // typeof( ar[1] ) p = 3; // is int, i.e., the type of referenced object
58 // typeof( &ar[1] ) q = &x; // is int *, i.e., the type of pointer
59 // _Static_assert( sizeof( ar[1] ) == sizeof( int ), "Array type should be int." ); // is true, i.e., the size of referenced object
60 // _Static_assert( sizeof( &ar[1] ) == sizeof( int *), "Address of array should be int *." ); // is true, i.e., the size of a reference
61
62 ((int*&)&r3) = &x; // change r1, (&*)**r3
63 x = 3;
64 // test that basic reference properties are true - r1 should be an alias for x
65 sout | x | r1 | &x == &r1;
66 r1 = 12;
67 sout | x | r1 | &x == &r1;
68
69 // test that functions using basic references work
70 sout | toref( &x ) | toref( p1 ) | toptr( r1 ) == toptr( x ) | toptr( r1 ) == &x;
71
72 changeRef( x );
73 changeRef( y );
74 changeRef( z );
75 sout | x | y | z;
76 changeRef( r1 );
77 sout | r1 | x;
78
79 r3 = 6; // change x, ***r3
80 sout | "x = " | x | " ; x2 = " | x2; // check that x was changed
81 &r3 = &x2; // change r1 to refer to x2, (&*)**r3
82 r3 = 999; // modify x2
83 sout | "x = " | x | " ; x2 = " | x2; // check that x2 was changed
84 ((int**&)&&r3) = p2; // change r2, (&(&*)*)*r3, ensure explicit cast to reference works
85 r3 = 12345; // modify x
86 sout | "x = " | x | " ; x2 = " | x2; // check that x was changed
87 &&&r3 = p3; // change r3 to p3, (&(&(&*)*)*)r3
88 ((int&)r3) = 22222; // modify x, ensure explicit cast to reference works
89 sout | "x = " | x | " ; x2 = " | x2; // check that x was changed
90
91 // test that reference members are not implicitly constructed/destructed/assigned
92 X x1, x2 = x1;
93 x1 = x2;
94
95 Z z1, z2 = z1;
96 Y z1r = 56, z2r = 78;
97 &z1.r = &z1r;
98 &z2.r = &z2r;
99
100 z1 = z2;
101
102 // test rvalue-to-reference conversion
103 {
104 struct S { double x, y; };
105 void f( int & i, int & j, S & s, int v[] ) {
106 sout | i | j | "{ " | s.[x, y] | " }," | "[" | v[0] | "," | v[1] | "," | v[2] | "]";
107 }
108 void g(int & i) { sout | i; }
109 void h(int &&& i) { sout | i; }
110
111 int &&& r = 3; // rvalue to reference
112 int i = r;
113 sout | i | r; // both 3
114
115 g( 3 ); // rvalue to reference
116 h( (int &&&)3 ); // rvalue to reference
117
118 int a = 5, b = 4;
119 f( 3, a + b, (S){ 1.0, 7.0 }, (int [3]){ 1, 2, 3 } ); // two rvalue to reference
120 }
121
122 {
123 int a = 3;
124 int *p = &a;
125 asm (
126 #if defined( __i386 ) || defined( __x86_64 )
127 "incl %[p]\n\t"
128 : [p] "+m" (*p)
129 #elif defined( __aarch64__ )
130 "ldr w1, %[p]\n\t"
131 "add w1, w1, 1\n\t"
132 "str w1, %[p]\n\t"
133 : [p] "+m" (*p) ::"w1"
134 #endif
135 );
136 printf("%d\n", a);
137 }
138}
139
140// Local Variables: //
141// tab-width: 4 //
142// End: //
Note: See TracBrowser for help on using the repository browser.