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 : Thu Dec 12 18:22:26 2024
|
---|
13 | // Update Count : 150
|
---|
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
|
---|
21 | forall( T & )
|
---|
22 | struct __Destructor {
|
---|
23 | T * object;
|
---|
24 | void (*dtor)( T * );
|
---|
25 | };
|
---|
26 |
|
---|
27 | // Defined destructor in the case that non-generated code wants to use __Destructor.
|
---|
28 | forall( T & )
|
---|
29 | 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.
|
---|
36 | extern "C" {
|
---|
37 | forall( T & )
|
---|
38 | static inline void __destroy_Destructor( __Destructor(T) * dtor ) {
|
---|
39 | ^(*dtor){};
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | // exception implementation
|
---|
44 |
|
---|
45 | typedef unsigned long long __cfaabi_abi_exception_type_t;
|
---|
46 |
|
---|
47 | #include "../src/virtual.h"
|
---|
48 | #include "../src/exception.h"
|
---|
49 |
|
---|
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 | inline T & identity( T & i ) {
|
---|
55 | return i;
|
---|
56 | }
|
---|
57 |
|
---|
58 | // generator support
|
---|
59 | struct generator$ {
|
---|
60 | inline int;
|
---|
61 | };
|
---|
62 |
|
---|
63 | inline void ?{}( generator$ & this ) { ((int&)this) = 0; }
|
---|
64 | inline void ^?{}( generator$ & ) {}
|
---|
65 |
|
---|
66 | forall( T & )
|
---|
67 | trait is_generator {
|
---|
68 | void main( T & this );
|
---|
69 | generator$ * get_generator( T & this );
|
---|
70 | };
|
---|
71 |
|
---|
72 | forall( T & | is_generator( T ) )
|
---|
73 | inline T & resume( T & gen ) {
|
---|
74 | main( gen );
|
---|
75 | return gen;
|
---|
76 | }
|
---|
77 |
|
---|
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) ...
|
---|
84 | forall( T * )
|
---|
85 | trait is_value {
|
---|
86 | void ?{}( T&, T );
|
---|
87 | T ?=?( T&, T );
|
---|
88 | void ^?{}( T& );
|
---|
89 | };
|
---|
90 |
|
---|
91 | // implicit increment, decrement if += defined, and implicit not if != defined
|
---|
92 |
|
---|
93 | // C11 reference manual Section 6.5.16 (page 101): "An assignment expression has the value of the left operand after the
|
---|
94 | // assignment, but is not an lvalue." Hence, return a value not a reference.
|
---|
95 | inline {
|
---|
96 | forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } )
|
---|
97 | T ++?( T & x ) { return x += 1; }
|
---|
98 |
|
---|
99 | forall( T& | is_value(T) | { T ?+=?( T &, one_t ); } )
|
---|
100 | T ?++( T & x ) { T tmp = x; x += 1; return tmp; }
|
---|
101 |
|
---|
102 | forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } )
|
---|
103 | T --?( T & x ) { return x -= 1; }
|
---|
104 |
|
---|
105 | forall( T& | is_value(T) | { T ?-=?( T &, one_t ); } )
|
---|
106 | T ?--( T & x ) { T tmp = x; x -= 1; return tmp; }
|
---|
107 |
|
---|
108 | forall( T& | is_value(T) | { int ?!=?( T, zero_t ); } )
|
---|
109 | int !?( T & x ) { return !( x != 0 ); }
|
---|
110 | } // distribution
|
---|
111 |
|
---|
112 | // universal typed pointer constant
|
---|
113 | static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
|
---|
114 | static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; }
|
---|
115 |
|
---|
116 | #if defined(__SIZEOF_INT128__)
|
---|
117 | // constructor for 128-bit numbers (all constants are unsigned as +/- are operators)
|
---|
118 | inline void ?{}( unsigned int128 & this, unsigned long int h, unsigned long int l ) {
|
---|
119 | this = (unsigned int128)h << 64 | (unsigned int128)l;
|
---|
120 | } // ?{}
|
---|
121 | #endif // __SIZEOF_INT128__
|
---|
122 |
|
---|
123 |
|
---|
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 |
|
---|
135 | inline {
|
---|
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 ); }
|
---|
139 | float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf( x, y ); }
|
---|
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
|
---|
143 |
|
---|
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 );
|
---|
156 | } // distribution
|
---|
157 |
|
---|
158 | inline {
|
---|
159 | int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; }
|
---|
160 | long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
|
---|
161 | long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; }
|
---|
162 | unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; }
|
---|
163 | unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
|
---|
164 | unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; }
|
---|
165 | } // distribution
|
---|
166 |
|
---|
167 |
|
---|
168 | // enumeration implementation
|
---|
169 |
|
---|
170 | forall( E ) trait Bounded {
|
---|
171 | E lowerBound( void );
|
---|
172 | E upperBound( void );
|
---|
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 |
|
---|
191 | inline forall( E | Serial( E ) ) {
|
---|
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 ) );
|
---|
206 | return succ_unsafe( e );
|
---|
207 | }
|
---|
208 |
|
---|
209 | E pred( E e ) {
|
---|
210 | E lower = lowerBound();
|
---|
211 | if ( fromInstance( e ) <= fromInstance( lower ) )
|
---|
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 |
|
---|
223 | inline forall( E | CfaEnum( E ) ) {
|
---|
224 | int ?==?( E l, E r ) { return posn( l ) == posn( r ); }
|
---|
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 ); }
|
---|
230 | }
|
---|
231 |
|
---|
232 | inline forall( E | Serial( E ) ) {
|
---|
233 | E ?+=?( E & l, one_t ) {
|
---|
234 | int pos = fromInstance( l );
|
---|
235 | l = fromInt_unsafe( pos + 1 );
|
---|
236 | return l;
|
---|
237 | }
|
---|
238 |
|
---|
239 | E ?-=?( E & l, one_t ) {
|
---|
240 | int pos = fromInstance( l );
|
---|
241 | l = fromInt_unsafe( pos - 1 );
|
---|
242 | return l;
|
---|
243 | }
|
---|
244 |
|
---|
245 | E ?+=?( E & l, int i ) {
|
---|
246 | int pos = fromInstance( l );
|
---|
247 | l = fromInt_unsafe( pos + i );
|
---|
248 | return l;
|
---|
249 | }
|
---|
250 |
|
---|
251 | E ?-=?( E & l, int i ) {
|
---|
252 | int pos = fromInstance( l );
|
---|
253 | l = fromInt_unsafe( pos - i );
|
---|
254 | return l;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | // Local Variables: //
|
---|
259 | // mode: c //
|
---|
260 | // tab-width: 4 //
|
---|
261 | // End: //
|
---|