1 | \chapter{Introduction} |
---|
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 (pronounced sea-for-all and may be written Cforall or CFA). |
---|
7 | \CFA is a new programming language that extends C, that maintains |
---|
8 | backwards-compatibility while introducing modern programming features. |
---|
9 | Adding exception handling to \CFA gives it new ways to handle errors and |
---|
10 | make other large control-flow jumps. |
---|
11 | |
---|
12 | % Now take a step back and explain what exceptions are generally. |
---|
13 | Exception handling provides dynamic inter-function control flow. |
---|
14 | There are two forms of exception handling covered in this thesis: |
---|
15 | termination, which acts as a multi-level return, |
---|
16 | and resumption, which is a dynamic function call. |
---|
17 | Termination handling is much more common, |
---|
18 | to the extent that it is often seen |
---|
19 | This seperation is uncommon because termination exception handling is so |
---|
20 | much more common that it is often assumed. |
---|
21 | % WHY: Mention other forms of continuation and \cite{CommonLisp} here? |
---|
22 | A language's EHM is the combination of language syntax and run-time |
---|
23 | components that are used to construct, raise and handle exceptions, |
---|
24 | including all control flow. |
---|
25 | |
---|
26 | Termination exception handling allows control to return to any previous |
---|
27 | function on the stack directly, skipping any functions between it and the |
---|
28 | current function. |
---|
29 | \begin{center} |
---|
30 | \input{callreturn} |
---|
31 | \end{center} |
---|
32 | |
---|
33 | Resumption exception handling seaches the stack for a handler and then calls |
---|
34 | it without adding or removing any other stack frames. |
---|
35 | \todo{Add a diagram showing control flow for resumption.} |
---|
36 | |
---|
37 | Although a powerful feature, exception handling tends to be complex to set up |
---|
38 | and expensive to use |
---|
39 | so they are often limited to unusual or ``exceptional" cases. |
---|
40 | The classic example of this is error handling, exceptions can be used to |
---|
41 | remove error handling logic from the main execution path and while paying |
---|
42 | most of the cost only when the error actually occurs. |
---|
43 | |
---|
44 | \section{Thesis Overview} |
---|
45 | This work describes the design and implementation of the \CFA EHM. |
---|
46 | The \CFA EHM implements all of the common exception features (or an |
---|
47 | equivalent) found in most other EHMs and adds some features of its own. |
---|
48 | The design of all the features had to be adapted to \CFA's feature set as |
---|
49 | some of the underlying tools used to implement and express exception handling |
---|
50 | in other languages are absent in \CFA. |
---|
51 | Still the resulting syntax resembles that of other languages: |
---|
52 | \begin{cfa} |
---|
53 | try { |
---|
54 | ... |
---|
55 | T * object = malloc(request_size); |
---|
56 | if (!object) { |
---|
57 | throw OutOfMemory{fixed_allocation, request_size}; |
---|
58 | } |
---|
59 | ... |
---|
60 | } catch (OutOfMemory * error) { |
---|
61 | ... |
---|
62 | } |
---|
63 | \end{cfa} |
---|
64 | |
---|
65 | % A note that yes, that was a very fast overview. |
---|
66 | The design and implementation of all of \CFA's EHM's features are |
---|
67 | described in detail throughout this thesis, whether they are a common feature |
---|
68 | or one unique to \CFA. |
---|
69 | |
---|
70 | % The current state of the project and what it contributes. |
---|
71 | All of these features have been implemented in \CFA, along with |
---|
72 | a suite of test cases as part of this project. |
---|
73 | The implementation techniques are generally applicable in other programming |
---|
74 | languages and much of the design is as well. |
---|
75 | Some parts of the EHM use other features unique to \CFA and these would be |
---|
76 | harder to replicate in other programming languages. |
---|
77 | |
---|
78 | % Talk about other programming languages. |
---|
79 | Some existing programming languages that include EHMs/exception handling |
---|
80 | include C++, Java and Python. All three examples focus on termination |
---|
81 | exceptions which unwind the stack as part of the |
---|
82 | Exceptions also can replace return codes and return unions. |
---|
83 | |
---|
84 | The contributions of this work are: |
---|
85 | \begin{enumerate} |
---|
86 | \item Designing \CFA's exception handling mechanism, adapting designs from |
---|
87 | other programming languages and the creation of new features. |
---|
88 | \item Implementing stack unwinding and the EHM in \CFA, including updating |
---|
89 | the compiler and the run-time environment. |
---|
90 | \item Designed and implemented a prototype virtual system. |
---|
91 | % I think the virtual system and per-call site default handlers are the only |
---|
92 | % "new" features, everything else is a matter of implementation. |
---|
93 | \end{enumerate} |
---|
94 | |
---|
95 | \todo{I can't figure out a good lead-in to the roadmap.} |
---|
96 | The next section covers the existing state of exceptions. |
---|
97 | The existing state of \CFA is also covered in \autoref{c:existing}. |
---|
98 | The new features are introduced in \autoref{c:features}, |
---|
99 | which explains their usage and design. |
---|
100 | That is followed by the implementation of those features in |
---|
101 | \autoref{c:implement}. |
---|
102 | The performance results are examined in \autoref{c:performance}. |
---|
103 | Possibilities to extend this project are discussed in \autoref{c:future}. |
---|
104 | |
---|
105 | \section{Background} |
---|
106 | \label{s:background} |
---|
107 | |
---|
108 | Exception handling is not a new concept, |
---|
109 | with papers on the subject dating back 70s. |
---|
110 | |
---|
111 | Their were popularised by \Cpp, |
---|
112 | which added them in its first major wave of non-object-orientated features |
---|
113 | in 1990. |
---|
114 | % https://en.cppreference.com/w/cpp/language/history |
---|
115 | |
---|
116 | Java was the next popular language to use exceptions. It is also the most |
---|
117 | popular language with checked exceptions. |
---|
118 | Checked exceptions are part of the function interface they are raised from. |
---|
119 | This includes functions they propogate through, until a handler for that |
---|
120 | type of exception is found. |
---|
121 | This makes exception information explicit, which can improve clarity and |
---|
122 | safety, but can slow down programming. |
---|
123 | Some of these, such as dealing with high-order methods or an overly specified |
---|
124 | throws clause, are technical. However some of the issues are much more |
---|
125 | human, in that writing/updating all the exception signatures can be enough |
---|
126 | of a burden people will hack the system to avoid them. |
---|
127 | Including the ``catch-and-ignore" pattern where a catch block is used without |
---|
128 | anything to repair or recover from the exception. |
---|
129 | |
---|
130 | %\subsection |
---|
131 | Resumption exceptions have been much less popular. |
---|
132 | Although resumption has a history as old as termination's, very few |
---|
133 | programming languages have implement them. |
---|
134 | % http://bitsavers.informatik.uni-stuttgart.de/pdf/xerox/parc/techReports/ |
---|
135 | % CSL-79-3_Mesa_Language_Manual_Version_5.0.pdf |
---|
136 | Mesa is one programming languages that did and experiance with that |
---|
137 | languages is quoted as being one of the reasons resumptions were not |
---|
138 | included in the \Cpp standard. |
---|
139 | % https://en.wikipedia.org/wiki/Exception_handling |
---|
140 | \todo{A comment about why we did include them when they are so unpopular |
---|
141 | might be approprate.} |
---|
142 | |
---|
143 | %\subsection |
---|
144 | Functional languages, tend to use solutions like the return union, but some |
---|
145 | exception-like constructs still appear. |
---|
146 | |
---|
147 | For instance Haskell's built in error mechanism can make the result of any |
---|
148 | expression, including function calls. Any expression that examines an |
---|
149 | error value will in-turn produce an error. This continues until the main |
---|
150 | function produces an error or until it is handled by one of the catch |
---|
151 | functions. |
---|
152 | |
---|
153 | %\subsection |
---|
154 | More recently exceptions seem to be vanishing from newer programming |
---|
155 | languages. |
---|
156 | Rust and Go reduce this feature to panics. |
---|
157 | Panicing is somewhere between a termination exception and a program abort. |
---|
158 | Notably in Rust a panic can trigger either, a panic may unwind the stack or |
---|
159 | simply kill the process. |
---|
160 | % https://doc.rust-lang.org/std/panic/fn.catch_unwind.html |
---|
161 | Go's panic is much more similar to a termination exception but there is |
---|
162 | only a catch-all function with \code{Go}{recover()}. |
---|
163 | So exceptions still are appearing, just in reduced forms. |
---|
164 | |
---|
165 | %\subsection |
---|
166 | Exception handling's most common use cases are in error handling. |
---|
167 | Here are some other ways to handle errors and comparisons with exceptions. |
---|
168 | \begin{itemize} |
---|
169 | \item\emph{Error Codes}: |
---|
170 | This pattern uses an enumeration (or just a set of fixed values) to indicate |
---|
171 | that an error has occured and which error it was. |
---|
172 | |
---|
173 | There are some issues if a function wants to return an error code and another |
---|
174 | value. The main issue is that it can be easy to forget checking the error |
---|
175 | code, which can lead to an error being quitely and implicitly ignored. |
---|
176 | Some new languages have tools that raise warnings if the return value is |
---|
177 | discarded to avoid this. |
---|
178 | It also puts more code on the main execution path. |
---|
179 | \item\emph{Special Return with Global Store}: |
---|
180 | A function that encounters an error returns some value indicating that it |
---|
181 | encountered a value but store which error occured in a fixed global location. |
---|
182 | |
---|
183 | Perhaps the C standard @errno@ is the most famous example of this, |
---|
184 | where some standard library functions will return some non-value (often a |
---|
185 | NULL pointer) and set @errno@. |
---|
186 | |
---|
187 | This avoids the multiple results issue encountered with straight error codes |
---|
188 | but otherwise many of the same advantages and disadvantages. |
---|
189 | It does however introduce one other major disadvantage: |
---|
190 | Everything that uses that global location must agree on all possible errors. |
---|
191 | \item\emph{Return Union}: |
---|
192 | Replaces error codes with a tagged union. |
---|
193 | Success is one tag and the errors are another. |
---|
194 | It is also possible to make each possible error its own tag and carry its own |
---|
195 | additional information, but the two branch format is easy to make generic |
---|
196 | so that one type can be used everywhere in error handling code. |
---|
197 | |
---|
198 | This pattern is very popular in functional or semi-functional language, |
---|
199 | anything with primitive support for tagged unions (or algebraic data types). |
---|
200 | % We need listing Rust/rust to format code snipits from it. |
---|
201 | % Rust's \code{rust}{Result<T, E>} |
---|
202 | |
---|
203 | The main disadvantage is again it puts code on the main execution path. |
---|
204 | This is also the first technique that allows for more information about an |
---|
205 | error, other than one of a fix-set of ids, to be sent. |
---|
206 | They can be missed but some languages can force that they are checked. |
---|
207 | It is also implicitly forced in any languages with checked union access. |
---|
208 | \item\emph{Handler Functions}: |
---|
209 | On error the function that produced the error calls another function to |
---|
210 | handle it. |
---|
211 | The handler function can be provided locally (passed in as an argument, |
---|
212 | either directly as as a field of a structure/object) or globally (a global |
---|
213 | variable). |
---|
214 | |
---|
215 | C++ uses this as its fallback system if exception handling fails. |
---|
216 | \snake{std::terminate_handler} and for a time \snake{std::unexpected_handler} |
---|
217 | |
---|
218 | Handler functions work a lot like resumption exceptions. |
---|
219 | The difference is they are more expencive to set up but cheaper to use, and |
---|
220 | so are more suited to more fequent errors. |
---|
221 | The exception being global handlers if they are rarely change as the time |
---|
222 | in both cases strinks towards zero. |
---|
223 | \end{itemize} |
---|
224 | |
---|
225 | %\subsection |
---|
226 | Because of their cost exceptions are rarely used for hot paths of execution. |
---|
227 | There is an element of self-fulfilling prophocy here as implementation |
---|
228 | techniques have been designed to make exceptions cheap to set-up at the cost |
---|
229 | of making them expencive to use. |
---|
230 | Still, use of exceptions for other tasks is more common in higher-level |
---|
231 | scripting languages. |
---|
232 | An iconic example is Python's StopIteration exception which is thrown by |
---|
233 | an iterator to indicate that it is exausted. Combined with Python's heavy |
---|
234 | use of the iterator based for-loop. |
---|
235 | % https://docs.python.org/3/library/exceptions.html#StopIteration |
---|