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