source: libcfa/prelude/builtins.c@ 96ac72c0

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 96ac72c0 was 0d8266c, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add polymorphic "not" (bang) to work with != 0

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