Download presentation
Presentation is loading. Please wait.
Published byGwen McKenzie Modified over 9 years ago
1
COMPILING EXCEPTIONS CORRECTLY Graham Hutton and Joel Wright University of Nottingham
2
1 What Is An Exception? zDivision by zero; zStack overflow; zNull pointer. Examples: An event within a computation that causes termination in a non-standard way.
3
2 This Talk zMost modern languages support programming with exceptions, e.g. using throw and catch; zThe compilation of such exception primitives is traditionally viewed as an advanced topic; zWe give a simple explanation and verification, using elementary functional techniques.
4
3 Step 1 - Arithmetic Expressions data Expr = Val Int | Add Expr Expr eval :: Expr Int eval (Val n) = n eval (Add x y) = eval x + eval y Syntax: Semantics:
5
4 type Stack = [Int] data Op = PUSH Int | ADD type Code = [Op] comp :: Expr Code comp (Val n) = [PUSH n] comp (Add x y) = comp x ++ comp y ++ [ADD] Virtual machine: Compiler:
6
5 Compiler Correctness ExprInt CodeStack eval exec [] comp [][] exec s (comp e) = eval e : s Theorem:
7
6 Proof: by induction, using a distribution lemma: However, we can avoid this lemma and shorten the proof by 60% by generalising the theorem: exec s (comp e ++ ops) = exec (eval e : s) ops exec s (xs ++ ys) = exec (exec s xs) ys
8
7 eval :: Expr Maybe Int eval (Val n) = Just n eval (Throw) = Nothing eval (Add x y) = eval x eval y eval (Catch x h) = eval x eval h Step 2 - Adding Exceptions data Expr = | Throw | Catch Expr Expr Syntax: Semantics:
9
8 Add (Val 1) (Val 2) Add Throw (Val 2) Catch (Val 1) (Val 2) Catch Throw (Val 2) Just 3 Nothing Just 1 Just 2 eval Examples:
10
9 data Op = | THROW | MARK Code | UNMARK type Stack = [Item] data Item = VAL Int | HAN Code comp (Throw) = [THROW] comp (Catch x h) = [MARK (comp h)] ++ comp x ++ [UNMARK] Virtual machine: Compiler:
11
10 How Is THROW Executed? Informally, we must: zUnwind the stack seeking a handler; zExecute the first handler found, if any; zSkip to the next part of the computation.
12
11 skip :: Code Code skip [] = [] skip (UNMARK : ops) = ops skip (MARK h : ops) = skip (skip ops) skip (op : ops) = skip ops unwind :: Stack Code Stack unwind [] ops = [] unwind (VAL n : s) ops = unwind s ops unwind (HAN h : s) ops = exec s (h ++ skip ops) Implementation:
13
12 1 + (catch (2 + throw) 3) Example StackCode
14
13 1 + (catch (2 + throw) 3) Example PUSH 1 MARK [PUSH 3] PUSH 2 THROW ADD UNMARK ADD StackCode
15
14 1 + (catch (2 + throw) 3) Example MARK [PUSH 3] PUSH 2 THROW ADD UNMARK ADD StackCode VAL 1
16
15 1 + (catch (2 + throw) 3) Example PUSH 2 THROW ADD UNMARK ADD StackCode HAN [PUSH 3] VAL 1
17
16 1 + (catch (2 + throw) 3) Example THROW ADD UNMARK ADD StackCode VAL 2 HAN [PUSH 3] VAL 1
18
17 1 + (catch (2 + throw) 3) Example THROW ADD UNMARK ADD StackCode HAN [PUSH 3] VAL 1
19
18 1 + (catch (2 + throw) 3) Example PUSH 3 THROW ADD UNMARK ADD StackCode VAL 1
20
19 1 + (catch (2 + throw) 3) Example THROW ADD UNMARK ADD StackCode VAL 3 VAL 1
21
20 1 + (catch (2 + throw) 3) Example ADD UNMARK ADD StackCode VAL 3 VAL 1
22
21 1 + (catch (2 + throw) 3) Example UNMARK ADD StackCode VAL 3 VAL 1
23
22 1 + (catch (2 + throw) 3) Example ADD StackCode VAL 3 VAL 1
24
23 1 + (catch (2 + throw) 3) Example StackCode VAL 4
25
24 Compiler Correctness ExprMaybe Int CodeStack eval exec [] compconv conv Nothing = [] conv (Just n) = [VAL n] where
26
25 As previously, we generalise to an arbitrary initial stack and arbitrary additional code. Theorem: exec s (comp e ++ ops) = exec s (trans (eval e) : ops) trans :: Maybe Int Op trans Nothing = THROW trans (Just n) = PUSH n where
27
26 Proof: by induction, using a skipping lemma: skip (comp e ++ ops) = skip ops Notes: zThe proof is 3.5 pages of simple calculation; zThe quickcheck tool was very useful as an aid to simplifying the definitions and results.
28
27 Step 3 - Adding Jumps MARK a code for x UNMARK JUMP b a: code for h b: remaining code Catch x h is now compiled to Basic idea: See the paper for further details.
29
28 Summary zExplanation and verification of the compilation of exceptions using stack unwinding. zStepwise development to aid understanding: 1 - Arithmetic expressions; 2 - Adding exceptions; 3 - Adding jumps.
30
29 Further Work zMechanical verification; zModular compilers; zCalculating the compiler; zGeneralising the language; zCompiler optimisations.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.