1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // builtins.c -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Fri Jul 21 16:21:03 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Dec 12 18:22:26 2024 |
---|
13 | // Update Count : 150 |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_builtins__ |
---|
17 | |
---|
18 | // Type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct |
---|
19 | // members in user-defined functions. Note: needs to occur early, because it is used to generate destructor calls during |
---|
20 | // code generation |
---|
21 | forall( T & ) |
---|
22 | struct __Destructor { |
---|
23 | T * object; |
---|
24 | void (*dtor)( T * ); |
---|
25 | }; |
---|
26 | |
---|
27 | // Defined destructor in the case that non-generated code wants to use __Destructor. |
---|
28 | forall( T & ) |
---|
29 | static inline void ^?{}( __Destructor(T) & x ) { |
---|
30 | if ( x.object && x.dtor ) { |
---|
31 | x.dtor( x.object ); |
---|
32 | } |
---|
33 | } |
---|
34 | |
---|
35 | // Easy interface into __Destructor's destructor for easy codegen purposes. |
---|
36 | extern "C" { |
---|
37 | forall( T & ) |
---|
38 | static inline void __destroy_Destructor( __Destructor(T) * dtor ) { |
---|
39 | ^(*dtor){}; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | // exception implementation |
---|
44 | |
---|
45 | typedef unsigned long long __cfaabi_abi_exception_type_t; |
---|
46 | |
---|
47 | #include "../src/virtual.h" |
---|
48 | #include "../src/exception.h" |
---|
49 | |
---|
50 | void exit( int status, const char fmt[], ... ) __attribute__ (( format( printf, 2, 3 ), __nothrow__, __leaf__, __noreturn__ )); |
---|
51 | void abort( const char fmt[], ... ) __attribute__ (( format( printf, 1, 2 ), __nothrow__, __leaf__, __noreturn__ )); |
---|
52 | |
---|
53 | forall( T & ) |
---|
54 | static inline T & identity( T & i ) { |
---|
55 | return i; |
---|
56 | } |
---|
57 | |
---|
58 | // generator support |
---|
59 | struct generator$ { |
---|
60 | inline int; |
---|
61 | }; |
---|
62 | |
---|
63 | static inline void ?{}( generator$ & this ) { ((int&)this) = 0; } |
---|
64 | static inline void ^?{}( generator$ & ) {} |
---|
65 | |
---|
66 | forall( T & ) |
---|
67 | trait is_generator { |
---|
68 | void main( T & this ); |
---|
69 | generator$ * get_generator( T & this ); |
---|
70 | }; |
---|
71 | |
---|
72 | forall( T & | is_generator( T ) ) |
---|
73 | static inline T & resume( T & gen ) { |
---|
74 | main( gen ); |
---|
75 | return gen; |
---|
76 | } |
---|
77 | |
---|
78 | // Nearly "otype," except no parameterless constructor. |
---|
79 | // All you need, to store values in variables and to pass and return by value. |
---|
80 | // Many cases of |
---|
81 | // forall( T ... |
---|
82 | // can be "simplified" to |
---|
83 | // forall( T& | is_value(T) ... |
---|
84 | forall( T * ) |
---|
85 | trait is_value { |
---|
86 | void ?{}( T&, T ); |
---|
87 | T ?=?( T&, T ); |
---|
88 | void ^?{}( T& ); |
---|
89 | }; |
---|
90 | |
---|
91 | // implicit increment, decrement if += defined, and implicit not if != defined |
---|
92 | |
---|
93 | // C11 reference manual Section 6.5.16 (page 101): "An assignment expression has the value of the left operand after the |
---|
94 | // assignment, but is not an lvalue." Hence, return a value not a reference. |
---|
95 | static inline { |
---|
96 | forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } ) |
---|
97 | T ++?( T & x ) { return x += 1; } |
---|
98 | |
---|
99 | forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } ) |
---|
100 | T ?++( T & x ) { T tmp = x; x += 1; return tmp; } |
---|
101 | |
---|
102 | forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } ) |
---|
103 | T --?( T & x ) { return x -= 1; } |
---|
104 | |
---|
105 | forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } ) |
---|
106 | T ?--( T & x ) { T tmp = x; x -= 1; return tmp; } |
---|
107 | |
---|
108 | forall( T& | is_value(T) | { int ?!=?( T, zero_t ); } ) |
---|
109 | int !?( T & x ) { return !( x != 0 ); } |
---|
110 | } // distribution |
---|
111 | |
---|
112 | // universal typed pointer constant |
---|
113 | static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; } |
---|
114 | static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; } |
---|
115 | |
---|
116 | #if defined(__SIZEOF_INT128__) |
---|
117 | // constructor for 128-bit numbers (all constants are unsigned as +/- are operators) |
---|
118 | static inline void ?{}( unsigned int128 & this, unsigned long int h, unsigned long int l ) { |
---|
119 | this = (unsigned int128)h << 64 | (unsigned int128)l; |
---|
120 | } // ?{} |
---|
121 | #endif // __SIZEOF_INT128__ |
---|
122 | |
---|
123 | |
---|
124 | // exponentiation operator implementation |
---|
125 | |
---|
126 | extern "C" { |
---|
127 | float powf( float x, float y ); |
---|
128 | double pow( double x, double y ); |
---|
129 | long double powl( long double x, long double y ); |
---|
130 | float _Complex cpowf( float _Complex x, _Complex float z ); |
---|
131 | double _Complex cpow( double _Complex x, _Complex double z ); |
---|
132 | long double _Complex cpowl( long double _Complex x, _Complex long double z ); |
---|
133 | } // extern "C" |
---|
134 | |
---|
135 | static inline { // wrappers |
---|
136 | float ?\?( float x, float y ) { return powf( x, y ); } |
---|
137 | double ?\?( double x, double y ) { return pow( x, y ); } |
---|
138 | long double ?\?( long double x, long double y ) { return powl( x, y ); } |
---|
139 | float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf( x, y ); } |
---|
140 | double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); } |
---|
141 | long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); } |
---|
142 | } // distribution |
---|
143 | |
---|
144 | int ?\?( int x, unsigned int y ); |
---|
145 | long int ?\?( long int x, unsigned long int y ); |
---|
146 | long long int ?\?( long long int x, unsigned long long int y ); |
---|
147 | // unsigned computation may be faster and larger |
---|
148 | unsigned int ?\?( unsigned int x, unsigned int y ); |
---|
149 | unsigned long int ?\?( unsigned long int x, unsigned long int y ); |
---|
150 | unsigned long long int ?\?( unsigned long long int x, unsigned long long int y ); |
---|
151 | |
---|
152 | forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) { |
---|
153 | OT ?\?( OT x, unsigned int y ); |
---|
154 | OT ?\?( OT x, unsigned long int y ); |
---|
155 | OT ?\?( OT x, unsigned long long int y ); |
---|
156 | } // distribution |
---|
157 | |
---|
158 | static inline { |
---|
159 | int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; } |
---|
160 | long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
161 | long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; } |
---|
162 | unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; } |
---|
163 | unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
164 | unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; } |
---|
165 | } // distribution |
---|
166 | |
---|
167 | |
---|
168 | // enumeration implementation |
---|
169 | |
---|
170 | forall( E ) trait Bounded { |
---|
171 | E lowerBound( void ); |
---|
172 | E upperBound( void ); |
---|
173 | }; |
---|
174 | |
---|
175 | forall( E | Bounded( E ) ) trait Serial { |
---|
176 | int fromInstance( E e ); |
---|
177 | E fromInt_unsafe( int i ); |
---|
178 | E succ_unsafe( E e ); |
---|
179 | E pred_unsafe( E e ); |
---|
180 | }; |
---|
181 | |
---|
182 | forall( E ) trait CfaEnum { |
---|
183 | const char * label( E e ); |
---|
184 | int posn( E e ); |
---|
185 | }; |
---|
186 | |
---|
187 | forall( E, V | CfaEnum( E ) ) trait TypedEnum { |
---|
188 | V value( E e ); |
---|
189 | }; |
---|
190 | |
---|
191 | static inline |
---|
192 | forall( E | Serial( E ) ) { |
---|
193 | E fromInt( int i ) { |
---|
194 | E upper = upperBound(); |
---|
195 | E lower = lowerBound(); |
---|
196 | // It is okay to overflow as overflow will be theoretically caught by the other bound |
---|
197 | if ( i < fromInstance( lower ) || i > fromInstance( upper ) ) |
---|
198 | abort( "call to fromInt has index %d outside of enumeration range %d-%d.", |
---|
199 | i, fromInstance( lower ), fromInstance( upper ) ); |
---|
200 | return fromInt_unsafe( i ); |
---|
201 | } |
---|
202 | |
---|
203 | E succ( E e ) { |
---|
204 | E upper = upperBound(); |
---|
205 | if ( fromInstance( e ) >= fromInstance( upper ) ) |
---|
206 | abort( "call to succ() exceeds enumeration upper bound of %d.", fromInstance( upper ) ); |
---|
207 | return succ_unsafe( e ); |
---|
208 | } |
---|
209 | |
---|
210 | E pred( E e ) { |
---|
211 | E lower = lowerBound(); |
---|
212 | if ( fromInstance( e ) <= fromInstance( lower ) ) |
---|
213 | abort( "call to pred() exceeds enumeration lower bound of %d.", fromInstance( lower ) ); |
---|
214 | return pred_unsafe( e ); |
---|
215 | } |
---|
216 | |
---|
217 | int Countof( E ) { |
---|
218 | E upper = upperBound(); |
---|
219 | E lower = lowerBound(); |
---|
220 | return fromInstance( upper ) + fromInstance( lower ) + 1; |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | static inline |
---|
225 | forall( E | CfaEnum( E ) ) { |
---|
226 | int ?==?( E l, E r ) { return posn( l ) == posn( r ); } |
---|
227 | int ?!=?( E l, E r ) { return posn( l ) != posn( r ); } |
---|
228 | int ?<?( E l, E r ) { return posn( l ) < posn( r ); } |
---|
229 | int ?<=?( E l, E r ) { return posn( l ) <= posn( r ); } |
---|
230 | int ?>?( E l, E r ) { return posn( l ) > posn( r ); } |
---|
231 | int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); } |
---|
232 | } |
---|
233 | |
---|
234 | static inline |
---|
235 | forall( E | Serial( E ) ) { |
---|
236 | E ++?( E & l ) { |
---|
237 | int pos = fromInstance( l ); |
---|
238 | l = fromInt_unsafe( pos + 1 ); |
---|
239 | return l; |
---|
240 | } |
---|
241 | |
---|
242 | E --?( E & l ) { |
---|
243 | int pos = fromInstance( l ); |
---|
244 | l = fromInt_unsafe( pos - 1 ); |
---|
245 | return l; |
---|
246 | } |
---|
247 | |
---|
248 | E ?+=?( E & l, one_t ) { |
---|
249 | int pos = fromInstance( l ); |
---|
250 | l = fromInt_unsafe( pos + 1 ); |
---|
251 | return l; |
---|
252 | } |
---|
253 | |
---|
254 | E ?-=?( E & l, one_t ) { |
---|
255 | int pos = fromInstance( l ); |
---|
256 | l = fromInt_unsafe( pos - 1 ); |
---|
257 | return l; |
---|
258 | } |
---|
259 | |
---|
260 | E ?+=?( E & l, int i ) { |
---|
261 | int pos = fromInstance( l ); |
---|
262 | l = fromInt_unsafe( pos + i ); |
---|
263 | return l; |
---|
264 | } |
---|
265 | |
---|
266 | E ?-=?( E & l, int i ) { |
---|
267 | int pos = fromInstance( l ); |
---|
268 | l = fromInt_unsafe( pos - i ); |
---|
269 | return l; |
---|
270 | } |
---|
271 | |
---|
272 | E ?++( E & l ) { |
---|
273 | int pos = fromInstance( l ); |
---|
274 | l = fromInt_unsafe( pos + 1 ); |
---|
275 | return fromInt_unsafe( pos ); |
---|
276 | } |
---|
277 | |
---|
278 | E ?--( E & l ) { |
---|
279 | int pos = fromInstance( l ); |
---|
280 | l = fromInt_unsafe( pos - 1 ); |
---|
281 | return fromInt_unsafe( pos ); |
---|
282 | } |
---|
283 | } |
---|
284 | |
---|
285 | // Local Variables: // |
---|
286 | // mode: c // |
---|
287 | // tab-width: 4 // |
---|
288 | // End: // |
---|