Ignore:
Timestamp:
Jan 21, 2021, 2:24:01 PM (3 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:
7d01186d
Parents:
5869cea (diff), 1adab3e (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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/andrew_beach_MMath/existing.tex

    r5869cea r7b91c0e  
    1 \chapter{\CFA{} Existing Features}
     1\chapter{\CFA Existing Features}
     2
     3\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.
     4\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.
    25
    36\section{Overloading and extern}
    47Cforall has overloading, allowing multiple definitions of the same name to
    5 be defined.
     8be defined.~\cite{Moss18}
    69
    710This also adds name mangling so that the assembly symbols are unique for
     
    1114
    1215The syntax for disabling mangling is:
    13 \begin{lstlisting}
     16\begin{cfa}
    1417extern "C" {
    1518    ...
    1619}
    17 \end{lstlisting}
     20\end{cfa}
    1821
    1922To re-enable mangling once it is disabled the syntax is:
    20 \begin{lstlisting}
     23\begin{cfa}
    2124extern "Cforall" {
    2225    ...
    2326}
    24 \end{lstlisting}
     27\end{cfa}
    2528
    2629Both should occur at the declaration level and effect all the declarations
    27 in \texttt{...}. Neither care about the state of mangling when they begin
     30in @...@. Neither care about the state of mangling when they begin
    2831and will return to that state after the group is finished. So re-enabling
    2932is only used to nest areas of mangled and unmangled declarations.
     
    3134\section{References}
    3235\CFA adds references to C. These are auto-dereferencing pointers and use the
    33 same syntax as pointers except they use ampersand (\codeCFA{\&}) instead of
    34 the asterisk (\codeCFA{*}). They can also be constaint or mutable, if they
     36same syntax as pointers except they use ampersand (@&@) instead of
     37the asterisk (@*@). They can also be constaint or mutable, if they
    3538are mutable they may be assigned to by using the address-of operator
    36 (\codeCFA\&) which converts them into a pointer.
     39(@&@) which converts them into a pointer.
    3740
    3841\section{Constructors and Destructors}
     
    4144functions with special names. The special names are used to define them and
    4245may be used to call the functions expicately. The \CFA special names are
    43 constructed by taking the tokens in the operators and putting \texttt{?} where
    44 the arguments would go. So multiplication is \texttt{?*?} while dereference
    45 is \texttt{*?}. This also make it easy to tell the difference between
    46 pre-fix operations (such as \texttt{++?}) and post-fix operations
    47 (\texttt{?++}).
    48 
    49 The special name for contructors is \texttt{?\{\}}, which comes from the
     46constructed by taking the tokens in the operators and putting @?@ where
     47the arguments would go. So multiplication is @?*?@ while dereference
     48is @*?@. This also make it easy to tell the difference between
     49pre-fix operations (such as @++?@) and post-fix operations
     50(@?++@).
     51
     52The special name for contructors is @?{}@, which comes from the
    5053initialization syntax in C. The special name for destructors is
    51 \texttt{\^{}?\{\}}. % I don't like the \^{} symbol but $^\wedge$ isn't better.
     54@^{}@. % I don't like the \^{} symbol but $^\wedge$ isn't better.
    5255
    5356Any time a type T goes out of scope the destructor matching
    54 \codeCFA{void ^?\{\}(T \&);} is called. In theory this is also true of
    55 primitive types such as \codeCFA{int}, but in practice those are no-ops and
     57@void ^?{}(T &);@ is called. In theory this is also true of
     58primitive types such as @int@, but in practice those are no-ops and
    5659are usually omitted for optimization.
    5760
    5861\section{Polymorphism}
    5962\CFA uses polymorphism to create functions and types that are defined over
    60 different types. \CFA polymorphic declarations serve the same role as \CPP
     63different types. \CFA polymorphic declarations serve the same role as \CC
    6164templates or Java generics.
    6265
     
    6568except that you may use the names introduced by the forall clause in them.
    6669
    67 Forall clauses are written \codeCFA{forall( ... )} where \codeCFA{...} becomes
     70Forall clauses are written @forall( ... )@ where @...@ becomes
    6871the list of polymorphic variables (local type names) and assertions, which
    6972repersent required operations on those types.
    7073
    71 \begin{lstlisting}
     74\begin{cfa}
    7275forall(dtype T | { void do_once(T &); })
    7376void do_twice(T & value) {
     
    7578    do_once(value);
    7679}
    77 \end{lstlisting}
     80\end{cfa}
    7881
    7982A polymorphic function can be used in the same way normal functions are.
     
    8386the the call site.
    8487
    85 As an example, even if no function named \codeCFA{do_once} is not defined
    86 near the definition of \codeCFA{do_twice} the following code will work.
    87 \begin{lstlisting}
     88As an example, even if no function named @do_once@ is not defined
     89near the definition of @do_twice@ the following code will work.
     90\begin{cfa}
    8891int quadruple(int x) {
    8992    void do_once(int & y) {
     
    9396    return x;
    9497}
    95 \end{lstlisting}
     98\end{cfa}
    9699This is not the recommended way to implement a quadruple function but it
    97 does work. The complier will deduce that \codeCFA{do_twice}'s T is an
     100does work. The complier will deduce that @do_twice@'s T is an
    98101integer from the argument. It will then look for a definition matching the
    99 assertion which is the \codeCFA{do_once} defined within the function. That
    100 function will be passed in as a function pointer to \codeCFA{do_twice} and
     102assertion which is the @do_once@ defined within the function. That
     103function will be passed in as a function pointer to @do_twice@ and
    101104called within it.
    102105
     
    104107traits which collect assertions into convenent packages that can then be used
    105108in assertion lists instead of all of their components.
    106 \begin{lstlisting}
     109\begin{cfa}
    107110trait done_once(dtype T) {
    108111    void do_once(T &);
    109112}
    110 \end{lstlisting}
     113\end{cfa}
    111114
    112115After this the forall list in the previous example could instead be written
    113116with the trait instead of the assertion itself.
    114 \begin{lstlisting}
     117\begin{cfa}
    115118forall(dtype T | done_once(T))
    116 \end{lstlisting}
     119\end{cfa}
    117120
    118121Traits can have arbitrary number of assertions in them and are usually used to
     
    124127are now used in field declaractions instead of parameters and local variables.
    125128
    126 \begin{lstlisting}
     129\begin{cfa}
    127130forall(dtype T)
    128131struct node {
     
    130133    T * data;
    131134}
    132 \end{lstlisting}
    133 
    134 The \codeCFA{node(T)} is a use of a polymorphic structure. Polymorphic types
     135\end{cfa}
     136
     137The @node(T)@ is a use of a polymorphic structure. Polymorphic types
    135138must be provided their polymorphic parameters.
    136139
     
    140143\section{Concurrency}
    141144
    142 \CFA has a number of concurrency features, \codeCFA{thread}s,
    143 \codeCFA{monitor}s and \codeCFA{mutex} parameters, \codeCFA{coroutine}s and
    144 \codeCFA{generator}s. The two features that interact with the exception system
    145 are \codeCFA{thread}s and \codeCFA{coroutine}s; they and their supporting
     145\CFA has a number of concurrency features, @thread@s,
     146@monitor@s and @mutex@ parameters, @coroutine@s and
     147@generator@s. The two features that interact with the exception system
     148are @thread@s and @coroutine@s; they and their supporting
    146149constructs will be described here.
    147150
     
    154157library.
    155158
    156 In \CFA coroutines are created using the \codeCFA{coroutine} keyword which
    157 works just like \codeCFA{struct} except that the created structure will be
    158 modified by the compiler to satify the \codeCFA{is_coroutine} trait.
     159In \CFA coroutines are created using the @coroutine@ keyword which
     160works just like @struct@ except that the created structure will be
     161modified by the compiler to satify the @is_coroutine@ trait.
    159162
    160163These structures act as the interface between callers and the coroutine,
    161164the fields are used to pass information in and out. Here is a simple example
    162165where the single field is used to pass the next number in a sequence out.
    163 \begin{lstlisting}
     166\begin{cfa}
    164167coroutine CountUp {
    165168    unsigned int next;
    166169}
    167 \end{lstlisting}
     170\end{cfa}
    168171
    169172The routine part of the coroutine is a main function for the coroutine. It
     
    173176function it continue from that same suspend statement instead of at the top
    174177of the function.
    175 \begin{lstlisting}
     178\begin{cfa}
    176179void main(CountUp & this) {
    177180    unsigned int next = 0;
     
    182185    }
    183186}
    184 \end{lstlisting}
     187\end{cfa}
    185188
    186189Control is passed to the coroutine with the resume function. This includes the
     
    189192return value is for easy access to communication variables. For example the
    190193next value from a count-up can be generated and collected in a single
    191 expression: \codeCFA{resume(count).next}.
     194expression: @resume(count).next@.
    192195
    193196\subsection{Monitors and Mutex}
     
    198201parameters.
    199202
    200 Function parameters can have the \codeCFA{mutex} qualifiers on reference
    201 arguments, for example \codeCFA{void example(a_monitor & mutex arg);}. When the
     203Function parameters can have the @mutex@ qualifiers on reference
     204arguments, for example @void example(a_monitor & mutex arg);@. When the
    202205function is called it will acquire the lock on all of the mutex parameters.
    203206
     
    214217
    215218Threads are created like coroutines except the keyword is changed:
    216 \begin{lstlisting}
     219\begin{cfa}
    217220thread StringWorker {
    218221    const char * input;
     
    225228    this.result = result;
    226229}
    227 \end{lstlisting}
     230\end{cfa}
    228231The main function will start executing after the fork operation and continue
    229232executing until it is finished. If another thread joins with this one it will
     
    233236From the outside this is the creation and destruction of the thread object.
    234237Fork happens after the constructor is run and join happens before the
    235 destructor runs. Join also happens during the \codeCFA{join} function which
     238destructor runs. Join also happens during the @join@ function which
    236239can be used to join a thread earlier. If it is used the destructor does not
    237240join as that has already been completed.
Note: See TracChangeset for help on using the changeset viewer.