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