[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 |
---|
[ad8b6df] | 12 | // Last Modified On : Thu Dec 12 18:22:26 2024 |
---|
| 13 | // Update Count : 150 |
---|
[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 | |
---|
[8e4f34e] | 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) ... |
---|
[ad8b6df] | 84 | forall( T * ) |
---|
[8e4f34e] | 85 | trait is_value { |
---|
| 86 | void ?{}( T&, T ); |
---|
| 87 | T ?=?( T&, T ); |
---|
| 88 | void ^?{}( T& ); |
---|
| 89 | }; |
---|
| 90 | |
---|
[0d8266c] | 91 | // implicit increment, decrement if += defined, and implicit not if != defined |
---|
[d2887f7] | 92 | |
---|
[9f7285e] | 93 | // C11 reference manual Section 6.5.16 (page 101): "An assignment expression has the value of the left operand after the |
---|
[2ead704] | 94 | // assignment, but is not an lvalue." Hence, return a value not a reference. |
---|
[8a30423] | 95 | static inline { |
---|
[8e4f34e] | 96 | forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } ) |
---|
[8477fc4] | 97 | T ++?( T & x ) { return x += 1; } |
---|
[d2887f7] | 98 | |
---|
[8e4f34e] | 99 | forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } ) |
---|
[8477fc4] | 100 | T ?++( T & x ) { T tmp = x; x += 1; return tmp; } |
---|
[d2887f7] | 101 | |
---|
[8e4f34e] | 102 | forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } ) |
---|
[8477fc4] | 103 | T --?( T & x ) { return x -= 1; } |
---|
[d2887f7] | 104 | |
---|
[8e4f34e] | 105 | forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } ) |
---|
[8477fc4] | 106 | T ?--( T & x ) { T tmp = x; x -= 1; return tmp; } |
---|
[0d8266c] | 107 | |
---|
[8e4f34e] | 108 | forall( T& | is_value(T) | { int ?!=?( T, zero_t ); } ) |
---|
[8477fc4] | 109 | int !?( T & x ) { return !( x != 0 ); } |
---|
[8a30423] | 110 | } // distribution |
---|
[7579ac0] | 111 | |
---|
| 112 | // universal typed pointer constant |
---|
[fd54fef] | 113 | static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; } |
---|
[04423b53] | 114 | static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; } |
---|
[a1edafa] | 115 | |
---|
[cf5af9c] | 116 | #if defined(__SIZEOF_INT128__) |
---|
[b56f55c] | 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 | } // ?{} |
---|
[cf5af9c] | 121 | #endif // __SIZEOF_INT128__ |
---|
[b56f55c] | 122 | |
---|
[9f7285e] | 123 | |
---|
[a1edafa] | 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 | |
---|
[9f7285e] | 135 | static inline { // wrappers |
---|
[8a30423] | 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 ); } |
---|
[9f7285e] | 139 | float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf( x, y ); } |
---|
[8a30423] | 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 |
---|
[a1edafa] | 143 | |
---|
[108b2c7] | 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 ); |
---|
[8a30423] | 156 | } // distribution |
---|
[8dbfb7e] | 157 | |
---|
[8a30423] | 158 | static inline { |
---|
[24d6572] | 159 | int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; } |
---|
[8a30423] | 160 | long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
[88ac8672] | 161 | long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; } |
---|
[24d6572] | 162 | unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; } |
---|
[8a30423] | 163 | unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; } |
---|
[88ac8672] | 164 | unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; } |
---|
[8a30423] | 165 | } // distribution |
---|
[a1edafa] | 166 | |
---|
[9f7285e] | 167 | |
---|
| 168 | // enumeration implementation |
---|
[85855b0] | 169 | |
---|
[b006c51e] | 170 | forall( E ) trait Bounded { |
---|
[9f7285e] | 171 | E lowerBound( void ); |
---|
| 172 | E upperBound( void ); |
---|
[b006c51e] | 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 | |
---|
[532c0cd] | 191 | static inline |
---|
[eae8b37] | 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 ) ); |
---|
[9f7285e] | 207 | return succ_unsafe( e ); |
---|
[eae8b37] | 208 | } |
---|
| 209 | |
---|
| 210 | E pred( E e ) { |
---|
| 211 | E lower = lowerBound(); |
---|
[9f7285e] | 212 | if ( fromInstance( e ) <= fromInstance( lower ) ) |
---|
[eae8b37] | 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 |
---|
[532c0cd] | 225 | forall( E | CfaEnum( E ) ) { |
---|
| 226 | int ?==?( E l, E r ) { return posn( l ) == posn( r ); } |
---|
[eae8b37] | 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 ); } |
---|
[532c0cd] | 232 | } |
---|
[eae8b37] | 233 | |
---|
[532c0cd] | 234 | static inline |
---|
| 235 | forall( E | Serial( E ) ) { |
---|
| 236 | E ++?( E & l ) { |
---|
| 237 | int pos = fromInstance( l ); |
---|
[9f7285e] | 238 | l = fromInt_unsafe( pos + 1 ); |
---|
[eae8b37] | 239 | return l; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | E --?( E & l ) { |
---|
[532c0cd] | 243 | int pos = fromInstance( l ); |
---|
[9f7285e] | 244 | l = fromInt_unsafe( pos - 1 ); |
---|
[eae8b37] | 245 | return l; |
---|
| 246 | } |
---|
| 247 | |
---|
[50be6444] | 248 | E ?+=?( E & l, one_t ) { |
---|
[532c0cd] | 249 | int pos = fromInstance( l ); |
---|
[9f7285e] | 250 | l = fromInt_unsafe( pos + 1 ); |
---|
[eae8b37] | 251 | return l; |
---|
| 252 | } |
---|
| 253 | |
---|
[50be6444] | 254 | E ?-=?( E & l, one_t ) { |
---|
[532c0cd] | 255 | int pos = fromInstance( l ); |
---|
[9f7285e] | 256 | l = fromInt_unsafe( pos - 1 ); |
---|
[eae8b37] | 257 | return l; |
---|
| 258 | } |
---|
| 259 | |
---|
[50be6444] | 260 | E ?+=?( E & l, int i ) { |
---|
[532c0cd] | 261 | int pos = fromInstance( l ); |
---|
[9f7285e] | 262 | l = fromInt_unsafe( pos + i ); |
---|
[eae8b37] | 263 | return l; |
---|
| 264 | } |
---|
| 265 | |
---|
[50be6444] | 266 | E ?-=?( E & l, int i ) { |
---|
[532c0cd] | 267 | int pos = fromInstance( l ); |
---|
[9f7285e] | 268 | l = fromInt_unsafe( pos - i ); |
---|
[eae8b37] | 269 | return l; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | E ?++( E & l ) { |
---|
[532c0cd] | 273 | int pos = fromInstance( l ); |
---|
[9f7285e] | 274 | l = fromInt_unsafe( pos + 1 ); |
---|
| 275 | return fromInt_unsafe( pos ); |
---|
[eae8b37] | 276 | } |
---|
| 277 | |
---|
| 278 | E ?--( E & l ) { |
---|
[532c0cd] | 279 | int pos = fromInstance( l ); |
---|
[9f7285e] | 280 | l = fromInt_unsafe( pos - 1 ); |
---|
| 281 | return fromInt_unsafe( pos ); |
---|
[eae8b37] | 282 | } |
---|
| 283 | } |
---|
| 284 | |
---|
[a1edafa] | 285 | // Local Variables: // |
---|
| 286 | // mode: c // |
---|
| 287 | // tab-width: 4 // |
---|
| 288 | // End: // |
---|