# Changeset f28fdee for doc/theses/andrew_beach_MMath/existing.tex

Ignore:
Timestamp:
Jan 20, 2021, 5:27:42 PM (2 years ago)
Branches:
arm-eh, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
09ee131
Parents:
79e261b7
Message:

incorporate uw-thesis macros and CFA common macros

File:
1 edited

### Legend:

Unmodified
 r79e261b7 \chapter{\CFA{} Existing Features} \chapter{\CFA Existing Features} \CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with modern safety and productivity features, while still ensuring backwards compatibility with C and its programmers. \CFA is designed to have an orthogonal feature-set based closely on the C programming paradigm (non-object-oriented) and these features can be added incrementally to an existing C code-base allowing programmers to learn \CFA on an as-needed basis. \section{Overloading and extern} Cforall has overloading, allowing multiple definitions of the same name to be defined. be defined.~\cite{Moss18} This also adds name mangling so that the assembly symbols are unique for The syntax for disabling mangling is: \begin{lstlisting} \begin{cfa} extern "C" { ... } \end{lstlisting} \end{cfa} To re-enable mangling once it is disabled the syntax is: \begin{lstlisting} \begin{cfa} extern "Cforall" { ... } \end{lstlisting} \end{cfa} Both should occur at the declaration level and effect all the declarations in \texttt{...}. Neither care about the state of mangling when they begin in @...@. Neither care about the state of mangling when they begin and will return to that state after the group is finished. So re-enabling is only used to nest areas of mangled and unmangled declarations. \section{References} \CFA adds references to C. These are auto-dereferencing pointers and use the same syntax as pointers except they use ampersand (\codeCFA{\&}) instead of the asterisk (\codeCFA{*}). They can also be constaint or mutable, if they same syntax as pointers except they use ampersand (@&@) instead of the asterisk (@*@). They can also be constaint or mutable, if they are mutable they may be assigned to by using the address-of operator (\codeCFA\&) which converts them into a pointer. (@&@) which converts them into a pointer. \section{Constructors and Destructors} functions with special names. The special names are used to define them and may be used to call the functions expicately. The \CFA special names are constructed by taking the tokens in the operators and putting \texttt{?} where the arguments would go. So multiplication is \texttt{?*?} while dereference is \texttt{*?}. This also make it easy to tell the difference between pre-fix operations (such as \texttt{++?}) and post-fix operations (\texttt{?++}). The special name for contructors is \texttt{?\{\}}, which comes from the constructed by taking the tokens in the operators and putting @?@ where the arguments would go. So multiplication is @?*?@ while dereference is @*?@. This also make it easy to tell the difference between pre-fix operations (such as @++?@) and post-fix operations (@?++@). The special name for contructors is @?{}@, which comes from the initialization syntax in C. The special name for destructors is \texttt{\^{}?\{\}}. % I don't like the \^{} symbol but $^\wedge$ isn't better. @^{}@. % I don't like the \^{} symbol but $^\wedge$ isn't better. Any time a type T goes out of scope the destructor matching \codeCFA{void ^?\{\}(T \&);} is called. In theory this is also true of primitive types such as \codeCFA{int}, but in practice those are no-ops and @void ^?{}(T &);@ is called. In theory this is also true of primitive types such as @int@, but in practice those are no-ops and are usually omitted for optimization. \section{Polymorphism} \CFA uses polymorphism to create functions and types that are defined over different types. \CFA polymorphic declarations serve the same role as \CPP different types. \CFA polymorphic declarations serve the same role as \CC templates or Java generics. except that you may use the names introduced by the forall clause in them. Forall clauses are written \codeCFA{forall( ... )} where \codeCFA{...} becomes Forall clauses are written @forall( ... )@ where @...@ becomes the list of polymorphic variables (local type names) and assertions, which repersent required operations on those types. \begin{lstlisting} \begin{cfa} forall(dtype T | { void do_once(T &); }) void do_twice(T & value) { do_once(value); } \end{lstlisting} \end{cfa} A polymorphic function can be used in the same way normal functions are. the the call site. As an example, even if no function named \codeCFA{do_once} is not defined near the definition of \codeCFA{do_twice} the following code will work. \begin{lstlisting} As an example, even if no function named @do_once@ is not defined near the definition of @do_twice@ the following code will work. \begin{cfa} int quadruple(int x) { void do_once(int & y) { return x; } \end{lstlisting} \end{cfa} This is not the recommended way to implement a quadruple function but it does work. The complier will deduce that \codeCFA{do_twice}'s T is an does work. The complier will deduce that @do_twice@'s T is an integer from the argument. It will then look for a definition matching the assertion which is the \codeCFA{do_once} defined within the function. That function will be passed in as a function pointer to \codeCFA{do_twice} and assertion which is the @do_once@ defined within the function. That function will be passed in as a function pointer to @do_twice@ and called within it. traits which collect assertions into convenent packages that can then be used in assertion lists instead of all of their components. \begin{lstlisting} \begin{cfa} trait done_once(dtype T) { void do_once(T &); } \end{lstlisting} \end{cfa} After this the forall list in the previous example could instead be written with the trait instead of the assertion itself. \begin{lstlisting} \begin{cfa} forall(dtype T | done_once(T)) \end{lstlisting} \end{cfa} Traits can have arbitrary number of assertions in them and are usually used to are now used in field declaractions instead of parameters and local variables. \begin{lstlisting} \begin{cfa} forall(dtype T) struct node { T * data; } \end{lstlisting} The \codeCFA{node(T)} is a use of a polymorphic structure. Polymorphic types \end{cfa} The @node(T)@ is a use of a polymorphic structure. Polymorphic types must be provided their polymorphic parameters. \section{Concurrency} \CFA has a number of concurrency features, \codeCFA{thread}s, \codeCFA{monitor}s and \codeCFA{mutex} parameters, \codeCFA{coroutine}s and \codeCFA{generator}s. The two features that interact with the exception system are \codeCFA{thread}s and \codeCFA{coroutine}s; they and their supporting \CFA has a number of concurrency features, @thread@s, @monitor@s and @mutex@ parameters, @coroutine@s and @generator@s. The two features that interact with the exception system are @thread@s and @coroutine@s; they and their supporting constructs will be described here. library. In \CFA coroutines are created using the \codeCFA{coroutine} keyword which works just like \codeCFA{struct} except that the created structure will be modified by the compiler to satify the \codeCFA{is_coroutine} trait. In \CFA coroutines are created using the @coroutine@ keyword which works just like @struct@ except that the created structure will be modified by the compiler to satify the @is_coroutine@ trait. These structures act as the interface between callers and the coroutine, the fields are used to pass information in and out. Here is a simple example where the single field is used to pass the next number in a sequence out. \begin{lstlisting} \begin{cfa} coroutine CountUp { unsigned int next; } \end{lstlisting} \end{cfa} The routine part of the coroutine is a main function for the coroutine. It function it continue from that same suspend statement instead of at the top of the function. \begin{lstlisting} \begin{cfa} void main(CountUp & this) { unsigned int next = 0; } } \end{lstlisting} \end{cfa} Control is passed to the coroutine with the resume function. This includes the return value is for easy access to communication variables. For example the next value from a count-up can be generated and collected in a single expression: \codeCFA{resume(count).next}. expression: @resume(count).next@. \subsection{Monitors and Mutex} parameters. Function parameters can have the \codeCFA{mutex} qualifiers on reference arguments, for example \codeCFA{void example(a_monitor & mutex arg);}. When the Function parameters can have the @mutex@ qualifiers on reference arguments, for example @void example(a_monitor & mutex arg);@. When the function is called it will acquire the lock on all of the mutex parameters. Threads are created like coroutines except the keyword is changed: \begin{lstlisting} \begin{cfa} thread StringWorker { const char * input; this.result = result; } \end{lstlisting} \end{cfa} The main function will start executing after the fork operation and continue executing until it is finished. If another thread joins with this one it will From the outside this is the creation and destruction of the thread object. Fork happens after the constructor is run and join happens before the destructor runs. Join also happens during the \codeCFA{join} function which destructor runs. Join also happens during the @join@ function which can be used to join a thread earlier. If it is used the destructor does not join as that has already been completed.