Changeset 2b4daf2 for libcfa/src/bits


Ignore:
Timestamp:
Jan 7, 2021, 5:06:22 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
5ad381b
Parents:
42f6e07 (diff), 58fe85a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
libcfa/src/bits
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/bits/collection.hfa

    r42f6e07 r2b4daf2  
    11#pragma once
     2#include <stdio.h> // REMOVE THIS AFTER DEBUGGING
     3
    24
    35struct Colable {
    4         Colable * next;                                                                         // next node in the list
     6        struct Colable * next;                                                                          // next node in the list
    57        // invariant: (next != 0) <=> listed()
    68};
    7 
    8 inline {
     9#ifdef __cforall
     10static inline {
    911        // PUBLIC
    1012
     
    2830        }
    2931
    30         // wrappers to make Collection have T
    31         forall( dtype T ) {
    32                 T *& Next( T * n ) {
    33                         return (T *)Next( (Colable *)n );
    34                 }
    35 
    36                 bool listed( T * n ) {
    37                         return Next( (Colable *)n ) != 0p;
    38                 }
    39         } // distribution
     32        // // wrappers to make Collection have T
     33        // forall( dtype T ) {
     34        //      T *& Next( T * n ) {
     35        //              return (T *)Next( (Colable *)n );
     36        //      }
     37        // } // distribution
    4038} // distribution
    4139
     40forall( dtype T | { T *& Next ( T * ); } ) {
     41        bool listed( T * n ) {
     42                return Next( n ) != 0p;
     43        }
     44}
    4245
    4346struct Collection {
     
    4548};
    4649
    47 inline {
     50static inline {
    4851        // class invariant: root == 0 & empty() | *root in *this
    4952        void ?{}( Collection &, const Collection & ) = void; // no copy
     
    6568
    6669struct ColIter {
    67         void * curr;                                                                            // element to be returned by >>
     70        void * curr;                                                                            // element returned by |
    6871};
    6972
    70 inline {
     73static inline {
    7174        void ?{}( ColIter & colIter ) with( colIter ) {
    7275                curr = 0p;
     
    7982        } // distribution
    8083} // distribution
     84#endif
  • libcfa/src/bits/containers.hfa

    r42f6e07 r2b4daf2  
    3636        #define __small_array_t(T) __small_array(T)
    3737#else
    38         #define __small_array_t(T) struct __small_array
     38        #define __small_array_t(T) __small_array
    3939#endif
    4040
  • libcfa/src/bits/defs.hfa

    r42f6e07 r2b4daf2  
    2929#define __cfa_anonymous_object(x) inline struct x
    3030#else
    31 #define __cfa_anonymous_object(x) x __cfa_anonymous_object
     31#define __cfa_anonymous_object(x) struct x __cfa_anonymous_object
    3232#endif
    3333
  • libcfa/src/bits/locks.hfa

    r42f6e07 r2b4daf2  
    283283                void ^?{}(future_t &) {}
    284284
     285                void reset(future_t & this) {
     286                        // needs to be in 0p or 1p
     287                        __atomic_exchange_n( &this.ptr, 0p, __ATOMIC_SEQ_CST);
     288                }
     289
    285290                // check if the future is available
    286291                bool available( future_t & this ) {
     
    340345
    341346                // Mark the future as abandoned, meaning it will be deleted by the server
    342                 void abandon( future_t & this ) {
     347                bool abandon( future_t & this ) {
     348                        /* paranoid */ verify( this.ptr != 3p );
     349
     350                        // Mark the future as abandonned
    343351                        struct oneshot * got = __atomic_exchange_n( &this.ptr, 3p, __ATOMIC_SEQ_CST);
     352
     353                        // If the future isn't already fulfilled, let the server delete it
     354                        if( got == 0p ) return false;
    344355
    345356                        // got == 2p: the future is ready but the context hasn't fully been consumed
     
    347358                        if( got == 2p ) {
    348359                                while( this.ptr != 1p ) Pause();
    349                         }
    350                         return;
     360                                got = 1p;
     361                        }
     362
     363                        // The future is completed delete it now
     364                        /* paranoid */ verify( this.ptr != 1p );
     365                        free( &this );
     366                        return true;
    351367                }
    352368
  • libcfa/src/bits/queue.hfa

    r42f6e07 r2b4daf2  
    33#include "bits/collection.hfa"
    44
    5 forall( dtype T ) {
     5// A Queue(T) is a Collection(T) defining the ordering that nodes are returned by drop() in the same order from those
     6// added by add(). T must be a public descendant of uColable.
     7
     8// The implementation is a typical singly-linked list, except the next field of the last element points to itself
     9// instead of being null.
     10
     11forall( dtype T | { T *& Next ( T * ); } ) {
    612        struct Queue {
    713                inline Collection;                                                              // Plan 9 inheritance
     
    3440                } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *q
    3541
    36                 void addHead( Queue(T) & q, T & n ) with( q ) {
     42                T & addHead( Queue(T) & q, T & n ) with( q ) {
    3743                        #ifdef __CFA_DEBUG__
    3844                        if ( listed( &n ) ) abort( "(Queue &)%p.addHead( %p ) : Node is already on another list.", &q, &n );
     
    4551                                Next( &n ) = &n;                                                // last node points to itself
    4652                        }
     53                        return n;
    4754                }
    4855
    49                 void addTail( Queue(T) & q, T & n ) with( q ) {
     56                T & addTail( Queue(T) & q, T & n ) with( q ) {
    5057                        #ifdef __CFA_DEBUG__
    5158                        if ( listed( &n ) ) abort( "(Queue &)%p.addTail( %p ) : Node is already on another list.", &q, &n );
     
    5562                        last = &n;
    5663                        Next( &n ) = &n;                                                        // last node points to itself
     64                        return n;
    5765                }
    5866
    59                 void add( Queue(T) & q, T & n ) with( q ) {
    60                         addTail( q, n );
     67                T & add( Queue(T) & q, T & n ) with( q ) {
     68                        return addTail( q, n );
    6169                }
    6270
     
    6472                        T & t = head( q );
    6573                        if ( root ) {
    66                                 root = Next( root );
     74                                root = Next( (T *)root );
    6775                                if ( &head( q ) == &t ) {
    6876                                        root = last = 0p;                                       // only one element
     
    7785                }
    7886
    79                 void remove( Queue(T) & q, T & n ) with( q ) {  // O(n)
     87                T & remove( Queue(T) & q, T & n ) with( q ) {   // O(n)
    8088                        #ifdef __CFA_DEBUG__
    8189                        if ( ! listed( (Colable &)n ) ) abort( "(Queue &)%p.remove( %p ) : Node is not on a list.", &q, &n );
     
    103111                                curr = Next( curr );
    104112                        }
     113                        return n;
    105114                } // post: ! listed( n )
    106115
    107                 T & dropTail( Queue(T) & q ) with( q ) { // O(n)
     116                T & dropTail( Queue(T) & q ) with( q ) {                // O(n)
    108117                        T & n = tail( q );
    109118                        return &n ? remove( q, n ), n : *0p;
     
    142151} // distribution
    143152
    144 forall( dtype T ) {
     153forall( dtype T | { T *& Next ( T * ); } ) {
    145154        struct QueueIter {
    146155                inline ColIter;                                                                 // Plan 9 inheritance
     
    152161                } // post: curr == 0p
    153162
    154                 // create an iterator active in Queue q
     163                // create an iterator active in queue q
    155164                void ?{}( QueueIter(T) & qi, Queue(T) & q ) with( qi ) {
    156165                        curr = &head( q );
     
    161170                } // post: curr = {e in q}
    162171
    163                 // make existing iterator active in Queue q
     172                // make existing iterator active in queue q
    164173                void over( QueueIter(T) & qi, Queue(T) & q ) with( qi ) {
    165174                        curr = &head( q );
    166175                } // post: curr = {e in q}
    167176
    168                 bool ?>>?( QueueIter(T) & qi, T && tp ) with( qi ) {
     177                bool ?|?( QueueIter(T) & qi, T && tp ) with( qi ) {
    169178                        if ( curr ) {
    170179                                &tp = Curr( qi );
     
    174183                        return &tp != 0p;
    175184                }
    176                 // post: elts == null & !operator>>(tp) | elts != null & *tp' in elts & elts' == elts - *tp & operator>>(tp)
     185                // post: elts == null & !operator|(tp) | elts != null & *tp' in elts & elts' == elts - *tp & operator|(tp)
    177186        } // distribution
    178187} // distribution
  • libcfa/src/bits/sequence.hfa

    r42f6e07 r2b4daf2  
    22
    33#include "bits/collection.hfa"
     4#include "bits/defs.hfa"
    45
    56struct Seqable {
    6         inline Colable;
    7         Seqable * back;                                                                         // pointer to previous node in the list
     7        __cfa_anonymous_object(Colable);
     8        struct Seqable * back;                                                                          // pointer to previous node in the list
    89};
    910
    10 inline {
     11#ifdef __cforall
     12static inline {
    1113        // PUBLIC
    1214
     
    2628        }
    2729
    28         // wrappers to make Collection have T
    29         forall( dtype T ) {
    30                 T *& Back( T * n ) {
    31                         return (T *)Back( (Seqable *)n );
    32                 }
    33         } // distribution
     30        // // wrappers to make Collection have T
     31        // forall( dtype T ) {
     32        //      T *& Back( T * n ) {
     33        //              return (T *)Back( (Seqable *)n );
     34        //      }
     35        // } // distribution
    3436} // distribution
    3537
    36 forall( dtype T ) {
     38
     39// A Sequence(T) is a Collection(T) defining the ordering of a uStack and uQueue, and to insert and remove elements
     40// anywhere in the sequence. T must be a public descendant of uSeqable.
     41
     42// The implementation is a typical doubly-linked list, except the next field of the last node points at the first node
     43// and the back field of the last node points at the first node (circular).
     44
     45forall( dtype T | { T *& Back ( T * ); T *& Next ( T * ); } ) {
    3746        struct Sequence {
    3847                inline Collection;                                                              // Plan 9 inheritance
    3948        };
    4049
    41         inline {
     50        static inline {
    4251                // wrappers to make Collection have T
    4352                T & head( Sequence(T) & s ) with( s ) {
     
    5059                void ?{}( Sequence(T) & s ) with( s ) {
    5160                        ((Collection &)s){};
    52                 }       // post: isEmpty().
    53 
    54                 // Return a pointer to the last sequence element, without removing it. 
     61                }       // post: isEmpty()
     62
     63                // Return a pointer to the last sequence element, without removing it.
    5564                T & tail( Sequence(T) & s ) with( s ) {
    5665                        return root ? (T &)*Back( &head( s ) ) : *0p;
     
    6574                } // post: n == tail() & succ(n) == 0 | n != tail() & *succ(n) in *s
    6675
    67                 // Return a pointer to the element before *n, or 0p if there isn't one.
     76                // Return a pointer to the element before *n, or 0p if list empty.
    6877                T * pred( Sequence(T) & s, T * n ) with( s ) {  // pre: *n in *s
    6978                        #ifdef __CFA_DEBUG__
     
    7180                        #endif // __CFA_DEBUG__
    7281                        return n == &head( s ) ? 0p : Back( n );
    73                 }       // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s
    74 
    75 
    76                 // Insert *n into the sequence before *bef, or at the end if bef == 0.
    77                 void insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s
     82                } // post: n == head() & head(n) == 0 | n != head() & *pred(n) in *s
     83
     84
     85                // Insert *n into the sequence before *bef, or at the end if bef == 0p.
     86                T & insertBef( Sequence(T) & s, T & n, T & bef ) with( s ) { // pre: !n->listed() & *bef in *s
    7887                        #ifdef __CFA_DEBUG__
    7988                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertBef( %p, %p ) : Node is already on another list.", &s, n, &bef );
     
    103112                                Next( Back( &n ) ) = &n;
    104113                        } // if
     114                        return n;
    105115                }       // post: n->listed() & *n in *s & succ(n) == bef
    106116
    107117
    108118                // Insert *n into the sequence after *aft, or at the beginning if aft == 0.
    109                 void insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) {   // pre: !n->listed() & *aft in *s
     119                T & insertAft( Sequence(T) & s, T & aft, T & n ) with( s ) {    // pre: !n->listed() & *aft in *s
    110120                        #ifdef __CFA_DEBUG__
    111121                        if ( listed( &n ) ) abort( "(Sequence &)%p.insertAft( %p, %p ) : Node is already on another list.", &s, &aft, &n );
     
    133143                                Next( &aft ) = &n;
    134144                        } // if
    135                 }         // post: n->listed() & *n in *s & succ(n) == bef
     145                        return n;
     146                } // post: n->listed() & *n in *s & succ(n) == bef
    136147               
    137148                // pre: n->listed() & *n in *s
    138                 void remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
     149                T & remove( Sequence(T) & s, T & n ) with( s ) { // O(1)
    139150                        #ifdef __CFA_DEBUG__
    140151                        if ( ! listed( &n ) ) abort( "(Sequence &)%p.remove( %p ) : Node is not on a list.", &s, &n );
     
    147158                        Next( Back( &n ) ) = Next( &n );
    148159                        Next( &n ) = Back( &n ) = 0p;
    149                 }                                                       // post: !n->listed().
     160                        return n;
     161                } // post: !n->listed()
    150162
    151163                // Add an element to the head of the sequence.
    152                 void addHead( Sequence(T) & s, T & n ) {                // pre: !n->listed(); post: n->listed() & head() == n
    153                         insertAft( s, *0p, n );
    154                 }
     164                T & addHead( Sequence(T) & s, T & n ) {                 // pre: !n->listed(); post: n->listed() & head() == n
     165                        return insertAft( s, *0p, n );
     166                }
     167
    155168                // Add an element to the tail of the sequence.
    156                 void addTail( Sequence(T) & s, T & n ) {                // pre: !n->listed(); post: n->listed() & head() == n
    157                         insertBef( s, n, *0p );
    158                 }
     169                T & addTail( Sequence(T) & s, T & n ) {                 // pre: !n->listed(); post: n->listed() & head() == n
     170                        return insertBef( s, n, *0p );
     171                }
     172
    159173                // Add an element to the tail of the sequence.
    160                 void add( Sequence(T) & s, T & n ) {                    // pre: !n->listed(); post: n->listed() & head() == n
    161                         addTail( s, n );
    162                 }
     174                T & add( Sequence(T) & s, T & n ) {                             // pre: !n->listed(); post: n->listed() & head() == n
     175                        return addTail( s, n );
     176                }
     177
    163178                // Remove and return the head element in the sequence.
    164179                T & dropHead( Sequence(T) & s ) {
     
    166181                        return &n ? remove( s, n ), n : *0p;
    167182                }
     183
    168184                // Remove and return the head element in the sequence.
    169185                T & drop( Sequence(T) & s ) {
    170186                        return dropHead( s );
    171187                }
     188
    172189                // Remove and return the tail element in the sequence.
    173190                T & dropTail( Sequence(T) & s ) {
     
    184201                                T * toEnd = Back( &head( s ) );
    185202                                T * fromEnd = Back( &head( from ) );
    186                                 Back( root ) = fromEnd;
     203                                Back( (T *)root ) = fromEnd;
    187204                                Next( fromEnd ) = &head( s );
    188                                 Back( from.root ) = toEnd;
     205                                Back( (T *)from.root ) = toEnd;
    189206                                Next( toEnd ) = &head( from );
    190207                        } // if
     
    214231} // distribution
    215232
    216 forall( dtype T ) {
     233forall( dtype T | { T *& Back ( T * ); T *& Next ( T * ); } ) {
    217234        // SeqIter(T) is used to iterate over a Sequence(T) in head-to-tail order.
    218235        struct SeqIter {
     
    224241        };
    225242
    226         inline {
     243        static inline {
    227244                void ?{}( SeqIter(T) & si ) with( si ) {
    228245                        ((ColIter &)si){};
    229246                        seq = 0p;
    230                 } // post: elts = null.
    231 
     247                } // post: elts = null
     248
     249                // Create a iterator active in sequence s.
    232250                void ?{}( SeqIter(T) & si, Sequence(T) & s ) with( si ) {
    233251                        ((ColIter &)si){};
    234252                        seq = &s;
    235253                        curr = &head( s );
    236                 } // post: elts = null.
     254                } // post: elts = null
    237255
    238256                void ?{}( SeqIter(T) & si, Sequence(T) & s, T & start ) with( si ) {
     
    240258                        seq = &s;
    241259                        curr = &start;
    242                 } // post: elts = null.
    243 
     260                } // post: elts = null
     261
     262                // Make the iterator active in sequence s.
    244263                void over( SeqIter(T) & si, Sequence(T) & s ) with( si ) {
    245264                        seq = &s;
    246265                        curr = &head( s );
    247                 } // post: elts = {e in s}.
    248 
    249                 bool ?>>?( SeqIter(T) & si, T && tp ) with( si ) {
     266                } // post: elts = {e in s}
     267
     268                bool ?|?( SeqIter(T) & si, T && tp ) with( si ) {
    250269                        if ( curr ) {
    251270                                &tp = Curr( si );
     
    265284        };
    266285
    267         inline {
     286        static inline {
    268287                void ?{}( SeqIterRev(T) & si ) with( si ) {     
    269288                        ((ColIter &)si){};
    270289                        seq = 0p;
    271                 } // post: elts = null.
    272 
     290                } // post: elts = null
     291
     292                // Create a iterator active in sequence s.
    273293                void ?{}( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {   
    274294                        ((ColIter &)si){};
    275295                        seq = &s;
    276296                        curr = &tail( s );
    277                 } // post: elts = null.
     297                } // post: elts = null
    278298
    279299                void ?{}( SeqIterRev(T) & si, Sequence(T) & s, T & start ) with( si ) {
     
    281301                        seq = &s;
    282302                        curr = &start;
    283                 } // post: elts = null.
    284 
     303                } // post: elts = null
     304
     305                // Make the iterator active in sequence s.
    285306                void over( SeqIterRev(T) & si, Sequence(T) & s ) with( si ) {
    286307                        seq = &s;
    287308                        curr = &tail( s );
    288                 } // post: elts = {e in s}.
    289 
    290                 bool ?>>?( SeqIterRev(T) & si, T && tp ) with( si ) {
     309                } // post: elts = {e in s}
     310
     311                bool ?|?( SeqIterRev(T) & si, T && tp ) with( si ) {
    291312                        if ( curr ) {
    292313                                &tp = Curr( si );
     
    298319        } // distribution
    299320} // distribution
     321
     322#endif
  • libcfa/src/bits/stack.hfa

    r42f6e07 r2b4daf2  
    33#include "bits/collection.hfa"
    44
    5 forall( dtype T ) {
     5// A Stack(T) is a Collection(T) defining the ordering that nodes are returned by drop() in the reverse order from those
     6// added by add(). T must be a public descendant of uColable.
     7
     8// The implementation is a typical singly-linked list, except the next field of the last element points to itself
     9// instead of being null.
     10
     11forall( dtype T | { T *& Next ( T * ); } ) {
    612        struct Stack {
    713                inline Collection;                                                              // Plan 9 inheritance
     
    2531                }
    2632
    27                 void addHead( Stack(T) & s, T & n ) with( s ) {
     33                T & addHead( Stack(T) & s, T & n ) with( s ) {
    2834                        #ifdef __CFA_DEBUG__
    2935                        if ( listed( (Colable &)(n) ) ) abort( "(Stack &)%p.addHead( %p ) : Node is already on another list.", &s, n );
     
    3137                        Next( &n ) = &head( s ) ? &head( s ) : &n;
    3238                        root = &n;
     39                        return n;
    3340                }
    3441
    35                 void add( Stack(T) & s, T & n ) with( s ) {
    36                         addHead( s, n );
     42                T & add( Stack(T) & s, T & n ) with( s ) {
     43                        return addHead( s, n );
    3744                }
    3845
    39                 void push( Stack(T) & s, T & n ) with( s ) {
    40                         addHead( s, n );
     46                T & push( Stack(T) & s, T & n ) with( s ) {
     47                        return addHead( s, n );
    4148                }
    4249
     
    4451                        T & t = head( s );
    4552                        if ( root ) {
    46                                 root = ( T *)Next( root );
     53                                root = ( T *)Next( (T *)root );
    4754                                if ( &head( s ) == &t ) root = 0p;              // only one element ?
    4855                                Next( &t ) = 0p;
     
    5764} // distribution
    5865
     66// A StackIter(T) is a subclass of ColIter(T) that generates the elements of a Stack(T).  It returns the elements in the
     67// order returned by drop().
    5968
    60 forall( dtype T ) {
     69forall( dtype T | { T *& Next ( T * ); } ) {
    6170        struct StackIter {
    6271                inline ColIter;                                                                 // Plan 9 inheritance
     
    6877                } // post: curr == 0p
    6978
    70                 // create an iterator active in Stack s
     79                // create an iterator active in stack s
    7180                void ?{}( StackIter(T) & si, Stack(T) & s ) with( si ) {
    7281                        curr = &head( s );
     
    7786                } // post: curr = {e in s}
    7887
    79                 // make existing iterator active in Stack q
     88                // make existing iterator active in stack s
    8089                void over( StackIter(T) & si, Stack(T) & s ) with( si ) {
    8190                        curr = &head( s );
    8291                } // post: curr = {e in s}
    8392
    84                 bool ?>>?( StackIter(T) & si, T && tp ) with( si ) {
     93                bool ?|?( StackIter(T) & si, T && tp ) with( si ) {
    8594                        if ( curr ) {
    8695                                &tp = Curr( si );
Note: See TracChangeset for help on using the changeset viewer.