[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 & )
|
---|
[4f4ae60] | 29 | inline void ^?{}( __Destructor(T) & x ) {
|
---|
[9f7285e] | 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 & )
|
---|
[4f4ae60] | 54 | inline T & identity( T & i ) {
|
---|
[427854b] | 55 | return i;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | // generator support
|
---|
[e84ab3d] | 59 | struct generator$ {
|
---|
[427854b] | 60 | inline int;
|
---|
| 61 | };
|
---|
| 62 |
|
---|
[4f4ae60] | 63 | inline void ?{}( generator$ & this ) { ((int&)this) = 0; }
|
---|
| 64 | inline void ^?{}( generator$ & ) {}
|
---|
[427854b] | 65 |
|
---|
[8a97248] | 66 | forall( T & )
|
---|
| 67 | trait is_generator {
|
---|
[5780d0d] | 68 | void main( T & this );
|
---|
| 69 | generator$ * get_generator( T & this );
|
---|
[427854b] | 70 | };
|
---|
| 71 |
|
---|
[9f7285e] | 72 | forall( T & | is_generator( T ) )
|
---|
[4f4ae60] | 73 | inline T & resume( T & gen ) {
|
---|
[9f7285e] | 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.
|
---|
[4f4ae60] | 95 | 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)
|
---|
[4f4ae60] | 118 | inline void ?{}( unsigned int128 & this, unsigned long int h, unsigned long int l ) {
|
---|
[b56f55c] | 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 |
|
---|
[4f4ae60] | 135 | inline {
|
---|
[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 |
|
---|
[4f4ae60] | 158 | 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 |
|
---|
[4f4ae60] | 191 | inline forall( E | Serial( E ) ) {
|
---|
[eae8b37] | 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 ) );
|
---|
[9f7285e] | 206 | return succ_unsafe( e );
|
---|
[eae8b37] | 207 | }
|
---|
| 208 |
|
---|
| 209 | E pred( E e ) {
|
---|
| 210 | E lower = lowerBound();
|
---|
[9f7285e] | 211 | if ( fromInstance( e ) <= fromInstance( lower ) )
|
---|
[eae8b37] | 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 |
|
---|
[4f4ae60] | 223 | inline forall( E | CfaEnum( E ) ) {
|
---|
[532c0cd] | 224 | int ?==?( E l, E r ) { return posn( l ) == posn( r ); }
|
---|
[eae8b37] | 225 | int ?!=?( E l, E r ) { return posn( l ) != posn( r ); }
|
---|
| 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 ); }
|
---|
[532c0cd] | 230 | }
|
---|
[eae8b37] | 231 |
|
---|
[4f4ae60] | 232 | inline forall( E | Serial( E ) ) {
|
---|
[50be6444] | 233 | E ?+=?( E & l, one_t ) {
|
---|
[532c0cd] | 234 | int pos = fromInstance( l );
|
---|
[9f7285e] | 235 | l = fromInt_unsafe( pos + 1 );
|
---|
[eae8b37] | 236 | return l;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[50be6444] | 239 | E ?-=?( E & l, one_t ) {
|
---|
[532c0cd] | 240 | int pos = fromInstance( l );
|
---|
[9f7285e] | 241 | l = fromInt_unsafe( pos - 1 );
|
---|
[eae8b37] | 242 | return l;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[50be6444] | 245 | E ?+=?( E & l, int i ) {
|
---|
[532c0cd] | 246 | int pos = fromInstance( l );
|
---|
[9f7285e] | 247 | l = fromInt_unsafe( pos + i );
|
---|
[eae8b37] | 248 | return l;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[50be6444] | 251 | E ?-=?( E & l, int i ) {
|
---|
[532c0cd] | 252 | int pos = fromInstance( l );
|
---|
[9f7285e] | 253 | l = fromInt_unsafe( pos - i );
|
---|
[eae8b37] | 254 | return l;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[a1edafa] | 258 | // Local Variables: //
|
---|
| 259 | // mode: c //
|
---|
| 260 | // tab-width: 4 //
|
---|
| 261 | // End: //
|
---|