source: libcfa/prelude/builtins.c @ ab5498ec

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ab5498ec was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 6.0 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
[c960331]11// Last Modified By : Andrew Beach
12// Last Modified On : Tue Oct 27 14:42:00 2020
13// Update Count     : 111
[a5f0529]14//
[a1edafa]15
[c960331]16#define __cforall_builtins__
17
[b867278]18// type that wraps a pointer and a destructor-like function - used in generating implicit destructor calls for struct members in user-defined functions
19// Note: needs to occur early, because it is used to generate destructor calls during code generation
[fd54fef]20forall(T &)
[b867278]21struct __Destructor {
22        T * object;
23        void (*dtor)(T *);
24};
25
26// defined destructor in the case that non-generated code wants to use __Destructor
[fd54fef]27forall(T &)
[b867278]28static inline void ^?{}(__Destructor(T) & x) {
29        if (x.object && x.dtor) {
30                x.dtor(x.object);
31        }
32}
33
34// easy interface into __Destructor's destructor for easy codegen purposes
35extern "C" {
[fd54fef]36        forall(T &)
[b867278]37        static inline void __destroy_Destructor(__Destructor(T) * dtor) {
38                ^(*dtor){};
39        }
40}
41
[a1edafa]42// exception implementation
43
[36982fc]44typedef unsigned long long __cfaabi_abi_exception_type_t;
[fa4805f]45
[7726839]46#include <limits.h>                                                                             // CHAR_BIT
[bf71cfd]47#include "../src/virtual.h"
48#include "../src/exception.h"
[a1edafa]49
[169d944]50void exit( int status, const char fmt[], ... ) __attribute__ (( format(printf, 2, 3), __nothrow__, __leaf__, __noreturn__ ));
[f79cdf8]51void abort( const char fmt[], ... ) __attribute__ (( format(printf, 1, 2), __nothrow__, __leaf__, __noreturn__ ));
[169d944]52
[fd54fef]53forall(T &)
[427854b]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
[fd54fef]66trait is_generator(T &) {
[427854b]67      void main(T & this);
68      $generator * get_generator(T & this);
69};
70
[fd54fef]71forall(T & | is_generator(T))
[427854b]72static inline T & resume(T & gen) {
73        main(gen);
74        return gen;
75}
76
[0d8266c]77// implicit increment, decrement if += defined, and implicit not if != defined
[d2887f7]78
[8a30423]79static inline {
[fd54fef]80        forall( DT & | { DT & ?+=?( DT &, one_t ); } )
[8a30423]81        DT & ++?( DT & x ) { return x += 1; }
[d2887f7]82
[fd54fef]83        forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } )
[8a30423]84        DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; }
[d2887f7]85
[fd54fef]86        forall( DT & | { DT & ?-=?( DT &, one_t ); } )
[8a30423]87        DT & --?( DT & x ) { return x -= 1; }
[d2887f7]88
[fd54fef]89        forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } )
[8a30423]90        DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; }
[0d8266c]91
[fd54fef]92        forall( DT & | { int ?!=?( const DT &, zero_t ); } )
[0d8266c]93        int !?( const DT & x ) { return !( x != 0 ); }
[8a30423]94} // distribution
[7579ac0]95
96// universal typed pointer constant
[fd54fef]97static inline forall( DT & ) DT * intptr( uintptr_t addr ) { return (DT *)addr; }
[04423b53]98static inline forall( ftype FT ) FT * intptr( uintptr_t addr ) { return (FT *)addr; }
[a1edafa]99
[cf5af9c]100#if defined(__SIZEOF_INT128__)
[b56f55c]101// constructor for 128-bit numbers (all constants are unsigned as +/- are operators)
102static inline void ?{}( unsigned int128 & this, unsigned long int h, unsigned long int l ) {
103        this = (unsigned int128)h << 64 | (unsigned int128)l;
104} // ?{}
[cf5af9c]105#endif // __SIZEOF_INT128__
[b56f55c]106
[a1edafa]107// exponentiation operator implementation
108
109extern "C" {
110        float powf( float x, float y );
111        double pow( double x, double y );
112        long double powl( long double x, long double y );
113        float _Complex cpowf( float _Complex x, _Complex float z );
114        double _Complex cpow( double _Complex x, _Complex double z );
115        long double _Complex cpowl( long double _Complex x, _Complex long double z );
116} // extern "C"
117
[8a30423]118static inline {
119        float ?\?( float x, float y ) { return powf( x, y ); }
120        double ?\?( double x, double y ) { return pow( x, y ); }
121        long double ?\?( long double x, long double y ) { return powl( x, y ); }
122        float _Complex ?\?( float _Complex x, _Complex float y ) { return cpowf(x, y ); }
123        double _Complex ?\?( double _Complex x, _Complex double y ) { return cpow( x, y ); }
124        long double _Complex ?\?( long double _Complex x, _Complex long double y ) { return cpowl( x, y ); }
125} // distribution
[a1edafa]126
[7726839]127#define __CFA_BASE_COMP_1__() if ( ep == 1 ) return 1
128#define __CFA_BASE_COMP_2__() if ( ep == 2 ) return ep << (y - 1)
129#define __CFA_EXP_OVERFLOW__() if ( y >= sizeof(y) * CHAR_BIT ) return 0
130
[8dbfb7e]131#define __CFA_EXP__() \
[8a30423]132        if ( y == 0 ) return 1;                                                         /* convention */ \
[7726839]133        __CFA_BASE_COMP_1__();                                                          /* base case */ \
134        __CFA_BASE_COMP_2__();                                                          /* special case, positive shifting for integral types */ \
135        __CFA_EXP_OVERFLOW__();                                                         /* immediate overflow, negative exponent > 2^size-1 */ \
[8dbfb7e]136        typeof(ep) op = 1;                                                                      /* accumulate odd product */ \
137        for ( ; y > 1; y >>= 1 ) {                                                      /* squaring exponentiation, O(log2 y) */ \
138                if ( (y & 1) == 1 ) op = op * ep;                               /* odd ? */ \
139                ep = ep * ep; \
140        } \
141        return ep * op
142
[8a30423]143static inline {
144        long int ?\?( int ep, unsigned int y ) { __CFA_EXP__(); }
145        long int ?\?( long int ep, unsigned long int y ) { __CFA_EXP__(); }
146        // unsigned computation may be faster and larger
147        unsigned long int ?\?( unsigned int ep, unsigned int y ) { __CFA_EXP__(); }
148        unsigned long int ?\?( unsigned long int ep, unsigned long int y ) { __CFA_EXP__(); }
149} // distribution
[a1edafa]150
[8dbfb7e]151#undef __CFA_BASE_COMP_1__
152#undef __CFA_BASE_COMP_2__
153#undef __CFA_EXP_OVERFLOW__
154#define __CFA_BASE_COMP_1__()
155#define __CFA_BASE_COMP_2__()
156#define __CFA_EXP_OVERFLOW__()
[e472d54]157
[fd54fef]158static inline forall( OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) {
[8a30423]159        OT ?\?( OT ep, unsigned int y ) { __CFA_EXP__(); }
160        OT ?\?( OT ep, unsigned long int y ) { __CFA_EXP__(); }
161} // distribution
[8dbfb7e]162
163#undef __CFA_BASE_COMP_1__
164#undef __CFA_BASE_COMP_2__
165#undef __CFA_EXP_OVERFLOW__
[a1edafa]166
[8a30423]167static inline {
168        long int ?\=?( long int & x, unsigned long int y ) { x = x \ y; return x; }
169        unsigned long int ?\=?( unsigned long int & x, unsigned long int y ) { x = x \ y; return x; }
170        int ?\=?( int & x, unsigned long int y ) { x = x \ y; return x; }
171        unsigned int ?\=?( unsigned int & x, unsigned long int y ) { x = x \ y; return x; }
172} // distribution
[a1edafa]173
174// Local Variables: //
175// mode: c //
176// tab-width: 4 //
177// End: //
Note: See TracBrowser for help on using the repository browser.