1 | \chapter{Future Work} |
---|
2 | \label{c:future} |
---|
3 | |
---|
4 | The following discussion covers both possible interesting research |
---|
5 | that could follow from this work as well as simple implementation |
---|
6 | improvements. |
---|
7 | |
---|
8 | \section{Language Improvements} |
---|
9 | |
---|
10 | \CFA is a developing programming language. As such, there are partially or |
---|
11 | unimplemented features (including several broken components) |
---|
12 | that I had to work around while building the EHM largely in |
---|
13 | the \CFA language (some C components). Below are a few of these issues |
---|
14 | and how implementing/fixing them would affect the EHM. |
---|
15 | In addition, there are some simple improvements that had no interesting |
---|
16 | research attached to them but would make using the language easier. |
---|
17 | \begin{itemize} |
---|
18 | \item |
---|
19 | Due to a type-system problem, the catch clause cannot bind the exception to a |
---|
20 | reference instead of a pointer. Since \CFA has a very general reference |
---|
21 | capability, programmers will want to use it. Once fixed, this capability should |
---|
22 | result in little or no change in the exception system but simplify usage. |
---|
23 | \item |
---|
24 | The @copy@ function in the exception virtual table is an adapter to address |
---|
25 | some limitations in the \CFA copy constructor. If the copy constructor is |
---|
26 | improved it can be used directly without the adapter. |
---|
27 | \item |
---|
28 | Termination handlers cannot use local control-flow transfers, \eg by @break@, |
---|
29 | @return@, \etc. The reason is that current code generation hoists a handler |
---|
30 | into a nested function for convenience (versus assembly-code generation at the |
---|
31 | try statement). Hence, when the handler runs, it can still access local |
---|
32 | variables in the lexical scope of the try statement. Still, it does mean |
---|
33 | that seemingly local control flow is not in fact local and crosses a function |
---|
34 | boundary. |
---|
35 | Making the termination handler's code within the surrounding |
---|
36 | function would remove this limitation. |
---|
37 | % Try blocks are much more difficult to do practically (requires our own |
---|
38 | % assembly) and resumption handlers have some theoretical complexity. |
---|
39 | \item |
---|
40 | There is no detection of colliding unwinds. It is possible for cleanup code |
---|
41 | run during an unwind to trigger another unwind that escapes the cleanup code |
---|
42 | itself, such as a termination exception caught further down the stack or a |
---|
43 | cancellation. There do exist ways to handle this case, but currently there is |
---|
44 | no detection and the first unwind will simply be forgotten, often leaving |
---|
45 | it in a bad state. |
---|
46 | \item |
---|
47 | Finally, the exception system has not had a lot of programmer testing. |
---|
48 | More time with encouraged usage will reveal new |
---|
49 | quality of life upgrades that can be made. |
---|
50 | \end{itemize} |
---|
51 | |
---|
52 | \section{Complete Virtual System} |
---|
53 | The virtual system should be completed. It was not supposed to be part of this |
---|
54 | project, but was thrust upon it to do exception inheritance; hence, only |
---|
55 | minimal work is done. A draft for a complete virtual system is available but |
---|
56 | not finalized. A future \CFA project is to complete that work and then |
---|
57 | update the exception system that uses the current version. |
---|
58 | |
---|
59 | There are several improvements to the virtual system that would improve the |
---|
60 | exception traits. The most important one is an assertion to check one virtual |
---|
61 | type is a child of another. This check precisely captures many of the |
---|
62 | current ad-hoc correctness requirements. |
---|
63 | |
---|
64 | Other features of the virtual system could also remove some of the |
---|
65 | special cases around exception virtual tables, such as the generation |
---|
66 | of the @msg@ function. |
---|
67 | |
---|
68 | The full virtual system might also include other improvement like associated |
---|
69 | types to allow traits to refer to types not listed in their header. This |
---|
70 | feature allows exception traits to not refer to the virtual-table type |
---|
71 | explicitly, removing the need for the current interface macros, |
---|
72 | such as @EHM_IS_EXCEPTION@. |
---|
73 | |
---|
74 | \section{Additional Raises} |
---|
75 | Several other kinds of exception raises were considered beyond termination |
---|
76 | (@throw@), resumption (@throwResume@), and re-raise. |
---|
77 | |
---|
78 | The first is a non-local/concurrent raise providing asynchronous exceptions, |
---|
79 | \ie raising an exception on another stack. This semantics acts like signals |
---|
80 | allowing for out-of-band communication among coroutines and threads. This kind |
---|
81 | of raise is often restricted to resumption to allow the target stack to |
---|
82 | continue execution normally after the exception has been handled. That is, |
---|
83 | allowing one coroutine/thread to unwind the stack of another via termination is |
---|
84 | bad software engineering. |
---|
85 | |
---|
86 | Non-local/concurrent raise requires more |
---|
87 | coordination between the concurrency system |
---|
88 | and the exception system. Many of the interesting design decisions center |
---|
89 | around masking, \ie controlling which exceptions may be thrown at a stack. It |
---|
90 | would likely require more of the virtual system and would also effect how |
---|
91 | default handlers are set. |
---|
92 | |
---|
93 | Other raises were considered to mimic bidirectional algebraic effects. |
---|
94 | Algebraic effects are used in some functional languages allowing one function |
---|
95 | to have another function on the stack resolve an effect (which is defined with |
---|
96 | a functional-like interface). This semantics can be mimicked with resumptions |
---|
97 | and new raises were discussed to mimic bidirectional algebraic-effects, where |
---|
98 | control can go back and forth between the function-effect caller and handler |
---|
99 | while the effect is underway. |
---|
100 | % resume-top & resume-reply |
---|
101 | These raises would be like the resumption raise except using different search |
---|
102 | patterns to find the handler. |
---|
103 | |
---|
104 | \section{Checked Exceptions} |
---|
105 | Checked exceptions make exceptions part of a function's type by adding an |
---|
106 | exception signature. An exception signature must declare all checked |
---|
107 | exceptions that could propagate from the function, either because they were |
---|
108 | raised inside the function or came from a sub-function. This improves safety |
---|
109 | by making sure every checked exception is either handled or consciously |
---|
110 | passed on. |
---|
111 | |
---|
112 | Checked exceptions were never seriously considered for this project |
---|
113 | because they have significant trade-offs in usability and code reuse in |
---|
114 | exchange for the increased safety. |
---|
115 | These trade-offs are most problematic when trying to pass exceptions through |
---|
116 | higher-order functions from the functions the user passed into the |
---|
117 | higher-order function. There are no well known solutions to this problem |
---|
118 | that were satisfactory for \CFA (which carries some of C's |
---|
119 | flexibility-over-safety design) so additional research is needed. |
---|
120 | |
---|
121 | Follow-up work might add some form of checked exceptions to \CFA, |
---|
122 | possibly using polymorphic exception signatures, |
---|
123 | a form of tunneling\cite{Zhang19} or |
---|
124 | checked and unchecked raises. |
---|
125 | |
---|
126 | \section{Zero-Cost Try} |
---|
127 | \CFA does not have zero-cost try-statements because the compiler generates C |
---|
128 | code rather than assembler code (see \vpageref{p:zero-cost}). When the compiler |
---|
129 | does create its own assembly (or LLVM byte-code), then zero-cost try-statements |
---|
130 | are possible. The downside of zero-cost try-statements is the LSDA complexity, |
---|
131 | its size (program bloat), and the high cost of raising an exception. |
---|
132 | |
---|
133 | Alternatively, some research could be done into the simpler alternative method |
---|
134 | with a non-zero-cost try-statement but much lower cost exception raise. For |
---|
135 | example, programs are starting to use exception in the normal control path, so |
---|
136 | more exceptions are thrown. In these cases, the cost balance switches towards |
---|
137 | low-cost raise. Unfortunately, while exceptions remain exceptional, the |
---|
138 | libunwind model will probably remain the most effective option. |
---|
139 | |
---|
140 | Zero-cost resumptions is still an open problem. First, because libunwind does |
---|
141 | not support a successful-exiting stack-search without doing an unwind. |
---|
142 | Workarounds are possible but awkward. Ideally, an extension to libunwind could |
---|
143 | be made, but that would either require separate maintenance or gaining enough |
---|
144 | support to have it folded into the official library itself. |
---|
145 | |
---|
146 | Also, new techniques to skip previously searched parts of the stack need to be |
---|
147 | developed to handle the recursive resume problem and support advanced algebraic |
---|
148 | effects. |
---|
149 | |
---|
150 | \section{Signal Exceptions} |
---|
151 | Goodenough~\cite{Goodenough75} suggests three types of exceptions: escape, |
---|
152 | notify and signal. Escape are termination exceptions, notify are resumption |
---|
153 | exceptions, leaving signal unimplemented. |
---|
154 | |
---|
155 | A signal exception allows either behaviour, \ie after an exception is handled, |
---|
156 | the handler has the option of returning to the raise or after the @try@ |
---|
157 | statement. Currently, \CFA fixes the semantics of the handler return |
---|
158 | syntactically by the @catch@ or @catchResume@ clause. |
---|
159 | |
---|
160 | Signal exception should be reexamined and possibly be supported in \CFA. A very |
---|
161 | direct translation is to have a new raise and catch pair, and a new statement |
---|
162 | (or statements) would indicate if the handler returns to the raise or continues |
---|
163 | where it is; but there may be other options. |
---|
164 | |
---|
165 | For instance, resumption could be extended to cover this use by allowing local |
---|
166 | control flow out of it. This approach would require an unwind as part of the |
---|
167 | transition as there are stack frames that have to be removed between where |
---|
168 | the resumption handler is installed and where it is defined. |
---|
169 | This approach would not require, but might benefit from, a special statement |
---|
170 | to leave the handler. |
---|
171 | Currently, mimicking this behaviour in \CFA is possible by throwing a |
---|
172 | termination exception inside a resumption handler. |
---|
173 | |
---|
174 | % Maybe talk about the escape; and escape CONTROL_STMT; statements or how |
---|
175 | % if we could choose if _Unwind_Resume proceeded to the clean-up stage this |
---|
176 | % would be much easier to implement. |
---|