source: libcfa/prelude/builtins.c@ fbb5bdd

Last change on this file since fbb5bdd was 9f7285e, checked in by Peter A. Buhr <pabuhr@…>, 11 months ago

formatting, temporarily remove quasi_void

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[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
21forall( T &)
[b867278]22struct __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.
28forall( T &)
29static 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]36extern "C" {
[9f7285e]37 forall( T &)
38 static inline void __destroy_Destructor( __Destructor(T) * dtor ) {
[b867278]39 ^(*dtor){};
40 }
41}
42
[a1edafa]43// exception implementation
44
[36982fc]45typedef unsigned long long __cfaabi_abi_exception_type_t;
[fa4805f]46
[bf71cfd]47#include "../src/virtual.h"
48#include "../src/exception.h"
[a1edafa]49
[9f7285e]50void exit( int status, const char fmt[], ... ) __attribute__ (( format( printf, 2, 3 ), __nothrow__, __leaf__, __noreturn__ ));
51void abort( const char fmt[], ... ) __attribute__ (( format( printf, 1, 2 ), __nothrow__, __leaf__, __noreturn__ ));
[169d944]52
[9f7285e]53forall( T &)
54static inline T & identity( T & i ) {
[427854b]55 return i;
56}
57
58// generator support
[e84ab3d]59struct generator$ {
[427854b]60 inline int;
61};
62
[9f7285e]63static inline void ?{}( generator$ & this ) { ((int&)this) = 0; }
64static inline void ^?{}( generator$ &) {}
[427854b]65
[8a97248]66forall( T & )
67trait is_generator {
[9f7285e]68 void main( T & this );
69 generator$ * get_generator( T & this );
[427854b]70};
71
[9f7285e]72forall( T & | is_generator( T ) )
73static 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]82static 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]100static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
[04423b53]101static 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)
105static 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
[c99a0d1]110// for-control index constraints
111// forall( T | { void ?{}( T &, zero_t ); void ?{}( T &, one_t ); T ?+=?( T &, T ); T ?-=?( T &, T ); int ?<?( T, T ); } )
112// static inline T __for_control_index_constraints__( T t ) { return t; }
113
[9f7285e]114
[a1edafa]115// exponentiation operator implementation
116
117extern "C" {
118 float powf( float x, float y );
119 double pow( double x, double y );
120 long double powl( long double x, long double y );
121 float _Complex cpowf( float _Complex x, _Complex float z );
122 double _Complex cpow( double _Complex x, _Complex double z );
123 long double _Complex cpowl( long double _Complex x, _Complex long double z );
124} // extern "C"
125
[9f7285e]126static inline { // wrappers
[8a30423]127 float ?\?( float x, float y ) { return powf( x, y ); }
128 double ?\?( double x, double y ) { return pow( x, y ); }
129 long double ?\?( long double x, long double y ) { return powl( x, y ); }
[9f7285e]130 float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf( x, y ); }
[8a30423]131 double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
132 long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
133} // distribution
[a1edafa]134
[108b2c7]135int ?\?( int x, unsigned int y );
136long int ?\?( long int x, unsigned long int y );
137long long int ?\?( long long int x, unsigned long long int y );
138// unsigned computation may be faster and larger
139unsigned int ?\?( unsigned int x, unsigned int y );
140unsigned long int ?\?( unsigned long int x, unsigned long int y );
141unsigned long long int ?\?( unsigned long long int x, unsigned long long int y );
142
143forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
144 OT ?\?( OT x, unsigned int y );
145 OT ?\?( OT x, unsigned long int y );
146 OT ?\?( OT x, unsigned long long int y );
[8a30423]147} // distribution
[8dbfb7e]148
[8a30423]149static inline {
[24d6572]150 int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; }
[8a30423]151 long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
[88ac8672]152 long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; }
[24d6572]153 unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; }
[8a30423]154 unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
[88ac8672]155 unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; }
[8a30423]156} // distribution
[a1edafa]157
[9f7285e]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
[85855b0]166
[b006c51e]167forall( E ) trait Bounded {
[9f7285e]168 E lowerBound( void );
169 E upperBound( void );
[b006c51e]170};
171
172forall( E | Bounded( E ) ) trait Serial {
173 int fromInstance( E e );
174 E fromInt_unsafe( int i );
175 E succ_unsafe( E e );
176 E pred_unsafe( E e );
177};
178
[eae8b37]179static inline forall( E | Serial( E ) ) {
[b006c51e]180 E fromInt( int i );
181 E succ( E e );
182 E pred( E e );
183 int Countof( E );
184}
185
186forall( E ) trait CfaEnum {
187 const char * label( E e );
188 int posn( E e );
189};
190
191forall( E, V | CfaEnum( E ) ) trait TypedEnum {
192 V value( E e );
193};
194
[eae8b37]195static inline {
196forall( E | Serial( E ) ) {
197 E fromInt( int i ) {
198 E upper = upperBound();
199 E lower = lowerBound();
200 // It is okay to overflow as overflow will be theoretically caught by the other bound
201 if ( i < fromInstance( lower ) || i > fromInstance( upper ) )
202 abort( "call to fromInt has index %d outside of enumeration range %d-%d.",
203 i, fromInstance( lower ), fromInstance( upper ) );
204 return fromInt_unsafe( i );
205 }
206
207 E succ( E e ) {
208 E upper = upperBound();
209 if ( fromInstance( e ) >= fromInstance( upper ) )
210 abort( "call to succ() exceeds enumeration upper bound of %d.", fromInstance( upper ) );
[9f7285e]211 return succ_unsafe( e );
[eae8b37]212 }
213
214 E pred( E e ) {
215 E lower = lowerBound();
[9f7285e]216 if ( fromInstance( e ) <= fromInstance( lower ) )
[eae8b37]217 abort( "call to pred() exceeds enumeration lower bound of %d.", fromInstance( lower ) );
218 return pred_unsafe( e );
219 }
220
221 int Countof( E ) {
222 E upper = upperBound();
223 E lower = lowerBound();
224 return fromInstance( upper ) + fromInstance( lower ) + 1;
225 }
226}
227}
228
229static inline
[9f7285e]230forall( E | CfaEnum( E ) | Serial( E ) ) {
[eae8b37]231 int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators
232 int ?!=?( E l, E r ) { return posn( l ) != posn( r ); }
233 int ?<?( E l, E r ) { return posn( l ) < posn( r ); }
234 int ?<=?( E l, E r ) { return posn( l ) <= posn( r ); }
235 int ?>?( E l, E r ) { return posn( l ) > posn( r ); }
236 int ?>=?( E l, E r ) { return posn( l ) >= posn( r ); }
237
238 E ++?( E & l ) { // increment operators
[9f7285e]239 int pos = posn( l );
240 l = fromInt_unsafe( pos + 1 );
[eae8b37]241 return l;
242 }
243
244 E --?( E & l ) {
[9f7285e]245 int pos = posn( l );
246 l = fromInt_unsafe( pos - 1 );
[eae8b37]247 return l;
248 }
249
250 E ?+=? ( E & l, one_t ) {
[9f7285e]251 int pos = posn( l );
252 l = fromInt_unsafe( pos + 1 );
[eae8b37]253 return l;
254 }
255
256 E ?-=? ( E & l, one_t ) {
[9f7285e]257 int pos = posn( l );
258 l = fromInt_unsafe( pos - 1 );
[eae8b37]259 return l;
260 }
261
262 E ?+=? ( E & l, int i ) {
[9f7285e]263 int pos = posn( l );
264 l = fromInt_unsafe( pos + i );
[eae8b37]265 return l;
266 }
267
268 E ?-=? ( E & l, int i ) {
[9f7285e]269 int pos = posn( l );
270 l = fromInt_unsafe( pos - i );
[eae8b37]271 return l;
272 }
273
274 E ?++( E & l ) {
[9f7285e]275 int pos = posn( l );
276 l = fromInt_unsafe( pos + 1 );
277 return fromInt_unsafe( pos );
[eae8b37]278 }
279
280 E ?--( E & l ) {
[9f7285e]281 int pos = posn( l );
282 l = fromInt_unsafe( pos - 1 );
283 return fromInt_unsafe( pos );
[eae8b37]284 }
285}
286
[a1edafa]287// Local Variables: //
288// mode: c //
289// tab-width: 4 //
290// End: //
Note: See TracBrowser for help on using the repository browser.