Changeset 5407cdc for doc/theses/andrew_beach_MMath/features.tex
- Timestamp:
- Apr 28, 2021, 4:56:50 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
- Children:
- 8d66610
- Parents:
- feacef9 (diff), b7fd2db6 (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. - File:
-
- 1 edited
-
doc/theses/andrew_beach_MMath/features.tex (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/features.tex
rfeacef9 r5407cdc 2 2 3 3 This chapter covers the design and user interface of the \CFA 4 exception-handling mechanism. 4 exception-handling mechanism (EHM). % or exception system. 5 6 We will begin with an overview of EHMs in general. It is not a strict 7 definition of all EHMs nor an exaustive list of all possible features. 8 However it does cover the most common structure and features found in them. 9 10 % We should cover what is an exception handling mechanism and what is an 11 % exception before this. Probably in the introduction. Some of this could 12 % move there. 13 \paragraph{Raise / Handle} 14 An exception operation has two main parts: raise and handle. 15 These terms are sometimes also known as throw and catch but this work uses 16 throw/catch as a particular kind of raise/handle. 17 These are the two parts that the user will write themselves and may 18 be the only two pieces of the EHM that have any syntax in the language. 19 20 \subparagraph{Raise} 21 The raise is the starting point for exception handling. It marks the beginning 22 of exception handling by \newterm{raising} an excepion, which passes it to 23 the EHM. 24 25 Some well known examples include the @throw@ statements of \Cpp and Java and 26 the \codePy{raise} statement from Python. In real systems a raise may preform 27 some other work (such as memory management) but for the purposes of this 28 overview that can be ignored. 29 30 \subparagraph{Handle} 31 The purpose of most exception operations is to run some user code to handle 32 that exception. This code is given, with some other information, in a handler. 33 34 A handler has three common features: the previously mentioned user code, a 35 region of code they cover and an exception label/condition that matches 36 certain exceptions. 37 Only raises inside the covered region and raising exceptions that match the 38 label can be handled by a given handler. 39 Different EHMs will have different rules to pick a handler 40 if multipe handlers could be used such as ``best match" or ``first found". 41 42 The @try@ statements of \Cpp, Java and Python are common examples. All three 43 also show another common feature of handlers, they are grouped by the covered 44 region. 45 46 \paragraph{Propagation} 47 After an exception is raised comes what is usually the biggest step for the 48 EHM: finding and setting up the handler. The propogation from raise to 49 handler can be broken up into three different tasks: searching for a handler, 50 matching against the handler and installing the handler. 51 52 \subparagraph{Searching} 53 The EHM begins by searching for handlers that might be used to handle 54 the exception. Searching is usually independent of the exception that was 55 thrown as it looks for handlers that have the raise site in their covered 56 region. 57 This includes handlers in the current function, as well as any in callers 58 on the stack that have the function call in their covered region. 59 60 \subparagraph{Matching} 61 Each handler found has to be matched with the raised exception. The exception 62 label defines a condition that be use used with exception and decides if 63 there is a match or not. 64 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 finds 67 a possible handler. 68 69 \subparagraph{Installing} 70 After a handler is chosen it must be made ready to run. 71 The implementation can vary widely to fit with the rest of the 72 design of the EHM. The installation step might be trivial or it could be 73 the most expensive step in handling an exception. The latter tends to be the 74 case when stack unwinding is involved. 75 76 If a matching handler is not guarantied to be found the EHM will need a 77 different course of action here in the cases where no handler matches. 78 This is only required with unchecked exceptions as checked exceptions 79 (such as in Java) can make than guaranty. 80 This different action can also be installing a handler but it is usually an 81 implicat and much more general one. 82 83 \subparagraph{Hierarchy} 84 A common way to organize exceptions is in a hierarchical structure. 85 This is especially true in object-orientated languages where the 86 exception hierarchy is a natural extension of the object hierarchy. 87 88 Consider the following hierarchy of exceptions: 89 \begin{center} 90 \input{exception-hierarchy} 91 \end{center} 92 93 A handler labelled with any given exception can handle exceptions of that 94 type or any child type of that exception. The root of the exception hierarchy 95 (here \codeC{exception}) acts as a catch-all, leaf types catch single types 96 and the exceptions in the middle can be used to catch different groups of 97 related exceptions. 98 99 This system has some notable advantages, such as multiple levels of grouping, 100 the ability for libraries to add new exception types and the isolation 101 between different sub-hierarchies. 102 This design is used in \CFA even though it is not a object-orientated 103 language using different tools to create the hierarchy. 104 105 % Could I cite the rational for the Python IO exception rework? 106 107 \paragraph{Completion} 108 After the handler has finished the entire exception operation has to complete 109 and continue executing somewhere else. This step is usually simple, 110 both logically and in its implementation, as the installation of the handler 111 is usually set up to do most of the work. 112 113 The EHM can return control to many different places, 114 the most common are after the handler definition and after the raise. 115 116 \paragraph{Communication} 117 For effective exception handling, additional information is usually passed 118 from the raise to the handler. 119 So far only communication of the exceptions' identity has been covered. 120 A common method is putting fields into the exception instance and giving the 121 handler access to them. 5 122 6 123 \section{Virtuals} 7 Virtual types and casts are not part of the exception system nor are they 8 required for an exception system. But an object-oriented style hierarchy is a 9 great way of organizing exceptions so a minimal virtual system has been added 10 to \CFA. 11 12 The pattern of a simple hierarchy was borrowed from object-oriented 13 programming was chosen for several reasons. 14 The first is that it allows new exceptions to be added in user code 15 and in libraries independently of each other. Another is it allows for 16 different levels of exception grouping (all exceptions, all IO exceptions or 17 a particular IO exception). Also it also provides a simple way of passing 18 data back and forth across the throw. 19 20 Virtual types and casts are not required for a basic exception-system but are 21 useful for advanced exception features. However, \CFA is not object-oriented so 22 there is no obvious concept of virtuals. Hence, to create advanced exception 23 features for this work, I needed to design and implement a virtual-like 24 system for \CFA. 25 26 % NOTE: Maybe we should but less of the rational here. 27 Object-oriented languages often organized exceptions into a simple hierarchy, 28 \eg Java. 29 \begin{center} 30 \setlength{\unitlength}{4000sp}% 31 \begin{picture}(1605,612)(2011,-1951) 32 \put(2100,-1411){\vector(1, 0){225}} 33 \put(3450,-1411){\vector(1, 0){225}} 34 \put(3550,-1411){\line(0,-1){225}} 35 \put(3550,-1636){\vector(1, 0){150}} 36 \put(3550,-1636){\line(0,-1){225}} 37 \put(3550,-1861){\vector(1, 0){150}} 38 \put(2025,-1490){\makebox(0,0)[rb]{\LstBasicStyle{exception}}} 39 \put(2400,-1460){\makebox(0,0)[lb]{\LstBasicStyle{arithmetic}}} 40 \put(3750,-1460){\makebox(0,0)[lb]{\LstBasicStyle{underflow}}} 41 \put(3750,-1690){\makebox(0,0)[lb]{\LstBasicStyle{overflow}}} 42 \put(3750,-1920){\makebox(0,0)[lb]{\LstBasicStyle{zerodivide}}} 43 \end{picture}% 44 \end{center} 45 The hierarchy provides the ability to handle an exception at different degrees 46 of specificity (left to right). Hence, it is possible to catch a more general 47 exception-type in higher-level code where the implementation details are 48 unknown, which reduces tight coupling to the lower-level implementation. 49 Otherwise, low-level code changes require higher-level code changes, \eg, 50 changing from raising @underflow@ to @overflow@ at the low level means changing 51 the matching catch at the high level versus catching the general @arithmetic@ 52 exception. In detail, each virtual type may have a parent and can have any 53 number of children. A type's descendants are its children and its children's 54 descendants. A type may not be its own descendant. 55 56 The exception hierarchy allows a handler (@catch@ clause) to match multiple 57 exceptions, \eg a base-type handler catches both base and derived 58 exception-types. 59 \begin{cfa} 60 try { 61 ... 62 } catch(arithmetic &) { 63 ... // handle arithmetic, underflow, overflow, zerodivide 64 } 65 \end{cfa} 66 Most exception mechanisms perform a linear search of the handlers and select 67 the first matching handler, so the order of handers is now important because 68 matching is many to one. 69 70 Each virtual type needs an associated virtual table. A virtual table is a 71 structure with fields for all the virtual members of a type. A virtual type has 72 all the virtual members of its parent and can add more. It may also update the 73 values of the virtual members and often does. 124 Virtual types and casts are not part of \CFA's EHM nor are they required for 125 any EHM. But \CFA uses a hierarchial system of exceptions and this feature 126 is leveraged to create that. 127 128 % Maybe talk about why the virtual system is so minimal. 129 % Created for but not a part of the exception system. 130 131 The virtual system supports multiple ``trees" of types. Each tree is 132 a simple hierarchy with a single root type. Each type in a tree has exactly 133 one parent -- except for the root type which has zero parents -- and any 134 number of children. 135 Any type that belongs to any of these trees is called a virtual type. 136 137 % A type's ancestors are its parent and its parent's ancestors. 138 % The root type has no ancestors. 139 % A type's decendents are its children and its children's decendents. 140 141 Every virtual type also has a list of virtual members. Children inherit 142 their parent's list of virtual members but may add new members to it. 143 It is important to note that these are virtual members, not virtual methods 144 of object-orientated programming, and can be of any type. 145 However, since \CFA has function pointers and they are allowed, virtual 146 members can be used to mimic virtual methods. 147 148 Each virtual type has a unique id. 149 This unique id and all the virtual members are combined 150 into a virtual table type. Each virtual type has a pointer to a virtual table 151 as a hidden field. 152 153 Up until this point the virtual system is similar to ones found in 154 object-orientated languages but this where \CFA diverges. Objects encapsulate a 155 single set of behaviours in each type, universally across the entire program, 156 and indeed all programs that use that type definition. In this sense the 157 types are ``closed" and cannot be altered. 158 159 In \CFA types do not encapsulate any behaviour. Traits are local and 160 types can begin to statify a trait, stop satifying a trait or satify the same 161 trait in a different way at any lexical location in the program. 162 In this sense they are ``open" as they can change at any time. This means it 163 is implossible to pick a single set of functions that repersent the type's 164 implementation across the program. 165 166 \CFA side-steps this issue by not having a single virtual table for each 167 type. A user can define virtual tables which are filled in at their 168 declaration and given a name. Anywhere that name is visible, even if it was 169 defined locally inside a function (although that means it will not have a 170 static lifetime), it can be used. 171 Specifically, a virtual type is ``bound" to a virtual table which 172 sets the virtual members for that object. The virtual members can be accessed 173 through the object. 74 174 75 175 While much of the virtual infrastructure is created, it is currently only used … … 83 183 \Cpp syntax for special casts. Both the type of @EXPRESSION@ and @TYPE@ must be 84 184 a pointer to a virtual type. 85 The cast dynamically checks if the @EXPRESSION@ type is the same or a sub type185 The cast dynamically checks if the @EXPRESSION@ type is the same or a sub-type 86 186 of @TYPE@, and if true, returns a pointer to the 87 187 @EXPRESSION@ object, otherwise it returns @0p@ (null pointer). … … 101 201 \end{cfa} 102 202 The trait is defined over two types, the exception type and the virtual table 103 type. This should be one-to-one ,each exception type has only one virtual203 type. This should be one-to-one: each exception type has only one virtual 104 204 table type and vice versa. The only assertion in the trait is 105 205 @get_exception_vtable@, which takes a pointer of the exception type and 106 206 returns a reference to the virtual table type instance. 107 207 208 % TODO: This section, and all references to get_exception_vtable, are 209 % out-of-data. Perhaps wait until the update is finished before rewriting it. 108 210 The function @get_exception_vtable@ is actually a constant function. 109 Re cardless of the value passed in (including the null pointer) it should211 Regardless of the value passed in (including the null pointer) it should 110 212 return a reference to the virtual table instance for that type. 111 213 The reason it is a function instead of a constant is that it make type … … 119 221 % similar system I know of (except Agda's I guess) so I took it out. 120 222 121 There are two more traits for exceptions @is_termination_exception@ and 122 @is_resumption_exception@. They are defined as follows: 123 223 There are two more traits for exceptions defined as follows: 124 224 \begin{cfa} 125 225 trait is_termination_exception( … … 133 233 }; 134 234 \end{cfa} 135 136 In other words they make sure that a given type and virtual type is an 137 exception and defines one of the two default handlers. These default handlers 138 are used in the main exception handling operations \see{Exception Handling} 139 and their use will be detailed there. 140 141 However all three of these traits can be trickly to use directly. 142 There is a bit of repetition required but 235 Both traits ensure a pair of types are an exception type and its virtual table 236 and defines one of the two default handlers. The default handlers are used 237 as fallbacks and are discussed in detail in \VRef{s:ExceptionHandling}. 238 239 However, all three of these traits can be tricky to use directly. 240 While there is a bit of repetition required, 143 241 the largest issue is that the virtual table type is mangled and not in a user 144 facing way. So the re are three macros that can be used to wrap these traits145 when you need to referto the names:242 facing way. So these three macros are provided to wrap these traits to 243 simplify referring to the names: 146 244 @IS_EXCEPTION@, @IS_TERMINATION_EXCEPTION@ and @IS_RESUMPTION_EXCEPTION@. 147 245 148 All t ake one or two arguments. The first argument is the name of the149 exception type. Its unmangled and mangled form are passedto the trait.246 All three take one or two arguments. The first argument is the name of the 247 exception type. The macro passes its unmangled and mangled form to the trait. 150 248 The second (optional) argument is a parenthesized list of polymorphic 151 arguments. This argument should onlywith polymorphic exceptions and the152 list willbe passed to both types.153 In the current set-up the base name and the polymorphic arguments have to154 match so these macros can be used without losing flexability.249 arguments. This argument is only used with polymorphic exceptions and the 250 list is be passed to both types. 251 In the current set-up, the two types always have the same polymorphic 252 arguments so these macros can be used without losing flexibility. 155 253 156 254 For example consider a function that is polymorphic over types that have a … … 162 260 163 261 \section{Exception Handling} 164 \ CFA provides two kinds of exception handling, termination and resumption.165 These twin operations are the core of the exception handling mechanism and 166 are the reason for the features of exceptions.262 \label{s:ExceptionHandling} 263 \CFA provides two kinds of exception handling: termination and resumption. 264 These twin operations are the core of \CFA's exception handling mechanism. 167 265 This section will cover the general patterns shared by the two operations and 168 266 then go on to cover the details each individual operation. 169 267 170 Both operations follow the same set of steps to do their operation. They both 171 start with the user preforming a throw on an exception. 172 Then there is the search for a handler, if one is found than the exception 173 is caught and the handler is run. After that control returns to normal 174 execution. 175 268 Both operations follow the same set of steps. 269 Both start with the user preforming a raise on an exception. 270 Then the exception propogates up the stack. 271 If a handler is found the exception is caught and the handler is run. 272 After that control returns to normal execution. 176 273 If the search fails a default handler is run and then control 177 returns to normal execution immediately. That is where the default handlers 178 @defaultTermiationHandler@ and @defaultResumptionHandler@ are used. 274 returns to normal execution after the raise. 275 276 This general description covers what the two kinds have in common. 277 Differences include how propogation is preformed, where exception continues 278 after an exception is caught and handled and which default handler is run. 179 279 180 280 \subsection{Termination} 181 281 \label{s:Termination} 182 183 Termination handling is more familiar kind and used in most programming 282 Termination handling is the familiar kind and used in most programming 184 283 languages with exception handling. 185 It is dynamic, non-local goto. If a throw is successful then the stack will 186 be unwound and control will (usually) continue in a different function on 187 the call stack. They are commonly used when an error has occured and recovery 188 is impossible in the current function. 284 It is dynamic, non-local goto. If the raised exception is matched and 285 handled the stack is unwound and control will (usually) continue the function 286 on the call stack that defined the handler. 287 Termination is commonly used when an error has occurred and recovery is 288 impossible locally. 189 289 190 290 % (usually) Control can continue in the current function but then a different 191 291 % control flow construct should be used. 192 292 193 A termination throwis started with the @throw@ statement:293 A termination raise is started with the @throw@ statement: 194 294 \begin{cfa} 195 295 throw EXPRESSION; 196 296 \end{cfa} 197 297 The expression must return a reference to a termination exception, where the 198 termination exception is any type that satifies @is_termination_exception@ 199 at the call site. 200 Through \CFA's trait system the functions in the traits are passed into the 201 throw code. A new @defaultTerminationHandler@ can be defined in any scope to 298 termination exception is any type that satisfies the trait 299 @is_termination_exception@ at the call site. 300 Through \CFA's trait system the trait functions are implicity passed into the 301 throw code and the EHM. 302 A new @defaultTerminationHandler@ can be defined in any scope to 202 303 change the throw's behavior (see below). 203 304 204 The throw will copy the provided exception into managed memory. It is the 205 user's responcibility to ensure the original exception is cleaned up if the 206 stack is unwound (allocating it on the stack should be sufficient). 207 208 Then the exception system searches the stack using the copied exception. 209 It starts starts from the throw and proceeds to the base of the stack, 305 The throw will copy the provided exception into managed memory to ensure 306 the exception is not destroyed if the stack is unwound. 307 It is the user's responsibility to ensure the original exception is cleaned 308 up wheither the stack is unwound or not. Allocating it on the stack is 309 usually sufficient. 310 311 Then propogation starts with the search. \CFA uses a ``first match" rule so 312 matching is preformed with the copied exception as the search continues. 313 It starts from the throwing function and proceeds to the base of the stack, 210 314 from callee to caller. 211 315 At each stack frame, a check is made for resumption handlers defined by the … … 214 318 try { 215 319 GUARDED_BLOCK 216 } catch (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) {320 } catch (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { 217 321 HANDLER_BLOCK$\(_1\)$ 218 } catch (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) {322 } catch (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { 219 323 HANDLER_BLOCK$\(_2\)$ 220 324 } 221 325 \end{cfa} 222 When viewed on its own a try statement will simply exceute the statements in223 @GUARDED_BLOCK@ and when those are finished the try statement finishes.326 When viewed on its own, a try statement will simply execute the statements 327 in @GUARDED_BLOCK@ and when those are finished the try statement finishes. 224 328 225 329 However, while the guarded statements are being executed, including any 226 functions they invoke, all the handlers following the try block are now 227 or any functions invoked from those 228 statements, throws an exception, and the exception 229 is not handled by a try statement further up the stack, the termination 230 handlers are searched for a matching exception type from top to bottom. 231 232 Exception matching checks the representation of the thrown exception-type is 233 the same or a descendant type of the exception types in the handler clauses. If 234 it is the same of a descendent of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ is 330 invoked functions, all the handlers in the statement are now on the search 331 path. If a termination exception is thrown and not handled further up the 332 stack they will be matched against the exception. 333 334 Exception matching checks the handler in each catch clause in the order 335 they appear, top to bottom. If the representation of the thrown exception type 336 is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ 337 (if provided) is 235 338 bound to a pointer to the exception and the statements in @HANDLER_BLOCK@$_i$ 236 339 are executed. If control reaches the end of the handler, the exception is 237 340 freed and control continues after the try statement. 238 341 239 If no handler is found during the search then the default handler is run. 342 If no termination handler is found during the search then the default handler 343 (@defaultTerminationHandler@) is run. 240 344 Through \CFA's trait system the best match at the throw sight will be used. 241 345 This function is run and is passed the copied exception. After the default 242 346 handler is run control continues after the throw statement. 243 347 244 There is a global @defaultTerminationHandler@ that cancels the current stack 245 with the copied exception. However it is generic over all exception types so 246 new default handlers can be defined for different exception types and so 247 different exception types can have different default handlers. 348 There is a global @defaultTerminationHandler@ that is polymorphic over all 349 exception types. Since it is so general a more specific handler can be 350 defined and will be used for those types, effectively overriding the handler 351 for particular exception type. 352 The global default termination handler performs a cancellation 353 \see{\VRef{s:Cancellation}} on the current stack with the copied exception. 248 354 249 355 \subsection{Resumption} 250 356 \label{s:Resumption} 251 357 252 Resumption exception handling is a less common formthan termination but is253 just as old~\cite{Goodenough75} and is in some sense simpler.254 It is a dynamic, non-local function call. If the throw is successful a255 closure will be taken from up the stack and executed, after which the throwing 256 function will continue executing.257 These are most often used when an error occur ed and if the error is repaired358 Resumption exception handling is less common than termination but is 359 just as old~\cite{Goodenough75} and is simpler in many ways. 360 It is a dynamic, non-local function call. If the raised exception is 361 matched a closure will be taken from up the stack and executed, 362 after which the raising function will continue executing. 363 These are most often used when an error occurred and if the error is repaired 258 364 then the function can continue. 259 365 … … 262 368 throwResume EXPRESSION; 263 369 \end{cfa} 264 The semantics of the @throwResume@ statement are like the @throw@, but the 265 expression has return a reference a type that satifies the trait 266 @is_resumption_exception@. The assertions from this trait are available to 370 It works much the same way as the termination throw. 371 The expression must return a reference to a resumption exception, 372 where the resumption exception is any type that satisfies the trait 373 @is_resumption_exception@ at the call site. 374 The assertions from this trait are available to 267 375 the exception system while handling the exception. 268 376 269 At runtime, no copies are made. As the stack is not unwound the exception and 377 At run-time, no exception copy is made. 378 As the stack is not unwound the exception and 270 379 any values on the stack will remain in scope while the resumption is handled. 271 380 272 Then the exception system searches the stack using the provided exception. 273 It starts starts from the throw and proceeds to the base of the stack, 274 from callee to caller. 381 The EHM then begins propogation. The search starts from the raise in the 382 resuming function and proceeds to the base of the stack, from callee to caller. 275 383 At each stack frame, a check is made for resumption handlers defined by the 276 384 @catchResume@ clauses of a @try@ statement. … … 278 386 try { 279 387 GUARDED_BLOCK 280 } catchResume (EXCEPTION_TYPE$\(_1\)$ * NAME$\(_1\)$) {388 } catchResume (EXCEPTION_TYPE$\(_1\)$ * [NAME$\(_1\)$]) { 281 389 HANDLER_BLOCK$\(_1\)$ 282 } catchResume (EXCEPTION_TYPE$\(_2\)$ * NAME$\(_2\)$) {390 } catchResume (EXCEPTION_TYPE$\(_2\)$ * [NAME$\(_2\)$]) { 283 391 HANDLER_BLOCK$\(_2\)$ 284 392 } 285 393 \end{cfa} 286 If the handlers are not involved in a search this will simply execute the 287 @GUARDED_BLOCK@ and then continue to the next statement. 288 Its purpose is to add handlers onto the stack. 289 (Note, termination and resumption handlers may be intermixed in a @try@ 290 statement but the kind of throw must be the same as the handler for it to be 291 considered as a possible match.) 292 293 If a search for a resumption handler reaches a try block it will check each 294 @catchResume@ clause, top-to-bottom. 295 At each handler if the thrown exception is or is a child type of 296 @EXCEPTION_TYPE@$_i$ then the a pointer to the exception is bound to 297 @NAME@$_i$ and then @HANDLER_BLOCK@$_i$ is executed. After the block is 298 finished control will return to the @throwResume@ statement. 394 % I wonder if there would be some good central place for this. 395 Note that termination handlers and resumption handlers may be used together 396 in a single try statement, intermixing @catch@ and @catchResume@ freely. 397 Each type of handler will only interact with exceptions from the matching 398 type of raise. 399 When a try statement is executed it simply executes the statements in the 400 @GUARDED_BLOCK@ and then finishes. 401 402 However, while the guarded statements are being executed, including any 403 invoked functions, all the handlers in the statement are now on the search 404 path. If a resumption exception is reported and not handled further up the 405 stack they will be matched against the exception. 406 407 Exception matching checks the handler in each catch clause in the order 408 they appear, top to bottom. If the representation of the thrown exception type 409 is the same or a descendant of @EXCEPTION_TYPE@$_i$ then @NAME@$_i$ 410 (if provided) is bound to a pointer to the exception and the statements in 411 @HANDLER_BLOCK@$_i$ are executed. 412 If control reaches the end of the handler, execution continues after the 413 the raise statement that raised the handled exception. 299 414 300 415 Like termination, if no resumption handler is found, the default handler … … 302 417 call sight according to \CFA's overloading rules. The default handler is 303 418 passed the exception given to the throw. When the default handler finishes 304 execution continues after the throwstatement.419 execution continues after the raise statement. 305 420 306 421 There is a global @defaultResumptionHandler@ is polymorphic over all 307 422 termination exceptions and preforms a termination throw on the exception. 308 The @defaultTerminationHandler@ for that throwis matched at the original309 throwstatement (the resumption @throwResume@) and it can be customized by423 The @defaultTerminationHandler@ for that raise is matched at the original 424 raise statement (the resumption @throwResume@) and it can be customized by 310 425 introducing a new or better match as well. 311 426 312 % \subsubsection? 313 427 \subsubsection{Resumption Marking} 314 428 A key difference between resumption and termination is that resumption does 315 429 not unwind the stack. A side effect that is that when a handler is matched … … 331 445 search and match the handler in the @catchResume@ clause. This will be 332 446 call and placed on the stack on top of the try-block. The second throw then 333 throws and will sea ch the same try block and put call another instance of the447 throws and will search the same try block and put call another instance of the 334 448 same handler leading to an infinite loop. 335 449 … … 337 451 can form with multiple handlers and different exception types. 338 452 339 To prevent all of these cases we mask sections of the stack, or equvilantly 340 the try statements on the stack, so that the resumption seach skips over 341 them and continues with the next unmasked section of the stack. 342 343 A section of the stack is marked when it is searched to see if it contains 344 a handler for an exception and unmarked when that exception has been handled 345 or the search was completed without finding a handler. 346 347 % This might need a diagram. But it is an important part of the justification 348 % of the design of the traversal order. 349 \begin{verbatim} 350 throwResume2 ----------. 351 | | 352 generated from handler | 353 | | 354 handler | 355 | | 356 throwResume1 -----. : 357 | | : 358 try | : search skip 359 | | : 360 catchResume <----' : 361 | | 362 \end{verbatim} 363 364 The rules can be remembered as thinking about what would be searched in 365 termination. So when a throw happens in a handler; a termination handler 366 skips everything from the original throw to the original catch because that 367 part of the stack has been unwound, a resumption handler skips the same 368 section of stack because it has been masked. 369 A throw in a default handler will preform the same search as the original 370 throw because; for termination nothing has been unwound, for resumption 371 the mask will be the same. 372 373 The symmetry with termination is why this pattern was picked. Other patterns, 374 such as marking just the handlers that caught, also work but lack the 375 symmetry whih means there is more to remember. 453 To prevent all of these cases we mark try statements on the stack. 454 A try statement is marked when a match check is preformed with it and an 455 exception. The statement will be unmarked when the handling of that exception 456 is completed or the search completes without finding a handler. 457 While a try statement is marked its handlers are never matched, effectify 458 skipping over it to the next try statement. 459 460 \begin{center} 461 \input{stack-marking} 462 \end{center} 463 464 These rules mirror what happens with termination. 465 When a termination throw happens in a handler the search will not look at 466 any handlers from the original throw to the original catch because that 467 part of the stack has been unwound. 468 A resumption raise in the same situation wants to search the entire stack, 469 but it will not try to match the exception with try statements in the section 470 that would have been unwound as they are marked. 471 472 The symmetry between resumption termination is why this pattern was picked. 473 Other patterns, such as marking just the handlers that caught, also work but 474 lack the symmetry means there are less rules to remember. 376 475 377 476 \section{Conditional Catch} … … 379 478 condition to further control which exceptions they handle: 380 479 \begin{cfa} 381 catch (EXCEPTION_TYPE * NAME; CONDITION)480 catch (EXCEPTION_TYPE * [NAME] ; CONDITION) 382 481 \end{cfa} 383 482 First, the same semantics is used to match the exception type. Second, if the … … 387 486 matches. Otherwise, the exception search continues as if the exception type 388 487 did not match. 389 \begin{cfa} 390 try { 391 f1 = open( ... ); 392 f2 = open( ... ); 488 489 The condition matching allows finer matching by allowing the match to check 490 more kinds of information than just the exception type. 491 \begin{cfa} 492 try { 493 handle1 = open( f1, ... ); 494 handle2 = open( f2, ... ); 495 handle3 = open( f3, ... ); 393 496 ... 394 497 } catch( IOFailure * f ; fd( f ) == f1 ) { 395 // only handle IO failure for f1 396 } 397 \end{cfa} 398 Note, catching @IOFailure@, checking for @f1@ in the handler, and reraising the 399 exception if not @f1@ is different because the reraise does not examine any of 400 remaining handlers in the current try statement. 401 402 \section{Rethrowing} 403 \colour{red}{From Andrew: I recomend we talk about why the language doesn't 404 have rethrows/reraises instead.} 405 406 \label{s:Rethrowing} 498 // Only handle IO failure for f1. 499 } catch( IOFailure * f ; fd( f ) == f3 ) { 500 // Only handle IO failure for f3. 501 } 502 // Can't handle a failure relating to f2 here. 503 \end{cfa} 504 In this example the file that experianced the IO error is used to decide 505 which handler should be run, if any at all. 506 507 \begin{comment} 508 % I know I actually haven't got rid of them yet, but I'm going to try 509 % to write it as if I had and see if that makes sense: 510 \section{Reraising} 511 \label{s:Reraising} 407 512 Within the handler block or functions called from the handler block, it is 408 513 possible to reraise the most recently caught exception with @throw@ or … … 423 528 is part of an unwound stack frame. To prevent this problem, a new default 424 529 handler is generated that does a program-level abort. 530 \end{comment} 531 532 \subsection{Comparison with Reraising} 533 A more popular way to allow handlers to match in more detail is to reraise 534 the exception after it has been caught if it could not be handled here. 535 On the surface these two features seem interchangable. 536 537 If we used @throw;@ to start a termination reraise then these two statements 538 would have the same behaviour: 539 \begin{cfa} 540 try { 541 do_work_may_throw(); 542 } catch(exception_t * exc ; can_handle(exc)) { 543 handle(exc); 544 } 545 \end{cfa} 546 547 \begin{cfa} 548 try { 549 do_work_may_throw(); 550 } catch(exception_t * exc) { 551 if (can_handle(exc)) { 552 handle(exc); 553 } else { 554 throw; 555 } 556 } 557 \end{cfa} 558 If there are further handlers after this handler only the first version will 559 check them. If multiple handlers on a single try block could handle the same 560 exception the translations get more complex but they are equivilantly 561 powerful. 562 563 Until stack unwinding comes into the picture. In termination handling, a 564 conditional catch happens before the stack is unwound, but a reraise happens 565 afterwards. Normally this might only cause you to loose some debug 566 information you could get from a stack trace (and that can be side stepped 567 entirely by collecting information during the unwind). But for \CFA there is 568 another issue, if the exception isn't handled the default handler should be 569 run at the site of the original raise. 570 571 There are two problems with this: the site of the original raise doesn't 572 exist anymore and the default handler might not exist anymore. The site will 573 always be removed as part of the unwinding, often with the entirety of the 574 function it was in. The default handler could be a stack allocated nested 575 function removed during the unwind. 576 577 This means actually trying to pretend the catch didn't happening, continuing 578 the original raise instead of starting a new one, is infeasible. 579 That is the expected behaviour for most languages and we can't replicate 580 that behaviour. 425 581 426 582 \section{Finally Clauses} 583 \label{s:FinallyClauses} 427 584 Finally clauses are used to preform unconditional clean-up when leaving a 428 scope . They are placed at the end of a try statement:585 scope and are placed at the end of a try statement after any handler clauses: 429 586 \begin{cfa} 430 587 try { … … 442 599 443 600 Execution of the finally block should always finish, meaning control runs off 444 the end of the block. This requirement ensures always continues as if the 445 finally clause is not present, \ie finally is for cleanup not changing control 446 flow. Because of this requirement, local control flow out of the finally block 601 the end of the block. This requirement ensures control always continues as if 602 the finally clause is not present, \ie finally is for cleanup not changing 603 control flow. 604 Because of this requirement, local control flow out of the finally block 447 605 is forbidden. The compiler precludes any @break@, @continue@, @fallthru@ or 448 606 @return@ that causes control to leave the finally block. Other ways to leave 449 607 the finally block, such as a long jump or termination are much harder to check, 450 and at best requiring additional run-time overhead, and so are mearly608 and at best requiring additional run-time overhead, and so are only 451 609 discouraged. 452 610 453 Not all languages with exceptionshave finally clauses. Notably \Cpp does611 Not all languages with unwinding have finally clauses. Notably \Cpp does 454 612 without it as descructors serve a similar role. Although destructors and 455 613 finally clauses can be used in many of the same areas they have their own 456 614 use cases like top-level functions and lambda functions with closures. 457 615 Destructors take a bit more work to set up but are much easier to reuse while 458 finally clauses are good for once offs and can include local information. 616 finally clauses are good for one-off uses and 617 can easily include local information. 459 618 460 619 \section{Cancellation} 620 \label{s:Cancellation} 461 621 Cancellation is a stack-level abort, which can be thought of as as an 462 uncatchable termination. It unwinds the entire ty of thecurrent stack, and if622 uncatchable termination. It unwinds the entire current stack, and if 463 623 possible forwards the cancellation exception to a different stack. 464 624 … … 466 626 There is no special statement for starting a cancellation; instead the standard 467 627 library function @cancel_stack@ is called passing an exception. Unlike a 468 throw, this exception is not used in matching only to pass information about628 raise, this exception is not used in matching only to pass information about 469 629 the cause of the cancellation. 470 (This also means matching cannot fail so there is no default handler either.)471 472 After @cancel_stack@ is called the exception is copied into the exception473 handling mechanism's memory. Then the entirety ofthe current stack is630 (This also means matching cannot fail so there is no default handler.) 631 632 After @cancel_stack@ is called the exception is copied into the EHM's memory 633 and the current stack is 474 634 unwound. After that it depends one which stack is being cancelled. 475 635 \begin{description} 476 636 \item[Main Stack:] 477 637 The main stack is the one used by the program main at the start of execution, 478 and is the only stack in a sequential program. Even in a concurrent program 479 the main stack is only dependent on the environment that started the program. 480 Hence, when the main stack is cancelled there is nowhere else in the program 481 to notify. After the stack is unwound, there is a program-level abort. 638 and is the only stack in a sequential program. 639 After the main stack is unwound there is a program-level abort. 640 641 There are two reasons for this. The first is that it obviously had to do this 642 in a sequential program as there is nothing else to notify and the simplicity 643 of keeping the same behaviour in sequential and concurrent programs is good. 644 Also, even in concurrent programs there is no stack that an innate connection 645 to, so it would have be explicitly managed. 482 646 483 647 \item[Thread Stack:] 484 A thread stack is created for a @thread@ object or object that satisfies the 485 @is_thread@ trait. A thread only has two points of communication that must 486 happen: start and join. As the thread must be running to perform a 487 cancellation, it must occur after start and before join, so join is used 488 for communication here. 489 After the stack is unwound, the thread halts and waits for 490 another thread to join with it. The joining thread checks for a cancellation, 491 and if present, resumes exception @ThreadCancelled@. 492 493 There is a subtle difference between the explicit join (@join@ function) and 494 implicit join (from a destructor call). The explicit join takes the default 495 handler (@defaultResumptionHandler@) from its calling context, which is used if 496 the exception is not caught. The implicit join does a program abort instead. 497 498 This semantics is for safety. If an unwind is triggered while another unwind 499 is underway only one of them can proceed as they both want to ``consume'' the 500 stack. Letting both try to proceed leads to very undefined behaviour. 501 Both termination and cancellation involve unwinding and, since the default 502 @defaultResumptionHandler@ preforms a termination that could more easily 503 happen in an implicate join inside a destructor. So there is an error message 504 and an abort instead. 505 \todo{Perhaps have a more general disucssion of unwind collisions before 506 this point.} 507 508 The recommended way to avoid the abort is to handle the intial resumption 509 from the implicate join. If required you may put an explicate join inside a 510 finally clause to disable the check and use the local 511 @defaultResumptionHandler@ instead. 512 513 \item[Coroutine Stack:] A coroutine stack is created for a @coroutine@ object 514 or object that satisfies the @is_coroutine@ trait. A coroutine only knows of 515 two other coroutines, its starter and its last resumer. Of the two the last 516 resumer has the tightest coupling to the coroutine it activated and the most 517 up-to-date information. 518 519 Hence, cancellation of the active coroutine is forwarded to the last resumer 520 after the stack is unwound. When the resumer restarts, it resumes exception 521 @CoroutineCancelled@, which is polymorphic over the coroutine type and has a 522 pointer to the cancelled coroutine. 523 524 The resume function also has an assertion that the @defaultResumptionHandler@ 525 for the exception. So it will use the default handler like a regular throw. 648 A thread stack is created for a \CFA @thread@ object or object that satisfies 649 the @is_thread@ trait. 650 After a thread stack is unwound there exception is stored until another 651 thread attempts to join with it. Then the exception @ThreadCancelled@, 652 which stores a reference to the thread and to the exception passed to the 653 cancellation, is reported from the join. 654 There is one difference between an explicit join (with the @join@ function) 655 and an implicit join (from a destructor call). The explicit join takes the 656 default handler (@defaultResumptionHandler@) from its calling context while 657 the implicit join provides its own which does a program abort if the 658 @ThreadCancelled@ exception cannot be handled. 659 660 Communication is done at join because a thread only has to have to points of 661 communication with other threads: start and join. 662 Since a thread must be running to perform a cancellation (and cannot be 663 cancelled from another stack), the cancellation must be after start and 664 before the join. So join is the one that we will use. 665 666 % TODO: Find somewhere to discuss unwind collisions. 667 The difference between the explicit and implicit join is for safety and 668 debugging. It helps prevent unwinding collisions by avoiding throwing from 669 a destructor and prevents cascading the error across multiple threads if 670 the user is not equipped to deal with it. 671 Also you can always add an explicit join if that is the desired behaviour. 672 673 \item[Coroutine Stack:] 674 A coroutine stack is created for a @coroutine@ object or object that 675 satisfies the @is_coroutine@ trait. 676 After a coroutine stack is unwound control returns to the resume function 677 that most recently resumed it. The resume statement reports a 678 @CoroutineCancelled@ exception, which contains a references to the cancelled 679 coroutine and the exception used to cancel it. 680 The resume function also takes the @defaultResumptionHandler@ from the 681 caller's context and passes it to the internal report. 682 683 A coroutine knows of two other coroutines, its starter and its last resumer. 684 The starter has a much more distant connection while the last resumer just 685 (in terms of coroutine state) called resume on this coroutine, so the message 686 is passed to the latter. 526 687 \end{description}
Note:
See TracChangeset
for help on using the changeset viewer.