Changeset eaeca5f for doc/theses/andrew_beach_MMath/intro.tex
- Timestamp:
- Aug 29, 2021, 11:46:13 AM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, pthread-emulation, qualifiedEnum
- Children:
- 75f8e04
- Parents:
- 1d402be (diff), cfbab07 (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
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/intro.tex
r1d402be reaeca5f 11 11 12 12 % Now take a step back and explain what exceptions are generally. 13 Exception handling provides dynamic inter-function control flow. 13 14 A language's EHM is a combination of language syntax and run-time 14 components that are used to construct, raise, and handle exceptions, 15 including all control flow. 16 Exceptions are an active mechanism for replacing passive error/return codes and return unions (Go and Rust). 17 Exception handling provides dynamic inter-function control flow. 15 components that construct, raise, propagate and handle exceptions, 16 to provide all of that control flow. 18 17 There are two forms of exception handling covered in this thesis: 19 18 termination, which acts as a multi-level return, 20 19 and resumption, which is a dynamic function call. 21 % PAB: Maybe this sentence was suppose to be deleted? 22 Termination handling is much more common, 23 to the extent that it is often seen as the only form of handling. 24 % PAB: I like this sentence better than the next sentence. 25 % This separation is uncommon because termination exception handling is so 26 % much more common that it is often assumed. 27 % WHY: Mention other forms of continuation and \cite{CommonLisp} here? 28 29 Exception handling relies on the concept of nested functions to create handlers that deal with exceptions. 20 % About other works: 21 Often, when this separation is not made, termination exceptions are assumed 22 as they are more common and may be the only form of handling provided in 23 a language. 24 25 All types of exception handling link a raise with a handler. 26 Both operations are usually language primitives, although raises can be 27 treated as a primitive function that takes an exception argument. 28 Handlers are more complex as they are added to and removed from the stack 29 during execution, must specify what they can handle and give the code to 30 handle the exception. 31 32 Exceptions work with different execution models but for the descriptions 33 that follow a simple call stack, with functions added and removed in a 34 first-in-last-out order, is assumed. 35 36 Termination exception handling searches the stack for the handler, then 37 unwinds the stack to where the handler was found before calling it. 38 The handler is run inside the function that defined it and when it finishes 39 it returns control to that function. 30 40 \begin{center} 31 \begin{tabular}[t]{ll} 32 \begin{lstlisting}[aboveskip=0pt,belowskip=0pt,language=CFA,{moredelim=**[is][\color{red}]{@}{@}}] 33 void f( void (*hp)() ) { 34 hp(); 35 } 36 void g( void (*hp)() ) { 37 f( hp ); 38 } 39 void h( int @i@, void (*hp)() ) { 40 void @handler@() { // nested 41 printf( "%d\n", @i@ ); 42 } 43 if ( i == 1 ) hp = handler; 44 if ( i > 0 ) h( i - 1, hp ); 45 else g( hp ); 46 } 47 h( 2, 0 ); 48 \end{lstlisting} 49 & 50 \raisebox{-0.5\totalheight}{\input{handler}} 51 \end{tabular} 41 \input{callreturn} 52 42 \end{center} 53 The nested function @handler@ in the second stack frame is explicitly passed to function @f@. 54 When this handler is called in @f@, it uses the parameter @i@ in the second stack frame, which is accessible by an implicit lexical-link pointer. 55 Setting @hp@ in @h@ at different points in the recursion, results in invoking a different handler. 56 Exception handling extends this idea by eliminating explicit handler passing, and instead, performing a stack search for a handler that matches some criteria (conditional dynamic call), and calls the handler at the top of the stack. 57 It is the runtime search $O(N)$ that differentiates an EHM call (raise) from normal dynamic call $O(1)$ via a function or virtual-member pointer. 58 59 Termination exception handling searches the stack for a handler, unwinds the stack to the frame containing the matching handler, and calling the handler at the top of the stack. 60 \begin{center} 61 \input{termination} 62 \end{center} 63 Note, since the handler can reference variables in @h@, @h@ must remain on the stack for the handler call. 64 After the handler returns, control continues after the lexical location of the handler in @h@ (static return)~\cite[p.~108]{Tennent77}. 65 Unwinding allows recover to any previous 66 function on the stack, skipping any functions between it and the 67 function containing the matching handler. 68 69 Resumption exception handling searches the stack for a handler, does \emph{not} unwind the stack to the frame containing the matching handler, and calls the handler at the top of the stack. 43 44 Resumption exception handling searches the stack for a handler and then calls 45 it without removing any other stack frames. 46 The handler is run on top of the existing stack, often as a new function or 47 closure capturing the context in which the handler was defined. 48 After the handler has finished running it returns control to the function 49 that preformed the raise, usually starting after the raise. 70 50 \begin{center} 71 51 \input{resumption} 72 52 \end{center} 73 After the handler returns, control continues after the resume in @f@ (dynamic return).74 Not unwinding allows fix up of the problem in @f@ by any previous function on the stack, without disrupting the current set of stack frames.75 53 76 54 Although a powerful feature, exception handling tends to be complex to set up 77 55 and expensive to use 78 56 so it is often limited to unusual or ``exceptional" cases. 79 The classic example is error handling, where exceptions are used to80 remove error handling logic from the main execution path, while paying57 The classic example is error handling, exceptions can be used to 58 remove error handling logic from the main execution path, and pay 81 59 most of the cost only when the error actually occurs. 82 60 … … 88 66 some of the underlying tools used to implement and express exception handling 89 67 in other languages are absent in \CFA. 90 Still the resulting basicsyntax resembles that of other languages:91 \begin{ lstlisting}[language=CFA,{moredelim=**[is][\color{red}]{@}{@}}]92 @try@{68 Still the resulting syntax resembles that of other languages: 69 \begin{cfa} 70 try { 93 71 ... 94 72 T * object = malloc(request_size); 95 73 if (!object) { 96 @throw@OutOfMemory{fixed_allocation, request_size};74 throw OutOfMemory{fixed_allocation, request_size}; 97 75 } 98 76 ... 99 } @catch@(OutOfMemory * error) {77 } catch (OutOfMemory * error) { 100 78 ... 101 79 } 102 \end{ lstlisting}80 \end{cfa} 103 81 % A note that yes, that was a very fast overview. 104 82 The design and implementation of all of \CFA's EHM's features are … … 107 85 108 86 % The current state of the project and what it contributes. 109 The majority of the \CFA EHM is implemented in \CFA, except for a small amount of assembler code. 110 In addition, 111 a suite of tests and performance benchmarks were created as part of this project. 112 The \CFA implementation techniques are generally applicable in other programming 87 All of these features have been implemented in \CFA, 88 covering both changes to the compiler and the run-time. 89 In addition, a suite of test cases and performance benchmarks were created 90 along side the implementation. 91 The implementation techniques are generally applicable in other programming 113 92 languages and much of the design is as well. 114 Some parts of the EHM use features unique to \CFA, and hence, 115 are harder to replicate in other programming languages. 116 % Talk about other programming languages. 117 Three well known programming languages with EHMs, %/exception handling 118 C++, Java and Python are examined in the performance work. However, these languages focus on termination 119 exceptions, so there is no comparison with resumption. 93 Some parts of the EHM use other features unique to \CFA and would be 94 harder to replicate in other programming languages. 120 95 121 96 The contributions of this work are: 122 97 \begin{enumerate} 123 98 \item Designing \CFA's exception handling mechanism, adapting designs from 124 other programming languages ,and creating new features.125 \item Implementing stack unwinding forthe \CFA EHM, including updating126 the \CFA compiler and run-time environment to generate and execute the EHM code.127 \item Design ing and implementinga prototype virtual system.99 other programming languages and creating new features. 100 \item Implementing stack unwinding and the \CFA EHM, including updating 101 the \CFA compiler and the run-time environment. 102 \item Designed and implemented a prototype virtual system. 128 103 % I think the virtual system and per-call site default handlers are the only 129 104 % "new" features, everything else is a matter of implementation. 130 \item Creating tests and performance benchmarks to compare with EHM's in other languages. 105 \item Creating tests to check the behaviour of the EHM. 106 \item Creating benchmarks to check the performances of the EHM, 107 as compared to other languages. 131 108 \end{enumerate} 132 109 133 %\todo{I can't figure out a good lead-in to the roadmap.} 134 The thesis is organization as follows.135 The next section and parts of \autoref{c:existing} cover existing EHMs.136 New \CFAEHM features are introduced in \autoref{c:features},110 The rest of this thesis is organized as follows. 111 The current state of exceptions is covered in \autoref{s:background}. 112 The existing state of \CFA is also covered in \autoref{c:existing}. 113 New EHM features are introduced in \autoref{c:features}, 137 114 covering their usage and design. 138 115 That is followed by the implementation of these features in 139 116 \autoref{c:implement}. 140 Performance results are presented in \autoref{c:performance}. 141 Summing up and possibilities for extending this project are discussed in \autoref{c:future}. 117 Performance results are examined in \autoref{c:performance}. 118 Possibilities to extend this project are discussed in \autoref{c:future}. 119 Finally, the project is summarized in \autoref{c:conclusion}. 142 120 143 121 \section{Background} 144 122 \label{s:background} 145 123 146 Exception handling is a well examined areain programming languages,147 with papers on the subject dating back the 70s~\cite{Goodenough75}.124 Exception handling has been examined before in programming languages, 125 with papers on the subject dating back 70s.\cite{Goodenough75} 148 126 Early exceptions were often treated as signals, which carried no information 149 except their identity. Ada ~\cite{Ada} still uses this system.127 except their identity. Ada still uses this system.\todo{cite Ada} 150 128 151 129 The modern flag-ship for termination exceptions is \Cpp, 152 130 which added them in its first major wave of non-object-orientated features 153 131 in 1990. 154 % https://en.cppreference.com/w/cpp/language/history 155 While many EHMs have special exception types, 156 \Cpp has the ability to use any type as an exception. 157 However, this generality is not particularly useful, and has been pushed aside for classes, with a convention of inheriting from 132 \todo{cite https://en.cppreference.com/w/cpp/language/history} 133 Many EHMs have special exception types, 134 however \Cpp has the ability to use any type as an exception. 135 These were found to be not very useful and have been pushed aside for classes 136 inheriting from 158 137 \code{C++}{std::exception}. 159 While \Cpp has a special catch-all syntax @catch(...)@, there is no way to discriminate its exception type, so nothing can 160 be done with the caught value because nothing is known about it. 161 Instead the base exception-type \code{C++}{std::exception} is defined with common functionality (such as 162 the ability to print a message when the exception is raised but not caught) and all 138 Although there is a special catch-all syntax (@catch(...)@) there are no 139 operations that can be performed on the caught value, not even type inspection. 140 Instead the base exception-type \code{C++}{std::exception} defines common 141 functionality (such as 142 the ability to describe the reason the exception was raised) and all 163 143 exceptions have this functionality. 164 Having a root exception-type seems to be the standard now, as the guaranteed functionality is worth 165 any lost in flexibility from limiting exceptions types to classes. 166 167 Java~\cite{Java} was the next popular language to use exceptions. 168 Its exception system largely reflects that of \Cpp, except it requires 169 exceptions to be a subtype of \code{Java}{java.lang.Throwable} 144 That trade-off, restricting usable types to gain guaranteed functionality, 145 is almost universal now, as without some common functionality it is almost 146 impossible to actually handle any errors. 147 148 Java was the next popular language to use exceptions. \todo{cite Java} 149 Its exception system largely reflects that of \Cpp, except that requires 150 you throw a child type of \code{Java}{java.lang.Throwable} 170 151 and it uses checked exceptions. 171 Checked exceptions are part of a function's interface defining all exceptions it or its called functions raise. 172 Using this information, it is possible to statically verify if a handler exists for all raised exception, \ie no uncaught exceptions. 173 Making exception information explicit, improves clarity and 174 safety, but can slow down programming. 175 For example, programming complexity increases when dealing with high-order methods or an overly specified 176 throws clause. However some of the issues are more 177 programming annoyances, such as writing/updating many exception signatures after adding or remove calls. 178 Java programmers have developed multiple programming ``hacks'' to circumvent checked exceptions negating the robustness it is suppose to provide. 179 For example, the ``catch-and-ignore" pattern, where the handler is empty because the exception does not appear relevant to the programmer versus 180 repairing or recovering from the exception. 152 Checked exceptions are part of a function's interface, 153 the exception signature of the function. 154 Every function that could be raised from a function, either directly or 155 because it is not handled from a called function, is given. 156 Using this information, it is possible to statically verify if any given 157 exception is handled and guarantee that no exception will go unhandled. 158 Making exception information explicit improves clarity and safety, 159 but can slow down or restrict programming. 160 For example, programming high-order functions becomes much more complex 161 if the argument functions could raise exceptions. 162 However, as odd it may seem, the worst problems are rooted in the simple 163 inconvenience of writing and updating exception signatures. 164 This has caused Java programmers to develop multiple programming ``hacks'' 165 to circumvent checked exceptions, negating their advantages. 166 One particularly problematic example is the ``catch-and-ignore'' pattern, 167 where an empty handler is used to handle an exception without doing any 168 recovery or repair. In theory that could be good enough to properly handle 169 the exception, but more often is used to ignore an exception that the 170 programmer does not feel is worth the effort of handling it, for instance if 171 they do not believe it will ever be raised. 172 If they are incorrect the exception will be silenced, while in a similar 173 situation with unchecked exceptions the exception would at least activate 174 the language's unhandled exception code (usually program abort with an 175 error message). 181 176 182 177 %\subsection 183 178 Resumption exceptions are less popular, 184 although resumption is as old as termination; 185 hence, few 179 although resumption is as old as termination; hence, few 186 180 programming languages have implemented them. 187 181 % http://bitsavers.informatik.uni-stuttgart.de/pdf/xerox/parc/techReports/ 188 182 % CSL-79-3_Mesa_Language_Manual_Version_5.0.pdf 189 Mesa ~\cite{Mesa} is one programming languages that did.Experience with Mesa190 is quoted as being one of the reasons resumptions are not183 Mesa is one programming language that did.\todo{cite Mesa} Experience with Mesa 184 is quoted as being one of the reasons resumptions were not 191 185 included in the \Cpp standard. 192 186 % https://en.wikipedia.org/wiki/Exception_handling 193 As a result, resumption has ignored in main-stream programming languages. 194 However, ``what goes around comes around'' and resumption is being revisited now (like user-level threading). 195 While rejecting resumption might have been the right decision in the past, there are decades 196 of developments in computer science that have changed the situation. 197 Some of these developments, such as functional programming's resumption 198 equivalent, algebraic effects\cite{Zhang19}, are enjoying significant success. 199 A complete reexamination of resumptions is beyond this thesis, but their re-emergence is 200 enough to try them in \CFA. 187 Since then resumptions have been ignored in main-stream programming languages. 188 However, resumption is being revisited in the context of decades of other 189 developments in programming languages. 190 While rejecting resumption may have been the right decision in the past, 191 the situation has changed since then. 192 Some developments, such as the function programming equivalent to resumptions, 193 algebraic effects\cite{Zhang19}, are enjoying success. 194 A complete reexamination of resumptions is beyond this thesis, 195 but there reemergence is enough to try them in \CFA. 201 196 % Especially considering how much easier they are to implement than 202 % termination exceptions .203 204 %\subsection 205 Functional languages tend to use other solutions for their primary EHM,206 but exception-like constructs still appear.207 Termination appears in error construct, which marks the result of an208 expression as an error; the reafter, the result of any expression that tries to use it is also an209 error, and so on until an appropriate handler is reached.197 % termination exceptions and how much Peter likes them. 198 199 %\subsection 200 Functional languages tend to use other solutions for their primary error 201 handling mechanism, but exception-like constructs still appear. 202 Termination appears in the error construct, which marks the result of an 203 expression as an error; then the result of any expression that tries to use 204 it also results in an error, and so on until an appropriate handler is reached. 210 205 Resumption appears in algebraic effects, where a function dispatches its 211 206 side-effects to its caller for handling. 212 207 213 208 %\subsection 214 Some programming languages have moved to a restricted kind of EHM 215 called``panic".216 In Rust ~\cite{Rust}, a panic is just a program level abort that may be implemented by217 unwinding the stack like in termination exception handling. 209 More recently exceptions seem to be vanishing from newer programming 210 languages, replaced by ``panic". 211 In Rust, a panic is just a program level abort that may be implemented by 212 unwinding the stack like in termination exception handling.\todo{cite Rust} 218 213 % https://doc.rust-lang.org/std/panic/fn.catch_unwind.html 219 In Go~\cite{Go}, a panicis very similar to a termination, except it only supports214 Go's panic through is very similar to a termination, except it only supports 220 215 a catch-all by calling \code{Go}{recover()}, simplifying the interface at 221 the cost of flexibility. 216 the cost of flexibility.\todo{cite Go} 222 217 223 218 %\subsection 224 219 While exception handling's most common use cases are in error handling, 225 here are other ways to handle errors with comparisons toexceptions.220 here are some other ways to handle errors with comparisons with exceptions. 226 221 \begin{itemize} 227 222 \item\emph{Error Codes}: 228 This pattern has a function return an enumeration (or just a set of fixed values) to indicate 229 if an error occurred and possibly which error it was. 230 231 Error codes mix exceptional and normal values, artificially enlarging the type and/or value range. 232 Some languages address this issue by returning multiple values or a tuple, separating the error code from the function result. 233 However, the main issue with error codes is forgetting to checking them, 223 This pattern has a function return an enumeration (or just a set of fixed 224 values) to indicate if an error has occurred and possibly which error it was. 225 226 Error codes mix exceptional/error and normal values, enlarging the range of 227 possible return values. This can be addressed with multiple return values 228 (or a tuple) or a tagged union. 229 However, the main issue with error codes is forgetting to check them, 234 230 which leads to an error being quietly and implicitly ignored. 235 Some new languages have tools that issue warnings, if the error code is 236 discarded to avoid this problem. 237 Checking error codes also results in bloating the main execution path, especially if an error is not dealt with locally and has to be cascaded down the call stack to a higher-level function.. 231 Some new languages and tools will try to issue warnings when an error code 232 is discarded to avoid this problem. 233 Checking error codes also bloats the main execution path, 234 especially if the error is not handled immediately hand has to be passed 235 through multiple functions before it is addressed. 238 236 239 237 \item\emph{Special Return with Global Store}: 240 Some functions only return a boolean indicating success or failure 241 and store the exact reason for the error in a fixed global location. 242 For example, many C routines return non-zero or -1, indicating success or failure, 243 and write error details into the C standard variable @errno@. 244 245 This approach avoids the multiple results issue encountered with straight error codes 246 but otherwise has many (if not more) of the disadvantages. 247 For example, everything that uses the global location must agree on all possible errors and global variable are unsafe with concurrency. 238 Similar to the error codes pattern but the function itself only returns 239 that there was an error 240 and store the reason for the error in a fixed global location. 241 For example many routines in the C standard library will only return some 242 error value (such as -1 or a null pointer) and the error code is written into 243 the standard variable @errno@. 244 245 This approach avoids the multiple results issue encountered with straight 246 error codes but otherwise has the same disadvantages and more. 247 Every function that reads or writes to the global store must agree on all 248 possible errors and managing it becomes more complex with concurrency. 248 249 249 250 \item\emph{Return Union}: … … 254 255 so that one type can be used everywhere in error handling code. 255 256 256 This pattern is very popular in functional or any semi-functional language with257 primitive support for tagged unions (or algebraic data types).258 % We need listing Rust/rust to format code snip its from it.257 This pattern is very popular in any functional or semi-functional language 258 with primitive support for tagged unions (or algebraic data types). 259 % We need listing Rust/rust to format code snippets from it. 259 260 % Rust's \code{rust}{Result<T, E>} 260 The main advantage is providing for more information about an261 error , other than one of a fix-set of ids.262 While some languages use checked union access to force error-code checking, 263 it is still possible to bypass the checking. 264 The main disadvantage is again significant error code on the main execution path and cascading through called functions.261 The main advantage is that an arbitrary object can be used to represent an 262 error so it can include a lot more information than a simple error code. 263 The disadvantages include that the it does have to be checked along the main 264 execution and if there aren't primitive tagged unions proper usage can be 265 hard to enforce. 265 266 266 267 \item\emph{Handler Functions}: 267 This pattern implicitly associates functions with errors.268 On error, the function that produced the error implicitlycalls another function to268 This pattern associates errors with functions. 269 On error, the function that produced the error calls another function to 269 270 handle it. 270 271 The handler function can be provided locally (passed in as an argument, 271 272 either directly as as a field of a structure/object) or globally (a global 272 273 variable). 273 C++ uses this approach as its fallback system if exception handling fails, \eg 274 \snake{std::terminate_handler} and for a time \snake{std::unexpected_handler} 275 276 Handler functions work a lot like resumption exceptions, without the dynamic handler search. 277 Therefore, setting setting up the handler can be more complex/expensive, especially if the handle must be passed through multiple function calls, but cheaper to call $O(1)$, and hence, 278 are more suited to frequent exceptional situations. 279 % The exception being global handlers if they are rarely change as the time 280 % in both cases shrinks towards zero. 274 C++ uses this approach as its fallback system if exception handling fails, 275 such as \snake{std::terminate_handler} and, for a time, 276 \snake{std::unexpected_handler}. 277 278 Handler functions work a lot like resumption exceptions, 279 but without the dynamic search for a handler. 280 Since setting up the handler can be more complex/expensive, 281 especially when the handler has to be passed through multiple layers of 282 function calls, but cheaper (constant time) to call, 283 they are more suited to more frequent (less exceptional) situations. 281 284 \end{itemize} 282 285 283 286 %\subsection 284 287 Because of their cost, exceptions are rarely used for hot paths of execution. 285 Therefore, there is an element of self-fulfilling prophecy for implementation 286 techniques to make exceptions cheap to set-up at the cost 287 of expensive usage. 288 This cost differential is less important in higher-level scripting languages, where use of exceptions for other tasks is more common. 289 An iconic example is Python's @StopIteration@ exception that is thrown by 290 an iterator to indicate that it is exhausted, especially when combined with Python's heavy 291 use of the iterator-based for-loop. 288 Hence, there is an element of self-fulfilling prophecy as implementation 289 techniques have been focused on making them cheap to set-up, 290 happily making them expensive to use in exchange. 291 This difference is less important in higher-level scripting languages, 292 where using exception for other tasks is more common. 293 An iconic example is Python's \code{Python}{StopIteration} exception that 294 is thrown by an iterator to indicate that it is exhausted. 295 When paired with Python's iterator-based for-loop this will be thrown every 296 time the end of the loop is reached. 297 \todo{Cite Python StopIteration and for-each loop.} 292 298 % https://docs.python.org/3/library/exceptions.html#StopIteration
Note: See TracChangeset
for help on using the changeset viewer.