source: libcfa/prelude/builtins.c@ fd0a1799

Last change on this file since fd0a1799 was 283fbdd, checked in by Andrew Beach <ajbeach@…>, 12 months ago

Removed some unused (commented) code from the builtins.

  • Property mode set to 100644
File size: 8.0 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
111// exponentiation operator implementation
112
113extern "C" {
114 float powf( float x, float y );
115 double pow( double x, double y );
116 long double powl( long double x, long double y );
117 float _Complex cpowf( float _Complex x, _Complex float z );
118 double _Complex cpow( double _Complex x, _Complex double z );
119 long double _Complex cpowl( long double _Complex x, _Complex long double z );
120} // extern "C"
121
122static inline { // wrappers
123 float ?\?( float x, float y ) { return powf( x, y ); }
124 double ?\?( double x, double y ) { return pow( x, y ); }
125 long double ?\?( long double x, long double y ) { return powl( x, y ); }
126 float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf( x, y ); }
127 double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
128 long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
129} // distribution
130
131int ?\?( int x, unsigned int y );
132long int ?\?( long int x, unsigned long int y );
133long long int ?\?( long long int x, unsigned long long int y );
134// unsigned computation may be faster and larger
135unsigned int ?\?( unsigned int x, unsigned int y );
136unsigned long int ?\?( unsigned long int x, unsigned long int y );
137unsigned long long int ?\?( unsigned long long int x, unsigned long long int y );
138
139forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
140 OT ?\?( OT x, unsigned int y );
141 OT ?\?( OT x, unsigned long int y );
142 OT ?\?( OT x, unsigned long long int y );
143} // distribution
144
145static inline {
146 int ?\=?( int & x, unsigned int y ) { x = x \ y; return x; }
147 long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
148 long long int ?\=?( long long int & x, unsigned long long int y ) { x = x \ y; return x; }
149 unsigned int ?\=?( unsigned int & x, unsigned int y ) { x = x \ y; return x; }
150 unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
151 unsigned long long int ?\=?( unsigned long long int & x, unsigned long long int y ) { x = x \ y; return x; }
152} // distribution
153
154
155// enumeration implementation
156
157forall( E ) trait Bounded {
158 E lowerBound( void );
159 E upperBound( void );
160};
161
162forall( E | Bounded( E ) ) trait Serial {
163 int fromInstance( E e );
164 E fromInt_unsafe( int i );
165 E succ_unsafe( E e );
166 E pred_unsafe( E e );
167};
168
169static inline forall( E | Serial( E ) ) {
170 E fromInt( int i );
171 E succ( E e );
172 E pred( E e );
173 int Countof( E );
174}
175
176forall( E ) trait CfaEnum {
177 const char * label( E e );
178 int posn( E e );
179};
180
181forall( E, V | CfaEnum( E ) ) trait TypedEnum {
182 V value( E e );
183};
184
185static inline {
186forall( E | Serial( E ) ) {
187 E fromInt( int i ) {
188 E upper = upperBound();
189 E lower = lowerBound();
190 // It is okay to overflow as overflow will be theoretically caught by the other bound
191 if ( i < fromInstance( lower ) || i > fromInstance( upper ) )
192 abort( "call to fromInt has index %d outside of enumeration range %d-%d.",
193 i, fromInstance( lower ), fromInstance( upper ) );
194 return fromInt_unsafe( i );
195 }
196
197 E succ( E e ) {
198 E upper = upperBound();
199 if ( fromInstance( e ) >= fromInstance( upper ) )
200 abort( "call to succ() exceeds enumeration upper bound of %d.", fromInstance( upper ) );
201 return succ_unsafe( e );
202 }
203
204 E pred( E e ) {
205 E lower = lowerBound();
206 if ( fromInstance( e ) <= fromInstance( lower ) )
207 abort( "call to pred() exceeds enumeration lower bound of %d.", fromInstance( lower ) );
208 return pred_unsafe( e );
209 }
210
211 int Countof( E ) {
212 E upper = upperBound();
213 E lower = lowerBound();
214 return fromInstance( upper ) + fromInstance( lower ) + 1;
215 }
216}
217}
218
219static inline
220forall( E | CfaEnum( E ) | Serial( E ) ) {
221 int ?==?( E l, E r ) { return posn( l ) == posn( r ); } // relational operators
222 int ?!=?( E l, E r ) { return posn( l ) != posn( r ); }
223 int ?<?( E l, E r ) { return posn( l ) < posn( r ); }
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
228 E ++?( E & l ) { // increment operators
229 int pos = posn( l );
230 l = fromInt_unsafe( pos + 1 );
231 return l;
232 }
233
234 E --?( E & l ) {
235 int pos = posn( l );
236 l = fromInt_unsafe( pos - 1 );
237 return l;
238 }
239
240 E ?+=?( E & l, one_t ) {
241 int pos = posn( l );
242 l = fromInt_unsafe( pos + 1 );
243 return l;
244 }
245
246 E ?-=?( E & l, one_t ) {
247 int pos = posn( l );
248 l = fromInt_unsafe( pos - 1 );
249 return l;
250 }
251
252 E ?+=?( E & l, int i ) {
253 int pos = posn( l );
254 l = fromInt_unsafe( pos + i );
255 return l;
256 }
257
258 E ?-=?( E & l, int i ) {
259 int pos = posn( l );
260 l = fromInt_unsafe( pos - i );
261 return l;
262 }
263
264 E ?++( E & l ) {
265 int pos = posn( l );
266 l = fromInt_unsafe( pos + 1 );
267 return fromInt_unsafe( pos );
268 }
269
270 E ?--( E & l ) {
271 int pos = posn( l );
272 l = fromInt_unsafe( pos - 1 );
273 return fromInt_unsafe( pos );
274 }
275}
276
277// Local Variables: //
278// mode: c //
279// tab-width: 4 //
280// End: //
Note: See TracBrowser for help on using the repository browser.