[29c9b23] | 1 | \chapter{\CFA Existing Features}
|
---|
[f28fdee] | 2 |
|
---|
[6c79bef] | 3 | \CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with
|
---|
| 4 | modern safety and productivity features, while still ensuring backwards
|
---|
| 5 | compatibility with C and its programmers. \CFA is designed to have an
|
---|
| 6 | orthogonal feature-set based closely on the C programming paradigm
|
---|
| 7 | (non-object-oriented) and these features can be added incrementally to an
|
---|
| 8 | existing C code-base allowing programmers to learn \CFA on an as-needed basis.
|
---|
| 9 |
|
---|
| 10 | Only those \CFA features pertinent to this thesis are discussed. Many of the
|
---|
| 11 | \CFA syntactic and semantic features used in the thesis should be fairly
|
---|
| 12 | obvious to the reader.
|
---|
| 13 |
|
---|
[9af0fe2d] | 14 | \section{Overloading and \lstinline{extern}}
|
---|
[6c79bef] | 15 | \CFA has extensive overloading, allowing multiple definitions of the same name
|
---|
[67c6a47] | 16 | to be defined~\cite{Moss18}.
|
---|
[f28fdee] | 17 | \begin{cfa}
|
---|
[df24d37] | 18 | char i; int i; double i;
|
---|
| 19 | int f(); double f();
|
---|
| 20 | void g( int ); void g( double );
|
---|
[f28fdee] | 21 | \end{cfa}
|
---|
[6c79bef] | 22 | This feature requires name mangling so the assembly symbols are unique for
|
---|
| 23 | different overloads. For compatibility with names in C, there is also a syntax
|
---|
| 24 | to disable name mangling. These unmangled names cannot be overloaded but act as
|
---|
| 25 | the interface between C and \CFA code. The syntax for disabling/enabling
|
---|
| 26 | mangling is:
|
---|
[f28fdee] | 27 | \begin{cfa}
|
---|
[edc6ea2] | 28 | // name mangling on by default
|
---|
[6c79bef] | 29 | int i; // _X1ii_1
|
---|
[edc6ea2] | 30 | extern "C" { // disables name mangling
|
---|
[6c79bef] | 31 | int j; // j
|
---|
[edc6ea2] | 32 | extern "Cforall" { // enables name mangling
|
---|
[6c79bef] | 33 | int k; // _X1ki_1
|
---|
| 34 | }
|
---|
[edc6ea2] | 35 | // revert to no name mangling
|
---|
[6e7b969] | 36 | }
|
---|
[edc6ea2] | 37 | // revert to name mangling
|
---|
[6c79bef] | 38 | \end{cfa}
|
---|
| 39 | Both forms of @extern@ affect all the declarations within their nested lexical
|
---|
| 40 | scope and transition back to the previous mangling state when the lexical scope
|
---|
| 41 | ends.
|
---|
| 42 |
|
---|
| 43 | \section{Reference Type}
|
---|
[03c0e44] | 44 | \CFA adds a reference type to C as an auto-dereferencing pointer.
|
---|
| 45 | They work very similarly to pointers.
|
---|
| 46 | Reference-types are written the same way as a pointer-type is but each
|
---|
| 47 | asterisk (@*@) is replaced with a ampersand (@&@);
|
---|
| 48 | this includes cv-qualifiers and multiple levels of reference.
|
---|
| 49 |
|
---|
| 50 | They are intended for cases where you would want to use pointers but would
|
---|
| 51 | be dereferencing them (almost) every usage.
|
---|
| 52 | In most cases a reference can just be thought of as a pointer that
|
---|
| 53 | automatically puts a dereference infront of each of its uses (per-level of
|
---|
| 54 | reference).
|
---|
| 55 | The address-of operator (@&@) acts as an escape and removes one of the
|
---|
| 56 | automatic dereference operations.
|
---|
| 57 | Mutable references may be assigned to by converting them to a pointer
|
---|
| 58 | with a @&@ and then assigning a pointer too them.
|
---|
| 59 |
|
---|
| 60 | \begin{minipage}{0,5\textwidth}
|
---|
| 61 | With references:
|
---|
| 62 | \begin{cfa}
|
---|
| 63 | int i, j;
|
---|
| 64 | int & ri = i;
|
---|
| 65 | int && rri = ri;
|
---|
| 66 | rri = 3;
|
---|
| 67 | &ri = &j;
|
---|
| 68 | ri = 5;
|
---|
| 69 | \end{cfa}
|
---|
| 70 | \end{minipage}
|
---|
| 71 | \begin{minipage}{0,5\textwidth}
|
---|
| 72 | With pointers:
|
---|
[6c79bef] | 73 | \begin{cfa}
|
---|
| 74 | int i, j;
|
---|
[03c0e44] | 75 | int * pi = &i
|
---|
| 76 | int ** ppi = π
|
---|
| 77 | **ppi = 3;
|
---|
| 78 | pi = &j;
|
---|
| 79 | *pi = 5;
|
---|
[f28fdee] | 80 | \end{cfa}
|
---|
[03c0e44] | 81 | \end{minipage}
|
---|
[6e7b969] | 82 |
|
---|
| 83 | \section{Constructors and Destructors}
|
---|
| 84 |
|
---|
[67c6a47] | 85 | Both constructors and destructors are operators, which means they are
|
---|
[29c9b23] | 86 | functions with special operator names rather than type names in \Cpp. The
|
---|
[6c79bef] | 87 | special operator names may be used to call the functions explicitly (not
|
---|
[29c9b23] | 88 | allowed in \Cpp for constructors).
|
---|
[6c79bef] | 89 |
|
---|
| 90 | In general, operator names in \CFA are constructed by bracketing an operator
|
---|
[edc6ea2] | 91 | token with @?@, which indicates the position of the arguments. For example,
|
---|
| 92 | infixed multiplication is @?*?@ while prefix dereference is @*?@.
|
---|
| 93 | This syntax make it easy to tell the difference between prefix operations
|
---|
| 94 | (such as @++?@) and post-fix operations (@?++@).
|
---|
[6c79bef] | 95 |
|
---|
| 96 | The special name for a constructor is @?{}@, which comes from the
|
---|
[edc6ea2] | 97 | initialization syntax in C. That initialation syntax is also the operator
|
---|
| 98 | form. \CFA will generate a constructor call each time a variable is declared,
|
---|
| 99 | passing the initialization arguments to the constructort.
|
---|
| 100 | \begin{cfa}
|
---|
| 101 | struct Example { ... };
|
---|
| 102 | void ?{}(Example & this) { ... }
|
---|
| 103 | {
|
---|
| 104 | Example a;
|
---|
| 105 | Example b = {};
|
---|
| 106 | }
|
---|
| 107 | void ?{}(Example & this, char first, int num) { ... }
|
---|
| 108 | {
|
---|
| 109 | Example c = {'a', 2};
|
---|
| 110 | }
|
---|
| 111 | \end{cfa}
|
---|
| 112 | Both @a@ and @b@ will be initalized with the first constructor (there is no
|
---|
| 113 | general way to skip initialation) while @c@ will be initalized with the
|
---|
| 114 | second.
|
---|
| 115 |
|
---|
[6c79bef] | 116 | % I don't like the \^{} symbol but $^\wedge$ isn't better.
|
---|
[edc6ea2] | 117 | Similarly destructors use the special name @^?{}@ (the @^@ has no special
|
---|
| 118 | meaning). They can be called explicatly as well but normally they are
|
---|
| 119 | implicitly called on a variable when it goes out of scope.
|
---|
[6c79bef] | 120 | \begin{cfa}
|
---|
[edc6ea2] | 121 | void ^?{}(Example & this) { ... }
|
---|
[6c79bef] | 122 | {
|
---|
[edc6ea2] | 123 | Example d;
|
---|
| 124 | } // <- implicit destructor call
|
---|
[6c79bef] | 125 | \end{cfa}
|
---|
[edc6ea2] | 126 | No operator name is restricted in what function signatures they may be bound
|
---|
| 127 | to although most of the forms cannot be called in operator form. Some
|
---|
| 128 | ``near-misses" will generate warnings.
|
---|
| 129 |
|
---|
| 130 | Whenever a type is defined, \CFA will create a default zero-argument
|
---|
| 131 | constructor, a copy constructor, a series of argument-per-field constructors
|
---|
| 132 | and a destructor. All user constructors are defined after this.
|
---|
| 133 | Because operators are never part of the type definition they may be added
|
---|
| 134 | at any time, including on built-in types.
|
---|
[6e7b969] | 135 |
|
---|
| 136 | \section{Polymorphism}
|
---|
[6c79bef] | 137 | \CFA uses parametric polymorphism to create functions and types that are
|
---|
| 138 | defined over multiple types. \CFA polymorphic declarations serve the same role
|
---|
[29c9b23] | 139 | as \Cpp templates or Java generics. The ``parametric'' means the polymorphism is
|
---|
[6c79bef] | 140 | accomplished by passing argument operations to associate \emph{parameters} at
|
---|
| 141 | the call site, and these parameters are used in the function to differentiate
|
---|
| 142 | among the types the function operates on.
|
---|
| 143 |
|
---|
| 144 | Polymorphic declarations start with a universal @forall@ clause that goes
|
---|
| 145 | before the standard (monomorphic) declaration. These declarations have the same
|
---|
| 146 | syntax except they may use the universal type names introduced by the @forall@
|
---|
| 147 | clause. For example, the following is a polymorphic identity function that
|
---|
| 148 | works on any type @T@:
|
---|
| 149 | \begin{cfa}
|
---|
[edc6ea2] | 150 | forall( T ) T identity( T val ) { return val; }
|
---|
| 151 | int forty_two = identity( 42 );
|
---|
| 152 | char capital_a = identity( 'A' );
|
---|
[6c79bef] | 153 | \end{cfa}
|
---|
[edc6ea2] | 154 | Each use of a polymorphic declaration will resolve its polymorphic parameters
|
---|
| 155 | (in this case, just @T@) to concrete types (@int@ in the first use and @char@
|
---|
| 156 | in the second).
|
---|
[6e7b969] | 157 |
|
---|
[6c79bef] | 158 | To allow a polymorphic function to be separately compiled, the type @T@ must be
|
---|
| 159 | constrained by the operations used on @T@ in the function body. The @forall@
|
---|
| 160 | clauses is augmented with a list of polymorphic variables (local type names)
|
---|
| 161 | and assertions (constraints), which represent the required operations on those
|
---|
| 162 | types used in a function, \eg:
|
---|
[f28fdee] | 163 | \begin{cfa}
|
---|
[edc6ea2] | 164 | forall( T | { void do_once(T); })
|
---|
[6c79bef] | 165 | void do_twice(T value) {
|
---|
| 166 | do_once(value);
|
---|
| 167 | do_once(value);
|
---|
[6e7b969] | 168 | }
|
---|
[f28fdee] | 169 | \end{cfa}
|
---|
[6c79bef] | 170 |
|
---|
| 171 | A polymorphic function can be used in the same way as a normal function. The
|
---|
| 172 | polymorphic variables are filled in with concrete types and the assertions are
|
---|
| 173 | checked. An assertion is checked by verifying each assertion operation (with
|
---|
| 174 | all the variables replaced with the concrete types from the arguments) is
|
---|
| 175 | defined at a call site.
|
---|
[edc6ea2] | 176 | \begin{cfa}
|
---|
| 177 | void do_once(int i) { ... }
|
---|
| 178 | int i;
|
---|
| 179 | do_twice(i);
|
---|
| 180 | \end{cfa}
|
---|
| 181 | Any object with a type fulfilling the assertion may be passed as an argument to
|
---|
| 182 | a @do_twice@ call.
|
---|
[6c79bef] | 183 |
|
---|
| 184 | Note, a function named @do_once@ is not required in the scope of @do_twice@ to
|
---|
[29c9b23] | 185 | compile it, unlike \Cpp template expansion. Furthermore, call-site inferencing
|
---|
[6c79bef] | 186 | allows local replacement of the most specific parametric functions needs for a
|
---|
| 187 | call.
|
---|
[f28fdee] | 188 | \begin{cfa}
|
---|
[edc6ea2] | 189 | void do_once(double y) { ... }
|
---|
[6e7b969] | 190 | int quadruple(int x) {
|
---|
[edc6ea2] | 191 | void do_once(int y) { y = y * 2; }
|
---|
| 192 | do_twice(x);
|
---|
[6c79bef] | 193 | return x;
|
---|
[6e7b969] | 194 | }
|
---|
[f28fdee] | 195 | \end{cfa}
|
---|
[6c79bef] | 196 | Specifically, the complier deduces that @do_twice@'s T is an integer from the
|
---|
| 197 | argument @x@. It then looks for the most specific definition matching the
|
---|
| 198 | assertion, which is the nested integral @do_once@ defined within the
|
---|
| 199 | function. The matched assertion function is then passed as a function pointer
|
---|
| 200 | to @do_twice@ and called within it.
|
---|
[edc6ea2] | 201 | The global definition of @do_once@ is ignored.
|
---|
[6c79bef] | 202 |
|
---|
| 203 | To avoid typing long lists of assertions, constraints can be collect into
|
---|
| 204 | convenient packages called a @trait@, which can then be used in an assertion
|
---|
| 205 | instead of the individual constraints.
|
---|
[f28fdee] | 206 | \begin{cfa}
|
---|
[6c79bef] | 207 | trait done_once(T) {
|
---|
| 208 | void do_once(T);
|
---|
[6e7b969] | 209 | }
|
---|
[f28fdee] | 210 | \end{cfa}
|
---|
[6c79bef] | 211 | and the @forall@ list in the previous example is replaced with the trait.
|
---|
[f28fdee] | 212 | \begin{cfa}
|
---|
[edc6ea2] | 213 | forall(dtype T | done_once(T))
|
---|
[f28fdee] | 214 | \end{cfa}
|
---|
[6c79bef] | 215 | In general, a trait can contain an arbitrary number of assertions, both
|
---|
| 216 | functions and variables, and are usually used to create a shorthand for, and
|
---|
| 217 | give descriptive names to, common groupings of assertions describing a certain
|
---|
| 218 | functionality, like @sumable@, @listable@, \etc.
|
---|
| 219 |
|
---|
| 220 | Polymorphic structures and unions are defined by qualifying the aggregate type
|
---|
| 221 | with @forall@. The type variables work the same except they are used in field
|
---|
| 222 | declarations instead of parameters, returns, and local variable declarations.
|
---|
[f28fdee] | 223 | \begin{cfa}
|
---|
[edc6ea2] | 224 | forall(dtype T)
|
---|
[6e7b969] | 225 | struct node {
|
---|
[9b0bb79] | 226 | node(T) * next;
|
---|
[edc6ea2] | 227 | T * data;
|
---|
[6e7b969] | 228 | }
|
---|
[edc6ea2] | 229 | node(int) inode;
|
---|
[f28fdee] | 230 | \end{cfa}
|
---|
[edc6ea2] | 231 | The generic type @node(T)@ is an example of a polymorphic type usage. Like \Cpp
|
---|
| 232 | template usage, a polymorphic type usage must specify a type parameter.
|
---|
[6e7b969] | 233 |
|
---|
[6c79bef] | 234 | There are many other polymorphism features in \CFA but these are the ones used
|
---|
| 235 | by the exception system.
|
---|
[6e7b969] | 236 |
|
---|
[67c6a47] | 237 | \section{Control Flow}
|
---|
| 238 | \CFA has a number of advanced control-flow features: @generator@, @coroutine@, @monitor@, @mutex@ parameters, and @thread@.
|
---|
| 239 | The two features that interact with
|
---|
| 240 | the exception system are @coroutine@ and @thread@; they and their supporting
|
---|
[6c79bef] | 241 | constructs are described here.
|
---|
| 242 |
|
---|
| 243 | \subsection{Coroutine}
|
---|
| 244 | A coroutine is a type with associated functions, where the functions are not
|
---|
| 245 | required to finish execution when control is handed back to the caller. Instead
|
---|
| 246 | they may suspend execution at any time and be resumed later at the point of
|
---|
| 247 | last suspension. (Generators are stackless and coroutines are stackful.) These
|
---|
| 248 | types are not concurrent but share some similarities along with common
|
---|
| 249 | underpinnings, so they are combined with the \CFA threading library. Further
|
---|
| 250 | discussion in this section only refers to the coroutine because generators are
|
---|
| 251 | similar.
|
---|
| 252 |
|
---|
| 253 | In \CFA, a coroutine is created using the @coroutine@ keyword, which is an
|
---|
| 254 | aggregate type like @struct,@ except the structure is implicitly modified by
|
---|
| 255 | the compiler to satisfy the @is_coroutine@ trait; hence, a coroutine is
|
---|
| 256 | restricted by the type system to types that provide this special trait. The
|
---|
| 257 | coroutine structure acts as the interface between callers and the coroutine,
|
---|
| 258 | and its fields are used to pass information in and out of coroutine interface
|
---|
| 259 | functions.
|
---|
| 260 |
|
---|
| 261 | Here is a simple example where a single field is used to pass (communicate) the
|
---|
| 262 | next number in a sequence.
|
---|
[f28fdee] | 263 | \begin{cfa}
|
---|
[6e7b969] | 264 | coroutine CountUp {
|
---|
[9b0bb79] | 265 | unsigned int next;
|
---|
[6e7b969] | 266 | }
|
---|
[6c79bef] | 267 | CountUp countup;
|
---|
[f28fdee] | 268 | \end{cfa}
|
---|
[67c6a47] | 269 | Each coroutine has a @main@ function, which takes a reference to a coroutine
|
---|
[6c79bef] | 270 | object and returns @void@.
|
---|
[edc6ea2] | 271 | \begin{cfa}
|
---|
| 272 | void main(CountUp & this) {
|
---|
| 273 | for (unsigned int next = 0 ; true ; ++next) {
|
---|
| 274 | next = up;
|
---|
| 275 | suspend;$\label{suspend}$
|
---|
[6c79bef] | 276 | }
|
---|
[6e7b969] | 277 | }
|
---|
[f28fdee] | 278 | \end{cfa}
|
---|
[6c79bef] | 279 | In this function, or functions called by this function (helper functions), the
|
---|
| 280 | @suspend@ statement is used to return execution to the coroutine's caller
|
---|
[67c6a47] | 281 | without terminating the coroutine's function.
|
---|
[6c79bef] | 282 |
|
---|
| 283 | A coroutine is resumed by calling the @resume@ function, \eg @resume(countup)@.
|
---|
| 284 | The first resume calls the @main@ function at the top. Thereafter, resume calls
|
---|
| 285 | continue a coroutine in the last suspended function after the @suspend@
|
---|
| 286 | statement, in this case @main@ line~\ref{suspend}. The @resume@ function takes
|
---|
| 287 | a reference to the coroutine structure and returns the same reference. The
|
---|
| 288 | return value allows easy access to communication variables defined in the
|
---|
| 289 | coroutine object. For example, the @next@ value for coroutine object @countup@
|
---|
| 290 | is both generated and collected in the single expression:
|
---|
| 291 | @resume(countup).next@.
|
---|
[6e7b969] | 292 |
|
---|
[67c6a47] | 293 | \subsection{Monitor and Mutex Parameter}
|
---|
[6c79bef] | 294 | Concurrency does not guarantee ordering; without ordering results are
|
---|
| 295 | non-deterministic. To claw back ordering, \CFA uses monitors and @mutex@
|
---|
| 296 | (mutual exclusion) parameters. A monitor is another kind of aggregate, where
|
---|
| 297 | the compiler implicitly inserts a lock and instances are compatible with
|
---|
| 298 | @mutex@ parameters.
|
---|
| 299 |
|
---|
| 300 | A function that requires deterministic (ordered) execution, acquires mutual
|
---|
| 301 | exclusion on a monitor object by qualifying an object reference parameter with
|
---|
| 302 | @mutex@.
|
---|
| 303 | \begin{cfa}
|
---|
[edc6ea2] | 304 | void example(MonitorA & mutex argA, MonitorB & mutex argB);
|
---|
[6c79bef] | 305 | \end{cfa}
|
---|
| 306 | When the function is called, it implicitly acquires the monitor lock for all of
|
---|
| 307 | the mutex parameters without deadlock. This semantics means all functions with
|
---|
| 308 | the same mutex type(s) are part of a critical section for objects of that type
|
---|
| 309 | and only one runs at a time.
|
---|
[6e7b969] | 310 |
|
---|
[67c6a47] | 311 | \subsection{Thread}
|
---|
[6c79bef] | 312 | Functions, generators, and coroutines are sequential so there is only a single
|
---|
| 313 | (but potentially sophisticated) execution path in a program. Threads introduce
|
---|
| 314 | multiple execution paths that continue independently.
|
---|
[6e7b969] | 315 |
|
---|
[6c79bef] | 316 | For threads to work safely with objects requires mutual exclusion using
|
---|
| 317 | monitors and mutex parameters. For threads to work safely with other threads,
|
---|
| 318 | also requires mutual exclusion in the form of a communication rendezvous, which
|
---|
[67c6a47] | 319 | also supports internal synchronization as for mutex objects. For exceptions,
|
---|
| 320 | only two basic thread operations are important: fork and join.
|
---|
[6e7b969] | 321 |
|
---|
[6c79bef] | 322 | Threads are created like coroutines with an associated @main@ function:
|
---|
[f28fdee] | 323 | \begin{cfa}
|
---|
[6e7b969] | 324 | thread StringWorker {
|
---|
[6c79bef] | 325 | const char * input;
|
---|
| 326 | int result;
|
---|
[6e7b969] | 327 | };
|
---|
| 328 | void main(StringWorker & this) {
|
---|
[6c79bef] | 329 | const char * localCopy = this.input;
|
---|
| 330 | // ... do some work, perhaps hashing the string ...
|
---|
| 331 | this.result = result;
|
---|
[6e7b969] | 332 | }
|
---|
[6c79bef] | 333 | {
|
---|
| 334 | StringWorker stringworker; // fork thread running in "main"
|
---|
[9b0bb79] | 335 | } // <- implicitly join with thread / wait for completion
|
---|
[f28fdee] | 336 | \end{cfa}
|
---|
[6c79bef] | 337 | The thread main is where a new thread starts execution after a fork operation
|
---|
| 338 | and then the thread continues executing until it is finished. If another thread
|
---|
| 339 | joins with an executing thread, it waits until the executing main completes
|
---|
| 340 | execution. In other words, everything a thread does is between a fork and join.
|
---|
| 341 |
|
---|
| 342 | From the outside, this behaviour is accomplished through creation and
|
---|
| 343 | destruction of a thread object. Implicitly, fork happens after a thread
|
---|
| 344 | object's constructor is run and join happens before the destructor runs. Join
|
---|
| 345 | can also be specified explicitly using the @join@ function to wait for a
|
---|
| 346 | thread's completion independently from its deallocation (\ie destructor
|
---|
| 347 | call). If @join@ is called explicitly, the destructor does not implicitly join.
|
---|