Changes in / [2f19e03:4f1b8f3f]
- Location:
- doc/theses/andrew_beach_MMath
- Files:
-
- 3 edited
-
existing.tex (modified) (8 diffs)
-
features.tex (modified) (36 diffs)
-
intro.tex (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/existing.tex
r2f19e03 r4f1b8f3f 1 \chapter{\CFA {}Existing Features}1 \chapter{\CFA Existing Features} 2 2 \label{c:existing} 3 3 … … 9 9 existing C code-base allowing programmers to learn \CFA on an as-needed basis. 10 10 11 Only those \CFA features pertaining to this thesis are discussed. 12 Also, only new features of \CFA will be discussed, a basic familiarity with 13 C or C-like languages is assumed.11 Only those \CFA features pertaining to this thesis are discussed. Many of the 12 \CFA syntactic and semantic features used in the thesis should be fairly 13 obvious to the reader. 14 14 15 15 \section{Overloading and \lstinline{extern}} … … 29 29 // name mangling on by default 30 30 int i; // _X1ii_1 31 extern "C"{ // disables name mangling31 @extern "C"@ { // disables name mangling 32 32 int j; // j 33 extern "Cforall"{ // enables name mangling33 @extern "Cforall"@ { // enables name mangling 34 34 int k; // _X1ki_1 35 35 } … … 47 47 Reference-types are written the same way as a pointer-type but each 48 48 asterisk (@*@) is replaced with a ampersand (@&@); 49 this includes cv-qualifiers and multiple levels of reference. 50 51 Generally, references act like pointers with an implicate dereferencing 52 operation added to each use of the variable. 53 These automatic dereferences may be disabled with the address-of operator 54 (@&@). 55 56 % Check to see if these are generating errors. 49 this includes cv-qualifiers and multiple levels of reference, \eg: 50 57 51 \begin{minipage}{0,5\textwidth} 58 52 With references: … … 62 56 int && rri = ri; 63 57 rri = 3; 64 &ri = &j; 58 &ri = &j; // reference assignment 65 59 ri = 5; 66 60 \end{cfa} … … 73 67 int ** ppi = π 74 68 **ppi = 3; 75 pi = &j; 69 pi = &j; // pointer assignment 76 70 *pi = 5; 77 71 \end{cfa} 78 72 \end{minipage} 79 73 80 References are intended to be used when you woulduse pointers but would74 References are intended for cases where you would want to use pointers but would 81 75 be dereferencing them (almost) every usage. 82 Mutable references may be assigned to by converting them to a pointer 83 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above 76 In most cases a reference can just be thought of as a pointer that 77 automatically puts a dereference in front of each of its uses (per-level of 78 reference). 79 The address-of operator (@&@) acts as an escape and removes one of the 80 automatic dereference operations. 81 Mutable references may be assigned by converting them to a pointer 82 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above. 84 83 85 84 \section{Operators} 86 85 87 \CFA implements operator overloading by providing special names. 88 Operator uses are translated into function calls using these names. 89 These names are created by taking the operator symbols and joining them with 90 @?@ where the arguments would go. 91 For example, 86 In general, operator names in \CFA are constructed by bracketing an operator 87 token with @?@, which indicates the position of the arguments. For example, 92 88 infixed multiplication is @?*?@ while prefix dereference is @*?@. 93 89 This syntax make it easy to tell the difference between prefix operations 94 90 (such as @++?@) and post-fix operations (@?++@). 95 91 96 \begin{cfa} 97 int ?+?(point a, point b) { return point{a.x + b.x, a.y + b.y}; } 98 bool ?==?(point a, point b) { return a.x == b.x && a.y == b.y; } 99 { 100 assert(point{1, 2} + point{3, 4} == point{4, 6}); 101 } 102 \end{cfa} 103 Note that these special names are not limited to just being used for these 104 operator functions, and may be used name other declarations. 105 Some ``near misses", that will not match an operator form but looks like 106 it may have been supposed to, will generate wantings but otherwise they are 107 left alone. 108 109 %\subsection{Constructors and Destructors} 92 An operator name may describe any function signature (it is just a name) but 93 only certain signatures may be called in operator form. 94 \begin{cfa} 95 int ?+?( int i, int j, int k ) { return i + j + k; } 96 { 97 sout | ?+?( 3, 4, 5 ); // no infix form 98 } 99 \end{cfa} 100 Some ``near-misses" for unary/binary operator prototypes generate warnings. 110 101 111 102 Both constructors and destructors are operators, which means they are 112 103 functions with special operator names rather than type names in \Cpp. The 113 special operator names may be used to call the functions explicitly. 114 % Placement new means that this is actually equivant to C++. 115 116 The special name for a constructor is @?{}@, which comes from the 117 initialization syntax in C, \eg @Example e = { ... }@. 118 \CFA will generate a constructor call each time a variable is declared, 119 passing the initialization arguments to the constructort. 120 \begin{cfa} 121 struct Example { ... }; 122 void ?{}(Example & this) { ... } 123 { 124 Example a; 125 Example b = {}; 126 } 127 void ?{}(Example & this, char first, int num) { ... } 128 { 129 Example c = {'a', 2}; 130 } 131 \end{cfa} 132 Both @a@ and @b@ will be initalized with the first constructor, 133 while @c@ will be initalized with the second. 134 Currently, there is no general way to skip initialation. 104 special operator names may be used to call the functions explicitly (not 105 allowed in \Cpp for constructors). 106 107 The special name for a constructor is @?{}@, where the name @{}@ comes from the 108 initialization syntax in C, \eg @Structure s = {...}@. 109 % That initialization syntax is also the operator form. 110 \CFA generates a constructor call each time a variable is declared, 111 passing the initialization arguments to the constructor. 112 \begin{cfa} 113 struct Structure { ... }; 114 void ?{}(Structure & this) { ... } 115 { 116 Structure a; 117 Structure b = {}; 118 } 119 void ?{}(Structure & this, char first, int num) { ... } 120 { 121 Structure c = {'a', 2}; 122 } 123 \end{cfa} 124 Both @a@ and @b@ are initialized with the first constructor, 125 while @c@ is initialized with the second. 126 Currently, there is no general way to skip initialization. 135 127 136 128 % I don't like the \^{} symbol but $^\wedge$ isn't better. 137 Similarly destructors use the special name @^?{}@ (the @^@ has no special 138 meaning). 139 These are a normally called implicitly called on a variable when it goes out 140 of scope. They can be called explicitly as well. 141 \begin{cfa} 142 void ^?{}(Example & this) { ... } 143 { 144 Example d; 129 Similarly, destructors use the special name @^?{}@ (the @^@ has no special 130 meaning). Normally, they are implicitly called on a variable when it goes out 131 of scope but they can be called explicitly as well. 132 \begin{cfa} 133 void ^?{}(Structure & this) { ... } 134 { 135 Structure d; 145 136 } // <- implicit destructor call 146 137 \end{cfa} 147 138 148 Whenever a type is defined, \CFA will createa default zero-argument139 Whenever a type is defined, \CFA creates a default zero-argument 149 140 constructor, a copy constructor, a series of argument-per-field constructors 150 141 and a destructor. All user constructors are defined after this. … … 207 198 void do_once(double y) { ... } 208 199 int quadruple(int x) { 209 void do_once(int & y) { y = y * 2; } 210 do_twice(x); 200 void do_once(int y) { y = y * 2; } // replace global do_once 201 do_twice(x); // use local do_once 202 do_twice(x + 1.5); // use global do_once 211 203 return x; 212 204 } 213 205 \end{cfa} 214 206 Specifically, the complier deduces that @do_twice@'s T is an integer from the 215 argument @x@. It then looks for the most specificdefinition matching the207 argument @x@. It then looks for the most \emph{specific} definition matching the 216 208 assertion, which is the nested integral @do_once@ defined within the 217 209 function. The matched assertion function is then passed as a function pointer 218 to @do_twice@ and called within it. 219 The global definition of @do_once@ is ignored, however if quadruple took a 220 @double@ argument then the global definition would be used instead as it 221 would be a better match. 222 % Aaron's thesis might be a good reference here. 210 to @do_twice@ and called within it. The global definition of @do_once@ is used 211 for the second call because the float-point argument is a better match. 223 212 224 213 To avoid typing long lists of assertions, constraints can be collect into … … 290 279 Each coroutine has a @main@ function, which takes a reference to a coroutine 291 280 object and returns @void@. 292 %[numbers=left] Why numbers on this one? 293 \begin{cfa} 281 \begin{cfa}[numbers=left] 294 282 void main(CountUp & this) { 295 283 for (unsigned int next = 0 ; true ; ++next) { -
doc/theses/andrew_beach_MMath/features.tex
r2f19e03 r4f1b8f3f 2 2 \label{c:features} 3 3 4 This chapter covers the design and user interface of the \CFA EHM 4 This chapter covers the design and user interface of the \CFA 5 EHM, % or exception system. 5 6 and begins with a general overview of EHMs. It is not a strict 6 7 definition of all EHMs nor an exhaustive list of all possible features. 7 However it does cover the most common structure and features found in them. 8 9 \section{Overview of EHMs} 8 However it does cover the most common structures and features found in them. 9 10 10 % We should cover what is an exception handling mechanism and what is an 11 11 % exception before this. Probably in the introduction. Some of this could 12 12 % move there. 13 \s ubsection{Raise / Handle}13 \section{Raise / Handle} 14 14 An exception operation has two main parts: raise and handle. 15 15 These terms are sometimes also known as throw and catch but this work uses … … 24 24 25 25 Some well known examples include the @throw@ statements of \Cpp and Java and 26 the \code{Python}{raise} statement from Python. In real systems araise may27 p reform some other work (such as memory management) but for the26 the \code{Python}{raise} statement from Python. A raise may 27 perform some other work (such as memory management) but for the 28 28 purposes of this overview that can be ignored. 29 29 … … 33 33 34 34 A handler has three common features: the previously mentioned user code, a 35 region of code they guard and an exception label/condition that matches35 region of code they guard, and an exception label/condition that matches 36 36 certain exceptions. 37 37 Only raises inside the guarded region and raising exceptions that match the 38 38 label can be handled by a given handler. 39 Different EHMs use different rules to pick a handler,40 if multiple handlers could be used such as ``best match" or ``first found".39 Different EHMs have different rules to pick a handler, 40 if multiple handlers could be used, such as ``best match" or ``first found". 41 41 42 42 The @try@ statements of \Cpp, Java and Python are common examples. All three … … 44 44 region. 45 45 46 \s ubsection{Propagation}46 \section{Propagation} 47 47 After an exception is raised comes what is usually the biggest step for the 48 48 EHM: finding and setting up the handler. The propagation from raise to 49 49 handler can be broken up into three different tasks: searching for a handler, 50 matching against the handler and installing the handler.50 matching against the handler, and installing the handler. 51 51 52 52 \paragraph{Searching} … … 55 55 thrown as it looks for handlers that have the raise site in their guarded 56 56 region. 57 Th e search includes handlers in the current function, as well as any in58 callerson the stack that have the function call in their guarded region.57 This search includes handlers in the current function, as well as any in callers 58 on the stack that have the function call in their guarded region. 59 59 60 60 \paragraph{Matching} 61 61 Each handler found has to be matched with the raised exception. The exception 62 label defines a condition that is used with exception and decidesif62 label defines a condition that is used with the exception to decide if 63 63 there is a match or not. 64 64 65 65 In languages where the first match is used, this step is intertwined with 66 searching ; a match check is preformed immediately after the search finds66 searching: a match check is performed immediately after the search finds 67 67 a possible handler. 68 68 69 \ paragraph{Installing}69 \section{Installing} 70 70 After a handler is chosen it must be made ready to run. 71 71 The implementation can vary widely to fit with the rest of the … … 78 78 This situation only occurs with unchecked exceptions as checked exceptions 79 79 (such as in Java) can make the guarantee. 80 This unhandled action is usually very general, such as aborting the program.80 This unhandled action can abort the program or install a very general handler. 81 81 82 82 \paragraph{Hierarchy} 83 83 A common way to organize exceptions is in a hierarchical structure. 84 This pattern comes fromobject-orientated languages where the84 This organization is often used in object-orientated languages where the 85 85 exception hierarchy is a natural extension of the object hierarchy. 86 86 … … 90 90 \end{center} 91 91 92 A handler label ed with any given exception can handle exceptions of that92 A handler labelled with any given exception can handle exceptions of that 93 93 type or any child type of that exception. The root of the exception hierarchy 94 94 (here \code{C}{exception}) acts as a catch-all, leaf types catch single types … … 104 104 % Could I cite the rational for the Python IO exception rework? 105 105 106 \ subsection{Completion}106 \paragraph{Completion} 107 107 After the handler has finished the entire exception operation has to complete 108 108 and continue executing somewhere else. This step is usually simple, … … 111 111 112 112 The EHM can return control to many different places, 113 the most common are after the handler definition (termination) 114 and after the raise (resumption). 115 116 \subsection{Communication} 113 the most common are after the handler definition (termination) and after the raise (resumption). 114 115 \paragraph{Communication} 117 116 For effective exception handling, additional information is often passed 118 117 from the raise to the handler and back again. 119 118 So far only communication of the exceptions' identity has been covered. 120 A common communication method is putting fields into the exception instance 121 and giving the handler access to them. 122 Passing the exception by reference instead of by value can allow data to be 123 passed in both directions. 119 A common communication method is putting fields into the exception instance and giving the 120 handler access to them. References in the exception instance can push data back to the raise. 124 121 125 122 \section{Virtuals} 126 123 Virtual types and casts are not part of \CFA's EHM nor are they required for 127 124 any EHM. 128 However, it is one of the best ways to support an exception hierachy129 is via a virtual hierarchy and dispatch system.125 However, one of the best ways to support an exception hierarchy is via a virtual system 126 among exceptions and used for exception matching. 130 127 131 128 Ideally, the virtual system would have been part of \CFA before the work 132 129 on exception handling began, but unfortunately it was not. 133 Hence, only the features and framework needed for the EHM were130 Therefore, only the features and framework needed for the EHM were 134 131 designed and implemented. Other features were considered to ensure that 135 the structure could accommodate other desirable features in the future 136 but they were notimplemented.137 The rest of this section will only discuss the implemented subset of the138 virtual system design.132 the structure could accommodate other desirable features in the future but they were not 133 implemented. 134 The rest of this section discusses the implemented subset of the 135 virtual-system design. 139 136 140 137 The virtual system supports multiple ``trees" of types. Each tree is … … 146 143 % A type's ancestors are its parent and its parent's ancestors. 147 144 % The root type has no ancestors. 148 % A type's de scendants are its children and its children's descendants.145 % A type's decedents are its children and its children's decedents. 149 146 150 147 Every virtual type also has a list of virtual members. Children inherit … … 153 150 of object-orientated programming, and can be of any type. 154 151 152 \PAB{I do not understand these sentences. Can you add an example? $\Rightarrow$ 155 153 \CFA still supports virtual methods as a special case of virtual members. 156 154 Function pointers that take a pointer to the virtual type are modified 157 155 with each level of inheritance so that refers to the new type. 158 156 This means an object can always be passed to a function in its virtual table 159 as if it were a method. 160 \todo{Clarify (with an example) virtual methods.} 157 as if it were a method.} 161 158 162 159 Each virtual type has a unique id. … … 164 161 into a virtual table type. Each virtual type has a pointer to a virtual table 165 162 as a hidden field. 166 \todo{Might need a diagram for virtual structure.} 163 164 \PAB{God forbid, maybe you need a UML diagram to relate these entities.} 167 165 168 166 Up until this point the virtual system is similar to ones found in … … 175 173 types can begin to satisfy a trait, stop satisfying a trait or satisfy the same 176 174 trait in a different way at any lexical location in the program. 177 In this sense, they are ``open" as they can change at any time. 178 This capability means it is impossible to pick a single set of functions179 that represent the type'simplementation across the program.175 In this sense, they are ``open" as they can change at any time. This capability means it 176 is impossible to pick a single set of functions that represent the type's 177 implementation across the program. 180 178 181 179 \CFA side-steps this issue by not having a single virtual table for each 182 180 type. A user can define virtual tables that are filled in at their 183 declaration and given a name. Anywhere that name is visible, even if it is181 declaration and given a name. Anywhere that name is visible, even if 184 182 defined locally inside a function (although that means it does not have a 185 183 static lifetime), it can be used. … … 188 186 through the object. 189 187 188 \PAB{The above explanation is very good!} 189 190 190 While much of the virtual infrastructure is created, it is currently only used 191 191 internally for exception handling. The only user-level feature is the virtual 192 cast , which is the same as the \Cpp \code{C++}{dynamic_cast}.192 cast 193 193 \label{p:VirtualCast} 194 194 \begin{cfa} 195 195 (virtual TYPE)EXPRESSION 196 196 \end{cfa} 197 which is the same as the \Cpp \code{C++}{dynamic_cast}. 197 198 Note, the syntax and semantics matches a C-cast, rather than the function-like 198 199 \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be … … 217 218 The trait is defined over two types, the exception type and the virtual table 218 219 type. Each exception type should have a single virtual table type. 219 There are no actual assertions in this trait because the trait system220 cannot express them yet(adding such assertions would be part of220 There are no actual assertions in this trait because currently the trait system 221 cannot express them (adding such assertions would be part of 221 222 completing the virtual system). The imaginary assertions would probably come 222 223 from a trait defined by the virtual system, and state that the exception type 223 is a virtual type, is a descend ant of @exception_t@ (the base exception type)224 is a virtual type, is a descendent of @exception_t@ (the base exception type) 224 225 and note its virtual table type. 225 226 … … 240 241 }; 241 242 \end{cfa} 242 Both traits ensure a pair of types are an exception type, its virtual table 243 type 243 Both traits ensure a pair of types are an exception type and its virtual table, 244 244 and defines one of the two default handlers. The default handlers are used 245 245 as fallbacks and are discussed in detail in \vref{s:ExceptionHandling}. … … 269 269 \section{Exception Handling} 270 270 \label{s:ExceptionHandling} 271 As stated, 272 \CFA provides two kinds of exception handling: termination and resumption. 271 As stated, \CFA provides two kinds of exception handling: termination and resumption. 273 272 These twin operations are the core of \CFA's exception handling mechanism. 274 This section will coverthe general patterns shared by the two operations and275 then go on to cover the details each individual operation.273 This section covers the general patterns shared by the two operations and 274 then go on to cover the details of each individual operation. 276 275 277 276 Both operations follow the same set of steps. 278 Both start with the user p reforming a raise on an exception.277 Both start with the user performing a raise on an exception. 279 278 Then the exception propagates up the stack. 280 279 If a handler is found the exception is caught and the handler is run. 281 After that control continues at a raise-dependent location.282 If the search fails a default handler is run and, if it returns, thencontrol283 continues after the raise. 280 After that control returns to a point specific to the kind of exception. 281 If the search fails a default handler is run, and if it returns, control 282 continues after the raise. Note, the default handler may further change control flow rather than return. 284 283 285 284 This general description covers what the two kinds have in common. 286 Differences include how propagation is p reformed, where exception continues285 Differences include how propagation is performed, where exception continues 287 286 after an exception is caught and handled and which default handler is run. 288 287 289 288 \subsection{Termination} 290 289 \label{s:Termination} 290 291 291 Termination handling is the familiar kind and used in most programming 292 292 languages with exception handling. … … 313 313 314 314 The throw copies the provided exception into managed memory to ensure 315 the exception is not destroyed ifthe stack is unwound.315 the exception is not destroyed when the stack is unwound. 316 316 It is the user's responsibility to ensure the original exception is cleaned 317 317 up whether the stack is unwound or not. Allocating it on the stack is 318 318 usually sufficient. 319 319 320 % How to say propagation starts, its first sub-step is the search. 321 Then propagation starts with the search. \CFA uses a ``first match" rule so 322 matching is preformed with the copied exception as the search continues. 323 It starts from the throwing function and proceeds towards base of the stack, 320 Then propagation starts the search. \CFA uses a ``first match" rule so 321 matching is performed with the copied exception as the search continues. 322 It starts from the throwing function and proceeds towards the base of the stack, 324 323 from callee to caller. 325 324 At each stack frame, a check is made for resumption handlers defined by the … … 335 334 \end{cfa} 336 335 When viewed on its own, a try statement simply executes the statements 337 in \snake{GUARDED_BLOCK} and when those are finished, 338 the try statement finishes. 336 in \snake{GUARDED_BLOCK} and when those are finished, the try statement finishes. 339 337 340 338 However, while the guarded statements are being executed, including any 341 invoked functions, all the handlers in these statements are included in the 342 search path. 343 Hence, if a termination exception is raised these handlers may be matched 344 against the exception and may handle it. 339 invoked functions, all the handlers in these statements are included on the search 340 path. Hence, if a termination exception is raised, the search includes the added handlers associated with the guarded block and those further up the 341 stack from the guarded block. 345 342 346 343 Exception matching checks the handler in each catch clause in the order 347 344 they appear, top to bottom. If the representation of the raised exception type 348 345 is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ 349 (if provided) is 350 bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$ 351 are executed.If control reaches the end of the handler, the exception is346 (if provided) is bound to a pointer to the exception and the statements in 347 @HANDLER_BLOCK@$_i$ are executed. 348 If control reaches the end of the handler, the exception is 352 349 freed and control continues after the try statement. 353 350 354 If no termination handler is found during the search thenthe default handler355 (\defaultTerminationHandler) visible at the raise statement is run.356 Through \CFA's trait system the best match at the raise statement will beused.357 This function is run and is passed the copied exception. 358 If the default handler is run control continues after the raisestatement.351 If no termination handler is found during the search, the default handler 352 (\defaultTerminationHandler) visible at the raise statement is called. 353 Through \CFA's trait system, the best match at the raise sight is used. 354 This function is run and is passed the copied exception. If the default 355 handler returns, control continues after the throw statement. 359 356 360 357 There is a global @defaultTerminationHandler@ that is polymorphic over all 361 termination exception types. 362 Since it is so general a more specific handler can be 358 termination exception types. Since it is so general, a more specific handler can be 363 359 defined and is used for those types, effectively overriding the handler 364 360 for a particular exception type. … … 374 370 matched a closure is taken from up the stack and executed, 375 371 after which the raising function continues executing. 376 The common uses for resumption exceptions include 377 potentially repairable errors, where execution can continue in the same 378 function once the error is corrected, and 379 ignorable events, such as logging where nothing needs to happen and control 380 should always continue from the same place. 372 These are most often used when a potentially repairable error occurs, some handler is found on the stack to fix it, and 373 the raising function can continue with the correction. 374 Another common usage is dynamic event analysis, \eg logging, without disrupting control flow. 375 Note, if an event is raised and there is no interest, control continues normally. 376 377 \PAB{We also have \lstinline{report} instead of \lstinline{throwResume}, \lstinline{recover} instead of \lstinline{catch}, and \lstinline{fixup} instead of \lstinline{catchResume}. 378 You may or may not want to mention it. You can still stick with \lstinline{catch} and \lstinline{throw/catchResume} in the thesis.} 381 379 382 380 A resumption raise is started with the @throwResume@ statement: … … 384 382 throwResume EXPRESSION; 385 383 \end{cfa} 386 \todo{Decide on a final set of keywords and use them everywhere.}387 384 It works much the same way as the termination throw. 388 385 The expression must return a reference to a resumption exception, … … 390 387 @is_resumption_exception@ at the call site. 391 388 The assertions from this trait are available to 392 the exception system while handling the exception. 393 394 At run-time, no exception copy is made. 395 Resumption does not unwind the stack nor otherwise remove values from the 396 current scope, so there is no need to manage memory to keep things in scope. 397 398 The EHM then begins propagation. The search starts from the raise in the 399 resuming function and proceeds towards the base of the stack, 400 from callee to caller. 389 the exception system, while handling the exception. 390 391 Resumption does not need to copy the raised exception, as the stack is not unwound. 392 The exception and 393 any values on the stack remain in scope, while the resumption is handled. 394 395 The EHM then begins propogation. The search starts from the raise in the 396 resuming function and proceeds towards the base of the stack, from callee to caller. 401 397 At each stack frame, a check is made for resumption handlers defined by the 402 398 @catchResume@ clauses of a @try@ statement. … … 416 412 kind of raise. 417 413 When a try statement is executed, it simply executes the statements in the 418 @GUARDED_BLOCK@ and then finishes.414 @GUARDED_BLOCK@ and then returns. 419 415 420 416 However, while the guarded statements are being executed, including any 421 invoked functions, all the handlers in these statements are included in the 422 search path. 423 Hence, if a resumption exception is raised these handlers may be matched 424 against the exception and may handle it. 417 invoked functions, all the handlers in these statements are included on the search 418 path. Hence, if a resumption exception is raised the search includes the added handlers associated with the guarded block and those further up the 419 stack from the guarded block. 425 420 426 421 Exception matching checks the handler in each catch clause in the order … … 432 427 the raise statement that raised the handled exception. 433 428 434 Like termination, if no resumption handler is found during the search, 435 the default handler (\defaultResumptionHandler) visible at the raise 436 statement is called. It will use the best match at the raise sight according 437 to \CFA's overloading rules. The default handler is438 passed the exception given to the raise. When the default handler finishes429 Like termination, if no resumption handler is found during the search, the default handler 430 (\defaultResumptionHandler) visible at the raise statement is called. 431 It uses the best match at the 432 raise sight according to \CFA's overloading rules. The default handler is 433 passed the exception given to the throw. When the default handler finishes 439 434 execution continues after the raise statement. 440 435 441 There is a global \defaultResumptionHandler{} is polymorphic over all442 resumption exception s and preforms a termination throw on the exception.443 The \defaultTerminationHandler{} can be overriden by providing a new444 function that is a better match.436 There is a global \defaultResumptionHandler{} that is polymorphic over all 437 resumption exception types and preforms a termination throw on the exception. 438 The \defaultTerminationHandler{} can be 439 customized by introducing a new or better match as well. 445 440 446 441 \subsubsection{Resumption Marking} 447 442 \label{s:ResumptionMarking} 443 448 444 A key difference between resumption and termination is that resumption does 449 445 not unwind the stack. A side effect that is that when a handler is matched 450 and run it's try block (the guarded statements) and every try statement451 searched before it are still on the stack. The re presence can lead to452 the recursiveresumption problem.446 and run, its try block (the guarded statements) and every try statement 447 searched before it are still on the stack. Their existence can lead to the recursive 448 resumption problem. 453 449 454 450 The recursive resumption problem is any situation where a resumption handler … … 463 459 \end{cfa} 464 460 When this code is executed, the guarded @throwResume@ starts a 465 search and match es the handler in the @catchResume@ clause. This466 call is placed on the stack above the try-block. The second raise then467 search es the same try block and putsanother instance of the468 same handler on the stack leading to infinite recursion.461 search and matchs the handler in the @catchResume@ clause. This 462 call is placed on the top of stack above the try-block. The second throw 463 searchs the same try block and puts call another instance of the 464 same handler on the stack leading to an infinite recursion. 469 465 470 466 While this situation is trivial and easy to avoid, much more complex cycles 471 467 can form with multiple handlers and different exception types. 472 468 473 To prevent all of these cases, a each try statement is ``marked" from the474 time the exception search reaches it to either when the exception is being 475 handled completes the matching handler or when the search reaches the base 476 of the stack.477 While a try statement is marked, its handlers are never matched, effecti vely478 skipping over itto the next try statement.469 To prevent all of these cases, the exception search marks the try statements it visits. 470 A try statement is marked when a match check is preformed with it and an 471 exception. The statement is unmarked when the handling of that exception 472 is completed or the search completes without finding a handler. 473 While a try statement is marked, its handlers are never matched, effectify 474 skipping over them to the next try statement. 479 475 480 476 \begin{center} … … 482 478 \end{center} 483 479 484 There are other sets of marking rules that could be used, 485 for instance, marking just the handlers that caught the exception, 486 would also prevent recursive resumption. 487 However, these rules mirror what happens with termination. 488 489 The try statements that are marked are the ones that would be removed from 490 the stack if this was a termination exception, that is those on the stack 491 between the handler and the raise statement. 492 This symmetry applies to the default handler as well, as both kinds of 493 default handlers are run at the raise statement, rather than (physically 494 or logically) at the bottom of the stack. 495 % In early development having the default handler happen after 496 % unmarking was just more useful. We assume that will continue. 480 These rules mirror what happens with termination. 481 When a termination throw happens in a handler, the search does not look at 482 any handlers from the original throw to the original catch because that 483 part of the stack is unwound. 484 A resumption raise in the same situation wants to search the entire stack, 485 but with marking, the search does match exceptions for try statements at equivalent sections 486 that would have been unwound by termination. 487 488 The symmetry between resumption termination is why this pattern is picked. 489 Other patterns, such as marking just the handlers that caught the exception, also work but 490 lack the symmetry, meaning there are more rules to remember. 497 491 498 492 \section{Conditional Catch} 493 499 494 Both termination and resumption handler clauses can be given an additional 500 495 condition to further control which exceptions they handle: … … 509 504 did not match. 510 505 511 The condition matching allows finer matching by checking506 The condition matching allows finer matching to check 512 507 more kinds of information than just the exception type. 513 508 \begin{cfa} … … 524 519 // Can't handle a failure relating to f2 here. 525 520 \end{cfa} 526 In this example the file that experienced the IO error is used to decide521 In this example, the file that experianced the IO error is used to decide 527 522 which handler should be run, if any at all. 528 523 … … 553 548 554 549 \subsection{Comparison with Reraising} 550 555 551 A more popular way to allow handlers to match in more detail is to reraise 556 552 the exception after it has been caught, if it could not be handled here. 557 On the surface these two features seem interchangeable. 558 559 If @throw;@ (no argument) starts a termination reraise, 560 which is the same as a raise but reuses the last caught exception, 561 then these two statements have the same behaviour: 553 On the surface these two features seem interchangable. 554 555 If @throw@ is used to start a termination reraise then these two statements 556 have the same behaviour: 562 557 \begin{cfa} 563 558 try { … … 579 574 } 580 575 \end{cfa} 581 That is, they will have the same behaviour in isolation. 582 Two things can expose differences between these cases. 583 584 One is the existance of multiple handlers on a single try statement. 585 A reraise skips all later handlers on this try statement but a conditional 586 catch does not. 587 Hence, if an earlier handler contains a reraise later handlers are 588 implicitly skipped, with a conditional catch they are not. 589 Still, they are equivalently powerful, 590 both can be used two mimick the behaviour of the other, 591 as reraise can pack arbitrary code in the handler and conditional catches 592 can put arbitrary code in the predicate. 593 % I was struggling with a long explination about some simple solutions, 594 % like repeating a condition on later handlers, and the general solution of 595 % merging everything together. I don't think it is useful though unless its 596 % for a proof. 597 % https://en.cppreference.com/w/cpp/language/throw 598 599 The question then becomes ``Which is a better default?" 600 We believe that not skipping possibly useful handlers is a better default. 601 If a handler can handle an exception it should and if the handler can not 602 handle the exception then it is probably safer to have that explicitly 603 described in the handler itself instead of implicitly described by its 604 ordering with other handlers. 605 % Or you could just alter the semantics of the throw statement. The handler 606 % index is in the exception so you could use it to know where to start 607 % searching from in the current try statement. 608 % No place for the `goto else;` metaphor. 609 610 The other issue is all of the discussion above assumes that the only 611 way to tell apart two raises is the exception being raised and the remaining 612 search path. 613 This is not true generally, the current state of the stack can matter in 614 a number of cases, even only for a stack trace after an program abort. 615 But \CFA has a much more significant need of the rest of the stack, the 616 default handlers for both termination and resumption. 617 618 % For resumption it turns out it is possible continue a raise after the 619 % exception has been caught, as if it hadn't been caught in the first place. 620 This becomes a problem combined with the stack unwinding used in termination 621 exception handling. 622 The stack is unwound before the handler is installed, and hence before any 623 reraises can run. So if a reraise happens the previous stack is gone, 624 the place on the stack where the default handler was supposed to run is gone, 625 if the default handler was a local function it may have been unwound too. 626 There is no reasonable way to restore that information, so the reraise has 627 to be considered as a new raise. 628 This is the strongest advantage conditional catches have over reraising, 629 they happen before stack unwinding and avoid this problem. 630 631 % The one possible disadvantage of conditional catch is that it runs user 632 % code during the exception search. While this is a new place that user code 633 % can be run destructors and finally clauses are already run during the stack 634 % unwinding. 635 % 636 % https://www.cplusplus.com/reference/exception/current_exception/ 637 % `exception_ptr current_exception() noexcept;` 638 % https://www.python.org/dev/peps/pep-0343/ 576 However, if there are further handlers after this handler only the first is 577 check. For multiple handlers on a single try block that could handle the 578 same exception, the equivalent translations to conditional catch becomes more complex, resulting is multiple nested try blocks for all possible reraises. 579 So while catch-with-reraise is logically equivilant to conditional catch, there is a lexical explosion for the former. 580 581 \PAB{I think the following discussion makes an incorrect assumption. 582 A conditional catch CAN happen with the stack unwound. 583 Roy talked about this issue in Section 2.3.3 here: \newline 584 \url{http://plg.uwaterloo.ca/theses/KrischerThesis.pdf}} 585 586 Specifically for termination handling, a 587 conditional catch happens before the stack is unwound, but a reraise happens 588 afterwards. Normally this might only cause you to loose some debug 589 information you could get from a stack trace (and that can be side stepped 590 entirely by collecting information during the unwind). But for \CFA there is 591 another issue, if the exception is not handled the default handler should be 592 run at the site of the original raise. 593 594 There are two problems with this: the site of the original raise does not 595 exist anymore and the default handler might not exist anymore. The site is 596 always removed as part of the unwinding, often with the entirety of the 597 function it was in. The default handler could be a stack allocated nested 598 function removed during the unwind. 599 600 This means actually trying to pretend the catch didn't happening, continuing 601 the original raise instead of starting a new one, is infeasible. 602 That is the expected behaviour for most languages and we can't replicate 603 that behaviour. 639 604 640 605 \section{Finally Clauses} 641 606 \label{s:FinallyClauses} 607 642 608 Finally clauses are used to preform unconditional clean-up when leaving a 643 609 scope and are placed at the end of a try statement after any handler clauses: … … 652 618 The @FINALLY_BLOCK@ is executed when the try statement is removed from the 653 619 stack, including when the @GUARDED_BLOCK@ finishes, any termination handler 654 finishes or during an unwind.620 finishes, or during an unwind. 655 621 The only time the block is not executed is if the program is exited before 656 622 the stack is unwound. … … 668 634 669 635 Not all languages with unwinding have finally clauses. Notably \Cpp does 670 without it as descructors, and the RAII design pattern, serve a similar role. 671 Although destructors and finally clauses can be used in the same cases, 672 they have their own strengths, similar to top-level function and lambda 673 functions with closures. 674 Destructors take more work for their first use, but if there is clean-up code 675 that needs to be run every time a type is used they soon become much easier 676 to set-up. 677 On the other hand finally clauses capture the local context, so is easy to 678 use when the clean-up is not dependent on the type of a variable or requires 679 information from multiple variables. 680 % To Peter: I think these are the main points you were going for. 636 without it as destructors with RAII serve a similar role. Although destructors and 637 finally clauses have overlapping usage cases, they have their own 638 specializations, like top-level functions and lambda functions with closures. 639 Destructors take more work if a number of unrelated, local variables without destructors or dynamically allocated variables must be passed for de-intialization. 640 Maintaining this destructor during local-block modification is a source of errors. 641 A finally clause places local de-intialization inline with direct access to all local variables. 681 642 682 643 \section{Cancellation} … … 691 652 raise, this exception is not used in matching only to pass information about 692 653 the cause of the cancellation. 693 (This also means matching cannot fail so there is no default handler.)654 (This restriction also means matching cannot fail so there is no default handler.) 694 655 695 656 After @cancel_stack@ is called the exception is copied into the EHM's memory 696 and the current stack is unwound. 697 The behaviour after that depends on the kind of stack being cancelled. 657 and the current stack is 658 unwound. 659 The result of a cancellation depends on the kind of stack that is being unwound. 698 660 699 661 \paragraph{Main Stack} … … 702 664 After the main stack is unwound there is a program-level abort. 703 665 704 There are two reasons for these semantics. 705 The first is that it had to do this abort. 666 There are two reasons for this semantics. The first is that it obviously had to do the abort 706 667 in a sequential program as there is nothing else to notify and the simplicity 707 668 of keeping the same behaviour in sequential and concurrent programs is good. 708 Also, even in concurrent programs there may not currently be any other stacks 709 and even if other stacks do exist, main has no way to know where they are. 669 \PAB{I do not understand this sentence. $\Rightarrow$ Also, even in concurrent programs, there is no stack that an innate connection 670 to, so it would have be explicitly managed.} 710 671 711 672 \paragraph{Thread Stack} … … 719 680 and an implicit join (from a destructor call). The explicit join takes the 720 681 default handler (@defaultResumptionHandler@) from its calling context while 721 the implicit join provides its own ;which does a program abort if the682 the implicit join provides its own, which does a program abort if the 722 683 @ThreadCancelled@ exception cannot be handled. 723 684 724 The communication and synchronization are done here because threads only have 725 two structural points (not dependent on user-code) where 726 communication /synchronization happens: start and join.685 \PAB{Communication can occur during the lifetime of a thread using shared variable and \lstinline{waitfor} statements. 686 Are you sure you mean communication here? Maybe you mean synchronization (rendezvous) point. $\Rightarrow$ Communication is done at join because a thread only has two points of 687 communication with other threads: start and join.} 727 688 Since a thread must be running to perform a cancellation (and cannot be 728 689 cancelled from another stack), the cancellation must be after start and 729 before the join, so join is use d.690 before the join, so join is use. 730 691 731 692 % TODO: Find somewhere to discuss unwind collisions. … … 740 701 satisfies the @is_coroutine@ trait. 741 702 After a coroutine stack is unwound, control returns to the @resume@ function 742 that most recently resumed it. @resume@reports a743 @CoroutineCancelled@ exception, which contains areferences to the cancelled703 that most recently resumed it. The resume reports a 704 @CoroutineCancelled@ exception, which contains references to the cancelled 744 705 coroutine and the exception used to cancel it. 745 706 The @resume@ function also takes the \defaultResumptionHandler{} from the 746 caller's context and passes it to the internal report.707 caller's context and passes it to the internal cancellation. 747 708 748 709 A coroutine knows of two other coroutines, its starter and its last resumer. -
doc/theses/andrew_beach_MMath/intro.tex
r2f19e03 r4f1b8f3f 1 1 \chapter{Introduction} 2 2 3 % The highest level overview of Cforall and EHMs. Get this done right away. 4 This thesis goes over the design and implementation of the exception handling 5 mechanism (EHM) of 6 \ CFA (pernounced sea-for-all and may be written Cforall or CFA).3 \PAB{Stay in the present tense. \newline 4 \url{https://plg.uwaterloo.ca/~pabuhr/technicalWriting.shtml}} 5 \newline 6 \PAB{Note, \lstinline{lstlisting} normally bolds keywords. None of the keywords in your thesis are bolded.} 7 7 8 % Now take a step back and explain what exceptions are generally. 9 Exception handling provides dynamic inter-function control flow. 8 % Talk about Cforall and exceptions generally. 9 %This thesis goes over the design and implementation of the exception handling 10 %mechanism (EHM) of 11 %\CFA (pernounced sea-for-all and may be written Cforall or CFA). 12 Exception handling provides alternative dynamic inter-function control flow. 10 13 There are two forms of exception handling covered in this thesis: 11 14 termination, which acts as a multi-level return, 12 15 and resumption, which is a dynamic function call. 13 Termination handling is much more common, 14 to the extent that it is often seen 15 This seperation is uncommon because termination exception handling is so 16 much more common that it is often assumed. 17 % WHY: Mention other forms of continuation and \cite{CommonLisp} here? 18 A language's EHM is the combination of language syntax and run-time 19 components that are used to construct, raise and handle exceptions, 20 including all control flow. 16 Note, termination exception handling is so common it is often assumed to be the only form. 17 Lesser know derivations of inter-function control flow are continuation passing in Lisp~\cite{CommonLisp}. 21 18 22 19 Termination exception handling allows control to return to any previous … … 38 35 most of the cost only when the error actually occurs. 39 36 37 % Overview of exceptions in Cforall. 38 39 \PAB{You need section titles here. Don't take them out.} 40 40 41 \section{Thesis Overview} 41 This work describes the design and implementation of the \CFA EHM. 42 43 This thesis goes over the design and implementation of the exception handling 44 mechanism (EHM) of 45 \CFA (pernounced sea-for-all and may be written Cforall or CFA). 46 %This thesis describes the design and implementation of the \CFA EHM. 42 47 The \CFA EHM implements all of the common exception features (or an 43 48 equivalent) found in most other EHMs and adds some features of its own. … … 72 77 harder to replicate in other programming languages. 73 78 79 \section{Background} 80 74 81 % Talk about other programming languages. 75 82 Some existing programming languages that include EHMs/exception handling … … 78 85 Exceptions also can replace return codes and return unions. 79 86 In functional languages will also sometimes fold exceptions into monads. 87 88 \PAB{You must demonstrate knowledge of background material here. 89 It should be at least a full page.} 90 91 \section{Contributions} 80 92 81 93 The contributions of this work are: … … 90 102 \end{enumerate} 91 103 92 \todo{I can't figure out a good lead-in to the roadmap.} 93 The next section covers the existing state of exceptions. 94 The existing state of \CFA is also covered in \autoref{c:existing}. 95 The new features are introduced in \autoref{c:features}, 96 which explains their usage and design. 104 \todo{I can't figure out a good lead-in to the overview.} 105 Covering the existing \CFA features in \autoref{c:existing}. 106 Then the new features are introduce in \autoref{c:features}, explaining their 107 usage and design. 97 108 That is followed by the implementation of those features in 98 109 \autoref{c:implement}. 99 The performance results are examined in \autoref{c:performance}. 100 Possibilities to extend this project are discussed in \autoref{c:future}. 101 102 \section{Background} 103 \label{s:background} 104 105 Exception handling is not a new concept, 106 with papers on the subject dating back 70s. 107 108 Their were popularised by \Cpp, 109 which added them in its first major wave of non-object-orientated features 110 in 1990. 111 % https://en.cppreference.com/w/cpp/language/history 112 113 Java was the next popular language to use exceptions. It is also the most 114 popular language with checked exceptions. 115 Checked exceptions are part of the function interface they are raised from. 116 This includes functions they propogate through, until a handler for that 117 type of exception is found. 118 This makes exception information explicit, which can improve clarity and 119 safety, but can slow down programming. 120 Some of these, such as dealing with high-order methods or an overly specified 121 throws clause, are technical. However some of the issues are much more 122 human, in that writing/updating all the exception signatures can be enough 123 of a burden people will hack the system to avoid them. 124 Including the ``catch-and-ignore" pattern where a catch block is used without 125 anything to repair or recover from the exception. 126 127 %\subsection 128 Resumption exceptions have been much less popular. 129 Although resumption has a history as old as termination's, very few 130 programming languages have implement them. 131 % http://bitsavers.informatik.uni-stuttgart.de/pdf/xerox/parc/techReports/ 132 % CSL-79-3_Mesa_Language_Manual_Version_5.0.pdf 133 Mesa is one programming languages that did and experiance with that 134 languages is quoted as being one of the reasons resumptions were not 135 included in the \Cpp standard. 136 % https://en.wikipedia.org/wiki/Exception_handling 137 \todo{A comment about why we did include them when they are so unpopular 138 might be approprate.} 139 140 %\subsection 141 Functional languages, tend to use solutions like the return union, but some 142 exception-like constructs still appear. 143 144 For instance Haskell's built in error mechanism can make the result of any 145 expression, including function calls. Any expression that examines an 146 error value will in-turn produce an error. This continues until the main 147 function produces an error or until it is handled by one of the catch 148 functions. 149 150 %\subsection 151 More recently exceptions seem to be vanishing from newer programming 152 languages. 153 Rust and Go reduce this feature to panics. 154 Panicing is somewhere between a termination exception and a program abort. 155 Notably in Rust a panic can trigger either, a panic may unwind the stack or 156 simply kill the process. 157 % https://doc.rust-lang.org/std/panic/fn.catch_unwind.html 158 Go's panic is much more similar to a termination exception but there is 159 only a catch-all function with \code{Go}{recover()}. 160 So exceptions still are appearing, just in reduced forms. 161 162 %\subsection 163 Exception handling's most common use cases are in error handling. 164 Here are some other ways to handle errors and comparisons with exceptions. 165 \begin{itemize} 166 \item\emph{Error Codes}: 167 This pattern uses an enumeration (or just a set of fixed values) to indicate 168 that an error has occured and which error it was. 169 170 There are some issues if a function wants to return an error code and another 171 value. The main issue is that it can be easy to forget checking the error 172 code, which can lead to an error being quitely and implicitly ignored. 173 Some new languages have tools that raise warnings if the return value is 174 discarded to avoid this. 175 It also puts more code on the main execution path. 176 \item\emph{Special Return with Global Store}: 177 A function that encounters an error returns some value indicating that it 178 encountered a value but store which error occured in a fixed global location. 179 180 Perhaps the C standard @errno@ is the most famous example of this, 181 where some standard library functions will return some non-value (often a 182 NULL pointer) and set @errno@. 183 184 This avoids the multiple results issue encountered with straight error codes 185 but otherwise many of the same advantages and disadvantages. 186 It does however introduce one other major disadvantage: 187 Everything that uses that global location must agree on all possible errors. 188 \item\emph{Return Union}: 189 Replaces error codes with a tagged union. 190 Success is one tag and the errors are another. 191 It is also possible to make each possible error its own tag and carry its own 192 additional information, but the two branch format is easy to make generic 193 so that one type can be used everywhere in error handling code. 194 195 This pattern is very popular in functional or semi-functional language, 196 anything with primitive support for tagged unions (or algebraic data types). 197 % We need listing Rust/rust to format code snipits from it. 198 % Rust's \code{rust}{Result<T, E>} 199 200 The main disadvantage is again it puts code on the main execution path. 201 This is also the first technique that allows for more information about an 202 error, other than one of a fix-set of ids, to be sent. 203 They can be missed but some languages can force that they are checked. 204 It is also implicitly forced in any languages with checked union access. 205 \item\emph{Handler Functions}: 206 On error the function that produced the error calls another function to 207 handle it. 208 The handler function can be provided locally (passed in as an argument, 209 either directly as as a field of a structure/object) or globally (a global 210 variable). 211 212 C++ uses this as its fallback system if exception handling fails. 213 \snake{std::terminate_handler} and for a time \snake{std::unexpected_handler} 214 215 Handler functions work a lot like resumption exceptions. 216 The difference is they are more expencive to set up but cheaper to use, and 217 so are more suited to more fequent errors. 218 The exception being global handlers if they are rarely change as the time 219 in both cases strinks towards zero. 220 \end{itemize} 221 222 %\subsection 223 Because of their cost exceptions are rarely used for hot paths of execution. 224 There is an element of self-fulfilling prophocy here as implementation 225 techniques have been designed to make exceptions cheap to set-up at the cost 226 of making them expencive to use. 227 Still, use of exceptions for other tasks is more common in higher-level 228 scripting languages. 229 An iconic example is Python's StopIteration exception which is thrown by 230 an iterator to indicate that it is exausted. Combined with Python's heavy 231 use of the iterator based for-loop. 232 % https://docs.python.org/3/library/exceptions.html#StopIteration 110 % Future Work \autoref{c:future}
Note:
See TracChangeset
for help on using the changeset viewer.