source: libcfa/prelude/builtins.c @ b867278

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since b867278 was b867278, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Move Destructor to beginning of builtins.c

  • Property mode set to 100644
File size: 5.4 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 Aug  5 21:40:38 2018
13// Update Count     : 20
14//
15
16// type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions
17// Note: needs to occur early, because it is used to generate destructor calls during code generation
18forall(dtype T)
19struct __Destructor {
20        T * object;
21        void (*dtor)(T *);
22};
23
24// defined destructor in the case that non-generated code wants to use __Destructor
25forall(dtype T)
26static inline void ^?{}(__Destructor(T) & x) {
27        if (x.object && x.dtor) {
28                x.dtor(x.object);
29        }
30}
31
32// easy interface into __Destructor's destructor for easy codegen purposes
33extern "C" {
34        forall(dtype T)
35        static inline void __destroy_Destructor(__Destructor(T) * dtor) {
36                ^(*dtor){};
37        }
38}
39
40// exception implementation
41
42typedef unsigned long long __cfaabi_abi_exception_type_t;
43
44#include "../src/virtual.h"
45#include "../src/exception.h"
46
47void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
48void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
49
50// increment/decrement unification
51
52static inline forall( dtype T | { T& ?+=?( T&, one_t ); } )
53T& ++? ( T& x ) { return x += 1; }
54
55static inline forall( dtype T | sized(T) | { void ?{}( T&, T ); void ^?{}( T& ); T& ?+=?( T&, one_t ); } )
56T& ?++ ( T& x ) { T tmp = x; x += 1; return tmp; }
57
58static inline forall( dtype T | { T& ?-=?( T&, one_t ); } )
59T& --? ( T& x ) { return x -= 1; }
60
61static inline forall( dtype T | sized(T) | { void ?{}( T&, T ); void ^?{}( T& ); T& ?-=?( T&, one_t ); } )
62T& ?-- ( T& x ) { T tmp = x; x -= 1; return tmp; }
63
64// exponentiation operator implementation
65
66extern "C" {
67        float powf( float x, float y );
68        double pow( double x, double y );
69        long double powl( long double x, long double y );
70        float _Complex cpowf( float _Complex x, _Complex float z );
71        double _Complex cpow( double _Complex x, _Complex double z );
72        long double _Complex cpowl( long double _Complex x, _Complex long double z );
73} // extern "C"
74
75static inline float ?\?( float x, float y ) { return powf( x, y ); }
76static inline double ?\?( double x, double y ) { return pow( x, y ); }
77static inline long double ?\?( long double x, long double y ) { return powl( x, y ); }
78static inline float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
79static inline double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
80static inline long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
81
82static inline long int ?\?( long int ep, unsigned long int y ) { // disallow negative exponent
83        if ( y == 0 ) return 1;                                                         // base case
84        if ( ep == 2 ) return ep << (y - 1);                            // special case, positive shifting only
85        typeof( ep ) op = 1;                                                            // accumulate odd product
86        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
87                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
88                ep *= ep;
89        } // for
90        return ep * op;
91} // ?\?
92
93static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); } )
94T ?\?( T ep, unsigned long int y ) {
95        if ( y == 0 ) return 1;
96        T op = 1;
97        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
98                if ( (y & 1) == 1 ) op = op * ep;                               // odd ?
99                ep = ep * ep;
100        } // for
101        return ep * op;
102} // ?\?
103
104// unsigned computation may be faster and larger
105static inline unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { // disallow negative exponent
106        if ( y == 0 ) return 1;                                                         // base case
107        if ( ep == 2 ) return ep << (y - 1);                            // special case, positive shifting only
108        typeof( ep ) op = 1;                                                            // accumulate odd product
109        for ( ; y > 1; y >>= 1 ) {                                                      // squaring exponentiation, O(log2 y)
110                if ( (y & 1) == 1 ) op *= ep;                                   // odd ?
111                ep *= ep;
112        } // for
113        return ep * op;
114} // ?\?
115
116static inline double ?\?( long int x, signed long int y ) {     // allow negative exponent
117        if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
118        else return 1.0 / x \ (unsigned int)(-y);
119} // ?\?
120
121// FIXME (x \ (unsigned long int)y) relies on X ?\?(T, unsigned long) a function that is neither
122// defined, nor passed as an assertion parameter. Without user-defined conversions, cannot specify
123// X as a type that casts to double, yet it doesn't make sense to write functions with that type
124// signature where X is double.
125
126// static inline forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T, T ); double ?/?( double, T ); } )
127// double ?\?( T x, signed long int y ) {
128//     if ( y >=  0 ) return (double)(x \ (unsigned long int)y);
129//     else return 1.0 / x \ (unsigned long int)(-y);
130// } // ?\?
131
132static inline long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
133static inline unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
134static inline int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; }
135static inline unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; }
136
137// Local Variables: //
138// mode: c //
139// tab-width: 4 //
140// End: //
Note: See TracBrowser for help on using the repository browser.