source: libcfa/prelude/builtins.c @ d908563

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d908563 was 933f32f, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Merge branch 'master' into cleanup-dtors

  • Property mode set to 100644
File size: 5.1 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
[169d944]11// Last Modified By : Peter A. Buhr
[8a30423]12// Last Modified On : Tue Mar 26 23:10:36 2019
13// Update Count     : 95
[a5f0529]14//
[a1edafa]15
[b867278]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
[a1edafa]40// exception implementation
41
[36982fc]42typedef unsigned long long __cfaabi_abi_exception_type_t;
[fa4805f]43
[7726839]44#include <limits.h>                                                                             // CHAR_BIT
[bf71cfd]45#include "../src/virtual.h"
46#include "../src/exception.h"
[a1edafa]47
[169d944]48void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
[f79cdf8]49void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
[169d944]50
[d2887f7]51// increment/decrement unification
52
[8a30423]53static inline {
54        forall( dtype DT | { DT & ?+=?( DT &, one_t ); } )
55        DT & ++?( DT & x ) { return x += 1; }
[d2887f7]56
[8a30423]57        forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )
58        DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; }
[d2887f7]59
[8a30423]60        forall( dtype DT | { DT & ?-=?( DT &, one_t ); } )
61        DT & --?( DT & x ) { return x -= 1; }
[d2887f7]62
[8a30423]63        forall( dtype DT | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )
64        DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; }
65} // distribution
[7579ac0]66
67// universal typed pointer constant
[8a30423]68// Compiler issue: there is a problem with anonymous types that do not have a size.
[8dbfb7e]69static inline forall( dtype DT | sized(DT) ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
[a1edafa]70
71// exponentiation operator implementation
72
73extern "C" {
74        float powf( float x, float y );
75        double pow( double x, double y );
76        long double powl( long double x, long double y );
77        float _Complex cpowf( float _Complex x, _Complex float z );
78        double _Complex cpow( double _Complex x, _Complex double z );
79        long double _Complex cpowl( long double _Complex x, _Complex long double z );
80} // extern "C"
81
[8a30423]82static inline {
83        float ?\?( float x, float y ) { return powf( x, y ); }
84        double ?\?( double x, double y ) { return pow( x, y ); }
85        long double ?\?( long double x, long double y ) { return powl( x, y ); }
86        float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
87        double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
88        long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
89} // distribution
[a1edafa]90
[7726839]91#define __CFA_BASE_COMP_1__() if ( ep == 1 ) return 1
92#define __CFA_BASE_COMP_2__() if ( ep == 2 ) return ep << (y - 1)
93#define __CFA_EXP_OVERFLOW__() if ( y >= sizeof(y) * CHAR_BIT ) return 0
94
[8dbfb7e]95#define __CFA_EXP__() \
[8a30423]96        if ( y == 0 ) return 1;                                                         /* convention */ \
[7726839]97        __CFA_BASE_COMP_1__();                                                          /* base case */ \
98        __CFA_BASE_COMP_2__();                                                          /* special case, positive shifting for integral types */ \
99        __CFA_EXP_OVERFLOW__();                                                         /* immediate overflow, negative exponent > 2^size-1 */ \
[8dbfb7e]100        typeof(ep) op = 1;                                                                      /* accumulate odd product */ \
101        for ( ; y > 1; y >>= 1 ) {                                                      /* squaring exponentiation, O(log2 y) */ \
102                if ( (y & 1) == 1 ) op = op * ep;                               /* odd ? */ \
103                ep = ep * ep; \
104        } \
105        return ep * op
106
[8a30423]107static inline {
108        long int ?\?( int ep, unsigned int y ) { __CFA_EXP__(); }
109        long int ?\?( long int ep, unsigned long int y ) { __CFA_EXP__(); }
110        // unsigned computation may be faster and larger
111        unsigned long int ?\?( unsigned int ep, unsigned int y ) { __CFA_EXP__(); }
112        unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { __CFA_EXP__(); }
113} // distribution
[a1edafa]114
[8dbfb7e]115#undef __CFA_BASE_COMP_1__
116#undef __CFA_BASE_COMP_2__
117#undef __CFA_EXP_OVERFLOW__
118#define __CFA_BASE_COMP_1__()
119#define __CFA_BASE_COMP_2__()
120#define __CFA_EXP_OVERFLOW__()
[e472d54]121
[8a30423]122static inline forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
123        OT ?\?( OT ep, unsigned int y ) { __CFA_EXP__(); }
124        OT ?\?( OT ep, unsigned long int y ) { __CFA_EXP__(); }
125} // distribution
[8dbfb7e]126
127#undef __CFA_BASE_COMP_1__
128#undef __CFA_BASE_COMP_2__
129#undef __CFA_EXP_OVERFLOW__
[a1edafa]130
[8a30423]131static inline {
132        long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
133        unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
134        int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; }
135        unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; }
136} // distribution
[a1edafa]137
138// Local Variables: //
139// mode: c //
140// tab-width: 4 //
141// End: //
Note: See TracBrowser for help on using the repository browser.