- Timestamp:
- Dec 8, 2024, 9:02:49 AM (10 days ago)
- Branches:
- master
- Children:
- bc999b7
- Parents:
- 6503ef4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/prelude/builtins.c
r6503ef4 r9f7285e 10 10 // Created On : Fri Jul 21 16:21:03 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Nov 8 17:07:15202413 // Update Count : 14 412 // Last Modified On : Sun Dec 8 09:01:23 2024 13 // Update Count : 149 14 14 // 15 15 16 16 #define __cforall_builtins__ 17 17 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 20 forall(T &) 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 &) 21 22 struct __Destructor { 22 23 T * object; 23 void (*dtor)( T *);24 }; 25 26 // defined destructor in the case that non-generated code wants to use __Destructor27 forall( T &)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 purposes24 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. 35 36 extern "C" { 36 forall( T &)37 static inline void __destroy_Destructor( __Destructor(T) * dtor) {37 forall( T &) 38 static inline void __destroy_Destructor( __Destructor(T) * dtor ) { 38 39 ^(*dtor){}; 39 40 } … … 47 48 #include "../src/exception.h" 48 49 49 void exit( int status, const char fmt[], ... ) __attribute__ (( format( printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));50 void abort( const char fmt[], ... ) __attribute__ (( format( printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));51 52 forall( T &)53 static inline T & identity( T & i) {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 ) { 54 55 return i; 55 56 } … … 60 61 }; 61 62 62 static inline void ?{}( generator$ & this) { ((int&)this) = 0; }63 static inline void ^?{}( generator$ &) {}63 static inline void ?{}( generator$ & this ) { ((int&)this) = 0; } 64 static inline void ^?{}( generator$ &) {} 64 65 65 66 forall( T & ) 66 67 trait is_generator { 67 void main( T & this);68 generator$ * get_generator( T & this);69 }; 70 71 forall( T & | is_generator(T))72 static inline T & resume( T & gen) {73 main( gen);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 ); 74 75 return gen; 75 76 } … … 77 78 // implicit increment, decrement if += defined, and implicit not if != defined 78 79 79 // C11 reference manual Section 6.5.16 (page 101): "An assignment expression has the value of the left operand after the80 // C11 reference manual Section 6.5.16 (page 101): "An assignment expression has the value of the left operand after the 80 81 // assignment, but is not an lvalue." Hence, return a value not a reference. 81 82 static inline { … … 110 111 // forall( T | { void ?{}( T &, zero_t ); void ?{}( T &, one_t ); T ?+=?( T &, T ); T ?-=?( T &, T ); int ?<?( T, T ); } ) 111 112 // static inline T __for_control_index_constraints__( T t ) { return t; } 113 112 114 113 115 // exponentiation operator implementation … … 122 124 } // extern "C" 123 125 124 static inline { 126 static inline { // wrappers 125 127 float ?\?( float x, float y ) { return powf( x, y ); } 126 128 double ?\?( double x, double y ) { return pow( x, y ); } 127 129 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 ); }130 float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf( x, y ); } 129 131 double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); } 130 132 long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); } … … 154 156 } // distribution 155 157 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; } 158 // struct quasi_void {}; 159 // static inline void ?{}( quasi_void &) {} 160 // static inline void ?{}( quasi_void &, quasi_void ) {} 161 // static inline void ^?{}( quasi_void &) {} 162 // static inline quasi_void ?=?( quasi_void &, quasi_void & _src ) { return _src; } 163 164 165 // enumeration implementation 161 166 162 167 forall( E ) trait Bounded { 163 E lowerBound( void);164 E upperBound( void);168 E lowerBound( void ); 169 E upperBound( void ); 165 170 }; 166 171 … … 204 209 if ( fromInstance( e ) >= fromInstance( upper ) ) 205 210 abort( "call to succ() exceeds enumeration upper bound of %d.", fromInstance( upper ) ); 206 return succ_unsafe( e);211 return succ_unsafe( e ); 207 212 } 208 213 209 214 E pred( E e ) { 210 215 E lower = lowerBound(); 211 if ( fromInstance( e ) <= fromInstance( lower ) )216 if ( fromInstance( e ) <= fromInstance( lower ) ) 212 217 abort( "call to pred() exceeds enumeration lower bound of %d.", fromInstance( lower ) ); 213 218 return pred_unsafe( e ); … … 223 228 224 229 static inline 225 forall( E | CfaEnum( E) | Serial(E) ) {230 forall( E | CfaEnum( E ) | Serial( E ) ) { 226 231 int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators 227 232 int ?!=?( E l, E r ) { return posn( l ) != posn( r ); } … … 232 237 233 238 E ++?( E & l ) { // increment operators 234 int pos = posn( l);235 l = fromInt_unsafe( pos+1);239 int pos = posn( l ); 240 l = fromInt_unsafe( pos + 1 ); 236 241 return l; 237 242 } 238 243 239 244 E --?( E & l ) { 240 int pos = posn( l);241 l = fromInt_unsafe( pos-1);245 int pos = posn( l ); 246 l = fromInt_unsafe( pos - 1 ); 242 247 return l; 243 248 } 244 249 245 250 E ?+=? ( E & l, one_t ) { 246 int pos = posn( l);247 l = fromInt_unsafe( pos+1);251 int pos = posn( l ); 252 l = fromInt_unsafe( pos + 1 ); 248 253 return l; 249 254 } 250 255 251 256 E ?-=? ( E & l, one_t ) { 252 int pos = posn( l);253 l = fromInt_unsafe( pos-1);257 int pos = posn( l ); 258 l = fromInt_unsafe( pos - 1 ); 254 259 return l; 255 260 } 256 261 257 262 E ?+=? ( E & l, int i ) { 258 int pos = posn( l);259 l = fromInt_unsafe( pos+i);263 int pos = posn( l ); 264 l = fromInt_unsafe( pos + i ); 260 265 return l; 261 266 } 262 267 263 268 E ?-=? ( E & l, int i ) { 264 int pos = posn( l);265 l = fromInt_unsafe( pos-i);269 int pos = posn( l ); 270 l = fromInt_unsafe( pos - i ); 266 271 return l; 267 272 } 268 273 269 274 E ?++( E & l ) { 270 int pos = posn( l);271 l = fromInt_unsafe( pos+1);272 return fromInt_unsafe( pos);275 int pos = posn( l ); 276 l = fromInt_unsafe( pos + 1 ); 277 return fromInt_unsafe( pos ); 273 278 } 274 279 275 280 E ?--( E & l ) { 276 int pos = posn( l);277 l = fromInt_unsafe( pos-1);278 return fromInt_unsafe( pos);281 int pos = posn( l ); 282 l = fromInt_unsafe( pos - 1 ); 283 return fromInt_unsafe( pos ); 279 284 } 280 285 }
Note: See TracChangeset
for help on using the changeset viewer.