source: libcfa/prelude/builtins.c@ fbb5bdd

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

formatting, temporarily remove quasi_void

  • Property mode set to 100644
File size: 8.5 KB
Line 
1//
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.
6//
7// builtins.c --
8//
9// Author : Peter A. Buhr
10// Created On : Fri Jul 21 16:21:03 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Dec 8 09:01:23 2024
13// Update Count : 149
14//
15
16#define __cforall_builtins__
17
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 &)
22struct __Destructor {
23 T * object;
24 void (*dtor)( T * );
25};
26
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 );
32 }
33}
34
35// Easy interface into __Destructor's destructor for easy codegen purposes.
36extern "C" {
37 forall( T &)
38 static inline void __destroy_Destructor( __Destructor(T) * dtor ) {
39 ^(*dtor){};
40 }
41}
42
43// exception implementation
44
45typedef unsigned long long __cfaabi_abi_exception_type_t;
46
47#include "../src/virtual.h"
48#include "../src/exception.h"
49
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__ ));
52
53forall( T &)
54static inline T & identity( T & i ) {
55 return i;
56}
57
58// generator support
59struct generator$ {
60 inline int;
61};
62
63static inline void ?{}( generator$ & this ) { ((int&)this) = 0; }
64static inline void ^?{}( generator$ &) {}
65
66forall( T & )
67trait is_generator {
68 void main( T & this );
69 generator$ * get_generator( T & this );
70};
71
72forall( T & | is_generator( T ) )
73static inline T & resume( T & gen ) {
74 main( gen );
75 return gen;
76}
77
78// implicit increment, decrement if += defined, and implicit not if != defined
79
80// C11 reference manual Section 6.5.16 (page 101): "An assignment expression has the value of the left operand after the
81// assignment, but is not an lvalue." Hence, return a value not a reference.
82static inline {
83 forall( T | { T ?+=?( T &, one_t ); } )
84 T ++?( T & x ) { return x += 1; }
85
86 forall( T | { T ?+=?( T &, one_t ); } )
87 T ?++( T & x ) { T tmp = x; x += 1; return tmp; }
88
89 forall( T | { T ?-=?( T &, one_t ); } )
90 T --?( T & x ) { return x -= 1; }
91
92 forall( T | { T ?-=?( T &, one_t ); } )
93 T ?--( T & x ) { T tmp = x; x -= 1; return tmp; }
94
95 forall( T | { int ?!=?( T, zero_t ); } )
96 int !?( T & x ) { return !( x != 0 ); }
97} // distribution
98
99// universal typed pointer constant
100static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
101static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; }
102
103#if defined(__SIZEOF_INT128__)
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} // ?{}
108#endif // __SIZEOF_INT128__
109
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
114
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
126static inline { // wrappers
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 ); }
130 float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf( x, y ); }
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
134
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 );
147} // distribution
148
149static inline {
150 int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; }
151 long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
152 long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; }
153 unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; }
154 unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
155 unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; }
156} // distribution
157
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
166
167forall( E ) trait Bounded {
168 E lowerBound( void );
169 E upperBound( void );
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
179static inline forall( E | Serial( E ) ) {
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
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 ) );
211 return succ_unsafe( e );
212 }
213
214 E pred( E e ) {
215 E lower = lowerBound();
216 if ( fromInstance( e ) <= fromInstance( lower ) )
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
230forall( E | CfaEnum( E ) | Serial( E ) ) {
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
239 int pos = posn( l );
240 l = fromInt_unsafe( pos + 1 );
241 return l;
242 }
243
244 E --?( E & l ) {
245 int pos = posn( l );
246 l = fromInt_unsafe( pos - 1 );
247 return l;
248 }
249
250 E ?+=? ( E & l, one_t ) {
251 int pos = posn( l );
252 l = fromInt_unsafe( pos + 1 );
253 return l;
254 }
255
256 E ?-=? ( E & l, one_t ) {
257 int pos = posn( l );
258 l = fromInt_unsafe( pos - 1 );
259 return l;
260 }
261
262 E ?+=? ( E & l, int i ) {
263 int pos = posn( l );
264 l = fromInt_unsafe( pos + i );
265 return l;
266 }
267
268 E ?-=? ( E & l, int i ) {
269 int pos = posn( l );
270 l = fromInt_unsafe( pos - i );
271 return l;
272 }
273
274 E ?++( E & l ) {
275 int pos = posn( l );
276 l = fromInt_unsafe( pos + 1 );
277 return fromInt_unsafe( pos );
278 }
279
280 E ?--( E & l ) {
281 int pos = posn( l );
282 l = fromInt_unsafe( pos - 1 );
283 return fromInt_unsafe( pos );
284 }
285}
286
287// Local Variables: //
288// mode: c //
289// tab-width: 4 //
290// End: //
Note: See TracBrowser for help on using the repository browser.