Changeset f28fdee for doc/theses/andrew_beach_MMath/existing.tex
- Timestamp:
- Jan 20, 2021, 5:27:42 PM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 09ee131
- Parents:
- 79e261b7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/existing.tex
r79e261b7 rf28fdee 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. 2 5 3 6 \section{Overloading and extern} 4 7 Cforall has overloading, allowing multiple definitions of the same name to 5 be defined. 8 be defined.~\cite{Moss18} 6 9 7 10 This also adds name mangling so that the assembly symbols are unique for … … 11 14 12 15 The syntax for disabling mangling is: 13 \begin{ lstlisting}16 \begin{cfa} 14 17 extern "C" { 15 18 ... 16 19 } 17 \end{ lstlisting}20 \end{cfa} 18 21 19 22 To re-enable mangling once it is disabled the syntax is: 20 \begin{ lstlisting}23 \begin{cfa} 21 24 extern "Cforall" { 22 25 ... 23 26 } 24 \end{ lstlisting}27 \end{cfa} 25 28 26 29 Both should occur at the declaration level and effect all the declarations 27 in \texttt{...}. Neither care about the state of mangling when they begin30 in @...@. Neither care about the state of mangling when they begin 28 31 and will return to that state after the group is finished. So re-enabling 29 32 is only used to nest areas of mangled and unmangled declarations. … … 31 34 \section{References} 32 35 \CFA adds references to C. These are auto-dereferencing pointers and use the 33 same syntax as pointers except they use ampersand ( \codeCFA{\&}) instead of34 the asterisk ( \codeCFA{*}). They can also be constaint or mutable, if they36 same syntax as pointers except they use ampersand (@&@) instead of 37 the asterisk (@*@). They can also be constaint or mutable, if they 35 38 are 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. 37 40 38 41 \section{Constructors and Destructors} … … 41 44 functions with special names. The special names are used to define them and 42 45 may be used to call the functions expicately. The \CFA special names are 43 constructed by taking the tokens in the operators and putting \texttt{?}where44 the arguments would go. So multiplication is \texttt{?*?}while dereference45 is \texttt{*?}. This also make it easy to tell the difference between46 pre-fix operations (such as \texttt{++?}) and post-fix operations47 ( \texttt{?++}).48 49 The special name for contructors is \texttt{?\{\}}, which comes from the46 constructed by taking the tokens in the operators and putting @?@ where 47 the arguments would go. So multiplication is @?*?@ while dereference 48 is @*?@. This also make it easy to tell the difference between 49 pre-fix operations (such as @++?@) and post-fix operations 50 (@?++@). 51 52 The special name for contructors is @?{}@, which comes from the 50 53 initialization 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. 52 55 53 56 Any time a type T goes out of scope the destructor matching 54 \codeCFA{void ^?\{\}(T \&);}is called. In theory this is also true of55 primitive types such as \codeCFA{int}, but in practice those are no-ops and57 @void ^?{}(T &);@ is called. In theory this is also true of 58 primitive types such as @int@, but in practice those are no-ops and 56 59 are usually omitted for optimization. 57 60 58 61 \section{Polymorphism} 59 62 \CFA uses polymorphism to create functions and types that are defined over 60 different types. \CFA polymorphic declarations serve the same role as \C PP63 different types. \CFA polymorphic declarations serve the same role as \CC 61 64 templates or Java generics. 62 65 … … 65 68 except that you may use the names introduced by the forall clause in them. 66 69 67 Forall clauses are written \codeCFA{forall( ... )} where \codeCFA{...}becomes70 Forall clauses are written @forall( ... )@ where @...@ becomes 68 71 the list of polymorphic variables (local type names) and assertions, which 69 72 repersent required operations on those types. 70 73 71 \begin{ lstlisting}74 \begin{cfa} 72 75 forall(dtype T | { void do_once(T &); }) 73 76 void do_twice(T & value) { … … 75 78 do_once(value); 76 79 } 77 \end{ lstlisting}80 \end{cfa} 78 81 79 82 A polymorphic function can be used in the same way normal functions are. … … 83 86 the the call site. 84 87 85 As an example, even if no function named \codeCFA{do_once}is not defined86 near the definition of \codeCFA{do_twice}the following code will work.87 \begin{ lstlisting}88 As an example, even if no function named @do_once@ is not defined 89 near the definition of @do_twice@ the following code will work. 90 \begin{cfa} 88 91 int quadruple(int x) { 89 92 void do_once(int & y) { … … 93 96 return x; 94 97 } 95 \end{ lstlisting}98 \end{cfa} 96 99 This 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 an100 does work. The complier will deduce that @do_twice@'s T is an 98 101 integer from the argument. It will then look for a definition matching the 99 assertion which is the \codeCFA{do_once}defined within the function. That100 function will be passed in as a function pointer to \codeCFA{do_twice}and102 assertion which is the @do_once@ defined within the function. That 103 function will be passed in as a function pointer to @do_twice@ and 101 104 called within it. 102 105 … … 104 107 traits which collect assertions into convenent packages that can then be used 105 108 in assertion lists instead of all of their components. 106 \begin{ lstlisting}109 \begin{cfa} 107 110 trait done_once(dtype T) { 108 111 void do_once(T &); 109 112 } 110 \end{ lstlisting}113 \end{cfa} 111 114 112 115 After this the forall list in the previous example could instead be written 113 116 with the trait instead of the assertion itself. 114 \begin{ lstlisting}117 \begin{cfa} 115 118 forall(dtype T | done_once(T)) 116 \end{ lstlisting}119 \end{cfa} 117 120 118 121 Traits can have arbitrary number of assertions in them and are usually used to … … 124 127 are now used in field declaractions instead of parameters and local variables. 125 128 126 \begin{ lstlisting}129 \begin{cfa} 127 130 forall(dtype T) 128 131 struct node { … … 130 133 T * data; 131 134 } 132 \end{ lstlisting}133 134 The \codeCFA{node(T)}is a use of a polymorphic structure. Polymorphic types135 \end{cfa} 136 137 The @node(T)@ is a use of a polymorphic structure. Polymorphic types 135 138 must be provided their polymorphic parameters. 136 139 … … 140 143 \section{Concurrency} 141 144 142 \CFA has a number of concurrency features, \codeCFA{thread}s,143 \codeCFA{monitor}s and \codeCFA{mutex} parameters, \codeCFA{coroutine}s and144 \codeCFA{generator}s. The two features that interact with the exception system145 are \codeCFA{thread}s and \codeCFA{coroutine}s; they and their supporting145 \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 148 are @thread@s and @coroutine@s; they and their supporting 146 149 constructs will be described here. 147 150 … … 154 157 library. 155 158 156 In \CFA coroutines are created using the \codeCFA{coroutine}keyword which157 works just like \codeCFA{struct}except that the created structure will be158 modified by the compiler to satify the \codeCFA{is_coroutine}trait.159 In \CFA coroutines are created using the @coroutine@ keyword which 160 works just like @struct@ except that the created structure will be 161 modified by the compiler to satify the @is_coroutine@ trait. 159 162 160 163 These structures act as the interface between callers and the coroutine, 161 164 the fields are used to pass information in and out. Here is a simple example 162 165 where the single field is used to pass the next number in a sequence out. 163 \begin{ lstlisting}166 \begin{cfa} 164 167 coroutine CountUp { 165 168 unsigned int next; 166 169 } 167 \end{ lstlisting}170 \end{cfa} 168 171 169 172 The routine part of the coroutine is a main function for the coroutine. It … … 173 176 function it continue from that same suspend statement instead of at the top 174 177 of the function. 175 \begin{ lstlisting}178 \begin{cfa} 176 179 void main(CountUp & this) { 177 180 unsigned int next = 0; … … 182 185 } 183 186 } 184 \end{ lstlisting}187 \end{cfa} 185 188 186 189 Control is passed to the coroutine with the resume function. This includes the … … 189 192 return value is for easy access to communication variables. For example the 190 193 next value from a count-up can be generated and collected in a single 191 expression: \codeCFA{resume(count).next}.194 expression: @resume(count).next@. 192 195 193 196 \subsection{Monitors and Mutex} … … 198 201 parameters. 199 202 200 Function parameters can have the \codeCFA{mutex}qualifiers on reference201 arguments, for example \codeCFA{void example(a_monitor & mutex arg);}. When the203 Function parameters can have the @mutex@ qualifiers on reference 204 arguments, for example @void example(a_monitor & mutex arg);@. When the 202 205 function is called it will acquire the lock on all of the mutex parameters. 203 206 … … 214 217 215 218 Threads are created like coroutines except the keyword is changed: 216 \begin{ lstlisting}219 \begin{cfa} 217 220 thread StringWorker { 218 221 const char * input; … … 225 228 this.result = result; 226 229 } 227 \end{ lstlisting}230 \end{cfa} 228 231 The main function will start executing after the fork operation and continue 229 232 executing until it is finished. If another thread joins with this one it will … … 233 236 From the outside this is the creation and destruction of the thread object. 234 237 Fork happens after the constructor is run and join happens before the 235 destructor runs. Join also happens during the \codeCFA{join}function which238 destructor runs. Join also happens during the @join@ function which 236 239 can be used to join a thread earlier. If it is used the destructor does not 237 240 join as that has already been completed.
Note: See TracChangeset
for help on using the changeset viewer.